|
| 1 | +from graph_net.sample_pass.sample_pass import SamplePass |
| 2 | +import graph_net.subgraph_range_util as subgraph_range_util |
| 3 | +from pathlib import Path |
| 4 | +import json |
| 5 | +from typing import Union |
| 6 | + |
| 7 | + |
| 8 | +class MergeSubgraphSources(SamplePass): |
| 9 | + def __init__(self, config=None): |
| 10 | + super().__init__(config) |
| 11 | + |
| 12 | + def declare_config( |
| 13 | + self, |
| 14 | + model_path_prefix: str = "", |
| 15 | + source_model_path_prefixes: list = None, |
| 16 | + subgraph_sources_json_file_name: str = "subgraph_sources.json", |
| 17 | + ): |
| 18 | + pass |
| 19 | + |
| 20 | + def __call__(self, rel_model_path: str): |
| 21 | + model_path_prefix = self.config.get("model_path_prefix", "") |
| 22 | + target_model_path = ( |
| 23 | + Path(model_path_prefix) / rel_model_path |
| 24 | + if model_path_prefix |
| 25 | + else Path(rel_model_path) |
| 26 | + ) |
| 27 | + |
| 28 | + source_model_path_prefixes = self.config.get("source_model_path_prefixes") or [] |
| 29 | + source_model_paths = [ |
| 30 | + Path(prefix) / rel_model_path for prefix in source_model_path_prefixes |
| 31 | + ] |
| 32 | + |
| 33 | + self.merge_sources_for_deduplication(target_model_path, source_model_paths) |
| 34 | + print(f"Merged {len(source_model_paths)} sources into {target_model_path}") |
| 35 | + |
| 36 | + def merge_sources_for_deduplication( |
| 37 | + self, |
| 38 | + target_model_path: Union[str, Path], |
| 39 | + source_model_paths: list[Union[str, Path]], |
| 40 | + ) -> dict[str, list[tuple[int, int]]]: |
| 41 | + merged_sources = self._load_sources(target_model_path) |
| 42 | + for source_path in source_model_paths: |
| 43 | + source_sources = self._load_sources(source_path) |
| 44 | + if source_sources: |
| 45 | + merged_sources = subgraph_range_util.merge_subgraph_ranges( |
| 46 | + merged_sources, source_sources |
| 47 | + ) |
| 48 | + self._save_sources(target_model_path, merged_sources) |
| 49 | + return merged_sources |
| 50 | + |
| 51 | + def _get_sources_file_path(self, model_path: Union[str, Path]) -> Path: |
| 52 | + return Path(model_path) / self.config["subgraph_sources_json_file_name"] |
| 53 | + |
| 54 | + def _load_sources( |
| 55 | + self, model_path: Union[str, Path] |
| 56 | + ) -> dict[str, list[tuple[int, int]]]: |
| 57 | + file_path = self._get_sources_file_path(model_path) |
| 58 | + if not file_path.exists(): |
| 59 | + return {} |
| 60 | + with open(file_path, "r", encoding="utf-8") as f: |
| 61 | + return json.load(f) |
| 62 | + |
| 63 | + def _save_sources( |
| 64 | + self, |
| 65 | + model_path: Union[str, Path], |
| 66 | + sources: dict[str, list[tuple[int, int]]], |
| 67 | + ) -> None: |
| 68 | + file_path = self._get_sources_file_path(model_path) |
| 69 | + with open(file_path, "w", encoding="utf-8") as f: |
| 70 | + json.dump(sources, f, indent=4) |
0 commit comments