import io import json from pathlib import Path from typing import Any, Dict, List, Union import pandas as pd from datasets import load_dataset class ADTDatasetLoader: """Loader for ADT Dataset from Hugging Face.""" def __init__(self, repo_id: str = "ariakang/ADT-test", sequence_name: str = None): """ Initialize the dataset loader. Args: repo_id: Hugging Face repository ID sequence_name: Specific sequence to load. If None, loads first available sequence """ self.dataset = load_dataset(repo_id) if sequence_name is None: sequence_name = list(self.dataset.keys())[0] self.sequence_name = sequence_name self.sequence = self.dataset[sequence_name] # Create a mapping of filenames to indices for faster access self.file_index = { filename: idx for idx, filename in enumerate(self.sequence["filename"]) } def _deserialize_csv(self, csv_string: str) -> pd.DataFrame: """Convert CSV string to DataFrame.""" return pd.read_csv(io.StringIO(csv_string)) def _deserialize_json(self, json_string: str) -> Union[Dict, List]: """Convert JSON string to Python object.""" return json.loads(json_string) def get_available_files(self) -> List[Dict[str, str]]: """Get list of all available files and their types.""" return [ {"filename": filename, "type": dtype} for filename, dtype in zip( self.sequence["filename"], self.sequence["data_type"] ) ] def load_file_by_name(self, filename: str) -> Any: """ Load specific file by name. Args: filename: Name of the file to load (e.g., "2d_bounding_box.csv") Returns: DataFrame for CSV files, dict/list for JSON files """ if filename not in self.file_index: raise ValueError(f"File {filename} not found in dataset") idx = self.file_index[filename] data_type = self.sequence["data_type"][idx] data = self.sequence["data"][idx] if data_type == "csv": return self._deserialize_csv(data) elif data_type in ["json", "jsonl"]: return self._deserialize_json(data) else: return data def load_2d_bounding_boxes(self) -> pd.DataFrame: """Load 2D bounding box data.""" return self.load_file_by_name("2d_bounding_box.csv") def load_3d_bounding_boxes(self) -> pd.DataFrame: """Load 3D bounding box data.""" return self.load_file_by_name("3d_bounding_box.csv") def load_aria_trajectory(self) -> pd.DataFrame: """Load Aria device trajectory data.""" return self.load_file_by_name("aria_trajectory.csv") def load_eyegaze(self) -> pd.DataFrame: """Load eye gaze data.""" return self.load_file_by_name("eyegaze.csv") def load_scene_objects(self) -> pd.DataFrame: """Load scene objects data.""" return self.load_file_by_name("scene_objects.csv") def load_instances(self) -> Dict: """Load instances data.""" return self.load_file_by_name("instances.json") def load_metadata(self) -> Dict: """Load metadata.""" return self.load_file_by_name("metadata.json") def load_mps_eye_gaze(self) -> Dict[str, Union[pd.DataFrame, Dict]]: """Load MPS eye gaze data.""" return { "general": self.load_file_by_name("mps/eye_gaze/general_eye_gaze.csv"), "summary": self.load_file_by_name("mps/eye_gaze/summary.json"), } def load_mps_slam(self) -> Dict[str, Union[pd.DataFrame, List]]: """Load MPS SLAM data.""" return { "closed_loop": self.load_file_by_name( "mps/slam/closed_loop_trajectory.csv" ), "open_loop": self.load_file_by_name("mps/slam/open_loop_trajectory.csv"), "calibration": self.load_file_by_name("mps/slam/online_calibration.jsonl"), } def get_vrs_files_info(self) -> List[Dict]: """Get information about VRS files.""" vrs_info = self.load_file_by_name("vrs_files_info.json") return self._deserialize_json(vrs_info) def print_dataset_summary(data: Any, name: str): """Print summary of loaded data.""" if isinstance(data, pd.DataFrame): print(f"\n{name}:") print(f"Shape: {data.shape}") print("Columns:", list(data.columns)) print("Sample data:") print(data.head(2)) elif isinstance(data, dict): print(f"\n{name}:") print("Keys:", list(data.keys())) elif isinstance(data, list): print(f"\n{name}:") print(f"Number of items: {len(data)}") if data: print("First item sample:", data[0]) def main(): """Example usage of the dataset loader.""" loader = ADTDatasetLoader() print(f"Loading sequence: {loader.sequence_name}") # Print available files print("\nAvailable files:") for file_info in loader.get_available_files(): print(f"- {file_info['filename']} ({file_info['type']})") # Load and print summaries of all data types print("\n=== Loading all data types ===") # Bounding boxes print_dataset_summary(loader.load_2d_bounding_boxes(), "2D Bounding Boxes") print_dataset_summary(loader.load_3d_bounding_boxes(), "3D Bounding Boxes") # Trajectory and eye gaze print_dataset_summary(loader.load_aria_trajectory(), "Aria Trajectory") print_dataset_summary(loader.load_eyegaze(), "Eye Gaze") # Scene objects print_dataset_summary(loader.load_scene_objects(), "Scene Objects") # JSON data print_dataset_summary(loader.load_instances(), "Instances") print_dataset_summary(loader.load_metadata(), "Metadata") # MPS data print_dataset_summary(loader.load_mps_eye_gaze(), "MPS Eye Gaze") print_dataset_summary(loader.load_mps_slam(), "MPS SLAM") # VRS files info print_dataset_summary(loader.get_vrs_files_info(), "VRS Files") if __name__ == "__main__": main()