Python numpy.iinfo() Examples
The following are 30 code examples for showing how to use numpy.iinfo(). 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: fine-lm Author: akzaidi File: speech_recognition.py License: MIT License | 6 votes |
def hparams(self, defaults, model_hparams): p = model_hparams # Filterbank extraction in bottom instead of preprocess_example is faster. p.add_hparam("audio_preproc_in_bottom", False) # The trainer seems to reserve memory for all members of the input dict p.add_hparam("audio_keep_example_waveforms", False) p.add_hparam("audio_sample_rate", 16000) p.add_hparam("audio_preemphasis", 0.97) p.add_hparam("audio_dither", 1.0 / np.iinfo(np.int16).max) p.add_hparam("audio_frame_length", 25.0) p.add_hparam("audio_frame_step", 10.0) p.add_hparam("audio_lower_edge_hertz", 20.0) p.add_hparam("audio_upper_edge_hertz", 8000.0) p.add_hparam("audio_num_mel_bins", 80) p.add_hparam("audio_add_delta_deltas", True) p.add_hparam("num_zeropad_frames", 250) p = defaults # p.stop_at_eos = int(False) p.input_modality = {"inputs": ("audio:speech_recognition_modality", None)} p.target_modality = (registry.Modalities.SYMBOL, 256)
Example 2
Project: contextualbandits Author: david-cortes File: utils.py License: BSD 2-Clause "Simplified" License | 6 votes |
def fit(self, X, y): if X.shape[0] == 0: return self elif np.unique(y).shape[0] <= 1: self.update_aux(y) return self seed = self.random_state.integers(np.iinfo(np.int32).max) self.model.set_params(random_state = seed) self.model.fit(X, y) n_nodes = self.model.tree_.node_count self.pos = np.zeros(n_nodes, dtype=ctypes.c_long) self.neg = np.zeros(n_nodes, dtype=ctypes.c_long) pred_node = self.model.apply(X).astype(ctypes.c_long) _create_node_counters(self.pos, self.neg, pred_node, y.astype(ctypes.c_double)) self.pos = self.pos.astype(ctypes.c_double) + self.beta_prior[0] self.neg = self.neg.astype(ctypes.c_double) + self.beta_prior[1] self.is_fitted = True return self
Example 3
Project: me-ica Author: ME-ICA File: arraywriters.py License: GNU Lesser General Public License v2.1 | 6 votes |
def _do_scaling(self): arr = self._array out_dtype = self._out_dtype assert out_dtype.kind in 'iu' mn, mx = self.finite_range() if arr.dtype.kind == 'f': # Float to (u)int scaling self._range_scale() return # (u)int to (u)int info = np.iinfo(out_dtype) out_max, out_min = info.max, info.min # If left as int64, uint64, comparisons will default to floats, and # these are inexact for > 2**53 - so convert to int if (as_int(mx) <= as_int(out_max) and as_int(mn) >= as_int(out_min)): # already in range return # (u)int to (u)int scaling self._iu2iu()
Example 4
Project: me-ica Author: ME-ICA File: minc.py License: GNU Lesser General Public License v2.1 | 6 votes |
def _get_valid_range(self): ''' Return valid range for image data The valid range can come from the image 'valid_range' or image 'valid_min' and 'valid_max', or, failing that, from the data type range ''' ddt = self.get_data_dtype() info = np.iinfo(ddt.type) try: valid_range = self._image.valid_range except AttributeError: try: valid_range = [self._image.valid_min, self._image.valid_max] except AttributeError: valid_range = [info.min, info.max] if valid_range[0] < info.min or valid_range[1] > info.max: raise ValueError('Valid range outside input ' 'data type range') return np.asarray(valid_range, dtype=np.float)
Example 5
Project: me-ica Author: ME-ICA File: test_arraywriters.py License: GNU Lesser General Public License v2.1 | 6 votes |
def test_int_int_min_max(): # Conversion between (u)int and (u)int eps = np.finfo(np.float64).eps rtol = 1e-6 for in_dt in IUINT_TYPES: iinf = np.iinfo(in_dt) arr = np.array([iinf.min, iinf.max], dtype=in_dt) for out_dt in IUINT_TYPES: try: aw = SlopeInterArrayWriter(arr, out_dt) except ScalingError: continue arr_back_sc = round_trip(aw) # integer allclose adiff = int_abs(arr - arr_back_sc) rdiff = adiff / (arr + eps) assert_true(np.all(rdiff < rtol))
Example 6
Project: me-ica Author: ME-ICA File: test_arraywriters.py License: GNU Lesser General Public License v2.1 | 6 votes |
def test_int_int_slope(): # Conversion between (u)int and (u)int for slopes only eps = np.finfo(np.float64).eps rtol = 1e-7 for in_dt in IUINT_TYPES: iinf = np.iinfo(in_dt) for out_dt in IUINT_TYPES: kinds = np.dtype(in_dt).kind + np.dtype(out_dt).kind if kinds in ('ii', 'uu', 'ui'): arrs = (np.array([iinf.min, iinf.max], dtype=in_dt),) elif kinds == 'iu': arrs = (np.array([iinf.min, 0], dtype=in_dt), np.array([0, iinf.max], dtype=in_dt)) for arr in arrs: try: aw = SlopeArrayWriter(arr, out_dt) except ScalingError: continue assert_false(aw.slope == 0) arr_back_sc = round_trip(aw) # integer allclose adiff = int_abs(arr - arr_back_sc) rdiff = adiff / (arr + eps) assert_true(np.all(rdiff < rtol))
Example 7
Project: me-ica Author: ME-ICA File: test_casting.py License: GNU Lesser General Public License v2.1 | 6 votes |
def test_able_casting(): # Check the able_int_type function guesses numpy out type types = np.sctypes['int'] + np.sctypes['uint'] for in_type in types: in_info = np.iinfo(in_type) in_mn, in_mx = in_info.min, in_info.max A = np.zeros((1,), dtype=in_type) for out_type in types: out_info = np.iinfo(out_type) out_mn, out_mx = out_info.min, out_info.max B = np.zeros((1,), dtype=out_type) ApBt = (A + B).dtype.type able_type = able_int_type([in_mn, in_mx, out_mn, out_mx]) if able_type is None: assert_equal(ApBt, np.float64) continue # Use str for comparison to avoid int32/64 vs intp comparison # failures assert_equal(np.dtype(ApBt).str, np.dtype(able_type).str)
Example 8
Project: radiometric_normalization Author: planetlabs File: normalize.py License: Apache License 2.0 | 6 votes |
def _linear_transformation_to_lut(linear_transformation, max_value=None, dtype=numpy.uint16): min_value = 0 if max_value is None: max_value = numpy.iinfo(dtype).max def gain_offset_to_lut(gain, offset): logging.debug( 'Normalize: Calculating lut values for gain ' '{} and offset {}'.format(gain, offset)) lut = numpy.arange(min_value, max_value + 1, dtype=numpy.float) return gain * lut + offset lut = gain_offset_to_lut(linear_transformation.gain, linear_transformation.offset) logging.debug('Normalize: Clipping lut from [{}, {}] to [{},{}]'.format( min(lut), max(lut), min_value, max_value)) numpy.clip(lut, min_value, max_value, lut) return lut.astype(dtype)
Example 9
Project: radiometric_normalization Author: planetlabs File: time_stack.py License: Apache License 2.0 | 6 votes |
def _uniform_weight_alpha(sum_masked_arrays, output_datatype): '''Calculates the cumulative mask of a list of masked array Input: sum_masked_arrays (list of numpy masked arrays): The list of masked arrays to find the cumulative mask of, each element represents one band. (sums_masked_array.mask has a 1 for a no data pixel and a 0 otherwise) output_datatype (numpy datatype): The output datatype Output: output_alpha (numpy uint16 array): The output mask (0 for a no data pixel, uint16 max value otherwise) ''' output_alpha = numpy.ones(sum_masked_arrays[0].shape) for band_sum_masked_array in sum_masked_arrays: output_alpha[numpy.nonzero(band_sum_masked_array.mask == 1)] = 0 output_alpha = output_alpha.astype(output_datatype) * \ numpy.iinfo(output_datatype).max return output_alpha
Example 10
Project: mabwiser Author: fidelity File: base_mab.py License: Apache License 2.0 | 6 votes |
def _parallel_predict(self, contexts: np.ndarray, is_predict: bool): # Total number of contexts to predict n_contexts = len(contexts) # Partition contexts by job n_jobs, n_contexts, starts = self._partition_contexts(n_contexts) total_contexts = sum(n_contexts) # Get seed value for each context seeds = self.rng.randint(np.iinfo(np.int32).max, size=total_contexts) # Perform parallel predictions predictions = Parallel(n_jobs=n_jobs, backend=self.backend)( delayed(self._predict_contexts)( contexts[starts[i]:starts[i + 1]], is_predict, seeds[starts[i]:starts[i + 1]], starts[i]) for i in range(n_jobs)) # Reduce predictions = list(chain.from_iterable(t for t in predictions)) return predictions if len(predictions) > 1 else predictions[0]
Example 11
Project: recruit Author: Frank-qlu File: test_index_tricks.py License: Apache License 2.0 | 6 votes |
def test_big_indices(self): # ravel_multi_index for big indices (issue #7546) if np.intp == np.int64: arr = ([1, 29], [3, 5], [3, 117], [19, 2], [2379, 1284], [2, 2], [0, 1]) assert_equal( np.ravel_multi_index(arr, (41, 7, 120, 36, 2706, 8, 6)), [5627771580, 117259570957]) # test overflow checking for too big array (issue #7546) dummy_arr = ([0],[0]) half_max = np.iinfo(np.intp).max // 2 assert_equal( np.ravel_multi_index(dummy_arr, (half_max, 2)), [0]) assert_raises(ValueError, np.ravel_multi_index, dummy_arr, (half_max+1, 2)) assert_equal( np.ravel_multi_index(dummy_arr, (half_max, 2), order='F'), [0]) assert_raises(ValueError, np.ravel_multi_index, dummy_arr, (half_max+1, 2), order='F')
Example 12
Project: recruit Author: Frank-qlu File: test_core.py License: Apache License 2.0 | 6 votes |
def test_allclose(self): # Tests allclose on arrays a = np.random.rand(10) b = a + np.random.rand(10) * 1e-8 assert_(allclose(a, b)) # Test allclose w/ infs a[0] = np.inf assert_(not allclose(a, b)) b[0] = np.inf assert_(allclose(a, b)) # Test allclose w/ masked a = masked_array(a) a[-1] = masked assert_(allclose(a, b, masked_equal=True)) assert_(not allclose(a, b, masked_equal=False)) # Test comparison w/ scalar a *= 1e-8 a[0] = 0 assert_(allclose(a, 0, masked_equal=True)) # Test that the function works for MIN_INT integer typed arrays a = masked_array([np.iinfo(np.int_).min], dtype=np.int_) assert_(allclose(a, a))
Example 13
Project: recruit Author: Frank-qlu File: test_random.py License: Apache License 2.0 | 6 votes |
def test_respect_dtype_singleton(self): # See gh-7203 for dt in self.itype: lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1 sample = self.rfunc(lbnd, ubnd, dtype=dt) assert_equal(sample.dtype, np.dtype(dt)) for dt in (bool, int, np.long): lbnd = 0 if dt is bool else np.iinfo(dt).min ubnd = 2 if dt is bool else np.iinfo(dt).max + 1 # gh-7284: Ensure that we get Python data types sample = self.rfunc(lbnd, ubnd, dtype=dt) assert_(not hasattr(sample, 'dtype')) assert_equal(type(sample), dt)
Example 14
Project: recruit Author: Frank-qlu File: test_numeric.py License: Apache License 2.0 | 6 votes |
def test_can_cast_values(self): # gh-5917 for dt in np.sctypes['int'] + np.sctypes['uint']: ii = np.iinfo(dt) assert_(np.can_cast(ii.min, dt)) assert_(np.can_cast(ii.max, dt)) assert_(not np.can_cast(ii.min - 1, dt)) assert_(not np.can_cast(ii.max + 1, dt)) for dt in np.sctypes['float']: fi = np.finfo(dt) assert_(np.can_cast(fi.min, dt)) assert_(np.can_cast(fi.max, dt)) # Custom exception class to test exception propagation in fromiter
Example 15
Project: recruit Author: Frank-qlu File: datetimelike.py License: Apache License 2.0 | 6 votes |
def argmin(self, axis=None, skipna=True, *args, **kwargs): """ Returns the indices of the minimum values along an axis. See `numpy.ndarray.argmin` for more information on the `axis` parameter. See Also -------- numpy.ndarray.argmin """ nv.validate_argmin(args, kwargs) nv.validate_minmax_axis(axis) i8 = self.asi8 if self.hasnans: mask = self._isnan if mask.all() or not skipna: return -1 i8 = i8.copy() i8[mask] = np.iinfo('int64').max return i8.argmin()
Example 16
Project: ffn Author: google File: segmentation.py License: Apache License 2.0 | 6 votes |
def reduce_id_bits(segmentation): """Reduces the number of bits used for IDs. Assumes that one additional ID beyond the max of 'segmentation' is necessary (used by GALA to mark boundary areas). Args: segmentation: ndarray of int type Returns: segmentation ndarray converted to minimal uint type large enough to keep all the IDs. """ max_id = segmentation.max() if max_id <= np.iinfo(np.uint8).max: return segmentation.astype(np.uint8) elif max_id <= np.iinfo(np.uint16).max: return segmentation.astype(np.uint16) elif max_id <= np.iinfo(np.uint32).max: return segmentation.astype(np.uint32)
Example 17
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: common.py License: Apache License 2.0 | 5 votes |
def random_seed(seed=None): """ Runs a code block with a new seed for np, mx and python's random. Parameters ---------- seed : the seed to pass to np.random, mx.random and python's random. To impose rng determinism, invoke e.g. as in: with random_seed(1234): ... To impose rng non-determinism, invoke as in: with random_seed(): ... Upon conclusion of the block, the rng's are returned to a state that is a function of their pre-block state, so any prior non-determinism is preserved. """ try: next_seed = np.random.randint(0, np.iinfo(np.int32).max) if seed is None: np.random.seed() seed = np.random.randint(0, np.iinfo(np.int32).max) logger = default_logger() logger.debug('Setting np, mx and python random seeds = %s', seed) np.random.seed(seed) mx.random.seed(seed) random.seed(seed) yield finally: # Reinstate prior state of np.random and other generators np.random.seed(next_seed) mx.random.seed(next_seed) random.seed(next_seed)
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: common.py License: Apache License 2.0 | 5 votes |
def random_seed(seed=None): """ Runs a code block with a new seed for np, mx and python's random. Parameters ---------- seed : the seed to pass to np.random, mx.random and python's random. To impose rng determinism, invoke e.g. as in: with random_seed(1234): ... To impose rng non-determinism, invoke as in: with random_seed(): ... Upon conclusion of the block, the rng's are returned to a state that is a function of their pre-block state, so any prior non-determinism is preserved. """ try: next_seed = np.random.randint(0, np.iinfo(np.int32).max) if seed is None: np.random.seed() seed = np.random.randint(0, np.iinfo(np.int32).max) logger = default_logger() logger.debug('Setting np, mx and python random seeds = %s', seed) np.random.seed(seed) mx.random.seed(seed) random.seed(seed) yield finally: # Reinstate prior state of np.random and other generators np.random.seed(next_seed) mx.random.seed(next_seed) random.seed(next_seed)
Example 19
Project: fine-lm Author: akzaidi File: speech_recognition.py License: MIT License | 5 votes |
def encode(self, s): """Transform a string with a filename into a list of float32. Args: s: path to the file with a waveform. Returns: samples: list of int16s """ # Make sure that the data is a single channel, 16bit, 16kHz wave. # TODO(chorowski): the directory may not be writable, this should fallback # to a temp path, and provide instructions for installing sox. if s.endswith(".mp3"): # TODO(dliebling) On Linux, check if libsox-fmt-mp3 is installed. out_filepath = s[:-4] + ".wav" call([ "sox", "--guard", s, "-r", "16k", "-b", "16", "-c", "1", out_filepath ]) s = out_filepath elif not s.endswith(".wav"): out_filepath = s + ".wav" if not os.path.exists(out_filepath): call(["sox", "-r", "16k", "-b", "16", "-c", "1", s, out_filepath]) s = out_filepath rate, data = wavfile.read(s) assert rate == self._sample_rate assert len(data.shape) == 1 if data.dtype not in [np.float32, np.float64]: data = data.astype(np.float32) / np.iinfo(data.dtype).max return data.tolist()
Example 20
Project: contextualbandits Author: david-cortes File: utils.py License: BSD 2-Clause "Simplified" License | 5 votes |
def _check_random_state(random_state): if random_state is None: return np.random.Generator(np.random.MT19937()) if isinstance(random_state, np.random.Generator): return random_state elif isinstance(random_state, np.random.RandomState) or (random_state == np.random): random_state = int(random_state.randint(np.iinfo(np.int32).max) + 1) if isinstance(random_state, float): random_state = int(random_state) assert random_state > 0 return np.random.Generator(np.random.MT19937(seed = random_state))
Example 21
Project: contextualbandits Author: david-cortes File: utils.py License: BSD 2-Clause "Simplified" License | 5 votes |
def _spawn_arm(self, fitted_classifier = None, n_w_rew = 0, n_wo_rew = 0, buffer_X = None, buffer_y = None): self.n += 1 self.rng_arm.append(self.random_state if (self.random_state == np.random) else \ _check_random_state( self.random_state.integers(np.iinfo(np.int32).max) + 1)) if self.smooth is not None: self.counters = np.c_[self.counters, np.array([n_w_rew + n_wo_rew]).reshape((1, 1)).astype(self.counters.dtype)] if (self.force_counters) or (self.thr > 0 and not self.force_fit): new_beta_col = np.array([0 if (n_w_rew + n_wo_rew) < self.thr else 1, self.alpha + n_w_rew, self.beta + n_wo_rew]).reshape((3, 1)).astype(self.beta_counters.dtype) self.beta_counters = np.c_[self.beta_counters, new_beta_col] if fitted_classifier is not None: if 'predict_proba' not in dir(fitted_classifier): fitted_classifier = _convert_decision_function_w_sigmoid(fitted_classifier) if partialfit: fitted_classifier = _add_method_predict_robust(fitted_classifier) self.algos.append(fitted_classifier) else: if self.force_fit or self.partialfit: if self.base is None: raise ValueError("Must provide a classifier when initializing with different classifiers per arm.") self.algos.append( deepcopy(self.base) ) else: if (self.force_counters) or (self.thr > 0 and not self.force_fit): self.algos.append(_BetaPredictor(self.beta_counters[:, -1][1], self.beta_counters[:, -1][2], self.rng_arm[-1])) else: self.algos.append(_ZeroPredictor()) if (self.buffer is not None): self.buffer.append(_RefitBuffer(self.refit_buffer, self.deep_copy, self.rng_arm[-1])) if (buffer_X is not None): self.buffer[-1].add_obs(bufferX, buffer_y)
Example 22
Project: contextualbandits Author: david-cortes File: offpolicy.py License: BSD 2-Clause "Simplified" License | 5 votes |
def fit(self, X, a, r, p): """ Fits the Offset Tree estimator to partially-labeled data collected from a different policy. Parameters ---------- X : array (n_samples, n_features) Matrix of covariates for the available data. a : array (n_samples), int type Arms or actions that were chosen for each observations. r : array (n_samples), {0,1} Rewards that were observed for the chosen actions. Must be binary rewards 0/1. p : array (n_samples) Reward estimates for the actions that were chosen by the policy. """ X, a, r = _check_fit_input(X, a, r) p = _check_1d_inp(p) assert p.shape[0] == X.shape[0] if self.c is not None: p = self.c * p if self.pmin is not None: p = np.clip(p, a_min = self.pmin, a_max = None) self._oracles = [deepcopy(self.base_algorithm) for c in range(self.nchoices - 1)] rs = self.random_state.integers(np.iinfo(np.int32).max, size=self.nchoices) Parallel(n_jobs=self.njobs, verbose=0, require="sharedmem")\ (delayed(self._fit)(classif, X, a, r, p, rs) \ for classif in range(len(self._oracles))) self.is_fitted = True
Example 23
Project: tensortrade Author: tensortrade-org File: parallel_dqn_trainer.py License: Apache License 2.0 | 5 votes |
def __init__(self, agent: 'ParallelDQNAgent', create_env: Callable[[None], 'TrainingEnvironment'], memory_queue: Queue, model_update_queue: Queue, done_queue: Queue, n_steps: int, n_episodes: int, eps_end: int = 0.05, eps_start: int = 0.99, eps_decay_steps: int = 2000, update_target_every: int = 2): super().__init__() self.agent = agent self.env = create_env() self.memory_queue = memory_queue self.model_update_queue = model_update_queue self.done_queue = done_queue self.n_steps = n_steps or np.iinfo(np.int32).max self.n_episodes = n_episodes or np.iinfo(np.int32).max self.eps_start = eps_start self.eps_end = eps_end self.eps_decay_steps = eps_decay_steps self.update_target_every = update_target_every self.env.agent_id = self.agent.id
Example 24
Project: me-ica Author: ME-ICA File: nifti1.py License: GNU Lesser General Public License v2.1 | 5 votes |
def set_data_shape(self, shape): ''' Set shape of data If ``ndims == len(shape)`` then we set zooms for dimensions higher than ``ndims`` to 1.0 Parameters ---------- shape : sequence sequence of integers specifying data array shape Notes ----- Applies freesurfer hack for large vectors described in https://github.com/nipy/nibabel/issues/100 and http://code.google.com/p/fieldtrip/source/browse/trunk/external/freesurfer/save_nifti.m?spec=svn5022&r=5022#77 ''' # Apply freesurfer hack for vector hdr = self._structarr shape = tuple(shape) if (len(shape) == 3 and shape[1:] == (1, 1) and shape[0] > np.iinfo(hdr['dim'].dtype.base).max): # Freesurfer case try: hdr['glmin'] = shape[0] except OverflowError: overflow = True else: overflow = hdr['glmin'] != shape[0] if overflow: raise HeaderDataError('shape[0] %s does not fit in glmax datatype' % shape[0]) warnings.warn('Using large vector Freesurfer hack; header will ' 'not be compatible with SPM or FSL', stacklevel=2) shape = (-1, 1, 1) super(Nifti1Header, self).set_data_shape(shape)
Example 25
Project: me-ica Author: ME-ICA File: casting.py License: GNU Lesser General Public License v2.1 | 5 votes |
def able_int_type(values): """ Find the smallest integer numpy type to contain sequence `values` Prefers uint to int if minimum is >= 0 Parameters ---------- values : sequence sequence of integer values Returns ------- itype : None or numpy type numpy integer type or None if no integer type holds all `values` Examples -------- >>> able_int_type([0, 1]) == np.uint8 True >>> able_int_type([-1, 1]) == np.int8 True """ if any([v % 1 for v in values]): return None mn = min(values) mx = max(values) if mn >= 0: for ityp in np.sctypes['uint']: if mx <= np.iinfo(ityp).max: return ityp for ityp in np.sctypes['int']: info = np.iinfo(ityp) if mn >= info.min and mx <= info.max: return ityp return None
Example 26
Project: me-ica Author: ME-ICA File: arraywriters.py License: GNU Lesser General Public License v2.1 | 5 votes |
def _iu2iu(self): # (u)int to (u)int mn, mx = [as_int(v) for v in self.finite_range()] # range may be greater than the largest integer for this type. # as_int needed to work round numpy 1.4.1 int casting bug out_dtype = self._out_dtype t_min, t_max = np.iinfo(out_dtype).min, np.iinfo(out_dtype).max type_range = as_int(t_max) - as_int(t_min) mn2mx = mx - mn if mn2mx <= type_range: # might offset be enough? if t_min == 0: # uint output - take min to 0 # decrease offset with floor_exact, meaning mn >= t_min after # subtraction. But we may have pushed the data over t_max, # which we check below inter = floor_exact(mn - t_min, self.scaler_dtype) else: # int output - take midpoint to 0 # ceil below increases inter, pushing scale up to 0.5 towards # -inf, because ints have abs min == abs max + 1 midpoint = mn + as_int(np.ceil(mn2mx / 2.0)) # Floor exact decreases inter, so pulling scaled values more # positive. This may make mx - inter > t_max inter = floor_exact(midpoint, self.scaler_dtype) # Need to check still in range after floor_exact-ing int_inter = as_int(inter) assert mn - int_inter >= t_min if mx - int_inter <= t_max: self.inter = inter return # Try slope options (sign flip) and then range scaling super(SlopeInterArrayWriter, self)._iu2iu()
Example 27
Project: me-ica Author: ME-ICA File: arraywriters.py License: GNU Lesser General Public License v2.1 | 5 votes |
def _range_scale(self): """ Calculate scaling, intercept based on data range and output type """ mn, mx = self.finite_range() # Values of self.array.dtype type out_dtype = self._out_dtype if mx == mn: # Only one number in array self.inter = mn return # Straight mx-mn can overflow. big_float = best_float() # usually longdouble except in win 32 if mn.dtype.kind == 'f': # Already floats # float64 and below cast correctly to longdouble. Longdouble needs # no casting mn2mx = np.diff(np.array([mn, mx], dtype=big_float)) else: # max possible (u)int range is 2**64-1 (int64, uint64) # int_to_float covers this range. On windows longdouble is the same # as double so mn2mx will be 2**64 - thus overestimating slope # slightly. Casting to int needed to allow mx-mn to be larger than # the largest (u)int value mn2mx = int_to_float(as_int(mx) - as_int(mn), big_float) if out_dtype.kind == 'f': # Type range, these are also floats info = type_info(out_dtype) t_mn_mx = info['min'], info['max'] else: t_mn_mx = np.iinfo(out_dtype).min, np.iinfo(out_dtype).max t_mn_mx= [int_to_float(v, big_float) for v in t_mn_mx] # We want maximum precision for the calculations. Casting will # not lose precision because min/max are of fp type. assert [v.dtype.kind for v in t_mn_mx] == ['f', 'f'] scaled_mn2mx = np.diff(np.array(t_mn_mx, dtype = big_float)) slope = mn2mx / scaled_mn2mx self.inter = mn - t_mn_mx[0] * slope self.slope = slope if not np.all(np.isfinite([self.slope, self.inter])): raise ScalingError("Slope / inter not both finite")
Example 28
Project: me-ica Author: ME-ICA File: test_casting.py License: GNU Lesser General Public License v2.1 | 5 votes |
def test_int_abs(): for itype in np.sctypes['int']: info = np.iinfo(itype) in_arr = np.array([info.min, info.max], dtype=itype) idtype = np.dtype(itype) udtype = np.dtype(idtype.str.replace('i', 'u')) assert_equal(udtype.kind, 'u') assert_equal(idtype.itemsize, udtype.itemsize) mn, mx = in_arr e_mn = as_int(mx) + 1 # as_int needed for numpy 1.4.1 casting assert_equal(int_abs(mx), mx) assert_equal(int_abs(mn), e_mn) assert_array_equal(int_abs(in_arr), [e_mn, mx])
Example 29
Project: me-ica Author: ME-ICA File: test_round_trip.py License: GNU Lesser General Public License v2.1 | 5 votes |
def test_round_trip(): scaling_type = np.float32 rng = np.random.RandomState(20111121) N = 10000 sd_10s = range(-20, 51, 5) iuint_types = np.sctypes['int'] + np.sctypes['uint'] # Remove intp types, which cannot be set into nifti header datatype iuint_types.remove(np.intp) iuint_types.remove(np.uintp) f_types = [np.float32, np.float64] # Expanding standard deviations for i, sd_10 in enumerate(sd_10s): sd = 10.0**sd_10 V_in = rng.normal(0, sd, size=(N,1)) for j, in_type in enumerate(f_types): for k, out_type in enumerate(iuint_types): check_arr(sd_10, V_in, in_type, out_type, scaling_type) # Spread integers across range for i, sd in enumerate(np.linspace(0.05, 0.5, 5)): for j, in_type in enumerate(iuint_types): info = np.iinfo(in_type) mn, mx = info.min, info.max type_range = mx - mn center = type_range / 2.0 + mn # float(sd) because type_range can be type 'long' width = type_range * float(sd) V_in = rng.normal(center, width, size=(N,1)) for k, out_type in enumerate(iuint_types): check_arr(sd, V_in, in_type, out_type, scaling_type)
Example 30
Project: me-ica Author: ME-ICA File: test_floating.py License: GNU Lesser General Public License v2.1 | 5 votes |
def test_as_int_np_fix(): # Test as_int works for integers. We need as_int for integers because of a # numpy 1.4.1 bug such that int(np.uint32(2**32-1) == -1 for t in np.sctypes['int'] + np.sctypes['uint']: info = np.iinfo(t) mn, mx = np.array([info.min, info.max], dtype=t) assert_equal((mn, mx), (as_int(mn), as_int(mx)))