Exemple #1
0
def test_quasi_newton_broyden_bad():
    op = p.OptimizationProblem(c.chebyquad)
    guess=linspace(0,1,4)
    cn  = p.QuasiNewtonBroydenBad(op)
    assert near(sol4,(cn.optimize(guess)))
    guess=linspace(0,1,8)
    assert near(sol8,(cn.optimize(guess)))
def extract_seg_plot(data, output_filename_base):
    subclone_plot_file_name = output_filename_base + '.MixClone.segplot.png'
    
    subclone_prev_lst = []
    copynumber_lst = []
    seg_num = data.seg_num

    print "Extracting segments plot file..."
    sys.stdout.flush()

    for j in range(0, seg_num):
        if data.segments[j].baseline_label == True or data.segments[j].allele_type == 'PM':
            continue
        subclone_prev_lst.append(data.segments[j].subclone_prev)
        copynumber_lst.append(data.segments[j].copy_number)

    X = len(subclone_prev_lst)
    
    plt.figure(figsize=(8,8), dpi = 150)
    plt.plot(range(1, X+1), subclone_prev_lst, 'o')
    plt.xlim(0, X+1)
    plt.ylim(0, 1)
    plt.xlabel('Copy number')
    plt.ylabel('Subclonal cellular prevalence')
    plt.xticks(sp.linspace(1, X, X), copynumber_lst)
    plt.yticks(sp.linspace(0, 1, 11), ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'])
    plt.savefig(subclone_plot_file_name, bbox_inches='tight')
Exemple #3
0
    def split(self, sagi, meri):
        """ utilizes geometry.grid to change the rectangle into a generalized surface,
        it is specified with a single set of basis vectors to describe the meridonial,
        normal, and sagittal planes."""
        ins = float((sagi - 1))/sagi
        inm = float((meri - 1))/meri
        stemp = self.norm.s/sagi
        mtemp = self.meri.s/meri

        z,theta = scipy.meshgrid(scipy.linspace(-self.norm.s*ins,
                                                self.norm.s*ins,
                                                sagi),
                                 scipy.linspace(-self.meri.s*inm,
                                                self.meri.s*inm,
                                                meri))

        vecin =geometry.Vecr((self.sagi.s*scipy.ones(theta.shape),
                              theta+scipy.pi/2,
                              scipy.zeros(theta.shape))) #this produces an artificial
        # meri vector, which is in the 'y_hat' direction in the space of the cylinder
        # This is a definite patch over the larger problem, where norm is not normal
        # to the cylinder surface, but is instead the axis of rotation.  This was
        # done to match the Vecr input, which works better with norm in the z direction
               
        pt1 = geometry.Point(geometry.Vecr((scipy.zeros(theta.shape),
                                            theta,
                                            z)),
                             self)

        pt1.redefine(self._origin)

        vecin = vecin.split()

        x_hat = self + pt1 #creates a vector which includes all the centers of the subsurface

        out = []
        #this for loop makes me cringe super hard
        for i in xrange(meri):
            try:
                temp = []
                for j in xrange(sagi):
                    inp = self.rot(vecin[i][j])
                    temp += [Cyl(geometry.Vecx(x_hat.x()[:,i,j]),
                                 self._origin,
                                 [2*stemp,2*mtemp],
                                 self.sagi.s,
                                 vec=[inp, self.norm.copy()],
                                 flag=self.flag)]
                out += [temp]
            except IndexError:
                inp = self.rot(vecin[i])
                out += [Cyl(geometry.Vecx(x_hat.x()[:,i]),
                            self._origin,
                            [2*stemp,2*mtemp],
                            self.norm.s,
                            vec=[inp, self.norm.copy()],
                            flag=self.flag)]
                

        return out
Exemple #4
0
 def loadImageAsGreyScale(self, subscriber=0):
     im = Image.open(self.paramFilename.value)
     if im.mode == "I;16":
         im = im.convert("I")
         data = scipy.misc.fromimage(im).astype("int16")
     else:
         data = scipy.misc.fromimage(im, flatten=True)
     Ny, Nx = data.shape
     xUnit = Quantity(self.paramXScale.value.encode("utf-8"))
     xAxis = DataContainer.FieldContainer(
         scipy.linspace(0.0, xUnit.value, Nx, True), xUnit / xUnit.value, longname="x-coordinate", shortname="x"
     )
     if self.paramYScale.value == "link2X":
         yUnit = xUnit * float(Ny) / Nx
     else:
         yUnit = Quantity(self.paramYScale.value.encode("utf-8"))
     yAxis = DataContainer.FieldContainer(
         scipy.linspace(0.0, yUnit.value, Ny, True), yUnit / yUnit.value, longname="y-coordinate", shortname="y"
     )
     try:
         FieldUnit = Quantity(self.paramFieldUnit.value.encode("utf-8"))
     except AttributeError:
         FieldUnit = self.paramFieldUnit.value
     result = DataContainer.FieldContainer(
         data, FieldUnit, longname="Image", shortname="I", dimensions=[yAxis, xAxis]
     )
     result.seal()
     return result
def uvToELz_grid(ulinspace,vlinspace,R=1.,t=-4.,pot='bar',beta=0.,
            potparams=(0.9,0.01,25.*_degtorad,.8,None)):
    """
    NAME:
       uvToELz_grid
    PURPOSE:
       calculate uvToLz on a grid in (u,v)
    INPUT:
       ulinspace, vlinspace - build the grid using scipy's linspace with
                              these arguments
       R - Galactocentric Radius
       t - time to integrate backwards for 
           (interpretation depends on potential)
       pot - type of non-axisymmetric, time-dependent potential
       beta - power-law index of rotation curve
       potparams - parameters for this potential
    OUTPUT:
       final (E,Lz) on grid [nus,nvs,2]
       E=E/vo^2; Lz= Lz/Ro/vo
    HISTORY:
       2010-03-01 - Written - Bovy (NYU)
    """
    us= sc.linspace(*ulinspace)
    vs= sc.linspace(*vlinspace)
    nus= len(us)
    nvs= len(vs)
    out= sc.zeros((nus,nvs,2))
    for ii in range(nus):
        for jj in range(nvs):
            tmp_out= uvToELz(UV=(us[ii],vs[jj]),R=R,t=t,pot=pot,beta=beta,potparams=potparams)
            out[ii,jj,0]= tmp_out[0]
            out[ii,jj,1]= tmp_out[1]
    return out
Exemple #6
0
def volweight(numsplit=(3,3), factor=1, fact2=None, eq='/home/ian/python/g1120824019.01400'):

    b =  TRIPPy.Tokamak(eqtools.EqdskReader(gfile=eq))
    
    rgrid = b.eq.getRGrid()
    zgrid = b.eq.getZGrid()
    rgrid = scipy.linspace(rgrid[0],rgrid[-1],len(rgrid)*factor)
    zgrid = scipy.linspace(zgrid[0],zgrid[-1],len(zgrid)*factor)
    
    twopi2 = twopi(b)
    
    surfs = twopi2[0].split(numsplit[0],numsplit[1])

    out = scipy.zeros((len(rgrid)-1,len(zgrid)-1))

    for i in surfs:
        for j in i:
            surf2 = j
            if fact2 is None:
                surf2 = j
            else:
                surf2 = j.split(fact2,fact2)

            beam = TRIPPy.beam.multiBeam(surf2,twopi2[1])
            b.trace(beam)
            #TRIPPy.plot.mayaplot.plotLine(beam)
            out += TRIPPy.beam.volWeightBeam(beam,rgrid,zgrid)

    
    return out
def getImageDescriptor(model, im, conf):
	im = standardizeImage(im)
	height, width = im.shape[:2]
	numWords = model.vocab.shape[1]
	frames, descrs = getPhowFeatures(im, conf.phowOpts)
	# quantize appearance
	if model.quantizer == 'vq':
		binsa, _ = vq(descrs.T, model.vocab.T)
	elif model.quantizer == 'kdtree':
		raise ValueError('quantizer kdtree not implemented')
	else:
		raise ValueError('quantizer {0} not known or understood'.format(model.quantizer))
	hist = []
	for n_spatial_bins_x, n_spatial_bins_y in zip(model.numSpatialX, model.numSpatialX):
		binsx, distsx = vq(frames[0, :], linspace(0, width, n_spatial_bins_x))
		binsy, distsy = vq(frames[1, :], linspace(0, height, n_spatial_bins_y))
		# binsx and binsy list to what spatial bin each feature point belongs to
		if (numpy.any(distsx < 0)) | (numpy.any(distsx > (width/n_spatial_bins_x+0.5))):
			print ("something went wrong")
			import pdb; pdb.set_trace()
		if (numpy.any(distsy < 0)) | (numpy.any(distsy > (height/n_spatial_bins_y+0.5))):
			print ("something went wrong")
			import pdb; pdb.set_trace()
		# combined quantization
		number_of_bins = n_spatial_bins_x * n_spatial_bins_y * numWords
		temp = arange(number_of_bins)
		# update using this: http://stackoverflow.com/questions/15230179/how-to-get-the-linear-index-for-a-numpy-array-sub2ind
		temp = temp.reshape([n_spatial_bins_x, n_spatial_bins_y, numWords])
		bin_comb = temp[binsx, binsy, binsa]
		hist_temp, _ = histogram(bin_comb, bins=range(number_of_bins+1), density=True)
		hist.append(hist_temp)
	
	hist = hstack(hist)
	hist = array(hist, 'float32') / sum(hist)
	return hist
Exemple #8
0
def test_interpolateArray():
	grid_x = scipy.linspace(1, 5, 20)
	grid_y = scipy.linspace(-1, 1, 10)
	def fn(x):
		return scipy.sin(x[0] + x[1])
	((xlist, ylist), f) = applyGrid([grid_x, grid_y], fn)
	fig = plt.figure()
	ax = Axes3D(fig)	
	ax.scatter(xlist, ylist, f.ravel())
	
	grid2_x = scipy.linspace(1, 5, 40)
	grid2_y = scipy.linspace(-1, 1, 20)
	f2 = interpolateArray([grid_x, grid_y], [grid2_x, grid2_y], f)
	xy_list = itertools.product(grid2_x, grid2_y)
	(xlist2, ylist2) = zip(*xy_list)	
	fig = plt.figure()	
	ax = Axes3D(fig)
	ax.scatter(xlist2, ylist2, f2.ravel())
	
	grid3_x = grid_x
	grid3_y = grid_y
	f3 = interpolateArray([grid2_x, grid2_y], [grid3_x, grid3_y], f2)
	xy_list = itertools.product(grid3_x, grid3_y)
	(xlist3, ylist3) = zip(*xy_list)	
	fig = plt.figure()	
	ax = Axes3D(fig)	
	ax.scatter(xlist3, ylist3, f3.ravel())	
	
	
	
	
	
	
	
def subdivide(g, rmin, rmax, m):
    """
    Subdivide the given interval by equal integrals of g.

    **example**
     >>> g = lambda r : r**2
     >>> rmin = 0.0
     >>> rmax = 1.0
     >>> m = 2
     >>> r = subdivide(g, rmin, rmax, m)
     >>> abs(r - array([0.0, .5**(1./3.), 1.0])).sum() <= 1e-4
     True
    """
    r = sp.linspace(rmin, rmax, m+1)

    # total integral value
    tot = quad(g, rmin, rmax)[0]
    cuts = sp.linspace(0.0, tot, m+1)[1:-1]
    
    # define intervals r_n to r_n+1 to give equal area under g(r)
    r[1:-1] = [brentq((lambda r_: quad(g, rmin, r_)[0]-cut), rmin, rmax,
                      xtol=1.0e-4)
                for cut in cuts]
    # return the sequence of subinterval boundaries
    return r
Exemple #10
0
	def draw_plot(self):
		self.axes.clear()
		self.histax.clear()
		global mode
		if mode == 'hsv':
			self.axes.set_xlabel('Hue')
			self.axes.set_ylabel('Saturation')
			self.axes.set_zlabel('Value')
			tubepixels = np.array(list(self.hsv[greenloc[0], greenloc[1]] for greenloc in coloredpixels(self.mask, (0,255,0))))
			nontubepixels = np.array(list(self.hsv[redloc[0], redloc[1]] for redloc in coloredpixels(self.mask, (0,0,255))))
		else:
			self.axes.set_xlabel('Blue')
			self.axes.set_ylabel('Green')
			self.axes.set_zlabel('Red')
			tubepixels = np.array(list(self.smallimg[greenloc[0], greenloc[1]] for greenloc in coloredpixels(self.mask, (0,255,0))))
			nontubepixels = np.array(list(self.smallimg[redloc[0], redloc[1]] for redloc in coloredpixels(self.mask, (0,0,255))))
		onehundredtube = [int(x) for x in scipy.linspace(0,len(tubepixels)-1, 500)]
		onehundrednon = [int(x) for x in scipy.linspace(0,len(nontubepixels)-1, 500)]
		if len(tubepixels):
			self.axes.scatter(tubepixels[onehundredtube,0], tubepixels[onehundredtube,1], tubepixels[onehundredtube,2], c='g')
			self.histax.hist(tubepixels[onehundredtube, 1], 100, normed=1, facecolor='green')
		if len(nontubepixels):
			self.axes.scatter(nontubepixels[onehundrednon,0], nontubepixels[onehundrednon,1], nontubepixels[onehundrednon,2], c='r')
			self.histax.hist(nontubepixels[onehundrednon, 1], 100, normed=1, facecolor='red')
		self.figure.canvas.draw()
		self.histfig.canvas.draw()
Exemple #11
0
    def testSnrFuncs(self):
        """test for signal to noise ratio functions"""

        # trivial
        data_triv = sp.ones((3, 10))
        snr_triv_test = sp.ones(3)
        assert_equal(
            snr_peak(data_triv, 1.0),
            snr_triv_test)
        assert_equal(
            snr_power(data_triv, 1.0),
            snr_triv_test)
        assert_equal(
            snr_maha(data_triv, sp.eye(data_triv.shape[1])),
            snr_triv_test)

        # application
        data = sp.array([
            sp.sin(sp.linspace(0.0, 2 * sp.pi, 100)),
            sp.sin(sp.linspace(0.0, 2 * sp.pi, 100)) * 2,
            sp.sin(sp.linspace(0.0, 2 * sp.pi, 100)) * 5,
        ])
        assert_equal(
            snr_peak(data, 1.0),
            sp.absolute(data).max(axis=1))
        assert_equal(
            snr_power(data, 1.0),
            sp.sqrt((data * data).sum(axis=1) / data.shape[1]))
        assert_almost_equal(
            snr_maha(data, sp.eye(data.shape[1])),
            sp.sqrt((data * data).sum(axis=1) / data.shape[1]))
Exemple #12
0
def test_newton_exact_line_search():
    op = p.OptimizationProblem(c.chebyquad)
    guess=linspace(0,1,4)
    cn  = p.NewtonExactLine(op)
    assert near(sol4,(cn.optimize(guess)))
    guess=linspace(0,1,8)
    assert near(sol8,(cn.optimize(guess)))
Exemple #13
0
def test_classic_newton():
    op = p.OptimizationProblem(c.chebyquad)
    guess=linspace(0,1,4)
    cn  = p.ClassicNewton(op)
    assert near(sol4,(cn.optimize(guess)))
    guess=linspace(0,1,8)
    assert near(sol8,(cn.optimize(guess)))
 def get_znodes(self):
     " Compute a nodes for a log-lower and linear-upper grid. "
     zlower = sp.exp(sp.linspace(sp.log(self.zmin), sp.log(self.zmid), self.Nlo))
     zupper = sp.linspace(self.zmid, self.zmax, self.Nhi)
     znodes = sp.concatenate([zlower, 
                              zupper[1:]])
     return znodes
Exemple #15
0
def add_intron_patch2(ax, start, stop, cnt, color='green'):
    ### compute a quadratic function through the three points

    ### we set the first root to 0 and shift only the plotting ...
    x2 = ((stop - start) / 2.0)
    x3 = float(stop - start)

    ### compute coefficients
    #z = float((x1*x1*x2 + x1*x3*x3 + x2*x2*x3) - (x3*x3*x2 + x2*x2*x1 + x1*x1*x3))
    z = float((x2*x2*x3) - (x3*x3*x2))
    if z == 0:
        return

    #a = float(cnt) * (x3 - x1) / z
    #b = float(cnt) * ((x1*x1) - (x3*x3)) / z
    #c = float(cnt) * ((x1*x3*x3) - (x1*x1*x3)) / z
    a = float(cnt) * x3 / z
    b = float(cnt) * (-1*(x3*x3)) / z

    ### get points
    #x = sp.linspace(start, stop, 100)
    x = sp.linspace(0, stop-start, 100)
    #y = (a*x*x) + (b*x) + c
    y = (a*x*x) + (b*x)
    ax.plot(sp.linspace(start, stop, 100), y, '-', color=color)
def plot_disc_norm():
    x = sp.linspace(-3,3,100);
    y = st.norm.pdf(x,0,1);
    fig, ax = plt.subplots()
    fig.canvas.draw()
    
    ax.plot(x,y)
    
    fill1_x = sp.linspace(-2,-1.5,100)
    fill1_y = st.norm.pdf(fill1_x,0,1)
    fill2_x = sp.linspace(-1.5,-1,100)
    fill2_y = st.norm.pdf(fill2_x,0,1)
    ax.fill_between(fill1_x,0,fill1_y,facecolor = 'blue', edgecolor = 'k',alpha = 0.75)
    ax.fill_between(fill2_x,0,fill2_y,facecolor = 'blue', edgecolor = 'k',alpha = 0.75)
    for label in ax.get_yticklabels():
        label.set_visible(False)
    for tick in ax.get_xticklines():
        tick.set_visible(False)
    for tick in ax.get_yticklines():
        tick.set_visible(False)
    
    plt.rc("font", size = 16)
    plt.xticks([-2,-1.5,-1])
    labels = [item.get_text() for item in ax.get_xticklabels()]
    labels[0] = r"$v_k$"
    labels[1] = r"$\varepsilon_k$"
    labels[2] = r"$v_{k+1}$"
    ax.set_xticklabels(labels)
    plt.ylim([0, .45])

    
    plt.savefig('discnorm.pdf')
 def loadImageAsGreyScale(self, subscriber=0):
     im = PIL.Image.open(self.paramFilename.value)
     if im.mode == "I;16":
         im = im.convert("I")
         data = scipy.misc.fromimage(im).astype("int16")
     else:
         data = scipy.misc.fromimage(im, flatten=True)
     Ny, Nx = data.shape
     xUnit = parseFCUnit(self.paramXScale.value)
     xAxis = DataContainer.FieldContainer(
         scipy.linspace(0.0, self._getScalar(xUnit), Nx, True),
         xUnit / self._getScalar(xUnit),
         longname="x-coordinate",
         shortname="x",
     )
     if self.paramYScale.value == "link2X":
         yUnit = xUnit * float(Ny) / Nx
     else:
         yUnit = parseFCUnit(self.paramYScale.value)
     yAxis = DataContainer.FieldContainer(
         scipy.linspace(0.0, self._getScalar(yUnit), Ny, True),
         yUnit / self._getScalar(yUnit),
         longname="y-coordinate",
         shortname="y",
     )
     fieldUnit = parseFCUnit(self.paramFieldUnit.value)
     result = DataContainer.FieldContainer(
         data, fieldUnit, longname="Image", shortname="I", dimensions=[yAxis, xAxis]
     )
     result.seal()
     return result
def joint(dataA, dataB, g, minLimitA, maxLimitA, minLimitB, maxLimitB, alpha=2):
    '''Calculates a joint PDF based on the DEFT algorithm.

    Keyword arguments:
    dataA           data variable A NxP
                        N = elements
                        P = population
    dataB           data variable B NxP
                        N = elements
                        P = population
    minLimitA       Minimal possible value for variable A
    maxLimitA       Maximal possible value for variable A
    minLimitB       Minimal possible value for variable B
    maxLimitB       Maximal possible value for variable B
    g               Number of bins on which to compute the estimated density
    alpha           The smoothness parameter, specifying which derivative of the filed is constrained by the prior. May be any integer >= 1.
    Returns:    
                    pdf NxB
                        N = elements
                        BA = Bin A
                        BB = Bin B
    '''
    assert alpha >= 1, "Alpha must be greater or equal than 1"
    assert g >= 1, "Number of bins must be greater or equal than 1"
    result = np.zeros((dataA.shape[0], g, g))
    bbox = [minLimitA, maxLimitA, minLimitB, maxLimitB]
    xgridA = sp.linspace(bbox[0], bbox[1], g)
    xgridB = sp.linspace(bbox[2], bbox[3], g)

    for i in xrange(dataA.shape[0]):
        if len(dataA[i,:]) > 1 and len(dataB[i,:]) > 1:
            da = deft_2d(dataA[i,:], dataB[i,:], bbox, G=g, alpha=alpha)(xgridA, xgridB)
            result[i, :, :] = da

    return result
Exemple #19
0
def create_grid(r_in, r_out, nshell, space = 'powerlaw1', end = True):
    # function to create grid
    if space == 'log10':
        from scipy import log10, logspace
        # get the exponent of the start- and
        # stop-radius in input units
        start = [log10(r_in), 0][r_in == 0]
        stop = log10(r_out)
        radii = logspace(start, stop, num=nshell, endpoint=end)
    elif space == "powerlaw1":
        from scipy import arange
        radii = r_in * (r_out/r_in)**(arange(nshell)/(nshell - 1.0))
    elif space == 'linear':
        from scipy import linspace
        # linearly spaced grid
        radii = linspace(r_in, r_out, num=nshell, endpoint=end)
    elif space == 'powerlaw2':
        from scipy import linspace
        # first check if coefficients to the power-law was given
        #~ if 'exp' in kwargs:
            #~ p_exp = kwargs['exp']
        #~ else: # if not, set it to 2, i.e. r^2
            #~ p_exp = 2
        radii = r_in + (r_out - r_in)*(linspace(r_in, r_out, num=nshell, endpoint=end)/(r_out))**2
        #pr_int('Not implemented yet.')
        #raise ParError(spaced)
    else:
        raise Exception(space)
    return radii
Exemple #20
0
    def generateNodesAdaptive(self):
        innerDomainSize = self.innerDomainSize
        innerMeshSize   = self.innerMeshSize
        numberElementsInnerDomain = innerDomainSize/innerMeshSize
	assert(numberElementsInnerDomain < self.numberElements)
        domainCenter = (self.domainStart+self.domainEnd)/2
        nodes0 = np.linspace(domainCenter,innerDomainSize/2.0,(numberElementsInnerDomain/2.0)+1.0)
        nodes0 = np.delete(nodes0,-1)
        numberOuterIntervalsFromDomainCenter = (self.numberElements - numberElementsInnerDomain)/2.0
        const = np.log2(innerDomainSize/2.0)/0.5
        exp = np.linspace(const,np.log2(self.domainEnd*self.domainEnd),numberOuterIntervalsFromDomainCenter+1)
        nodes1 = np.power(np.sqrt(2),exp)
        nodesp = np.concatenate((nodes0,nodes1))
        nodesn = -nodesp[::-1]
        nodesn = np.delete(nodesn,-1)
        linNodalCoordinates = np.concatenate((nodesn,nodesp))
        nodalCoordinates = 0

        #Introduce higher order nodes
        if self.elementType == "quadratic" or self.elementType == "cubic":
           if self.elementType == "quadratic":
              numberNodesPerElement = 3 
           elif self.elementType == "cubic":
              numberNodesPerElement = 4

           for i in range(0,len(linNodalCoordinates)-1):
              newnodes = np.linspace(linNodalCoordinates[i],linNodalCoordinates[i+1],numberNodesPerElement)
              nodalCoordinates = np.delete(nodalCoordinates,-1)
              nodalCoordinates = np.concatenate((nodalCoordinates,newnodes))

        else:
           nodalCoordinates = linNodalCoordinates
    
        return nodalCoordinates
def plotDecisionBoundary(data, X, theta):
    """
    Plots the data points X and y into a new figure with the
    decision boundary defined by theta
    """
    plotData(data)
    if X.shape[1] <= 3:
        plot_x = sp.array([min(X[:, 1]), max(X[:, 1])])
        plot_y = (-1.0 / theta[2]) * (theta[1] * plot_x + theta[0])
        plt.plot(plot_x, plot_y)
        plt.axis([30, 100, 30, 100])
    else:
        u = sp.linspace(-1, 1.5, 50)
        v = sp.linspace(-1, 1.5, 50)
        z = sp.zeros((len(u), len(v)))

        for i in range(0, len(u)):
            for j in range(0, len(v)):
                z[i, j] = (mapFeature(sp.array([u[i]]), sp.array([v[j]]))).dot(theta)

        z = z.T # important to transpose z before calling contour

        u, v = sp.meshgrid(u, v)

        plt.contour(u, v, z, [0.0, 0.0])
Exemple #22
0
def genBG(img, mask):
    x=[]
    y=[]
    z=[]
    w=[]
    
    #while len(x)<500:
    #    i=random.randrange(img.shape[0])
    #    j=random.randrange(img.shape[1])
     
    for i in linspace(0, img.shape[0]-1, 20): 
      for j in linspace(0, img.shape[1]-1, 20): 
        i=int(i)
        j=int(j)
        
        if mask!=None and mask[i, j]==0.0:
            continue
        a=img[max(0, i-10):i+10, max(0, j-10):j+10]
        #print sqrt(a.sum()), a.std()
        
        x.append(1.0*i)
        y.append(1.0*j)
        z.append(img[i, j])
        w.append(1.0/a.std())
        
    
    
    s=scipy.interpolate.bisplrep(x, y, z, w=w, xb=0,  xe=img.shape[0], yb=0, ye=img.shape[1], s=1e3)
    BG=scipy.interpolate.bisplev(arange(img.shape[0]), arange(img.shape[1]), s)
    
    return BG
Exemple #23
0
def draw_cubic_bezier(p0,p1,p2,p3):
	global curr_pos, curr_vel, time, fig, ax, time_per_unit, meters_per_second, time_between
	t0 = sp.linspace(0,0,1)
	t1 = sp.linspace(1,1,1)

	# draw cubic bezier curve
	
	#Setup the parameterisation
	seg_length = bezier_cubic_length(p0, p1, p2, p3)
	num_points = int(seg_length*meters_per_unit*pts_per_meter)
	if num_points < min_pts: num_points = min_pts
	T_max = 1.0
	t = sp.linspace(0,1,num_points)

	#Use the Cubic Bezier formula

	# add the position (Bx,By)
	Bx, By, Bz = bezier_cubic(p0, p1, p2, p3, t)

	# add the velocity (Bdx,Bdy)
	Bdx, Bdy, Bdz = bezier_cubic_dt(p0, p1, p2, p3, t)

	dxi, dyi, dzi = bezier_cubic_dt(p0, p1, p2, p3, t0)
	dxf, dyf, dzf = bezier_cubic_dt(p0, p1, p2, p3, t1)

	continuous = is_continuous(p0, [dxi[0], dyi[0], dzi[0]], [dxf[0], dyf[0], dzf[0]])

	#Plot the Bezier curve
	ax.plot(Bx, By, "k")
	

	# Add to trajectory msg
	P = np.array([Bx,By,Bz])
	V = np.array([Bdx,Bdy,Bdz])

	# find the duration of the curent segment at constant velocity
	duration = seg_length*meters_per_unit/meters_per_second

	times = []

	# allow time to stop if not continuous path
	if not continuous:			
		P = np.insert(P,0,P[:,0],axis=1)
		V = np.insert(V,0,[0.0,0.0,0.0],axis=1)
		time += time_between/2.0
		times.append(time)
		time += time_between/2.0

	# find the duration of a piece of the segment at constant velocity
	seconds_per_unit = meters_per_unit/meters_per_second
	for curr_t in t:
		dt = bezier_cubic_length(p0, p1, p2, p3, ti=0.0, tf=curr_t)*seconds_per_unit
		times.append(time + dt)

	# ax.plot(times, Bx, "r")
	# ax.plot(times, By, "g")
	# ax.plot(times, Bz, "b")

	add_to_plane_traj_msg(P,V,times)
	time += duration
def grid(x, y, z, resX=100, resY=100):
    "Convert 3 column data to matplotlib grid"
    xi = sp.linspace(min(x), max(x), resX)
    yi = sp.linspace(min(y), max(y), resY)
    Z = griddata(x, y, z, xi, yi, interp='linear')
    X, Y = sp.meshgrid(xi, yi)
    return X, Y, Z
def extract_BAFheatmap(data, output_filename_base):
    BAF_counts_min = constants.BAF_COUNTS_MIN
    BAF_counts_max = constants.BAF_COUNTS_MAX    

    outheatmap_dir_name = output_filename_base + '.MixClone.heatmap'
    if os.path.exists(outheatmap_dir_name) == False:
        os.mkdir(outheatmap_dir_name)
        
    seg_num = data.seg_num
    
    for j in range(0, seg_num):
        BAF_counts_j = data.segments[j].BAF_counts
        seg_name_j = data.segments[j].name
        BAF_counts_sub = BAF_counts_j[BAF_counts_min:BAF_counts_max, BAF_counts_min:BAF_counts_max]
        color_max_j = BAF_counts_sub.max()
        
        print 'Plotting segment {0}...'.format(seg_name_j)
        sys.stdout.flush()
        
        plt.figure(figsize=(8,8), dpi = 150)
        plt.xlim((0, 100))
        plt.ylim((0, 100))
        plt.xticks(sp.linspace(0, 100, 11), sp.linspace(0, 1, 11))
        plt.yticks(sp.linspace(0, 100, 11), sp.linspace(0, 1, 11))
        plt.xlabel('Tumor sample B allele frequency')
        plt.ylabel('Normal sample B allele frequency')
        plt.imshow(BAF_counts_j, vmin = 0, vmax = max(1, color_max_j))
        cbar = plt.colorbar(ticks=[0, color_max_j], orientation='vertical', shrink=0.78)
        cbar.ax.set_yticklabels(['0', '>= ' + str(int(color_max_j))])
        plt.savefig('./' + outheatmap_dir_name + '/' + seg_name_j, bbox_inches='tight')
def plot_median_errors(RefinementLevels):
        for i in RefinementLevels[0].cases:
            x =[];
            y =[];
            print "Analyzing median error on: ", i ;
            for r in RefinementLevels:                
                x.append(r.LUT.D_dim*r.LUT.P_dim)
                r.get_REL_ERR_SU2(i)
                y.append(r.SU2[i].median_ERR*100)
            
            x = sp.array(x)
            y = sp.array(y)            
            y = y[sp.argsort(x)]
            x = x[sp.argsort(x)]
                                    
            LHM = sp.ones((len(x),2))
            RHS = sp.ones((len(x),1))            
            LHM[:,1] = sp.log10(x)
            RHS[:,0] = sp.log10(y)

            sols = sp.linalg.lstsq(LHM,RHS)
            b = -sols[0][1]
            plt.loglog(x,y, label='%s, %s'%(i,r'$O(\frac{1}{N})^{%s}$'%str(sp.around(b,2))), basex=10, basey=10, \
                       subsy=sp.linspace(10**(-5), 10**(-2),20),\
                       subsx=sp.linspace(10**(2), 10**(5),50))
            
            #for r in RefinementLevels:                
               # x.append(r.LUT.D_dim*r.LUT.P_dim)
              #  r.get_REL_ERR_SciPy(i)
             #   y.append(r.SciPy[i].median_ERR*100)
            #plt.plot(x,y, label='SciPy: %s'%i)
        plt.grid(which='both')
        plt.xlabel('Grid Nodes (N)')
        plt.ylabel('Median relative error [%]')
        return;
Exemple #27
0
    def split(self, sagi, meri):
        """ utilizes geometry.grid to change the rectangle into a generalized surface,
        it is specified with a single set of basis vectors to describe the meridonial,
        normal, and sagittal planes."""
        ins = float((sagi - 1))/sagi
        inm = float((meri - 1))/meri
        stemp = self.sagi.s/sagi
        mtemp = self.meri.s/meri

        self.sagi.s,self.meri.s = scipy.meshgrid(scipy.linspace(-self.sagi.s*ins,
                                                                 self.sagi.s*ins,
                                                                 sagi),
                                                 scipy.linspace(-self.meri.s*inm,
                                                                 self.meri.s*inm,
                                                                 meri))

        x_hat = self + (self.sagi + self.meri) #creates a vector which includes all the centers of the subsurface
        self.sagi.s = stemp*sagi #returns values to previous numbers
        self.meri.s = mtemp*meri

        print(x_hat.x().shape)

        temp = Rect(x_hat,
                    self._origin,
                    [2*stemp,2*mtemp],
                    vec=[self.meri.copy(), self.norm.copy()],
                    flag=self.flag)
        #return temp

        return super(Rect, temp).split(temp._origin,
                                       [2*stemp,2*mtemp],
                                       vec=[temp.meri,temp.norm],
                                       flag=temp.flag,
                                       obj=type(temp))
 def make_voltage_list(self,start,stop,number_points,bowtie=False):
      """ Makes a voltage sweep list given stop,start,number_of_points
      and a boolen that determines if all sweeps begin and end on zero"""
      if not bowtie:
         try:
             if (type(start)!=float or type(stop)!=float or type(number_points)!=float):
                 [start,stop,number_points]=map(lambda x: float(x),[start,stop,number_points])
             voltage_array=linspace(start,stop,number_points)
             voltage_list=voltage_array.tolist()
             return voltage_list
         except:
             print "make_voltage_list failed"
      elif bowtie:
         try:
             if (type(start)!=float or type(stop)!=float or type(number_points)!=float):
                 [start,stop,number_points]=map(lambda x: float(x),[start,stop,number_points])
             array_1=linspace(float(0),start,number_points)
             list_1=array_1.tolist()
             array_2=linspace(float(0),stop,number_points)
             list_2=array_2.tolist()
             voltage_list=array_1.tolist()
             list_1.reverse()
             voltage_list=voltage_list+list_1
             voltage_list=voltage_list+list_2
             list_2.reverse()
             voltage_list=voltage_list+list_2
             return voltage_list 
         except:
             raise
             print "make_voltage_list failed"    
    def __init__(self, R, a, B0, Ip, betat, length_unit="m", npts=257):
        # instantiate superclass, forcing time splining to false (no time variation
        # in equilibrium)
        super(CircSolovievEFIT, self).__init__(length_unit=length_unit, tspline=False, monotonic=False)

        self._defaultUnits = {}

        self._R = R
        self._defaultUnits["_R"] = length_unit
        self._a = a
        self._defaultUnits["_a"] = length_unit
        self._B0 = B0
        self._defaultUnits["_B0"] = "T"
        self._Ip = Ip
        self._defaultUnits["_Ip"] = "MA"
        self._betat = betat
        self._npts = npts

        self._currentSign = -1 if Ip > 0 else 1
        # Remember: Ip is in MA.
        self._qstar = (2.0 * scipy.pi * a ** 2 * B0) / (4.0 * scipy.pi * 1.0e-1 * R * Ip)

        # flux definitions
        self._psiLCFS = scipy.array([0.0])
        self._psi0 = -0.5 * self._B0 * self._a ** 2 / self._qstar
        self._psi0 = scipy.array([self._psi0])

        # RZ grid
        self._rGrid = scipy.linspace(R - 1.25 * a, R + 1.25 * a, self._npts)
        self._defaultUnits["_rGrid"] = length_unit
        self._zGrid = scipy.linspace(-1.25 * a, 1.25 * a, self._npts)
        self._defaultUnits["_zGrid"] = length_unit

        self._psiRZ = self.rz2psi_analytic(self._rGrid, self._zGrid, length_unit=length_unit, make_grid=True)
        self._psiRZ = scipy.reshape(self._psiRZ, (1, npts, npts))
Exemple #30
0
    def setUp(self):
        np.random.seed(1)

        # generate data
        N = 400
        s_x = 0.05
        s_y = 0.1
        X = (sp.linspace(0, 2, N) + s_x * sp.randn(N))[:, sp.newaxis]
        Y = sp.sin(X) + s_y * sp.randn(N, 1)
        Y -= Y.mean(0)
        Y /= Y.std(0)

        Xstar = sp.linspace(0, 2, 1000)[:, sp.newaxis]

        # define mean term
        F = 1.0 * (sp.rand(N, 2) < 0.2)
        mean = lin_mean(Y, F)

        # define covariance matrices
        covar1 = SQExpCov(X, Xstar=Xstar)
        covar2 = FixedCov(sp.eye(N))
        covar = SumCov(covar1, covar2)

        # define gp
        self._gp = GP(covar=covar, mean=mean)