示例#1
0
    def spheres(shape, radius, porosity):
        r"""
        Generate a packing of overlapping mono-disperse spheres

        Parameters
        ----------
        shape : list
            The size of the image to generate in [Nx, Ny, Nz] where N is the
            number of voxels.

        radius : scalar
            The radius of spheres in the packing

        porosity : scalar
            The porosity of the final image.  This number is approximated by
            the method so the returned result may not have exactly the
            specified value.

        Notes
        -----
        This method can also be used to generate a dispersion of hollows by
        treating ``porosity`` as solid volume fraction and inverting the
        returned image.
        """
        if sp.size(shape) == 1:
            shape = sp.full((3, ), int(shape))
        im = sp.zeros(shape, dtype=bool)
        while sp.sum(im)/sp.size(im) < (1 - porosity):
            temp = sp.rand(shape[0], shape[1], shape[2]) < 0.9995
            im = im + (spim.distance_transform_edt(temp) < radius)
        return ~im
 def test_check_network_health_bidirectional_throat(self):
     net = OpenPNM.Network.Cubic(shape=[5, 5, 5])
     P12 = net['throat.conns'][0]
     net['throat.conns'][0] = [P12[1], P12[0]]
     a = net.check_network_health()
     assert sp.size(a['bidirectional_throats']) == 1
     assert sp.size(a['duplicate_throats']) == 0
示例#3
0
def ideal_data(num, dimU, dimY, dimX, noise=1):
    """Linear system data"""
    # generate randomized linear system matrices
    A = randn(dimX, dimX)
    B = randn(dimX, dimU)
    C = randn(dimY, dimX)
    D = randn(dimY, dimU)

    # make sure state evolution is stable
    U, S, V = svd(A)
    A = dot(U, dot(diag(S / max(S)), V))
    U, S, V = svd(B)
    S2 = zeros((size(U,1), size(V,0)))
    S2[:,:size(U,1)] = diag(S / max(S))
    B = dot(U, dot(S2, V))

    # random input
    U = randn(num, dimU)

    # initial state
    X = reshape(randn(dimX), (1,-1))

    # initial output
    Y = reshape(dot(C, X[-1]) + dot(D, U[0]), (1,-1))

    # generate next state
    X = concatenate((X, reshape(dot(A, X[-1]) + dot(B, U[0]), (1,-1))))

    # and so forth
    for u in U[1:]:
        Y = concatenate((Y, reshape(dot(C, X[-1]) + dot(D, u), (1,-1))))
        X = concatenate((X, reshape(dot(A, X[-1]) + dot(B, u), (1,-1))))

    return U, Y + randn(num, dimY) * noise
示例#4
0
def plotConn():
    # Create plot
    figh = figure(figsize=(8,6))
    figh.subplots_adjust(left=0.02) # Less space on left
    figh.subplots_adjust(right=0.98) # Less space on right
    figh.subplots_adjust(top=0.96) # Less space on bottom
    figh.subplots_adjust(bottom=0.02) # Less space on bottom
    figh.subplots_adjust(wspace=0) # More space between
    figh.subplots_adjust(hspace=0) # More space between
    h = axes()
    totalconns = zeros(shape(f.connprobs))
    for c1 in range(size(f.connprobs,0)):
        for c2 in range(size(f.connprobs,1)):
            for w in range(f.nreceptors):
                totalconns[c1,c2] += f.connprobs[c1,c2]*f.connweights[c1,c2,w]*(-1 if w>=2 else 1)
    imshow(totalconns,interpolation='nearest',cmap=bicolormap(gap=0))

    # Plot grid lines
    hold(True)
    for pop in range(f.npops):
        plot(array([0,f.npops])-0.5,array([pop,pop])-0.5,'-',c=(0.7,0.7,0.7))
        plot(array([pop,pop])-0.5,array([0,f.npops])-0.5,'-',c=(0.7,0.7,0.7))

    # Make pretty
    h.set_xticks(range(f.npops))
    h.set_yticks(range(f.npops))
    h.set_xticklabels(f.popnames)
    h.set_yticklabels(f.popnames)
    h.xaxis.set_ticks_position('top')
    xlim(-0.5,f.npops-0.5)
    ylim(f.npops-0.5,-0.5)
    clim(-abs(totalconns).max(),abs(totalconns).max())
    colorbar()
示例#5
0
    def blobs(shape, porosity, blobiness=8):
        """
        Generates an image containing amorphous blobs

        Parameters
        ----------
        shape : list
            The size of the image to generate in [Nx, Ny, Nz] where N is the
            number of voxels

        blobiness : scalar
            Controls the morphology of the image.  A higher number results in
            a larger number of smaller blobs.

        porosity : scalar
            The porosity of the final image.  This number is approximated by
            the method so the returned result may not have exactly the
            specified value.

        """
        if sp.size(shape) == 1:
            shape = sp.full((3, ), int(shape))
        [Nx, Ny, Nz] = shape
        sigma = sp.mean(shape)/(4*blobiness)
        mask = sp.rand(Nx, Ny, Nz)
        mask = spim.gaussian_filter(mask, sigma=sigma)
        hist = sp.histogram(mask, bins=1000)
        cdf = sp.cumsum(hist[0])/sp.size(mask)
        xN = sp.where(cdf >= porosity)[0][0]
        im = mask <= hist[1][xN]
        return im
示例#6
0
    def test_set_boundary_conditions_bctypes(self):
        self.alg.setup(invading_phase=self.water,
                       defending_phase=self.air,
                       trapping=True)
        Ps = sp.random.randint(0, self.net.Np, 10)

        self.alg.set_boundary_conditions(pores=Ps, bc_type='inlets')
        assert sp.sum(self.alg['pore.inlets']) == sp.size(sp.unique(Ps))
        self.alg['pore.inlets'] = False

        self.alg.set_boundary_conditions(pores=Ps, bc_type='outlets')
        assert sp.sum(self.alg['pore.outlets']) == sp.size(sp.unique(Ps))
        self.alg['pore.outlets'] = False

        self.alg.set_boundary_conditions(pores=Ps, bc_type='residual')
        assert sp.sum(self.alg['pore.residual']) == sp.size(sp.unique(Ps))
        self.alg['pore.residual'] = False

        flag = False
        try:
            self.alg.set_boundary_conditions(pores=Ps, bc_type='bad_type')
        except:
            flag = True
        assert flag

        flag = False
        try:
            self.alg.set_boundary_conditions(bc_type=None, mode='bad_type')
        except:
            flag = True
        assert flag
示例#7
0
def jplot2d(*args):
	arg=' -2D -l SOUTH '
	i=0
	while (i<len(args)):
		i_arg=0
		q=False
		arg_txt=True
		while ((len(args)>(i_arg+i+1)) & arg_txt):
			if (type(args[i+i_arg+1]) is str):
				i_arg+=1
				arg=arg+' '+args[i+i_arg]
			else:
				arg_txt=False
				
		name='.'+str(scipy.stats.random_integers(10000))+'.plot.tmp'
		
		if ((len(args[i])==scipy.size(args[i]))):
			a=scipy.dstack((scipy.arange(1,scipy.size(args[i])+1),args[i]))[0]
			scipy.io.write_array(name,a)
		else:
			scipy.io.write_array(name,args[i])
			
		i+=i_arg+1
		arg=arg+' '+name
	
	os.system('java -cp jmathplot.jar org.math.plot.PlotPanel '+arg)
示例#8
0
    def run(self, N=100):
        r'''
        '''
        im = self.image
        # Create a list of N random points to use as box centers
        pad = [0.1,0.1,0.45]  # Ensure points are near middle
        Cx = sp.random.randint(pad[0]*sp.shape(im)[0],(1-pad[0])*sp.shape(im)[0],N)
        Cy = sp.random.randint(pad[1]*sp.shape(im)[1],(1-pad[1])*sp.shape(im)[1],N)
        Cz = sp.random.randint(pad[2]*sp.shape(im)[2],(1-pad[2])*sp.shape(im)[2],N)
        C = sp.vstack((Cx,Cy,Cz)).T

        # Find maximum radius allowable for each point
        Rmax = sp.array(C>sp.array(sp.shape(im))/2)
        Rlim = sp.zeros(sp.shape(Rmax))
        Rlim[Rmax[:,0],0] = sp.shape(im)[0]
        Rlim[Rmax[:,1],1] = sp.shape(im)[1]
        Rlim[Rmax[:,2],2] = sp.shape(im)[2]
        R = sp.absolute(C-Rlim)
        R = R.astype(sp.int_)
        Rmin = sp.amin(R,axis=1)

        vol = []
        size = []
        porosity = []
        for i in range(0,N):
            for r in sp.arange(Rmin[i],1,-10):
                imtemp = im[C[i,0]-150:C[i,0]+150,C[i,1]-150:C[i,1]+150:,C[i,2]-r:C[i,2]+r]
                vol.append(sp.size(imtemp))
                size.append(2*r)
                porosity.append(sp.sum(imtemp==1)/(sp.size(imtemp)))

        vals = namedtuple('REV', ('porosity', 'size'))
        vals.porosity = porosity
        vals.size = size
        return vals
