Read dataset from parquet files with a strict limit to prevent memory issues.
(dataset_name, max_vectors=50000)
| 431 | |
| 432 | |
| 433 | def read_dataset_from_parquet(dataset_name, max_vectors=50000): |
| 434 | """Read dataset from parquet files with a strict limit to prevent memory issues.""" |
| 435 | metadata = datasets[dataset_name] |
| 436 | |
| 437 | datasets_dir = "datasets" |
| 438 | dataset_dir = os.path.join(datasets_dir, dataset_name) |
| 439 | |
| 440 | path = os.path.join(dataset_dir, "test0.parquet") |
| 441 | if not os.path.exists(path): |
| 442 | raise FileNotFoundError( |
| 443 | f"No parquet files found for dataset {dataset_name}. " |
| 444 | f"Please place your parquet files in: {os.path.abspath(dataset_dir)}" |
| 445 | ) |
| 446 | |
| 447 | vectors = [] |
| 448 | file_count = 0 |
| 449 | |
| 450 | print(f"Reading dataset with limit of {max_vectors} vectors to prevent memory issues...") |
| 451 | |
| 452 | while os.path.exists(path) and len(vectors) < max_vectors: |
| 453 | # Read the full parquet file but limit processing |
| 454 | df = pd.read_parquet(path) |
| 455 | |
| 456 | # Limit rows to prevent memory overload |
| 457 | rows_to_read = min(len(df), max_vectors - len(vectors)) |
| 458 | df = df.head(rows_to_read) |
| 459 | |
| 460 | emb_col = "dense_values" if "dense_values" in df.columns else metadata["embeddings"] |
| 461 | |
| 462 | if metadata["id"] is not None: |
| 463 | id_col = metadata["id"] |
| 464 | dataset_chunk = df[[id_col, emb_col]].values.tolist() |
| 465 | else: |
| 466 | dataset_chunk = list(enumerate(df[emb_col].values)) |
| 467 | |
| 468 | for row in dataset_chunk: |
| 469 | if len(vectors) >= max_vectors: |
| 470 | break |
| 471 | vector = pre_process_vector(row[0], row[1]) |
| 472 | vectors.append(vector) |
| 473 | |
| 474 | file_count += 1 |
| 475 | path = os.path.join(dataset_dir, f"test{file_count}.parquet") |
| 476 | |
| 477 | # Clear dataframe from memory |
| 478 | del df |
| 479 | |
| 480 | if not vectors: |
| 481 | raise ValueError(f"No data found in dataset {dataset_name}") |
| 482 | |
| 483 | print(f"Loaded {len(vectors)} vectors from dataset {dataset_name}") |
| 484 | return vectors |
| 485 | |
| 486 | def read_single_parquet_file(path, dataset_name, file_index, base_id, quick_test=False): |
| 487 | """Read and process a single parquet file""" |
nothing calls this directly
no test coverage detected