enterpriseでのCare Portalへのアクセス可否は、試験の設定によって決まります。試験の設定について詳しくは、試験チームまたはアカウントマネージャーにお問い合わせください。
EmpaticaのAvroをCSVに変換するPythonの例
Avroデータ形式はプラットフォームに依存せず、Python用を含むさまざまな公式APIが提供されています。
Apache Avro™ 1.11.1 Getting Started (Python)
Avro形式で提供されるデータに慣れていただくため、Avroファイルに含まれるデータにアクセスする基本的な方法を示すPythonスクリプトの例を用意しています。
このスクリプトは、1つのEmpaticaのAvroファイルを入力として受け取り、センサーごとに1つずつ、複数の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]"スクリプトを保存して実行します。
- Download PythonからPythonをインストールします。
- ターミナルを開き、
pip3 install avroを実行します。 - このファイルをダウンロードします:avro_to_csv_example_script.py
- ダウンロードしたファイルをテキストエディタで開き、次の内容を置き換えます。
-
avro_file_pathを1つの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キーを押してスクリプトを実行します。
-
-
Download PythonからPythonをインストールします。
- インストール中に、PythonをPATHに追加してください
- ターミナルを開き、
pip install avroを実行します。 - このファイルをダウンロードします:avro_to_csv_example_script.py
- ダウンロードしたファイルをテキストエディタで開き、次の内容を置き換えます。
-
avro_file_pathを1つの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