def filter_params(io, rsh, rs, ee, isc): # Function filter_params identifies bad parameter sets. A bad set contains # Nan, non-positive or imaginary values for parameters; Rs > Rsh; or data # where effective irradiance Ee differs by more than 5% from a linear fit # to Isc vs. Ee badrsh = np.logical_or(rsh < 0., np.isnan(rsh)) negrs = rs < 0. badrs = np.logical_or(rs > rsh, np.isnan(rs)) imagrs = ~(np.isreal(rs)) badio = np.logical_or(~(np.isreal(rs)), io <= 0) goodr = np.logical_and(~badrsh, ~imagrs) goodr = np.logical_and(goodr, ~negrs) goodr = np.logical_and(goodr, ~badrs) goodr = np.logical_and(goodr, ~badio) matrix = np.vstack((ee / 1000., np.zeros(len(ee)))).T eff = np.linalg.lstsq(matrix, isc)[0][0] pisc = eff * ee / 1000 pisc_error = np.abs(pisc - isc) / isc # check for departure from linear relation between Isc and Ee badiph = pisc_error > .05 u = np.logical_and(goodr, ~badiph) return u
def findSigT(xIn, yIn): ## ## Added to check their sig test. Yes I know it is paranoid rjel ## regPoints are the number of points going into the ## regression calculation. ## Check that there are some points not masked out first. Else you ## get horrible errors. if np.any(np.isreal(((xIn <= 1.0) & (np.isreal(yIn))))): rPoints = int(((xIn <= 1.0) & np.isreal(yIn)).sum()) else: rPoints = 0 ## print( 'There are ', regPoints,' points in the calculation') ## ## Find the value in the ttest table corresponding to n and ## the p value of interest. The table I downloaded had 1-100 ## points then more than 100 if (rPoints < 103) & (rPoints > 2): nIndex = rPoints - 2 ## ## if there are less than 2 points don't bother elif rPoints <= 2: return 3000.0, rPoints else: nIndex = 101 ## The first line of the t-table file has the p values in it ## so we can search a split first line for the column ## that contains a given p-value ## pIndex = tTable[0].index(str(1.0 - p.pVal)) sigLocal = tTable[nIndex][pIndex] return sigLocal, rPoints
def update_data(self, attr, old, new): mat = matrix(self.a_slider.value, self.b_slider.value, self.c_slider.value, self.d_slider.value) evals, evecs = np.linalg.eig(mat) ev0, ev1 = evals evec0 = evecs.T[0] evec1 = evecs.T[1] trans0 = mat @ evec0 trans1 = mat @ evec1 self.eigen0.text = "eigen0 = " + \ str(ev0) if np.isreal(ev0) else "eigen0 = None" self.eigen1.text = "eigen1 = " + \ str(ev1) if np.isreal(ev1) else "eigen1 = None" transXs, transYs = mat @ np.array([self.Xs, self.Ys]) self.source.data = dict(Xs=self.Xs, Ys=self.Ys, transXs=transXs, transYs=transYs) data0x = [0, evec0[0]] if np.isreal(ev0) else [0, 0] data0y = [0, evec0[1]] if np.isreal(ev0) else [0, 0] data1x = [0, evec1[0]] if np.isreal(ev1) else [0, 0] data1y = [0, evec1[1]] if np.isreal(ev1) else [0, 0] trans0x = [0, trans0[0]] if np.isreal(ev0) else [0, 0] trans0y = [0, trans0[1]] if np.isreal(ev0) else [0, 0] trans1x = [0, trans1[0]] if np.isreal(ev1) else [0, 0] trans1y = [0, trans1[1]] if np.isreal(ev1) else [0, 0] self.evectors.data = dict(ev0x=data0x, ev0y=data0y, ev1x=data1x, ev1y=data1y, transev0x=trans0x, transev0y=trans0y, transev1x=trans1x, transev1y=trans1y)
def idct(self, X): '''Compute inverse discrete cosine transform of a 1-d array X''' N = len(X) w = np.sqrt(2*N)*np.exp(1j*np.arange(N)*np.pi/(2.*N)) if (N%2==1) or (any(np.isreal(X))== False): w[0] = w[0] * np.sqrt(2) yy = np.zeros(2*N) yy[0:N] = w * X yy[N+1:2*N] = -1j * w[1:N] * X[1:N][::-1] y = fftpack.ifft(yy) x = y[0:N] else: w[0] = w[0]/np.sqrt(2) yy = X *w y = fftpack.ifft(yy) x = np.zeros(N) x[0:N:2] = y[0:N/2] x[1:N:2] = y[N:(N/2)-1:-1] if all(np.isreal(x)): x = np.real(x) return x
def kurt(x, opt): if opt=='kurt2': if np.all(x==0): K=0 E=0 return K x = x - np.mean(x) E = np.mean(np.abs(x)**2) if E < eps: K=0 return K K = np.mean(np.abs(x)**4)/E**2 if np.all(np.isreal(x)): K = K - 3 else: K = K - 2 if opt=='kurt1': if np.all(x==0): K=0 E=0 return K x = x - np.mean(x) E = np.mean(np.abs(x)) if E < eps: K=0 return K K = np.mean(np.abs(x)**2)/E**2 if np.all(np.isreal(x)): K=K-1.57 else: K=K-1.27 return K
def dct(self, x): '''Compute discrete cosine transform of 1-d array x''' #probably don't need this here anymore since it is in fftpack now N = len(x) #calculate DCT weights w = (np.exp(-1j*np.arange(N)*np.pi/(2*N))/np.sqrt(2*N)) w[0] = w[0]/np.sqrt(2) #test for odd or even function if (N%2==1) or (any(np.isreal(x)) == False): y = np.zeros(2*N) y[0:N] = x y[N:2*N] = x[::-1] yy = fftpack.fft(y) yy = yy[0:N] else: y = np.r_[(x[0:N:2], x[N:0:-2])] yy = fftpack.fft(y) w = 2*w #apply weights X = w * yy if all(np.isreal(x)): X = np.real(X) return X
def is_nan(x): try: if type(x) == str: raise TypeError() return np.array([e == NA_character_ or (np.isreal(e) and np.isnan(e)) for e in x], dtype=bool) except TypeError: return x == NA_character_ or (np.isreal(x) and np.isnan(x))
def test_multib0_dsi(): data, gtab = dsi_voxels() # Create a new data-set with a b0 measurement: new_data = np.concatenate([data, data[..., 0, None]], -1) new_bvecs = np.concatenate([gtab.bvecs, np.zeros((1, 3))]) new_bvals = np.concatenate([gtab.bvals, [0]]) new_gtab = gradient_table(new_bvals, new_bvecs) ds = DiffusionSpectrumModel(new_gtab) sphere = get_sphere('repulsion724') dsfit = ds.fit(new_data) pdf = dsfit.pdf() dsfit.odf(sphere) assert_equal(new_data.shape[:-1] + (17, 17, 17), pdf.shape) assert_equal(np.alltrue(np.isreal(pdf)), True) # And again, with one more b0 measurement (two in total): new_data = np.concatenate([data, data[..., 0, None]], -1) new_bvecs = np.concatenate([gtab.bvecs, np.zeros((1, 3))]) new_bvals = np.concatenate([gtab.bvals, [0]]) new_gtab = gradient_table(new_bvals, new_bvecs) ds = DiffusionSpectrumModel(new_gtab) dsfit = ds.fit(new_data) pdf = dsfit.pdf() dsfit.odf(sphere) assert_equal(new_data.shape[:-1] + (17, 17, 17), pdf.shape) assert_equal(np.alltrue(np.isreal(pdf)), True)
def is_finite(x): try: if type(x) == str: return False return np.array([np.isreal(e) and np.isfinite(e) for e in x], dtype=bool) except TypeError: return np.isreal(x) and np.isfinite(x)
def read_matrix_sparse(filename, dtype=float, comments='#'): coo=np.loadtxt(filename, comments=comments, dtype=dtype) """Check if coo is (M, 3) ndarray""" if len(coo.shape)==2 and coo.shape[1]==3: row=coo[:, 0] col=coo[:, 1] values=coo[:, 2] """Check if imaginary part of row and col is zero""" if np.all(np.isreal(row)) and np.all(np.isreal(col)): row=row.real col=col.real """Check if first and second column contain only integer entries""" if np.all(is_integer(row)) and np.all(is_integer(col)): """Convert row and col to int""" row=row.astype(int) col=col.astype(int) """Create coo-matrix""" A=scipy.sparse.coo_matrix((values,(row, col))) return A else: raise ValueError('File contains non-integer entries for row and col.') else: raise ValueError('File contains complex entries for row and col.') else: raise ValueError('Given file is not a sparse matrix in coo-format.')
def bezier_curve_y_out(shift_angle, P0, P1, P2, P3, second_of_day=None): ''' For a cubic Bezier segment described by the 2-tuples P0, ..., P3, return the y-value associated with the given x-value. Ex: getYfromXforBezSegment((10,0), (5,-5), (5,5), (0,0), 3.2) ''' seconds_per_day = 24*60*60 # Check if the second of the day is provided. # If provided, calculate y of the Bezier curve with that x # Otherwise, use the current second of the day if second_of_day is None: now = datetime.datetime.now() dt = datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second) seconds = dt.total_seconds() else: seconds = second_of_day # Shift the entire graph using 0 - 360 to determine the degree if shift_angle: percent_angle = shift_angle/360 angle_seconds = percent_angle*seconds_per_day if seconds+angle_seconds > seconds_per_day: seconds_shifted = seconds+angle_seconds-seconds_per_day else: seconds_shifted = seconds+angle_seconds percent_of_day = seconds_shifted/seconds_per_day else: percent_of_day = seconds/seconds_per_day x = percent_of_day*(P0[0]-P3[0]) # First, get the t-value associated with x-value, where t is the # parameterization of the Bezier curve and ranges from 0 to 1. # We need the coefficients of the polynomial describing cubic Bezier # (cubic polynomial in t) coefficients = [-P0[0] + 3*P1[0] - 3*P2[0] + P3[0], 3*P0[0] - 6*P1[0] + 3*P2[0], -3*P0[0] + 3*P1[0], P0[0] - x] # Find roots of the polynomial to determine the parameter t roots = np.roots(coefficients) # Find the root which is between 0 and 1, and is also real correct_root = None for root in roots: if np.isreal(root) and 0 <= root <= 1: correct_root = root # Check a valid root was found if correct_root is None: print('Error, no valid root found. Are you sure your Bezier curve ' 'represents a valid function when projected into the xy-plane?') param_t = correct_root # From the value for the t parameter, find the corresponding y-value # using the formula for cubic Bezier curves y = (1-param_t)**3*P0[1] + 3*(1-param_t)**2*param_t*P1[1] + 3*(1-param_t)*param_t**2*P2[1] + param_t**3*P3[1] assert np.isreal(y) # Typecast y from np.complex128 to float64 y = y.real return y
def sp_bin_by_space_and_time(data, frameTime, xybin=8, counterTime=40.0e-9): """Takes a dataset collected by the SSI photon counting fast camera and bins the data in time and xy. arguments: data - data to bin. This is an (N, 4) array where N is the number of elements. frameTime - Amout of time between bins. Measured in seconds xybin - amount to bin in x and y. defaults to 8, which is a 512x512 output. counterTime - clock time of the camera. Anton Tremsin says its 40 ns. returns: binnedData - a 3-dimension array (frames, y, x) of the binned data. """ assert isinstance(data, np.ndarray), "data must be an ndarray" assert data.shape[1] == 4, "Second dimension must be 4." assert np.isreal(frameTime), "frameTime (%s) must be real" % repr(frameTime) assert np.isreal(counterTime), "counterTime (%s) must be real" % repr(counterTime) # TODO: This could be accelerated if written for a GPU firsttime = data[:,3].min() lasttime = data[:, 3].max() datalen = len(data[:, 0]) nbins = int(np.ceil(float(lasttime - firsttime)*counterTime/frameTime)) xybinnedDim = int(np.ceil(SP_MAX_SIZE/xybin)) binnedData = np.zeros((nbins, xybinnedDim, xybinnedDim), dtype='int') bin_edges = np.linspace(firsttime, lasttime, nbins+1) # slightly increase the last bin. For some reason the last bin is slightly too small, and the photons on the edges are not included bin_edges[-1] = bin_edges[-1]*1.01 # If the frameTime is larger than the total acq time, then sum up everything if frameTime >= (lasttime-firsttime)*counterTime: histfn = lambda a,b: (len(a), b) else: # this is the default; bin data using histogram histfn = np.histogram for i in range(xybinnedDim): x = data[:, 0] y = data[:, 1] xc = np.argwhere( (i*xybin <= x) & (x < (i+1)*xybin) ) idx_to_delete = np.array([]) for j in range(xybinnedDim): yc = np.argwhere( (j*xybin <= y) & (y < (j+1)*xybin) ) isect, a, b = intersect(xc, yc, assume_unique=True) if len(isect) != 0: binned, bin_edges = histfn(data[isect,3]+0.5, bin_edges) binnedData[:, i, j] = binned idx_to_delete = np.append(idx_to_delete, isect) data = np.delete(data, idx_to_delete, axis=0) if binnedData.sum() != datalen: print "warning: # of binned data photons (%d) do not match original dataset (%d)" % (binnedData.sum(), datalen) return binnedData
def test_it(self): self.assertTrue(np.isreal(ef.return_real_part(1))) self.assertTrue(np.isreal(ef.return_real_part(1 + 0j))) self.assertTrue(np.isreal(ef.return_real_part(1 + 1e-20j))) self.assertRaises(TypeError, ef.return_real_part, None) self.assertRaises(TypeError, ef.return_real_part, (1, 2.0, 2 + 2j)) self.assertRaises(TypeError, ef.return_real_part, [None, 2.0, 2 + 2j]) self.assertRaises(ValueError, ef.return_real_part, [1, 2.0, 2 + 2j]) self.assertRaises(ValueError, ef.return_real_part, 1 + 1e-10j) self.assertRaises(ValueError, ef.return_real_part, 1j)
def symmNormalization(MPS, chir, chic): omega, R = getLargestW(MPS, 'R') R = fixPhase(R) if np.isreal(R).all(): omega, R = omega.real, R.real print "wR", omega, np.isreal(R).all(), "R\n", R assym = np.linalg.norm(R - R.T.conj()) print "assym R", assym Rvals, Rvecs = spla.eigh(R) Rvals_s = np.sqrt(abs(Rvals)) Rvals_si = 1. / Rvals_s print "Rvals", Rvals Rs = np.dot(Rvecs, np.dot(np.diag(Rvals_s), Rvecs.T.conj())) ARs = np.tensordot(MPS, Rs, axes=([1,0])) Rsi = np.dot(Rvecs, np.dot(np.diag(Rvals_si), Rvecs.T.conj())) A1 = np.tensordot(Rsi, ARs, axes=([1,0])) A1 = np.transpose(A1, (0, 2, 1)) omega, L = getLargestW(A1, 'L') L = fixPhase(L) if np.isreal(L).all(): omega, L = omega.real, L.real print "wL", omega, np.isreal(L).all(), "L\n", L assym = np.linalg.norm(L - L.T.conj()) print "assym L", assym Lambda2, U = spla.eigh(L) A1U = np.tensordot(A1, U, axes=([1,0])) A2 = np.tensordot(U.T.conj(), A1U, axes=([1,0])) A2 = np.transpose(A2, (0, 2, 1)) print "Lambda**2", Lambda2#, "\n", U Lambda = np.sqrt(abs(Lambda2)) Lambdas = np.sqrt(Lambda) Lambdasi = 1. / Lambdas A2Lsi = np.tensordot(A2, np.diag(Lambdasi), axes=([1,0])) A3 = np.tensordot(np.diag(Lambdas), A2Lsi, axes=([1,0])) A3 = np.transpose(A3, (0, 2, 1)) print "Lambda", Lambda nMPS = A3 / np.sqrt(omega) RealLambda = fixPhase(np.diag(Lambda)) #print "nMPS", nMPS.shape, "\n", nMPS print "RealLambda", RealLambda.shape, "\n", RealLambda ######### CHECKING RESULT ######### Trace = np.linalg.norm(RealLambda) ELambda = linearOpForR(nMPS, RealLambda).reshape(chir, chic) LambdaE = linearOpForL(nMPS, RealLambda).reshape(chir, chic) print "Trace(RealLambda)", Trace, "\nE|r)\n", ELambda, "\n(l|E\n", LambdaE return RealLambda, nMPS
def _finalize_limits(self, axis, view, subplots, ranges): # Extents extents = self.get_extents(view, ranges) if extents and not self.overlaid: coords = [coord if np.isreal(coord) else np.NaN for coord in extents] if isinstance(view, Element3D) or self.projection == '3d': l, b, zmin, r, t, zmax = coords if not np.NaN in (zmin, zmax) and not zmin==zmax: axis.set_zlim((zmin, zmax)) else: l, b, r, t = [coord if np.isreal(coord) else np.NaN for coord in extents] if not np.NaN in (l, r) and not l==r: axis.set_xlim((l, r)) if not np.NaN in (b, t) and not b==t: axis.set_ylim((b, t))
def radialUndistort(rpp, k, quot=False, der=False): '''dqD takes distorted radius and returns the radius undistorted optioally it returns the undistortion quotient rp = rpp * q ''' # polynomial coeffs, grade 7 # # (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) # polynomial coeffs, grade 7 # # (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) k.shape = -1 poly = [[k[4], # k3 0, k[1], # k2 0, k[0], # k1 0, 1, -r] for r in rpp] # calculate roots rootsPoly = array([roots(p) for p in poly]) # return flag, True if there is a suitable (real AND positive) solution rPRB = isreal(rootsPoly) & (0 <= real(rootsPoly)) # real Positive Real Bool retVal = any(rPRB, axis=1) rp = empty_like(rpp) if any(~ retVal): # if at least one case of non solution # calculate extrema of polyniomial rExtrema = roots([7*k[4], 0, 5*k[1], 0, 3*k[0], 0, 1]) # select real extrema in positive side, keep smallest rRealPos = min(rExtrema.real[isreal(rExtrema) & (0<=rExtrema.real)]) # assign to problematic values rp[~retVal] = rRealPos # choose minimum positive roots rp[retVal] = [min(rootsPoly[i, rPRB[i]].real) for i in arange(rpp.shape[0])[retVal]] if der: # derivada de la directa q, dQdP, dQdK = radialDistort(rp, k, der=True) if quot: return q, retVal, dQdP, dQdK else: return rp, retVal, dQdP, dQdK else: if quot: return rp / rpp, retVal else: return rp, retVal
def kurt(x, opt): """ Calculates kurtosis of a signal according to the option chosen :param x: signal :param opt: ['kurt1' | 'kurt2'] * 'kurt1' = variance of the envelope magnitude * 'kurt2' = kurtosis of the complex envelope :type x: numpy array :type opt: string :rtype: float :returns: Kurtosis """ if opt == 'kurt2': if np.all(x == 0): K = 0 E = 0 return K x = x - np.mean(x) E = np.mean(np.abs(x)**2) if E < eps: K = 0 return K K = np.mean(np.abs(x)**4)/E**2 if np.all(np.isreal(x)): K = K - 3 else: K = K - 2 if opt == 'kurt1': if np.all(x == 0): K = 0 E = 0 return K x = x - np.mean(x) E = np.mean(np.abs(x)) if E < eps: K = 0 return K K = np.mean(np.abs(x)**2)/E**2 if np.all(np.isreal(x)): K = K-1.57 else: K = K-1.27 return K
def radialUndistort(rpp, k, quot=False, der=False): ''' takes distorted radius and returns the radius undistorted optionally it returns the distortion quotioent rpp = rp * q ''' # polynomial coeffs, grade 7 # # (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) # polynomial coeffs, grade 7 # # (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) k.shape = -1 poly = [[k[3], 0, k[2], 0, k[1], 0, k[0], 0, 1, -r] for r in rpp] # calculate roots rootsPoly = array([roots(p) for p in poly]) # return flag, True if there is a suitable (real AND positive) solution rPRB = isreal(rootsPoly) & (0 <= real(rootsPoly)) # real Positive Real Bool retVal = any(rPRB, axis=1) thetap = empty_like(rpp) if any(~retVal): # if at least one case of non solution # calculate extrema of polyniomial thExtrema = roots([9*k[3], 0, 7*k[2], 0, 5*k[1], 0, 3*k[0], 0, 1]) # select real extrema in positive side, keep smallest thRealPos = min(thExtrema.real[isreal(thExtrema) & (0<=thExtrema.real)]) # assign to problematic values thetap[~retVal] = thRealPos # choose minimum positive roots thetap[retVal] = [min(rootsPoly[i, rPRB[i]].real) for i in arange(rpp.shape[0])[retVal]] rp = abs(tan(thetap)) # correct negative values # if theta angle is greater than pi/2, retVal=False retVal[thetap >= pi/2] = False if der: # derivada de la directa q, dQdP, dQdK = radialDistort(rp, k, quot, der) if quot: return q, retVal, dQdP, dQdK else: return rp, retVal, dQdP, dQdK else: if quot: # returns q return rpp / rp, retVal else: return rp, retVal
def quantize(sig, partition, varargin): if sig.size == 0 or not np.isreal(sig).all() or sig.shape[0] != 1: sys.exit("comm:quantize:invalid_Sig") if len(partition) == 0 or not np.isreal(partition).all() or type(partition) != list: sys.exit("comm:quantize:invalid_Partition") nrows, ncols = np.shape(sig) indx = np.zeros((nrows, ncols)) for i in range(len(partition)): indx += (sig > partition[i]) return indx
def multiply(self,x,mode): op1 = self.children[0] if np.isreal(x).all(): # Purely real y = np.real(op1.applyMultiply(x,mode)) elif np.isreal(1j*x).all(): # Purely imaginary y = np.real(op1.applyMultiply(np.imag(x),mode)) * 1j else: # Mixed y = np.real(op1.applyMultiply(np.real(x),mode)) + np.real(op1.applyMultiply(np.imag(x),mode)) * 1j return y
def computeStationaryCovariance(self, ls): '''Compute the asymptotic covariance for a given linear system. (Eq. 104-106) ''' A, H, G, Q, M, R = map(np.mat, [ls.getA(), ls.getH(), ls.getG(), ls.getQ(), ls.getM(), ls.getR()]) try: Pprd = np.mat(dare(A.T, H.T, G * Q * G.T, M * R * M.T)) except: raise AssertionError("Dare Unsolvable: " + "The given system state is not stable/observable.") assert np.all(np.isreal(Pprd)) and np.all(Pprd == Pprd.T) Pest = Pprd - (Pprd * H.T) * (H * Pprd * H.T + M * R * M.T).I * (Pprd * H.T).T assert np.all(np.isreal(Pest)) and np.all(Pest == Pest.T) return Pest
def y(self, value): if value is None: self._y = value elif np.isscalar(value) and np.isreal(value): self._y = value else: raise TypeError("y should be a scalar floating point value")
def splitBimodal(self, x, y, largepoly=30): p = np.polyfit(x, y, largepoly) # polynomial coefficients for fit extrema = np.roots(np.polyder(p)) extrema = extrema[np.isreal(extrema)] extrema = extrema[(extrema - x[1]) * (x[-2] - extrema) > 0] # exclude the endpoints due false maxima during fitting try: root_vals = [sum([p[::-1][i]*(root**i) for i in range(len(p))]) for root in extrema] peaks = extrema[np.argpartition(root_vals, -2)][-2:] # find two peaks of bimodal distribution mid, = np.where((x - peaks[0])* (peaks[1] - x) > 0) # want data points between the peaks except: warnings.warn("Peak finding failed!") return None try: p_mid = np.polyfit(x[mid], y[mid], 2) # fit middle section to a parabola midpoint = np.roots(np.polyder(p_mid))[0] except: warnings.warn("Polynomial fit between peaks of distribution poorly conditioned. Falling back on using the minimum! May result in inaccurate split determination.") if len(mid) == 0: return None midx = np.argmin(y[mid]) midpoint = x[mid][midx] return midpoint
def getPosteriorMeanAndVar(self, diagKTestTest, KtrainTest, post, intercept=0): L = post['L'] if (np.size(L) == 0): raise Exception('L is an empty array') #possible to compute it here Lchol = np.all((np.all(np.tril(L, -1)==0, axis=0) & (np.diag(L)>0)) & np.isreal(np.diag(L))) ns = diagKTestTest.shape[0] nperbatch = 5000 nact = 0 #allocate mem fmu = np.zeros(ns) #column vector (of length ns) of predictive latent means fs2 = np.zeros(ns) #column vector (of length ns) of predictive latent variances while (nact<(ns-1)): id = np.arange(nact, np.minimum(nact+nperbatch, ns)) kss = diagKTestTest[id] Ks = KtrainTest[:, id] if (len(post['alpha'].shape) == 1): try: Fmu = intercept[id] + Ks.T.dot(post['alpha']) except: Fmu = intercept + Ks.T.dot(post['alpha']) fmu[id] = Fmu else: try: Fmu = intercept[id][:, np.newaxis] + Ks.T.dot(post['alpha']) except: Fmu = intercept + Ks.T.dot(post['alpha']) fmu[id] = Fmu.mean(axis=1) if Lchol: V = la.solve_triangular(L, Ks*np.tile(post['sW'], (id.shape[0], 1)).T, trans=1, check_finite=False, overwrite_b=True) fs2[id] = kss - np.sum(V**2, axis=0) #predictive variances else: fs2[id] = kss + np.sum(Ks * (L.dot(Ks)), axis=0) #predictive variances fs2[id] = np.maximum(fs2[id],0) #remove numerical noise i.e. negative variances nact = id[-1] #set counter to index of last processed data point return fmu, fs2
def UpdateKappaSigmaSq(self,it): for ii in range(self.T-1): new_kappa_sigma_sq = self.kappa_sigma_sq[ii]+(2*np.ceil(2*np.random.random())-3)*(np.random.geometric(1.0/(1+np.exp(self.log_kappa_sigma_sqq[ii])))-1) if new_kappa_sigma_sq <0: accept = 0 else: lam1 = 1.0*self.lambda_sigma + self.kappa_sigma_sq[ii] gam1 = 1.0*self.lambda_sigma/self.mu_sigma + 1.0*self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma loglike = lam1*np.log(gam1)-math.lgamma(lam1)+(lam1-1)*np.log(self.sigma_sq[ii+1]) pnmean = self.sigma_sq[ii]*self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma loglike = loglike + self.kappa_sigma_sq[ii]*np.log(pnmean)- math.lgamma(1.0*self.kappa_sigma_sq[ii]+1) lam1 = 1.0*self.lambda_sigma + new_kappa_sigma_sq gam1 = 1.0*self.lambda_sigma/self.mu_sigma + self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma new_loglike = lam1*np.log(gam1)-math.lgamma(lam1)+(lam1-1)*np.log(self.sigma_sq[ii+1]) pnmean = self.sigma_sq[ii]*self.rho_sigma/(1-self.rho_sigma)*self.lambda_sigma/self.mu_sigma new_loglike = new_loglike + new_kappa_sigma_sq*np.log(pnmean)-math.lgamma(1.0*new_kappa_sigma_sq+1) log_accept = new_loglike - loglike accept =1 if np.isnan(log_accept) or np.isinf(log_accept): accept = 0 elif log_accept <0: accept = np.exp(log_accept) self.kappa_lambda_sigma_accept = self.kappa_lambda_sigma_accept + accept self.kappa_sigma_sq_count = self.kappa_sigma_sq_count +1 if np.random.random()<accept : self.kappa_sigma_sq[ii] = new_kappa_sigma_sq self.log_kappa_sigma_sqq[ii] = self.log_kappa_sigma_sqq[ii]+1.0/it**0.55*(accept-0.3) if np.isnan(self.kappa_sigma_sq[ii]) or np.isreal(self.kappa_sigma_sq[ii]) == 0: stop
def sMCI(x, y, ksize): """ Memoryless Cross Intensity kernel (mCI) Input: x: (N1x1) numpy array of sorted spike times y: (N2x1) numpy array of sorted spike times ksize: kernel size y_neg_cum_sum_exp = sum_j exp(-y[-j]/ksize) y_pos_cum_sum_exp = sum_j exp(y[j]/ksize) Output: v: sum_i sum_j exp(-|x[i] - y[j]|/ksize) """ v = 0. x = check_spike_train(x) y = check_spike_train(y) if x.shape[0]==0 or y.shape[0]==0: return v assert (ksize>0. and np.isreal(ksize)), "Kernel size must be non-negative real" #v = pairwise_l1(x, y) #v = np.exp(-v/ksize) #v = v.sum() v = fs.mci(x, y, ksize) return v
def get_fixed_point(I=0., eps=0.1, a=2.0): """Computes the fixed point of the FitzHugh Nagumo model as a function of the input current I. We solve the 3rd order poylnomial equation: v**3 + V + a - I0 = 0 Args: I: Constant input [mV] eps: Inverse time constant of the recovery variable w [1/ms] a: Offset of the w-nullcline [mV] Returns: tuple: (v_fp, w_fp) fixed point of the equations """ # Use poly1d function from numpy to compute the # roots of 3rd order polynomial P = np.poly1d([1, 0, 1, (a - I)], variable="x") # take only the real root v_fp = np.real(P.r[np.isreal(P.r)])[0] w_fp = 2. * v_fp + a return (v_fp, w_fp)
def test_SparseValsOnly(): """ Sparse eigvals only Hermitian. """ H = rand_herm(10) spvals = H.eigenenergies(sparse=True) assert_equal(len(spvals), 10) # check that sorting is lowest eigval first assert_equal(spvals[0] <= spvals[-1], True) # check that spvals equal expect vals for k in range(10): # check that ouput is real for Hermitian operator assert_equal(isreal(spvals[k]), True) spvals = H.eigenenergies(sparse=True, sort='high') # check that sorting is lowest eigval first assert_equal(spvals[0] >= spvals[-1], True) spvals = H.eigenenergies(sparse=True, sort='high', eigvals=4) assert_equal(len(spvals), 4) U = rand_unitary(10) spvals = U.eigenenergies(sparse=True) assert_equal(len(spvals), 10) # check that sorting is lowest eigval first assert_equal(spvals[0] <= spvals[-1], True) # check that spvals equal expect vals for k in range(10): # check that ouput is real for Hermitian operator assert_equal(iscomplex(spvals[k]), True) spvals = U.eigenenergies(sparse=True, sort='high') # check that sorting is lowest eigval first assert_equal(spvals[0] >= spvals[-1], True) spvals = U.eigenenergies(sparse=True, sort='high', eigvals=4) assert_equal(len(spvals), 4)
def validate_scalar(name, value, domain=None, physical_type=None): validate_physical_type(name, value, physical_type) if not physical_type: if np.isscalar(value) or not np.isreal(value): raise TypeError("{0} should be a scalar floating point value".format(name)) if domain == 'positive': if value < 0.: raise ValueError("{0} should be positive".format(name)) elif domain == 'strictly-positive': if value <= 0.: raise ValueError("{0} should be strictly positive".format(name)) elif domain == 'negative': if value > 0.: raise ValueError("{0} should be negative".format(name)) elif domain == 'strictly-negative': if value >= 0.: raise ValueError("{0} should be strictly negative".format(name)) elif type(domain) in [tuple, list] and len(domain) == 2: if value < domain[0] or value > domain[-1]: raise ValueError("{0} should be in the range [{1}:{2}]".format(name, domain[0], domain[-1])) return value
def UpdateKappa(self, it): for ii in xrange(self.T-1): for jj in xrange(self.p): new_kappa = self.kappa[ii][jj]+(2*np.ceil(2*np.random.random())-3)*(np.random.geometric(1.0/(1+np.exp(self.log_kappa_q[ii][jj])))-1) if new_kappa < 0: accept = 0 else: lam1 = self.lambda_[jj] + 1.0*self.kappa[ii][jj] gam1 = self.lambda_[jj]/self.mu[jj] + self.delta[jj] loglike = lam1*np.log(gam1) - math.lgamma(lam1)+(lam1-1)*np.log(self.psi[ii+1][jj]) pnmean = self.psi[ii][jj] * self.delta[jj] loglike = loglike + 1.0*self.kappa[ii][jj]*np.log(pnmean) - math.lgamma(1.0*self.kappa[ii][jj]+1) lam1 = self.lambda_[jj] + 1.0*new_kappa gam1 = self.lambda_[jj]/self.mu[jj] + self.delta[jj] new_loglike = lam1*np.log(gam1) - math.lgamma(lam1)+(lam1-1)*np.log(self.psi[ii+1][jj]) pnmean = self.psi[ii][jj]*self.delta[jj] new_loglike = new_loglike + new_kappa*np.log(pnmean)-math.lgamma(1.0*new_kappa+1) log_accept = new_loglike - loglike accept =1 if np.isnan(log_accept) or np.isinf(log_accept): accept =0 elif log_accept <0: accept = np.exp(log_accept) self.kappa_accept = self.kappa_accept + accept self.kappa_count = self.kappa_count +1 if np.random.random() < accept: self.kappa[ii][jj] = new_kappa self.log_kappa_q[ii][jj] = self.log_kappa_q[ii][jj] + 1.0/it**0.55*(accept-0.3) if np.isnan(self.kappa[ii][jj]) or np.isreal(self.kappa[ii][jj]) ==False: stop
def levinson_1d(r, order): """Levinson-Durbin recursion, to efficiently solve symmetric linear systems with toeplitz structure. Parameters --------- r : array-like input array to invert (since the matrix is symmetric Toeplitz, the corresponding pxp matrix is defined by p items only). Generally the autocorrelation of the signal for linear prediction coefficients estimation. The first item must be a non zero real. Notes ---- This implementation is in python, hence unsuitable for any serious computation. Use it as educational and reference purpose only. Levinson is a well-known algorithm to solve the Hermitian toeplitz equation: _ _ -R[1] = R[0] R[1] ... R[p-1] a[1] : : : : * : : : : _ * : -R[p] = R[p-1] R[p-2] ... R[0] a[p] _ with respect to a ( is the complex conjugate). Using the special symmetry in the matrix, the inversion can be done in O(p^2) instead of O(p^3). """ r = np.atleast_1d(r) if r.ndim > 1: raise ValueError("Only rank 1 are supported for now.") n = r.size if n < 1: raise ValueError("Cannot operate on empty array !") elif order > n - 1: raise ValueError("Order should be <= size-1") if not np.isreal(r[0]): raise ValueError("First item of input must be real.") elif not np.isfinite(1 / r[0]): raise ValueError("First item should be != 0") # Estimated coefficients a = np.empty(order + 1, r.dtype) # temporary array t = np.empty(order + 1, r.dtype) # Reflection coefficients k = np.empty(order, r.dtype) a[0] = 1. e = r[0] for i in xrange(1, order + 1): acc = r[i] for j in range(1, i): acc += a[j] * r[i - j] k[i - 1] = -acc / e a[i] = k[i - 1] for j in range(order): t[j] = a[j] for j in range(1, i): a[j] += k[i - 1] * np.conj(t[i - j]) e *= 1 - k[i - 1] * np.conj(k[i - 1]) return a, e, k
def to_real(x): return x.real if np.isreal(x) else x
def infer_kaldi_data_type(obj): '''Infer the appropriate kaldi data type for this object The following map is used (in order): +------------------------------+---------------------+ | Object | KaldiDataType | +==============================+=====================+ | an int | Int32 | +------------------------------+---------------------+ | a boolean | Bool | +------------------------------+---------------------+ | a float* | Base | +------------------------------+---------------------+ | str | Token | +------------------------------+---------------------+ | 2-dim numpy array float32 | FloatMatrix | +------------------------------+---------------------+ | 1-dim numpy array float32 | FloatVector | +------------------------------+---------------------+ | 2-dim numpy array float64 | DoubleMatrix | +------------------------------+---------------------+ | 1-dim numpy array float64 | DoubleVector | +------------------------------+---------------------+ | 1-dim numpy array of int32 | Int32Vector | +------------------------------+---------------------+ | 2-dim numpy array of int32* | Int32VectorVector | +------------------------------+---------------------+ | (matrix-like, float or int) | WaveMatrix** | +------------------------------+---------------------+ | an empty container | BaseMatrix | +------------------------------+---------------------+ | container of str | TokenVector | +------------------------------+---------------------+ | 1-dim py container of ints | Int32Vector | +------------------------------+---------------------+ | 2-dim py container of ints* | Int32VectorVector | +------------------------------+---------------------+ | 2-dim py container of pairs | BasePairVector | | of floats | | +------------------------------+---------------------+ | matrix-like python container | DoubleMatrix | +------------------------------+---------------------+ | vector-like python container | DoubleVector | +------------------------------+---------------------+ *The same data types could represent a `Double` or an `Int32PairVector`, respectively. Care should be taken in these cases. **The first element is the wave data, the second its sample frequency. The wave data can be a 2d numpy float array of the same precision as ``KaldiDataType.BaseMatrix``, or a matrix-like python container of floats and/or ints. Returns ------- pydrobert.kaldi.io.enums.KaldiDataType or None ''' if isinstance(obj, int): return KaldiDataType.Int32 elif isinstance(obj, bool): return KaldiDataType.Bool elif isinstance(obj, float): return KaldiDataType.Base elif isinstance(obj, str) or isinstance(obj, text): return KaldiDataType.Token # the remainder are expected to be containers if not hasattr(obj, '__len__'): return None # numpy array or wav tuple? try: if len(obj.shape) == 1: if obj.dtype == np.float32: return KaldiDataType.FloatVector elif obj.dtype == np.float64: return KaldiDataType.DoubleVector elif obj.dtype == np.int32: return KaldiDataType.Int32Vector elif len(obj.shape) == 2: if obj.dtype == np.float32: return KaldiDataType.FloatMatrix elif obj.dtype == np.float64: return KaldiDataType.DoubleMatrix elif obj.dtype == np.int32: return KaldiDataType.Int32Vector elif len(obj) == 2 and \ len(obj[0].shape) == 2 and \ (obj[0].dtype == np.float32 and \ not KaldiDataType.BaseMatrix.is_double) or \ (obj[0].dtype == np.float64 and \ KaldiDataType.BaseMatrix.is_double) and \ (isinstance(obj[1], int) or isinstance(obj[1], float)): return KaldiDataType.WaveMatrix except AttributeError: pass if not len(obj): return KaldiDataType.BaseMatrix elif all(isinstance(x, str) or isinstance(x, text) for x in obj): return KaldiDataType.TokenVector elif all(isinstance(x, int) for x in obj): return KaldiDataType.Int32Vector elif all(hasattr(x, '__len__') and hasattr(x, '__getitem__') for x in obj): if all(all(isinstance(y, int) for y in x) for x in obj): return KaldiDataType.Int32VectorVector try: if all(len(x) == 2 and all(np.isreal(y) for y in x) for x in obj): return KaldiDataType.BasePairVector elif len(np.array(obj).astype(np.float64).shape) == 2: return KaldiDataType.DoubleMatrix except ValueError: pass else: try: if len(np.array(obj).astype(np.float64).shape) == 1: return KaldiDataType.DoubleVector except ValueError: pass return None
def hashkey(block, Qangle, W): # gradient gy, gx = np.gradient(block) # 將 2D 矩陣轉換為 1D 數組 gx = gx.ravel() gy = gy.ravel() # SVD G = np.vstack((gx, gy)).T GTWG = G.T.dot(W).dot(G) w, v = np.linalg.eig(GTWG) # 確保 V 和 D 僅有實數 nonzerow = np.count_nonzero(np.isreal(w)) nonzerov = np.count_nonzero(np.isreal(v)) if nonzerow != 0: w = np.real(w) if nonzerov != 0: v = np.real(v) # 根據 w 的降序對 w 和 v 進行排序 idx = w.argsort()[::-1] w = w[idx] v = v[:, idx] # theta theta = atan2(v[1, 0], v[0, 0]) if theta < 0: theta += pi # lamda lamda = w[0] # u sqrtlamda1 = np.sqrt(w[0]) sqrtlamda2 = np.sqrt(w[1]) # bug if sqrtlamda1 + sqrtlamda2 == 0: u = 0 else: u = (sqrtlamda1 - sqrtlamda2) / (sqrtlamda1 + sqrtlamda2) # 量化 angle = floor(theta / pi * Qangle) if lamda < 0.0001: strength = 0 elif lamda > 0.001: strength = 2 else: strength = 1 if u < 0.25: coherence = 0 elif u > 0.5: coherence = 2 else: coherence = 1 # 將輸出綁定到所需範圍 if angle > 23: angle = 23 elif angle < 0: angle = 0 return angle, strength, coherence
def parallel_coordinates(frame, class_column, cols=None, ax=None, color=None, use_columns=False, xticks=None, colormap=None, axvlines=True, axvlines_kwds=None, sort_labels=False, **kwds): """Parallel coordinates plotting. Parameters ---------- frame: DataFrame class_column: str Column name containing class names cols: list, optional A list of column names to use ax: matplotlib.axis, optional matplotlib axis object color: list or tuple, optional Colors to use for the different classes use_columns: bool, optional If true, columns will be used as xticks xticks: list or tuple, optional A list of values to use for xticks colormap: str or matplotlib colormap, default None Colormap to use for line colors. axvlines: bool, optional If true, vertical lines will be added at each xtick axvlines_kwds: keywords, optional Options to be passed to axvline method for vertical lines sort_labels: bool, False Sort class_column labels, useful when assigning colors .. versionadded:: 0.20.0 kwds: keywords Options to pass to matplotlib plotting method Returns ------- ax: matplotlib axis object Examples -------- >>> from pandas import read_csv >>> from pandas.tools.plotting import parallel_coordinates >>> from matplotlib import pyplot as plt >>> df = read_csv('https://raw.github.com/pandas-dev/pandas/master' '/pandas/tests/data/iris.csv') >>> parallel_coordinates(df, 'Name', color=('#556270', '#4ECDC4', '#C7F464')) >>> plt.show() """ if axvlines_kwds is None: axvlines_kwds = {'linewidth': 1, 'color': 'black'} import matplotlib.pyplot as plt n = len(frame) classes = frame[class_column].drop_duplicates() class_col = frame[class_column] if cols is None: df = frame.drop(class_column, axis=1) else: df = frame[cols] used_legends = set([]) ncols = len(df.columns) # determine values to use for xticks if use_columns is True: if not np.all(np.isreal(list(df.columns))): raise ValueError('Columns must be numeric to be used as xticks') x = df.columns elif xticks is not None: if not np.all(np.isreal(xticks)): raise ValueError('xticks specified must be numeric') elif len(xticks) != ncols: raise ValueError('Length of xticks must match number of columns') x = xticks else: x = lrange(ncols) if ax is None: ax = plt.gca() color_values = _get_standard_colors(num_colors=len(classes), colormap=colormap, color_type='random', color=color) if sort_labels: classes = sorted(classes) color_values = sorted(color_values) colors = dict(zip(classes, color_values)) for i in range(n): y = df.iloc[i].values kls = class_col.iat[i] label = pprint_thing(kls) if label not in used_legends: used_legends.add(label) ax.plot(x, y, color=colors[kls], label=label, **kwds) else: ax.plot(x, y, color=colors[kls], **kwds) if axvlines: for i in x: ax.axvline(i, **axvlines_kwds) ax.set_xticks(x) ax.set_xticklabels(df.columns) ax.set_xlim(x[0], x[-1]) ax.legend(loc='upper right') ax.grid() return ax
def create_target_list(p_elg=1., p_lbg=1., p_qso=1., nobs=1): """ This function creates target list files for the Fiber-to-Target Allocator. :param p_elg: priority score for the ELGs, either a scalar float to give all ELGs the same score, or a string to force random scores :param p_lbg: same as p_elg but for the LBGs :param p_qso: same as p_elg but for the QSOs :param nobs: number of observations required (more than 1 to test dithering or multiple iterations for instance) :return: nothing, it just writes files """ # Read full KiDS catalog from Christophe (7,303,813 entries too) hdul = fits.open('TARGETS/kids_dr4_171ra190_23.0r24.5.01Apr2020.fits') cat = hdul[1].data # Get coordinates ra = cat['RAJ2000'] dec = cat['DECJ2000'] # Get mask mask = cat['MASK'] # Get magnitudes umag, gmag, rmag = cat['MAG_GAAP_u'], cat['MAG_GAAP_g'], cat['MAG_GAAP_r'] imag, zmag, ymag = cat['MAG_GAAP_i'], cat['MAG_GAAP_z'], cat['MAG_GAAP_y'] jmag, hmag, kmag = cat['MAG_GAAP_J'], cat['MAG_GAAP_H'], cat['MAG_GAAP_Ks'] # Get magnitudes errors umag_err, gmag_err, rmag_err = cat['MAGERR_GAAP_u'], cat[ 'MAGERR_GAAP_g'], cat['MAGERR_GAAP_r'] imag_err, zmag_err, ymag_err = cat['MAGERR_GAAP_i'], cat[ 'MAGERR_GAAP_Z'], cat['MAGERR_GAAP_Y'] jmag_err, hmag_err, kmag_err = cat['MAGERR_GAAP_J'], cat[ 'MAGERR_GAAP_H'], cat['MAGERR_GAAP_Ks'] # Compute colors u_g = umag - gmag g_r = gmag - rmag r_i = rmag - imag r_J = rmag - jmag r_H = rmag - hmag r_Ks = rmag - kmag # Galaxy/star selection (0 for galaxies, 1 for stars ... in between most of the time?) sgclass = cat['CLASS_STAR'] # Selection star star = (sgclass > 0.985) & (rmag < 23.5) & (rmag_err < 0.2) # for bit in [1, 2, 3]: star &= ((mask & 2 ** bit) == 0) # Compute coverage area = (np.sin(np.min(dec) / 180 * np.pi) - np.sin(np.max(dec) / 180 * np.pi)) * (np.min(ra / 180 * np.pi) - np.max(ra / 180 * np.pi)) area *= (180 / np.pi)**2 # square degrees # LBG selection color_box_lbg = (u_g > 0.0) & (g_r < 1.0) & ((u_g > 2 * g_r + 0.6) | (g_r < 0)) sel_lbg = color_box_lbg & (rmag > 23.0) & (rmag < 24.5) & (rmag_err < 0.4) # ELG selection color_box_elg = (u_g < 0.5) & (g_r > 0.0) & (g_r < 0.3) & (r_i < 0.3) sel_elg = color_box_elg & (rmag > 23.0) & (rmag < 23.74) & (rmag_err < 0.4) # Ly-a QSO selection # Parameters defined by X. Fan c1 = 0.95 * u_g + 0.31 * g_r + 0.11 * r_i c2 = 0.07 * u_g - 0.49 * g_r + 0.87 * r_i c3 = -0.39 * u_g + 0.79 * g_r + 0.47 * r_i # All criteria together color_box_qso = (u_g > 0.3) & (c3 < 0.7 - 0.5 * c1) & (c1 > 0.3) & (g_r < 0.6) nir_color = (r_J < 1.0) & (r_J > -0.5) & (r_H > -0.3) & (r_Ks > -0.2) psf_object = (sgclass > 0.93) sel_qso = color_box_qso & (rmag > 19.0) & (rmag < 24.0) & ( rmag_err < 0.2) & psf_object & nir_color # apply photometric mask for bit in [1, 2, 3]: sel_lbg &= ((mask & 2**bit) == 0) sel_elg &= ((mask & 2**bit) == 0) sel_qso &= ((mask & 2**bit) == 0) # number of ELG number of LBG print(f"{len(ra[sel_lbg])} LBG total, " f"{len(ra[sel_lbg]) / area} LBG per sq.deg, " f"{len(ra[sel_lbg]) / area * 1.5} LBG per FoV") print(f"{len(ra[sel_elg])} ELG total, " f"{len(ra[sel_elg]) / area} ELG per sq.deg, " f"{len(ra[sel_elg]) / area * 1.5} ELG per FoV") print(f"{len(ra[sel_qso])} QSO total, " f"{len(ra[sel_qso]) / area} QSO per sq.deg, " f"{len(ra[sel_qso]) / area * 1.5} QSO per FoV") # make lists for small and large field # (RA, DEC, Nexp, Nrep, umag, gmag, rmag, imag, zmag, Jmag, Hmag, priority, spriority) names = [ 'PID', 'RAJ2000', 'DECJ2000', 'spectro', 'umag', 'gmag', 'rmag', 'imag', 'zmag', 'Jmag', 'Hmag', 'Nobsreq', 'Nrepeat', 'priority', 'surveypriority', 'Nobsdone' ] for i in range(2): if i == 0: # make a list within a 2 by 2 sq.degree box sel_lbg = sel_lbg & (ra > 178.) & (ra < 180) & (dec < 1) & (dec > -1) sel_elg = sel_elg & (ra > 178.) & (ra < 180) & (dec < 1) & (dec > -1) sel_qso = sel_qso & (ra > 178.) & (ra < 180) & (dec < 1) & (dec > -1) file = 'large' else: # make a list within a 200 by 200 sq.arcsec box sel_lbg = sel_lbg & (ra > 178.9) & (ra < 179) & (dec < .1) & (dec > -.1) sel_elg = sel_elg & (ra > 178.9) & (ra < 179) & (dec < .1) & (dec > -.1) sel_qso = sel_qso & (ra > 178.9) & (ra < 179) & (dec < .1) & (dec > -.1) file = 'small' # Deal with random priorities if not np.isreal(p_lbg): p_lbg_n = np.random.randint(1, 10, len(sel_lbg[sel_lbg])) * 1. # rng.choice([0.25, 0.42, 0.69, 0.6, 1., 1.67, 1.44, 2.4, 4], size=len(sel_lbg[sel_lbg])) else: p_lbg_n = np.ones_like(range(len(sel_lbg[sel_lbg]))) * p_lbg if not np.isreal(p_elg): p_elg_n = np.random.randint(1, 10, len(sel_elg[sel_elg])) * 1. else: p_elg_n = np.ones_like(range(len(sel_elg[sel_elg]))) * p_elg if not np.isreal(p_qso): p_qso_n = np.random.randint(1, 10, len(sel_qso[sel_qso])) * 1. else: p_qso_n = np.ones_like(range(len(sel_qso[sel_qso]))) * p_qso # create Table for LBG PID = ['cosmo_lbg' for i in range(len(sel_lbg[sel_lbg]))] lr = ['LR' for i in range(len(sel_lbg[sel_lbg]))] data_lbg = Table([ PID, ra[sel_lbg], dec[sel_lbg], lr, umag[sel_lbg], gmag[sel_lbg], rmag[sel_lbg], imag[sel_lbg], zmag[sel_lbg], jmag[sel_lbg], hmag[sel_lbg], ra[sel_lbg] * 0 + nobs, ra[sel_lbg] * 0 + 1, ra[sel_lbg] * 0 + p_lbg_n, ra[sel_lbg] * 0 + 1, ra[sel_lbg] * 0 ], names=names) # add ELG PID = ['cosmo_elg' for i in range(len(sel_elg[sel_elg]))] lr = ['LR' for i in range(len(sel_elg[sel_elg]))] data_elg = Table([ PID, ra[sel_elg], dec[sel_elg], lr, umag[sel_elg], gmag[sel_elg], rmag[sel_elg], imag[sel_elg], zmag[sel_elg], jmag[sel_elg], hmag[sel_elg], ra[sel_elg] * 0 + nobs, ra[sel_elg] * 0 + 1, ra[sel_elg] * 0 + p_elg_n, ra[sel_elg] * 0 + 1, ra[sel_elg] * 0 ], names=names) # add QSO PID = ['cosmo_qso' for i in range(len(sel_qso[sel_qso]))] lr = ['LR' for i in range(len(sel_qso[sel_qso]))] data_qso = Table([ PID, ra[sel_qso], dec[sel_qso], lr, umag[sel_qso], gmag[sel_qso], rmag[sel_qso], imag[sel_qso], zmag[sel_qso], jmag[sel_qso], hmag[sel_qso], ra[sel_qso] * 0 + nobs, ra[sel_qso] * 0 + 1, ra[sel_qso] * 0 + p_qso_n, ra[sel_qso] * 0 + 1, ra[sel_qso] * 0 ], names=names) # concatenate tables and save into file vstack([data_lbg, data_elg, data_qso]).write('TARGETS/cosmo_targets_' + file + '_new.csv', overwrite=True, format='csv')
def gaussIOD(coords, observation_times, coords_obs, velocity_method="gibbs", light_time=True, iterate=True, iterator="state transition", mu=MU, max_iter=10, tol=1e-15): """ Compute up to three intial orbits using three observations in angular equatorial coordinates. Parameters ---------- coords : `~numpy.ndarray` (3, 2) RA and Dec of three observations in units of degrees. observation_times : `~numpy.ndarray` (3) Times of the three observations in units of decimal days (MJD or JD for example). coords_obs : `~numpy.ndarray` (3, 2) Heliocentric position vector of the observer at times t in units of AU. velocity_method : {'gauss', gibbs', 'herrick+gibbs'}, optional Which method to use for calculating the velocity at the second observation. [Default = 'gibbs'] light_time : bool, optional Correct for light travel time. [Default = True] iterate : bool, optional Iterate initial orbit using universal anomaly to better approximate the Lagrange coefficients. mu : float, optional Gravitational parameter (GM) of the attracting body in units of AU**3 / d**2. max_iter : int, optional Maximum number of iterations over which to converge to solution. tol : float, optional Numerical tolerance to which to compute chi using the Newtown-Raphson method. Returns ------- epochs : `~numpy.ndarry` (<3) Epochs in units of decimal days at which preliminary orbits are defined. orbits : `~numpy.ndarray` ((<3, 6) or (0)) Up to three preliminary orbits (as cartesian state vectors). """ coords = np.array([np.ones(len(coords)), coords[:, 0], coords[:, 1]]).T.copy() rho = transformCoordinates(coords, "equatorial", "ecliptic", representation_in="spherical", representation_out="cartesian") rho1_hat = rho[0, :] rho2_hat = rho[1, :] rho3_hat = rho[2, :] q1 = coords_obs[0, :] q2 = coords_obs[1, :] q3 = coords_obs[2, :] q2_mag = np.linalg.norm(q2) # Make sure rhohats are unit vectors rho1_hat = rho1_hat / np.linalg.norm(rho1_hat) rho2_hat = rho2_hat / np.linalg.norm(rho2_hat) rho3_hat = rho3_hat / np.linalg.norm(rho3_hat) t1 = observation_times[0] t2 = observation_times[1] t3 = observation_times[2] t31 = t3 - t1 t21 = t2 - t1 t32 = t3 - t2 A = _calcA(q1, q2, q3, rho1_hat, rho3_hat, t31, t32, t21) B = _calcB(q1, q3, rho1_hat, rho3_hat, t31, t32, t21) V = _calcV(rho1_hat, rho2_hat, rho3_hat) coseps2 = np.dot(q2, rho2_hat) / q2_mag C0 = V * t31 * q2_mag**4 / B h0 = -A / B if np.isnan(C0) or np.isnan(h0): return np.array([]) # Find roots to eighth order polynomial all_roots = roots([ C0**2, 0, -q2_mag**2 * (h0**2 + 2 * C0 * h0 * coseps2 + C0**2), 0, 0, 2 * q2_mag**5 * (h0 + C0 * coseps2), 0, 0, -q2_mag**8 ]) # Keep only positive real roots (which should at most be 3) r2_mags = np.real(all_roots[np.isreal(all_roots) & (np.real(all_roots) >= 0)]) num_solutions = len(r2_mags) if num_solutions == 0: return np.array([]) orbits = [] epochs = [] for r2_mag in r2_mags: lambda1, lambda3 = _calcLambdas(r2_mag, t31, t32, t21) rho1, rho2, rho3 = _calcRhos(lambda1, lambda3, q1, q2, q3, rho1_hat, rho2_hat, rho3_hat, V) # Test if we get the same rho2 as using equation 22 in Milani et al. 2008 rho2_mag = (h0 - q2_mag**3 / r2_mag**3) * q2_mag / C0 #np.testing.assert_almost_equal(np.dot(rho2_mag, rho2_hat), rho2) r1 = q1 + rho1 r2 = q2 + rho2 r3 = q3 + rho3 if velocity_method == "gauss": v2 = calcGauss(r1, r2, r3, t1, t2, t3) elif velocity_method == "gibbs": v2 = calcGibbs(r1, r2, r3) elif velocity_method == "herrick+gibbs": v2 = calcHerrickGibbs(r1, r2, r3, t1, t2, t3) else: raise ValueError( "velocity_method should be one of {'gauss', 'gibbs', 'herrick+gibbs'}" ) epoch = t2 orbit = np.concatenate([r2, v2]) if iterate == True: if iterator == "state transition": orbit = iterateStateTransition(orbit, t21, t32, q1, q2, q3, rho1, rho2, rho3, light_time=light_time, mu=mu, max_iter=max_iter, tol=tol) #if light_time == True: # rho2_mag = np.linalg.norm(orbit[:3] - q2) # lt = rho2_mag / C # epoch -= lt if np.linalg.norm(orbit[3:]) >= C: print("Velocity is greater than speed of light!") if (np.linalg.norm(orbit[:3]) > 300.) and (np.linalg.norm(orbit[3:]) > 25.): continue orbits.append(orbit) epochs.append(epoch) orbits = np.array(orbits) orbits = orbits[~np.isnan(orbits).any(axis=1)] epochs = np.array(epochs) epochs = epochs[~np.isnan(orbits).any(axis=1)] return epochs, orbits
def quadgr(fun, a, b, abseps=1e-5, max_iter=17): ''' Gauss-Legendre quadrature with Richardson extrapolation. [Q,ERR] = QUADGR(FUN,A,B,TOL) approximates the integral of a function FUN from A to B with an absolute error tolerance TOL. FUN is a function handle and must accept vector arguments. TOL is 1e-6 by default. Q is the integral approximation and ERR is an estimate of the absolute error. QUADGR uses a 12-point Gauss-Legendre quadrature. The error estimate is based on successive interval bisection. Richardson extrapolation accelerates the convergence for some integrals, especially integrals with endpoint singularities. Examples -------- >>> import numpy as np >>> Q, err = quadgr(np.log,0,1) >>> quadgr(np.exp,0,9999*1j*np.pi) (-2.0000000000122662, 2.1933237448479304e-09) >>> quadgr(lambda x: np.sqrt(4-x**2),0,2,1e-12) (3.1415926535897811, 1.5809575870662229e-13) >>> quadgr(lambda x: x**-0.75,0,1) (4.0000000000000266, 5.6843418860808015e-14) >>> quadgr(lambda x: 1./np.sqrt(1-x**2),-1,1) (3.141596056985029, 6.2146261559092864e-06) >>> quadgr(lambda x: np.exp(-x**2),-np.inf,np.inf,1e-9) #% sqrt(pi) (1.7724538509055152, 1.9722334876348668e-11) >>> quadgr(lambda x: np.cos(x)*np.exp(-x),0,np.inf,1e-9) (0.50000000000000044, 7.3296813063450372e-11) See also -------- QUAD, QUADGK ''' # Author: [email protected], 2009. license BSD # Order limits (required if infinite limits) if a == b: Q = b - a err = b - a return Q, err elif np.real(a) > np.real(b): reverse = True a, b = b, a else: reverse = False # Infinite limits if np.isinf(a) | np.isinf(b): # Check real limits if ~np.isreal(a) | ~np.isreal(b) | np.isnan(a) | np.isnan(b): raise ValueError('Infinite intervals must be real.') # Change of variable if np.isfinite(a) & np.isinf(b): # a to inf fun1 = lambda t: fun(a + t / (1 - t)) / (1 - t)**2 [Q, err] = quadgr(fun1, 0, 1, abseps) elif np.isinf(a) & np.isfinite(b): # -inf to b fun2 = lambda t: fun(b + t / (1 + t)) / (1 + t)**2 [Q, err] = quadgr(fun2, -1, 0, abseps) else: # -inf to inf fun1 = lambda t: fun(t / (1 - t)) / (1 - t)**2 fun2 = lambda t: fun(t / (1 + t)) / (1 + t)**2 [Q1, err1] = quadgr(fun1, 0, 1, abseps / 2) [Q2, err2] = quadgr(fun2, -1, 0, abseps / 2) Q = Q1 + Q2 err = err1 + err2 # Reverse direction if reverse: Q = -Q return Q, err # Gauss-Legendre quadrature (12-point) xq = np.asarray([ 0.12523340851146894, 0.36783149899818018, 0.58731795428661748, 0.76990267419430469, 0.9041172563704748, 0.98156063424671924 ]) wq = np.asarray([ 0.24914704581340288, 0.23349253653835478, 0.20316742672306584, 0.16007832854334636, 0.10693932599531818, 0.047175336386511842 ]) xq = np.hstack((xq, -xq)) wq = np.hstack((wq, wq)) nq = len(xq) dtype = np.result_type(fun(a), fun(b)) # Initiate vectors Q0 = zeros(max_iter, dtype=dtype) # Quadrature Q1 = zeros(max_iter, dtype=dtype) # First Richardson extrapolation Q2 = zeros(max_iter, dtype=dtype) # Second Richardson extrapolation # One interval hh = (b - a) / 2 # Half interval length x = (a + b) / 2 + hh * xq # Nodes # Quadrature Q0[0] = hh * np.sum(wq * fun(x), axis=0) # Successive bisection of intervals for k in range(1, max_iter): # Interval bisection hh = hh / 2 x = np.hstack([x + a, x + b]) / 2 # Quadrature Q0[k] = hh * \ np.sum(wq * np.sum(np.reshape(fun(x), (-1, nq)), axis=0), axis=0) # Richardson extrapolation if k >= 5: Q1[k] = richardson(Q0, k) Q2[k] = richardson(Q1, k) elif k >= 3: Q1[k] = richardson(Q0, k) # Estimate absolute error if k >= 6: Qv = np.hstack((Q0[k], Q1[k], Q2[k])) Qw = np.hstack((Q0[k - 1], Q1[k - 1], Q2[k - 1])) elif k >= 4: Qv = np.hstack((Q0[k], Q1[k])) Qw = np.hstack((Q0[k - 1], Q1[k - 1])) else: Qv = np.atleast_1d(Q0[k]) Qw = Q0[k - 1] errors = np.atleast_1d(abs(Qv - Qw)) j = errors.argmin() err = errors[j] Q = Qv[j] if k >= 2: # and not iscomplex: _val, err1 = dea3(Q0[k - 2], Q0[k - 1], Q0[k]) # Convergence if (err < abseps) | ~np.isfinite(Q): break else: warnings.warn('Max number of iterations reached without convergence.') if ~np.isfinite(Q): warnings.warn('Integral approximation is Infinite or NaN.') # The error estimate should not be zero err = err + 2 * np.finfo(Q).eps # Reverse direction if reverse: Q = -Q return Q, err
def test_epoch(self, env, nb_episodes=1, action_repetition=1, callbacks=None, visualize=True, nb_max_episode_steps=None, nb_max_start_steps=0, start_step_policy=None, verbose=1): """Callback that is called before training begins." """ if not self.compiled: raise RuntimeError('Your tried to test your agent but it hasn\'t been compiled yet. Please call `compile()` before `test()`.') if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format(action_repetition)) self.training = False step = 0 callbacks = [] if not callbacks else callbacks[:] if verbose >= 1: callbacks += [TestLogger()] if visualize: callbacks += [Visualizer()] history = History() callbacks += [history] callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_episodes': nb_episodes, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_test_begin() callbacks.on_train_begin() episode_rewards = [] for episode in range(nb_episodes): callbacks.on_episode_begin(episode) episode_reward = 0. episode_step = 0 # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) assert observation is not None for _ in range(self.no_ops): action = 0 if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, reward, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, reward, done, info = self.processor.process_step(observation, reward, done, info) callbacks.on_action_end(action) if done: warnings.warn('Env ended before {} No-ops could be performed at the start. You should probably lower the no_ops parameter') observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) break # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint(nb_max_start_steps) for _ in range(nb_random_start_steps): if start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, r, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step(observation, r, done, info) callbacks.on_action_end(action) if done: warnings.warn('Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.'.format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) break # Run the episode until we're done. done = False while not done: callbacks.on_step_begin(episode_step) action = self.forward(observation) if self.processor is not None: action = self.processor.process_action(action) reward = 0. accumulated_info = {} for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, d, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, d, info = self.processor.process_step(observation, r, d, info) callbacks.on_action_end(action) reward += r for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value if d: done = True break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: done = True self.backward(reward, terminal=done) episode_reward += reward step_logs = { 'action': action, 'observation': observation, 'reward': reward, 'episode': episode, 'info': accumulated_info, } callbacks.on_step_end(episode_step, step_logs) episode_step += 1 step += 1 episode_rewards.append(episode_reward) # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # Report end of episode. episode_logs = { 'episode_reward': episode_reward, 'nb_steps': episode_step, } callbacks.on_episode_end(episode, episode_logs) callbacks.on_train_end() self._on_test_end() self.training = True return history, np.mean(episode_rewards)
def predict(seq, aa_cut=-1, percent_peptide=-1, model=None, model_file=None, pam_audit=True, length_audit=False, learn_options_override=None): """ if pam_audit==False, then it will not check for GG in the expected position this is useful if predicting on PAM mismatches, such as with off-target """ # assert not (model is None and model_file is None), "you have to specify either a model or a model_file" assert isinstance(seq, (np.ndarray)), "Please ensure seq is a numpy array" assert len(seq[0]) > 0, "Make sure that seq is not empty" assert isinstance(seq[0], str), "Please ensure input sequences are in string format, i.e. 'AGAG' rather than ['A' 'G' 'A' 'G'] or alternate representations" if aa_cut is not None: assert len(aa_cut) > 0, "Make sure that aa_cut is not empty" assert isinstance(aa_cut, (np.ndarray)), "Please ensure aa_cut is a numpy array" assert np.all(np.isreal(aa_cut)), "amino-acid cut position needs to be a real number" if percent_peptide is not None: assert len(percent_peptide) > 0, "Make sure that percent_peptide is not empty" assert isinstance(percent_peptide, (np.ndarray)), "Please ensure percent_peptide is a numpy array" assert np.all(np.isreal(percent_peptide)), "percent_peptide needs to be a real number" if model_file is None: azimuth_saved_model_dir = os.path.join(os.path.dirname(azimuth.__file__), 'saved_models') #print(azimuth_saved_model_dir) if np.any(percent_peptide == -1) or (percent_peptide is None and aa_cut is None): #print("No model file specified, using V3_model_nopos") model_name = 'V3_model_nopos.pickle' else: #print("No model file specified, using V3_model_full") model_name = 'V3_model_full.pickle' model_file = os.path.join(azimuth_saved_model_dir, model_name) if model is None: with open(model_file, 'rb') as f: model, learn_options = pickle.load(f) else: model, learn_options = model learn_options["V"] = 2 learn_options = override_learn_options(learn_options_override, learn_options) # Y, feature_sets, target_genes, learn_options, num_proc = setup(test=False, order=2, learn_options=learn_options, data_file=test_filename) # inputs, dim, dimsum, feature_names = pd.concatenate_feature_sets(feature_sets) Xdf = pandas.DataFrame(columns=[u'30mer', u'Strand'], data=zip(seq, ['NA' for x in range(len(seq))])) if np.all(percent_peptide != -1) and (percent_peptide is not None and aa_cut is not None): gene_position = pandas.DataFrame(columns=[u'Percent Peptide', u'Amino Acid Cut position'], data=zip(percent_peptide, aa_cut)) else: gene_position = pandas.DataFrame(columns=[u'Percent Peptide', u'Amino Acid Cut position'], data=zip(np.ones(seq.shape[0])*-1, np.ones(seq.shape[0])*-1)) feature_sets = feat.featurize_data(Xdf, learn_options, pandas.DataFrame(), gene_position, pam_audit=pam_audit, length_audit=length_audit) inputs, dim, dimsum, feature_names = azimuth.util.concatenate_feature_sets(feature_sets) #print "CRISPR" #pandas.DataFrame(inputs).to_csv("CRISPR.inputs.test.csv") #import ipdb; ipdb.set_trace() # call to scikit-learn, returns a vector of predicted values preds = model.predict(inputs) # also check that predictions are not 0/1 from a classifier.predict() (instead of predict_proba() or decision_function()) unique_preds = np.unique(preds) ok = False for pr in preds: if pr not in [0,1]: ok = True assert ok, "model returned only 0s and 1s" return preds
def parallel_coordinates( frame, class_column, cols=None, ax=None, color=None, use_columns=False, xticks=None, colormap=None, axvlines=True, axvlines_kwds=None, sort_labels=False, **kwds ): import matplotlib.pyplot as plt if axvlines_kwds is None: axvlines_kwds = {"linewidth": 1, "color": "black"} n = len(frame) classes = frame[class_column].drop_duplicates() class_col = frame[class_column] if cols is None: df = frame.drop(class_column, axis=1) else: df = frame[cols] used_legends = set() ncols = len(df.columns) # determine values to use for xticks if use_columns is True: if not np.all(np.isreal(list(df.columns))): raise ValueError("Columns must be numeric to be used as xticks") x = df.columns elif xticks is not None: if not np.all(np.isreal(xticks)): raise ValueError("xticks specified must be numeric") elif len(xticks) != ncols: raise ValueError("Length of xticks must match number of columns") x = xticks else: x = list(range(ncols)) if ax is None: ax = plt.gca() color_values = _get_standard_colors( num_colors=len(classes), colormap=colormap, color_type="random", color=color ) if sort_labels: classes = sorted(classes) color_values = sorted(color_values) colors = dict(zip(classes, color_values)) for i in range(n): y = df.iloc[i].values kls = class_col.iat[i] label = pprint_thing(kls) if label not in used_legends: used_legends.add(label) ax.plot(x, y, color=colors[kls], label=label, **kwds) else: ax.plot(x, y, color=colors[kls], **kwds) if axvlines: for i in x: ax.axvline(i, **axvlines_kwds) ax.set_xticks(x) ax.set_xticklabels(df.columns) ax.set_xlim(x[0], x[-1]) ax.legend(loc="upper right") ax.grid() return ax
def coeffs2vals(coeffs): """ #COEFFS2VALS Convert Chebyshev coefficients to values at Chebyshev points #of the 2nd kind. # V = COEFFS2VALS(C) returns the values of the polynomial V(i,1) = P(x_i) = # C(1,1)*T_{0}(x_i) + ... + C(N,1)*T_{N-1}(x_i), where the x_i are # 2nd-kind Chebyshev nodes. # # Input: coeffs is numpy.ndarray # Output: values is a numpy.ndarray of the same length as coeffs # # # See also VALS2COEFFS, CHEBPTS. # Copyright 2016 by The University of Oxford and The Chebfun Developers. # See http://www.chebfun.org/ for Chebfun information. ################################################################################ # [Developer Note]: This is equivalent to Discrete Cosine Transform of Type I. # # [Mathematical reference]: Sections 4.7 and 6.3 Mason & Handscomb, "Chebyshev # Polynomials". Chapman & Hall/CRC (2003). ################################################################################ """ # *Note about symmetries* The code below takes steps to # ensure that the following symmetries are enforced: # even Chebyshev COEFFS exactly zero ==> VALUES are exactly odd # odd Chebychev COEFFS exactly zero ==> VALUES are exactly even # These corrections are required because the MATLAB FFT does not # guarantee that these symmetries are enforced. # Get the length of the input: n = len(coeffs) # Trivial case (constant or empty): if n <= 1: values = coeffs.copy() return values # check for symmetry # isEven = max(abs(coeffs(2:2:end,:)),[],1) == 0; # isOdd = max(abs(coeffs(1:2:end,:)),[],1) == 0; # Scale the interior coefficients by 1/2: coeffs[1:n - 1] = coeffs[1:n - 1] / 2 # Mirror the coefficients (to fake a DCT using an FFT): tmp = np.r_[coeffs, coeffs[n - 2:0:-1]] if np.isreal(coeffs).all(): # Real-valued case: values = np.fft.fft(tmp).real elif np.isreal(1j * coeffs).all(): # Imaginary-valued case: values = 1j * (np.fft.fft(tmp.imag).real) else: # General case: values = np.fft.fft(tmp) # Flip and truncate: values = values[n - 1::-1] # [TODO]enforce symmetry # values(:,isEven) = (values(:,isEven)+flipud(values(:,isEven)))/2; # values(:,isOdd) = (values(:,isOdd)-flipud(values(:,isOdd)))/2; return values
def interp_wrapper(vol, coords, interptype='cubic'): """ interp_wrapper(vol, coords, interptype) <vol> is a 3D matrix (can be complex-valued) <coords> is 3 x N with the matrix coordinates to interpolate at. one or more of the entries can be NaN. <interptype> (optional) is 'nearest' | 'linear' | 'cubic' | 'wta'. default: 'cubic'. this is a convenient wrapper for ba_interp3. the main problem with normal calls to ba_interp3 is that it assigns values to interpolation points that lie outside the original data range. what we do is to ensure that coordinates that are outside the original field-of-view (i.e. if the value along a dimension is less than 1 or greater than the number of voxels in the original volume along that dimension) are returned as NaN and coordinates that have any NaNs are returned as NaN. another feature is 'wta' (winner-take-all). this involves the assumption that <vol> contains only discrete integers. each distinct integer is mapped as a binary volume (0s and 1s) using linear interpolation to each coordinate, the integer with the largest resulting value at that coordinate wins, and that coordinate is assigned the winning integer. for complex-valued data, we separately interpolate the real and imaginary parts. history: 2019/09/01 - ported to python by ian charest """ # input if interptype == 'cubic': order = 3 elif interptype == 'linear': order = 1 elif interptype == 'nearest': order = 0 elif interptype == 'wta': order = 1 # linear else: raise ValueError('interpolation method not implemented.') # convert vol to float (needed) # vol = vol.astype(np.float32) # bad locations must get set to NaN bad = np.any(isnotfinite(coords), axis=0) coords[:, bad] = 1 # out of range must become NaN, too bad = np.any(np.c_[bad, coords[0, :] < 1, coords[0, :] > vol.shape[0], coords[1, :] < 1, coords[1, :] > vol.shape[1], coords[2, :] < 1, coords[2, :] > vol.shape[2]], axis=1).astype(bool) # resample the volume if not np.any(np.isreal(vol)): # we interpolate the real and imaginary parts independently transformeddata = map_coordinates( np.nan_to_num(np.real(vol)).astype(np.float64), coords, order=order, mode='nearest') + 1j * map_coordinates(np.nan_to_num( np.imag(vol)).astype(np.float64), coords, order=order, mode='nearest') else: # this is the tricky 'wta' case if interptype == 'wta': # figure out the discrete integer labels alllabels = np.unique(vol.ravel()) assert np.all(np.isfinite(alllabels)) if len(alllabels) > 1000: print('warning: more than 1000 labels are present') # loop over each label allvols = [] for c_label in alllabels: allvols.append( map_coordinates(np.nan_to_num(vol == c_label).astype( np.float64), coords, order=order, mode='nearest')) # make into a numpuy stack allvols = np.vstack(allvols) # which coordinates have no label contribution? realbad = np.sum(allvols, axis=0) == 0 # perform winner-take-all (wta_is is the # index relative to alllabels!) wta_is = np.argmax(allvols, axis=0) # figure out the final labeling scheme transformeddata = alllabels[wta_is] # fill in NaNs for coordinates with no label # contribution and bad coordinates too transformeddata[realbad] = np.nan transformeddata[bad] = np.nan # this is the usual easy case else: # consider using mode constant with a cval. transformeddata = map_coordinates(np.nan_to_num(vol).astype( np.float64), coords, order=order, mode='nearest') transformeddata[bad] = np.nan return transformeddata
def parallel_coordinates(model, data, c=None, cols=None, ax=None, colors=None, use_columns=False, xticks=None, colormap=None, target="top", brush=None, zorder=None, **kwds): if "axes.facecolor" in mpl.rcParams: orig_facecolor = mpl.rcParams["axes.facecolor"] mpl.rcParams["axes.facecolor"] = "white" df = data.as_dataframe(_combine_keys(model.responses.keys(), cols, c)) #, exclude_dtypes=["object"]) if brush is not None: brush_set = BrushSet(brush) assignment = apply_brush(brush_set, data) color_map = brush_color_map(brush_set, assignment) class_col = pd.DataFrame({"class" : assignment})["class"] is_class = True else: if c is None: c = df.columns.values[-1] class_col = df[c] is_class = df.dtypes[c].name == "object" color_map = None if is_class: df = df.drop(c, axis=1) if c in cols: cols.remove(c) else: class_min = class_col.min() class_max = class_col.max() if cols is not None: df = df[cols] df_min = df.min() df_max = df.max() df = (df - df_min) / (df_max - df_min) n = len(df) used_legends = set([]) ncols = len(df.columns) for i in range(ncols): if target == "top": if model.responses[df.columns.values[i]].dir == Response.MINIMIZE: df.ix[:,i] = 1-df.ix[:,i] elif target == "bottom": if model.responses[df.columns.values[i]].dir == Response.MAXIMIZE: df.ix[:,i] = 1-df.ix[:,i] # determine values to use for xticks if use_columns is True: if not np.all(np.isreal(list(df.columns))): raise ValueError('Columns must be numeric to be used as xticks') x = df.columns elif xticks is not None: if not np.all(np.isreal(xticks)): raise ValueError('xticks specified must be numeric') elif len(xticks) != ncols: raise ValueError('Length of xticks must match number of columns') x = xticks else: x = range(ncols) if ax is None: fig = plt.figure() ax = plt.gca() else: fig = ax.get_figure() cmap = plt.get_cmap(colormap) if is_class: if color_map is None: if isinstance(colors, dict): cmap = colors else: from pandas.tools.plotting import _get_standard_colors classes = class_col.drop_duplicates() color_values = _get_standard_colors(num_colors=len(classes), colormap=colormap, color_type='random', color=colors) cmap = dict(zip(classes, color_values)) else: cmap = color_map if zorder is None: indices = range(n) else: indices = [i[0] for i in sorted(enumerate(df[zorder]), key=lambda x : x[1])] for i in indices: y = df.iloc[i].values kls = class_col.iat[i] if is_class: label = str(kls) if label not in used_legends: used_legends.add(label) ax.plot(x, y, label=label, color=cmap[kls], **kwds) else: ax.plot(x, y, color=cmap[kls], **kwds) else: ax.plot(x, y, color=cmap((kls - class_min)/(class_max-class_min)), **kwds) for i in x: ax.axvline(i, linewidth=2, color='black') format = "%.2f" if target == "top": value = df_min[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_max[i] if model.responses[df.columns.values[i]].dir != Response.INFO: format = format + "*" elif target == "bottom": value = df_max[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_min[i] else: value = df_max[i] if model.responses[df.columns.values[i]].dir == Response.MAXIMIZE: format = format + "*" ax.text(i, 1.001, format % value, ha="center", fontsize=10) format = "%.2f" if target == "top": value = df_max[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_min[i] elif target == "bottom": value = df_min[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_max[i] if model.responses[df.columns.values[i]].dir != Response.INFO: format = format + "*" else: value = df_min[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE: format = format + "*" ax.text(i, -0.001, format % value, ha="center", va="top", fontsize=10) ax.set_yticks([]) ax.set_xticks(x) ax.set_xticklabels(df.columns, {"weight" : "bold", "size" : 12}) ax.set_xlim(x[0]-0.1, x[-1]+0.1) ax.tick_params(direction="out", pad=10) bbox_props = dict(boxstyle="rarrow,pad=0.3", fc="white", ec="black", lw=2) if target == "top": ax.text(-0.05, 0.5, "Target", ha="center", va="center", rotation=90, bbox=bbox_props, transform=ax.transAxes) elif target == "bottom": ax.text(-0.05, 0.5, "Target", ha="center", va="center", rotation=-90, bbox=bbox_props, transform=ax.transAxes) if is_class: ax.legend(loc='center right', bbox_to_anchor=(1.25, 0.5)) fig.subplots_adjust(right=0.8) else: cax,_ = mpl.colorbar.make_axes(ax) cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, spacing='proportional', norm=mpl.colors.Normalize(vmin=class_min, vmax=class_max), format='%.2f') cb.set_label(c) cb.set_clim(class_min, class_max) mpl.rcParams["axes.facecolor"] = orig_facecolor return fig
def stability_analysis(derivatives): """Stability analysis of fixed points for low-dimensional system. The analysis is referred to [1]_. Parameters ---------- derivatives : float, tuple, list, np.ndarray The derivative of the f. Returns ------- fp_type : str The type of the fixed point. References ---------- .. [1] http://www.egwald.ca/nonlineardynamics/twodimensionaldynamics.php """ if np.size(derivatives) == 1: # 1D dynamical system if derivatives == 0: return SADDLE_NODE elif derivatives > 0: return UNSTABLE_POINT_1D else: return STABLE_POINT_1D elif np.size(derivatives) == 4: # 2D dynamical system a = derivatives[0][0] b = derivatives[0][1] c = derivatives[1][0] d = derivatives[1][1] # trace p = a + d # det q = a * d - b * c # judgement if q < 0: return SADDLE_NODE elif q == 0: if p <= 0: return CENTER_MANIFOLD else: return UNSTABLE_LINE_2D else: # parabola e = p * p - 4 * q if p == 0: return CENTER_2D elif p > 0: if e < 0: return UNSTABLE_FOCUS_2D elif e > 0: return UNSTABLE_NODE_2D else: w = np.linalg.eigvals(derivatives) if w[0] == w[1]: return UNSTABLE_DEGENERATE_2D else: return UNSTABLE_STAR_2D else: if e < 0: return STABLE_FOCUS_2D elif e > 0: return STABLE_NODE_2D else: w = np.linalg.eigvals(derivatives) if w[0] == w[1]: return STABLE_DEGENERATE_2D else: return STABLE_STAR_2D elif np.size(derivatives) == 9: # 3D dynamical system eigenvalues = np.linalg.eigvals(np.array(derivatives)) is_real = np.isreal(eigenvalues) if is_real.all(): eigenvalues = np.sort(eigenvalues) if eigenvalues[2] < 0: return STABLE_NODE_3D elif eigenvalues[2] == 0: return UNKNOWN_3D else: if eigenvalues[0] > 0: return UNSTABLE_NODE_3D elif eigenvalues[0] == 0: return UNKNOWN_3D else: if eigenvalues[1] < 0: return SADDLE_NODE elif eigenvalues[1] == 0: return UNKNOWN_3D else: return UNSTABLE_SADDLE_3D else: if is_real.sum() == 1: real_id = np.where(is_real)[0] non_real_id = np.where(np.logical_not(is_real))[0] v0 = eigenvalues[real_id] v1 = eigenvalues[non_real_id[0]] v2 = eigenvalues[non_real_id[1]] v1_real = np.real(v1) assert np.conj(v1) == v2 if v0 < 0: if v1_real < 0: return STABLE_FOCUS_3D elif v1_real == 0: # 零实部 return UNKNOWN_3D else: return UNSTABLE_FOCUS_3D elif v0 == 0: if v1_real <= 0: return UNKNOWN_3D # 零实部 else: return UNSTABLE_POINT_3D # TODO else: if v1_real < 0: return UNSTABLE_FOCUS_3D elif v1_real == 0: return UNSTABLE_CENTER_3D else: return UNSTABLE_POINT_3D # TODO # else: # raise ValueError() eigenvalues = np.real(eigenvalues) if np.all(eigenvalues < 0): return STABLE_POINT_3D # TODO else: return UNSTABLE_POINT_3D # TODO else: raise ValueError('Unknown derivatives, only supports the jacobian ' 'matrix with the shape of (1), (2, 2), or (3, 3).')
def bezier_curve_y_out(shift_angle, P0, P1, P2, P3, second_of_day): """ For a cubic Bezier segment described by the 2-tuples P0, ..., P3, return the y-value associated with the given x-value. Ex: getYfromXforBezSegment((10,0), (5,-5), (5,5), (0,0), 3.2) """ try: import numpy as np except ImportError: np = None if not np: return 0 seconds_per_day = 24 * 60 * 60 # Check if the second of the day is provided. # If provided, calculate y of the Bezier curve with that x # Otherwise, use the current second of the day if second_of_day is None: raise Exception("second_of_day must be specified") else: seconds = second_of_day # Shift the entire graph using 0 - 360 to determine the degree if shift_angle: percent_angle = shift_angle / 360 angle_seconds = percent_angle * seconds_per_day if seconds + angle_seconds > seconds_per_day: seconds_shifted = seconds + angle_seconds - seconds_per_day else: seconds_shifted = seconds + angle_seconds percent_of_day = seconds_shifted / seconds_per_day else: percent_of_day = seconds / seconds_per_day x = percent_of_day * (P0[0] - P3[0]) # First, get the t-value associated with x-value, where t is the # parameterization of the Bezier curve and ranges from 0 to 1. # We need the coefficients of the polynomial describing cubic Bezier # (cubic polynomial in t) coefficients = [ -P0[0] + 3 * P1[0] - 3 * P2[0] + P3[0], 3 * P0[0] - 6 * P1[0] + 3 * P2[0], -3 * P0[0] + 3 * P1[0], P0[0] - x ] # Find roots of the polynomial to determine the parameter t roots = np.roots(coefficients) # Find the root which is between 0 and 1, and is also real correct_root = None for root in roots: if np.isreal(root) and 0 <= root <= 1: correct_root = root # Check a valid root was found if correct_root is None: print('Error, no valid root found. Are you sure your Bezier curve ' 'represents a valid function when projected into the xy-plane?') return 0 param_t = correct_root # From the value for the t parameter, find the corresponding y-value # using the formula for cubic Bezier curves y = (1 - param_t)**3 * P0[1] + 3 * (1 - param_t)**2 * param_t * P1[1] + 3 * ( 1 - param_t) * param_t**2 * P2[1] + param_t**3 * P3[1] if not np.isreal(y): raise AssertionError # Typecast y from np.complex128 to float64 y = y.real return y
def polyinterp(points, x_min_bound=None, x_max_bound=None, plot=False): """ Gives the minimizer and minimum of the interpolating polynomial over given points based on function and derivative information. Defaults to bisection if no critical points are valid. Based on polyinterp.m Matlab function in minFunc by Mark Schmidt with some slight modifications. Implemented by: Hao-Jun Michael Shi and Dheevatsa Mudigere Last edited 12/6/18. Inputs: points (nparray): two-dimensional array with each point of form [x f g] x_min_bound (float): minimum value that brackets minimum (default: minimum of points) x_max_bound (float): maximum value that brackets minimum (default: maximum of points) plot (bool): plot interpolating polynomial Outputs: x_sol (float): minimizer of interpolating polynomial F_min (float): minimum of interpolating polynomial Note: . Set f or g to np.nan if they are unknown """ no_points = points.shape[0] order = np.sum(1 - np.isnan(points[:,1:3]).astype('int')) - 1 x_min = np.min(points[:, 0]) x_max = np.max(points[:, 0]) # compute bounds of interpolation area if(x_min_bound is None): x_min_bound = x_min if(x_max_bound is None): x_max_bound = x_max # explicit formula for quadratic interpolation if no_points == 2 and order == 2 and plot is False: # Solution to quadratic interpolation is given by: # a = -(f1 - f2 - g1(x1 - x2))/(x1 - x2)^2 # x_min = x1 - g1/(2a) # if x1 = 0, then is given by: # x_min = - (g1*x2^2)/(2(f2 - f1 - g1*x2)) if(points[0, 0] == 0): x_sol = -points[0, 2]*points[1, 0]**2/(2*(points[1, 1] - points[0, 1] - points[0, 2]*points[1, 0])) else: a = -(points[0, 1] - points[1, 1] - points[0, 2]*(points[0, 0] - points[1, 0]))/(points[0, 0] - points[1, 0])**2 x_sol = points[0, 0] - points[0, 2]/(2*a) x_sol = np.minimum(np.maximum(x_min_bound, x_sol), x_max_bound) # explicit formula for cubic interpolation elif no_points == 2 and order == 3 and plot is False: # Solution to cubic interpolation is given by: # d1 = g1 + g2 - 3((f1 - f2)/(x1 - x2)) # d2 = sqrt(d1^2 - g1*g2) # x_min = x2 - (x2 - x1)*((g2 + d2 - d1)/(g2 - g1 + 2*d2)) d1 = points[0, 2] + points[1, 2] - 3*((points[0, 1] - points[1, 1])/(points[0, 0] - points[1, 0])) d2 = np.sqrt(d1**2 - points[0, 2]*points[1, 2]) if np.isreal(d2): x_sol = points[1, 0] - (points[1, 0] - points[0, 0])*((points[1, 2] + d2 - d1)/(points[1, 2] - points[0, 2] + 2*d2)) x_sol = np.minimum(np.maximum(x_min_bound, x_sol), x_max_bound) else: x_sol = (x_max_bound + x_min_bound)/2 # solve linear system else: # define linear constraints A = np.zeros((0, order+1)) b = np.zeros((0, 1)) # add linear constraints on function values for i in range(no_points): if not np.isnan(points[i, 1]): constraint = np.zeros((1, order+1)) for j in range(order, -1, -1): constraint[0, order - j] = points[i, 0]**j A = np.append(A, constraint, 0) b = np.append(b, points[i, 1]) # add linear constraints on gradient values for i in range(no_points): if not np.isnan(points[i, 2]): constraint = np.zeros((1, order+1)) for j in range(order): constraint[0, j] = (order-j)*points[i,0]**(order-j-1) A = np.append(A, constraint, 0) b = np.append(b, points[i, 2]) # check if system is solvable if(A.shape[0] != A.shape[1] or np.linalg.matrix_rank(A) != A.shape[0]): x_sol = (x_min_bound + x_max_bound)/2 f_min = np.Inf else: # solve linear system for interpolating polynomial coeff = np.linalg.solve(A, b) # compute critical points dcoeff = np.zeros(order) for i in range(len(coeff) - 1): dcoeff[i] = coeff[i]*(order-i) crit_pts = np.array([x_min_bound, x_max_bound]) crit_pts = np.append(crit_pts, points[:, 0]) if not np.isinf(dcoeff).any(): roots = np.roots(dcoeff) crit_pts = np.append(crit_pts, roots) # test critical points f_min = np.Inf x_sol = (x_min_bound + x_max_bound)/2 # defaults to bisection for crit_pt in crit_pts: if np.isreal(crit_pt) and crit_pt >= x_min_bound and crit_pt <= x_max_bound: F_cp = np.polyval(coeff, crit_pt) if np.isreal(F_cp) and F_cp < f_min: x_sol = np.real(crit_pt) f_min = np.real(F_cp) if(plot): plt.figure() x = np.arange(x_min_bound, x_max_bound, (x_max_bound - x_min_bound)/10000) f = np.polyval(coeff, x) plt.plot(x, f) plt.plot(x_sol, f_min, 'x') return x_sol
def _zpk2sos(z, p, k, pairing='nearest'): """ Return second-order sections from zeros, poles, and gain of a system Parameters ---------- z : array_like Zeros of the transfer function. p : array_like Poles of the transfer function. k : float System gain. pairing : {'nearest', 'keep_odd'}, optional The method to use to combine pairs of poles and zeros into sections. See Notes below. Returns ------- sos : ndarray Array of second-order filter coefficients, with shape ``(n_sections, 6)``. See `sosfilt` for the SOS filter format specification. See Also -------- sosfilt Notes ----- The algorithm used to convert ZPK to SOS format is designed to minimize errors due to numerical precision issues. The pairing algorithm attempts to minimize the peak gain of each biquadratic section. This is done by pairing poles with the nearest zeros, starting with the poles closest to the unit circle. *Algorithms* The current algorithms are designed specifically for use with digital filters. Although they can operate on analog filters, the results may be sub-optimal. The steps in the ``pairing='nearest'`` and ``pairing='keep_odd'`` algorithms are mostly shared. The ``nearest`` algorithm attempts to minimize the peak gain, while ``'keep_odd'`` minimizes peak gain under the constraint that odd-order systems should retain one section as first order. The algorithm steps and are as follows: As a pre-processing step, add poles or zeros to the origin as necessary to obtain the same number of poles and zeros for pairing. If ``pairing == 'nearest'`` and there are an odd number of poles, add an additional pole and a zero at the origin. The following steps are then iterated over until no more poles or zeros remain: 1. Take the (next remaining) pole (complex or real) closest to the unit circle to begin a new filter section. 2. If the pole is real and there are no other remaining real poles [#]_, add the closest real zero to the section and leave it as a first order section. Note that after this step we are guaranteed to be left with an even number of real poles, complex poles, real zeros, and complex zeros for subsequent pairing iterations. 3. Else: 1. If the pole is complex and the zero is the only remaining real zero*, then pair the pole with the *next* closest zero (guaranteed to be complex). This is necessary to ensure that there will be a real zero remaining to eventually create a first-order section (thus keeping the odd order). 2. Else pair the pole with the closest remaining zero (complex or real). 3. Proceed to complete the second-order section by adding another pole and zero to the current pole and zero in the section: 1. If the current pole and zero are both complex, add their conjugates. 2. Else if the pole is complex and the zero is real, add the conjugate pole and the next closest real zero. 3. Else if the pole is real and the zero is complex, add the conjugate zero and the real pole closest to those zeros. 4. Else (we must have a real pole and real zero) add the next real pole closest to the unit circle, and then add the real zero closest to that pole. .. [#] This conditional can only be met for specific odd-order inputs with the ``pairing == 'keep_odd'`` method. Examples -------- Design a 6th order low-pass elliptic digital filter for a system with a sampling rate of 8000 Hz that has a pass-band corner frequency of 1000 Hz. The ripple in the pass-band should not exceed 0.087 dB, and the attenuation in the stop-band should be at least 90 dB. In the following call to `signal.ellip`, we could use ``output='sos'``, but for this example, we'll use ``output='zpk'``, and then convert to SOS format with `zpk2sos`: >>> from scipy import signal >>> z, p, k = signal.ellip(6, 0.087, 90, 1000/(0.5*8000), output='zpk') Now convert to SOS format. >>> sos = _zpk2sos(z, p, k) The coefficents of the numerators of the sections: >>> sos[:, :3] array([[ 0.0014154 , 0.00248707, 0.0014154 ], [ 1. , 0.72965193, 1. ], [ 1. , 0.17594966, 1. ]]) The symmetry in the coefficients occurs because all the zeros are on the unit circle. The coefficients of the denominators of the sections: >>> sos[:, 3:] array([[ 1. , -1.32543251, 0.46989499], [ 1. , -1.26117915, 0.6262586 ], [ 1. , -1.25707217, 0.86199667]]) The next example shows the effect of the `pairing` option. We have a system with three poles and three zeros, so the SOS array will have shape (2, 6). The means there is, in effect, an extra pole and an extra zero at the origin in the SOS representation. >>> z1 = np.array([-1, -0.5-0.5j, -0.5+0.5j]) >>> p1 = np.array([0.75, 0.8+0.1j, 0.8-0.1j]) With ``pairing='nearest'`` (the default), we obtain >>> _zpk2sos(z1, p1, 1) array([[ 1. , 1. , 0.5 , 1. , -0.75, 0. ], [ 1. , 1. , 0. , 1. , -1.6 , 0.65]]) The first section has the zeros {-0.5-0.05j, -0.5+0.5j} and the poles {0, 0.75}, and the second section has the zeros {-1, 0} and poles {0.8+0.1j, 0.8-0.1j}. Note that the extra pole and zero at the origin have been assigned to different sections. With ``pairing='keep_odd'``, we obtain: >>> _zpk2sos(z1, p1, 1, pairing='keep_odd') array([[ 1. , 1. , 0. , 1. , -0.75, 0. ], [ 1. , 1. , 0.5 , 1. , -1.6 , 0.65]]) The extra pole and zero at the origin are in the same section. The first section is, in effect, a first-order section. """ # TODO in the near future: # 1. Add SOS capability to `filtfilt`, `freqz`, etc. somehow (#3259). # 2. Make `decimate` use `sosfilt` instead of `lfilter`. # 3. Make sosfilt automatically simplify sections to first order # when possible. Note this might make `sosfiltfilt` a bit harder (ICs). # 4. Further optimizations of the section ordering / pole-zero pairing. # See the wiki for other potential issues. valid_pairings = ['nearest', 'keep_odd'] if pairing not in valid_pairings: raise ValueError('pairing must be one of %s, not %s' % (valid_pairings, pairing)) if len(z) == len(p) == 0: return np.array([[k, 0., 0., 1., 0., 0.]]) # ensure we have the same number of poles and zeros, and make copies p = np.concatenate((p, np.zeros(max(len(z) - len(p), 0)))) z = np.concatenate((z, np.zeros(max(len(p) - len(z), 0)))) n_sections = (max(len(p), len(z)) + 1) // 2 sos = np.zeros((n_sections, 6)) if len(p) % 2 == 1 and pairing == 'nearest': p = np.concatenate((p, [0.])) z = np.concatenate((z, [0.])) assert len(p) == len(z) # Ensure we have complex conjugate pairs # (note that _cplxreal only gives us one element of each complex pair): z = np.concatenate(_cplxreal(z)) p = np.concatenate(_cplxreal(p)) p_sos = np.zeros((n_sections, 2), np.complex128) z_sos = np.zeros_like(p_sos) for si in range(n_sections): # Select the next "worst" pole p1_idx = np.argmin(np.abs(1 - np.abs(p))) p1 = p[p1_idx] p = np.delete(p, p1_idx) # Pair that pole with a zero if np.isreal(p1) and np.isreal(p).sum() == 0: # Special case to set a first-order section z1_idx = _nearest_real_complex_idx(z, p1, 'real') z1 = z[z1_idx] z = np.delete(z, z1_idx) p2 = z2 = 0 else: if not np.isreal(p1) and np.isreal(z).sum() == 1: # Special case to ensure we choose a complex zero to pair # with so later (setting up a first-order section) z1_idx = _nearest_real_complex_idx(z, p1, 'complex') assert not np.isreal(z[z1_idx]) else: # Pair the pole with the closest zero (real or complex) z1_idx = np.argmin(np.abs(p1 - z)) z1 = z[z1_idx] z = np.delete(z, z1_idx) # Now that we have p1 and z1, figure out what p2 and z2 need to be if not np.isreal(p1): if not np.isreal(z1): # complex pole, complex zero p2 = p1.conj() z2 = z1.conj() else: # complex pole, real zero p2 = p1.conj() z2_idx = _nearest_real_complex_idx(z, p1, 'real') z2 = z[z2_idx] assert np.isreal(z2) z = np.delete(z, z2_idx) else: if not np.isreal(z1): # real pole, complex zero z2 = z1.conj() p2_idx = _nearest_real_complex_idx(p, z1, 'real') p2 = p[p2_idx] assert np.isreal(p2) else: # real pole, real zero # pick the next "worst" pole to use idx = np.where(np.isreal(p))[0] assert len(idx) > 0 p2_idx = idx[np.argmin(np.abs(np.abs(p[idx]) - 1))] p2 = p[p2_idx] # find a real zero to match the added pole assert np.isreal(p2) z2_idx = _nearest_real_complex_idx(z, p2, 'real') z2 = z[z2_idx] assert np.isreal(z2) z = np.delete(z, z2_idx) p = np.delete(p, p2_idx) p_sos[si] = [p1, p2] z_sos[si] = [z1, z2] assert len(p) == len(z) == 0 # we've consumed all poles and zeros del p, z # Construct the system, reversing order so the "worst" are last p_sos = np.reshape(p_sos[::-1], (n_sections, 2)) z_sos = np.reshape(z_sos[::-1], (n_sections, 2)) gains = np.ones(n_sections) gains[0] = k for si in range(n_sections): x = zpk2tf(z_sos[si], p_sos[si], gains[si]) sos[si] = np.concatenate(x) return sos
def isLegal(v): return (np.all(np.isreal(v)) and not np.any(np.isnan(v)) and not np.any(np.isinf(v)))
def test_isreal(self): self.check(np.isreal) o = np.isreal(Masked([1. + 1j], mask=False)) assert not o.unmasked and not o.mask o = np.isreal(Masked([1. + 1j], mask=True)) assert not o.unmasked and o.mask
def ut_linci(X, Y, sigX, sigY): # UT_LINCI() # current ellipse parameter uncertainties from cosine/sine coefficient # uncertainties, by linearized relations w/ correlations presumed zero # inputs: (two-dim case complex, one-dim case real) # X = Xu + i*Xv # Y = Yu + i*Yv # for Xu =real(X) = u cosine coeff; Yu =real(Y) = u sine coeff # Xv =imag(X) = v cosine coeff; Yv =imag(Y) = v sine coeff # sigX = sigXu + i*sigXv # sigY = sigYu + i*sigYv # for sigXu =real(sigX) =stddev(Xu); sigYu =real(sigY) =stddev(Yu) # sigXv =imag(sigX) =stddev(Xv); sigYv =imag(sigY) =stddev(Yv) # outputs: # two-dim case, complex # sig1 = sig_Lsmaj +1i*sig_Lsmin [same units as inputs] # sig2 = sig_g + 1i*sig_theta [degrees] # one-dim case, real # sig1 = sig_A [same units as inputs] # sig2 = sig_g [degrees] # UTide v1p0 9/2011 [email protected] # (adapted from errell.m of t_tide, Pawlowicz et al 2002) X = np.array([X]) Y = np.array([Y]) sigX = np.array([sigX]) sigY = np.array([sigY]) Xu = np.real(X[:]) sigXu = np.real(sigX) Yu = np.real(Y[:]) sigYu = np.real(sigY) Xv = np.imag(X[:]) sigXv = np.imag(sigX[:]) Yv = np.imag(Y[:]) sigYv = np.imag(sigY[:]) rp = 0.5 * np.sqrt((Xu + Yv)**2 + (Xv - Yu)**2) rm = 0.5 * np.sqrt((Xu - Yv)**2 + (Xv + Yu)**2) sigXu2 = sigXu**2 sigYu2 = sigYu**2 sigXv2 = sigXv**2 sigYv2 = sigYv**2 ex = (Xu + Yv) / rp fx = (Xu - Yv) / rm gx = (Yu - Xv) / rp hx = (Yu + Xv) / rm # major axis dXu2 = (0.25 * (ex + fx))**2 dYu2 = (0.25 * (gx + hx))**2 dXv2 = (0.25 * (hx - gx))**2 dYv2 = (0.25 * (ex - fx))**2 sig1 = np.sqrt(dXu2 * sigXu2 + dYu2 * sigYu2 + dXv2 * sigXv2 + dYv2 * sigYv2) # phase rn = 2 * (Xu * Yu + Xv * Yv) rd = Xu**2 - Yu**2 + Xv**2 - Yv**2 den = rn**2 + rd**2 dXu2 = ((rd * Yu - rn * Xu) / den)**2 dYu2 = ((rd * Xu + rn * Yu) / den)**2 dXv2 = ((rd * Yv - rn * Xv) / den)**2 dYv2 = ((rd * Xv + rn * Yv) / den)**2 sig2 = (180 / np.pi) * np.sqrt(dXu2 * sigXu2 + dYu2 * sigYu2 + dXv2 * sigXv2 + dYv2 * sigYv2) # if ~isreal(X) if not np.isreal(X): # Minor axis. dXu2 = (0.25 * (ex - fx))**2 dYu2 = (0.25 * (gx - hx))**2 dXv2 = (0.25 * (hx + gx))**2 dYv2 = (0.25 * (ex + fx))**2 sig1 = sig1 + 1j * np.sqrt(dXu2 * sigXu2 + dYu2 * sigYu2 + dXv2 * sigXv2 + dYv2 * sigYv2) # Orientation. rn = 2.0 * (Xu * Xv + Yu * Yv) rd = Xu**2 + Yu**2 - (Xv**2 + Yv**2) den = rn**2 + rd**2 dXu2 = ((rd * Xv - rn * Xu) / den)**2 dYu2 = ((rd * Yv - rn * Yu) / den)**2 dXv2 = ((rd * Xu + rn * Xv) / den)**2 dYv2 = ((rd * Yu + rn * Yv) / den)**2 sig2 = sig2 + 1j * (180 / np.pi) * np.sqrt( dXu2 * sigXu2 + dYu2 * sigYu2 + dXv2 * sigXv2 + dYv2 * sigYv2) return sig1, sig2
def format_sign(x): return x.real if np.isreal(x) else x
def run(f, X, args=(), length=None, red=1.0, verbose=False): ''' This is a function that performs unconstrained gradient based optimization using nonlinear conjugate gradients. The function is a straightforward Python-translation of Carl Rasmussen's Matlab-function minimize.m ''' INT = 0.1 # don't reevaluate within 0.1 of the limit of the current bracket EXT = 3.0 # extrapolate maximum 3 times the current step-size MAX = 20 # max 20 function evaluations per line search RATIO = 10 # maximum allowed slope ratio SIG = 0.1 RHO = SIG / 2 # SIG and RHO are the constants controlling the Wolfe- #Powell conditions. SIG is the maximum allowed absolute ratio between #previous and new slopes (derivatives in the search direction), thus setting #SIG to low (positive) values forces higher precision in the line-searches. #RHO is the minimum allowed fraction of the expected (from the slope at the #initial point in the linesearch). Constants must satisfy 0 < RHO < SIG < 1. #Tuning of SIG (depending on the nature of the function to be optimized) may #speed up the minimization; it is probably not worth playing much with RHO. SMALL = 10.**-16 #minimize.m uses matlab's realmin i = 0 # zero the run length counter ls_failed = 0 # no previous line search has failed result = f(X, *args) f0 = result[0] # get function value and gradient df0 = result[1] fX = [f0] i = i + (length < 0) # count epochs?! s = -df0 d0 = -dot(s, s) # initial search direction (steepest) and slope x3 = red / (1.0 - d0) # initial step is red/(|s|+1) while i < abs(length): # while not finished i = i + (length > 0) # count iterations?! X0 = X F0 = f0 dF0 = df0 # make a copy of current values if length > 0: M = MAX else: M = min(MAX, -length - i) while 1: # keep extrapolating as long as necessary x2 = 0 f2 = f0 d2 = d0 f3 = f0 df3 = df0 success = 0 while (not success) and (M > 0): try: M = M - 1 i = i + (length < 0) # count epochs?! result3 = f(X + x3 * s, *args) f3 = result3[0] df3 = result3[1] if isnan(f3) or isinf(f3) or any(isnan(df3) + isinf(df3)): return success = 1 except: # catch any error which occured in f x3 = (x2 + x3) / 2 # bisect and try again if f3 < F0: X0 = X + x3 * s F0 = f3 dF0 = df3 # keep best values d3 = dot(df3, s) # new slope if d3 > SIG * d0 or f3 > f0 + x3 * RHO * d0 or M == 0: # are we done extrapolating? break x1 = x2 f1 = f2 d1 = d2 # move point 2 to point 1 x2 = x3 f2 = f3 d2 = d3 # move point 3 to point 2 A = 6 * (f1 - f2) + 3 * (d2 + d1) * (x2 - x1 ) # make cubic extrapolation B = 3 * (f2 - f1) - (2 * d1 + d2) * (x2 - x1) Z = B + sqrt(complex(B * B - A * d1 * (x2 - x1))) if Z != 0.0: x3 = x1 - d1 * (x2 - x1)**2 / Z # num. error possible, ok! else: x3 = inf if (not isreal(x3)) or isnan(x3) or isinf(x3) or (x3 < 0): # num prob | wrong sign? x3 = x2 * EXT # extrapolate maximum amount elif x3 > x2 * EXT: # new point beyond extrapolation limit? x3 = x2 * EXT # extrapolate maximum amount elif x3 < x2 + INT * ( x2 - x1): # new point too close to previous point? x3 = x2 + INT * (x2 - x1) x3 = real(x3) while (abs(d3) > -SIG * d0 or f3 > f0 + x3 * RHO * d0) and M > 0: # keep interpolating if (d3 > 0) or (f3 > f0 + x3 * RHO * d0): # choose subinterval x4 = x3 f4 = f3 d4 = d3 # move point 3 to point 4 else: x2 = x3 f2 = f3 d2 = d3 # move point 3 to point 2 if f4 > f0: x3 = x2 - (0.5 * d2 * (x4 - x2)**2) / (f4 - f2 - d2 * (x4 - x2)) # quadratic interpolation else: A = 6 * (f2 - f4) / (x4 - x2) + 3 * (d4 + d2 ) # cubic interpolation B = 3 * (f4 - f2) - (2 * d2 + d4) * (x4 - x2) if A != 0: x3 = x2 + (sqrt(B * B - A * d2 * (x4 - x2)**2) - B) / A # num. error possible, ok! else: x3 = inf if isnan(x3) or isinf(x3): x3 = (x2 + x4) / 2 # if we had a numerical problem then bisect x3 = max(min(x3, x4 - INT * (x4 - x2)), x2 + INT * (x4 - x2)) # don't accept too close result3 = f(X + x3 * s, *args) f3 = result3[0] df3 = result3[1] if f3 < F0: X0 = X + x3 * s F0 = f3 dF0 = df3 # keep best values M = M - 1 i = i + (length < 0) # count epochs?! d3 = dot(df3, s) # new slope if abs( d3 ) < -SIG * d0 and f3 < f0 + x3 * RHO * d0: # if line search succeeded X = X + x3 * s f0 = f3 fX.append(f0) # update variables s = (dot(df3, df3) - dot(df0, df3)) / dot(df0, df0) * s - df3 # Polack-Ribiere CG direction df0 = df3 # swap derivatives d3 = d0 d0 = dot(df0, s) if d0 > 0: # new slope must be negative s = -df0 d0 = -dot(s, s) # otherwise use steepest direction x3 = x3 * min(RATIO, d3 / (d0 - SMALL)) # slope ratio but max RATIO ls_failed = 0 # this line search did not fail else: X = X0 f0 = F0 df0 = dF0 # restore best point so far if ls_failed or (i > abs(length)): # line search failed twice in a row break # or we ran out of time, so we give up s = -df0 d0 = -dot(s, s) # try steepest x3 = 1 / (1 - d0) ls_failed = 1 # this line search failed if verbose: print "\n" #print fX return X, fX, i
def lanbpro(A, K, RO, Opts = [], U = [], B_k = [], V = []): # LANBPRO Lanczos bidiagonalization with partial reorthogonalization. # LANBPRO computes the Lanczos bidiagonalization of a real # matrix using the with partial reorthogonalization. # # [U_k,B_k,V_k,R,ierr,work] = LANBPRO(A,K,R0,OPTIONS,U_old,B_old,V_old) # [U_k,B_k,V_k,R,ierr,work] = LANBPRO('Afun','Atransfun',M,N,K,R0, ... # OPTIONS,U_old,B_old,V_old) # # Computes K steps of the Lanczos bidiagonalization algorithm with partial # reorthogonalization (BPRO) with M-by-1 starting vector R0, producing a # lower bidiagonal K-by-K matrix B_k, an N-by-K matrix V_k, an M-by-K # matrix U_k and an M-by-1 vector R such that # A*V_k = U_k*B_k + R # Partial reorthogonalization is used to keep the columns of V_K and U_k # semiorthogonal: # MAX(DIAG((EYE(K) - V_K'*V_K))) <= OPTIONS.delta # and # MAX(DIAG((EYE(K) - U_K'*U_K))) <= OPTIONS.delta. # # B_k = LANBPRO(...) returns the bidiagonal matrix only. # # The first input argument is either a real matrix, or a string # containing the name of an M-file which applies a linear operator # to the columns of a given matrix. In the latter case, the second # input must be the name of an M-file which applies the transpose of # the same linear operator to the columns of a given matrix, # and the third and fourth arguments must be M and N, the dimensions # of then problem. # # The OPTIONS structure is used to control the reorthogonalization: # OPTIONS.delta: Desired level of orthogonality # (default = sqrt(eps/K)). # OPTIONS.eta : Level of orthogonality after reorthogonalization # (default = eps^(3/4)/sqrt(K)). # OPTIONS.cgs : Flag for switching between different reorthogonalization # algorithms: # 0 = iterated modified Gram-Schmidt (default) # 1 = iterated classical Gram-Schmidt # OPTIONS.elr : If OPTIONS.elr = 1 (default) then extended local # reorthogonalization is enforced. # OPTIONS.onesided # : If OPTIONS.onesided = 0 (default) then both the left # (U) and right (V) Lanczos vectors are kept # semiorthogonal. # OPTIONS.onesided = 1 then only the columns of U are # are reorthogonalized. # OPTIONS.onesided = -1 then only the columns of V are # are reorthogonalized. # OPTIONS.waitbar # : The progress of the algorithm is display graphically. # # If both R0, U_old, B_old, and V_old are provided, they must # contain a partial Lanczos bidiagonalization of A on the form # # A V_old = U_old B_old + R0 . # # In this case the factorization is extended to dimension K x K by # continuing the Lanczos bidiagonalization algorithm with R0 as a # starting vector. # # The output array work contains information about the work used in # reorthogonalizing the u- and v-vectors. # work = [ RU PU ] # [ RV PV ] # where # RU = Number of reorthogonalizations of U. # PU = Number of inner products used in reorthogonalizing U. # RV = Number of reorthogonalizations of V. # PV = Number of inner products used in reorthogonalizing V. # References: # R.M. Larsen, Ph.D. Thesis, Aarhus University, 1998. # # G. H. Golub & C. F. Van Loan, "Matrix Computations", # 3. Ed., Johns Hopkins, 1996. Section 9.3.4. # # B. N. Parlett, ``The Symmetric Eigenvalue Problem'', # Prentice-Hall, Englewood Cliffs, NJ, 1980. # # H. D. Simon, ``The Lanczos algorithm with partial reorthogonalization'', # Math. Comp. 42 (1984), no. 165, 115--142. # # Rasmus Munk Larsen, DAIMI, 1998. # Python Implementation: Matt Dioso, Seattle University Department of Electrical and Computer Engineering 2018 # NOTE: Purposely omitting the Atrans option. don't think it applies in Python implementation # Ref: https://github.com/areslp/matlab/blob/master/PROPACK/lanbpro.m global LANBPRO_TRUTH global MU global NU global MUTRUE global NUTRUE global MU_AFTER global NU_AFTER global MUTRUE_AFTER global NUTRUE_AFTER if len(locals()) < 1 or len(locals()) < 2: print("[LANBPRO] Not enough input arguments") return narg = len(locals()) if not np.isreal(A): print("[LANPRO] A must be real") return m, n = A.shape if narg >= 3: p = RO else: p = np.random.rand(m, 1) - 0.5 anorm = [] if math.min(m, n) == 0: U = [] B_k = [] V = [] p = [] ierr = 0 work = np.zeros(2,2) return U, B_k, V, p, ierr, work elif math.min(m, n) == 1: U = 1 B_k = A V = 1 p = 0 ierr = 0 work = np.zeros(2, 2) return U, B_k, V, p, ierr, work m2 = 3/2 n2 = 3/2 delta = math.sqrt(np.spacing(1)/K) eta = np.spacing(1) ** (3/4)/math.sqrt(K) cgs = 0 elr = 2 gamma = 1/math.sqrt(2) onesided = 0 t = 0 waitb = 0 if not opts and isinstance(opts, dict): c = opts.keys() if "delta" in c: delta = opts.get("delta") if "eta" in c: eta = opts.get("eta") if "cgs" in c: cgs = opts.get("cgs") if "elr" in c: elr = opts.get("elr") if "gamma" in c: gamma = opts.get("gamma") if "onesided" in c: onesided = opts.get("onsided") if "waitbar" in c: waitb = 1 #if waitb: #wait if not anorm: anorm = [] est_anorm = 1 else: est_anorm = 0 FUDGE = 1.01 npu = 0 npv = 0 ierr = 0 p = p[:] if not U: V = np.zeros(n, K) U = np.zeros(m, K) beta = np.zeros(K+1, 1) alpha = np.zeros(K, 1) beta[0] = np.linalg.norm(p) nu = np.zeros(K, 1) mu = np.zeros(K+1, 1) mu[0] = 1 nu[0] = 1 numax = np.zeros(K, 1) mumax = np.zeros(K, 1) force_reorth = 0 nreorthu = 0 nreorthv = 0 j0 = 1 else: j = U.shape[1] U = np.append(U, np.zeros(m, K-j)) V = np.append(V, np.zeros(n, K-j)) alpha = np.zeros(K+1, 1) beta = np.zeros(K+1, 1) alpha[0:j] = np.diag(B_k) if j > 1: beta[1:j] = np.diag(B_k, -1) beta[j+1] = np.linalg.norm(p) if j<K and beta[j+1] * delta < anorm * np.spacing(1): fro = 1 ierr = j int = [0:j].conj().transpose p, beta[j+1], rr = reorth(U, p, beta[j+1], int, gamma, cgs) #IMPLEMENT REORTH npu = rr*j nreorthu = 1 force_reorth = 1 if est_anorm: anorm = FUDGE * math.sqrt(np.linalg.norm(B_k.conj().transpose * B_k, 1)) mu = m2*np.spacing(1) * np.ones(K+1, 1) nu = np.zeros(K, 1) numax = np.zeros(K, 1) mumax = np.zeros(K, 1) force_reorth = 1 nreorthu = 0 nreorthv = 0 j0 = j+1 At = A.conj().transpose() if delta == 0: fro = 1 else: fro = 0 if LANBPRO_TRUTH == 1: MUTRUE = np.zeros(K, K) NUTRUE = np.zeros(K-1, K-1) MU = np.zeros(K, K) NU = np.zeros(K-1, K-1) MUTRUE_AFTER = np.zeros(K, K) NUTRUE_AFTER = np.zeros(K-1, K-1) MU_AFTER = np.zeros(K, K) NU_AFTER = np.zeros(K-1, K-1) for j in range(j0, K): if beta[j] != 0: U[:, j] = p/beta[j] else: U[:, j] = p if j == 6: B = np.append(np.diag(alpha[0:j-1])+np.diag(beta[1:j-1], -1), np.append(np.zeros(1, j-1), beta[j])) anorm = FUDGE * np.linalg.norm(B) est_anorm = 0 if j == 1: r = At*U[:, 1] alpha[1] = np.linalg.norm(r) if est_anorm: anorm = FUDGE * alpha[1] else: r - At*U[:, j] - beta[j]*V[:, j-1] alpha[j] = np.linalg.norm(r) if alpha[j] < gamma*beta[j] and elr and not fro: normold = alpha[j] stop = 0 while not stop: t = V[:, j-1].conj().transpose() * r r = r - V[:, j-1] * t alpha[j] = np.linalg.norm(r) if beta[j] != 0: beta[j] = beta[j] + t if alpha[j] >= gamma*normold: stop = 1 else: normold = alpha[j] if est_anorm: if j==2: anorm = math.max(anorm, FUDGE*math.sqrt(alpha[0]**2 + beta[1]**2 + alpha[1]*beta[1])) else: anorm = math.max(anorm, FUDGE* math.sqrt(alpha(j-1)**2 + beta[j]**2+alpha[j-1]*beta[j-1] + alpha[j]*beta[j])) if not fro and alpha[j] != 0: nu = update_nu(nu, mu, j, alpha, beta, anorm) numax[j] = math.max(math.abs(nu[1:j-1])) if j>1 and LANBPRO_TRUTH: NU[0:j-1, j-1] = nu[0:j-1] NUTRUE[0:j-1, j-1] = V[:, 1:j-1].conj().transpose()*r/alpha[j] if elr > 0: nu[j-1] = n2*np.spacing(1) if onsided != -1 and (fro or numax[j] > delta or force_reorth) and alpha[j] != 0: if fro or eta == 0: int = [0:j-1].conj().transpose() elif force_reorth == 0: int = compute_int(nu, j-1, delta, eta, 0, 0, 0) r, alpha[j], rr = reorth(V, r, alpha[j], int, gamma, cgs) npv = npv + rr*len(int) nu[int] = n2*np.spacing(1) if force_reorth == 0: force_reorth = 1 else: force_reorth = 0 nreorthv = nreorthv + 1 if alpha[j] < math.max(n,m)*anorm*np.spacing(1) and j < k: alpha[j] = 0 bailout = 1 for i in range(0, 2): r = np.random.rand(m, 1)-0.5 r = At * r nrm = math.sqrt((r.conj().transpose()) * r) int = [1:j-1].conj().transpose() r, nrmnew, rr = reorth(V, r, nrm, int, gamma, cgs) npv = npv + rr*len(int[:]) nreorthv = nreorthv + 1 nu[int] = n2 * np.spacing(1) if nrmnew > 0: bailout = 0 break if bailout: j=j-1 ierr = -j break else: r = r/nrmnew force_reorth = 1 if delta > 0: fro =0 elif j<l and not fro and anorm*np.spacing(1) > delta*alpha[j]: fro =1 ierr = j if J>1 and LANBPRO_TRUTH: NU_AFTER[0:j-1, j-1] = nu[0:j-1] NUTRUE_AFTER[1:j-1, j-1] = (V[:, 0:j-1].conj().transpose)*r/alpha[j] if alpha[j] != 0: V[:, j] = r/alpha[j] else: V[:,j] = r p = A * V[:, j] - alpha[j]*U[:,j] beta[J+1] = np.linalg.norm(p) if beta[j+1] < gamma*alpha[j] and elr and not fro: normold = beta[j+1] stop = 0 while not stop: t = U[:, j].conj().transpose() * p p = p - U[:, j]*t beta[j+1] = np.linalg.norm(p) if alpha[j] != 0: alpha[j] = alpha[j] + t if beta[j+1] >= gamma*normold: stop = 1 else: normold = beta[j+1] if est_anorm: if j==1: anorm = math.max(anorm, FUDGE*pythag(alpha[0], alpha[1])) else: anorm = math.max(anorm, FUDGE*math.sqrt(alpha[j] ** 2 + alpha[j]*beta[j])) if not fro and beta[j+1] != 0: mu = update_mu(mu, nu, j, alpha, beta, anorm) mumax[j] = math.max(math.abs(mu[1:j])) #i think this is getting the max of an array? if LANBPRO_TRUTH == 1: MU[0:j, j] = mu[0:j] MUTRUE[1:j, j] = U[:, 0:j].conj().transpose() * p/beta[j+1] if elr > 0: mu[j] = m2 * np.spacing(1) if onesided != 1 and (fro or mumax[j] > delta or force_reorth) and beta[j+1] != 0: if fro or eta ==1: int = [0:j].conj().transpose() elif force_reorth == 0: int = compute_int(mu, j, delta, eta, 0, 0, 0) else: int = [int; max[int]+1] p, beta[j+1], rr = reorth(U, p, beta[j+1], int, gamma, cgs) nppu = npu + rr*len(int) nreorthu = nreorthu + 1 mu[int] = m2 * np.spacing(1) if force_reorth == 0: force_reorth =1 else: force_reorth = 0 if beta[j + 1] < math.max(m, n) * anorm * np.spacing(1) and j<k: beta[j+1] = 0 bailout = 1 for i in range(0, 2): p = np.random.rand(n, 1) - 0.5 p = A * p nrm = math.sqrt((p.conj().transpose) * p) int = [0:j].conj().transpose() p, nrmnew, rr = reorth(U, p, nrm, int, gamma, cgs) npu = npu + rr*len(int[:]) nreorthu = nreorthu + 1 mu[int] = m2 * np.spacing(1) if nrmnew > 0: bailout = 0 break if bailout: ierr = -j break else: p = p/nrmnew force_reorth = 1 if delta > 0: fro = 0 elif j<k and not fro and anorm*np.spacing(1) > delta*beta[j+1]: fro = 1 ierr = j if LANBPRO_TRUTH == 1: MU_AFTER[0:j, j] = mu[0:j] MUTRUE_AFTER[0:j, j] = (U[:, 0:j].conj().transpose())*p/beta[j+1] if j<K: j=K B_k = scipy.sparse.spdiags([alpha[1:K]; [beta[1:K]; 0]], [0, -1], K, K) if K != U.shape(1) or K != V.shape(1): U = U[:, 0:K] V = V[:, 0:K] work = [[nreorthu, npu]; [nreorthv, npv]] return U, B_k, V, p, ierr, work
def fit(self, env, nb_steps, action_repetition=1, callbacks=None, verbose=1, visualize=False, nb_max_start_steps=0, start_step_policy=None, log_interval=10000, nb_max_episode_steps=None, version=None): """Trains the agent on the given environment. # Arguments env: (`Env` instance): Environment that the agent interacts with. See [Env](#env) for details. nb_steps (integer): Number of training steps to be performed. action_repetition (integer): Number of times the agent repeats the same action without observing the environment again. Setting this to a value > 1 can be useful if a single action only has a very small effect on the environment. callbacks (list of `keras.callbacks.Callback` or `rl.callbacks.Callback` instances): List of callbacks to apply during training. See [callbacks](/callbacks) for details. verbose (integer): 0 for no logging, 1 for interval logging (compare `log_interval`), 2 for episode logging visualize (boolean): If `True`, the environment is visualized during training. However, this is likely going to slow down training significantly and is thus intended to be a debugging instrument. nb_max_start_steps (integer): Number of maximum steps that the agent performs at the beginning of each episode using `start_step_policy`. Notice that this is an upper limit since the exact number of steps to be performed is sampled uniformly from [0, max_start_steps] at the beginning of each episode. start_step_policy (`lambda observation: action`): The policy to follow if `nb_max_start_steps` > 0. If set to `None`, a random action is performed. log_interval (integer): If `verbose` = 1, the number of steps that are considered to be an interval. nb_max_episode_steps (integer): Number of steps per episode that the agent performs before automatically resetting the environment. Set to `None` if each episode should run (potentially indefinitely) until the environment signals a terminal state. # Returns A `keras.callbacks.History` instance that recorded the entire training process. """ if not self.compiled: raise RuntimeError( 'Your tried to fit your agent but it hasn\'t been compiled yet. Please call `compile()` before `fit()`.' ) if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format( action_repetition)) self.training = True callbacks = [] if not callbacks else callbacks[:] if verbose == 1: callbacks += [TrainIntervalLogger(interval=log_interval)] elif verbose > 1: callbacks += [TrainEpisodeLogger()] if visualize: callbacks += [Visualizer()] history = History() callbacks += [history] callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_steps': nb_steps, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_train_begin() callbacks.on_train_begin() episode = np.int16(0) self.step = np.int16(0) observation = None episode_reward = None episode_step = None did_abort = False # open workbook to store result workbook = xlwt.Workbook() sheet = workbook.add_sheet('DQN') sheet_step = workbook.add_sheet('step') try: while self.step < nb_steps: if observation is None: # start of a new episode callbacks.on_episode_begin(episode) episode_step = np.int16(0) episode_reward = np.float32(0) # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) assert observation is not None # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint( nb_max_start_steps) for _ in range(nb_random_start_steps): if start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, reward, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, reward, done, info = self.processor.process_step( observation, reward, done, info) callbacks.on_action_end(action) if done: warnings.warn( 'Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.' .format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) break # At this point, we expect to be fully initialized. assert episode_reward is not None assert episode_step is not None assert observation is not None # Run a single step. callbacks.on_step_begin(episode_step) # This is were all of the work happens. We first perceive and compute the action # (forward step) and then use the reward to improve (backward step). action = self.forward(observation) if self.processor is not None: action = self.processor.process_action(action) reward = np.float32(0) accumulated_info = {} done = False for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, done, info = env.step(action) # print(observation, r, done, info) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step( observation, r, done, info) for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value callbacks.on_action_end(action) reward += r if done: break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: # Force a terminal state. done = True metrics = self.backward(reward[0], terminal=done) episode_reward += reward step_logs = { 'action': action, 'observation': observation, 'reward': reward[0], 'metrics': metrics, 'episode': episode, 'info': accumulated_info, } callbacks.on_step_end(episode_step, step_logs) episode_step += 1 self.step += 1 if done: # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # This episode is finished, report and reset. episode_logs = { 'episode_reward': episode_reward, 'nb_episode_steps': episode_step, 'nb_steps': self.step, } callbacks.on_episode_end(episode, episode_logs) sheet.write(episode + 1, 0, str(episode)) sheet.write(episode + 1, 1, str(episode_reward[0])) sheet.write(episode + 1, 2, str(episode_reward[1])) sheet.write(episode + 1, 3, str(episode_reward[2])) sheet.write(episode + 1, 4, str(episode_reward[3])) episode += 1 observation = None episode_step = None episode_reward = None except KeyboardInterrupt: # We catch keyboard interrupts here so that training can be be safely aborted. # This is so common that we've built this right into this function, which ensures that # the `on_train_end` method is properly called. did_abort = True callbacks.on_train_end(logs={'did_abort': did_abort}) self._on_train_end() file_name = 'result_v' + version + '.xls' # if (self.enable_double_dqn): # file_name = 'DDQN_' + file_name # if (self.enable_dueling_network): # file_name = 'Dueling_' + file_name workbook.save('../results/' + file_name) return history
def CheckMGRepresentation(alpha, A, prec=None): """ Checks if the given vector and matrix define a valid matrix- geometric representation. Parameters ---------- alpha : matrix, shape (1,M) Initial vector of the matrix-geometric distribution to check A : matrix, shape (M,M) Matrix parameter of the matrix-geometric distribution to check prec : double, optional Numerical precision. The default value is 1e-14. Returns ------- r : bool True, if the matrix is a square matrix, the vector and the matrix have the same size, the dominant eigenvalue is positive, less than 1 and real. Notes ----- This procedure does not check the positivity of the density! The discrete counterpart of 'CheckMEPositiveDensity' does not exist yet (research is needed). """ if prec == None: prec = butools.checkPrecision if len(alpha.shape) < 2: if butools.verbose: print( "CheckMGRepresentation: Initial vector must be a matrix of size (1,N)!" ) return False if A.shape[0] != A.shape[1]: if butools.verbose: print("CheckMGRepresentation: The matrix is not a square matrix!") return False if alpha.shape[1] != A.shape[0]: if butools.verbose: print( "CheckMGRepresentation: The vector and the matrix have different sizes!" ) return False if np.sum(alpha) < -prec * alpha.size or np.sum( alpha) > 1.0 + prec * alpha.size: if butools.verbose: print( "CheckMGRepresentation: The sum of the vector elements is less than zero or greater than one!" ) return False ev = la.eigvals(A) ix = np.argsort(-np.abs(np.real(ev))) maxev = ev[ix[0]] if not np.isreal(maxev): if butools.verbose: print( "CheckMGRepresentation: The largest eigenvalue of the matrix is complex!" ) return False if maxev > 1.0 + prec: if butools.verbose: print( "CheckMGRepresentation: The largest eigenvalue of the matrix is greater than 1!" ) return False if np.sum(np.abs(ev) == abs(maxev)) > 1 and butools.verbose: print( "CheckMGRepresentation warning: There are more than one eigenvalue with the same absolute value as the largest eigenvalue!" ) return True
def test(self, env, nb_episodes=1, action_repetition=1, callbacks=None, visualize=True, nb_max_episode_steps=None, nb_max_start_steps=0, start_step_policy=None, verbose=1): """Callback that is called before training begins. # Arguments env: (`Env` instance): Environment that the agent interacts with. See [Env](#env) for details. nb_episodes (integer): Number of episodes to perform. action_repetition (integer): Number of times the agent repeats the same action without observing the environment again. Setting this to a value > 1 can be useful if a single action only has a very small effect on the environment. callbacks (list of `keras.callbacks.Callback` or `rl.callbacks.Callback` instances): List of callbacks to apply during training. See [callbacks](/callbacks) for details. verbose (integer): 0 for no logging, 1 for interval logging (compare `log_interval`), 2 for episode logging visualize (boolean): If `True`, the environment is visualized during training. However, this is likely going to slow down training significantly and is thus intended to be a debugging instrument. nb_max_start_steps (integer): Number of maximum steps that the agent performs at the beginning of each episode using `start_step_policy`. Notice that this is an upper limit since the exact number of steps to be performed is sampled uniformly from [0, max_start_steps] at the beginning of each episode. start_step_policy (`lambda observation: action`): The policy to follow if `nb_max_start_steps` > 0. If set to `None`, a random action is performed. log_interval (integer): If `verbose` = 1, the number of steps that are considered to be an interval. nb_max_episode_steps (integer): Number of steps per episode that the agent performs before automatically resetting the environment. Set to `None` if each episode should run (potentially indefinitely) until the environment signals a terminal state. # Returns A `keras.callbacks.History` instance that recorded the entire training process. """ if not self.compiled: raise RuntimeError( 'Your tried to test your agent but it hasn\'t been compiled yet. Please call `compile()` before `test()`.' ) if action_repetition < 1: raise ValueError('action_repetition must be >= 1, is {}'.format( action_repetition)) self.training = False self.step = 0 callbacks = [] if not callbacks else callbacks[:] if verbose >= 1: callbacks += [TestLogger()] if visualize: callbacks += [Visualizer()] history = History() callbacks += [history] callbacks = CallbackList(callbacks) if hasattr(callbacks, 'set_model'): callbacks.set_model(self) else: callbacks._set_model(self) callbacks._set_env(env) params = { 'nb_episodes': nb_episodes, } if hasattr(callbacks, 'set_params'): callbacks.set_params(params) else: callbacks._set_params(params) self._on_test_begin() callbacks.on_train_begin() for episode in range(nb_episodes): callbacks.on_episode_begin(episode) episode_reward = 0. episode_step = 0 # Obtain the initial observation by resetting the environment. self.reset_states() observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation(observation) assert observation is not None # Perform random starts at beginning of episode and do not record them into the experience. # This slightly changes the start position between games. nb_random_start_steps = 0 if nb_max_start_steps == 0 else np.random.randint( nb_max_start_steps) for _ in range(nb_random_start_steps): if start_step_policy is None: action = env.action_space.sample() else: action = start_step_policy(observation) if self.processor is not None: action = self.processor.process_action(action) callbacks.on_action_begin(action) observation, r, done, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, done, info = self.processor.process_step( observation, r, done, info) callbacks.on_action_end(action) if done: warnings.warn( 'Env ended before {} random steps could be performed at the start. You should probably lower the `nb_max_start_steps` parameter.' .format(nb_random_start_steps)) observation = deepcopy(env.reset()) if self.processor is not None: observation = self.processor.process_observation( observation) break # Run the episode until we're done. done = False while not done: callbacks.on_step_begin(episode_step) action = self.forward(observation) if self.processor is not None: action = self.processor.process_action(action) reward = 0. accumulated_info = {} for _ in range(action_repetition): callbacks.on_action_begin(action) observation, r, d, info = env.step(action) observation = deepcopy(observation) if self.processor is not None: observation, r, d, info = self.processor.process_step( observation, r, d, info) callbacks.on_action_end(action) reward += r for key, value in info.items(): if not np.isreal(value): continue if key not in accumulated_info: accumulated_info[key] = np.zeros_like(value) accumulated_info[key] += value if d: done = True break if nb_max_episode_steps and episode_step >= nb_max_episode_steps - 1: done = True self.backward(reward, terminal=done) episode_reward += reward step_logs = { 'action': action, 'observation': observation, 'reward': reward, 'episode': episode, 'info': accumulated_info, } callbacks.on_step_end(episode_step, step_logs) episode_step += 1 self.step += 1 # We are in a terminal state but the agent hasn't yet seen it. We therefore # perform one more forward-backward call and simply ignore the action before # resetting the environment. We need to pass in `terminal=False` here since # the *next* state, that is the state of the newly reset environment, is # always non-terminal by convention. self.forward(observation) self.backward(0., terminal=False) # Report end of episode. episode_logs = { 'episode_reward': episode_reward, 'nb_steps': episode_step, } callbacks.on_episode_end(episode, episode_logs) callbacks.on_train_end() self._on_test_end() return history
import numpy as np a = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j]) print("Original array") print(a) print("Checking for complex number:") print(np.iscomplex(a)) print("Checking for real number:") print(np.isreal(a)) print("Checking for scalar type:") print(np.isscalar(3.1)) print(np.isscalar([3.1]))
), lambda x: x.reshape((x.shape[0] * x.shape[1], x.shape[2])), # Rechunking here is required, see https://github.com/dask/dask/issues/2561 lambda x: (x.rechunk(x.shape)).reshape((x.shape[1], x.shape[0], x.shape[2])), lambda x: x.reshape((x.shape[0], x.shape[1], x.shape[2] / 2, x.shape[2] / 2)), lambda x: abs(x), lambda x: x > 0.5, lambda x: x.rechunk((4, 4, 4)), lambda x: x.rechunk((2, 2, 1)), pytest.param( lambda x: da.einsum("ijk,ijk", x, x), ), lambda x: np.isneginf(x), lambda x: np.isposinf(x), pytest.param( lambda x: np.isreal(x), marks=pytest.mark.skipif( not IS_NEP18_ACTIVE, reason="NEP-18 support is not available in NumPy" ), ), pytest.param( lambda x: np.iscomplex(x), marks=pytest.mark.skipif( not IS_NEP18_ACTIVE, reason="NEP-18 support is not available in NumPy" ), ), pytest.param( lambda x: np.real(x), marks=pytest.mark.skipif( not IS_NEP18_ACTIVE, reason="NEP-18 support is not available in NumPy" ),
d,sd=np.zeros(len(k)),np.zeros(len(k)) fwhm_lo,fwhm_hi = np.zeros(len(k)),np.zeros(len(k)) tgb = np.zeros(len(k)) spt = np.zeros(len(k),dtype=object) start1=time.time() print 'Staring distances' for i in range(len(k)): ############### compute distance: # establish the coefficients of the mode-finding polynomial: coeff = np.array([(1./L),(-2),((p[i]/1000.)/((pe[i]/1000.)**2)),-(1./((pe[i]/1000.)**2))]) # use numpy to find the roots: g = np.roots(coeff) # Find the number of real roots: reals = np.isreal(g) realsum = np.sum(reals) # If there is one real root, that root is the mode: if realsum == 1: gd = np.real(g[np.where(reals)[0]]) # If all roots are real: elif realsum == 3: if p[i] >= 0: # Take the smallest root: gd = np.min(g) elif p[i] < 0: # Take the positive root (there should be only one): gd = g[np.where(g>0)[0]] d[i] = gd end=time.time() print 'That took ',(end-start1)/60/60,' hrs'