예제 #1
0
    def test_add_recordset(self):
        manager = DBManager(filename='openimu.db', overwrite=True, echo=False)

        # Participant information
        group = manager.update_group(
            Group(name='My Group', description='My Group Description'))
        name = 'Participant Name'
        description = 'Participant Description'
        participant = manager.update_participant(
            Participant(name=name, description=description, group=group))
        # This gives datetime from seconds from epoch
        time1 = datetime.datetime.fromtimestamp(
            datetime.datetime.now().timestamp())
        time2 = datetime.datetime.fromtimestamp(
            datetime.datetime.now().timestamp() + 1)
        recordset = manager.add_recordset(participant, 'Record Name', time1,
                                          time2)
        recordset2 = manager.get_recordset(recordset.id_recordset)

        self.assertGreater(recordset.id_recordset, 0)
        self.assertEqual(recordset.participant, participant)
        self.assertEqual(recordset.name, 'Record Name')
        self.assertEqual(recordset.start_timestamp, time1)
        self.assertEqual(recordset.end_timestamp, time2)
        self.assertEqual(recordset, recordset2)

        manager.close()
예제 #2
0
    def test_add_sensor_data(self):
        manager = DBManager(filename='openimu.db', overwrite=True)

        # 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)
        channel = manager.add_channel(sensor, Units.GRAVITY_G,
                                      DataFormat.FLOAT32, 'Accelerometer_X')

        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, channel,
                                             timestamps, data)
        manager.commit()

        sensordata2 = manager.get_sensor_data(sensordata.id_sensor_data)
        self.assertEqual(sensordata, sensordata2)

        manager.close()
예제 #3
0
    def test_get_all_participants(self):
        manager = DBManager(filename='openimu.db', overwrite=True)
        # This will add participants

        # Participant information
        group = manager.update_group(
            Group(name='My Group', description='My Group Description'))
        name = 'Participant Name'
        description = 'Participant Description'

        participants = []

        # Multiple participants, all the same info...
        for i in range(0, 10):
            participants.append(
                manager.update_participant(
                    Participant(name=name,
                                description=description,
                                group=group)))

        # Read back list of participants
        all_participants = manager.get_participants_for_group(group)

        # Compare size
        self.assertEqual(len(participants), len(all_participants))

        # Compare content
        for i in range(0, len(participants)):
            self.assertEqual(participants[i], all_participants[i])

        manager.close()
예제 #4
0
    def test_get_all_recordsets(self):
        manager = DBManager(filename='openimu.db', overwrite=True)

        # Participant information
        group = manager.update_group(
            Group(name='My Group', description='My Group Description'))
        name = 'Participant Name'
        description = 'Participant Description'
        participant1 = manager.update_participant(
            Participant(name=name, description=description, group=group))
        participant2 = manager.update_participant(
            Participant(name=name, description=description, group=group))
        participant3 = manager.update_participant(
            Participant(name=name, description=description, group=group))

        count = 10
        recordsets1 = []
        recordsets2 = []

        # Adding recordsets
        for i in range(0, count):
            time1 = datetime.datetime.now()
            time2 = datetime.datetime.now()

            recordsets1.append(
                manager.add_recordset(participant1, 'Record Name' + str(i),
                                      time1, time2))
            recordsets2.append(
                manager.add_recordset(participant2, 'Record Name' + str(i),
                                      time1, time2))

        # Compare size
        all_from_participant_1 = manager.get_all_recordsets(participant1)
        all_from_participant_2 = manager.get_all_recordsets(participant2)
        self.assertEqual(len(all_from_participant_1), len(recordsets1))
        self.assertEqual(len(all_from_participant_2), len(recordsets2))
        self.assertEqual(len(manager.get_all_recordsets()),
                         len(recordsets1) + len(recordsets2))
        self.assertEqual(0, len(manager.get_all_recordsets(participant3)))

        # Compare contents
        for i in range(0, count):
            self.assertEqual(recordsets1[i], all_from_participant_1[i])
            self.assertEqual(recordsets2[i], all_from_participant_2[i])

        manager.close()
예제 #5
0
    def get_all_recordsets(self, participant=Participant()):

        if participant.id_participant is None:
            query = self.session.query(Recordset).order_by(asc(Recordset.start_timestamp))
            # print (query)
            return query.all()
        else:
            query = self.session.query(Recordset).filter(Recordset.id_participant == participant.id_participant).order_by(asc(Recordset.start_timestamp))
            return query.all()
