De beschikbaarheid van toegang tot de Care-portal op enterprise wordt bepaald door de studieconfiguratie. Neem voor meer informatie over de configuratie van je studie contact op met je Study-team of accountmanager.
Python-voorbeeld om Avro van Empatica naar CSV te converteren
Het Avro-gegevensformaat is platformonafhankelijk en biedt verschillende officiële API's, waaronder een voor Python:
Apache Avro™ 1.11.1 Getting Started (Python)
Om vertrouwd te raken met gegevens die in Avro-formaat worden aangeleverd, bieden we een Python-voorbeeldscript dat de basisbeginselen laat zien van het openen van gegevens in Avro-bestanden.
Dit script gebruikt één Avro-bestand van Empatica als invoer en converteert dit naar meerdere CSV-bestanden, één voor elke sensor.
De onderstaande code laat zien hoe je de velden van een Avro-bestand laadt en opent (let op: deze code is getest voor versie 6 van de Avro-bestanden). Het script slaat onbewerkte gegevens op in een CSV-bestand voor elke sensor. Houd er rekening mee dat de beschikbaarheid van de verschillende onbewerkte gegevens afhankelijk is van de studieconfiguratie: sommige uitvoerbestanden kunnen leeg zijn.
Het pakket avro moet in je Python-omgeving zijn geïnstalleerd voordat je dit script uitvoert (voer bijvoorbeeld pip install avro uit in je terminal).
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"])])Het voorbeeldscript uitvoeren
Installeer het pakket avro en voer vervolgens het pad naar een Avro-bestand en het pad naar de uitvoermap in het script in:
avro_file_path = "[insert the avro filename with the full path]"
output_dir = "[insert the path to a directory where to save csv files]"Sla het script op en voer het uit.
- Installeer Python via Download Python
- Open een terminal en voer
pip3 install avrouit - Download dit bestand: avro_to_csv_example_script.py
- Open het gedownloade bestand in een teksteditor en vervang het volgende:
-
avro_file_path door het pad naar één Avro-bestand, bijvoorbeeld:
avro_file_path = "/Users/your_ursename/RawData/Avros/1-1-001_1671668087.avro" -
output_dir door het pad naar de door jou gekozen uitvoermap, bijvoorbeeld:
output_dir = "/Users/your_ursename/RawData/Output/" - Sla het bestand op
-
- Voer het script uit:
-
Typ
python3in de terminal, druk op de spatiebalk en sleep avro_to_csv_example_script.py vervolgens naar de terminal. Bijvoorbeeld:python3 /Users/your_ursename/RawData/avro_to_csv_example_script.py - Druk op Enter om het script uit te voeren.
-
- Installeer Python via Download Python
- Zorg ervoor dat je Python tijdens de installatie aan PATH toevoegt
- Open een terminal en voer
pip install avrouit - Download dit bestand: avro_to_csv_example_script.py
- Open het gedownloade bestand in een teksteditor en vervang het volgende:
-
avro_file_path door het pad naar één Avro-bestand, bijvoorbeeld:
avro_file_path = "C:/RawData/Avros/1-1-001_1671668087.avro"Gebruik uitsluitend een forward slash
/in bestandspaden om fouten te voorkomen -
output_dir door het pad naar de door jou gekozen uitvoermap, bijvoorbeeld:
output_dir = "C:/RawData/Output/" - Sla het bestand op
-
- Voer het script uit:
-
Typ
pythonin de terminal, druk op de spatiebalk en sleep avro_to_csv_example_script.py vervolgens naar de terminal. Bijvoorbeeld:python C:/RawData/avro_to_csv_example_script.py - Druk op Enter om het script uit te voeren.
-
De uitvoermap bevat nu 7 CSV-bestanden met onbewerkte gegevens die uit het Avro-bestand zijn geëxtraheerd:
- accelerometer.csv
- bvp.csv
- eda.csv
- steps.csv
- systolic_peaks.csv
- tags.csv
- temperature.csv