Skip to content

Commit

Permalink
Remove LUT default clamp values and deal with edge condition
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-f-cruz committed Aug 14, 2024
1 parent 6926b0c commit 34726cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 169 deletions.
12 changes: 0 additions & 12 deletions src/DataSchemas/aind_behavior_force_foraging/task_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,6 @@ class ForceLookUpTable(BaseModel):
right_max: float = Field(
..., description="The upper value of Right force used to linearly scale the input coordinate to."
)
left_min_bound_to: Optional[float] = Field(
None, description="The value given to Left < left_min. if null it will default to left_min."
)
left_max_bound_to: Optional[float] = Field(
None, description="The value given to Left > left_max. if null it will default to left_max."
)
right_min_bound_to: Optional[float] = Field(
None, description="The value given to Right < right_min. if null it will default to right_min."
)
right_max_bound_to: Optional[float] = Field(
None, description="The value given to Right > right_max. if null it will default to right_max."
)

@model_validator(mode="after")
def _validate_bounds(self) -> Self:
Expand Down
52 changes: 0 additions & 52 deletions src/DataSchemas/aind_force_foraging_task_logic.json
Original file line number Diff line number Diff line change
Expand Up @@ -654,58 +654,6 @@
"description": "The upper value of Right force used to linearly scale the input coordinate to.",
"title": "Right Max",
"type": "number"
},
"left_min_bound_to": {
"default": null,
"description": "The value given to Left < left_min. if null it will default to left_min.",
"oneOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"title": "Left Min Bound To"
},
"left_max_bound_to": {
"default": null,
"description": "The value given to Left > left_max. if null it will default to left_max.",
"oneOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"title": "Left Max Bound To"
},
"right_min_bound_to": {
"default": null,
"description": "The value given to Right < right_min. if null it will default to right_min.",
"oneOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"title": "Right Min Bound To"
},
"right_max_bound_to": {
"default": null,
"description": "The value given to Right > right_max. if null it will default to right_max.",
"oneOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"title": "Right Max Bound To"
}
},
"required": [
Expand Down
90 changes: 1 addition & 89 deletions src/Extensions/AindForceForagingTaskLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,14 +1533,6 @@ public partial class ForceLookUpTable

private double _rightMax;

private double? _leftMinBoundTo;

private double? _leftMaxBoundTo;

private double? _rightMinBoundTo;

private double? _rightMaxBoundTo;

public ForceLookUpTable()
{
}
Expand All @@ -1554,10 +1546,6 @@ protected ForceLookUpTable(ForceLookUpTable other)
_leftMax = other._leftMax;
_rightMin = other._rightMin;
_rightMax = other._rightMax;
_leftMinBoundTo = other._leftMinBoundTo;
_leftMaxBoundTo = other._leftMaxBoundTo;
_rightMinBoundTo = other._rightMinBoundTo;
_rightMaxBoundTo = other._rightMaxBoundTo;
}

/// <summary>
Expand Down Expand Up @@ -1680,78 +1668,6 @@ public double RightMax
}
}

/// <summary>
/// The value given to Left < left_min. if null it will default to left_min.
/// </summary>
[System.Xml.Serialization.XmlIgnoreAttribute()]
[Newtonsoft.Json.JsonPropertyAttribute("left_min_bound_to")]
[System.ComponentModel.DescriptionAttribute("The value given to Left < left_min. if null it will default to left_min.")]
public double? LeftMinBoundTo
{
get
{
return _leftMinBoundTo;
}
set
{
_leftMinBoundTo = value;
}
}

/// <summary>
/// The value given to Left > left_max. if null it will default to left_max.
/// </summary>
[System.Xml.Serialization.XmlIgnoreAttribute()]
[Newtonsoft.Json.JsonPropertyAttribute("left_max_bound_to")]
[System.ComponentModel.DescriptionAttribute("The value given to Left > left_max. if null it will default to left_max.")]
public double? LeftMaxBoundTo
{
get
{
return _leftMaxBoundTo;
}
set
{
_leftMaxBoundTo = value;
}
}

/// <summary>
/// The value given to Right < right_min. if null it will default to right_min.
/// </summary>
[System.Xml.Serialization.XmlIgnoreAttribute()]
[Newtonsoft.Json.JsonPropertyAttribute("right_min_bound_to")]
[System.ComponentModel.DescriptionAttribute("The value given to Right < right_min. if null it will default to right_min.")]
public double? RightMinBoundTo
{
get
{
return _rightMinBoundTo;
}
set
{
_rightMinBoundTo = value;
}
}

/// <summary>
/// The value given to Right > right_max. if null it will default to right_max.
/// </summary>
[System.Xml.Serialization.XmlIgnoreAttribute()]
[Newtonsoft.Json.JsonPropertyAttribute("right_max_bound_to")]
[System.ComponentModel.DescriptionAttribute("The value given to Right > right_max. if null it will default to right_max.")]
public double? RightMaxBoundTo
{
get
{
return _rightMaxBoundTo;
}
set
{
_rightMaxBoundTo = value;
}
}

