def plotCrossCorrelation(events1, events2, **kwargs): if (len(events1['ts']) == 0) or (len(events2['ts']) == 0): return # use other library functions to do spatiotemporal cropping # If this is called from plotautocorrelation then this has already been done, # But do it here as well so that this function can be independent. events1 = cropSpace(events1, **kwargs) events1 = cropTime(events1, zeroTime=False, **kwargs) events2 = cropSpace(events2, **kwargs) events2 = cropTime(events2, zeroTime=False, **kwargs) boundaries, densities, centres, widths = defineBoundariesAndDensities( **kwargs) # Spatial cropping has happened; now iterate through defacto spatial range minX = min(np.min(events1['x']), np.min(events2['x'])) maxX = max(np.max(events1['x']), np.max(events2['x'])) minY = min(np.min(events1['y']), np.min(events2['y'])) maxY = max(np.max(events1['y']), np.max(events2['y'])) hist = np.zeros((len(densities)), dtype=np.float64) # TODO: This loop could be much more efficient, following the pattern in plotInterSpikeInterval, above for currX in trange(minX, maxX + 1, leave=True, position=0): for currY in range(minY, maxY + 1): ts1 = events1['ts'][np.logical_and(events1['x'] == currX, events1['y'] == currY)] ts2 = events2['ts'][np.logical_and(events2['x'] == currX, events2['y'] == currY)] if np.any(ts1) and np.any(ts2): hist = hist + crossCorrelation(ts1, ts2, boundaries, densities, **kwargs) # Normalise weightedSum = np.sum(hist / densities) if weightedSum > 0: hist = hist / np.sum(weightedSum) axes = kwargs.get('axes') if axes is None: fig, axes = plt.subplots() kwargs['axes'] = axes axes.bar(centres, hist, widths, antialiased=False) #axes.set_ylim(minX + minY*numX - 1, maxX + maxY*numX + 1) #plt.xticks(theRange, labels) title = kwargs.get('title', '') if kwargs.get('minX', False) or kwargs.get('maxX', False): title = title + ' ' + str(minX) + '<=X<=' + str(maxX) if kwargs.get('minY', False) or kwargs.get('maxY', False): title = title + ' ' + str(minY) + '<=Y<=' + str(maxY) axes.set_title(title) callback = kwargs.get('callback') if callback is not None: callback(**kwargs)
def plotInterSpikeInterval(inDict, **kwargs): # Boilerplate for descending container hierarchy if isinstance(inDict, list): for inDictInst in inDict: plotInterSpikeInterval(inDictInst, **kwargs) return if 'info' in inDict: # Top level container fileName = inDict['info'].get('filePathOrName', '') print('plotInterSpikeInterval was called for file ' + fileName) if not inDict['data']: print('The import contains no data.') return for channelName in inDict['data']: channelData = inDict['data'][channelName] if 'dvs' in channelData and len(channelData['dvs']['ts']) > 0: kwargs['title'] = ' '.join([fileName, str(channelName)]) plotInterSpikeInterval(channelData['dvs'], **kwargs) else: print('Channel ' + channelName + ' skipped because it contains no polarity data') return print('plotInterSpikeInterval working: ' + kwargs.get('title', 'unnamed')) # use other library functions to do spatiotemporal cropping inDict = cropSpace(inDict, **kwargs) inDict = cropTime(inDict, zeroTime=False, **kwargs) if kwargs.get('splitByPol', False): splitDict = splitByPolarity(inDict) fig, axes = plt.subplots(1, 3) kwargs['axes'] = axes[0] kwargs['title'] = 'on + off' print(kwargs['title']) plotInterSpikeIntervalSingle(inDict, **kwargs) kwargs['axes'] = axes[1] kwargs['title'] = 'off' print(kwargs['title']) plotInterSpikeIntervalSingle(splitDict['0'], **kwargs) kwargs['axes'] = axes[2] kwargs['title'] = 'on' print(kwargs['title']) plotInterSpikeIntervalSingle(splitDict['1'], **kwargs) else: axes = kwargs.get('axes') if axes is None: fig, axes = plt.subplots() kwargs['axes'] = axes kwargs['title'] = 'auto' plotInterSpikeIntervalSingle(inDict, **kwargs)
def plotInterSpikeIntervalSingle(events, **kwargs): if (len(events['ts']) == 0): return # use other library functions to do spatiotemporal cropping # If this is called from plotInterSpikeInterval then this has already been done, # But do it here as well so that this function can be independent. events = cropSpace(events, **kwargs) events = cropTime(events, zeroTime=False, **kwargs) kwargs['mirrored'] = False boundaries, densities, centres, widths = defineBoundariesAndDensities( **kwargs) # Spatial cropping has happened; now iterate through defacto spatial range minX = np.min(events['x']) maxX = np.max(events['x']) minY = np.min(events['y']) maxY = np.max(events['y']) hist = np.zeros((len(densities)), dtype=np.float64) for currX in trange(minX, maxX + 1, leave=True, position=0): currXLogical = events['x'] == currX tsForCurrX = events['ts'][currXLogical] yForCurrX = events['y'][currXLogical] for currY in range(minY, maxY + 1): tsXY = tsForCurrX[yForCurrX == currY] if np.any(tsXY): hist = hist + interSpikeInterval(tsXY, boundaries, densities, **kwargs) # Normalise weightedSum = np.sum(hist / densities) if weightedSum > 0: hist = hist / np.sum(weightedSum) axes = kwargs.get('axes') if axes is None: fig, axes = plt.subplots() kwargs['axes'] = axes axes.bar(centres, hist, widths, antialiased=False) #axes.set_ylim(minX + minY*numX - 1, maxX + maxY*numX + 1) #plt.xticks(theRange, labels) title = kwargs.get('title', '') if kwargs.get('minX', False) or kwargs.get('maxX', False): title = title + ' ' + str(minX) + '<=X<=' + str(maxX) if kwargs.get('minY', False) or kwargs.get('maxY', False): title = title + ' ' + str(minY) + '<=Y<=' + str(maxY) axes.set_title(title) callback = kwargs.get('callback') if callback is not None: callback(**kwargs)
def plotPose(inDicts, **kwargs): if isinstance(inDicts, list): for inDict in inDicts: plotPose(inDict, **kwargs) return else: inDict = inDicts if not isinstance(inDict, dict): return if 'ts' not in inDict: title = kwargs.pop('title', '') if 'info' in inDict and isinstance(inDict, dict): fileName = inDict['info'].get('filePathOrName') if fileName is not None: print('plotPose was called for file ' + fileName) title = (title + ' ' + fileName).lstrip() for key in inDict.keys(): kwargs['title'] = (title + ' ' + key).lstrip() plotPose(inDict[key], **kwargs) return # From this point onwards, it's a data-type container if 'point' not in inDict: return # From this point onwards, it's a pose or point data-type container ts = inDict['ts'] minTime = kwargs.get( 'minTime', kwargs.get('startTime', kwargs.get('beginTime', ts.min()))) maxTime = kwargs.get( 'maxTime', kwargs.get('stopTime', kwargs.get('endTime', ts.max()))) if minTime > ts.min() or maxTime < ts.max(): inDict = cropTime(inDict, minTime=minTime, maxTime=maxTime, zeroTime=False) ts = inDict['ts'] if 'rotation' in inDict: fig, allAxes = plt.subplots(2, 1) fig.suptitle(kwargs.get('title', '')) axesT = allAxes[0] axesR = allAxes[1] rotation = inDict['rotation'] axesR.plot(ts, rotation[:, 0], 'k') axesR.plot(ts, rotation[:, 1], 'r') axesR.plot(ts, rotation[:, 2], 'g') axesR.plot(ts, rotation[:, 3], 'b') axesR.legend(['r_w', 'r_x', 'r_y', 'r_z']) axesR.set_xlabel('Time (s)') axesR.set_ylabel('Quaternion components') axesR.set_ylim([-1, 1]) if 'point' in inDict: if not 'axesT' in locals(): fig, axesT = plt.subplots() if kwargs.get('zeroT', False): point = np.copy(inDict['point']) firstPoint = inDict['point'][0, :] point = point - firstPoint else: point = inDict['point'] axesT.plot(ts, point[:, 0], 'r') axesT.plot(ts, point[:, 1], 'g') axesT.plot(ts, point[:, 2], 'b') axesT.legend(['x', 'y', 'z']) axesT.set_xlabel('Time (s)') axesT.set_ylabel('Coords (m)') callback = kwargs.get('callback') if callback is not None: kwargs['axesT'] = axesT kwargs['axesR'] = axesR kwargs['minTime'] = minTime kwargs['maxTime'] = maxTime callback(**kwargs)
#%% Import realsense from importIntelRealsense import importIntelRealsense filePathOrName = os.path.join(prefix, '/data/2019_10_23_static/20191023_165520.bag') imported = importIntelRealsense(filePathOrName=filePathOrName) #%% MANIPULATION FUNCTIONS #%% Cropping a dataset to a desired time range from split import cropTime cropped = cropTime(imported, minTime=35, maxTime=38.5) #%% Cropping a dataset to a desired spatial range # works for dvs and derived data types 2020_01 doesn't yet work for frame datatype from split import cropSpace # This example takes all events with x in 9-19 inclusive, and with y in 0-9 inclusive cropped = cropSpace(imported, minX=9, maxX=19, maxY=9) #%% Splitting a dataset by labels from split import splitByLabel, selectByLabel # select your labelled data from an import (alternatively label it using some processing) labelledData = imported['data']['right']['dvslbl']