Python numpy.select() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def atLoc(mkNameFile,diction,inFol,outFol):
            mkArray = funcs.singleTifToArray(mkNameFile)
            outArray= np.zeros(mkArray.shape)
            
            for key in diction:
                dictVal = diction[key]
                keyArray = funcs.singleTifToArray(inFol + str(key) + "_rval.tif")
                
                condlist = [ mkArray == dictVal ]
                choicelist = [ keyArray ]
                outArray = np.select(condlist, choicelist, outArray)
                
            funcs.array_to_raster(mkNameFile,outArray, outFol+"rval_MK_name_sig2.tif")
                
            
#VERY INEFFICIENT -> deprecated:
#iterate over the best_pix Names of Mann Kendall and extract the rvals... of this combination
#return as own raster
#mkNameFile is the Mann-Kendall file to iterate over, inFol contains the rval rasters, 
# diction is a dictionary with short numbers as keys  (the ones in mkNameFile)
#    and long numbers as values
# noData is the value to be skipped as it does not indicate a real combination 

Example 2

def getLargVal(*inA):
    inputlen = len(inA)
    
    for i in range(inputlen):
        if i == 0:            
            condlist = [ inA[0] > inA[1] ]
            choicelist  = [ inA[0] ]
            result = np.select(condlist, choicelist, inA[1])
        elif i == 1:
            continue
        else:
            condlist = [ result > inA[i] ]
            choicelist  = [ result ]
            result = np.select(condlist, choicelist, inA[i])
    
    return result


#DEPRECATED: ONLY WORKS FOR UP TO 4 INPUT ARRAYS, OLD MANUAL WAY 

Example 3

def hsv_to_rgb(hsv):
    """ Input HSV image [0~1] return RGB image [0~255].

    Parameters
    -------------
    hsv : should be a numpy arrays with values between 0.0 and 1.0
    """
    # Translated from source of colorsys.hsv_to_rgb
    # h,s should be a numpy arrays with values between 0.0 and 1.0
    # v should be a numpy array with values between 0.0 and 255.0
    # hsv_to_rgb returns an array of uints between 0 and 255.
    rgb = np.empty_like(hsv)
    rgb[..., 3:] = hsv[..., 3:]
    h, s, v = hsv[..., 0], hsv[..., 1], hsv[..., 2]
    i = (h * 6.0).astype('uint8')
    f = (h * 6.0) - i
    p = v * (1.0 - s)
    q = v * (1.0 - s * f)
    t = v * (1.0 - s * (1.0 - f))
    i = i % 6
    conditions = [s == 0.0, i == 1, i == 2, i == 3, i == 4, i == 5]
    rgb[..., 0] = np.select(conditions, [v, q, p, p, t, v], default=v)
    rgb[..., 1] = np.select(conditions, [v, v, v, q, p, p], default=t)
    rgb[..., 2] = np.select(conditions, [v, p, t, v, v, q], default=p)
    return rgb.astype('uint8') 

Example 4

def process1(self, m79, hz):
    if len(m79) < 79:
        return

    snr = self.snr(m79)

    # convert to S/N. this seems to help, though I do not know why.
    mm = numpy.median(m79, 1)
    mm = numpy.select( [ mm > 0.000001 ], [ mm ], default=numpy.mean(mm))
    sm = numpy.stack([mm,mm,mm,mm,mm,mm,mm,mm], 1)
    m79 = numpy.divide(m79, sm)

    [ winmean, winstd, losemean, losestd ] = self.softstats(m79)
    dec = self.process2(m79, hz, snr, winmean, winstd, losemean, losestd)

    return dec 

Example 5

def getLargVal_man(*inA):
    inputlen = len(inA) 
    
    if inputlen == 2:
        condlist = [ inA[0] > inA[1] ]
        choicelist  = [ inA[0] ]
        result = np.select(condlist, choicelist, inA[1])
    
    elif inputlen == 3:
        condlist = [ np.logical_and(inA[0]>inA[1],inA[0]>inA[2]), 
                                    inA[1]>inA[2] ]
        choicelist  = [ inA[0], inA[1] ]
        result = np.select(condlist, choicelist, inA[2])        
        
        
    elif inputlen == 4: 
        condlist = [ np.logical_and(inA[0]>inA[1], 
                     np.logical_and(inA[0]>inA[2], inA[0]>inA[3])),
                     np.logical_and(inA[1]>inA[2], inA[1]>inA[3]),
                     inA[2]>inA[3] ]
        choicelist  = [ inA[0], inA[1], inA[2] ]
        result = np.select(condlist, choicelist, inA[3])
    
    else:
        print("Only up to 4 arrays supported")
    
    
    return result

#########################################################################################
#Array to Raster conversion, two ways
######################################################################################### 

Example 6

