Skip to content

Commit

Permalink
Merge pull request #55 from introlab/dev
Browse files Browse the repository at this point in the history
Added Matlab export.
  • Loading branch information
doumdi authored Mar 22, 2020
2 parents 870d838 + 6125803 commit 89058bf
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 45 deletions.
2 changes: 2 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ if(WIN32)
set (installer_args
${installer_args}
--hidden-import win32api
--hidden-import pkg_resources.py2_warn
--paths ${PYTHON_ENV_DIRECTORY}/Lib/site-packages/scipy/.libs # PyInstaller doesn't seem to find all required DLLs for SciPy...
--paths ${PYTHON_ENV_DIRECTORY}/Library/bin # PyInstaller doesn't seem to find sqlite3.dll...
)

# Build this target to make a package
Expand Down
4 changes: 2 additions & 2 deletions python/env/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ if(WIN32)
set (pip_packages
${pip_packages}
pypiwin32
# PyInstaller
https://github.com/pyinstaller/pyinstaller/tarball/develop
PyInstaller
# https://github.com/pyinstaller/pyinstaller/tarball/develop
)

set (conda_packages
Expand Down
46 changes: 31 additions & 15 deletions python/libopenimu/db/DBManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pickle
import sys
import warnings
import scipy.io as sio

# Basic definitions
from libopenimu.models.data_formats import DataFormat
Expand Down Expand Up @@ -561,8 +562,7 @@ def delete_processed_data(self, result):
raise

#####################
def export_csv(self, directory):
print('DBManager, export_csv in :', directory)
def export_file(self, file_format, directory):

groups = self.get_all_groups()

Expand All @@ -575,7 +575,7 @@ def export_csv(self, directory):
# Get all participants
participants = self.get_all_participants()
for participant in participants:
self.export_csv_participant(participant, group_dir)
self.export_file_participant(participant, file_format, group_dir)

else:
for group in groups:
Expand All @@ -586,9 +586,9 @@ def export_csv(self, directory):
# Get all participants
participants = self.get_participants_for_group(group)
for participant in participants:
self.export_csv_participant(participant, group_dir)
self.export_file_participant(participant, file_format, group_dir)

def export_csv_participant(self, participant : Participant, directory):
def export_file_participant(self, participant: Participant, file_format: str, directory):
if os.path.exists(directory):
participant_dir = directory + '/PARTICIPANT_ID_' + str(participant.id_participant) + '_' + \
participant.name + '/'
Expand All @@ -598,9 +598,9 @@ def export_csv_participant(self, participant : Participant, directory):
# Process all recordsets
records = self.get_all_recordsets(participant)
for record in records:
self.export_csv_recordset(participant, record, participant_dir)
self.export_file_recordset(participant, record, file_format, participant_dir)

def export_csv_recordset(self, participant : Participant, recordset : Recordset, directory):
def export_file_recordset(self, participant: Participant, recordset: Recordset, file_format: str, directory):
if os.path.exists(directory):
# Create recordset directory
record_dir = directory + 'RECORDSET_ID_' + str(recordset.id_recordset) + '_' + str(recordset.name) + '/'
Expand All @@ -614,16 +614,21 @@ def export_csv_recordset(self, participant : Participant, recordset : Recordset,
# Do something
all_data = self.get_all_sensor_data(recordset=recordset, sensor=sensor)
if sensor.id_sensor_type is not SensorType.GPS:
self.export_csv_sensor_data(sensor, all_data, record_dir)
self.export_file_sensor_data(sensor, all_data, file_format, record_dir)
else:
self.export_csv_sensor_data_gps(sensor, all_data, record_dir)
self.export_file_sensor_data_gps(sensor, all_data, file_format, record_dir)

def export_csv_sensor_data_gps(self, sensor: Sensor, sensors_data: list, directory):
def export_file_sensor_data_gps(self, sensor: Sensor, sensors_data: list, file_format, directory):

# GPS is stored as SIRF data structures.
from libopenimu.importers.wimu import GPSGeodetic

filename = directory + sensor.name + '.CSV'
filename = directory + sensor.name
if 'CSV' in file_format:
filename = filename + '.CSV'
elif 'Matlab' in file_format:
filename = filename + '.mat'

print('output to file : ', filename)

# Write CSV header
Expand All @@ -648,9 +653,12 @@ def export_csv_sensor_data_gps(self, sensor: Sensor, sensors_data: list, directo
# Save CSV
# print('dims:', my_array.shape)
# Write values
np.savetxt(filename, my_array, delimiter=";", header=header)
if 'CSV' in file_format:
np.savetxt(filename, my_array, delimiter=";", header=header)
elif 'Matlab' in file_format:
sio.savemat(filename, {sensor.name: my_array.transpose(), 'labels': header.split(';')}, do_compression=True)

def export_csv_sensor_data(self, sensor: Sensor, sensors_data: list, directory):
def export_file_sensor_data(self, sensor: Sensor, sensors_data: list, file_format, directory):
result = {}
for sensor_data in sensors_data:
if not result.__contains__(sensor_data.channel.id_channel):
Expand All @@ -659,7 +667,12 @@ def export_csv_sensor_data(self, sensor: Sensor, sensors_data: list, directory):
series = sensor_data.to_time_series()
result[sensor_data.channel.id_channel].append(series)

filename = directory + sensor.name + '.CSV'
filename = directory + sensor.name
if 'CSV' in file_format:
filename = filename + '.CSV'
elif 'Matlab' in file_format:
filename = filename + '.mat'

print('output to file : ', filename)

value_list = []
Expand All @@ -685,4 +698,7 @@ def export_csv_sensor_data(self, sensor: Sensor, sensors_data: list, directory):
my_array = np.array(value_list)
# print('dims:', my_array.shape)
# Write values
np.savetxt(filename, my_array.transpose(), delimiter=";", header=header)
if 'CSV' in file_format:
np.savetxt(filename, my_array.transpose(), delimiter=";", header=header)
elif 'Matlab' in file_format:
sio.savemat(filename, {sensor.name: my_array.transpose(), 'labels': header.split(';')}, do_compression=True)
19 changes: 11 additions & 8 deletions python/libopenimu/qt/ExportWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,29 @@ def directory_changed(self):
@pyqtSlot()
def export(self):
directory = self.UI.lineDir.text()
file_format = self.UI.comboFormat.currentText()

print('Should export in : ', directory)

class CSVExporter(WorkerTask):
def __init__(self, dbmanager, directory):
super().__init__('Exportation CSV', 0)
self.dbMan = dbmanager
self.directory = directory
class FileExporter(WorkerTask):
def __init__(self, _dbman, _format, _directory):
super().__init__('Exportation :' + _format, 0)
self.dbMan = _dbman
self.directory = _directory
self.format = _format

def process(self):
print('Exporting in :', self.directory)
self.dbMan.export_csv(directory)
self.dbMan.export_file(self.format, self.directory)
self.update_progress.emit(100)
print('Exporting done!')

exporter = CSVExporter(self.dbMan, directory)
exporter = FileExporter(self.dbMan, file_format, directory)

process = BackgroundProcess([exporter])

# Create progress dialog
dialog = ProgressDialog(process, 'CSV Export', self)
dialog = ProgressDialog(process, 'File Export to format: ' + file_format, self)
process.finished.connect(dialog.accept)
process.start()

Expand Down
Loading

0 comments on commit 89058bf

Please sign in to comment.