示例#9
0
    def __init__(self, network=None, phase=None, geometry=None,
                 pores=[], throats=[], **kwargs):
        super().__init__(**kwargs)
        logger.name = self.name

        # Associate with Network
        if network is None:
            self._net = GenericNetwork()
        else:
            self._net = network  # Attach network to self
            self._net._physics.append(self)  # Register self with network

        # Associate with Phase
        if phase is None:
            phase = GenericPhase(network=self._net)
        phase._physics.append(self)  # Register self with phase
        self._phases.append(phase)  # Register phase with self

        if geometry is not None:
            if (sp.size(pores) > 0) or (sp.size(throats) > 0):
                raise Exception('Cannot specify a Geometry AND pores or throats')
            pores = self._net.toindices(self._net['pore.' + geometry.name])
            throats = self._net.toindices(self._net['throat.' + geometry.name])

        # Initialize a label dictionary in the associated phase and network
        self._phases[0]['pore.'+self.name] = False
        self._phases[0]['throat.'+self.name] = False
        self._net['pore.'+self.name] = False
        self._net['throat.'+self.name] = False
        try:
            self.set_locations(pores=pores, throats=throats)
        except:
            self.controller.purge_object(self)
            raise Exception('Provided locations are in use, instantiation cancelled')
示例#10
0
文件: stats.py 项目: squireg/tcrm
def cdf2d(x, y, z):
    """
    2D Cumulative Density Function extracted from cdf2d.m
    Assumes the grid is uniformly defined, i.e. dx and dy are
    constant.
    """
    if size(x) < 2 or size(y) < 2:
        logger.critical("X or Y grids are not arrays")
        raise TypeError, "X or Y grids are not arrays"
    grid_area = abs(x[1] - x[0])*abs(y[1] - y[0])
    grid_volume = grid_area*z

    cz = zeros([x.size, y.size], 'd')
    cz[:, 0] = (grid_volume[:,0]).cumsum()
    cz[0, :] = (grid_volume[0,:]).cumsum()

    for i in xrange(1, len(x)):
        for j in xrange(1, len(y)):
            cz[i,j] = cz[i-1,j] + cz[i,j-1] - cz[i-1,j-1] + grid_volume[i,j]

    if cz[-1,-1] == 0:
        return cz
    #normalize cy
    cz = cz/cz[-1,-1]
    return cz
示例#11
0
    def find_clusters(self, mask=[]):
        r"""
        Identify connected clusters of pores in the network.

        Parameters
        ----------
        mask : array_like, boolean
            A list of active nodes.  This method will automatically search
            for clusters based on site or bond connectivity depending on
            wheather the received mask is Np or Nt long.

        Returns
        -------
        clusters : array_like
            An Np long list of clusters numbers

        """
        if sp.size(mask) == self.num_throats():
            # Convert to boolean mask if not already
            temp = sp.zeros((self.num_throats(),), dtype=bool)
            temp[mask] = True
        elif sp.size(mask) == self.num_pores():
            conns = self.find_connected_pores(throats=self.throats())
            conns[:, 0] = mask[conns[:, 0]]
            conns[:, 1] = mask[conns[:, 1]]
            temp = sp.array(conns[:, 0]*conns[:, 1], dtype=bool)
        else:
            raise Exception('Mask received was neither Nt nor Np long')
        temp = self.create_adjacency_matrix(data=temp,
                                            sprsfmt='csr',
                                            dropzeros=True)
        clusters = sprs.csgraph.connected_components(csgraph=temp,
                                                     directed=False)[1]
        return clusters
示例#12
0
文件: MNEfit.py 项目: MarvinT/pyMNE
def MNEfit(stim,resp,order):
    # in order for dlogloss to work, we need to know -<g(yt(n),xt)>data
    # == calculate the constrained averages over the data set
    Nsamples = sp.size(stim,0)
    Ndim = sp.size(stim,1)
    psp = sp.mean(sp.mean(resp)) #spike probability (first constraint)
    avg = (1.0*stim.T*resp)/(Nsamples*1.0)
    avgs = sp.vstack((psp,avg))
    if(order > 1):
        avgsqrd = (stim.T*1.0)*(sp.array(sp.tile(resp,(1,Ndim)))*sp.array(stim))/(Nsamples*1.0)
        avgsqrd = sp.reshape(avgsqrd,(Ndim**2,1))
        avgs = sp.vstack((avgs,avgsqrd))
    
    #initialize params:
    pstart = sp.log(1/avgs[0,0] - 1)
    pstart = sp.hstack((pstart,(.001*(2*sp.random.rand(Ndim)-1))))
    if(order > 1):
        temp = .0005*(2*sp.random.rand(Ndim,Ndim)-1)
        pstart = sp.hstack((pstart,sp.reshape(temp+temp.T,(1,Ndim**2))[0]))
    
    #redefine functions with fixed vals:
    def logLoss(p):
        return LLF.log_loss(p, stim, resp, order)
    def dlogLoss(p):
        return LLF.d_log_loss(p, stim, avgs, order)
    #run the function:
    #pfinal = opt.fmin_tnc(logLoss,pstart,fprime=dlogLoss)
    # conjugate-gradient:
    pfinal = opt.fmin_cg(logLoss,pstart,fprime=dlogLoss)
    #pfinal = opt.fmin(logLoss,pstart,fprime=dlogLoss)
    return pfinal
 def test_far_apart_clusters_estimate_all(self):
     cluster1 = sp.randn(40,1000)
     cluster2 = sp.randn(40,1000) * 2
     cluster2[0,:] += 10
     clusterList1 = [cluster1[:,i]
                     for i in xrange(sp.size(cluster1,1))]
     clusterList2 = [cluster2[:,i]
                     for i in xrange(sp.size(cluster2,1))]
     total, pair = qa.overlap_fp_fn(
         {1: clusterList1, 2: clusterList2})
     self.assertLess(total[1][0], 1e-4)
     self.assertLess(total[1][1], 1e-4)
     self.assertLess(total[2][0], 1e-4)
     self.assertLess(total[2][1], 1e-4)
     self.assertLess(pair[1][2][0], 1e-4)
     self.assertLess(pair[1][2][1], 1e-4)
     self.assertLess(pair[2][1][0], 1e-4)
     self.assertLess(pair[2][1][1], 1e-4)
     self.assertGreater(total[1][0], 0.0)
     self.assertGreater(total[1][1], 0.0)
     self.assertGreater(total[2][0], 0.0)
     self.assertGreater(total[2][1], 0.0)
     self.assertGreater(pair[1][2][0], 0.0)
     self.assertGreater(pair[1][2][1], 0.0)
     self.assertGreater(pair[2][1][0], 0.0)
     self.assertGreater(pair[2][1][1], 0.0)
示例#14
0
    def __init__(self,filename='', path='', xtra_pore_data=None, xtra_throat_data=None,**kwargs):

        r"""
        """
        super(MatFile,self).__init__(**kwargs)
        if filename == '':
            return
        if path == '':
            path = os.path.abspath('.')
        self._path = path
        filepath = os.path.join(self._path,filename)
        self._xtra_pore_data=xtra_pore_data
        self._xtra_throat_data=xtra_throat_data
        self._dictionary=spio.loadmat(filepath)

        self._Np=sp.size(self._dictionary['pnumbering'])
        self._Nt=sp.size(self._dictionary['tnumbering'])

        #Run through generation steps
        self._add_pores()
        self._add_throats()
        self._remove_disconnected_clusters()
        self._add_xtra_pore_data()
        self._add_xtra_throat_data()
        self._add_geometry()
示例#15
0
  def convertToTimeAndFrequencyData(self, grain, target):
    d = self.sample_duration
    length = max(sp.shape(sp.arange(1, sp.size(target) - sp.size(self.gabor[1]), d)))
    scale = sp.zeros((88, length))
    datacapsule = sp.zeros((sp.shape(self.gabor)[1], grain))

    # 行列を束ねて処理
    #   個々にgabor*datadataを計算すると時間がかかる
    #   一気にdatadataを束ねるとメモリ消費量が半端ない
    #   よってdatadataに定義された数だけ束ねて処理
    m = 0
    datasize = sp.size(target) - sp.size(self.gabor[1])

    for k in sp.arange(1, datasize+1, d*grain):
      capsule_pointer = 0
      endl = k+d*grain

      if endl > datasize:
        endl = k + datasize%(d*grain)

      for l in sp.arange(k, endl, d):
        datadata = target[l:l+sp.size(self.gabor[1])]
        datacapsule[:, capsule_pointer] = datadata
        capsule_pointer += 1

      try:
        scale[:, m:m+grain] = sp.absolute(
            sp.dot(self.gabor,datacapsule[:, :capsule_pointer]))
      except ValueError:
        pass
      m += grain
    
    self.time_freq = scale
示例#16
0
文件: stats.py 项目: squireg/tcrm
def maxCellNum(gridLimit, gridSpace):
    """
    Get maximum cell number based on grid and grid space
    """
    latCells = size(arange(gridLimit['yMax'], gridLimit['yMin'], -gridSpace['y']))
    lonCells = size(arange(gridLimit['xMin'], gridLimit['xMax'], gridSpace['x']))

    return latCells*lonCells - 1
