(self, regenerate_cache=False, trust_cache=False)
| 530 | quit() |
| 531 | |
| 532 | def cache_metadata(self, regenerate_cache=False, trust_cache=False): |
| 533 | def check_grouped_metadata(): |
| 534 | all_grouped_metadata_exists = False |
| 535 | unique_grouping_keys = None |
| 536 | if self.grouping_keys_json_file.exists(): |
| 537 | with open(self.grouping_keys_json_file) as f: |
| 538 | unique_grouping_keys = json.load(f) |
| 539 | if self.use_size_buckets and not all(len(key) == 3 for key in unique_grouping_keys): |
| 540 | # Using size buckets but have AR keys. |
| 541 | return False, unique_grouping_keys |
| 542 | elif not all(len(key) == 2 for key in unique_grouping_keys): |
| 543 | # Using AR buckets but have size bucket keys |
| 544 | return False, unique_grouping_keys |
| 545 | all_grouped_metadata_exists = all( |
| 546 | (self.cache_dir / f'metadata/grouped_metadata_{bucket_suffix(key)}').exists() |
| 547 | for key in unique_grouping_keys |
| 548 | ) |
| 549 | return all_grouped_metadata_exists, unique_grouping_keys |
| 550 | |
| 551 | # Check if all the grouped metadata datasets exist. If so, we can directly load them. |
| 552 | all_grouped_metadata_exists, unique_grouping_keys = check_grouped_metadata() |
| 553 | if regenerate_cache or not all_grouped_metadata_exists or not trust_cache: |
| 554 | # Otherwise, need to compute the ungrouped metadata and then group. |
| 555 | print('Grouped metadata is not cached. Computing ungrouped metadata and then grouping.') |
| 556 | unique_grouping_keys = self._group_metadata_and_save_to_disk(regenerate_cache=regenerate_cache, trust_cache=trust_cache) |
| 557 | else: |
| 558 | print('Found grouped metadata cache. Directly loading it.') |
| 559 | |
| 560 | for grouping_key in unique_grouping_keys: |
| 561 | grouped_cache_dir = self.cache_dir / f'metadata/grouped_metadata_{bucket_suffix(grouping_key)}' |
| 562 | print(f'Loading grouped metadata with grouping key {grouping_key}') |
| 563 | metadata = datasets.load_from_disk(str(grouped_cache_dir)) |
| 564 | if self.use_size_buckets: |
| 565 | assert len(grouping_key) == 3 |
| 566 | self.size_bucket_datasets.append( |
| 567 | SizeBucketDataset( |
| 568 | metadata, |
| 569 | self.directory_config, |
| 570 | grouping_key, |
| 571 | self.cache_dir, |
| 572 | self, |
| 573 | ) |
| 574 | ) |
| 575 | else: |
| 576 | self.ar_bucket_datasets.append( |
| 577 | ARBucketDataset( |
| 578 | grouping_key, |
| 579 | self.resolutions, |
| 580 | metadata, |
| 581 | self.directory_config, |
| 582 | self.cache_dir, |
| 583 | self.round_to_multiple, |
| 584 | self, |
| 585 | ) |
| 586 | ) |
| 587 | |
| 588 | def _group_metadata_and_save_to_disk(self, regenerate_cache=False, trust_cache=False): |
| 589 | metadata_dataset = self._get_ungrouped_metadata(regenerate_cache=regenerate_cache, trust_cache=trust_cache) |
nothing calls this directly
no test coverage detected