Python numpy.promote_types() Examples
The following are 30
code examples of numpy.promote_types().
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_numeric.py From mxnet-lambda with Apache License 2.0 | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #2
Source File: test_numeric.py From Computable with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U16')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U16')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U16')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U16')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #3
Source File: test_numeric.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #4
Source File: test_numeric.py From vnpy_crypto with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #5
Source File: test_numeric.py From pySINDy with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #6
Source File: test_numeric.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #7
Source File: type_mapping.py From coremltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
def promote_types(dtype1, dtype2): """ Get the smallest type to which the given scalar types can be cast. Args: dtype1 (builtin): dtype2 (builtin): Returns: A builtin datatype or None. """ nptype1 = nptype_from_builtin(dtype1) nptype2 = nptype_from_builtin(dtype2) # Circumvent the undesirable np type promotion: # >> np.promote_types(np.float32, np.int) # dtype('float64') if np.issubdtype(nptype1, np.floating) and np.issubdtype(nptype2, np.signedinteger): nppromoted = nptype1 elif np.issubdtype(nptype2, np.floating) and np.issubdtype( nptype1, np.signedinteger ): nppromoted = nptype2 else: nppromoted = np.promote_types(nptype1, nptype2) return numpy_type_to_builtin_type(nppromoted)
Example #8
Source File: test_numeric.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #9
Source File: __init__.py From cupy with MIT License | 6 votes |
def common_type(*arrays): """Return a scalar type which is common to the input arrays. .. seealso:: :func:`numpy.common_type` """ if len(arrays) == 0: return numpy.float16 default_float_dtype = numpy.dtype('float64') dtypes = [] for a in arrays: if a.dtype.kind == 'b': raise TypeError('can\'t get common type for non-numeric array') elif a.dtype.kind in 'iu': dtypes.append(default_float_dtype) else: dtypes.append(a.dtype) return functools.reduce(numpy.promote_types, dtypes).type
Example #10
Source File: discrete_model.py From vnpy_crypto with MIT License | 6 votes |
def __init__(self, endog, exog, offset=None, exposure=None, missing='none', **kwargs): super(CountModel, self).__init__(endog, exog, missing=missing, offset=offset, exposure=exposure, **kwargs) if exposure is not None: self.exposure = np.log(self.exposure) self._check_inputs(self.offset, self.exposure, self.endog) if offset is None: delattr(self, 'offset') if exposure is None: delattr(self, 'exposure') # promote dtype to float64 if needed dt = np.promote_types(self.endog.dtype, np.float64) self.endog = np.asarray(self.endog, dt) dt = np.promote_types(self.exog.dtype, np.float64) self.exog = np.asarray(self.exog, dt)
Example #11
Source File: common.py From dicom2nifti with MIT License | 6 votes |
def get_volume_pixeldata(sorted_slices): """ the slice and intercept calculation can cause the slices to have different dtypes we should get the correct dtype that can cover all of them :type sorted_slices: list of slices :param sorted_slices: sliced sored in the correct order to create volume """ slices = [] combined_dtype = None for slice_ in sorted_slices: slice_data = _get_slice_pixeldata(slice_) slice_data = slice_data[numpy.newaxis, :, :] slices.append(slice_data) if combined_dtype is None: combined_dtype = slice_data.dtype else: combined_dtype = numpy.promote_types(combined_dtype, slice_data.dtype) # create the new volume with with the correct data vol = numpy.concatenate(slices, axis=0) # Done vol = numpy.transpose(vol, (2, 1, 0)) return vol
Example #12
Source File: test_numeric.py From recruit with Apache License 2.0 | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #13
Source File: array.py From OpenModes with GNU General Public License v3.0 | 6 votes |
def dot(self, other): """Matrix/vector multiplication with another LookupArray""" if not isinstance(other, LookupArray): assert(self.shape[-1] == other.shape[0]) new_lookup = self.lookup[:-1]+[None,]*(other.ndim-1) new_shape = self.shape[:-1]+other.shape[1:] elif compatible_quantity_part(self.lookup[-2:], other.lookup[:2]): new_lookup = self.lookup[:-2]+other.lookup[2:] new_shape = self.shape[:-2]+other.shape[2:] other = other.simple_view() else: raise NotImplementedError if len(new_shape) == 0: # handle the case of a scalar result new_array = np.dot(self.simple_view(), other) else: new_array = LookupArray(lookup=new_lookup, shape=new_shape, dtype=np.promote_types(self.dtype, other.dtype)) new_array.simple_view()[:] = np.dot(self.simple_view(), other) return new_array
Example #14
Source File: test_numeric.py From ImageFusion with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #15
Source File: test_numeric.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def test_promote_types_endian(self): # promote_types should always return native-endian types assert_equal(np.promote_types('<i8', '<i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>i8'), np.dtype('i8')) assert_equal(np.promote_types('>i8', '>U16'), np.dtype('U21')) assert_equal(np.promote_types('<i8', '<U16'), np.dtype('U21')) assert_equal(np.promote_types('>U16', '>i8'), np.dtype('U21')) assert_equal(np.promote_types('<U16', '<i8'), np.dtype('U21')) assert_equal(np.promote_types('<S5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>S5', '>U8'), np.dtype('U8')) assert_equal(np.promote_types('<U8', '<S5'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>S5'), np.dtype('U8')) assert_equal(np.promote_types('<U5', '<U8'), np.dtype('U8')) assert_equal(np.promote_types('>U8', '>U5'), np.dtype('U8')) assert_equal(np.promote_types('<M8', '<M8'), np.dtype('M8')) assert_equal(np.promote_types('>M8', '>M8'), np.dtype('M8')) assert_equal(np.promote_types('<m8', '<m8'), np.dtype('m8')) assert_equal(np.promote_types('>m8', '>m8'), np.dtype('m8'))
Example #16
Source File: test_numeric.py From mxnet-lambda with Apache License 2.0 | 5 votes |
def test_promote_types_strings(self): assert_equal(np.promote_types('bool', 'S'), np.dtype('S5')) assert_equal(np.promote_types('b', 'S'), np.dtype('S4')) assert_equal(np.promote_types('u1', 'S'), np.dtype('S3')) assert_equal(np.promote_types('u2', 'S'), np.dtype('S5')) assert_equal(np.promote_types('u4', 'S'), np.dtype('S10')) assert_equal(np.promote_types('u8', 'S'), np.dtype('S20')) assert_equal(np.promote_types('i1', 'S'), np.dtype('S4')) assert_equal(np.promote_types('i2', 'S'), np.dtype('S6')) assert_equal(np.promote_types('i4', 'S'), np.dtype('S11')) assert_equal(np.promote_types('i8', 'S'), np.dtype('S21')) assert_equal(np.promote_types('bool', 'U'), np.dtype('U5')) assert_equal(np.promote_types('b', 'U'), np.dtype('U4')) assert_equal(np.promote_types('u1', 'U'), np.dtype('U3')) assert_equal(np.promote_types('u2', 'U'), np.dtype('U5')) assert_equal(np.promote_types('u4', 'U'), np.dtype('U10')) assert_equal(np.promote_types('u8', 'U'), np.dtype('U20')) assert_equal(np.promote_types('i1', 'U'), np.dtype('U4')) assert_equal(np.promote_types('i2', 'U'), np.dtype('U6')) assert_equal(np.promote_types('i4', 'U'), np.dtype('U11')) assert_equal(np.promote_types('i8', 'U'), np.dtype('U21')) assert_equal(np.promote_types('bool', 'S1'), np.dtype('S5')) assert_equal(np.promote_types('bool', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('b', 'S1'), np.dtype('S4')) assert_equal(np.promote_types('b', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u1', 'S1'), np.dtype('S3')) assert_equal(np.promote_types('u1', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u2', 'S1'), np.dtype('S5')) assert_equal(np.promote_types('u2', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u4', 'S1'), np.dtype('S10')) assert_equal(np.promote_types('u4', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u8', 'S1'), np.dtype('S20')) assert_equal(np.promote_types('u8', 'S30'), np.dtype('S30'))
Example #17
Source File: print_coercion_tables.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray, use_promote_types=False): print('+', end=' ') for char in ntypes: print(char, end=' ') print() for row in ntypes: if row == 'O': rowtype = GenericObject else: rowtype = np.obj2sctype(row) print(row, end=' ') for col in ntypes: if col == 'O': coltype = GenericObject else: coltype = np.obj2sctype(col) try: if firstarray: rowvalue = np.array([rowtype(inputfirstvalue)], dtype=rowtype) else: rowvalue = rowtype(inputfirstvalue) colvalue = coltype(inputsecondvalue) if use_promote_types: char = np.promote_types(rowvalue.dtype, colvalue.dtype).char else: value = np.add(rowvalue, colvalue) if isinstance(value, np.ndarray): char = value.dtype.char else: char = np.dtype(type(value)).char except ValueError: char = '!' except OverflowError: char = '@' except TypeError: char = '#' print(char, end=' ') print()
Example #18
Source File: czifile.py From spimagine with BSD 3-Clause "New" or "Revised" License | 5 votes |
def dtype(self): """Return dtype of image data in file.""" # subblock data can be of different pixel type dtype = self.filtered_subblock_directory[0].dtype[-2:] for directory_entry in self.filtered_subblock_directory: dtype = numpy.promote_types(dtype, directory_entry.dtype[-2:]) return dtype
Example #19
Source File: print_coercion_tables.py From ImageFusion with MIT License | 5 votes |
def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray, use_promote_types=False): print('+', end=' ') for char in ntypes: print(char, end=' ') print() for row in ntypes: if row == 'O': rowtype = GenericObject else: rowtype = np.obj2sctype(row) print(row, end=' ') for col in ntypes: if col == 'O': coltype = GenericObject else: coltype = np.obj2sctype(col) try: if firstarray: rowvalue = np.array([rowtype(inputfirstvalue)], dtype=rowtype) else: rowvalue = rowtype(inputfirstvalue) colvalue = coltype(inputsecondvalue) if use_promote_types: char = np.promote_types(rowvalue.dtype, colvalue.dtype).char else: value = np.add(rowvalue, colvalue) if isinstance(value, np.ndarray): char = value.dtype.char else: char = np.dtype(type(value)).char except ValueError: char = '!' except OverflowError: char = '@' except TypeError: char = '#' print(char, end=' ') print()
Example #20
Source File: test_datetime.py From ImageFusion with MIT License | 5 votes |
def test_dtype_promotion(self): # datetime <op> datetime computes the metadata gcd # timedelta <op> timedelta computes the metadata gcd for mM in ['m', 'M']: assert_equal( np.promote_types(np.dtype(mM+'8[2Y]'), np.dtype(mM+'8[2Y]')), np.dtype(mM+'8[2Y]')) assert_equal( np.promote_types(np.dtype(mM+'8[12Y]'), np.dtype(mM+'8[15Y]')), np.dtype(mM+'8[3Y]')) assert_equal( np.promote_types(np.dtype(mM+'8[62M]'), np.dtype(mM+'8[24M]')), np.dtype(mM+'8[2M]')) assert_equal( np.promote_types(np.dtype(mM+'8[1W]'), np.dtype(mM+'8[2D]')), np.dtype(mM+'8[1D]')) assert_equal( np.promote_types(np.dtype(mM+'8[W]'), np.dtype(mM+'8[13s]')), np.dtype(mM+'8[s]')) assert_equal( np.promote_types(np.dtype(mM+'8[13W]'), np.dtype(mM+'8[49s]')), np.dtype(mM+'8[7s]')) # timedelta <op> timedelta raises when there is no reasonable gcd assert_raises(TypeError, np.promote_types, np.dtype('m8[Y]'), np.dtype('m8[D]')) assert_raises(TypeError, np.promote_types, np.dtype('m8[M]'), np.dtype('m8[W]')) # timedelta <op> timedelta may overflow with big unit ranges assert_raises(OverflowError, np.promote_types, np.dtype('m8[W]'), np.dtype('m8[fs]')) assert_raises(OverflowError, np.promote_types, np.dtype('m8[s]'), np.dtype('m8[as]'))
Example #21
Source File: test_numeric.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_promote_types_strings(self): assert_equal(np.promote_types('bool', 'S'), np.dtype('S5')) assert_equal(np.promote_types('b', 'S'), np.dtype('S4')) assert_equal(np.promote_types('u1', 'S'), np.dtype('S3')) assert_equal(np.promote_types('u2', 'S'), np.dtype('S5')) assert_equal(np.promote_types('u4', 'S'), np.dtype('S10')) assert_equal(np.promote_types('u8', 'S'), np.dtype('S20')) assert_equal(np.promote_types('i1', 'S'), np.dtype('S4')) assert_equal(np.promote_types('i2', 'S'), np.dtype('S6')) assert_equal(np.promote_types('i4', 'S'), np.dtype('S11')) assert_equal(np.promote_types('i8', 'S'), np.dtype('S21')) assert_equal(np.promote_types('bool', 'U'), np.dtype('U5')) assert_equal(np.promote_types('b', 'U'), np.dtype('U4')) assert_equal(np.promote_types('u1', 'U'), np.dtype('U3')) assert_equal(np.promote_types('u2', 'U'), np.dtype('U5')) assert_equal(np.promote_types('u4', 'U'), np.dtype('U10')) assert_equal(np.promote_types('u8', 'U'), np.dtype('U20')) assert_equal(np.promote_types('i1', 'U'), np.dtype('U4')) assert_equal(np.promote_types('i2', 'U'), np.dtype('U6')) assert_equal(np.promote_types('i4', 'U'), np.dtype('U11')) assert_equal(np.promote_types('i8', 'U'), np.dtype('U21')) assert_equal(np.promote_types('bool', 'S1'), np.dtype('S5')) assert_equal(np.promote_types('bool', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('b', 'S1'), np.dtype('S4')) assert_equal(np.promote_types('b', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u1', 'S1'), np.dtype('S3')) assert_equal(np.promote_types('u1', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u2', 'S1'), np.dtype('S5')) assert_equal(np.promote_types('u2', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u4', 'S1'), np.dtype('S10')) assert_equal(np.promote_types('u4', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u8', 'S1'), np.dtype('S20')) assert_equal(np.promote_types('u8', 'S30'), np.dtype('S30'))
Example #22
Source File: print_coercion_tables.py From pySINDy with MIT License | 5 votes |
def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray, use_promote_types=False): print('+', end=' ') for char in ntypes: print(char, end=' ') print() for row in ntypes: if row == 'O': rowtype = GenericObject else: rowtype = np.obj2sctype(row) print(row, end=' ') for col in ntypes: if col == 'O': coltype = GenericObject else: coltype = np.obj2sctype(col) try: if firstarray: rowvalue = np.array([rowtype(inputfirstvalue)], dtype=rowtype) else: rowvalue = rowtype(inputfirstvalue) colvalue = coltype(inputsecondvalue) if use_promote_types: char = np.promote_types(rowvalue.dtype, colvalue.dtype).char else: value = np.add(rowvalue, colvalue) if isinstance(value, np.ndarray): char = value.dtype.char else: char = np.dtype(type(value)).char except ValueError: char = '!' except OverflowError: char = '@' except TypeError: char = '#' print(char, end=' ') print()
Example #23
Source File: test_datetime.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_dtype_promotion(self): # datetime <op> datetime computes the metadata gcd # timedelta <op> timedelta computes the metadata gcd for mM in ['m', 'M']: assert_equal( np.promote_types(np.dtype(mM+'8[2Y]'), np.dtype(mM+'8[2Y]')), np.dtype(mM+'8[2Y]')) assert_equal( np.promote_types(np.dtype(mM+'8[12Y]'), np.dtype(mM+'8[15Y]')), np.dtype(mM+'8[3Y]')) assert_equal( np.promote_types(np.dtype(mM+'8[62M]'), np.dtype(mM+'8[24M]')), np.dtype(mM+'8[2M]')) assert_equal( np.promote_types(np.dtype(mM+'8[1W]'), np.dtype(mM+'8[2D]')), np.dtype(mM+'8[1D]')) assert_equal( np.promote_types(np.dtype(mM+'8[W]'), np.dtype(mM+'8[13s]')), np.dtype(mM+'8[s]')) assert_equal( np.promote_types(np.dtype(mM+'8[13W]'), np.dtype(mM+'8[49s]')), np.dtype(mM+'8[7s]')) # timedelta <op> timedelta raises when there is no reasonable gcd assert_raises(TypeError, np.promote_types, np.dtype('m8[Y]'), np.dtype('m8[D]')) assert_raises(TypeError, np.promote_types, np.dtype('m8[M]'), np.dtype('m8[W]')) # timedelta <op> timedelta may overflow with big unit ranges assert_raises(OverflowError, np.promote_types, np.dtype('m8[W]'), np.dtype('m8[fs]')) assert_raises(OverflowError, np.promote_types, np.dtype('m8[s]'), np.dtype('m8[as]'))
Example #24
Source File: print_coercion_tables.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def print_coercion_table(ntypes, inputfirstvalue, inputsecondvalue, firstarray, use_promote_types=False): print('+', end=' ') for char in ntypes: print(char, end=' ') print() for row in ntypes: if row == 'O': rowtype = GenericObject else: rowtype = np.obj2sctype(row) print(row, end=' ') for col in ntypes: if col == 'O': coltype = GenericObject else: coltype = np.obj2sctype(col) try: if firstarray: rowvalue = np.array([rowtype(inputfirstvalue)], dtype=rowtype) else: rowvalue = rowtype(inputfirstvalue) colvalue = coltype(inputsecondvalue) if use_promote_types: char = np.promote_types(rowvalue.dtype, colvalue.dtype).char else: value = np.add(rowvalue, colvalue) if isinstance(value, np.ndarray): char = value.dtype.char else: char = np.dtype(type(value)).char except ValueError: char = '!' except OverflowError: char = '@' except TypeError: char = '#' print(char, end=' ') print()
Example #25
Source File: log_backends.py From attention-lvcsr with MIT License | 5 votes |
def __getitem__(self, time): self._check_time(time) if time == self._current_time: return self._current_dict elif time > self._current_time: # Append the last value to column arrays for k, v in self._current_dict.iteritems(): if k in self._columns: col = self._columns[k] if col.dtype[1] != self.get_dtype(v): new_dtype = [ ('idx', col.dtype[0]), ('val', numpy.promote_types(col.dtype[1], self.get_dtype(v))) ] self._columns[k] = col.astype(new_dtype, copy=False) col = self._columns[k] idx = self._col_tops[k] self._col_tops[k] = idx + 1 if idx >= col.shape[0]: col2 = numpy.empty((1.3 * idx), col.dtype) col2[:idx] = col col2[idx:]['idx'] = 2147483647 col = col2 self._columns[k] = col2 col[idx] = (self._current_time, v) else: self._columns[k] = numpy.empty( (10,), dtype=[('idx', numpy.int32), ('val', self.get_dtype(v))]) self._columns[k]['idx'][:] = 2147483647 self._columns[k][0] = (self._current_time, v) self._col_tops[k] = 1 self._current_time = time self._current_dict = {} return self._current_dict else: return _TimeSlice(time, self)
Example #26
Source File: dtypes.py From imgaug with MIT License | 5 votes |
def get_minimal_dtype(arrays, increase_itemsize_factor=1): assert isinstance(arrays, list), ( "Expected a list of arrays or dtypes, got type %s." % (type(arrays),)) assert len(arrays) > 0, ( "Cannot estimate minimal dtype of an empty iterable.") input_dts = normalize_dtypes(arrays) # This loop construct handles (1) list of a single dtype, (2) list of two # dtypes and (3) list of 3+ dtypes. Note that promote_dtypes() always # expects exactly two dtypes. promoted_dt = input_dts[0] input_dts = input_dts[1:] while len(input_dts) >= 1: promoted_dt = np.promote_types(promoted_dt, input_dts[0]) input_dts = input_dts[1:] if increase_itemsize_factor > 1: assert isinstance(promoted_dt, np.dtype), ( "Expected numpy.dtype output from numpy.promote_dtypes, got type " "%s." % (type(promoted_dt),)) return increase_itemsize_of_dtype(promoted_dt, increase_itemsize_factor) return promoted_dt # TODO rename to: promote_arrays_to_minimal_dtype_
Example #27
Source File: colors.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
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.float64. 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. """ is_scalar = not cbook.iterable(value) if is_scalar: value = [value] dtype = np.min_scalar_type(value) if np.issubdtype(dtype, np.integer) or dtype.type is np.bool_: # bool_/int8/int16 -> float32; int32/int64 -> float64 dtype = np.promote_types(dtype, np.float32) # ensure data passed in as an ndarray subclass are interpreted as # an ndarray. See issue #6622. mask = np.ma.getmask(value) data = np.asarray(np.ma.getdata(value)) result = np.ma.array(data, mask=mask, dtype=dtype, copy=True) return result, is_scalar
Example #28
Source File: colors.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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.float64. 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. """ is_scalar = not cbook.iterable(value) if is_scalar: value = [value] dtype = np.min_scalar_type(value) if np.issubdtype(dtype, np.integer) or dtype.type is np.bool_: # bool_/int8/int16 -> float32; int32/int64 -> float64 dtype = np.promote_types(dtype, np.float32) # ensure data passed in as an ndarray subclass are interpreted as # an ndarray. See issue #6622. mask = np.ma.getmask(value) data = np.asarray(np.ma.getdata(value)) result = np.ma.array(data, mask=mask, dtype=dtype, copy=True) return result, is_scalar
Example #29
Source File: test_numeric.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_promote_types_strings(self): assert_equal(np.promote_types('bool', 'S'), np.dtype('S5')) assert_equal(np.promote_types('b', 'S'), np.dtype('S4')) assert_equal(np.promote_types('u1', 'S'), np.dtype('S3')) assert_equal(np.promote_types('u2', 'S'), np.dtype('S5')) assert_equal(np.promote_types('u4', 'S'), np.dtype('S10')) assert_equal(np.promote_types('u8', 'S'), np.dtype('S20')) assert_equal(np.promote_types('i1', 'S'), np.dtype('S4')) assert_equal(np.promote_types('i2', 'S'), np.dtype('S6')) assert_equal(np.promote_types('i4', 'S'), np.dtype('S11')) assert_equal(np.promote_types('i8', 'S'), np.dtype('S21')) assert_equal(np.promote_types('bool', 'U'), np.dtype('U5')) assert_equal(np.promote_types('b', 'U'), np.dtype('U4')) assert_equal(np.promote_types('u1', 'U'), np.dtype('U3')) assert_equal(np.promote_types('u2', 'U'), np.dtype('U5')) assert_equal(np.promote_types('u4', 'U'), np.dtype('U10')) assert_equal(np.promote_types('u8', 'U'), np.dtype('U20')) assert_equal(np.promote_types('i1', 'U'), np.dtype('U4')) assert_equal(np.promote_types('i2', 'U'), np.dtype('U6')) assert_equal(np.promote_types('i4', 'U'), np.dtype('U11')) assert_equal(np.promote_types('i8', 'U'), np.dtype('U21')) assert_equal(np.promote_types('bool', 'S1'), np.dtype('S5')) assert_equal(np.promote_types('bool', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('b', 'S1'), np.dtype('S4')) assert_equal(np.promote_types('b', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u1', 'S1'), np.dtype('S3')) assert_equal(np.promote_types('u1', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u2', 'S1'), np.dtype('S5')) assert_equal(np.promote_types('u2', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u4', 'S1'), np.dtype('S10')) assert_equal(np.promote_types('u4', 'S30'), np.dtype('S30')) assert_equal(np.promote_types('u8', 'S1'), np.dtype('S20')) assert_equal(np.promote_types('u8', 'S30'), np.dtype('S30'))
Example #30
Source File: test_datetime.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_dtype_promotion(self): # datetime <op> datetime computes the metadata gcd # timedelta <op> timedelta computes the metadata gcd for mM in ['m', 'M']: assert_equal( np.promote_types(np.dtype(mM+'8[2Y]'), np.dtype(mM+'8[2Y]')), np.dtype(mM+'8[2Y]')) assert_equal( np.promote_types(np.dtype(mM+'8[12Y]'), np.dtype(mM+'8[15Y]')), np.dtype(mM+'8[3Y]')) assert_equal( np.promote_types(np.dtype(mM+'8[62M]'), np.dtype(mM+'8[24M]')), np.dtype(mM+'8[2M]')) assert_equal( np.promote_types(np.dtype(mM+'8[1W]'), np.dtype(mM+'8[2D]')), np.dtype(mM+'8[1D]')) assert_equal( np.promote_types(np.dtype(mM+'8[W]'), np.dtype(mM+'8[13s]')), np.dtype(mM+'8[s]')) assert_equal( np.promote_types(np.dtype(mM+'8[13W]'), np.dtype(mM+'8[49s]')), np.dtype(mM+'8[7s]')) # timedelta <op> timedelta raises when there is no reasonable gcd assert_raises(TypeError, np.promote_types, np.dtype('m8[Y]'), np.dtype('m8[D]')) assert_raises(TypeError, np.promote_types, np.dtype('m8[M]'), np.dtype('m8[W]')) # timedelta <op> timedelta may overflow with big unit ranges assert_raises(OverflowError, np.promote_types, np.dtype('m8[W]'), np.dtype('m8[fs]')) assert_raises(OverflowError, np.promote_types, np.dtype('m8[s]'), np.dtype('m8[as]'))