Skip to content

Instantly share code, notes, and snippets.

@hibetterheyj
Created November 6, 2021 00:42
Show Gist options
  • Save hibetterheyj/14ef7b054510f7cffcb5c7cd9337a274 to your computer and use it in GitHub Desktop.
Save hibetterheyj/14ef7b054510f7cffcb5c7cd9337a274 to your computer and use it in GitHub Desktop.
Template for converting repetitive scripts to for-loop and dict structure
# ref: https://stackoverflow.com/a/14241195/7961693
"""
# timestamp can be read from lidars/ folder
stamp_file_path = os.path.join(allf.lidar_dir, seq+'_stamped.npy')
lidar_stamped_dict = np.load(stamp_file_path, allow_pickle=True)
ts_np = lidar_stamped_dict.item().get('timestamp')
all_det_list = []
within_det5_list = []
within_det10_list = []
min_dist_list = []
crowd_density5_list = []
crowd_density10_list = []
for fr_idx in range(allf.nr_frames(seq_idx)):
_, _, _, trks = allf[seq_idx, fr_idx]
(all_det,
within_det5, within_det10,
crowd_density5, crowd_density10,
min_dist) = compute_metrics(trks)
# append into list
all_det_list.append(all_det)
within_det5_list.append(within_det5)
within_det10_list.append(within_det10)
min_dist_list.append(min_dist)
crowd_density5_list.append(crowd_density5)
crowd_density10_list.append(crowd_density10)
ad_np = np.asarray(all_det_list, dtype=np.uint8)
wd5_np = np.asarray(within_det5_list, dtype=np.uint8)
wd10_np = np.asarray(within_det10_list, dtype=np.uint8)
md_np = np.asarray(min_dist_list, dtype=np.float32)
cd5_np = np.asarray(crowd_density5_list, dtype=np.float32)
cd10_np = np.asarray(crowd_density10_list, dtype=np.float32)
crowd_eval_dict = {'timestamp': ts_np,
'all_det': ad_np,
'within_det5': wd5_np,
'within_det10': wd10_np,
'min_dist': md_np,
'crowd_density5': cd5_np,
'crowd_density10': cd10_np}
np.save(crowd_eval_npy, crowd_eval_dict)
"""
# timestamp can be read from lidars/ folder
stamp_file_path = os.path.join(allf.lidar_dir, seq+'_stamped.npy')
lidar_stamped_dict = np.load(stamp_file_path, allow_pickle=True)
ts = lidar_stamped_dict.item().get('timestamp')
# targeted metrics and correspoding dtype
attrs = ('all_det',
'within_det5', 'within_det10',
'crowd_density5', 'crowd_density10',
'min_dist')
dtypes = (np.uint8,
np.uint8, np.uint8,
np.float32, np.float32,
np.float32)
crowd_eval_list_dict = {k:[] for k in attrs}
for fr_idx in range(allf.nr_frames(seq_idx)):
_, _, _, trks = allf[seq_idx, fr_idx]
metrics = compute_metrics(trks)
# update value for each attr
for idx, val in enumerate(metrics):
crowd_eval_list_dict[attrs[idx]].append(val)
crowd_eval_dict = {name:np.asarray(crowd_eval_list_dict[attrs[idx]], dtype=dtype) for idx, (name, dtype) in enumerate(zip(attrs, dtypes))}
crowd_eval_dict.update({'timestamp': ts})
np.save(crowd_eval_npy, crowd_eval_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment