Example #1
0
def _handle_ts_or_scalar(op_name, ts1, ts2, all = True, fill = None, name = None):
    '''
    this is the main entry point for any arithmetic type function performed on a timeseries
    and/or a scalar. 
    op_name - name of the function to be performed
    ts1, ts2 - timeseries or scalars that the function is to performed over
    all - whether all dates should be included in the result
    fill - the value that should be used to represent "missing values"
    name - the name of the resulting time series
    '''
    from dynts import istimeseries
    op = _get_op(op_name)
    fill = fill if fill is not None else settings.missing_value
    if hasattr(fill,'__call__'):
        fill_fn = fill
    else:
        fill_fn = lambda : fill

    name = name or '%s(%s,%s)' % (op_name,ts1,ts2)
    ts = None
    if istimeseries(ts1):
        ts = ts1
        if istimeseries(ts2):
            dts, data =  _handle_ts_ts(op_name, op, ts1, ts2, all, fill_fn)
            
        else:
            dts, data = _handle_ts_scalar(op_name, op, ts1, ts2, fill_fn)
    else:
        if istimeseries(ts2):
            ts = ts2
            dts, data = _handle_scalar_ts(op_name, op, ts1, ts2, fill_fn)
        else:
            return op(ts1,ts2)
        
    return ts.clone(date = dts, data = data, name = name)
Example #2
0
 def merge(self, tserie, fill=nan, **kwargs):
     if dynts.istimeseries(tserie):
         tserie = [tserie]
     else:
         tserie = tuple(tserie)
     alldates = set(self.dates())
     hash = self.ashash()
     namespace = self.namespace
     with_namespace = False
     for ts in tserie:
         if ts.namespace != namespace:
             with_namespace = True
             break
     thashes  = [(hash, np.array([fill]*self.count()))]
     names = self.names(with_namespace)
     for ts in tserie:
         alldates = alldates.union(ts.dates())
         names.extend(ts.names(with_namespace))
         thashes.append((ts.ashash(),np.array([fill]*ts.count())))
     hash.names = names
     stack = np.hstack
     mdt = lambda dt: stack((h.get(dt,ln) for h,ln in thashes))
     for dt in alldates:
         hash[dt] = mdt(dt)
     return hash.getts()
Example #3
0
 def merge(self, ts, all = True):
     if dynts.istimeseries(ts):
         return self._mergesingle(ts, all = all)
     else:
         rs = self
         for t in ts:
             rs = rs._mergesingle(t, all = all)
         return rs
Example #4
0
 def testNoParameters(self):
     '''Test mean function with zero parameters'''
     expression = '%s(GOOG)' % self.function
     result = dynts.evaluate(expression)
     self.assertEqual(str(result),expression)
     self.assertEqual(len(result.data),1)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(),1)
Example #5
0
 def testTwoSeries(self):
     e = '%s(GOOG,YHOO)' % self.function
     result = dynts.evaluate(e)
     symbols = result.expression.symbols()
     self.assertEqual(len(symbols), 2)
     self.assertEqual(len(result.data), 2)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(), 2)
Example #6
0
 def testNoParameters(self):
     '''Test mean function with zero parameters'''
     expression = '%s(GOOG)' % self.function
     result = dynts.evaluate(expression)
     self.assertEqual(str(result), expression)
     self.assertEqual(len(result.data), 1)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(), 1)
Example #7
0
 def testTwoSeries(self):
     e = '%s(GOOG, YHOO)' % self.function
     result = dynts.evaluate(e)
     symbols = result.expression.symbols()
     self.assertEqual(len(symbols), 2)
     self.assertEqual(len(result.data), 2)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(), 2)
Example #8
0
 def testWindowParameter(self):
     '''Test mean function with one parameter'''
     e = '%(f)s(GOOG,window=30),%(f)s(GOOG,window=60)' % {'f':self.function}
     result = dynts.evaluate(e)
     symbols = result.expression.symbols()
     self.assertEqual(len(symbols),1)
     self.assertEqual(len(result.data),1)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(),2)
Example #9
0
 def testDataProvider(self):
     result = dynts.evaluate('2*GOOG, GOOG')
     self.assertEqual(len(result.data), 1)
     self.assertEqual(result.expression, dynts.parse('2*GOOG, GOOG'))
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(), 2)
     ts1 = data.serie(0)
     ts2 = data.serie(1)
     for v1, v2 in zip(ts1, ts2):
         self.assertAlmostEqual(v1, 2. * v2)
Example #10
0
 def testDataProvider(self):
     result = dynts.evaluate('2*GOOG,GOOG')
     self.assertEqual(len(result.data),1)
     self.assertEqual(result.expression,dynts.parse('2*GOOG,GOOG'))
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(),2)
     ts1 = data.serie(0)
     ts2 = data.serie(1)
     for v1,v2 in zip(ts1,ts2):
         self.assertAlmostEqual(v1,2.*v2)
