Get data into a Json file using Unity
Hello everyone,
How can we access for example Heart Rate and collect every data into a list? Imagine a list made on the Update() which is collecting the information in everyframe.
My problem is that I want to creat a variable of _heartRate and associate it with the heart rate data obtained by the HP Omnicept.
I am reading the C# Reference, but it's getting a little bit hard for me.
Also, I can have that data on the console using the Unity sample correctly, but is there a way to obtain a Json file in order to make an statistical analysis later? It's an easier way to import data into an excel file.
Thank you so much,
Almudena.
-
Official comment
Hello Almudena,
The best way will be to output directly to your desired format. For accumulating the data you have two options:
- You can subscribe to the events and accumulate when you receive the message. You have the base code on how to do this here.
- Update: you will need the reference to the glia behavior (same as with the above option) and call the function GetLast{Sensor}();
Note that on Update you are defining your own sampling rate and with delegates, you are getting the frequency of the sensor.
To save this data you also have two options:
- JSON: With unity's JsonUtility you can serialize to JSON. You will need to create a serializable struct that holds the List<HR> values and then use the Json Utility to convert it to Json and save it to a file.
[System.Serializable]
private struct MyAppHR{
public List<uint> rateValues;
}private MyAppHR myAppHR;
- CSV: You can use a C# helper class that handles the CSV (you will find many open-source implementations on GitHub) or by yourself, you can use the streamwriter to write the new CSV lines
A quick example of how this will work altogether:
using System.Collections.Generic;
using UnityEngine;
using HP.Omnicept.Unity;
using HP.Omnicept.Messaging.Messages;
using System.IO;
public class WriteHRToJSON : MonoBehaviour {
[System.Serializable]
private struct MyAppHR
{
public List<uint> rateValues;
}
public string filename = "HR.json";
private MyAppHR myAppHR;
private GliaBehaviour _gliaBehaviour;
private GliaBehaviour m_gliaBehaviour {
get{
if(_gliaBehaviour == null){
_gliaBehaviour = FindObjectOfType<GliaBehaviour>();
}
return _gliaBehaviour;
}
}
private string savePath {
get{
return $"{Application.persistentDataPath}/{filename}";
}
}
#region Unity LifeCycle
private void Start() {
myAppHR = new MyAppHR{ rateValues = new List<uint>()};
}
private void OnEnable() {
m_gliaBehaviour.OnHeartRate.AddListener(HandleHR);
}
private void OnDisable() {
m_gliaBehaviour.OnHeartRate.RemoveListener(HandleHR);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
SaveToJson();
}
}
#endregion
private void HandleHR(HeartRate hr)
{
if (hr != null)
{
myAppHR.rateValues.Add(hr.Rate);
}
}
private void SaveToJson()
{
string json = JsonUtility.ToJson(myAppHR, true);
File.WriteAllText(savePath, json);
Debug.Log($"Saved values at: {savePath}");
}
} -
Hi Sergi,
Thanks so much for your rapid response! It is a pleasure to have professionals like you to help.
Thank you!
1 -
Thank you for asking great questions on the forum! Do not hesitate to reach us with any other issues or questions :)
1 -
Great! Just say that your script works perfectly, and I was able to modify it in order to have another data into my json file (as pupil diameter).
I had a question about the frequency rate of the sensor... is there a site to see which frequency of update does every sensor have for the physiological measures? I've seen that in a few seconds a get more pupil size data than heart rate.
Thank you again!
0 -
Yes, you are right, we have the fundamentals page which contains information on the frequency of the different sensors.
See that eye-tracking is reported at a 120HZ rate. Which will give you approximately 2 samples per frame assuming you are at 60 fps. And for HR you have 1 sample every 5 seconds.
One option here for you will be saving a timestamp with the values so you later can put them in the right order/time despite them having different frequencies.
Hope this helps :)
0 -
Hello again Sergi,
I was trying to export the eye gaze coordinates into a json file. I used the same script as the one used for seeing the heart rate, but I am not able to access the combined eye tracking data. I am seeing this website: https://developers.hp.com/omnicept/docs/unity/sensors#eyeTracking, but I don't understand how to export the combinedgaze data.
I modified the script this way:
But, as you see, gd.CombinedGaze is underlined red.
Thank you,
Almudena.
0 -
This is the whole script:
using System.Collections.Generic;
using UnityEngine;
using HP.Omnicept.Unity;
using HP.Omnicept.Messaging.Messages;
using System.IO;
public class WriteGazeData : MonoBehaviour
{
[System.Serializable]
private struct MyAppGD
{
public List<uint> rateValues;
}
public string filename = "GazeData_FirstExperiment.json";
private MyAppGD myAppGD;
private GliaBehaviour _gliaBehaviour;
private GliaBehaviour m_gliaBehaviour
{
get
{
if (_gliaBehaviour == null)
{
_gliaBehaviour = FindObjectOfType<GliaBehaviour>();
}
return _gliaBehaviour;
}
}
private string savePath
{
get
{
return $"{Application.streamingAssetsPath}/{filename}";
}
}
#region Unity LifeCycle
private void Start()
{
myAppGD = new MyAppGD { rateValues = new List<uint>() };
}
private void OnEnable()
{
m_gliaBehaviour.OnEyeTracking.AddListener(HandleGD);
}
private void OnDisable()
{
m_gliaBehaviour.OnEyeTracking.RemoveListener(HandleGD);
}
private void Update()
{
}
#endregion
private void HandleGD(EyeTracking gd)
{
if (gd != null)
{
myAppGD.rateValues.Add(gd.CombinedGaze));
}
}
private void SaveToJson()
{
string json = JsonUtility.ToJson(myAppGD, true);
File.WriteAllText(savePath, json);
Debug.Log($"Saved values at: {savePath}");
}
}0 -
Hello again,
I modified the script and I got the data into the json file with x, y and z as separated columns, but it seems that the headset refresh rate turns slow, I mean, it's impossible to run the application... any ideas of what is going on?
using System.Collections.Generic;
using UnityEngine;
using HP.Omnicept.Unity;
using HP.Omnicept.Messaging.Messages;
using System.IO;
public class WriteGazeData : MonoBehaviour
{
[System.Serializable]
private struct MyAppGD
{
public List<Vector3> rateValues;
}
public float timer;
public bool dataExported;
public string filename = "GazeData_FirstExperiment.json";
private MyAppGD myAppGD;
private GliaBehaviour _gliaBehaviour;
private GliaBehaviour m_gliaBehaviour
{
get
{
if (_gliaBehaviour == null)
{
_gliaBehaviour = FindObjectOfType<GliaBehaviour>();
}
return _gliaBehaviour;
}
}
private string savePath
{
get
{
return $"{Application.streamingAssetsPath}/{filename}";
}
}
#region Unity LifeCycle
private void OnEnable()
{
m_gliaBehaviour.OnEyeTracking.AddListener(HandleGD);
}
private void OnDisable()
{
m_gliaBehaviour.OnEyeTracking.RemoveListener(HandleGD);
}
private void Start()
{
myAppGD = new MyAppGD { rateValues = new List<Vector3>() };
dataExported = false;
}
private void Update()
{
timer -= Time.deltaTime;
if (timer < 0 && dataExported == false)
{
Debug.Log("Data was exported");
SaveToJson();
dataExported = true;
}
}
#endregion
private void HandleGD(EyeTracking gd)
{
if (gd != null)
{
myAppGD.rateValues.Add(new Vector3 (gd.CombinedGaze.X, gd.CombinedGaze.Y, gd.CombinedGaze.Z));
}
}
private void SaveToJson()
{
string json = JsonUtility.ToJson(myAppGD, true);
File.WriteAllText(savePath, json);
Debug.Log($"Saved values at: {savePath}");
}
}0
Please sign in to leave a comment.
Comments
8 comments