Python numpy.generic() Examples
The following are 30
code examples of numpy.generic().
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 also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: test_scalarinherit.py From Mastering-Elasticsearch-7.0 with 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 #2
Source File: padding.py From MatchZoo-py with 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 #3
Source File: nsview.py From spyder-kernels with 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 #4
Source File: utils.py From category_encoders with 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 #5
Source File: test_scalarinherit.py From recruit with 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 #6
Source File: common.py From vnpy_crypto with 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 #7
Source File: test_scalarinherit.py From vnpy_crypto with 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 #8
Source File: continuous.py From PynPoint with 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 #9
Source File: test_sparse_ndarray.py From dynamic-training-with-apache-mxnet-on-aws with 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 #10
Source File: dtype_utils.py From chainer with 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
Source File: io.py From deep_pipe with MIT License | 5 votes |
def default(self, o): if isinstance(o, (np.generic, np.ndarray)): return o.tolist() return super().default(o)
Example #12
Source File: joy_driver_pid.py From crazyflieROS with GNU General Public License v2.0 | 5 votes |
def numpy2python(l): if isinstance(l,list): return [np.asscalar(e) if isinstance(e, np.generic) else e for e in l] if isinstance(l,tuple): return tuple([np.asscalar(e) if isinstance(e, np.generic) else e for e in l]) if isinstance(l,dict): d = {} for k, v in l.iteritems(): d[np.asscalar(k) if isinstance(k, np.generic) else k] = np.asscalar(v) if isinstance(v, np.generic) else v return d
Example #13
Source File: nsview.py From spyder-kernels with MIT License | 5 votes |
def get_supported_types(): """ Return a dictionnary containing types lists supported by the namespace browser. Note: If you update this list, don't forget to update variablexplorer.rst in spyder-docs """ from datetime import date, timedelta editable_types = [int, float, complex, list, set, dict, tuple, date, timedelta] + list(TEXT_TYPES) + list(INT_TYPES) try: from numpy import ndarray, matrix, generic editable_types += [ndarray, matrix, generic] except: pass try: from pandas import DataFrame, Series, Index editable_types += [DataFrame, Series, Index] except: pass picklable_types = editable_types[:] try: from spyder.pil_patch import Image editable_types.append(Image.Image) except: pass return dict(picklable=picklable_types, editable=editable_types)
Example #14
Source File: utils.py From parasol with MIT License | 5 votes |
def capture_frame(self, frame): if not isinstance(frame, (np.ndarray, np.generic)): raise error.InvalidFrame('Wrong type {} for {} (must be np.ndarray or np.generic)'.format(type(frame), frame)) if frame.shape != self.frame_shape: raise error.InvalidFrame("Your frame has shape {}, but the VideoRecorder is configured for shape {}.".format(frame.shape, self.frame_shape)) if frame.dtype != np.uint8: raise error.InvalidFrame("Your frame has data type {}, but we require uint8 (i.e. RGB values from 0-255).".format(frame.dtype)) if distutils.version.LooseVersion(np.__version__) >= distutils.version.LooseVersion('1.9.0'): self.proc.stdin.write(frame.tobytes()) else: self.proc.stdin.write(frame.tostring())
Example #15
Source File: helper.py From chainer with MIT License | 5 votes |
def _is_numpy_type(result): return isinstance(result, (numpy.ndarray, numpy.generic))
Example #16
Source File: test_core.py From Mastering-Elasticsearch-7.0 with MIT License | 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 #17
Source File: ndarray.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def __setitem__(self, in_slice, value): """Set ndarray value""" if (not isinstance(in_slice, slice) or in_slice.start is not None or in_slice.stop is not None): raise ValueError('Array only support set from numpy array') if isinstance(value, NDArrayBase): if value.handle is not self.handle: value.copyto(self) elif isinstance(value, (np.ndarray, np.generic)): self.copyfrom(value) else: raise TypeError('type %s not supported' % str(type(value)))
Example #18
Source File: tensor.py From funsor with Apache License 2.0 | 5 votes |
def __call__(cls, data, inputs=None, dtype="real"): if inputs is None: inputs = tuple() elif isinstance(inputs, OrderedDict): inputs = tuple(inputs.items()) # XXX: memoize tests fail for np.generic because those scalar values are hashable? # it seems that there is no harm with the conversion generic -> ndarray here if isinstance(data, np.generic): data = data.__array__() return super(TensorMeta, cls).__call__(data, inputs, dtype)
Example #19
Source File: block.py From gluon-cv with Apache License 2.0 | 5 votes |
def __init__(self, size, pad=None, interpolation=2): super(RandomCrop, self).__init__() numeric_types = (float, int, np.generic) if isinstance(size, numeric_types): size = (size, size) self._args = (size, interpolation) if isinstance(pad, int): self.pad = ((pad, pad), (pad, pad), (0, 0)) else: self.pad = pad
Example #20
Source File: basic_math.py From chainer with MIT License | 5 votes |
def _chainerx_preprocess_const(x, value, label): # Allow mixing of numpy/cupy array and chainerx array as long as # conversion without copy is possible. if isinstance(value, (numpy.ndarray, cuda.ndarray)): # TODO(niboshi): force zero-copy return backend.to_chx(value) if isinstance(value, (six.integer_types, float)): return value if isinstance(value, numpy.generic): return value.item() if isinstance(value, variable.Variable): value = variable.as_array(value) utils._check_arrays_forward_compatible((x, value), label) return value
Example #21
Source File: serde.py From ngraph-python with Apache License 2.0 | 5 votes |
def protobuf_to_op(pb_op): """ This will convert a protobuf Op object into its corresponding Python object. But this cannot setup links to other ops (such as args, control_deps) since those ops may not exist yet. We have to wait until all ops are created before connecting them back up together in a second pass, so args, etc will be uninitialized. """ cls = get_ngraph_op_cls(pb_op.op_type) # Skip the class constructor but we'll use the generic op constructor because it sets a lot of # helpful defaults py_op = cls.__new__(cls) op_graph.Op.__init__(py_op) py_op.name = str(pb_op.name) if 'valfun_value' in pb_op.attrs: valfun_value = pb_to_tensor(pb_op.attrs['valfun_value'].tensor) py_op.valfun = lambda x: valfun_value # op.uuid py_op.uuid = uuid.UUID(bytes=pb_op.uuid.uuid) # op.metadata and remaining keys ignored_keys = {'valfun_value', 'dtype', 'metadata'} remaining_keys = set(pb_op.attrs.keys()).difference(ignored_keys) for key in remaining_keys: if key == '_ngraph_ser_handle': py_op._ngraph_ser_handle = True if key.startswith('_ngraph_metadata_'): value = pb_op.attrs[key] py_op.metadata[key[17:]] = protobuf_attr_to_python(value) elif not key.startswith('_is_') and key not in EXCEPTION_ATTRIBUTES and \ key.startswith('_'): continue else: value = pb_op.attrs[key] setattr(py_op, key, protobuf_attr_to_python(value)) return py_op
Example #22
Source File: serde.py From ngraph-python with Apache License 2.0 | 5 votes |
def assign_scalar(message, value): """ Adds the appropriate scalar type of value to the protobuf message """ if value is None: message.null_val = True elif isinstance(value, np.generic): assign_scalar(message, np.asscalar(value)) elif isinstance(value, (str, six.text_type)): message.string_val = value elif isinstance(value, np.dtype): message.dtype_val = dtype_to_protobuf(value) elif isinstance(value, float): message.double_val = value elif isinstance(value, bool): message.bool_val = value elif isinstance(value, six.integer_types): message.int_val = value elif isinstance(value, slice): slice_val = ops_pb.Slice() if value.start is not None: slice_val.start.value = value.start if value.step is not None: slice_val.step.value = value.step if value.stop is not None: slice_val.stop.value = value.stop message.slice_val.CopyFrom(slice_val) elif isinstance(value, dict): for key in value: assign_scalar(message.map_val.map[key], value[key]) # This encodes an empty dict for deserialization assign_scalar(message.map_val.map['_ngraph_map_sentinel_'], '') elif isinstance(value, Axis): message.axis.CopyFrom(axis_to_protobuf(value)) elif isinstance(value, AxesMap): message.axes_map.CopyFrom(axes_map_to_protobuf(value)) else: raise unhandled_scalar_value(value)
Example #23
Source File: serde.py From ngraph-python with Apache License 2.0 | 5 votes |
def is_scalar_type(value): return value is None or \ isinstance(value, (str, six.text_type, float, bool, Axis, AxesMap, dict, slice, np.generic) + six.integer_types)
Example #24
Source File: serde.py From ngraph-python with Apache License 2.0 | 5 votes |
def tensor_to_protobuf(tensor): pb_tensor = ops_pb.Tensor() pb_tensor.info.dtype = dtype_to_protobuf(tensor.dtype) pb_tensor.info.shape.extend(tensor.shape) if isinstance(tensor, (np.ndarray, np.generic)): pb_tensor.data = tensor.tobytes() else: raise ValueError("Unknown tensor value of {}".format(tensor)) return pb_tensor
Example #25
Source File: array.py From gpkit with MIT License | 5 votes |
def __array_wrap__(self, out_arr, context=None): # pylint: disable=arguments-differ """Called by numpy ufuncs. Special case to avoid creation of 0-dimensional arrays See http://docs.scipy.org/doc/numpy/user/basics.subclassing.html""" if out_arr.ndim: return np.ndarray.__array_wrap__(self, out_arr, context) # pylint: disable=too-many-function-args val = out_arr.item() return np.float(val) if isinstance(val, np.generic) else val
Example #26
Source File: common.py From vnpy_crypto with MIT License | 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 """ if arr_or_dtype is None: return False try: tipo = _get_dtype(arr_or_dtype) return tipo == _TD_DTYPE except TypeError: return False
Example #27
Source File: test_core.py From vnpy_crypto with MIT License | 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 #28
Source File: test_core.py From vnpy_crypto with MIT License | 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 #29
Source File: test_core.py From vnpy_crypto with MIT License | 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 #30
Source File: rollout.py From imitation with MIT License | 5 votes |
def rollout_stats(trajectories: Sequence[types.TrajectoryWithRew]) -> Dict[str, float]: """Calculates various stats for a sequence of trajectories. Args: trajectories: Sequence of trajectories. Returns: Dictionary containing `n_traj` collected (int), along with episode return statistics (keys: `{monitor_,}return_{min,mean,std,max}`, float values) and trajectory length statistics (keys: `len_{min,mean,std,max}`, float values). `return_*` values are calculated from environment rewards. `monitor_*` values are calculated from Monitor-captured rewards, and are only included if the `trajectories` contain Monitor infos. """ assert len(trajectories) > 0 out_stats: Dict[str, float] = {"n_traj": len(trajectories)} traj_descriptors = { "return": np.asarray([sum(t.rews) for t in trajectories]), "len": np.asarray([len(t.rews) for t in trajectories]), } infos_peek = trajectories[0].infos if infos_peek is not None and "episode" in infos_peek[-1]: monitor_ep_returns = [t.infos[-1]["episode"]["r"] for t in trajectories] traj_descriptors["monitor_return"] = np.asarray(monitor_ep_returns) stat_names = ["min", "mean", "std", "max"] for desc_name, desc_vals in traj_descriptors.items(): for stat_name in stat_names: stat_value: np.generic = getattr(np, stat_name)(desc_vals) # Convert numpy type to float or int. The numpy operators always return # a numpy type, but we want to return type float. (int satisfies # float type for the purposes of static-typing). out_stats[f"{desc_name}_{stat_name}"] = stat_value.item() for v in out_stats.values(): assert isinstance(v, (int, float)) return out_stats