浏览代码

test json grabs the runtime platform dynamically so it doesnt fail outside osx

/0.9.0.preview.1_staging
Aryan Mann 3 年前
当前提交
9f3e917e
共有 4 个文件被更改,包括 279 次插入274 次删除
  1. 65
      com.unity.perception/Runtime/PerceptionAnalytics.cs
  2. 34
      com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs
  3. 230
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs
  4. 224
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/analyticsSerializationExample.json

65
com.unity.perception/Runtime/PerceptionAnalytics.cs


/// 1. Create a constant with the name of the event (eg: <see cref="k_EventScenarioInformation"/>)
/// 2. Add the constant to <see cref="allEvents" />
/// 3. Create a function that will report data for the event and at the start of it call
/// <see cref="TryRegisterEvent" /> with the event name defined in step 1.
/// <see cref="TryRegisterPerceptionAnalyticsEvent" /> with the event name defined in step 1.
enum AnalyticsEventType
{
Runtime,
Editor,
RuntimeAndEditor
}
struct AnalyticsEvent
{
public string name { get; private set; }
public AnalyticsEventType type { get; private set; }
public int versionId { get; private set; }
public string prefix { get; private set; }
public AnalyticsEvent(string name, AnalyticsEventType type, int versionId, string prefix = "")
{
this.name = name;
this.type = type;
this.versionId = versionId;
this.prefix = prefix;
// Make sure prefix is defined if the event is a runtime event
if (this.type == AnalyticsEventType.RuntimeAndEditor || this.type == AnalyticsEventType.Runtime)
{
Assert.IsTrue(!string.IsNullOrWhiteSpace(this.prefix));
}
}
}
const string k_VendorKey = "unity.perception";
const int k_MaxElementsInStruct = 100;

