def show_graph_comparison_attention_data_mindwave_and_userfeedback(self,datetime_from, datetime_to):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from,datetime_to)

        attention_data = []
        userfeedback_data = []
        x = []
        index = 0
        while index < len(list_mindwave_data):
            try:
                datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
            except :
                datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

            try:
                datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
            except :
                datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

            if list_mindwave_data[index][5] == self.treshold_poorSignalMindwave:
                attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
            else:
                attention_data.append((datetime_from_mindwave,-1))
            try:
                avg_userfeedback = Userfeedback_processing.get_average_userfeedback_specific_period(datetime_from_mindwave,datetime_to_mindwave)
            except Exception.ListEmpty:
                avg_userfeedback = -1
            userfeedback_data.append((datetime_from_mindwave,avg_userfeedback))
            index += 1

        x = [date2num(date) for (date, value) in attention_data]
        y_attention = [value for (date, value) in attention_data]
        y_attention = List_operations.movingaverage(y_attention,10)
        y_userfeedback = [value for (date, value) in userfeedback_data]



        y_attention = List_operations.filter_list(y_attention)
        y_userfeedback = List_operations.filter_list(y_userfeedback)

        y_attention = List_operations.moving_median(y_attention,self.window_median)

        y_attention = List_operations.movingaverage(y_attention, self.window_other)


        fig = plt.figure("Comparison attention mindwave headset and user indicated concentration")

        graph = fig.add_subplot(111)

        # Plot the data as a red line with round markers
        graph.plot(x,y_attention,'r-o')
        graph.plot(x,y_userfeedback,'b-o')

        # Set the xtick locations to correspond to just the dates you entered.
        graph.set_xticks(x)

        # Set the xtick labels to correspond to just the dates you entered.
        graph.set_xticklabels(
                [date.strftime("%Y-%m-%d %H:%M:%S") for (date, value) in attention_data]
                )
    def generate_bar_chart_activities_manual_log(self,title,mindwave_data,manual_log):
        #mindwave plot data

        attention_data, x, y_attention, y_meditation = mindwave_data
        list_activities_color = []
        unique_activities = List_operations.get_1D_list_from_2D_doubles_removed(manual_log,2)
        list_unique_activities_color = self.barchartdata_converter.get_list_activities_color(unique_activities)



        # x wordt geconverteerd omdat bar integers verwacht, dus om bar en plot te combineren kan er beter met integers gewerkt worden.
        # Tenslotte worden achteraf de labels van de bars er manueel opgezet.
        x_converted = []
        start_Datetime_x_converted = attention_data[0][0]

        for i in range(0,len(attention_data)):
            value = attention_data[i][0] - start_Datetime_x_converted
            x_converted.append(int(value.seconds))
        """
        for i in range(0,len(x)):
            x_converted.append(i)
        """

        # barchart data RescueTime
        #prepared_array,list_activities_color,list_dates = self.prepare_bar_chart_activities(session_id)

        # voorbereiding graph
        fig = plt.figure(title)
        ax = fig.gca()
        N = len(manual_log)
        ind =  np.zeros(N)

        index_ind = 0
        index_l = 0

        for index_m in range(0,len(attention_data)):
            try:
                datetime_from_attention_data = datetime.datetime(attention_data[index_m][0].year,attention_data[index_m][0].month,attention_data[index_m][0].day,attention_data[index_m][0].hour,attention_data[index_m][0].minute,attention_data[index_m][0].second)
            except Exception,e:
                datetime_from_attention_data =  attention_data[index_m][0]

            if datetime_from_attention_data >= manual_log[index_l][0] and datetime_from_attention_data < manual_log[index_l][1]:
                ind[index_ind] = x_converted[index_m]
                index_ind += 1
                index_l += 1
            else:
                if index_l < len(manual_log) - 1:
                    if datetime_from_attention_data >= manual_log[index_l + 1][0] and datetime_from_attention_data < manual_log[index_l + 1][1]:
                        #index_l += 1
                        manual_log = List_operations.delete_entry(manual_log,index_l)
                        N -= 1
                        ind = np.delete(ind,N - 1)
                        ind[index_ind] = x_converted[index_m]
                        index_ind += 1
                        index_l += 1

            if index_ind >= len(ind) or index_l >= len(manual_log):
                break
 def test_delete_entry(self):
     a = [5,8,9,6,4,5,1,2,3]
     a_deleted = List_operations.delete_entry(a,3)
     self.assertEqual(len(a) - 1,len(a_deleted))
     self.assertEqual(a[0],a_deleted[0])
     self.assertEqual(a[1],a_deleted[1])
     self.assertEqual(a[2],a_deleted[2])
     self.assertEqual(a[4],a_deleted[3])
     self.assertEqual(a[5],a_deleted[4])
     self.assertEqual(a[6],a_deleted[5])
     self.assertEqual(a[7],a_deleted[6])
     self.assertEqual(a[8],a_deleted[7])
    def get_mindwave_data_smoothened(self,datetime_from, datetime_to):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
        attention_data = []
        meditation_data = []

        for item in list_mindwave_data:
            attention_data.append(item[3])
            meditation_data.append(item[4])
        attention_data = List_operations.filter_list(attention_data)
        meditation_data = List_operations.filter_list(meditation_data)

        attention_data = List_operations.moving_median(attention_data,self.window_median)
        meditation_data = List_operations.moving_median(meditation_data,self.window_median)

        attention_data = List_operations.movingaverage(attention_data, self.window_other)
        meditation_data = List_operations.movingaverage(meditation_data, self.window_other)
        list_mindwave_data_modified = []
        for i in range(0,len(list_mindwave_data)):
            row = []
            row.append(list_mindwave_data[i][0])
            row.append(list_mindwave_data[i][1])
            row.append(list_mindwave_data[i][2])
            row.append(attention_data[i])
            row.append(meditation_data[i])
            row.append(list_mindwave_data[i][5])
            row.append(list_mindwave_data[i][6])

            list_mindwave_data_modified.append(row)

        return list_mindwave_data_modified
    def get_mindwave_data(self, datetime_from, datetime_to,window_moving_average):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
        attention_data = []
        meditation_data = []

        x = []
        index = 0
        while index < len(list_mindwave_data):
            try:
                datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

            if list_mindwave_data[index][5] == self.treshold_poorSignalMindwave:
                attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
                meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))
            else:
                attention_data.append((datetime_from_mindwave,-1))
                meditation_data.append((datetime_from_mindwave,-1))

            index += 1
        x = [date2num(date) for (date, value) in attention_data]
        y_attention = [value for (date, value) in attention_data]
        y_meditation = [value for (date, value) in meditation_data]

        y_attention = List_operations.filter_list(y_attention)
        y_meditation = List_operations.filter_list(y_meditation)

        y_attention = List_operations.moving_median(y_attention,self.window_median)
        y_meditation = List_operations.moving_median(y_meditation,self.window_median)

        y_attention = List_operations.movingaverage(y_attention, window_moving_average)
        y_meditation = List_operations.movingaverage(y_meditation, window_moving_average)

        return attention_data, x, y_attention, y_meditation
    def get_mindwave_data_smoothened(self, datetime_from, datetime_to):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
            datetime_from, datetime_to)
        attention_data = []
        meditation_data = []

        for item in list_mindwave_data:
            attention_data.append(item[3])
            meditation_data.append(item[4])
        attention_data = List_operations.filter_list(attention_data)
        meditation_data = List_operations.filter_list(meditation_data)

        attention_data = List_operations.moving_median(attention_data,
                                                       self.window_median)
        meditation_data = List_operations.moving_median(
            meditation_data, self.window_median)

        attention_data = List_operations.movingaverage(attention_data,
                                                       self.window_other)
        meditation_data = List_operations.movingaverage(
            meditation_data, self.window_other)
        list_mindwave_data_modified = []
        for i in range(0, len(list_mindwave_data)):
            row = []
            row.append(list_mindwave_data[i][0])
            row.append(list_mindwave_data[i][1])
            row.append(list_mindwave_data[i][2])
            row.append(attention_data[i])
            row.append(meditation_data[i])
            row.append(list_mindwave_data[i][5])
            row.append(list_mindwave_data[i][6])

            list_mindwave_data_modified.append(row)

        return list_mindwave_data_modified
    def show_graph_attention_data_mindwave(self,datetime_from, datetime_to):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from,datetime_to)
        attention_data = []
        meditation_data = []
        x = []
        index = 0
        while index < len(list_mindwave_data):

            try:
                datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
            except :
                datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

            try:
                datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
            except :
                datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

            if list_mindwave_data[index][5] <= self.treshold_poorSignalMindwave:
                attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
                meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))
            else:
                attention_data.append((datetime_from_mindwave,-1))
                meditation_data.append((datetime_from_mindwave,-1))
            #meditation_data.append((list_mindwave_data[index][1], list_mindwave_data[index][4]))
            index += 1

        x = [date2num(date) for (date, value) in attention_data]
        y_attention = [value for (date, value) in attention_data]
        y_meditation = [value for (date, value) in meditation_data]

        y_attention = List_operations.filter_list(y_attention)
        y_meditation = List_operations.filter_list(y_meditation)


        y_attention = List_operations.moving_median(y_attention,self.window_median)
        y_meditation = List_operations.moving_median(y_meditation,self.window_median)



        y_attention = List_operations.movingaverage(y_attention, self.window_other)
        y_meditation = List_operations.movingaverage(y_meditation, self.window_other)


        fig = plt.figure("Line chart data mindwave headset")

        graph = fig.add_subplot(111)

        # Plot the data as a red line with round markers
        graph.plot(x,y_attention,'r-o')
        graph.plot(x,y_meditation,'y-o')

        # Set the xtick locations to correspond to just the dates you entered.
        graph.set_xticks(x)

        # Set the xtick labels to correspond to just the dates you entered.
        graph.set_xticklabels(
                [date.strftime("%Y-%m-%d %H:%M:%S") for (date, value) in attention_data]
                )
    def test_moving_median_mindwave_array_3(self):
        mindwaveArray = [0.20,0.10,0.30,0.80,0.40,0.21,0.54,0.25,0.45,0.90]
        result = List_operations.moving_median_middle(mindwaveArray,3)

        self.assertEqual(round(result[0],2),round(0.15,2))
        self.assertEqual(round(result[1],2),round(0.20,2))
        self.assertEqual(round(result[2],2),round(0.3,2))
        self.assertEqual(round(result[3],2),round(0.4,2))
        self.assertEqual(round(result[4],2),round(0.4,2))
        self.assertEqual(round(result[5],2),round(0.4,2))
        self.assertEqual(round(result[6],2),round(0.25,2))
        self.assertEqual(round(result[7],2),round(0.45,2))
        self.assertEqual(round(result[8],2),round(0.45,2))
        self.assertEqual(round(result[9],2),round(0.68,2))
    def analyse_list_for_reading_patterns(self,x_coordinates_iris,datetime_array,title):
        filtered_list = self.filter_list(x_coordinates_iris)
        #filtered_list = list_operations.moving_median(filtered_list,self.median_window)

        if self.threshold_window_selection == -1:
            smoothened_list = self.smooth_list(filtered_list)
        else:
            smoothened_list = List_operations.movingaverage(filtered_list,self.threshold_window_selection)

        dataset = self.analyse_list_for_reading_patterns_smooth_list(smoothened_list,datetime_array)

        try:
            self.show_plot(datetime_array,filtered_list,smoothened_list,title)
        except Exception, e:
            print str(e)
