Esempio n. 1
0
    def __init__(self,filename,path='',**kwargs):
        store = pd.HDFStore(filename,'r')
        fns = store[path+'/fns']
        if '{}/samples'.format(path) in store:
            samples = store[path+'/samples']
            self.samples = np.array(samples)
        minval = fns['vals'].iloc[0]
        maxval = fns['vals'].iloc[-1]
        pdf = interpolate(fns['vals'],fns['pdf'],s=0,k=1)
        
        #check to see if tabulated CDF is monotonically increasing
        d_cdf = fns['cdf'][1:] - fns['cdf'][:-1]
        if np.any(d_cdf < 0):
            logging.warning('tabulated CDF in {} is not strictly increasing. Recalculating CDF from PDF'.format(filename))
            cdf = None  #in this case, just recalc cdf from pdf
        else:
            cdf = interpolate(fns['vals'],fns['cdf'],s=0,k=1)
        Distribution.__init__(self,pdf,cdf,minval=minval,maxval=maxval,
                              **kwargs)

        store = pd.HDFStore(filename,'r')
        try:
            keywords = store.get_storer('{}/fns'.format(path)).attrs.keywords
            for kw,val in keywords.iteritems():
                setattr(self,kw,val)
        except AttributeError:
            logging.warning('saved distribution {} does not have keywords or disttype saved; perhaps this distribution was written with an older version.'.format(filename))
        store.close()
Esempio n. 2
0
    def __init__(self,samples,bins=10,equibin=True,smooth=0,order=1,**kwargs):
        self.samples = samples
        
        if type(bins)==type(10) and equibin:
            N = len(samples)//bins
            sortsamples = np.sort(samples)
            bins = sortsamples[0::N]
            if bins[-1] != sortsamples[-1]:
                bins = np.concatenate([bins,np.array([sortsamples[-1]])])

        hist,bins = np.histogram(samples,bins=bins,density=True)
        self.bins = bins
        bins = (bins[1:] + bins[:-1])/2.
        pdf_initial = interpolate(bins,hist,s=smooth,k=order)
        def pdf(x):
            x = np.atleast_1d(x)
            y = pdf_initial(x)
            w = np.where((x < self.bins[0]) | (x > self.bins[-1]))
            y[w] = 0
            return y
        cdf = interpolate(bins,hist.cumsum()/hist.cumsum().max(),s=smooth,
                          k=order)

        if 'maxval' not in kwargs:
            kwargs['maxval'] = samples.max()
        if 'minval' not in kwargs:
            kwargs['minval'] = samples.min()

        keywords = {'bins':bins,'smooth':smooth,'order':order}

        Distribution.__init__(self,pdf,cdf,keywords=keywords,**kwargs)
def polar_to_cartesian(data, rs, thetas, order = 3):
    # Source: http://stackoverflow.com/questions/2164570/reprojecting-polar-to-cartesian-grid
    # Set up xy-grid
    max_r = rs[-1]
    resolution = 2 * len(rad)

    xs = np.linspace(-max_r, max_r, resolution)
    ys = np.linspace(-max_r, max_r, resolution)

    xs_grid, ys_grid = np.meshgrid(xs, ys)

    # Interpolate rt-grid

    interpolated_rs = interpolate(rs, np.arange(len(rs)), bounds_error = False)
    interpolated_thetas = interpolate(thetas, np.arange(len(thetas)), bounds_error = False)

    # Match up xy-grid with rt-grid

    new_rs = np.sqrt(np.power(xs_grid, 2) + np.power(ys_grid, 2))
    new_thetas = np.arctan2(ys_grid, xs_grid)

    # Convert from [-pi, pi] to [0, 2 * pi]
    new_thetas[new_thetas < 0] = new_thetas[new_thetas < 0] + 2 * np.pi

    new_interpolated_rs = interpolated_rs(new_rs.ravel())
    new_interpolated_thetas = interpolated_thetas(new_thetas.ravel())

    # Fix Bounds (outside of polar image, but inside cartesian image)
    new_interpolated_rs[new_rs.ravel() > max(rs)] = len(rs) - 1
    new_interpolated_rs[new_rs.ravel() < min(rs)] = 0

    cart_data = map_coordinates(data, np.array([new_interpolated_rs, new_interpolated_thetas]), order = order).reshape(new_rs.shape)

    return xs_grid, ys_grid, cart_data
Esempio n. 4
0
 def __init__(self,filename,path='',disttype=None,**kwargs):
     """if disttype is 'hist' or 'kde' then samples are required
     """
     fns = pd.read_hdf(filename,path+'/fns')
     self.disttype = disttype
     if disttype in ['hist','kde']:
         samples = pd.read_hdf(filename,path+'/samples')
         self.samples = np.array(samples)
     minval = fns['vals'].iloc[0]
     maxval = fns['vals'].iloc[-1]
     pdf = interpolate(fns['vals'],fns['pdf'],s=0)
     cdf = interpolate(fns['vals'],fns['cdf'],s=0)
     Distribution.__init__(self,pdf,cdf,minval=minval,maxval=maxval,
                           **kwargs)
Esempio n. 5
0
    def __init__(self,samples,bins=10,smooth=0,**kwargs):
        self.samples = samples
        hist,bins = np.histogram(samples,bins=bins,normed=True)
        self.bins = bins
        self.hist = hist #debug
        bins = (bins[1:] + bins[:-1])/2.
        pdf = interpolate(bins,hist,s=smooth)
        cdf = interpolate(bins,hist.cumsum()/hist.cumsum().max(),s=smooth)

        if 'maxval' not in kwargs:
            kwargs['maxval'] = samples.max()
        if 'minval' not in kwargs:
            kwargs['minval'] = samples.min()

        Distribution.__init__(self,pdf,cdf,**kwargs)
Esempio n. 6
0
    def __init__(self,pdf,cdf=None,name='',minval=-np.inf,maxval=np.inf,norm=None,
                 no_cdf=False,cdf_pts=100):
        self.name = name
        self.pdf = pdf
        self.cdf = cdf
        self.minval = minval
        self.maxval = maxval

        if not hasattr(self,'Ndists'):
            self.Ndists = 1

        if norm is None:
            self.norm = quad(pdf,minval,maxval,full_output=1)[0]
        else:
            self.norm = norm

        if cdf is None and not no_cdf and minval != -np.inf and maxval != np.inf:
            pts = np.linspace(minval,maxval,cdf_pts)
            pdfgrid = self(pts)
            cdfgrid = pdfgrid.cumsum()/pdfgrid.cumsum().max()
            cdf_fn = interpolate(pts,cdfgrid,s=0)
            def cdf(x):
                x = np.atleast_1d(x)
                y = np.atleast_1d(cdf_fn(x))
                y[np.where(x < self.minval)] = 0
                y[np.where(x > self.maxval)] = 1
                return y
            self.cdf = cdf
Esempio n. 7
0
def load_distribution(filename,path=''):
    fns = pd.read_hdf(filename,path)
    store = pd.HDFStore(filename)
    if '{}/samples'.format(path) in store:
        samples = pd.read_hdf(filename,path+'/samples')
        samples = np.array(samples)
    minval = fns['vals'].iloc[0]
    maxval = fns['vals'].iloc[-1]
    pdf = interpolate(fns['vals'],fns['pdf'],s=0)
    cdf = interpolate(fns['vals'],fns['cdf'],s=0)

    attrs = store.get_storer('{}/fns'.format(path)).attrs
    keywords = attrs.keywords
    t = attrs.disttype
    store.close()
    return t.__init__()
Esempio n. 8
0
def iterate():
    global solution, G3, iteration
    iteration+=1
    psi=interpolate([0.]+psi_range, [G3]+solution.tolist())
    new_solution=[]
    for i,p in enumerate(psi_range):
        new_solution.append(integratus(lambda k:K(k,p)*psi(k),0.,L-dx/2.)[0]+b[i])
    solution=np.array(new_solution)
    G3=G3_proper()
Esempio n. 9
0
 def _directivity(self, theta, phi):
     """
     Custom directivity.
     
     Interpolate the directivity given longitude and latitude vectors.
     """
     f = interpolate(self.theta, self.phi, self.r)
     
     return f(theta, phi)
Esempio n. 10
0
def rescale(factor):
    global solution, L
    psi=interpolate([0.]+psi_range, [G3]+solution.tolist())
    old_L=L
    L=L*factor
    define_ranges()
    new_solution=[]
    for p in psi_range:
        new_solution.append(integratus(lambda k:K(k,p)*psi(k),0.,old_L-dx/2.)[0]+right_side(p))
    solution=np.array(new_solution)
