ghammad / pyActigraphy

Python-based open source package for actigraphy data analysis

Home Page:https://ghammad.github.io/pyActigraphy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get the L5 and M10 start times?

amondino opened this issue · comments

Hi! I can see in the paper that you got the L5 and M10 start times, but I only see in the tutorials how to get the mean activity but not their start time.
Is it possible to get this?
Thanks!
Ale

Hi @ghammad,

I also wanted to ask this question. The code can already calculate the average activity in the L5/M10 periods, would it be possible to identify the time corresponding to the first activity data point included in the L5/M10? Although I appreciate this may be easier said than done.

Thanks,

Rory

Hello @amondino @roryos1

Sorry for the late reply. Feedback and requests are always appreciated.

Indeed, the code is there and actually the L5/M10 times are calculated. I just did not think about exposing these variables via a convenient function. I'll make one asap but for the time being, here is a simple workaround:

import pyActigraphy
from pyActigraphy.metrics.metrics import _lmx

import os
fpath = os.path.join(os.path.dirname(pyActigraphy.__file__),'tests/data/')
raw = pyActigraphy.io.read_raw_awd(fpath+'example_01.AWD')

# The _lmx function returns the start time of the LX or MX period (depending on the value of the "lowest" switch variable)
# as well as the mean activity during this period.
l5_ts, l5 = _lmx(raw.resampled_data('15min'), '5h', lowest=True)
m10_ts, m10 = _lmx(raw.resampled_data('15min'), '10h', lowest=False)

Be aware that 'l5_ts' or 'm10_ts' are of type 'pd.Timedelta'. It is easy to use them for further computation (such as average or differences). However, if you want to manipulate them as strings, you can the following function:

from pyActigraphy.sleep.scoring_base import _td_format

l5_ts_as_str = _td_format(l5_ts)
print(l5_ts_as_str)

Hope that helps.

Cheers,

Greg

Hi @ghammad,

Thank you so much for this response, it's been a great help for my PhD.

I understand that the _lmx function retrieves the start times of the LX and MX period, however I was wondering whether there was a function that could return the start times for L5p and M10p?

Best wishes,
Rory

Hello @roryos1

Unfortunately, no. But that would be a nice addition.

For the time being, I suggest to use a plain python trick. Group your data by the required amount of time (ex: '3 days') and then apply the _lmx function to each chunk. This can easily done:

raw.data.groupby(pd.Grouper(freq='3D')).apply(_lmx, period='5h', lowest=True)

To be tested of course. But that should do the trick.

Hope that helps.

Grégory

Hi @ghammad,

Thank you very much for taking the time to respond.

I've tried to use the trick you provided, adjusted for daily M10 onset values:

raw.data.groupby(pd.Grouper(freq='1D')).apply(_lmx, period='10h', lowest=False)

Like you said, this is subject to testing. I keep being presented with the following error:

ValueError: Length mismatch: Expected axis has 2 elements, new values have 5760 elements

The code works fine when grouping the data by 2 days or 3 days, just not for 1 day. I have done nothing with the data besides import it. There may be a very simple answer to this that I am overlooking...

If you have any insights into what is going on, it would be massively appreciated.

Best wishes,
Rory

Hello @roryos1

Without having access to the data or a way to reproduce the pb, difficult for me to make a precise diagnosis but I suspect the following:

  • L5/M10 needs an average daily profile, meaning that a profile is made for each time period. Therefore, a daily L5/M10 needs a full day of data. If, for any reason, masking or NaN activity counts, there is less than a day of data, I guess the code will produce the error you encountered. To be checked but very likely.

So, my advice is to use more than a day and reflect on the variability of a L5/M10 variable calculated on a short time range.

Side remark: having a clear error message when there is less than 24h of data would be nice.

Hope that helps.

Greg