immersivecognition / unity-experiment-framework

UXF - Framework for creating human behaviour experiments in Unity
https://immersivecognition.github.io/unity-experiment-framework/
MIT License
221 stars 43 forks source link

Bug that the last column of the csv table is not found in the settings hierachy. #168

Closed JAQuent closed 3 months ago

JAQuent commented 3 months ago

For a WebGL experiment I created a method to download .csv files and build the experiment from the table:

        Debug.Log("Start download...");
        Debug.Log("Path: " + csvPath);
        // download file from StreamingAssets folder
        UnityWebRequest www = UnityWebRequest.Get(csvPath);
        yield return www.SendWebRequest();

        if (www.result != UnityWebRequest.Result.Success){
            Debug.LogError("Error downloading CSV file: " + www.error);
            yield break;
        }

        string csvText = www.downloadHandler.text;
        Debug.Log(csvText);
        string[] csvLines = csvText.Split("\n");

        // parse as table
        UXFDataTable table = UXFDataTable.FromCSV(csvLines);

        // build the experiment.
        // this adds a new trial to the session for each row in the table
        // the trial will be created with the settings from the values from the table
        // if "block_num" is specified in the table, the trial will be added to the block with that number
        session.BuildFromTable(table, copyToResults);

However, there is this super odd bug that when I try to access the last column of the .csv e.g. trial.settings.GetFloat("hello") ,I get the following error message:

KeyNotFoundException: The key "hello" was not found in the settings heirarchy. Use UXF Session Debugger (UXF menu at top of unity editor) to check your settings are being applied correctly.
UXF.Settings.Get (System.String key) (at Assets/UXF/Scripts/Etc/Settings.cs:413)
UXF.Settings.Get (System.String key) (at Assets/UXF/Scripts/Etc/Settings.cs:411)
UXF.Settings.Get (System.String key) (at Assets/UXF/Scripts/Etc/Settings.cs:411)
UXF.Settings.GetFloat (System.String key) (at Assets/UXF/Scripts/Etc/Settings.cs:94)
ExperimentController.locationRetrieved () (at Assets/Scripts/ExperimentController.cs:268)
ExperimentController.Update () (at Assets/Scripts/ExperimentController.cs:160)

However, hello is in the session debugger: image

The problem disappears when I add a dummy column that doesn't matter, so this column can't be accessed by trial.settings.GetFloat("dummy") but hello can.

JAQuent commented 3 months ago

I found the issue: it seems that somewhere in the process from downloading the .csv file to splitting it by rows a \r was added to each end of line this a) let to the error I described above but also b) that the .csv data is not saved correctly so that the columns copied from the table and the trial results added during the the experiment are split into two rows. This let me to investigate whether there are still new line characters in the strings. I now simply replace each '\r` with an empty string:

        string csvText = www.downloadHandler.text;
        string[] csvLines = csvText.Split("\n");
        // Loop through the lines
        for (int i = 0; i < csvLines.Length; i++){
            csvLines[i] = csvLines[i].Replace("\r", String.Empty);
        }

        // parse as table
        UXFDataTable table = UXFDataTable.FromCSV(csvLines);