|
| 1 | +# %% |
| 2 | +import os |
| 3 | +from importlib.metadata import version |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import wandb |
| 7 | +from sklearn.ensemble import RandomForestRegressor |
| 8 | +from sklearn.impute import SimpleImputer |
| 9 | +from sklearn.metrics import r2_score |
| 10 | +from sklearn.pipeline import Pipeline |
| 11 | + |
| 12 | +from matbench_discovery import DEBUG, ROOT, today |
| 13 | +from matbench_discovery.plot_scripts import df_wbm |
| 14 | +from matbench_discovery.plots import wandb_log_scatter |
| 15 | +from matbench_discovery.slurm import slurm_submit |
| 16 | +from models.voronoi import featurizer |
| 17 | + |
| 18 | +__author__ = "Janosh Riebesell" |
| 19 | +__date__ = "2022-11-26" |
| 20 | + |
| 21 | + |
| 22 | +# %% |
| 23 | +module_dir = os.path.dirname(__file__) |
| 24 | +task_type = "IS2RE" |
| 25 | +print(f"{task_type=}") |
| 26 | + |
| 27 | +out_dir = f"{module_dir}/{today}-train-test" |
| 28 | +out_path = f"{out_dir}/e-form-preds-{task_type}.csv" |
| 29 | +if os.path.isfile(out_path): |
| 30 | + raise SystemExit(f"{out_path = } already exists, exciting early") |
| 31 | + |
| 32 | +job_name = f"train-test-voronoi-rf{'-debug' if DEBUG else ''}" |
| 33 | + |
| 34 | +slurm_vars = slurm_submit( |
| 35 | + job_name=job_name, |
| 36 | + out_dir=out_dir, |
| 37 | + partition="icelake-himem", |
| 38 | + account="LEE-SL3-CPU", |
| 39 | + time="6:0:0", |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +# %% |
| 44 | +train_path = f"{module_dir}/2022-11-25-features-mp.csv.bz2" |
| 45 | +print(f"{train_path=}") |
| 46 | +df_train = pd.read_csv(train_path).set_index("material_id") |
| 47 | +print(f"{df_train.shape=}") |
| 48 | + |
| 49 | +mp_energies_path = f"{ROOT}/data/mp/2022-08-13-mp-energies.json.gz" |
| 50 | +df_mp = pd.read_json(mp_energies_path).set_index("material_id") |
| 51 | +train_target_col = "formation_energy_per_atom" |
| 52 | +df_train[train_target_col] = df_mp[train_target_col] |
| 53 | + |
| 54 | + |
| 55 | +test_path = f"{module_dir}/2022-11-18-features-wbm-{task_type}.csv.bz2" |
| 56 | +print(f"{test_path=}") |
| 57 | +df_test = pd.read_csv(test_path).set_index("material_id") |
| 58 | +print(f"{df_test.shape=}") |
| 59 | + |
| 60 | +test_target_col = "e_form_per_atom_mp2020_corrected" |
| 61 | +df_test[test_target_col] = df_wbm[test_target_col] |
| 62 | +model_name = "Voronoi RandomForestRegressor" |
| 63 | + |
| 64 | +run_params = dict( |
| 65 | + train_path=train_path, |
| 66 | + test_path=test_path, |
| 67 | + mp_energies_path=mp_energies_path, |
| 68 | + scikit_learn_version=version("scikit-learn"), |
| 69 | + matminer_version=version("matminer"), |
| 70 | + model_name=model_name, |
| 71 | + train_target_col=train_target_col, |
| 72 | + test_target_col=test_target_col, |
| 73 | + df_train=dict(shape=str(df_train.shape)), |
| 74 | + df_test=dict(shape=str(df_test.shape)), |
| 75 | + slurm_vars=slurm_vars, |
| 76 | +) |
| 77 | + |
| 78 | +wandb.init(project="matbench-discovery", name=job_name, config=run_params) |
| 79 | + |
| 80 | + |
| 81 | +# %% |
| 82 | +feature_names = featurizer.feature_labels() |
| 83 | +n_nans = df_train[feature_names].isna().any(axis=1).sum() |
| 84 | + |
| 85 | +print(f"train set NaNs: {n_nans:,} / {len(df_train):,} = {n_nans/len(df_train):.3%}") |
| 86 | + |
| 87 | +df_train = df_train.dropna(subset=feature_names) |
| 88 | + |
| 89 | + |
| 90 | +# %% |
| 91 | +model = Pipeline( |
| 92 | + [ |
| 93 | + ("imputer", SimpleImputer()), # For the failed structures |
| 94 | + ("model", RandomForestRegressor(n_estimators=150, n_jobs=-1, verbose=1)), |
| 95 | + ] |
| 96 | +) |
| 97 | + |
| 98 | + |
| 99 | +# %% |
| 100 | +model.fit(df_train[feature_names], df_train[train_target_col]) |
| 101 | + |
| 102 | + |
| 103 | +# %% |
| 104 | +n_nans = df_test[feature_names].isna().any(axis=1).sum() |
| 105 | +print(f"test set NaNs: {n_nans:,} / {len(df_train):,} = {n_nans/len(df_train):.1%}") |
| 106 | + |
| 107 | +df_test = df_test.dropna(subset=feature_names) |
| 108 | + |
| 109 | +pred_col = "e_form_per_atom_voronoi_rf" |
| 110 | +df_test[pred_col] = model.predict(df_test[feature_names]) |
| 111 | +df_wbm[pred_col] = df_test[pred_col] |
| 112 | + |
| 113 | +df_wbm[pred_col].to_csv(out_path) |
| 114 | + |
| 115 | +table = wandb.Table( |
| 116 | + dataframe=df_wbm[["formula", test_target_col, pred_col]].reset_index() |
| 117 | +) |
| 118 | + |
| 119 | +df_wbm[pred_col].isna().sum() |
| 120 | +MAE = (df_wbm[test_target_col] - df_wbm[pred_col]).abs().mean() |
| 121 | +R2 = r2_score(*df_wbm[[test_target_col, pred_col]].dropna().to_numpy().T) |
| 122 | +title = f"{model_name} {task_type} {MAE=:.3} {R2=:.3}" |
| 123 | +print(title) |
| 124 | + |
| 125 | +wandb_log_scatter(table, fields=dict(x=test_target_col, y=pred_col), title=title) |
0 commit comments