In [44]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys, os, os.path

from rotation.timeseries import TimeSeries
aigrain_root = '/Users/tdm/aigrain_rotation'
aigrain_root = os.getenv('AIGRAIN_ROTATION', 
                        os.path.expanduser('~/aigrain_rotation'))

import logging
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.INFO)

In [53]:
partable = pd.read_table('{}/par/final_table.txt'.format(aigrain_root),
                        delim_whitespace=True)

In [60]:
def true_P(i):
    return partable.ix[i, ['PEQ', 'P_MIN', 'P_MAX']]

In [61]:
true_P(9)


Out[61]:
PEQ      31.490000
P_MIN    32.425915
P_MAX    35.918658
Name: 9, dtype: float64

In [62]:
i = 9
datafile = '{}/noise_free/lightcurve_{:04.0f}.txt'.format(aigrain_root,i)
t,f = np.loadtxt(datafile, unpack=True)

s = TimeSeries(t,f, default_maxlag_days=400, flatten_order=4)
try:
    s.plot_acorr(mark_period=False)
except:
    logging.error('Acorr breaks.', exc_info=True)
plt.figure()
plt.plot(s.t,s.f);



In [16]:
s.make_chunks(7)

In [17]:
s.save_hdf('test1.h5')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-73ac0112f610> in <module>()
----> 1 s.save_hdf('test1.h5')

/Users/tdm/anaconda/lib/python2.7/site-packages/rotation-0.1-py2.7.egg/rotation/timeseries.pyc in save_hdf(self, filename, path)
    224         pks, lphs = self.acorr_peaks()
    225         acorr_peaks = pd.DataFrame({'lag':pks,
--> 226                                     'lph':lphs})
    227 
    228         pers,pg = self.periodogram()

/Users/tdm/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in __init__(self, data, index, columns, dtype, copy)
    205                                  dtype=dtype, copy=copy)
    206         elif isinstance(data, dict):
--> 207             mgr = self._init_dict(data, index, columns, dtype=dtype)
    208         elif isinstance(data, ma.MaskedArray):
    209             import numpy.ma.mrecords as mrecords

