Skip to content

Commit

Permalink
add isa.ipynb (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
ualiangzhang authored Mar 21, 2024
1 parent ff46a0c commit 06cddfc
Showing 1 changed file with 348 additions and 0 deletions.
348 changes: 348 additions & 0 deletions end-to-end-rest/notebooks/isa.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "9b699411",
"metadata": {
"ExecuteTime": {
"end_time": "2024-03-21T17:38:21.002985Z",
"start_time": "2024-03-21T17:38:20.927113Z"
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from IPython.display import Image, display, HTML\n",
"import requests\n",
"import json\n",
"import os\n",
"import base64\n",
"\n",
"pp = lambda x: print(json.dumps(x, indent=2))\n",
"\n",
"SKEMA_ADDRESS = os.environ.get(\"SKEMA_ADDRESS\", \"https://api.askem.lum.ai/isa/align-eqns\")"
]
},
{
"cell_type": "markdown",
"id": "391aea16",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Aligning two equations in presentation MathML"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "03b6e8f4",
"metadata": {},
"outputs": [],
"source": [
"# Define two mathematical equations in MathML format\n",
"halfar_dome_eqn1 = \"\"\"<math>\n",
" <mfrac>\n",
" <mrow>\n",
" <mi>&#x2202;</mi>\n",
" <mi>H</mi>\n",
" </mrow>\n",
" <mrow>\n",
" <mi>&#x2202;</mi>\n",
" <mi>t</mi>\n",
" </mrow>\n",
" </mfrac>\n",
" <mo>=</mo>\n",
" <mi>&#x2207;</mi>\n",
" <mo>&#x22C5;</mo>\n",
" <mo>(</mo>\n",
" <mi>&#x0393;</mi>\n",
" <msup>\n",
" <mi>H</mi>\n",
" <mrow>\n",
" <mi>n</mi>\n",
" <mo>+</mo>\n",
" <mn>2</mn>\n",
" </mrow>\n",
" </msup>\n",
" <mo>|</mo>\n",
" <mi>&#x2207;</mi>\n",
" <mi>H</mi>\n",
" <msup>\n",
" <mo>|</mo>\n",
" <mrow>\n",
" <mi>n</mi>\n",
" <mo>&#x2212;</mo>\n",
" <mn>1</mn>\n",
" </mrow>\n",
" </msup>\n",
" <mi>&#x2207;</mi>\n",
" <mi>H</mi>\n",
" <mo>)</mo>\n",
" </math>\n",
" \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4fe878ff",
"metadata": {},
"outputs": [],
"source": [
"# Equation 2: Remove <mi>&#x0393;</mi> (Γ) from the last equation\n",
"halfar_dome_eqn2 = \"\"\"<math>\n",
" <mfrac>\n",
" <mrow>\n",
" <mi>&#x2202;</mi>\n",
" <mi>H</mi>\n",
" </mrow>\n",
" <mrow>\n",
" <mi>&#x2202;</mi>\n",
" <mi>t</mi>\n",
" </mrow>\n",
" </mfrac>\n",
" <mo>=</mo>\n",
" <mi>&#x2207;</mi>\n",
" <mo>&#x22C5;</mo>\n",
" <mo>(</mo>\n",
" <msup>\n",
" <mi>H</mi>\n",
" <mrow>\n",
" <mi>n</mi>\n",
" <mo>+</mo>\n",
" <mn>2</mn>\n",
" </mrow>\n",
" </msup>\n",
" <mo>|</mo>\n",
" <mi>&#x2207;</mi>\n",
" <mi>H</mi>\n",
" <msup>\n",
" <mo>|</mo>\n",
" <mrow>\n",
" <mi>n</mi>\n",
" <mo>&#x2212;</mo>\n",
" <mn>1</mn>\n",
" </mrow>\n",
" </msup>\n",
" <mi>&#x2207;</mi>\n",
" <mi>H</mi>\n",
" <mo>)</mo>\n",
" </math>\n",
" \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "47d3cd3e",
"metadata": {},
"outputs": [],
"source": [
"# Optional: Mention file directories for each equation\n",
"mention_json1_content = \"\" # Mention file directory for Eqn 1 (optional)\n",
"mention_json2_content = \"\" # Mention file directory for Eqn 2 (optional)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9052e4a6",
"metadata": {},
"outputs": [],
"source": [
"# Prepare data for the POST request\n",
"data = {\n",
" \"mml1\": halfar_dome_eqn1,\n",
" \"mml2\": halfar_dome_eqn2,\n",
" \"mention_json1\": mention_json1_content,\n",
" \"mention_json2\": mention_json2_content,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "567e8b16",
"metadata": {},
"outputs": [],
"source": [
"# Make a POST request to the Skema service\n",
"r = requests.post(SKEMA_ADDRESS, params=data)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2c1f5d04",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.94\n",
"digraph G {\n",
"0 [color=blue, label=\"Div(Γ*(H^(n+2))*(Abs(Grad(H))^(n-1))*Grad(H)) <<|>> Div((H^(n+2))*(Abs(Grad(H))^(n-1))*Grad(H))\"];\n",
"1 [color=blue, label=\"PD(1, t)(H)\"];\n",
"2 [color=blue, label=\"Γ*(H^(n+2))*(Abs(Grad(H))^(n-1))*Grad(H) <<|>> (H^(n+2))*(Abs(Grad(H))^(n-1))*Grad(H)\"];\n",
"3 [color=red, label=\"Γ\"];\n",
"4 [color=blue, label=\"H^(n+2)\"];\n",
"5 [color=blue, label=\"H\"];\n",
"6 [color=blue, label=\"n+2\"];\n",
"7 [color=blue, label=\"n\"];\n",
"8 [color=blue, label=\"2\"];\n",
"9 [color=blue, label=\"Abs(Grad(H))^(n-1)\"];\n",
"10 [color=blue, label=\"Abs(Grad(H))\"];\n",
"11 [color=blue, label=\"Grad(H)\"];\n",
"12 [color=blue, label=\"n-1\"];\n",
"13 [color=blue, label=\"1\"];\n",
"1 -> 0 [color=blue, label=\"=\"];\n",
"2 -> 0 [color=blue, label=\"Div\"];\n",
"3 -> 2 [color=red, label=\"*\"];\n",
"4 -> 2 [color=blue, label=\"*\"];\n",
"5 -> 4 [color=blue, label=\"^\"];\n",
"6 -> 4 [color=blue, label=\"^\"];\n",
"7 -> 6 [color=blue, label=\"+\"];\n",
"8 -> 6 [color=blue, label=\"+\"];\n",
"9 -> 2 [color=blue, label=\"*\"];\n",
"10 -> 9 [color=blue, label=\"^\"];\n",
"11 -> 10 [color=blue, label=\"Abs\"];\n",
"5 -> 11 [color=blue, label=\"Grad\"];\n",
"12 -> 9 [color=blue, label=\"^\"];\n",
"7 -> 12 [color=blue, label=\"+\"];\n",
"13 -> 12 [color=blue, label=\"-\"];\n",
"11 -> 2 [color=blue, label=\"*\"];\n",
"}\n",
"\n"
]
}
],
"source": [
"# Print the matching ratio and union graph from the response\n",
"print(json.loads(r.text)[\"matching_ratio\"])\n",
"print(json.loads(r.text)[\"union_graph\"])"
]
},
{
"cell_type": "markdown",
"id": "201b9307",
"metadata": {},
"source": [
"## Aligning code and equation\n",
"We use the code for the SIR model and the equation I in SIR as an example."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "23c07a9c",
"metadata": {},
"outputs": [],
"source": [
"SKEMA_LOCAL_ADDRESS = \"https://api.askem.lum.ai\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d522dfcc",
"metadata": {},
"outputs": [],
"source": [
"single_snippet_payload = {\n",
" \"system\": {\n",
" \"files\": [\"code.py\"],\n",
" \"blobs\": [\n",
" 'def sir(\\n s: float, i: float, r: float, beta: float, gamma: float, n: float\\n) -> Tuple[float, float, float]:\\n \"\"\"The SIR model, one time step.\"\"\"\\n s_n = (-beta * s * i) + s\\n i_n = (beta * s * i - gamma * i) + i\\n r_n = gamma * i + r\\n scale = n / (s_n + i_n + r_n)\\n return s_n * scale, i_n * scale, r_n * scale'\n",
" ],\n",
" },\n",
" \"mml\": \"\"\"<math>\n",
" <mfrac>\n",
" <mrow>\n",
" <mi>d</mi>\n",
" <mi>I</mi>\n",
" </mrow>\n",
" <mrow>\n",
" <mi>d</mi>\n",
" <mi>t</mi>\n",
" </mrow>\n",
" </mfrac>\n",
" <mo>=</mo>\n",
" <mfrac>\n",
" <mrow>\n",
" <mi>&#x03B2;</mi>\n",
" <mi>I</mi>\n",
" <mi>S</mi>\n",
" </mrow>\n",
" <mi>N</mi>\n",
" </mfrac>\n",
" <mo>&#x2212;</mo>\n",
" <mi>&#x03B3;</mi>\n",
" <mi>I</mi>\n",
"</math>\"\"\",\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1f38f35c",
"metadata": {},
"source": [
"The output consists of a JSON response containing aligned equation and code information. The structure of the response includes:\n",
"* Matching Ratio: Indicates the similarity between the equations.\n",
"* Number of Different Edges: Represents the dissimilarity between the equations.\n",
"* Node Labels Lists: Names of variables and terms in the equations.\n",
"* Aligned Indices Lists: Indices indicating alignment between equation components.\n",
"* Union Graph Visualization: Graphical representation of the alignment result.\n",
"* Perfectly Matched Indices: Indices of strictly matched nodes in Graph 1 of equation 1."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "64b58d16",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'3': [0.36, 7.0, ['β*I*S/N-γ*I', 'D(1, t)(I)', 'β*I*S/N', 'β', 'I', 'S', 'N', 'γ*I', 'γ'], ['i*-(beta)*s+s', 'D(1, t)(s)', 'i*-(beta)*s', 'i', '-(beta)', 'beta', 's'], [0.0, 1.0, 2.0, 5.0, 3.0, 6.0, 4.0, -1.0, -1.0], [0.0, 1.0, 2.0, 4.0, 6.0, 3.0, 5.0, -1.0, -1.0], 'digraph G {\\n0 [color=blue, label=\"β*I*S/N-γ*I <<|>> i*-(beta)*s+s\"];\\n1 [color=blue, label=\"D(1, t)(I) <<|>> D(1, t)(s)\"];\\n2 [color=blue, label=\"β*I*S/N <<|>> i*-(beta)*s\"];\\n3 [color=blue, label=\"β <<|>> beta\"];\\n4 [color=blue, label=\"I\"];\\n5 [color=blue, label=\"S\"];\\n6 [color=blue, label=\"N <<|>> -(beta)\"];\\n7 [color=red, label=\"γ*I\"];\\n8 [color=red, label=\"γ\"];\\n1 -> 0 [color=blue, label=\"=\"];\\n2 -> 0 [color=blue, label=\"+\"];\\n3 -> 2 [color=red, label=\"*\"];\\n4 -> 2 [color=blue, label=\"*\"];\\n5 -> 2 [color=blue, label=\"*\"];\\n6 -> 2 [color=red, label=\"/\"];\\n7 -> 0 [color=red, label=\"-\"];\\n8 -> 7 [color=red, label=\"*\"];\\n4 -> 7 [color=red, label=\"*\"];\\n6 -> 2 [color=green, label=\"*\"];\\n3 -> 6 [color=green, label=\"-\"];\\n5 -> 0 [color=green, label=\"+\"];\\n}\\n', [1, 0, 1, 1, 1, 1, 1, 1, 1]], '1': [0.8, 2.0, ['β*I*S/N-γ*I', 'D(1, t)(I)', 'β*I*S/N', 'β', 'I', 'S', 'N', 'γ*I', 'γ'], ['i*beta*s-gamma*i+i', 'D(1, t)(i)', 'i*beta*s', 'i', 'beta', 's', 'gamma*i', 'gamma'], [0.0, 1.0, 2.0, 4.0, 3.0, 5.0, -1.0, 6.0, 7.0], [0.0, 1.0, 2.0, 4.0, 3.0, 5.0, 7.0, 8.0, -1.0], 'digraph G {\\n0 [color=blue, label=\"β*I*S/N-γ*I <<|>> i*beta*s-gamma*i+i\"];\\n1 [color=blue, label=\"D(1, t)(I)\"];\\n2 [color=blue, label=\"β*I*S/N <<|>> i*beta*s\"];\\n3 [color=blue, label=\"β <<|>> beta\"];\\n4 [color=blue, label=\"I\"];\\n5 [color=blue, label=\"S\"];\\n6 [color=red, label=\"N\"];\\n7 [color=blue, label=\"γ*I <<|>> gamma*i\"];\\n8 [color=blue, label=\"γ <<|>> gamma\"];\\n1 -> 0 [color=blue, label=\"=\"];\\n2 -> 0 [color=blue, label=\"+\"];\\n3 -> 2 [color=blue, label=\"*\"];\\n4 -> 2 [color=blue, label=\"*\"];\\n5 -> 2 [color=blue, label=\"*\"];\\n6 -> 2 [color=red, label=\"/\"];\\n7 -> 0 [color=blue, label=\"-\"];\\n8 -> 7 [color=blue, label=\"*\"];\\n4 -> 7 [color=blue, label=\"*\"];\\n4 -> 0 [color=green, label=\"+\"];\\n}\\n', [1, 0, 1, 0, 1, 0, 1, 0, 0]], '2': [0.3, 7.0, ['β*I*S/N-γ*I', 'D(1, t)(I)', 'β*I*S/N', 'β', 'I', 'S', 'N', 'γ*I', 'γ'], ['gamma*i+r', 'D(1, t)(r)', 'gamma*i', 'gamma', 'i', 'r'], [0.0, 1.0, -1.0, -1.0, 4.0, 5.0, -1.0, 2.0, 3.0], [0.0, 1.0, 7.0, 8.0, 4.0, 5.0, -1.0, -1.0, -1.0], 'digraph G {\\n0 [color=blue, label=\"β*I*S/N-γ*I <<|>> gamma*i+r\"];\\n1 [color=blue, label=\"D(1, t)(I) <<|>> D(1, t)(r)\"];\\n2 [color=red, label=\"β*I*S/N\"];\\n3 [color=red, label=\"β\"];\\n4 [color=blue, label=\"I\"];\\n5 [color=blue, label=\"S <<|>> r\"];\\n6 [color=red, label=\"N\"];\\n7 [color=blue, label=\"γ*I <<|>> gamma*i\"];\\n8 [color=blue, label=\"γ <<|>> gamma\"];\\n1 -> 0 [color=blue, label=\"=\"];\\n2 -> 0 [color=red, label=\"+\"];\\n3 -> 2 [color=red, label=\"*\"];\\n4 -> 2 [color=red, label=\"*\"];\\n5 -> 2 [color=red, label=\"*\"];\\n6 -> 2 [color=red, label=\"/\"];\\n7 -> 0 [color=red, label=\"-\"];\\n8 -> 7 [color=blue, label=\"*\"];\\n4 -> 7 [color=blue, label=\"*\"];\\n7 -> 0 [color=green, label=\"+\"];\\n5 -> 0 [color=green, label=\"+\"];\\n}\\n', [1, 0, 1, 1, 1, 1, 1, 1, 0]]}\n"
]
}
],
"source": [
"URL = f\"{SKEMA_LOCAL_ADDRESS}/workflows/isa/code-eqn-align\"\n",
"response = requests.post(URL, json=single_snippet_payload)\n",
"print(response.json())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 06cddfc

Please sign in to comment.