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

Open
opened 2025-09-17 14:53:58 +02:00 by Aitor · 10 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"

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" **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)**
lisa changed title from Open the notebook “Step_1_classes_exercise” and follow the instructions to 3. Discussion: how can we break out the context map generation? 2025-09-22 17:47:23 +02:00
Aitor added the
Enhancement
Conceptual
labels 2025-09-24 10:01:43 +02:00
Member

Context map is separate class with the constructor taking size and map_type

# Create context map
context_map = ContextMap(size=200, map_type='hills')
# Create a Walker instance
walker = Walker(sigma_i=3, sigma_j=4, 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, walker.context_map)

Context map is separate class with the constructor taking size and map_type ```python # Create context map context_map = ContextMap(size=200, map_type='hills') # Create a Walker instance walker = Walker(sigma_i=3, sigma_j=4, 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, walker.context_map) ```
Member

We create a different funtion for each plot type within the context_maps module

import context_maps

context_map = context_maps.hills(size=12) # call whichever map type is desired, with necessary args 

# Create a Walker instance
walker = Walker(sigma_i=3, sigma_j=4, size=200)

# 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)
We create a different funtion for each plot type within the context_maps module ```python import context_maps context_map = context_maps.hills(size=12) # call whichever map type is desired, with necessary args # Create a Walker instance walker = Walker(sigma_i=3, sigma_j=4, size=200) # 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

I would like the functionality of the creation of the context map and the walker to be separated. Like so:

form walker import Walker, build_hills
size = 200
context_map = build_hills(size)
walker = Walker(sigma_i=3, sigma_j=4, context_map=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)
I would like the functionality of the creation of the context map and the walker to be separated. Like so: ``` form walker import Walker, build_hills size = 200 context_map = build_hills(size) walker = Walker(sigma_i=3, sigma_j=4, context_map=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

We suggest:

  • to move function create_context_map to the separate module context_map. The module will contain separate function for each context map type;
  • the create_context_map will take arguments "size" and "type";
  • the Walker class will not have the method context_map anymore.

`from context_map import create_context_map

context_map = create_context_map(size, 'hills')

walker = Walker(sigma_i=3, sigma_j=4)`

We suggest: - to move function create_context_map to the separate module context_map. The module will contain separate function for each context map type; - the create_context_map will take arguments "size" and "type"; - the Walker class will not have the method context_map anymore. `from context_map import create_context_map context_map = create_context_map(size, 'hills') walker = Walker(sigma_i=3, sigma_j=4)`
Member

The independent module context_maps provides various functions to build various maps.
After instantiating our walker we will build a map outside the Walker class.
The class method sample_next_step and plot_trajectory takes a built map of certain size as argument.

from context_maps import build_hills_map

# Create a Walker instance
walker = Walker(sigma_i=3, sigma_j=4)
# create context_map 
context_map = build_hills_map(size=200)

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

plot_trajectory(trajectory, context_map) 
The independent module `context_maps` provides various functions to build various maps. After instantiating our walker we will build a map outside the `Walker` class. The class method `sample_next_step` and `plot_trajectory` takes a built map of certain size as argument. ``` from context_maps import build_hills_map # Create a Walker instance walker = Walker(sigma_i=3, sigma_j=4) # create context_map context_map = build_hills_map(size=200) # Sample a next step 1000 times i, j = 100, 50 trajectory = [] for _ in range(1000): i, j = walker.sample_next_step(i, j, context_map) trajectory.append((i, j)) plot_trajectory(trajectory, context_map) ```
Member

We could externalize the context map construction into a separate module called context_map, containg functions that take care of the individual map types.

Imports would change to

from context_map import create_cm_hills, ...

Input arguments for the functions would be size, the interface of Walker would be unchanged.

We could externalize the context map construction into a separate module called `context_map`, containg functions that take care of the individual map types. Imports would change to ```python from context_map import create_cm_hills, ... ``` Input arguments for the functions would be size, the interface of `Walker` would be unchanged.
Member

Committed as pesudocode in the notebook

Committed as pesudocode in the notebook
Member
from context_map import context_map
context_map_instance = context_map(size, type)
walker = Walker(sigma_i, sigma_j, context_map = context_map_instance )
```python from context_map import context_map context_map_instance = context_map(size, type) walker = Walker(sigma_i, sigma_j, context_map = context_map_instance ) ```
Member
from plotting import plot_trajectory, plot_trajectory_hexbin
from walker import Walker

from context_map import hills, flat, labyrinth


# Create map
cmap = hills(size)

# Create a Walker instance
walker = Walker(sigma_i=3, sigma_j=4, size=200, context_map=cmap)

# 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, walker.context_map)

```python from plotting import plot_trajectory, plot_trajectory_hexbin from walker import Walker from context_map import hills, flat, labyrinth # Create map cmap = hills(size) # Create a Walker instance walker = Walker(sigma_i=3, sigma_j=4, size=200, context_map=cmap) # 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, walker.context_map) ```
Author
Owner

@martinco wrote in #3 (comment):

We create a different funtion for each plot type within the context_maps module

import context_maps

context_map = context_maps.hills(size=12) # call whichever map type is desired, with necessary args 

# Create a Walker instance
walker = Walker(sigma_i=3, sigma_j=4, size=200)

# 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)

I dont think you passed the context map :/

@martinco wrote in https://git.aspp.school/ASPP/2025-plovdiv-scientific-patterns/issues/3#issuecomment-722: > We create a different funtion for each plot type within the context_maps module > > ```python > import context_maps > > context_map = context_maps.hills(size=12) # call whichever map type is desired, with necessary args > > # Create a Walker instance > walker = Walker(sigma_i=3, sigma_j=4, size=200) > > # 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) > ``` I dont think you passed the context map :/
Sign in to join this conversation.
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/2025-plovdiv-scientific-patterns#3
No description provided.