muschellij2 / gt3x

MIT License
1 stars 1 forks source link

ValueError: cannot convert float NaN to integer #2

Open cosmicnet opened 3 years ago

cosmicnet commented 3 years ago

Hello,

When I try to read in gt3x files I get the error:

ValueError: cannot convert float NaN to integer

It's caused by line 184:

log_data[:] = np.NaN

I've tested on Python 3.10 and 3.8.8, for the later numpy version 1.20.3.

Code to snippet to reproduce:

import numpy as np
acc_data_type = np.int16
#acc_data_type = np.float #uncomment and this works

# create empty array for the acceleration data
log_data = np.empty((10, 3), dtype=acc_data_type)
log_data[:] = np.NaN

Hard coding to enabling scaling fixes the issue as it uses np.float which can be NaN. I'm guessing older versions of numpy allowed np.int16 to have NaN or silently converted to 0 but I haven't tested.

muschellij2 commented 3 years ago

Any thoughts @shaheen-syed

cosmicnet commented 3 years ago

I just tested on python 2.7.14 with numpy 1.16.0 and it does indeed silently convert to 0.

Suggested patch:

@@ -181,7 +181,11 @@

        # create empty array for the acceleration data
        log_data = np.empty((sample_rate * SIZE , NUM_AXES), dtype=acc_data_type)
-       log_data[:] = np.NaN
+       if use_scaling:
+               log_data[:] = np.NaN
+       else:
+               log_data[:] = 0
+
        # empty numpy array to store the timestamps
        time_data = np.empty((SIZE,1), dtype=np.uint32)
muschellij2 commented 3 years ago

Hmm - I will incorporate when @shaheen-syed indicates that this 0 was the intended effect, as I wrapped their code up here.

cosmicnet commented 3 years ago

It could be that you don't want NaN at all, even with floating point, as to date the behaviour has actually been 0s.

If 0s are a problem, then there is a much bigger issue here.

shaheen-syed commented 3 years ago

Not sure from the top of my head. That line isn't part of the original code I had written a while ago:

https://github.com/shaheen-syed/ActiGraph-ActiWave-Analysis/blob/master/functions/gt3x_functions.py#L185

muschellij2 commented 3 years ago

@cosmicnet - can you share the GT3X file that caused this issue?

cosmicnet commented 3 years ago

No I can't as the data is restricted. Nor would it help. Try running my code example on a newer Python and numpy, the error isn't related to GT3X files at all. Newer numpy versions error rather than silently setting types that don't support NaN to 0.

cosmicnet commented 3 years ago
Python 3.8.11 (default, Aug  6 2021, 09:57:55) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> acc_data_type = np.int16
>>> #acc_data_type = np.float #uncomment and this works
>>>
>>> # create empty array for the acceleration data
>>> log_data = np.empty((10, 3), dtype=acc_data_type)
>>> log_data[:] = np.NaN
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot convert float NaN to integer
hillolsarker commented 2 years ago

It's not a gt3x file issue. I am also facing the same issue starting from Python 3.7 onward (upto 3.6 it was okay). I believe replacing the above mentioned np.NaN with 0 will resolve the issue.