|
| 1 | +""" |
| 2 | +Regression test for https://github.com/shenyangHuang/TGB/issues/127. |
| 3 | +
|
| 4 | +Simulates torch/torch_geometric being uninstalled and verifies that the |
| 5 | +numpy-only import surface (dataset classes, ``Evaluator``, negative samplers, |
| 6 | +``tgb.utils.utils``) still imports and works. Also locks in the intentional |
| 7 | +boundary: the PyG-only wrapper modules (``dataset_pyg``) still require torch |
| 8 | +and should fail with a clear ``ImportError`` rather than something more |
| 9 | +confusing, since guarding them would strip their only purpose. |
| 10 | +""" |
| 11 | +import builtins |
| 12 | +import importlib |
| 13 | +import sys |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | + |
| 18 | +TGB_MODULES = [ |
| 19 | + "tgb.utils.utils", |
| 20 | + "tgb.utils.pre_process", |
| 21 | + "tgb.linkproppred.dataset", |
| 22 | + "tgb.linkproppred.evaluate", |
| 23 | + "tgb.linkproppred.negative_sampler", |
| 24 | + "tgb.linkproppred.thg_negative_sampler", |
| 25 | + "tgb.linkproppred.tkg_negative_sampler", |
| 26 | + "tgb.nodeproppred.dataset", |
| 27 | + "tgb.nodeproppred.evaluate", |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def no_torch(monkeypatch): |
| 33 | + """Simulate torch/torch_geometric being uninstalled and force TGB |
| 34 | + modules to re-import under that condition.""" |
| 35 | + saved_modules = sys.modules.copy() |
| 36 | + for name in list(sys.modules): |
| 37 | + if name == "torch" or name.startswith(("torch.", "torch_geometric")) \ |
| 38 | + or name.startswith("tgb."): |
| 39 | + sys.modules.pop(name, None) |
| 40 | + |
| 41 | + real_import = builtins.__import__ |
| 42 | + |
| 43 | + def fake_import(name, *args, **kwargs): |
| 44 | + if name == "torch" or name.startswith(("torch.", "torch_geometric")): |
| 45 | + raise ImportError(f"simulated: {name} is not installed") |
| 46 | + return real_import(name, *args, **kwargs) |
| 47 | + |
| 48 | + monkeypatch.setattr(builtins, "__import__", fake_import) |
| 49 | + yield |
| 50 | + sys.modules.clear() |
| 51 | + sys.modules.update(saved_modules) |
| 52 | + |
| 53 | + |
| 54 | +def test_core_modules_import_without_torch(no_torch): |
| 55 | + for name in TGB_MODULES: |
| 56 | + mod = importlib.import_module(name) |
| 57 | + assert getattr(mod, "torch", None) is None |
| 58 | + |
| 59 | + |
| 60 | +def test_negative_sampler_works_without_torch(no_torch): |
| 61 | + negative_sampler = importlib.import_module("tgb.linkproppred.negative_sampler") |
| 62 | + sampler = negative_sampler.NegativeEdgeSampler(dataset_name="tgbl-mock") |
| 63 | + sampler.eval_set["test"] = {(0, 1, 0): [5, 6, 7]} |
| 64 | + neg = sampler.query_batch( |
| 65 | + np.array([0]), np.array([1]), np.array([0]), split_mode="test" |
| 66 | + ) |
| 67 | + assert neg == [[5, 6, 7]] |
| 68 | + |
| 69 | + |
| 70 | +def test_evaluator_works_without_torch(no_torch): |
| 71 | + evaluate = importlib.import_module("tgb.linkproppred.evaluate") |
| 72 | + evaluator = evaluate.Evaluator(name="tgbl-wiki", k_value=10) |
| 73 | + result = evaluator.eval( |
| 74 | + { |
| 75 | + "y_pred_pos": np.array([1.0]), |
| 76 | + "y_pred_neg": np.array([[0.5, 0.9, 2.0]]), |
| 77 | + "eval_metric": ["mrr"], |
| 78 | + } |
| 79 | + ) |
| 80 | + assert result["mrr"] == pytest.approx(0.5) |
| 81 | + |
| 82 | + |
| 83 | +def test_set_random_seed_works_without_torch(no_torch): |
| 84 | + utils = importlib.import_module("tgb.utils.utils") |
| 85 | + utils.set_random_seed(0) # should not raise even though torch is None |
| 86 | + |
| 87 | + |
| 88 | +def test_pyg_dataset_still_requires_torch(no_torch): |
| 89 | + with pytest.raises(ImportError): |
| 90 | + importlib.import_module("tgb.linkproppred.dataset_pyg") |
0 commit comments