/Users/tdm/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in _init_dict(self, data, index, columns, dtype)
    329 
    330         return _arrays_to_mgr(arrays, data_names, index, columns,
--> 331                               dtype=dtype)
    332 
    333     def _init_ndarray(self, values, index, columns, dtype=None,

/Users/tdm/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
   4639     # figure out the index, if necessary
   4640     if index is None:
-> 4641         index = extract_index(arrays)
   4642     else:
   4643         index = _ensure_index(index)

/Users/tdm/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in extract_index(data)
   4687             lengths = list(set(raw_lengths))
   4688             if len(lengths) > 1:
-> 4689                 raise ValueError('arrays must all be same length')
   4690 
   4691             if have_dicts:

ValueError: arrays must all be same length

In [6]:
for sub in [s.subseries['sub{}'.format(i+1)] for i in range(len(s.subseries))]:
    sub.plot_acorr()


DEBUG:root:maxes: [[  2.45000000e+01   9.81730455e-03]
 [  8.37083333e+01   3.53790956e-03]] (shape=(2, 2))
DEBUG:root:mins: [[  5.57375000e+01  -8.45244037e-03]
 [  9.39166667e+01  -1.29806584e-03]] (shape=(2, 2))
DEBUG:root:first extremum is a max; removing
DEBUG:root:now, maxes: [[  8.37083333e+01   3.53790956e-03]]
DEBUG:root:now, mins: [[  5.57375000e+01  -8.45244037e-03]
 [  9.39166667e+01  -1.29806584e-03]]
DEBUG:root:lphs: [ 0.00841316]
DEBUG:root:maxes: [[  2.53166667e+01   1.46652028e-02]
 [  4.13437500e+01   3.29450799e-03]
 [  5.59416667e+01   4.56064177e-03]
 [  6.89062500e+01  -3.60411292e-03]
 [  9.37125000e+01   1.19135538e-02]] (shape=(5, 2))
DEBUG:root:mins: [[  1.04125000e+01  -1.41097733e-02]
 [  3.46062500e+01  -3.14536301e-04]
 [  4.83875000e+01  -3.48383336e-04]
 [  6.64562500e+01  -3.81744704e-03]
 [  7.95229167e+01  -1.56693234e-02]] (shape=(5, 2))
DEBUG:root:last extremum is a max; removing
DEBUG:root:now, maxes: [[  2.53166667e+01   1.46652028e-02]
 [  4.13437500e+01   3.29450799e-03]
 [  5.59416667e+01   4.56064177e-03]
 [  6.89062500e+01  -3.60411292e-03]]
DEBUG:root:now, mins: [[  1.04125000e+01  -1.41097733e-02]
 [  3.46062500e+01  -3.14536301e-04]
 [  4.83875000e+01  -3.48383336e-04]
 [  6.64562500e+01  -3.81744704e-03]
 [  7.95229167e+01  -1.56693234e-02]]
DEBUG:root:lphs: [ 0.02187736  0.00362597  0.00664356  0.00613927]
DEBUG:root:maxes: [[  5.48187500e+01   2.48311356e-03]
 [  7.54395833e+01   1.06091428e-02]] (shape=(2, 2))
DEBUG:root:mins: [[  3.02166667e+01  -1.18989452e-02]
 [  5.67583333e+01   2.45529669e-03]] (shape=(2, 2))
DEBUG:root:last extremum is a max; removing
DEBUG:root:now, maxes: [[  5.48187500e+01   2.48311356e-03]]
DEBUG:root:now, mins: [[  3.02166667e+01  -1.18989452e-02]
 [  5.67583333e+01   2.45529669e-03]]
DEBUG:root:lphs: [ 0.00720494]
DEBUG:root:maxes: [[  2.05187500e+01   3.11573764e-03]
 [  4.78770833e+01  -2.12610707e-03]
 [  7.43166667e+01   6.53065944e-03]] (shape=(3, 2))
DEBUG:root:mins: [[  4.05270833e+01  -2.80894919e-03]
 [  5.50229167e+01  -2.53501112e-03]] (shape=(2, 2))
DEBUG:root:first extremum is a max; removing
DEBUG:root:now, maxes: [[  4.78770833e+01  -2.12610707e-03]
 [  7.43166667e+01   6.53065944e-03]]
DEBUG:root:now, mins: [[  4.05270833e+01  -2.80894919e-03]
 [  5.50229167e+01  -2.53501112e-03]]
DEBUG:root:last extremum is a max; removing
DEBUG:root:now, maxes: [[  4.78770833e+01  -2.12610707e-03]]
DEBUG:root:now, mins: [[  4.05270833e+01  -2.80894919e-03]
 [  5.50229167e+01  -2.53501112e-03]]
DEBUG:root:lphs: [ 0.00054587]
DEBUG:root:maxes: [[  2.90937500e+01   8.05805706e-03]] (shape=(1, 2))
DEBUG:root:mins: [[  5.61458333e+00  -5.67043117e-03]
 [  7.65625000e+01  -6.56120497e-03]] (shape=(2, 2))
DEBUG:root:lphs: [ 0.01417388]
DEBUG:root:maxes: [[  3.16458333e+00  -2.69572976e-03]
 [  2.74604167e+01   1.11090869e-02]
 [  5.65541667e+01   2.56769152e-03]
 [  8.14625000e+01   3.88804636e-03]] (shape=(4, 2))
DEBUG:root:mins: [[  1.09229167e+01  -4.39900792e-03]
 [  4.41000000e+01  -6.52687455e-03]
 [  7.02333333e+01  -7.77857604e-03]
 [  9.34062500e+01   3.60041060e-04]] (shape=(4, 2))
DEBUG:root:first extremum is a max; removing
DEBUG:root:now, maxes: [[  2.74604167e+01   1.11090869e-02]
 [  5.65541667e+01   2.56769152e-03]
 [  8.14625000e+01   3.88804636e-03]]
DEBUG:root:now, mins: [[  1.09229167e+01  -4.39900792e-03]
 [  4.41000000e+01  -6.52687455e-03]
 [  7.02333333e+01  -7.77857604e-03]
 [  9.34062500e+01   3.60041060e-04]]
DEBUG:root:lphs: [ 0.01657203  0.00972042  0.00759731]
DEBUG:root:maxes: [[  3.26666667e+00  -1.94901310e-02]
 [  2.81750000e+01   4.79787808e-02]
 [  5.16541667e+01   8.07929192e-03]] (shape=(3, 2))
DEBUG:root:mins: [[  1.01062500e+01  -2.40518178e-02]
 [  4.55291667e+01   4.64341214e-03]
 [  7.22750000e+01  -4.72034963e-02]] (shape=(3, 2))
DEBUG:root:first extremum is a max; removing
DEBUG:root:now, maxes: [[  2.81750000e+01   4.79787808e-02]
 [  5.16541667e+01   8.07929192e-03]]
DEBUG:root:now, mins: [[  1.01062500e+01  -2.40518178e-02]
 [  4.55291667e+01   4.64341214e-03]
 [  7.22750000e+01  -4.72034963e-02]]
DEBUG:root:lphs: [ 0.05768298  0.02935933]
DEBUG:root:maxes: [[  4.03229167e+01   2.41046341e-02]
 [  7.27854167e+01   1.76577753e-02]] (shape=(2, 2))
DEBUG:root:mins: [[  2.21520833e+01  -5.46065522e-02]
 [  6.08416667e+01  -7.76668511e-03]
 [  9.46312500e+01  -1.90249697e-02]] (shape=(3, 2))
DEBUG:root:lphs: [ 0.05529125  0.0310536 ]
DEBUG:root:maxes: [[  3.40958333e+01   2.85857022e-02]
 [  7.20708333e+01   4.31631519e-03]] (shape=(2, 2))
DEBUG:root:mins: [[  1.66395833e+01  -1.50688898e-02]
 [  5.35937500e+01  -1.72569182e-02]
 [  8.96291667e+01  -5.51332347e-03]] (shape=(3, 2))
DEBUG:root:lphs: [ 0.04474861  0.01570144]
DEBUG:root:maxes: [[  3.23604167e+01   5.76044978e-02]
 [  6.18625000e+01  -4.17658399e-03]] (shape=(2, 2))
DEBUG:root:mins: [[  5.47166667e+01  -9.81828730e-03]
 [  7.71750000e+01  -6.63320989e-02]] (shape=(2, 2))
DEBUG:root:first extremum is a max; removing
DEBUG:root:now, maxes: [[  6.18625000e+01  -4.17658399e-03]]
DEBUG:root:now, mins: [[  5.47166667e+01  -9.81828730e-03]
 [  7.71750000e+01  -6.63320989e-02]]
DEBUG:root:lphs: [ 0.03389861]
DEBUG:root:maxes: [[ 28.99166667   0.48107215]
 [ 60.6375       0.23051979]
 [ 90.24166667   0.87019734]] (shape=(3, 2))
DEBUG:root:mins: [[  8.47291667  -0.26654283]
 [ 50.12291667  -0.12736841]
 [ 76.35833333  -0.84983077]] (shape=(3, 2))
DEBUG:root:last extremum is a max; removing
DEBUG:root:now, maxes: [[ 28.99166667   0.48107215]
 [ 60.6375       0.23051979]]
DEBUG:root:now, mins: [[  8.47291667  -0.26654283]
 [ 50.12291667  -0.12736841]
 [ 76.35833333  -0.84983077]]
DEBUG:root:lphs: [ 0.67802778  0.71911938]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-a707ef220eba> in <module>()
      1 for sub in [s.subseries['sub{}'.format(i+1)] for i in range(len(s.subseries))]:
----> 2     sub.plot_acorr()

/Users/tdm/anaconda/lib/python2.7/site-packages/rotation-0.1-py2.7.egg/rotation/timeseries.pyc in plot_acorr(self, days, smooth, maxlag, mark_period, lookahead, fit_npeaks, tol, **kwargs)
     94                    **kwargs):
     95 