#region Event Definition
#region Event Definitions
static readonly AnalyticsEvent k_EventScenarioInformation = new AnalyticsEvent(
"perceptionScenarioInformation", AnalyticsEventType.RuntimeAndEditor, 1,
"ai.cv"

);
/// <see cref="TryRegisterEvent" />.
/// <see cref="TryRegisterPerceptionAnalyticsEvent" />.
/// </summary>
static IEnumerable<AnalyticsEvent> allEvents => new[]
{

/// </summary>
/// <param name="theEvent">The name of the event.</param>
/// <returns>Whether the event was successfully registered/</returns>
static bool TryRegisterEvent(AnalyticsEvent theEvent)
static bool TryRegisterPerceptionAnalyticsEvent(AnalyticsEvent theEvent)
{
if (!s_EventRegistrationStatus.ContainsKey(theEvent))
{

/// </summary>
/// <param name="theEvent">The analytics event.</param>
/// <param name="data">Payload of the event.</param>
static void SendEvent(AnalyticsEvent theEvent, object data)
static void SendPerceptionAnalyticsEvent(AnalyticsEvent theEvent, object data)
{
// Debug.Log($"Reporting {theEvent.name}.");
#if UNITY_EDITOR

IEnumerable<Randomizer> randomizers
)
{
try
{
if (!TryRegisterEvent(k_EventScenarioInformation))
return;
if (!TryRegisterPerceptionAnalyticsEvent(k_EventScenarioInformation))
return;
var data = ScenarioCompletedData.FromCameraAndRandomizers(cam, randomizers);
SendEvent(k_EventScenarioInformation, data);
}
catch
{
//ignored
}
var data = ScenarioCompletedData.FromCameraAndRandomizers(cam, randomizers);
SendPerceptionAnalyticsEvent(k_EventScenarioInformation, data);
}
#endregion

{
if (!TryRegisterEvent(k_EventRunInUnitySimulation))
if (!TryRegisterPerceptionAnalyticsEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData

runStatus = RunStatus.Started.ToString()
};
SendEvent(k_EventRunInUnitySimulation, data);
SendPerceptionAnalyticsEvent(k_EventRunInUnitySimulation, data);
if (!TryRegisterEvent(k_EventRunInUnitySimulation))
if (!TryRegisterPerceptionAnalyticsEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData

runStatus = RunStatus.Failed.ToString()
};
SendEvent(k_EventRunInUnitySimulation, data);
SendPerceptionAnalyticsEvent(k_EventRunInUnitySimulation, data);
if (!TryRegisterEvent(k_EventRunInUnitySimulation))
if (!TryRegisterPerceptionAnalyticsEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData

runStatus = RunStatus.Succeeded.ToString()
};
SendEvent(k_EventRunInUnitySimulation, data);
SendPerceptionAnalyticsEvent(k_EventRunInUnitySimulation, data);
}
#endregion

34
com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs


using System.Linq;
using System.Reflection;
using Unity.Simulation;
using UnityEngine.Assertions;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;

{
#region Common
enum AnalyticsEventType
{
Runtime,
Editor,
RuntimeAndEditor
}
struct AnalyticsEvent
{
public string name { get; private set; }
public AnalyticsEventType type { get; private set; }
public int versionId { get; private set; }
public string prefix { get; private set; }
public AnalyticsEvent(string name, AnalyticsEventType type, int versionId, string prefix = "")
{
this.name = name;
this.type = type;
this.versionId = versionId;
this.prefix = prefix;
// Make sure prefix is defined if the event is a runtime event
if (this.type == AnalyticsEventType.RuntimeAndEditor || this.type == AnalyticsEventType.Runtime)
{
Assert.IsTrue(!string.IsNullOrWhiteSpace(this.prefix));
}
}
}
#endregion
#region Run In Unity Simulation
[Serializable]

#endregion
#region Scenario Information
[Serializable]
public class PerceptionCameraData
{

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


// Scenario Completed Serialization
var scenarioCompletedData = ScenarioCompletedData.FromCameraAndRandomizers(perceptionCamera, randomizers);
var expectedRandomizerJson = RemoveWhitespace(((TextAsset)Resources.Load("analyticsSerializationExample")).text);
#region expectedRandomizerJson
var expectedRandomizerJson = RemoveWhitespace($@"
{{
""platform"": ""${Application.platform}"",
""perceptionCamera"": {{
""captureTriggerMode"": ""Scheduled"",
""startAtFrame"": 2,
""framesBetweenCaptures"": 10
}},
""labelers"": [
{{ ""name"": ""BoundingBox2DLabeler"", ""labelConfigCount"": 3, ""objectFilter"": """", ""animationPoseCount"": 0 }},
{{ ""name"": ""RenderedObjectInfoLabeler"", ""labelConfigCount"": 3, ""objectFilter"": """", ""animationPoseCount"": 0 }},
{{ ""name"": ""KeypointLabeler"", ""labelConfigCount"": 3, ""objectFilter"": ""Visible"", ""animationPoseCount"": 2 }}
],
""randomizers"": [
{{
""name"": ""AllMembersAndParametersTestRandomizer"",
""members"": [
{{
""name"": ""booleanMember"",
""value"": ""False"",
""type"": ""System.Boolean""
}},
{{
""name"": ""intMember"",
""value"": ""4"",
""type"": ""System.Int32""
}},
{{
""name"": ""uintMember"",
""value"": ""2"",
""type"": ""System.UInt32""
}},
{{
""name"": ""floatMember"",
""value"": ""5"",
""type"": ""System.Single""
}},
{{
""name"": ""vector2Member"",
""value"": ""(4.0, 7.0)"",
""type"": ""UnityEngine.Vector2""
}},
{{
""name"": ""unsupportedMember"",
""value"": ""UnityEngine.Perception.Randomization.Samplers.UniformSampler"",
""type"": ""UnityEngine.Perception.Randomization.Samplers.UniformSampler""
}}
],
""parameters"": [
{{
""name"": ""booleanParam"",
""type"": ""BooleanParameter"",
""fields"": [
{{
""name"": ""value"",
""distribution"": ""Constant"",
""value"": 1.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""floatParam"",
""type"": ""FloatParameter"",
""fields"": [
{{
""name"": ""value"",
""distribution"": ""AnimationCurve"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""integerParam"",
""type"": ""IntegerParameter"",
""fields"": [
{{
""name"": ""value"",
""distribution"": ""Uniform"",
""value"": 0.0,
""rangeMinimum"": -3.0,
""rangeMaximum"": 7.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""vector2Param"",
""type"": ""Vector2Parameter"",
""fields"": [
{{
""name"": ""x"",
""distribution"": ""Constant"",
""value"": 2.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""y"",
""distribution"": ""Uniform"",
""value"": 0.0,
""rangeMinimum"": -4.0,
""rangeMaximum"": 8.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""vector3Param"",
""type"": ""Vector3Parameter"",
""fields"": [
{{
""name"": ""x"",
""distribution"": ""Normal"",
""value"": 0.0,
""rangeMinimum"": -5.0,
""rangeMaximum"": 9.0,
""mean"": 4.0,
""stdDev"": 2.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""y"",
""distribution"": ""Constant"",
""value"": 3.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""z"",
""distribution"": ""AnimationCurve"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""vector4Param"",
""type"": ""Vector4Parameter"",
""fields"": [
{{
""name"": ""x"",
""distribution"": ""Normal"",
""value"": 0.0,
""rangeMinimum"": -5.0,
""rangeMaximum"": 9.0,
""mean"": 4.0,
""stdDev"": 2.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""y"",
""distribution"": ""Constant"",
""value"": 3.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""z"",
""distribution"": ""AnimationCurve"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""w"",
""distribution"": ""Uniform"",
""value"": 0.0,
""rangeMinimum"": -12.0,
""rangeMaximum"": 42.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""colorRgbCategoricalParam"",
""type"": ""ColorRgbCategoricalParameter"",
""fields"": [
{{
""name"": ""values"",
""distribution"": ""Categorical"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 3
}}
]
}}
]
}}
]
}}
");
#endregion
var actualRandomizerJson = RemoveWhitespace(JsonConvert.SerializeObject(scenarioCompletedData));
Assert.AreEqual(expectedRandomizerJson, actualRandomizerJson);

224
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/analyticsSerializationExample.json


{
"platform": "OSXEditor",
"perceptionCamera": {
"captureTriggerMode": "Scheduled",
"startAtFrame": 2,
"framesBetweenCaptures": 10
},
"labelers": [
{ "name": "BoundingBox2DLabeler", "labelConfigCount": 3, "objectFilter": "", "animationPoseCount": 0 },
{ "name": "RenderedObjectInfoLabeler", "labelConfigCount": 3, "objectFilter": "", "animationPoseCount": 0 },
{ "name": "KeypointLabeler", "labelConfigCount": 3, "objectFilter": "Visible", "animationPoseCount": 2 }
],
"randomizers": [
{
"name": "AllMembersAndParametersTestRandomizer",
"members": [
{
"name": "booleanMember",
"value": "False",
"type": "System.Boolean"
},
{
"name": "intMember",
"value": "4",
"type": "System.Int32"
},
{
"name": "uintMember",
"value": "2",
"type": "System.UInt32"
},
{
"name": "floatMember",
"value": "5",
"type": "System.Single"
},
{
"name": "vector2Member",
"value": "(4.0, 7.0)",
"type": "UnityEngine.Vector2"
},
{
"name": "unsupportedMember",
"value": "UnityEngine.Perception.Randomization.Samplers.UniformSampler",
"type": "UnityEngine.Perception.Randomization.Samplers.UniformSampler"
}
],
"parameters": [
{
"name": "booleanParam",
"type": "BooleanParameter",
"fields": [
{
"name": "value",
"distribution": "Constant",
"value": 1.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
}
]
},
{
"name": "floatParam",
"type": "FloatParameter",
"fields": [
{
"name": "value",
"distribution": "AnimationCurve",
"value": 0.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
}
]
},
{
"name": "integerParam",
"type": "IntegerParameter",
"fields": [
{
"name": "value",
"distribution": "Uniform",
"value": 0.0,
"rangeMinimum": -3.0,
"rangeMaximum": 7.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
}
]
},
{
"name": "vector2Param",
"type": "Vector2Parameter",
"fields": [
{
"name": "x",
"distribution": "Constant",
"value": 2.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
},
{
"name": "y",
"distribution": "Uniform",
"value": 0.0,
"rangeMinimum": -4.0,
"rangeMaximum": 8.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
}
]
},
{
"name": "vector3Param",
"type": "Vector3Parameter",
"fields": [
{
"name": "x",
"distribution": "Normal",
"value": 0.0,
"rangeMinimum": -5.0,
"rangeMaximum": 9.0,
"mean": 4.0,
"stdDev": 2.0,
"categoricalParameterCount": 0
},
{
"name": "y",
"distribution": "Constant",
"value": 3.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
},
{
"name": "z",
"distribution": "AnimationCurve",
"value": 0.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
}
]
},
{
"name": "vector4Param",
"type": "Vector4Parameter",
"fields": [
{
"name": "x",
"distribution": "Normal",
"value": 0.0,
"rangeMinimum": -5.0,
"rangeMaximum": 9.0,
"mean": 4.0,
"stdDev": 2.0,
"categoricalParameterCount": 0
},
{
"name": "y",
"distribution": "Constant",
"value": 3.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
},
{
"name": "z",
"distribution": "AnimationCurve",
"value": 0.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
},
{
"name": "w",
"distribution": "Uniform",
"value": 0.0,
"rangeMinimum": -12.0,
"rangeMaximum": 42.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 0
}
]
},
{
"name": "colorRgbCategoricalParam",
"type": "ColorRgbCategoricalParameter",
"fields": [
{
"name": "values",
"distribution": "Categorical",
"value": 0.0,
"rangeMinimum": 0.0,
"rangeMaximum": 0.0,
"mean": 0.0,
"stdDev": 0.0,
"categoricalParameterCount": 3
}
]
}
]
}
]
}
正在加载...
取消
保存