-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from OpenDrugDiscovery/adding_datasets
Adding datasets
- Loading branch information
Showing
13 changed files
with
686 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,3 +146,4 @@ nohup.out | |
*.xyz | ||
*.csv | ||
*.txt | ||
*.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from os.path import join as p_join | ||
|
||
import datamol as dm | ||
import numpy as np | ||
import pandas as pd | ||
from tqdm import tqdm | ||
|
||
from openqdc.datasets.base import BaseDataset | ||
from openqdc.utils.constants import MAX_ATOMIC_NUMBER | ||
from openqdc.utils.molecule import get_atomic_number_and_charge | ||
|
||
|
||
def read_mol(mol_path, smiles, subset, targets): | ||
try: | ||
with open(mol_path, "r") as f: | ||
mol_block = f.read() | ||
mol = dm.read_molblock(mol_block, remove_hs=False, fail_if_invalid=True) | ||
|
||
x = get_atomic_number_and_charge(mol) | ||
positions = mol.GetConformer().GetPositions() | ||
|
||
res = dict( | ||
name=np.array([smiles]), | ||
subset=np.array([subset]), | ||
energies=np.array(targets).astype(np.float32)[None, :], | ||
atomic_inputs=np.concatenate((x, positions), axis=-1, dtype=np.float32), | ||
n_atoms=np.array([x.shape[0]], dtype=np.int32), | ||
) | ||
except Exception as e: | ||
print(f"Skipping: {mol_path} due to {e}") | ||
res = None | ||
|
||
return res | ||
|
||
|
||
class DESS(BaseDataset): | ||
__name__ = "dess" | ||
__energy_methods__ = [ | ||
"mp2_cc", | ||
"mp2_qz", | ||
"mp2_tz", | ||
"mp2_cbs", | ||
"ccsd(t)_cc", | ||
"ccsd(t)_cbs", | ||
"ccsd(t)_nn", | ||
"sapt", | ||
] | ||
|
||
energy_target_names = [ | ||
"cc_MP2_all", | ||
"qz_MP2_all", | ||
"tz_MP2_all", | ||
"cbs_MP2_all", | ||
"cc_CCSD(T)_all", | ||
"cbs_CCSD(T)_all", | ||
"nn_CCSD(T)_all", | ||
"sapt_all", | ||
] | ||
# ['qz_MP2_all', 'tz_MP2_all', 'cbs_MP2_all', 'sapt_all', 'nn_CCSD(T)_all'] | ||
|
||
# Energy in hartree, all zeros by default | ||
atomic_energies = np.zeros((MAX_ATOMIC_NUMBER,), dtype=np.float32) | ||
|
||
partitions = ["DES370K", "DES5M"] | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def _read_raw_(self, part): | ||
df = pd.read_csv(p_join(self.root, f"{part}.csv")) | ||
for col in self.energy_target_names: | ||
if col not in df.columns: | ||
df[col] = np.nan | ||
smiles = (df["smiles0"] + "." + df["smiles1"]).tolist() | ||
subsets = (f"{part}_" + df["group_orig"]).tolist() | ||
targets = df[self.energy_target_names].values | ||
paths = ( | ||
p_join(self.root, "geometries/") | ||
+ df["system_id"].astype(str) | ||
+ f"/{part}_" | ||
+ df["geom_id"].astype(str) | ||
+ ".mol" | ||
) | ||
|
||
inputs = [ | ||
dict(smiles=smiles[i], subset=subsets[i], targets=targets[i], mol_path=paths[i]) | ||
for i in tqdm(range(len(smiles))) | ||
] | ||
f = lambda xs: [read_mol(**x) for x in xs] | ||
samples = dm.parallelized_with_batches( | ||
f, inputs, n_jobs=-1, progress=True, batch_size=1024, scheduler="threads" | ||
) | ||
return samples | ||
|
||
def read_raw_entries(self): | ||
samples = sum([self._read_raw_(partition) for partition in self.partitions], []) | ||
return samples | ||
|
||
|
||
if __name__ == "__main__": | ||
for data_class in [DESS]: | ||
data = data_class() | ||
n = len(data) | ||
|
||
for i in np.random.choice(n, 3, replace=False): | ||
x = data[i] | ||
print(x.name, x.subset, end=" ") | ||
for k in x: | ||
if x[k] is not None: | ||
print(k, x[k].shape, end=" ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import json | ||
import tarfile | ||
from glob import glob | ||
from os.path import join as p_join | ||
|
||
import datamol as dm | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from openqdc.datasets.base import BaseDataset | ||
from openqdc.utils.constants import MAX_ATOMIC_NUMBER | ||
|
||
|
||
def flatten_dict(d, sep: str = "."): | ||
return pd.json_normalize(d, sep=sep).to_dict(orient="records")[0] | ||
|
||
|
||
def read_content(f): | ||
try: | ||
r = flatten_dict(json.load(f)) | ||
x = np.concatenate( | ||
( | ||
r["atoms.elements.number"][:, None], | ||
r["atoms.core electrons"][:, None], | ||
r["atoms.coords.3d"].reshape(-1, 3), | ||
), | ||
axis=-1, | ||
).astype(np.float32) | ||
|
||
res = dict( | ||
name=np.array([r["smiles"]]), | ||
subset=np.array([r["formula"]]), | ||
energies=np.array(["properties.energy.total"]).astype(np.float32)[None, :], | ||
atomic_inputs=x, | ||
n_atoms=np.array([x.shape[0]], dtype=np.int32), | ||
) | ||
except Exception: | ||
res = None | ||
|
||
return res | ||
|
||
|
||
def read_archive(path): | ||
with tarfile.open(path) as tar: | ||
res = [read_content(tar.extractfile(member)) for member in tar.getmembers()] | ||
# print(len(res)) | ||
return res | ||
|
||
|
||
class PubchemQC(BaseDataset): | ||
__name__ = "pubchemqc" | ||
__energy_methods__ = [ | ||
"b3lyp", | ||
"pm6", | ||
] | ||
|
||
energy_target_names = [ | ||
"b3lyp", | ||
"pm6", | ||
] | ||
|
||
# Energy in hartree, all zeros by default | ||
atomic_energies = np.zeros((MAX_ATOMIC_NUMBER,), dtype=np.float32) | ||
|
||
partitions = ["b3lyp", "pm6"] | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def _read_raw_(self, part): | ||
arxiv_paths = glob(p_join(self.root, f"{part}", "*.tar.gz")) | ||
print(len(arxiv_paths)) | ||
samples = dm.parallelized(read_archive, arxiv_paths, n_jobs=-1, progress=True, scheduler="threads") | ||
res = sum(samples, []) | ||
print(len(res)) | ||
exit() | ||
return res | ||
|
||
def read_raw_entries(self): | ||
samples = sum([self._read_raw_(partition) for partition in self.partitions], []) | ||
return samples | ||
|
||
|
||
if __name__ == "__main__": | ||
for data_class in [PubchemQC]: | ||
data = data_class() | ||
n = len(data) | ||
|
||
for i in np.random.choice(n, 3, replace=False): | ||
x = data[i] | ||
print(x.name, x.subset, end=" ") | ||
for k in x: | ||
if x[k] is not None: | ||
print(k, x[k].shape, end=" ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.