Skip to content

Commit 5c536da

Browse files
committed
docs: add comprehensive module and function docstrings
- Added module-level docstrings to all Python files explaining purpose, main functions, and usage - Significantly expanded read_device() and process() docstrings with: - Detailed parameter descriptions with types and defaults - Multiple usage examples covering common scenarios - Return value documentation with DataFrame and info dict structure - "See Also" and "Notes" sections for cross-references - References to GLOSSARY.md for info dict field descriptions - Improved consistency and completeness across all documentation
1 parent e72fbc0 commit 5c536da

5 files changed

Lines changed: 335 additions & 53 deletions

File tree

src/actipy/__init__.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
"""
2+
Actipy: A Python package for processing accelerometer data.
3+
4+
This package provides tools to read and process data from wearable accelerometer
5+
devices including Axivity3/6 (.cwa), Actigraph (.gt3x), GENEActiv (.bin), and
6+
Matrix (.bin) files.
7+
8+
Main Functions
9+
--------------
10+
read_device : Read and process accelerometer device file
11+
process : Process pandas.DataFrame of acceleration time-series
12+
13+
Modules
14+
-------
15+
reader : Device file reading and high-level processing
16+
processing : Signal processing functions (filtering, calibration, resampling, etc.)
17+
matrix_reader : Matrix device-specific binary file reader
18+
19+
Examples
20+
--------
21+
Basic usage with default processing:
22+
23+
>>> import actipy
24+
>>> data, info = actipy.read_device("sample.cwa.gz")
25+
26+
With custom processing options:
27+
28+
>>> data, info = actipy.read_device(
29+
... "sample.cwa.gz",
30+
... lowpass_hz=20,
31+
... calibrate_gravity=True,
32+
... detect_nonwear=True,
33+
... resample_hz=50
34+
... )
35+
36+
See Also
37+
--------
38+
For detailed documentation, visit: https://actipy.readthedocs.io/
39+
"""
40+
141
name = "actipy"
242
__author__ = "Shing Chan, Aiden Doherty"
343
__email__ = "shing.chan@ndph.ox.ac.uk, aiden.doherty@ndph.ox.ac.uk"

src/actipy/matrix_reader.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
"""
2+
Binary file reader for Matrix wearable devices.
3+
4+
This module provides a pure Python implementation for reading and parsing
5+
binary data files from Matrix wearable devices. Unlike other device readers
6+
in actipy, this does not use Java parsers.
7+
8+
Binary File Structure
9+
---------------------
10+
Matrix .bin files consist of:
11+
1. Remarks block (512 bytes) - metadata text
12+
2. File header (16 bytes) - signature, packet count, sensor ranges
13+
3. Data packets - each containing timestamped sensor readings
14+
15+
Each packet contains:
16+
- Header: CRC32 checksum, timestamps, sample counts
17+
- Payload: Accelerometer, gyroscope, temperature, heart rate data
18+
19+
Main Functions
20+
--------------
21+
bin2csv : Convert Matrix .bin file to CSV format
22+
extract_metadata : Read file metadata without processing full dataset
23+
is_matrix_bin_file : Check if file is a valid Matrix binary file
24+
25+
Notes
26+
-----
27+
- Supports compressed files (.gz, .zip, .tar.gz, .tgz)
28+
- Validates data integrity using CRC32 checksums
29+
- Resamples multi-rate sensor data to uniform time grid
30+
- Progress tracking via tqdm progress bars
31+
32+
See Also
33+
--------
34+
Binary format documentation: Matrix wearable device specification
35+
"""
36+
137
import os
238
import logging
339
import struct

src/actipy/processing.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
"""
2+
Signal processing functions for accelerometer data.
3+
4+
This module provides a suite of signal processing operations for cleaning,
5+
calibrating, and analyzing accelerometer time-series data. All functions
6+
operate on pandas DataFrames with DateTimeIndex and return both processed
7+
data and metadata dictionaries.
8+
9+
Main Processing Functions
10+
-------------------------
11+
quality_control : Basic data quality checks and statistics
12+
lowpass : Butterworth lowpass filtering
13+
calibrate_gravity : Gravity-based calibration (van Hees et al. 2014)
14+
flag_nonwear : Detect and flag non-wear periods
15+
resample : Nearest-neighbor resampling to uniform frequency
16+
17+
Utility Functions
18+
-----------------
19+
find_nonwear_segments : Identify non-wear periods without flagging data
20+
butterfilt : Butterworth filter implementation
21+
chunker : Generator for processing data in time-based chunks
22+
23+
Memory Efficiency
24+
-----------------
25+
Functions that process large datasets (lowpass, calibrate_gravity, resample)
26+
use chunked processing and memory-mapped temporary files to minimize RAM usage.
27+
28+
Notes
29+
-----
30+
All functions preserve the input DataFrame structure and return tuples of
31+
(processed_data, info_dict) where info_dict contains processing metadata.
32+
"""
33+
134
import os
235
import tempfile
336
import numpy as np

src/actipy/read_cwa.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
"""
2+
Command-line interface for processing accelerometer device files.
3+
4+
This script provides a command-line tool (read_cwa) for reading and processing
5+
accelerometer data from device files. Despite the name 'read_cwa', it supports
6+
multiple device formats.
7+
8+
Supported Formats
9+
-----------------
10+
- Axivity AX3/AX6 (.cwa)
11+
- Actigraph (.gt3x)
12+
- GENEActiv (.bin)
13+
- Matrix (.bin)
14+
15+
Usage
16+
-----
17+
Basic usage with default processing:
18+
19+
$ read_cwa sample.cwa.gz -o outputs/
20+
21+
With custom processing options:
22+
23+
$ read_cwa sample.cwa.gz -o outputs/ --lowpass-hz 20 --resample-hz 50 \\
24+
--calibrate-gravity --detect-nonwear
25+
26+
Time filtering:
27+
28+
$ read_cwa sample.cwa.gz -o outputs/ --start-time "2014-05-07 18:00:00" \\
29+
--end-time "2014-05-09 18:00:00" --skipdays 1 --cutdays 2
30+
31+
Output
32+
------
33+
Creates two files in the output directory:
34+
- {basename}.csv.gz : Processed acceleration data
35+
- {basename}-Info.json : Processing metadata and statistics
36+
37+
For full option list, run:
38+
$ read_cwa --help
39+
"""
40+
141
import time
242
from pathlib import Path
343
import argparse
@@ -9,16 +49,6 @@
949

1050
from actipy import read_device
1151

12-
"""
13-
How to run the script:
14-
15-
```bash
16-
python src/actipy/read_cwa.py data/test.bin
17-
18-
python src/actipy/read_cwa.py data/test.bin -o data/prepared/ -r 30 -g -f 20 -w -c x y z -q
19-
```
20-
"""
21-
2252

2353
def main():
2454
parser = argparse.ArgumentParser(

0 commit comments

Comments
 (0)