Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPatrie committed Sep 6, 2024
1 parent 1b8a3a6 commit d861469
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 12 deletions.
54 changes: 48 additions & 6 deletions bio_compose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time


current_dir = os.path.dirname(__file__)
Expand All @@ -8,9 +9,52 @@
__version__ = f.read().strip()


def run_simulation(*args, **kwargs):
# TODO: implement this
pass
def run_simulation(*args):
"""
Run a simulation with BioCompose
Args:
- args: Positional arguments
- 1 argument(**smoldyn simulation only**): smoldyn simulation configuration in which time parameters (dt, duration) are already defined.
- 3 arguments(**smoldyn simulation only**): smoldyn configuration file, smoldyn simulation duration, smoldyn simulation dt
- 5 arguments(**sbml simulation only**): sbml filepath, simulation start, simulation end, simulation steps, simulator
"""
from bio_compose.runner import SimulationRunner, SimulationResult

# set up submission
runner = SimulationRunner()
in_file = args[0]
n_args = len(args)
submission = None

if n_args == 1:
submission = runner.run_smoldyn_simulation(smoldyn_configuration_filepath=in_file)
elif n_args == 3:
dur = args[1]
dt = args[2]
submission = runner.run_smoldyn_simulation(smoldyn_configuration_filepath=in_file, duration=dur, dt=dt)
elif n_args == 5:
start = args[1]
end = args[2]
steps = args[3]
simulator = args[4]
submission = runner.run_utc_simulation(sbml_filepath=in_file, start=start, end=end, steps=steps, simulator=simulator)

# fetch results
job_id = submission.get('job_id')
output = None
if job_id is not None:
while True:
simulation_result = runner.get_output(job_id=job_id)
status = simulation_result['content']['status']
if not 'COMPLETED' in status:
time.sleep(1)
else:
output = simulation_result
break

return SimulationResult(data=output)


def verify(*args, **kwargs):
Expand All @@ -28,9 +72,7 @@ def verify(*args, **kwargs):
Returns:
Verification result instance. See documentation for more details.
"""
import time
from bio_compose.verifier import Verifier
from bio_compose.verifier import VerificationResult
from bio_compose.verifier import Verifier, VerificationResult

verifier = Verifier()
simulators = kwargs.get('simulators')
Expand Down
2 changes: 1 addition & 1 deletion bio_compose/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def get_output(self, job_id: str, download_dest: str = None, filename: str = Non
except:
import traceback
tb_str = traceback.format_exc()
return RequestError(error=error_message)
return RequestError(error=tb_str)

def get_job_status(self, job_id: str):
output = self.get_output(job_id=job_id)
Expand Down
9 changes: 9 additions & 0 deletions bio_compose/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ def generate_simularium_file(self, smoldyn_output_filepath: str, box_size: float
headers = {'Content-Type': multidata.content_type}

return self._execute_request(endpoint=endpoint, headers=headers, multidata=multidata, query_params=query_params)


class SimulationResult(dict):
def __init__(self, data):
self.data = data
self.job_id = self.data.get('job_id')

def visualize(self):
pass
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This project configuration should relate only to the high level notebook api (as primary deps).
[tool.poetry]
name = "bio-compose"
version = "0.5.1"
version = "0.5.2"
description = "Create, execute, and introspect reproducible composite simulations of dynamic biological systems."
authors = ["Alex Patrie <[email protected]>"]
readme = "README.md"
Expand Down
79 changes: 75 additions & 4 deletions tests/test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"__init__.py \u001b[34mfixtures\u001b[m\u001b[m observables.pdf rmse.pdf\n",
"\u001b[34m__pycache__\u001b[m\u001b[m main.py plot.pdf test.ipynb\n"
"__init__.py \u001B[34mfixtures\u001B[m\u001B[m observables.pdf rmse.pdf\n",
"\u001B[34m__pycache__\u001B[m\u001B[m main.py plot.pdf test.ipynb\n"
]
}
],
Expand Down Expand Up @@ -169,11 +169,82 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9c91ddd-cb0f-414e-acd4-7e0a3536d302",
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-06T15:17:04.630824Z",
"start_time": "2024-09-06T15:17:04.625194Z"
}
},
"source": [
"def f(*args):\n",
" if len(args) == 1:\n",
" return args[0] ** 2\n",
" elif len(args) == 3:\n",
" return args[0] + args[1] + args[2]\n",
" \n",
" \n",
"f(10, 10, 10)"
],
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2024-09-06T15:25:24.177897Z"
}
},
"cell_type": "code",
"source": [
"from bio_compose import run_simulation\n",
"\n",
"\n",
"results = run_simulation('/Users/alexanderpatrie/Desktop/repos/bio-compose/tests/fixtures/smoldyn/MinE.txt')"
],
"id": "9f65ea642fe5cfc5",
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mKeyboardInterrupt\u001B[0m Traceback (most recent call last)",
"Cell \u001B[0;32mIn[1], line 4\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mbio_compose\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m run_simulation\n\u001B[0;32m----> 4\u001B[0m results \u001B[38;5;241m=\u001B[39m \u001B[43mrun_simulation\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43m/Users/alexanderpatrie/Desktop/repos/bio-compose/tests/fixtures/smoldyn/MinE.txt\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n",
"File \u001B[0;32m~/Desktop/repos/bio-compose/bio_compose/__init__.py:52\u001B[0m, in \u001B[0;36mrun_simulation\u001B[0;34m(*args)\u001B[0m\n\u001B[1;32m 50\u001B[0m status \u001B[38;5;241m=\u001B[39m simulation_result[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mcontent\u001B[39m\u001B[38;5;124m'\u001B[39m][\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mstatus\u001B[39m\u001B[38;5;124m'\u001B[39m]\n\u001B[1;32m 51\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mCOMPLETED\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;129;01min\u001B[39;00m status:\n\u001B[0;32m---> 52\u001B[0m \u001B[43mtime\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43msleep\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;241;43m1\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[1;32m 53\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m 54\u001B[0m output \u001B[38;5;241m=\u001B[39m simulation_result\n",
"\u001B[0;31mKeyboardInterrupt\u001B[0m: "
]
}
],
"execution_count": 1
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "results",
"id": "a2eb33fd435820b7"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"source": []
"execution_count": null,
"source": "",
"id": "791935b3397ac5d6"
}
],
"metadata": {
Expand Down

0 comments on commit d861469

Please sign in to comment.