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

TypeError: array indices must be integers or slices, not int64 #495

Closed
phatnguyen1999p opened this issue May 12, 2021 · 3 comments
Closed
Labels

Comments

@phatnguyen1999p
Copy link

Chaquopy version

  • Chaquopy version 9.1.0

Devices or emulators where the issue happens

  • Android Smartphone - Vsmart Live - Android 10

Relevant parts of your code

//Java part

PyObject PRV = pyObject.callAttr("PRV_analyser", tempData.getIRstoArray(), tempData.getIRstoArray(), 25);
PRV.callAttr("signal_filter");
PRV.callAttr("signal_calculator");
PyObject SpO2 = PRV.callAttr("SpO2_calculator");

// Python part

import heartpy
import numpy

class PRV_analyser():
    /.../
     def SpO2_calculator(self):
         R = []
         size = len(self.working_data_IR['peaklist'])
         print(type(size))
 
         for x in range(size):
             IR_median = self.Signal_IR[self.working_data_IR['peaklist'][int(x)]] - \
                 [self.working_data_IR['ybeat'][int(x)]]
             RED_median = self.Signal_RED[self.working_data_RED['peaklist'][int(x)]] - \
                 [self.working_data_RED['ybeat'][int(x)]]
 
             # Calculating R ratio (AC_IR * DC_RED)/(DC_IR * AC_RED)
             R.append(float((self.working_data_IR['ybeat'][int(x)] * RED_median) /
                            (self.working_data_RED['ybeat'][int(x)] * IR_median)))
 
         R = numpy.array(R)
         SpO = 104 - 17*R
         SpO2 = numpy.sum(SpO)/len(SpO)
         return SpO2

Describe your issue

I got an issue about TypeError Exception of x value when I launch my python code from java. But when I calling this function from python script, the error doesn't happen. Here is my debug error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.healthdevice, PID: 13761
    com.chaquo.python.PyException: TypeError: array indices must be integers or slices, not int64
        at <python>.java.chaquopy.JavaArray._invalid_key(array.pxi:170)
        at <python>.java.chaquopy.JavaArray.__getitem__(array.pxi:132)
        at <python>.PRV_analyser.SpO2_calculator(PRV_analyser.py:46)
        at <python>.chaquopy_java.call(chaquopy_java.pyx:380)
        at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:352)
        at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
        at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
        at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
        at com.example.healthdevice.MainActivity.lambda$onCreate$2$MainActivity(MainActivity.java:136)
        at com.example.healthdevice.-$$Lambda$MainActivity$CtWii84LN6gVm9CXafLLaSQv6Ik.onItemClick(Unknown Source:8)
        at android.widget.AdapterView.performItemClick(AdapterView.java:330)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1213)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3221)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7458)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
@mhsmith
Copy link
Member

mhsmith commented May 12, 2021

It looks like Chaquopy doesn't support using NumPy scalars as indices for Java arrays. The standard Python list doesn't have this problem, so we'll fix it in the next version of Chaquopy. Thanks for the report.

Meanwhile, you can work around it by converting the array index to an int. I see you've already tried to do this, but x isn't the problem because it's already an int. You need to convert the larger expression instead, like this:

             IR_median = self.Signal_IR[int(self.working_data_IR['peaklist'][x])] - \
                 [self.working_data_IR['ybeat'][x]]

@mhsmith mhsmith added the bug label May 12, 2021
@mhsmith mhsmith changed the title TypeError Exception, but it doesn't happen when launching from Python directly TypeError: array indices must be integers or slices, not int64 May 12, 2021
@phatnguyen1999p
Copy link
Author

I have applied your fix, and the problem truly doesn't come from x indice but it come from the type of array value, or something like that. So I applied this fix:
IR_median = self.Signal_IR[int(self.working_data_IR['peaklist'][x])] - \ float(self.working_data_IR['ybeat'][x])
Trying to convert the value to its type, and the issue have been fixed.

@mhsmith
Copy link
Member

mhsmith commented Sep 22, 2021

This issue is fixed in Chaquopy 10.0.1. To upgrade, edit your app's top-level build.gradle file and change the version number of com.chaquo.python:gradle.

@mhsmith mhsmith closed this as completed Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants