def test_properties(self): sensor = Sensor() my_id = 0 my_id_type = SensorType.GPS my_name = 'My Name' my_hw_name = 'HW Name' my_location = 'My Location' my_sampling_rate = 1.0 my_data_rate = 1 sensor.id_sensor = my_id sensor.id_sensor_type = my_id_type sensor.name = my_name sensor.hw_name = my_hw_name sensor.location = my_location sensor.sampling_rate = my_sampling_rate sensor.data_rate = my_data_rate self.assertEqual(sensor.id_sensor, my_id) self.assertEqual(sensor.id_sensor_type, my_id_type) self.assertEqual(sensor.name, my_name) self.assertEqual(sensor.hw_name, my_hw_name) self.assertEqual(sensor.location, my_location) self.assertEqual(sensor.sampling_rate, my_sampling_rate) self.assertEqual(sensor.data_rate, my_data_rate)
def test_to_time_series(self): sensordata = SensorData() sensordata.id_sensor_data = 1 recordset = Recordset() sensordata.recordset = recordset sensor = Sensor() sensor.sampling_rate = 5 sensor.data_rate = 1 sensordata.sensor = sensor channel = Channel() sensordata.channel = channel sensordata.start_timestamp = datetime.datetime.fromtimestamp(0) sensordata.stop_timestamp = datetime.datetime.fromtimestamp(0) sensordata.data = np.array([1, 2, 3, 4, 5], dtype=float) print(sensordata.to_time_series()) print(sensordata)
def test_get_all_channels(self): manager = DBManager(filename='openimu.db', overwrite=True) # Create sensor in DB sensor = manager.add_sensor(SensorType.ACCELEROMETER, 'Sensor Name', 'Hardware Name', 'Wrist', 30.0, 1) channelx = manager.add_channel(sensor, Units.GRAVITY_G, DataFormat.FLOAT32, 'Accelerometer_X') channely = manager.add_channel(sensor, Units.GRAVITY_G, DataFormat.FLOAT32, 'Accelerometer_Y') channelz = manager.add_channel(sensor, Units.GRAVITY_G, DataFormat.FLOAT32, 'Accelerometer_Z') # Get all channels (from all sensor) channels = manager.get_all_channels() self.assertEqual(len(channels), 3) # Get all channels (from valid sensor) channels = manager.get_all_channels(sensor=sensor) self.assertEqual(len(channels), 3) # Get all channels (from invalid sensor) channels = manager.get_all_channels(sensor=Sensor()) self.assertEqual(len(channels), 0) manager.close()
def test_ctor_no_args(self): sensor = Sensor() self.assertEqual(sensor.id_sensor, None) self.assertEqual(sensor.id_sensor_type, None) self.assertEqual(sensor.name, None) self.assertEqual(sensor.hw_name, None) self.assertEqual(sensor.location, None) self.assertEqual(sensor.sampling_rate, None) self.assertEqual(sensor.data_rate, None)
def test_to_time_series(self): sensordata = SensorData() sensordata.id_sensor_data = 1 recordset = Recordset() sensordata.recordset = recordset sensor = Sensor() sensor.sampling_rate = 5 sensor.data_rate = 1 sensordata.sensor = sensor channel = Channel() sensordata.channel = channel sensordata.data = np.array([1, 2, 3, 4, 5], dtype=float) sensordata.timestamps = SensorTimestamps() sensordata.timestamps.timestamps = np.array([0, 1, 2, 3, 4], dtype=np.float64) sensordata.timestamps.update_timestamps() print(sensordata.to_time_series()) print(sensordata)
def add_sensor(self, _id_sensor_type, _name, _hw_name, _location, _sampling_rate, _data_rate): # Create object sensor = Sensor( id_sensor_type=_id_sensor_type, name=_name, hw_name=_hw_name, location=_location, sampling_rate=_sampling_rate, data_rate=_data_rate) self.session.add(sensor) self.commit() return sensor
def test_properties(self): channel = Channel() channel.id_channel = 1 sensor = Sensor() channel.sensor = sensor # print(channel.sensor) channel.id_data_format = 0 channel.id_sensor_unit = 0 channel.label = 'My label' self.assertEqual(channel.id_channel, 1) self.assertEqual(channel.sensor, sensor) self.assertEqual(channel.id_data_format, 0) self.assertEqual(channel.id_sensor_unit, 0) self.assertEqual(channel.label, 'My label')
def test_properties(self): sensordata = SensorData() sensordata.id_sensor_data = 1 recordset = Recordset() sensordata.recordset = recordset sensor = Sensor() sensordata.sensor = sensor channel = Channel() sensordata.channel = channel sensordata.data_timestamp = 10 sensordata.data = [1, 2, 3, 4, 5] self.assertEqual(sensordata.id_sensor_data, 1) self.assertEqual(sensordata.recordset, recordset) self.assertEqual(sensordata.sensor, sensor) self.assertEqual(sensordata.channel, channel) self.assertEqual(sensordata.data_timestamp, 10) self.assertEqual(sensordata.data, [1, 2, 3, 4, 5])
def test_ctor_all_args(self): _id_sensor = 0 _id_sensor_type = 0 _name = 'My Name' _hw_name = 'HW Name' _location = 'wrist' _sampling_rate = 30.0 _data_rate = 1 sensor = Sensor(id_sensor=_id_sensor, id_sensor_type=_id_sensor_type, name=_name, hw_name=_hw_name, location=_location, sampling_rate=_sampling_rate, data_rate=_data_rate) self.assertEqual(_id_sensor, sensor.id_sensor) self.assertEqual(_id_sensor_type, sensor.id_sensor_type) self.assertEqual(_name, sensor.name) self.assertEqual(_hw_name, sensor.hw_name) self.assertEqual(_location, sensor.location) self.assertEqual(_sampling_rate, sensor.sampling_rate) self.assertEqual(_data_rate, sensor.data_rate)
def add_sensor(self, _id_sensor_type, _name, _hw_name, _location, _sampling_rate, _data_rate): # Check if that sensor is already present in the database query = self.session.query(Sensor).filter( (Sensor.id_sensor_type == _id_sensor_type) & (Sensor.location == _location) & (Sensor.name == _name) & (Sensor.hw_name == _hw_name) & (Sensor.sampling_rate == _sampling_rate) & (Sensor.data_rate) == _data_rate) if query.first(): # print("Sensor " + _name + " already present in DB!") return query.first() # Create object sensor = Sensor(id_sensor_type=_id_sensor_type, name=_name, hw_name=_hw_name, location=_location, sampling_rate=_sampling_rate, data_rate=_data_rate) self.session.add(sensor) self.commit() return sensor
def test_get_all_sensor_data_with_args(self): manager = DBManager(filename='openimu.db', overwrite=True, echo=False) # Create sensor in DB group = manager.update_group( Group(name='Group Name', description='Group Description')) participant = manager.update_participant( Participant(name='Participant Name', description='Participant Description', group=group)) sensor = manager.add_sensor(SensorType.ACCELEROMETER, 'Sensor Name', 'Hardware Name', 'Wrist', 30.0, 1) sensor2 = manager.add_sensor(SensorType.GYROMETER, 'Sensor Name', 'Hardware Name', 'Wrist', 30.0, 1) channel1 = manager.add_channel(sensor, Units.GRAVITY_G, DataFormat.FLOAT32, 'Accelerometer_X') channel2 = manager.add_channel(sensor, Units.GRAVITY_G, DataFormat.FLOAT32, 'Accelerometer_Y') timestamps = SensorTimestamps() timestamps.timestamps = np.zeros(40, dtype=np.float64) # will set start and end timestamps.update_timestamps() recordset = manager.add_recordset(participant, 'My Record', timestamps.start_timestamp, timestamps.end_timestamp) data = np.zeros(40, dtype=np.float32) sensordata = manager.add_sensor_data(recordset, sensor, channel1, timestamps, data) sensordata = manager.add_sensor_data(recordset, sensor, channel2, timestamps, data) manager.commit() # Test with no args, return everything in the recordset sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True) self.assertEqual(len(sensordata_res), 2) for sensor_data in sensordata_res: self.assertEqual(len(sensor_data.data), len(data)) # Test with a valid sensor arg sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True, sensor=sensor) self.assertEqual(len(sensordata_res), 2) for sensor_data in sensordata_res: self.assertEqual(len(sensor_data.data), len(data)) # Test with not the right sensor arg sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True, sensor=sensor2) self.assertEqual(len(sensordata_res), 0) # Testing with invalid sensor arg sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True, sensor=Sensor()) self.assertEqual(len(sensordata_res), 0) # Testing with channel1 sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True, channel=channel1) self.assertEqual(len(sensordata_res), 1) for sensor_data in sensordata_res: self.assertEqual(len(sensor_data.data), len(data)) # Testing with channel2 sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True, channel=channel2) self.assertEqual(len(sensordata_res), 1) for sensor_data in sensordata_res: self.assertEqual(len(sensor_data.data), len(data)) # Testing with invalid channel sensordata_res = manager.get_all_sensor_data(recordset=recordset, convert=True, channel=Channel()) self.assertEqual(len(sensordata_res), 0) manager.close()