Esempio n. 11
0
    def __init__(self,samples,bins=10,smooth=0,order=1,**kwargs):
        self.samples = samples
        hist,bins = np.histogram(samples,bins=bins,density=True)
        self.bins = bins
        bins = (bins[1:] + bins[:-1])/2.
        pdf_initial = interpolate(bins,hist,s=smooth,k=order)
        def pdf(x):
            x = np.atleast_1d(x)
            y = pdf_initial(x)
            w = np.where((x < bins[0]) | (x > bins[-1]))
            y[w] = 0
            return y
        cdf = interpolate(bins,hist.cumsum()/hist.cumsum().max(),s=smooth,
                          k=order)

        if 'maxval' not in kwargs:
            kwargs['maxval'] = samples.max()
        if 'minval' not in kwargs:
            kwargs['minval'] = samples.min()

        Distribution.__init__(self,pdf,cdf,**kwargs)
Esempio n. 12
0
def cum_draw(df=exo,color='k',mindate=1995,maxdate=2015,ax=None,norm=False,fill=True, kois=False,
             alpha=0.2,interp=False,kepler=False,kepsmall=False,label=None,xylabel=(0.1,0.8),
            zorder=0):
    if ax is None:
        fig, ax = plt.subplots(1,1)
    else:
        fig = ax.figure

    dates = np.sort(df.DATE)
    ds = np.unique(dates).astype(float)
    ns = np.zeros(len(ds))
    for i,d in enumerate(ds):
        ns[i] = (dates<=d).sum()
    if norm:
        ns /= ns.max()
        ns *= norm
    
    if interp:
        dgrid = np.arange(mindate,maxdate,0.1)
        fn = interpolate(ds,ns,s=0)
        y1 = fn(dgrid)
        y2 = -y1
    else:
        dgrid = ds
        y1 = ns
        y2 = -ns
    #ax.plot(dgrid,y1,color=color)
    #ax.plot(dgrid,y2,color=color)
    ax.fill_between(dgrid,y1,y2,alpha=alpha,color=color, zorder=zorder)
    ax.set_xlim(xmin=mindate,xmax=maxdate)
    ax.set_yticks([])
    
    date_ticks = np.arange(mindate,maxdate+1,2)
    
    #for d in date_ticks:
    #    ax.axvline(d, ls=':', color='k', alpha=0.2)
    ax.set_xticks(date_ticks)
    ax.tick_params(labelsize=16)
    
    ax.spines["top"].set_visible(False)    
    ax.spines["bottom"].set_visible(False)    
    ax.spines["right"].set_visible(False)    
    ax.spines["left"].set_visible(False)  
    
    ax.get_xaxis().tick_bottom()    
    ax.xaxis.set_tick_params(width=3, length=10)
    
    if label is not None:
        label = '%s (%i)' % (label,ns.max())
        pl.annotate(label,xy=xylabel,xycoords='axes fraction',fontsize=18,color=color)

    return fig
Esempio n. 13
0
    def readens(self, fname):

        ens = np.loadtxt(fname).T
        self.qv = ens[0]

        #Linear
        self.xi00lin = ens[1]
        xi0lin = ens[2]
        xi2lin = ens[3]
        self.Xlin = 2 / 3. * (self.xi00lin - xi0lin - xi2lin)
        ylinv = 2 * xi2lin

        #Since we divide by ylin, check for zeros
        mask = (ylinv == 0)
        ylinv[mask] = interpolate(self.qv[~mask], ylinv[~mask])(self.qv[mask])
        self.Ylin = ylinv

        #Useful variables here
        self.XYlin = (self.Xlin + self.Ylin)
        self.sigma = self.XYlin[-1]
        self.yq = (1 * self.Ylin / self.qv)

        #Loop
        self.xi00loop = ens[4] + ens[5]
        xi0loop = ens[6] + ens[7]
        xi2loop = ens[8] + ens[9]
        self.Xloop = 2 / 3. * (self.xi00loop - xi0loop - xi2loop)
        self.Yloop = 2 * xi2loop
        self.XYloop = (self.Xloop + self.Yloop)
        self.sigmaloop = self.XYloop[-1]

        #Loop2
        self.xi1loop = ens[10]
        self.xi3loop = ens[11]

        self.corr = ens[12]
        self.zeta = ens[13]
        self.chi = ens[14]

        self.u10 = ens[15]
        self.u30 = ens[16]
        self.u11 = ens[17]
        self.u20 = ens[18]

        self.v10 = ens[19]
        self.v12 = ens[20]
        self.x10 = ens[21]
        self.y10 = ens[22]
        self.x20 = ens[23]
        self.y20 = ens[24]
Esempio n. 14
0
def generate_rand_from_spline(pdf, x_grid, xmin, xmax):
    pdf_initial = interpolate(x_grid, pdf, s=0, k=1)
    xscan = np.linspace(xmin, xmax, 1000)
    pdfstep = pdf_initial(xscan)
    ymax = max(pdfstep)
    i = 0
    vals = []
    while (i < 1000):
        x = np.random.uniform(xmin, xmax)
        y = np.random.uniform(0, ymax)
        yp = pdf_initial(x)
        if (y < yp):
            i += 1
            vals.append(x)
            #print(i,x,y,y_max,p_accept)
    return vals
Esempio n. 15
0
    def __init__(self,pdf,cdf=None,name='',minval=-np.inf,maxval=np.inf,norm=None,
                 no_cdf=False,prior=None,cdf_pts=500):
        self.name = name

        def newpdf(x):
            if prior is None:
                return pdf(x)
            else:
                return prior(x)*pdf(x)

        self.lhood = pdf
            
        self.pdf = newpdf
        self.cdf = cdf #won't be right if prior given
        self.minval = minval
        self.maxval = maxval

        self.prior = prior

        if not hasattr(self,'Ndists'):
            self.Ndists = 1

        if norm is None:
            self.norm = quad(self.pdf,minval,maxval,full_output=1)[0]
        else:
            self.norm = norm

        if (cdf is None and not no_cdf and minval != -np.inf and maxval != np.inf) or prior is not None:
            pts = np.linspace(minval,maxval,cdf_pts)
            pdfgrid = self(pts)
            #fix special case: last value is infinity (i.e. from isotropic prior)
            if np.isinf(pdfgrid[-1]):
                tip_integral = quad(self,pts[-2],pts[-1])[0]
                #print tip_integral
                cdfgrid = pdfgrid[:-1].cumsum()/pdfgrid[:-1].cumsum().max() * (1-tip_integral)
                cdfgrid = np.concatenate((cdfgrid,[1]))
            else:
                cdfgrid = pdfgrid.cumsum()/pdfgrid.cumsum().max()

            cdf_fn = interpolate(pts,cdfgrid,s=0)
            def cdf(x):
                x = np.atleast_1d(x)
                y = np.atleast_1d(cdf_fn(x))
                y[np.where(x < self.minval)] = 0
                y[np.where(x > self.maxval)] = 1
                return y
            self.cdf = cdf
Esempio n. 16
0
def power_spectrum_transfer_function(frequency,
                                     t,
                                     contrast,
                                     fAMs,
                                     kernel,
                                     nperseg,
                                     amp=1,
                                     **cellparams):
    """
    Calculate the transfer function for a given set of AM (amplitude modulation) frequencies and cell model
    
    Parameters
    ----------
    frequency: float
        The stimulus sine frequency
    t: 1-D array
        The time aray
    contrast: float
        The AM strength (can be between 0 and inf)
    fAMs: 1-D array
        The array containing the AM frequencies 
    kernel: 1-D array
        The kernel array for convolution
    nperseg: float
        The power spectrum nperseg variable
    amp: float
        The amplitude of the sinus wave, set to 1 by default.
    *stimulusparams: list
        List of stimulus parameters. 
    **cellparams: dictionary
        Dictionary containing parameters of the cell model
    
    Returns
    -------
    tfAMs: 1-D array
        The array containing the transfer function values for the given array of fAMs.
    """
    pfAMs = np.zeros(len(fAMs))
    for idx, fAM in enumerate(fAMs):
        stimulus = amp * np.sin(2 * np.pi * frequency * t) * (
            1 + contrast * np.sin(2 * np.pi * fAM * t))
        spiketimes = mod.simulate(stimulus, **cellparams)
        f, p, __ = power_spectrum(stimulus, spiketimes, t, kernel, nperseg)
        power_interpolator = interpolate(f, p)
        pfAMs[idx] = power_interpolator(fAM)
    tfAMs = np.sqrt(pfAMs) / contrast  #transfer function value
    return tfAMs
