From f1e968ac210bc93909eab9edd562fda33b8964bb Mon Sep 17 00:00:00 2001 From: Max Veit Date: Fri, 11 Dec 2020 18:04:55 +0100 Subject: [PATCH 1/3] Fix circular import of SparsePoints fix #294 Still need to update some examples, since this required taking some "convenience imports" out of the __init__ files --- bindings/rascal/models/__init__.py | 1 - bindings/rascal/utils/__init__.py | 8 ++++---- bindings/rascal/utils/fps.py | 2 +- tests/python/python_models_test.py | 3 ++- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bindings/rascal/models/__init__.py b/bindings/rascal/models/__init__.py index d8634b992..42737a256 100644 --- a/bindings/rascal/models/__init__.py +++ b/bindings/rascal/models/__init__.py @@ -1,3 +1,2 @@ -from .sparse_points import SparsePoints from .krr import train_gap_model, KRR from .kernels import Kernel diff --git a/bindings/rascal/utils/__init__.py b/bindings/rascal/utils/__init__.py index f33c1aaae..8a9b63aeb 100644 --- a/bindings/rascal/utils/__init__.py +++ b/bindings/rascal/utils/__init__.py @@ -1,7 +1,3 @@ -from .fps import fps, FPSFilter -from ..lib._rascal.utils import ostream_redirect -from ..lib import utils -from copy import deepcopy from .io import ( BaseIO, to_dict, @@ -11,6 +7,10 @@ dump_obj, load_obj, ) +from .fps import fps, FPSFilter +from ..lib._rascal.utils import ostream_redirect +from ..lib import utils +from copy import deepcopy from .cur import CURFilter from .scorer import get_score, print_score diff --git a/bindings/rascal/utils/fps.py b/bindings/rascal/utils/fps.py index 06089d1e3..06b712cc2 100644 --- a/bindings/rascal/utils/fps.py +++ b/bindings/rascal/utils/fps.py @@ -1,5 +1,6 @@ from .io import BaseIO from ..lib import sparsification +from ..models.sparse_points import SparsePoints from .filter_utils import ( get_index_mappings_sample_per_species, @@ -236,7 +237,6 @@ def select(self, managers): raise NotImplementedError("method: {}".format(self.act_on)) def filter(self, managers, n_select=None): - from ..models import SparsePoints if n_select is None: n_select = self.Nselect diff --git a/tests/python/python_models_test.py b/tests/python/python_models_test.py index 3970071a0..ec94ac2ed 100644 --- a/tests/python/python_models_test.py +++ b/tests/python/python_models_test.py @@ -1,5 +1,6 @@ from rascal.representations import SphericalInvariants -from rascal.models import Kernel, SparsePoints +from rascal.models import Kernel +from rascal.models.sparse_points import SparsePoints from rascal.models.kernels import compute_numerical_kernel_gradients from rascal.utils import from_dict, to_dict from test_utils import load_json_frame, BoxList, Box, compute_relative_error From 7eede1a2f736d4ff1b226c346e9d4630cb60f143 Mon Sep 17 00:00:00 2001 From: Max Veit Date: Thu, 25 Feb 2021 16:00:04 +0100 Subject: [PATCH 2/3] Resolve more circular dependencies in rascal.utils --- bindings/rascal/utils/__init__.py | 25 ++++++------------------- bindings/rascal/utils/misc.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 19 deletions(-) create mode 100644 bindings/rascal/utils/misc.py diff --git a/bindings/rascal/utils/__init__.py b/bindings/rascal/utils/__init__.py index 8a9b63aeb..70cb05b88 100644 --- a/bindings/rascal/utils/__init__.py +++ b/bindings/rascal/utils/__init__.py @@ -7,27 +7,14 @@ dump_obj, load_obj, ) +from .misc import is_notebook + +# Warning potential dependency loop: FPS imports models, which imports KRR, +# which imports this file again from .fps import fps, FPSFilter + +# function to redirect c++'s standard output to python's one from ..lib._rascal.utils import ostream_redirect -from ..lib import utils from copy import deepcopy from .cur import CURFilter from .scorer import get_score, print_score - -# function to redirect c++'s standard output to python's one -ostream_redirect = utils.__dict__["ostream_redirect"] - - -def is_notebook(): - from IPython import get_ipython - - try: - shell = get_ipython().__class__.__name__ - if shell == "ZMQInteractiveShell": - return True # Jupyter notebook or qtconsole - elif shell == "TerminalInteractiveShell": - return False # Terminal running IPython - else: - return False # Other type (?) - except NameError: - return False # Probably standard Python interpreter diff --git a/bindings/rascal/utils/misc.py b/bindings/rascal/utils/misc.py new file mode 100644 index 000000000..8d77f6c79 --- /dev/null +++ b/bindings/rascal/utils/misc.py @@ -0,0 +1,17 @@ +"""Miscellaneous useful utilities""" + + +def is_notebook(): + """Is this being run inside an IPython Notebook?""" + from IPython import get_ipython + + try: + shell = get_ipython().__class__.__name__ + if shell == "ZMQInteractiveShell": + return True # Jupyter notebook or qtconsole + elif shell == "TerminalInteractiveShell": + return False # Terminal running IPython + else: + return False # Other type (?) + except NameError: + return False # Probably standard Python interpreter From ac8002c95d1742fe07a3dd2f4cfcc51f3bb48cb2 Mon Sep 17 00:00:00 2001 From: Max Veit Date: Tue, 23 Mar 2021 15:33:50 +0100 Subject: [PATCH 3/3] Update tests and examples with explicit SparsePoints location --- examples/MLIP_example.ipynb | 3 ++- examples/Spherical_invariants_and_database_exploration.ipynb | 3 ++- tests/python/python_representation_calculator_test.py | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/MLIP_example.ipynb b/examples/MLIP_example.ipynb index d87e018cf..dcb0b31b4 100644 --- a/examples/MLIP_example.ipynb +++ b/examples/MLIP_example.ipynb @@ -43,7 +43,8 @@ "import json\n", "\n", "from rascal.representations import SphericalInvariants\n", - "from rascal.models import Kernel, SparsePoints, train_gap_model\n", + "from rascal.models import Kernel, train_gap_model\n", + "from rascal.models.sparse_points import SparsePoints\n", "from rascal.models.IP_ase_interface import ASEMLCalculator\n", "from rascal.neighbourlist import AtomsList\n", "from rascal.utils import from_dict, to_dict, CURFilter, dump_obj, load_obj, get_score, print_score" diff --git a/examples/Spherical_invariants_and_database_exploration.ipynb b/examples/Spherical_invariants_and_database_exploration.ipynb index a72977f8b..dfd31dc17 100644 --- a/examples/Spherical_invariants_and_database_exploration.ipynb +++ b/examples/Spherical_invariants_and_database_exploration.ipynb @@ -45,7 +45,8 @@ "from tqdm.notebook import tqdm\n", "\n", "from rascal.representations import SphericalInvariants\n", - "from rascal.models import Kernel, KRR, train_gap_model, SparsePoints\n", + "from rascal.models import Kernel, KRR, train_gap_model\n", + "from rascal.models.sparse_points import SparsePoints\n", "from rascal.utils import from_dict, to_dict, CURFilter, FPSFilter, get_score, print_score" ] }, diff --git a/tests/python/python_representation_calculator_test.py b/tests/python/python_representation_calculator_test.py index f8d6e4b12..e71de807e 100644 --- a/tests/python/python_representation_calculator_test.py +++ b/tests/python/python_representation_calculator_test.py @@ -4,7 +4,8 @@ SphericalInvariants, ) from rascal.utils import from_dict, to_dict, FPSFilter -from rascal.models import Kernel, SparsePoints +from rascal.models import Kernel +from rascal.models.sparse_points import SparsePoints from test_utils import load_json_frame, BoxList, Box, dot import unittest import numpy as np