def parse_preds(preds_path, labels): """ Returns a list of Prediction objects parsed from a given txt file, and matched with a corresponding label. :param preds_path: The path to the txt file that contains predictions in the (x, y, r) format. The predictions are line separated and the coordinates are space separated. :param labels: A list of labels to match with. :return: A list of Prediction objects parsed from the given file, each matched with a single Label object from the labels list (or with None if not found). """ preds = [] with open(preds_path, 'r') as pred_f: time = float(pred_f.readline()) for line in pred_f.readlines(): log = [float(x) for x in line.split()] pred = Prediction(int(log[0]), int(log[1]), log[2], log[3]) pred.match_label(labels) preds.append(pred) return preds, time
def generate_fake_preds(preds_path, labels, persist=False): preds = [] time = np.random.exponential(0.3) if persist: with open(preds_path, 'a') as file: file.write('%g\n' % time) for label in labels: pred_num_for_label = np.random.choice(NUM_PREDS_LIST, p=NUM_PREDS_PROBS) for i in range(pred_num_for_label): x, y, r, sc = _generate_fake_pred(label) if persist: with open(preds_path, 'a') as pred_f: pred_f.write(('%g ' * 4 + '\n') % (x, y, r, sc)) pred = Prediction(x, y, r, sc) pred.match_label(labels) preds.append(pred) return preds, time