Esempio n. 17
0
	def load(self):
		(magic,curve_count)=self.read(">hh")
		for i in range(0, curve_count):
			self.curves.append(self.loadcurve())
		for c in self.curves:
			x = [v[0] for v in c]
			y = [v[1] for v in c]
			if(len(x) > 2):
				f = interpolate(x, y, kind='cubic')
				sweep = xrange(0,256)
				values = [int(round(v)) for v in f(xrange(0,256))]
				self.precomputed.append(zip(sweep, values))
			else:
				# interpolate(x,y) 
				# but this is useless - why bother
				# usually will just be a 0,0 - 255,255 line
				self.precomputed.append([])
Esempio n. 18
0
    def __init__(self,pdf,cdf=None,name='',minval=-np.inf,maxval=np.inf,norm=None,
                 cdf_pts=500,keywords=None):
        self.name = name            
        self.pdf = pdf
        self.cdf = cdf 
        self.minval = minval
        self.maxval = maxval
        
        if keywords is None:
            self.keywords = {}
        else:
            self.keywords = keywords
        self.keywords['name'] = name
        self.keywords['minval'] = minval
        self.keywords['maxval'] = maxval


        if norm is None:
            self.norm = quad(self.pdf,minval,maxval,full_output=1)[0]
        else:
            self.norm = norm

        if cdf is None and (minval == -np.inf or maxval == np.inf):
            raise ValueError('must provide either explicit cdf function or explicit min/max values')

        else: #tabulate & interpolate CDF.
            pts = np.linspace(minval,maxval,cdf_pts)
            pdfgrid = self(pts)

            cdfgrid = pdfgrid.cumsum()/pdfgrid.cumsum().max()
            cdf_fn = interpolate(pts,cdfgrid,s=0,k=1)
            
            def cdf(x):
                x = np.atleast_1d(x)
                y = np.atleast_1d(cdf_fn(x))
                y[np.where(x < self.minval)] = 0
                y[np.where(x > self.maxval)] = 1
                return y
            self.cdf = cdf
            #define minval_cdf, maxval_cdf 
            zero_mask = cdfgrid==0
            one_mask = cdfgrid==1
            if zero_mask.sum()>0:
                self.minval_cdf = pts[zero_mask][-1] #last 0 value
            if one_mask.sum()>0:
                self.maxval_cdf = pts[one_mask][0] #first 1 value
Esempio n. 19
0
    def __init__(self,R_dist,Prot_dist,vsini_dist,nsamples=1e3,
                 vgrid=None,npts=100,vgrid_pts=1000,vsini_corrected=False,
                 veq_simple=False,
                 N_veq_samples=1e3,alpha=0.23,l0=20,sigl=20,
                 veq_bandwidth=0.1):

        if type(R_dist) == type(''):
            R_dist = dists.Distribution_FromH5(R_dist,'radius')
        elif type(R_dist) in [type([]),type((1,))]:
            if len(R_dist)==2:
                R,dR = R_dist
                R=float(R); dR=float(dR)
                R_dist = dists.Gaussian_Distribution(R,dR,name='radius')
            elif len(R_dist)==3:
                R_dist = dists.fit_doublegauss(float(R_dist[0]),float(R_dist[1]),float(R_dist[2]),name='radius')
        if type(Prot_dist) in [type([]),type((1,))]:
            P,dP = Prot_dist
            P=float(P); dP=float(dP)
            Prot_dist = dists.Gaussian_Distribution(P,dP,name='Prot')
        if type(vsini_dist) in [type([]),type((1,))]:
            vsini,dvsini = vsini_dist
            vsini=float(vsini); dvsini=float(dvsini)
            if not vsini_corrected:
                vsini = float(vsini)/(1 - (alpha/2))
            vsini_dist = dists.Gaussian_Distribution(vsini,dvsini,name='vsini')
        if vsini_dist.minval < 0:
            vsini_dist.minval = 0


        if veq_simple:
            veq_dist = Veq_Distribution_Simple(R,dR,P,dP)
        else:
            veq_dist = Veq_Distribution(R_dist,Prot_dist,N=N_veq_samples,
                                        alpha=alpha,l0=l0,sigl=sigl,
                                        bandwidth=veq_bandwidth)
        self.vsini_dist = vsini_dist
        self.veq_dist = veq_dist


        cs,Ls = cosi_posterior(vsini_dist, veq_dist, vgrid=vgrid,
                             npts=npts, vgrid_pts=vgrid_pts)
        self.samples = sample_posterior(cs,Ls,nsamples=nsamples)
        pdf = interpolate(cs,Ls,s=0)
        
        dists.Distribution.__init__(self,pdf,name='cos(I)',
                                    minval=0,maxval=1)
Esempio n. 20
0
def Equation_Of_State_NS(P):
    POW = 3
    z_i = np.zeros(9 * POW)

    for i in np.arange(POW):
        for j in np.arange(9):
            z_i[i * 9 + j] = (j + 1) * pow(10., i - 3)

    z = np.insert(z_i, 0, 0)

    N = 1  #*mn[SYS]*c[SYS]**2/(np.pi**2*ln[SYS]**3)
    P_NS = N / 8 * ((2 * z**3 / 3 - z) * np.sqrt(1 + z * z) + np.arcsinh(z))
    E_NS = N / 8 * ((2 * z**3 + z) * np.sqrt(1 + z * z) - np.arcsinh(z))

    f = interpolate(P_NS, E_NS)

    return f(P)
Esempio n. 21
0
    def __init__(self, rs, dmags, band, mag=None, name=None):

        # if band=='K' or band=="K'":
        #    band = 'Ks'

        rs = np.atleast_1d(rs)
        dmags = np.atleast_1d(dmags)
        self.rs = rs
        self.dmags = dmags
        self.band = band
        self.mag = mag
        self.contrastfn = interpolate(rs, dmags, s=0)
        self.rmax = rs.max()
        self.rmin = rs.min()
        if name is None:
            self.name = "%s band" % self.band
        else:
            self.name = name
Esempio n. 22
0
    def __init__(self,rs,dmags,band,mag=None,name=None):

        #if band=='K' or band=="K'":
        #    band = 'Ks'

        rs = np.atleast_1d(rs)
        dmags = np.atleast_1d(dmags)
        self.rs = rs
        self.dmags = dmags
        self.band = band
        self.mag = mag
        self.contrastfn = interpolate(rs,dmags,s=0)
        self.rmax = rs.max()
        self.rmin = rs.min()
        if name is None:
            self.name = '%s band' % self.band
        else:
            self.name = name
Esempio n. 23
0
 def __init__(self,vs,dmags,band='g'):
     self.vs = vs
     self.dmags = dmags
     self.band = band
     if np.size(vs) > 1:
         self.contrastfn = interpolate(vs,dmags,s=0)
         self.vmax = vs.max()
         self.vmin = vs.min()
     else: #simple case; one v, one dmag
         def cfn(v):
             v = np.atleast_1d(abs(v))
             dmags = np.zeros(v.shape)
             dmags[v>=self.vs] = self.dmags
             dmags[v<self.vs] = 0
             return dmags
         self.contrastfn = cfn
         self.vmax = self.vs
         self.vmin = self.vs
Esempio n. 24
0
    def __init__(self, rs, dmags, band, mag=None, name=None):
        """band is self-explanatory; 'mag' is mag of the primary in 'band' """
        #if band=='K' or band=="K'":
        #    band = 'Ks'

        rs = np.atleast_1d(rs)
        dmags = np.atleast_1d(dmags)
        self.rs = rs
        self.dmags = dmags
        self.band = band
        self.mag = mag
        self.contrastfn = interpolate(rs, dmags, s=0)
        self.rmax = rs.max()
        self.rmin = rs.min()
        if name is None:
            self.name = '%s band' % self.band
        else:
            self.name = name
Esempio n. 25
0
    def interpolate_diff_model(self,model_int=None):
        if model_int is None:
            model_timeseries = self.normalized_roi0_intensity
        else:
            model_timeseries = model_int

        interpolated_function = interpolate(self.cut_model_time,model_timeseries[self.cut_model_idx])

        interpolated_model  = interpolated_function(self.cut_exp_time)
        interpolated_model  = np.reshape(interpolated_model,np.shape(self.cut_exp_roi0))

        residuals   = np.subtract(interpolated_model,self.cut_exp_roi0)

        if model_int is None:
            self.interpolated_model = np.copy(interpolated_model)
            self.residuals          = np.copy(residuals)
        else:
            return interpolated_model,residuals