public System.IObservable<ForceLookUpTable> Process()
{
return System.Reactive.Linq.Observable.Defer(() => System.Reactive.Linq.Observable.Return(new ForceLookUpTable(this)));
Expand All @@ -1770,11 +1686,7 @@ protected virtual bool PrintMembers(System.Text.StringBuilder stringBuilder)
stringBuilder.Append("left_min = " + _leftMin + ", ");
stringBuilder.Append("left_max = " + _leftMax + ", ");
stringBuilder.Append("right_min = " + _rightMin + ", ");
stringBuilder.Append("right_max = " + _rightMax + ", ");
stringBuilder.Append("left_min_bound_to = " + _leftMinBoundTo + ", ");
stringBuilder.Append("left_max_bound_to = " + _leftMaxBoundTo + ", ");
stringBuilder.Append("right_min_bound_to = " + _rightMinBoundTo + ", ");
stringBuilder.Append("right_max_bound_to = " + _rightMaxBoundTo);
stringBuilder.Append("right_max = " + _rightMax);
return true;
}

Expand Down
27 changes: 11 additions & 16 deletions src/Extensions/ParseForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public override IObservable<Force> Process(IObservable<Timestamped<short[]>> sou
LeftMax = forceLutSettings.LeftMax,
RightMin = forceLutSettings.RightMin,
RightMax = forceLutSettings.RightMax,
LeftMinBoundTo = forceLutSettings.LeftMinBoundTo.HasValue ? forceLutSettings.LeftMinBoundTo.Value : forceLutSettings.LeftMin,
LeftMaxBoundTo = forceLutSettings.LeftMaxBoundTo.HasValue ? forceLutSettings.LeftMaxBoundTo.Value : forceLutSettings.LeftMax,
RightMinBoundTo = forceLutSettings.RightMinBoundTo.HasValue ? forceLutSettings.RightMinBoundTo.Value : forceLutSettings.RightMin,
RightMaxBoundTo = forceLutSettings.RightMaxBoundTo.HasValue ? forceLutSettings.RightMaxBoundTo.Value : forceLutSettings.RightMax
},
LookUpTable = lookUpTable
);
Expand Down Expand Up @@ -156,10 +152,6 @@ public void Validate()
{
throw new ArgumentException("Minimum must be strictly lower than maximum.");
}
if (Limits.LeftMinBoundTo >= Limits.LeftMaxBoundTo || Limits.RightMinBoundTo >= Limits.RightMaxBoundTo)
{
throw new ArgumentException("The minimum bound must be less than the maximum bound.");
}
if (LookUpTable.Channels > 1)
{
throw new ArgumentException("Input matrix must have a single channel");
Expand All @@ -168,23 +160,23 @@ public void Validate()

public float LookUp(float leftValue, float rightValue)
{
leftValue = Rescale(leftValue, (float)Limits.LeftMin, (float)Limits.LeftMax, 0, LookUpTable.Size.Height);
rightValue = Rescale(rightValue, (float)Limits.RightMin, (float)Limits.RightMax, 0, LookUpTable.Size.Width);
leftValue = Rescale(leftValue, Limits.LeftMin, Limits.LeftMax, 0, LookUpTable.Size.Height);
rightValue = Rescale(rightValue, Limits.RightMin, Limits.RightMax, 0, LookUpTable.Size.Width);

leftValue = ClampValue(leftValue, (float)Limits.LeftMinBoundTo, (float)Limits.LeftMaxBoundTo);
rightValue = ClampValue(rightValue, (float)Limits.RightMinBoundTo, (float)Limits.RightMaxBoundTo);
leftValue = ClampValue(leftValue, Limits.LeftMin, Limits.LeftMax);
rightValue = ClampValue(rightValue, Limits.RightMin, Limits.RightMax);

return GetSubPixel(LookUpTable, leftValue, rightValue);
}

private static float Rescale(float value, float minFrom, float maxFrom, float minTo, float maxTo)
private static float Rescale(float value, double minFrom, double maxFrom, double minTo, double maxTo)
{
return (value - minFrom) / (maxFrom - minFrom) * (maxTo - minTo) + minTo;
return (float) ((value - minFrom) / (maxFrom - minFrom) * (maxTo - minTo) + minTo);
}

private static float ClampValue(float value, float MinBoundTo, float MaxBoundTo)
private static float ClampValue(float value, double MinBoundTo, double MaxBoundTo)
{
return Math.Min(Math.Max(value, MinBoundTo), MaxBoundTo);
return (float) Math.Min(Math.Max(value, MinBoundTo), MaxBoundTo);
}

private static float GetSubPixel(Mat src, float leftValue, float rightValue)
Expand All @@ -194,6 +186,9 @@ private static float GetSubPixel(Mat src, float leftValue, float rightValue)
float dL = leftValue - idxL;
float dR = rightValue - idxR;

idxL = Math.Min(idxL, src.Size.Height - 2);
idxR = Math.Min(idxR, src.Size.Width - 2);

var p00 = src[idxL, idxR];
var p01 = src[idxL, idxR + 1];
var p10 = src[idxL + 1, idxR];
Expand Down

0 comments on commit 34726cd

Please sign in to comment.