11from __future__ import annotations
22
33import os
4- from typing import TYPE_CHECKING
54
5+ import pandas as pd
66from dash import dcc
77from pymatgen .symmetry .analyzer import SpacegroupAnalyzer
88from tqdm import tqdm
99
10- if TYPE_CHECKING :
11- import pandas as pd
10+ try :
11+ from matminer .datasets import load_dataset
12+ except ImportError :
13+ load_dataset = None
1214
1315
1416def load_and_store_matbench_dataset (dataset_name : str ) -> pd .DataFrame :
@@ -18,34 +20,30 @@ def load_and_store_matbench_dataset(dataset_name: str) -> pd.DataFrame:
1820 data_path = os .path .join (os .path .dirname (__file__ ), f"{ dataset_name } .json.gz" )
1921
2022 if os .path .isfile (data_path ):
21- import pandas as pd
22-
2323 df = pd .read_json (data_path )
2424 else :
25- try :
26- from matminer .datasets import load_dataset
25+ if not load_dataset :
26+ raise ImportError (
27+ "matminer is not installed but needed to download a dataset. Run "
28+ "`pip install matminer`"
29+ )
2730
28- df = load_dataset (dataset_name )
31+ df = load_dataset (dataset_name )
2932
30- if "structure" in df :
31- df [["spg_symbol" , "spg_num" ]] = [
32- struct .get_space_group_info ()
33- for struct in tqdm (df .structure , desc = "Getting space groups" )
34- ]
33+ if "structure" in df :
34+ df [["spg_symbol" , "spg_num" ]] = [
35+ struct .get_space_group_info ()
36+ for struct in tqdm (df .structure , desc = "Getting space groups" )
37+ ]
3538
36- df ["crystal_sys" ] = [
37- SpacegroupAnalyzer (x ).get_crystal_system () for x in df .structure
38- ]
39+ df ["crystal_sys" ] = [
40+ SpacegroupAnalyzer (x ).get_crystal_system () for x in df .structure
41+ ]
3942
40- df ["volume" ] = [x .volume for x in df .structure ]
41- df ["formula" ] = [x .formula for x in df .structure ]
43+ df ["volume" ] = [x .volume for x in df .structure ]
44+ df ["formula" ] = [x .formula for x in df .structure ]
4245
43- df .to_json (data_path , default_handler = lambda x : x .as_dict ())
44- except ImportError :
45- print (
46- "matminer is not installed but needed to download a dataset. Run "
47- "`pip install matminer`"
48- )
46+ df .to_json (data_path , default_handler = lambda x : x .as_dict ())
4947
5048 return df
5149
0 commit comments