Esempio n. 26
0
File: basis.py Progetto: bd-j/dice
def rectify_basis(wave, spectra, wlow=0, whigh=np.inf,
                  exclude=[], outwave=None, filters=None, **extras):
    """Mask a spectral basis using lists of include and exclude ranges
    """
    if filters is not None:
        flist = observate.load_filters(filters)
        sed = observate.getSED(wave, spectra, filterlist=flist)
        return np.array([f.wave_effective for f in flist]), 10**(-0.4 * sed)

    if outwave is not None:
        onedinterp = interpolate(wave, spectra, axis=-1)
        spectra = onedinterp(outwave)
        wave = outwave

    g = (wave >= wlow) & (wave <= whigh)
    for (lo, hi) in exclude:
        g = g & ((wave < lo) | (wave > hi))
    return wave[g], spectra[:, g]
Esempio n. 27
0
    def __init__(self,
                 k,
                 p,
                 Qfile=None,
                 Rfile=None,
                 npool=4,
                 extrapker=True,
                 saveqfile=None):

        self.kp = k
        self.p = p
        self.ipk = interpolate(k, p)
        self.ilpk = self.loginterp(k, p)
        self.renorm = numpy.sqrt(numpy.pi / 2.)
        self.tpi2 = 2 * numpy.pi**2.
        self.kint = numpy.logspace(-5, 6, 1e4)
        self.npool = npool
        self.extrapker = extrapker  #If the kernels should be extrapolated. True recommended

        if Qfile is None:
            self.kq, self.Q1, self.Q2, self.Q3, self.Q5, self.Q8, self.Qs2 = self.calc_Q(
            )
        else:
            print("Reading in Qs from" + Qfile)
            self.kq, self.Q1, self.Q2, self.Q3, self.Q5, self.Q8, self.Qs2 = numpy.loadtxt(
                Qfile)
        self.ilQ1 = self.loginterp(self.kq, self.Q1, rp=-5)
        self.ilQ2 = self.loginterp(self.kq, self.Q2, rp=-5)
        self.ilQ3 = self.loginterp(self.kq, self.Q3, rp=-5)
        self.ilQ5 = self.loginterp(self.kq, self.Q5, rp=-5)
        self.ilQ8 = self.loginterp(self.kq, self.Q8, rp=-5)
        self.ilQs2 = self.loginterp(self.kq, self.Qs2, rp=-5)

        if Rfile is None:
            self.kr, self.R1, self.R2 = self.calc_R()
        else:
            print("Reading in Rs from" + Rfile)
            self.kr, self.R1, self.R2 = numpy.loadtxt(Rfile)

        self.ilR1 = self.loginterp(self.kr, self.R1)
        self.ilR2 = self.loginterp(self.kr, self.R2)

        if saveqfile is not None:
            self.save_qfunc(saveqfile)
Esempio n. 28
0
 def setup(self):
     '''
     Create X_L, Y_L, xi_L, U1_L \& 0lag sigma.
     '''
     self.xi0lag = self.xi0lin0()
     self.qv, xi0v = self.xi0lin()
     xi2v = self.xi2lin()[1]
     self.corlin = self.corr()[1]
     self.Ulin = self.u10lin()[1]
     #
     self.Xlin = 2 / 3. * (self.xi0lag - xi0v - xi2v)
     ylinv = 2 * xi2v
     #Since we divide by ylin, check for zeros
     mask = (ylinv == 0)
     ylinv[mask] = interpolate(self.qv[~mask], ylinv[~mask])(self.qv[mask])
     self.Ylin = ylinv
     self.XYlin = (self.Xlin + self.Ylin)
     self.sigma = self.XYlin[-1]
     self.yq = (1 * self.Ylin / self.qv)
Esempio n. 29
0
    def symmetrize_dihedral(angles, energies, regions):
        sym_profile = np.array([])
        energy_sum = np.inf
        angles_deg = np.degrees(angles)
        spacing = get_periodic_angle(abs(angles_deg[1] - angles_deg[0]))

        for region in regions:
            select = get_periodic_range(angles_deg, region['start'],
                                        region['end'], spacing)
            ang_reg = angles_deg[select]
            energy_reg = energies[select]

            ang_continious = np.copy(ang_reg)
            ang_continious[ang_reg < region['start'] - spacing] += 360
            ip_funct = interpolate(ang_continious,
                                   energy_reg,
                                   fill_value="extrapolate",
                                   kind=2)
            ip_angle = np.arange(
                region['start'],
                make_contin(region['start'], region['end']) + 1)
            ip_energy = ip_funct(ip_angle)

            if ip_energy.sum() < energy_sum:
                energy_sum = ip_energy.sum()
                if not region['direct']:
                    lowest = ip_energy[::-1]
                else:
                    lowest = ip_energy

        for region in regions:
            if region['direct']:
                current = lowest
            else:
                current = lowest[::-1]
            sym_profile = np.concatenate((sym_profile, current[:-1]))

        sym_angle = np.radians(
            np.arange(regions[0]['start'],
                      make_contin(regions[0]['start'], regions[-1]['end'])))
        sym_profile -= sym_profile.min()
        return sym_angle, sym_profile
Esempio n. 30
0
    def _eval_Dplus(self):
        """return un-normalized D(a)
        ...evaluated only once because lazy"""
        M = self.M
        L = self.L
        H0 = self.H0
        logamin = -20.
        Np = 1000
        logx = numpy.linspace(logamin, 0, Np)
        x = numpy.exp(logx)

        def kernel(loga):
            a = numpy.exp(loga)
            return (a * self.Ea(a=a))**-3 * a  # da = a * d loga

        y = self.Ea(a=x) * numpy.array([
            romberg(kernel, logx.min(), loga, vec_func=True) for loga in logx
        ])

        return interpolate(x, y)
Esempio n. 31
0
def _Dgrow(z, M, L = None, H0 = 100):
    """return D(a)/D(1.)"""
    a = 1/(1+z)
    if L is None:
        L = 1-M
    logamin = -20.
    Np = 1000
    logx = numpy.linspace(logamin, 0, Np)
    x = numpy.exp(logx)

    Ea = lambda a: (a ** -1.5 * (M + (1 - M - L) * a + L * a ** 3) ** 0.5)

    def kernel(loga):
        a = numpy.exp(loga)
        return (a * Ea(a = a)) ** -3 * a # da = a * d loga                                                                                            

    y = Ea(a = x) * numpy.array([ romberg(kernel, logx.min(), loga, vec_func=True) for loga in logx])
    
    Da = interpolate(x, y)
    return Da(a)/Da(1.)
Esempio n. 32
0
    def __init__(self, vs, dmags, band="g"):
        self.vs = vs
        self.dmags = dmags
        self.band = band
        if np.size(vs) > 1:
            self.contrastfn = interpolate(vs, dmags, s=0)
            self.vmax = vs.max()
            self.vmin = vs.min()
        else:  # simple case; one v, one dmag

            def cfn(v):
                v = np.atleast_1d(abs(v))
                dmags = np.zeros(v.shape)
                dmags[v >= self.vs] = self.dmags
                dmags[v < self.vs] = 0
                return dmags

            self.contrastfn = cfn
            self.vmax = self.vs
            self.vmin = self.vs
Esempio n. 33
0
    def _plot(self, ax=None):
        if ax is None:
            ax = plt.gca()

        # Consider order of plotted graphs to increase z-order appropriately
        # This ensure that each graph neatly overlays over the last and is not
        # stacked in the same z-layer.
        for i, (x, y, color, fmt, fill, alpha) in enumerate(self._graphs):
            if fmt == 'interpolate':
                x_new = np.linspace(min(x), max(x), RESOLUTION)
                x, y = x_new, interpolate(x, y)(x_new)

            # Fill under x, y values with the same color as the plot. Otherwise
            # plot just the line.
            if fill is True:
                ax.fill_between(x, [0] * len(x),
                                y,
                                interpolate=True,
                                lw=None,
                                edgecolor=None,
                                facecolor=color,
                                antialiased=True,
                                alpha=alpha,
                                zorder=2.7 + i)
            else:
                ax.plot(x, y, color=color, alpha=alpha, zorder=2.7 + i)

        if max(ax.get_ylim()) > 999:
            ax = ticklabels_to_thousands_sep(ax, 'y')

        # If this is proportional data then convert y-axis to percents.
        if self.is_proportional is True:
            ax = ticklabels_to_percent(ax, 'y')

        for k, spine in ax.spines.items():
            spine.set_zorder(2.7 + i + 1)

        ax.yaxis.grid(True, color='0.8', ls='-', zorder=0)
        ax.set_ylim(0, ax.get_ylim()[1])

        return ax
