У меня есть сотни файлов gz, каждый из которых включает в себя координаты прямоугольников размером примерно 0,5 ~ 1 м, каждый блок имеет уникальный индекс, называемый localIdx
, координаты для каждого блока llx, lly, urx, ury,
. Я могу получить x / y длякаждый блок на x=(llx+urx)/2, y=(lly+ury)/2
, так что я конвертирую поля в точки, теперь я хочу найти ближайшие 8 точек (блоков) для каждой точки (блока), который возвращает их localIdx.
Вот что я делаю:
1. read in the gz files with python pandas
2. set the column 'localIdx' for each point as index
3. get the height and width for each box by h=ury-lly, w=urx-llx
4. for each point, filter in points that x is in range current_point_x +/- 20*w, y is in range current_point_y +/- 20*h
5. convert to the filtered_in_points x/y and current_point x/y into two 2D numpy array
6. get the Euclidean Distance by scipy.spatial.distance.cdist
7. merge the result of step6 to the filtered_in pandas to map the localIdx
8. selected 8 nearest localIdx and combine them as a string
9. give the localIdx string for each point
Вот основная функция из моего кода:
def seek_norm_list(line, target_df=None, rmax=None, nmax=None, keycol=None):
if line.padType == 'DUT':
res_id = []
key_value = line[keycol]
current_pad = np.array([[line.xbbox, line.ybbox]])
h, w = line['h'], line['w']
h1, h2 = line.ybbox - h*20, line.ybbox + h*20
w1, w2 = line.xbbox - w*20, line.xbbox + w*20
target_mask = (target_df['xbbox'] > h1) & (target_df['xbbox'] < h2) & (target_df['ybbox'] > w1) & (target_df['ybbox'] < w2)
target_df = target_df[target_mask]
nbh_blks = line.nbh_blk.split(":")
a = np.array(list(zip(target_df.xbbox, target_df.ybbox)))
if len(a) > 0:
d = scipy.spatial.distance.cdist(a, current_pad)
target_df['dist'] = d
key_target = target_df[target_df[keycol] == key_value]
key_target.sort_values(by='dist', inplace=True)
res_target = key_target[key_target.dist < rmax]
keep_id = list(res_target['localIdx'])
if line['localIdx'] in keep_id:
keep_id.remove(line['localIdx'])
if len(keep_id) > int(nmax):
keep_id = keep_id[:int(nmax)]
for bk in nbh_blks:
for id in keep_id:
if bk in id:
res_id.append(id)
line['normList'] = ":".join(res_id)
line['refCount'] = len(res_id)
if len(res_id) > 0:
min, max = keep_id[0], keep_id[-1]
line['minDist'] = res_target.loc[min, 'dist']
line['maxDist'] = res_target.loc[max, 'dist']
else:
line['minDist'] = ''
line['maxDist'] = ''
else:
line['normList'], line['refCount'] = '', ''
line['minDist'], line['maxDist'] = '', ''
return line
else:
line['normList'], line['refCount'] = '', ''
line['minDist'], line['maxDist'] = '', ''
return line
это очень, очень медленно для каждого файла gz, и в моем случае ~ 600 файлов.общая строка всех файлов> 120 миллионов строк.и я использовал многопроцессорность в моей машине, которая является 16-ядерным.
Я хочу, чтобы он получил результат в течение 3 часов, это возможно с python?