def slepian_bw(windows,Fs):
    l = s.size(windows,axis = 0)
    N = s.size(windows,axis = 1)
    avg = s.zeros(2*N-1)
    for i in range(l):
        w_norm = windows[i,:]/s.sqrt(s.sum(windows[i,:]**2))
        avg += s.signal.correlate(w_norm,w_norm)**2        
    return Fs/s.sum(avg/l)
示例#18
0
def off(matrix):
    h_size = sp.size(matrix, 1) // 3
    v_size = sp.size(matrix, 0) // 4
    size = sp.size(matrix, 0) * sp.size(matrix, 1)
    T = sp.zeros(size)
    for i in range(size):
        T[i] = matrix[(i // 12) // h_size + (i % 12) // 3 * v_size, (i // 12) % h_size + i % 3 * h_size]
    return T
示例#19
0
文件: pso.py 项目: HKou/pybrain
 def __init__(self, start):
     """Initialize a Particle at the given start vector."""
     self.dim = scipy.size(start)
     self.position = start
     self.velocity = scipy.zeros(scipy.size(start))
     self.bestPosition = scipy.zeros(scipy.size(start))
     self._fitness = None
     self.bestFitness = -scipy.inf
示例#20
0
    def folderinfo(self, path):
        """
        accept folder path and return tuple:
            - target path
            - item count
            - list of items
        :param path:
        :return:
        """
        loc = self.pstruct[1]
        for p in path.split(":"): #drill down to the folder specified in the path...
            try:
                loc[p]
            except:
                loc=loc[p.encode()]
            else:
                loc=loc[p]
        items=[]
        for key in loc.keys():
            try:
                key.decode()
            except:
                items.append(key)
            else:
                items.append(key.decode())

        waves=[]
        variables=[]
        strings=[]
        folders=[]
        for item in items:
            try:
                loc[item]
            except:
                if type(loc[item.encode()])==igor.record.wave.WaveRecord:
                    waves.append(item)
                if type(loc[item.encode()])==int or type(loc[item.encode()])==float or type(loc[item.encode()])==numpy.float64:
                    variables.append(item)
                if type(loc[item.encode()])==str:
                    strings.append(item)
                if type(loc[item.encode()])==dict:
                    folders.append(item)
            else:
                if type(loc[item])==igor.record.wave.WaveRecord:
                    waves.append(item)
                if type(loc[item])==int or type(loc[item])==float:
                    variables.append(item)
                if type(loc[item])==str:
                    strings.append(item)
                if type(loc[item])==dict:
                    folders.append(item)

        # waves = [item for item in items if type(loc[item.encode()])==igor.record.wave.WaveRecord]
        # variables = [item for item in items if type(loc[item.encode()])==int or type(item)==float]
        # strings = [item for item in items if type(loc[item.encode()])==str]
        # folders = [item for item in items if type(loc[item.encode()])==dict]

        return dict(fullpath=path,waves=(scipy.size(waves), waves),variables=(scipy.size(variables),variables),strings=(scipy.size(strings),strings),folders=(scipy.size(folders),folders))
示例#21
0
def connect_pores(network, pores1, pores2, labels=[], add_conns=True):
    r'''
    Returns the possible connections between two group of pores, and optionally
    makes the connections.

    Parameters
    ----------
    network : OpenPNM Network Object

    pores1 : array_like
        The first group of pores on the network

    pores2 : array_like
        The second group of pores on the network

    labels : list of strings
        The labels to apply to the new throats.  This argument is only needed
        if ``add_conns`` is True.

    add_conns : bool
        Indicates whether the connections should be added to the supplied
        network (default is True).  Otherwise, the connections are returned
        as an Nt x 2 array that can be passed directly to ``extend``.

    Notes
    -----
    It creates the connections in a format which is acceptable by
    the default OpenPNM connection ('throat.conns') and either adds them to
    the network or returns them.

    Examples
    --------
    >>> import OpenPNM
    >>> pn = OpenPNM.Network.TestNet()
    >>> pn.Nt
    300
    >>> pn.connect_pores(pores1=[22, 32], pores2=[16, 80, 68])
    >>> pn.Nt
    306
    >>> pn['throat.conns'][300:306]
    array([[16, 22],
           [22, 80],
           [22, 68],
           [16, 32],
           [32, 80],
           [32, 68]])

    '''
    size1 = _sp.size(pores1)
    size2 = _sp.size(pores2)
    array1 = _sp.repeat(pores1, size2)
    array2 = _sp.tile(pores2, size1)
    conns = _sp.vstack([array1, array2]).T
    if add_conns:
        extend(network=network, throat_conns=conns, labels=labels)
    else:
        return conns
示例#22
0
    def __init__(self, X, Y, reg=None):
        if size(shape(X)) is 1:
            X = reshape(X, (-1, 1))
        if size(shape(Y)) is 1:
            Y = reshape(Y, (-1, 1))
        if reg is None:
            reg = 0

        W1 = pinv(dot(X.T, X) + reg * eye(size(X, 1)))
        W2 = dot(X, W1)
        self.W = dot(Y.T, W2)
示例#23
0
    def generate(self,filename='standard_cubic_5x5x5.mat', path='LocalFiles', xtra_pore_data=None, xtra_throat_data=None):
        '''
        Create network from Matlab file. Returns OpenPNM.Network.GenericNetwork() object.

        Parameters
        ----------

        Critical\n
        filename : string
            filename = 'standard_cubic_5x5x5.mat' (default)\n
            Name of mat file\n
        path : string
            path='LocalFiles' (default)\n
            the location of the mat file on your computer \n
        xtra_pore_data : list of strings
            xtra_pore_data = ['type','shape','material']
            any additional props to look for in the dictionary
        xtra_throat_data : list of strings
            xtra_throat_data = ['type','shape','material']
            any additional props to look for in the dictionary

        Examples:
        ---------

        generate network using example mat file

        >>> import OpenPNM as PNM
        >>> pn=PNM.Geometry.MatFile(filename='standard_cubic_5x5x5.mat', path='LocalFiles')
        '''
        if path == 'LocalFiles':
            long_path = os.path.abspath(__file__)
            short_path, fname = os.path.split(long_path)
            short_path, foldername = os.path.split(short_path)  
            path, foldername = os.path.split(short_path)  
            path = os.path.join(path,'LocalFiles')
        self._path = path
        filepath = os.path.join(self._path,filename)
        self._xtra_pore_data=xtra_pore_data
        self._xtra_throat_data=xtra_throat_data
        self._dictionary=spio.loadmat(filepath)
        
        self._Np=sp.size(self._dictionary['pnumbering'])
        self._Nt=sp.size(self._dictionary['tnumbering'])
        
        #Run through generation steps
        self._add_pores()
        self._add_throats()
        self._add_geometry()
        self._add_xtra_pore_data()
        self._add_xtra_throat_data()
 
        return self
示例#24
0
def mcUnitConvergeEst(func,dims,minPoints,maxPoints,numTestPoints,testRuns):
	#Couldn't get this to work, spits out an answer near 0
	testPoints=sp.around(sp.linspace(minPoints,maxPoints,numTestPoints))
	error = sp.zeros(sp.size(testPoints))
	area = sp.zeros(testRuns)

	for i in range(0,numTestPoints):
		for k in range(0,testRuns):
			area[k]=mcUnit(func,testPoints[i],dims)
		error[i] = sp.mean(sp.absolute(area-sp.pi))

	estimate = la.lstsq(sp.vstack((sp.log(testPoints),sp.ones(sp.size(testPoints)))).T,sp.log(error))
	return estimate
示例#25
0
def permuteToBlocks2d(arr, blockheight, blockwidth):
    _height, width = arr.shape
    arr = arr.flatten()
    new = zeros(size(arr))
    for i in xrange(size(arr)):
        blockx = (i % width) / blockwidth
        blocky = i / width / blockheight
        blockoffset = blocky * width / blockwidth + blockx
        blockoffset *= blockwidth * blockheight
        inblockx = i % blockwidth
        inblocky = (i / width) % blockheight
        j = blockoffset + inblocky * blockwidth + inblockx
        new[j] = arr[i]
    return new
示例#26
0
def test_find_neighbor_throats():
    pn = OpenPNM.Network.Cubic(shape=(10, 10, 10))
    a = pn.find_neighbor_throats(pores=[])
    assert sp.size(a) == 0
    Pind = sp.zeros((pn.Np,), dtype=bool)
    Pind[[0, 1]] = True
    a = pn.find_neighbor_throats(pores=Pind)
    assert sp.all(a == [0, 1, 900, 901, 1800, 1801])
    a = pn.find_neighbor_throats(pores=[0, 2], mode='union')
    assert sp.all(a == [0, 1, 2, 900, 902, 1800, 1802])
    a = pn.find_neighbor_throats(pores=[0, 2], mode='intersection')
    assert sp.size(a) == 0
    a = pn.find_neighbor_throats(pores=[0, 2], mode='not_intersection')
    assert sp.all(a == [0, 1, 2, 900, 902, 1800, 1802])
示例#27
0
    def __init__(self, X, Y, rank, reg=None):
        if size(shape(X)) == 1:
            X = reshape(X, (-1, 1))
        if size(shape(Y)) == 1:
            Y = reshape(Y, (-1, 1))
        if reg is None:
            reg = 0
        self.rank = rank

        CXX = dot(X.T, X) + reg * eye(size(X, 1))
        CXY = dot(X.T, Y)
        _U, _S, V = svd(dot(CXY.T, dot(pinv(CXX), CXY)))
        self.W = V[0:rank, :].T
        self.A = dot(pinv(CXX), dot(CXY, self.W)).T
示例#28
0
    def num_neighbors(self, pores, flatten=False):
        r"""
        Returns an ndarray containing the number of neigbhor pores for each
        element in pores

        Parameters
        ----------
        pores : array_like
            Pores whose neighbors are to be counted
        flatten : boolean (optional)
            If False (default) the number pore neighbors for each input are
            returned as an array.  If True the sum total number of unique
            neighbors is counted, not including the input pores even if they
            neighbor each other.

        Returns
        -------
        num_neighbors : 1D array with number of neighbors in each element

        Examples
        --------
        >>> import OpenPNM
        >>> pn = OpenPNM.Network.TestNet()
        >>> pn.num_neighbors(pores=[0, 1], flatten=False)
        array([3, 4])
        >>> pn.num_neighbors(pores=[0, 1], flatten=True)
        5
        >>> pn.num_neighbors(pores=[0, 2], flatten=True)
        6
        """
        pores = sp.array(pores, ndmin=1)
        if pores.dtype == bool:
            pores = self.toindices(pores)
        if sp.size(pores) == 0:
            return sp.array([], ndmin=1, dtype=int)

        # Count number of neighbors
        if flatten:
            neighborPs = self.find_neighbor_pores(pores,
                                                  flatten=True,
                                                  mode='union',
                                                  excl_self=True)
            num = sp.shape(neighborPs)[0]
        else:
            neighborPs = self.find_neighbor_pores(pores, flatten=False)
            num = sp.zeros(sp.shape(neighborPs), dtype=int)
            for i in range(0, sp.shape(num)[0]):
                num[i] = sp.size(neighborPs[i])
        return num
示例#29
0
文件: stats.py 项目: squireg/tcrm
def validCellNum(cellNum, gridLimit, gridSpace):
    """
    Checks whether the given cell number is valid
    """
    if (cellNum < 0):
        return False

    latCells = size(arange(gridLimit['yMax'], gridLimit['yMin'], -gridSpace['y']))
    lonCells = size(arange(gridLimit['xMin'], gridLimit['xMax'], gridSpace['x']))

    numCells = latCells*lonCells - 1
    if (cellNum > numCells):
        return False
    else:
        return True
 def test_equal_clusters_estimate_all(self):
     # Smaller dimensionality and more data for reliable estimates
     cluster1 = self.rand.randn(8, 100000)
     cluster2 = self.rand.randn(8, 100000)
     clusterList1 = [cluster1[:, i] for i in xrange(sp.size(cluster1, 1))]
     clusterList2 = [cluster2[:, i] for i in xrange(sp.size(cluster2, 1))]
     total, pair = qa.overlap_fp_fn({1: clusterList1, 2: clusterList2})
     self.assertAlmostEqual(total[1][0], 0.5, 2)
     self.assertAlmostEqual(total[1][1], 0.5, 2)
     self.assertAlmostEqual(total[2][0], 0.5, 2)
     self.assertAlmostEqual(total[2][1], 0.5, 2)
     self.assertAlmostEqual(pair[1][2][0], 0.5, 2)
     self.assertAlmostEqual(pair[1][2][1], 0.5, 2)
     self.assertAlmostEqual(pair[2][1][0], 0.5, 2)
     self.assertAlmostEqual(pair[2][1][1], 0.5, 2)
示例#31
0
 def value(self,t,xs,ys):
     array_at_time = self.array_at_time(t)
     # plt.imshow(array_at_time, extent=self.simulation_region,
     # cmap=self.cmap,vmin=self.vmin,vmax=self.vmax)
     # print(scipy.shape(scipy.sum(array_at_time,0)))
     # print(scipy.where(scipy.sum(array_at_time,0)>0.01))
     # print(scipy.where(scipy.sum(array_at_time,1)>0.01))
     conc_list = scipy.zeros(scipy.size(xs))
     x_inds = scipy.floor(
     (xs-self.xmin)/(self.xmax-self.xmin)*self.grid_size[0])
     y_inds = scipy.floor(
     abs(ys-self.ymax)/(self.ymax-self.ymin)*self.grid_size[1])
     # print(x_inds,y_inds)
     # plt.show()
     for i,(x_ind,y_ind) in zip(range(len(xs)),zip(x_inds,y_inds)):
         try:
             conc_list[i] = array_at_time[y_ind,x_ind]
         except(IndexError):
             # print(x_ind,y_ind)
             conc_list[i]=0.
     return conc_list
def HMC_Stepper(epsilon, L, q_current):
    q = q_current
    p = scipy.random.normal(
        0.0, 1.0, scipy.size(q_current)
    )  #choose fictitious momentum variables that are normally distributed
    p_current = p
    #START LEAPFROG METHOD
    #make half step for momentum at the beginning of the Leapfrog method
    p = p - epsilon * UDE.gradV(q) / 2.0
    #alternate full steps for microscopic state and momentum
    for l in range(
            1, L
    ):  #We take L-1 steps, last step is done explicitly below the loop
        #make full step for the microscopic state
        q = q + epsilon * p
        #make full step for the momentum
        p = p - epsilon * UDE.gradV(q)
    #final (L-th) step: full step for q, half step for p
    q = q + epsilon * p
    p = p - epsilon * UDE.gradV(q) / 2.0
    #END LEAPFROG METHOD

    #negate momentum at end of trajectory to make the proposal symmetric
    p = -p
    #evaluate potential and kinetic energies at start and end of trajectory
    currentV = UDE.V(q_current)
    currentK = scipy.inner(p_current, p_current) / 2.0
    proposedV = UDE.V(q)
    proposedK = scipy.inner(p, p) / 2.0

    #Accept or reject the state at end of trajectory, returning either
    #the position at the end of the trajectory or the initial position
    U = math.log(scipy.random.uniform(
        0.0,
        1.0))  # generate uniform distributed random number and take logarithm
    testvalue = currentV - proposedV + currentK - proposedK
    if U > testvalue:
        #print("reject")
        q = q_current
    return q
示例#33
0
def vpd_calc(airtemp= scipy.array([]),\
             rh= scipy.array([])):
    '''
    Function to calculate vapour pressure deficit.

    Input:
        - airtemp: measured air temperatures [Celsius]
        - rh: (array of) rRelative humidity [%]
        
    Output:
        - vpd: (array of) vapour pressure deficits [Pa]
        
    Examples:
        >>> vpd_calc(30,60)
        1697.0903978626527
        >>> T=[20,25]
        >>> RH=[50,100]
        >>> vpd_calc(T,RH)
        array([ 1168.540099,   0.        ])
    '''

    # Determine length of array
    n = scipy.size(airtemp)
    # Check if we have a single value or an array
    if n < 2:  # Dealing with single value...
        # Calculate saturation vapour pressures
        es = es_calc(airtemp)
        eact = ea_calc(airtemp, rh)
        # Calculate vapour pressure deficit
        vpd = es - eact
    else:  # Dealing with an array
        # Initiate the output arrays
        vpd = scipy.zeros(n)
        # Calculate saturation vapor pressures
        es = es_calc(airtemp)
        eact = ea_calc(airtemp, rh)
        # Calculate vapour pressure deficit
        for i in range(0, n):
            vpd[i] = es[i] - eact[i]
    return vpd  # in hPa
示例#34
0
 def plot_primary_drainage_curve(self,
                                 pore_volume='volume',
                                 throat_volume='volume',
                                 pore_label='all',
                                 throat_label='all'):
     r"""
       Plot the primary drainage curve as the capillary pressure on ordinate
       and total saturation of the wetting phase on the abscissa.
       This is the preffered style in the petroleum engineering
       """
     try:
         PcPoints = sp.unique(self['pore.inv_Pc'])
     except:
         raise Exception(
             'Cannot print drainage curve: ordinary percolation simulation has not been run'
         )
     pores = self._net.pores(labels=pore_label)
     throats = self._net.throats(labels=throat_label)
     Snwp_t = sp.zeros_like(PcPoints)
     Snwp_p = sp.zeros_like(PcPoints)
     Snwp_all = sp.zeros_like(PcPoints)
     Swp_all = sp.zeros_like(PcPoints)
     Pvol = self._net['pore.' + pore_volume]
     Tvol = self._net['throat.' + throat_volume]
     Pvol_tot = sum(Pvol)
     Tvol_tot = sum(Tvol)
     for i in range(0, sp.size(PcPoints)):
         Pc = PcPoints[i]
         Snwp_p[i] = sum(Pvol[self._p_inv[pores] <= Pc]) / Pvol_tot
         Snwp_t[i] = sum(Tvol[self._t_inv[throats] <= Pc]) / Tvol_tot
         Snwp_all[i] = (sum(Tvol[self._t_inv[throats] <= Pc]) + sum(
             Pvol[self._p_inv[pores] <= Pc])) / (Tvol_tot + Pvol_tot)
         Swp_all[i] = 1 - Snwp_all[i]
     plt.plot(Swp_all, PcPoints, 'k.-')
     plt.xlim(xmin=0)
     plt.xlabel('Saturation of wetting phase')
     plt.ylabel('Capillary Pressure [Pa]')
     plt.title('Primay Drainage Curve')
     plt.grid(True)
     plt.show()
示例#35
0
def discretelognorm(xpoints, m, v):

    mu = sp.log(m**2 / float(sp.sqrt(v + m**2)))
    sigma = sp.sqrt(sp.log((v / float(m**2)) + 1))

    xmax = sp.amax(xpoints)
    xmin = sp.amin(xpoints)
    N = sp.size(xpoints)
    xincr = (xmax - xmin) / float(N - 1)

    binnodes = sp.arange(xmin + .5 * xincr, xmax + .5 * xincr, xincr)
    lnormcdf = lognorm.cdf(binnodes, sigma, 0, sp.exp(mu))
    discrpdf = sp.zeros((N, 1))
    for i in sp.arange(N):
        if i == 0:
            discrpdf[i] = lnormcdf[i]
        elif (i > 0) and (i < N - 1):
            discrpdf[i] = lnormcdf[i] - lnormcdf[i - 1]
        elif (i == N - 1):
            discrpdf[i] = discrpdf[i - 1]

    return discrpdf
示例#36
0
 def write_point_data(self):
     pore_keys = self._net.pore_properties.keys()
     num_pore_keys = sp.size(pore_keys)
     self._f.write('<PointData Scalars="pore_data">\n')
     for j in range(num_pore_keys):
         if pore_keys[j] != 'coords':
             self._f.write('<DataArray type="Float32" Name="')
             self._f.write(pore_keys[j])
             self._f.write('" format="ascii">\n')
             shape = np.shape(self._net.pore_properties[pore_keys[j]])
             if np.size(shape) == 1:
                 for i in range(self._net.get_num_pores()):
                     self._f.write(
                         str(self._net.pore_properties[pore_keys[j]][i]))
                     self._f.write(' ')
             else:
                 for i in range(self._net.get_num_pores()):
                     self._f.write(
                         str(self._net.pore_properties[pore_keys[j]][i][0]))
                     self._f.write(' ')
             self._f.write('\n</DataArray>\n')
     self._f.write('</PointData>\n')
示例#37
0
def sarfilter(H, signal):
    """
    image = sarfilter(H, signal);

    Apply the SAR filter H to the radar data signal
    using Fourier transforms.
    H should be a matched filter.

    Patrik Dammert 1998-02-23

    Smal modifications made to avoid alliasing effects.
    Björn Hallberg 2002-12-27
    get right linear convolution from ifft.
    Wiebke Aldenhoff 2016-05-24
    """
    rows, cols = np.shape(H)
    if (rows != 350) or (cols > 2000):
        print('Wrong size of filter input!')
        return

    image = np.zeros((350, 4096))

    # Choose between
    # 1 - Fourier transform of whole matrix (uses MUCH memory)
    # 2 - Fourier transform of row-by-row (uses little memory)
    choice = 2

    if choice == 1:
        # This implementation will be affected by alliasing
        image = ifftrows(fftrows(H)*fftrows(signal))
    elif choice == 2:
        for s in range(350):
            temp = npfft.ifft(npfft.fft(H[s], 8192)*npfft.fft(signal[s], 8192))
            # center portion of the convolution
            a = np.floor(np.size(H, axis=1)/2) + 1
            b = np.floor(sp.size(H, axis=2)/2) + 4096
            image[s] = temp[a:b]
    return image
示例#38
0
def map_to_regions(regions, values):
    r"""
    Maps pore values from a network onto the image from which it was extracted

    This function assumes that the pore numbering in the network has remained
    unchanged from the region labels in the partitioned image.

    Parameters
    ----------
    regions : ND-array
        An image of the pore space partitioned into regions and labeled

    values : array_like
        An array containing the numerical values to insert into each region.
        The value at location *n* will be inserted into the image where
        ``regions`` is *n+1*.  This mis-match is caused by the fact that 0's
        in the ``regions`` image is assumed to be the backgroung phase, while
        pore index 0 is valid.

    Returns
    -------
    image : ND-array
        A copy of the ``regions`` image, with with values in each region
        replaced by those specified by ``values``

    Notes
    -----
    This function assumes that the array of pore values are indexed starting
    at location 0, while in the region image 0's indicate background phase and
    the region indexing starts at 1.  That is, region 1 corresponds to pore 0.

    """
    values = sp.array(values).flatten()
    if sp.size(values) != regions.max() + 1:
        raise Exception('Number of values does not match number of regions')
    im = sp.zeros_like(regions)
    im = values[regions]
    return im
示例#39
0
def rbf_exe3(net, x):
    Nin = net.Nin
    Nmes = np.size(x, 1)
    Nhid = net.Nhid
    Nhid2 = net.Nhid2
    Nout = net.Nout
    Y1 = np.zeros((Nhid, Nmes))
    Y2 = np.zeros((Nout, Nmes))
    for i in range(Nin):
        for j in range(Nhid2):
            for k in range(Nhid):
                for l in range(Nout):
                    xi = x[i]
                    yi = xi
                    xj = yi * net.IW[i, j]
                    yj = basisfunc(xj, net.centers[i, j], func=net.func)
                    Y1[l] = Y1[l] + yj
                    xk = yj * net.HW[j, k]
                    yk = basisfunc(xk, net.centers[j, k], func=net.func)
                    xl = yk * net.OW[k, l]
                    yl = xl
                    Y2[l] = Y2[l] + yl
    return Y2, Y1
示例#40
0
def msini(a_AU, K_min=1, dur_yr=10, M_Msun=1):
    """Calculate the minimum detectable mass (in Jupiter masses) as a
    function of semi-major axis (in AU) and for a given minimum
    semi-amplitude (in m/s), stellar mass (in Solar masses) and survey
    duration (in years)."""
    a = a_AU * 1.49e11
    print a_AU
    dur = 365.0 * 3600.0 * 24.0 * dur_yr
    print dur_yr
    M = M_Msun * 1.989e30
    print M_Msun
    G = 6.67e-11
    P = 2 * scipy.pi * (a**3 / (G * M))**(1 / 2.)
    P_yr = P / (365.0 * 3600.0 * 24.0)
    print P_yr
    mlim = K_min * (M**2 * P / (2 * scipy.pi * G))**(1 / 3.)
    if scipy.size(mlim) > 1:
        mlim[P > dur] = scipy.nan
    else:
        if P > dur: mlim = scipy.nan
    m_mjup = mlim / 1.89e27
    print m_mjup
    return m_mjup
示例#41
0
def getHessAALogTime(amounts=None, gammas=None, numExps=3):
    """use this routine for the new paper"""
    if amounts is None:
        amounts = getAmounts(numExps)
    if gammas is None:
        gammas = getGammas(numExps)

    numExps = scipy.size(amounts)
    amountsLess1 = amounts[0:-1]
    gammasLess1 = gammas[0:-1]

    numerAA1 = scipy.array(scipy.outer(amountsLess1, amountsLess1))
    numerAA2 = scipy.array(
        scipy.outer(gammasLess1 + gammas[-1], scipy.ones(numExps - 1)))
    numerAA3 = scipy.array(
        scipy.outer(scipy.ones(numExps - 1), gammasLess1 + gammas[-1]))
    denomAA1 = scipy.array(
        scipy.outer(scipy.ones(numExps - 1), 2 * gammas[-1] * gammasLess1))
    denomAA2 = scipy.array(scipy.transpose(denomAA1) + denomAA1)
    hessAA = 2. * scipy.array(
        numerAA1 * scipy.log(old_div(numerAA2 * numerAA3, denomAA2)))

    return hessAA
示例#42
0
 def plot_drainage_curve(self,
                         pore_volume='volume',
                         throat_volume='volume',
                         pore_label='all',
                         throat_label='all'):
     r"""
       Plot drainage capillary pressure curve
       """
     try:
         PcPoints = sp.unique(self['pore.inv_Pc'])
     except:
         raise Exception(
             'Cannot print drainage curve: ordinary percolation simulation has not been run'
         )
     pores = self._net.pores(labels=pore_label)
     throats = self._net.throats(labels=throat_label)
     Snwp_t = sp.zeros_like(PcPoints)
     Snwp_p = sp.zeros_like(PcPoints)
     Pvol = self._net['pore.' + pore_volume]
     Tvol = self._net['throat.' + throat_volume]
     Pvol_tot = sum(Pvol)
     Tvol_tot = sum(Tvol)
     for i in range(0, sp.size(PcPoints)):
         Pc = PcPoints[i]
         Snwp_p[i] = sum(Pvol[self._p_inv[pores] <= Pc]) / Pvol_tot
         Snwp_t[i] = sum(Tvol[self._t_inv[throats] <= Pc]) / Tvol_tot
     if sp.mean(self._phase_inv["pore.contact_angle"]) < 90:
         Snwp_p = 1 - Snwp_p
         Snwp_t = 1 - Snwp_t
         PcPoints *= -1
     plt.plot(PcPoints, Snwp_p, 'r.-')
     plt.plot(PcPoints, Snwp_t, 'b.-')
     r'''
       TODO: Add legend to distinguish the pore and throat curves
       '''
     #plt.xlim(xmin=0)
     plt.show()
示例#43
0
def test_find_neighbor_pores():
    pn = OpenPNM.Network.Cubic(shape=(10, 10, 10))
    a = pn.find_neighbor_pores(pores=[])
    assert sp.size(a) == 0
    Pind = sp.zeros((pn.Np, ), dtype=bool)
    Pind[[0, 1]] = True
    a = pn.find_neighbor_pores(pores=Pind)
    assert sp.all(a == [2, 10, 11, 100, 101])
    a = pn.find_neighbor_pores(pores=[0, 2], mode='union')
    assert sp.all(a == [1, 3, 10, 12, 100, 102])
    a = pn.find_neighbor_pores(pores=[0, 2], mode='intersection')
    assert sp.all(a == [1])
    a = pn.find_neighbor_pores(pores=[0, 2], mode='not_intersection')
    assert sp.all(a == [3, 10, 12, 100, 102])
    a = pn.find_neighbor_pores(pores=[0, 2], mode='union', excl_self=False)
    assert sp.all(a == [0, 1, 2, 3, 10, 12, 100, 102])
    a = pn.find_neighbor_pores(pores=[0, 2],
                               mode='intersection',
                               excl_self=False)
    assert sp.all(a == [1])
    a = pn.find_neighbor_pores(pores=[0, 2],
                               mode='not_intersection',
                               excl_self=False)
    assert sp.all(a == [0, 2, 3, 10, 12, 100, 102])
    def run(self, npts=25, inv_points=None, access_limited=True, **kwargs):
        r"""
        Parameters
        ----------
        npts : int (default = 25)
            The number of pressure points to apply.  The list of pressures
            is logarithmically spaced between the lowest and highest throat
            entry pressures in the network.

        inv_points : array_like, optional
            A list of specific pressure point(s) to apply.

        """
        if 'inlets' in kwargs.keys():
            logger.info('Inlets recieved, passing to set_inlets')
            self.set_inlets(pores=kwargs['inlets'])
        if 'outlets' in kwargs.keys():
            logger.info('Outlets recieved, passing to set_outlets')
            self.set_outlets(pores=kwargs['outlets'])
        self._AL = access_limited
        if inv_points is None:
            logger.info('Generating list of invasion pressures')
            if self._percolation_type == 'bond':
                min_p = sp.amin(
                    self['throat.entry_pressure']) * 0.98  # nudge down
                max_p = sp.amax(
                    self['throat.entry_pressure']) * 1.02  # bump up
            else:
                min_p = sp.amin(
                    self['pore.entry_pressure']) * 0.98  # nudge down
                max_p = sp.amax(self['pore.entry_pressure']) * 1.02  # bump up
            inv_points = sp.logspace(sp.log10(min_p), sp.log10(max_p), npts)

        self._npts = sp.size(inv_points)
        # Execute calculation
        self._do_outer_iteration_stage(inv_points)
    def set_outlets(self, pores, defending_phase=None):
        r"""
        Specify outlet locations

        Parameters
        ----------
        pores : array_like
            The pores through which the defending phase exits the Network.

        defending_phase : OpenPNM Phase Object
            The Phase object defining the defending phase.  The defending Phase
            may be specified during the ``setup`` step, or through this method.
        """
        if defending_phase is not None:
            self._def_phase = defending_phase

        self._trapping = True

        Ps = sp.array(pores)
        if sp.size(Ps) > 0:
            if Ps.dtype == bool:
                Ps = self._net.Ps[Ps]
            self['pore.outlets'] = False
            self['pore.outlets'][Ps] = True
示例#46
0
def ra(z=float,\
       z0=float,\
       d=float,\
       u = scipy.array([])):
    '''
    Function to calculate the aerodynamic resistance 
    (in s/m) from windspeed and height/roughness values
    
    Input (measured at 2 m height):
        - z: measurement height [m]
        - z0: roughness length [m]
        - d: displacement length [m]
        - u: (array of) windspeed [m/s]


    Output:
        - ra: (array of) aerodynamic resistances [s/m]

    Examples:
        >>> ra(3,0.12,2.4,5.0)
        3.2378629924752942
        >>> u=([2,4,6])
        >>> ra(3,0.12,2.4,u)
        array([ 8.09465748,  4.04732874,  2.69821916])
    '''
    # Determine length of array
    l = scipy.size(u)
    # Check if we have a single value or an array
    if l < 2:  # Dealing with single value...
        ra = (scipy.log((z - d) / z0))**2 / (0.16 * u)
    else:  # Dealing with an array
        # Initiate output arrays
        ra = scipy.zeros(l)
        for i in range(0, l):
            ra[i] = (scipy.log((z - d) / z0))**2 / (0.16 * u[i])
    return ra  # aerodynamic resistanc in s/m
 def __init__(self, indent="\t",filename="dakota_tabular.dat",
              responsenames={"obj1":"current"}):
     r"""
     Constructor of the class
     - Reads and parses datafile
     """
     self._indent=indent
     
     print indent,"="*40
     print indent,"= Dakota tabular file parser ="
     print indent,"-"*40
     
     print indent,"= - Parse body of the data file (1st row skipped)"
     print indent,"=   Filename: ", filename
     self.myData=sp.genfromtxt(fname=filename,names=True)
     print indent,"= - The following columns have been detected"
     print indent,"=   ", self.myData.dtype.names
     
     self.DictParameters={}
     self.DictObjective={};iter1=0
     
     for item in self.myData.dtype.names:
         if item.find("obj") != -1:
             print indent, "=\t - Response detected: ", item
             if sp.size(responsenames)>iter1:
                 print indent, "=\t   - Response identified as: ", responsenames[iter1]
                 self.DictObjective[responsenames[iter1]] = item
             else:
                 print indent, "=\t   - Response not identified"
             iter1=iter1+1
         if item.find("obj") == -1:
             print indent, "=\t - Parameter detected: ", item
             self.DictParameters[item]=item
             
     self.DictAll=self.DictParameters.copy()
     self.DictAll.update(self.DictObjective)
示例#48
0
    def setup(self, conductance, quantity):
        r'''
        This setup provides the initial data for the solver
        '''
        if sp.size(self._phase) == 1:
            self._conductance = 'throat.' + conductance.split('.')[-1]
            self._quantity = 'pore.' + self._phase.name + '_' + quantity.split(
                '.')[-1]

            #Check health of conductance vector
            if self._phase.check_data_health(props=self._conductance,
                                             quiet=True):
                #If no nans, check for 0's
                ind = sp.nonzero(self._phase[self._conductance])[0]
                gmin = sp.amin(self._phase[self._conductance][ind])
                ind = sp.where(self._phase[self._conductance] == 0)[0]
                self['throat.conductance'] = self._phase[self._conductance]
                #To prevent singular matrix
                self['throat.conductance'][ind] = gmin / 1000000
            else:
                raise Exception('The provided throat conductance has problems')
        else:
            raise Exception(
                'The linear transport solver accepts just one phase.')
示例#49
0
    def getGlobalGradients(self, coords, IP=None):
        """ Input:  coords = global coordinates of shape nodes
                    IP = integration point
            Output: N_x = array of global shape gradients at given IP
                    w = w*j = weight at given IP """

        if coords.ndim > 1:
            if np.size(coords, axis=1) > self.ndim:
                raise ValueError("Element dimensions exceeded!")

        if IP is None:  # ==================================================
            N_x = []
            w = []
            for ip in range(self.nIP):
                [J, j] = self.getJacobian(coords, ip)  # J = N_xi * coords
                N_x.append(inverse(J).dot(self.N_xi[ip]))
                w.append(self.w[ip] * j)

        else:  # ===========================================================
            [J, j] = self.getJacobian(coords, IP)
            N_x = inverse(J).dot(self.N_xi[IP])
            w = self.w[IP] * j

        return [N_x, w]
示例#50
0
def __first_dim__(X):
    if sp.size(X) == 1:
        m = 1
    else:
        m = sp.size(X,0)
    return m
示例#51
0
def removeQuadOff(input_unw_path, width, length):

	ramp_removed_unw_path = "ramp_removed_" + input_unw_path[input_unw_path.rfind("/") + 1 : input_unw_path.rfind(".")] + ".unw";

	assert not os.path.exists(ramp_removed_unw_path), "\n***** ERROR: " + ramp_removed_unw_path + " already exists, exiting...\n";

	import subprocess;

	mag = input_unw_path[input_unw_path.rfind("/") + 1 : input_unw_path.rfind(".")] + ".mag";
	phs = input_unw_path[input_unw_path.rfind("/") + 1 : input_unw_path.rfind(".")] + ".phs";
	
	cmd  = "\nrmg2mag_phs " + input_unw_path + " " + mag + " " + phs + " " + width + "\n";
	cmd += "\nrm " + mag + "\n";
	subprocess.call(cmd,shell=True);

	infile = open(phs, "rb");

	import pylab;

	indat = pylab.fromfile(infile,pylab.float32,-1).reshape(int(length), int(width));
	#indat = pylab.fromfile(infile,pylab.float32,-1).reshape(int(width) * int(length), -1);

	infile.close();

	import scipy;

	x = scipy.arange(0, int(length));
	y = scipy.arange(0, int(width));

	import numpy;

	x_grid, y_grid = numpy.meshgrid(x, y);

	indices = numpy.arange(0, int(width) * int(length));

	mx = scipy.asarray(x_grid).reshape(-1);
	my = scipy.asarray(y_grid).reshape(-1);
	d  = scipy.asarray(indat).reshape(-1); 

	nonan_ids = indices[scipy.logical_not(numpy.isnan(d))];

	mx = mx[nonan_ids];
	my = my[nonan_ids];
	d  = d[nonan_ids];

	init_mx      = scipy.asarray(x_grid).reshape(-1)[nonan_ids];
	init_my      = scipy.asarray(y_grid).reshape(-1)[nonan_ids];
	#ramp_removed = scipy.asarray(indat).reshape(-1)[nonan_ids];
	ramp_removed = scipy.zeros(int(length) * int(width))[nonan_ids];
	init_m_ones  = scipy.ones(int(length) * int(width))[nonan_ids];

#	init_xs = [init_m_ones, init_mx, init_my, scipy.multiply(init_mx,init_my), scipy.power(init_mx,2), scipy.power(init_my,2)];
	init_xs = [init_mx];

	print(len(init_xs));

	p0 = scipy.zeros(len(init_xs));
	p  = scipy.zeros(len(init_xs));

	import scipy.optimize;

	for i in scipy.arange(0,10):

		m_ones = scipy.ones(scipy.size(mx));

#		xs     = [m_ones, mx, my, scipy.multiply(mx,my), scipy.power(mx,2), scipy.power(my,2)];
		xs     = [mx];

		G      = scipy.vstack(xs).T;
		print(mx);
		plsq   = scipy.optimize.leastsq(residuals, p0, args = (d, xs));

		res    = d - peval(xs, plsq[0]);
		mod    = plsq[0];

		p = p + mod;
		print(plsq[0]);

#		synth  = G * scipy.matrix(mod).T;
#		cutoff = res.std(axis=0,ddof=1);
		#print(cutoff);

#		indices  = numpy.arange(0, numpy.size(mx));
#		good_ids = indices[abs(res) <= cutoff];

#		plt.figure(i + 2);
#		plt.plot(mx,d,'b.',label='alloff');
#		plt.plot(mx[good_ids],synth[good_ids],'.',label='fit',color='lightgreen');
#		plt.plot(mx[bad_ids],d[bad_ids],'r.',label='cull #' + str(i + 1));
#		plt.legend();
 
#		mx = mx[good_ids];
#		my = my[good_ids];
#		d  = res[good_ids];

		d  = res;
		print(sum(res));

#		ramp_removed = scipy.asarray(ramp_removed - peval(init_xs, plsq[0]));
		ramp_removed = scipy.asarray(ramp_removed + peval(init_xs, plsq[0]));

	d = scipy.asarray(indat).reshape(-1);

	for i in range(0, scipy.size(nonan_ids)):
	
		d[nonan_ids[i]] = ramp_removed[i];

	ramp_removed = d.reshape(int(length), int(width));

#	import matplotlib;
#	matplotlib.pyplot.imshow(scipy.array(indat),interpolation='nearest',origin='lower');
#	matplotlib.pyplot.show();

	outfile = open(ramp_removed_unw_path, "wb");

	outoff = scipy.matrix(scipy.hstack((ramp_removed, ramp_removed)), scipy.float32);

	outoff.tofile(outfile);

	outfile.close();
示例#52
0
def norm2(a):
    """
    Mean squared norm.
    """
    return la.norm(a) / sp.size(a)
示例#53
0
        assert array_equal(
            Y,
            sp.array([[0., 0., 0.], [1., 1., 1.], [2., 2., 2.], [3., 3., 3.]]))

    if '## indices':

        assert array_equal(
            sp.indices((2, 3)),
            sp.array([[[0, 0, 0], [1, 1, 1]], [[0, 1, 2], [0, 1, 2]]]))

if '## size':

    # Get total number of elements:

    assert sp.zeros((2, 3, 4)).size == 24
    assert sp.size(sp.zeros((2, 3, 4))) == 24
    assert sp.size(1) == 1

    # 2 vs 1x2 vs 1x2:

    assert not sp.array_equal(
        [1, 2],  # number of dimensions: 1. size of dimension 1: 2
        [
            [1, 2]
        ]  #                       2.                    : 1 size of dimension 2: 2
    )

if '## shape':

    # Get / set size of each dimension.
示例#54
0
def cylinders(shape: List[int], radius: int, ncylinders: int,
              phi_max: float = 0, theta_max: float = 90, length: float = None):
    r"""
    Generates a binary image of overlapping cylinders.  This is a good
    approximation of a fibrous mat.

    Parameters
    ----------
    shape : list
        The size of the image to generate in [Nx, Ny, Nz] where N is the
        number of voxels. 2D images are not permitted.
    radius : scalar
        The radius of the cylinders in voxels
    ncylinders : scalar
        The number of cylinders to add to the domain. Adjust this value to
        control the final porosity, which is not easily specified since
        cylinders overlap and intersect different fractions of the domain.
    theta_max : scalar
        A value between 0 and 90 that controls the amount of rotation *in the*
        XY plane, with 0 meaning all cylinders point in the X-direction, and
        90 meaning they are randomly rotated about the Z axis by as much
        as +/- 90 degrees.
    phi_max : scalar
        A value between 0 and 90 that controls the amount that the cylinders
        lie *out of* the XY plane, with 0 meaning all cylinders lie in the XY
        plane, and 90 meaning that cylinders are randomly oriented out of the
        plane by as much as +/- 90 degrees.
    length : scalar
        The length of the cylinders to add.  If ``None`` (default) then the
        cylinders will extend beyond the domain in both directions so no ends
        will exist. If a scalar value is given it will be interpreted as the
        Euclidean distance between the two ends of the cylinder.  Note that
        one or both of the ends *may* still lie outside the domain, depending
        on the randomly chosen center point of the cylinder.

    Returns
    -------
    image : ND-array
        A boolean array with ``True`` values denoting the pore space
    """
    shape = sp.array(shape)
    if sp.size(shape) == 1:
        shape = sp.full((3, ), int(shape))
    elif sp.size(shape) == 2:
        raise Exception("2D cylinders don't make sense")
    if length is None:
        R = sp.sqrt(sp.sum(sp.square(shape))).astype(int)
    else:
        R = length/2
    im = sp.zeros(shape)
    # Adjust max angles to be between 0 and 90
    if (phi_max > 90) or (phi_max < 0):
        raise Exception('phi_max must be betwen 0 and 90')
    if (theta_max > 90) or (theta_max < 0):
        raise Exception('theta_max must be betwen 0 and 90')
    n = 0
    while n < ncylinders:
        # Choose a random starting point in domain
        x = sp.rand(3)*shape
        # Chose a random phi and theta within given ranges
        phi = (sp.pi/2 - sp.pi*sp.rand())*phi_max/90
        theta = (sp.pi/2 - sp.pi*sp.rand())*theta_max/90
        X0 = R*sp.array([sp.cos(phi)*sp.cos(theta),
                         sp.cos(phi)*sp.sin(theta),
                         sp.sin(phi)])
        [X0, X1] = [x + X0, x - X0]
        crds = line_segment(X0, X1)
        lower = ~sp.any(sp.vstack(crds).T < [0, 0, 0], axis=1)
        upper = ~sp.any(sp.vstack(crds).T >= shape, axis=1)
        valid = upper*lower
        if sp.any(valid):
            im[crds[0][valid], crds[1][valid], crds[2][valid]] = 1
            n += 1
    im = sp.array(im, dtype=bool)
    dt = spim.distance_transform_edt(~im) < radius
    return ~dt
示例#55
0
def generate_noise(shape: List[int], porosity=None, octaves: int = 3,
                   frequency: int = 32, mode: str = 'simplex'):
    r"""
    Generate a field of spatially correlated random noise using the Perlin
    noise algorithm, or the updated Simplex noise algorithm.

    Parameters
    ----------
    shape : array_like
        The size of the image to generate in [Nx, Ny, Nz] where N is the
        number of voxels.

    porosity : float
        If specified, this will threshold the image to the specified value
        prior to returning.  If no value is given (the default), then the
        scalar noise field is returned.

    octaves : int
        Controls the *texture* of the noise, with higher octaves giving more
        complex features over larger length scales.

    frequency : array_like
        Controls the relative sizes of the features, with higher frequencies
        giving larger features.  A scalar value will apply the same frequency
        in all directions, given an isotropic field; a vector value will
        apply the specified values along each axis to create anisotropy.

    mode : string
        Which noise algorithm to use, either ``'simplex'`` (default) or
        ``'perlin'``.

    Returns
    -------
    image : ND-array
        If porosity is given, then a boolean array with ``True`` values
        denoting the pore space is returned.  If not, then normally
        distributed and spatially correlated randomly noise is returned.

    Notes
    -----
    This method depends the a package called 'noise' which must be
    compiled. It is included in the Anaconda distribution, or a platform
    specific binary can be downloaded.

    See Also
    --------
    porespy.tools.norm_to_uniform

    """
    try:
        import noise
    except ModuleNotFoundError:
        raise Exception("The noise package must be installed")
    shape = sp.array(shape)
    if sp.size(shape) == 1:
        Lx, Ly, Lz = sp.full((3, ), int(shape))
    elif len(shape) == 2:
        Lx, Ly = shape
        Lz = 1
    elif len(shape) == 3:
        Lx, Ly, Lz = shape
    if mode == 'simplex':
        f = noise.snoise3
    else:
        f = noise.pnoise3
    frequency = sp.atleast_1d(frequency)
    if frequency.size == 1:
        freq = sp.full(shape=[3, ], fill_value=frequency[0])
    elif frequency.size == 2:
        freq = sp.concatenate((frequency, [1]))
    else:
        freq = sp.array(frequency)
    im = sp.zeros(shape=[Lx, Ly, Lz], dtype=float)
    for x in range(Lx):
        for y in range(Ly):
            for z in range(Lz):
                im[x, y, z] = f(x=x/freq[0], y=y/freq[1], z=z/freq[2],
                                octaves=octaves)
    im = im.squeeze()
    if porosity:
        im = norm_to_uniform(im, scale=[0, 1])
        im = im < porosity
    return im
示例#56
0
def overlapping_spheres(shape: List[int], radius: int, porosity: float,
                        iter_max: int = 10, tol: float = 0.01):
    r"""
    Generate a packing of overlapping mono-disperse spheres

    Parameters
    ----------
    shape : list
        The size of the image to generate in [Nx, Ny, Nz] where Ni is the
        number of voxels in the i-th direction.

    radius : scalar
        The radius of spheres in the packing.

    porosity : scalar
        The porosity of the final image, accurate to the given tolerance.

    iter_max : int
        Maximum number of iterations for the iterative algorithm that improves
        the porosity of the final image to match the given value.

    tol : float
        Tolerance for porosity of the final image compared to the given value.

    Returns
    -------
    image : ND-array
        A boolean array with ``True`` values denoting the pore space

    Notes
    -----
    This method can also be used to generate a dispersion of hollows by
    treating ``porosity`` as solid volume fraction and inverting the
    returned image.

    """
    shape = sp.array(shape)
    if sp.size(shape) == 1:
        shape = sp.full((3, ), int(shape))
    ndim = (shape != 1).sum()
    s_vol = ps_disk(radius).sum() if ndim == 2 else ps_ball(radius).sum()

    bulk_vol = sp.prod(shape)
    N = int(sp.ceil((1 - porosity)*bulk_vol/s_vol))
    im = sp.random.random(size=shape)

    # Helper functions for calculating porosity: phi = g(f(N))
    f = lambda N: spim.distance_transform_edt(im > N/bulk_vol) < radius
    g = lambda im: 1 - im.sum() / sp.prod(shape)

    # # Newton's method for getting image porosity match the given
    # w = 1.0                         # Damping factor
    # dN = 5 if ndim == 2 else 25     # Perturbation
    # for i in range(iter_max):
    #     err = g(f(N)) - porosity
    #     d_err = (g(f(N+dN)) - g(f(N))) / dN
    #     if d_err == 0:
    #         break
    #     if abs(err) <= tol:
    #         break
    #     N2 = N - int(err/d_err)   # xnew = xold - f/df
    #     N = w * N2 + (1-w) * N

    # Bisection search: N is always undershoot (bc. of overlaps)
    N_low, N_high = N, 4*N
    for i in range(iter_max):
        N = sp.mean([N_high, N_low], dtype=int)
        err = g(f(N)) - porosity
        if err > 0:
            N_low = N
        else:
            N_high = N
        if abs(err) <= tol:
            break

    return ~f(N)
示例#57
0
def lattice_spheres(shape: List[int], radius: int, offset: int = 0,
                    lattice: str = 'sc'):
    r"""
    Generates a cubic packing of spheres in a specified lattice arrangement

    Parameters
    ----------
    shape : list
        The size of the image to generate in [Nx, Ny, Nz] where N is the
        number of voxels in each direction.  For a 2D image, use [Nx, Ny].

    radius : scalar
        The radius of spheres (circles) in the packing

    offset : scalar
        The amount offset (+ or -) to add between sphere centers.

    lattice : string
        Specifies the type of lattice to create.  Options are:

        'sc' - Simple Cubic (default)

        'fcc' - Face Centered Cubic

        'bcc' - Body Centered Cubic

        For 2D images, 'sc' gives a square lattice and both 'fcc' and 'bcc'
        give a triangular lattice.

    Returns
    -------
    image : ND-array
        A boolean array with ``True`` values denoting the pore space
    """
    print(78*'―')
    print('lattice_spheres: Generating ' + lattice + ' lattice')
    r = radius
    shape = sp.array(shape)
    if sp.size(shape) == 1:
        shape = sp.full((3, ), int(shape))
    im = sp.zeros(shape, dtype=bool)
    im = im.squeeze()

    # Parse lattice type
    lattice = lattice.lower()
    if im.ndim == 2:
        if lattice in ['sc']:
            lattice = 'sq'
        if lattice in ['bcc', 'fcc']:
            lattice = 'tri'

    if lattice in ['sq', 'square']:
        spacing = 2*r
        s = int(spacing/2) + sp.array(offset)
        coords = sp.mgrid[r:im.shape[0]-r:2*s,
                          r:im.shape[1]-r:2*s]
        im[coords[0], coords[1]] = 1
    elif lattice in ['tri', 'triangular']:
        spacing = 2*sp.floor(sp.sqrt(2*(r**2))).astype(int)
        s = int(spacing/2) + offset
        coords = sp.mgrid[r:im.shape[0]-r:2*s,
                          r:im.shape[1]-r:2*s]
        im[coords[0], coords[1]] = 1
        coords = sp.mgrid[s+r:im.shape[0]-r:2*s,
                          s+r:im.shape[1]-r:2*s]
        im[coords[0], coords[1]] = 1
    elif lattice in ['sc', 'simple cubic', 'cubic']:
        spacing = 2*r
        s = int(spacing/2) + sp.array(offset)
        coords = sp.mgrid[r:im.shape[0]-r:2*s,
                          r:im.shape[1]-r:2*s,
                          r:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
    elif lattice in ['bcc', 'body cenetered cubic']:
        spacing = 2*sp.floor(sp.sqrt(4/3*(r**2))).astype(int)
        s = int(spacing/2) + offset
        coords = sp.mgrid[r:im.shape[0]-r:2*s,
                          r:im.shape[1]-r:2*s,
                          r:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
        coords = sp.mgrid[s+r:im.shape[0]-r:2*s,
                          s+r:im.shape[1]-r:2*s,
                          s+r:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
    elif lattice in ['fcc', 'face centered cubic']:
        spacing = 2*sp.floor(sp.sqrt(2*(r**2))).astype(int)
        s = int(spacing/2) + offset
        coords = sp.mgrid[r:im.shape[0]-r:2*s,
                          r:im.shape[1]-r:2*s,
                          r:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
        coords = sp.mgrid[r:im.shape[0]-r:2*s,
                          s+r:im.shape[1]-r:2*s,
                          s+r:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
        coords = sp.mgrid[s+r:im.shape[0]-r:2*s,
                          s:im.shape[1]-r:2*s,
                          s+r:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
        coords = sp.mgrid[s+r:im.shape[0]-r:2*s,
                          s+r:im.shape[1]-r:2*s,
                          s:im.shape[2]-r:2*s]
        im[coords[0], coords[1], coords[2]] = 1
    im = ~(spim.distance_transform_edt(~im) < r)
    return im
示例#58
0
def __second_dim__(X):
    if sp.size(X) == 1:
        m = 1
    else:
        m = sp.size(X,1)
    return  m
import scipy.stats
import scipy as sp
import pandas as pd

alpha = 0.05
output = []
for i in sp.arange(1, 36):
    dfDenominator = i
    for j in sp.arange(1, 11):
        dfNumerator = j
        f = scipy.stats.f.ppf(q=1 - alpha, dfn=dfNumerator, dfd=dfDenominator)
        output.append(round(f, 2))
        #print(f)

n = sp.size(output)
out = sp.reshape(output, [n / 10, 10])
rowNames = list(range(1, 36))
#index=list(range(1,36))
out2 = pd.DataFrame(out,
                    index=rowNames,
                    columns=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# output to screen
import sys
out2.to_csv(sys.stdout)

#out2=round(out,2)

#old_names = ['$a', '$b', '$c', '$d', '$e']
#new_names = ['a', 'b', 'c', 'd', 'e']
#df.rename(columns=dict(zip(old_names, new_names)), inplace=True)
示例#60
0
__author__ = 'fabio.lana'
import numpy as np
import pylab as plt
import scipy as sp
from scipy import stats
import win32api

scores = np.array([
    114, 100, 104, 89, 102, 91, 114, 114, 103, 105, 108, 130, 120, 132, 111,
    128, 118, 119, 86, 72, 111, 103, 74, 112, 107, 103, 98, 96, 112, 112, 93
])

xmean = sp.mean(scores)
sigma = sp.std(scores)
n = sp.size(scores)
print xmean, xmean - 2.576 * sigma / sp.sqrt(
    n), xmean + 2.576 * sigma / sp.sqrt(n)

result = sp.stats.bayes_mvs(scores)
print result