Esempio n. 34
0
def createicdf(pdf=None, mv=None, mfv=None, Mthresh=10**10.5, M0=1e13):
    '''For the galaxies, based on the functions from Reddick et.al above
    Create a cdf for a halo mass and then sample from ssampleicdf
    - pdf (f) should be with the differential of log10(M)
    '''

    Nint = 500  #No.of points to interpolate
    if pdf is None:
        if mfv is None:
            print('need pdf or the values to interpolate over')
            return None
        else:
            imf = interpolate(mv, mfv, k=5)
    else:
        imf = pdf
    ilmf = lambda x: imf(10**(x))
    lmm = np.linspace(np.log10(M0), np.log10(Mthresh), Nint)

    #Calculate the other mass-limit M2 of the catalog by integrating mf and comparing total number of halos
    nbin = abs(
        np.array(
            [romberg(ilmf, lmm[i], lmm[i + 1]) for i in range(lmm.size - 1)]))
    ncum = np.cumsum(nbin)
    ncum /= ncum.max()
    lmm = 0.5 * (lmm[:-1] + lmm[1:])
    if (ncum == 0).sum() > 0:
        zeros = np.where(ncum == 0)[0]
        ncum = ncum[zeros[-1]:]
        lmm = lmm[zeros[-1]:]
    if (ncum == 1).sum() > 0:
        ones = np.where(ncum == 1)[0]
        ncum = ncum[:ones[0] + 1]
        lmm = lmm[:ones[0] + 1]
    #try: icdf = interpolate(ncum, lmm)
    try:
        icdf = interp1d(ncum, lmm)
    except Exception as e:
        print(e)
        print(ncum, lmm)
    return icdf
Esempio n. 35
0
    def setup(self):
        '''
            Create X_L, Y_L, xi_L, U1_L \& 0lag sigma.
            '''
        self.xi0lag = self.xi0lin0()
        self.qv, xi0v = self.xi0lin()
        xi2v = self.xi2lin(
            tilt=-0.5)[1]  # this tilt gives better low q behavior
        self.corlin = self.corr()[1]
        self.Ulin = self.u10lin()[1]
        #
        self.Xlin = 2 / 3. * (self.xi0lag - xi0v - xi2v)
        ylinv = 2 * xi2v
        #Since we divide by ylin, check for zeros
        mask = (ylinv == 0)
        ylinv[mask] = interpolate(self.qv[~mask], ylinv[~mask])(self.qv[mask])
        self.Ylin = ylinv
        self.XYlin = (self.Xlin + self.Ylin)
        self.sigma = self.XYlin[-1]
        self.yq = (1 * self.Ylin / self.qv)

        # calculate shear terms
        xi0lin = self.xi0lin(k_power=2, tilt=1.5)[1]
        xi2lin = self.xi2lin(k_power=2, tilt=0.5)[1]
        xi4lin = self.xi4lin(k_power=2, tilt=0.5)[1]

        xi1lin = self.xi1lin(k_power=1, tilt=0.5)[1]
        xi3lin = self.xi3lin(k_power=1, tilt=0.5)[1]

        J2 = 2. * xi1lin / 15 - 0.2 * xi3lin
        J3 = -0.2 * xi1lin - 0.2 * xi3lin
        J4 = xi3lin

        self.zeta = 2 * (4 * xi0lin**2 / 45. + 8 * xi2lin**2 / 63. +
                         8 * xi4lin**2 / 35)
        self.chi = 4 * xi2lin**2 / 3.
        self.V = 4 * J2 * xi2lin
        self.Xs2 = 4 * J3**2
        self.Ys2 = 6 * J2**2 + 8 * J2 * J3 + 4 * J2 * J4 + 4 * J3**2 + 8 * J3 * J4 + 2 * J4**2
Esempio n. 36
0
    def hmp(self, rbins, rhocritperh):
        """Generating halo mass profiles of each halo

        Parameters
        ----------
        rbins : array of float
            Radius bin edges
        rhocritperh : float
            Critical density
        """

        self.profiles = []
        self.r = rbins

        for i, halo in enumerate(self.halos):
            _write('\rGenerating halo mass profile [%d out of %d]',
                   (i + 1, len(self.halos)))

            sorted_distances = _sorteddistances(halo, self.particles)

            self.profiles.append(_densitycalc(
                sorted_distances, rbins, self.pmass, rhocritperh))
        _write('[done]\n')

        # Stacking profiles
        self.rho_rhocrit = []

        for i in range(len(self.r)):
            self.rho_rhocrit.append(
                sum([d[i] for d in self.profiles]) / len(self.profiles))

        # Finding r200
        reduced_profile = [r - 200.0 for r in self.rho_rhocrit]
        profile_func = interpolate(self.r, reduced_profile)

        r200 = profile_func.roots()[0]

        self.rr200 = [r / r200 for r in self.r]
Esempio n. 37
0
    def hmp(self, rbins, rhocritperh):
        """Generating halo mass profiles of each halo

        Parameters
        ----------
        rbins : array of float
            Radius bin edges
        rhocritperh : float
            Critical density
        """

        self.profiles = []
        self.r = rbins

        for i, halo in enumerate(self.halos):
            _write('\rGenerating halo mass profile [%d out of %d]',
                   (i + 1, len(self.halos)))

            sorted_distances = _sorteddistances(halo, self.particles)

            self.profiles.append(
                _densitycalc(sorted_distances, rbins, self.pmass, rhocritperh))
        _write('[done]\n')

        # Stacking profiles
        self.rho_rhocrit = []

        for i in range(len(self.r)):
            self.rho_rhocrit.append(
                sum([d[i] for d in self.profiles]) / len(self.profiles))

        # Finding r200
        reduced_profile = [r - 200.0 for r in self.rho_rhocrit]
        profile_func = interpolate(self.r, reduced_profile)

        r200 = profile_func.roots()[0]

        self.rr200 = [r / r200 for r in self.r]
Esempio n. 38
0
def stellar_relation():
    x1 = 10**15 * 1.6
    x2 = 10**12 * 4

    y1 = x1 * 1.5 * 10**-3
    y2 = x2 * 3.2 * 10**-2
    line1, dummy = cf(line, numpy.array([log10(x1), log10(x2)]),
                      numpy.array([log10(y1), log10(y2)]))

    y1 = x1 * 3.5 * 10**-4
    y2 = x2 * 2.2 * 10**-2
    line2, dummy = cf(line, numpy.array([log10(x1), log10(x2)]),
                      numpy.array([log10(y1), log10(y2)]))

    #mean line
    m = (line1[0] + line2[0]) * 0.5
    c = (line1[1] + line2[1]) * 0.5

    xval = numpy.linspace(log10(x2), log10(x1))
    xline = line(xval, m, c)
    stellar_sigma = interpolate(
        xline, 0.01 + (line(xval, line1[0], line1[1]) - xline))
    return stellar_sigma, m, c
Esempio n. 39
0
    def setup_dm(self):
        qf = self.qf

        #Linear
        self.xi00lin = qf.xi0lin0()
        self.qv, xi0lin = qf.xi0lin() #qv determined here
        xi2lin = qf.xi2lin()[1]
        self.Xlin = 2/3.*(self.xi00lin - xi0lin - xi2lin)
        ylinv = 2*xi2lin

        #Since we divide by ylin, check for zeros
        mask = (ylinv == 0)
        ylinv[mask] = interpolate(self.qv[~mask], ylinv[~mask])(self.qv[mask])
        self.Ylin = ylinv

        #Useful variables here
        self.XYlin = (self.Xlin + self.Ylin)
        self.sigma = self.XYlin[-1]
        self.yq = (1*self.Ylin/self.qv)

        #Loop
        if self.order == 2:
            self.xi00loop = qf.xi0loop0()
            xi0loop = qf.xi0loop()[1] 
            xi2loop = qf.xi2loop()[1]
            self.Xloop = 2/3.*(self.xi00loop - xi0loop - xi2loop)
            self.Yloop = 2*xi2loop
            self.XYloop = (self.Xloop + self.Yloop)
            self.sigmaloop = self.XYloop[-1]

            #Loop2
            self.xi1loop = qf.xi1loop(tilt = 0.5)[1]
            self.xi3loop = qf.xi3loop(tilt = 0.5)[1]
        else:
            self.Xloop, self.Yloop, self.XYloop, self.xi1loop, self.xi3loop = [np.zeros_like(self.qv) for i in range(5)]
            self.sigmaloop = 0
