浏览代码

Added scenario serialization tests

/main
Steven Leal 4 年前
当前提交
44102c51
共有 6 个文件被更改,包括 77 次插入29 次删除
  1. 2
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  2. 4
      com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs
  3. 23
      com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs
  4. 32
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  5. 4
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs
  6. 41
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs

2
com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs


{
var appParamIds = new List<AppParam>();
var scenario = (ScenarioBase)m_ScenarioField.value;
var configuration = JObject.Parse(scenario.Serialize());
var configuration = JObject.Parse(scenario.SerializeToJson());
var constants = configuration["constants"];
constants["totalIterations"] = m_TotalIterationsField.value;

4
com.unity.perception/Editor/Randomization/Editors/ScenarioBaseEditor.cs


var serializeConstantsButton = m_Root.Q<Button>("serialize-constants");
serializeConstantsButton.clicked += () =>
{
m_Scenario.SerializeToConfigFile();
m_Scenario.SerializeToFile();
AssetDatabase.Refresh();
var newConfigFileAsset = AssetDatabase.LoadAssetAtPath<Object>(m_Scenario.defaultConfigFileAssetPath);
EditorGUIUtility.PingObject(newConfigFileAsset);

deserializeConstantsButton.clicked += () =>
{
Undo.RecordObject(m_Scenario, "Deserialized scenario configuration");
m_Scenario.Deserialize(m_Scenario.defaultConfigFilePath);
m_Scenario.DeserializeFromFile(m_Scenario.defaultConfigFilePath);
};
m_RandomizerListPlaceholder = m_Root.Q<VisualElement>("randomizer-list-placeholder");

23
com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs


public override ScenarioConstants genericConstants => constants;
/// <inheritdoc/>
public override string Serialize()
public override string SerializeToJson()
{
var configObj = new JObject
{

}
/// <inheritdoc/>
public override void Deserialize(string configFilePath)
public override void DeserializeFromFile(string configFilePath)
Debug.Log("No configuration file specified. Running scenario with preset editor authored configuration.");
else if (File.Exists(configFilePath))
{
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>");
var jsonText = File.ReadAllText(configFilePath);
DeserializeConfig(jsonText);
}
else
Debug.LogError($"A scenario configuration file does not exist at path {configFilePath}");
throw new ArgumentNullException();
if (!File.Exists(configFilePath))
throw new FileNotFoundException($"A scenario configuration file does not exist at path {configFilePath}");
Debug.Log($"Deserialized scenario configuration from <a href=\"file:///${configFilePath}\">{configFilePath}</a>");
var jsonText = File.ReadAllText(configFilePath);
DeserializeFromJson(jsonText);
void DeserializeConfig(string json)
/// <inheritdoc/>
public override void DeserializeFromJson(string json)
{
var jsonObj = JObject.Parse(json);
var constantsObj = (JObject)jsonObj["constants"];

32
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


/// Serializes the scenario's constants and randomizer configuration to a JSON string
/// </summary>
/// <returns>The scenario configuration as a JSON string</returns>
public abstract string Serialize();
public abstract string SerializeToJson();
public virtual void SerializeToConfigFile()
public void SerializeToFile()
writer.Write(Serialize());
writer.Write(SerializeToJson());
/// Deserializes this scenario's constants from a json file in the Unity StreamingAssets folder
/// Overwrites this scenario's randomizer settings and scenario constants from a JSON serialized configuration
/// </summary>
/// <param name="json">The JSON string to deserialize</param>
public abstract void DeserializeFromJson(string json);
/// <summary>
/// Overwrites this scenario's randomizer settings and scenario constants using a configuration file located at
/// the provided file path
public abstract void Deserialize(string configFilePath);
public abstract void DeserializeFromFile(string configFilePath);
/// <summary>
/// Overwrites this scenario's randomizer settings and scenario constants using a configuration file located at
/// this scenario's defaultConfigFilePath
/// </summary>
public void DeserializeFromFile()
{
DeserializeFromFile(defaultConfigFilePath);
}
/// <summary>
/// This method executed directly after this scenario has been registered and initialized

Guid.Parse("14adb394-46c0-47e8-a3f0-99e754483b76"));
DatasetCapture.ReportMetric(randomSeedMetricDefinition, new[] { genericConstants.randomSeed });
#if !UNITY_EDITOR
Deserialize(defaultConfigFilePath);
if (File.Exists(defaultConfigFilePath))
DeserializeFromFile();
else
Debug.Log($"No configuration file found at {defaultConfigFilePath}. " +
"Proceeding with built in scenario constants and randomizer settings.");
#endif
}

4
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs


}
/// <inheritdoc/>
public sealed override void Deserialize(string configFilePath)
public sealed override void DeserializeFromFile(string configFilePath)
base.Deserialize(Configuration.Instance.IsSimulationRunningInCloud()
base.DeserializeFromFile(Configuration.Instance.IsSimulationRunningInCloud()
? new Uri(Configuration.Instance.SimulationConfig.app_param_uri).LocalPath
: configFilePath);
currentIteration = constants.instanceIndex;

41
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs


using System.IO;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Experimental.Perception.Randomization.Scenarios;
using UnityEngine.TestTools;

}
[UnityTest]
public IEnumerator OverwritesConstantsOnSerialization()
public IEnumerator ScenarioConfigurationSerializesProperly()
{
yield return CreateNewScenario(10, 10);
var scenario = m_Scenario.GetComponent<FixedLengthScenario>();
scenario.CreateRandomizer<HueOffsetRandomizer>();
const string expectedConfig = @"{
""constants"": {
""framesPerIteration"": 10,
""totalIterations"": 10,
""instanceCount"": 1,
""instanceIndex"": 0,
""randomSeed"": 539662031
},
""randomizers"": {
""HueOffsetRandomizer"": {
""hueOffset"": {
""value"": {
""range"": {
""minimum"": -180.0,
""maximum"": 180.0
}
}
}
}
}
}";
Assert.AreEqual(expectedConfig, scenario.SerializeToJson());
}
[UnityTest]
public IEnumerator ScenarioConfigurationOverwrittenDuringDeserialization()
{
yield return CreateNewScenario(10, 10);

// Serialize some values
m_Scenario.constants = constants;
m_Scenario.Serialize();
var serializedConfig = m_Scenario.SerializeToJson();
m_Scenario.Deserialize();
m_Scenario.DeserializeFromJson(serializedConfig);
// Clean up serialized constants
File.Delete(m_Scenario.serializedConstantsFilePath);
yield return null;
}

正在加载...
取消
保存