Python numpy.digitize() Examples
The following are 30 code examples for showing how to use numpy.digitize(). 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: xrft Author: xgcm File: xrft.py License: MIT License | 6 votes |
def _radial_wvnum(k, l, N, nfactor): """ Creates a radial wavenumber based on two horizontal wavenumbers along with the appropriate index map """ # compute target wavenumbers k = k.values l = l.values K = np.sqrt(k[np.newaxis,:]**2 + l[:,np.newaxis]**2) nbins = int(N/nfactor) if k.max() > l.max(): ki = np.linspace(0., l.max(), nbins) else: ki = np.linspace(0., k.max(), nbins) # compute bin index kidx = np.digitize(np.ravel(K), ki) # compute number of points for each wavenumber area = np.bincount(kidx) # compute the average radial wavenumber for each bin kr = (np.bincount(kidx, weights=K.ravel()) / np.ma.masked_where(area==0, area)) return ki, kr[1:-1]
Example 2
Project: EXOSIMS Author: dsavransky File: kopparapuPlot.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def quantize(self, specs, plan_id, star_ind): r'''Compute the radius, luminosity, and combined bins for a given planet and star. This is Rhonda's original code. It is here to allow cross-checks but is not used now. Returns None if the planet/star lies outside the bin boundaries.''' # return value indicating error error_rval = (None, None, None) # extract planet values Rp_single = strip_units(specs['Rp'][plan_id]) a_single = strip_units(specs['a'][plan_id]) L_star = specs['L'][star_ind] L_plan = L_star/a_single**2 # bin them tmpbinplace_Rp = np.digitize(Rp_single, self.Rp_bins) if tmpbinplace_Rp >= len(self.Rp_bins): return error_rval # out of range tmpbinplace_L = np.digitize(L_plan, self.L_bins[tmpbinplace_Rp-1]) if tmpbinplace_L >= len(self.L_bins[tmpbinplace_Rp-1]): return error_rval # out of range Rp_bin = tmpbinplace_Rp.item() L_bin = tmpbinplace_L.item() RpL_bin = tmpbinplace_L.item() + 10*tmpbinplace_Rp.item() return Rp_bin, L_bin, RpL_bin
Example 3
Project: EXOSIMS Author: dsavransky File: kopparapuPlot.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def quantize_final(self, specs, plan_id, star_ind): r'''Compute the final radius/luminosity bin, an integer, for a given planet and star. Returns 0 if the planet/star lies outside the bin boundaries. Returns 1..15 otherwise.''' # extract planet and star properties Rp_plan = strip_units(specs['Rp'][plan_id]) a_plan = strip_units(specs['a'][plan_id]) L_star = specs['L'][star_ind] L_plan = L_star / (a_plan**2) # adjust star luminosity by distance^2 in AU # Bin by Rp. "too low" maps to 0, and "too high" maps to len(Rp_bins). Rp_bin = np.digitize(Rp_plan, self.Rp_bins) # index into L_bins array: if Rp is out-of-range, index is irrelevant Rp_bin_index = Rp_bin-1 if (Rp_bin > 0) and (Rp_bin < len(self.Rp_bins)) else 0 # bin by L L_bin = np.digitize(L_plan, self.L_bins[Rp_bin_index]) # map the pair (Rp,L) -> RpL # Rp_bin and L_bin are np arrays, so need to cast to integers return self.Rp_L_to_RpL_bin[(int(Rp_bin), int(L_bin))]
Example 4
Project: ibllib Author: int-brain-lab File: population.py License: MIT License | 6 votes |
def _index_of(arr, lookup): """Replace scalars in an array by their indices in a lookup table. Implicitely assume that: * All elements of arr and lookup are non-negative integers. * All elements or arr belong to lookup. This is not checked for performance reasons. """ # Equivalent of np.digitize(arr, lookup) - 1, but much faster. # TODO: assertions to disable in production for performance reasons. # TODO: np.searchsorted(lookup, arr) is faster on small arrays with large # values lookup = np.asarray(lookup, dtype=np.int32) m = (lookup.max() if len(lookup) else 0) + 1 tmp = np.zeros(m + 1, dtype=np.int) # Ensure that -1 values are kept. tmp[-1] = -1 if len(lookup): tmp[lookup] = np.arange(len(lookup)) return tmp[arr]
Example 5
Project: ibllib Author: int-brain-lab File: cca.py License: MIT License | 6 votes |
def bin_spikes_trials(spikes, trials, bin_size=0.01): """ Binarizes the spike times into a raster and assigns a trial number to each bin :param spikes: spikes object :type spikes: Bunch :param trials: trials object :type trials: Bunch :param bin_size: size, in s, of the bins :type bin_size: float :return: a matrix (bins, SpikeCounts), and a vector of bins size with trial ID, and a vector bins size with the time that the bins start """ binned_spikes, bin_times, _ = bincount2D(spikes['times'], spikes['clusters'], bin_size) trial_start_times = trials['intervals'][:, 0] binned_trialIDs = np.digitize(bin_times, trial_start_times) # correct, as index 0 is whatever happens before the first trial binned_trialIDs_corrected = binned_trialIDs - 1 return binned_spikes.T, binned_trialIDs_corrected, bin_times
Example 6
Project: typhon Author: atmtools File: stats.py License: MIT License | 6 votes |
def binned_statistic(coords, data, bins, statistic=None): """Bin data and calculate statistics on them As :func:`scipy.stats.binned_statistic` but faster for simple statistics. Args: coords: 1-dimensional numpy.array with the coordinates that should be binned. data: A numpy.array on which the statistic should be applied. bins: The bins used for the binning. statistic: So far only *mean* is implemented. Returns: A numpy array with statistic results. """ bin_indices = numpy.digitize(coords, bins) data_sum = numpy.bincount(bin_indices, weights=data, minlength=bins.size) data_number = numpy.bincount(bin_indices, minlength=bins.size) return data_sum / data_number
Example 7
Project: westpa Author: westpa File: data_manager.py License: MIT License | 6 votes |
def _find_multi_iter_group(self, n_iter, master_group_name): with self.lock: master_group = self.we_h5file[master_group_name] master_index = master_group['index'][...] set_id = numpy.digitize([n_iter], master_index['iter_valid']) - 1 group_ref = master_index[set_id]['group_ref'] # Check if reference is Null if not bool(group_ref): return None # This extra [0] is to work around a bug in h5py try: group = self.we_h5file[group_ref] except AttributeError: group = self.we_h5file[group_ref[0]] else: log.debug('h5py fixed; remove alternate code path') log.debug('reference {!r} points to group {!r}'.format(group_ref, group)) return group
Example 8
Project: lingvo Author: tensorflow File: breakdown_metric.py License: Apache License 2.0 | 6 votes |
def Discretize(self, num_points): num_points_binned = np.digitize(num_points, self._LogSpacedBinEdgesofPoints()) # index == 0 corresponds to boxes with 0 points. Because we plot everything # logarithmically, this is a pain in the buttocks. For simplicity, we merely # accumulate the boxes with 0 points into the first bin. num_points_binned[num_points_binned == 0] = 1 num_bins = len(self._LogSpacedBinEdgesofPoints()) # index == len(self._LogSpacedBinEdgesofPoints()) corresponds to # points with to points outside of the range of the last edge. We map # these points back to the final bucket for simplicity. num_points_binned[num_points_binned == num_bins] -= 1 # There is an inconsistency between how np.digitize() and np.histogram() # index their bins and this is due to the fact that index == 0 is reserved # for examples less than the minimum bin edge. num_points_binned -= 1 return num_points_binned
Example 9
Project: lingvo Author: tensorflow File: breakdown_metric.py License: Apache License 2.0 | 6 votes |
def Discretize(self, bboxes): rotations = self._CalculateRotation(bboxes) p = self.params bin_width = ( p.metadata.MaximumRotation() / float(self.NumBinsOfHistogram())) # TODO(shlens): Consider merging the entries with -1 and 0 bin index # because rotation is circular. rotations_binned = np.digitize( rotations, np.arange(0.0, p.metadata.MaximumRotation(), bin_width)) # index == 0 corresponds to distances outside less than 0.0. Since this is # not possible, we discard this possibility and make the output 0 indexed to # match the behavior of np.histogram(). assert np.all(rotations_binned > 0.0), ('Rotation is negative: %s' % rotations_binned) rotations_binned -= 1 return rotations_binned
Example 10
Project: deep-head-pose Author: natanielruiz File: datasets.py License: Apache License 2.0 | 6 votes |
def __getitem__(self, index): img = Image.open(os.path.join(self.data_dir, self.X_train[index] + self.img_ext)) img = img.convert(self.image_mode) txt_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext) # We get the pose in radians annot = open(txt_path, 'r') line = annot.readline().split(' ') pose = [float(line[1]), float(line[2]), float(line[3])] # And convert to degrees. yaw = pose[0] * 180 / np.pi pitch = pose[1] * 180 / np.pi roll = pose[2] * 180 / np.pi # Fix the roll in AFLW roll *= -1 # Bin values bins = np.array(range(-99, 102, 3)) labels = torch.LongTensor(np.digitize([yaw, pitch, roll], bins) - 1) cont_labels = torch.FloatTensor([yaw, pitch, roll]) if self.transform is not None: img = self.transform(img) return img, labels, cont_labels, self.X_train[index]
Example 11
Project: visual_foresight Author: SudeepDasari File: combine_score.py License: MIT License | 6 votes |
def make_stats(dir, score, name, bounds): bin_edges = np.linspace(bounds[0], bounds[1], 11) binned_ind = np.digitize(score, bin_edges) occurrence, _ = np.histogram(score, bin_edges, density=False) bin_width = bin_edges[1] - bin_edges[0] bin_mid = bin_edges + bin_width / 2 plt.figure() plt.bar(bin_mid[:-1], occurrence, bin_width, facecolor='b', alpha=0.5) plt.title(name) plt.xlabel(name) plt.ylabel('occurences') plt.savefig(dir + '/' + name + '.png') plt.close() f = open(dir + '/{}_histo.txt'.format(name), 'w') for i in range(bin_edges.shape[0]-1): f.write('indices for bin {}, {} to {} : {} \n'.format(i, bin_edges[i], bin_edges[i+1], np.where(binned_ind == i+1)[0].tolist()))
Example 12
Project: RaptorX-Contact Author: j3xugit File: DistanceUtils.py License: GNU General Public License v3.0 | 6 votes |
def DiscretizeDistMatrix(distm, bins=None, invalidDistanceSeparated=False): assert (bins is not None) result = np.digitize(distm, bins) - 1 if invalidDistanceSeparated: ## -1 and the maximum distance bin are separated np.putmask(result, result == -1, len(bins) ) labels = range( len(bins) + 1 ) else: ## -1 and the maximum distance bin are merged into a single bin np.putmask(result, result == -1, len(bins) -1 ) labels = range( len(bins) ) return result.astype(np.int32), np.int32(labels), bins ## calculate the natural logarithm of a matrix
Example 13
Project: ms_deisotope Author: mobiusklein File: lsh.py License: Apache License 2.0 | 6 votes |
def discretize(self, span=None): if span is None: span = self.span else: span = self.span = max(span, self._guess_span()) num_bins = power_of_2(int(math.ceil(span / self.epsilon))) bins = np.linspace(0, span + self.epsilon, num=num_bins) min_mz = self.peak_set[0].mz # This ignores the possibility that a peak delta falls outside of span self.indices = np.digitize([peak.mz - min_mz for peak in self.peak_set], bins) self.binned_signal = np.zeros(num_bins) for i, peak in enumerate(self.peak_set): # The original implementation just set the bin to exp(intensity) of the last # peak to fall in it, but exp() overflows with numbers higher than ~700. # # The absolute magnitude of the value here doesn't matter, just that it is # non-zero? self.binned_signal[self.indices[i]] += peak.intensity
Example 14
Project: DOTA_models Author: ringringyi File: graph_utils.py License: Apache License 2.0 | 5 votes |
def _rejection_sampling(rng, sampling_d, target_d, bins, hardness, M): bin_ind = np.digitize(hardness, bins)-1 i = 0 ratio = target_d[bin_ind] / (M*sampling_d[bin_ind]) while i < ratio.size and rng.rand() > ratio[i]: i = i+1 return i
Example 15
Project: DOTA_models Author: ringringyi File: depth_utils.py License: Apache License 2.0 | 5 votes |
def bin_points(XYZ_cms, map_size, z_bins, xy_resolution): """Bins points into xy-z bins XYZ_cms is ... x H x W x3 Outputs is ... x map_size x map_size x (len(z_bins)+1) """ sh = XYZ_cms.shape XYZ_cms = XYZ_cms.reshape([-1, sh[-3], sh[-2], sh[-1]]) n_z_bins = len(z_bins)+1 map_center = (map_size-1.)/2. counts = [] isvalids = [] for XYZ_cm in XYZ_cms: isnotnan = np.logical_not(np.isnan(XYZ_cm[:,:,0])) X_bin = np.round(XYZ_cm[:,:,0] / xy_resolution + map_center).astype(np.int32) Y_bin = np.round(XYZ_cm[:,:,1] / xy_resolution + map_center).astype(np.int32) Z_bin = np.digitize(XYZ_cm[:,:,2], bins=z_bins).astype(np.int32) isvalid = np.array([X_bin >= 0, X_bin < map_size, Y_bin >= 0, Y_bin < map_size, Z_bin >= 0, Z_bin < n_z_bins, isnotnan]) isvalid = np.all(isvalid, axis=0) ind = (Y_bin * map_size + X_bin) * n_z_bins + Z_bin ind[np.logical_not(isvalid)] = 0 count = np.bincount(ind.ravel(), isvalid.ravel().astype(np.int32), minlength=map_size*map_size*n_z_bins) count = np.reshape(count, [map_size, map_size, n_z_bins]) counts.append(count) isvalids.append(isvalid) counts = np.array(counts).reshape(list(sh[:-3]) + [map_size, map_size, n_z_bins]) isvalids = np.array(isvalids).reshape(list(sh[:-3]) + [sh[-3], sh[-2], 1]) return counts, isvalids
Example 16
Project: reinforcement_learning Author: yrlu File: cartpole_qlearning.py License: MIT License | 5 votes |
def discretize(obs): return tuple([int(numpy.digitize(obs[i], BINS[i])) for i in xrange(4)])
Example 17
Project: sadl Author: coinse File: sa.py License: MIT License | 5 votes |
def get_sc(lower, upper, k, sa): """Surprise Coverage Args: lower (int): Lower bound. upper (int): Upper bound. k (int): The number of buckets. sa (list): List of lsa or dsa. Returns: cov (int): Surprise coverage. """ buckets = np.digitize(sa, np.linspace(lower, upper, k)) return len(list(set(buckets))) / float(k) * 100
Example 18
Project: pysat Author: pysat File: stats.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def median1D(self, bin_params, bin_label, data_label): """Calculates the median for a series of binned data. ** Will be remvoed in a future version now that ssnl.avgs has a similar function Parameters ---------- bin_params : array_like Input array defining the bins in which the median is calculated bin_label : string Name of data parameter which the bins cover data_level : string Name of data parameter to take the median of in each bin Returns ------- medians : array_like The median data value in each bin """ warnings.warn(' '.join(["utils.stats.median1D is deprecated and will be", "removed in pysat 3.0.0. Please use", "ssnl.avg.median1D instead"]), DeprecationWarning, stacklevel=2) bins = np.arange(bin_params[0], bin_params[1] + bin_params[2], bin_params[2]) medians = 0. * bins[0:-1] ind = np.digitize(self.data[bin_label], bins) for i in range(bins.size-1): index, = np.where(ind == (i + 1)) if len(index) > 0: idx = self.data.index[index.astype(int)] medians[i] = self.data.loc[idx, data_label].median() return medians
Example 19
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_forward(self): x = np.arange(-6, 5) bins = np.arange(-5, 5) assert_array_equal(digitize(x, bins), np.arange(11))
Example 20
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_reverse(self): x = np.arange(5, -6, -1) bins = np.arange(5, -5, -1) assert_array_equal(digitize(x, bins), np.arange(11))
Example 21
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_random(self): x = rand(10) bin = np.linspace(x.min(), x.max(), 10) assert_(np.all(digitize(x, bin) != 0))
Example 22
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_right_basic(self): x = [1, 5, 4, 10, 8, 11, 0] bins = [1, 5, 10] default_answer = [1, 2, 1, 3, 2, 3, 0] assert_array_equal(digitize(x, bins), default_answer) right_answer = [0, 1, 1, 2, 2, 3, 0] assert_array_equal(digitize(x, bins, True), right_answer)
Example 23
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_right_open(self): x = np.arange(-6, 5) bins = np.arange(-6, 4) assert_array_equal(digitize(x, bins, True), np.arange(11))
Example 24
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_right_open_random(self): x = rand(10) bins = np.linspace(x.min(), x.max(), 10) assert_(np.all(digitize(x, bins, True) != 10))
Example 25
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_monotonic(self): x = [-1, 0, 1, 2] bins = [0, 0, 1] assert_array_equal(digitize(x, bins, False), [0, 2, 3, 3]) assert_array_equal(digitize(x, bins, True), [0, 0, 2, 3]) bins = [1, 1, 0] assert_array_equal(digitize(x, bins, False), [3, 2, 0, 0]) assert_array_equal(digitize(x, bins, True), [3, 3, 2, 0]) bins = [1, 1, 1, 1] assert_array_equal(digitize(x, bins, False), [0, 0, 4, 4]) assert_array_equal(digitize(x, bins, True), [0, 0, 0, 4]) bins = [0, 0, 1, 0] assert_raises(ValueError, digitize, x, bins) bins = [1, 1, 0, 1] assert_raises(ValueError, digitize, x, bins)
Example 26
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_casting_error(self): x = [1, 2, 3 + 1.j] bins = [1, 2, 3] assert_raises(TypeError, digitize, x, bins) x, bins = bins, x assert_raises(TypeError, digitize, x, bins)
Example 27
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_return_type(self): # Functions returning indices should always return base ndarrays class A(np.ndarray): pass a = np.arange(5).view(A) b = np.arange(1, 3).view(A) assert_(not isinstance(digitize(b, a, False), A)) assert_(not isinstance(digitize(b, a, True), A))
Example 28
Project: recruit Author: Frank-qlu File: test_function_base.py License: Apache License 2.0 | 5 votes |
def test_large_integers_increasing(self): # gh-11022 x = 2**54 # loses precision in a float assert_equal(np.digitize(x, [x - 1, x + 1]), 1)
Example 29
Project: recruit Author: Frank-qlu File: test_regression.py License: Apache License 2.0 | 5 votes |
def test_mem_digitize(self): # Ticket #95 for i in range(100): np.digitize([1, 2, 3, 4], [1, 3]) np.digitize([0, 1, 2, 3, 4], [1, 3])
Example 30
Project: interactive-deep-colorization Author: junyanz File: colorize_image.py License: MIT License | 5 votes |
def get_ab_reccs(self, h, w, K=5, N=25000, return_conf=False): ''' Recommended colors at point (h,w) Call this after calling net_forward ''' if not self.dist_ab_set: print('Need to set prediction first') return 0 # randomly sample from pdf cmf = np.cumsum(self.dist_ab[:, h, w]) # CMF cmf = cmf / cmf[-1] cmf_bins = cmf # randomly sample N points rnd_pts = np.random.uniform(low=0, high=1.0, size=N) inds = np.digitize(rnd_pts, bins=cmf_bins) rnd_pts_ab = self.pts_in_hull[inds, :] # run k-means kmeans = KMeans(n_clusters=K).fit(rnd_pts_ab) # sort by cluster occupancy k_label_cnt = np.histogram(kmeans.labels_, np.arange(0, K + 1))[0] k_inds = np.argsort(k_label_cnt, axis=0)[::-1] cluster_per = 1. * k_label_cnt[k_inds] / N # percentage of points within cluster cluster_centers = kmeans.cluster_centers_[k_inds, :] # cluster centers # cluster_centers = np.random.uniform(low=-100,high=100,size=(N,2)) if return_conf: return cluster_centers, cluster_per else: return cluster_centers