예제 #6
0
 def save_clicked(self):
     if self.validate():
         if self.participant is None:
             self.participant = Participant()
         self.participant.name = self.UI.txtName.text()
         self.participant.description = self.UI.txtDesc.toPlainText()
         self.participant.id_group = self.UI.cmbGroups.currentData()
         self.participant = self.dbMan.update_participant(self.participant)
         self.enable_buttons(False)
         self.dataSaved.emit()
예제 #7
0
    def test_load(self):
        manager = DBManager('test.db', overwrite=True)
        participant = Participant(name='My Participant', description='Participant Description')
        manager.update_participant(participant)

        # Import to database
        importer = OpenIMUImporter(manager, participant)
        results = importer.load('../../../resources/samples/openimu_sample.oimu')
        # print('results', results)
        importer.import_to_database(results)
예제 #8
0
    def test_load_zip_file():
        # Testing

        manager = DBManager('applewatch.oi', overwrite=True, newfile=True)
        participant = Participant(name='My Participant', description='Participant Description')
        manager.update_participant(participant)
        importer = AppleWatchImporter(manager, participant)
        results = importer.load('/Users/dominic/WA/OpenIMU.git/python/applewatch_data.zip')
        # print('results', results)
        importer.import_to_database(results)
예제 #9
0
def import_data_from_wimu_file(filename):
    # This will create the database (or overwrite it)s

    manager = DBManager(db_filename, overwrite=True)
    participant = Participant(name='My Participant', description='Participant Description')
    manager.update_participant(participant)

    importer = WIMUImporter(manager, participant)
    # Load content of the file to the database
    results = importer.load(filename)
    importer.import_to_database(results)
예제 #10
0
    def test_load_data_file():
        # Testing

        manager = DBManager('applewatch.oi', overwrite=True)
        participant = Participant(name='My Participant', description='Participant Description')
        manager.update_participant(participant)
        importer = AppleWatchImporter(manager, participant)

        results = importer.load('/Users/dominic/Documents/working_area/OpenIMU.git/python/watch_ProcessedMotion.data')
        # print('results', results)
        importer.import_to_database(results)
    def test_load_zip_file(self):
        # Testing

        manager = DBManager('applewatch.db', overwrite=True)
        participant = Participant(name='My Participant',
                                  description='Participant Description')
        manager.update_participant(participant)
        importer = AppleWatchImporter(manager, participant)

        results = importer.load('../../../resources/samples/AppleWatch.zip')
        # print('results', results)
        importer.import_to_database(results)
예제 #12
0
    def test_loading(self):

        manager = DBManager('test.db', overwrite=True)
        participant = Participant(name='My Participant',
                                  description='Participant Description')
        manager.update_participant(participant)

        # Import to database
        importer = ActigraphImporter(manager, participant)
        results = importer.load('../../../resources/samples/test.gt3x')

        samples = 0
        for activity in results[1]['activity']:
            samples += 3 * len(activity[1])

        print('samples imported (each channels)', samples)

        importer.import_to_database(results)

        # Reload from database
        manager = DBManager('test.db')

        recordsets = manager.get_all_recordsets()
        self.assertGreater(len(recordsets), 0)

        loaded_samples = 0

        for record in recordsets:
            # Get all sensors in record
            sensors = manager.get_all_sensors()
            for sensor in sensors:
                if sensor.id_sensor_type == SensorType.ACCELEROMETER:
                    channels = manager.get_all_channels(sensor=sensor)
                    for channel in channels:
                        print('processing channel: ', channel)
                        self.assertEqual(channel.id_sensor, sensor.id_sensor,
                                         "id_sensor test for channel")
                        # Will get all data (converted to floats)
                        channel_data = manager.get_all_sensor_data(
                            recordset=record, convert=True, channel=channel)
                        print('channel_data_length', len(channel_data))

                        for sensor_data in channel_data:
                            self.assertEqual(sensor_data.id_channel,
                                             channel.id_channel,
                                             "id_channel test for data")
                            self.assertEqual(sensor_data.id_sensor,
                                             sensor.id_sensor,
                                             "id_sensor test data")
                            loaded_samples += len(sensor_data.data)

        self.assertEqual(samples, loaded_samples)
예제 #13
0
    def get_all_processed_data(self, participant=Participant()):

        datas = None
        if participant.id_participant is None:
            query = self.session.query(ProcessedData)
            datas = query.all()
        else:
            query = self.session.query(ProcessedData).filter(
                ProcessedData.processed_data_ref.recordset.participant.
                id_participant == participant.id_participant)
            datas = query.all()

        return datas
예제 #14
0
    def import_data():

        manager = DBManager(db_filename)

        # Create participant
        participant = manager.update_participant(Participant(name='Participant', description='No description'))

        # Create importer
        importer = ActigraphImporter(manager, participant)

        # Load content of the file to the database
        results = importer.load('../../resources/samples/test.gt3x')
        importer.import_to_database(results)
예제 #15
0
    def test_properties(self):
        record = Recordset()
        record.id_recordset = 3
        participant = Participant()
        record.participant = participant
        record.name = 'Record Name'
        record.start_timestamp = 22
        record.end_timestamp = 33

        self.assertEqual(record.id_recordset, 3)
        self.assertEqual(record.participant, participant)
        self.assertEqual(record.name, 'Record Name')
        self.assertEqual(record.start_timestamp, 22)
        self.assertEqual(record.end_timestamp, 33)
예제 #16
0
    def test_load(self):
        manager = DBManager('openimu.oi', overwrite=True, newfile=True)
        participant = Participant(name='My Participant',
                                  description='Participant Description')
        manager.update_participant(participant)

        # Import to database
        importer = OpenIMUImporter(manager, participant)
        results = importer.load(
            '../../../resources/samples/openimu_sample.oimu')
        # results = importer.load('/Users/dominic/Desktop/35.DAT')
        # print('results', results)
        importer.import_to_database(results)

        self.assertTrue(True)
예제 #17
0
    def test_loading(self):
        manager = DBManager('test.db', overwrite=True)
        participant = Participant(name='My Participant',
                                  description='Participant Description')
        manager.update_participant(participant)

        # Import to database
        importer = WIMUImporter(manager, participant)
        # results = importer.load('../../../resources/samples/WIMU_ACC_GPS_GYRO_PreProcess.zip')
        results = importer.load(
            '../../../resources/samples/REPAR_Sujet7_Semaine_T4.zip')
        importer.import_to_database(results)

        recordsets = manager.get_all_recordsets(participant)
        print('recordsets', recordsets)
        self.assertGreater(len(recordsets), 0)
예제 #18
0
    def test_load(self):
        manager = DBManager('openimu.oi', overwrite=True, newfile=True)
        participant = Participant(name='My Participant',
                                  description='Participant Description')
        manager.update_participant(participant)

        # Import to database
        importer = OpenIMUImporter(manager, participant)
        results = importer.load(
            '../../../resources/samples/openimu_sample.oimu')

        # results = importer.load('/Volumes/MINILOGGER/log_20190101_000034/record_20190101_000034.mdat')
        # print('results', results)
        importer.import_to_database(results)

        self.assertTrue(True)
예제 #19
0
    def get_all_recordsets(self, participant=Participant(), start_date=None):
        from sqlalchemy import func
        if start_date is not None:
            query = self.session.query(Recordset).filter(func.date(Recordset.start_timestamp) == start_date) \
                .order_by(asc(Recordset.start_timestamp))
            if participant.id_participant is not None:
                query = query.filter(
                    Recordset.id_participant == participant.id_participant)
            return query.all()

        if participant.id_participant is None:
            query = self.session.query(Recordset).order_by(
                asc(Recordset.start_timestamp))
            # print (query)
            return query.all()
        else:
            query = self.session.query(Recordset).filter(Recordset.id_participant == participant.id_participant) \
                .order_by(asc(Recordset.start_timestamp))
            return query.all()
예제 #20
0
    def test_add_participant(self):
        manager = DBManager(filename='openimu.db', overwrite=True)

        # Participant information
        group_name = 'My Group'
        group_description = 'My Group Description'
        group = Group(name=group_name, description=group_description)
        manager.update_group(group)

        participant_name = 'Participant'
        participant_description = 'Participant Description'
        participant = Participant(name=participant_name,
                                  description=participant_description,
                                  group=group)
        manager.update_participant(participant)
        participant2 = manager.get_participant(participant.id_participant)

        self.assertEqual(participant.group, group)
        self.assertEqual(participant.name, participant_name)
        self.assertEqual(participant.description, participant_description)
        self.assertGreater(participant.id_participant, 0)
        self.assertEqual(participant, participant2)
예제 #21
0
    def test_properties(self):
        id_participant = 1
        group = Group(id_group=1,
                      name='Group Name',
                      description='Group Description')
        name = 'Participant Name'
        description = 'Participant Description'

        participant = Participant()
        participant.id_participant = id_participant
        participant.group = group
        participant.name = name
        participant.description = description

        self.assertEqual(participant.id_participant, id_participant)
        self.assertEqual(participant.group, group)
        self.assertEqual(participant.name, name)
        self.assertEqual(participant.description, description)
