def SubPlotFeature(runData, axarr): magData = np.array(runData['magnetometer']) magData = magData - magData[0,:] # normalize based on the first row # magData = magData - magData[-1,:] # normalize based on the last value magDomain = magData[:,0] # first index is time, second is accuracy X = magData[:,2] Y = magData[:,3] Z = magData[:,4] mag = np.sqrt(X**2 + Y**2 + Z**2) magDomain = magDomain - magDomain[0] # put in same timescale X = magnetak_util.scale(X) Y = magnetak_util.scale(Y) Z = magnetak_util.scale(Z) mag = magnetak_util.scale(mag) row = 0 if len(runData['labels']) > 0 else 1 axarr[row, 0].plot(magDomain, X, alpha=0.2) axarr[row, 1].plot(magDomain, Y, alpha=0.2) axarr[row, 2].plot(magDomain, Z, alpha=0.2) axarr[row, 3].plot(magDomain, mag, alpha=0.2) if row == 0: axarr[row, 0].set_ylabel('True Positive') axarr[row, 0].set_title('X') axarr[row, 1].set_title('Y') axarr[row, 2].set_title('Z') axarr[row, 3].set_title('Magnitude') else: axarr[row, 0].set_ylabel('True Negative') axarr[row, 0].set_ylim(axarr[0, 0].get_ylim()) axarr[row, 1].set_ylim(axarr[0, 1].get_ylim()) axarr[row, 2].set_ylim(axarr[0, 2].get_ylim()) axarr[row, 3].set_ylim(axarr[0, 3].get_ylim())
def SumOfDifferences(self, domain, axis, template): domain = np.array(domain) domain = domain - domain[0] axis = magnetak_util.scale(axis) distances = [abs(data - template(t)) for data, t in zip(axis, domain) if t < self.window_size] return sum(distances) / len(distances)
def CosineSimilarity(self, domain, axis, template): domain = np.array(domain) domain = domain - domain[0] axis = magnetak_util.scale(axis) otherVect = [template(t) for t in domain if t < self.window_size] distance = scipy.spatial.distance.cosine(axis, otherVect) return distance
def PlotInterpolatedSnips(runDataList): fcns = [] for runData in runDataList: if len(runData['labels']) == 0: continue magData = np.array(runData['magnetometer']) magData = magData - magData[0,:] # normalize data based on first row # magData = magData - magData[-1,:] # normalize data based on last row magDomain = magData[:,0] # first index is time, second is accuracy X = magData[:,2] Y = magData[:,3] Z = magData[:,4] mag = np.sqrt(X**2 + Y**2 + Z**2) magDomain = magDomain - magDomain[0] # put in same timescale mag = magnetak_util.scale(mag) fcns.append(scipy.interpolate.interp1d(magDomain, mag, kind='cubic')) pl.plot(magDomain, mag, alpha=0.2) numFcns = float(len(fcns)) BigF = lambda x: sum([f(x) for f in fcns]) / numFcns newX = range(0,450000000,1000000) newY = [BigF(x) for x in newX] pl.plot(newX, newY, color='red')
def CosineSimilarity(self, domain, axis, template): domain = np.array(domain) domain = domain - domain[0] axis = magnetak_util.scale(axis) otherVect = [template(t) for t in domain if t < self.window_size] distance = scipy.spatial.distance.cosine(axis, otherVect) # features = [f if not np.isnan(f) else 0 for f in features] # return features return distance if not np.isnan(distance) else 0
def featurize(self, data): """This method should take in magnetometer data and output a feature vector""" segment_time_ns = self.segment_time * 1e6 # convert to nanoseconds window_size = segment_time_ns * 2 data = np.array(data) domain = data[:,0] # first index is time, second is accuracy # magData = magData[ domain < domain[0] + self.window_size ] segment1 = data[(domain <= domain[0] + segment_time_ns)] segment2 = data[(domain > domain[0] + segment_time_ns) & (domain <= domain[0] + window_size)] if len(segment1) == 0 or len(segment2) == 0: return np.array([0,0,0,0,0,0,0,0,0,0,0,0]) S0 = segment2[-1, 2:5] offsets1 = segment1[:, 2:5] - S0 offsets2 = segment2[:, 2:5] - S0 norms1 = [np.linalg.norm(row) for row in offsets1] norms2 = [np.linalg.norm(row) for row in offsets2] window = data[(domain <= domain[0] + window_size)] window = window - window[0,:] norms_scaled = [np.linalg.norm(row[2:5]) for row in window] # X = window[:,2] # Y = window[:,3] # Z = window[:,4] # magnitudes = np.sqrt(X**2 + Y**2 + Z**2) scaled_magnitudes = np.array(magnetak_util.scale(norms_scaled)) scaled_segment1 = np.array(scaled_magnitudes[(window[:,0] < segment_time_ns)]) scaled_segment2 = np.array(scaled_magnitudes[(window[:,0] > segment_time_ns) & (window[:,0] <= window_size)]) # print len(norms1), len(norms2) # print len(scaled_segment1), len(scaled_segment2) return np.array([ min(norms1), max(norms1), np.mean(norms1), min(norms2), max(norms2), np.mean(norms2), min(scaled_segment1), max(scaled_segment1), np.mean(scaled_segment1), min(scaled_segment2), max(scaled_segment2), np.mean(scaled_segment2), ])