From 30c2f4d297b8f1e373183fd6cda9699a7e46626b Mon Sep 17 00:00:00 2001 From: Haoyan Huo Date: Thu, 25 Jul 2019 13:26:45 -0700 Subject: [PATCH] add test for reaction completer --- .gitignore | 3 + reaction_completer/__init__.py | 2 +- reaction_completer/completer.py | 91 ------------------- reaction_completer/test/test_completer.py | 106 ++++++++++++++++++++++ 4 files changed, 110 insertions(+), 92 deletions(-) create mode 100644 reaction_completer/test/test_completer.py diff --git a/.gitignore b/.gitignore index 894a44c..6a82e9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.DS_Store +.idea/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/reaction_completer/__init__.py b/reaction_completer/__init__.py index 23f26b0..3ef3ea2 100644 --- a/reaction_completer/__init__.py +++ b/reaction_completer/__init__.py @@ -1,3 +1,3 @@ -from reaction_completer.completer import ReactionCompleter +from reaction_completer.completer import ReactionCompleter, balance_recipe from reaction_completer.errors import * from reaction_completer.material import MaterialInformation diff --git a/reaction_completer/completer.py b/reaction_completer/completer.py index 2c8ef0e..b24b1fd 100644 --- a/reaction_completer/completer.py +++ b/reaction_completer/completer.py @@ -530,94 +530,3 @@ def material_dict_to_object(material_dict, sub_dict=None): target_object.material_formula, [x.material_formula for x in precursor_objects], e) return solutions - - -def demo(): - logging.basicConfig(level=logging.DEBUG) - precursors = [ - { - "material_formula": "SrCO3", - "material_string": "SrCO3", - "composition": [ - { - "formula": "SrCO3", - "elements": {"Sr": "1.0", "C": "1.0", "O": "3.0"}, - "amount": "1.0" - } - ], - }, - { - "material_formula": "Al2O3", - "material_string": "Al2O3", - "composition": [ - { - "formula": "Al2O3", - "elements": {"Al": "2.0", "O": "3.0"}, - "amount": "1.0" - } - ], - }, - { - "material_formula": "Fe2O3", - "material_string": "Fe2O3", - "composition": [ - { - "formula": "Fe2O3", - "elements": {"Fe": "2.0", "O": "3.0"}, - "amount": "1.0" - } - ], - }, - { - "material_formula": "ZrO2", - "material_string": "ZrO2", - "composition": [ - { - "formula": "ZrO2", - "elements": {"Zr": "1.0", "O": "2.0"}, - "amount": "1.0" - } - ] - }, - { - "material_formula": "H2O", - "material_string": "H2O", - "composition": [ - { - "formula": "H2O", - "elements": {"O": "1.0", "H": "2.0"}, - "amount": "1.0" - } - ] - }, - ] - targets = [ - { - "material_formula": "Sr6(A2O4)6", - "material_string": "Sr6(A2O4)6", - "composition": [ - { - "formula": "Sr6(Fe2O4)6", - "elements": {"A": "12.0", "O": "24.0", "Sr": "6.0"}, - "amount": "1.0" - } - ], - "elements_vars": { - "A": ["Fe", "Al"] - }, - }, - ] - text = [ - "SrCO3, Al2O3 and Fe2O3 are used to synthesize Sr6(A2O4)6, A=Fe, Al.", - "Milling media is ZrO2", - "There is some H2O found in the final product." - ] - - reactions = balance_recipe(precursors, targets, text) - print('Found', len(reactions), 'reactions') - for reaction in reactions: - print(reaction) - - -if __name__ == '__main__': - demo() diff --git a/reaction_completer/test/test_completer.py b/reaction_completer/test/test_completer.py new file mode 100644 index 0000000..e337642 --- /dev/null +++ b/reaction_completer/test/test_completer.py @@ -0,0 +1,106 @@ +from unittest import TestCase + +from reaction_completer import balance_recipe + + +class TestElementSubstitution(TestCase): + def test_simple(self): + """ + SrCO3 + Al2O3 + Fe2O3 ==== Sr6(A2O4)6, A=Al, Fe + """ + precursors = [ + { + "material_formula": "SrCO3", + "material_string": "SrCO3", + "composition": [ + { + "formula": "SrCO3", + "elements": {"Sr": "1.0", "C": "1.0", "O": "3.0"}, + "amount": "1.0" + } + ], + }, + { + "material_formula": "Al2O3", + "material_string": "Al2O3", + "composition": [ + { + "formula": "Al2O3", + "elements": {"Al": "2.0", "O": "3.0"}, + "amount": "1.0" + } + ], + }, + { + "material_formula": "Fe2O3", + "material_string": "Fe2O3", + "composition": [ + { + "formula": "Fe2O3", + "elements": {"Fe": "2.0", "O": "3.0"}, + "amount": "1.0" + } + ], + }, + { + "material_formula": "ZrO2", + "material_string": "ZrO2", + "composition": [ + { + "formula": "ZrO2", + "elements": {"Zr": "1.0", "O": "2.0"}, + "amount": "1.0" + } + ] + }, + { + "material_formula": "H2O", + "material_string": "H2O", + "composition": [ + { + "formula": "H2O", + "elements": {"O": "1.0", "H": "2.0"}, + "amount": "1.0" + } + ] + }, + ] + targets = [ + { + "material_formula": "Sr6(A2O4)6", + "material_string": "Sr6(A2O4)6", + "composition": [ + { + "formula": "Sr6(Fe2O4)6", + "elements": {"A": "12.0", "O": "24.0", "Sr": "6.0"}, + "amount": "1.0" + } + ], + "elements_vars": { + "A": ["Fe", "Al"] + }, + }, + ] + text = [ + "SrCO3, Al2O3 and Fe2O3 are used to synthesize Sr6(A2O4)6, A=Fe, Al.", + "Milling media is ZrO2", + "There is some H2O found in the final product." + ] + + reactions = balance_recipe(precursors, targets, text) + self.assertListEqual(reactions, [ + ( + 'Sr6(A2O4)6', { + 'left': {'SrCO3': '6', 'Fe2O3': '6'}, + 'right': {'Sr6(A2O4)6': '1', 'CO2': '6'} + }, + {'A': 'Fe'} + ), + ( + 'Sr6(A2O4)6', { + 'left': {'SrCO3': '6', 'Al2O3': '6'}, + 'right': {'Sr6(A2O4)6': '1', 'CO2': '6'} + }, + {'A': 'Al'} + ) + ])