Skip to content

Instantly share code, notes, and snippets.

@darcyabjones
Created January 11, 2024 15:25
Show Gist options
  • Save darcyabjones/0708c80aa2386c69c03d4d8063af129d to your computer and use it in GitHub Desktop.
Save darcyabjones/0708c80aa2386c69c03d4d8063af129d to your computer and use it in GitHub Desktop.
A handy notebook to perform molarity conversions using Sympy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "a5770e7a-9548-411a-8c0a-4fdc5f4564a1",
"metadata": {},
"outputs": [],
"source": [
"from sympy.physics.units import mol, g, L, milli, micro, mg, ug, mL, Quantity, convert_to\n",
"from sympy import Symbol\n",
"from sympy.physics.units.systems import SI\n",
"from sympy.core.numbers import Rational\n",
"from sympy.physics.units import Dimension\n",
"import sympy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "41315a46-05f3-4862-80ef-c8c0551a7f73",
"metadata": {},
"outputs": [],
"source": [
"ul = uL = microliter = microliters = Quantity(\"microliter\", abbrev=\"uL\", latex_repr=r\"\\mu\\text{L}\")\n",
"ul.set_global_relative_scale_factor(micro, L)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0c36ead6-11a0-4428-9173-4b4851365995",
"metadata": {},
"outputs": [],
"source": [
"mmol = millimol = mmole = millimole = Quantity(\"millimole\", abbrev=\"mmol\")\n",
"mmol.set_global_relative_scale_factor(milli, L)\n",
"\n",
"umol = umole = micromol = micromole = Quantity(\"micromole\", abbrev=\"umol\", latex_repr=r\"\\mu\\text{mol}\")\n",
"umol.set_global_relative_scale_factor(micro, mol)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0e5f5df1-e8d0-41ab-9457-9f12459bf5ef",
"metadata": {},
"outputs": [],
"source": [
"M = molar = Quantity(\"molar\", abbrev=\"M\")\n",
"molar.set_global_dimension(mol.dimension / L.dimension)\n",
"SI.set_quantity_dimension(molar, mol.dimension / L.dimension)\n",
"SI.set_quantity_scale_factor(molar, mol / L)\n",
"\n",
"mM = millimolar = Quantity(\"millimolar\", abbrev=\"mM\")\n",
"millimolar.set_global_relative_scale_factor(milli, molar)\n",
"\n",
"uM = micromolar = Quantity(\"millimolar\", abbrev=\"uM\", latex_repr=r\"\\mu\\text{M}\")\n",
"micromolar.set_global_relative_scale_factor(micro, molar)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "941ae744-5921-4e25-9131-1c5e902f44fd",
"metadata": {},
"outputs": [],
"source": [
"def molecular_weight(num):\n",
" return num * g / mol"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "12b7ba93-5132-4da9-9821-a8f4ecf60440",
"metadata": {},
"outputs": [],
"source": [
"def molar_to_gram_per_liter(MW, concentration):\n",
" x = MW * concentration\n",
" return convert_to(x, [g, L])\n",
"\n",
"def gram_per_liter_to_molar(MW, concentration):\n",
" x = concentration / MW\n",
" return convert_to(x, [M])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9f171254-3137-4116-8f42-af62b2592112",
"metadata": {},
"outputs": [],
"source": [
"class Compound(object):\n",
"\n",
" def __init__(self, name, MW, DMSO):\n",
" self.name = name\n",
" if isinstance(MW, (float, int)):\n",
" self.MW = molecular_weight(MW)\n",
" else:\n",
" assert isinstance(MW, sympy.Expr)\n",
" self.MW = MW\n",
"\n",
" self.DMSO = DMSO\n",
" return\n",
"\n",
" def __repr__(self):\n",
" return f\"Compound({repr(self.name)}, {repr(self.MW)})\"\n",
"\n",
" def molar_to_gram_per_liter(self, concentration):\n",
" return molar_to_gram_per_liter(self.MW, concentration)\n",
"\n",
" def gram_per_liter_to_molar(self, concentration):\n",
" return gram_per_liter_to_molar(self.MW, concentration)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "9763ce73-19e9-49f9-a832-d2287ecbef2d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Compound('camalexin', 200.26*gram/mole)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"camalexin = Compound(\n",
" \"camalexin\",\n",
" MW = 200.26 * g / mol,\n",
" DMSO = 20 * mg / mL # 40?\n",
")\n",
"\n",
"camalexin"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "31533591-20eb-46a1-adfa-d42749609b70",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 249.675421951463 \\mu\\text{M}$"
],
"text/plain": [
"249.675421951463*millimolar"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(camalexin.gram_per_liter_to_molar(50 * ug / mL), [uM])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "345dc28c-a281-4a5e-9025-c1bf478e7aa4",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{50.065 \\mu\\text{g}}{\\text{milliliter}}$"
],
"text/plain": [
"50.065*microgram/milliliter"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(camalexin.molar_to_gram_per_liter(250 * uM), [ug, mL])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "95abf8bd-d1b3-43d7-b6a5-5769a7984494",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Compound('gossypol', 578.6*gram/mole)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gossypol = Compound(\n",
" \"gossypol\",\n",
" MW = 578.6 * g / mol,\n",
" DMSO = 39.22 * mg / mL\n",
")\n",
"\n",
"gossypol"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "2ee7dd20-b856-4cfb-846f-687dcd606849",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 34.5661942620118 \\mu\\text{M}$"
],
"text/plain": [
"34.5661942620118*millimolar"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(gossypol.gram_per_liter_to_molar(20 * ug / mL), [uM])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e656138d-08a2-4101-9e85-96258f4cae93",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{14.465 \\mu\\text{g}}{\\text{milliliter}}$"
],
"text/plain": [
"14.465*microgram/milliliter"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(gossypol.molar_to_gram_per_liter(25 * uM), [ug, mL])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "2e239494-dab1-44ea-9b4a-48c055c87cb6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Compound('scopoletin', 192.17*gram/mole)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"scopoletin = Compound(\n",
" \"scopoletin\",\n",
" MW = 192.17 * g / mol,\n",
" DMSO = 38 * mg/mL\n",
")\n",
"\n",
"scopoletin"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ef46652e-9770-4c93-8934-a4ecec11d92a",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 0.520372586772129 \\mu\\text{M}$"
],
"text/plain": [
"0.520372586772129*millimolar"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(scopoletin.gram_per_liter_to_molar(0.1 * ug / mL), [uM])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "522fbcf3-7a40-4de7-8bf6-92d9bdbf8c6e",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{19.217 \\mu\\text{g}}{\\text{milliliter}}$"
],
"text/plain": [
"19.217*microgram/milliliter"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(scopoletin.molar_to_gram_per_liter(100 * uM), [ug, mL])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d2d5aadb-fca4-48de-a6eb-c105123dec70",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Compound('resveratrol', 228.24*gram/mole)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"resveratrol = Compound(\n",
" \"resveratrol\",\n",
" MW = 228.24 * g / mol,\n",
" DMSO = 50 * mg/mL\n",
")\n",
"\n",
"resveratrol"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1c6e0297-e4fe-42b8-8035-84c4fe35ff6e",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 438.13529617946 \\mu\\text{M}$"
],
"text/plain": [
"438.13529617946*millimolar"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(resveratrol.gram_per_liter_to_molar(100 * ug / mL), [uM])"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "09d66367-a4fd-4e20-b536-c5a5853642cf",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{13.6944 \\mu\\text{g}}{\\text{milliliter}}$"
],
"text/plain": [
"13.6944*microgram/milliliter"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"convert_to(resveratrol.molar_to_gram_per_liter(60 * uM), [ug, mL])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90613377-62a3-4e0c-954d-c749b2b44d63",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf879e55-a515-4a41-bf65-1cf0309bc954",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment