Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the parser for DReyeVR actor data #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions simple_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from typing import Dict, Optional, List
from src.parser import parse_file
from src.utils import get_good_idxs, fill_gaps, smooth_arr, compute_YP, convert_to_df, flatten_dict
import numpy as np
import argparse
from src.visualizer import (
set_results_dir,
)


def main(filename: str, results_dir: str, vlines: Optional[List[float]] = None):
set_results_dir(results_dir)

"""parse the file"""
data: Dict[str, np.ndarray or dict] = parse_file(filename)
print(data.keys())
# Convert to pandas dataframe
import pandas as pd
actors_data = {}
actors_data["TimeElapsed"] = data["TimeElapsed"]
actors_data["Actors"] = {}
actors_data["Actors"]["Location"] = data["Actors"]["Location"]



data_frame = convert_to_df(data)
actors_df = convert_to_df(actors_data)

# Display the frame
from IPython.display import display
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This display variable is unused in the rest of the program.

Also I'd recommend to move all your imports to the top of the file for better readability

from tabulate import tabulate
print(data_frame.keys())
print(tabulate(actors_df, headers = 'keys', tablefmt = 'fancy_grid'))



if __name__ == "__main__":
argparser = argparse.ArgumentParser(description="DReyeVR recording parser")
argparser.add_argument(
"-f",
"--file",
metavar="P",
type=str,
help="path of the (human readable) recording file",
)
argparser.add_argument(
"-o",
"--out",
default="results",
type=str,
help="path of the results folder",
)
args = argparser.parse_args()

main(args.file, args.out)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be nice to include an example log (txt) file that someone can run using this simple test just to see what the outputs look like

34 changes: 19 additions & 15 deletions src/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def parse_custom_actor(
def parse_actor_location_rotation(
data: Dict[str, Any],
data_line: str,
title: Optional[str] = "",
actors_key: str,
t: Optional[int] = 0,
):
Id: int = int(data_line[: data_line.find("Location")])
Expand All @@ -118,12 +118,14 @@ def parse_actor_location_rotation(
open_1: int = close_0 + 2 + len("Rotation") # always " Rotation "
loc_data: tuple = eval(data_line[open_0:close_0])
rot_data: tuple = eval(data_line[open_1:])
if Id not in data[title]:
data[title][Id] = {"Time": [], "Location": [], "Rotation": []}
data[title][Id]["Time"].append(t)
data[title][Id]["Location"].append(loc_data)
data[title][Id]["Rotation"].append(rot_data)

if len(data[actors_key]["Id"]) <= t:
data[actors_key]["Id"].append([])
data[actors_key]["Location"].append({})
data[actors_key]["Rotation"].append({})
data[actors_key]["Id"][t].append(Id)
data[actors_key]["Location"][t][Id] = loc_data
data[actors_key]["Rotation"][t][Id] = rot_data


def validate(data: Dict[str, Any], L: Optional[int] = None) -> None:
# verify the data structure is reasonable
Expand Down Expand Up @@ -239,6 +241,9 @@ def parse_file(

actors_key: str = "Actors"
data[actors_key] = {}
data[actors_key]["Id"] = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fine change, but as is you'll need to fix the example.py script (Line ~470) which still uses the "Id" method to index into a dictionary containing "Location" and "Rotation". Can you please fix that?

data[actors_key]["Location"] = []
data[actors_key]["Rotation"] = []

with open(path, "r") as f:
start_t: float = time.time()
Expand Down Expand Up @@ -273,13 +278,12 @@ def parse_file(
data_line: str = line.strip(Carla_Actor).strip("\n")
if "Location:" not in data_line or "Rotation" not in data_line:
continue # don't care about state, light, animation, etc.
if "TimestampCarla" in data:
t = data["TimestampCarla"][_no_title_key][-1] # get carla time
else:
t = 0
parse_actor_location_rotation(data, data_line, title="Actors", t=t)
if debug:
validate(data)

t = len(data["TimeElapsed"]) - 1 # get carla time
#print('!', t)

parse_actor_location_rotation(data, data_line, actors_key, t=t)


# print status
if i % 500 == 0:
Expand Down Expand Up @@ -316,4 +320,4 @@ def cache_data(data: Dict[str, Any], filename: str) -> None:
filename = f"{os.path.join(cache_dir, actual_name)}.pkl"
with open(filename, "wb") as filehandler:
pickle.dump(data, filehandler)
print(f"cached data to {filename}")
print(f"cached data to {filename}")
11 changes: 8 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def cleanup_data_line(data_line: str) -> str:
def convert_to_list(data: Dict[str, np.ndarray or dict]) -> Dict[str, list or dict]:
list_data = {}
for k in data.keys():
if isinstance(data[k], dict):
if isinstance(data[k], list):
list_data[k] = data[k]
elif isinstance(data[k], dict):
list_data[k] = convert_to_list(data[k])
else:
assert isinstance(data[k], np.ndarray)
Expand Down Expand Up @@ -252,8 +254,11 @@ def convert_to_df(data: Dict[str, Any]) -> pd.DataFrame:
data = convert_to_list(data)
data = flatten_dict(data)
lens = [len(x) for x in data.values()]
assert min(lens) == max(lens) # all lengths are equal!
# NOTE: pandas can't haneld high dimensional np arrays, so we just use lists
# all lengths are equal!
if not min(lens) == max(lens):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the benefit of this is over a simple assert. Requires users to have the ipdb module loaded and will issue a nasty ModuleNotFoundError which might confuse the users.

import ipdb; ipdb.set_trace()

# NOTE: pandas can't handle high dimensional np arrays, so we just use lists
df = pd.DataFrame.from_dict(data)
print(f"created DReyeVR df in {time.time() - start_t:.3f}s")
return df
Expand Down