2. Discussion: What would a Walker class look like? #2

Open
opened 2026-08-31 16:02:08 +02:00 by lisa · 9 comments
Owner

Right now, the walker logic is implemented as a collection of standalone functions (sample_next_step, next_step_proposal, compute_next_step_probability, etc.). While this works, it might be more maintainable to encapsulate the walker’s state and behavior into a Walker class.

Exercise instructions:
Open the notebook walker/Step_1_classes and follow the instructions. Think about what you think the Walker interface should look like, answer the three questions in the notebook, and post your ideas as a comment to this issue.

Feel free to comment on other group's suggestions, respectfully.

Make sure to include your suggested code snippet(s).

Right now, the walker logic is implemented as a collection of standalone functions (sample_next_step, next_step_proposal, compute_next_step_probability, etc.). While this works, it might be more maintainable to encapsulate the walker’s state and behavior into a Walker class. Exercise instructions: Open the notebook walker/Step_1_classes and follow the instructions. Think about what you think the Walker interface should look like, answer the three questions in the notebook, and post your ideas as a comment to this issue. Feel free to comment on other group's suggestions, respectfully. Make sure to include your suggested code snippet(s).
Member
size = 200  # size of the image
context_map = create_context_map(size, 'hills')  # fixed context map
walker.context_map = context_map

Sample a next step 1000 times
trajectory = []
for _ in range(1000):
    walker.sample_next_step()
    trajectory.append((walker.i, walker.j))

plot_trajectory(trajectory, context_map)

Inside Walker class: i, j, sigma_i, sigma_j, context_map, sample_next_step()

```walker = Walker(100, 50, 3, 4) # initial position #Walker has a default context_map = None, given in init size = 200 # size of the image context_map = create_context_map(size, 'hills') # fixed context map walker.context_map = context_map Sample a next step 1000 times trajectory = [] for _ in range(1000): walker.sample_next_step() trajectory.append((walker.i, walker.j)) plot_trajectory(trajectory, context_map) ``` Inside Walker class: i, j, sigma_i, sigma_j, context_map, sample_next_step()
Member

We imagine Walker as an agent that, well, walks, given a set of inputs. Non-agentic things that pre-set environment like create_context_map should be, probably, outside of class Walker.

What's inside the Walker:

  • sample_next_step
  • i, j
  • sigma_i, sigma_j
  • context_map
  • trajectory

What's outside:

  • create_context_map
  • plot_trajectory
  • size
We imagine Walker as an agent that, well, walks, given a set of inputs. Non-agentic things that pre-set environment like `create_context_map` should be, probably, outside of class Walker. What's **inside** the Walker: - `sample_next_step` - `i, j` - `sigma_i`, `sigma_j` - `context_map` - `trajectory` What's **outside**: - `create_context_map` - `plot_trajectory` - `size`
Member

@atillake wrote in #2 (comment):

walker = Walker(100, 50, 3, 4) # initial position #Walker has a default context_map = None, it could also be given in init? size = 200 # size of the image context_map = create_context_map(size, 'hills') # fixed context map walker.context_map = context_map

Sample a next step 1000 times

trajectory = [] for _ in range(1000): walker.sample_next_step() trajectory.append((walker.i, walker.j))

plot_trajectory(trajectory, context_map)

Inside Walker class: i, j, sigma_i, sigma_j, context_map, sample_next_step()

agreed.

@atillake wrote in https://git.aspp.school/ASPP/2026-prague-scientific-patterns/issues/2#issuecomment-1439: > walker = Walker(100, 50, 3, 4) # initial position #Walker has a default context_map = None, it could also be given in init? size = 200 # size of the image context_map = create_context_map(size, 'hills') # fixed context map walker.context_map = context_map > # [](#sample-a-next-step-1000-times)Sample a next step 1000 times > > trajectory = [] for _ in range(1000): walker.sample_next_step() trajectory.append((walker.i, walker.j)) > > plot_trajectory(trajectory, context_map) > > Inside Walker class: i, j, sigma_i, sigma_j, context_map, sample_next_step() agreed.
Member

Write the (pseudo)code that uses your new walker interface here!
walker_1 = Walker(i=100, j=50 , sigma_i=3, sigma_j=4, trajectory)
walker_1.create_trajectory()

create_trajectory():
creates the trajectory -- for loop
includes sample_next_step fuction

Write the (pseudo)code that uses your new `walker` interface here! walker_1 = Walker(i=100, j=50 , sigma_i=3, sigma_j=4, trajectory) walker_1.create_trajectory() create_trajectory(): creates the trajectory -- for loop includes sample_next_step fuction
Member
  1. We thought that all of the functions and parameters coulf feasibly go inside the Walker class. However, sigma_i and sigma_j may make more sense outside the class if they are not constant.

w = Walker(i, j, sigma_i, sigma_j, size, trajectory)
w.create_context_map()
for _ in range(1000):
w.sample_next_step()

w.plot_trajectory()

1. We thought that all of the functions and parameters coulf feasibly go inside the Walker class. However, sigma_i and sigma_j may make more sense outside the class if they are not constant. 2. w = Walker(i, j, sigma_i, sigma_j, size, trajectory) w.create_context_map() for _ in range(1000): w.sample_next_step() w.plot_trajectory()
Member
class Walker:
    def __init__(self, i, j, sigma_i, sigma_j):
        self.i = i
        self.j = j
        self.sigma_i = sigma_i
        self.sigma_j = sigma_j

    def walk(self)
        return sample_next_step, trajectory

We would define the walker by its initial position and degrees of freedom, while the context map could be still a separate function called by trajectory and along size. (Maybe size can be part of the walker though)

``` class Walker: def __init__(self, i, j, sigma_i, sigma_j): self.i = i self.j = j self.sigma_i = sigma_i self.sigma_j = sigma_j def walk(self) return sample_next_step, trajectory ``` We would define the walker by its initial position and degrees of freedom, while the context map could be still a separate function called by trajectory and along size. (Maybe size can be part of the walker though)
Member

we are not sure if we need to import the class (and what that looks like)

from walker import Walker, create_context_map, plot_trajectory

size = 200 # size of the image
context_map = create_context_map(size, 'hills')

walker = Walker(sigma_i=3, sigma_j=4, i=100, j=50, context_map)

trajectory = []
for _ in range(1000):
i, j = walker.sample_next_step(i, j, sigma_i, sigma_j, context_map)
trajectory.append((i, j))

plot_trajectory(trajectory, context_map)

# we are not sure if we need to import the class (and what that looks like) from walker import Walker, create_context_map, plot_trajectory size = 200 # size of the image context_map = create_context_map(size, 'hills') walker = Walker(sigma_i=3, sigma_j=4, i=100, j=50, context_map) trajectory = [] for _ in range(1000): i, j = walker.sample_next_step(i, j, sigma_i, sigma_j, context_map) trajectory.append((i, j)) plot_trajectory(trajectory, context_map)
Member

our pseudo code:
`class Walker:
def init(self, i, j, sigma_i, sigma_j):
self.i = i
self.j = j
self.sigma_i = sigma_i
self.sigma_j = sigma_j

def trajectory(self, context_map):
    for _ in range(1000):
        self.i, self.j = sample_next_step(self.i, self.j, self.sigma_i, self.sigma_j, context_map)            
        self.trajectory = trajectory.append((self.i, self.j))
    return self.trajectory`
our pseudo code: `class Walker: def __init__(self, i, j, sigma_i, sigma_j): self.i = i self.j = j self.sigma_i = sigma_i self.sigma_j = sigma_j def trajectory(self, context_map): for _ in range(1000): self.i, self.j = sample_next_step(self.i, self.j, self.sigma_i, self.sigma_j, context_map) self.trajectory = trajectory.append((self.i, self.j)) return self.trajectory`
Member

In the class:

  • sample_next_step
  • i, j
  • sigma_i and sigma_j
  • trajectory

These are properties of the walker.

Outside the class:

  • create_context_map
  • plot_trajectory
  • size
  • context_map

These are properties of the context, independent of the walker.

walker = Walker(sigma_i=3, sigma_j=4, i=100, j=50)

size = 200  # size of the image
context_map = create_context_map(size, 'hills')  # fixed context map

for _ in range(1000):
    walker.i, walker.j = walker.sample_next_step(walker.i, walker.j, walker.sigma_i, walker.sigma_j, context_map)
    walker.trajectory.append((walker.i, walker.j))


plot_trajectory(walker.trajectory, context_map)
In the class: - `sample_next_step` - `i, j` - `sigma_i` and `sigma_j` - `trajectory` These are properties of the walker. Outside the class: - `create_context_map` - `plot_trajectory` - `size` - `context_map` These are properties of the context, independent of the walker. ``` walker = Walker(sigma_i=3, sigma_j=4, i=100, j=50) size = 200 # size of the image context_map = create_context_map(size, 'hills') # fixed context map for _ in range(1000): walker.i, walker.j = walker.sample_next_step(walker.i, walker.j, walker.sigma_i, walker.sigma_j, context_map) walker.trajectory.append((walker.i, walker.j)) plot_trajectory(walker.trajectory, context_map)
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
10 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ASPP/2026-prague-scientific-patterns#2
No description provided.