Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print "do histogram"
def minmax(x, fraction=0.005):
indices = np.argsort(x)
N = len(x)
i1 = int(fraction*N)
i2 = int((1-fraction)*N)
return x[indices[i1]], x[indices[i2]]
xmin, xmax = minmax(x)
ymin, ymax = minmax(y)
zmin, zmax = minmax(z)
print "done"
import vaex.vaexfast
vaex.vaexfast.histogram3d(x, y, z, None, data_matrix , xmin, xmax, ymin, ymax, zmin, zmax)
data_matrix = np.log(data_matrix + 1)
#vaex.histogram.hist3d(x, y, z, data_matrix, xmin, xmax, ymin, ymax, zmin, zmax)
print "done"
maxvalue = data_matrix.max()
print maxvalue
ar = vtk.util.numpy_support.numpy_to_vtk(data_matrix.reshape((-1,)))
print ar.GetNumberOfComponents(), ar.GetNumberOfTuples()
print ar
print dir(ar)
grid = vtk.vtkImageData()
grid.SetOrigin(0, 0, 0) # default values
dx = 1
grid.SetSpacing(dx, dx, dx)
grid.SetDimensions(256, 256, 256)
def test_histogram1d(self):
x = np.arange(-5-0.1, 5-0.1, dtype=np.float64)
print x, sum(x)
for N in [1, 2, 4, 256, 512]:
counts = np.zeros(N, dtype=np.float64)
min, max = -5, 5
vaex.vaexfast.histogram1d(x, None, counts, min, max) #+1e-15)
#print np.sum(counts), len(x), x
self.assertEqual(np.sum(counts), 9, "histogram1d test") # 1 should fall outside
counts = np.zeros((N, N), dtype=np.float64)
vaex.vaexfast.histogram2d(x, x, None, counts, min, max, min, max)
self.assertEqual(np.sum(counts), 9, "histogram2d test") # 1 should fall outside
counts = np.zeros((N, N, N), dtype=np.float64)
vaex.vaexfast.histogram3d(x, x, x, None, counts, min, max, min, max, min, max)
self.assertEqual(np.sum(counts), 9, "histogram3d test") # 1 should fall outside
if 0:
print np.sum(counts)
counts = np.zeros((N, N, N), dtype=np.float64)
vaex.vaexfast.histogram3d(x, x, x, None, counts, 0., 9., 0., 9., 0., 9.)
print np.sum(counts)
x, y, z, vx, vy, vz = [k.astype(np.float64) - k.mean() for k in [x, y, z, vx, vy, vz]]
grid3d = np.zeros((grid_size, grid_size, grid_size), dtype=np.float64)
vectorgrid = np.zeros((4, grid_size_vector, grid_size_vector, grid_size_vector), dtype=np.float64)
# mi, ma = -30., 30.
# mi, ma = 45., 55
# mi, ma = -20, 20
s = 0.
mi, ma = -4, 4
print("histogram3d")
vaex.vaexfast.histogram3d(x, y, z, None, grid3d, mi + s, ma + s, mi, ma, mi, ma)
if 0:
vx = vx - vx.mean()
vy = vy - vy.mean()
vz = vz - vz.mean()
vaex.vaexfast.histogram3d(x, y, z, vx, vectorgrid[0], mi + s, ma + s, mi, ma, mi, ma)
vaex.vaexfast.histogram3d(x, y, z, vy, vectorgrid[1], mi + s, ma + s, mi, ma, mi, ma)
vaex.vaexfast.histogram3d(x, y, z, vz, vectorgrid[2], mi + s, ma + s, mi, ma, mi, ma)
print(vx)
print(vectorgrid[0])
print(vaex.vaexfast.resize(vectorgrid[0], 4))
print(vaex.vaexfast.resize(vectorgrid[1], 4))
print(vaex.vaexfast.resize(vectorgrid[2], 4))
print(vaex.vaexfast.resize(grid3d, 4))
print("$" * 80)
vectorgrid[3][:] = vaex.vaexfast.resize(grid3d, grid_size_vector)
for i in range(3):
vectorgrid[i] /= vectorgrid[3] # go from weighted to mean
if 1:
vmax = max([np.nanmax(vectorgrid[0]), np.nanmax(vectorgrid[1]), np.nanmax(vectorgrid[2])])
for i in range(3):
masks = [np.ma.getmaskarray(block) for block in blocks if np.ma.isMaskedArray(block)]
blocks = [block.data if np.ma.isMaskedArray(block) else block for block in blocks]
mask = None
#blocks = [as_flat_float(block) for block in blocks]
if len(blocks) != 0:
dtype = np.find_common_type([block.dtype for block in blocks], [])
histogram2d = vaex.vaexfast.histogram2d
if dtype.str in ">f8 f4 i8
def min_max_map(thread_index, i1, i2, *blocks):
if self.is_masked or self.df.filtered:
mask = self.df.evaluate_selection_mask("default" if self.is_masked else None, i1=i1, i2=i2, pre_filtered=False)
blocks = [block[mask] for block in blocks]
is_empty = all(~mask)
if is_empty:
return None
# with lock:
# print blocks
# with lock:
# print thread_index, i1, i2, blocks
blocks = [_asfloat(block) for block in blocks]
return [vaex.vaexfast.find_nan_min_max(block) for block in blocks]
if 0: # TODO: implement using statisticNd and benchmark
minmaxes = np.zeros((len(blocks), 2), dtype=float)
minmaxes[:, 0] = np.inf
minmaxes[:, 1] = -np.inf
for i, block in enumerate(blocks):
vaex.vaexfast.statisticNd([], block, minmaxes[i, :], [], [], 2)
# minmaxes[~np.isfinite(minmaxes)] = np.nan
return minmaxes
task = TaskMapReduceLegacy(self.df, self.expressions, min_max_map, min_max_reduce, self._toarray, info=True, name="minmax")
def min_max_map(thread_index, i1, i2, *blocks):
if self.is_masked:
mask = self.dataset.mask
blocks = [block[mask[i1:i2]] for block in blocks]
is_empty = all(~mask[i1:i2])
if is_empty:
return None
#with lock:
#print blocks
#with lock:
# print thread_index, i1, i2, blocks
return [vaex.vaexfast.find_nan_min_max(block) for block in blocks]
task = TaskMapReduce(self.dataset, self.expressions, min_max_map, min_max_reduce, self._toarray, info=True)
info.first = i1 == 0
info.last = i2 == self.df.length_unfiltered()
info.size = i2 - i1
masks = [np.ma.getmaskarray(block) for block in blocks if np.ma.isMaskedArray(block)]
blocks = [block.data if np.ma.isMaskedArray(block) else block for block in blocks]
mask = None
#blocks = [as_flat_float(block) for block in blocks]
if len(blocks) != 0:
dtype = np.find_common_type([block.dtype for block in blocks], [])
histogram2d = vaex.vaexfast.histogram2d
if dtype.str in ">f8 f4 i8
if use_mask and dataset.mask is not None:
mask = dataset.mask[info.i1:info.i2]
blocks = [block[mask] for block in blocks]
else:
mask = None
ranges = []
minima = []
maxima = []
for dim in range(dimension):
ranges += list(range_map[pairs[index][dim]])
mi, ma = range_map[pairs[index][dim]]
minima.append(mi)
maxima.append(ma)
if len(blocks) == 2:
print(("mask", mask))
vaex.vaexfast.histogram2d(blocks[0], blocks[1], None, counts[index], *(ranges + [0, 0]))
# vaex.vaexfast.histogram2d(blocks[0], blocks[1], None, counts_shuffled[index], *(ranges + [1, 0]))
if len(blocks) == 3:
vaex.vaexfast.histogram3d(blocks[0], blocks[1], blocks[2], None, counts[index], *(ranges + [0, 0, 0]))
# vaex.vaexfast.histogramNd(list(blocks), None, counts[index], minima, maxima)
# for i in range(5):
# vaex.vaexfast.histogram3d(blocks[0], blocks[1], blocks[2], None, counts_shuffled[index], *(ranges + [2+i,1+i,0]))
if len(blocks) > 3:
vaex.vaexfast.histogramNd(list(blocks), None, counts[index], minima, maxima)
# for i in range(5):
# vaex.vaexfast.histogram3d(blocks[0], blocks[1], blocks[2], None, counts_shuffled[index], *(ranges + [2+i,1+i,0]))
if feedback:
wrapper.N_done += len(blocks[0]) * dimension
if feedback:
cancel = feedback(wrapper.N_done * 100. / N_total)
if cancel:
raise Exception("cancelled")
previous_mask = None
if filter_mask is not None:
N = filter_mask.astype(np.uint8).sum()
else:
N = i2 - i1
current_mask = np.full(N, False)
x, y = np.array(self.xseq, dtype=np.float64), np.array(self.yseq, dtype=np.float64)
meanx = x.mean()
meany = y.mean()
radius = np.sqrt((meanx - x)**2 + (meany - y)**2).max()
blockx = df._evaluate(self.boolean_expression_x, i1=i1, i2=i2, filter_mask=filter_mask)
blocky = df._evaluate(self.boolean_expression_y, i1=i1, i2=i2, filter_mask=filter_mask)
(blockx, blocky), excluding_mask = _split_and_combine_mask([blockx, blocky])
blockx = as_flat_float(blockx)
blocky = as_flat_float(blocky)
vaex.vaexfast.pnpoly(x, y, blockx, blocky, current_mask, meanx, meany, radius)
if previous_mask is None:
logger.debug("setting mask")
mask = current_mask
else:
logger.debug("combining previous mask with current mask using op %r", self.mode)
mode_function = _select_functions[self.mode]
mask = mode_function(previous_mask, current_mask)
if excluding_mask is not None:
mask = mask & (~excluding_mask)
return mask
subblock_weight = None
if len(blocks) == len(self.expressions) + 1:
subblock_weight = blocks[-1]
blocks = list(blocks[:-1])
# print subblocks[0]
# print subblocks[1]
if self.dimension == 1:
vaex.vaexfast.histogram1d(blocks[0], subblock_weight, data, *self.ranges_flat)
elif self.dimension == 2:
# if subblock_weight is None:
# #print "speedup?"
# histogram_numba(blocks[0], blocks[1], subblock_weight, data, *self.ranges_flat)
# else:
vaex.vaexfast.histogram2d(blocks[0], blocks[1], subblock_weight, data, *self.ranges_flat)
# vaex.vaexfast.statisticNd([blocks[0], blocks[1]], subblock_weight, data, self.minima, self.maxima, 0)
elif self.dimension == 3:
vaex.vaexfast.histogram3d(blocks[0], blocks[1], blocks[2], subblock_weight, data, *self.ranges_flat)
else:
blocks = list(blocks) # histogramNd wants blocks to be a list
vaex.vaexfast.histogramNd(blocks, subblock_weight, data, self.minima, self.maxima)
return i1
# return map(self._map, blocks)#[self.map(block) for block in blocks]