浏览代码

incorporated PR feedback

- shouldCheckValidRange field excluded from JSON serialization. If the limits block is provided, it means range should be checked.
- added some checking and error messages for the cases where the user tries to provide or change the valid range for a scalar from the json config. this is not supported.
/0.9.0.preview.1_staging
Mohsen Kamalzadeh 3 年前
当前提交
dadcd7d8
共有 5 个文件被更改,包括 67 次插入42 次删除
  1. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs
  2. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs
  3. 13
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs
  4. 91
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs
  5. 1
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs

2
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs


/// </summary>
public void Validate()
{
Assert.IsTrue(!shouldCheckValidRange || value >= minAllowed && value <= maxAllowed);
CheckAgainstValidRange();
}
public void CheckAgainstValidRange()

2
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs


public void CheckAgainstValidRange()
{
if (shouldCheckValidRange)
if (shouldCheckValidRange && (range.minimum < minAllowed || range.maximum > maxAllowed))
{
Debug.LogError($"The provided min and max values for a {GetType().Name} exceed the allowed valid range. Clamping to valid range.");
range.minimum = Mathf.Clamp(range.minimum, minAllowed, maxAllowed);

13
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs


if (itemValue is Parameter)
newObj["param"] = JObject.FromObject(itemValue);
else
newObj["scalar"] = JObject.FromObject(itemValue);
newObj["scalar"] = JObject.FromObject(itemValue, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore });
output[itemKey] = newObj;
}
output.WriteTo(writer);

key = "normal";
else
throw new TypeAccessException($"Cannot serialize type ${options.defaultSampler.GetType()}");
output[key] = JObject.FromObject(options.defaultSampler);
output[key] = JObject.FromObject(options.defaultSampler, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore });
output.WriteTo(writer);
}

if (value.ContainsKey("str"))
scalar.value = new StringScalarValue { str = value["str"].Value<string>() };
else if (value.ContainsKey("num"))
scalar.value = new DoubleScalarValue { num = value["num"].Value<double>() };
{
Limits limits = null;
if (value.ContainsKey("limits"))
{
limits = value["limits"].ToObject<Limits>();
}
scalar.value = new DoubleScalarValue { num = value["num"].Value<double>(), limits = limits};
}
else if (value.ContainsKey("bool"))
scalar.value = new BooleanScalarValue { boolean = value["bool"].Value<bool>() };
else

91
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerializer.cs


samplerData.defaultSampler = new ConstantSampler
{
value = constantSampler.value,
limits = new Limits {
limits = constantSampler.shouldCheckValidRange? new Limits {
shouldValidateRange = constantSampler.shouldCheckValidRange
}
} : null
};
else if (sampler is Samplers.UniformSampler uniformSampler)
samplerData.defaultSampler = new UniformSampler

limits = new Limits {
limits = uniformSampler.shouldCheckValidRange? new Limits {
shouldValidateRange = uniformSampler.shouldCheckValidRange
}
} : null
};
else if (sampler is Samplers.NormalSampler normalSampler)
samplerData.defaultSampler = new NormalSampler

mean = normalSampler.mean,
stddev = normalSampler.standardDeviation,
limits = new Limits {
limits = normalSampler.shouldCheckValidRange? new Limits {
shouldValidateRange = normalSampler.shouldCheckValidRange
}
} : null
};
else
throw new ArgumentException($"Invalid sampler type ({sampler.GetType()})");

return new DoubleScalarValue
{
num = Convert.ToDouble(field.GetValue(obj)),
limits = new Limits {
limits = shouldCheckValidRange? new Limits {
shouldValidateRange = shouldCheckValidRange
}
} : null
};
}

return new Samplers.ConstantSampler
{
value = (float)constantSampler.value,
minAllowed = (float)constantSampler.limits.min,
maxAllowed = (float)constantSampler.limits.max,
shouldCheckValidRange = constantSampler.limits.shouldValidateRange
minAllowed = constantSampler.limits != null ? (float)constantSampler.limits.min : 0,
maxAllowed = constantSampler.limits != null ? (float)constantSampler.limits.max : 0,
shouldCheckValidRange = constantSampler.limits != null
};
if (samplerOption is UniformSampler uniformSampler)
return new Samplers.UniformSampler

