2024-heraklion-scientific-p.../code_snippets/walker_next_step.ipynb
2024-08-27 15:52:41 +03:00

6.7 KiB

In [ ]:
class Walker:
    # ...
    
    def sample_next_step(self, current_i, current_j, random_state=np.random):
        """ Sample a new position for the walker. """
        # Combine the next-step proposal with the context map to get a
        # next-step probability map
        next_step_map = self._next_step_proposal(current_i, current_j)
        selection_map = self._compute_next_step_probability(next_step_map)

        # Draw a new position from the next-step probability map
        r = random_state.rand()
        cumulative_map = np.cumsum(selection_map)
        cumulative_map = cumulative_map.reshape(selection_map.shape)
        i_next, j_next = np.argwhere(cumulative_map >= r)[0]

        return i_next, j_next

    def _next_step_proposal(self, current_i, current_j):
        """ Create the 2D proposal map for the next step of the walker. """
        # 2D Gaussian distribution , centered at current position,
        # and with different standard deviations for i and j
        grid_ii, grid_jj = self._grid_ii, self._grid_jj
        sigma_i, sigma_j = self.sigma_i, self.sigma_j

        rad = (
            (((grid_ii - current_i) ** 2) / (sigma_i ** 2))
            + (((grid_jj - current_j) ** 2) / (sigma_j ** 2))
        )

        p_next_step = np.exp(-(rad / 2.0)) / (2.0 * np.pi * sigma_i * sigma_j)
        return p_next_step / p_next_step.sum()
In [1]:
class Walker:
    # ...

    def _next_step_proposal(self, current_i, current_j):
        """ Create the 2D proposal map for the next step of the walker. """
        raise NotImplementedError("`_next_step_proposal` not implemented")
In [ ]:
class GaussianWalker(Walker):
    # ...

    def _next_step_proposal(self, current_i, current_j):
 
        
        
        
        
        
        
        
        
        
        
        
class RectangularWalker(Walker):
    # ...

    def _next_step_proposal(self, current_i, current_j):

        
        
        
        
        
        
        
        
        
        
        
class JumpingWalker(Walker):
    # ...

    def _next_step_proposal(self, current_i, current_j):
In [ ]:
class Walker:
    # ...


    def _compute_next_step_probability(self, next_step_map):
        """ Compute the next step probability map from next step proposal and
        context map. """
        next_step_probability = next_step_map * self.context_map
        next_step_probability /= next_step_probability.sum()
        return next_step_probability
In [ ]:
class GaussianWalkerWithProductInteraction(Walker):
    def _next_step_proposal(self, current_i, current_j):
        # ...
    def _compute_next_step_probability(self, next_step_map):
        # ...

        
class GaussianWalkerWithSumInteraction(Walker):
    def _next_step_proposal(self, current_i, current_j):
        # ...
    def _compute_next_step_probability(self, next_step_map):
        # ...

        
class RectangularWalkerWithProductInteraction(Walker):
    def _next_step_proposal(self, current_i, current_j):
        # ...
    def _compute_next_step_probability(self, next_step_map):
        # ...

        
class RectangularWalkerWithSumInteraction(Walker):
    def _next_step_proposal(self, current_i, current_j):
        # ...
    def _compute_next_step_probability(self, next_step_map):
        # ...
In [ ]:
class Walker:
    def __init__(self, size, context_map, next_step_proposal, next_step_proposal_arguments):
        self.next_step_proposal = next_step_proposal
        # ...

    
    def sample_next_step(self, current_i, current_j, random_state=np.random):
        """ Sample a new position for the walker. """
        # Combine the next-step proposal with the context map to get a
        # next-step probability map
        next_step_map = self.next_step_proposal(current_i, current_j, **next_step_proposal_arguments)
        selection_map = self._compute_next_step_probability(next_step_map)

        # Draw a new position from the next-step probability map
        r = random_state.rand()
        cumulative_map = np.cumsum(selection_map)
        cumulative_map = cumulative_map.reshape(selection_map.shape)
        i_next, j_next = np.argwhere(cumulative_map >= r)[0]

        return i_next, j_next