Skip to content

Commit 67ff98f

Browse files
authored
Merge pull request #17 from virtualcell/image-fielddata
add image-based field data support
2 parents 83f1f04 + 3e33121 commit 67ff98f

24 files changed

+1184
-113
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import tempfile
3+
from pathlib import Path
4+
5+
import numpy as np
6+
7+
from pyvcell.sim_results.var_types import NDArray3D
8+
from pyvcell.vcml import VcmlReader
9+
from pyvcell.vcml.field import Field
10+
from pyvcell.vcml.vcml_simulation import VcmlSpatialSimulation as Solver
11+
12+
13+
def create_sinusoid(shape: tuple[int, ...], freq: float) -> NDArray3D:
14+
indices = np.indices(shape)
15+
sinusoid: NDArray3D = (
16+
np.cos(freq * indices[0, :, :, :]) * np.sin(freq * indices[1, :, :, :]) * np.sin(freq * indices[2, :, :, :])
17+
)
18+
return sinusoid.astype(dtype=np.float64)
19+
20+
21+
with tempfile.TemporaryDirectory() as temp_dir_name, Path(temp_dir_name) as temp_dir:
22+
print(f"temp_dir: {temp_dir}, exists={temp_dir.exists()}")
23+
# ----- make a workspace
24+
workspace_dir = temp_dir / "workspace"
25+
workspace_dir.mkdir(parents=True, exist_ok=True)
26+
print(f"workspace_dir: {workspace_dir}, exists={workspace_dir.exists()}")
27+
sim_dir = workspace_dir / "sim1_dir"
28+
sim_dir.mkdir(parents=True, exist_ok=True)
29+
30+
print(f"sim_dir: {sim_dir}, exists={sim_dir.exists()}")
31+
32+
# ---- read in VCML file
33+
model_path = Path(os.getcwd()).parent / "models" / "SmallSpatialProject_3D.vcml"
34+
bio_model = VcmlReader.biomodel_from_file(vcml_path=model_path)
35+
36+
# ---- get the species mappings for species "s0" and "s1"
37+
app = bio_model.applications[0]
38+
s1_mapping = next(s for s in app.species_mappings if s.species_name == "s1")
39+
s0_mapping = next(s for s in app.species_mappings if s.species_name == "s0")
40+
41+
# ---- set initial concentrations to reference external field data
42+
s0_mapping.init_conc = "vcField('test2_lsm_DEMO', 'species0_cyt', 0.5, 'Volume')"
43+
s1_mapping.init_conc = "vcField('checkerboard', 'v', 0.0, 'Volume')"
44+
45+
sim = app.add_sim(name="new_sim", duration=10.0, output_time_step=0.1, mesh_size=(20, 20, 20))
46+
fields = Field.create_fields(bio_model=bio_model, sim=sim)
47+
print(fields)
48+
49+
shape = fields[0].data_nD.shape
50+
fields[0].data_nD = np.multiply(create_sinusoid(shape=shape, freq=0.5), 8.0)
51+
fields[1].data_nD = np.multiply(create_sinusoid(shape=shape, freq=0.3), 4.0)
52+
53+
# ---- add field data to the simulation
54+
55+
sim1_result = Solver(bio_model=bio_model, out_dir=sim_dir, fields=fields).run(sim.name)
56+
print([c.label for c in sim1_result.channel_data])
57+
print(sim1_result.time_points[::11])
58+
sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s0")
59+
sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s1")
60+
sim1_result.plotter.plot_concentrations()

examples/scripts/fielddata_workflow.py renamed to examples/scripts/fielddata_from_sim_workflow.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import tempfile
44
from pathlib import Path
55

6-
from pyvcell.vcml import Simulation, VcmlReader
6+
from pyvcell.vcml import VcmlReader
77
from pyvcell.vcml.vcml_simulation import VcmlSpatialSimulation as Solver
88

99
with tempfile.TemporaryDirectory() as temp_dir_name, Path(temp_dir_name) as temp_dir:
@@ -26,16 +26,15 @@
2626
s0_mapping = next(s for s in app.species_mappings if s.species_name == "s0")
2727

2828
# ---- add a simulation to the first application in the biomodel (didn't already have a simulation in the VCML file)
29-
new_sim = Simulation(name="new_sim", duration=10.0, output_time_step=0.1, mesh_size=(20, 20, 20))
30-
app.simulations.append(new_sim)
29+
sim = app.add_sim(name="new_sim", duration=10.0, output_time_step=0.1, mesh_size=(20, 20, 20))
3130

3231
# ---- set the initial concentration of species "s0" and "s1" in the first application
3332
s0_mapping.init_conc = "3+sin(x)+cos(y)+sin(z)"
3433
s1_mapping.init_conc = "3+sin(x+y+z)"
3534

3635
# ---- run simulation, store in sim1_dir, and plot results
3736
# >>>>> This forms the data for the "Field Data" identified by 'sim1_dir' <<<<<<
38-
sim1_result = Solver(bio_model=bio_model1, out_dir=sim1_dir).run(new_sim.name)
37+
sim1_result = Solver(bio_model=bio_model1, out_dir=sim1_dir).run(sim.name)
3938
print([c.label for c in sim1_result.channel_data])
4039
print(sim1_result.time_points[::11])
4140
sim1_result.plotter.plot_slice_3d(time_index=0, channel_id="s0")
@@ -47,7 +46,7 @@
4746
s1_mapping.init_conc = "5.0"
4847
# ---- re-run simulation and store in sim2_dir
4948
# note that the solution of s0 draws from the data from sim1_dir
50-
sim2_result = Solver(bio_model=bio_model1, out_dir=sim2_dir).run(new_sim.name)
49+
sim2_result = Solver(bio_model=bio_model1, out_dir=sim2_dir).run(sim.name)
5150
sim2_result.plotter.plot_slice_3d(time_index=0, channel_id="s0")
5251
sim2_result.plotter.plot_slice_3d(time_index=0, channel_id="s1")
5352
sim2_result.plotter.plot_concentrations()

poetry.lock

Lines changed: 50 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pyvcell"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "This is the python wrapper for vcell modeling and simulation"
55
authors = ["Jim Schaff <[email protected]>"]
66
repository = "https://github.com/virtualcell/pyvcell"
@@ -32,12 +32,13 @@ matplotlib = "^3.10.0"
3232
lxml = "^5.3.1"
3333
imageio = "^2.37.0"
3434
tensorstore = "^0.1.72"
35-
libvcell = "^0.0.8"
35+
libvcell = "^0.0.12"
3636
trame = "^3.8.1"
3737
trame-vtk = "^2.8.15"
3838
trame-vuetify = "^2.8.1"
3939
pyvista = "^0.44.2"
4040
trame-server = "^3.4.0"
41+
sympy = "^1.13.3"
4142

4243
[tool.poetry.group.dev.dependencies]
4344
pytest = "^7.2.0"

0 commit comments

Comments
 (0)