Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow user to use custom function for simulation callback #211

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions bluecellulab/simulation/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
class Simulation:
"""Class that represents a neuron simulation."""

def __init__(self, parallel_context=None) -> None:
def __init__(self, parallel_context=None, custom_progress_function=None) -> None:
self.cells: list[bluecellulab.Cell] = []
self.fih_progress = None
self.progress = None
self.progress_closed = None
self.progress_dt: Optional[float] = None
self.pc = parallel_context
self.custom_progress_function = custom_progress_function

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

def progress_callback(self):
"""Callback function for the progress bar."""
if self.progress > 0:
sys.stdout.write("\x1b[3F")

self.progress += 1
self.progress_closed = not self.progress_closed
if self.progress_closed:
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), " ", " " * (100 - self.progress)))
sys.stdout.write("[%s%s%s]\n" % ("#" * (
self.progress - 1), "-", "." * (100 - self.progress)))
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), " ", " " * (100 - self.progress)))
if self.custom_progress_function is None:
if self.progress > 0:
sys.stdout.write("\x1b[3F")

self.progress += 1
self.progress_closed = not self.progress_closed
if self.progress_closed:
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), " ", " " * (100 - self.progress)))
sys.stdout.write("[%s%s%s]\n" % ("#" * (
self.progress - 1), "-", "." * (100 - self.progress)))
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), " ", " " * (100 - self.progress)))
else:
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), "/", " " * (100 - self.progress)))
sys.stdout.write("[%s%s%s]\n" % ("#" * (
self.progress - 1), ">", "." * (100 - self.progress)))
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), "\\", " " * (100 - self.progress)))
sys.stdout.flush()
else:
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), "/", " " * (100 - self.progress)))
sys.stdout.write("[%s%s%s]\n" % ("#" * (
self.progress - 1), ">", "." * (100 - self.progress)))
sys.stdout.write(" %s%s%s \n" % (" " * (
self.progress - 1), "\\", " " * (100 - self.progress)))
sys.stdout.flush()
self.custom_progress_function()

neuron.h.cvode.event(
neuron.h.t + self.progress_dt, self.progress_callback)
Comment on lines 78 to 79
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the cvode is disabled
for synaptome simulation, we have to disable the cvode, so i don't know if the event will be triggered hence the progress_callback will work ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested with cvode disabled, and the callback function is still called.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey thanks

Expand Down