Python numpy.min_scalar_type() Examples

The following are 30 code examples of numpy.min_scalar_type(). 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: column.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _scalar_to_format(value):
    """
    Given a scalar value or string, returns the minimum FITS column format
    that can represent that value.  'minimum' is defined by the order given in
    FORMATORDER.
    """

    # First, if value is a string, try to convert to the appropriate scalar
    # value
    for type_ in (int, float, complex):
        try:
            value = type_(value)
            break
        except ValueError:
            continue

    numpy_dtype_str = np.min_scalar_type(value).str
    numpy_dtype_str = numpy_dtype_str[1:]  # Strip endianness

    try:
        fits_format = NUMPY2FITS[numpy_dtype_str]
        return FITSUPCONVERTERS.get(fits_format, fits_format)
    except KeyError:
        return "A" + str(len(value)) 
Example #2
Source File: test_matrix_ops.py    From openprescribing with MIT License 6 votes vote down vote up
def _random_matrix(self, minimum, maximum, floor):
        """
        Return an integer matrix with random values between `minimum` and
        `maximum` with at least one being larger than `floor` and at least one
        being negative if `minimum` is negative
        """
        value = self.random.randint(floor + 1, maximum)
        if minimum < 0:
            small_value = self.random.randint(minimum, -1)
        else:
            small_value = 0
        # Ensure that the dtype is big enough to hold the maximum value (int_
        # will do for all cases apart from uint64)
        dtype = numpy.promote_types(numpy.int_, numpy.min_scalar_type(maximum))
        matrix = numpy.zeros((2, 2), dtype=dtype)
        matrix[0, 0] = value
        matrix[0, 1] = small_value
        return matrix 
Example #3
Source File: inference.py    From SERT with MIT License 6 votes vote down vote up
def create(predict_fn, word_representations,
           batch_size, window_size, vocabulary_size,
           result_callback):
    assert result_callback is not None

    instance_dtype = np.min_scalar_type(vocabulary_size - 1)
    logging.info('Instance elements will be stored using %s.', instance_dtype)

    if result_callback.should_average_input():
        batcher = EmbeddingMapper(
            predict_fn,
            word_representations,
            result_callback)
    else:
        batcher = WordBatcher(
            predict_fn,
            batch_size, window_size,
            instance_dtype,
            result_callback)

    return batcher 
Example #4
Source File: evaluation.py    From polara with MIT License 6 votes vote down vote up
def build_rank_matrix(recommendations, shape):
    # handle singletone case for a single user
    recommendations = np.array(recommendations, copy=False, ndmin=2)
    n_keys, topn = recommendations.shape
    rank_arr = np.arange(1, topn+1, dtype=np.min_scalar_type(topn))
    recs_rnk = np.lib.stride_tricks.as_strided(rank_arr, (n_keys, topn), (0, rank_arr.itemsize))
    # support models that may generate < top-n recommendations
    # such models generate self._pad_const, which is negative by convention
    valid_recommendations = recommendations >= 0
    if not valid_recommendations.all():
        data = recs_rnk[valid_recommendations]
        indices = recommendations[valid_recommendations]
        indptr = np.r_[0, np.cumsum(valid_recommendations.sum(axis=1))]
    else:
        data = recs_rnk.ravel()
        indices = recommendations.ravel()
        indptr = np.arange(0, n_keys*topn+1, topn)

    rank_matrix = no_copy_csr_matrix(data, indices, indptr, shape, rank_arr.dtype)
    return rank_matrix 
Example #5
Source File: test_multiarray.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_object(self):
        dt = np.min_scalar_type(2**64)
        wanted = np.dtype('O')
        assert_equal(wanted, dt) 
Example #6
Source File: colors.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
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 #7
Source File: colors.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #8
Source File: colors.py    From napari with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 np.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 #9
Source File: specs.py    From dm_env with Apache License 2.0 5 votes vote down vote up
def __init__(self, num_values, dtype=np.int32, name=None):
    """Initializes a new `DiscreteArray` spec.

    Args:
      num_values: Integer specifying the number of possible values to represent.
      dtype: The dtype of the array. Must be an integral type large enough to
        hold `num_values` without overflow.
      name: Optional string specifying the name of the array.

    Raises:
      ValueError: If `num_values` is not positive, if `dtype` is not integral,
        or if `dtype` is not large enough to hold `num_values` without overflow.
    """
    if num_values <= 0 or not np.issubdtype(type(num_values), np.integer):
      raise ValueError(_NUM_VALUES_NOT_POSITIVE.format(num_values))

    if not np.issubdtype(dtype, np.integer):
      raise ValueError(_DTYPE_NOT_INTEGRAL.format(dtype))

    num_values = int(num_values)
    maximum = num_values - 1
    dtype = np.dtype(dtype)

    if np.min_scalar_type(maximum) > dtype:
      raise ValueError(_DTYPE_OVERFLOW.format(dtype, num_values))

    super(DiscreteArray, self).__init__(
        shape=(),
        dtype=dtype,
        minimum=0,
        maximum=maximum,
        name=name)
    self._num_values = num_values 
Example #10
Source File: bundle_utils.py    From catalyst with Apache License 2.0 5 votes vote down vote up
def safely_reduce_dtype(ser):  # pandas.Series or numpy.array
    orig_dtype = "".join(
        [x for x in ser.dtype.name if x.isalpha()])  # float/int
    mx = 1
    for val in ser.values:
        new_itemsize = np.min_scalar_type(val).itemsize
        if mx < new_itemsize:
            mx = new_itemsize
        if orig_dtype == 'int':
            mx = max(mx, 4)
    new_dtype = orig_dtype + str(mx * 8)
    return ser.astype(new_dtype) 
Example #11
Source File: test_multiarray.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_usigned_shortshort(self):
        dt = np.min_scalar_type(2**8-1)
        wanted = np.dtype('uint8')
        assert_equal(wanted, dt) 
Example #12
Source File: test_multiarray.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_usigned_short(self):
        dt = np.min_scalar_type(2**16-1)
        wanted = np.dtype('uint16')
        assert_equal(wanted, dt) 
Example #13
Source File: test_multiarray.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_usigned_int(self):
        dt = np.min_scalar_type(2**32-1)
        wanted = np.dtype('uint32')
        assert_equal(wanted, dt) 
Example #14
Source File: test_multiarray.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_usigned_longlong(self):
        dt = np.min_scalar_type(2**63-1)
        wanted = np.dtype('uint64')
        assert_equal(wanted, dt) 
Example #15
Source File: systems.py    From westpa with MIT License 5 votes vote down vote up
def bin_target_counts(self, target_counts):
        maxcount = max(target_counts)
        self._bin_target_counts = numpy.array(target_counts, dtype=numpy.min_scalar_type(maxcount)) 
Example #16
Source File: colors.py    From coffeegrindsize with MIT License 5 votes vote down vote up
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 #17
Source File: colors.py    From CogAlg with MIT License 5 votes vote down vote up
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 np.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 #18
Source File: test_quantity_non_ufuncs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_min_scalar_type(self):
        out = np.min_scalar_type(self.q[0])
        expected = np.min_scalar_type(self.q.value[0])
        assert out == expected 
Example #19
Source File: colors.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
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 #20
Source File: test_grid.py    From pysheds with GNU General Public License v3.0 5 votes vote down vote up
def test_accumulation():
    # TODO: This breaks if clip_to's padding of dir is nonzero
    grid.clip_to('dir')
    grid.accumulation(data='dir', dirmap=dirmap, out_name='acc')
    assert(grid.acc.max() == acc_in_frame)
    # set nodata to 1
    eff = grid.view("eff")
    eff[eff==grid.eff.nodata] = 1
    grid.accumulation(data='dir', dirmap=dirmap, out_name='acc_eff', efficiency=eff)
    assert(abs(grid.acc_eff.max() - acc_in_frame_eff) < 0.001)
    assert(abs(grid.acc_eff[grid.acc==grid.acc.max()] - acc_in_frame_eff1) < 0.001)
    # TODO: Should eventually assert: grid.acc.dtype == np.min_scalar_type(grid.acc.max())
    grid.clip_to('catch', pad=(1,1,1,1))
    grid.accumulation(data='catch', dirmap=dirmap, out_name='acc')
    assert(grid.acc.max() == cells_in_catch)
    # Test accumulation on computed flowdirs
    grid.accumulation(data='d8_dir', dirmap=dirmap, out_name='d8_acc', routing='d8')
    grid.accumulation(data='dinf_dir', dirmap=dirmap, out_name='dinf_acc', routing='dinf')
    grid.accumulation(data='dinf_dir', dirmap=dirmap, out_name='dinf_acc', as_crs=new_crs,
                      routing='dinf')
    assert(grid.d8_acc.max() > 11300)
    assert(grid.dinf_acc.max() > 11400)
    #set nodata to 1
    eff = grid.view("dinf_eff")
    eff[eff==grid.dinf_eff.nodata] = 1
    grid.accumulation(data='dinf_dir', dirmap=dirmap, out_name='dinf_acc_eff', routing='dinf',
                      efficiency=eff)
    pos = np.where(grid.dinf_acc==grid.dinf_acc.max())
    assert(np.round(grid.dinf_acc[pos] / grid.dinf_acc_eff[pos]) == 4.) 
