import os import numpy class DataReader: def __init__(self, timestamp = 'all'): self.accData = [] self.gyroData = [] self.magData = [] self.lightData = [] self.wifiData = [] self.pressureData = [] self.dataSetPath = os.path.abspath('./datasets/7-8Building') fileListAll = os.listdir(self.dataSetPath) self.filelist = [] if timestamp is not 'all': for file in fileListAll: if file.find(timestamp) is not -1: self.filelist.append(file) else: self.filelist = fileListAll def IMUSensorReader(self): for file in self.filelist: if file.find('ACCELEROMETER') is not -1: accFile = file elif file.find('GYROSCOPE') is not -1: gyroFile = file elif file.find('MAGNETICFIELD') is not -1: magFile = file self.accData = self.SensorDataReader(self.dataSetPath + '/' + accFile) self.accData = self.accData.astype(float) self.accData[:, 0] = self.accData[:, 0]/1000.0 self.gyroData = self.SensorDataReader(self.dataSetPath + '/' + gyroFile) self.gyroData = self.gyroData.astype(float) self.gyroData[:, 0] = self.gyroData[:, 0]/1000.0 self.magData = self.SensorDataReader(self.dataSetPath + '/' + magFile) self.magData = self.magData.astype(float) self.magData[:, 0] = self.magData[:, 0]/1000.0 self.SensorTimestampMatching() def SensorTimestampMatching(self): temp = self.accData[:,0] - self.accData[0,0] #convert absolute time to relative time to the starting point self.gyroData[:,0] = self.gyroData[:,0] - self.gyroData[0,0] #convert absolute time to relative time to the starting point self.magData[:,0] = self.magData[:,0] - self.magData[0,0] #convert absolute time to relative time to the starting point # convert the North-West-down to North-East-Down self.accData[:,1] = - self.accData[:,1] self.gyroData[:,1] = - self.gyroData[:,1] self.magData[:,1] = - self.magData[:,1] self.accData[:,3] = - self.accData[:,3] self.gyroData[:,3] = - self.gyroData[:,3] self.magData[:,3] = - self.magData[:,3] updatedGyro = np.zeros([len(self.accData), 3]) updatedMag = np.zeros([len(self.accData), 3]) for i in range(len(self.accData)): diffAccGyro = np.abs(self.gyroData[:,0] - temp[i]) diffAccMag = np.abs(self.magData[:,0] - temp[i]) indexGyr = np.argmin(diffAccGyro) indexMag = np.argmin(diffAccMag) updatedGyro[i,:] = self.gyroData[indexGyr, 1:4] updatedMag[i,:] = self.magData[indexMag, 1:4] self.gyroData = updatedGyro self.magData = updatedMag def LightSensorReader(self): for file in self.filelist: if file.find('LIGHT') is not -1: lightFile = file self.lightData = self.SensorDataReader(self.dataSetPath + '/' + lightFile) self.lightData = self.lightData.astype(float) self.lightData[:, 0] = self.lightData[:, 0]/1000.0 def PressureSensorReader(self): for file in self.filelist: if file.find('PRESSURE') is not -1: pressureFile = file self.pressureData = self.SensorDataReader(self.dataSetPath + '/' + pressureFile) self.pressureData = self.pressureData.astype(float) self.pressureData[:, 0] = self.pressureData[:, 0]/1000.0 def WiFiSensorReader(self): for file in self.filelist: if file.find('WIFI') is not -1: wifiFile = file tempWiFiData = self.SensorDataReader(self.dataSetPath + '/' + wifiFile) self.wifiData = self.WiFiDataProcessor(tempWiFiData) def SensorDataReader(self, sensorFile): fp = open(sensorFile) lines = fp.readlines() returnMatrix = [] for line in lines: line = line.strip() returnMatrix.append(line.split(',')) return np.array(returnMatrix) def WiFiDataProcessor(self, wifiData): lengthWiFiPacket = len(wifiData) returnWiFiPDRDataSet = [] for i in range(lengthWiFiPacket): lengthAPNumber = len(wifiData[i]) temp = [float(wifiData[i][0])/1000.0] macRSSI = {} for j in range(1, lengthAPNumber): tempAP = wifiData[i][j].split('_') macRSSI[tempAP[-5]] = tempAP[-4] temp.append(macRSSI) returnWiFiPDRDataSet.append(temp) return returnWiFiPDRDataSet