def test2d(self): #generate random 2D points M = 2 N = 1000 R = quasirand(M, N, type='sobol', spherical=False) assert R.shape == (M, N) assert R.max() <= 1.0 assert R.min() >= 0.0 plt.figure() plt.plot(R[0, :].squeeze(), R[1, :].squeeze(), 'ro') plt.axis('tight') #generate random 3D points on a sphere M = 3 N = 1000 R = quasirand(M, N, type='sobol', spherical=True) assert R.shape == (M, N) assert R.max() <= 1.0 assert R.min() >= -1.0 #ensure that all points lie on the surface of a sphere Rnorm = np.sqrt((R**2).sum(axis=0)) for k in range(N): print Rnorm[k] assert (Rnorm[k] - 1.0) < 1e-6 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(R[0, :], R[1, :], R[2, :]) plt.show()
def compute_mean_envelope(s, nsamps=1000): """ Use random sampling to compute the mean envelope of a multi-dimensional signal. Args: s (np.ndarray): an NxT matrix describing a multi-variate signal. N is the number of channels, T is the number of time points. nsamps (int): the number of N dimensional projections to use in computing the multi-variate envelope. Returns: env (np.ndarray): an NxT matrix giving the multi-dimensional envelope of s. """ N,T = s.shape #pre-allocate the mean envelope matrix mean_env = np.zeros([N, T]) #generate quasi-random points on an N-dimensional sphere #stime = time.time() R = quasirand(N, nsamps, spherical=True) #etime = time.time() - stime #print 'Elapsed time for quasirand: %d seconds' % int(etime) stime = time.time() for k in range(nsamps): #istime = time.time() r = R[:, k].squeeze() #print 'k=%d, s.shape=%s, r.shape=%s' % (k, str(s.shape), str(r.shape)) #project s onto a scalar time series using random vector #dtime = time.time() p = np.dot(s.T, r) #detime = time.time() - dtime #print '\t[%d] Time to do dot %0.6f s' % (k, detime) #print 'p.shape=',p.shape #identify minima and maxima of projection #eetime = time.time() mini_p,maxi_p = find_extrema(p) #eeetime = time.time() - eetime #print '\t[%d] time to find extrema: %0.6fs' % (k, eeetime) #for each signal dimension, fit maxima with cubic spline to produce envelope t = np.arange(T) for n in range(N): #sptime = time.time() mini = copy.copy(mini_p) maxi = copy.copy(maxi_p) if len(mini) < 4 or len(maxi) < 4: return None #extrapolate edges using mirroring lower_env_spline, upper_env_spline = create_mirrored_spline(mini, maxi, s[n, :].squeeze()) if lower_env_spline is None or upper_env_spline is None: return None #evaluate upper and lower envelopes upper_env = splev(t, upper_env_spline) lower_env = splev(t, lower_env_spline) #compute the envelope for this projected dimension env = (upper_env + lower_env) / 2.0 #update the mean envelope for this dimension in an online way delta = env - mean_env[n, :] mean_env[n, :] += delta / (k+1) #esptime = time.time() - sptime #print '\t[%d] time for spline iteration on dimension %d: %0.6fs' % (k, n, esptime) #ietime = time.time() - istime #print '\t[%d] took %0.6f seconds' % (k, ietime) etime = time.time() - stime #print '%d samples took %0.6f seconds' % (nsamps, etime) return mean_env
def compute_mean_envelope(s, nsamps=1000): """ Use random sampling to compute the mean envelope of a multi-dimensional signal. Args: s (np.ndarray): an NxT matrix describing a multi-variate signal. N is the number of channels, T is the number of time points. nsamps (int): the number of N dimensional projections to use in computing the multi-variate envelope. Returns: env (np.ndarray): an NxT matrix giving the multi-dimensional envelope of s. """ N, T = s.shape #pre-allocate the mean envelope matrix mean_env = np.zeros([N, T]) #generate quasi-random points on an N-dimensional sphere #stime = time.time() R = quasirand(N, nsamps, spherical=True) #etime = time.time() - stime #print 'Elapsed time for quasirand: %d seconds' % int(etime) stime = time.time() for k in range(nsamps): #istime = time.time() r = R[:, k].squeeze() #print 'k=%d, s.shape=%s, r.shape=%s' % (k, str(s.shape), str(r.shape)) #project s onto a scalar time series using random vector #dtime = time.time() p = np.dot(s.T, r) #detime = time.time() - dtime #print '\t[%d] Time to do dot %0.6f s' % (k, detime) #print 'p.shape=',p.shape #identify minima and maxima of projection #eetime = time.time() mini_p, maxi_p = find_extrema(p) #eeetime = time.time() - eetime #print '\t[%d] time to find extrema: %0.6fs' % (k, eeetime) #for each signal dimension, fit maxima with cubic spline to produce envelope t = np.arange(T) for n in range(N): #sptime = time.time() mini = copy.copy(mini_p) maxi = copy.copy(maxi_p) if len(mini) < 4 or len(maxi) < 4: return None #extrapolate edges using mirroring lower_env_spline, upper_env_spline = create_mirrored_spline( mini, maxi, s[n, :].squeeze()) if lower_env_spline is None or upper_env_spline is None: return None #evaluate upper and lower envelopes upper_env = splev(t, upper_env_spline) lower_env = splev(t, lower_env_spline) #compute the envelope for this projected dimension env = (upper_env + lower_env) / 2.0 #update the mean envelope for this dimension in an online way delta = env - mean_env[n, :] mean_env[n, :] += delta / (k + 1) #esptime = time.time() - sptime #print '\t[%d] time for spline iteration on dimension %d: %0.6fs' % (k, n, esptime) #ietime = time.time() - istime #print '\t[%d] took %0.6f seconds' % (k, ietime) etime = time.time() - stime #print '%d samples took %0.6f seconds' % (nsamps, etime) return mean_env