Example #11
0
 def testBinOpSerieFunction(self):
     '''Get a timeseries and a function and check for consistency'''
     res = dynts.parse('goog:yahoo-ma(goog:yahoo, window=30)')
     self.assertEqual(res.symbols(), ['GOOG:YAHOO'])
     self.assertEqual(len(res), 2)
     self.assertEqual(str(res[0]), 'GOOG:YAHOO')
     self.assertEqual(str(res[1]), 'ma(GOOG:YAHOO, window=30)')
     result = dynts.evaluate(res)
     self.assertEqual(len(result.data), 1)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(), 1)
Example #12
0
 def testBinOpSerieFunction(self):
     '''Get a timeseries and a function and check for consistency'''
     res = dynts.parse('goog:yahoo-ma(goog:yahoo,window=30)')
     self.assertEqual(res.symbols(),['GOOG:YAHOO'])
     self.assertEqual(len(res),2)
     self.assertEqual(str(res[0]),'GOOG:YAHOO')
     self.assertEqual(str(res[1]),'ma(GOOG:YAHOO,window=30)')
     result = dynts.evaluate(res)
     self.assertEqual(len(result.data),1)
     data = result.ts()
     self.assertTrue(dynts.istimeseries(data))
     self.assertEqual(data.count(),1)
Example #13
0
 def testWindowParameter(self):
     '''Test mean function with one parameter'''
     e = '%(f)s(GOOG,window=30),%(f)s(GOOG,window=60)' % {'f':self.function}
     result = dynts.evaluate(e)
     symbols = result.expression.symbols()
     self.assertEqual(len(symbols),1)
     self.assertEqual(len(result.data),1)
     ts = result.ts()
     self.assertTrue(dynts.istimeseries(ts))
     self.assertEqual(ts.count(),2)
     back = ts[-1]
     self.assertEqual(len(back),2)
     self.assertEqual(back[0],back[0])
     self.assertEqual(back[-1],back[-1])
Example #14
0
 def merge(self, tserie, fill = float("nan"), **kwargs):
     if dynts.istimeseries(tserie):
         tserie = [tserie]
     alldates = set(self.dates())
     hash     = self.ashash()
     thashes  = [(hash,np.array([fill]*self.count()))]
     for ts in tserie:
         alldates = alldates.union(ts.dates())
         hash.names.extend(ts.names())
         thashes.append((ts.ashash(),np.array([fill]*ts.count())))
     stack = np.hstack
     mdt = lambda dt: stack((h.get(dt,ln) for h,ln in thashes))
     for dt in alldates:
         hash[dt] = mdt(dt)
     return hash.getts()
Example #15
0
 def _unwind(self, values, backend, **kwargs):
     sdata = values[self.value]
     if istimeseries(sdata):
         return sdata
     else:
         ts = timeseries(name=str(self),
                         date=sdata['date'],
                         data=sdata['value'],
                         backend=backend)
         # Uses this hack to make sure timeseries are ordered
         # Lots of room for performance improvement
         hash = ts.ashash()
         hash.modified = True
         values[ts.name] = hash.getts()
         return ts
Example #16
0
 def merge(self, tserie, fill=float("nan"), **kwargs):
     if dynts.istimeseries(tserie):
         tserie = [tserie]
     alldates = set(self.dates())
     hash = self.ashash()
     thashes = [(hash, np.array([fill] * self.count()))]
     for ts in tserie:
         alldates = alldates.union(ts.dates())
         hash.names.extend(ts.names())
         thashes.append((ts.ashash(), np.array([fill] * ts.count())))
     stack = np.hstack
     mdt = lambda dt: stack((h.get(dt, ln) for h, ln in thashes))
     for dt in alldates:
         hash[dt] = mdt(dt)
     return hash.getts()
Example #17
0
 def testWindowParameter(self):
     '''Test mean function with one parameter'''
     e = ('%(f)s(GOOG, window=30), %(f)s(GOOG, window=60)' % {
         'f': self.function
     })
     result = dynts.evaluate(e)
     symbols = result.expression.symbols()
     self.assertEqual(len(symbols), 1)
     self.assertEqual(len(result.data), 1)
     ts = result.ts()
     self.assertTrue(dynts.istimeseries(ts))
     self.assertEqual(ts.count(), 2)
     back = ts[-1]
     self.assertEqual(len(back), 2)
     self.assertEqual(back[0], back[0])
     self.assertEqual(back[-1], back[-1])
Example #18
0
 def _unwind(self, values, backend, **kwargs):
     sdata = values[self.value]
     if istimeseries(sdata):
         return sdata
     else:
         ts = timeseries(name = str(self),
                         date = sdata['date'],
                         data = sdata['value'],
                         backend = backend)
         # Uses this hack to make sure timeseries are ordered
         # Lots of room for performance improvement
         hash = ts.ashash()
         hash.modified = True
         ts = hash.getts()
         values[ts.name] = ts
         return ts