def test_unroll(self): stream = fstream([(10,12,0.5,'a'), (14,15,1.2,'b')], fields=['start','end','score','name']) expected = [(0,),(0.5,'a'),(0.5,'a'),(0,),(0,),(1.2,'b'),(0,)] res = list(unroll(stream,(9,16))) self.assertListEqual(res, expected) stream = fstream([(0,1,5),(1,2,9),(2,3,11)], fields=['start','end','score']) expected = [(5,),(9,),(11,)] res = list(unroll(stream,(0,3))) self.assertListEqual(res, expected)
def test_unroll(self): stream = fstream([(10, 12, 0.5, 'a'), (14, 15, 1.2, 'b')], fields=['start', 'end', 'score', 'name']) expected = [(0, ), (0.5, 'a'), (0.5, 'a'), (0, ), (0, ), (1.2, 'b'), (0, )] res = list(unroll(stream, (9, 16))) self.assertListEqual(res, expected) stream = fstream([(0, 1, 5), (1, 2, 9), (2, 3, 11)], fields=['start', 'end', 'score']) expected = [(5, ), (9, ), (11, )] res = list(unroll(stream, (0, 3))) self.assertListEqual(res, expected)
def correlation(trackList, regions, limits=(-1000,1000), with_acf=False): """ Calculates the cross-correlation between two streams and returns a vector containing the correlation at each lag in this order (L/R for resp. limits[0], limits[1]): [L,L+1,...,R-1,R]. If more than two tracks are given in *trackList*, returns a list of correlation vectors, one for every distinct pair of tracks. If `with_acf` is True, self-correlations will also be included in the list. A negative lag indicates that track 2 is shifted to the right w.r.t track 1, a positive lag - to the left. So to get the correlation at lag +4, one has to look at the 4-L th element of the array; for lag -4, at the -4-L th element. Example:: |_____ /^\ _________| lag 0 |______________/^\__| |_____ /^\ _________| lag -8 -> |______________/^\__| |_____ /^\ _________| lag +8 |______________/^\__| <- :param trackList: list of FeatureStream objects :param regions: a tuple (start,end) or a FeatureStream with the bounds of the regions to consider (see `unroll`). In the latter case, all regions will be concatenated. :param limits: (tuple (int,int)) maximum lag to consider. [-1000,1000] :param with_acf: (bool) include auto-correlations. [False] :rtype: list of floats, or list of lists of floats. """ ##### One could profit from numpy to reduce the memory space used for ##### storing these - long - arrays ('dtype' is float64 by default). if isinstance(regions,FeatureStream): _reg = list(regions) x = [array([s[0] for s in unroll(t,FeatureStream(_reg,fields=regions.fields))]) for t in trackList] _reg = [] else: x = [array([s[0] for s in unroll(t,regions)]) for t in trackList] if limits[1]-limits[0] > 2*len(x[0]): limits = (-len(x[0])+1,len(x[0])-1) N0 = len(x[0])+limits[1]-limits[0]-1 ##### convert to a power of 2, fft gets orders of magnitude faster... N = 2**int(log(max(1,N0/200.0),2)+8.5) #2**int(log(2+N0,2)+.5) if N < N0: x = [vec_reduce(t[:N]) for t in x] else: x = [vec_reduce(t) for t in x] def _corr(x1,x2,N): corr = ifft(conjugate(fft(x1,N))*fft(x2,N))/len(x1) corr = ncat((corr[N+limits[0]:], corr[:limits[1]+1])) return real(corr) if with_acf: return [[_corr(x1,x2,N) for x2 in x[n:]] for n,x1 in enumerate(x)] elif len(trackList) == 2: return _corr(x[0],x[1],N) else: return [[_corr(x1,x2,N) for x2 in x[n+1:]] for n,x1 in enumerate(x[:-1])]
def correlation(trackList, regions, limits=(-1000, 1000), with_acf=False): """ Calculates the cross-correlation between two streams and returns a vector containing the correlation at each lag in this order (L/R for resp. limits[0], limits[1]): [L,L+1,...,R-1,R]. If more than two tracks are given in *trackList*, returns a list of correlation vectors, one for every distinct pair of tracks. If `with_acf` is True, self-correlations will also be included in the list. A negative lag indicates that track 2 is shifted to the right w.r.t track 1, a positive lag - to the left. So to get the correlation at lag +4, one has to look at the 4-L th element of the array; for lag -4, at the -4-L th element. Example:: |_____ /^\ _________| lag 0 |______________/^\__| |_____ /^\ _________| lag -8 -> |______________/^\__| |_____ /^\ _________| lag +8 |______________/^\__| <- :param trackList: list of FeatureStream objects :param regions: a tuple (start,end) or a FeatureStream with the bounds of the regions to consider (see `unroll`). In the latter case, all regions will be concatenated. :param limits: (tuple (int,int)) maximum lag to consider. [-1000,1000] :param with_acf: (bool) include auto-correlations. [False] :rtype: list of floats, or list of lists of floats. """ ##### One could profit from numpy to reduce the memory space used for ##### storing these - long - arrays ('dtype' is float64 by default). if isinstance(regions, FeatureStream): _reg = list(regions) x = [ array([ s[0] for s in unroll(t, FeatureStream(_reg, fields=regions.fields)) ]) for t in trackList ] _reg = [] else: x = [array([s[0] for s in unroll(t, regions)]) for t in trackList] if limits[1] - limits[0] > 2 * len(x[0]): limits = (-len(x[0]) + 1, len(x[0]) - 1) N0 = len(x[0]) + limits[1] - limits[0] - 1 ##### convert to a power of 2, fft gets orders of magnitude faster... N = 2**int(log(max(1, N0 / 200.0), 2) + 8.5) #2**int(log(2+N0,2)+.5) if N < N0: x = [vec_reduce(t[:N]) for t in x] else: x = [vec_reduce(t) for t in x] def _corr(x1, x2, N): corr = ifft(conjugate(fft(x1, N)) * fft(x2, N)) / len(x1) corr = ncat((corr[N + limits[0]:], corr[:limits[1] + 1])) return real(corr) if with_acf: return [[_corr(x1, x2, N) for x2 in x[n:]] for n, x1 in enumerate(x)] elif len(trackList) == 2: return _corr(x[0], x[1], N) else: return [[_corr(x1, x2, N) for x2 in x[n + 1:]] for n, x1 in enumerate(x[:-1])]