def set_UVC(self, U, V, C=None): self.u = ma.asarray(U).ravel() self.v = ma.asarray(V).ravel() if C is not None: c = ma.asarray(C).ravel() x,y,u,v,c = delete_masked_points(self.x.ravel(), self.y.ravel(), self.u, self.v, c) else: x,y,u,v = delete_masked_points(self.x.ravel(), self.y.ravel(), self.u, self.v) magnitude = np.sqrt(u*u + v*v) flags, barbs, halves, empty = self._find_tails(magnitude, self.rounding, **self.barb_increments) #Get the vertices for each of the barbs plot_barbs = self._make_barbs(u, v, flags, barbs, halves, empty, self._length, self._pivot, self.sizes, self.fill_empty, self.flip) self.set_verts(plot_barbs) #Set the color array if C is not None: self.set_array(c) #Update the offsets in case the masked data changed xy = np.hstack((x[:,np.newaxis], y[:,np.newaxis])) self._offsets = xy
def recache(self, always=False): if always or self._invalidx: xconv = self.convert_xunits(self._xorig) if ma.isMaskedArray(self._xorig): x = ma.asarray(xconv, np.float_).filled(np.nan) else: x = np.asarray(xconv, np.float_) x = x.ravel() else: x = self._x if always or self._invalidy: yconv = self.convert_yunits(self._yorig) if ma.isMaskedArray(self._yorig): y = ma.asarray(yconv, np.float_).filled(np.nan) else: y = np.asarray(yconv, np.float_) y = y.ravel() else: y = self._y if len(x) == 1 and len(y) > 1: x = x * np.ones(y.shape, np.float_) if len(y) == 1 and len(x) > 1: y = y * np.ones(x.shape, np.float_) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') self._xy = np.empty((len(x), 2), dtype=np.float_) self._xy[:, 0] = x self._xy[:, 1] = y self._x = self._xy[:, 0] # just a view self._y = self._xy[:, 1] # just a view self._subslice = False if (self.axes and len(x) > 1000 and self._is_sorted(x) and self.axes.name == 'rectilinear' and self.axes.get_xscale() == 'linear' and self._markevery is None and self.get_clip_on() is True): self._subslice = True nanmask = np.isnan(x) if nanmask.any(): self._x_filled = self._x.copy() indices = np.arange(len(x)) self._x_filled[nanmask] = np.interp(indices[nanmask], indices[~nanmask], self._x[~nanmask]) else: self._x_filled = self._x if self._path is not None: interpolation_steps = self._path._interpolation_steps else: interpolation_steps = 1 xy = STEP_LOOKUP_MAP[self._drawstyle](*self._xy.T) self._path = Path(np.asarray(xy).T, None, interpolation_steps) self._transformed_path = None self._invalidx = False self._invalidy = False
def __init__(self,*args,**kwargs): TestCase.__init__(self,*args,**kwargs) self.presidents = [nan, 87, 82, 75, 63, 50, 43, 32, 35, 60, 54, 55, 36, 39,nan,nan, 69, 57, 57, 51, 45, 37, 46, 39, 36, 24, 32, 23, 25, 32,nan, 32, 59, 74, 75, 60, 71, 61, 71, 57, 71, 68, 79, 73, 76, 71, 67, 75, 79, 62, 63, 57, 60, 49, 48, 52, 57, 62, 61, 66, 71, 62, 61, 57, 72, 83, 71, 78, 79, 71, 62, 74, 76, 64, 62, 57, 80, 73, 69, 69, 71, 64, 69, 62, 63, 46, 56, 44, 44, 52, 38, 46, 36, 49, 35, 44, 59, 65, 65, 56, 66, 53, 61, 52, 51, 48, 54, 49, 49, 61,nan,nan, 68, 44, 40, 27, 28, 25, 24, 24] self.mdeaths = [2134,1863,1877,1877,1492,1249,1280,1131,1209,1492,1621, 1846,2103,2137,2153,1833,1403,1288,1186,1133,1053,1347, 1545,2066,2020,2750,2283,1479,1189,1160,1113, 970, 999, 1208,1467,2059,2240,1634,1722,1801,1246,1162,1087,1013, 959,1179,1229,1655,2019,2284,1942,1423,1340,1187,1098, 1004, 970,1140,1110,1812,2263,1820,1846,1531,1215,1075, 1056, 975, 940,1081,1294,1341] self.fdeaths = [901, 689, 827, 677, 522, 406, 441, 393, 387, 582, 578, 666, 830, 752, 785, 664, 467, 438, 421, 412, 343, 440, 531, 771, 767,1141, 896, 532, 447, 420, 376, 330, 357, 445, 546, 764, 862, 660, 663, 643, 502, 392, 411, 348, 387, 385, 411, 638, 796, 853, 737, 546, 530, 446, 431, 362, 387, 430, 425, 679, 821, 785, 727, 612, 478, 429, 405, 379, 393, 411, 487, 574] self.mdeaths = ma.asarray(self.mdeaths) self.fdeaths = ma.asarray(self.fdeaths)
def recache(self, always=False): if always or self._invalidx: xconv = self.convert_xunits(self._xorig) if ma.isMaskedArray(self._xorig): x = ma.asarray(xconv, np.float_) else: x = np.asarray(xconv, np.float_) x = x.ravel() else: x = self._x if always or self._invalidy: yconv = self.convert_yunits(self._yorig) if ma.isMaskedArray(self._yorig): y = ma.asarray(yconv, np.float_) else: y = np.asarray(yconv, np.float_) y = y.ravel() else: y = self._y if len(x) == 1 and len(y) > 1: x = x * np.ones(y.shape, np.float_) if len(y) == 1 and len(x) > 1: y = y * np.ones(x.shape, np.float_) if len(x) != len(y): raise RuntimeError("xdata and ydata must be the same length") x = x.reshape((len(x), 1)) y = y.reshape((len(y), 1)) if ma.isMaskedArray(x) or ma.isMaskedArray(y): self._xy = ma.concatenate((x, y), 1) else: self._xy = np.concatenate((x, y), 1) self._x = self._xy[:, 0] # just a view self._y = self._xy[:, 1] # just a view self._subslice = False if ( self.axes and len(x) > 100 and self._is_sorted(x) and self.axes.name == "rectilinear" and self.axes.get_xscale() == "linear" and self._markevery is None ): self._subslice = True if hasattr(self, "_path"): interpolation_steps = self._path._interpolation_steps else: interpolation_steps = 1 self._path = Path(self._xy, None, interpolation_steps) self._transformed_path = None self._invalidx = False self._invalidy = False
def _check(self, data): array = biggus.NumpyArrayAdapter(data) result = self.biggus_operator(array, axis=0).masked_array() expected = self.numpy_masked_operator(data, axis=0) if expected.ndim == 0: if expected is np.ma.masked: expected = ma.asarray(expected, dtype=array.dtype) else: expected = ma.asarray(expected) np.testing.assert_array_equal(result.filled(), expected.filled()) np.testing.assert_array_equal(result.mask, expected.mask)
def __new__(cls, data=None, name=None, mask=None, fill_value=None, dtype=None, shape=(), length=0, description=None, unit=None, format=None, meta=None, units=None, dtypes=None): if dtypes is not None: dtype = dtypes warnings.warn("'dtypes' has been renamed to the singular 'dtype'.", AstropyDeprecationWarning) if units is not None: unit = units warnings.warn("'units' has been renamed to the singular 'unit'.", AstropyDeprecationWarning) if data is None: dtype = (np.dtype(dtype).str, shape) self_data = ma.zeros(length, dtype=dtype) elif isinstance(data, (Column, MaskedColumn)): self_data = ma.asarray(data.data, dtype=dtype) if description is None: description = data.description if unit is None: unit = unit or data.unit if format is None: format = data.format if meta is None: meta = deepcopy(data.meta) if name is None: name = data.name elif isinstance(data, Quantity): if unit is None: self_data = ma.asarray(data, dtype=dtype) unit = data.unit else: self_data = ma.asarray(data.to(unit), dtype=dtype) else: self_data = ma.asarray(data, dtype=dtype) self = self_data.view(MaskedColumn) if mask is None and hasattr(data, 'mask'): mask = data.mask if fill_value is None and hasattr(data, 'fill_value'): fill_value = data.fill_value self.mask = mask self.fill_value = fill_value self._name = name self.unit = unit self.format = format self.description = description self.parent_table = None self.meta = meta return self
def _mann_whitney_u(x, y=None): """ Calculate the Mann-Whitney-U test. The Wilcoxon signed-rank test tests the null hypothesis that two related paired samples come from the same distribution. In particular, it tests whether the distribution of the differences x - y is symmetric about zero. It is a non-parametric version of the paired T-test. """ # A significance-dict for a two-tailed test with 0.05 confidence # from http://de.wikipedia.org/wiki/Wilcoxon-Mann-Whitney-Test significance_table = \ [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2], [0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8], [0, 0, 0, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14], [0, 0, 0, 0, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 17, 18, 19, 20], [0, 0, 0, 0, 0, 5, 6, 8, 10, 11, 13, 14, 16, 17, 19, 21, 22, 24, 25, 27], [0, 0, 0, 0, 0, 0, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34], [0, 0, 0, 0, 0, 0, 0, 13, 15, 17, 19, 22, 24, 26, 29, 31, 34, 36, 38, 41], [0, 0, 0, 0, 0, 0, 0, 0, 17, 20, 23, 26, 28, 31, 34, 37, 39, 42, 45, 48], [0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 26, 29, 33, 36, 39, 42, 45, 48, 52, 55], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 33, 37, 40, 44, 47, 51, 55, 58, 62], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 41, 45, 49, 53, 57, 61, 65, 69], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 50, 54, 59, 63, 67, 72, 76], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 55, 59, 64, 69, 74, 78, 83], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 70, 75, 80, 85, 90], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75, 81, 86, 92, 98], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87, 93, 99, 105], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 106, 112], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 113, 119], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127]] U, p = stats.mannwhitneyu(x, y) x = ma.asarray(x).compressed().view(numpy.ndarray) y = ma.asarray(y).compressed().view(numpy.ndarray) n1 = len(x) n2 = len(y) print n1, n2 if n1 > n2: tmp = n2 n2 = n1 n1 = tmp if n1 < 5 or n2 < 5: return 10000, 10000 if n1 < 20 or n2 < 20: print "WARNING: scipy.stat might not be accurate, p value is %f and significance according to table is %s" % \ (p, U <= significance_table[n1 - 1][n2 - 1]) return U, p
def test_masked_fill_value(self): cubes = [] y = (0, 2) cube = _make_cube((0, 2), y, 1) cube.data = ma.asarray(cube.data) cube.data.fill_value = 10 cubes.append(cube) cube = _make_cube((2, 4), y, 1) cube.data = ma.asarray(cube.data) cube.data.fill_value = 20 cubes.append(cube) result = concatenate(cubes) self.assertEqual(len(result), 2)
def sim(): np.random.seed(1) # Generate the time vector dt = 0.05 N = int(30 // dt) k = np.arange(N) t = k * dt # Instantiate the model given = dict( alpha=1, beta=-1, delta=0.2, gamma=0.3, omega=1, g1=0, g2=0.1, x_meas_std=0.1, x0=1, v0=0, x0_std=0.1, v0_std=0.1 ) q = GeneratedDTDuffing.pack('q', given) c = GeneratedDTDuffing.pack('c', given) params = dict(q=q, c=c, dt=dt) sampled = dict(t=t) model = GeneratedDTDuffing(params, sampled) # Simulate the system w = np.random.randn(N - 1, model.nw) x = np.zeros((N, model.nx)) x[0] = stats.multivariate_normal.rvs(model.x0(), model.Px0()) for k in range(N - 1): x[k + 1] = model.f(k, x[k]) + model.g(k, x[k]).dot(w[k]) # Sample the outputs R = model.R() v = np.random.multivariate_normal(np.zeros(model.ny), R, N) y = ma.asarray(model.h(k, x) + v) y[np.arange(N) % 4 != 0] = ma.masked return model, t, x, y, q
def to_rgba(self, x, alpha=None, bytes=False): '''Return a normalized rgba array corresponding to *x*. If *x* is already an rgb array, insert *alpha*; if it is already rgba, return it unchanged. If *bytes* is True, return rgba as 4 uint8s instead of 4 floats. ''' if alpha is None: _alpha = 1.0 else: _alpha = alpha try: if x.ndim == 3: if x.shape[2] == 3: if x.dtype == np.uint8: _alpha = np.array(_alpha*255, np.uint8) m, n = x.shape[:2] xx = np.empty(shape=(m,n,4), dtype = x.dtype) xx[:,:,:3] = x xx[:,:,3] = _alpha elif x.shape[2] == 4: xx = x else: raise ValueError("third dimension must be 3 or 4") if bytes and xx.dtype != np.uint8: xx = (xx * 255).astype(np.uint8) return xx except AttributeError: pass x = ma.asarray(x) x = self.norm(x) x = self.cmap(x, alpha=alpha, bytes=bytes) return x
def __call__(self, value, clip=None): if clip is None: clip = self.clip if cbook.iterable(value): vtype = 'array' val = ma.asarray(value).astype(np.float) else: vtype = 'scalar' val = ma.array([value]).astype(np.float) self.autoscale_None(val) vmin, vmax = self.vmin, self.vmax cmin, cmax = self.cmin * vmin, self.cmax * vmax if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin == vmax: result = 0.0 * val else: if clip: mask = ma.getmask(val) val = ma.array(np.clip(val.filled(vmax), vmin, vmax), mask=mask) result = 0. * val + 0.5 result[val > cmax] = (ma.log10(val[val > cmax]) - ma.log10(cmax)) / (np.log10(vmax) - np.log10(cmax)) / 2. + 0.5 result[val < cmin] = -(ma.log10(-val[val < cmin]) - ma.log10(-cmin)) / (np.log10(-vmin) - np.log10(-cmin)) / 2. + 0.5 if vtype == 'scalar': result = result[0] return result
def bodytrack(threadstuple, serv): #serv_M = ma.asarray([[0, 0, 0, 0, 6983544, 91465, 23131612, 171783], # [340208, 21308, 230049, 18250, 83687, 35901, 0, 0], # [0, 0, 0, 0, 0, 0, 22032093, 103155]]) serv_M = ma.asarray(serv) rout = [ [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 1], [0, 0, 1, 0]], [[39, 1, 0, 0], [0, 4, 1, 0], [1, 0, 1, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]] ] qt = list(islice(cycle((1,0)), serv_M.shape[1])) routL = map (lambda x: rout_insert_inter(normalizeRowWise(np.asarray(x))), rout) res = mva_multiclass (routL, 1./serv_M.T, threadstuple, qt) cont = (res[0][1::2] - serv_M.T[1::2]).T print "%.0f %.0f %.0f" % (cont[1,0], cont[0,2], cont[2,3]) return res
def __call__(self, value, clip=None, midpoint=None): if clip is None: clip = self.clip if cbook.iterable(value): vtype = 'array' val = ma.asarray(value).astype(np.float) else: vtype = 'scalar' val = ma.array([value]).astype(np.float) self.autoscale_None(val) vmin, vmax = self.vmin, self.vmax if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin==vmax: return 0.0 * val else: if clip: mask = ma.getmask(val) val = ma.array(np.clip(val.filled(vmax), vmin, vmax), mask=mask) result = (val-vmin) * (1.0/(vmax-vmin)) #result = (ma.arcsinh(val)-np.arcsinh(vmin))/(np.arcsinh(vmax)-np.arcsinh(vmin)) result = result**(1./self.nthroot) if vtype == 'scalar': result = result[0] return result
def process_value(value): """ Homogenize the input *value* for easy and efficient normalization. *value* can be a scalar or sequence. Returns *result*, *is_scalar*, where *result* is a masked array matching *value*. Float dtypes are preserved; integer types with two bytes or smaller are converted to np.float32, and larger types are converted to np.float. Preserving float32 when possible, and using in-place operations, can greatly improve speed for large arrays. Experimental; we may want to add an option to force the use of float32. """ if cbook.iterable(value): is_scalar = False result = ma.asarray(value) if result.dtype.kind == 'f': if isinstance(value, np.ndarray): result = result.copy() elif result.dtype.itemsize > 2: result = result.astype(np.float) else: result = result.astype(np.float32) else: is_scalar = True result = ma.array([value]).astype(np.float) return result, is_scalar
def smooth(self, Y): '''Run UKS Params ------ Y : [T, n_dim_state] array Y[t] = t obs If Y is a masked array and any Y[t] is masked, obs is assumed missing and ignored. Returns ------- smoothed_state_means : [T, n_dim_state] array filtered_state_means[t] = mean of t state distribution | [0, T-1] obs smoothed_state_covariances : [T, n_dim_state, n_dim_state] array filtered_state_covariances[t] = covariance of t state distribution | [0, T-1] obs ''' Y = ma.asarray(Y) (transition_functions, observation_functions, transition_covariance, observation_covariance, initial_state_mean, initial_state_covariance) = ( self._initialize_parameters() ) (filtered_state_means, filtered_state_covariances) = self.filter(Y) (smoothed_state_means, smoothed_state_covariances) = ( additive_unscented_smoother( filtered_state_means, filtered_state_covariances, transition_functions, transition_covariance ) ) return (smoothed_state_means, smoothed_state_covariances)
def detect_contour(img, level): """Returns list of vertices of contours at a given level Arguments: img (array): the image array level (number): the level at which to create the contour Returns: (list of nx2 arrays): list of list of vertices of the different contours Note: The contour detection is based on matplotlib's QuadContourGenerator """ #parameter mask = None; corner_mask = True; nchunk = 0; #prepare image data z = ma.asarray(img, dtype=np.float64); ny, nx = z.shape; x, y = np.meshgrid(np.arange(nx), np.arange(ny)); #find contour contour_generator = _contour.QuadContourGenerator(x, y, z.filled(), mask, corner_mask, nchunk) vertices = contour_generator.create_contour(level); return vertices;
def set_data(self, x, y, A): A = ma.asarray(A) if x is None: x = np.arange(0, A.shape[1] + 1, dtype=np.float64) else: x = np.asarray(x, np.float64).ravel() if y is None: y = np.arange(0, A.shape[0] + 1, dtype=np.float64) else: y = np.asarray(y, np.float64).ravel() if A.shape[:2] != (y.size - 1, x.size - 1): print A.shape print y.size print x.size raise ValueError("Axes don't match array shape") if A.ndim not in [2, 3]: raise ValueError("A must be 2D or 3D") if A.ndim == 3 and A.shape[2] == 1: A.shape = A.shape[:2] self.is_grayscale = False if A.ndim == 3: if A.shape[2] in [3, 4]: if (A[:, :, 0] == A[:, :, 1]).all() and (A[:, :, 0] == A[:, :, 2]).all(): self.is_grayscale = True else: raise ValueError("3D arrays must have RGB or RGBA as last dim") self._A = A self._Ax = x self._Ay = y self.update_dict["array"] = True
def __call__(self, value, clip=None): if clip is None: clip = self.clip if cbook.iterable(value): vtype = 'array' val = ma.asarray(value).astype(np.float) else: vtype = 'scalar' val = ma.array([value]).astype(np.float) self.autoscale_None(val) vmin, vmax = self.vmin, self.vmax if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin<=0: raise ValueError("values must all be positive") elif vmin==vmax: return 0.0 * val else: if clip: mask = ma.getmask(val) val = ma.array(np.clip(val.filled(vmax), vmin, vmax), mask=mask) result = (ma.log(val)-np.log(vmin))/(np.log(vmax)-np.log(vmin)) if vtype == 'scalar': result = result[0] return result
def _check_xyz(self, args): ''' For functions like contour, check that the dimensions of the input arrays match; if x and y are 1D, convert them to 2D using meshgrid. Possible change: I think we should make and use an ArgumentError Exception class (here and elsewhere). ''' x = self.ax.convert_xunits( args[0] ) y = self.ax.convert_yunits( args[1] ) x = np.asarray(x, dtype=np.float64) y = np.asarray(y, dtype=np.float64) z = ma.asarray(args[2], dtype=np.float64) if z.ndim != 2: raise TypeError("Input z must be a 2D array.") else: Ny, Nx = z.shape if x.shape == z.shape and y.shape == z.shape: return x,y,z if x.ndim != 1 or y.ndim != 1: raise TypeError("Inputs x and y must be 1D or 2D.") nx, = x.shape ny, = y.shape if nx != Nx or ny != Ny: raise TypeError("Length of x must be number of columns in z,\n" + "and length of y must be number of rows.") x,y = np.meshgrid(x,y) return x,y,z
def set_data(self, x, y, A): x = np.asarray(x, np.float32) y = np.asarray(y, np.float32) A = ma.asarray(A) if len(x.shape) != 1 or len(y.shape) != 1\ or A.shape[0:2] != (y.shape[0], x.shape[0]): raise TypeError("Axes don't match array shape") if len(A.shape) not in [2, 3]: raise TypeError("Can only plot 2D or 3D data") if len(A.shape) == 3 and A.shape[2] not in [1, 3, 4]: raise TypeError( "3D arrays must have three (RGB) or " "four (RGBA) color components" ) if len(A.shape) == 3 and A.shape[2] == 1: A.shape = A.shape[0:2] if len(A.shape) == 2: if A.dtype != np.uint8: A = (self.cmap(self.norm(A)) * 255).astype(np.uint8) else: A = np.repeat(A[:, :, np.newaxis], 4, 2) A[:, :, 3] = 255 else: if A.dtype != np.uint8: A = (255 * A).astype(np.uint8) if A.shape[2] == 3: B = np.zeros(tuple(list(A.shape[0:2]) + [4]), np.uint8) B[:, :, 0:3] = A B[:, :, 3] = 255 A = B self._A = A self._Ax = x self._Ay = y self._imcache = None
def _expected(self, transpose=False): data = self.data if transpose: data = self.data.T # Expected raster weights per target grid cell. # This is the (fractional) source cell contribution # to each target cell (out of 255) weights = np.array([[[63, 127, 127], # top left hand cell (tlhc) [127, 255, 255]], [[127, 127, 63], # top right hand cell (trhc) [255, 255, 127]], [[127, 255, 255], # bottom left hand cell (blhc) [63, 127, 127]], [[255, 255, 127], # bottom right hand cell (brhc) [127, 127, 63]]], dtype=np.uint8) weights = weights / 255 # Expected source points per target grid cell. tmp = data[1:-1, 1:-1] shape = (-1, 2, 3) cells = [tmp[slice(0, 2), slice(0, 3)].reshape(shape), # tlhc tmp[slice(0, 2), slice(3, None)].reshape(shape), # trhc tmp[slice(2, None), slice(0, 3)].reshape(shape), # blhc tmp[slice(2, None), slice(3, None)].reshape(shape)] # brhc cells = ma.vstack(cells) # Expected fractional weighted result. num = (cells * weights).sum(axis=(1, 2)) dom = weights.sum(axis=(1, 2)) expected = num / dom expected = ma.asarray(expected.reshape(2, 2)) if transpose: expected = expected.T return expected
def __call__(self, value, clip=None): if clip is None: clip = self.clip if cbook.iterable(value): vtype = 'array' val = ma.asarray(value).astype(numpy.float) else: vtype = 'scalar' val = ma.array([value]).astype(numpy.float) if self.staticrange is None: self.autoscale_None(val) vmin, vmax = self.vmin, self.vmax else: self.vmin, self.vmax = None, None self.autoscale_None(val) vmin, vmax = self.vmax - self.staticrange, self.vmax if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin==vmax: result = 0.0 * val else: vmin = float(vmin) vmax = float(vmax) rmin = float(self.rmin) rmax = float(self.rmax) if clip: mask = ma.getmask(val) val = ma.array(np.clip(val.filled(vmax), vmin, vmax), mask=mask) result = (val-vmin) * ((rmax-rmin) / (vmax-vmin)) + rmin if vtype == 'scalar': result = result[0] return result
def _check(self, data): array = biggus.NumpyArrayAdapter(data) result = std(array, axis=0, ddof=0).masked_array() expected = ma.std(data, axis=0, ddof=0) if expected.ndim == 0: expected = ma.asarray(expected) np.testing.assert_array_equal(result.filled(), expected.filled()) np.testing.assert_array_equal(result.mask, expected.mask)
def test_concat_masked_2x2d(self): cubes = [] y = (0, 2) cube = _make_cube((0, 2), y, 1) cube.data = ma.asarray(cube.data) cube.data[(0, 1), (0, 1)] = ma.masked cubes.append(cube) cube = _make_cube((2, 4), y, 2) cube.data = ma.asarray(cube.data) cube.data[(0, 1), (1, 0)] = ma.masked cubes.append(cube) result = concatenate(cubes) self.assertCML(result, ("concatenate", "concat_masked_2x2d.cml")) self.assertEqual(len(result), 1) self.assertEqual(result[0].shape, (2, 4)) mask = np.array([[True, False, False, True], [False, True, True, False]], dtype=np.bool) self.assertArrayEqual(result[0].data.mask, mask)
def _make_verts(self, U, V): uv = ma.asarray(U+V*1j) a = ma.absolute(uv) if self.scale is None: sn = max(10, math.sqrt(self.N)) scale = 1.8 * a.mean() * sn / self.span # crude auto-scaling self.scale = scale length = a/(self.scale*self.width) X, Y = self._h_arrows(length) # There seems to be a ma bug such that indexing # a masked array with one element converts it to # an ndarray. theta = np.angle(ma.asarray(uv[..., np.newaxis]).filled(0)) xy = (X+Y*1j) * np.exp(1j*theta)*self.width xy = xy[:,:,np.newaxis] XY = ma.concatenate((xy.real, xy.imag), axis=2) return XY
def to_rgba(self, x, alpha=None, bytes=False): """ Return a normalized rgba array corresponding to *x*. In the normal case, *x* is a 1-D or 2-D sequence of scalars, and the corresponding ndarray of rgba values will be returned, based on the norm and colormap set for this ScalarMappable. There is one special case, for handling images that are already rgb or rgba, such as might have been read from an image file. If *x* is an ndarray with 3 dimensions, and the last dimension is either 3 or 4, then it will be treated as an rgb or rgba array, and no mapping will be done. If the last dimension is 3, the *alpha* kwarg (defaulting to 1) will be used to fill in the transparency. If the last dimension is 4, the *alpha* kwarg is ignored; it does not replace the pre-existing alpha. A ValueError will be raised if the third dimension is other than 3 or 4. In either case, if *bytes* is *False* (default), the rgba array will be floats in the 0-1 range; if it is *True*, the returned rgba array will be uint8 in the 0 to 255 range. Note: this method assumes the input is well-behaved; it does not check for anomalies such as *x* being a masked rgba array, or being an integer type other than uint8, or being a floating point rgba array with values outside the 0-1 range. """ # First check for special case, image input: try: if x.ndim == 3: if x.shape[2] == 3: if alpha is None: alpha = 1 if x.dtype == np.uint8: alpha = np.uint8(alpha * 255) m, n = x.shape[:2] xx = np.empty(shape=(m, n, 4), dtype=x.dtype) xx[:, :, :3] = x xx[:, :, 3] = alpha elif x.shape[2] == 4: xx = x else: raise ValueError("third dimension must be 3 or 4") if bytes and xx.dtype != np.uint8: xx = (xx * 255).astype(np.uint8) if not bytes and xx.dtype == np.uint8: xx = xx.astype(float) / 255 return xx except AttributeError: # e.g., x is not an ndarray; so try mapping it pass # This is the normal case, mapping a scalar array: x = ma.asarray(x) x = self.norm(x) x = self.cmap(x, alpha=alpha, bytes=bytes) return x
def RhoMask(RomsNC, latbounds, lonbounds) : """ defines mask on rho points only, without time dimension """ Rholats = (RomsNC.variables['lat_rho'][:] >= latbounds[0])*(RomsNC.variables['lat_rho'][:] <= latbounds[1]) Rholons = (RomsNC.variables['lon_rho'][:] >= lonbounds[0])*(RomsNC.variables['lon_rho'][:] <= lonbounds[1]) Rho_Mask = ma.asarray(np.invert(Rholats*Rholons)) return Rho_Mask
def test_masked_and_unmasked(self): cubes = [] y = (0, 2) cube = _make_cube((2, 4), y, 2) cube.data = ma.asarray(cube.data) cubes.append(cube) cubes.append(_make_cube((0, 2), y, 1)) result = concatenate(cubes) self.assertEqual(len(result), 1)
def test_concat_masked_2x2d(self): cubes = [] y = (0, 2) cube = _make_cube((0, 2), y, 1) cube.data = ma.asarray(cube.data) cube.data[(0, 1), (0, 1)] = ma.masked cubes.append(cube) cube = _make_cube((2, 4), y, 2) cube.data = ma.asarray(cube.data) cube.data[(0, 1), (1, 0)] = ma.masked cubes.append(cube) result = concatenate(cubes) self.assertCML(result, ('concatenate', 'concat_masked_2x2d.cml')) self.assertEqual(len(result), 1) self.assertEqual(result[0].shape, (2, 4)) mask = np.array([[True, False, False, True], [False, True, True, False]], dtype=np.bool) self.assertArrayEqual(result[0].data.mask, mask)
def test_masked_vs_unmasked(self): cubes = [] y = (0, 2) cubes.append(_make_cube((0, 2), y, 1)) cube = _make_cube((2, 4), y, 2) cube.data = ma.asarray(cube.data) cubes.append(cube) result = concatenate(cubes) self.assertEqual(len(result), 2)
def inverse(self, value): if not self.scaled(): raise ValueError("Not invertible until scaled") vmin, vmax = self.vmin, self.vmax if cbook.iterable(value): val = ma.asarray(value) return vmin * ma.power((vmax / vmin), val) else: return vmin * pow((vmax / vmin), value)
def _make_verts(self, U, V): uv = ma.asarray(U+V*1j) a = ma.absolute(uv) if self.scale is None: sn = max(10, math.sqrt(self.N)) scale = 1.8 * a.mean() * sn / self.span # crude auto-scaling self.scale = scale length = a/(self.scale*self.width) X, Y = self._h_arrows(length) if self.angles == 'xy': theta = self._angles(U, V).filled(0)[:,np.newaxis] elif self.angles == 'uv': theta = np.angle(ma.asarray(uv[..., np.newaxis]).filled(0)) else: theta = ma.asarray(self.angles*np.pi/180.0).filled(0) xy = (X+Y*1j) * np.exp(1j*theta)*self.width xy = xy[:,:,np.newaxis] XY = ma.concatenate((xy.real, xy.imag), axis=2) return XY
def _parse_args(self, *args): X, Y, U, V, C = [None] * 5 args = list(args) if len(args) == 3 or len(args) == 5: C = ma.asarray(args.pop(-1)) V = ma.asarray(args.pop(-1)) U = ma.asarray(args.pop(-1)) if U.ndim == 1: nr, nc = 1, U.shape[0] else: nr, nc = U.shape if len(args) == 2: # remaining after removing U,V,C X, Y = [np.array(a).ravel() for a in args] if len(X) == nc and len(Y) == nr: X, Y = [a.ravel() for a in np.meshgrid(X, Y)] else: indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) X, Y = [np.ravel(a) for a in indexgrid] return X, Y, U, V, C
def _make_verts(self, U, V): uv = ma.asarray(U + V * 1j) a = ma.absolute(uv) if self.scale is None: sn = max(10, math.sqrt(self.N)) scale = 1.8 * a.mean() * sn / self.span # crude auto-scaling self.scale = scale length = a / (self.scale * self.width) X, Y = self._h_arrows(length) if self.angles == 'xy': theta = self._angles(U, V).filled(0)[:, np.newaxis] elif self.angles == 'uv': theta = np.angle(ma.asarray(uv[..., np.newaxis]).filled(0)) else: theta = ma.asarray(self.angles * np.pi / 180.0).filled(0) xy = (X + Y * 1j) * np.exp(1j * theta) * self.width xy = xy[:, :, np.newaxis] XY = ma.concatenate((xy.real, xy.imag), axis=2) return XY
def unmasked_data_as_1d_array(array): array = ma.asarray(array) if array.ndim == 0: if array.mask: data = np.array([]) else: data = np.array([array.data]) else: data = array.data[~ma.getmaskarray(array)] return data
def __call__(self, X, alpha=1.0, bytes=False): """ *X* is either a scalar or an array (of any dimension). If scalar, a tuple of rgba values is returned, otherwise an array with the new shape = oldshape+(4,). If the X-values are integers, then they are used as indices into the array. If they are floating point, then they must be in the interval (0.0, 1.0). Alpha must be a scalar. If bytes is False, the rgba values will be floats on a 0-1 scale; if True, they will be uint8, 0-255. """ if not self._isinit: self._init() alpha = min(alpha, 1.0) # alpha must be between 0 and 1 alpha = max(alpha, 0.0) self._lut[:-3, -1] = alpha mask_bad = None if not cbook.iterable(X): vtype = 'scalar' xa = np.array([X]) else: vtype = 'array' xma = ma.asarray(X) xa = xma.filled(0) mask_bad = ma.getmask(xma) if xa.dtype.char in np.typecodes['Float']: np.putmask(xa, xa == 1.0, 0.9999999) #Treat 1.0 as slightly less than 1. # The following clip is fast, and prevents possible # conversion of large positive values to negative integers. if NP_CLIP_OUT: np.clip(xa * self.N, -1, self.N, out=xa) else: xa = np.clip(xa * self.N, -1, self.N) xa = xa.astype(int) # Set the over-range indices before the under-range; # otherwise the under-range values get converted to over-range. np.putmask(xa, xa > self.N - 1, self._i_over) np.putmask(xa, xa < 0, self._i_under) if mask_bad is not None and mask_bad.shape == xa.shape: np.putmask(xa, mask_bad, self._i_bad) if bytes: lut = (self._lut * 255).astype(np.uint8) else: lut = self._lut rgba = np.empty(shape=xa.shape + (4, ), dtype=lut.dtype) lut.take(xa, axis=0, mode='clip', out=rgba) # twice as fast as lut[xa]; # using the clip or wrap mode and providing an # output array speeds it up a little more. if vtype == 'scalar': rgba = tuple(rgba[0, :]) return rgba
def _parse_args(self, *args): X, Y, U, V, C = [None] * 5 args = list(args) if len(args) == 3 or len(args) == 5: C = ma.asarray(args.pop(-1)).ravel() V = ma.asarray(args.pop(-1)) U = ma.asarray(args.pop(-1)) nn = np.shape(U) nc = nn[0] nr = 1 if len(nn) > 1: nr = nn[1] if len(args) == 2: # remaining after removing U,V,C X, Y = [np.array(a).ravel() for a in args] if len(X) == nc and len(Y) == nr: X, Y = [a.ravel() for a in np.meshgrid(X, Y)] else: indexgrid = np.meshgrid(np.arange(nc), np.arange(nr)) X, Y = [np.ravel(a) for a in indexgrid] return X, Y, U, V, C
def inverse(self, value): if not self.scaled(): raise ValueError("Not invertible until scaled") gamma = self.gamma vmin, vmax = self.vmin, self.vmax if cbook.iterable(value): val = ma.asarray(value) return ma.power(value, 1. / gamma) * (vmax - vmin) + vmin else: return pow(value, 1. / gamma) * (vmax - vmin) + vmin
def inverse(self, value): if not self.scaled(): raise ValueError("Not invertible until scaled") vmin = float(self.vmin) vmax = float(self.vmax) if cbook.iterable(value): val = ma.asarray(value) return vmin + val * (vmax - vmin) else: return vmin + value * (vmax - vmin)
def test_neural_network(data, model=None): y = asarray(data) x = arange(len(data)) y_train, y_test, x_train, x_test = train_test_split(y, x, test_size=0.33) guesser = Predictioner(model_input=model) guesser.update_input(train_x=x_train, train_y=y_train) history = guesser.fit_model(verbose=0) eva = guesser.evaluate(y_test, x_test) guesser.predict(x) guesser.visualize() return eva[0]
def sample(self, n_timesteps, initial_state=None, random_state=None): '''Sample from model defined by the Unscented Kalman Filter Parameters ---------- n_timesteps : int number of time steps initial_state : optional, [n_dim_state] array initial state. If unspecified, will be sampled from initial state distribution. random_state : optional, int or Random random number generator ''' (transition_functions, observation_functions, transition_covariance, observation_covariance, initial_state_mean, initial_state_covariance) = (self._initialize_parameters()) n_dim_state = transition_covariance.shape[-1] n_dim_obs = observation_covariance.shape[-1] # logic for instantiating rng if random_state is None: rng = check_random_state(self.random_state) else: rng = check_random_state(random_state) # logic for selecting initial state if initial_state is None: initial_state = rng.multivariate_normal(initial_state_mean, initial_state_covariance) # logic for generating samples x = np.zeros((n_timesteps, n_dim_state)) z = np.zeros((n_timesteps, n_dim_obs)) for t in range(n_timesteps): if t == 0: x[0] = initial_state else: transition_function = (_last_dims(transition_functions, t - 1, ndims=1)[0]) transition_noise = (rng.multivariate_normal( np.zeros(n_dim_state), transition_covariance.newbyteorder('='))) x[t] = transition_function(x[t - 1], transition_noise) observation_function = (_last_dims(observation_functions, t, ndims=1)[0]) observation_noise = (rng.multivariate_normal( np.zeros(n_dim_obs), observation_covariance.newbyteorder('='))) z[t] = observation_function(x[t], observation_noise) return (x, ma.asarray(z))
def __init__(self, *members, **kwargs): ArrayChain.__init__(self, *members, **kwargs) self.combined = np.empty(self.shape) # adapted copy-paste from ArrayChain.__init__ if not members[1:]: members = members[0] kwargs = dict(zip(members[0::2], members[1::2])) # if any([isinstance(v, ma.MaskedArray) for v in kwargs.values()]): self.combined = ma.asarray(self.combined) for k in kwargs: self[k] = kwargs[k]
def inverse(self, value): if not self.scaled(): raise ValueError("Not invertible until scaled") vmin, vmax = self.vmin, self.vmax if cbook.iterable(value): val = ma.asarray(value) val = val * (self._upper - self._lower) + self._lower return self.invfn(val) else: value = value * (self._upper - self._lower) + self._lower return self.invfn(value)
def generate_nearly_covered_modis_lst(area, doy, lat1, lat2, lon1, lon2): in_path = os.path.join("Data", "MOD11A1", "3km") out_path = get_out_path( os.path.join("Data", "MOD11A1", "3km_nearly_overlapped")) log_file = os.path.join(out_path, 'log.txt') sys.stdout = Logger(log_file) print(area, doy) if doy + ".nc" in os.listdir( os.path.join("Data", "Sentinel", "usa_filtered")): fh_sentinel = Dataset( os.path.join("Data", "Sentinel", "usa_filtered", doy + ".nc"), "r") elif doy + ".nc" in os.listdir( os.path.join("Data", "Sentinel", "usa_filtered_real_gap")): fh_sentinel = Dataset( os.path.join("Data", "Sentinel", "usa_filtered_real_gap", doy + ".nc"), "r") lat_indices, lon_indices = select_area(lat1, lat2, lon1, lon2, "M03") lats, lons = get_lat_lon("M03") assert (len(lats) != 0 and len(lons) != 0) lats = lats[lat_indices[0]:lat_indices[1]] lons = lons[lon_indices[0]:lon_indices[1]] i_lat_start, i_lat_end, i_lon_start, i_lon_end = match_lat_lon( fh_sentinel.variables["lat"][:], fh_sentinel.variables["lon"][:], lats, lons) sm_mask = ~ma.getmaskarray( fh_sentinel.variables["soil_moisture"][i_lat_start:i_lat_end + 1, i_lon_start:i_lon_end + 1]) if np.sum(sm_mask) > 0: s_day = datetime.datetime.strptime(doy, "%Y%m%d").date() n_days = 0 lst_day_ref = [] while True: hist_day = str(s_day + datetime.timedelta(days=-n_days)).replace( "-", "") print(hist_day, ) fh = Dataset(os.path.join(in_path, hist_day + ".nc"), "r") lst_day_ref.append( fh.variables["LST_Day"][i_lat_start:i_lat_end + 1, i_lon_start:i_lon_end + 1]) fh.close() lst_day_ref_mask = ~np.all( ma.getmaskarray(ma.asarray(lst_day_ref)), axis=0) overlap = np.logical_and(sm_mask, lst_day_ref_mask) if np.sum(overlap) / float(np.sum(sm_mask)) > 0.95: modis_lst_fill_missing_by_most_recent_values(doy, area, n_days) break n_days += 1
def inverse(self, value): # ORIGINAL MATPLOTLIB CODE if not self.scaled(): raise ValueError("Not invertible until scaled") vmin, vmax = self.vmin, self.vmax # CUSTOM APLPY CODE if cbook.iterable(value): val = ma.asarray(value) else: val = value if self.stretch == 'Linear': pass elif self.stretch == 'Log': val = (ma.power(10., val * ma.log10(self.midpoint)) - 1.) / (self.midpoint - 1.) elif self.stretch == 'Sqrt': val = val * val elif self.stretch == 'Arcsinh': val = self.midpoint * \ ma.sinh(val * ma.arcsinh(1. / self.midpoint)) elif self.stretch == 'Arccosh': val = self.midpoint * \ ma.cosh(val * ma.arccosh(1. / self.midpoint)) elif self.stretch == 'Power': val = ma.power(val, (1. / self.exponent)) elif self.stretch == 'Exp': val = 1. / np.exp(val) else: raise Exception("Unknown stretch in APLpyNormalize: %s" % self.stretch) return vmin + val * (vmax - vmin)
def filter_auxfiles(data, isets, auxf, auxpar, auxconds): if auxf: data = ma.asarray(data) for f in range(len(auxf)): catlg = cl.ClumpCatalog.from_file([auxf[f]]) size = np.shape(catlg.clumps)[0] npars = len(auxpar[f]) tmparr = np.zeros((npars, size)) for i in range(size): for j in range(npars): tmparr[j, i] = catlg.clumps[i].record[auxpar[f][j]] tmparr = ma.asarray(tmparr) fcond = auxconds[f] col = 0 while fcond: cond = fcond.pop(0) cut = fcond.pop(0) if cond == ">": mask_aux = ma.masked_greater(tmparr[col, :], cut) elif cond == "=": mask_aux = ma.masked_equal(tmparr[col, :], cut) elif cond == "<": mask_aux = ma.masked_less(tmparr[col, :], cut) elif cond == "!=": mask_aux = ma.masked_not_equal(tmparr[col, :], cut) col += 1 tmparr.mask = mask_aux.mask if np.shape(tmparr.mask): data.mask = tmparr.mask else: data = data.data return data
def dotd(A, B, out=None): """Diagonal of :math:`\mathrm A\mathrm B^\intercal`. If ``A`` is :math:`n\times p` and ``B`` is :math:`p\times n`, it is done in :math:`O(pn)`. Args: A (array_like): Left matrix. B (array_like): Right matrix. out (:class:`numpy.ndarray`, optional): copy result to. Returns: :class:`numpy.ndarray`: Resulting diagonal. """ A = ma.asarray(A, float) B = ma.asarray(B, float) if A.ndim == 1 and B.ndim == 1: if out is None: return ma.dot(A, B) return ma.dot(A, B, out) if out is None: out = ma.empty((A.shape[0], ), float) out[:] = ma.sum(A * B.T, axis=1) return out
def estimate_kdp_vulpiani(psidp,dr,windsize = 7, band = 'C', interpolate = False): masked = psidp.mask pool = mp.Pool(processes = mp.cpu_count(),maxtasksperchild=1) func=partial(kdp_estimation_vulpiani,dr = dr, band = band, windsize = windsize, interpolate = interpolate) all_psidp = list(psidp) list_est = pool.map(func,all_psidp) kdp = np.zeros(psidp.shape)*np.nan phidp_rec = np.zeros(psidp.shape)*np.nan for i,l in enumerate(list_est): kdp[i,0:len(l[0])] = l[0] phidp_rec[i,0:len(l[1])] = l[1] kdp = ma.asarray(kdp) kdp.mask = masked phidp_rec = ma.asarray(phidp_rec) phidp_rec.mask = masked pool.close() return kdp, phidp_rec
def dotd(A, B): r"""Diagonal of :math:`\mathrm A\mathrm B^\intercal`. If ``A`` is :math:`n\times p` and ``B`` is :math:`p\times n`, it is done in :math:`O(pn)`. Args: A (array_like): Left matrix. B (array_like): Right matrix. Returns: :class:`numpy.ndarray`: Resulting diagonal. """ A = asarray(A, float) B = asarray(B, float) if A.ndim == 1 and B.ndim == 1: return dot(A, B) out = empty((A.shape[0],), float) out[:] = sum(A * B.T, axis=1) return out
def msokalsneath(u, v): r""" Computes the Sokal-Sneath dissimilarity between two boolean vectors ``u`` and ``v``, .. math:: \frac{R} {c_{TT} + R} where :math:`c_{ij}` is the number of occurrences of :math:`\mathtt{u[k]} = i` and :math:`\mathtt{v[k]} = j` for :math:`k < n` and :math:`R = 2(c_{TF} + c_{FT})`. Parameters ---------- u : ndarray An :math:`n`-dimensional vector. v : ndarray An :math:`n`-dimensional vector. Returns ------- d : double The Sokal-Sneath dissimilarity between vectors ``u`` and ``v``. """ u = ma.asarray(u, order='c') v = ma.asarray(v, order='c') if u.dtype == np.bool: ntt = (u & v).sum() else: ntt = (u * v).sum() (nft, ntf) = _nbool_correspond_ft_tf(u, v) denom = ntt + 2.0 * (ntf + nft) if denom == 0: raise ValueError('Sokal-Sneath dissimilarity is not defined for ' 'vectors that are entirely false.') return float(2.0 * (ntf + nft)) / denom
def recache(self): #if self.axes is None: print 'recache no axes' #else: print 'recache units', self.axes.xaxis.units, self.axes.yaxis.units if ma.isMaskedArray(self._xorig) or ma.isMaskedArray(self._yorig): x = ma.asarray(self.convert_xunits(self._xorig), float) y = ma.asarray(self.convert_yunits(self._yorig), float) x = ma.ravel(x) y = ma.ravel(y) else: x = np.asarray(self.convert_xunits(self._xorig), float) y = np.asarray(self.convert_yunits(self._yorig), float) x = np.ravel(x) y = np.ravel(y) if len(x) == 1 and len(y) > 1: x = x * np.ones(y.shape, float) if len(y) == 1 and len(x) > 1: y = y * np.ones(x.shape, float) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') x = x.reshape((len(x), 1)) y = y.reshape((len(y), 1)) if ma.isMaskedArray(x) or ma.isMaskedArray(y): self._xy = ma.concatenate((x, y), 1) else: self._xy = np.concatenate((x, y), 1) self._x = self._xy[:, 0] # just a view self._y = self._xy[:, 1] # just a view # Masked arrays are now handled by the Path class itself self._path = Path(self._xy) self._transformed_path = TransformedPath(self._path, self.get_transform()) self._invalid = False
def __call__(self, value, clip=None): method = self.stretch exponent = self.exponent midpoint = self.midpoint if clip is None: clip = self.clip if cbook.iterable(value): vtype = 'array' val = ma.asarray(value).astype(np.float) else: vtype = 'scalar' val = ma.array([value]).astype(np.float) self.autoscale_None(val) vmin, vmax = self.vmin, self.vmax if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin == vmax: return 0.0 * val else: if clip: mask = ma.getmask(val) val = ma.array(np.clip(val.filled(vmax), vmin, vmax), mask=mask) result = (val - vmin) * (1.0 / (vmax - vmin)) negative = result < 0. if self.stretch == 'linear': pass elif self.stretch == 'log': result = ma.log10(result * (self.midpoint - 1.) + 1.) \ / ma.log10(self.midpoint) elif self.stretch == 'sqrt': result = ma.sqrt(result) elif self.stretch == 'arcsinh': result = ma.arcsinh(result / self.midpoint) \ / ma.arcsinh(1. / self.midpoint) elif self.stretch == 'power': result = ma.power(result, exponent) else: raise Exception("Unknown stretch in APLpyNormalize: %s" % self.stretch) result[negative] = -np.inf if vtype == 'scalar': result = result[0] return result
def msokalmichener(u, v): r""" Computes the Sokal-Michener dissimilarity between two boolean vectors ``u`` and ``v``, which is defined as .. math:: \frac{R} {S + R} where :math:`c_{ij}` is the number of occurrences of :math:`\mathtt{u[k]} = i` and :math:`\mathtt{v[k]} = j` for :math:`k < n`, :math:`R = 2 * (c_{TF} + c_{FT})` and :math:`S = c_{FF} + c_{TT}`. Parameters ---------- u : ndarray An :math:`n`-dimensional vector. v : ndarray An :math:`n`-dimensional vector. Returns ------- d : double The Sokal-Michener dissimilarity between vectors ``u`` and ``v``. """ u = ma.asarray(u, order='c') v = ma.asarray(v, order='c') if u.dtype == np.bool: ntt = (u & v).sum() nff = (~u & ~v).sum() else: ntt = (u * v).sum() nff = ((1.0 - u) * (1.0 - v)).sum() (nft, ntf) = _nbool_correspond_ft_tf(u, v) return float(2.0 * (ntf + nft)) / float(ntt + nff + 2.0 * (ntf + nft))
def mcityblock(u, v): r""" Computes the Manhattan distance between two n-vectors u and v, which is defined as .. math:: \sum_i {(u_i-v_i)}. Parameters ---------- u : ndarray An :math:`n`-dimensional vector. v : ndarray An :math:`n`-dimensional vector. Returns ------- d : double The City Block distance between vectors ``u`` and ``v``. """ u = ma.asarray(u, order='c') v = ma.asarray(v, order='c') return abs(u - v).sum()
def mchebyshev(u, v): r""" Computes the Chebyshev distance between two n-vectors u and v, which is defined as .. math:: \max_i {|u_i-v_i|}. Parameters ---------- u : ndarray An :math:`n`-dimensional vector. v : ndarray An :math:`n`-dimensional vector. Returns ------- d : double The Chebyshev distance between vectors ``u`` and ``v``. """ u = ma.asarray(u, order='c') v = ma.asarray(v, order='c') return max(abs(u - v))
def mbraycurtis(u, v): r""" Computes the Bray-Curtis distance between two n-vectors ``u`` and ``v``, which is defined as .. math:: \sum{|u_i-v_i|} / \sum{|u_i+v_i|}. Parameters ---------- u : ndarray An :math:`n`-dimensional vector. v : ndarray An :math:`n`-dimensional vector. Returns ------- d : double The Bray-Curtis distance between vectors ``u`` and ``v``. """ u = ma.asarray(u, order='c') v = ma.asarray(v, order='c') return abs(u - v).sum() / abs(u + v).sum()
def set_data(self, A, shape=None): """ Set the image array ACCEPTS: numpy/PIL Image A""" # check if data is PIL Image without importing Image if hasattr(A,'getpixel'): X = pil_to_array(A) else: X = ma.asarray(A) # assume array self._A = X self._imcache =None self._rgbacache = None self._oldxslice = None self._oldyslice = None
def test_regrid_ok_src_masked(self): self.data = ma.asarray(self.data) self.data[2, 3] = ma.masked # tlhc 1x masked of 6x src cells self.data[2, 4] = ma.masked # trhc 2x masked of 6x src cells self.data[2, 5] = ma.masked self.data[3, 2] = ma.masked # blhc 3x masked of 6x src cells self.data[3, 3] = ma.masked self.data[4, 3] = ma.masked self.data[3, 4] = ma.masked # brhc 4x masked of 6x src cells self.data[3, 5] = ma.masked self.data[4, 4] = ma.masked self.data[4, 5] = ma.masked result = agg(self.data, self.sx_points, self.sx_bounds, self.sy_points, self.sy_bounds, self.sx_dim, self.sy_dim, self.gx_bounds, self.gy_bounds) assert_array_equal(result, self._expected())