class Gait(FeatureExtractorAbstract): def __init__(self): self.dc = DistanceCalc() def getCSVheader(self): return ["gaitPeriodX", "gaitErrorX", "gaitPeriodY", "gaitErrorY", "gaitPeriodZ", "gaitErrorZ"] def extract(self, experiment, type, indiv): filepath = experiment[2] + os.path.sep + PathConfig.traceFolderNormal + os.path.sep + indiv[0] + ".trace" if not os.path.isfile(filepath): filepath = experiment[2] + os.path.sep + PathConfig.traceFoldersAlt[type] + os.path.sep + indiv[ 0] + ".trace" if not os.path.isfile(filepath): return ['NA'] * 3 with open(filepath) as fh: xs = [] ys = [] zs = [] for line in fh: linesplit = line.split("\t") if not self.dc.isValidLine(linesplit): linesplit = line.split(" ") if not self.dc.isValidLine(linesplit): continue xs.append(linesplit[-3]) ys.append(linesplit[-2]) zs.append(linesplit[-1]) xs = map(float,xs) ys = map(float,ys) zs = map(float,zs) xPeriod, xError = self._getPeriod(xs) yPeriod, yError = self._getPeriod(ys) zPeriod, zError = self._getPeriod(zs) return xPeriod, xError, yPeriod, yError, zPeriod, zError @staticmethod def _getPeriod(signal): if len(signal) == 0: return 'NA' signal = np.array(signal) fft = np.fft.rfft(signal).real fft = fft[:len(fft) / 2 + 1] fft[1:] = fft[1:] / (len(signal)/2) fft[0] = fft[0]/len(signal) period = np.argmax(fft[1:]) + 1 period_value = fft[1:].max() linspace = np.linspace(0,len(signal), len(signal)) mse = np.average(signal - (period_value * np.sin(period*linspace+np.average(signal)))**2) return period, mse
def extract(self, experiment, type, indiv): errorReturnVal = ['NA'] * 2 filepath = experiment[2] + os.path.sep + PathConfig.traceFolderNormal + os.path.sep + indiv[0] + ".trace" dc = DistanceCalc() if not os.path.isfile(filepath): filepath = experiment[2] + os.path.sep + PathConfig.traceFoldersAlt[type] + os.path.sep + indiv[ 0] + ".trace" if not os.path.isfile(filepath): return errorReturnVal with open(filepath, 'r') as inputFile: traceLines = [] lines = [] for line in inputFile: lines.append(line) if len(lines) == 110: break if len(lines) < 20: return errorReturnVal for line in lines[-10:]: lineSplit = line.split("\t") if not dc.isValidLine(lineSplit): lineSplit = line.split(" ") if not dc.isValidLine(lineSplit): continue traceLines.append( (float(lineSplit[2]), float(lineSplit[2])) ) if len(traceLines) < 6: return errorReturnVal self._setPoints(traceLines[0], traceLines[3], traceLines[5]) self._getCenter() self._getRadius() self._getCircumference() dist = dc.distanceStep(filepath, "euclidean") percentCircle = dist / self.circumference return [self.radius, percentCircle]
def extract(self, experiment, type, indiv): filepath = experiment[2] + os.path.sep + PathConfig.traceFolderNormal + os.path.sep + indiv[0] + ".trace" dc = DistanceCalc() if not os.path.isfile(filepath): filepath = ( experiment[2] + os.path.sep + PathConfig.traceFoldersAlt[type] + os.path.sep + indiv[0] + ".trace" ) if not os.path.isfile(filepath): return ["NA"] with open(filepath, "r") as inputFile: monotonyUp = 0 monotonyDown = 0 monotonyLeft = 0 monotonyRight = 0 firstLine = True xy = (0.0, 0.0) for line in inputFile: lineSplit = line.split("\t") if not dc.isValidLine(lineSplit): lineSplit = line.split(" ") if not dc.isValidLine(lineSplit): continue if firstLine: firstLine = False xy = (lineSplit[2], lineSplit[3]) else: xyNew = (lineSplit[2], lineSplit[3]) if xyNew[1] > xy[1]: monotonyUp += 1 if xyNew[1] < xy[1]: monotonyDown += 1 if xyNew[0] > xy[0]: monotonyRight += 1 if xyNew[0] < xy[0]: monotonyLeft += 1 xy = xyNew return [monotonyRight - monotonyLeft, monotonyUp - monotonyDown]