3. Discussion: how can we break out the context map generation? #3

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

Right now, the context map generation logic is in the Walker constructor. This works but is ugly and not very extensible. How could we solve this?

  1. how would the import statement change as a result of needing a separate context_map module?
  2. what input arguments do the context_map functions need to take?
  3. how does the initialization of the walker change?
    • i.e. instead of "map_type"
  4. Do you think these changes improve the code? Why?

Exercise instructions:
Go to the notebook walker/Step_3_break_out_the_context_map_initialization and follow the instructions. Comment here what you think the solution could look like. Also feel free to comment on other group's suggestions.

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

Right now, the context map generation logic is in the Walker constructor. This works but is ugly and not very extensible. How could we solve this? 1. how would the import statement change as a result of needing a separate context_map module? 2. what input arguments do the context_map functions need to take? 3. how does the initialization of the walker change? - i.e. instead of "map_type" 4. Do you think these changes improve the code? Why? Exercise instructions: Go to the notebook walker/Step_3_break_out_the_context_map_initialization and follow the instructions. Comment here what you think the solution could look like. Also feel free to comment on other group's suggestions. Make sure to include your suggested code snippet(s).
Member
  1. from context_map import create_flat_map, create_hills_map, ...
  2. size
  3. map = create_hills_map(size)
    walker = Walker(..., map = map)
  4. yes. It allows for new or stored context map types (flexibility), makes the Walker class more readable
1. from context_map import create_flat_map, create_hills_map, ... 2. size 3. map = create_hills_map(size) walker = Walker(..., map = map) 4. yes. It allows for new or stored context map types (flexibility), makes the Walker class more readable
Member
  1. we take the whole if-elif chain and make it a separate function outside class Walker.
    import construct_context_map

  2. Change the init to reflect changes and make it directly inject the context_map from outside`.

 def __init__(self, sigma_i, sigma_j, context_map):
  1. Still assign self.context_map = context_map, since _compute_next_step_probability uses it
1. we take the whole `if-elif` chain and make it a separate function outside class Walker. ``` import construct_context_map``` 2. Change the `init` to reflect changes and make it directly inject the `context_map` from outside`. ``` def __init__(self, sigma_i, sigma_j, context_map): ``` 3. Still assign `self.context_map = context_map`, since `_compute_next_step_probability` uses it
Member
  1. from context_map import context_map

  2. context_map = context_map(size, type)

  3. def init(self, sigma_i, sigma_j, context_map):

  4. Improvement! Improved flexibility, adaptability, reduced complexity.....

1. from context_map import context_map 2. context_map = context_map(size, type) 3. def __init__(self, sigma_i, sigma_j, context_map): 4. Improvement! Improved flexibility, adaptability, reduced complexity.....
Member
  1. imports change> from context_maps import create_context_map
  2. create_context_map(size, type)
  3. __init__(self, sigma_i, sigma_j)
  4. yes, definitely readability, and it's easier to test, modify
1. imports change> `from context_maps import create_context_map` 2. `create_context_map(size, type)` 3. `__init__(self, sigma_i, sigma_j)` 4. yes, definitely readability, and it's easier to test, modify
Member
  1. We would need to add another import statement for the context_map.
  2. Size and map_type.
  3. We would no longer need to put size and map_type in the initialisation of the walker but we would need to add the context_map separately with the size and map_type initialised in it.
  4. Yes, because it is separating what the functions do and gives more control over the individual functions. It is more flexible and also makes it clearer which properties relate to which functions or variables.
1. We would need to add another import statement for the context_map. 2. Size and map_type. 3. We would no longer need to put size and map_type in the initialisation of the walker but we would need to add the context_map separately with the size and map_type initialised in it. 4. Yes, because it is separating what the functions do and gives more control over the individual functions. It is more flexible and also makes it clearer which properties relate to which functions or variables.
Member
from plotting import plot_trajectory, plot_trajectory_hexbin
from walker import Walker
# Import new context functions
import context_map

SIZE = 200
# Create a map
context_map = context_map.make_labyrinth(size=SIZE)
# Create a Walker instance
walker = Walker(sigma_i=3, sigma_j=4, size=SIZE,context_map)

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

plot_trajectory(trajectory, context_map)
``` from plotting import plot_trajectory, plot_trajectory_hexbin from walker import Walker # Import new context functions import context_map SIZE = 200 # Create a map context_map = context_map.make_labyrinth(size=SIZE) # Create a Walker instance walker = Walker(sigma_i=3, sigma_j=4, size=SIZE,context_map) # Sample a next step 1000 times i, j = 100, 50 trajectory = [] for _ in range(1000): i, j = walker.sample_next_step(i, j) trajectory.append((i, j)) plot_trajectory(trajectory, context_map) ```
Member

"""Question 1"""

%matplotlib inline

from plotting import plot_trajectory, plot_trajectory_hexbin
from walker import Walker
from context_map import context_map_builder as cmb #new separte context map module

"""Question 2"""

map1 = cmb(size = 200, map_type = 'mountain')

"""Question 3"""

walker = Walker(sigma_i=3, sigma_j=4, size=200, context_map = map1)

"""Question 4"""
"""yes, because the context map gets built outside the walker class. The context map was not using any Walker attributes earlier anyway. This makes the code more flexible."""

"""Question 1""" %matplotlib inline from plotting import plot_trajectory, plot_trajectory_hexbin from walker import Walker from context_map import context_map_builder as cmb #new separte context map module """Question 2""" map1 = cmb(size = 200, map_type = 'mountain') """Question 3""" walker = Walker(sigma_i=3, sigma_j=4, size=200, context_map = map1) """Question 4""" """yes, because the context map gets built outside the walker class. The context map was not using any Walker attributes earlier anyway. This makes the code more flexible."""
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
8 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#3
No description provided.