0

sig_txt: [45 67 83 32767 101 90 50]

idx_rr:[101] // index after 32767

sig: [45 67 83 101 90 50] // all the elements except 32767

Basically what happens is from the original array, all except the element whose value is equal to 32767 is passed to the sig array. And idx_rr gets the value immediate after 32767. I have written a python for the same. But I am getting this error:

"'int' object does not support item assignment"

when I am trying to plot the idx_rr value of sig. Can I get some help on this.

R_peak = False   
j = 0
k = 0
idx_rr = []
sig = []

for i in range (len(sig_txt)):
    if (sig_txt[i] == 32767):
        idx_rr = i+1
        idx_rr[j] = np.array(sig_txt[i+1])
        sig = i+1
        sig[k] = np.array(sig_txt[i+1])
        k = k + 1
        j = j + 1
        print(idx_rr)
        R_peak = True
    else:
        if (R_peak == False):
            sig = i
            sig[k] = np.array(sig_txt[i])
            k = k + 1

        else:
            R_peak = False

plt.figure(figsize=(20,8))
plt.plot(sig)
plt.scatter([idx_rr], [sig[idx_rr]], c='g')  
plt.show() 

TypeError Traceback (most recent   call last)
<ipython-input-1474-dcea2717b9f2> in <module>
----> 1 get_intervals('/home/yasaswini/hp2-notebooks/ecg_data   
/Recorded_Data_Patch_Simulator/TXT_Files   
/ECG_data_128Hz_Simulator_Patch_Normal_data.txt',128)

<ipython-input-1471-5a6b384defd1> in get_intervals(fname,sampling_rate)
22             if (R_peak == False):
23                 sig = i
---> 24                 sig[k] = np.array(sig_txt[i])
25                 k = k + 1
26 

TypeError: 'int' object does not support item assignment
3
  • 1
    idx_rr = i+1 is assigning the index after 32767 to idx_rr. Commented Nov 22, 2019 at 7:44
  • I think that there are a lot of errors in this program, so you should not expect any of the answers below to fix all of them, just the error you quote. Commented Nov 22, 2019 at 7:55
  • Please update your question with the full traceback of the error. Commented Nov 22, 2019 at 7:58

2 Answers 2

1

With idx_rr = i+1 the formerly defined list becomes an int so there's no index idx_rr[j] to assign a value to. The same applies to the sig variable.

Note that the problem is not the declaration, python allows you to re-assign a variable to a new type. The problem is that idx_rr is of type int and you cannot access and index idx_rr[j] of an int.

Try this

R_peak = False   
j = 0
k = 0
idx_rr = []
idx_rr_int = 0
sig = []
sig_int = 0
for i in range (len(sig_txt)):
    if (sig_txt[i] == 32767):
        idx_rr_int = i+1
        idx_rr[j] = np.array(sig_txt[i+1])
        sig_int = i+1
        sig[k] = np.array(sig_txt[i+1])
        k = k + 1
        j = j + 1
        print(idx_rr_int)
        R_peak = True
    else:
        if (R_peak == False):
            sig_int = i
            sig[k] = np.array(sig_txt[i])
            k = k + 1

        else:
            R_peak = False

plt.figure(figsize=(20,8))
plt.plot(sig)
plt.scatter([idx_rr], [sig[idx_rr]], c='g')  
plt.show() 
Sign up to request clarification or add additional context in comments.

3 Comments

I should think that the quoted error actually occurs first just after ‘sig = i’ for the same reason.
I included idx_rr = i+1 because when I print idx_rr, I need to get the sig_txt index which is after the value 32767. I removed the list declaration of idx_rr and sig_txt but the error still remains.
@Jasmin, then use a different variable for the idx_rr = i+1 assignment... and sig=i+1 of course
0

After the line idx__rr = i+1, you are changing the data type of idx_rr, which was list previously, to int.

Change the variable name of the list to get rid of the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.