Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chemical formulas option for system names, cleaner tables. #179

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
=======
History
=======
2024.11.29 -- Chemical formulas option for system names, cleaner tables.
* Added chemical formula to possible names for systems and configurations.
* Truncated results put in tables, based on units, to make more readable.

2024.11.3 -- Bugfix: removed debug printing in the flowchart
* Removed debug printing that was accidentally left in the flowchart code.

Expand Down
67 changes: 33 additions & 34 deletions seamm/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def scale(data, factor):
return result


def_fmt = {
"kJ/mol": 3,
"kcal/mol": 3,
"kJ/mol/Å": 3,
"kcal/mol/Å": 3,
"eV": 3,
"E_h": 6,
"E_h/a0": 6,
"E_h/Å": 6,
"debye": 3,
"Å": 3,
}


class Node(collections.abc.Hashable):
"""The base class for nodes (steps) in flowcharts.

Expand Down Expand Up @@ -590,34 +604,7 @@ def get_system_configuration(
)

# Attend to naming
if "system name" in P:
if P["system name"] == "keep current name":
pass
elif P["system name"] == "use SMILES string":
system.name = configuration.smiles
elif P["system name"] == "use Canonical SMILES string":
system.name = configuration.canonical_smiles
else:
# Presume it is a string, perhaps with variables.
if len(kwargs) == 0:
system.name = str(P["system name"])
else:
system.name = str(P["system name"]).format(**kwargs)
if "configuration name" in P:
if P["configuration name"] == "keep current name":
pass
elif P["configuration name"] == "use SMILES string":
configuration.name = configuration.smiles
elif P["configuration name"] == "use Canonical SMILES string":
configuration.name = configuration.canonical_smiles
else:
# Presume it is a string, perhaps with variables.
if len(kwargs) == 0:
configuration.name = str(P["configuration name"])
else:
configuration.name = str(P["configuration name"]).format(
**kwargs
)
seamm.standard_parameters.set_names(system, configuration, P, **kwargs)

return (system, configuration)

Expand Down Expand Up @@ -1074,12 +1061,17 @@ def store_results(
results = self.parameters["results"].value

json_data = {}
metadata = self.metadata["results"]
for key, value in results.items():
if key not in data or data[key] is None:
if key not in metadata:
continue

# The metadata describing this result
result_metadata = self.metadata["results"][key]
result_metadata = metadata[key]
if "standard name" in result_metadata:
key = result_metadata["standard name"]

if key not in data or data[key] is None:
continue

# Store the value in the database as a property.
if "property" in value and value["property"]:
Expand Down Expand Up @@ -1164,7 +1156,7 @@ def store_results(

# Store in a table
if "table" in value:
tablename = value["table"]
tablename = self.get_value(value["table"])
column = self.get_value(value["column"])
# Does the table exist?
if not self.variable_exists(tablename):
Expand Down Expand Up @@ -1293,7 +1285,12 @@ def store_results(
if units != current_units:
if result_metadata["dimensionality"] == "scalar":
tmp = Q_(data[key], current_units)
table.at[row_index, column] = tmp.m_as(units)
if units in def_fmt:
table.at[row_index, column] = round(
tmp.m_as(units), def_fmt[units]
)
else:
table.at[row_index, column] = tmp.m_as(units)
else:
factor = Q_(1, current_units).m_as(units)
tmp = scale(data[key], factor)
Expand All @@ -1302,7 +1299,9 @@ def store_results(
)
else:
if result_metadata["dimensionality"] == "scalar":
table.at[row_index, column] = data[key]
table.at[row_index, column] = round(
data[key], def_fmt[units]
)
else:
table.at[row_index, column] = json.dumps(
data[key], separators=(",", ":")
Expand Down
12 changes: 10 additions & 2 deletions seamm/standard_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
"default_units": "",
"enumeration": (
"keep current name",
"title",
"use SMILES string",
"use Canonical SMILES string",
"use IUPAC name",
"use InChI",
"use InChIKey",
"use chemical formula",
),
"format_string": "s",
"description": "System name:",
Expand All @@ -72,13 +72,13 @@
"default_units": "",
"enumeration": (
"keep current name",
"title",
"use SMILES string",
"use Canonical SMILES string",
"use IUPAC name",
"use InChI",
"use InChIKey",
"sequential",
"use chemical formula",
),
"format_string": "s",
"description": "Configuration name:",
Expand Down Expand Up @@ -129,6 +129,8 @@ def structure_handling_description(P, **kwargs):
text += " The name of the system will be its InChI."
elif sysname == "use InChIKey":
text += " The name of the system will be its InChIKey."
elif sysname == "use chemical formula":
text += " The name of the system will be its chemical formula."
else:
tmp = safe_format(sysname, **kwargs)
text += f" The name of the system will be '{tmp}'."
Expand All @@ -146,6 +148,8 @@ def structure_handling_description(P, **kwargs):
text += " The name of the configuration will be its InChI."
elif confname == "use InChIKey":
text += " The name of the configuration will be its InChIKey."
elif confname == "use chemical formula":
text += " The name of the configuration will be its chemical formula."
else:
tmp = safe_format(confname, **kwargs)
text += f" The name of the configuration will be '{tmp}'."
Expand Down Expand Up @@ -269,6 +273,8 @@ def set_names(system, configuration, P, _first=True, **kwargs):
system.name = configuration.inchi
elif sysname == "use InChIKey":
system.name = configuration.inchikey
elif sysname == "use chemical formula":
system.name = configuration.formula[0]
else:
system.name = safe_format(sysname, **kwargs)

Expand All @@ -287,6 +293,8 @@ def set_names(system, configuration, P, _first=True, **kwargs):
configuration.name = configuration.inchi
elif confname == "use InChIKey":
configuration.name = configuration.inchikey
elif confname == "use chemical formula":
configuration.name = configuration.formula[0]
else:
configuration.name = safe_format(confname, **kwargs)

Expand Down
10 changes: 8 additions & 2 deletions seamm/tk_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,10 @@ def setup_results(self):
table.cell(row, 6, w)
widgets.append(w)
e = ttk.Entry(frame, width=15)
e.insert(0, key.replace(" ", "_"))
if "standard name" in entry:
e.insert(0, entry["standard name"].replace(" ", "_"))
else:
e.insert(0, key.replace(" ", "_"))
table.cell(row, 7, e)
widgets.append(e)

Expand All @@ -1151,7 +1154,10 @@ def setup_results(self):
w.bind("<Return>", self._table_cb)
w.bind("<FocusOut>", self._table_cb)
e = ttk.Entry(frame, width=15)
e.insert(0, key.replace("_", " "))
if "standard name" in entry:
e.insert(0, entry["standard name"].replace("_", " "))
else:
e.insert(0, key.replace("_", " "))
table.cell(row, 10, e)
widgets.append(e)

Expand Down
Loading