Skip to content

Commit

Permalink
fix to steps for simulariumio
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPatrie committed Aug 16, 2024
1 parent 08eac17 commit 8592fda
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 20 deletions.
Binary file added compose_worker/None.simularium
Binary file not shown.
45 changes: 38 additions & 7 deletions compose_worker/bigraph_steps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from tempfile import mkdtemp
from typing import Dict
from uuid import uuid4
from typing import *

Expand Down Expand Up @@ -285,6 +286,14 @@ class SimulariumSmoldynStep(Step):
'_default': True,
'_type': 'boolean'
},
'write_json': {
'_default': True,
'_type': 'boolean'
},
'run_validation': {
'_default': True,
'_type': 'boolean'
},
'file_save_name': 'maybe[string]',
'translation_magnitude': 'maybe[float]',
'meta_data': 'maybe[tree[string]]',
Expand All @@ -296,6 +305,7 @@ def __init__(self, config=None, core=CORE):

# io params
self.output_dest = self.config['output_dest']
self.write_json = self.config['write_json']
self.filename = self.config.get('file_save_name')

# display params
Expand All @@ -310,6 +320,7 @@ def __init__(self, config=None, core=CORE):

# info params
self.meta_data = self.config.get('meta_data')
self.run_validation = self.config['run_validation']

def inputs(self):
return {'results_file': 'string', 'species_counts': 'tree[integer]'}
Expand Down Expand Up @@ -342,11 +353,13 @@ def update(self, inputs):

# write data to simularium file
if self.filename is None:
self.filename = f'{in_file.replace(".", "")}-simulation'
simularium_fp = os.path.join(self.output_dest, self.filename)
write_simularium_file(data=io_data, simularium_fp=simularium_fp, json=True, validate=True)
file_root = os.path.dirname(in_file)
self.filename = in_file.split('/')[-1].replace('.', '') + "-simulation"

save_path = os.path.join(self.output_dest, self.filename)
write_simularium_file(data=io_data, simularium_fp=save_path, json=self.write_json, validate=self.run_validation)

return {'simularium_file': simularium_fp}
return {'simularium_file': save_path + '.simularium'}

def _generate_display_data(self, species_names) -> Dict | None:
# user is specifying display data for agents
Expand Down Expand Up @@ -390,7 +403,14 @@ def _generate_display_data(self, species_names) -> Dict | None:
print(f'{process_name} could not be registered because {str(e)}')


