0

So I'm trying to share data between two threads in a continuous way. The program should keep running until asked to stop. The problem I have is with the queue. Normally it should be inside the while loop in the get_ratio function. But when I put it there I get the error: 'numpy.complex128' object has no attribute 'get'. Also when I put it before the while loop it treats only part of the data because it's before the loop. I don't know how to keep getting data from the queue. Any ideas?

import time
import random
import threading as th
import Queue
import numpy as np
import matplotlib.pyplot as plt
import bluetooth
import scipy.fftpack
from MindwaveDataPoints import RawDataPoint
from MindwaveDataPointReader import MindwaveDataPointReader


def get_data(q):
    mindwaveDataPointReader = MindwaveDataPointReader()
    mindwaveDataPointReader.start()

    data=[]
    while(1):


        dataPoint = mindwaveDataPointReader.readNextDataPoint()

        if (dataPoint.__class__ is RawDataPoint):
            data.append(dataPoint)
            q.put(data) #data keeps going into the queue







def get_ratio(q):

    M = [[],[],[],[],[],[],[],[]]
    fft_avg = []
    fft_avg_mod = []
    #loop to cover all the data
    j=0
    k=0
    l=0
    data = q.get() #HERE 

    while(1):
        #data = q.get() #HERE
        if k > 7 :
            fft_avg[:] = []
            fft_avg_mod[:] = []
            fft_avg = [(x+y+z+t+u+v+w+q)/8 for x,y,z,t,u,v,w,q in zip(M[0],M[1],M[2],M[3],M[4],M[5],M[6],M[7])]
            fft_avg_mod = [ abs(x) for x in fft_avg]


        if fft_avg_mod:
            d=random.randint(8,14)
            e=random.randint(14,20)
            alpha=fft_avg_mod[d]
            beta=fft_avg_mod[e]
            ratio=beta/alpha
            print 'ratio' , ratio
        f=k%8
        M[f] = np.fft.fft(data[j:j+32])
        j = j + 32  
        k = k + 1


if __name__ == '__main__':

    q = Queue.Queue(maxsize=0)

    try:
       threadLock = th.Lock()
       threads=[]
       thread1=th.Thread( target=get_data, args=(q,))
       thread2=th.Thread( target=get_ratio, args=(q,))
       thread1.start()
       print 'T1 started'
       time.sleep(10)
       thread2.start()
       print 'T2 started'
       threads.append(thread1)
       threads.append(thread2)
       for t in threads:
            t.join()
    except:
       print "Error: unable to start thread"
2
  • You put the list data which is growing to q repeatedly. Is this what you want? Why don't you just put dataPoint into q? Commented Aug 15, 2018 at 10:34
  • I tried but gave the error 'RawDataPoint instance has no attribute getitem ' so I added the attribute and then got 'int object has no attribute getitem ' and don't know how to deal with that Commented Aug 15, 2018 at 11:26

2 Answers 2

2

This has nothing to do with threads or sharing data. You are overwriting the data in variable q:

while(1):
    data = q.get()

and then:

        fft_avg = [(x+y+z+t+u+v+w+q)/8 for x,y,z,t,u,v,w,q in zip(M[0],M[1],M[2],M[3],M[4],M[5],M[6],M[7])]

The last line assigns a different value to q in the list comprehension, so you get an error the next time q.get() is called.


Advice 1: As a general rule, whenever you get an error of type XXX object has no attribute YYY, it is probably the case that your variable has completely wrong data (i.e. not the type you expect).

Advice 2: don't be lazy and use variable names like x, y, z, p, q, r, a, b, c. Give them real names. You will have less errors.

Sign up to request clarification or add additional context in comments.

1 Comment

it solved the issue but I now get the error ' Invalid number of FFT data points (0) specified'. I don't know why since I'm always extracting data from the queue which is always getting data from the first thread
0

You are using q inside the list comprehension, and therefore overwriting the queue q. Better use speaking variable names, so that such errors are less likely.

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.