minimum = (float)uniformSampler.min,
maximum = (float)uniformSampler.max,
},
minAllowed = (float)uniformSampler.limits.min,
maxAllowed = (float)uniformSampler.limits.max,
shouldCheckValidRange = uniformSampler.limits.shouldValidateRange
minAllowed = uniformSampler.limits != null ? (float)uniformSampler.limits.min : 0,
maxAllowed = uniformSampler.limits != null ? (float)uniformSampler.limits.max : 0,
shouldCheckValidRange = uniformSampler.limits != null
};
if (samplerOption is NormalSampler normalSampler)
return new Samplers.NormalSampler

},
mean = (float)normalSampler.mean,
standardDeviation = (float)normalSampler.stddev,
minAllowed = (float)normalSampler.limits.min,
maxAllowed = (float)normalSampler.limits.max,
shouldCheckValidRange = normalSampler.limits.shouldValidateRange
minAllowed = normalSampler.limits != null ? (float)normalSampler.limits.min : 0,
maxAllowed = normalSampler.limits != null ? (float)normalSampler.limits.max : 0,
shouldCheckValidRange = normalSampler.limits != null
};
throw new ArgumentException($"Cannot deserialize unsupported sampler type {samplerOption.GetType()}");
}

rangeAttribute = (RangeAttribute) rangeAttributes.First();
}
var value = ReadScalarValue(obj, scalar);
var readScalar = ReadScalarValue(obj, scalar);
var tolerance = 0.00001f;
if (value is double num && rangeAttribute != null && (num < rangeAttribute.min || num > rangeAttribute.max))
if (readScalar.Item1 is double num)
Debug.LogError($"The provided value for the field \"{field.Name}\" of \"{obj.GetType().Name}\" exceeds the allowed valid range. Clamping to valid range.");
var clamped = Mathf.Clamp((float)num, rangeAttribute.min, rangeAttribute.max);
field.SetValue(obj, Convert.ChangeType(clamped, field.FieldType));
if (rangeAttribute != null)
{
if (readScalar.Item2 != null &&
(Math.Abs(rangeAttribute.min - readScalar.Item2.max) > tolerance || Math.Abs(rangeAttribute.max - readScalar.Item2.max) > tolerance))
{
//the field has a range attribute and the json has a limits block for this field, but the numbers don't match
Debug.LogError($"The limits provided in the Scenario JSON for the field \"{field.Name}\" of \"{obj.GetType().Name}\" do not match this field's range set in the code. Ranges for scalar fields can only be set in code using the Range attribute and not from the Scenario JSON.");
}
else if (readScalar.Item2 == null)
{
//the field has a range attribute but the json has no limits block for this field
Debug.LogError($"The provided Scenario JSON specifies limits for the field \"{field.Name}\" of \"{obj.GetType().Name}\", while the field has no Range attribute in the code. Ranges for scalar fields can only be set in code using the Range attribute and not from the Scenario JSON.");
}
if (num < rangeAttribute.min || num > rangeAttribute.max)
{
Debug.LogError($"The provided value for the field \"{field.Name}\" of \"{obj.GetType().Name}\" exceeds the allowed valid range. Clamping to valid range.");
var clamped = Mathf.Clamp((float)num, rangeAttribute.min, rangeAttribute.max);
field.SetValue(obj, Convert.ChangeType(clamped, field.FieldType));
}
}
else
{
if (readScalar.Item2 != null)
//the field does not have a range attribute but the json has a limits block for this field
Debug.LogError($"The provided Scenario JSON specifies limits for the field \"{field.Name}\" of \"{obj.GetType().Name}\", but the field has no Range attribute in code. Ranges for scalar fields can only be set in code using the Range attribute and not from the Scenario JSON.");
}
field.SetValue(obj, Convert.ChangeType(value, field.FieldType));
field.SetValue(obj, Convert.ChangeType(readScalar, field.FieldType));
static void DeserializeScalarValue(object obj, PropertyInfo property, Scalar scalar)
{
//Properties cannot have a Range attribute like fields so no need to check for it here (see this function's overload for fields)
var value = ReadScalarValue(obj, scalar);
property.SetValue(obj, Convert.ChangeType(value, property.PropertyType));
}
static object ReadScalarValue(object obj, Scalar scalar)
static (object, Limits) ReadScalarValue(object obj, Scalar scalar)
Limits limits = null;
{
limits = doubleValue.limits;
}
return value;
return (value, limits);
}
#endregion

1
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs


{
public double min;
public double max;
public bool shouldValidateRange;
}
class Group

正在加载...
取消
保存