示例#1
0
class HistoryPlotter:
    def __init__(self,saved_filename):
        self.plotter = RealTimePlotter()
        self.plotter.fig.suptitle('History data', fontsize='14', fontweight='bold')
        self.plotter.fig.subplots_adjust(bottom=0.23,hspace=0.5)
        self.index = 0
        self.saved_filename = saved_filename
        self.data = []
        self.prev_axis = plt.axes([0.395, 0.03, 0.1, 0.06])
        self.next_axis = plt.axes([0.505, 0.03, 0.1, 0.06])
        self.btn_next = Button(self.next_axis, 'Next')
        self.btn_next.on_clicked(self.next)
        self.btn_prev = Button(self.prev_axis, 'Previous')
        self.btn_prev.on_clicked(self.prev)
        self.slider_axis = plt.axes([0.25, 0.11, 0.5, 0.03])
        self.read_data()
        self.slider = Slider(self.slider_axis, 'chunk',0,len(self.data)-1, valinit=0,valfmt='%10.0f')
        self.slider.on_changed(self.update)

    def read_data(self):
        with bz2.BZ2File(self.saved_filename,'r') as f:
            self.data = pickle.load(f)
            print('Number of chunks',len(self.data) )
        

    def loop(self):
        try:
            self.plotter.show()
            self.plotter.plot(self.data[self.index]['buffer'],self.data[self.index]['stats'])
            plt.show(block=True)
        except KeyboardInterrupt:
            print("Stopping...")

    def update(self,val):
        self.index=int(val)
        self.plotter.plot(self.data[self.index]['buffer'],self.data[self.index]['stats'])

    def next(self,event):
        if(self.index < len(self.data)-1):
            self.index +=1
            self.plotter.plot(self.data[self.index]['buffer'],self.data[self.index]['stats'])
            self.slider.set_val(self.index)

    def prev(self,event):
        if(self.index >0):
            self.index -=1
            self.plotter.plot(self.data[self.index]['buffer'],self.data[self.index]['stats'])
            self.slider.set_val(self.index)
示例#2
0
 def __init__(self,saved_filename):
     self.plotter = RealTimePlotter()
     self.plotter.fig.suptitle('History data', fontsize='14', fontweight='bold')
     self.plotter.fig.subplots_adjust(bottom=0.23,hspace=0.5)
     self.index = 0
     self.saved_filename = saved_filename
     self.data = []
     self.prev_axis = plt.axes([0.395, 0.03, 0.1, 0.06])
     self.next_axis = plt.axes([0.505, 0.03, 0.1, 0.06])
     self.btn_next = Button(self.next_axis, 'Next')
     self.btn_next.on_clicked(self.next)
     self.btn_prev = Button(self.prev_axis, 'Previous')
     self.btn_prev.on_clicked(self.prev)
     self.slider_axis = plt.axes([0.25, 0.11, 0.5, 0.03])
     self.read_data()
     self.slider = Slider(self.slider_axis, 'chunk',0,len(self.data)-1, valinit=0,valfmt='%10.0f')
     self.slider.on_changed(self.update)
 def __init__(self, fs, buffer_size, filename, questions=[], port="COM3"):
     self.serialBuffer = SerialBuffer(port, buffer_size)
     self.serialBuffer.callbacks.append(self.callback)
     self.filename = filename
     self.questions = questions
     self.current_question = 0
     self.running = False
     self.finished = False
     self.plotter = RealTimePlotter(fs)
     self.plotter.fig.suptitle('Interactive Data Acquitter',
                               fontsize='12',
                               fontweight='bold')
     self.plotter.fig.subplots_adjust(left=0.27)
     questions_axes = plt.axes([0, 0.9, 0.2, 0.8])
     questions_axes.get_xaxis().set_visible(False)
     questions_axes.get_yaxis().set_visible(False)
     self.next_axes = plt.axes([0, 0.05, 0.2, 0.10])
     if (len(self.questions) == 0):
         self.questions_text = questions_axes.text(0,
                                                   0,
                                                   "No question provided",
                                                   ha='left',
                                                   fontsize=11,
                                                   wrap=True)
     else:
         qtext = '\n'.join(wrap(self.questions[0].text, 40))
         self.questions_text = questions_axes.text(0,
                                                   0,
                                                   qtext,
                                                   ha='left',
                                                   fontsize=11,
                                                   wrap=True)
     self.btn_next = Button(self.next_axes, 'Continue')
     self.btn_next.on_clicked(self.next)
     for q in self.questions:
         q.currentSample = 0
