Python numpy.generic() Examples
The following are 30 code examples for showing how to use numpy.generic(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_sparse_ndarray.py License: Apache License 2.0 | 6 votes |
def test_sparse_nd_setitem(): def check_sparse_nd_setitem(stype, shape, dst): x = mx.nd.zeros(shape=shape, stype=stype) x[:] = dst dst_nd = mx.nd.array(dst) if isinstance(dst, (np.ndarray, np.generic)) else dst assert np.all(x.asnumpy() == dst_nd.asnumpy() if isinstance(dst_nd, NDArray) else dst) shape = rand_shape_2d() for stype in ['row_sparse', 'csr']: # ndarray assignment check_sparse_nd_setitem(stype, shape, rand_ndarray(shape, 'default')) check_sparse_nd_setitem(stype, shape, rand_ndarray(shape, stype)) # numpy assignment check_sparse_nd_setitem(stype, shape, np.ones(shape)) # scalar assigned to row_sparse NDArray check_sparse_nd_setitem('row_sparse', shape, 2)
Example 2
Project: recruit Author: Frank-qlu File: test_scalarinherit.py License: Apache License 2.0 | 6 votes |
def test_char_radd(self): # GH issue 9620, reached gentype_add and raise TypeError np_s = np.string_('abc') np_u = np.unicode_('abc') s = b'def' u = u'def' assert_(np_s.__radd__(np_s) is NotImplemented) assert_(np_s.__radd__(np_u) is NotImplemented) assert_(np_s.__radd__(s) is NotImplemented) assert_(np_s.__radd__(u) is NotImplemented) assert_(np_u.__radd__(np_s) is NotImplemented) assert_(np_u.__radd__(np_u) is NotImplemented) assert_(np_u.__radd__(s) is NotImplemented) assert_(np_u.__radd__(u) is NotImplemented) assert_(s + np_s == b'defabc') assert_(u + np_u == u'defabc') class Mystr(str, np.generic): # would segfault pass ret = s + Mystr('abc') assert_(type(ret) is type(s))
Example 3
Project: PynPoint Author: PynPoint File: continuous.py License: GNU General Public License v3.0 | 6 votes |
def normalization(s: Union[np.ndarray, np.generic], dt: int) -> Union[np.ndarray, np.generic]: """" Parameters ---------- s : numpy.ndarray Scales. dt : int Time step. Returns ------- numpy.ndarray Normalized data. """ return np.sqrt((2 * np.pi * s) / dt)
Example 4
Project: vnpy_crypto Author: birforce File: test_scalarinherit.py License: MIT License | 6 votes |
def test_char_radd(self): # GH issue 9620, reached gentype_add and raise TypeError np_s = np.string_('abc') np_u = np.unicode_('abc') s = b'def' u = u'def' assert_(np_s.__radd__(np_s) is NotImplemented) assert_(np_s.__radd__(np_u) is NotImplemented) assert_(np_s.__radd__(s) is NotImplemented) assert_(np_s.__radd__(u) is NotImplemented) assert_(np_u.__radd__(np_s) is NotImplemented) assert_(np_u.__radd__(np_u) is NotImplemented) assert_(np_u.__radd__(s) is NotImplemented) assert_(np_u.__radd__(u) is NotImplemented) assert_(s + np_s == b'defabc') assert_(u + np_u == u'defabc') class Mystr(str, np.generic): # would segfault pass ret = s + Mystr('abc') assert_(type(ret) is type(s))
Example 5
Project: vnpy_crypto Author: birforce File: common.py License: MIT License | 6 votes |
def _validate_date_like_dtype(dtype): """ Check whether the dtype is a date-like dtype. Raises an error if invalid. Parameters ---------- dtype : dtype, type The dtype to check. Raises ------ TypeError : The dtype could not be casted to a date-like dtype. ValueError : The dtype is an illegal date-like dtype (e.g. the the frequency provided is too specific) """ try: typ = np.datetime_data(dtype)[0] except ValueError as e: raise TypeError('{error}'.format(error=e)) if typ != 'generic' and typ != 'ns': msg = '{name!r} is too specific of a frequency, try passing {type!r}' raise ValueError(msg.format(name=dtype.name, type=dtype.type.__name__))
Example 6
Project: category_encoders Author: scikit-learn-contrib File: utils.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def convert_input(X, columns=None, deep=False): """ Unite data into a DataFrame. Objects that do not contain column names take the names from the argument. Optionally perform deep copy of the data. """ if not isinstance(X, pd.DataFrame): if isinstance(X, pd.Series): X = pd.DataFrame(X, copy=deep) else: if columns is not None and np.size(X,1) != len(columns): raise ValueError('The count of the column names does not correspond to the count of the columns') if isinstance(X, list): X = pd.DataFrame(X, columns=columns, copy=deep) # lists are always copied, but for consistency, we still pass the argument elif isinstance(X, (np.generic, np.ndarray)): X = pd.DataFrame(X, columns=columns, copy=deep) elif isinstance(X, csr_matrix): X = pd.DataFrame(X.todense(), columns=columns, copy=deep) else: raise ValueError('Unexpected input type: %s' % (str(type(X)))) elif deep: X = X.copy(deep=True) return X
Example 7
Project: spyder-kernels Author: spyder-ide File: nsview.py License: MIT License | 6 votes |
def get_numpy_dtype(obj): """Return NumPy data type associated to obj Return None if NumPy is not available or if obj is not a NumPy array or scalar""" if ndarray is not FakeObject: # NumPy is available import numpy as np if isinstance(obj, np.generic) or isinstance(obj, np.ndarray): # Numpy scalars all inherit from np.generic. # Numpy arrays all inherit from np.ndarray. # If we check that we are certain we have one of these # types then we are less likely to generate an exception below. try: return obj.dtype.type except (AttributeError, RuntimeError): # AttributeError: some NumPy objects have no dtype attribute # RuntimeError: happens with NetCDF objects (Issue 998) return #============================================================================== # Pandas support #==============================================================================
Example 8
Project: MatchZoo-py Author: NTMC-Community File: padding.py License: Apache License 2.0 | 6 votes |
def _infer_dtype(value): """Infer the dtype for the features. It is required as the input is usually array of objects before padding. """ while isinstance(value, (list, tuple)) and len(value) > 0: value = value[0] if not isinstance(value, Iterable): return np.array(value).dtype if value is not None and len(value) > 0 and np.issubdtype( np.array(value).dtype, np.generic): dtype = np.array(value[0]).dtype else: dtype = value.dtype # Single Precision if dtype == np.double: dtype = np.float32 return dtype
Example 9
Project: Mastering-Elasticsearch-7.0 Author: PacktPublishing File: test_scalarinherit.py License: MIT License | 6 votes |
def test_char_radd(self): # GH issue 9620, reached gentype_add and raise TypeError np_s = np.string_('abc') np_u = np.unicode_('abc') s = b'def' u = u'def' assert_(np_s.__radd__(np_s) is NotImplemented) assert_(np_s.__radd__(np_u) is NotImplemented) assert_(np_s.__radd__(s) is NotImplemented) assert_(np_s.__radd__(u) is NotImplemented) assert_(np_u.__radd__(np_s) is NotImplemented) assert_(np_u.__radd__(np_u) is NotImplemented) assert_(np_u.__radd__(s) is NotImplemented) assert_(np_u.__radd__(u) is NotImplemented) assert_(s + np_s == b'defabc') assert_(u + np_u == u'defabc') class Mystr(str, np.generic): # would segfault pass ret = s + Mystr('abc') assert_(type(ret) is type(s))
Example 10
Project: chainer Author: chainer File: dtype_utils.py License: MIT License | 6 votes |
def cast_if_numpy_array(xp, array, chx_expected_dtype): """Casts NumPy result array to match the dtype of ChainerX's corresponding result. This function receives result arrays for both NumPy and ChainerX and only converts dtype of the NumPy array. """ if xp is chainerx: assert isinstance(array, chainerx.ndarray) return array if xp is numpy: assert isinstance(array, (numpy.ndarray, numpy.generic)) # Dtype conversion to allow comparing the correctnesses of the values. return array.astype(chx_expected_dtype, copy=False) assert False
Example 11
Project: cat-bbs Author: aleju File: bbs.py License: MIT License | 5 votes |
def fix_by_image_dimensions(self, height, width=None): if isinstance(height, (tuple, list)): assert width is None height, width = height[0], height[1] elif isinstance(height, (np.ndarray, np.generic)): assert width is None height, width = height.shape[0], height.shape[1] else: assert width is not None assert isinstance(height, int) assert isinstance(width, int) self.x1 = int(np.clip(self.x1, 0, width-1)) self.x2 = int(np.clip(self.x2, 0, width-1)) self.y1 = int(np.clip(self.y1, 0, height-1)) self.y2 = int(np.clip(self.y2, 0, height-1)) if self.x1 > self.x2: self.x1, self.x2 = self.x2, self.x1 if self.y1 > self.y2: self.y1, self.y2 = self.y2, self.y1 if self.x1 == self.x2: if self.x1 > 0: self.x1 = self.x1 - 1 else: self.x2 = self.x2 + 1 if self.y1 == self.y2: if self.y1 > 0: self.y1 = self.y1 - 1 else: self.y2 = self.y2 + 1 #self.width = self.x2 - self.x1 #self.height = self.y2 - self.y1
Example 12
Project: hsds Author: HDFGroup File: chunkUtil.py License: Apache License 2.0 | 5 votes |
def _bytesArrayToList(data): if type(data) in (bytes, str): is_list = False elif isinstance(data, (np.ndarray, np.generic)): if len(data.shape) == 0: is_list = False data = data.tolist() # tolist will return a scalar in this case if type(data) in (list, tuple): is_list = True else: is_list = False else: is_list = True elif type(data) in (list, tuple): is_list = True else: is_list = False if is_list: out = [] for item in data: out.append(_bytesArrayToList(item)) # recursive call elif type(data) is bytes: out = data.decode("utf-8") else: out = data return out
Example 13
Project: hsds Author: HDFGroup File: arrayUtil.py License: Apache License 2.0 | 5 votes |
def bytesArrayToList(data): if type(data) in (bytes, str): is_list = False elif isinstance(data, (np.ndarray, np.generic)): if len(data.shape) == 0: is_list = False data = data.tolist() # tolist will return a scalar in this case if type(data) in (list, tuple): is_list = True else: is_list = False else: is_list = True elif type(data) in (list, tuple): is_list = True else: is_list = False if is_list: out = [] for item in data: out.append(bytesArrayToList(item)) # recursive call elif type(data) is bytes: out = data.decode("utf-8") else: out = data return out
Example 14
Project: hsds Author: HDFGroup File: chunkUtil.py License: Apache License 2.0 | 5 votes |
def _bytesArrayToList(data): if type(data) in (bytes, str): is_list = False elif isinstance(data, (np.ndarray, np.generic)): if len(data.shape) == 0: is_list = False data = data.tolist() # tolist will return a scalar in this case if type(data) in (list, tuple): is_list = True else: is_list = False else: is_list = True elif type(data) in (list, tuple): is_list = True else: is_list = False if is_list: out = [] for item in data: out.append(_bytesArrayToList(item)) # recursive call elif type(data) is bytes: out = data.decode("utf-8") else: out = data return out
Example 15
Project: hsds Author: HDFGroup File: arrayUtil.py License: Apache License 2.0 | 5 votes |
def bytesArrayToList(data): if type(data) in (bytes, str): is_list = False elif isinstance(data, (np.ndarray, np.generic)): if len(data.shape) == 0: is_list = False data = data.tolist() # tolist will return a scalar in this case if type(data) in (list, tuple): is_list = True else: is_list = False else: is_list = True elif type(data) in (list, tuple): is_list = True else: is_list = False if is_list: out = [] for item in data: out.append(bytesArrayToList(item)) # recursive call elif type(data) is bytes: out = data.decode("utf-8") else: out = data return out
Example 16
Project: recruit Author: Frank-qlu File: test_core.py License: Apache License 2.0 | 5 votes |
def test_oddfeatures_3(self): # Tests some generic features atest = array([10], mask=True) btest = array([20]) idx = atest.mask atest[idx] = btest[idx] assert_equal(atest, [20])
Example 17
Project: recruit Author: Frank-qlu File: test_core.py License: Apache License 2.0 | 5 votes |
def test_tolist_specialcase(self): # Test mvoid.tolist: make sure we return a standard Python object a = array([(0, 1), (2, 3)], dtype=[('a', int), ('b', int)]) # w/o mask: each entry is a np.void whose elements are standard Python for entry in a: for item in entry.tolist(): assert_(not isinstance(item, np.generic)) # w/ mask: each entry is a ma.void whose elements should be # standard Python a.mask[0] = (0, 1) for entry in a: for item in entry.tolist(): assert_(not isinstance(item, np.generic))
Example 18
Project: recruit Author: Frank-qlu File: test_core.py License: Apache License 2.0 | 5 votes |
def test_masked_where_oddities(self): # Tests some generic features. atest = ones((10, 10, 10), dtype=float) btest = zeros(atest.shape, MaskType) ctest = masked_where(btest, atest) assert_equal(atest, ctest)
Example 19
Project: recruit Author: Frank-qlu File: common.py License: Apache License 2.0 | 5 votes |
def is_timedelta64_ns_dtype(arr_or_dtype): """ Check whether the provided array or dtype is of the timedelta64[ns] dtype. This is a very specific dtype, so generic ones like `np.timedelta64` will return False if passed into this function. Parameters ---------- arr_or_dtype : array-like The array or dtype to check. Returns ------- boolean : Whether or not the array or dtype is of the timedelta64[ns] dtype. Examples -------- >>> is_timedelta64_ns_dtype(np.dtype('m8[ns]')) True >>> is_timedelta64_ns_dtype(np.dtype('m8[ps]')) # Wrong frequency False >>> is_timedelta64_ns_dtype(np.array([1, 2], dtype='m8[ns]')) True >>> is_timedelta64_ns_dtype(np.array([1, 2], dtype=np.timedelta64)) False """ return _is_dtype(arr_or_dtype, lambda dtype: dtype == _TD_DTYPE)
Example 20
Project: deep-smoke-machine Author: CMU-CREATE-Lab File: viz_functional.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def save_image(im, path): """ Saves a numpy matrix or PIL image as an image Args: im_as_arr (Numpy array): Matrix of shape DxWxH path (str): Path to the image """ if isinstance(im, (np.ndarray, np.generic)): im = format_np_output(im) im = Image.fromarray(im) im.save(path)
Example 21
Project: mars Author: mars-project File: aggregation.py License: Apache License 2.0 | 5 votes |
def _wrap_df(xdf, value, columns=None, transform=False): if isinstance(value, (np.generic, int, float, complex)): value = xdf.DataFrame([value], columns=columns) else: value = xdf.DataFrame(value, columns=columns) return value.T if transform else value
Example 22
Project: mars Author: mars-project File: utils.py License: Apache License 2.0 | 5 votes |
def on_serialize_numpy_type(value): if value is pd.NaT: value = None return value.item() if isinstance(value, np.generic) else value
Example 23
Project: mars Author: mars-project File: core.py License: Apache License 2.0 | 5 votes |
def assert_tensor_consistent(cls, expected, real): from mars.lib.sparse import SparseNDArray if isinstance(real, (str, int, bool, float, complex)): real = np.array([real])[0] if not isinstance(real, (np.generic, np.ndarray, SparseNDArray)): raise AssertionError('Type of real value (%r) not one of ' '(np.generic, np.array, SparseNDArray)' % type(real)) if not hasattr(expected, 'dtype'): return cls.assert_dtype_consistent(expected.dtype, real.dtype) cls.assert_shape_consistent(expected.shape, real.shape)
Example 24
Project: aetros-cli Author: aetros File: __init__.py License: MIT License | 5 votes |
def invalid_json_values(obj): if isinstance(obj, np.generic): return obj.item() if isinstance(obj, np.ndarray): return obj.tolist() if isinstance(obj, bytes): return obj.decode('cp437') if isinstance(map, type) and isinstance(obj, map): # python 3 map return list(obj) raise TypeError('Invalid data type passed to json encoder: ' + type(obj).__name__)
Example 25
Project: aetros-cli Author: aetros File: KerasCallback.py License: MIT License | 5 votes |
def filter_invalid_json_values(self, dict): for k, v in six.iteritems(dict): if isinstance(v, (np.ndarray, np.generic)): dict[k] = v.tolist() if math.isnan(v) or math.isinf(v): dict[k] = -1
Example 26
Project: kernel_tuner Author: benvanwerkhoven File: c.py License: Apache License 2.0 | 5 votes |
def ready_argument_list(self, arguments): """ready argument list to be passed to the C function :param arguments: List of arguments to be passed to the C function. The order should match the argument list on the C function. Allowed values are numpy.ndarray, and/or numpy.int32, numpy.float32, and so on. :type arguments: list(numpy objects) :returns: A list of arguments that can be passed to the C function. :rtype: list(Argument) """ ctype_args = [ None for _ in arguments] for i, arg in enumerate(arguments): if not isinstance(arg, (numpy.ndarray, numpy.number)): raise TypeError("Argument is not numpy ndarray or numpy scalar %s" % type(arg)) dtype_str = str(arg.dtype) data = arg.copy() if isinstance(arg, numpy.ndarray): if dtype_str in dtype_map.keys(): # In numpy <= 1.15, ndarray.ctypes.data_as does not itself keep a reference # to its underlying array, so we need to store a reference to arg.copy() # in the Argument object manually to avoid it being deleted. # (This changed in numpy > 1.15.) data_ctypes = data.ctypes.data_as(C.POINTER(dtype_map[dtype_str])) else: raise TypeError("unknown dtype for ndarray") elif isinstance(arg, numpy.generic): data_ctypes = dtype_map[dtype_str](arg) ctype_args[i] = Argument(numpy=data, ctypes=data_ctypes) return ctype_args
Example 27
Project: kernel_tuner Author: benvanwerkhoven File: util.py License: Apache License 2.0 | 5 votes |
def check_argument_list(kernel_name, kernel_string, args): """ raise an exception if a kernel arguments do not match host arguments """ kernel_arguments = list() collected_errors = list() for iterator in re.finditer(kernel_name + "[ \n\t]*" + "\(", kernel_string): kernel_start = iterator.end() kernel_end = kernel_string.find(")", kernel_start) if kernel_start != 0: kernel_arguments.append(kernel_string[kernel_start:kernel_end].split(",")) for arguments_set, arguments in enumerate(kernel_arguments): collected_errors.append(list()) if len(arguments) != len(args): collected_errors[arguments_set].append("Kernel and host argument lists do not match in size.") continue for (i, arg) in enumerate(args): kernel_argument = arguments[i] if not isinstance(arg, (numpy.ndarray, numpy.generic)): raise TypeError("Argument at position " + str(i) + " of type: " + str(type(arg)) + " should be of type numpy.ndarray or numpy scalar") correct = True if isinstance(arg, numpy.ndarray) and not "*" in kernel_argument: correct = False #array is passed to non-pointer kernel argument if correct and check_argument_type(str(arg.dtype), kernel_argument): continue collected_errors[arguments_set].append("Argument at position " + str(i) + " of dtype: " + str(arg.dtype) + " does not match " + kernel_argument + ".") if not collected_errors[arguments_set]: # We assume that if there is a possible list of arguments that matches with the provided one # it is the right one return for errors in collected_errors: warnings.warn(errors[0], UserWarning) #raise TypeError(errors[0])
Example 28
Project: kernel_tuner Author: benvanwerkhoven File: kernelbuilder.py License: Apache License 2.0 | 5 votes |
def run_kernel(self, args): """Run the GPU kernel Copy the arguments marked as inputs to the GPU Call the GPU kernel Copy the arguments marked as outputs from the GPU Return the outputs in a list of numpy arrays :param args: A list with the kernel arguments as numpy arrays or numpy scalars :type args: list(np.ndarray or np.generic) """ self.update_gpu_args(args) self.dev.run_kernel(self.func, self.gpu_args, self.kernel_instance) return self.get_gpu_result(args)
Example 29
Project: kernel_tuner Author: benvanwerkhoven File: kernelbuilder.py License: Apache License 2.0 | 5 votes |
def __call__(self, *args): """Run the GPU kernel Copy the arguments marked as inputs to the GPU Call the GPU kernel Copy the arguments marked as outputs from the GPU Return the outputs in a list of numpy arrays :param *args: A variable number of kernel arguments as numpy arrays or numpy scalars :type *args: np.ndarray or np.generic """ return self.run_kernel(args)
Example 30
Project: pygraphistry Author: graphistry File: pygraphistry.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def default(self, obj): if isinstance(obj, numpy.ndarray) and obj.ndim == 1: return obj.tolist() elif isinstance(obj, numpy.generic): return obj.item() elif isinstance(obj, type(pandas.NaT)): return None elif isinstance(obj, datetime): return obj.isoformat() return json.JSONEncoder.default(self, obj)