def get_mindwave_data_filtered_smoothed_middle_moving_median(
        datetime_from, datetime_to, window_moving_average):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    mindwave_data_result = []
    list_dates = []
    attention_data = []
    meditation_data = []
    x = []
    index = 0
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")
        list_dates.append([datetime_from_mindwave, datetime_to_mindwave])

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append(list_mindwave_data[index][3])
            meditation_data.append(list_mindwave_data[index][4])
        else:
            attention_data.append(-1)
            meditation_data.append(-1)

        index += 1

    attention_data = List_operations.filter_list(attention_data)
    meditation_data = List_operations.filter_list(meditation_data)

    attention_data = List_operations.moving_median_middle(
        attention_data, window_median)
    meditation_data = List_operations.moving_median_middle(
        meditation_data, window_median)

    attention_data = List_operations.movingaverage(attention_data,
                                                   window_moving_average)
    meditation_data = List_operations.movingaverage(meditation_data,
                                                    window_moving_average)

    for i in range(0, len(attention_data)):
        mindwave_data_result.append([
            list_dates[i][0], list_dates[i][1], attention_data[i],
            meditation_data[i]
        ])

    return mindwave_data_result
    def test_get_1D_list_from_2D_doubles_removed(self):
        list = []

        list.append([None,None,"Peter"])
        list.append([None,None,"Peter"])
        list.append([None,None,"AAA"])
        list.append([None,None,"AA"])
        list.append([None,None,"AB"])
        list.append([None,None,"AC"])
        list.append([None,None,"AAA"])
        result = List_operations.get_1D_list_from_2D_doubles_removed(list,2)
        self.assertEqual(len(result),5)
        self.assertEqual(result[0],"Peter")
        self.assertEqual(result[1],"AAA")
        self.assertEqual(result[2],"AA")
        self.assertEqual(result[3],"AB")
        self.assertEqual(result[4],"AC")
