연구 참여자이신가요?
연구 참여자인 경우, 관리자에게 직접 문의하세요. 지원 센터의 정보는 자격을 갖춘 연구자 및 의료 전문가를 위한 것입니다. 아래 정보를 잘못 적용하면 데이터 수집에 영향을 줄 수 있습니다.
E4를 이용해 주셔서 감사합니다
2025년 2월 14일부로 E4 및 관련 소프트웨어 제품군의 서비스가 공식적으로 종료되었습니다. 자세한 내용은 여기를 클릭하세요.
웨어러블 연구 솔루션을 찾고 계신다면 Empatica Health Monitoring Platform을 살펴보세요.

Python으로 Avro 파일에 액세스하는 방법

enterprise에서 Care 포털에 액세스할 수 있는지는 연구 구성에 따라 결정됩니다. 연구 구성에 대한 자세한 내용은 연구팀 또는 계정 관리자에게 문의하세요.

Empatica의 Avro를 CSV로 변환하는 Python 예

Avro 데이터 형식은 플랫폼에 독립적이며, Python용 API를 포함한 다양한 공식 API를 제공합니다.
Apache Avro™ 1.11.1 시작하기(Python)

Avro 형식으로 제공되는 데이터에 익숙해질 수 있도록 Avro 파일에 포함된 데이터에 액세스하는 기본 방법을 보여 주는 Python 스크립트 예를 제공합니다.

이 스크립트는 하나의 Empatica Avro 파일을 입력으로 사용하고, 센서별로 하나씩 여러 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. 다음에서 Python을 설치합니다. Download Python
  2. 터미널을 열고 pip3 install avro를 실행합니다.
  3. 다음 파일을 다운로드합니다. avro_to_csv_example_script.py
  4. 다운로드한 파일을 텍스트 편집기에서 열고 다음 항목을 바꿉니다.
    1. avro_file_path를 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. 다음에서 Python을 설치합니다. Download Python
    1. 설치하는 동안 Python을 PATH에 추가해야 합니다.
  2. 터미널을 열고 pip install avro를 실행합니다.
  3. 다음 파일을 다운로드합니다. avro_to_csv_example_script.py
  4. 다운로드한 파일을 텍스트 편집기에서 열고 다음 항목을 바꿉니다.
    1. avro_file_path를 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 파일에서 추출한 원시 데이터가 포함된 CSV 파일 7개가 생성됩니다.

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

이 섹션의 문서

헬프라인 운영 시간
9:00am to 1:00pm EST