I have a large data array (about million rows) where each row consists of coordinates of a quantity. I am using KDtree algorithm in scipy.spatial to group these coordinates in predefined cells and append the cell no. at the end of each row. I am using mulitprocessing.pool to process a slice of array. The no. of slices equals no. of cores of the system. The code takes around a minute to process. Can I increase the speed with some modifications or I have reached the limit. I have to process around 900 such arrays so time is really of essence here ;)
Relevant part of my code is as follows,
#function called by external code
def AnodeAssignment(sim_data_dir,filename,energy_data_dir):
argv = sim_data_dir + filename
global data
data = np.loadtxt(argv,str,usecols=(0,2,5,6,8,9)) #load the array from file
good = np.where(data[:,2]=='phot')
data = data[good]
data = np.delete(data,2,1).astype(float)
slice_width = data.shape[0]/multiprocessing.cpu_count()
#slice array based on no. of core
data_slice = []
for i in range(multiprocessing.cpu_count()):
data_slice.append(data[i*slice_width:(i+1)*slice_width])
data_slice = np.array(data_slice)
#pool processes
pool = multiprocessing.Pool()
results = pool.map(pool_process,data_slice)
pool.close()
data = results[0]
for i in range(1,multiprocessing.cpu_count()): #combine results
data = np.vstack((data,results[i]))
#function to process each slice using KDtree algo.
def pool_process(data_slice):
y = np.linspace(-19.5, 19.5, 14 ,endpoint=True)
z = np.linspace(-9, 6, 6, endpoint=True)
yv, zv = np.meshgrid(y,z)
tree = spatial.KDTree(zip(yv.ravel(),zv.ravel()))
dist, indices = tree.query(data_slice[:,3:])
zz = tree.data[indices][:,1]
yy = tree.data[indices][:,0]
anode = np.ndarray((data_slice[:,0].size,1),int)
a1 = np.where(np.logical_and(np.in1d(yy,y[1:-1:2]),np.in1d(zz ,[6])))
a2 = np.where(np.logical_and(np.in1d(yy,y[2:-1:2]),np.in1d(zz ,[6])))
a3 = np.where(np.logical_and(np.in1d(yy,y[1:-1:2]),np.in1d(zz ,[3])))
a4 = np.where(np.logical_and(np.in1d(yy,y[2:-1:2]),np.in1d(zz ,[3])))
a5 = np.where(zz==0)
a6 = np.where(zz==-3)
a7 = np.where(zz==-6)
a8 = np.where(yy==-19.5)
a9 = np.where(zz==-9)
a10 = np.where(yy==19.5)
anode[a1] = 1
anode[a2] = 2
anode[a3] = 3
anode[a4] = 4
anode[a5] = 5
anode[a6] = 6
anode[a7] = 7
anode[a8] = 8
anode[a9] = 9
anode[a10] = 10
data_slice = np.hstack((data_slice,anode))
return data_slice