sustainable-processes / pura

Clean chemical data quickly
MIT License
10 stars 3 forks source link

Make sure serialization works #41

Closed marcosfelt closed 1 year ago

marcosfelt commented 1 year ago

This is a first attempt at getting serialization to work with pydantic

from pura.reaction import Reaction, reaction_from_smiles, ReactionRole
from pura.units import *
from pydantic.tools import parse_obj_as
import json

rxn_smiles = "CC(=O)O.OCC>[H+].[Cl-].OCC>CC(=O)OCC"
rxn = reaction_from_smiles(
    rxn_smiles,
    reaction_time=60 * ureg.minute,
    reaction_yield=50.0,
    desired_product_check=lambda c: True,
)
output = rxn.json(indent=2)
print(output)
rxn == Reaction.parse_obj(json.loads(output))
Click to see json output ```json { "inputs": [ { "compound": { "identifiers": [ { "identifier_type": "SMILES", "value": "CC(=O)O", "details": null }, { "identifier_type": "INCHI", "value": "InChI=1S/C2H4O2/c1-2(3)4/h1H3,(H,3,4)", "details": null } ], "quantity": null }, "role": "REACTANT", "addition_order": null, "addition_time": null, "addition_duration": null, "flow_rate": null, "addition_temperature": null }, { "compound": { "identifiers": [ { "identifier_type": "SMILES", "value": "CCO", "details": null }, { "identifier_type": "INCHI", "value": "InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3", "details": null } ], "quantity": null }, "role": "REACTANT", "addition_order": null, "addition_time": null, "addition_duration": null, "flow_rate": null, "addition_temperature": null }, { "compound": { "identifiers": [ { "identifier_type": "SMILES", "value": "[H+]", "details": null }, { "identifier_type": "INCHI", "value": "InChI=1S/p+1", "details": null } ], "quantity": null }, "role": "AGENT", "addition_order": null, "addition_time": null, "addition_duration": null, "flow_rate": null, "addition_temperature": null }, { "compound": { "identifiers": [ { "identifier_type": "SMILES", "value": "[Cl-]", "details": null }, { "identifier_type": "INCHI", "value": "InChI=1S/ClH/h1H/p-1", "details": null } ], "quantity": null }, "role": "AGENT", "addition_order": null, "addition_time": null, "addition_duration": null, "flow_rate": null, "addition_temperature": null }, { "compound": { "identifiers": [ { "identifier_type": "SMILES", "value": "CCO", "details": null }, { "identifier_type": "INCHI", "value": "InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3", "details": null } ], "quantity": null }, "role": "AGENT", "addition_order": null, "addition_time": null, "addition_duration": null, "flow_rate": null, "addition_temperature": null } ], "conditions": { "temperature": null, "pressure": null, "reflux": false, "ph": null }, "outcomes": [ { "products": [ { "compound": { "identifiers": [ { "identifier_type": "SMILES", "value": "CCOC(C)=O", "details": null }, { "identifier_type": "INCHI", "value": "InChI=1S/C4H8O2/c1-3-6-4(2)5/h3H2,1-2H3", "details": null } ], "quantity": null }, "role": "PRODUCT", "product_yield": 50.0, "selectivity": null, "peak_area": null, "counts": null, "is_desired_product": true, "isolated_color": null, "texture": null } ], "reaction_time": "60 minute" } ] } ```