315 lines
12 KiB
C#
315 lines
12 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Xml.Linq;
|
|
using Unity.VisualScripting;
|
|
using UnityEditor.PackageManager;
|
|
using UnityEngine;
|
|
using Varjo.XR;
|
|
using Varjo.XR.Input;
|
|
using static UnityEngine.UIElements.UxmlAttributeDescription;
|
|
using System.IO;
|
|
using Unity.VisualScripting.Antlr3.Runtime.Tree;
|
|
using System.Linq;
|
|
using EasyRoads3Dv3;
|
|
|
|
[Serializable]
|
|
public class SceneLog
|
|
{
|
|
public string SceneName; // Name of the scene
|
|
public string SceneStartTime; // Start time of the scene
|
|
public string SceneEndTime; // End time of the scene
|
|
public int TotalSafeObjectsCount; // Total number of safe objects in the scene
|
|
public int TotalDangerObjectsCount; // Total number of danger objects in the scene
|
|
public int SceneLogSelectedSafeCount; // Number of safe objects selected by the user
|
|
public int SceneLogSelectedDangerCount; // Number of danger objects selected by the user
|
|
public float OverallErrorRate; // Error rate for correctly selected safe objects
|
|
public string SceneCompletionTime; // Total time taken to complete the scene
|
|
public string CameraPerspective;
|
|
public string ObjectSelectionType;
|
|
// Constructor for convenience
|
|
public SceneLog(string sceneName, string cameraPerspective, string objectSelectionType, string sceneStartTime, string sceneEndTime, string sceneCompletionTime,
|
|
int totalSafeObjects, int totalDangerObjects,
|
|
int selectedSafe, int selectedDanger,
|
|
float overallErrorRate)
|
|
{
|
|
SceneName = sceneName;
|
|
CameraPerspective = cameraPerspective;
|
|
ObjectSelectionType = objectSelectionType;
|
|
SceneStartTime = sceneStartTime;
|
|
SceneEndTime = sceneEndTime;
|
|
SceneCompletionTime = sceneCompletionTime;
|
|
TotalSafeObjectsCount = totalSafeObjects;
|
|
TotalDangerObjectsCount = totalDangerObjects;
|
|
SceneLogSelectedSafeCount = selectedSafe;
|
|
SceneLogSelectedDangerCount = selectedDanger;
|
|
OverallErrorRate = overallErrorRate;
|
|
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
/*return $"SceneName: {SceneName}, " +
|
|
$"SceneStartTime: {SceneStartTime}, " +
|
|
$"SceneEndTime: {SceneEndTime}, " +
|
|
$"SceneCompletionTime: {SceneCompletionTime}, " +
|
|
$"TotalSafeObjectsCount: {TotalSafeObjectsCount}, " +
|
|
$"TotalDangerObjectsCount: {TotalDangerObjectsCount}, " +
|
|
$"SelectedSafeCount: {SelectedSafeCount}, " +
|
|
$"SelectedDangerCount: {SelectedDangerCount}, " +
|
|
$"ErrorRateSafe: {ErrorRateSafe:F2}, " +
|
|
$"ErrorRateDanger: {ErrorRateDanger:F2}, " +
|
|
$"SceneCompletionTime: {SceneCompletionTime}"; */
|
|
|
|
Debug.Log("Getting the String to write into File...");
|
|
|
|
return $"\n{SceneName}, " +
|
|
$"{CameraPerspective}, " +
|
|
$"{ObjectSelectionType}, " +
|
|
$"{SceneStartTime}, " +
|
|
$"{SceneEndTime}, " +
|
|
$"{SceneCompletionTime}, " +
|
|
$"{TotalSafeObjectsCount}, " +
|
|
$"{TotalDangerObjectsCount}, " +
|
|
$"{SceneLogSelectedSafeCount}, " +
|
|
$"{SceneLogSelectedDangerCount}, " +
|
|
$"{OverallErrorRate:F2}\n";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class SceneManager : MonoBehaviour
|
|
{
|
|
public DateTime sceneStartTime;
|
|
public DateTime sceneEndTime;
|
|
TimeSpan sceneCompletionTime;
|
|
private string directoryPath = @"C:\Users\absh8989\AutonomousCars_HDRP\QuantitativeLogs\";
|
|
string fullPath = "";
|
|
OrderManager OM = new OrderManager();
|
|
|
|
|
|
int FP;
|
|
int FN;
|
|
int TP;
|
|
int TN;
|
|
|
|
// Initialize counters for selected safe and danger objects
|
|
int selectedSafeCount = 0;
|
|
int selectedDangerCount = 0;
|
|
// Initialize counters for total safe and danger objects
|
|
int totalSafeObjectsCount;
|
|
int totalDangerObjectsCount;
|
|
// Initialize counters for errorRate
|
|
float errorRateDanger;
|
|
float errorRateSafe;
|
|
float overallErrorRate;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
// Full path for the new file
|
|
fullPath = Path.Combine(directoryPath, OrderManager.userNumber.ToString()+".csv");
|
|
//File.Create(fullPath).Dispose();
|
|
File.AppendAllText(fullPath, "");
|
|
string firstLine = File.ReadLines(fullPath).FirstOrDefault(); File.ReadLines(fullPath).FirstOrDefault();
|
|
if (!string.IsNullOrEmpty(firstLine) && firstLine.StartsWith("SceneName,CameraPerspective,"))
|
|
{
|
|
// nothing
|
|
}
|
|
else
|
|
{
|
|
File.AppendAllText(fullPath, "SceneName,CameraPerspective,TaskType,SceneStartTime,SceneEndTime,SceneCompletionTime,TotalSafeObjectsCount,TotalDangerObjectsCount,SelectedSafeCount,SelectedDangerCount,OverallErrorRate\n");
|
|
}
|
|
|
|
|
|
sceneStartTime = System.DateTime.Now;
|
|
Debug.Log("Scene Start Time:" + sceneStartTime.ToString());
|
|
|
|
// Find all GameObjects with the tag "selectingSignifier" and set them inactive
|
|
GameObject[] taggedObjects = GameObject.FindGameObjectsWithTag("selectingSignifier");
|
|
foreach (GameObject go in taggedObjects)
|
|
{
|
|
go.SetActive(false);
|
|
}
|
|
|
|
// Call the function to count SafeObject and DangerObject tagged items
|
|
CountTaggedObjects();
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown("space"))
|
|
{
|
|
lastSteps();
|
|
OM.ChangeSceneOrder();
|
|
}
|
|
}
|
|
|
|
|
|
// Method to count SafeObject and DangerObject tagged items
|
|
public void CountTaggedObjects()
|
|
{
|
|
// Find all GameObjects with the tag "SafeObject"
|
|
GameObject[] safeObjects = GameObject.FindGameObjectsWithTag("SafeObject");
|
|
totalSafeObjectsCount = safeObjects.Length;
|
|
|
|
// Find all GameObjects with the tag "DangerObject"
|
|
GameObject[] dangerObjects = GameObject.FindGameObjectsWithTag("DangerObject");
|
|
totalDangerObjectsCount = dangerObjects.Length;
|
|
|
|
// Output the counts to the console
|
|
Debug.Log("Safe Object Count: " + totalSafeObjectsCount);
|
|
Debug.Log("Danger Object Count: " + totalDangerObjectsCount);
|
|
}
|
|
|
|
// Method to count selected SafeObject and DangerObject items based on activating selectingSignifier
|
|
public void CountSelectedObjects()
|
|
{
|
|
Debug.Log("Counting Selected Objects");
|
|
|
|
// Find all GameObjects with the "selectingSignifier" tag
|
|
GameObject[] taggedObjects = GameObject.FindGameObjectsWithTag("selectingSignifier"); // FindGameObjectsWithTag only returns active game objects. This means that if an object is deactivated (its SetActive(false)), it will not be included in the array returned by this method, even if it exists in the scene hierarchy.
|
|
Debug.Log("Selected objects in total - "+taggedObjects.Length);
|
|
foreach (GameObject go in taggedObjects)
|
|
{
|
|
|
|
// Check if the object is tagged as "SafeObject" and is active
|
|
if (go.transform.parent.gameObject.CompareTag("SafeObject"))
|
|
{
|
|
selectedSafeCount++;
|
|
}
|
|
// Check if the object is tagged as "DangerObject" and is active
|
|
else if (go.transform.parent.gameObject.CompareTag("DangerObject"))
|
|
{
|
|
selectedDangerCount++;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Output the counts of selected objects to the console
|
|
Debug.Log("Selected Safe Objects: " + selectedSafeCount);
|
|
Debug.Log("Selected Danger Objects: " + selectedDangerCount);
|
|
}
|
|
|
|
public void CalculateErrorRateBasedOnCorrectDangerObjectsMarked()
|
|
{
|
|
if (selectedDangerCount > 0)
|
|
{
|
|
Debug.Log("Calculating Error rate based on Correctly selected Danger Objects");
|
|
errorRateDanger = 1 - ((float)selectedDangerCount / totalDangerObjectsCount);
|
|
Debug.Log("Error Rate - Correctly selected Danger Objects:" + errorRateDanger.ToString());
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Correctly selected Danger Objects is ZERO");
|
|
}
|
|
}
|
|
public void CalculateErrorRateBasedOnCorrectSafeObjectsMarked()
|
|
{
|
|
if (selectedSafeCount > 0)
|
|
{
|
|
Debug.Log("Calculating Error rate based on Correctly selected Safe Objects");
|
|
errorRateSafe = 1 - ((float)selectedSafeCount / totalSafeObjectsCount);
|
|
Debug.Log("Error Rate - Correctly selected Safe Objects:" + errorRateSafe.ToString());
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Correctly selected Safe Objects is ZERO");
|
|
}
|
|
}
|
|
|
|
public void CalculateOverAllErrorRate()
|
|
{
|
|
Debug.Log("Calculating Error rate....");
|
|
if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Contains("_Safe_"))
|
|
{
|
|
|
|
/*
|
|
* Scenario when the user is supposed to select Safe Objects
|
|
False Positives(FP): Danger objects incorrectly marked as safe.
|
|
False Negatives(FN): Safe objects not selected(missed by the user).
|
|
True Positives(TP): Safe objects correctly marked as safe.
|
|
True Negatives(TN): Danger objects correctly left unselected.
|
|
*/
|
|
TP = selectedSafeCount;
|
|
FN = totalSafeObjectsCount - TP;
|
|
FP = selectedDangerCount;
|
|
TN = totalDangerObjectsCount - selectedDangerCount;
|
|
overallErrorRate = ((float)(FP + FN) / (totalDangerObjectsCount + totalSafeObjectsCount));
|
|
Debug.Log("Error Rate(_Safe_) - " + overallErrorRate.ToString());
|
|
|
|
}
|
|
else if(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.Contains("_UnSafe_"))
|
|
{
|
|
/*
|
|
* Scenario when the user is supposed to select UNSAFE Objects
|
|
True Positives (TP): Danger objects correctly selected as danger.
|
|
False Positives (FP): Safe objects incorrectly marked as danger.
|
|
False Negatives (FN): Danger objects that the user missed selecting.
|
|
True Negatives (TN): Safe objects correctly left unselected.
|
|
*/
|
|
|
|
TP = selectedDangerCount;
|
|
FP = selectedSafeCount;
|
|
FN = totalDangerObjectsCount - TP;
|
|
TN = totalSafeObjectsCount - selectedSafeCount;
|
|
overallErrorRate = ((float)(FP + FN) / (totalDangerObjectsCount + totalSafeObjectsCount));
|
|
Debug.Log("Error Rate(_UnSafe_) - " + overallErrorRate.ToString());
|
|
}
|
|
|
|
|
|
|
|
}
|
|
public void lastSteps()
|
|
{
|
|
CountSelectedObjects();
|
|
|
|
CalculateOverAllErrorRate();
|
|
|
|
|
|
sceneEndTime = System.DateTime.Now;
|
|
Debug.Log("Scene End Time:" + sceneEndTime.ToString());
|
|
|
|
sceneCompletionTime = (sceneEndTime - sceneStartTime).Duration();
|
|
Debug.Log("Scene Completion Time:" + sceneCompletionTime.ToString());
|
|
|
|
writeIntoFile();
|
|
}
|
|
|
|
public void writeIntoFile()
|
|
{
|
|
String SceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.ToString();
|
|
|
|
String CameraPerspective = SceneName.Split("_")[0];
|
|
String ObjectSelectionType = SceneName.Split("_")[1];
|
|
|
|
Debug.Log("Writing into File...");
|
|
SceneLog log = new SceneLog(
|
|
UnityEngine.SceneManagement.SceneManager.GetActiveScene().name.ToString(),
|
|
CameraPerspective,
|
|
ObjectSelectionType,
|
|
sceneStartTime.ToString(),
|
|
sceneEndTime.ToString(),
|
|
sceneCompletionTime.ToString(),
|
|
totalSafeObjectsCount,
|
|
totalDangerObjectsCount,
|
|
selectedSafeCount,
|
|
selectedDangerCount,
|
|
overallErrorRate
|
|
);
|
|
|
|
|
|
|
|
File.AppendAllText(fullPath, log.ToString());
|
|
|
|
}
|
|
|
|
}
|