Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ret = {"meta": get_metadata_template(), "data": []}
query, errors = format_query(id=id, molecule_hash=molecule_hash, molecular_formula=molecular_formula)
# Don't include the hash or the molecular_formula in the returned result
# Make the query
data = MoleculeORM.objects(**query).exclude("molecule_hash", "molecular_formula")\
.limit(self.get_limit(limit))\
.skip(skip)\
ret["meta"]["success"] = True
ret["meta"]["n_found"] = data.count() # all data count, can be > len(data)
ret["meta"]["errors"].extend(errors)
# Data validated going in
data = [Molecule(**d.to_json_obj(), validate=False) for d in data]
ret["data"] = data
return ret
-------
bool
Whether the operation was successful.
"""
meta = add_metadata_template()
results = []
with self.session_scope() as session:
# Build out the ORMs
orm_molecules = []
for dmol in molecules:
if dmol.validated is False:
dmol = Molecule(**dmol.dict(), validate=True)
mol_dict = dmol.dict(exclude={"id", "validated"})
# TODO: can set them as defaults in the sql_models, not here
mol_dict["fix_com"] = True
mol_dict["fix_orientation"] = True
# Build fresh indices
mol_dict["molecule_hash"] = dmol.get_hash()
mol_dict["molecular_formula"] = dmol.get_molecular_formula()
mol_dict["identifiers"] = {}
mol_dict["identifiers"]["molecule_hash"] = mol_dict["molecule_hash"]
mol_dict["identifiers"]["molecular_formula"] = mol_dict["molecular_formula"]
# search by index keywords not by all keys, much faster
return df["molecule"].apply(lambda blob: Molecule(**blob, validate=False))
raise KeyError("Dataset: Parse stoichiometry: passed in as a list, must be of key : value type")
# Get the values
try:
mol_values.append(float(line[1]))
except:
raise TypeError("Dataset: Parse stoichiometry: must be able to cast second value to a float.")
# What kind of molecule is it?
mol = line[0]
if isinstance(mol, str) and (len(mol) == 40):
molecule_hash = mol
elif isinstance(mol, str):
qcf_mol = Molecule.from_data(mol)
molecule_hash = qcf_mol.get_hash()
if molecule_hash not in list(self._new_molecules):
self._new_molecules[molecule_hash] = qcf_mol
elif isinstance(mol, Molecule):
molecule_hash = mol.get_hash()
if molecule_hash not in list(self._new_molecules):
self._new_molecules[molecule_hash] = mol
else:
raise TypeError(
"Dataset: Parse stoichiometry: first value must either be a molecule hash, "
"a molecule str, or a Molecule class."
def get_molecules(self, id=None, molecule_hash=None, molecular_formula=None, limit: int = None, skip: int = 0):
meta = get_metadata_template()
query = format_query(MoleculeORM, id=id, molecule_hash=molecule_hash, molecular_formula=molecular_formula)
# Don't include the hash or the molecular_formula in the returned result
rdata, meta["n_found"] = self.get_query_projection(
MoleculeORM, query, limit=limit, skip=skip, exclude=["molecule_hash", "molecular_formula"]
)
meta["success"] = True
# ret["meta"]["errors"].extend(errors)
data = [Molecule(**d, validate=False, validated=True) for d in rdata]
return {"meta": meta, "data": data}
Get or add the given molecules (if they don't exit).
MoleculeORMs are given in a mixed format, either as a dict of mol data
or as existing mol id.
TODO: to be split into get by_id and get_by_data
"""
meta = get_metadata_template()
ordered_mol_dict = {indx: mol for indx, mol in enumerate(data)}
new_molecules = {}
id_mols = {}
for idx, mol in ordered_mol_dict.items():
if isinstance(mol, str):
id_mols[idx] = mol
elif isinstance(mol, Molecule):
new_molecules[idx] = mol
else:
meta["errors"].append((idx, "Data type not understood"))
ret_mols = {}
# Add all new molecules
flat_mols = []
flat_mol_keys = []
for k, v in new_molecules.items():
flat_mol_keys.append(k)
flat_mols.append(v)
flat_mols = self.add_molecules(flat_mols)["data"]
id_mols.update({k: v for k, v in zip(flat_mol_keys, flat_mols)})
# Get molecules by index and translate back to dict
"""
)
# bind and expand ids list
sql_statement = sql_statement.bindparams(bindparam("optimization_ids", expanding=True))
# column types:
columns = inspect(MoleculeORM).columns
sql_statement = sql_statement.columns(opt_id=Integer, *columns)
query_result = self.execute_query(sql_statement, optimization_ids=list(optimization_ids))
ret = {}
for rec in query_result:
self._remove_excluded_keys(rec)
key = rec.pop("opt_id")
ret[key] = Molecule(**rec)
return ret
def get_molecule(name, orient=True):
"""
Returns a Molecule object from the available stored objects.
"""
fname = get_file_name("molecules", name)
if not fname:
raise OSError("File: {}/{} not found".format("molecules", name))
return Molecule.from_file(fname, orient=orient)