| """ |
| Data loading for CCFM. |
| Imports scDFM Data/PerturbationDataset by temporarily swapping sys.modules |
| so that scDFM's 'src.*' packages are visible during import. |
| """ |
|
|
| import sys |
| import os |
| import types |
|
|
| _SCDFM_ROOT = os.path.normpath( |
| os.path.join(os.path.dirname(__file__), "..", "..", "..", "scDFM") |
| ) |
|
|
| |
| _cached_classes = {} |
|
|
|
|
| def get_data_classes(): |
| """Lazily import scDFM data classes with proper module isolation.""" |
| if _cached_classes: |
| return ( |
| _cached_classes["Data"], |
| _cached_classes["PerturbationDataset"], |
| _cached_classes["TrainSampler"], |
| _cached_classes["TestDataset"], |
| ) |
|
|
| |
| saved = {} |
| for key in list(sys.modules.keys()): |
| if key == "src" or key.startswith("src."): |
| saved[key] = sys.modules.pop(key) |
|
|
| |
| for d in ["src", "src/data_process", "src/utils", "src/tokenizer"]: |
| init_path = os.path.join(_SCDFM_ROOT, d, "__init__.py") |
| if not os.path.exists(init_path): |
| os.makedirs(os.path.dirname(init_path), exist_ok=True) |
| with open(init_path, "w") as f: |
| f.write("# Auto-created by CCFM\n") |
|
|
| sys.path.insert(0, _SCDFM_ROOT) |
| try: |
| from src.data_process.data import Data, PerturbationDataset, TrainSampler, TestDataset |
| _cached_classes["Data"] = Data |
| _cached_classes["PerturbationDataset"] = PerturbationDataset |
| _cached_classes["TrainSampler"] = TrainSampler |
| _cached_classes["TestDataset"] = TestDataset |
| finally: |
| |
| for key in list(sys.modules.keys()): |
| if (key == "src" or key.startswith("src.")) and not key.startswith("scdfm_"): |
| del sys.modules[key] |
|
|
| |
| for key, mod in saved.items(): |
| sys.modules[key] = mod |
|
|
| if _SCDFM_ROOT in sys.path: |
| sys.path.remove(_SCDFM_ROOT) |
|
|
| return Data, PerturbationDataset, TrainSampler, TestDataset |
|
|