Skip to content

Commit b389740

Browse files
committed
Fixed parameter placement when read from run call
1 parent fa546d3 commit b389740

File tree

5 files changed

+79
-38
lines changed

5 files changed

+79
-38
lines changed

for_local_use.py

+49-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,55 @@
22
from mobspy import modules
33

44
if __name__ == '__main__':
5-
from mobspy import *
65

7-
8-
# Replace parameters using units
9-
Color, Location = BaseSpecies()
10-
Color.red, Color.blue
11-
Location.here, Location.there
12-
Something = Color*Location
13-
14-
2*Something >> 3*Something [lambda r1, r2: 1*u.decimeter**2/u.h if Location(r1) == Location(r2) else 0.5*u.decimeter**2/u.h]
15-
16-
S = Simulation(Something)
17-
S.volume = 1*u.m**2
18-
print(S.compile())
19-
20-
# print(A.get_characteristics())
6+
A = BaseSpecies()
7+
p1, p2 = ModelParameters([1], [1 / u.hour, 2 / u.hour, 3 / u.hour])
8+
A >> Zero[p1 * p2]
9+
A(100)
10+
S = Simulation(A)
11+
S.run(duration=5 * u.hour, plot_data=False, level=-1)
12+
print(S.results)
13+
14+
exit()
15+
16+
"""
17+
This is the Tree model from the paper
18+
We have a population of Trees
19+
The Trees can die, age, have different colors and be in two different forests
20+
The colors can change randomly from time to time
21+
All old Trees can reproduce, but the Three is born green and young
22+
"""
23+
Ager, Mortal, Colored, Location = BaseSpecies()
24+
Colored.green, Colored.yellow, Colored.brown
25+
Location.dense, Location.sparse
26+
Ager.young >> Ager.old[1 / 10 / u.year]
27+
Mortal >> Zero [lambda r1: 0.1 / u.year if r1.old else 0]
28+
Tree = Ager * Colored * Mortal * Location
29+
30+
# replication
31+
Tree.old >> Tree + Tree.green.young[0.1/u.year]
32+
33+
# competition
34+
Tree.dense.old + Tree.dense.young >> Tree.dense.old[1e-10 * u.decimeter ** 2 / u.year]
35+
36+
# reproduction
37+
bf = 1e-10 * u.decimeter ** 2 / u.year
38+
rep_r = lambda t1, t2: 5 * bf if (Location(t1) == Location(t2) and Colored(t1) == Colored(t2)) else bf
39+
2 * Tree >> 2 * Tree + Tree.yound[rep_r]
40+
41+
# color cycling
42+
colors = ['green', 'yellow', 'brown']
43+
for color, next_color in zip(colors, colors[1:] + colors[:1]):
44+
Tree.c(color) >> Tree.c(next_color)[10/u.year]
45+
46+
# initial conditions
47+
Tree.dense(50), Tree.dense.old(50), Tree.sparse(50), Tree.sparse.old(50)
48+
# initial conditions
49+
Tree.dense(50), Tree.dense.old(50), Tree.sparse(50), Tree.sparse.old(50)
50+
MySim = Simulation(Tree)
51+
MySim.run(volume = 1 * u.meter ** 2, unit_x='year',
52+
duration=100 * u.years, repetitions=3, output_concentration=False,
53+
simulation_method='stochastic', save_data=False, plot_data=False)
54+
MySim.plot_stochastic(Tree.dense, Tree.sparse, Tree.brown)
2155

2256

local_debug_script.py

Whitespace-only changes.

mobspy/simulation.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,6 @@ def run(self, duration=None, volume=None, dimension=None,
346346
:param save_data: (bool) save data or not
347347
:param plot_data: (bool) plot data or not
348348
"""
349-
# Level needs to be set before compilation
350-
if level is not None:
351-
self.level = level
352-
353-
# Base case - If there are no events we compile the model here
354-
if self._species_for_sbml is None:
355-
self.compile(verbose=False)
356-
357349
# This is only here so the ide gives the users tips about the function argument.
358350
# I wish there was a way to loop over all argument without args and kargs
359351
pr_manually_process_each_parameter(self, duration, volume, dimension,
@@ -362,6 +354,14 @@ def run(self, duration=None, volume=None, dimension=None,
362354
jobs, unit_x, unit_y, output_concentration, output_event,
363355
output_file, save_data, plot_data, rate_type, plot_type)
364356

357+
# Level needs to be set before compilation
358+
if level is not None:
359+
self.level = level
360+
361+
# Base case - If there are no events we compile the model here
362+
if self._species_for_sbml is None:
363+
self.compile(verbose=False)
364+
365365
self._assemble_multi_simulation_structure()
366366

367367
simlog.debug('Starting Simulator')

test_script.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111
from copy import deepcopy
1212
import sys
1313
import os
14-
14+
import re
1515

1616
# Compare results with expected file
1717
def compare_model(comp_results, file_name):
18-
with open(file_name, 'r') as file:
18+
def normalize(text):
19+
""" Normalize text by removing extra spaces, tabs, and Unicode variations. """
20+
text = text.strip() # Remove leading/trailing spaces
21+
text = re.sub(r'\s+', ' ', text) # Replace multiple spaces/tabs/newlines with a single space
22+
return text
23+
24+
with open(file_name, 'r', encoding='utf-8') as file:
1925
for r, line in zip(comp_results.split('\n'), file.readlines()):
20-
line = line.replace('\n', '')
26+
line = normalize(line)
27+
r = normalize(r)
2128

2229
if r != line:
2330
print('file: ' + line)
@@ -1411,6 +1418,7 @@ def test_double_parameters_with_units():
14111418
A(100)
14121419
S = Simulation(A)
14131420
S.run(duration=5 * u.hour, plot_data=False, level=-1)
1421+
14141422
assert compare_model(str(S.results), 'test_tools/model_45.txt')
14151423

14161424

0 commit comments

Comments
 (0)