def test_f32_rounding(self):
# gh-4799, check that the rounding of the edges works with float32
x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32)
y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
assert_equal(counts_hist.sum(), 3.)
python类histogram2d()的实例源码
def compute_histogram(data, bins=36):
h, x, y = np.histogram2d(data[data.columns[0]], data[data.columns[1]], bins=bins, normed=True)
xc = (x[:-1] + x[1:]) / 2
yc = (y[:-1] + y[1:]) / 2
coords = np.array([(xi, yi) for xi in xc for yi in yc])
theta = coords[:, 0]
r = coords[:, 1]
return h.flatten(), theta, r
def create_cube(df, bins, bin_range):
_, x_edges, y_edges = np.histogram2d(
df.alt, df.az, bins=bins, range=bin_range)
slices = []
N = 100
for df in np.array_split(df, N):
H, _, _ = np.histogram2d(df.alt, df.az, bins=[
x_edges, y_edges], range=bin_range)
slices.append(H)
slices = np.array(slices)
return slices
def create_cube(self, points):
t, alt, az = points.T
alt = alt.astype(np.float)
az = az.astype(np.float)
_, x_edges, y_edges = np.histogram2d(
alt,
az,
bins=self.bins,
range=self.bin_range
)
slices = []
timestamps = []
for indeces in np.array_split(np.arange(0, len(points)), self.slices_per_cube):
timestamps.append(t[indeces][0])
H, _, _ = np.histogram2d(
alt[indeces],
az[indeces],
bins=[x_edges, y_edges],
range=self.bin_range)
slices.append(H)
slices = np.array(slices)
timestamps = np.array(timestamps)
return timestamps, slices
def myplot(x, y, nb=32, xsize=500, ysize=500):
heatmap, xedges, yedges = np.histogram2d(x, y, bins=nb)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
return heatmap.T, extent
def summarizeMap(mapDataFrame):
latlon = mapDataFrame[['meta_latitude','meta_longitude']]
latlon = latlon[pd.notnull(latlon['meta_latitude'])]
latlon = latlon[pd.notnull(latlon['meta_longitude'])]
minLat = np.amin(latlon['meta_latitude'])
maxLat = np.amax(latlon['meta_latitude'])
minLon = np.amin(latlon['meta_longitude'])
maxLon = np.amax(latlon['meta_longitude'])
if len(latlon) > 1:
latlon_map = np.histogram2d(x=latlon['meta_longitude'],y=latlon['meta_latitude'],bins=[36,18], range=[[minLon, maxLon], [minLat, maxLat]])
else:
latlon_map = np.histogram2d(x=[],y=[],bins=[36,18], range=[[-180, 180], [-90, 90]])
#define latlon map color bin info
percentiles, countRanges, fillColors = getMapBins(latlon_map[0], num_bins=10)
# range should be flexible to rules in DatasetSearchSummary
# latlon_map[0] is the lonxlat (XxY) array of counts; latlon_map[1] is the nx/lon bin starts; map[2] ny/lat bin starts
lonstepsize = (latlon_map[1][1]-latlon_map[1][0])/2
latstepsize = (latlon_map[2][1]-latlon_map[2][0])/2
maxMapCount = np.amax(latlon_map[0])
map_data = []
for lon_ix,lonbin in enumerate(latlon_map[0]):
for lat_ix,latbin in enumerate(lonbin):
#[latlon_map[2][ix]+latstepsize for ix,latbin in enumerate(latlon_map[0][0])]
lat = latlon_map[2][lat_ix]+latstepsize
lon = latlon_map[1][lon_ix]+lonstepsize
value = latbin
buffer=0.0001
#left-bottom, left-top, right-top, right-bottom, left-bottom
polygon = [[lon-lonstepsize+buffer,lat-latstepsize+buffer], [lon-lonstepsize+buffer,lat+latstepsize-buffer], [lon+lonstepsize-buffer,lat+latstepsize-buffer], [lon+lonstepsize-buffer,lat-latstepsize+buffer], [lon-lonstepsize,lat-latstepsize]]
bin_ix = np.amax(np.argwhere(np.array(percentiles)<=sp.percentileofscore(latlon_map[0].flatten(), value)))
fillColor = fillColors[bin_ix]
map_data.append({"lat":lat,"lon":lon,"count":value,"polygon":polygon, "fillColor":fillColor})
map_legend_info = {"ranges":countRanges, "fills":fillColors}
return (map_data,map_legend_info)
# Query Construction Helpers / Data Retrieval
# Based on a rule (field name, comparator and value), add a filter to a query object
# TODO add some better documentation here on what each type is
def hinton(W, bg='grey', facecolors=('w', 'k')):
"""Draw a hinton diagram of the matrix W on the current pylab axis
Hinton diagrams are a way of visualizing numerical values in a matrix/vector,
popular in the neural networks and machine learning literature. The area
occupied by a square is proportional to a value's magnitude, and the colour
indicates its sign (positive/negative).
Example usage:
R = np.random.normal(0, 1, (2,1000))
h, ex, ey = np.histogram2d(R[0], R[1], bins=15)
hh = h - h.T
hinton.hinton(hh)
"""
M, N = W.shape
square_x = np.array([-.5, .5, .5, -.5])
square_y = np.array([-.5, -.5, .5, .5])
ioff = False
if plt.isinteractive():
plt.ioff()
ioff = True
plt.fill([-.5, N - .5, N - .5, - .5], [-.5, -.5, M - .5, M - .5], bg)
Wmax = np.abs(W).max()
for m, Wrow in enumerate(W):
for n, w in enumerate(Wrow):
c = plt.signbit(w) and facecolors[1] or facecolors[0]
plt.fill(square_x * w / Wmax + n, square_y * w / Wmax + m, c, edgecolor=c)
plt.ylim(-0.5, M - 0.5)
plt.xlim(-0.5, M - 0.5)
if ioff is True:
plt.ion()
plt.draw_if_interactive()
def test_f32_rounding(self):
# gh-4799, check that the rounding of the edges works with float32
x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32)
y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
assert_equal(counts_hist.sum(), 3.)
def twoDimensionalHistogram(title, title_x, title_y,
z, bins_x, bins_y,
lim_x=None, lim_y=None,
vmin=None, vmax=None):
"""
Create a two-dimension histogram plot or binned map.
If using the outputs of numpy.histogram2d, remember to transpose the histogram.
INPUTS
"""
pylab.figure()
mesh_x, mesh_y = numpy.meshgrid(bins_x, bins_y)
if vmin != None and vmin == vmax:
pylab.pcolor(mesh_x, mesh_y, z)
else:
pylab.pcolor(mesh_x, mesh_y, z, vmin=vmin, vmax=vmax)
pylab.xlabel(title_x)
pylab.ylabel(title_y)
pylab.title(title)
pylab.colorbar()
if lim_x:
pylab.xlim(lim_x[0], lim_x[1])
if lim_y:
pylab.ylim(lim_y[0], lim_y[1])
############################################################
def drawHessDiagram(self,catalog=None):
ax = plt.gca()
if not catalog: catalog = self.get_stars()
r_peak = self.kernel.extension
angsep = ugali.utils.projector.angsep(self.ra, self.dec, catalog.ra, catalog.dec)
cut_inner = (angsep < r_peak)
cut_annulus = (angsep > 0.5) & (angsep < 1.) # deg
mmin, mmax = 16., 24.
cmin, cmax = -0.5, 1.0
mbins = np.linspace(mmin, mmax, 150)
cbins = np.linspace(cmin, cmax, 150)
color = catalog.color[cut_annulus]
mag = catalog.mag[cut_annulus]
h, xbins, ybins = numpy.histogram2d(color, mag, bins=[cbins,mbins])
blur = nd.filters.gaussian_filter(h.T, 2)
kwargs = dict(extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()],
cmap='gray_r', aspect='auto', origin='lower',
rasterized=True, interpolation='none')
ax.imshow(blur, **kwargs)
pylab.scatter(catalog.color[cut_inner], catalog.mag[cut_inner],
c='red', s=7, edgecolor='none')# label=r'$r < %.2f$ deg'%(r_peak))
ugali.utils.plotting.drawIsochrone(self.isochrone, c='b', zorder=10)
ax.set_xlim(-0.5, 1.)
ax.set_ylim(24., 16.)
plt.xlabel(r'$g - r$')
plt.ylabel(r'$g$')
plt.xticks([-0.5, 0., 0.5, 1.])
plt.yticks(numpy.arange(mmax - 1., mmin - 1., -1.))
radius_string = (r'${\rm r}<%.1f$ arcmin'%( 60 * r_peak))
pylab.text(0.05, 0.95, radius_string,
fontsize=10, ha='left', va='top', color='red',
transform=pylab.gca().transAxes,
bbox=dict(facecolor='white', alpha=1., edgecolor='none'))
def histogram2d(self,distance_modulus=None,delta_mag=0.03,steps=10000):
"""
Return a 2D histogram the isochrone in mag-mag space.
Parameters:
-----------
distance_modulus : distance modulus to calculate histogram at
delta_mag : magnitude bin size
mass_steps : number of steps to sample isochrone at
Returns:
--------
bins_mag_1 : bin edges for first magnitude
bins_mag_2 : bin edges for second magnitude
isochrone_pdf : weighted pdf of isochrone in each bin
"""
if distance_modulus is not None:
self.distance_modulus = distance_modulus
# Isochrone will be binned, so might as well sample lots of points
mass_init,mass_pdf,mass_act,mag_1,mag_2 = self.sample(mass_steps=steps)
#logger.warning("Fudging intrinisic dispersion in isochrone.")
#mag_1 += np.random.normal(scale=0.02,size=len(mag_1))
#mag_2 += np.random.normal(scale=0.02,size=len(mag_2))
# We cast to np.float32 to save memory
bins_mag_1 = np.arange(self.mod+mag_1.min() - (0.5*delta_mag),
self.mod+mag_1.max() + (0.5*delta_mag),
delta_mag).astype(np.float32)
bins_mag_2 = np.arange(self.mod+mag_2.min() - (0.5*delta_mag),
self.mod+mag_2.max() + (0.5*delta_mag),
delta_mag).astype(np.float32)
# ADW: Completeness needs to go in mass_pdf here...
isochrone_pdf = np.histogram2d(self.mod + mag_1,
self.mod + mag_2,
bins=[bins_mag_1, bins_mag_2],
weights=mass_pdf)[0].astype(np.float32)
return isochrone_pdf, bins_mag_1, bins_mag_2
def pixelize(jet_csts, edges, cutoff=0.1):
"""Return eta-phi histogram of transverse energy deposits.
Optionally set all instensities below cutoff to zero.
"""
image, _, _ = np.histogram2d(
jet_csts['eta'], jet_csts['phi'],
bins=(edges[0], edges[1]),
weights=jet_csts['ET'] * (jet_csts['ET'] > cutoff))
return image
def update_data(self):
var = getattr(self._sim, self._variable)[:,0:2]
mask = None
if self._sub_domain:
pos = self._sim.positions
mask_x = np.logical_or(pos[:, 0] <= self._sub_domain[0][0],
pos[:, 0] >= self._sub_domain[0][1])
mask_y = np.logical_or(pos[:, 1] <= self._sub_domain[1][0],
pos[:, 1] >= self._sub_domain[1][1])
mask = np.logical_or(mask_x, mask_y)
if self._particle_type is not None:
if mask is None:
mask = (self._sim.types != self._particle_type)
else:
mask = np.logical_or(mask, (self._sim.types != self._particle_type))
if mask is not None:
tiledmask = np.transpose(np.tile(mask, (2, 1)))
var = ma.masked_array(var, tiledmask)
var = var.compressed()
var = var.reshape([len(var)//2, 2])
hist, self._x_edges, self._y_edges = np.histogram2d(var[:, 0], var[:, 1],
bins=self._nr_of_bins, range=self._hist_range)
if self._window is not None:
self._dataHistory.append(hist)
if len(self._dataHistory) > self._window:
del self._dataHistory[0]
self._histogram_array = sum(self._dataHistory)
else:
self._histogram_array += hist
def test_f32_rounding(self):
# gh-4799, check that the rounding of the edges works with float32
x = np.array([276.318359, -69.593948, 21.329449], dtype=np.float32)
y = np.array([5005.689453, 4481.327637, 6010.369629], dtype=np.float32)
counts_hist, xedges, yedges = np.histogram2d(x, y, bins=100)
assert_equal(counts_hist.sum(), 3.)
def get_heatmaps(neuron_list, spikes, pos, num_bins=100):
""" Gets the 2D heatmaps for firing of a given set of neurons.
Parameters
----------
neuron_list : list of ints
These will be the indices into the full list of neuron spike times
spikes : list
Containing nept.SpikeTrain for each neuron.
pos : nept.Position
Must be 2D.
num_bins : int
This will specify how the 2D space is broken up, the greater the number
the more specific the heatmap will be. The default is set at 100.
Returns
-------
heatmaps : dict of lists
Where the key is the neuron number and the value is the heatmap for
that individual neuron.
"""
if not pos.dimensions == 2:
raise ValueError("pos must be two-dimensional")
xedges = np.linspace(np.min(pos.x)-2, np.max(pos.x)+2, num_bins+1)
yedges = np.linspace(np.min(pos.y)-2, np.max(pos.y)+2, num_bins+1)
heatmaps = dict()
count = 1
for neuron in neuron_list:
field_x = []
field_y = []
for spike in spikes[neuron].time:
spike_idx = find_nearest_idx(pos.time, spike)
field_x.append(pos.x[spike_idx])
field_y.append(pos.y[spike_idx])
heatmap, out_xedges, out_yedges = np.histogram2d(field_x, field_y, bins=[xedges, yedges])
heatmaps[neuron] = heatmap.T
print(str(neuron) + ' of ' + str(len(neuron_list)))
count += 1
return heatmaps
def test_image():
import matplotlib.pyplot as plt
con = config.Config()
data = DataGenerator(con)
xedges = [_ / 7 for _ in range(-14, 15)]
yedges = [_ / 7 for _ in range(-14, 15)]
image_data = {}
for x, y in data.get_train_data(1):
e, v = scipy.linalg.eigh(
x.values.reshape((10, 10))) # np.linalg.eig will return the complex data sometimes...
for i in range(1, len(v)):
new_v = preprocessing.scale(v[i])
for k in range(0, len(new_v), 2):
if k not in image_data:
image_data[k] = {}
image_data[k][0] = [new_v[k]]
image_data[k][1] = [new_v[k + 1]]
else:
image_data[k][0].append(new_v[k])
image_data[k][1].append(new_v[k + 1])
for k in image_data.keys():
H, new_xedges, new_yedges = np.histogram2d(image_data[k][0], image_data[k][1], bins=(xedges, yedges))
print(H)
plt.imshow(H, cmap=plt.cm.gray, interpolation='nearest', origin='low',
extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])
plt.show()
def get_fragment_ends_matrices(self, merged, sv_region, window_size):
mats = {}
selectors = {"+":"end_pos_{}",
"-":"start_pos_{}"}
binsx = numpy.arange(sv_region["startx"], sv_region["endx"]+window_size*2, window_size)
binsy = numpy.arange(sv_region["starty"], sv_region["endy"]+window_size*2, window_size)
for orientationx in "+-":
for orientationy in "+-":
fx = merged[selectors[orientationx].format("x")].values
fy = merged[selectors[orientationy].format("y")].values
hist = numpy.histogram2d(fy, fx, (binsy, binsx))[0]
mats[orientationx+orientationy] = hist
return mats
def plot_rc_alpha(ax, snr150=False):
nbins_x = 30
nbins_y = 40
ind = indrc
if snr150:
ind = ind_snr150
H, xedges, yedges = np.histogram2d(metals[ind], alphafe[ind],
bins=(nbins_x, nbins_y), normed=True)
x_bin_sizes = (xedges[1:] - xedges[:-1]).reshape((1, nbins_x))
y_bin_sizes = (yedges[1:] - yedges[:-1]).reshape((nbins_y, 1))
pdf = (H * np.multiply(x_bin_sizes, y_bin_sizes).T)
sig05 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.38))
sig1 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.68))
sig2 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.95))
sig3 = optimize.brentq(find_confidence_interval, 0., 1., args=(pdf, 0.99))
levels = [sig1, sig05, 1.]
X = 0.5 * (xedges[1:] + xedges[:-1])
Y = 0.5 * (yedges[1:] + yedges[:-1])
Z = pdf.T
ax.contourf(X, Y, Z, levels=levels, origin='lower',
colors=('darkgray', 'gray'), zorder=9)
ax.contour(X, Y, Z, levels=[levels[0], levels[1]], origin='lower',
colors='k', zorder=10)
for i in range(nbins_x):
for j in range(nbins_y):
if Z[j, i] <= sig1 * 1.2:
ind_tmp0 = np.where(
(metals >= xedges[i]) & (metals < xedges[i+1]) &
(alphafe >= yedges[j]) & (alphafe < yedges[j+1]))[0]
ind_tmp = np.intersect1d(ind, ind_tmp0)
if len(ind_tmp > 0):
ax.scatter(metals[ind_tmp], alphafe[ind_tmp], c='k', s=5)
def test_contingency_matrix():
labels_a = np.array([1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3])
labels_b = np.array([1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 3, 1, 3, 3, 3, 2, 2])
C = contingency_matrix(labels_a, labels_b)
C2 = np.histogram2d(labels_a, labels_b,
bins=(np.arange(1, 5),
np.arange(1, 5)))[0]
assert_array_almost_equal(C, C2)
C = contingency_matrix(labels_a, labels_b, eps=.1)
assert_array_almost_equal(C, C2 + .1)