def getSpeedAfterShock(self, time = 20, startTime = 0, after = 25, absolute = False): "returns direction that the rat travelled 'after' points after shock" time = time * 60000 start = self.findStart(startTime) shocks = [content[0] for content in self.data[start:] if content[5] == 2 and content[1] < time] if shocks: selected = [shocks[0]] prev = shocks[0] for shock in shocks[1:]: if shock - prev > after: selected.append(shock) prev = shock else: return "NA" angles = [] cx, cy = self.centerX, self.centerY for shock in selected: x1, y1 = self.data[shock][self.indices] if len(self.data) <= shock + after: break x2, y2 = self.data[shock + after][self.indices] angle = ((degrees(atan2(x2 - cx, y2 - cy + 0.0000001)) - degrees(atan2(x1 - cx, y1 - cy + 0.0000001)) + 180) % 360) - 180 angles.append(angle) if absolute: return format(median([abs(angle) for angle in angles]), "0.2f") else: return format(median(angles), "0.2f")
def test_median(self): self.assertEqual(funcs.median(3, 4, 5, 7, 9), [5], "Median(3,4,5,7,9) should equal [5]") self.assertEqual(funcs.median(1), [1], "Median(1) should equal [1]") self.assertEqual(funcs.median(1, 1, 1, 1, 1, 1), [1, 1], "Median(1,1,1,1,1,1) should return [1,1]") self.assertEqual(funcs.median(1, 2, 2, 3, 4, 4), [2, 3], "Median(1,2,2,3,4,4) should return [2,3]")
def getRotationSpeed(self, time=20, startTime=0, rows=25): "returns speed of arena rotation in degrees per minute" start = self.findStart(startTime) end = self.findStart(time) cx, cy = self.centerX, self.centerY speeds = [] ax0, ay0 = self.data[start][7:9] t0, rx0, ry0 = self.data[start][1:4] for content in self.data[(start + rows):end:rows]: ax1, ay1 = content[7:9] t1, rx1, ry1 = content[1:4] arenaAngle = ( (degrees(atan2(ax1 - cx, ay1 - cy + 0.0000001)) - degrees( atan2(ax0 - cx, ay0 - cy + 0.0000001)) + 180) % 360) - 180 roomAngle = ( (degrees(atan2(rx1 - cx, ry1 - cy + 0.0000001)) - degrees( atan2(rx0 - cx, ry0 - cy + 0.0000001)) + 180) % 360) - 180 speeds.append(((roomAngle - arenaAngle) * 60000) / (t1 - t0)) ax0, ay0, t0, rx0, ry0 = ax1, ay1, t1, rx1, ry1 if speeds: return format(median(speeds), "0.1f") else: return "NA"
def getRotationSpeed(self, time = 20, startTime = 0, rows = 25): "returns speed of arena rotation in degrees per minute" start = self.findStart(startTime) end = self.findStart(time) cx, cy = self.centerX, self.centerY speeds = [] ax0, ay0 = self.data[start][7:9] t0, rx0, ry0 = self.data[start][1:4] for content in self.data[(start+rows):end:rows]: ax1, ay1 = content[7:9] t1, rx1, ry1 = content[1:4] arenaAngle = ((degrees(atan2(ax1 - cx, ay1 - cy + 0.0000001)) - degrees(atan2(ax0 - cx, ay0 - cy + 0.0000001)) + 180) % 360) - 180 roomAngle = ((degrees(atan2(rx1 - cx, ry1 - cy + 0.0000001)) - degrees(atan2(rx0 - cx, ry0 - cy + 0.0000001)) + 180) % 360) - 180 speeds.append(((roomAngle - arenaAngle) * 60000) / (t1 - t0)) ax0, ay0, t0, rx0, ry0 = ax1, ay1, t1, rx1, ry1 if speeds: return format(median(speeds), "0.1f") else: return "NA"
def getPeriodicity(self, time = 20, startTime = 0, minSpeed = 10, skip = 12, smooth = 2, minTime = 9, forGraph = False): """returns periodicity, which is computed as a median time between two consecutive moments when a rat was mobile - times where its speed was higher than minSpeed ... additionally only periods more than minTime seconds are counted in a result ... this parameter is probably useful only in cases where a rat is moving and learned the task properly // minTime may be a list of values """ time = time * 60000 start = self.findStart(startTime) result = [] if type(minTime) != list: minTime = [minTime] moves = [] for minT in minTime: t0 = startTime * 60000 x0, y0 = self.data[start][self.indices] minT *= 1000 speeds = deque() prev = False periods = [] for content in self.data[start+skip::skip]: if content[1] > time: break x1, y1 = content[self.indices] t1 = content[1] speeds.append((sqrt(((x1 - x0)**2 + (y1 - y0)**2)) / self.trackerResolution) / ((t1 - t0) / 1000)) if len(speeds) == smooth: if sum(speeds) / len(speeds) > minSpeed: if t0 - prev > minT and prev: periods.append(t0 - prev) if forGraph: moves.append((prev, t0)) prev = t1 speeds.popleft() x0, y0, t0 = x1, y1, t1 if len(periods) > 1: periodicity = format((median(periods) / 1000), "0.2f") else: periodicity = "NA" result.append(periodicity) if forGraph: return moves return "|".join(map(str, result))
def getSpeedAfterShock(self, time=20, startTime=0, after=25, absolute=False): "returns direction that the rat travelled 'after' points after shock" time = time * 60000 start = self.findStart(startTime) shocks = [ content[0] for content in self.data[start:] if content[self.shockIndex] == 2 and content[1] < time ] if shocks: selected = [shocks[0]] prev = shocks[0] for shock in shocks[1:]: if shock - prev > after: selected.append(shock) prev = shock else: return "NA" angles = [] cx, cy = self.centerX, self.centerY for shock in selected: x1, y1 = self.data[shock][self.indices] if len(self.data) <= shock + after: break x2, y2 = self.data[shock + after][self.indices] angle = ((degrees(atan2(x2 - cx, y2 - cy + 0.0000001)) - degrees( atan2(x1 - cx, y1 - cy + 0.0000001)) + 180) % 360) - 180 angles.append(angle) if absolute: return format(median([abs(angle) for angle in angles]), "0.2f") else: return format(median(angles), "0.2f")
def testMedian(self, mock_connect): # mock_pymysql.return_value = mock.Mock() # mock_pymysql.return_value.cursor.return_value = mock.Mock() mock_fetchall.return_value = [{ 'kf': 1 }, { 'kf': 2 }, { 'kf': 3 }, { 'kf': 4 }, { 'kf': 5 }] self.assertEqual(3, funcs.median('ri_cities', 'kf'))
def getSpeedAfterShock(self, time = 20, startTime = 0, after = 25): "returns median speed that the rat travelled 'after' points after shock" time = time * 60000 start = self.findStart(startTime) shocks = [content[0] for content in self.data[start:] if content[5] == 2 and content[1] < time] if shocks: selected = [shocks[0]] prev = shocks[0] for shock in shocks[1:]: if shock - prev > after: selected.append(shock) prev = shock else: return "NA" speeds = [] for shock in selected: if len(self.data) <= shock + after: break speeds.append(self._computeSpeed(self.data[shock], self.data[shock + after])) return format(median(speeds), "0.2f")
def getPeriodicity(self, time=20, startTime=0, minSpeed=10, skip=12, smooth=2, minTime=9, forGraph=False): """returns periodicity, which is computed as a median time between two consecutive moments when a rat was mobile - times where its speed was higher than minSpeed ... additionally only periods more than minTime seconds are counted in a result ... this parameter is probably useful only in cases where a rat is moving and learned the task properly // minTime may be a list of values """ time = time * 60000 start = self.findStart(startTime) result = [] if type(minTime) != list: minTime = [minTime] moves = [] for minT in minTime: t0 = startTime * 60000 x0, y0 = self.data[start][self.indices] minT *= 1000 speeds = deque() prev = False periods = [] for content in self.data[start + skip::skip]: if content[1] > time: break x1, y1 = content[self.indices] t1 = content[1] speeds.append((sqrt( ((x1 - x0)**2 + (y1 - y0)**2)) / self.trackerResolution) / ((t1 - t0) / 1000)) if len(speeds) == smooth: if sum(speeds) / len(speeds) > minSpeed: if t0 - prev > minT and prev: periods.append(t0 - prev) if forGraph: moves.append((prev, t0)) prev = t1 speeds.popleft() x0, y0, t0 = x1, y1, t1 if len(periods) > 1: periodicity = format((median(periods) / 1000), "0.2f") else: periodicity = "NA" result.append(periodicity) if forGraph: return moves return "|".join(map(str, result))
def test_median_subgoal(self): self.assertEqual(funcs.median(1, 2, 3, 4, 5, 6), [3, 4])