Example #21
Source File: binning.py    From westpa with MIT License 5 votes vote down vote up
def assign_to_bins(self):
        '''Assign WEST segment data to bins.  Requires the DataReader mixin to be in the inheritance tree'''
        self.require_binning_group()        
        
        n_iters = self.last_iter - self.first_iter + 1
        max_n_segs = self.max_iter_segs_in_range(self.first_iter, self.last_iter)
        pcoord_len = self.get_pcoord_len(self.first_iter)
        
        assignments = numpy.zeros((n_iters, max_n_segs,pcoord_len), numpy.min_scalar_type(self.n_bins))
        populations = numpy.zeros((n_iters, pcoord_len, self.n_bins), numpy.float64)
        
        westpa.rc.pstatus('Assigning to bins...')
        
        for (iiter, n_iter) in enumerate(range(self.first_iter, self.last_iter+1)):
            westpa.rc.pstatus('\r  Iteration {:d}'.format(n_iter), end='')
            seg_index = self.get_seg_index(n_iter)
            pcoords = self.get_iter_group(n_iter)['pcoord'][...]
            weights = seg_index['weight']
            
            for seg_id in range(len(seg_index)):
                assignments[iiter,seg_id,:] = self.mapper.assign(pcoords[seg_id,:,:])
            
            for it in range(pcoord_len):
                populations[iiter, it, :] = numpy.bincount(assignments[iiter,:len(seg_index),it], weights, minlength=self.n_bins)
        
            westpa.rc.pflush()
            del pcoords, weights, seg_index
         
        assignments_ds = self.binning_h5group.create_dataset('bin_assignments', data=assignments, compression='gzip')
        populations_ds = self.binning_h5group.create_dataset('bin_populations', data=populations, compression='gzip')
        
        for h5object in (self.binning_h5group, assignments_ds, populations_ds):
            self.record_data_iter_range(h5object)
            self.record_data_iter_step(h5object, 1)
            self.record_data_binhash(h5object)
                
        westpa.rc.pstatus() 
Example #22
Source File: w_eddist.py    From westpa with MIT License 5 votes vote down vote up
def go(self):
        
        pi = self.progress.indicator
        pi.operation = 'Initializing'
        with pi:
            self.duration = self.kinetics_file['durations'][self.iter_start-1:self.iter_stop-1]

            ##Only select transition events from specified istate to fstate
            mask = (self.duration['istate'] == self.istate) & (self.duration['fstate'] == self.fstate)

            self.duration_dsspec = DurationDataset(self.kinetics_file['durations']['duration'], mask, self.iter_start)
            self.wt_dsspec = DurationDataset(self.kinetics_file['durations']['weight'], mask, self.iter_start)

            self.output_file = h5py.File(self.output_filename, 'w')
            h5io.stamp_creator_data(self.output_file)

            # Construct bin boundaries
            self.construct_bins(self.parse_binspec(self.binspec))
            for idim, (binbounds, midpoints) in enumerate(zip(self.binbounds, self.midpoints)):
                self.output_file['binbounds_{}'.format(idim)] = binbounds
                self.output_file['midpoints_{}'.format(idim)] = midpoints

            # construct histogram
            self.construct_histogram()

            # Record iteration range        
            iter_range = numpy.arange(self.iter_start, self.iter_stop, 1, dtype=(numpy.min_scalar_type(self.iter_stop)))
            self.output_file['n_iter'] = iter_range
            self.output_file['histograms'].attrs['iter_start'] = self.iter_start
            self.output_file['histograms'].attrs['iter_stop'] = self.iter_stop
            
            self.output_file.close() 
Example #23
Source File: yamlcfg.py    From westpa with MIT License 5 votes vote down vote up
def bin_target_counts(self, target_counts):
        maxcount = max(target_counts)
        self._bin_target_counts = numpy.array(target_counts, dtype=numpy.min_scalar_type(maxcount)) 
Example #24
Source File: assign.py    From westpa with MIT License 5 votes vote down vote up
def __init__(self, functions):
        self.functions = functions
        self.nbins = len(functions)
        self.index_dtype = numpy.min_scalar_type(self.nbins)
        self.labels = [repr(func) for func in functions] 
Example #25
Source File: assign.py    From westpa with MIT License 5 votes vote down vote up
def __init__(self, func, nbins, args=None, kwargs=None):
        self.func = func
        self.args = args or ()
        self.kwargs = kwargs or {}
        self.nbins = nbins
        self.index_dtype = numpy.min_scalar_type(self.nbins)
        self.labels = ['{!r} bin {:d}'.format(func, ibin) for ibin in range(nbins)] 
Example #26
Source File: test_multiarray.py    From Computable with MIT License 5 votes vote down vote up
def test_usigned_shortshort(self):
        dt = np.min_scalar_type(2**8-1)
        wanted = np.dtype('uint8')
        assert_equal(wanted, dt) 
Example #27
Source File: test_multiarray.py    From Computable with MIT License 5 votes vote down vote up
def test_usigned_short(self):
        dt = np.min_scalar_type(2**16-1)
        wanted = np.dtype('uint16')
        assert_equal(wanted, dt) 
Example #28
Source File: test_multiarray.py    From Computable with MIT License 5 votes vote down vote up
def test_usigned_int(self):
        dt = np.min_scalar_type(2**32-1)
        wanted = np.dtype('uint32')
        assert_equal(wanted, dt) 
Example #29
Source File: test_multiarray.py    From Computable with MIT License 5 votes vote down vote up
def test_usigned_longlong(self):
        dt = np.min_scalar_type(2**63-1)
        wanted = np.dtype('uint64')
        assert_equal(wanted, dt) 
Example #30
Source File: test_multiarray.py    From Computable with MIT License 5 votes vote down vote up
def test_object(self):
        dt = np.min_scalar_type(2**64)
        wanted = np.dtype('O')
        assert_equal(wanted, dt)