예제 #22
0
    def test_async_loading(self):

        manager = DBManager('test.db', overwrite=True)
        participant = Participant(name='My Participant',
                                  description='Participant Description')
        manager.update_participant(participant)

        importer = ActigraphImporter(manager, participant)

        print('Starting threads...')
        # Start threads
        threads = []
        for i in range(0, 1):
            threads.append(
                importer.async_load('../../../resources/samples/test.gt3x'))

        print('Waiting for threads...')
        # Wait for threads
        for t in threads:
            t.join()

        recordsets = manager.get_all_recordsets(participant)
        print('recordsets', recordsets)
        self.assertGreater(len(recordsets), 0)
예제 #23
0
class ParticipantWindow(DataEditor):

    participant = Participant()
    dbMan = None

    def __init__(self,
                 dbManager,
                 participant=None,
                 parent=None,
                 default_group=None):
        super(QWidget, self).__init__(parent=parent)
        self.UI = Ui_frmParticipant()
        self.UI.setupUi(self)

        self.participant = participant
        self.dbMan = dbManager
        self.data_type = "participant"

        # Signals / Slots connections
        self.UI.btnCancel.clicked.connect(self.cancel_clicked)
        self.UI.btnSave.clicked.connect(self.save_clicked)
        self.UI.txtName.textEdited.connect(self.name_edited)
        self.UI.txtDesc.textChanged.connect(self.desc_edited)
        self.UI.cmbGroups.currentIndexChanged.connect(self.group_edited)

        # Load groups
        groups = self.dbMan.get_all_groups()
        self.UI.cmbGroups.clear()
        self.UI.cmbGroups.addItem("Aucun", userData=None)

        for group in groups:
            self.UI.cmbGroups.addItem(group.name, userData=group.id_group)

        # Update data
        self.update_data()

        # Set default group for new participants
        if default_group is not None:
            self.UI.cmbGroups.setCurrentIndex(
                self.UI.cmbGroups.findData(default_group.id_group,
                                           Qt.UserRole))

        self.enable_buttons(False)

    def validate(self):
        rval = True
        if self.UI.txtName.text() == '':
            self.UI.txtName.setStyleSheet('background-color: #ffcccc;')
            rval = False
        else:
            self.UI.txtName.setStyleSheet(
                'background-color: rgba(226, 226, 226, 90%);')

        if self.UI.cmbGroups.currentIndex == -1:
            rval = False

        return rval

    def update_data(self):
        if self.participant is not None:
            self.UI.txtName.setText(self.participant.name)
            self.UI.txtDesc.setPlainText(self.participant.description)
            """if self.participant.group is not None and self.participant.group.name is not None:
                self.UI.lblGroupValue.setText(self.participant.group.name)
            else:
                self.UI.lblGroupValue.setText("Aucun")
            """
            self.UI.cmbGroups.setCurrentIndex(
                self.UI.cmbGroups.findData(self.participant.id_group))
        else:
            self.UI.txtName.setText("")
            self.UI.txtDesc.setPlainText("")
            self.UI.cmbGroups.setCurrentIndex(0)

    def enable_buttons(self, enable):
        self.UI.btnCancel.setEnabled(enable or self.participant is None)
        self.UI.btnSave.setEnabled(enable)

    def update_modified_status(self):
        self.enable_buttons(
            (self.participant is not None
             and self.UI.txtName.text() != self.participant.name)
            or (self.participant is None and self.UI.txtName.text() != "") or
            (self.participant is not None
             and self.UI.txtDesc.toPlainText() != self.participant.description)
            or
            (self.participant is None and self.UI.txtDesc.toPlainText() != "")
            or
            (self.participant is not None
             and self.UI.cmbGroups.currentData() != self.participant.id_group))

    @pyqtSlot()
    def save_clicked(self):
        if self.validate():
            if self.participant is None:
                self.participant = Participant()
            self.participant.name = self.UI.txtName.text()
            self.participant.description = self.UI.txtDesc.toPlainText()
            self.participant.id_group = self.UI.cmbGroups.currentData()
            self.participant = self.dbMan.update_participant(self.participant)
            self.enable_buttons(False)
            self.dataSaved.emit()

    @pyqtSlot()
    def cancel_clicked(self):
        self.update_data()
        self.dataCancelled.emit()

    @pyqtSlot(str)
    def name_edited(self, new_value):
        self.update_modified_status()

    @pyqtSlot()
    def desc_edited(self):
        self.update_modified_status()

    @pyqtSlot()
    def group_edited(self):
        self.update_modified_status()
예제 #24
0
    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()