cbmos/cbmodel/__init__.py
def simulate(
            self,
            cell_list,
            t_data,
            force_args,
            solver_args,
            seed=None,
            raw_t=True,
            max_execution_time=None,
            min_event_resolution=0.,
            ):
        """
        Run the simulation with the given arguments and return the position
        of the cells at each time steps in `t_data`

        Parameters
        ----------
        cell_list: [Cell]
            Initial cell layout
        t_data: [float]
            times at which the history should be recorded, if `raw_t` is set to `True`,
            only the start and end time are taken into account and the rest is ignored.
            The history is then recorded following the solver's output.
        force_args: dict
            arguments to pass to the force function
        solver_args: dict
            arguments to pass to the solver
        seed: int
            seed for the random number generator
        raw_t: bool
            whether or not to use the solver's raw output. In that case, `t_data`
            is ignored and the raw times are returned along the history
        max_execution_time: float
            Maximum execution time in seconds that the simulation should use.
            Since the elapsed time is only checked in between cell events, this
            only represents an approximate target. The exact duration is saved
            in self.last_exec_time
        min_event_resolution: float
            Minimum event resolution interval: events occurring within
            `min_event_resolution` of the current time will be resolved
            immediately.

        Returns
        -------
        (t_data, history)

        Note
        ----
        - Cell ordering in the output can vary between timepoints.
        - Cell indices need to be unique for the whole duration of the simulation.
        - If `raw_t` is false, t_data is returned as is, with the history. If
          `raw_t` is true, aggregated t_data from the solver is returned.

        """

        exec_time_start = time.time()

        _npr.seed(seed)

        for cell in cell_list:
                assert len(cell.position) == self.dim

        _logging.debug("Starting new simulation")

        t = t_data[0]
        t_end = t_data[-1]
        self.cell_list = [
                _cl.Cell(
                    cell.ID, cell.position, cell.birthtime,
                    cell.proliferating, cell.generate_division_time,
                    cell.division_time, cell.parent_ID)
                for cell in cell_list]

        self.next_cell_index = max(self.cell_list, key=lambda cell: cell.ID).ID + 1
        self.history = []
        self.t_data = [t] if raw_t else t_data[:]
        self._save_data()

        # build event queue once, since independent of environment (for now)
        self._queue = EventQueue(
                [(cell.division_time, cell) for cell in self.cell_list],
                min_resolution=min_event_resolution,
                )

        while t < t_end:

            # check if max_execution_time has elapsed
            exec_time = time.time() - exec_time_start
            if max_execution_time is not None and exec_time >= max_execution_time:
                self.last_exec_time = exec_time
                return (self.t_data, self.history)

            # generate next event
            tau, cells = self._queue.pop()

            if tau > t:
                # calculate positions until the last t_data smaller or equal to min(tau, t_end)
                t_eval = [t] \
                    + [time for time in self.t_data if t < time <= min(tau, t_end)]
                y0 = _np.array([cell.position for cell in self.cell_list]).reshape(-1)
                # only calculate positions if there is t_data before tau
                if len(t_eval) > 1:
                    sol = self._calculate_positions(t_eval, y0, force_args, solver_args, raw_t=raw_t)

                    # save data for all t_data points passed
                    for y_t in sol.y[:, 1:].T:
                        self._save_data(y_t.reshape(-1, self.dim))
                    if raw_t:
                        self.t_data.extend(sol.t[1:])

                # check if max_execution_time has elapsed
                exec_time = time.time() - exec_time_start
                if max_execution_time is not None and exec_time >= max_execution_time:
                    self.last_exec_time = exec_time
                    return (self.t_data, self.history)

                # continue the simulation until tau if necessary
                if tau > t_eval[-1] and tau <=t_end:
                    y0 = sol.y[:, -1] if len(t_eval) > 1 else y0
                    sol = self._calculate_positions([t_eval[-1], tau], y0, force_args, solver_args, raw_t=raw_t)

                    if raw_t:
                        for y_t in sol.y[:, 1:].T:
                            self._save_data(y_t.reshape(-1, self.dim))
                        self.t_data.extend(sol.t[1:])
                elif tau > t_end and raw_t:
                    # continue the simulation until t_end (only necessary if
                    # t_end is not in t_eval because raw_t is true (default))
                    y0 = sol.y[:, -1] if len(t_eval) > 1 else y0
                    sol = self._calculate_positions([t_eval[-1], t_end], y0, force_args, solver_args, raw_t=raw_t)

                    #if raw_t:
                    for y_t in sol.y[:, 1:].T:
                        self._save_data(y_t.reshape(-1, self.dim))
                    self.t_data.extend(sol.t[1:])

                # update the positions for the current time point
                self._update_positions(sol.y[:, -1].reshape(-1, self.dim).tolist())

            # check if max_execution_time has elapsed
            exec_time = time.time() - exec_time_start
            if max_execution_time is not None and exec_time >= max_execution_time:
                self.last_exec_time = exec_time
                return (self.t_data, self.history)

            # apply event if tau <= t_end
            if tau <= t_end:
                for cell in cells:
                    self._apply_division(cell, tau)

            # update current time t to min(tau, t_end)
            t = min(tau, t_end)

        exec_time = time.time() - exec_time_start
        self.last_exec_time = exec_time
        return (self.t_data, self.history)

        Run the simulation with the given arguments and return the position
        of the cells at each time steps in `t_data`

        Parameters
        ----------
        cell_list: [Cell]
            Initial cell layout
        t_data: [float]
            times at which the history should be recorded, if `raw_t` is set to `True`,
            only the start and end time are taken into account and the rest is ignored.
            The history is then recorded following the solver's output.
        force_args: dict
            arguments to pass to the force function
        solver_args: dict
            arguments to pass to the solver
        seed: int
            seed for the random number generator
        raw_t: bool
            whether or not to use the solver's raw output. In that case, `t_data`
            is ignored and the raw times are returned along the history
        max_execution_time: float
            Maximum execution time in seconds that the simulation should use.
            Since the elapsed time is only checked in between cell events, this
            only represents an approximate target. The exact duration is saved
            in self.last_exec_time
        min_event_resolution: float
            Minimum event resolution interval: events occurring within
            `min_event_resolution` of the current time will be resolved
            immediately.

        Returns
        -------
        (t_data, history)

        Note
        ----
        - Cell ordering in the output can vary between timepoints.
        - Cell indices need to be unique for the whole duration of the simulation.
        - If `raw_t` is false, t_data is returned as is, with the history. If
          `raw_t` is true, aggregated t_data from the solver is returned.

        
