#!/usr/bin/python3 import pandas as pd import numpy as np from glob import glob import re def natural_keys(text): # use a regex to sort in natural order return [int(text) if text.isdigit() else text.lower() for text in re.split('([0-9]+)', text) ] class Parser: # obtain the list of files # read them # normalize them # assign them to the correct gesture (according to filename) def __init__(self, dataDir): self.wFiles, self.xFiles, self.yFiles, self.zFiles = self.get_files(dataDir); # sort lists of file IN PLACE # needed for assign() self.wFiles.sort(key=natural_keys) self.xFiles.sort(key=natural_keys) self.yFiles.sort(key=natural_keys) self.zFiles.sort(key=natural_keys) def _read_(self, csvFile): dfile = pd.read_csv(csvFile, delimiter=',', header=None) return dfile # called on __init__ # obtain all files (recursively, even if not needed) def get_files(self, dDir): wExpr = dDir + "/W/*.csv" xExpr = dDir + "/X/*.csv" yExpr = dDir + "/Y/*.csv" zExpr = dDir + "/Z/*.csv" wFiles = glob(wExpr) xFiles = glob(xExpr) yFiles = glob(yExpr) zFiles = glob(zExpr) return wFiles, xFiles, yFiles, zFiles #### min - max utilities (easier than pandas way) def _min_(self, dframe): minCol = dframe.min() res = minCol[0] for num in minCol: if res > num: res = num return res def _max_(self, dframe): maxCol = dframe.max() res = maxCol[0] for num in maxCol: if res < num: res = num return res #### end of min-max utilities # normalize in [-1,1] included # normalizing a file here # this should be called for each file def normalize(self, csvFile): dfile = self._read_(csvFile) minimum = self._min_(dfile) maximum = self._max_(dfile) # apply a lambda to normalize in [-1,1] normDfile = dfile.apply(lambda x: ((x - minimum) / (maximum - minimum))*2 - 1) # print(normDfile, normDfile.max(), normDfile.min()) return normDfile # assign to each gesture the corresponding files # (according to readme) def assign(self, files): assignedFiles1 = [] assignedFiles2 = [] assignedFiles3 = [] for i in range(0,93): if (i < 31): assignedFiles1.append(files[i]) elif (i < 62): assignedFiles2.append(files[i]) else: assignedFiles3.append(files[i]) return assignedFiles1,assignedFiles2,assignedFiles3