enterprise 上的 Care Portal 访问权限由研究配置决定。有关研究配置的更多信息,请咨询您的研究团队或客户经理。
使用 Python 将 Empatica 的 Avro 转换为 CSV 的示例
Avro 数据格式独立于平台,并提供了各种官方 API,其中包括适用于 Python 的 API:
Apache Avro™ 1.11.1 入门指南(Python)
为了帮助您熟悉 Avro 格式提供的数据,我们提供了一个 Python 脚本示例,展示如何访问 Avro 文件中包含的数据。
此脚本以一个 Empatica Avro 文件作为输入,并将其转换为多个 CSV 文件,每个传感器对应一个文件。
以下代码展示了如何加载和访问 Avro 文件中的字段(注意:此代码已针对 Avro 文件 v6 进行测试)。该脚本会将每个传感器的原始数据保存到一个 CSV 文件中。请注意,不同原始数据的可用性取决于研究配置:某些输出文件可能为空。
运行此脚本前,需要在 Python 环境中安装 avro 软件包(例如,在终端中输入 pip install avro)。
from avro.datafile import DataFileReader
from avro.io import DatumReader
import json
import csv
import os
## Define the location of the Avro file and output folder.
# macOS example:
# avro_file_path = "/Users/timmytommy/Data/Avros/1-1-100_1707311064.avro"
# output_dir = "/Users/timmytommy/Data/Output/"
#
# Windows example:
# avro_file_path = "C:/Data/Avros/1-1-100_1707311064.avro"
# output_dir = "C:/Data/Output/"
avro_file_path = "[insert the avro filename with the full path]"
output_dir = "[insert the path to a directory where to save csv files]"
## Read Avro file
reader = DataFileReader(open(avro_file_path, "rb"), DatumReader())
schema = json.loads(reader.meta.get("avro.schema").decode("utf-8"))
data = next(reader)
## Uncomment the below 2 lines to print the Avro schema
# print(schema)
# print(" ")
## Uncomment the below 2 lines to export the entire content into a JSON file
# with open(os.path.join(output_dir, "content.json"), "w") as f:
# json.dump(data, f, indent=4)
## Export sensors data to csv files
avro_version = (
(data["schemaVersion"]["major"]),
(data["schemaVersion"]["minor"]),
(data["schemaVersion"]["patch"]),
)
# Accelerometer
acc = data["rawData"]["accelerometer"]
timestamp = [
round(acc["timestampStart"] + i * (1e6 / acc["samplingFrequency"]))
for i in range(len(acc["x"]))
]
# Convert ADC counts in g
if avro_version < (6, 5, 0):
delta_physical = acc["imuParams"]["physicalMax"] - acc["imuParams"]["physicalMin"]
delta_digital = acc["imuParams"]["digitalMax"] - acc["imuParams"]["digitalMin"]
x_g = [val * delta_physical / delta_digital for val in acc["x"]]
y_g = [val * delta_physical / delta_digital for val in acc["y"]]
z_g = [val * delta_physical / delta_digital for val in acc["z"]]
else:
conversion_factor = acc["imuParams"]["conversionFactor"]
x_g = [val * conversion_factor for val in acc["x"]]
y_g = [val * conversion_factor for val in acc["y"]]
z_g = [val * conversion_factor for val in acc["z"]]
with open(os.path.join(output_dir, "accelerometer.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["unix_timestamp", "x", "y", "z"])
writer.writerows([[ts, x, y, z] for ts, x, y, z in zip(timestamp, x_g, y_g, z_g)])
# Eda
eda = data["rawData"]["eda"]
timestamp = [
round(eda["timestampStart"] + i * (1e6 / eda["samplingFrequency"]))
for i in range(len(eda["values"]))
]
with open(os.path.join(output_dir, "eda.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["unix_timestamp", "eda"])
writer.writerows([[ts, eda] for ts, eda in zip(timestamp, eda["values"])])
# Temperature
tmp = data["rawData"]["temperature"]
timestamp = [
round(tmp["timestampStart"] + i * (1e6 / tmp["samplingFrequency"]))
for i in range(len(tmp["values"]))
]
with open(os.path.join(output_dir, "temperature.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["unix_timestamp", "temperature"])
writer.writerows([[ts, tmp] for ts, tmp in zip(timestamp, tmp["values"])])
# Tags
tags = data["rawData"]["tags"]
with open(os.path.join(output_dir, "tags.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["tags_timestamp"])
writer.writerows([[tag] for tag in tags["tagsTimeMicros"]])
# BVP
bvp = data["rawData"]["bvp"]
timestamp = [
round(bvp["timestampStart"] + i * (1e6 / bvp["samplingFrequency"]))
for i in range(len(bvp["values"]))
]
with open(os.path.join(output_dir, "bvp.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["unix_timestamp", "bvp"])
writer.writerows([[ts, bvp] for ts, bvp in zip(timestamp, bvp["values"])])
# Systolic peaks
sps = data["rawData"]["systolicPeaks"]
with open(os.path.join(output_dir, "systolic_peaks.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["systolic_peak_timestamp"])
writer.writerows([[sp] for sp in sps["peaksTimeNanos"]])
# Steps
steps = data["rawData"]["steps"]
timestamp = [
round(steps["timestampStart"] + i * (1e6 / steps["samplingFrequency"]))
for i in range(len(steps["values"]))
]
with open(os.path.join(output_dir, "steps.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["unix_timestamp", "steps"])
writer.writerows([[ts, step] for ts, step in zip(timestamp, steps["values"])])如何运行示例脚本
安装 avro 软件包后,在脚本中输入一个 Avro 文件的路径和输出目录路径:
avro_file_path = "[insert the avro filename with the full path]"
output_dir = "[insert the path to a directory where to save csv files]"保存脚本并运行。
- 从下载 Python安装 Python
- 打开终端并运行
pip3 install avro - 下载此文件:avro_to_csv_example_script.py
- 在文本编辑器中打开下载的文件,并替换以下内容:
-
将 avro_file_path 替换为一个 Avro 文件的路径,例如:
avro_file_path = "/Users/your_ursename/RawData/Avros/1-1-001_1671668087.avro" -
将 output_dir 替换为所选输出文件夹的路径,例如:
output_dir = "/Users/your_ursename/RawData/Output/" - 保存文件
-
- 运行脚本:
-
在终端中输入
python3,按键盘上的空格键,然后将 avro_to_csv_example_script.py 拖放到终端中。例如:python3 /Users/your_ursename/RawData/avro_to_csv_example_script.py - 按 Enter 键运行脚本。
-
- 从下载 Python安装 Python
- 确保在安装过程中将 Python 添加到 PATH
- 打开终端并运行
pip install avro - 下载此文件:avro_to_csv_example_script.py
- 在文本编辑器中打开下载的文件,并替换以下内容:
-
将 avro_file_path 替换为一个 Avro 文件的路径,例如:
avro_file_path = "C:/RawData/Avros/1-1-001_1671668087.avro"为避免错误,请确保文件路径仅使用正斜杠
/ -
将 output_dir 替换为所选输出文件夹的路径,例如:
output_dir = "C:/RawData/Output/" - 保存文件
-
- 运行脚本:
-
在终端中输入
python,按键盘上的空格键,然后将 avro_to_csv_example_script.py 拖放到终端中。例如:python C:/RawData/avro_to_csv_example_script.py - 按 Enter 键运行脚本。
-
输出目录现在将包含从 Avro 文件中提取的原始数据对应的 7 个 CSV 文件:
- accelerometer.csv
- bvp.csv
- eda.csv
- steps.csv
- systolic_peaks.csv
- tags.csv
- temperature.csv