From af3b449d95e777289d852335675eff9c9a65038c Mon Sep 17 00:00:00 2001 From: faneshala Date: Wed, 29 May 2024 21:12:52 +0200 Subject: [PATCH] almost lol --- src/chembalancer/chembalancer.py | 18 +++++++------ src/chembalancer/help.py | 0 tox.ini | 43 -------------------------------- 3 files changed, 11 insertions(+), 50 deletions(-) create mode 100644 src/chembalancer/help.py delete mode 100644 tox.ini diff --git a/src/chembalancer/chembalancer.py b/src/chembalancer/chembalancer.py index e8b5270..34db9bb 100644 --- a/src/chembalancer/chembalancer.py +++ b/src/chembalancer/chembalancer.py @@ -17,6 +17,7 @@ from rdkit import Chem import requests import base64 +import math def get_smiles_from_name(name): @@ -93,6 +94,7 @@ def get_molecular_formula(smiles): return "Invalid SMILES string" + def balance_chemical_equation(reactant_smiles, product_smiles): reactant_counts = [count_atoms(smiles) for smiles in reactant_smiles] product_counts = [count_atoms(smiles) for smiles in product_smiles] @@ -110,9 +112,16 @@ def balance_chemical_equation(reactant_smiles, product_smiles): error_message += f"Elements {missing_in_reactants} are in products but not in reactants." raise ValueError(error_message) + # Use the union of elements from reactants and products elements = sorted(reactant_elements.union(product_elements)) + A_reactants = setup_matrix(elements, reactant_counts) A_products = setup_matrix(elements, product_counts) + + # Debugging: Ensure matrices have the same number of rows + if A_reactants.shape[0] != A_products.shape[0]: + raise ValueError("Internal error: Reactants and products matrices do not match in row dimension.") + A = np.concatenate([A_reactants, -A_products], axis=1) integer_coefficients = solve_ilp(A) @@ -127,20 +136,15 @@ def balance_chemical_equation(reactant_smiles, product_smiles): return reactant_data, product_data - - def setup_matrix(elements, counts): - # Assuming counts is a list of dictionaries where each dictionary is the atomic count for a molecule + # Setup the matrix based on the element order and counts provided matrix = [] for count in counts: row = [count.get(element, 0) for element in elements] matrix.append(row) - - # Ensure matrix is 2D matrix = np.array(matrix) if matrix.ndim == 1: - matrix = matrix.reshape(1, -1) # Reshape to 2D if it's inadvertently 1D - + matrix = matrix.reshape(1, -1) return matrix diff --git a/src/chembalancer/help.py b/src/chembalancer/help.py new file mode 100644 index 0000000..e69de29 diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 810e901..0000000 --- a/tox.ini +++ /dev/null @@ -1,43 +0,0 @@ -[tox] -env_list = - check - docs - {py3}{,-coverage} - -[gh-actions] -python = - 3.10: check, py310 - 3.11: py311 - 3.12: py312 - -[testenv] -basepython = - pypy3: pypy3 - py38: python3.8 - py39: python3.9 - py310: python3.10 - py311: python3.11 - py312: python3.12 - {check,docs}: python3 -setenv = - PYTHONUNBUFFERED = yes - PYTEST_EXTRA_ARGS = -s - coverage: PYTEST_EXTRA_ARGS = --cov=src/chembalancer --cov-report xml:.tox/coverage.xml --cov-report term -passenv = - * -extras = - test -commands = - pytest {env:PYTEST_MARKERS:} {env:PYTEST_EXTRA_ARGS:} {posargs:-vv} - coverage: genbadge coverage -i .tox/coverage.xml -o assets/coverage-badge.svg -usedevelop = true - -[testenv:docs] -description = build HTML docs -setenv = - READTHEDOCS_PROJECT = chembalancer - READTHEDOCS_VERSION = latest -extras = - doc -commands = - sphinx-build -d "{toxworkdir}/docs_doctree" docs/source "docs/docs_out" --color -vW -bhtml