def asinh(inputArray, scale_min=None, scale_max=None, non_linear=2.0): """Performs asinh scaling of the input numpy array. @type inputArray: numpy array @param inputArray: image data array @type scale_min: float @param scale_min: minimum data value @type scale_max: float @param scale_max: maximum data value @type non_linear: float @param non_linear: non-linearity factor @rtype: numpy array @return: image data array """ print "img_scale : asinh" imageData=numpy.array(inputArray, copy=True) if scale_min == None: scale_min = imageData.min() if scale_max == None: scale_max = imageData.max() factor = numpy.arcsinh((scale_max - scale_min)/non_linear) indices0 = numpy.where(imageData < scale_min) indices1 = numpy.where((imageData >= scale_min) & (imageData <= scale_max)) indices2 = numpy.where(imageData > scale_max) imageData[indices0] = 0.0 imageData[indices2] = 1.0 imageData[indices1] = numpy.arcsinh((imageData[indices1] - \ scale_min)/non_linear)/factor return imageData
def test_arcsinh(self): import math from numpy import arcsinh for v in [float("inf"), float("-inf"), 1.0, math.e]: assert math.asinh(v) == arcsinh(v) assert math.isnan(arcsinh(float("nan")))
def imstretch(self): data = np.clip(self.data_array, self.threshold[0], self.threshold[1]) if self.mode == "linear": pass elif self.mode == "logarithmic": data = np.reciprocal(1 + np.power(0.5 / data, self.factor)) elif self.mode == "gamma": data = np.power(data, self.factor) elif self.mode == "arcsinh": mn = np.nanmin(data) mx = np.nanmax(data) tmp = bytescale(data, high=1.0) beta = np.clip(self.factor, 0.0, self.factor) sclbeta = (beta - mn) / (mx - mn) sclbeta = np.clip(sclbeta, 1.0e-12, sclbeta) nonlinearity = 1.0 / sclbeta extrema = np.arcsinh(np.array([0.0, nonlinearity])) data = np.clip(np.arcsinh(data * nonlinearity), extrema[0], extrema[1]) elif self.mode == "square root": data = np.sqrt(np.fabs(data)) * np.sign(data) elif self.mode == "histogram equalization": imhist, bins = np.histogram(data.flatten(), 256, normed=True) cdf = imhist.cumsum() # cumulative distribution function cdf = 255 * cdf / cdf[-1] # normalize im2 = np.interp(data.flatten(), bins[:-1], cdf) data = im2.reshape(data.shape) self.scaled = bytescale(data).flatten().tolist()
def plot_IQU(solution, title, col, ncol=6, coord='C'): # Es=solution[np.array(final_index).tolist()].reshape((4, len(final_index)/4)) # I = Es[0] + Es[3] # Q = Es[0] - Es[3] # U = Es[1] + Es[2] IQUV = sol2map(solution) IQUV.shape = (4, IQUV.shape[0] / 4) I = IQUV[0] Q = IQUV[1] U = IQUV[2] V = IQUV[3] pangle = 180 * np.arctan2(Q, U) / 2 / PI plotcoordtmp = coord hpv.mollview(np.log10(I), min=0, max=4, coord=plotcoordtmp, title=title, nest=True, sub=(4, ncol, col)) hpv.mollview((Q ** 2 + U ** 2) ** .5 / I, min=0, max=1, coord=plotcoordtmp, title=title, nest=True, sub=(4, ncol, ncol + col)) from matplotlib import cm cool_cmap = cm.hsv cool_cmap.set_under("w") # sets background to white hpv.mollview(pangle, min=-90, max=90, coord=plotcoordtmp, title=title, nest=True, sub=(4, ncol, 2 * ncol + col), cmap=cool_cmap) hpv.mollview(np.arcsinh(V) / np.log(10), min=-np.arcsinh(10. ** 4) / np.log(10), max=np.arcsinh(10. ** 4) / np.log(10), coord=plotcoordtmp, title=title, nest=True, sub=(4, ncol, 3 * ncol + col)) if col == ncol: plt.show()
def load_minibatch(self, filepath, nimg, farts, gridsize, cg, num,cg_additional=1,twoclasses=False): """ Load a mini batch of images and their labels. Labels need to be converted to tensorflow format inputs: filepath -- Path where the files are located nimg -- Number of images in the total batch farts -- Fraction of artifacts gridsize -- Number of pixels to a side cg -- Coarsegraining factor num -- The minibatch number cg_additional -- additional coursegraining to perform on the fly """ X = np.load('{0}/X_{1}_{2}_{3}_{4}_mb{5}.npy'.format(filepath, nimg, farts, gridsize, cg, num)) y = np.load('{0}/y_{1}_{2}_{3}_{4}_mb{5}.npy'.format(filepath, nimg, farts, gridsize, cg, num)) X[X==-99] = np.nan if cg_additional!=1: X = np.mean(np.mean(X.reshape([X.shape[0],gridsize//cg,gridsize//cg//cg_additional,cg_additional]),axis=3).T.reshape(gridsize//cg//cg_additional,gridsize//cg//cg_additional,cg_additional,X.shape[0]),axis=2).T.reshape([X.shape[0],(gridsize//cg//cg_additional)**2]) X = 255*(np.arcsinh(X)-np.atleast_2d(np.arcsinh(np.nanmin(X,axis=1))).T)/np.atleast_2d((np.arcsinh(np.nanmax(X,axis=1))-np.arcsinh(np.nanmin(X,axis=1)))).T X[np.isnan(X)] = 0 #X -= np.atleast_2d(np.mean(X,axis=1)).T #print(np.nanmean(X, axis=1)) ey = self.convert_labels(y, twoclasses) return X, ey
def test_arcsinh(self): import math from numpy import arcsinh, inf for v in [inf, -inf, 1.0, math.e]: assert math.asinh(v) == arcsinh(v) assert math.isnan(arcsinh(float("nan")))
def Scale_asinh(inputArray, scale_min=None, scale_max=None, non_linear=2.0): """Scale_asinh(inputArray, scale_min=None, scale_max=None, non_linear=2.0) Performs asinh scaling of the input numpy array. (from Min-Su Shin, Princeton) inputArray: image data array scale_min (None): minimum data value use inputArray.min() if None scale_max (None): maximum data value use inputArray.max() if None non_linear (2.0): non-linearity factor >>> scaledArray = Scale_asinh(inputArray) """ imageData=numpy.array(inputArray, copy=True) if scale_min == None: scale_min = imageData.min() if scale_max == None: scale_max = imageData.max() factor = numpy.arcsinh((scale_max - scale_min)/non_linear) indices0 = numpy.where(imageData < scale_min) indices1 = numpy.where((imageData >= scale_min) & (imageData <= scale_max)) indices2 = numpy.where(imageData > scale_max) imageData[indices0] = 0.0 imageData[indices2] = 1.0 imageData[indices1] = numpy.arcsinh((imageData[indices1] - scale_min)/non_linear)/factor return imageData
def test_asinh(self): """Test arcsinh scaling.""" img = scale_image(DATA, scale='asinh') mean, median, stddev = sigmaclip_stats(DATA, sigma=3.0) z = (mean + (2.0 * stddev)) / 2. ref = np.arcsinh(DATASCL / z) / np.arcsinh(1.0 / z) assert_allclose(img, ref, atol=0, rtol=1.e-5)
def __call__(self, value): self.autoscale_None(value) # set vmin, vmax if unset inverted = self.vmax <= self.vmin hi, lo = max(self.vmin, self.vmax), min(self.vmin, self.vmax) ra = hi - lo mid = lo + ra * self.bias mn = mid - ra * self.contrast mx = mid + ra * self.contrast if self.stretch == "linear": result = (value - mn) * (1.0 / (mx - mn)) result = np.clip(result, 0, 1) elif self.stretch == "arcsinh": b = max(self.bias, 1e-5) c = self.contrast result = (value - lo) / (1.0 * (hi - lo)) result = np.arcsinh(result / b) / np.arcsinh((b + c) / b) result = np.clip(result, 0, 1) elif self.stretch == "sqrt": result = (value - mn) * (1.0 / (mx - mn)) result = np.clip(result, 0, 1) result = np.sqrt(result) else: raise TypeError("Invalid stretch: %s" % self.stretch) if inverted: result = 1 - result return result
def int_pot_2D_moi(self, xp, yp, x, R, h, basis_func): """FWD model function. Incorporates the Method of Images. Returns contribution of a point xp,yp, belonging to a basis source support centered at (0,0) to the potential measured at (x,0), integrated over xp,yp gives the potential generated by a basis source element centered at (0,0) at point (x,0) #Eq 20, Ness(2015) Parameters ---------- xp, yp : floats or np.arrays point or set of points where function should be calculated x : float position at which potential is being measured R : float The size of the basis function h : float thickness of slice basis_func : method Fuction of the basis source Returns ------- pot : float """ L = ((x-xp)**2 + yp**2)**(0.5) if L < 0.00001: L = 0.00001 correction = np.arcsinh((h-(2*h*self.iters))/L) + np.arcsinh((h+(2*h*self.iters))/L) pot = np.arcsinh(h/L) + np.sum(self.iter_factor*correction) dist = np.sqrt(xp**2 + yp**2) pot *= basis_func(dist, R) # Eq 20, Ness et.al. return pot
def newspace(high): Smax = high K = exact deps = 1./size * (np.arcsinh((Smax - K)*(1/density)) - np.arcsinh(-K/density)) eps = np.arcsinh(-K/density) + np.arange(size)*deps space = K + density * np.sinh(eps) space -= min(space) return space
def test_arcsinh(): a = afnumpy.random.random((2,3)) b = numpy.array(a) fassert(afnumpy.arcsinh(a), numpy.arcsinh(b)) c = afnumpy.random.random((2,3)) d = numpy.array(a) fassert(afnumpy.arcsinh(a, out=c), numpy.arcsinh(b, out=d)) fassert(c, d)
def scale_two_arcsinh(x,up1,up2,down1,down2,m="normal"): if m != "inverse": if x >= 0: return up1*np.arcsinh(x*up2) if x < 0: return down1*np.arcsinh(x*down2) else: if x >= 0: return 1./up2*np.sinh(x/up1) if x < 0: return 1./down2*np.sinh(x/down1)
def asinh(x): """ Inverse hyperbolic sine """ if isinstance(x, UncertainFunction): mcpts = np.arcsinh(x._mcpts) return UncertainFunction(mcpts) else: return np.arcsinh(x)
def elec_catenary_hyperbolic_lowest_proj ( T0, w, l, h): Lh0 = elec_catenary_hyperbolic_length_equal_high (T0, w, l) if Lh0 != 0.: a = l/2. - T0/w*np.arcsinh(h/Lh0) b = l/2. + T0/w*np.arcsinh(h/Lh0) else: a = l/2. b = l/2. return a
def __call__(self, values, out=None, clip=True): values = _prepare(values, out=out, clip=clip) np.true_divide(values, self.a, out=values) np.arcsinh(values, out=values) np.true_divide(values, np.arcsinh(1. / self.a), out=values) return values
def colorImage(b,g,r,bMinusr=0.8,bMinusg=0.4,sdev=None,nonlin=5.,m=0.5,M=None): w = r.shape[0]/2-5 rb = r/b gb = g/b rnorm = numpy.median(rb[w:-w,w:-w]) gnorm = numpy.median(gb[w:-w,w:-w]) r /= rnorm g /= gnorm r *= 10**(0.4*bMinusr) g *= 10**(0.4*bMinusg) r /= 620. g /= 540. b /= 460. I = (r+g+b)/3. if sdev is None: sdev = clip(I)[1] m = m*sdev if M is None: M = I[w:-w,w:-w].max() nonlin = nonlin*sdev f = numpy.arcsinh((I-m)/nonlin)/numpy.arcsinh((M-m)/nonlin) f[I<m] = 0. f[I>M] = 1. R = r*f/I G = g*f/I B = b*f/I R[I<=0] = 0. G[I<=0] = 0. B[I<=0] = 0. R[R<=0] = 0. G[G<=0] = 0. B[B<=0] = 0. R[R>1] = 1. G[G>1] = 1. B[B>1] = 1. white = True if white: cond = (f==1) R[cond] = 1. G[cond] = 1. B[cond] = 1. arr = numpy.empty((R.shape[0],R.shape[1],3)) arr[:,:,0] = R arr[:,:,1] = G arr[:,:,2] = B return arr,sdev,M,rnorm,gnorm
def ref_table(PTEN,MWPL,S,FH,AE,MBL): n = 754. max_a =(MBL-MWPL*FH)/MWPL INC = max_a/n a = np.array(np.linspace(0.0,INC*n,num=n+1)) H = MWPL*a Ttop = H + MWPL*FH Vtop = (Ttop**2-H**2)**0.5 ang = 90. - np.arcsin(H/Ttop)*180/np.pi sp_temp = -(S/2.)+(FH/2.)*(1.+(4*a**2/(S**2.-FH**2)))**0.5 sp_temp = [0 if i < 0 else i for i in sp_temp] # INITIALIZE ARRanchor_yS Vbot = np.array([0.]*len(a)) Tbot = np.array([0.]*len(a)) Tave = np.array([0.]*len(a)) stretch = np.array([0.]*len(a)) x = np.array([0.]*len(a)) s = np.array([0.]*len(a)) yp = np.array([0.]*len(a)) sp = np.array([0.]*len(a)) for i in range (0,len(a)): if sp_temp[i] == 0.: Vbot[i] = 0. Tbot[i] = H[i] Tave[i] = 0.5*(Ttop[i]+Tbot[i]) stretch[i] = 1.+Tave[i]/AE if i == 0: x[i] = S*stretch[i] -FH else: x[i] = S*stretch[i-1] -FH*(1.+2.*a[i]/FH)**0.5+a[i]*np.arccosh(1.+FH/a[i]) s[i] = (FH**2+2.*FH*a[i])**0.5 sp [i] = 0. else: s[i] = S*stretch[i-1] Vbot[i] = Vtop[i]-MWPL*s[i] Tbot[i] = (H[i]**2+Vbot[i]**2)**0.5 Tave[i] = 0.5*(Ttop[i]+Tbot[i]) stretch[i] = 1.+Tave[i]/AE sp[i] = sp_temp[i]*stretch[i-1] x[i] = a[i]*(np.arcsinh((S+sp[i])/a[i])-np.arcsinh(sp[i]/a[i]))*stretch[i-1] if i == 0: yp[i] = -a[i]+(a[i]**2+sp[i]**2)**0.5 else: yp[i] = (-a[i]+(a[i]**2+sp[i]**2)**0.5)*stretch[i-1] x0 = np.interp(PTEN,Ttop,x) offset = x - x0 MKH = np.array([0.]*len(a)) MKV = np.array([0.]*len(a)) for i in range(1,len(a)): MKH[i] = (H[i]-H[i-1])/(offset[i]-offset[i-1]) MKV[i] = (Vtop[i]-Vtop[i-1])/(offset[i]-offset[i-1]) return x0,a,x,H,sp,yp,s,Ttop,Vtop,Tbot,Vbot,Tave,stretch,ang,offset,MKH,MKV,INC
def arcsinh(x, out=None): """ Raises a ValueError if input cannot be rescaled to a dimensionless quantity. """ if not isinstance(x, Quantity): return np.arcsinh(x, out) return Quantity( np.arcsinh(x.rescale(dimensionless).magnitude, out), dimensionless, copy=False )
def asinhmag(flux, fluxerr, m0 = 22.5, f0=1.0, b=0.01): """ Implements http://ssg.astro.washington.edu/elsst/opsim.shtml?lightcurve_mags """ mag = m0 -(2.5/np.log(10.)) * ( np.arcsinh( flux / (f0 * 2.0 * b)) + np.log(b) ) magplu = m0 -(2.5/np.log(10.)) * ( np.arcsinh( (flux+fluxerr) / (f0 * 2.0 * b)) + np.log(b) ) magmin = m0 -(2.5/np.log(10.)) * ( np.arcsinh( (flux-fluxerr) / (f0 * 2.0 * b)) + np.log(b) ) magerr = 0.5*(magmin - magplu) return (mag, magerr)
def arcsinhspace(start, stop, num=50): """ Return numbers spaced evenly on an arcsinh scale. Parameters ---------- start : number start value stop : number stop/end value, inclusive num : number number of intervales between start and stop """ return _np.sinh(_np.linspace(_np.arcsinh(start), _np.arcsinh(stop), num))
def testBijectorOverRange(self): for dtype in (np.float32, np.float64): skewness = np.array([1.2, 5.], dtype=dtype) tailweight = np.array([2., 10.], dtype=dtype) # The inverse will be defined up to where sinh is valid, which is # arcsinh(np.finfo(dtype).max). log_boundary = np.log( np.sinh(np.arcsinh(np.finfo(dtype).max) / tailweight - skewness)) x = np.array([ np.logspace(-2, log_boundary[0], base=np.e, num=1000), np.logspace(-2, log_boundary[1], base=np.e, num=1000) ], dtype=dtype) # Ensure broadcasting works. x = np.swapaxes(x, 0, 1) y = np.sinh((np.arcsinh(x) + skewness) * tailweight) bijector = tfb.SinhArcsinh( skewness=skewness, tailweight=tailweight, validate_args=True) self.assertAllClose( y, self.evaluate(bijector.forward(x)), rtol=1e-4, atol=0.) self.assertAllClose( x, self.evaluate(bijector.inverse(y)), rtol=1e-4, atol=0.) # On IBM PPC systems, longdouble (np.float128) is same as double except # that it can have more precision. Type double being of 8 bytes, can't # hold square of max of float64 (which is also 8 bytes). # Below test fails due to overflow error giving inf. This check avoids # that error by skipping square calculation and corresponding assert. if (np.amax(y) <= np.sqrt(np.finfo(np.float128).max) and np.fabs(np.amin(y)) <= np.sqrt(np.fabs(np.finfo(np.float128).min))): # Do the numpy calculation in float128 to avoid inf/nan. y_float128 = np.float128(y) self.assertAllClose( np.log(np.cosh( np.arcsinh(y_float128) / tailweight - skewness) / np.sqrt( y_float128**2 + 1)) - np.log(tailweight), self.evaluate( bijector.inverse_log_det_jacobian(y, event_ndims=0)), rtol=1e-4, atol=0.) self.assertAllClose( self.evaluate(-bijector.inverse_log_det_jacobian(y, event_ndims=0)), self.evaluate(bijector.forward_log_det_jacobian(x, event_ndims=0)), rtol=1e-4, atol=0.)
def check_equation_20(q,gamma=3.): ''' Kinematic factor difference between analytic and numerical for gamma = 3 ''' Q = q/qpot_from_q(q) F=.5*quad(lambda t: np.sin(t)**3*(np.sin(t)**2+np.cos(t)**2./Q/Q)**(-gamma/2.),0.,np.pi)[0]/quad(lambda t: np.sin(t)*np.cos(t)**2*(np.sin(t)**2+np.cos(t)**2./Q/Q)**(-gamma/2.),0.,np.pi)[0] F2 = binney_tremaine_virial_ratio(q) Qst = 1./Q/Q if(gamma==3.): if(Qst>1.): Q = np.sqrt(Qst-1.) G = .5*(Qst*Q-np.sqrt(Qst)*np.arcsinh(Q))/(np.sqrt(Qst)*np.arcsinh(Q)-Q) else: Q = np.sqrt(1.-Qst) G = .5*(Qst*Q*Q-np.sqrt(Qst)*Q*np.arccos(np.sqrt(Qst)))/(np.sqrt(Qst)*Q*np.arccos(np.sqrt(Qst))-Q*Q) if(gamma==2.): if(Qst>1.): Q = np.sqrt(Qst-1.) T = np.arctan(Q) G = .5*(Qst*T-Q)/(Q-T) else: Q = np.sqrt(1.-Qst) T = np.arctanh(Q) G = .5*(Qst*T-Q)/(Q-T) if(gamma==4.): if(Qst>1.): Q = np.sqrt(Qst-1.) G = (Qst*(Qst-2.)*np.arctan(Q)+Qst*Q)/(Qst*np.arctan(Q)-Q)/2. else: Q = np.sqrt(1.-Qst) G = (Qst*(Qst-2.)*np.arctanh(Q)+Qst*Q)/(Qst*np.arctanh(Q)-Q)/2. if(gamma==3.): q = np.linspace(0.2,4.) FF = np.zeros(len(q)) FF2 = np.zeros(len(q)) for n,i in enumerate(q): Q = i/qpot_from_q(i) FF[n]=.5*quad(lambda t: np.sin(t)**3*(np.sin(t)**2+np.cos(t)**2./Q/Q)**(-gamma/2.),0.,np.pi)[0]/quad(lambda t: np.sin(t)*np.cos(t)**2*(np.sin(t)**2+np.cos(t)**2./Q/Q)**(-gamma/2.),0.,np.pi)[0] FF2[n] = binney_tremaine_virial_ratio(i) plt.plot(q,np.log10(FF)) plt.plot(q,np.log10(FF2)) plt.savefig('tmp.pdf') plt.clf() if(gamma==3.): return F-G,F2-G else: return F-G
def schmidt_vals(dw,aas,aai,eps,deltaw,f): """ Args: dw: size of the grid spacing aas=relative slowness of the signal mode aai=relative slowness of the idler mode lnl=inverse of the strength of the nonlinearity deltaw: specifies the size of the frequency grid going from -deltaw to deltaw for each frequency f: shape of the pump function """ ddws=np.arange(-deltaw-dw/2,deltaw+dw/2,dw) deltaks=aas*ddws ddwi=np.arange(-deltaw-dw/2,deltaw+dw/2,dw) deltaki=aai*ddwi ds=np.diag(deltaks) di=np.diag(deltaki) def ff(x,y): return f(x+y) v=eps*(dw)*ff(ddwi[:,None],ddws[None,:]) G=1j*np.concatenate((np.concatenate((ds,v),axis=1),np.concatenate((-v,-di),axis=1)),axis=0) z=1; dsi=np.concatenate((deltaks,-deltaki),axis=0) U0=linalg.expm(-1j*np.diag(dsi)*z/2) GG=np.dot(np.dot(U0,linalg.expm(G)),U0) n=len(ddws) C=GG[0:n,n:2*n] na=np.dot(np.conj(np.transpose(C)),C)*dw vv=np.arcsinh(np.sqrt(np.diag(np.diag(linalg.svdvals(na))/dw))) return vv
def showFakeObjects(root1, root2, visit, ccd, root="", matchObjs=None, noMatch=None, badMatch=None): # get the image array before the fake objects are added imgBefore = getExpArray(root + root1, visit, ccd) imgAfter = getExpArray(root + root2, visit, ccd) # get the difference between the two image imgDiff = (imgAfter - imgBefore) # stretch it with arcsinh and make a png with pyplot fig, axes = pyplot.subplots(1, 3, sharex=True, sharey=True, figsize=(15,10)) pyplot.subplots_adjust(left=0.04, bottom=0.03, right=0.99, top=0.97, wspace=0.01, hspace = 0.01) imgs = imgBefore, imgAfter, imgDiff titles = "Before", "After", "Diff" for i in range(3): axes[i].imshow(numpy.arcsinh(imgs[i]), cmap='gray') axes[i].set_title(titles[i]) area1 = numpy.pi * 6 ** 2 area2 = numpy.pi * 4 ** 2 if matchObjs is not None: axes[i].scatter(matchObjs['X'], matchObjs['Y'], s=area1, edgecolors='g', alpha=0.9) if noMatch is not None: axes[i].scatter(noMatch['X'], noMatch['Y'], s=area2, c='r', alpha=0.3) if badMatch is not None: axes[i].scatter(badMatch['X'], badMatch['Y'], s=area2, c='b', alpha=0.4) pyplot.gcf().savefig("%s-%d-%s.png"%(root2, visit, str(ccd)))
def asinh_scale(image, alpha=0.02, nonlinearity=8.0): image_out=numpy.array(image, dtype='f8', copy=True) image_out[:] = \ numpy.arcsinh( alpha*nonlinearity*image )/nonlinearity return image_out
def plotFakeGalaxy(galObj, galID=None, suffix=None, size=0, addPoisson=False): """ Generate a PNG image of the model By default, the image will be named as 'fake_galaxy.png' """ import matplotlib.pyplot as plt if galID is None: outPNG = 'fake_galaxy' else: outPNG = 'fake_galaxy_%i' % galID if suffix is not None: outPNG = outPNG + '_' + suffix.strip() + '.png' plt.figure(1, figsize=(8, 8)) # Use "fft" just to be fast plt.imshow(np.arcsinh(galSimDrawImage(galObj, size=size, method="no_pixel", addPoisson=addPoisson, scale=1.0))) plt.savefig(outPNG)
def int_pot_2D(xp, yp, x, R, h, basis_func): """ Returns contribution of a point xp,yp, belonging to a basis source support centered at (0,0) to the potential measured at (x,0), integrated over xp,yp gives the potential generated by a basis source element centered at (0,0) at point (x,0) **Parameters** xp, yp : floats coordinates of some point laying in the support of a basis element centered at (0,0) x : float coordinates of a point (x,0) at which we calculate the potential R : float radius of the basis element basis_func: callable axis-symmetric 2D basis function in the source space **Returns** pot : float """ y = ((x-xp)**2 + yp**2)**(0.5) if y < 0.00001: y = 0.00001 pot = np.arcsinh(h/y) pot *= basis_func(xp, yp, [0, 0], R) return pot
def age_flat(z, **cosmo): """The age of the universe assuming a flat cosmology. Units are s. Analytical formula from Peebles, p. 317, eq. 13.2. Examples -------- >>> from utils import distance as cd >>> from core import constants as cc >>> cosmo = {'omega_M_0' : 0.3, 'omega_lambda_0' : 0.7, 'h' : 0.72} >>> cosmo = cd.set_omega_k_0(cosmo) >>> t = cd.age_flat(6.0, **cosmo) >>> print "age at z=6.0 is %.3g Gyr" % (t/cc.Gyr_s) age at z=6.0 is 0.892 Gyr """ omega_k = get_omega_k_0(**cosmo) if (numpy.any(omega_k != 0)): #raise ValueError("Not implemented for Omega_k != 0") print "Warning: using lambda = 1 - omega_M for non-flat cosmology!" om = cosmo['omega_M_0'] lam = 1. - cosmo['omega_M_0'] t_z = (2. * numpy.arcsinh(numpy.sqrt(lam / om) * (1. + z) ** (-3. / 2.)) / (cc.H100_s * cosmo['h'] * 3. * numpy.sqrt(lam))) return t_z
def true_to_anom(true_anom, e): """ Converts true anomaly to the proper orbit anomaly (Algorithm 5) Converts true anomaly to eccentric (E) anomaly for elliptical orbits, parabolic (B) anomaly for parabolic orbits, or hyperbolic anomaly (H) for hyperbolic orbits. For reference, see Algorithm 5 in Vallado (Fourth Edition), Section 2.2 (pg 77). Parameters ---------- true_anom: double True anomaly (radians) e: double Eccentricity Returns ------- E/B/H: double Eccentric, Parabolic, or Hyperbolic anomaly (radians) """ if e < 1.0: num = np.sin(true_anom)*np.sqrt(1.0 - e**2) denom = 1 + e*np.cos(true_anom) E = np.arcsin(num/denom) return E elif e == 1.0: B = np.tan(0.5*true_anom) return B else: num = np.sin(true_anom)*np.sqrt(e**2 - 1) denom = 1 + e*np.cos(true_anom) H = np.arcsinh(num/denom) return H
def calc_eta(array): return np.arcsinh(array['pz'] / calc_pt(array))
def check_loss_of_precision(self, dtype): """Check loss of precision in complex arc* functions""" # Check against known-good functions info = np.finfo(dtype) real_dtype = dtype(0.).real.dtype eps = info.eps def check(x, rtol): x = x.astype(real_dtype) z = x.astype(dtype) d = np.absolute(np.arcsinh(x) / np.arcsinh(z).real - 1) assert_(np.all(d < rtol), (np.argmax(d), x[np.argmax(d)], d.max(), 'arcsinh')) z = (1j * x).astype(dtype) d = np.absolute(np.arcsinh(x) / np.arcsin(z).imag - 1) assert_(np.all(d < rtol), (np.argmax(d), x[np.argmax(d)], d.max(), 'arcsin')) z = x.astype(dtype) d = np.absolute(np.arctanh(x) / np.arctanh(z).real - 1) assert_(np.all(d < rtol), (np.argmax(d), x[np.argmax(d)], d.max(), 'arctanh')) z = (1j * x).astype(dtype) d = np.absolute(np.arctanh(x) / np.arctan(z).imag - 1) assert_(np.all(d < rtol), (np.argmax(d), x[np.argmax(d)], d.max(), 'arctan')) # The switchover was chosen as 1e-3; hence there can be up to # ~eps/1e-3 of relative cancellation error before it x_series = np.logspace(-20, -3.001, 200) x_basic = np.logspace(-2.999, 0, 10, endpoint=False) if dtype is np.longcomplex: # It's not guaranteed that the system-provided arc functions # are accurate down to a few epsilons. (Eg. on Linux 64-bit) # So, give more leeway for long complex tests here: check(x_series, 50 * eps) else: check(x_series, 2 * eps) check(x_basic, 2 * eps / 1e-3) # Check a few points z = np.array([1e-5 * (1 + 1j)], dtype=dtype) p = 9.999999999333333333e-6 + 1.000000000066666666e-5j d = np.absolute(1 - np.arctanh(z) / p) assert_(np.all(d < 1e-15)) p = 1.0000000000333333333e-5 + 9.999999999666666667e-6j d = np.absolute(1 - np.arcsinh(z) / p) assert_(np.all(d < 1e-15)) p = 9.999999999333333333e-6j + 1.000000000066666666e-5 d = np.absolute(1 - np.arctan(z) / p) assert_(np.all(d < 1e-15)) p = 1.0000000000333333333e-5j + 9.999999999666666667e-6 d = np.absolute(1 - np.arcsin(z) / p) assert_(np.all(d < 1e-15)) # Check continuity across switchover points def check(func, z0, d=1): z0 = np.asarray(z0, dtype=dtype) zp = z0 + abs(z0) * d * eps * 2 zm = z0 - abs(z0) * d * eps * 2 assert_(np.all(zp != zm), (zp, zm)) # NB: the cancellation error at the switchover is at least eps good = (abs(func(zp) - func(zm)) < 2 * eps) assert_(np.all(good), (func, z0[~good])) for func in (np.arcsinh, np.arcsinh, np.arcsin, np.arctanh, np.arctan): pts = [ rp + 1j * ip for rp in (-1e-3, 0, 1e-3) for ip in (-1e-3, 0, 1e-3) if rp != 0 or ip != 0 ] check(func, pts, 1) check(func, pts, 1j) check(func, pts, 1 + 1j)
def lupton_stretch(I, Q, alpha): return numpy.arcsinh(alpha * Q * I) / (Q * I)
def transform(x): x = np.asarray(x) return np.arcsinh(x/(2*sigma)) / np.log(base)
def test_normalize_img(): # basic linear stretch img_arr = np.array([[1, 0], [.25, .75]]) assert ((img_arr * 255).astype(int) == cutouts.normalize_img( img_arr, stretch='linear')).all() # invert assert (255 - (img_arr * 255).astype(int) == cutouts.normalize_img( img_arr, stretch='linear', invert=True)).all() # linear stretch where input image must be scaled img_arr = np.array([[10, 5], [2.5, 7.5]]) norm_img = ((img_arr - img_arr.min()) / (img_arr.max() - img_arr.min()) * 255).astype(int) assert (norm_img == cutouts.normalize_img(img_arr, stretch='linear')).all() # min_max val minval, maxval = 0, 1 img_arr = np.array([[1, 0], [-1, 2]]) norm_img = cutouts.normalize_img(img_arr, stretch='linear', minmax_value=[minval, maxval]) img_arr[img_arr < minval] = minval img_arr[img_arr > maxval] = maxval assert ((img_arr * 255).astype(int) == norm_img).all() minval, maxval = 0, 1 img_arr = np.array([[1, 0], [.1, .2]]) norm_img = cutouts.normalize_img(img_arr, stretch='linear', minmax_value=[minval, maxval]) img_arr[img_arr < minval] = minval img_arr[img_arr > maxval] = maxval ((img_arr * 255).astype(int) == norm_img).all() # min_max percent img_arr = np.array([[1, 0], [0.1, 0.9], [.25, .75]]) norm_img = cutouts.normalize_img(img_arr, stretch='linear', minmax_percent=[25, 75]) assert (norm_img == [[255, 0], [0, 255], [39, 215]]).all() # asinh img_arr = np.array([[1, 0], [.25, .75]]) norm_img = cutouts.normalize_img(img_arr) assert ((np.arcsinh(img_arr * 10) / np.arcsinh(10) * 255).astype(int) == norm_img).all() # sinh img_arr = np.array([[1, 0], [.25, .75]]) norm_img = cutouts.normalize_img(img_arr, stretch='sinh') assert ((np.sinh(img_arr * 3) / np.sinh(3) * 255).astype(int) == norm_img).all() # sqrt img_arr = np.array([[1, 0], [.25, .75]]) norm_img = cutouts.normalize_img(img_arr, stretch='sqrt') assert ((np.sqrt(img_arr) * 255).astype(int) == norm_img).all() # log img_arr = np.array([[1, 0], [.25, .75]]) norm_img = cutouts.normalize_img(img_arr, stretch='log') assert ((np.log(img_arr * 1000 + 1) / np.log(1000) * 255).astype(int) == norm_img).all() # Bad stretch with pytest.raises(InvalidInputError): img_arr = np.array([[1, 0], [.25, .75]]) cutouts.normalize_img(img_arr, stretch='lin') # Giving both minmax percent and cut img_arr = np.array([[1, 0], [.25, .75]]) norm_img = cutouts.normalize_img(img_arr, stretch='asinh', minmax_percent=[0.7, 99.3]) with pytest.warns(InputWarning): test_img = cutouts.normalize_img(img_arr, stretch='asinh', minmax_value=[5, 2000], minmax_percent=[0.7, 99.3]) assert (test_img == norm_img).all()
def plot_ex1(cell, electrode, X, Y, Z): ''' plot the morphology and LFP contours, synaptic current and soma trace ''' # some plot parameters t_show = 30 # time point to show LFP tidx = np.where(cell.tvec == t_show) # contour lines: n_contours = 200 n_contours_black = 40 # This is the extracellular potential, reshaped to the X, Z mesh LFP = np.arcsinh(electrode.data[:, tidx]).reshape(X.shape) # figure object fig = plt.figure(figsize=(12, 8)) fig.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.4, hspace=0.4) # Plot LFP around the cell with in color and with equipotential lines ax1 = fig.add_subplot(121, aspect='equal', frameon=False) # plot_morphology(plot_synapses=True) for sec in neuron.h.allsec(): idx = cell.get_idx(sec.name()) ax1.plot(np.r_[cell.x[idx, 0], cell.x[idx, 1][-1]], np.r_[cell.z[idx, 0], cell.z[idx, 1][-1]], color='k') for i in range(len(cell.synapses)): ax1.plot([cell.synapses[i].x], [cell.synapses[i].z], '.', markersize=10) # contour lines ct1 = ax1.contourf(X, Z, LFP, n_contours) ct1.set_clim((-0.00007, 0.00002)) ax1.contour(X, Z, LFP, n_contours_black, colors='k') # Plot synaptic input current ax2 = fig.add_subplot(222) ax2.plot(cell.tvec, cell.synapses[0].i) # Plot soma potential ax3 = fig.add_subplot(224) ax3.plot(cell.tvec, cell.somav) # Figure formatting and labels fig.suptitle('example 1', fontsize=14) ax1.set_title('LFP at t=' + str(t_show) + ' ms', fontsize=12) ax1.set_xticks([]) ax1.set_xticklabels([]) ax1.set_yticks([]) ax1.set_yticklabels([]) ax2.set_title('synaptic input current', fontsize=12) ax2.set_ylabel('(nA)') ax2.set_xlabel('time (ms)') ax3.set_title('somatic membrane potential', fontsize=12) ax3.set_ylabel('(mV)') ax3.set_xlabel('time (ms)') return fig
def transform_non_affine(self, a): return self.linear_width * np.arcsinh(a / self.linear_width)
def inverse_sinh(X, deriv=False): if (deriv == True): return 1 / (np.sqrt(1 + np.square(X))) return np.arcsinh(X)
class Asinh(Activation): op = 'Asinh' version = 'opset4' operation = staticmethod(lambda x: np.arcsinh(x))
np.argmax(input, axis=0 if axis is None else axis) .astype(utils.numpy_dtype(output_type)))) argmin = utils.copy_docstring( tf.math.argmin, lambda input, axis=None, output_type=tf.int64, name=None: ( # pylint: disable=g-long-lambda np.argmin(input, axis=0 if axis is None else axis) .astype(utils.numpy_dtype(output_type)))) asin = utils.copy_docstring( tf.math.asin, lambda x, name=None: np.arcsin(x)) asinh = utils.copy_docstring( tf.math.asinh, lambda x, name=None: np.arcsinh(x)) atan = utils.copy_docstring( tf.math.atan, lambda x, name=None: np.arctan(x)) atan2 = utils.copy_docstring( tf.math.atan2, lambda y, x, name=None: np.arctan2(y, x)) atanh = utils.copy_docstring( tf.math.atanh, lambda x, name=None: np.arctanh(x)) bessel_i0 = utils.copy_docstring( tf.math.bessel_i0,
def create_quickview(filename, output_directory, verbose=False, clobber=True): logger = logging.getLogger("QuickView") create_otalevel = cmdline_arg_isset("-otalevel") scaling = cmdline_arg_set_or_default("-scaling", None) if (not scaling in ['linear', 'log', 'sqrt', 'arcsinh', 'asinh']): scaling = 'sqrt' hdulist = pyfits.open(filename) filter = hdulist[0].header['FILTER'] obsid = hdulist[0].header['OBSID'] object = hdulist[0].header['OBJECT'].replace(' ', '_').replace(',', '_') fullframe_image_filename = "%s/%s_%s.%s.jpg" % (output_directory, obsid, object, scaling) if (os.path.isfile(fullframe_image_filename) and not clobber): # File exists and we were asked not to overwrite anything stdout_write("\nFile (%s) exists, skipping ...\n" % (filename)) return if (verbose): stdout_write("\nWorking on file %s (%s, %s) ...\n" % (filename, object, filter)) fpl = podi_focalplanelayout.FocalPlaneLayout(hdulist) try: list_of_otas_to_normalize = fpl.otas_to_normalize_ff[filter] except: list_of_otas_to_normalize = fpl.central_2x2 # Allocate some memory to hold the data we need to determine the # most suitable intensity levels binned_data = numpy.zeros(shape=(13 * 512 * 512), dtype=numpy.float32) binned_data[:] = numpy.NaN # # # available_ota_coords = [] for ext in hdulist: if (not is_image_extension(ext)): continue try: ota = ext.header['OTA'] except: continue x = int(math.floor(ota / 10.)) y = int(ota % 10) available_ota_coords.append((x, y)) datapos = 0 # if (verbose): # stdout_write(" Finding contrast: Reading OTA") logger.info("Finding best contrast") for extension in range(1, len(hdulist)): if (not is_image_extension(hdulist[extension])): continue if ('CELLMODE' in hdulist[extension].header and hdulist[extension].header['CELLMODE'].find("V") > 0): logger.info("Skipping guide-OTA %s" % (hdulist[extension].header['EXTNAME'])) continue fppos = int(hdulist[extension].header['FPPOS'][2:4]) try: index = list_of_otas_to_normalize.index(fppos) except: # We didn't find this OTA in the list, so skip it hdulist[extension].header['FF_NORM'] = (False, "Used in normalization") extension += 1 continue logger.debug("Reading OTA %02d" % (fppos)) #stdout_write("\rReading OTA %02d" % (fppos)) # if (verbose): # stdout_write(" %02d" % (fppos)) # Rebin the frame 8x8 to make it more manageable binned = numpy.reshape(hdulist[extension].data, (512, 8, 512, 8)).mean(axis=-1).mean(axis=1) one_d = binned.flatten() binned_data[datapos:datapos + one_d.shape[0]] = one_d datapos += one_d.shape[0] del one_d del binned #if (verbose): # stdout_write(" - done!\n") # # Now we are through all OTA/extensions, compute the median value and stds # so we can scale the frames accordingly # #if (verbose): # stdout_write(" Finding best intensity levels ...") median = 0 std = 1e8 binned_data = binned_data[0:datapos] for looper in range(3): valid = (binned_data > (median - std)) & (binned_data < (median + 3 * std)) #print numpy.sum(valid) median = numpy.median(binned_data[valid]) std = numpy.std(binned_data[valid]) #print median, std, median-std, median+3*std # Compute the intensity levels, making sure not to exceed the maximum possible range min_level = numpy.max([median - 1 * std, 0]) max_level = numpy.min([median + 8 * std, 60000]) # stdout_write(" using %d ... %d\n" % (min_level, max_level)) logger.info("Using intensity scale %d ... %d" % (min_level, max_level)) # # Now that we have all the scaling factors, go ahead and create the preview images # # Create space to hold the full 8x8 OTA focal plane full_focalplane = numpy.zeros(shape=(4096, 4096)) # if (verbose): # stdout_write(" Creating jpeg for OTA") logger.info("Creating jpeg for OTAs and full focalplane") for extension in range(1, len(hdulist)): if (not is_image_extension(hdulist[extension])): continue fppos = int(hdulist[extension].header['FPPOS'][2:4]) logger.info("Creating JPG for extension %s" % (hdulist[extension].name)) #stdout_write("\r Creating jpegs (%02d) ..." % fppos) # if (verbose): # stdout_write(" %02d" % (fppos)) fp_x = fppos % 10 fp_y = math.floor(fppos / 10) hdulist[extension].data[numpy.isnan(hdulist[extension].data)] = 0 binned = numpy.reshape(hdulist[extension].data, (512, 8, 512, 8)).mean(axis=-1).mean(axis=1) greyscale = (binned - min_level) / (max_level - min_level) greyscale[greyscale < 0] = 0 greyscale[greyscale >= 1] = 1 print "ASINH?", (scaling in ['arcsinh', 'asinh']) if (scaling == 'sqrt'): greyscale = numpy.sqrt(greyscale) elif (scaling in ['arcsinh', 'asinh']): greyscale = numpy.arcsinh(greyscale) elif (scaling == 'log'): greyscale = numpy.log10(greyscale + 1) / numpy.log10(2.0) else: # (scaling == 'linear') pass ffp_x = fp_x * 512 ffp_y = fp_y * 512 full_focalplane[ffp_x:ffp_x + 512, ffp_y:ffp_y + 512] = greyscale[:, :] #.astype(numpy.int) #image = Image.fromarray(numpy.uint8(greyscale*255)) #image_filename = "%s/%s_%s.%02d.jpg" % (output_directory, obsid, object, fppos) #image.transpose(Image.FLIP_TOP_BOTTOM).save(image_filename, "JPEG") #del image if (create_otalevel): # # Mark all overexposed pixels in a different color # channel_r = greyscale + 0 channel_g = greyscale + 0 channel_b = greyscale + 0 channel_r[binned > limit_overexposed] = overexposed[0] channel_g[binned > limit_overexposed] = overexposed[1] channel_b[binned > limit_overexposed] = overexposed[2] im_r = Image.fromarray(numpy.uint8(channel_r * 255)) im_g = Image.fromarray(numpy.uint8(channel_g * 255)) im_b = Image.fromarray(numpy.uint8(channel_b * 255)) im_rgb = Image.merge('RGB', (im_r, im_g, im_b)) image_filename = "%s/%s_%s.%02d.rgb-%s.jpg" % ( output_directory, obsid, object, fppos, scaling) im_rgb.transpose(Image.FLIP_TOP_BOTTOM).save( image_filename, "JPEG") # Delete all temporary images to keep memory demands low del im_r del im_g del im_b del im_rgb # # Prepare the preview for the full focal plane # #stdout_write("\r Creating jpegs (full-frame) ...") # if (verbose): # stdout_write(" full-frame") image = Image.fromarray(numpy.uint8(full_focalplane * 255)) # Add lines to indicate detector borders. Make sure to make them wider than # just one pixel, otherwise the lines completely disappear if displaying the # image zoomed out draw = ImageDraw.Draw(image) for i in range(1, 8): for linewidth in range(-5, 0): draw.line( (0, i * 512 + linewidth, image.size[0], i * 512 + linewidth), fill=128) draw.line( (i * 512 + linewidth, 0, i * 512 + linewidth, image.size[1]), fill=128) if (crossout_missing_otas): # Now loop over all OTAs and mark the ones that do not exist for y in range(8): for x in range(8): tuple = (x, y) try: index = available_ota_coords.index(tuple) except: # We only get here if the OTA is not listed as available # cross it out in the full focal plane image #print "Strokign out ",tuple for lw in range(-5, 0): draw.line((x * 512 + lw, y * 512, (x + 1) * 512 + lw, (y + 1) * 512), fill=128) draw.line((x * 512 + lw, (y + 1) * 512, (x + 1) * 512 + lw, y * 512), fill=128) image = image.transpose(Image.FLIP_TOP_BOTTOM) watermark_OTA = True dx, dy = 150, 150 if (watermark_OTA): image = image.convert('RGBA') draw = ImageDraw.Draw(image) font = ImageFont.truetype('/usr/share/./fonts/truetype/DroidSans.ttf', size=200) for x, y in itertools.product(range(8), repeat=2): try: index = available_ota_coords.index((x, y)) except: continue draw.text((x * 512 + dx, (7 - y) * 512 + dy), "%02d" % (x * 10 + y), font=font, fill=(0, 64, 128, 255)) image.save(fullframe_image_filename, "JPEG") del image logger.info("Writing file to %s" % (fullframe_image_filename)) # stdout_write(" - done!\n") logger.info("all done!\n")
def ode(x: np.ndarray, t: np.ndarray, *args) -> np.ndarray: """ Ordinary differential equation (ODE) for Process DA. Arguments: X : [biomasa, dqo_out, agv_out] t : timesteps args: [dil, agv_in, dqo_in] Returns: np.array([new_ace_oute, new_xa, new_xm, new_xh, new_Mox, new_imec, qh2]) """ agv_in = args[0] dil = args[1] eapp = args[2] A = x[0] xa = x[1] xm = x[2] xh = x[3] Mox = x[4] Imec = x[5] qh2 = x[6] # Parametros del modelo para ode qmaxa = 13.14 # d^-1 qmaxm = 14.12 # d^-1 Ksa = 20.0 # mg L^-1 Ksm = 80.0 # mg L^-1 KM = .01 # mg L^-1 umaxa = 1.97 # d^-1 Kda = .04 # d^-1 --> modificado umaxm = .3 # d^-1 Kdm = .01 # d^-1 --> modificado umaxh = .5 # d^-1 Kdh = .01 # d^-1 Kh = .001 # mg L^-1 H2 = 1.0 # mg L^-1 gamma = 663400.0 # mg-M/mol_med Vr = 1.0 # L m = 2.0 # mol-e/mol-H2 F = 96485.0 # C mol-e^-1 F1 = F / (60.0 * 60.0 * 24.0) # A d mol-e^-1 # YM = 34.85 # mg-M/mg-A --> modificado YM = 3.3 # Yh = 0.05 # mLH2/mgX/d # Xmax1 = 512.5 # mg L^-1 # Xmax2 = (1680.0+750.0)/2.0 # mg L^-1 Mtotal = 1000.0 # mgM mg x^-1 beta = 0.5 AsA = 0.01 # m^2 # io =.005 # Am^-2 io = 1.0 E_CEF = -0.35 # V E_app = eapp # Default= .5 V == 500mv # Si E_app está en 0, existe una falla) # Por encima de 10 V, igual hay falla # Rmin = 20 Rmin = 2.0 # Rmax = 2000 Rmax = 200.0 KR = .024 R = 8.314 # J mol^-1 K^-1 # R1 = 82.05 # mL atm mol^-1 K^-1 T = 298.15 # K # Ecuaciones cinéticas qa = ((qmaxa * A) / (Ksa + A)) * (Mox / (KM + Mox)) qm = (qmaxm * A) / (Ksm + A) mua = ((umaxa * A) / (Ksa + A)) * (Mox / (KM + Mox)) mum = (umaxm * A) / (Ksm + A) muh = (umaxh * H2) / (Kh + H2) alpha1 = 0.5410 alpha2 = 0.4894 # ecuaciones electroquimicas # print("Mox:{}".format(Mox)) Mred = Mtotal - Mox # print("Mtotal:{}".format(Mtotal)) # print("Mred:{}".format(Mred)) # print("xa:{}".format(xa)) Rint = Rmin + (Rmax - Rmin) * np.exp(-KR * xa) etha_concA = ((R * T) / (m * F)) * np.log(Mtotal / Mred) etha_actC = ((R * T) / (beta * m * F)) * np.arcsinh(Imec / (AsA * io)) corriente = (E_CEF + E_app - etha_concA - etha_actC) / Rint # print("Corriente:{}".format(Corriente)) # balance de masas dS = dil * (agv_in - A) - qa * xa - qm * xm dXa = mua * xa - Kda * xa - alpha1 * dil * xa dXm = mum * xm - Kdm * xm - alpha1 * dil * xm dXh = muh * xh - Kdh * xh - alpha2 * dil * xh dMox = ((gamma * Imec) / (Vr * xa * m * F1)) - YM * qa return np.array([dS, dXa, dXm, dXh, dMox, (corriente - Imec), qh2])
def simulate_images(psffits, theofits, distance, obsfits=None, extfits=None, posang=0, foi_as=4, fnucdiam_as=0.45, outname=None, scale='log', inv=True, cmap='gist_heat', manscale=None, suffix=None, fitsize=None, outfolder='.', labelcolor='black', writeoutfits=False, writefitplot=False, debug=False, fitpsf=False, theomax=99.99, theomin=0.01, wavelens=None, pfovs=None, filts=None, immin=None, immax=None, theoscale='log'): # --- TO DO: # - Writefitplot currently collides with the main plot and makes it empty # for unknown reasons # --- check whether a list (or just a single file) was provided if not isinstance(psffits, list): psffits = [psffits] nf = len(psffits) if obsfits is not None: if not isinstance(obsfits, list): obsfits = [obsfits] else: obsfits = [None] * nf if wavelens is None: wavelens = [None] * nf if pfovs is None: pfovs = [None] * nf if filts is None: filts = [None] * nf # --- prepare the plot if debug is False: plt.clf() mpl.rcdefaults() # fig = plt.figure(1, (10, 24)) fig = plt.figure(figsize=(16, 1 + 4 * nf)) fig.subplots_adjust(bottom=0.2) # ax = fig.add_subplot(111) if inv: cmap = cmap + '_r' mpl.rc('axes', edgecolor=labelcolor) mpl.rc('xtick', color=labelcolor) mpl.rc('ytick', color=labelcolor) grid = ImageGrid( fig, 111, # similar to subplot(111) nrows_ncols=(nf, 4), # creates 2x2 grid of axes axes_pad=0.1, aspect=True # pad between axes in inch. ) wliststr = '' # --- simulate the individual images for i in range(nf): (psf, mod, im, simim, wlen, pfov, theopfov) = simulate_image(psffits=psffits[i], theofits=theofits, obsfits=obsfits[i], distance=distance, extfits=extfits, posang=posang, foi_as=foi_as, fnucdiam_as=fnucdiam_as, outname=outname, manscale=manscale, suffix=suffix, fitsize=fitsize, outfolder=outfolder, writeallfits=writeoutfits, fitpsf=fitpsf, writefitplot=writefitplot, debug=debug, wlen=wavelens[i], pfov=pfovs[i], filt=filts[i]) print("") # -- set the ranges if immin is not None: minval = np.nanpercentile(im, immin) im[im < minval] = minval simim[simim < minval] = minval if immax is not None: maxval = np.nanpercentile(im, immax) im[im > maxval] = maxval simim[simim > maxval] = maxval # --- cut the low background noise # im[im < np.median(im)] = np.median(im) # simim[simim < np.median(simim)] = np.median(simim) # --- scale the model image differently if theomin is not None: theomin_val = np.nanpercentile(mod[mod > 0], theomin) mod[mod < theomin_val] = theomin_val if theomax is not None: theomax_val = np.nanpercentile(mod[mod > 0], theomax) mod[mod > theomax_val] = theomax_val if theoscale == 'log': mod = np.log10(1000 * (mod - np.nanmin(mod)) / np.nanmax(mod) + 1) elif theoscale == 'asinh': mod = np.arcsinh(mod) #print(theomin_val, theomin) if scale == 'log': norm = np.max(im) psf = np.log10(1000 * (psf - np.min(psf)) / np.max(psf) + 1) im = np.log10(1000 * (im - np.min(im)) / norm + 1) simim = np.log10(1000 * (simim - np.min(simim)) / norm + 1) # print(np.min(im)) # print(np.nanmin(mod)) if scale == 'sqrt': psf = np.sqrt(psf) im = np.sqrt(im) simim = np.sqrt(simim) mod = np.sqrt(mod) if scale == 'asinh': psf = np.arcsinh(psf) im = np.arcsinh(im) simim = np.arcsinh(simim) # dirty trick to ensure that the simulated image has the same scaling # as the real image simim[0, 0] = np.max(im) # --- do the actual plotting if debug is False: handle = grid[0 + i * 4].imshow(psf, cmap=cmap, origin='lower', interpolation='nearest') grid[0 + i * 4].set_title('PSF', x=0.5, y=0.85, color=labelcolor) wlenstr = "{:.1f}".format(wlen) grid[0 + i * 4].text(0.05, 0.95, wlenstr + '$\mu\mathrm{m}$', fontsize=20, transform=grid[0 + i * 4].transAxes, verticalalignment='top', horizontalalignment='left') ny, nx = im.shape handle.set_extent( np.array([-nx / 2, nx / 2 - 1, -nx / 2, nx / 2 - 1]) * pfov) handle = grid[1 + i * 4].imshow(im, cmap=cmap, origin='lower', interpolation='nearest') grid[1 + i * 4].set_title('real', x=0.5, y=0.85, color=labelcolor) handle.set_extent( np.array([-nx / 2, nx / 2 - 1, -nx / 2, nx / 2 - 1]) * pfov) handle = grid[2 + i * 4].imshow(simim, cmap=cmap, origin='lower', interpolation='nearest') grid[2 + i * 4].set_title('simulated', x=0.5, y=0.85, color=labelcolor) handle.set_extent( np.array([-nx / 2, nx / 2 - 1, -nx / 2, nx / 2 - 1]) * pfov) handle = grid[3 + i * 4].imshow(mod, cmap=cmap, origin='lower', interpolation='nearest') mny, mnx = mod.shape grid[3 + i * 4].set_title('model', x=0.5, y=0.85, color=labelcolor) handle.set_extent( np.array([-mnx / 2, mnx / 2 - 1, -mnx / 2, mnx / 2 - 1]) * theopfov) wliststr = wliststr + wlenstr + "+" # --- formatting of axis # get the extent of the largest box containing all the axes/subplots extents = np.array([bla.get_position().extents for bla in grid]) bigextents = np.empty(4) bigextents[:2] = extents[:, :2].min(axis=0) bigextents[2:] = extents[:, 2:].max(axis=0) # --- text to mimic the x and y label. The text is positioned in # the middle xlabelpad = 0.03 # distance between the external axis and the text ylabelpad = 0.03 fig.text((bigextents[2] + bigextents[0]) / 2, bigextents[1] - xlabelpad, 'RA offset ["]', horizontalalignment='center', verticalalignment='bottom') fig.text(bigextents[0] - ylabelpad, (bigextents[3] + bigextents[1]) / 2, 'DEC offset ["]', rotation='vertical', horizontalalignment='left', verticalalignment='center') #plt.show() if debug is False: if not outname: theofitsfile = theofits.split("/")[-1] out_str = theofitsfile.replace(".fits", "") diststr = "{:.1f}".format(distance) pastr = "{:.0f}".format(posang) wliststr = wliststr[0:-1] out_str = (out_str + "_pa" + pastr + "_dist" + diststr + "_wlen" + wliststr) if suffix: out_str = out_str + "_" + suffix else: out_str = outname out_str = outfolder + '/' + out_str imout = out_str + ".pdf" plt.savefig(imout, bbox_inches='tight', pad_inches=0.1)
def test_asinh_with_asinh_a(self): """Test asinh scaling with a custom asinh_a.""" asinh_a = 0.5 norm = simple_norm(DATA2, stretch='asinh', asinh_a=asinh_a) ref = np.arcsinh(DATA2SCL / asinh_a) / np.arcsinh(1. / asinh_a) assert_allclose(norm(DATA2), ref, atol=0, rtol=1.e-5)
def test_asinh(self): """Test asinh scaling.""" norm = simple_norm(DATA2, stretch='asinh') ref = np.arcsinh(10 * DATA2SCL) / np.arcsinh(10) assert_allclose(norm(DATA2), ref, atol=0, rtol=1.e-5)
import numpy as np x = 1.0 y = 2.0 # trigonometric functions print(np.sin(x)) print(np.cos(x)) print(np.tan(x)) print(np.arcsin(x)) print(np.arccos(x)) print(np.arctan(x)) print(np.arctan2(x, y)) # arctan(x/y) print(np.rad2deg(x)) # hyperbolic functions print(np.sinh(x)) print(np.cosh(x)) print(np.tanh(x)) print(np.arcsinh(x)) print(np.arccosh(x)) print(np.arctanh(x))
def func_Lrope(w, vr): L1 = np.sqrt(w**2 / 4 + 4 * vr**2) L2 = w**2 / (8 * vr) * np.arcsinh(4 * vr / w) return L1 + L2
def z_from_t_analytic(my_time, hubble_constant=0.7, omega_matter=0.3, omega_lambda=0.7): """ Compute the redshift from time after the big bang. This is based on Enzo's CosmologyComputeExpansionFactor.C, but altered to use physical units. """ hubble_constant = YTQuantity(hubble_constant, "100*km/s/Mpc") omega_curvature = 1.0 - omega_matter - omega_lambda OMEGA_TOLERANCE = 1e-5 ETA_TOLERANCE = 1.0e-10 # Convert the time to Time * H0. if not isinstance(my_time, YTArray): my_time = YTArray(my_time, "s") t0 = (my_time.in_units("s") * hubble_constant.in_units("1/s")).to_ndarray() # For a flat universe with omega_matter = 1, it's easy. if np.fabs(omega_matter-1) < OMEGA_TOLERANCE and \ omega_lambda < OMEGA_TOLERANCE: a = np.power(1.5 * t0, 2.0 / 3.0) # For omega_matter < 1 and omega_lambda == 0 see # Peebles 1993, eq. 13-3, 13-10. # Actually, this is a little tricky since we must solve an equation # of the form eta - np.sinh(eta) + x = 0.. elif omega_matter < 1 and omega_lambda < OMEGA_TOLERANCE: x = 2 * t0 * np.power(1.0 - omega_matter, 1.5) / omega_matter # Compute eta in a three step process, first from a third-order # Taylor expansion of the formula above, then use that in a fifth-order # approximation. Then finally, iterate on the formula itself, solving for # eta. This works well because parts 1 & 2 are an excellent approximation # when x is small and part 3 converges quickly when x is large. eta = np.power(6 * x, 1.0 / 3.0) # part 1 eta = np.power(120 * x / (20 + eta * eta), 1.0 / 3.0) # part 2 mask = np.ones(eta.size, dtype=bool) max_iter = 1000 for i in range(max_iter): # part 3 eta_old = eta[mask] eta[mask] = np.arcsinh(eta[mask] + x[mask]) mask[mask] = np.fabs(eta[mask] - eta_old) >= ETA_TOLERANCE if not mask.any(): break if (i == max_iter - 1): raise RuntimeError("No convergence after %d iterations." % i) # Now use eta to compute the expansion factor (eq. 13-10, part 2). a = omega_matter/(2.0*(1.0 - omega_matter))*\ (np.cosh(eta) - 1.0) # For flat universe, with non-zero omega_lambda, see eq. 13-20. elif np.fabs(omega_curvature) < OMEGA_TOLERANCE and \ omega_lambda > OMEGA_TOLERANCE: a = np.power(omega_matter / (1 - omega_matter), 1.0/3.0) * \ np.power(np.sinh(1.5 * np.sqrt(1.0 - omega_matter) * \ t0), 2.0/3.0) else: raise NotImplementedError redshift = (1.0 / a) - 1.0 return redshift
def evaluate_dp(self, dx, dydx): """Evalate arc length along the trace given trace polynomial coefficients Parameters ---------- dx : array-like x pixel to evaluate dydx : array-like Coefficients of the trace polynomial Returns ------- dp : array-like Arc length along the trace at position `dx`. For `dydx` polynomial orders 0, 1 or 2, integrate analytically. Higher orders must be integrated numerically. **Constant:** .. math:: dp = dx **Linear:** .. math:: dp = \sqrt{1+\mathrm{DYDX}[1]}\cdot dx **Quadratic:** .. math:: u = \mathrm{DYDX}[1] + 2\ \mathrm{DYDX}[2]\cdot dx .. math:: dp = (u \sqrt{1+u^2} + \mathrm{arcsinh}\ u) / (4\cdot \mathrm{DYDX}[2]) """ ## dp is the arc length along the trace ## $\lambda = dldp_0 + dldp_1 dp + dldp_2 dp^2$ ... poly_order = len(dydx) - 1 if (poly_order == 2): if np.abs(np.unique(dydx[2])).max() == 0: poly_order = 1 if poly_order == 0: ## dy=0 dp = dx elif poly_order == 1: ## constant dy/dx dp = np.sqrt(1 + dydx[1]**2) * (dx) elif poly_order == 2: ## quadratic trace u0 = dydx[1] + 2 * dydx[2] * (0) dp0 = (u0 * np.sqrt(1 + u0**2) + np.arcsinh(u0)) / (4 * dydx[2]) u = dydx[1] + 2 * dydx[2] * (dx) dp = (u * np.sqrt(1 + u**2) + np.arcsinh(u)) / (4 * dydx[2]) - dp0 else: ## high order shape, numerical integration along trace ## (this can be slow) xmin = np.minimum((dx).min(), 0) xmax = np.maximum((dx).max(), 0) xfull = np.arange(xmin, xmax) dyfull = 0 for i in range(1, poly_order): dyfull += i * dydx[i] * (xfull - 0.5)**(i - 1) ## Integrate from 0 to dx / -dx dpfull = xfull * 0. lt0 = xfull < 0 if lt0.sum() > 1: dpfull[lt0] = np.cumsum(np.sqrt(1 + dyfull[lt0][::-1]**2))[::-1] dpfull[lt0] *= -1 # gt0 = xfull > 0 if gt0.sum() > 0: dpfull[gt0] = np.cumsum(np.sqrt(1 + dyfull[gt0]**2)) dp = np.interp(dx, xfull, dpfull) if dp[-1] == dp[-2]: dp[-1] = dp[-2] + np.diff(dp)[-2] return dp
def plotResults(orig, data, gmm, patch=None, description=None, disp=None): fig = plt.figure(figsize=(6, 6)) ax = fig.add_subplot(111, aspect='equal') # plot inner and outer points ax.plot(orig[:, 0], orig[:, 1], 'o', mfc='None', mec='r', mew=1) missing = np.isnan(data) if missing.any(): data_ = data.copy() data_[missing] = -5 # put at limits of plotting range else: data_ = data ax.plot(data_[:, 0], data_[:, 1], 's', mfc='b', mec='None') #, mew=1) # prediction B = 100 x, y = np.meshgrid(np.linspace(-5, 15, B), np.linspace(-5, 15, B)) coords = np.dstack((x.flatten(), y.flatten()))[0] # compute sum_k(p_k(x)) for all x p = gmm(coords).reshape((B, B)) # for better visibility use arcshinh stretch p = np.arcsinh(p / 1e-4) cs = ax.contourf(p, 10, extent=(-5, 15, -5, 15), cmap=plt.cm.Greys) for c in cs.collections: c.set_edgecolor(c.get_facecolor()) # plot boundary if patch is not None: import copy if hasattr(patch, '__iter__'): for p in patch: ax.add_artist(copy.copy(p)) else: ax.add_artist(copy.copy(patch)) # add description and complete data logL to plot logL = gmm(orig, as_log=True).mean() if description is not None: ax.text(0.05, 0.95, r'%s' % description, ha='left', va='top', transform=ax.transAxes, fontsize=20) ax.text(0.05, 0.89, '$\log{\mathcal{L}} = %.3f$' % logL, ha='left', va='top', transform=ax.transAxes, fontsize=20) else: ax.text(0.05, 0.95, '$\log{\mathcal{L}} = %.3f$' % logL, ha='left', va='top', transform=ax.transAxes, fontsize=20) # show size of error dispersion as Circle if disp is not None: circ1 = patches.Circle((12.5, -2.5), radius=disp, fc='b', ec='None', alpha=0.5) circ2 = patches.Circle((12.5, -2.5), radius=2 * disp, fc='b', ec='None', alpha=0.3) circ3 = patches.Circle((12.5, -2.5), radius=3 * disp, fc='b', ec='None', alpha=0.1) ax.add_artist(circ1) ax.add_artist(circ2) ax.add_artist(circ3) ax.text(12.5, -2.5, r'$\sigma$', color='w', fontsize=20, ha='center', va='center') ax.set_xlim(-5, 15) ax.set_ylim(-5, 15) ax.set_xticks([]) ax.set_yticks([]) fig.subplots_adjust(bottom=0.01, top=0.99, left=0.01, right=0.99) fig.show()
Q_T_est = asinh(-v_0 / u_0) # Initial estimate for Newton's method Q_T = newton(y_s, Q_T_est, y_s_p) T = t_s(Q_T) R = x_s(Q_T) H = y_s(0.0) t_vec = np.vectorize(t_s) x_vec = np.vectorize(x_s) y_vec = np.vectorize(y_s) u_vec = np.vectorize(u_s) v_vec = np.vectorize(v_s) N = 300 psi_T = degrees(atan(sinh(Q_T))) Q = np.arcsinh(np.tan(np.radians(np.linspace(degrees(psi), psi_T, N)))) t = t_vec(Q) x = x_vec(Q) y = y_vec(Q) u = u_vec(Q) v = v_vec(Q) def get_intervals(): t_flight = t[-1] intervals = [] t_init = 0 while t_init < t_flight: intervals.append(t_init) t_init += T / N return intervals
def nlmap(X): return np.arcsinh(X / (3. * sigma1))
def mercator(x): return np.arcsinh(np.tan(x * np.pi / 180.)) * 180. / np.pi
def calc_phi_self(self, intf: Signal): fenzi = arcsinh(pi * pi / 2 * abs(self.beta2) * ((2 * self.alpha_lin)**(-1)) * intf.baud_rate**2) fenmu = 2 * pi * abs(self.beta2) * (2 * self.alpha_lin)**(-1) return fenzi / fenmu
def asinh_warp(x, vmin, vmax, bias, contrast): x = norm(x, vmin, vmax) x = np.divide(np.arcsinh(np.multiply(x, 10, out=x), out=x), 3, out=x) x = cscale(x, bias, contrast) return x
def StateEqn(self,t,X,U,N,dt=1): # StateEqn Compute the new states of the battery model # # XNew = StateEqn(self,t,X,U,N,dt) computes the new states of the # battery model given the self strcucture, the current time, the # current states, inputs, process noise, and the sampling time. # Extract states Tb = X[0,:] Vo = X[1,:] Vsn = X[2,:] Vsp = X[3,:] qnB = X[4,:] qnS = X[5,:] qpB = X[6,:] qpS = X[7,:] # Extract inputs P = U[:] # Constraints Tbdot = 0 CnBulk = qnB/ self.VolB CnSurface = qnS/ self.VolS CpSurface = qpS/ self.VolS xnS = min(max(qnS/ self.qSMax,0.01),0.99) Ven5 = self.An5* ((2* xnS - 1)** (5 + 1) - (2* xnS* 5* (1 - xnS))/ (2* xnS - 1)** ( 1 - 5))/ self.F xpS = min(max(qpS/ self.qSMax,0.01),0.99) Vep3 = self.Ap3* ((2* xpS - 1)** (3 + 1) - (2* xpS* 3* (1 - xpS))/ (2* xpS - 1)** ( 1 - 3))/ self.F Vep12 = self.Ap12* ((2* xpS - 1)** (12 + 1) - (2* xpS* 12* (1 - xpS))/ (2* xpS - 1)** ( 1 - 12))/ self.F Vep4 = self.Ap4* ((2* xpS - 1)** (4 + 1) - (2* xpS* 4* (1 - xpS))/ (2* xpS - 1)** ( 1 - 4))/ self.F Vep11 = self.Ap11* ((2* xpS - 1)** (11 + 1) - (2* xpS* 11* (1 - xpS))/ (2* xpS - 1)** ( 1 - 11))/ self.F Vep2 = self.Ap2* ((2* xpS - 1)** (2 + 1) - (2* xpS* 2* (1 - xpS))/ (2* xpS - 1)** ( 1 - 2))/ self.F Vep7 = self.Ap7* ((2* xpS - 1)** (7 + 1) - (2* xpS* 7* (1 - xpS))/ (2* xpS - 1)** ( 1 - 7))/ self.F CpBulk = qpB/ self.VolB Vep8 = self.Ap8* ((2* xpS - 1)** (8 + 1) - (2* xpS* 8* (1 - xpS))/ (2* xpS - 1)** ( 1 - 8))/ self.F qdotDiffusionBSn = (CnBulk - CnSurface)/ self.tDiffusion qnBdot = - qdotDiffusionBSn Jn0 = self.kn* (1 - xnS)** self.alpha* (xnS)** self.alpha Ven3 = self.An3* ((2* xnS - 1)** (3 + 1) - (2* xnS* 3* (1 - xnS))/ (2* xnS - 1)** ( 1 - 3))/ self.F qdotDiffusionBSp = (CpBulk - CpSurface)/ self.tDiffusion Ven0 = self.An0* ((2* xnS - 1)** (0 + 1))/ self.F Jp0 = self.kp* (1 - xpS)** self.alpha* (xpS)** self.alpha Ven10 = self.An10* ((2* xnS - 1)** (10 + 1) - (2* xnS* 10* (1 - xnS))/ (2* xnS - 1)** ( 1 - 10))/ self.F Ven7 = self.An7* ((2* xnS - 1)** (7 + 1) - (2* xnS* 7* (1 - xnS))/ (2* xnS - 1)** ( 1 - 7))/ self.F Ven2 = self.An2* ((2* xnS - 1)** (2 + 1) - (2* xnS* 2* (1 - xnS))/ (2* xnS - 1)** ( 1 - 2))/ self.F Ven11 = self.An11* ((2* xnS - 1)** (11 + 1) - (2* xnS* 11* (1 - xnS))/ (2* xnS - 1)** ( 1 - 11))/ self.F Ven8 = self.An8* ((2* xnS - 1)** (8 + 1) - (2* xnS* 8* (1 - xnS))/ (2* xnS - 1)** ( 1 - 8))/ self.F Ven12 = self.An12* ((2* xnS - 1)** (12 + 1) - (2* xnS* 12* (1 - xnS))/ (2* xnS - 1)** ( 1 - 12))/ self.F Ven1 = self.An1* ((2* xnS - 1)** (1 + 1) - (2* xnS* 1* (1 - xnS))/ (2* xnS - 1)** ( 1 - 1))/ self.F Ven4 = self.An4* ((2* xnS - 1)** (4 + 1) - (2* xnS* 4* (1 - xnS))/ (2* xnS - 1)** ( 1 - 4))/ self.F Ven6 = self.An6* ((2* xnS - 1)** (6 + 1) - (2* xnS* 6* (1 - xnS))/ (2* xnS - 1)** ( 1 - 6))/ self.F Ven9 = self.An9* ((2* xnS - 1)** (9 + 1) - (2* xnS* 9* (1 - xnS))/ (2* xnS - 1)** ( 1 - 9))/ self.F Vep0 = self.Ap0* ((2* xpS - 1)** (0 + 1))/ self.F Vep5 = self.Ap5* ((2* xpS - 1)** (5 + 1) - (2* xpS* 5* (1 - xpS))/ (2* xpS - 1)** ( 1 - 5))/ self.F Vep6 = self.Ap6* ((2* xpS - 1)** (6 + 1) - (2* xpS* 6* (1 - xpS))/ (2* xpS - 1)** ( 1 - 6))/ self.F Vep1 = self.Ap1* ((2* xpS - 1)** (1 + 1) - (2* xpS* 1* (1 - xpS))/ (2* xpS - 1)** ( 1 - 1))/ self.F Vep10 = self.Ap10* ((2* xpS - 1)** (10 + 1) - (2* xpS* 10* (1 - xpS))/ (2* xpS - 1)** ( 1 - 10))/ self.F Vep9 = self.Ap9* ((2* xpS - 1)** (9 + 1) - (2* xpS* 9* (1 - xpS))/ (2* xpS - 1)** ( 1 - 9))/ self.F qpBdot = - qdotDiffusionBSp Ven = self.U0n + self.R* Tb/ self.F* np.log((1 - xnS)/ xnS) + Ven0 + Ven1 + Ven2 + Ven3 + Ven4 + Ven5 + Ven6 + Ven7 + Ven8 + Ven9 + Ven10 + Ven11 + Ven12 Vep = self.U0p + self.R* Tb/ self.F* np.log((1 - xpS)/ xpS) + Vep0 + Vep1 + Vep2 + Vep3 + Vep4 + Vep5 + Vep6 + Vep7 + Vep8 + Vep9 + Vep10 + Vep11 + Vep12 V = Vep - Ven - Vo - Vsn - Vsp i = P/ V qpSdot = i + qdotDiffusionBSp Jn = i/ self.Sn VoNominal = i* self.Ro Jp = i/ self.Sp qnSdot = qdotDiffusionBSn - i VsnNominal = self.R* Tb/ self.F/ self.alpha* np.arcsinh(Jn/ (2* Jn0)) Vodot = (VoNominal - Vo)/ self.to VspNominal = self.R* Tb/ self.F/ self.alpha* np.arcsinh(Jp/ (2* Jp0)) Vsndot = (VsnNominal - Vsn)/ self.tsn Vspdot = (VspNominal - Vsp)/ self.tsp # Update state XNew = np.zeros(np.shape(X)) XNew[0,:] = Tb + Tbdot*dt XNew[1,:] = Vo + Vodot*dt XNew[2,:] = Vsn + Vsndot*dt XNew[3,:] = Vsp + Vspdot*dt XNew[4,:] = qnB + qnBdot*dt XNew[5,:] = qnS + qnSdot*dt XNew[6,:] = qpB + qpBdot*dt XNew[7,:] = qpS + qpSdot*dt # Add process noise XNew = XNew + dt*N return XNew
def expr_math_ops(ip, port): # Connect to h2o h2o.init(ip, port) sin_cos_tan_atan_sinh_cosh_tanh_asinh_data = [ [random.uniform(-10, 10) for r in range(10)] for c in range(10) ] asin_acos_atanh_data = [[random.uniform(-1, 1) for r in range(10)] for c in range(10)] acosh_data = [[random.uniform(1, 10) for r in range(10)] for c in range(10)] abs_data = [[random.uniform(-100000, 0) for r in range(10)] for c in range(10)] h2o_data1 = h2o.H2OFrame( python_obj=sin_cos_tan_atan_sinh_cosh_tanh_asinh_data) h2o_data2 = h2o.H2OFrame(python_obj=asin_acos_atanh_data) h2o_data3 = h2o.H2OFrame(python_obj=acosh_data) h2o_data4 = h2o.H2OFrame(python_obj=abs_data) np_data1 = np.array(sin_cos_tan_atan_sinh_cosh_tanh_asinh_data) np_data2 = np.array(asin_acos_atanh_data) np_data3 = np.array(acosh_data) np_data4 = np.array(abs_data) row, col = h2o_data1.dim() def check_values(h2o_data, numpy_data): success = True for i in range(10): r = random.randint(0, row - 1) c = random.randint(0, col - 1) h2o_val = h2o.as_list(h2o_data[r, c])[0][0] num_val = numpy_data[r, c] if not abs(h2o_val - num_val) < 1e-06: success = False print "check unsuccessful! h2o computed {0} and numpy computed {1}".format( h2o_val, num_val) return success h2o_data1 = h2o_data1 + 2 h2o_data2 = h2o_data2 / 1.01 h2o_data3 = h2o_data3 * 1.5 h2o_data4 = h2o_data4 - 1.5 np_data1 = np_data1 + 2 np_data2 = np_data2 / 1.01 np_data3 = np_data3 * 1.5 np_data4 = np_data4 - 1.5 assert check_values( h2o.cos(h2o_data1), np.cos(np_data1)), "expected equal cos values between h2o and numpy" assert check_values( h2o.sin(h2o_data1), np.sin(np_data1)), "expected equal sin values between h2o and numpy" assert check_values( h2o.tan(h2o_data1), np.tan(np_data1)), "expected equal tan values between h2o and numpy" assert check_values(h2o.acos(h2o_data2), np.arccos( np_data2)), "expected equal acos values between h2o and numpy" assert check_values(h2o.asin(h2o_data2), np.arcsin( np_data2)), "expected equal asin values between h2o and numpy" assert check_values(h2o.atan(h2o_data1), np.arctan( np_data1)), "expected equal atan values between h2o and numpy" assert check_values( h2o.cosh(h2o_data1), np.cosh(np_data1)), "expected equal cosh values between h2o and numpy" assert check_values( h2o.sinh(h2o_data1), np.sinh(np_data1)), "expected equal sinh values between h2o and numpy" assert check_values( h2o.tanh(h2o_data1), np.tanh(np_data1)), "expected equal tanh values between h2o and numpy" assert check_values(h2o.acosh(h2o_data3), np.arccosh( np_data3)), "expected equal acosh values between h2o and numpy" assert check_values(h2o.asinh(h2o_data1), np.arcsinh( np_data1)), "expected equal asinh values between h2o and numpy" assert check_values(h2o.atanh(h2o_data2), np.arctanh( np_data2)), "expected equal atanh values between h2o and numpy" assert check_values( h2o.cospi(h2o_data2 / math.pi), np.cos(np_data2)), "expected equal cospi values between h2o and numpy" assert check_values( h2o.sinpi(h2o_data2 / math.pi), np.sin(np_data2)), "expected equal sinpi values between h2o and numpy" assert check_values( h2o.tanpi(h2o_data2 / math.pi), np.tan(np_data2)), "expected equal tanpi values between h2o and numpy" assert check_values( h2o.abs(h2o_data4), np.fabs(np_data4)), "expected equal abs values between h2o and numpy" assert check_values( h2o.sign(h2o_data2), np.sign(np_data2)), "expected equal sign values between h2o and numpy" assert check_values( h2o.sqrt(h2o_data3), np.sqrt(np_data3)), "expected equal sqrt values between h2o and numpy" assert check_values(h2o.trunc(h2o_data3), np.trunc( np_data3)), "expected equal trunc values between h2o and numpy" assert check_values( h2o.ceil(h2o_data3), np.ceil(np_data3)), "expected equal ceil values between h2o and numpy" assert check_values(h2o.floor(h2o_data3), np.floor( np_data3)), "expected equal floor values between h2o and numpy" assert check_values( h2o.log(h2o_data3), np.log(np_data3)), "expected equal log values between h2o and numpy" assert check_values(h2o.log10(h2o_data3), np.log10( np_data3)), "expected equal log10 values between h2o and numpy" assert check_values(h2o.log1p(h2o_data3), np.log1p( np_data3)), "expected equal log1p values between h2o and numpy" assert check_values( h2o.log2(h2o_data3), np.log2(np_data3)), "expected equal log2 values between h2o and numpy" assert check_values( h2o.exp(h2o_data3), np.exp(np_data3)), "expected equal exp values between h2o and numpy" assert check_values(h2o.expm1(h2o_data3), np.expm1( np_data3)), "expected equal expm1 values between h2o and numpy" h2o_val = h2o.as_list(h2o.gamma(h2o_data3))[5][5] num_val = math.gamma(h2o.as_list(h2o_data3)[5][5]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal gamma values between h2o and math".format(h2o_val,num_val) h2o_val = h2o.as_list(h2o.lgamma(h2o_data3))[5][5] num_val = math.lgamma(h2o.as_list(h2o_data3)[5][5]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal lgamma values between h2o and math".format(h2o_val,num_val) h2o_val = h2o.as_list(h2o.digamma(h2o_data3))[5][5] num_val = scipy.special.polygamma(0, h2o.as_list(h2o_data3)[5][5]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal digamma values between h2o and math".format(h2o_val,num_val) h2o_val = h2o.as_list(h2o.trigamma(h2o_data3))[5][5] num_val = scipy.special.polygamma(1, h2o.as_list(h2o_data3)[5][5]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal trigamma values between h2o and math".format(h2o_val,num_val)
from pyspark.sql import functions as F, Column from pyspark.sql.types import DoubleType, LongType, BooleanType unary_np_spark_mappings = OrderedDict({ "abs": F.abs, "absolute": F.abs, "arccos": F.acos, "arccosh": F.pandas_udf(lambda s: np.arccosh(s), DoubleType()), "arcsin": F.asin, "arcsinh": F.pandas_udf(lambda s: np.arcsinh(s), DoubleType()), "arctan": F.atan, "arctanh": F.pandas_udf(lambda s: np.arctanh(s), DoubleType()), "bitwise_not": F.bitwiseNOT, "cbrt": F.cbrt, "ceil": F.ceil, # It requires complex type which Koalas does not support yet "conj": lambda _: NotImplemented, "conjugate": lambda _: NotImplemented, # It requires complex type
def linearize(x, xmin=None, xmax=None): if np.isscalar(x): return x else: if xmin is None: xmin = np.nanmin(x) if xmax is None: xmax = np.nanmax(x) return ((x - xmin) / (xmax - xmin)) v2scale = lambda x: (np.sinh((linearize(x) - 0.1) * 4.) / np.sinh(4) + 1) / 2.1 v2scale = lambda x: np.log10(1000 * linearize(x) + 1) / np.log10(1000) v2scale = lambda x: np.arcsinh(linearize(x) * 10) / np.arcsinh( 10) #lambda x: x # was np.log10 def logscale(array, logexp=3.0, **kwargs): linarr = linearize(arr, **kwargs) return np.log10(linarr * 10**logexp + 1) if not 'v2scaled' in locals(): if test: myslice = slice(None, None, 4), slice(None, None, 4) myslice = slice(2048, 3072), slice( 0, 1024) #slice(3072,4096)#1024,2048)#2048,3072) rgb = np.ones([1024, 1024, 4]) rgb[:, :, 0] = (np.log10(
def vec_math_ops(ip,port): sin_cos_tan_atan_sinh_cosh_tanh_asinh_data = [[random.uniform(-10,10) for r in range(10)] for c in range(10)] asin_acos_atanh_data = [[random.uniform(-1,1) for r in range(10)] for c in range(10)] acosh_data = [[random.uniform(1,10) for r in range(10)] for c in range(10)] abs_data = [[random.uniform(-100000,0) for r in range(10)] for c in range(10)] zero_one_data = [random.randint(0,1) for c in range(10)] zero_one_data = [zero_one_data, zero_one_data] h2o_data1 = h2o.H2OFrame(python_obj=sin_cos_tan_atan_sinh_cosh_tanh_asinh_data) h2o_data2 = h2o.H2OFrame(python_obj=asin_acos_atanh_data) h2o_data3 = h2o.H2OFrame(python_obj=acosh_data) h2o_data4 = h2o.H2OFrame(python_obj=abs_data) h2o_data5 = h2o.H2OFrame(python_obj=zero_one_data) np_data1 = np.array(sin_cos_tan_atan_sinh_cosh_tanh_asinh_data) np_data2 = np.array(asin_acos_atanh_data) np_data3 = np.array(acosh_data) np_data4 = np.array(abs_data) np_data5 = np.array(zero_one_data) row, col = h2o_data1.dim c = random.randint(0,col-1) for d in range(1,6): h2o_signif = h2o_data5[c].signif(digits=d) h2o_round = h2o_data5[c].round(digits=d+4) s = h2o_signif[0] r = h2o_round[0] assert s == r, "Expected these to be equal, but signif: {0}, round: {1}".format(s, r) h2o_transposed = h2o_data1[c].transpose() x, y = h2o_transposed.dim assert x == 1 and y == 10, "Expected 1 row and 10 columns, but got {0} rows and {1} columns".format(x,y) tests.np_comparison_check(h2o_data1[:,c].cos(), np.cos(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data1[:,c].sin(), np.sin(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data1[:,c].tan(), np.tan(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data2[:,c].acos(), np.arccos(np_data2[:,c]), 10) tests.np_comparison_check(h2o_data2[:,c].asin(), np.arcsin(np_data2[:,c]), 10) tests.np_comparison_check(h2o_data1[:,c].atan(), np.arctan(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data1[:,c].cosh(), np.cosh(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data1[c].sinh(), np.sinh(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data1[c].tanh(), np.tanh(np_data1[:,c]), 10) tests.np_comparison_check(h2o_data3[c].acosh(), np.arccosh(np_data3[:,c]), 10) tests.np_comparison_check(h2o_data1[c].asinh(), np.arcsinh(np_data1[:,c]), 10) h2o_val = h2o_data3[c].gamma()[5,:] num_val = math.gamma(h2o_data3[5,c]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal gamma values between h2o and" \ "math".format(h2o_val,num_val) h2o_val = h2o_data3[c].lgamma()[5,:] num_val = math.lgamma(h2o_data3[5,c]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal lgamma values between h2o and " \ "math".format(h2o_val,num_val) h2o_val = h2o_data3[c].digamma()[5,:]._scalar() num_val = scipy.special.polygamma(0,h2o_data3[5,c]) assert abs(h2o_val - num_val) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal digamma values between h2o and " \ "math".format(h2o_val,num_val) h2o_val = h2o_data3[c].trigamma()[5,:] num_val = scipy.special.polygamma(1,h2o_data3[5,c]) assert abs(h2o_val - float(num_val)) < max(abs(h2o_val), abs(num_val)) * 1e-6, \ "check unsuccessful! h2o computed {0} and math computed {1}. expected equal trigamma values between h2o and " \ "math".format(h2o_val,num_val)