def __call__(self, filename): """ Process the file """ print(f"ax3_seconds_stats.py processing {filename}") # Count number of lines in file to get array dimension print("Count lines in file") count = 0 with open(filename, "rt", newline="\n") as self.fh: line = self.fh.readline().strip() while line: row = Row(line) if row.skip: pass else: count += 1 if count % 1000000 == 0: print(f"{count} lines counted") line = self.fh.readline().strip() self.filename = filename self.timestamp = np.array(["(empty)" for _ in range(count)]) self.epoch = np.zeros((count,)) self.x = np.zeros((count,)) self.y = np.zeros((count,)) self.z = np.zeros((count,)) self.tot = np.zeros((count,)) print("Read file") self.firstLine = None with open(filename, "rt", newline="\n") as self.fh: line = self.fh.readline().strip() index = 0 while line: row = Row(line) if row.skip: pass else: if self.firstLine is None: self.firstLine = row.timestamp self.timestamp[index] = row.timestamp self.epoch[index] = row.getEpoch() self.x[index] = row.val[0] self.y[index] = row.val[1] self.z[index] = row.val[2] self.tot[index] = row.getTotAcc() index += 1 if index % 1000000 == 0: print(f"{index} data lines read") line = self.fh.readline().strip()
def __call__(self, filename): """ Process the file """ # Count number of valid lines in file to get array dimension count = 0 with open(filename, "rt", newline="\n") as self.fh: line = self.fh.readline().strip() while line: row = Row(line, decode=False) if row.skip: pass else: count += 1 if count % 1000000 == 0: print(f"{count} lines counted") line = self.fh.readline().strip() self.filename = filename self.timestamp = np.array(["(empty)" for _ in range(count)]) self.epoch = np.zeros((count,)) self.x = np.zeros((count,)) self.y = np.zeros((count,)) self.z = np.zeros((count,)) self.tot = np.zeros((count,)) rowAbove8 = [0,0,0] rowAbove7 = [0,0,0] rowAbove6 = [0,0,0] rowAbove1 = [0,0,0] self.firstLine = None with open(filename, "rt", newline="\n") as self.fh: line = self.fh.readline().strip() index = 0 while line: row = Row(line) if row.skip: pass else: if self.firstLine is None: self.firstLine = row.timestamp self.timestamp[index] = row.timestamp self.epoch[index] = row.getEpoch() self.x[index] = row.val[0] self.y[index] = row.val[1] self.z[index] = row.val[2] self.tot[index] = row.getTotAcc() index += 1 if index % 1000000 == 0: print(f"{index} lines read") for axis in range(len(row.val)): if abs(row.val[axis]) >= 8: rowAbove8[axis] = rowAbove8[axis] + 1 if abs(row.val[axis]) >= 7: rowAbove7[axis] = rowAbove7[axis] + 1 if abs(row.val[axis]) >= 6: rowAbove6[axis] = rowAbove6[axis] + 1 if abs(row.val[axis]) >= 1: rowAbove1[axis] = rowAbove1[axis] + 1 line = self.fh.readline().strip() for axis in range(len(row.val)): # size of x is the same as y and z proportion = rowAbove8[axis]/self.x.size print(f"axis {axis}, +/-8 or more {rowAbove8[axis]} times") print(f"axis {axis}, +/-7 or more {rowAbove7[axis]} times") print(f"axis {axis}, +/-6 or more {rowAbove6[axis]} times") print(f"axis {axis}, +/-1 or more {rowAbove1[axis]} times") print() outputFilename = self.makeOutFile(filename, self.firstLine) outfile = open(outputFilename, "w") with outfile: writer = csv.writer(outfile) for index in range(self.epoch.size): writer.writerow([self.epoch[index], self.x[index], self.y[index], self.z[index], self.tot[index]]) return outputFilename
def __call__(self, filename, threshold): """ Process the file """ # Count number of seconds in the file count = 0 firstEpoch = None lastEpoch = None with open(filename, "rt", newline="\n") as self.fh: line = self.fh.readline().strip() while line: row = Row(line) if row.skip: pass elif firstEpoch is None: firstEpoch = row.getEpoch() line = self.fh.readline().strip() lastEpoch = row.getEpoch() secondsInFile = int(lastEpoch) - int(firstEpoch) + 1 days = round(secondsInFile / 86400, 1) print( f"File contains {days} days ({secondsInFile} seconds) worth of data" ) epochTimestamps = np.zeros(secondsInFile) maxAccPerSecond = np.zeros(secondsInFile) firstSecondEpoch = int(firstEpoch) # Now scan through the file and pick out the highest acceleration # in each second with open(filename, "rt", newline="\n") as self.fh: line = self.fh.readline().strip() while line: row = Row(line) if row.skip: pass else: second = int(row.getEpoch()) - firstSecondEpoch epochTimestamps[second] = int(row.getEpoch()) totalAcc = row.getTotAcc() if totalAcc > maxAccPerSecond[second]: maxAccPerSecond[second] = totalAcc line = self.fh.readline().strip() print(f"Max per second accelerations extracted") aboveThresholdValues = np.copy(maxAccPerSecond) # Now scan through looking for movement above a threshold for second in range(len(aboveThresholdValues)): if aboveThresholdValues[second] < threshold: aboveThresholdValues[second] = 0 print(f"Determining periods of movement") # Scan through the above threshold values to set +/-5 minutes to # 1 when there is an above threshold value inUse = np.zeros(secondsInFile) for second in range(len(aboveThresholdValues)): if aboveThresholdValues[second] > 0: # Set the previous 5 * 60 values, less one, to 1 backIndex = second - ((4 * 60) + 59) for index in range(backIndex, second): inUse[index] = 1 # Set the next 5 * 60 values to 1 for index in range(second, second + (5 * 60)): aboveThresholdValues[second] = 1 plotFilename, outputFilename = self.makeOutFile(filename, threshold) self._plot(plotFilename, threshold, epochTimestamps, maxAccPerSecond, aboveThresholdValues, inUse) # Output this as a data file outfile = open(outputFilename, "w") with outfile: writer = csv.writer(outfile) for second in range(len(aboveThresholdValues)): writer.writerow([ int(firstEpoch) + second, second, round(maxAccPerSecond[second], 6), aboveThresholdValues[second], inUse[second] ]) return [plotFilename, outputFilename]