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

phatnguyen1999p opened this issue May 12, 2021 · 3 comments


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);
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'])
         for x in range(size):
             IR_median = self.Signal_IR[self.working_data_IR['peaklist'][int(x)]] - \
             RED_median = self.Signal_RED[self.working_data_RED['peaklist'][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(
        at <python>
        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(
        at com.chaquo.python.PyObject.callAttr(
        at com.example.healthdevice.MainActivity.lambda$onCreate$2$MainActivity(
        at com.example.healthdevice.-$$Lambda$MainActivity$CtWii84LN6gVm9CXafLLaSQv6Ik.onItemClick(Unknown Source:8)
        at android.widget.AdapterView.performItemClick(
        at android.widget.AbsListView.performItemClick(
        at android.widget.AbsListView$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)
Copy link

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])] - \

@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
Copy link

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.

Copy link

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
None yet

No branches or pull requests

2 participants