def get_mindwave_data(datetime_from, datetime_to, window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    attention_data = []
    meditation_data = []

    x = []
    index = 0
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

        attention_data.append(
            (datetime_from_mindwave, list_mindwave_data[index][3]))
        meditation_data.append(
            (datetime_from_mindwave, list_mindwave_data[index][4]))

        index += 1
    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention, window_median)
    y_meditation = List_operations.moving_median(y_meditation, window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    return attention_data, x, y_attention, y_meditation
    def analyse_list_for_reading_patterns_2(self,x_coordinates_iris,datetime_array,title):
        filtered_list = self.filter_list(x_coordinates_iris)
        #filtered_list = list_operations.moving_median(filtered_list,self.median_window)

        if self.threshold_window_selection == -1:
            smoothened_list = self.smooth_list(filtered_list)
        else:
            smoothened_list = List_operations.movingaverage(filtered_list,self.threshold_window_selection)

        dataset = self.analyse_list_for_reading_patterns_smooth_list_2(smoothened_list,datetime_array)
        """
        try:
            self.show_plot(datetime_array,filtered_list,smoothened_list,title)
        except Exception, e:
            print str(e)
        """
        for item in dataset:
            self.dareading.insert_row_reading(item[0],item[1],item[2],item[3])
        return dataset
def get_mindwave_data( datetime_from, datetime_to,window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
    attention_data = []
    meditation_data = []

    x = []
    index = 0
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

        attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
        meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))

        index += 1
    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention,window_median)
    y_meditation = List_operations.moving_median(y_meditation,window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    return attention_data, x, y_attention, y_meditation
    def get_mindwave_data(self, datetime_from, datetime_to,
                          window_moving_average):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
            datetime_from, datetime_to)
        attention_data = []
        meditation_data = []

        x = []
        index = 0
        while index < len(list_mindwave_data):
            try:
                datetime_from_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_from_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

            if list_mindwave_data[index][
                    5] == self.treshold_poorSignalMindwave:
                attention_data.append(
                    (datetime_from_mindwave, list_mindwave_data[index][3]))
                meditation_data.append(
                    (datetime_from_mindwave, list_mindwave_data[index][4]))
            else:
                attention_data.append((datetime_from_mindwave, -1))
                meditation_data.append((datetime_from_mindwave, -1))

            index += 1
        x = [date2num(date) for (date, value) in attention_data]
        y_attention = [value for (date, value) in attention_data]
        y_meditation = [value for (date, value) in meditation_data]

        y_attention = List_operations.filter_list(y_attention)
        y_meditation = List_operations.filter_list(y_meditation)

        y_attention = List_operations.moving_median(y_attention,
                                                    self.window_median)
        y_meditation = List_operations.moving_median(y_meditation,
                                                     self.window_median)

        y_attention = List_operations.movingaverage(y_attention,
                                                    window_moving_average)
        y_meditation = List_operations.movingaverage(y_meditation,
                                                     window_moving_average)

        return attention_data, x, y_attention, y_meditation
def get_mindwave_data_filtered_smoothed_middle_moving_median( datetime_from, datetime_to,window_moving_average):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
    mindwave_data_result = []
    list_dates = []
    attention_data = []
    meditation_data = []
    x = []
    index = 0
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")
        list_dates.append([datetime_from_mindwave,datetime_to_mindwave])

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append(list_mindwave_data[index][3])
            meditation_data.append(list_mindwave_data[index][4])
        else:
            attention_data.append(-1)
            meditation_data.append(-1)

        index += 1

    attention_data = List_operations.filter_list(attention_data)
    meditation_data = List_operations.filter_list(meditation_data)

    attention_data = List_operations.moving_median_middle(attention_data,window_median)
    meditation_data = List_operations.moving_median_middle(meditation_data,window_median)

    attention_data = List_operations.movingaverage(attention_data, window_moving_average)
    meditation_data = List_operations.movingaverage(meditation_data, window_moving_average)

    for i in range(0,len(attention_data)):
        mindwave_data_result.append([list_dates[i][0],list_dates[i][1],attention_data[i],meditation_data[i]])

    return mindwave_data_result