Run the simulation with the given arguments and return the position
of the cells at each time steps in `t_data`

Parameters

[Cell]
Initial cell layout
t_data: [float]
times at which the history should be recorded, if `raw_t` is set to `True`,
only the start and end time are taken into account and the rest is ignored.
The history is then recorded following the solver's output.
force_args: dict
arguments to pass to the force function
solver_args: dict
arguments to pass to the solver
seed: int
seed for the random number generator
raw_t: bool
whether or not to use the solver's raw output. In that case, `t_data`
is ignored and the raw times are returned along the history
max_execution_time: float
Maximum execution time in seconds that the simulation should use.
Since the elapsed time is only checked in between cell events, this
only represents an approximate target. The exact duration is saved
in self.last_exec_time
min_event_resolution: float
Minimum event resolution interval: events occurring within
`min_event_resolution` of the current time will be resolved
immediately.

Returns



Note

Cell ordering in the output can vary between timepoints.
Cell indices need to be unique for the whole duration of the simulation.
If `raw_t` is false, t_data is returned as is, with the history. If
`raw_t` is true, aggregated t_data from the solver is returned.
Run the simulation with the given arguments and return the position of the cells at each time steps in ` t_data ` Parameters [ Cell ] Initial cell layout t_data : [ float ] times at which the history should be recorded if ` raw_t ` is set to ` True ` only the start and end time are taken into account and the rest is ignored . The history is then recorded following the solver ' s output . force_args : dict arguments to pass to the force function solver_args : dict arguments to pass to the solver seed : int seed for the random number generator raw_t : bool whether or not to use the solver ' s raw output . In that case ` t_data ` is ignored and the raw times are returned along the history max_execution_time : float Maximum execution time in seconds that the simulation should use . Since the elapsed time is only checked in between cell events this only represents an approximate target . The exact duration is saved in self . last_exec_time min_event_resolution : float Minimum event resolution interval : events occurring within ` min_event_resolution ` of the current time will be resolved immediately . Returns Note Cell ordering in the output can vary between timepoints . Cell indices need to be unique for the whole duration of the simulation . If ` raw_t ` is false t_data is returned as is with the history . If ` raw_t ` is true aggregated t_data from the solver is returned .
def simulate(
            self,
            cell_list,
            t_data,
            force_args,
            solver_args,
            seed=None,
            raw_t=True,
            max_execution_time=None,
            min_event_resolution=0.,
            ):
        exec_time_start = time.time()
        _npr.seed(seed)
        for cell in cell_list:
                assert len(cell.position) == self.dim
        _logging.debug("Starting new simulation")
        t = t_data[0]
        t_end = t_data[-1]
        self.cell_list = [
                _cl.Cell(
                    cell.ID, cell.position, cell.birthtime,
                    cell.proliferating, cell.generate_division_time,
                    cell.division_time, cell.parent_ID)
                for cell in cell_list]
        self.next_cell_index = max(self.cell_list, key=lambda cell: cell.ID).ID + 1
        self.history = []
        self.t_data = [t] if raw_t else t_data[:]
        self._save_data()
        self._queue = EventQueue(
                [(cell.division_time, cell) for cell in self.cell_list],
                min_resolution=min_event_resolution,
                )
        while t < t_end:
            exec_time = time.time() - exec_time_start
            if max_execution_time is not None and exec_time >= max_execution_time:
                self.last_exec_time = exec_time
                return (self.t_data, self.history)
            tau, cells = self._queue.pop()
            if tau > t:
                t_eval = [t] \
                    + [time for time in self.t_data if t < time <= min(tau, t_end)]
                y0 = _np.array([cell.position for cell in self.cell_list]).reshape(-1)
                if len(t_eval) > 1:
                    sol = self._calculate_positions(t_eval, y0, force_args, solver_args, raw_t=raw_t)
                    for y_t in sol.y[:, 1:].T:
                        self._save_data(y_t.reshape(-1, self.dim))
                    if raw_t:
                        self.t_data.extend(sol.t[1:])
                exec_time = time.time() - exec_time_start
                if max_execution_time is not None and exec_time >= max_execution_time:
                    self.last_exec_time = exec_time
                    return (self.t_data, self.history)
                if tau > t_eval[-1] and tau <=t_end:
                    y0 = sol.y[:, -1] if len(t_eval) > 1 else y0
                    sol = self._calculate_positions([t_eval[-1], tau], y0, force_args, solver_args, raw_t=raw_t)
                    if raw_t:
                        for y_t in sol.y[:, 1:].T:
                            self._save_data(y_t.reshape(-1, self.dim))
                        self.t_data.extend(sol.t[1:])
                elif tau > t_end and raw_t:
                    y0 = sol.y[:, -1] if len(t_eval) > 1 else y0
                    sol = self._calculate_positions([t_eval[-1], t_end], y0, force_args, solver_args, raw_t=raw_t)
                    for y_t in sol.y[:, 1:].T:
                        self._save_data(y_t.reshape(-1, self.dim))
                    self.t_data.extend(sol.t[1:])
                self._update_positions(sol.y[:, -1].reshape(-1, self.dim).tolist())
            exec_time = time.time() - exec_time_start
            if max_execution_time is not None and exec_time >= max_execution_time:
                self.last_exec_time = exec_time
                return (self.t_data, self.history)
            if tau <= t_end:
                for cell in cells:
                    self._apply_division(cell, tau)
            t = min(tau, t_end)
        exec_time = time.time() - exec_time_start
        self.last_exec_time = exec_time
        return (self.t_data, self.history)
