<commit_before># coding=utf-8
"""
PowerGraphs.
"""
# Copyright (c) 2015 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
# AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import tkinter as tk
from tkinter import ttk
import logging

from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
from .monitor_sqlite import SQLiteDB


class PowerGraphs(ttk.Frame):
    """Graphical representation of EEG data."""

    def __init__(self, master):
        self.logger = logging.getLogger('mind_monitor.ui')
        super().__init__(master, borderwidth=2, relief=tk.GROOVE)
        self.fig = Figure(figsize=(15, 12))
        self.subplot_base = 410
        self.canvas = FigureCanvas(self.fig, master=self)
        self.canvas.get_tk_widget().grid()
        self.fig.clear()

    def plot_records(self, records, raw_records=None, t_base=None):
        """Plot all power data.

        :param records: the data records as list of tuples.
        :param raw_records: optional raw records.
        :param t_base: optional base value for time.
        """
        self.logger.info('Ready to plot {} records ...'.format(len(records)))
        self.fig.clear()
        if len(records) > 0:
            if raw_records:
                session_id = raw_records[0].session
                if t_base is None:
                    t_base = raw_records[0].timestamp
                t_raws = [x.timestamp - t_base for x in raw_records]
                self.subplot_base = 511
                # raw wave
                raw_data = [x.data for x in raw_records]
                spl0 = self.fig.add_subplot(self._subplot_id(0),
                                            xlabel='time [sec]',
                                            ylabel='value',
                                            title='EEG raw data for session {}'.format(session_id))
                spl0.plot(t_raws, raw_data, label='raw')
            session_id = records[0].session
            if t_base is None:
                t_base = records[0].timestamp
            t_data = [x.timestamp - t_base for x in records]
            # delta and theta waves
            delta_data = [x.delta for x in records]
            theta_data = [x.theta for x in records]
            spl1 = self.fig.add_subplot(self._subplot_id(1),
                                        xlabel='time [sec]',
                                        ylabel='value',
                                        title='EEG data for session {}'.format(session_id))
            # self.canvas.ylim((0, 500000))
            spl1.plot(t_data, delta_data, label='delta')
            spl1.plot(t_data, theta_data, label='theta')
            spl1.legend(loc='best')

            # high alpha, beta, gamma waves
            high_alpha_data = [x.highAlpha for x in records]
            high_beta_data = [x.highBeta for x in records]
            high_gamma_data = [x.highGamma for x in records]

            spl2 = self.fig.add_subplot(self._subplot_id(2),
                                        xlabel='time [sec]',
                                        ylabel='value')
            # self.canvas.ylim((0, 70000))
            spl2.plot(t_data, high_alpha_data, label='highAlpha')
            spl2.plot(t_data, high_beta_data, label='highBeta')
            spl2.plot(t_data, high_gamma_data, label='highGamma')
            spl2.legend(loc='best')

            # low alpha, beta, gamma waves
            low_alpha_data = [x.lowAlpha for x in records]
            low_beta_data = [x.lowBeta for x in records]
            low_gamma_data = [x.lowGamma for x in records]

            spl3 = self.fig.add_subplot(self._subplot_id(3),
                                        xlabel='time [sec]',
                                        ylabel='value')
            # self.canvas.ylim((0, 70000))
            spl3.plot(t_data, low_alpha_data, label='lowAlpha')
            spl3.plot(t_data, low_beta_data, label='lowBeta')
            spl3.plot(t_data, low_gamma_data, label='lowGamma')
            spl3.legend(loc='best')

            # attention and meditaion waves
            attention_data = [x.attention for x in records]
            meditation_data = [x.meditation for x in records]

            spl4 = self.fig.add_subplot(self._subplot_id(4),
                                        xlabel='time [sec]',
                                        ylabel='%')
            spl4.plot(t_data, attention_data, label='attention')
            spl4.plot(t_data, meditation_data, label='meditation')
            spl4.legend(loc='best')

            self.canvas.show()

    def _subplot_id(self, cnt):
        return self.subplot_base + cnt


class FeedGraphData(ttk.Frame):
    """Provide data for PowerGraphs."""
<commit_msg>Use logarithmic scales for power data.<commit_after># coding=utf-8
"""
PowerGraphs.
"""
# Copyright (c) 2015 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
# AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import tkinter as tk
from tkinter import ttk
import logging