def get_mindwave_userfeedback_data(datetime_from, datetime_to, window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    userfeedback_data = []
    x = []
    index = 0
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][3]))
            meditation_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave, -1))
            meditation_data.append((datetime_from_mindwave, -1))

        try:
            avg_userfeedback = Userfeedback_processing.get_average_userfeedback_specific_period(
                datetime_from_mindwave, datetime_to_mindwave)
        except Exception.ListEmpty:
            avg_userfeedback = -1
        userfeedback_data.append((datetime_from_mindwave, avg_userfeedback))
        #meditation_data.append((list_mindwave_data[index][1], list_mindwave_data[index][4]))
        index += 1

    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention, window_median)
    y_meditation = List_operations.moving_median(y_meditation, window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    y_userfeedback = List_operations.filter_list(
        [value for (date, value) in userfeedback_data])

    return attention_data, x, y_attention, y_meditation, y_userfeedback
    def show_graph_attention_data_mindwave(self, datetime_from, datetime_to):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
            datetime_from, datetime_to)
        attention_data = []
        meditation_data = []
        x = []
        index = 0
        while index < len(list_mindwave_data):

            try:
                datetime_from_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_from_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

            try:
                datetime_to_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_to_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

            if list_mindwave_data[index][5] <= self.treshold_poorSignalMindwave:
                attention_data.append(
                    (datetime_from_mindwave, list_mindwave_data[index][3]))
                meditation_data.append(
                    (datetime_from_mindwave, list_mindwave_data[index][4]))
            else:
                attention_data.append((datetime_from_mindwave, -1))
                meditation_data.append((datetime_from_mindwave, -1))
            #meditation_data.append((list_mindwave_data[index][1], list_mindwave_data[index][4]))
            index += 1

        x = [date2num(date) for (date, value) in attention_data]
        y_attention = [value for (date, value) in attention_data]
        y_meditation = [value for (date, value) in meditation_data]

        y_attention = List_operations.filter_list(y_attention)
        y_meditation = List_operations.filter_list(y_meditation)

        y_attention = List_operations.moving_median(y_attention,
                                                    self.window_median)
        y_meditation = List_operations.moving_median(y_meditation,
                                                     self.window_median)

        y_attention = List_operations.movingaverage(y_attention,
                                                    self.window_other)
        y_meditation = List_operations.movingaverage(y_meditation,
                                                     self.window_other)

        fig = plt.figure("Line chart data mindwave headset")

        graph = fig.add_subplot(111)

        # Plot the data as a red line with round markers
        graph.plot(x, y_attention, 'r-o')
        graph.plot(x, y_meditation, 'y-o')

        # Set the xtick locations to correspond to just the dates you entered.
        graph.set_xticks(x)

        # Set the xtick labels to correspond to just the dates you entered.
        graph.set_xticklabels([
            date.strftime("%Y-%m-%d %H:%M:%S")
            for (date, value) in attention_data
        ])
    def show_graph_comparison_attention_data_mindwave_and_userfeedback(
            self, datetime_from, datetime_to):
        list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
            datetime_from, datetime_to)

        attention_data = []
        userfeedback_data = []
        x = []
        index = 0
        while index < len(list_mindwave_data):
            try:
                datetime_from_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_from_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")

            try:
                datetime_to_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_to_mindwave = datetime.datetime.strptime(
                    list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

            if list_mindwave_data[index][
                    5] == self.treshold_poorSignalMindwave:
                attention_data.append(
                    (datetime_from_mindwave, list_mindwave_data[index][3]))
            else:
                attention_data.append((datetime_from_mindwave, -1))
            try:
                avg_userfeedback = Userfeedback_processing.get_average_userfeedback_specific_period(
                    datetime_from_mindwave, datetime_to_mindwave)
            except Exception.ListEmpty:
                avg_userfeedback = -1
            userfeedback_data.append(
                (datetime_from_mindwave, avg_userfeedback))
            index += 1

        x = [date2num(date) for (date, value) in attention_data]
        y_attention = [value for (date, value) in attention_data]
        y_attention = List_operations.movingaverage(y_attention, 10)
        y_userfeedback = [value for (date, value) in userfeedback_data]

        y_attention = List_operations.filter_list(y_attention)
        y_userfeedback = List_operations.filter_list(y_userfeedback)

        y_attention = List_operations.moving_median(y_attention,
                                                    self.window_median)

        y_attention = List_operations.movingaverage(y_attention,
                                                    self.window_other)

        fig = plt.figure(
            "Comparison attention mindwave headset and user indicated concentration"
        )

        graph = fig.add_subplot(111)

        # Plot the data as a red line with round markers
        graph.plot(x, y_attention, 'r-o')
        graph.plot(x, y_userfeedback, 'b-o')

        # Set the xtick locations to correspond to just the dates you entered.
        graph.set_xticks(x)

        # Set the xtick labels to correspond to just the dates you entered.
        graph.set_xticklabels([
            date.strftime("%Y-%m-%d %H:%M:%S")
            for (date, value) in attention_data
        ])
 def test_statistics(self):
     input_list = [1,3,3,4,5,7,9,10,11,15,16]
     window_size = 4
     output_list = List_operations.movingaverage(input_list,window_size)
     print output_list
    def generate_bar_chart_activities_manual_log(self, title, mindwave_data,
                                                 manual_log):
        #mindwave plot data

        attention_data, x, y_attention, y_meditation = mindwave_data
        list_activities_color = []
        unique_activities = List_operations.get_1D_list_from_2D_doubles_removed(
            manual_log, 2)
        list_unique_activities_color = self.barchartdata_converter.get_list_activities_color(
            unique_activities)

        # x wordt geconverteerd omdat bar integers verwacht, dus om bar en plot te combineren kan er beter met integers gewerkt worden.
        # Tenslotte worden achteraf de labels van de bars er manueel opgezet.
        x_converted = []
        start_Datetime_x_converted = attention_data[0][0]

        for i in range(0, len(attention_data)):
            value = attention_data[i][0] - start_Datetime_x_converted
            x_converted.append(int(value.seconds))
        """
        for i in range(0,len(x)):
            x_converted.append(i)
        """

        # barchart data RescueTime
        #prepared_array,list_activities_color,list_dates = self.prepare_bar_chart_activities(session_id)

        # voorbereiding graph
        fig = plt.figure(title)
        ax = fig.gca()
        N = len(manual_log)
        ind = np.zeros(N)

        index_ind = 0
        index_l = 0

        for index_m in range(0, len(attention_data)):
            try:
                datetime_from_attention_data = datetime.datetime(
                    attention_data[index_m][0].year,
                    attention_data[index_m][0].month,
                    attention_data[index_m][0].day,
                    attention_data[index_m][0].hour,
                    attention_data[index_m][0].minute,
                    attention_data[index_m][0].second)
            except Exception, e:
                datetime_from_attention_data = attention_data[index_m][0]

            if datetime_from_attention_data >= manual_log[index_l][
                    0] and datetime_from_attention_data < manual_log[index_l][
                        1]:
                ind[index_ind] = x_converted[index_m]
                index_ind += 1
                index_l += 1
            else:
                if index_l < len(manual_log) - 1:
                    if datetime_from_attention_data >= manual_log[index_l + 1][
                            0] and datetime_from_attention_data < manual_log[
                                index_l + 1][1]:
                        #index_l += 1
                        manual_log = List_operations.delete_entry(
                            manual_log, index_l)
                        N -= 1
                        ind = np.delete(ind, N - 1)
                        ind[index_ind] = x_converted[index_m]
                        index_ind += 1
                        index_l += 1

            if index_ind >= len(ind) or index_l >= len(manual_log):
                break
def get_mindwave_userfeedback_data( datetime_from, datetime_to,window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    userfeedback_data = []
    x = []
    index = 0
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except :
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
            meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave,-1))
            meditation_data.append((datetime_from_mindwave,-1))

        try:
            avg_userfeedback = Userfeedback_processing.get_average_userfeedback_specific_period(datetime_from_mindwave,datetime_to_mindwave)
        except Exception.ListEmpty:
            avg_userfeedback = -1
        userfeedback_data.append((datetime_from_mindwave,avg_userfeedback))
        #meditation_data.append((list_mindwave_data[index][1], list_mindwave_data[index][4]))
        index += 1

    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention,window_median)
    y_meditation = List_operations.moving_median(y_meditation,window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    y_userfeedback = List_operations.filter_list([value for (date, value) in userfeedback_data])

    return attention_data, x, y_attention, y_meditation,y_userfeedback
    def generate_bar_chart_activities_attention_only(self, prepared_array,
                                                     list_activities_color,
                                                     list_dates, title,
                                                     mindwave_data):
        #mindwave plot data

        attention_data, x, y_attention, y_meditation = mindwave_data

        # x wordt geconverteerd omdat bar integers verwacht, dus om bar en plot te combineren kan er beter met integers gewerkt worden.
        # Tenslotte worden achteraf de labels van de bars er manueel opgezet.
        x_converted = []
        start_Datetime_x_converted = attention_data[0][0]

        for i in range(0, len(attention_data)):
            value = attention_data[i][0] - start_Datetime_x_converted
            x_converted.append(int(value.seconds))
        """
        for i in range(0,len(x)):
            x_converted.append(i)
        """
        # barchart data RescueTime
        #prepared_array,list_activities_color,list_dates = self.prepare_bar_chart_activities(session_id)

        # voorbereiding graph
        fig = plt.figure(title)
        ax = fig.gca()
        N = len(prepared_array[0])
        ind = np.zeros(N)
        ind[0] = 1
        width = np.zeros(N)
        index_ind = 0
        index_width = 0
        for date in list_dates:

            if index_ind > 0:
                start_datetime = datetime.datetime.strptime(
                    date[0], "%Y-%m-%d %H:%M:%S")
                difference = start_datetime - start_Datetime_x_converted
                ind[index_ind] = difference.seconds

                width[index_width] = ind[index_ind] - ind[index_ind - 1]
                index_width += 1
            index_ind += 1

        start_datetime = datetime.datetime.strptime(
            list_dates[len(list_dates) - 1][0], "%Y-%m-%d %H:%M:%S")
        end_datetime = start_datetime + datetime.timedelta(minutes=5)
        difference = end_datetime - start_datetime

        width[len(width) - 1] = difference.seconds

        # calculate std
        attention_std = []
        #meditation_std = []
        for i in range(1, len(ind)):
            attention_std.append(
                List_operations.calculate_std(
                    y_attention[int(ind[i - 1]):int(ind[i])]))
        #   meditation_std.append(list_operations.calculate_std(y_meditation[ind[i - 1] : ind[i]]))
        attention_std.append(
            List_operations.calculate_std(
                y_attention[int(ind[N -
                                    1]):x_converted[int(len(x_converted) -
                                                        1)]]))

        # p_array opvullen
        p_array = []
        p_array.append(
            ax.bar(ind,
                   prepared_array[0],
                   width,
                   color=list_activities_color[0][1]))
        bottom_array = prepared_array[0]
        for i in range(1, len(prepared_array)):
            if i == len(prepared_array) - 1:
                p_array.append(
                    ax.bar(ind,
                           prepared_array[i],
                           width,
                           color=list_activities_color[i][1],
                           yerr=attention_std,
                           bottom=bottom_array))
            else:
                p_array.append(
                    ax.bar(ind,
                           prepared_array[i],
                           width,
                           color=list_activities_color[i][1],
                           bottom=bottom_array))
            bottom_array = self.sum_array(bottom_array, prepared_array[i])

        x_labels = []
        for item in list_dates:
            datetime_from = datetime.datetime.strptime(item[0],
                                                       "%Y-%m-%d %H:%M:%S")
            datetime_to = datetime_from + datetime.timedelta(seconds=300)

            str_time_from = str(datetime_from.hour) + ":" + str(
                datetime_from.minute) + ":" + str(datetime_from.second)
            str_time_to = str(datetime_to.hour) + ":" + str(
                datetime_to.minute) + ":" + str(datetime_to.second)

            x_labels.append(str(str_time_from + " - " + str_time_to))

        ax.plot(x_converted, y_attention, 'r-o')
        #ax.plot(x_converted,y_meditation,'y-o')
        legend_labels = []
        for item in list_activities_color:
            legend_labels.append(item[0][0])

        legend = []
        for item in p_array:
            legend.append(item[0])

        plt.ylabel('percentage')
        plt.xlabel('RescueTime intervals')
        plt.title(title)
        lenInd = len(ind)
        if lenInd > 10:
            plt.xticks(ind + width / 2., x_labels, rotation='vertical')
        else:
            plt.xticks(ind + width / 2., x_labels)

        plt.yticks(np.arange(0, 1.1, 0.1))
        figLegend = pylab.figure()
        figLegend.legend(legend, legend_labels)
        figLegend.show()
        figure = plt.gcf()  # get current figure
        figure.set_size_inches(40, 15)
        plt.savefig(str('test_images/' + title + ".png"))
def get_mindwave_userGone_data(datetime_from, datetime_to, window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    userGoneNotStudying = []
    userGoneStudying = []
    x = []
    index = 0
    listUserGone = DAUserGone_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][3]))
            meditation_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave, -1))
            meditation_data.append((datetime_from_mindwave, -1))

        canAddUserGoneNotStudyingEntry = False
        canAddUserGoneStudyingEntry = False
        for item in listUserGone:
            try:
                datetime_from_userGone = datetime.datetime.strptime(
                    item[1], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_from_userGone = datetime.datetime.strptime(
                    item[1], "%Y-%m-%d %H:%M:%S")

            try:
                datetime_to_userGone = datetime.datetime.strptime(
                    item[2], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_to_userGone = datetime.datetime.strptime(
                    item[2], "%Y-%m-%d %H:%M:%S")

            if datetime_from_mindwave >= datetime_from_userGone and datetime_to_mindwave <= datetime_to_userGone and item[
                    5] == 0:
                canAddUserGoneNotStudyingEntry = True

            if datetime_from_mindwave >= datetime_from_userGone and datetime_to_mindwave <= datetime_to_userGone and item[
                    5] == 1:
                canAddUserGoneStudyingEntry = True
        if canAddUserGoneNotStudyingEntry is True:
            userGoneNotStudying.append((datetime_from_mindwave, 1))
        else:
            userGoneNotStudying.append((datetime_from_mindwave, 0))

        if canAddUserGoneStudyingEntry is True:
            userGoneStudying.append((datetime_from_mindwave, 1))
        else:
            userGoneStudying.append((datetime_from_mindwave, 0))

        index += 1
    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention, window_median)
    y_meditation = List_operations.moving_median(y_meditation, window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    y_userGoneNotStudying = List_operations.filter_list(
        [value for (date, value) in userGoneNotStudying])
    y_userGoneStudying = List_operations.filter_list(
        [value for (date, value) in userGoneStudying])

    return attention_data, x, y_attention, y_meditation, y_userGoneNotStudying, y_userGoneStudying
def get_mindwave_manual_logged_activities_data(self, datetime_from,
                                               datetime_to, list_manual_logs,
                                               selected_activity, window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    selectedActivity = []
    userGoneStudying = []
    x = []
    index = 0
    listUserGone = DAUserGone_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

        if list_mindwave_data[index][5] == self.treshold_poorSignalMindwave:
            attention_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][3]))
            meditation_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave, -1))
            meditation_data.append((datetime_from_mindwave, -1))

        canAddMatchedActivity = False

        for item in list_manual_logs:
            datetime_from_log = item[0]
            datetime_to_log = item[1]

            if datetime_from_mindwave >= datetime_from_log and datetime_to_mindwave <= datetime_to_log and item[
                    2] == selected_activity:
                canAddMatchedActivity = True

        if canAddMatchedActivity is True:
            selectedActivity.append((datetime_from_mindwave, 1))
        else:
            selectedActivity.append((datetime_from_mindwave, 0))

        index += 1
    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention, window_median)
    y_meditation = List_operations.moving_median(y_meditation, window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"
    y_selectedActivity = List_operations.filter_list(
        [value for (date, value) in selectedActivity])

    return attention_data, x, y_attention, y_meditation, y_selectedActivity
    def generate_bar_chart_activities_attention_only(self,prepared_array,list_activities_color,list_dates,title,mindwave_data):
        #mindwave plot data

        attention_data, x, y_attention, y_meditation = mindwave_data

        # x wordt geconverteerd omdat bar integers verwacht, dus om bar en plot te combineren kan er beter met integers gewerkt worden.
        # Tenslotte worden achteraf de labels van de bars er manueel opgezet.
        x_converted = []
        start_Datetime_x_converted = attention_data[0][0]

        for i in range(0,len(attention_data)):
            value = attention_data[i][0] - start_Datetime_x_converted
            x_converted.append(int(value.seconds))
        """
        for i in range(0,len(x)):
            x_converted.append(i)
        """
        # barchart data RescueTime
        #prepared_array,list_activities_color,list_dates = self.prepare_bar_chart_activities(session_id)

        # voorbereiding graph
        fig = plt.figure(title)
        ax = fig.gca()
        N = len(prepared_array[0])
        ind =  np.zeros(N)
        ind[0] = 1
        width = np.zeros(N)
        index_ind = 0
        index_width = 0
        for date in list_dates:

            if index_ind >0:
                start_datetime = datetime.datetime.strptime(date[0], "%Y-%m-%d %H:%M:%S")
                difference = start_datetime - start_Datetime_x_converted
                ind[index_ind] = difference.seconds

                width[index_width] = ind[index_ind] - ind[index_ind - 1]
                index_width += 1
            index_ind += 1

        start_datetime = datetime.datetime.strptime(list_dates[len(list_dates) - 1][0], "%Y-%m-%d %H:%M:%S")
        end_datetime = start_datetime + datetime.timedelta(minutes=5)
        difference = end_datetime - start_datetime

        width[len(width) - 1] = difference.seconds

        # calculate std
        attention_std = []
        #meditation_std = []
        for i in range(1,len(ind)):
            attention_std.append(List_operations.calculate_std(y_attention[int(ind[i - 1]) : int(ind[i])]))
         #   meditation_std.append(list_operations.calculate_std(y_meditation[ind[i - 1] : ind[i]]))
        attention_std.append(List_operations.calculate_std(y_attention[int(ind[N - 1]) : x_converted[int(len(x_converted) - 1)]]))


        # p_array opvullen
        p_array = []
        p_array.append(ax.bar(ind,prepared_array[0],width,color=list_activities_color[0][1]))
        bottom_array = prepared_array[0]
        for i in range(1,len(prepared_array)):
            if i == len(prepared_array) - 1:
                p_array.append(ax.bar(ind,prepared_array[i],width,color=list_activities_color[i][1],yerr=attention_std,bottom = bottom_array))
            else:
                p_array.append(ax.bar(ind,prepared_array[i],width,color=list_activities_color[i][1],bottom = bottom_array))
            bottom_array = self.sum_array(bottom_array,prepared_array[i])

        x_labels = []
        for item in list_dates:
            datetime_from = datetime.datetime.strptime(item[0], "%Y-%m-%d %H:%M:%S")
            datetime_to =  datetime_from + datetime.timedelta(seconds=300)

            str_time_from = str(datetime_from.hour) + ":" + str(datetime_from.minute) + ":" + str(datetime_from.second)
            str_time_to = str(datetime_to.hour) + ":" + str(datetime_to.minute) + ":" + str(datetime_to.second)

            x_labels.append(str(str_time_from + " - " + str_time_to))


        ax.plot(x_converted,y_attention,'r-o')
        #ax.plot(x_converted,y_meditation,'y-o')
        legend_labels = []
        for item in list_activities_color:
            legend_labels.append(item[0][0])

        legend = []
        for item in p_array:
            legend.append(item[0])

        plt.ylabel('percentage')
        plt.xlabel('RescueTime intervals')
        plt.title(title)
        lenInd = len(ind)
        if lenInd > 10:
            plt.xticks(ind+width/2., x_labels,rotation='vertical' )
        else:
            plt.xticks(ind+width/2., x_labels)

        plt.yticks(np.arange(0,1.1,0.1))
        figLegend = pylab.figure()
        figLegend.legend( legend, legend_labels )
        figLegend.show()
        figure = plt.gcf() # get current figure
        figure.set_size_inches(40, 15)
        plt.savefig(str('test_images/' + title + ".png"))
    def smooth_list(self,list):
        derivative_list = self.calculate_derivative(list)
        list_copy = []
        for item in list:
            list_copy.append(item)
        index = 0
        teller_pos = 0
        teller_pos_acc = 0.0
        teller_pos_total = 0.0
        current_window = 0.0
        start_index = 0
        result = []
        while index < len(list):

            if derivative_list[index] > 0:
                teller_pos += 1
            else:
                if teller_pos > 0:
                    if teller_pos < self.threshold_window_accept_pos:
                        teller_pos = 0
                    else:
                        teller_pos_acc += teller_pos
                        teller_pos_total += 1
                        teller_pos = 0
                        # Eerst gemiddelde van teller_pos nemen over enkele momenten dat de functie stijgt, weer daalt en weer stijgt.
                        # Dan gebaseerd daarop nieuw window berekenen door die avg_pos te vermenigvuldigen met percentage
                        # Dan kijken of het new_window binnen de marges van verschil liggen tov oud window
                        # Zo niet, lijst meegeven aan movingaverage tot deze index met oud window, dan oud window op new window zetten en alles resetten.
                        if teller_pos_total > self.threshold_window_selection_total_pos:
                            avg_pos = teller_pos_acc / teller_pos_total
                            new_window = math.ceil(float(avg_pos * self.threshold_scale_factor_teller_pos_for_window))

                            if new_window > 0:
                                if current_window == 0: # and new_window != 0
                                    current_window = new_window
                                if (float(abs(current_window - new_window))/float(current_window)) > self.threshold_window_selection_relative_marge_difference_amount_pos: #(new_window > 0) and
                                    if start_index - current_window >= 0:
                                        start_index_with_current_window_marge = int(start_index - current_window)
                                    else:
                                        start_index_with_current_window_marge = start_index
                                    temp_smoothed_list = List_operations.movingaverage(list[start_index_with_current_window_marge:index],int(current_window))

                                    # het resultaat van movingaverage gaat op het einde steeds dalend zijn omdat, als er minder items over zijn dan de size van de window, gaat het movingaverage foutief berekent worden. Om dit tegen te gaan worden den laatste elementen van subarray eraf geknipt
                                    for index_restore in range(0,index):
                                        list[index_restore] = list_copy[index_restore]
                                    if(index - current_window) > 0:
                                        end_index = int(index - current_window)
                                    else:
                                        end_index = index
                                    if(len(temp_smoothed_list) - current_window ) > 0:
                                        end_index_smooth = int(len(temp_smoothed_list)  - current_window)
                                    else:
                                        end_index_smooth = len(temp_smoothed_list)

                                    if start_index >= (1 * current_window) and current_window < end_index_smooth:
                                        start_index_smooth_temp = int(current_window)
                                    else:
                                        start_index_smooth_temp = 0

                                    for index_temp_list in range(start_index_smooth_temp,end_index_smooth):
                                        result.append(temp_smoothed_list[index_temp_list])
                                        if (len(result) >= 337):
                                            True
                                    current_window = new_window
                                    start_index = len(result)

                            teller_pos = 0
                            teller_pos_total = 0.0
                            teller_pos_acc = 0.0
            index += 1

        if start_index < len(list) and current_window > 0:
            temp_smoothed_list = List_operations.movingaverage(list[int(start_index - current_window):len(list)],current_window)
            for index in range(int(current_window),len(temp_smoothed_list)):
                result.append(temp_smoothed_list[index])
        return result
def get_mindwave_manual_logged_activities_data(self, datetime_from, datetime_to,list_manual_logs,selected_activity,window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    selectedActivity = []
    userGoneStudying = []
    x = []
    index = 0
    listUserGone = DAUserGone_SQLite.get_data_specific_period(datetime_from,datetime_to)
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except :
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")


        if list_mindwave_data[index][5] == self.treshold_poorSignalMindwave:
            attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
            meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave,-1))
            meditation_data.append((datetime_from_mindwave,-1))

        canAddMatchedActivity = False

        for item in list_manual_logs:
            datetime_from_log = item[0]
            datetime_to_log = item[1]

            if datetime_from_mindwave >= datetime_from_log and datetime_to_mindwave <= datetime_to_log and item[2] == selected_activity:
                canAddMatchedActivity = True

        if canAddMatchedActivity is True:
            selectedActivity.append((datetime_from_mindwave,1))
        else:
            selectedActivity.append((datetime_from_mindwave,0))

        index += 1
    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]


    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention,window_median)
    y_meditation = List_operations.moving_median(y_meditation,window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"
    y_selectedActivity = List_operations.filter_list([value for (date, value) in selectedActivity])

    return attention_data, x, y_attention, y_meditation,y_selectedActivity
def get_mindwave_userGone_data(datetime_from, datetime_to,window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    userGoneNotStudying = []
    userGoneStudying = []
    x = []
    index = 0
    listUserGone = DAUserGone_SQLite.get_data_specific_period(datetime_from,datetime_to)
    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except :
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")


        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
            meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave,-1))
            meditation_data.append((datetime_from_mindwave,-1))

        canAddUserGoneNotStudyingEntry = False
        canAddUserGoneStudyingEntry = False
        for item in listUserGone:
            try:
                datetime_from_userGone = datetime.datetime.strptime(item[1], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_from_userGone = datetime.datetime.strptime(item[1], "%Y-%m-%d %H:%M:%S")

            try:
                datetime_to_userGone = datetime.datetime.strptime(item[2], "%Y-%m-%d %H:%M:%S.%f")
            except:
                datetime_to_userGone = datetime.datetime.strptime(item[2], "%Y-%m-%d %H:%M:%S")

            if datetime_from_mindwave >= datetime_from_userGone and datetime_to_mindwave <= datetime_to_userGone and item[5] == 0:
                canAddUserGoneNotStudyingEntry = True

            if datetime_from_mindwave >= datetime_from_userGone and datetime_to_mindwave <= datetime_to_userGone and item[5] == 1:
                canAddUserGoneStudyingEntry = True
        if canAddUserGoneNotStudyingEntry is True:
            userGoneNotStudying.append((datetime_from_mindwave,1))
        else:
            userGoneNotStudying.append((datetime_from_mindwave,0))

        if canAddUserGoneStudyingEntry is True:
            userGoneStudying.append((datetime_from_mindwave,1))
        else:
            userGoneStudying.append((datetime_from_mindwave,0))

        index += 1
    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]


    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention,window_median)
    y_meditation = List_operations.moving_median(y_meditation,window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    y_userGoneNotStudying = List_operations.filter_list([value for (date, value) in userGoneNotStudying])
    y_userGoneStudying = List_operations.filter_list([value for (date, value) in userGoneStudying])

    return attention_data, x, y_attention, y_meditation,y_userGoneNotStudying,y_userGoneStudying
def get_mindwave_activity_data( datetime_from, datetime_to,activity_details,window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    activityDuration_data = []
    x = []
    index = 0

    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except :
            datetime_to_mindwave = datetime.datetime.strptime(list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append((datetime_from_mindwave,list_mindwave_data[index][3]))
            meditation_data.append((datetime_from_mindwave,list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave,-1))
            meditation_data.append((datetime_from_mindwave,-1))

        added = False
        for activityDetail in activity_details:
            start_datetime_RescueTime = datetime.datetime.strptime(activityDetail[0], "%Y-%m-%d %H:%M:%S")
            end_datetime_rescueTime = start_datetime_RescueTime + datetime.timedelta(minutes=5)

            if datetime_from_mindwave >=start_datetime_RescueTime and datetime_to_mindwave <= end_datetime_rescueTime and added is False:
                activityDuration_data.append((datetime_from_mindwave,float((activityDetail[1]) / 300.0)))
                added = True
        if added is False:
            activityDuration_data.append((datetime_from_mindwave,-1))




        index += 1

    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention,window_median)
    y_meditation = List_operations.moving_median(y_meditation,window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    y_activityDuration = List_operations.filter_list([value for (date, value) in activityDuration_data])

    return attention_data, x, y_attention, y_meditation,y_activityDuration
def get_mindwave_activity_data(datetime_from, datetime_to, activity_details,
                               window):
    list_mindwave_data = DAMindwave_SQLite.get_data_specific_period(
        datetime_from, datetime_to)
    attention_data = []
    meditation_data = []
    activityDuration_data = []
    x = []
    index = 0

    while index < len(list_mindwave_data):
        try:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_from_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][1], "%Y-%m-%d %H:%M:%S")
        try:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S.%f")
        except:
            datetime_to_mindwave = datetime.datetime.strptime(
                list_mindwave_data[index][2], "%Y-%m-%d %H:%M:%S")

        if list_mindwave_data[index][5] == treshold_poorSignalMindwave:
            attention_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][3]))
            meditation_data.append(
                (datetime_from_mindwave, list_mindwave_data[index][4]))
        else:
            attention_data.append((datetime_from_mindwave, -1))
            meditation_data.append((datetime_from_mindwave, -1))

        added = False
        for activityDetail in activity_details:
            start_datetime_RescueTime = datetime.datetime.strptime(
                activityDetail[0], "%Y-%m-%d %H:%M:%S")
            end_datetime_rescueTime = start_datetime_RescueTime + datetime.timedelta(
                minutes=5)

            if datetime_from_mindwave >= start_datetime_RescueTime and datetime_to_mindwave <= end_datetime_rescueTime and added is False:
                activityDuration_data.append(
                    (datetime_from_mindwave, float(
                        (activityDetail[1]) / 300.0)))
                added = True
        if added is False:
            activityDuration_data.append((datetime_from_mindwave, -1))

        index += 1

    x = [date2num(date) for (date, value) in attention_data]
    y_attention = [value for (date, value) in attention_data]
    y_meditation = [value for (date, value) in meditation_data]

    print "--------------------------------"
    print "original"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    y_attention = List_operations.filter_list(y_attention)
    y_meditation = List_operations.filter_list(y_meditation)

    print "after filter_list"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.moving_median(y_attention, window_median)
    y_meditation = List_operations.moving_median(y_meditation, window_median)

    print "after moving_median"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))

    y_attention = List_operations.movingaverage(y_attention, window)
    y_meditation = List_operations.movingaverage(y_meditation, window)

    print "after movingaverage"
    print str("length y_attention: " + str(len(y_attention)))
    print str("length y_meditation: " + str(len(y_meditation)))
    print "---------------------------"

    y_activityDuration = List_operations.filter_list(
        [value for (date, value) in activityDuration_data])

    return attention_data, x, y_attention, y_meditation, y_activityDuration
 def test_statistics(self):
     input_list = [1, 3, 3, 4, 5, 7, 9, 10, 11, 15, 16]
     window_size = 4
     output_list = List_operations.movingaverage(input_list, window_size)
     print output_list