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

constant interpolation in mt_resample() #7

Closed
wants to merge 1 commit into from

Conversation

sbrockhaus
Copy link
Contributor

For technical reasons it is possible that the mouse-coordinates are recorded whenever the mouse changes the position. In this case, it makes sense to do a constant interpolation of the coordinates.
Thus, I propose an additional argument method within mt_resample that is passed to approx() and allows to use linear of constant interpolation.

The following example code compares linear and constant interpolation:

library(mousetrap)

## linear interpolation 
ex <- mt_resample(mt_example, exact_last_timestamp = FALSE, 
                  save_as="rs_trajectories",
                  step_size=1L, method = "linear")
# ex$rs_trajectories[1, , 995:1010]


## constant interpolation 
ex_c <- mt_resample(mt_example, exact_last_timestamp = FALSE, 
                  save_as="rs_trajectories",
                  step_size=1L, method = "constant")
# ex_c$rs_trajectories[1, ,  995:1010]

plot(ex$rs_trajectories[1, "ypos" , 990:1010], type="b")
points(ex_c$rs_trajectories[1, "ypos" , 990:1010], col=2, pty=2, type="b")

With the new argument method allow for constant interpolation in mt_resample, see

library(mousetrap)

## linear interpolation 
ex <- mt_resample(mt_example, exact_last_timestamp = FALSE, 
                  save_as="rs_trajectories",
                  step_size=1L, method = "linear")
# ex$rs_trajectories[1, , 999:1010]


## constant interpolation 
ex_c <- mt_resample(mt_example, exact_last_timestamp = FALSE, 
                  save_as="rs_trajectories",
                  step_size=1L, method = "constant")
# ex_c$rs_trajectories[1, ,  999:1010]

plot(ex$rs_trajectories[1, "ypos" , 990:1010], type="b")
points(ex_c$rs_trajectories[1, "ypos" , 990:1010], col=2, pty=2, type="b")
@PascalKieslich
Copy link
Owner

Thanks for your suggestions and for bringing up this topic!

The scenario you mentioned is an important one, i.e., the case were the sampling rate of the mouse is not constant, but the mouse only gets recorded on movement. In this case, it would be desirable to have a function that creates a trajectory array with a constant sampling rate, and, indeed, mt_resample could be used for achieving this.

Regarding the proposed extension: I am not sure whether this would be the best solution in this scenario. You are completely correct that for a phase in a trial without mouse movement the desired behavior when resampling the trajectory would be to keep the previous position constant (instead of interpolating between the previous and the next position). However, within the same trial there are also periods of movement. In these periods, the function should probably still perform a linear interpolation - instead of a constant one. So, one would have to come up with a solution when to perform constant and when to perform linear interpolation within the same trial.

Besides, if we assume that as soon as there is a mouse movement the position is logged relatively quickly (meaning that the spatial difference from the previous position is rather small), even for a period without mouse movement a linear interpolation would not lead to very different results than a constant interpolation.

What do you think?

Best,

Pascal

@sbrockhaus
Copy link
Contributor Author

You are right that for periods with movement a linear interpolation makes more sense than a constant interpolation. And of course, assuming that as soon as there is movement a new position is logged, it will only make a very small difference if the interpolation is done linearly or constant.
I came across the problem when I wanted to compute 'idle time', i.e. time without movement. Using a linear interpolation, there is always at least a small change between adjacent mouse-positions. And thus looking whether adjacent positions of the interpolated coordinates are exactly equal, leads to zero idle time, even if the original data does contain time without movement.

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.

None yet

2 participants