Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds option to use pygt3x to read gt3x files #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions paat/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@
return time_data.flatten()


def read_gt3x(file, rescale=True, pandas=True, metadata=False):
def read_gt3x(file, rescale=True, pandas=True, metadata=False, use_pygt3x=False):
"""
Reads a .gt3x file and returns the tri-axial acceleration values together
with the corresponding time stamps and all meta data.
Expand All @@ -485,6 +485,8 @@
boolean indicating whether the data should be returned as a pandas DataFrame
metadata : boolean (optional)
boolean indicating whether the full metadata should be returned
use_pygt3x : boolean (optional)
boolean indicating whether to use ActiGraph's Pygt3x library to read the file.

Returns
-------
Expand All @@ -503,24 +505,32 @@
a dict containing all meta data produced by ActiGraph

"""
with tempfile.TemporaryDirectory() as tmpdirname:
# unzip .gt3x file and get the file location of the binary log.bin (which contains the raw data) and the info.txt which contains the meta-data
log_bin, info_txt = _unzip_gt3x_file(file=file, save_location=tmpdirname)
if use_pygt3x:
with FileReader(file) as reader:
values = reader.to_pandas()

Check warning on line 510 in paat/io.py

View check run for this annotation

Codecov / codecov/patch

paat/io.py#L509-L510

Added lines #L509 - L510 were not covered by tests

time = pd.to_datetime(values.index, unit="s").values
values = values.values

Check warning on line 513 in paat/io.py

View check run for this annotation

Codecov / codecov/patch

paat/io.py#L512-L513

Added lines #L512 - L513 were not covered by tests

# get meta data from info.txt file
meta = _extract_info(info_txt)
meta = read_metadata(file)

Check warning on line 515 in paat/io.py

View check run for this annotation

Codecov / codecov/patch

paat/io.py#L515

Added line #L515 was not covered by tests
else:
with tempfile.TemporaryDirectory() as tmpdirname:
# unzip .gt3x file and get the file location of the binary log.bin (which contains the raw data) and the info.txt which contains the meta-data
log_bin, info_txt = _unzip_gt3x_file(file=file, save_location=tmpdirname)

meta = _format_meta_data(meta)
# get meta data from info.txt file
meta = _extract_info(info_txt)
meta = _format_meta_data(meta)

# extract acceleration data from the log file
values, time_data = _extract_log(log_bin, meta['Acceleration_Scale'], meta['Sample_Rate'], use_scaling=rescale)
# extract acceleration data from the log file
values, time_data = _extract_log(log_bin, meta['Acceleration_Scale'], meta['Sample_Rate'], use_scaling=rescale)

# create time array
time = _create_time_array(time_data, hz=meta['Sample_Rate'])
# create time array
time = _create_time_array(time_data, hz=meta['Sample_Rate'])

# Add additional keys to meta (Note: they are important to later reconstruct the time vector)
meta["Number_Of_Samples"] = values.shape[0]
meta["Start_Time"] = time[0].astype(int)
# Add additional keys to meta (Note: they are important to later reconstruct the time vector)
meta["Number_Of_Samples"] = values.shape[0]
meta["Start_Time"] = time[0].astype(int)

if pandas:
data = pd.DataFrame(values, columns=["Y", "X", "Z"], index=time)
Expand Down
Loading