from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
from .monitor_sqlite import SQLiteDB


class PowerGraphs(ttk.Frame):
    """Graphical representation of EEG data."""

    def __init__(self, master):
        self.logger = logging.getLogger('mind_monitor.ui')
        super().__init__(master, borderwidth=2, relief=tk.GROOVE)
        self.fig = Figure(figsize=(15, 12))
        self.subplot_base = 410
        self.canvas = FigureCanvas(self.fig, master=self)
        self.canvas.get_tk_widget().grid()
        self.fig.clear()

    def plot_records(self, records, raw_records=None, t_base=None):
        """Plot all power data.

        :param records: the data records as list of tuples.
        :param raw_records: optional raw records.
        :param t_base: optional base value for time.
        """
        self.logger.info('Ready to plot {} records ...'.format(len(records)))
        self.fig.clear()
        if len(records) > 0:
            if raw_records:
                session_id = raw_records[0].session
                if t_base is None:
                    t_base = raw_records[0].timestamp
                t_raws = [x.timestamp - t_base for x in raw_records]
                self.subplot_base = 511
                # raw wave
                raw_data = [x.data for x in raw_records]
                spl0 = self.fig.add_subplot(self._subplot_id(0),
                                            xlabel='time [sec]',
                                            ylabel='value',
                                            title='EEG raw data for session {}'.format(session_id))
                spl0.plot(t_raws, raw_data, label='raw')
            session_id = records[0].session
            if t_base is None:
                t_base = records[0].timestamp
            t_data = [x.timestamp - t_base for x in records]
            # delta and theta waves
            delta_data = [x.delta for x in records]
            theta_data = [x.theta for x in records]
            spl1 = self.fig.add_subplot(self._subplot_id(1),
                                        xlabel='time [sec]',
                                        ylabel='value',
                                        title='EEG power data for session {}'.format(session_id))
            # self.canvas.ylim((0, 500000))
            spl1.semilogy(t_data, delta_data, label='delta')
            spl1.semilogy(t_data, theta_data, label='theta')
            spl1.legend(loc='best')

            # high alpha, beta, gamma waves
            high_alpha_data = [x.highAlpha for x in records]
            high_beta_data = [x.highBeta for x in records]
            high_gamma_data = [x.highGamma for x in records]

            spl2 = self.fig.add_subplot(self._subplot_id(2),
                                        xlabel='time [sec]',
                                        ylabel='value')
            # self.canvas.ylim((0, 70000))
            spl2.semilogy(t_data, high_alpha_data, label='highAlpha')
            spl2.semilogy(t_data, high_beta_data, label='highBeta')
            spl2.semilogy(t_data, high_gamma_data, label='highGamma')
            spl2.legend(loc='best')

            # low alpha, beta, gamma waves
            low_alpha_data = [x.lowAlpha for x in records]
            low_beta_data = [x.lowBeta for x in records]
            low_gamma_data = [x.lowGamma for x in records]

            spl3 = self.fig.add_subplot(self._subplot_id(3),
                                        xlabel='time [sec]',
                                        ylabel='value')
            # self.canvas.ylim((0, 70000))
            spl3.semilogy(t_data, low_alpha_data, label='lowAlpha')
            spl3.semilogy(t_data, low_beta_data, label='lowBeta')
            spl3.semilogy(t_data, low_gamma_data, label='lowGamma')
            spl3.legend(loc='best')

            # attention and meditaion waves
            attention_data = [x.attention for x in records]
            meditation_data = [x.meditation for x in records]

            spl4 = self.fig.add_subplot(self._subplot_id(4),
                                        xlabel='time [sec]',
                                        ylabel='%')
            spl4.plot(t_data, attention_data, label='attention')
            spl4.plot(t_data, meditation_data, label='meditation')
            spl4.legend(loc='best')

            self.canvas.show()

    def _subplot_id(self, cnt):
        return self.subplot_base + cnt


class FeedGraphData(ttk.Frame):
    """Provide data for PowerGraphs."""

{'fraction_non_alphanumeric': 0.09179162693941233, 'fraction_numerical': 0.011268093958568086, 'mean_word_length': 1.982936918304033, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4637811', 'n_tokens_mistral': 3245, 'n_tokens_neox': 3076, 'n_words': 944}