Esempio n. 40
0
def Halos(bs, nc, seed, nstep, seed_hod, Omega_m, p_alpha, p_logMin, p_logM1, p_logM0, p_sigma_logM):
    '''
    '''
    # setup initial conditions
    Omegacdm = Omega_m-0.049,
    cosmo   = NBlab.cosmology.Planck15.clone(Omega_cdm=Omegacdm, 
                                       h = 0.6711, Omega_b = 0.049)
    power   = NBlab.cosmology.LinearPower(cosmo, 0)
    klin    = np.logspace(-4,2,1000) 
    plin    = power(klin)
    pkfunc  = interpolate(klin, plin)
    
    # run the simulation
    pm      = ParticleMesh(BoxSize=bs, Nmesh=[nc, nc, nc])
    Q       = pm.generate_uniform_particle_grid()
    stages  = numpy.linspace(0.1, 1.0, nstep, endpoint=True)
    solver  = Solver(pm, cosmo, B=2)
    wn      = solver.whitenoise(seed)
    dlin    = solver.linear(wn, pkfunc)
    state   = solver.lpt(dlin, Q, stages[0])
    state   = solver.nbody(state, leapfrog(stages))
    
    # create the catalogue
    cat = ArrayCatalog({'Position' : state.X, 
    	  		'Velocity' : state.V, 
			'Displacement' : state.S,
			'Density' : state.RHO}, 
			BoxSize=pm.BoxSize, 
			Nmesh=pm.Nmesh,
			M0 = Omega_m * 27.75e10 * bs**3 / (nc/2.0)**3)
    cat['KDDensity'] = KDDensity(cat).density

    # run FOF to construct Halo catalog 
    fof = FOF(cat, linking_length=0.2, nmin=12)
    fofcat = fof.to_halos(particle_mass=cat.attrs['M0'], cosmo = cosmo, redshift = 0.0)      
    return fofcat  
Esempio n. 41
0
    def setup(self):
        '''
        Create X_L, Y_L, xi_L, U1_L \& 0lag sigma.
        These will all be dictionaries labelled by 'species', except for the X, Y terms,
        which we'll have to deal with separately...
        
        '''
        self.qv, xi0v = self.xi0lin()

        pairs = ['mm', 'dm', 'sm', 'dd', 'ds', 'ss']
        self.corlins = {}
        self.Ulins = {}
        self.Xlins = {}
        self.Ylins = {}
        self.XYlins = {}
        self.sigmas = {}
        self.yqs = {}

        for pair in pairs:

            # the sign of the spectra of the shift field are hereby fixed....
            if pair == 'sm' or pair == 'ds':
                s = -1
            else:
                s = +1

            q_p = -0.5

            print('Calculating 2-pt functions for species pair ' + pair,
                  'tilt = ' + str(q_p))
            xi0lag = self.xi0lin0(species=pair)
            xi0v = s * self.xi0lin(
                species=pair, tilt=-q_p)[1]  # used to be 1 + q_p which was bad
            xi2v = s * self.xi2lin(species=pair, tilt=-q_p)[1]

            self.Xlins[pair] = 2 / 3. * (xi0lag - xi0v - xi2v)

            # Check Ylin for zeros
            ylinv = 2 * xi2v
            mask = (ylinv == 0)
            ylinv[mask] = interpolate(self.qv[~mask],
                                      ylinv[~mask])(self.qv[mask])
            self.Ylins[pair] = ylinv
            self.yqs[pair] = (1 * self.Ylins[pair] / self.qv)

            self.XYlins[pair] = self.Xlins[pair] + self.Ylins[pair]
            self.sigmas[pair] = self.XYlins[pair][-1]

            self.corlins[pair] = s * self.corr(
                species=pair, tilt=1.5)[1]  # used to be 3 + q_p
            self.Ulins[pair] = s * self.u10lin(
                species=pair, tilt=1.5)[1]  # used to be 2 + qp

        self.XYlin_mm = self.Xlins['mm'] + self.Ylins['mm']
        self.sigma_mm = self.XYlin_mm[-1]
        self.XYlin_dd = self.Xlins['dd'] + self.Ylins['dd']
        self.sigma_dd = self.XYlin_dd[-1]
        self.XYlin_ds = self.Xlins['ds'] + self.Ylins['ds']
        self.sigma_ds = self.XYlin_ds[-1]
        self.XYlin_ss = self.Xlins['ss'] + self.Ylins['ss']
        self.sigma_ss = self.XYlin_ss[-1]

        # calculate shear correlators
        self.zetas = {}
        self.chis = {}
        self.Xs2s = {}
        self.Ys2s = {}
        self.Vs = {}

        xi2lin_mm = self.xi2lin(species='mm', k_power=2)[1]

        for pair in ['mm', 'dm', 'sm']:
            if pair != 'mm':
                tilt = 0
            else:
                tilt = 1.5

            if pair == 'sm' or pair == 'ds':
                s = -1
            else:
                s = +1

            xi0lin = s * self.xi0lin(species=pair, k_power=2, tilt=1.5)[1]
            xi2lin = s * self.xi2lin(species=pair, k_power=2, tilt=0.5)[1]
            xi4lin = s * self.xi4lin(species=pair, k_power=2, tilt=0.5)[1]

            xi1lin = s * self.xi1lin(species=pair, k_power=1, tilt=0.5)[1]
            xi3lin = s * self.xi3lin(species=pair, k_power=1, tilt=0.5)[1]

            J2 = 2. * xi1lin / 15 - 0.2 * xi3lin
            J3 = -0.2 * xi1lin - 0.2 * xi3lin
            J4 = xi3lin

            self.zetas[pair] = 2 * (4 * xi0lin**2 / 45. + 8 * xi2lin**2 / 63. +
                                    8 * xi4lin**2 / 35)
            self.chis[pair] = 4 * xi2lin**2 / 3.
            self.Vs[pair] = 4 * J2 * xi2lin_mm
            self.Xs2s[pair] = 4 * J3**2
            self.Ys2s[
                pair] = 6 * J2**2 + 8 * J2 * J3 + 4 * J2 * J4 + 4 * J3**2 + 8 * J3 * J4 + 2 * J4**2
Esempio n. 42
0
        'resolution': t_delta
    }  #kernel is muhc shorter for power spectrum

    #create kernel
    kernel, kerneltime = helpers.spike_gauss_kernel(**kernelparams)

    #power spectrum parameters:
    nperseg = 2**15

    spiketimes = mod.simulate(stimulus, **cellparams)

    fexample, pexample, meanspkfr = helpers.power_spectrum(
        stimulus, spiketimes, t, kernel, nperseg)

    pdB = helpers.decibel_transformer(pexample)
    power_interpolator_decibel = interpolate(fexample, pdB)

    #stimulus power spectrum
    fexamplestim, pexamplestim = welch(
        np.abs(stimulus - np.mean(stimulus)), nperseg=nperseg,
        fs=1 / t_delta)  #zero peak of power spectrum is part of the stimulus,
    #which stays even when stimulus mean is substracted.
    #take absolute value to get the envelope
    pdBstim = helpers.decibel_transformer(pexamplestim)

    #cell f-I curve
    dataframe = pd.read_csv(savepath + '\\' + datafiles[cell_idx])
    vals = dataframe.to_numpy()
    baselinefs = vals[:, 0][~np.isnan(vals[:, 0])]
    initialfs = vals[:, 1][~np.isnan(vals[:, 1])]
    steadyfs = vals[:, 2][~np.isnan(vals[:, 2])]
Esempio n. 43
0
import numpy as np
from scipy.interpolate import UnivariateSpline as interpolate

import cosmo_functions as cf
#import pmfs_fisher as pf
reload(cf)
from constants import *
from globals import *

#set up kappas:
KAPPA = np.loadtxt(INPUTS_PATH + 'Kcoeffexpanded.txt')
TKAPPA = KAPPA[:, 0]
KAPPAP = KAPPA[:, 1]
KAPPAM = KAPPA[:, 2]

val_kappam = interpolate(TKAPPA, KAPPAM, s=0)
val_kappap = interpolate(TKAPPA, KAPPAP, s=0)

#from Allison & Delgarno 1969:
kappa10_array = np.array([
    2.2e-14, 4.2e-14, 1.8e-13, 5.1e-13, 1.2e-12, 2.3e-12, 7.4e-12, 1.5e-11,
    2.3e-11, 3.0e-11, 4.4e-11, 5.6e-11, 6.6e-11, 7.4e-11, 8.2e-11, 8.9e-11,
    9.5e-11, 1.4e-10, 1.6e-10, 1.8e-10, 2.0e-10, 2.1e-10, 2.2e-10, 2.3e-10,
    2.4e-10, 2.5e-10
])
T_kappa10_array = np.array([
    1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300,
    400, 500, 600, 700, 800, 900, 1000
])
val_kappa10 = interpolate(T_kappa10_array, kappa10_array, s=0)
Esempio n. 44
0
 def _interpolate(self, x, z, **kwds):
     from scipy.interpolate import CloughTocher2DInterpolator as interpolate
     return interpolate(x, z, **kwds)
Esempio n. 45
0
    def _traceback_warp(self):

        # setup initial position & warp parameter values for each region
        X0, Y0 = self.regions_X0, self.regions_Y0
        shape = self.regions_X0.shape
        P0, P1, P2, P3, P4, P5 = [
            np.array([a.p[b] for a in self.frame[0].region]).reshape(shape) 
            for b in xrange(6)]
        
        # setup parameters for smoothing before back-interpolating)
        if self.smoothtraceback:
            sgargs = {'window_length':11, 'polyorder': 3, 'mode':'interp'}
            
            if sgargs['window_length'] > np.min(X0.shape):
                warnings.warn('Too few regions to smooth data during traceback. Skipping.')
                self.smoothtraceback = False

        # for each time point
        for tt in xrange(1, self.num_frames):

            # data for each warp parameter at each region in current frame
            # warp is from previous to current frame
            d0, d1, d2, d3, d4, d5 = [
                np.array([a.p[b] for a in self.frame[tt].region]).reshape(shape)
                for b in range(6)]
            
            # smooth this data before creating interpolant
            if self.smoothtraceback:
                d0, d1, d2, d3, d4, d5 = [
                    sgfilt(sgfilt(a, axis=0, **sgargs), axis=1, **sgargs)
                    for a in (d0, d1, d2, d3, d4, d5)] 
#                gargs = {'sigma': 1., 'mode':'reflect'}
#                d0, d1, d2, d3, d4, d5 = [
#                    sp.ndimage.filters.gaussian_filter(a, **gargs) 
#                    for a in (d0, d1, d2, d3, d4, d5)]

            # scalar field interpolants for each parameter
            verts = (self.regions_Yv, self.regions_Xv)
            iargs = {'bounds_error':False, 'fill_value':None}
            i0, i1, i2, i3, i4, i5 = [interpolate(verts, a, **iargs) 
                for a in (d0, d1, d2, d3, d4, d5)]
            
            for rr in xrange(self.num_regions):
                
                # previous region center
                pt = np.vstack((Y0.flat[rr], X0.flat[rr])).T
                
                # previous parameters
                P = [a.flat[rr] for a in (P0, P1, P2, P3, P4, P5)]
                
                # previous F
                F = getF(P)
                
                # in-between parameters
                ptmp = [a(pt)[0] for a in (i0, i1, i2, i3, i4, i5)]
                
                # in-between F
                ftmp = getF(ptmp)
                
                # new F
                f = np.dot(ftmp, F)
                
                # new parameters
                p = np.array((f[0,0]-1, f[1,0], f[0,1], 
                              f[1,1]-1, ptmp[4]+P4.flat[rr], 
                              ptmp[5]+P5.flat[rr]), dtype=float)
                
                # new region center
                x0, y0 = X0.flat[rr] + p[4], Y0.flat[rr] + p[5]
                
                # overwrite current values as initial values for next time pt 
                X0.flat[rr], Y0.flat[rr] = x0, y0
                P0.flat[rr], P1.flat[rr], P2.flat[rr], P3.flat[rr], \
                    P4.flat[rr], P5.flat[rr] = p
                
                # store warp parameters for current region in current frame
                self.frame[tt].region[rr].set_warped_coordinates(p)
Esempio n. 46
0
 def __init__(self,xs,pdf,smooth=0,**kwargs):
     pdf /= np.trapz(pdf,xs)
     fn = interpolate(xs,pdf,s=smooth)
     keywords = {'smooth':smooth}
     Distribution.__init__(self,fn,minval=xs.min(),maxval=xs.max(),
                           keywords=keywords,**kwargs)
Esempio n. 47
0
 def _interpolate(self, x, z, **kwds):
     from scipy.interpolate import CloughTocher2DInterpolator as interpolate
     return interpolate(x, z, **kwds)
Esempio n. 48
0
 def get_interpolation(self, parameter_name_x, parameter_name_y, **kwargs):
     return interpolate(self.get_value_list(parameter_name_x),
                        self.get_value_list(parameter_name_y),
                        **kwargs)
Esempio n. 49
0
 def sigmaf(self, aa=1.):
     """ returns interpolating function for sigma. syntax to use - sigmaf(a)(M)"""
     d = self.cosmo.Dgrow(a=aa)
     return interpolate(self.masses, d * self.sigma)
Esempio n. 50
0
def icdf_sampling(bs,
                  mf=None,
                  mv=None,
                  mfv=None,
                  match_high=True,
                  hmass=None,
                  M0=None,
                  N0=None,
                  seed=100):
    '''
    Given samples from analytic mass function (dN/dln(M)), find halo masss by matching abundance via
    inverse cdf sampling. 
    bs : boxsize
    mv, mfv : (Semi-optional) analytic hmf sampled at masses 'mv'
    mf : (Semi-optional) Analytic hmf, if mv and mfv are not given
    match_high : if True, Match the highest mass of the catalog to analytic mass.
        if False, match the lowest mass
    hmass : (Semi-Optional) Halo mass catalog, used to calculate highest/lowest mass 
        and number of halos
    M0, N0 : (Semi-optional) If mass catalog not given, M0 and N0 are required to 
        correspond to highest/lowest mass and number if halos
        
    Returns: Abundance matched halo mass catalog
    '''
    Nint = 500  #No.of points to interpolate
    #Create interpolating function for mass_func
    if mf is not None:
        mv = np.logspace(10, 17, Nint)
        mfv = mf(mv)
    elif mv is None:
        print(
            "Need either a function or values sampled from the analytic mass function to match against"
        )
        return None
    #Interpolate
    imf = interpolate(mv, mfv, k=5)
    ilmf = lambda x: imf(np.exp(x))

    #Create array to integrate high or low from the matched mass based on match_high
    if N0 is None:
        N0 = hmass.size
    if match_high:
        if M0 is None:
            if hmass is None:
                print(
                    "Need either a halo mass catalog or a mass to be matched at"
                )
                return 0
            else:
                M0 = hmass.max()
        lmm = np.linspace(np.log(M0), np.log(mv.min()), Nint)
    else:
        if M0 is None:
            M0 = hmass.min()
        lmm = np.linspace(np.log(M0), np.log(mv.max()), Nint)

    #Calculate the other mass-limit M2 of the catalog by integrating mf and comparing total number of halos
    ncum = abs(
        np.array([romberg(ilmf, lmm[0], lmm[i])
                  for i in range(lmm.size)])) * bs**3
    M2 = np.exp(np.interp(N0, ncum, lmm))

    #Create pdf and cdf for N(M) from mf between M0 and M2
    lmm2 = np.linspace(np.log(M0), np.log(M2), Nint)
    nbin = abs(
        np.array([
            romberg(ilmf, lmm2[i], lmm2[i + 1]) for i in range(lmm2.size - 1)
        ])) * bs**3
    nprob = nbin / nbin.sum()
    cdf = np.array([nprob[:i + 1].sum() for i in range(nprob.size)])
    icdf = interpolate(cdf[:], 0.5 * (lmm2[:-1] + lmm2[1:]))

    #Sample random points from uniform distribution and find corresponding mass
    np.random.seed(seed)
    ran = np.random.uniform(0, 1, N0)
    hmatch = np.exp(icdf(ran))
    hmatch.sort()
    return hmatch[::-1]
Esempio n. 51
0
import numpy as np
from scipy.interpolate import UnivariateSpline as interpolate

import cosmo_functions as cf
#import pmfs_fisher as pf
reload(cf)
from constants import *
from globals import *

#set up kappas:
KAPPA = np.loadtxt(INPUTS_PATH + 'Kcoeffexpanded.txt')
TKAPPA = KAPPA[:,0]
KAPPAP = KAPPA[:,1]
KAPPAM = KAPPA[:,2]

val_kappam = interpolate(TKAPPA, KAPPAM, s=0)
val_kappap = interpolate(TKAPPA, KAPPAP, s=0)

#from Allison & Delgarno 1969:
kappa10_array = np.array([2.2e-14,4.2e-14,1.8e-13,5.1e-13,1.2e-12,2.3e-12,7.4e-12,1.5e-11,2.3e-11,3.0e-11,4.4e-11,5.6e-11,6.6e-11,7.4e-11,8.2e-11,8.9e-11,9.5e-11,1.4e-10,1.6e-10,1.8e-10,2.0e-10,2.1e-10,2.2e-10,2.3e-10,2.4e-10,2.5e-10])
T_kappa10_array = np.array([1,2,4,6,8,10,15,20,25,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000])
val_kappa10 = interpolate(T_kappa10_array, kappa10_array, s=0)