from RealTimePlotter import RealTimePlotter
from SerialBuffer import SerialBuffer

if __name__ == '__main__':
    import sys
    sb = None
    if (len(sys.argv) <= 1):
        sb = SerialBuffer('COM3', 1024)
    else:
        sb = SerialBuffer('COM3', 1024, sys.argv[1])
    p = RealTimePlotter()
    sb.callbacks.append(p.plot)
    sb.start()
    p.show()
    sb.loop()
class InteractiveDataAcquitter:
    def __init__(self, fs, buffer_size, filename, questions=[], port="COM3"):
        self.serialBuffer = SerialBuffer(port, buffer_size)
        self.serialBuffer.callbacks.append(self.callback)
        self.filename = filename
        self.questions = questions
        self.current_question = 0
        self.running = False
        self.finished = False
        self.plotter = RealTimePlotter(fs)
        self.plotter.fig.suptitle('Interactive Data Acquitter',
                                  fontsize='12',
                                  fontweight='bold')
        self.plotter.fig.subplots_adjust(left=0.27)
        questions_axes = plt.axes([0, 0.9, 0.2, 0.8])
        questions_axes.get_xaxis().set_visible(False)
        questions_axes.get_yaxis().set_visible(False)
        self.next_axes = plt.axes([0, 0.05, 0.2, 0.10])
        if (len(self.questions) == 0):
            self.questions_text = questions_axes.text(0,
                                                      0,
                                                      "No question provided",
                                                      ha='left',
                                                      fontsize=11,
                                                      wrap=True)
        else:
            qtext = '\n'.join(wrap(self.questions[0].text, 40))
            self.questions_text = questions_axes.text(0,
                                                      0,
                                                      qtext,
                                                      ha='left',
                                                      fontsize=11,
                                                      wrap=True)
        self.btn_next = Button(self.next_axes, 'Continue')
        self.btn_next.on_clicked(self.next)
        for q in self.questions:
            q.currentSample = 0

    def start(self):
        self.serialBuffer.start()
        self.plotter.show()
        self.serialBuffer.loop()

    def next(self, event):
        if (not self.finished and not self.running):
            self.running = True
            qtext = '\n'.join(
                wrap(
                    self.questions[self.current_question].text +
                    ". Collecting data ...", 40))
            self.questions_text.set_text(qtext)
        if (self.finished and not self.running):
            self.serialBuffer.stop()
            plt.close()

    def callback(self, buffer):
        self.plotter.plot(buffer)
        if (self.running):
            q = self.questions[self.current_question]
            if q.data is None:
                q.data = buffer
            else:
                q.data = np.vstack((q.data, buffer))
            q.currentSample += 1
            if (q.currentSample >= q.numSamples):
                self.running = False
                self.current_question += 1
                if (self.current_question >= len(self.questions)):
                    self.current_question = 0
                    qtext = '\n'.join(
                        wrap(
                            "Test Finished! Data will be saved to {0}. Press continue to exit!"
                            .format(self.filename), 40))
                    self.questions_text.set_text(qtext)
                    self.finished = True
                    self.save_data()

                else:
                    qtext = '\n'.join(
                        wrap(self.questions[self.current_question].text, 40))
                    self.questions_text.set_text(qtext)

    def save_data(self):
        with bz2.BZ2File(self.filename, 'w') as f:
            pickle.dump(self.questions, f)
from RealTimePlotter import RealTimePlotter
from SerialBuffer import SerialBuffer

if __name__ == '__main__':
    import sys
    sb = None
    if (len(sys.argv) <= 1):
        sb = SerialBuffer('COM3', 1024)
    else:
        sb = SerialBuffer('COM3', 1024, sys.argv[1])
    p = RealTimePlotter(1000)
    sb.callbacks.append(p.plot)
    sb.start()
    p.show()
    sb.loop()