Skip to content

Commit 2ad33cc

Browse files
authored
Merge pull request #270 from materialsproject/numba-optional-dep
Make `dscribe` an optional dependency
2 parents ae03e10 + 0a2f4cf commit 2ad33cc

File tree

3 files changed

+160
-208
lines changed

3 files changed

+160
-208
lines changed

crystal_toolkit/components/localenv.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import itertools
22
from multiprocessing import cpu_count
3+
from warnings import warn
34

4-
from dash import dcc
5-
from dash import html
65
import plotly.express as px
7-
from dash import callback_context
6+
from dash import callback_context, dcc, html
87
from dash.dependencies import Input, Output
98
from dash.exceptions import PreventUpdate
10-
from dash_mp_components import GraphComponent
11-
from dscribe.descriptors import SOAP
12-
from dscribe.kernels import REMatchKernel
13-
from pymatgen.ext.matproj import MPRester
9+
import dash_mp_components as mpc
1410
from pymatgen.analysis.chemenv.coordination_environments.chemenv_strategies import (
1511
SimplestChemenvStrategy,
1612
)
@@ -23,29 +19,40 @@
2319
from pymatgen.analysis.chemenv.coordination_environments.structure_environments import (
2420
LightStructureEnvironments,
2521
)
26-
from pymatgen.analysis.graphs import MoleculeGraph
27-
from pymatgen.analysis.graphs import StructureGraph
28-
from pymatgen.analysis.local_env import cn_opt_params, LocalStructOrderParams
22+
from pymatgen.analysis.graphs import MoleculeGraph, StructureGraph
23+
from pymatgen.analysis.local_env import LocalStructOrderParams, cn_opt_params
2924
from pymatgen.core.structure import Molecule, Structure
25+
from pymatgen.ext.matproj import MPRester
3026
from pymatgen.io.ase import AseAtomsAdaptor
3127
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
32-
from pymatgen.util.string import unicodeify_species, unicodeify
28+
from pymatgen.util.string import unicodeify, unicodeify_species
3329
from sklearn.preprocessing import normalize
3430

3531
from crystal_toolkit.components.structure import StructureMoleculeComponent
3632
from crystal_toolkit.core.legend import Legend
3733
from crystal_toolkit.core.panelcomponent import PanelComponent
3834
from crystal_toolkit.helpers.layouts import (
39-
get_data_list,
40-
Columns,
41-
Column,
42-
get_tooltip,
43-
cite_me,
44-
Loading,
4535
H5,
36+
Column,
37+
Columns,
4638
Label,
39+
Loading,
40+
cite_me,
41+
get_data_list,
42+
get_tooltip,
4743
)
4844

45+
try:
46+
from dscribe.descriptors import SOAP
47+
from dscribe.kernels import REMatchKernel
48+
except ImportError:
49+
warn(
50+
"Using dscribe SOAP and REMatchKernel requires the dscribe package "
51+
"which was made optional since it in turn requires numba and numba "
52+
"was a common source of installation issues."
53+
)
54+
SOAP = None
55+
4956

5057
def _get_local_order_parameters(structure_graph, n):
5158
"""
@@ -530,6 +537,9 @@ def update_soap_analysis(struct, all_kwargs):
530537

531538
if not struct:
532539
raise PreventUpdate
540+
541+
if not SOAP:
542+
return mpc.Markdown("This feature will not work unless `dscribe` is installed on the server.")
533543

534544
struct = self.from_data(struct)
535545
kwargs = self.reconstruct_kwargs_from_state(callback_context.inputs)
@@ -563,7 +573,7 @@ def _get_all_structs_from_elements(elements):
563573

564574
with MPRester() as mpr:
565575
docs = mpr.query(
566-
{"chemsys": {"$in": all_chemsyses}}, ["task_id", "structure"],
576+
{"chemsys": {"$in": all_chemsyses}}, ["task_id", "structure"]
567577
)
568578
structs.update({d["task_id"]: d["structure"] for d in docs})
569579
return structs
@@ -577,6 +587,10 @@ def update_soap_similarities(struct, all_kwargs):
577587
if not struct:
578588
raise PreventUpdate
579589

590+
if not SOAP:
591+
return mpc.Markdown("This feature will not work unless `dscribe` is installed on the server.")
592+
593+
580594
structs = {"input": self.from_data(struct)}
581595
kwargs = self.reconstruct_kwargs_from_state(callback_context.inputs)
582596

@@ -706,7 +720,7 @@ def update_bondinggraph_analysis(graph, display_options):
706720
}
707721

708722
return html.Div(
709-
[GraphComponent(graph=graph_data, options=options)],
723+
[mpc.GraphComponent(graph=graph_data, options=options)],
710724
style={"width": "65vmin", "height": "65vmin"},
711725
)
712726

0 commit comments

Comments
 (0)