Pnp = np.array([1, 0, 0.2609, 0.3078, 0.3259, 0.3353, 0.3410, 0.3448, 0.3476, 0.3496,
       0.3512, 0.3524, 0.3535, 0.3543, 0.3550, 0.3556, 0.3561, 0.3565, 0.3569,
       0.3572, 0.3575, 0.3578, 0.3580, 0.3582, 0.3584, 0.3586, 0.3587, 0.3589, 0.3590])
Pnp_ns = np.array([2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30])

Pnp_tuple = ((2,1.), (3,0), (4,0.2609), (5,0.3078), (6,0.3259), (7,0.3353), (8,0.3410), (9,0.3448),
       (10,0.3476), (11,0.3496), (12,0.3512), (13,0.3524), (14,0.3535), (15,0.3543), (16,0.3550),
       (17,0.3556), (18,0.3561), (19,0.3565), (20,0.3569), (21,0.3572), (22,0.3575), (23,0.3578),
Esempio n. 52
0
                        legends=plot_data["Rmaxfactor"])

# Compute statistics for cover page
reporting = 8

# Efolds
efolds = []
for avals in plot_data["lna"]:
    efolds.append(avals[-1])

# Two point phi
twoptphis = []
for i in range(len(plot_data["lna"])):
    x = plot_data["lna"][i]
    y = plot_data["phi2pt"][i]
    interp = interpolate(x, y)
    twoptphis.append(float(interp(reporting)))

# Two point phidot
twoptphidots = []
for i in range(len(plot_data["lna"])):
    x = plot_data["lna"][i]
    y = plot_data["phi2ptdt"][i]
    interp = interpolate(x, y)
    twoptphidots.append(float(interp(reporting)))

# Two point phigrad
twoptphigrads = []
for i in range(len(plot_data["lna"])):
    x = plot_data["lna"][i]
    y = plot_data["phi2ptgrad"][i]
Esempio n. 53
0
 def _interpolate(self, x, z, **kwds):
     import numpy as np
     from scipy.interpolate import Rbf as interpolate
     return interpolate(*np.vstack((x.T, z)), **kwds)
Esempio n. 54
0
    def render_segment(self, seg, key):
        print("  * Processing vector %d:" % (key + 1))

        # get vector parameters from the GUI
        harm_mode = self.gui.harm_mode_var[key].get()
        harm_count = self.gui.harm_count_val[key]
        midi_note_number = int(self.gui.baseline_freq[key].get())
        read_time = int(self.gui.read_speed[key].get())
        delay_time = int(self.gui.delay_time[key].get())
        base_freq = self.midi_notes[midi_note_number]

        # a list of frequencies for all harmonics in the vector
        harmonic_freqs = []
        harmonic_freqs.append(base_freq)

        # create lists with random harmonics and frequencies in Hz
        rnd_list = random.sample(range(2,256),128)
        rnd_list_hz = random.sample(range(int(base_freq) + 10,SAMPLE_RATE // (ANTIALIASING + 1)),128)

        # calculate frequencies of harmonics according to the selected formula ("harmonics mode" parameter)
        for h in range(1,harm_count):
            if harm_mode == 'All':
                freq = base_freq * (h + 1)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Even':
                freq = base_freq * (h * 2)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Odd':
                freq = base_freq * self.odds[h]
                harmonic_freqs.append(freq)
            elif harm_mode == 'Skip 2':
                freq = base_freq * (self.odds[h] + h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Skip 3':
                freq = base_freq * (self.odds[h] + h + h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Skip 4':
                freq = base_freq * (self.odds[h] + h + h + h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Primes':
                freq = base_freq * self.primes[h]
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub All':
                freq = base_freq / (h + 1)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub Even':
                freq = base_freq / (h * 2)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub Odd':
                freq = base_freq / self.odds[h]
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub Skip 2':
                freq = base_freq / (self.odds[h] + h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub Skip 3':
                freq = base_freq / (self.odds[h] + h + h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub Skip 4':
                freq = base_freq / (self.odds[h] + h + h + h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Sub Primes':
                freq = base_freq / self.primes[h]
                harmonic_freqs.append(freq)
            elif harm_mode == 'Inc 100 Hz':
                freq = base_freq + (100 * h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Inc 250 Hz':
                freq = base_freq + (250 * h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Inc 500 Hz':
                freq = base_freq + (500 * h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Inc 1000 Hz':
                freq = base_freq + (1000 * h)
                harmonic_freqs.append(freq)
            elif harm_mode == 'Random':
                freq = base_freq * rnd_list[h]
                harmonic_freqs.append(freq)
            elif harm_mode == 'Random Hz':
                freq = rnd_list_hz[h]
                harmonic_freqs.append(freq)

        # list with all the harmonics to be generated
        harmonics = list()

        # generate sine waves
        for j, harm in enumerate(harmonic_freqs):
            # if antialiasing enabled, check if harmonic goes beyond Nyquist and stop processing
            if harm > SAMPLE_RATE / 2 and ANTIALIASING == 1:
                break
            print("    * Processing harmonic " + str(j+1) + ', ' + str(harm) + ' Hz')
            sine = self.generate_sine(freq=harm, length=read_time / 1000, amp=MAX_AMPLITUDE, rate=SAMPLE_RATE)
            # get pixel luminosity data
            luminosity_values = []
            x, y = seg[j].shape
            for i in range(x):
                R, G, B = int(seg[j][i,0]), int(seg[j][i,1]), int(seg[j][i,2])
                luminosity = sqrt(0.299 * R * R + 0.587 * G * G + 0.114 * B * B) / 255
                luminosity_values.append(luminosity)

            # interpolate the values with the amplitude buffer
            luminosity_x = linspace(0,1,len(luminosity_values))
            luminosity_values = array(luminosity_values)
            spl = interpolate(luminosity_x, luminosity_values, k=1, s=0)
            amplitude_buff_space = linspace(0,1,(read_time / 1000) * SAMPLE_RATE)

            # add the calculated harmonic to the harmonics list
            harmonics.append(sine * spl(amplitude_buff_space))

        # sum all harmonics in the vector
        waveform = sum(harmonics)

        # generate empty buffer for delay time
        dly = self.generate_sine(1,delay_time / 1000, amp=0, rate=SAMPLE_RATE)

        # merge the delay time with the generated waveform
        rendered = append(dly,waveform)

        return rendered
Esempio n. 55
0
 def _interpolate(self, x, z, **kwds):
     from scipy.interpolate import NearestNDInterpolator as interpolate
     return interpolate(x, z, **kwds)
Esempio n. 56
0
 def _interpolate(self, x, z, **kwds):
     import numpy as np
     from scipy.interpolate import Rbf as interpolate
     return interpolate(*np.vstack((x.T, z)), **kwds)
Esempio n. 57
0
from cosmo4d import lab
from cosmo4d.lab import report, dg, objectives, mapnoise, std
from abopt.algs.lbfgs import scalar as scalar_diag

from nbodykit.cosmology import Planck15, EHPower, Cosmology
from nbodykit.algorithms.fof import FOF
from nbodykit.lab import KDDensity, BigFileMesh, BigFileCatalog, ArrayCatalog, FieldMesh, FFTPower
import sys, os, json, yaml
from solve import solve
from getbiasparams import getbias, eval_bfit
sys.path.append('../')
sys.path.append('../utils/')
import HImodels

klin, plin = numpy.loadtxt('../../data/pklin_1.0000.txt', unpack=True)
ipk = interpolate(klin, plin)
#cosmo = Planck15.clone(Omega_cdm = 0.2685, h = 0.6711, Omega_b = 0.049)
cosmodef = {'omegam': 0.309167, 'h': 0.677, 'omegab': 0.048}
cosmo = Cosmology.from_dict(cosmodef)

#########################################

#Set parameters here
##
cfname = sys.argv[1]
upsample = bool(float(sys.argv[2]))
with open(cfname, 'r') as ymlfile:
    cfg = yaml.load(ymlfile)
for i in cfg['basep'].keys():
    locals()[i] = cfg['basep'][i]
zz = 1 / aa - 1
Esempio n. 58
0
 def _interpolate(self, x, z, **kwds):
     from scipy.interpolate import NearestNDInterpolator as interpolate
     return interpolate(x, z, **kwds)
Esempio n. 59
0
def G3_proper():
    psi=interpolate([0.]+psi_range, [G3]+solution.tolist())
    return integratus(lambda k:K(k,0.)*psi(k),0.,L)[0]+right_side(0.)
Esempio n. 60
0
 def _create_image_interpolants(self, imsize):
     vert_y, vert_x = (np.arange(limit).astype(float) for limit in imsize)
     self.interp = interpolate((vert_y, vert_x), self.data,
                               bounds_error=False, fill_value=None) #(y,x)!