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

Error when passing data in tibble format. #4

Open
khataei opened this issue Jan 16, 2022 · 4 comments
Open

Error when passing data in tibble format. #4

khataei opened this issue Jan 16, 2022 · 4 comments

Comments

@khataei
Copy link
Collaborator

khataei commented Jan 16, 2022

Environment:

  • OS: Ubuntu 20.04
  • Tester: Javad Khataei
  • Software version: R 4.1.2 from RStudio docker image

Description:

When the input data is passed in tibble format, it throws the following error.
Error: Must subset rows with a valid subscript vector. x Can't convert from <double> to <integer> due to loss of precision.

Steps to reproduce:

library(activityCounts)  
bad_input =  dplyr::as_tibble(activityCounts::sampleXYZ)  
activityCounts::counts(bad_input)

Expected Result:

A data frame with counts:


                  Time  x  y  z
1   2022-01-16 17:57:49  4 93 13
2   2022-01-16 17:57:50 21 50 14
3   2022-01-16 17:57:51 17 22 15
4   2022-01-16 17:57:52 22 39 23
5   2022-01-16 17:57:53 15 25 16
...

Actual Result:


Error: Must subset rows with a valid subscript vector.
x Can't convert from <double> to <integer> due to loss of precision.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
In counts(data = dplyr::as_tibble(input), hertz = 100, x_axis = 2, 

acknowledgement:

This bug report is created based on Karl Brown feedback.

@khataei
Copy link
Collaborator Author

khataei commented Jan 16, 2022

First Solution:

Convert input data to dataframe before passing it to the function.

library(activityCounts)  
bad_input =  dplyr::as_tibble(activityCounts::sampleXYZ)  
good_input = as.data.farame(bad_input)
activityCounts::counts(bad_input)

Second solution:

A new version of the package is pushed to the dev branch. Installing it via devtools package fixes this issue.

Steps:

  • Restart R
  • Remove the CRAN version of ActivityCounts by running:
    • remove.packages('activityCounts')
  • Install 'devtools' package to install ActivityCounts from Github:
    • install.packages('devtools')
  • Install that latest version of ActivityCounts from Github dev branch.
    • devtools::install_git('https://github.com/walkabillylab/activityCounts', force=T, ref = 'dev')
  • Restart R

Note:

  • We will keep this issue open until we submit the modified version to CRAN.

@AaronRendahl
Copy link

AaronRendahl commented Jan 16, 2022

So I was having this issue too (and used the same workaround), but I now really think this issue is because the downsampling isn't doing the right thing; it currently takes the code to downsample from seewave::resamp (which is called directly in the version on the jbrond version), which looks like this:

r <- f/g
wave1 <- wave[seq(1, n, by = r), 1]

But what the seq command does is to create a list of evenly spaced numbers from 1 to n, which means they're not integers -- but are being used to index the wave variable anyway! And a tibble correctly won't let you do this but a data.frame will. A data frame silently converts non-integers to integers using floor, like this (demonstrating with a numeric vector, which does the same thing:

Suppose we downsample by a ratio of 1.3, over a range of 1 to 11; then the locations that we really want to see are these:

k <- seq(1, 11, by=1.3)
k
## [1]  1.0  2.3  3.6  4.9  6.2  7.5  8.8 10.1

But the locations we actually get are these:

(1:11)[k]
## [1]  1  2  3  4  6  7  8 10

This doesn't feel right to me...

@khataei
Copy link
Collaborator Author

khataei commented Jan 16, 2022

Hey @jranaraki. Any comment on this?

@AaronRendahl
Copy link

AaronRendahl commented Jan 16, 2022

For comparison, the original matlab code uses the resample function, which "applies an FIR Antialiasing Lowpass Filter to x and compensates for the delay introduced by the filter" (Documentation at mathworks.com) and the python code uses resampy.resample which uses a "Kaiser" filter (Documentation at readthedocs.io).

walkabilly added a commit that referenced this issue Nov 5, 2022
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