Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding origin shift to GridCells for translations #123

Closed
wants to merge 2 commits into from

Conversation

jquinnlee
Copy link
Contributor

Proposing a new param for the GridCell class - "shift_origin"
Enables user to provide x-y coordinates for origin of GridCells, with default at (0., 0.)

Sorry for taking ages to get this in!

shift_origin field added to GridCells class that translates origin to desired x-y location, with default at (0., 0.)
Update Neurons.py to allow origin translation for GridCells
@TomGeorge1234
Copy link
Collaborator

TomGeorge1234 commented Feb 6, 2025

Hey Quinn, I'm not sure I even remember where we spoke about this!

This functionality would be nice but the way you are proposing to do it just overwrites the phase offsets. It is also not implemented right, I think you would need the parameter to be a list or array, not a tuple.

Either way, I think the current API does strictly (if not super intuitively) expose this functionality by allowing uses to set the phase offsets as shown on line 1183. Adding a new parameter would be redundant and might risk confusing things.

origin = self.gridscales.reshape(-1, 1) * self.phase_offsets / (2 * np.pi)

Proposed solution exploiting current API

Wouldn't it be best for you to

  1. Choose your grid scales gridscales = np.array([]) #(n,)
  2. Choose your desired origins origins = np.array([]) # (n,2)
  3. Compute your phase offsets by inverting the equation in line 1183 phase_offsets = 2*np.pi*origins / grid scales[:,None] #(n,2)
  4. Initialise:
GCs = GridCells(Agent, params={
                        'gridscale':gridscales,
                        'phase_offset':phase_offsets})

If you don't know your grid scales (e.g. because they're being sampled at initialisation) you could initialise the grid cells, then get the scales, then set GCs.phase_offsets afterwards. If you wanted you could even have a different origin for each grid (as shown below).

Working example

You can see it works because each GC is centred on its new origin (green dot).

# Standard initialisation
Env = Environment()
Ag = Agent(Env)
GCs = GridCells(Ag)

# Set the desired origins
origins = np.random.uniform(0,1,(GCs.n,2)) # Put your desired origins here

# Overwrite phase offsets to place the grid cells at the desired origins
GCs.phase_offsets = 2 * np.pi * origins / GCs.gridscales[:,None]

# Visualise and check that the phase offsets are correct
fig, axs = GCs.plot_rate_map()
for i in range(GCs.n):
    axs[i].scatter(*origins[i], c='g', s=100)

dc8f72dd-2b84-42d7-9144-bb78c142dceb

@jquinnlee
Copy link
Contributor Author

Hey Tom,

Sorry for my oversight on this redundancy! I confirmed in my workflow that your suggested solution with overwriting phase_offsets works perfectly.

Thanks!

@jquinnlee jquinnlee closed this Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants