How to read data from DataFrames with pyActigraphy

Imported packages and input data

The usual suspects:

[1]:
import numpy as np
[2]:
import pandas as pd
[3]:
import pyActigraphy

In this example, let’s generate some input data:

NB: if you already have your data under a pandas.DataFrame format, jump directly to the next section.

[4]:
N = 1440*7 # 7 days of acquisition at a frequency of 60s.
[5]:
activity = np.random.normal(10,1,N)
light = np.random.normal(100,10,N)
[6]:
non_wear = np.empty(N)
[7]:
# Set up a segment of  spurious inactivity
activity[2060:2160] = 0.0
non_wear[2060:2160] = 1.0
[8]:
d = {'Activity': activity, 'Light': light, 'Non-wear': non_wear}
[9]:
index = pd.date_range(start='01-01-2020',freq='60s',periods=N)
[10]:
data = pd.DataFrame(index=index,data=d)
[11]:
data
[11]:
Activity Light Non-wear
2020-01-01 00:00:00 8.550218 104.999953 0.000000e+00
2020-01-01 00:01:00 10.923393 95.088518 2.194245e-314
2020-01-01 00:02:00 8.144349 103.872427 4.052524e-319
2020-01-01 00:03:00 9.285714 113.352058 NaN
2020-01-01 00:04:00 10.159414 116.400666 8.331307e-316
... ... ... ...
2020-01-07 23:55:00 9.421655 102.225861 2.072129e-309
2020-01-07 23:56:00 10.571214 97.894970 7.900019e+305
2020-01-07 23:57:00 8.522185 126.530327 8.157913e-312
2020-01-07 23:58:00 10.488261 101.407490 5.367234e-303
2020-01-07 23:59:00 11.041405 100.372527 1.333605e+241

10080 rows × 3 columns

Manual creation of a BaseRaw object

[12]:
from pyActigraphy.io import BaseRaw

help(BaseRaw)

Set activity and light data (if available)

[13]:
raw = BaseRaw(
    name="myName",
    uuid='DeviceId',
    format='Pandas',
    axial_mode=None,
    start_time=data.index[0],
    period=(data.index[-1]-data.index[0]),
    frequency=data.index.freq,
    data=data['Activity'],
    light=data['Light']
)
[14]:
raw.data
[14]:
2020-01-01 00:00:00     8.550218
2020-01-01 00:01:00    10.923393
2020-01-01 00:02:00     8.144349
2020-01-01 00:03:00     9.285714
2020-01-01 00:04:00    10.159414
                         ...
2020-01-07 23:55:00     9.421655
2020-01-07 23:56:00    10.571214
2020-01-07 23:57:00     8.522185
2020-01-07 23:58:00    10.488261
2020-01-07 23:59:00    11.041405
Freq: 60S, Name: Activity, Length: 10080, dtype: float64

Set up a mask

Most devices that have a wear sensor return this information as a binary time series with “1” when the device is most likely not worn and “0” otherwise. In pyActigraphy, this information can be used to create a mask and thus invalidate the corresponding data points (set to “0” most probably). However, the mask, the value “1” correspond to “no masking”. So, depending on your “non-wear” data, be careful to transform them appropriately:

[15]:
# Here, I assume that 0: the device is worn, 1: device not worn.
# As mentioned aboce, for the mask, 1: no masking. (NB: it is a convolution: data*mask)
raw.mask = np.abs(data['Non-wear']-1)

Tests

[16]:
raw.duration()
[16]:
<604800 * Seconds>
[17]:
raw.length()
[17]:
10080
[18]:
raw.ADAT(binarize=False)
[18]:
14232.551520242358
[19]:
raw.IV(binarize=False)
[19]:
1.0878760497043665
[20]:
# If you want to mask the data
raw.mask_inactivity = True
[21]:
# For a gaussian noise, IV should be close to 2.
raw.IV(binarize=False)
[21]:
2.0276331847662488

The masking seems to work!

Et voilà!