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

mt_import_long imports the wrong trajectories when mt_id_label contains mixed case data #17

Closed
LiKao opened this issue May 22, 2023 · 1 comment

Comments

@LiKao
Copy link

LiKao commented May 22, 2023

Description

If the function mt_import_long is called with a mt_id_label value that references columns that contain mixed case, then the data is imported in the wrong order, i.e. the wrong mousetracks are assigned to the incorrect mt_ids.

Reproducible example

dt <- expand.grid(sbj=c(1,2,3),condition=c(3,2,1),timestamp=seq(1:10)) %>%
    mutate(condition=c("a","b","C")[condition]) %>%
    mutate(xpos=c(a=10,b=20,C=30)[condition]*timestamp) %>%
    mutate(ypos=c(a=1,b=2,C=3)[condition]+timestamp)

 mt_import_long(dt,mt_id_label = c("sbj","condition"),timestamps_label = "timestamp")

Expected:

, , xpos

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1_C   30   60   90  120  150  180  210  240  270   300
2_C   30   60   90  120  150  180  210  240  270   300
3_C   30   60   90  120  150  180  210  240  270   300
1_b   20   40   60   80  100  120  140  160  180   200
2_b   20   40   60   80  100  120  140  160  180   200
3_b   20   40   60   80  100  120  140  160  180   200
1_a   10   20   30   40   50   60   70   80   90   100
2_a   10   20   30   40   50   60   70   80   90   100
3_a   10   20   30   40   50   60   70   80   90   100

Actual:

, , xpos

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1_C   10   20   30   40   50   60   70   80   90   100
2_C   10   20   30   40   50   60   70   80   90   100
3_C   10   20   30   40   50   60   70   80   90   100
1_b   30   60   90  120  150  180  210  240  270   300
2_b   30   60   90  120  150  180  210  240  270   300
3_b   30   60   90  120  150  180  210  240  270   300
1_a   20   40   60   80  100  120  140  160  180   200
2_a   20   40   60   80  100  120  140  160  180   200
3_a   20   40   60   80  100  120  140  160  180   200

I.e. the condition A is imported in the position of condition C.

Analysis:

This is caused by a difference how dplyr::count (here) and order (here) sort mixed case data.

Example:

dt <- dt %>% mutate(mt_id = paste0(sbj,"_",condition))

dt[order(dt$mt_id),]    # Produces order a,b,C
dplyr::count(dt,mt_id) # Produces order C,a,b
@PascalKieslich
Copy link
Owner

Thanks a lot for discovering this - and for providing the reproducible example and detailed description. I have fixed this issue now so that the original order of the trajectories is preserved.

Relating to your example above, the imported trajectory array now corresponds to the expected array:

...

, , xpos

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1_C   30   60   90  120  150  180  210  240  270   300
2_C   30   60   90  120  150  180  210  240  270   300
3_C   30   60   90  120  150  180  210  240  270   300
1_b   20   40   60   80  100  120  140  160  180   200
2_b   20   40   60   80  100  120  140  160  180   200
3_b   20   40   60   80  100  120  140  160  180   200
1_a   10   20   30   40   50   60   70   80   90   100
2_a   10   20   30   40   50   60   70   80   90   100
3_a   10   20   30   40   50   60   70   80   90   100

...

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

No branches or pull requests

2 participants