| import argparse |
| import json |
| import shutil |
| from pathlib import Path |
| from multiprocessing import Pool, cpu_count |
| import tqdm |
|
|
| def is_valid_json_structure(file_path): |
| """Check if the JSON file has the expected structure.""" |
| try: |
| with open(file_path, "r") as file: |
| data = json.load(file) |
| |
| |
| if not all(key in data for key in ["name", "objects", "roads", "tl_states"]): |
| return False |
| |
| |
| if not isinstance(data["objects"], list) or not all( |
| isinstance(obj, dict) and "position" in obj and "type" in obj |
| for obj in data["objects"] |
| ): |
| return False |
| |
| |
| if not isinstance(data["roads"], list) or not all( |
| isinstance(road, dict) and "geometry" in road |
| for road in data["roads"] |
| ): |
| return False |
| |
| |
| for road in data["roads"]: |
| if not all( |
| isinstance(geo, dict) and "x" in geo and "y" in geo |
| for geo in road.get("geometry", []) |
| ): |
| return False |
| |
| return True |
| except (json.JSONDecodeError, ValueError, IOError): |
| return False |
|
|
| def process_file(args): |
| """ |
| Validate JSON file and handle it according to the operation mode. |
| |
| Args: |
| args (tuple): (source_path, target_dir, should_move) |
| - source_path: Path to the source file |
| - target_dir: Path to target directory (if moving) |
| - should_move: Boolean indicating if file should be moved if valid |
| Returns: |
| tuple: (str, bool) - (file path, whether file was valid) |
| """ |
| source_path, target_dir, should_move = args |
| |
| |
| if not is_valid_json_structure(source_path): |
| try: |
| source_path.unlink() |
| return str(source_path), False |
| except Exception as e: |
| print(f"Error deleting invalid file {source_path}: {e}") |
| return str(source_path), False |
| |
| |
| if should_move and target_dir: |
| try: |
| target_path = Path(target_dir) / source_path.name |
| shutil.move(str(source_path), str(target_path)) |
| except Exception as e: |
| print(f"Error moving file {source_path}: {e}") |
| return str(source_path), False |
| |
| return str(source_path), True |
|
|
| def process_directory(dataset_dir, num_workers=None): |
| """ |
| Process all JSON files in a directory, automatically handling group extraction if needed. |
| |
| Args: |
| dataset_dir (str): Path to the dataset directory |
| num_workers (int, optional): Number of processes to use. Defaults to CPU count. |
| Returns: |
| tuple: (int, int) - (valid_files, invalid_files) |
| """ |
| dataset_path = Path(dataset_dir) |
| |
| if not dataset_path.is_dir(): |
| print(f"Directory {dataset_dir} does not exist, skipping...") |
| return 0, 0 |
| |
| |
| group_dirs = [d for d in dataset_path.iterdir() |
| if d.is_dir() and d.name.startswith("group_")] |
| |
| |
| all_files = [] |
| |
| if group_dirs: |
| |
| print(f"\nFound {len(group_dirs)} group directories in {dataset_dir}") |
| for group_dir in sorted(group_dirs): |
| files = list(group_dir.glob("*.json")) |
| all_files.extend([(file, dataset_path, True) for file in files]) |
| |
| |
| main_dir_files = [f for f in dataset_path.glob("*.json") |
| if not any(g.name in str(f) for g in group_dirs)] |
| all_files.extend([(file, None, False) for file in main_dir_files]) |
| |
| total_files = len(all_files) |
| if total_files == 0: |
| print(f"No JSON files found in {dataset_dir}") |
| return 0, 0 |
| |
| print(f"Total files to process: {total_files}") |
| |
| |
| if num_workers is None: |
| num_workers = cpu_count() |
| |
| |
| valid_files = 0 |
| invalid_files = 0 |
| |
| |
| with Pool(processes=num_workers) as pool: |
| |
| results = list(tqdm.tqdm( |
| pool.imap_unordered(process_file, all_files), |
| total=total_files, |
| desc=f"Processing files from {dataset_dir}" |
| )) |
| |
| |
| for _, is_valid in results: |
| if is_valid: |
| valid_files += 1 |
| else: |
| invalid_files += 1 |
| |
| |
| if group_dirs: |
| for group_dir in group_dirs: |
| try: |
| group_dir.rmdir() |
| except OSError: |
| print(f"Warning: Could not remove directory {group_dir} - it may not be empty") |
| |
| print(f"\nCompleted processing {dataset_dir}") |
| print(f"Valid files: {valid_files}") |
| print(f"Invalid files deleted: {invalid_files}") |
| |
| return valid_files, invalid_files |
|
|
| def process_all_directories(num_workers=None): |
| """Process all dataset directories (training, testing, validation).""" |
| directories = [ |
| "data/processed/training", |
| "data/processed/testing", |
| "data/processed/validation" |
| ] |
| |
| total_valid = 0 |
| total_invalid = 0 |
| |
| for directory in directories: |
| print(f"\nProcessing directory: {directory}") |
| valid, invalid = process_directory(directory, num_workers) |
| total_valid += valid |
| total_invalid += invalid |
| |
| print("\nOverall Statistics:") |
| print(f"Total valid files across all directories: {total_valid}") |
| print(f"Total invalid files deleted: {total_invalid}") |
| print(f"Total files processed: {total_valid + total_invalid}") |
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Process JSON files in dataset directories, validating their structure and " |
| "automatically extracting from group directories if they exist. " |
| "Invalid files are deleted. " |
| 'Use "all" to process training, testing, and validation directories.' |
| ) |
| parser.add_argument( |
| "dataset_dir", |
| nargs="?", |
| default="all", |
| help='Path to the dataset directory or "all" for processing all directories' |
| ) |
| parser.add_argument( |
| "--num_workers", |
| type=int, |
| help="Number of processes to use (defaults to number of CPU cores)", |
| default=cpu_count() |
| ) |
| |
| args = parser.parse_args() |
| |
| try: |
| if args.dataset_dir.lower() == "all": |
| process_all_directories(args.num_workers) |
| else: |
| process_directory(args.dataset_dir, args.num_workers) |
| except Exception as e: |
| print(f"Error: {e}") |
| return 1 |
| |
| return 0 |
|
|
| if __name__ == "__main__": |
| exit(main()) |
|
|