def rgb_to_hsv(rgb):
    """ Input RGB image [0~255] return HSV image [0~1].

    Parameters
    -------------
    rgb : should be a numpy arrays with values between 0 and 255.
    """
    # Translated from source of colorsys.rgb_to_hsv
    # r,g,b should be a numpy arrays with values between 0 and 255
    # rgb_to_hsv returns an array of floats between 0.0 and 1.0.
    rgb = rgb.astype('float')
    hsv = np.zeros_like(rgb)
    # in case an RGBA array was passed, just copy the A channel
    hsv[..., 3:] = rgb[..., 3:]
    r, g, b = rgb[..., 0], rgb[..., 1], rgb[..., 2]
    maxc = np.max(rgb[..., :3], axis=-1)
    minc = np.min(rgb[..., :3], axis=-1)
    hsv[..., 2] = maxc
    mask = maxc != minc
    hsv[mask, 1] = (maxc - minc)[mask] / maxc[mask]
    rc = np.zeros_like(r)
    gc = np.zeros_like(g)
    bc = np.zeros_like(b)
    rc[mask] = (maxc - r)[mask] / (maxc - minc)[mask]
    gc[mask] = (maxc - g)[mask] / (maxc - minc)[mask]
    bc[mask] = (maxc - b)[mask] / (maxc - minc)[mask]
    hsv[..., 0] = np.select(
        [r == maxc, g == maxc], [bc - gc, 2.0 + rc - bc], default=4.0 + gc - rc)
    hsv[..., 0] = (hsv[..., 0] / 6.0) % 1.0
    return hsv 

Example 7

def calc_correls(periods: ArrayLike, period_cond: float) -> np.ndarray:
    """Baker and Jayaram (2008, :cite:`baker08`) correlation model.

    Parameters
    ----------
    periods : array_like
        Periods at which the correlation should be computed.
    period_cond : float
        Conditioning period

    Returns
    -------
    correls : :class:`np.ndarray`
        Correlation coefficients
    """
    periods = np.asarray(periods)
    periods_min = np.minimum(periods, period_cond)
    periods_max = np.maximum(periods, period_cond)

    c_1 = (1 - np.cos(np.pi / 2 - 0.366 * np.log(periods_max / np.maximum(
        periods_min, 0.109))))

    c_2 = np.select([periods_max < 0.2, True], [
        1 - 0.105 * (1 - 1 / (1 + np.exp(100 * periods_max - 5))) *
        (periods_max - periods_min) / (periods_max - 0.0099), 0
    ])

    c_3 = np.select([periods_max < 0.109, True], [c_2, c_1])

    c_4 = (c_1 + 0.5 * (np.sqrt(c_3) - c_3) *
           (1 + np.cos(np.pi * periods_min / 0.109)))

    correls = np.select(
        [periods_max < 0.109, periods_min > 0.109, periods_max < 0.200, True],
        [c_2, c_1, np.minimum(c_2, c_4), c_4], )

    return correls 

Example 8

def forward(self, x):
        self.x = x
        self.y = np.select([x > 0], [x], 0)
        return self.y 

Example 9

def backward(self, d):
        return np.select([self.x > 0], [d], 0) 

Example 10

def squareFit(self,xReal,yReal):
		N=len(xReal)
		mx = yReal.max()
		mn = yReal.min()
		OFFSET = (mx+mn)/2.
		amplitude = (np.average(yReal[yReal>OFFSET]) - np.average(yReal[yReal<OFFSET]) )/2.0
		yTmp = np.select([yReal<OFFSET,yReal>OFFSET],[0,2])
		bools = abs(np.diff(yTmp))>1
		edges = xReal[bools]
		levels = yTmp[bools]
		frequency = 1./(edges[2]-edges[0])
		
		phase=edges[0]#.5*np.pi*((yReal[0]-offset)/amplitude)
		dc=0.5
		if len(edges)>=4:
			if levels[0]==0:
				dc = (edges[1]-edges[0])/(edges[2]-edges[0])
			else:
				dc = (edges[2]-edges[1])/(edges[3]-edges[1])
				phase = edges[1]

		guess = [amplitude, frequency, phase,dc,0]

		try:
			(amplitude, frequency, phase,dc,offset), pcov = self.optimize.curve_fit(self.squareFunc, xReal, yReal-OFFSET, guess)
			offset+=OFFSET

			if(frequency<0):
				#print ('negative frq')
				return False

			freq=1e6*abs(frequency)
			amp=abs(amplitude)
			pcov[0]*=1e6
			#print (pcov)
			if(abs(pcov[-1][0])>1e-6):
				False
			return [amp, freq, phase,dc,offset]
		except:
			return False 

Example 11

def squareFit(self,xReal,yReal):
		N=len(xReal)
		mx = yReal.max()
		mn = yReal.min()
		OFFSET = (mx+mn)/2.
		amplitude = (np.average(yReal[yReal>OFFSET]) - np.average(yReal[yReal<OFFSET]) )/2.0
		yTmp = np.select([yReal<OFFSET,yReal>OFFSET],[0,2])
		bools = abs(np.diff(yTmp))>1
		edges = xReal[bools]
		levels = yTmp[bools]
		frequency = 1./(edges[2]-edges[0])
		
		phase=edges[0]#.5*np.pi*((yReal[0]-offset)/amplitude)
		dc=0.5
		if len(edges)>=4:
			if levels[0]==0:
				dc = (edges[1]-edges[0])/(edges[2]-edges[0])
			else:
				dc = (edges[2]-edges[1])/(edges[3]-edges[1])
				phase = edges[1]

		guess = [amplitude, frequency, phase,dc,0]

		try:
			(amplitude, frequency, phase,dc,offset), pcov = self.optimize.curve_fit(self.squareFunc, xReal, yReal-OFFSET, guess)
			offset+=OFFSET

			if(frequency<0):
				#print ('negative frq')
				return False

			freq=1e6*abs(frequency)
			amp=abs(amplitude)
			pcov[0]*=1e6
			#print (pcov)
			if(abs(pcov[-1][0])>1e-6):
				False
			return [amp, freq, phase,dc,offset]
		except:
			return False 
点赞