def _on_remote_graph(self, msg: bytes): in_time: float = time.time() try: scene: PEMTrafficScene = PEMTrafficScene.from_bytes(msg) obs: PEMTrafficSceneObservation = PEMTrafficSceneObservation(time.time(), scene, meta={'sender': int(self.ego_id)}) if self.alive and self.recording and self.remote_grid_sink: if OBS_GRAPH_REMOTE not in self.remote_grid_sink.accumulator: self.remote_grid_sink.push(OBS_GRAPH_REMOTE, [obs]) else: self.remote_grid_sink.accumulator[OBS_GRAPH_REMOTE].append(obs) self.timings.start('d6', custom_time=in_time) self.timings.stop('d6') except KeyError: return self.timings.start('d5', custom_time=scene.last_timestamp) self.timings.stop('d5', custom_time=in_time)
def on_graph(message: bytes): try: graph: PEMTrafficScene = PEMTrafficScene.from_bytes(message) graph_queue.append(graph2json(graph)) except: print('Failed to parse graph.')
def read_data( self, downsample_ground_truth: float = 1., downsample_observations: float = 1. ) -> Tuple[List[PEMTrafficSceneObservation], List[PEMTrafficSceneObservation], List[Ogtc]]: assert downsample_ground_truth <= 1 and downsample_observations <= 1 logging.debug( f'downsample_ground_truth={downsample_ground_truth}, downsample_observations={downsample_observations}' ) logging.debug('Reading directory info.') files_actual: List[str] = list( filter(lambda s: s.startswith(self.file_prefix), os.listdir(self.data_dir_actual))) files_observed: List[str] = list( filter(lambda s: s.startswith(self.file_prefix), os.listdir(self.data_dir_observed))) occupancy_ground_truth: List[Ogtc] = [] occupancy_observations_local: List[PEMTrafficSceneObservation] = [] occupancy_observations_remote: List[PEMTrafficSceneObservation] = [] logging.debug('Reading ground truth.') for file_name in files_actual: with open(os.path.join(self.data_dir_actual, file_name), 'rb') as f: try: occupancy_ground_truth += pickle.load(f) except EOFError: logging.warning(f'File {file_name} corrupt.') logging.debug( f'Got {len(occupancy_ground_truth)} ground truth data points.') logging.debug(f'Reading and decoding observations.') for file_name in files_observed: with open(os.path.join(self.data_dir_observed, file_name), 'rb') as f: try: if 'remote' not in file_name: occupancy_observations_local.extend(pickle.load(f)) else: data = pickle.load(f) assert isinstance(data, list) if len(data) < 1: continue if isinstance(data[0], RawBytesObservation): for obs in data: try: occupancy_observations_remote.append( PEMTrafficSceneObservation( timestamp=obs.timestamp, scene=PEMTrafficScene.from_bytes( obs.value), meta=obs.meta)) except KeyError: continue elif isinstance(data[0], PEMTrafficSceneObservation): occupancy_observations_remote.extend(data) except EOFError: logging.warning(f'File {file_name} corrupt.') logging.debug( f'Got {len(occupancy_observations_local)} local and {len(occupancy_observations_remote)} remote observations.' ) occupancy_ground_truth = sorted(random.sample( occupancy_ground_truth, k=math.floor( len(occupancy_ground_truth) * downsample_ground_truth)), key=attrgetter('ts')) occupancy_observations_local = sorted(random.sample( occupancy_observations_local, k=math.floor( len(occupancy_observations_local) * downsample_observations)), key=attrgetter('timestamp')) occupancy_observations_remote = sorted(random.sample( occupancy_observations_remote, k=math.floor( len(occupancy_observations_remote) * downsample_observations)), key=attrgetter('timestamp')) return occupancy_observations_local, occupancy_observations_remote, occupancy_ground_truth
PEMRelation[GridCellState](confidence=0.65, object=GridCellState.occupied()), 'occupant': PEMRelation[PEMDynamicActor](confidence=0.91, object=ego2) }), PEMGridCell( **{ 'hash': 2222, 'state': PEMRelation[GridCellState](confidence=0.88, object=GridCellState.free()), 'occupant': None }) ] grid1.cells = cells scene1: PEMTrafficScene = PEMTrafficScene() scene1.occupancy_grid = grid1 scene1.measured_by = ego1 encoded_msg: bytes = scene1.to_bytes() scene1: PEMTrafficScene = PEMTrafficScene.from_bytes(encoded_msg) print(scene1.measured_by.id) print(scene1.measured_by.position.object.x) print(scene1.occupancy_grid.cells[0].state.confidence) print(scene1.occupancy_grid.cells[0].state.object) print(scene1.occupancy_grid.cells[0].occupant.object.id)