|
| 1 | +"""Compute t-SNE and UMAP projections of the WBM and MP datasets.""" |
| 2 | + |
| 3 | + |
| 4 | +# %% |
| 5 | +import os |
| 6 | +from typing import Any, Literal |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +from pymatgen.core import Composition |
| 11 | +from tqdm import tqdm |
| 12 | + |
| 13 | +from matbench_discovery import ROOT |
| 14 | +from matbench_discovery.data import DATA_FILES |
| 15 | +from matbench_discovery.slurm import slurm_submit |
| 16 | + |
| 17 | +__author__ = "Janosh Riebesell" |
| 18 | +__date__ = "2023-03-28" |
| 19 | + |
| 20 | + |
| 21 | +data_name = "mp" # which data to project |
| 22 | +projection_type: Literal["tsne", "umap"] = "tsne" # which projection method to use |
| 23 | +out_dim = 2 # number of dimensions to project to |
| 24 | +one_hot_dim = 112 # number of elements to use for one-hot encoding |
| 25 | + |
| 26 | +out_dir = f"{ROOT}/data/{data_name}/{projection_type}" |
| 27 | +os.makedirs(out_dir, exist_ok=True) |
| 28 | + |
| 29 | +slurm_vars = slurm_submit( |
| 30 | + job_name=f"{data_name}-{projection_type}-{out_dim}d", |
| 31 | + out_dir=out_dir, |
| 32 | + partition="icelake-himem", |
| 33 | + account="LEE-SL3-CPU", |
| 34 | + time="6:0:0", |
| 35 | +) |
| 36 | + |
| 37 | +data_path = {"wbm": DATA_FILES.wbm_summary, "mp": DATA_FILES.mp_energies}[data_name] |
| 38 | +print(f"{data_path=}") |
| 39 | +print(f"{out_dim=}") |
| 40 | +print(f"{projection_type=}") |
| 41 | +df_in = pd.read_csv(data_path, na_filter=False).set_index("material_id") |
| 42 | + |
| 43 | + |
| 44 | +def metric( |
| 45 | + x: np.ndarray, |
| 46 | + y: np.ndarray, |
| 47 | + err_weight: float = 3, |
| 48 | + split_dim: int = one_hot_dim, |
| 49 | +) -> float: |
| 50 | + """Custom metric for t-SNE/UMAP that weights the error dimension higher by a factor |
| 51 | + of err_weight than the composition dimensions. |
| 52 | + """ |
| 53 | + x_comp, x_err = np.split(x, [split_dim]) |
| 54 | + y_comp, y_err = np.split(y, [split_dim]) |
| 55 | + return np.linalg.norm(x_comp - y_comp) + err_weight * np.linalg.norm(x_err - y_err) |
| 56 | + |
| 57 | + |
| 58 | +if projection_type == "tsne": |
| 59 | + from sklearn.manifold import TSNE |
| 60 | + |
| 61 | + projector = TSNE( |
| 62 | + n_components=out_dim, random_state=0, n_iter=250, n_iter_without_progress=50 |
| 63 | + ) |
| 64 | + out_cols = [f"t-SNE {idx}" for idx in range(out_dim)] |
| 65 | +elif projection_type == "umap": |
| 66 | + from umap import UMAP |
| 67 | + |
| 68 | + # TODO this execution path is untested (was never run yet) |
| 69 | + projector = UMAP(n_components=out_dim, random_state=0, metric=metric) |
| 70 | + out_cols = [f"t-SNE {idx+1}" for idx in range(out_dim)] |
| 71 | + |
| 72 | +identity = np.eye(one_hot_dim) |
| 73 | + |
| 74 | + |
| 75 | +def sum_one_hot_elem(formula: str) -> np.ndarray[Any, np.int64]: |
| 76 | + """Return sum of one-hot encoded elements in weighted by amount in composition.""" |
| 77 | + return sum(identity[el.Z - 1] * amt for el, amt in Composition(formula).items()) |
| 78 | + |
| 79 | + |
| 80 | +in_col = {"wbm": "formula", "mp": "formula_pretty"}[data_name] |
| 81 | +df_in[f"one_hot_{one_hot_dim}"] = [ |
| 82 | + sum_one_hot_elem(formula) for formula in tqdm(df_in[in_col]) |
| 83 | +] |
| 84 | + |
| 85 | + |
| 86 | +one_hot_encoding = np.array(df_in[f"one_hot_{one_hot_dim}"].to_list()) |
| 87 | +projections = projector.fit_transform(one_hot_encoding) |
| 88 | + |
| 89 | +df_in[out_cols] = projections |
| 90 | + |
| 91 | +out_path = f"{out_dir}/one-hot-{one_hot_dim}-composition-{out_dim}d.csv" |
| 92 | +df_in[out_cols].to_csv(out_path) |
| 93 | + |
| 94 | +print(f"Wrote projections to {out_path!r}") |
0 commit comments