| import csv | |
| import json | |
| import os | |
| import datasets | |
| _CITATION = """ | |
| @inproceedings{gebhard2022inferring, | |
| title={Inferring molecular complexity from mass spectrometry data using machine learning}, | |
| author={Gebhard, Timothy D and Bell, Aaron C and Gong, Jian and Hastings, Jaden J. A. and Fricke, G. Matthew and Cabrol, Nathalie and Sandford, Scott and Phillips, Michael and Warren-Rhodes, Kimberley and Baydin, Atilim Gunes}, | |
| booktitle={NeurIPS Workshop on Machine Learning and the Physical Sciences}, | |
| year={2022} | |
| } | |
| """ | |
| _DESCRIPTION = """ | |
| SaganMC is a molecular dataset designed to support machine learning research in molecular complexity inference. It includes over 400,000 molecules with computed structural, physico-chemical, and complexity descriptors, and a subset of ~16k molecules that additionally include experimental mass spectra. | |
| """ | |
| _HOMEPAGE = "https://huggingface.co/datasets/oxai4science/sagan-mc" | |
| _LICENSE = "CC-BY-4.0" | |
| _URLS = { | |
| "sagan-mc-400k": "https://huggingface.co/datasets/oxai4science/sagan-mc/resolve/main/sagan-mc-400k.csv", | |
| "sagan-mc-spectra-16k": "https://huggingface.co/datasets/oxai4science/sagan-mc/resolve/main/sagan-mc-spectra-16k.csv", | |
| } | |
| class SaganMC(datasets.GeneratorBasedBuilder): | |
| VERSION = datasets.Version("1.0.0") | |
| BUILDER_CONFIGS = [ | |
| datasets.BuilderConfig(name="sagan-mc-400k", version=VERSION, description="Full dataset with ~400k molecules"), | |
| datasets.BuilderConfig(name="sagan-mc-spectra-16k", version=VERSION, description="Subset with mass spectra (~16k molecules)"), | |
| ] | |
| DEFAULT_CONFIG_NAME = "sagan-mc-400k" | |
| def _info(self): | |
| features = datasets.Features({ | |
| "inchi": datasets.Value("string"), | |
| "inchikey": datasets.Value("string"), | |
| "selfies": datasets.Value("string"), | |
| "smiles": datasets.Value("string"), | |
| "smiles_scaffold": datasets.Value("string"), | |
| "formula": datasets.Value("string"), | |
| "fingerprint_morgan": datasets.Value("string"), | |
| "num_atoms": datasets.Value("int32"), | |
| "num_atoms_all": datasets.Value("int32"), | |
| "num_bonds": datasets.Value("int32"), | |
| "num_bonds_all": datasets.Value("int32"), | |
| "num_rings": datasets.Value("int32"), | |
| "num_aromatic_rings": datasets.Value("int32"), | |
| "physchem_mol_weight": datasets.Value("float"), | |
| "physchem_logp": datasets.Value("float"), | |
| "physchem_tpsa": datasets.Value("float"), | |
| "physchem_qed": datasets.Value("float"), | |
| "physchem_h_acceptors": datasets.Value("int32"), | |
| "physchem_h_donors": datasets.Value("int32"), | |
| "physchem_rotatable_bonds": datasets.Value("int32"), | |
| "physchem_fraction_csp3": datasets.Value("float"), | |
| "mass_spectrum_nist": datasets.Value("string"), | |
| "complex_ma_score": datasets.Value("int32"), | |
| "complex_ma_runtime": datasets.Value("float"), | |
| "complex_bertz_score": datasets.Value("float"), | |
| "complex_bertz_runtime": datasets.Value("float"), | |
| "complex_boettcher_score": datasets.Value("float"), | |
| "complex_boettcher_runtime": datasets.Value("float"), | |
| "synth_sa_score": datasets.Value("float"), | |
| "meta_cas_number": datasets.Value("string"), | |
| "meta_names": datasets.Value("string"), | |
| "meta_iupac_name": datasets.Value("string"), | |
| "meta_comment": datasets.Value("string"), | |
| "meta_origin": datasets.Value("string"), | |
| "meta_reference": datasets.Value("string"), | |
| "split": datasets.ClassLabel(names=["train", "val", "test"]) | |
| }) | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=features, | |
| homepage=_HOMEPAGE, | |
| license=_LICENSE, | |
| citation=_CITATION, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| url = _URLS[self.config.name] | |
| data_path = dl_manager.download_and_extract(url) | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TRAIN, | |
| gen_kwargs={"filepath": data_path, "split_name": "train"}, | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.VALIDATION, | |
| gen_kwargs={"filepath": data_path, "split_name": "val"}, | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TEST, | |
| gen_kwargs={"filepath": data_path, "split_name": "test"}, | |
| ), | |
| ] | |
| def _generate_examples(self, filepath, split_name): | |
| numeric_fields = [ | |
| "num_atoms", "num_atoms_all", "num_bonds", "num_bonds_all", "num_rings", "num_aromatic_rings", | |
| "physchem_mol_weight", "physchem_logp", "physchem_tpsa", "physchem_qed", | |
| "physchem_h_acceptors", "physchem_h_donors", "physchem_rotatable_bonds", "physchem_fraction_csp3", | |
| "complex_ma_score", "complex_ma_runtime", "complex_bertz_score", "complex_bertz_runtime", | |
| "complex_boettcher_score", "complex_boettcher_runtime", "synth_sa_score" | |
| ] | |
| with open(filepath, encoding="utf-8") as f: | |
| reader = csv.DictReader(f) | |
| for idx, row in enumerate(reader): | |
| if row["split"] == split_name: | |
| for field in numeric_fields: | |
| if field in row and row[field] == "": | |
| row[field] = None | |
| yield idx, row | |