How to read raw files by batch

Visual inspections of individual files are usually useful to detect problems that occurred during the acquisition. However, at the analysis level, it is convenient to read files, and calculate the variables of interest, by batch.

In pyActigraphy, there is a dedicated class to read files by batch: RawReader. Objects from that class can easily be instanciated using the read_raw function.

Let’s see now how to use it.

Define imported packages and file path

[1]:
import pyActigraphy
[2]:
import os
[3]:
# retrieve path to example files
fpath = os.path.join(os.path.dirname(pyActigraphy.__file__),'tests/data/')
[4]:
import plotly.graph_objs as go

Read files by batch

As raw file extensions (such as .csv or .txt) do not allow to infer the type of device used to acquire the data, it is necessary to specify the “reader type” upon reading the files:

[5]:
# Read test files
raw = pyActigraphy.io.read_raw(fpath+'example_0*.AWD', reader_type='AWD')

In the example above, we use a wildcard “*“; it substitutes any character and it is a convenient way to specify multiple files at once. Any file whose name contains”example_0”, with a “.AWD” extension will be read. More info on wildcards: Wikipedia

The raw object that has been instanciated contains a list of objects that one would have otherwise had to create by hand using individual file functions such as read_raw_awd.

[6]:
# Check how many files have been read
len(raw.readers)
[6]:
6

Indeed, there are 6 example files of type ‘.AWD’ in the test data directory of the pyActigraphy:

[7]:
raw.names()
[7]:
['example_04',
 'example_05',
 'example_02',
 'example_03',
 'example_01',
 'example_01_mask']

Analyse files by batch

There are two ways to analyse files that have been read by batch:

  • the needed function already exists at the RawReader class level

  • the needed function does not exist and one needs to loop over the files

Direct analysis

For convenience, a certain number of functions, available at the individual reader level, have been extended to the RawReader class.

For example:

[8]:
# Check the duration of the recording
raw.duration()
[8]:
{'example_04': Timedelta('21 days 17:39:00'),
 'example_05': Timedelta('15 days 01:43:00'),
 'example_02': Timedelta('12 days 18:53:00'),
 'example_03': Timedelta('14 days 21:36:00'),
 'example_01': Timedelta('12 days 18:41:00'),
 'example_01_mask': Timedelta('12 days 18:41:00')}

These functions return a dictionary whose keys are the individual file names and values are the required outputs (here, the duration of the individual recordings).

Most rest-activity rhythm related variables are available:

[9]:
raw.IS()
[9]:
{'example_04': 0.2551624866267735,
 'example_05': 0.6595438549841431,
 'example_02': 0.5237758757150337,
 'example_03': 0.3720781873681576,
 'example_01': 0.527656245354158,
 'example_01_mask': 0.44565929945478583}
[10]:
raw.kAR(0)
[10]:
{'example_04': 0.03815016462875074,
 'example_05': 0.030282109908435897,
 'example_02': 0.026240794466679603,
 'example_03': 0.023099082226598566,
 'example_01': 0.033762534539447164,
 'example_01_mask': 0.036345931354120085}

Manual analysis

As already mentioned, the “raw” object is a container. It is possible to simply loop over the objects contained in the “raw” object:

[11]:
for iread in raw.readers:
    print("Object type: {}. Name: {}. Duration of the recording: {}. Number of acquisition points: {}".format(type(iread),iread.name,iread.duration(),len(iread.data)))
Object type: <class 'pyActigraphy.io.awd.awd.RawAWD'>. Name: example_04. Duration of the recording: 21 days 17:39:00. Number of acquisition points: 31299
Object type: <class 'pyActigraphy.io.awd.awd.RawAWD'>. Name: example_05. Duration of the recording: 15 days 01:43:00. Number of acquisition points: 21703
Object type: <class 'pyActigraphy.io.awd.awd.RawAWD'>. Name: example_02. Duration of the recording: 12 days 18:53:00. Number of acquisition points: 18413
Object type: <class 'pyActigraphy.io.awd.awd.RawAWD'>. Name: example_03. Duration of the recording: 14 days 21:36:00. Number of acquisition points: 21456
Object type: <class 'pyActigraphy.io.awd.awd.RawAWD'>. Name: example_01. Duration of the recording: 12 days 18:41:00. Number of acquisition points: 18401
Object type: <class 'pyActigraphy.io.awd.awd.RawAWD'>. Name: example_01_mask. Duration of the recording: 12 days 18:41:00. Number of acquisition points: 18401

So, for a more advanced analysis of the actigraphy recordings, simply replace the “print” statement by our analysis code.

Et voilà! Easy, isn’t it?