You can use either numpy broadcasting or a combination of numpy.repeat
and numpy.reshape
:
my_dtype = numpy.dtype([('Image', h5py.special_dtype(ref=h5py.Reference)),
('NextLevel', h5py.special_dtype(ref=h5py.Reference))])
ref_array = array( (image.ref, dataset.ref), dtype=my_dtype)
dataset = numpy.repeat(ref_array, n*n)
dataset = dataset.reshape( (n,n) )
Note that numpy.repeat
returns a flattened array, hence the use of numpy.reshape
. It seems repeat
is faster than just broadcasting it:
%timeit empty_dataset=np.empty(2*2,dtype=my_dtype); empty_dataset[:]=ref_array
100000 loops, best of 3: 9.09 us per loop
%timeit repeat_dataset=np.repeat(ref_array, 2*2).reshape((2,2))
100000 loops, best of 3: 5.92 us per loop