Skip to content

Commit

Permalink
fix tests for timeinterval vector query
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Niemeyer committed Jan 8, 2025
1 parent 94a32b7 commit a7d3128
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 31 deletions.
16 changes: 12 additions & 4 deletions examples/python/gRPC/images/gRPC_fb_queryImages.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
createPoint2d,
createPolygon2D,
createQuery,
createTimeInterval,
createTimeIntervalVector,
createTimeStamp,
getOrCreateProject,
)
Expand Down Expand Up @@ -92,9 +92,16 @@ def query_images(

polygon_2d = createPolygon2D(fbb, 700, -100, vertices)

time_min = createTimeStamp(fbb, 1610549273, 0)
time_max = createTimeStamp(fbb, 1938549273, 0)
time_interval = createTimeInterval(fbb, time_min, time_max)
time_min = []
time_max = []

time_min.append(createTimeStamp(fbb, 1661336500, 0))
time_min.append(createTimeStamp(fbb, 1661336560, 0))

time_max.append(createTimeStamp(fbb, 1661336550, 0))
time_max.append(createTimeStamp(fbb, 1661336600, 0))

time_intervals = createTimeIntervalVector(fbb, time_min, time_max)

project_uuids = [project_uuid]

Expand All @@ -116,6 +123,7 @@ def query_images(
fbb,
grpc_channel,
project_uuid,
timeIntervalVector=time_intervals,
# polygon2d=polygon_2d,
polygon2dSensorPos=polygon_2d,
labels=labelsCategory,
Expand Down
11 changes: 7 additions & 4 deletions examples/python/gRPC/images/gRPC_pb_queryImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from seerep.pb import meta_operations_pb2_grpc as metaOperations
from seerep.pb import point2d_pb2 as point2d
from seerep.pb import query_pb2 as query
from seerep.pb import time_interval_pb2 as timeInterval
from seerep.util.common import get_gRPC_channel


Expand Down Expand Up @@ -62,10 +63,12 @@ def query_images(
theQuery.polygon.vertices.extend(vertices)

# since epoche
theQuery.timeinterval.time_min.seconds = 1638549273
theQuery.timeinterval.time_min.nanos = 0
theQuery.timeinterval.time_max.seconds = 1938549273
theQuery.timeinterval.time_max.nanos = 0
time = timeInterval.TimeInterval()
time.time_min.seconds = 1638549273
time.time_min.nanos = 0
time.time_max.seconds = 1938549273
time.time_max.nanos = 0
theQuery.timeintervals.append(time)

# labels
labelsCategory = label_category_pb2.LabelCategory()
Expand Down
11 changes: 7 additions & 4 deletions examples/python/gRPC/images/gRPC_pb_queryImageGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from seerep.pb import meta_operations_pb2_grpc as metaOperations
from seerep.pb import point2d_pb2 as point2d
from seerep.pb import query_pb2 as query
from seerep.pb import time_interval_pb2 as timeInterval
from seerep.util.common import get_gRPC_channel

OFFSET = 0.5
Expand Down Expand Up @@ -42,10 +43,12 @@ def query_image_grid(
theQuery.projectuuid.append(target_project_uuid)

# since epoche
theQuery.timeinterval.time_min.seconds = 1638549273
theQuery.timeinterval.time_min.nanos = 0
theQuery.timeinterval.time_max.seconds = 1938549273
theQuery.timeinterval.time_max.nanos = 0
time = timeInterval.TimeInterval()
time.time_min.seconds = 1638549273
time.time_min.nanos = 0
time.time_max.seconds = 1938549273
time.time_max.nanos = 0
theQuery.timeintervals.append(time)

theQuery.polygon.z = -1
theQuery.polygon.height = 7
Expand Down
38 changes: 33 additions & 5 deletions examples/python/gRPC/util/fb_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def addToPointFieldVector(builder: Builder, pointFieldList: List[int]) -> int:

def createQuery(
builder: Builder,
timeInterval: Union[int, None] = None,
timeIntervalVector: Union[int, None] = None,
labels: Union[List[int], None] = None,
mustHaveAllLabels: bool = False,
projectUuids: List[str] = None,
Expand All @@ -647,8 +647,9 @@ def createQuery(
Args:
builder: A flatbuffers Builder
timeInterval: The pointer to a TimeInterval object representing the time
frame of the returned instances labels: A list of pointers to\
timeIntervalVector: The pointer to vector of TimeInterval objects
representing the time frames
labels: A list of pointers to\
[LabelsWithInstanceWithCategory](https://github.com/agri-gaia/seerep/blob/main/seerep_msgs/fbs/labels_with_instance_with_category.fbs)\
flatbuffers objects, which the instances should atleast have one of
mustHaveAllLabels: A boolean indicating if the returned instances should
Expand Down Expand Up @@ -714,8 +715,8 @@ def createQuery(
Query.AddPolygon(builder, polygon2d)
if polygon2dSensorPos:
Query.AddPolygonSensorPosition(builder, polygon2dSensorPos)
if timeInterval:
Query.AddTimeinterval(builder, timeInterval)
if timeIntervalVector:
Query.AddTimeintervals(builder, timeIntervalVector)
if labels:
Query.AddLabel(builder, labelOffset)
# no if; has default value
Expand Down Expand Up @@ -754,6 +755,33 @@ def createQueryInstance(builder: Builder, query: int, datatype: int) -> int:
return QueryInstance.End(builder)


def createTimeIntervalVector(
builder: Builder, timeMinList: List[int], timeMaxList: List[int]
) -> int:
"""
Create a vector of closed time intervals in flatbuffers.
Args:
builder: A flatbuffers Builder
timeMinList: The list of pointers to the Timestamp objects representing
the lower bound of the time of the intervals
timeMaxList: The list of pointers to the Timestamp objects representing
the upper bound of the time of the intervals
Returns:
A pointer to the constructed time interval vector object
"""
intervals = []
for timeMin, timeMax in zip(timeMinList, timeMaxList):
intervals.append(createTimeInterval(builder, timeMin, timeMax))

Query.StartTimeintervalsVector(builder, len(intervals))
for interval in intervals:
builder.PrependUOffsetTRelative(interval)

return builder.EndVector()


def createTimeInterval(builder: Builder, timeMin: int, timeMax: int) -> int:
"""
Create a closed time interval in flatbuffers.
Expand Down
33 changes: 24 additions & 9 deletions tests/python/gRPC/instances/test_gRPC_fb_getInstances.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,29 @@ def test_gRPC_getInstanceQueryTimeinterval(grpc_channel, project_setup):
min_time_ = cur_time - time_offset
max_time_ = cur_time + time_offset

query_builder = FbQuery(grpc_channel, enum_types={EnumFbQuery.TIMEINTERVAL})
query_builder = FbQuery(
grpc_channel, enum_types={EnumFbQuery.TIMEINTERVALVECTOR}
)
queryinst_builder = FbQueryInstance(
grpc_channel, enum_types={EnumFbQueryInstance.QUERY}
)

# test for time interval
min_timestamp = fbh.createTimeStamp(queryinst_builder.builder, min_time_, 0)
max_timestamp = fbh.createTimeStamp(queryinst_builder.builder, max_time_, 0)
min_timestamp = []
max_timestamp = []
min_timestamp.append(
fbh.createTimeStamp(queryinst_builder.builder, min_time_, 0)
)
max_timestamp.append(
fbh.createTimeStamp(queryinst_builder.builder, max_time_, 0)
)

img_time_interval = fbh.createTimeInterval(
img_time_interval = fbh.createTimeIntervalVector(
queryinst_builder.builder, min_timestamp, max_timestamp
)

query_builder.set_active_function(
EnumFbQuery.TIMEINTERVAL, lambda: img_time_interval
EnumFbQuery.TIMEINTERVALVECTOR, lambda: img_time_interval
)

queryinst_builder.set_active_function(
Expand All @@ -283,15 +291,22 @@ def test_gRPC_getInstanceQueryTimeinterval(grpc_channel, project_setup):
min_time_ = cur_time - 2 * time_offset
max_time_ = cur_time - time_offset

min_timestamp = fbh.createTimeStamp(queryinst_builder.builder, min_time_, 0)
max_timestamp = fbh.createTimeStamp(queryinst_builder.builder, max_time_, 0)
min_timestamp = []
max_timestamp = []

min_timestamp.append(
fbh.createTimeStamp(queryinst_builder.builder, min_time_, 0)
)
max_timestamp.append(
fbh.createTimeStamp(queryinst_builder.builder, max_time_, 0)
)

img_time_interval = fbh.createTimeInterval(
img_time_interval = fbh.createTimeIntervalVector(
queryinst_builder.builder, min_timestamp, max_timestamp
)

query_builder.set_active_function(
EnumFbQuery.TIMEINTERVAL, lambda: img_time_interval
EnumFbQuery.TIMEINTERVALVECTOR, lambda: img_time_interval
)

query_builder.assemble_datatype_instance()
Expand Down
21 changes: 16 additions & 5 deletions tests/python/gRPC/util/msg_abs/msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EnumFbQuery(FrozenEnum):
POLYGONSENSORPOSITION = auto() # def: None
FULLY_ENCAPSULATED = auto() # def: False
IN_MAP_FRAME = auto() # def: True
TIMEINTERVAL = auto() # def: None
TIMEINTERVALVECTOR = auto() # def: None
LABEL = auto() # def: None
SPARQL_QUERY = auto() # def: None
ONTOLOGY_URI = auto() # def: None
Expand Down Expand Up @@ -64,8 +64,9 @@ def _set_enum_func_mapping(self) -> Dict[EnumFbQuery, MsgsFunctions]:
EnumFbQuery.IN_MAP_FRAME: MsgsFunctions(
lambda: True, lambda: False
),
EnumFbQuery.TIMEINTERVAL: MsgsFunctions(
lambda: None, lambda: Dtypes.Fb.time_interval(self.builder)
EnumFbQuery.TIMEINTERVALVECTOR: MsgsFunctions(
lambda: None,
lambda: Dtypes.Fb.time_interval_vector(self.builder),
),
EnumFbQuery.LABEL: MsgsFunctions(
lambda: None, lambda: Dtypes.Fb.label_category(self.builder)
Expand Down Expand Up @@ -120,7 +121,7 @@ def _assemble_datatype_instance(self):
)
fully_encapsulated = self.get_component(EnumFbQuery.FULLY_ENCAPSULATED)
in_map_frame = self.get_component(EnumFbQuery.IN_MAP_FRAME)
timeinterval = self.get_component(EnumFbQuery.TIMEINTERVAL)
timeIntervalVector = self.get_component(EnumFbQuery.TIMEINTERVALVECTOR)
label = self.get_component(EnumFbQuery.LABEL)
must_have_all_labels = self.get_component(
EnumFbQuery.MUST_HAVE_ALL_LABELS
Expand All @@ -133,7 +134,7 @@ def _assemble_datatype_instance(self):

return fbh.createQuery(
self.builder,
timeInterval=timeinterval,
timeIntervalVector=timeIntervalVector,
labels=label,
mustHaveAllLabels=must_have_all_labels,
projectUuids=projectuuid,
Expand Down Expand Up @@ -215,6 +216,16 @@ def parameterized_polygon2d(
polygon_vertices.append(fbh.createPoint2d(builder, quad_extent, 0))
return fbh.createPolygon2D(builder, height, -100, polygon_vertices)

@classmethod
def time_interval_vector(cls, builder: Builder):
timeMin = []
timeMax = []
timeMin.append(fbh.createTimeStamp(builder, 1610549273, 0))
timeMin.append(fbh.createTimeStamp(builder, 1010549273, 0))
timeMax.append(fbh.createTimeStamp(builder, 1938549273, 0))
timeMax.append(fbh.createTimeStamp(builder, 1138549273, 0))
return fbh.createTimeIntervalVector(builder, timeMin, timeMax)

@classmethod
def time_interval(cls, builder: Builder) -> TimeInterval.TimeInterval:
timeMin = fbh.createTimeStamp(builder, 1610549273, 0)
Expand Down

0 comments on commit a7d3128

Please sign in to comment.