---> 96         lag, ac = self.acorr(days=days, smooth=smooth, maxlag=maxlag)
     97 
     98         plt.plot(lag, ac, **kwargs)

/Users/tdm/anaconda/lib/python2.7/site-packages/rotation-0.1-py2.7.egg/rotation/timeseries.pyc in acorr(self, maxlag, smooth, days, recalc)
     61             #logging.debug('{} nans in x'.format((np.isnan(x)).sum()))
     62 
---> 63             ac = acor.function(x, maxlag)
     64             lag = np.arange(maxlag)
     65 

/Users/tdm/anaconda/lib/python2.7/site-packages/acor-1.1.1-py2.7-macosx-10.5-x86_64.egg/acor/acor.py in function(data, maxt)
     55     if maxt is None:
     56         maxt = len(data)
---> 57     result = np.zeros(maxt, dtype=float)
     58     _acor.function(np.array(data, dtype=float), result)
     59     return result / result[0]

ValueError: cannot convert float NaN to integer

In [99]:
def fake_data(period=30, cadence=0.02,
             poly_coeffs=(5e-6,3e-5,0)):
    ts = np.arange(200,1300,cadence)
    fs = np.sin(2*np.pi*ts/(period))
    fs += np.polyval(poly_coeffs,ts)
    fs -= fs.mean()
    return ts, fs

In [100]:
plt.plot(*fake_data(200))


Out[100]:
[<matplotlib.lines.Line2D at 0x1131d1290>]

In [103]:
from numpy.fft import fft, ifft, fftfreq
t,f = fake_data()
ft = fft(f)
plt.plot(ft**2)
len(ft)


Out[103]:
55000

In [104]:
fftfreq?

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [109]:
x = 7
print 'this is the value of x: %.3f' % x
print 'this is also x: {:.3f}'.format(x)


this is the value of x: 7.000
this is also x: 7.000

In [114]:
from numpy.fft import fft, fftfreq, fftshift
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = fftfreq(N, T)
xf = fftshift(xf)
yplot = fftshift(yf)
#xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.plot(xf, 1.0/N * np.abs(yplot))
#plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()



In [ ]: