試験参加者の方ですか?
試験参加者の方は、担当の管理者に直接お問い合わせください。サポートセンターの情報は、資格を有する研究者および医療従事者を対象としています。以下の情報を誤って適用すると、データ収集に影響を及ぼす可能性があります。
E4 をご利用いただきありがとうございます
2025年2月14日をもって、E4 およびそのソフトウェアスイートは正式にサービスを終了しました。詳しくはこちらをクリックしてください。
ウェアラブルの研究用ソリューションをお探しの場合は、Empatica Health Monitoring Platform をご覧ください。

PythonでAvroファイルにアクセスする方法

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と入力します)。

 

PYTHONのサンプルスクリプト

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]"

スクリプトを保存して実行します。

 

macOS
  1. Download PythonからPythonをインストールします。
  2. ターミナルを開き、pip3 install avroを実行します。
  3. このファイルをダウンロードします:avro_to_csv_example_script.py
  4. ダウンロードしたファイルをテキストエディタで開き、次の内容を置き換えます。
    1. avro_file_pathを1つのAvroファイルのパスに置き換えます。例:

      avro_file_path = "/Users/your_ursename/RawData/Avros/1-1-001_1671668087.avro"
    2. output_dirを選択した出力フォルダーのパスに置き換えます。例:

      output_dir = "/Users/your_ursename/RawData/Output/"
    3. ファイルを保存します
  5. スクリプトを実行します。
    1. ターミナルでpython3と入力し、キーボードのスペースキーを押してから、avro_to_csv_example_script.pyをターミナルにドラッグアンドドロップします。例:

      python3 /Users/your_ursename/RawData/avro_to_csv_example_script.py
    2. Enterキーを押してスクリプトを実行します。
Windows
  1. Download PythonからPythonをインストールします。
    1. インストール中に、PythonをPATHに追加してください
  2. ターミナルを開き、pip install avroを実行します。
  3. このファイルをダウンロードします:avro_to_csv_example_script.py
  4. ダウンロードしたファイルをテキストエディタで開き、次の内容を置き換えます。
    1. avro_file_pathを1つのAvroファイルのパスに置き換えます。例:

      avro_file_path = "C:/RawData/Avros/1-1-001_1671668087.avro"

      エラーを避けるため、ファイルパスにはスラッシュ/のみを使用してください

    2. output_dirを選択した出力フォルダーのパスに置き換えます。例:

      output_dir = "C:/RawData/Output/"
    3. ファイルを保存します
  5. スクリプトを実行します。
    1. ターミナルでpythonと入力し、キーボードのスペースキーを押してから、avro_to_csv_example_script.pyをターミナルにドラッグアンドドロップします。例:

      python C:/RawData/avro_to_csv_example_script.py
    2. Enterキーを押してスクリプトを実行します。

 

これで、出力ディレクトリにAvroファイルから抽出された生データを含む7つのCSVファイルが作成されます。

  • accelerometer.csv
  • bvp.csv
  • eda.csv
  • steps.csv
  • systolic_peaks.csv
  • tags.csv
  • temperature.csv

このセクションの記事

ヘルプラインの受付時間
9:00am to 1:00pm EST