-
Notifications
You must be signed in to change notification settings - Fork 4.3k
write observations directly to protobuf #3229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,9 @@ public static class GrpcExtensions | |
/// Converts a AgentInfo to a protobuf generated AgentInfoActionPairProto | ||
/// </summary> | ||
/// <returns>The protobuf version of the AgentInfoActionPairProto.</returns> | ||
public static AgentInfoActionPairProto ToInfoActionPairProto(this AgentInfo ai, List<Observation> observations) | ||
public static AgentInfoActionPairProto ToInfoActionPairProto(this AgentInfo ai) | ||
{ | ||
var agentInfoProto = ai.ToAgentInfoProto(observations); | ||
var agentInfoProto = ai.ToAgentInfoProto(); | ||
|
||
var agentActionProto = new AgentActionProto | ||
{ | ||
|
@@ -36,7 +36,7 @@ public static AgentInfoActionPairProto ToInfoActionPairProto(this AgentInfo ai, | |
/// Converts a AgentInfo to a protobuf generated AgentInfoProto | ||
/// </summary> | ||
/// <returns>The protobuf version of the AgentInfo.</returns> | ||
public static AgentInfoProto ToAgentInfoProto(this AgentInfo ai, List<Observation> observations) | ||
public static AgentInfoProto ToAgentInfoProto(this AgentInfo ai) | ||
{ | ||
var agentInfoProto = new AgentInfoProto | ||
{ | ||
|
@@ -51,14 +51,6 @@ public static AgentInfoProto ToAgentInfoProto(this AgentInfo ai, List<Observatio | |
agentInfoProto.ActionMask.AddRange(ai.actionMasks); | ||
} | ||
|
||
if (observations != null) | ||
{ | ||
foreach (var obs in observations) | ||
{ | ||
agentInfoProto.Observations.Add(obs.ToProto()); | ||
} | ||
} | ||
|
||
return agentInfoProto; | ||
} | ||
|
||
|
@@ -197,5 +189,49 @@ public static ObservationProto ToProto(this Observation obs) | |
obsProto.Shape.AddRange(obs.Shape); | ||
return obsProto; | ||
} | ||
|
||
/// <summary> | ||
/// Generate an ObservationProto for the sensor using the provided WriteAdapter. | ||
/// This is equivalent to producing an Observation and calling Observation.ToProto(), | ||
/// but avoid some intermediate memory allocations. | ||
/// </summary> | ||
/// <param name="sensor"></param> | ||
/// <param name="writeAdapter"></param> | ||
/// <returns></returns> | ||
public static ObservationProto GetObservationProto(this ISensor sensor, WriteAdapter writeAdapter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a docstring on this ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
var shape = sensor.GetObservationShape(); | ||
ObservationProto observationProto = null; | ||
if (sensor.GetCompressionType() == SensorCompressionType.None) | ||
{ | ||
var numFloats = sensor.ObservationSize(); | ||
var floatDataProto = new ObservationProto.Types.FloatData(); | ||
// Resize the float array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, doesn't seem to be a way to set the Capacity on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, there is, but only in newer versions of the library: protocolbuffers/protobuf@da57400 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added code comment on this too. |
||
// TODO upgrade protobuf versions so that we can set the Capacity directly - see https://github.com/protocolbuffers/protobuf/pull/6530 | ||
for (var i = 0; i < numFloats; i++) | ||
{ | ||
floatDataProto.Data.Add(0.0f); | ||
} | ||
|
||
writeAdapter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0); | ||
sensor.Write(writeAdapter); | ||
|
||
observationProto = new ObservationProto | ||
{ | ||
FloatData = floatDataProto, | ||
CompressionType = (CompressionTypeProto)SensorCompressionType.None, | ||
}; | ||
} | ||
else | ||
{ | ||
observationProto = new ObservationProto | ||
{ | ||
CompressedData = ByteString.CopyFrom(sensor.GetCompressedObservation()), | ||
CompressionType = (CompressionTypeProto)sensor.GetCompressionType(), | ||
}; | ||
} | ||
observationProto.Shape.AddRange(shape); | ||
return observationProto; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not removed here because the other PR needs it, but this gets rid of the calls to GenerateSensorData