async def generate_simularium_file(input_fp: str, dest_dir: str, box_size: float):
async def generate_simularium_file(
input_fp: str,
dest_dir: str,
box_size: float,
translate_output: bool = True,
write_json: bool = True,
run_validation: bool = True
) -> Dict[str, str]:
species_names = []
with open(input_fp, 'r') as f:
output = [l.strip() for l in f.readlines()]
Expand All @@ -399,7 +419,18 @@ async def generate_simularium_file(input_fp: str, dest_dir: str, box_size: float
if not datum.isdigit():
species_names.append(datum)

simularium = SimulariumSmoldynStep(config={'output_dest': dest_dir, 'box_size': box_size})
return simularium.update(inputs={'results_file': input_fp, 'species_names': species_names})
simularium = SimulariumSmoldynStep(config={
'output_dest': dest_dir,
'box_size': box_size,
'translate_output': translate_output,
'file_save_name': None,
'write_json': write_json,
'run_validation': run_validation
})

return simularium.update(inputs={
'results_file': input_fp,
'species_names': species_names
})


Binary file added compose_worker/modelouttxt-simulation.simularium
Binary file not shown.
2 changes: 1 addition & 1 deletion compose_worker/simularium_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def translate_data_object(
def write_simularium_file(
data: Union[SmoldynData, TrajectoryData],
simularium_fp: str,
json: bool = False,
json: bool = True,
validate: bool = True
) -> None:
"""Takes in either a `SmoldynData` or `TrajectoryData` instance and saves a simularium file based on it
Expand Down
Binary file added compose_worker/simulation.simularium
Binary file not shown.
100 changes: 97 additions & 3 deletions compose_worker/test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
}
},
"cell_type": "code",
"source": "job = {'status': 'PENDING', 'job_id': 'files-generate-simularium-file00f049df-3211-469c-b009-1c95aae5dc11', 'timestamp': '2024-08-16 16:09:28.578099', 'path': 'file_uploads/files-generate-simularium-file00f049df-3211-469c-b009-1c95aae5dc11/modelout.txt', 'filename': 'simulation', 'box_size': 10.0}",
"source": "job = {'status': 'PENDING', 'job_id': 'files-generate-simularium-file00f049df-3211-469c-b009-1c95aae5dc11', 'timestamp': '2024-08-16 16:09:28.578099', 'path': 'uploads/files-generate-simularium-file00f049df-3211-469c-b009-1c95aae5dc11/modelout.txt', 'filename': 'simulation', 'box_size': 10.0}",
"id": "e28c150926800fd6",
"outputs": [],
"execution_count": 1
Expand Down Expand Up @@ -251,7 +251,7 @@
{
"data": {
"text/plain": [
"{'results': {'results_file': 'file_uploads/files-generate-simularium-file00f049df-3211-469c-b009-1c95aae5dc11/modelouttxt-simulation.simularium'}}"
"{'results': {'results_file': 'uploads/files-generate-simularium-file00f049df-3211-469c-b009-1c95aae5dc11/modelouttxt-simulation.simularium'}}"
]
},
"execution_count": 3,
Expand All @@ -261,13 +261,107 @@
],
"execution_count": 3
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-08-16T17:27:17.112917Z",
"start_time": "2024-08-16T17:27:17.107988Z"
}
},
"cell_type": "code",
"source": [
"fp = '../model-examples/smoldyn/modelout.txt'\n",
"import os\n",
"os.path.exists(fp)"
],
"id": "9920ec3288a1bd56",
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 1
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-08-16T17:27:18.048018Z",
"start_time": "2024-08-16T17:27:17.113991Z"
}
},
"cell_type": "code",
"source": [
"from bigraph_steps import generate_simularium_file\n",
"\n",
"\n"
],
"id": "e0f9fcb1045f39d1",
"outputs": [],
"execution_count": 2
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-08-16T17:27:18.487430Z",
"start_time": "2024-08-16T17:27:18.049090Z"
}
},
"cell_type": "code",
"source": "result = await generate_simularium_file(input_fp=fp, dest_dir='../model-examples/smoldyn', box_size=10.0, write_json=False, run_validation=False)",
"id": "c3e8ddfffb7e0ac9",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Reading Smoldyn Data -------------\n",
"Filtering: translation -------------\n",
"Converting Trajectory Data to Binary -------------\n",
"Writing Binary -------------\n",
"saved to ../model-examples/smoldyn/modelouttxt-simulation.simularium\n"
]
}
],
"execution_count": 3
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-08-16T17:27:21.221432Z",
"start_time": "2024-08-16T17:27:21.217836Z"
}
},
"cell_type": "code",
"source": "result",
"id": "1dd577b3febbc71f",
"outputs": [
{
"data": {
"text/plain": [
"{'simularium_file': '../model-examples/smoldyn/modelouttxt-simulation.simularium'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 4
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "9920ec3288a1bd56"
"id": "47692e72bd0d19a1"
}
],
"metadata": {
Expand Down
21 changes: 12 additions & 9 deletions compose_worker/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,18 @@ async def run(self):
job_id = self.job_params['job_id']
input_path = self.job_params.get('path')

# is a job related to a client file upload
if input_path is not None:
# download the input file
dest = tempfile.mkdtemp()
local_input_path = download_file(source_blob_path=input_path, bucket_name=BUCKET_NAME, out_dir=dest)
print(local_input_path)
# case: is a smoldyn output file and thus a simularium job
if input_path.endswith('.txt'):
await self._run_simularium(job_id=job_id, input_path=local_input_path, dest=dest)
try:
# is a job related to a client file upload
if input_path is not None:
# download the input file
dest = tempfile.mkdtemp()
local_input_path = download_file(source_blob_path=input_path, bucket_name=BUCKET_NAME, out_dir=dest)
print(local_input_path)
# case: is a smoldyn output file and thus a simularium job
if input_path.endswith('.txt'):
await self._run_simularium(job_id=job_id, input_path=local_input_path, dest=dest)
except Exception as e:
self.job_result['results'] = str(e)

return self.job_result

Expand Down
Binary file not shown.

0 comments on commit 8592fda

Please sign in to comment.