Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 3eec88b

Browse files
AurelienJaquierJaquier Aurélien Tristan
and
Jaquier Aurélien Tristan
authored
allow user to use custom function for simulation callback (#211)
Co-authored-by: Jaquier Aurélien Tristan <[email protected]>
1 parent d4b3597 commit 3eec88b

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

bluecellulab/simulation/simulation.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
class Simulation:
2929
"""Class that represents a neuron simulation."""
3030

31-
def __init__(self, parallel_context=None) -> None:
31+
def __init__(self, parallel_context=None, custom_progress_function=None) -> None:
3232
self.cells: list[bluecellulab.Cell] = []
3333
self.fih_progress = None
3434
self.progress = None
3535
self.progress_closed = None
3636
self.progress_dt: Optional[float] = None
3737
self.pc = parallel_context
38+
self.custom_progress_function = custom_progress_function
3839

3940
def add_cell(self, new_cell: bluecellulab.Cell) -> None:
4041
"""Add a cell to a simulation."""
@@ -50,26 +51,29 @@ def init_progress_callback(self):
5051

5152
def progress_callback(self):
5253
"""Callback function for the progress bar."""
53-
if self.progress > 0:
54-
sys.stdout.write("\x1b[3F")
55-
56-
self.progress += 1
57-
self.progress_closed = not self.progress_closed
58-
if self.progress_closed:
59-
sys.stdout.write(" %s%s%s \n" % (" " * (
60-
self.progress - 1), " ", " " * (100 - self.progress)))
61-
sys.stdout.write("[%s%s%s]\n" % ("#" * (
62-
self.progress - 1), "-", "." * (100 - self.progress)))
63-
sys.stdout.write(" %s%s%s \n" % (" " * (
64-
self.progress - 1), " ", " " * (100 - self.progress)))
54+
if self.custom_progress_function is None:
55+
if self.progress > 0:
56+
sys.stdout.write("\x1b[3F")
57+
58+
self.progress += 1
59+
self.progress_closed = not self.progress_closed
60+
if self.progress_closed:
61+
sys.stdout.write(" %s%s%s \n" % (" " * (
62+
self.progress - 1), " ", " " * (100 - self.progress)))
63+
sys.stdout.write("[%s%s%s]\n" % ("#" * (
64+
self.progress - 1), "-", "." * (100 - self.progress)))
65+
sys.stdout.write(" %s%s%s \n" % (" " * (
66+
self.progress - 1), " ", " " * (100 - self.progress)))
67+
else:
68+
sys.stdout.write(" %s%s%s \n" % (" " * (
69+
self.progress - 1), "/", " " * (100 - self.progress)))
70+
sys.stdout.write("[%s%s%s]\n" % ("#" * (
71+
self.progress - 1), ">", "." * (100 - self.progress)))
72+
sys.stdout.write(" %s%s%s \n" % (" " * (
73+
self.progress - 1), "\\", " " * (100 - self.progress)))
74+
sys.stdout.flush()
6575
else:
66-
sys.stdout.write(" %s%s%s \n" % (" " * (
67-
self.progress - 1), "/", " " * (100 - self.progress)))
68-
sys.stdout.write("[%s%s%s]\n" % ("#" * (
69-
self.progress - 1), ">", "." * (100 - self.progress)))
70-
sys.stdout.write(" %s%s%s \n" % (" " * (
71-
self.progress - 1), "\\", " " * (100 - self.progress)))
72-
sys.stdout.flush()
76+
self.custom_progress_function()
7377

7478
neuron.h.cvode.event(
7579
neuron.h.t + self.progress_dt, self.progress_callback)

0 commit comments

Comments
 (0)