def _group_by_identifier(sensor_grids): """Group sensor grids or views if they have the same full identifier.""" group_func = lambda grid: grid.full_identifier # noqa: E731 ordered_sensor_grids = sorted(sensor_grids, key=group_func) # check if there is any duplicated identifiers ids = {grid.full_identifier for grid in sensor_grids} if len(list(ids)) == len(sensor_grids): # there is no duplicated identifier - return the original list return sensor_grids updated_grids = [] for group_identifier, grids in itertools.groupby(ordered_sensor_grids, group_func): grids = list(grids) if len(grids) > 1: # merge grids into one sensors = [] for grid in grids: sensors.extend(grid.sensors) joined_grid = SensorGrid(grids[0].identifier, sensors) joined_grid.group_identifier = grids[0].group_identifier updated_grids.append(joined_grid) else: updated_grids.append(grids[0]) return updated_grids
def test_dict_to_object_sensor_grid(): """Test the dict_to_object method with SensorGrid objects.""" sensors = [Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1))] sg_obj = SensorGrid('sg_1', sensors) sg_dict = sg_obj.to_dict() new_sg = dict_to_object(sg_dict) assert isinstance(new_sg, SensorGrid)
def _load_grids(self) -> None: """Load sensor grids.""" if self._sensor_grids_option == SensorGridOptions.Ignore: return if hasattr(self._hb_model.properties, 'radiance'): # list of unique sensor_grid identifiers in the model ids = set([ grid.identifier for grid in self._hb_model.properties.radiance.sensor_grids ]) # if all the grids have the same identifier, merge them into one grid if len(ids) == 1: id = self._hb_model.properties.radiance.sensor_grids[ 0].identifier sensors = [ sensor for grid in self._hb_model.properties.radiance.sensor_grids for sensor in grid.sensors ] sensor_grid = SensorGrid(id, sensors) self._sensor_grids.data.append( convert_sensor_grid(sensor_grid, self._sensor_grids_option)) # else add them as separate grids else: for sensor_grid in self._hb_model.properties.radiance.sensor_grids: self._sensor_grids.data.append( convert_sensor_grid(sensor_grid, self._sensor_grids_option))
def test_creation(): sg = SensorGrid('sg_1', sensors) str(sg) # test string representation hash(sg) # test hashability assert sg.identifier == 'sg_1' assert len(sg) == 2 assert sg[0] == sensors[0] assert sg[1] == sensors[1]
def test_move(): sensor = Sensor((0, 0, 10), (0, 0, -1)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.move(pv.Vector3D(10, 20, 30)) assert sensor.pos == (10, 20, 40) assert sensor.dir == (0, 0, -1)
def test_assigning_group(): sg = SensorGrid('sg_1', sensors) sg.group_identifier = 'floor_1/dining_room' str(sg) # test string representation hash(sg) # test hashability assert sg.identifier == 'sg_1' assert sg.group_identifier == 'floor_1/dining_room' assert len(sg) == 2 assert sg[0] == sensors[0] assert sg[1] == sensors[1]
def test_scale(): sensor = Sensor((1, 0, 2), (1, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.scale(2) assert round(sensor.pos[0]) == 2 assert round(sensor.pos[1]) == 0 assert round(sensor.pos[2]) == 4 assert round(sensor.dir[0]) == 1 assert round(sensor.dir[1]) == 0 assert round(sensor.dir[2]) == 0
def test_reflect(): sensor = Sensor((1, 0, 2), (2, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.reflect(Plane(pv.Vector3D(1, 0, 0), pv.Point3D(0, 0, 0))) assert round(sensor.pos[0]) == -1 assert round(sensor.pos[1]) == 0 assert round(sensor.pos[2]) == 2 assert round(sensor.dir[0]) == -2 assert round(sensor.dir[1]) == 0 assert round(sensor.dir[2]) == 0
def test_rotate_xy(): sensor = Sensor((1, 0, 2), (2, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.rotate_xy(90, pv.Point3D(0, 0, 0)) assert round(sensor.pos[0]) == 0 assert round(sensor.pos[1]) == 1 assert round(sensor.pos[2]) == 2 assert round(sensor.dir[0]) == 0 assert round(sensor.dir[1]) == 2 assert round(sensor.dir[2]) == 0
def test_rotate(): sensor = Sensor((0, 0, 0), (1, 0, 0)) sensors = [ Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1)), sensor ] sg = SensorGrid('sg_1', sensors) sg.rotate(90, pv.Vector3D(0, 1, 1), pv.Point3D(0, 0, 20)) assert round(sensor.pos[0], 3) == -14.142 assert round(sensor.pos[1]) == -10 assert round(sensor.pos[2]) == 10 assert round(sensor.dir[0], 1) == 0.0 assert round(sensor.dir[1], 3) == 0.707 assert round(sensor.dir[2], 3) == -0.707
def test_to_and_from_dict(): sg = SensorGrid('sg', sensors) sg_dict = sg.to_dict() assert sg_dict == { 'type': 'SensorGrid', 'identifier': 'sg', 'sensors': [{ 'pos': (0, 0, 0), 'dir': (0, 0, 1) }, { 'pos': (0, 0, 10), 'dir': (0, 0, 1) }] } sensor_from = SensorGrid.from_dict(sg_dict) assert sensor_from == sg assert sg_dict == sensor_from.to_dict()
def test_invalid_input(): with pytest.raises(ValueError): SensorGrid('sg_1', ((0, 0, 0, 0, 0, 1), ))
def test_updating_values(): sg = SensorGrid('sg_1', sensors) # sensor is immutable - hence no assignment with pytest.raises(TypeError): sg.sensors[0] = Sensor((0, 0, 100), (0, 0, -10))
def test_to_radiance(): sg = SensorGrid('sg', sensors) sg.to_radiance() == \ """0.0 0.0 0.0 0.0 0.0 1.0\n0.0 0.0 10.0 0.0 0.0 1.0"""
def sensor_grid_simple(directory): sensors = [Sensor((0, 0, 0), (0, 0, 1)), Sensor((0, 0, 10), (0, 0, 1))] sg = SensorGrid('sg_1', sensors) dest_file = os.path.join(directory, 'sensor_grid_simple.json') with open(dest_file, 'w') as fp: json.dump(sg.to_dict(), fp, indent=4)