def simulate ( self , cell_list , t_data , force_args , solver_args , seed = None , raw_t = True , max_execution_time = None , min_event_resolution = 0. , ) : exec_time_start = time . time ( ) _npr . seed ( seed ) for cell in cell_list : assert len ( cell . position ) == self . dim _logging . debug ( "Starting new simulation" ) t = t_data [ 0 ] t_end = t_data [ - 1 ] self . cell_list = [ _cl . Cell ( cell . ID , cell . position , cell . birthtime , cell . proliferating , cell . generate_division_time , cell . division_time , cell . parent_ID ) for cell in cell_list ] self . next_cell_index = max ( self . cell_list , key = lambda cell : cell . ID ) . ID + 1 self . history = [ ] self . t_data = [ t ] if raw_t else t_data [ : ] self . _save_data ( ) self . _queue = EventQueue ( [ ( cell . division_time , cell ) for cell in self . cell_list ] , min_resolution = min_event_resolution , ) while t < t_end : exec_time = time . time ( ) - exec_time_start if max_execution_time is not None and exec_time >= max_execution_time : self . last_exec_time = exec_time return ( self . t_data , self . history ) tau , cells = self . _queue . pop ( ) if tau > t : t_eval = [ t ] + [ time for time in self . t_data if t < time <= min ( tau , t_end ) ] y0 = _np . array ( [ cell . position for cell in self . cell_list ] ) . reshape ( - 1 ) if len ( t_eval ) > 1 : sol = self . _calculate_positions ( t_eval , y0 , force_args , solver_args , raw_t = raw_t ) for y_t in sol . y [ : , 1 : ] . T : self . _save_data ( y_t . reshape ( - 1 , self . dim ) ) if raw_t : self . t_data . extend ( sol . t [ 1 : ] ) exec_time = time . time ( ) - exec_time_start if max_execution_time is not None and exec_time >= max_execution_time : self . last_exec_time = exec_time return ( self . t_data , self . history ) if tau > t_eval [ - 1 ] and tau <= t_end : y0 = sol . y [ : , - 1 ] if len ( t_eval ) > 1 else y0 sol = self . _calculate_positions ( [ t_eval [ - 1 ] , tau ] , y0 , force_args , solver_args , raw_t = raw_t ) if raw_t : for y_t in sol . y [ : , 1 : ] . T : self . _save_data ( y_t . reshape ( - 1 , self . dim ) ) self . t_data . extend ( sol . t [ 1 : ] ) elif tau > t_end and raw_t : y0 = sol . y [ : , - 1 ] if len ( t_eval ) > 1 else y0 sol = self . _calculate_positions ( [ t_eval [ - 1 ] , t_end ] , y0 , force_args , solver_args , raw_t = raw_t ) for y_t in sol . y [ : , 1 : ] . T : self . _save_data ( y_t . reshape ( - 1 , self . dim ) ) self . t_data . extend ( sol . t [ 1 : ] ) self . _update_positions ( sol . y [ : , - 1 ] . reshape ( - 1 , self . dim ) . tolist ( ) ) exec_time = time . time ( ) - exec_time_start if max_execution_time is not None and exec_time >= max_execution_time : self . last_exec_time = exec_time return ( self . t_data , self . history ) if tau <= t_end : for cell in cells : self . _apply_division ( cell , tau ) t = min ( tau , t_end ) exec_time = time . time ( ) - exec_time_start self . last_exec_time = exec_time return ( self . t_data , self . history )
Run the simulation with the given arguments and return the position
of the cells at each time steps in `t_data`
Run the simulation with the given arguments and return the position of the cells at each time steps in ` t_data `
"""
        Run the simulation with the given arguments and return the position
        of the cells at each time steps in `t_data`

        Parameters
        ----------
        cell_list: [Cell]
            Initial cell layout
        t_data: [float]
            times at which the history should be recorded, if `raw_t` is set to `True`,
            only the start and end time are taken into account and the rest is ignored.
            The history is then recorded following the solver's output.
        force_args: dict
            arguments to pass to the force function
        solver_args: dict
            arguments to pass to the solver
        seed: int
            seed for the random number generator
        raw_t: bool
            whether or not to use the solver's raw output. In that case, `t_data`
            is ignored and the raw times are returned along the history
        max_execution_time: float
            Maximum execution time in seconds that the simulation should use.
            Since the elapsed time is only checked in between cell events, this
            only represents an approximate target. The exact duration is saved
            in self.last_exec_time
        min_event_resolution: float
            Minimum event resolution interval: events occurring within
            `min_event_resolution` of the current time will be resolved
            immediately.

        Returns
        -------
        (t_data, history)

        Note
        ----
        - Cell ordering in the output can vary between timepoints.
        - Cell indices need to be unique for the whole duration of the simulation.
        - If `raw_t` is false, t_data is returned as is, with the history. If
          `raw_t` is true, aggregated t_data from the solver is returned.

        """ # build event queue once, since independent of environment (for now) # check if max_execution_time has elapsed # generate next event # calculate positions until the last t_data smaller or equal to min(tau, t_end) # only calculate positions if there is t_data before tau # save data for all t_data points passed # check if max_execution_time has elapsed # continue the simulation until tau if necessary # continue the simulation until t_end (only necessary if # t_end is not in t_eval because raw_t is true (default)) #if raw_t: # update the positions for the current time point # check if max_execution_time has elapsed # apply event if tau <= t_end # update current time t to min(tau, t_end)
self
cell_list
t_data
force_args
solver_args
seed
raw_t
max_execution_time
min_event_resolution