harirakul / chemlib Goto Github PK
View Code? Open in Web Editor NEW🧪 A comprehensive chemistry library for Python.
Home Page: https://chemlib.readthedocs.io/
License: MIT License
🧪 A comprehensive chemistry library for Python.
Home Page: https://chemlib.readthedocs.io/
License: MIT License
Nice lib! A good starting point for what I'm looking for. Currently I'm looking forward to below functionalities, ranked by extent of expectations from highest to suggestive:
Formula parsing should be able to handle different 'ways' of writing compounds
For example:
-> Checking for consecutive elements in CH3COO (currently need to input as C2H3O2)
Empirical formula from percent composition returning incorrect results sometimes.
P = 43.64
O = 56.36
Should get: P2O5, instead getting P1O3
chemlib==2.2.3
In [15]: r = Reaction.by_formula('KOH + H3PO4 --> K2HPO4 + H2O')
In [16]: r.balance()
In [17]: r.formula
Out[17]: '2K₁O₁H₁ + 1H₃P₁O₄ --> 1K₂H₁P₁O₄ + 2H₂O₁'
In [18]: dict(zip(r.constituents, [c["grams"] for c in r.get_amounts(3, grams=1)]))
Out[18]: {'K₁O₁H₁': 0.673, 'H₃P₁O₄': 0.588, 'K₂H₁P₁O₄': 1, 'H₂O₁': 0.216}
In [19]: dict(zip(r.constituents, [c["grams"] for c in r.get_amounts(3, grams=1000)]))
Out[19]: {'K₁O₁H₁': 644.255, 'H₃P₁O₄': 562.561, 'K₂H₁P₁O₄': 1000, 'H₂O₁': 206.906}
I'd expect KOH to be something like 673.xxx
Using this code for a project that requires equations with up to 4 products to be balanced. Noticed that the balancer only processes a maximum of 2 reactants and 2 products.
`from chemlib import Compound, Reaction
from chemlib import Compound, Reaction
H2 = Compound('H2')
C7H8O2 = Compound("C7H8O2")
C6H6O1 = Compound("C6H6O1")
C1H4 = Compound('C1H4')
H2O1 = Compound('H2O1')
r=Reaction(reactants = [H2,C7H8O2],products = [C6H6O1,C1H4,H2O1])
r.formula
'1H₂ + 1O₂ --> 1H₂O₁'
r.is_balanced
False
r.balance()
print(r.formula)
'2H₂ + 1O₂ --> 2H₂O₁'
r.is_balanced
True`
`---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
in
16 False
17
---> 18 r.balance()
19
20 print(r.formula)
~\Anaconda3\lib\site-packages\chemlib\chemistry.py in balance(self)
256
257 if compounds[sol].formula in self.product_formulas:
--> 258 final_products.append([compounds[sol]]*solutions[sol])
259
260 final_reactants = sum(final_reactants, [])
IndexError: list index out of range`
On a Mac, with PIL installed, I am getting a font error trying to run:
from chemlib import Galvanic_Cell
g = Galvanic_Cell("Pb", "Zn")
Is there a way to default to a font that is available, or hold off on the image generation, rather than erroring on the initial call of Galvanic_Cell()
?
/usr/local/lib/python3.9/site-packages/chemlib/electrochemistry.py in font(size)
62 def draw(self):
63 def font(size):
---> 64 return ImageFont.truetype("arial.ttf", size, encoding="unic")
65
66 img = Image.open(DIAGRAM_PATH)
/usr/local/lib/python3.9/site-packages/PIL/ImageFont.py in truetype(font, size, index, encoding, layout_engine)
850
851 try:
--> 852 return freetype(font)
853 except OSError:
854 if not isPath(font):
/usr/local/lib/python3.9/site-packages/PIL/ImageFont.py in freetype(font)
847
848 def freetype(font):
--> 849 return FreeTypeFont(font, size, index, encoding, layout_engine)
850
851 try:
/usr/local/lib/python3.9/site-packages/PIL/ImageFont.py in __init__(self, font, size, index, encoding, layout_engine)
207 load_from_bytes(f)
208 return
--> 209 self.font = core.getfont(
210 font, size, index, encoding, layout_engine=layout_engine
211 )
OSError: cannot open resource
chemlib==2.2.3
----> 1 Compound('CuSO4').oxidation_numbers()
File /opt/homebrew/lib/python3.10/site-packages/chemlib/chemistry.py:129, in Compound.oxidation_numbers(self)
126 left.remove(sym)
128 if len(left) > 1:
--> 129 raise NotImplementedError
131 ox_nums[left[0]] = int(-current()/self.occurences[left[0]])
132 return ox_nums
Is there a simple way to provide lists of compounds, eg in a reaction, in SMILES format?
Also, is it possible to suppress the 1
digit in the compound display (or is the digit used to help parse the separate elements out in a compound string?)
While using the limiting reagent function,
terminal cites line 337 : data = [a[-1][mode.capitalize()] for a in eq_amounts]
and shows : KeyError: 'Grams'
This is code where it is used:
if eventlr == 'Apply':
x=int(float(valueslr['R1']))
y=int(float(valueslr['R2']))
lr = rxn.limiting_reagent(x, y, mode = type)
winlr('lr').Update(value=lr.formula)
It is a program made using pysimplegui and chemlib
type is a string variable (grams,moles or molecules)
and x and y are integers
Please Help
I think it would be better if the standard salts in the galvanic elements were nitrates instead of sulphates to avoid ending up with insoluble salts when drawing the elements.
Also, the method "diagram" does not longer work (although "draw" works), but it is still used in the documentation.
The galvanic cell drawer assumes there exists a font called "arial.ttf". This is the case on Windows, but not on Linux or Mac, leading to an OSError when attempting to draw such a diagram.
There could perhaps be different fonts based on platform, or might be possible to use the load_default() method.
Balancing the combustion (both incomplete and complete) of benzene seems to miss one oxygen on the left side:
from chemlib import Compound, Reaction
A = Compound("H6C6")
B = Compound("O2")
C = Compound("CO")
D = Compound("H2O")
r = Reaction([A,B], [C,D])
r.balance()
print(r)
This gives: 1H₆C₆ + 4O₂ --> 6C₁O₁ + 3H₂O₁, lacking one O-atom on the left side. The same happens when switching CO with CO₂.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.