import numpy as np
import numba

parallel=True

@numba.jit(nopython=True)
def numba_combinations(pool, r):
    """
    a numba compatible version of
    itertools combinations function
    """
    n = len(pool)
    indices = list(range(r))
    empty = not(n and (0 < r <= n))

    if not empty:
        result = [pool[i] for i in indices]
        yield result

    while not empty:
        i = r - 1
        while i >= 0 and indices[i] == i + n - r:
            i -= 1
        if i < 0:
            empty = True
        else:
            indices[i] += 1
            for j in range(i+1, r):
                indices[j] = indices[j-1] + 1

            result = [pool[i] for i in indices]
            yield result

@numba.jit(nopython=True, parallel=parallel)
def multi_perm(M, D):
    N = M.shape[0]
    rangeN = np.arange(N)

    combs = []
    for i in numba.prange(1,N+1):
        combs.append(list(numba_combinations(rangeN, i)))

    W = 0j * np.zeros((N,N,N))
    for k in range(N):
        for l in range(N):
            for j in range(N):
                W[k,l,j] = M[j,k] * M[j,l].conjugate() * D[l,k]
    P = 0.
    for R_size in numba.prange(1,N+1):
        for S_size in numba.prange(R_size,N+1):
            a = (2 - int(R_size == S_size)) * (-1) ** (S_size + R_size)
            for R in combs[R_size-1]:
                for S in combs[S_size-1]:
                    b = a
                    for j in range(N):
                        c = 0.
                        for r in R:
                            for s in S:
                                c += W[s,r,j]
                        b *= c
                    P += b.real
    return P


from typing import Protocol, Any, Union, List
from abc import abstractclassmethod
from dataclasses import dataclass

class MyLinkedList(Protocol):
    @abstractclassmethod
    def push(item: Any) -> None:
        ...

    @abstractclassmethod
    def insert(index: int, item: Any) -> None:
        ...

    @abstractclassmethod
    def pop() -> Any:
        ...

    @abstractclassmethod
    def delete(index: int) -> None:
        ...

    @abstractclassmethod
    def get(index: int) -> Any:
        ...

    @abstractclassmethod
    def set(index: int, item: Any) -> None:
        ...

    @abstractclassmethod
    def is_empty() -> bool:
        ...

@dataclass
class Node:
    data: Any
    next: "Node" = None

class CustomLinkedList(MyLinkedList):
    def __init__(self):
        self.__length = 0
        self.__head: Union[Node, None] = None

    def __len__(self):
        return self.__length

    def push(self, item: Any) -> None:
        # if list is empty than set self.__head to new node
        if (not self.__head):
            newNode = Node(item)
            self.__head = newNode
            self.__length += 1
            return

        # Go to last Node
        tmp = self.__head
        while (tmp.next):
            tmp = tmp.next

        newNode = Node(item)
        tmp.next = newNode
        self.__length += 1

    def is_empty(self) -> bool:
        if (self.__head == None):
            return True
        return False

    def __get_node(self, index: int) -> Node:
        if (index >= self.__length and index < 0):
            # Raise An Error
            ...

        counter = 0
        tmp = self.__head
        while (counter < index):
            tmp = tmp.next
            counter += 1

        return tmp

    def pop(self) -> Any:
        # Delete last node and return that node data
        if (self.is_empty()):
            return None

        if (self.__length == 1):
            data = self.__head.data
            self.__head = None
            self.__length -= 1
            return data

        prev_node = self.__get_node(self.__length - 2)
        current_node = prev_node.next
        prev_node.next = None
        self.__length -= 1

        return current_node.data

    def get(self, index: int) -> Any:
        if (self.is_empty):
            # Raise An Error
            ...
        return self.__get_node(index).data

    def set(self, index: int, item: Any) -> None:
        node = self.__get_node(index)
        node.data = item

    def insert(self, index: int, item: Any) -> None:
        # In order to insert, length of linked list has to be at least 1
        if (self.is_empty()):
            # Raise Error
            ...

        if (index == 0):
            newNode = Node(item, self.__head)
            self.__head = newNode
            self.__length += 1
            return

        prevNode = self.__get_node(index - 1)
        newNode = Node(item, prevNode.next)
        prevNode.next = newNode
        self.__length += 1

    def delete(self, index: int):
        if (self.is_empty()):
            # Raise Error
            ...

        if (index == 0):
            self.__head = self.__head.next
            self.__length -= 1

        prevNode = self.__get_node(index - 1)
        currentNode = prevNode.next
        prevNode.next = currentNode.next
        self.__length -= 1

    def get_all(self) -> List[Any]:
        itemList = []
        tmp = self.__head
        while (tmp):
            itemList.append(tmp.data)
            tmp = tmp.next
        return itemList

def main():
    test = CustomLinkedList()
    test.push("hello")
    test.push("world")
    arr = test.get_all()
    print(arr)

    test.insert(0, "insertion")
    arr = test.get_all()
    print(arr)

    test.delete(1)
    arr = test.get_all()
    print(arr)

    print(len(test))

    element = test.pop()
    arr = test.get_all()
    print(f"poped Element: {element}, LinkedList: {arr}, length: {len(test)}")

if (__name__ == "__main__"):
    main()


# -*- coding: utf-8 -*-
"""
Created on Wed Sep 22 14:20:14 2021

@author: sp3660
"""
import os
import glob
import numpy as np
import tifffile
from bidicorrect_image import shiftBiDi, biDiPhaseOffsets
import matplotlib.pyplot as plt
import shutil

folder_path= r'C:\Users\sp3660\Desktop\Volumes'
files=os.listdir(folder_path)
minivolumes=sorted(glob.glob(folder_path+'\\20210917**'))

for minivolume in minivolumes:
        channel1_path=os.path.join(minivolume, 'Ch1')
        channel2_path=os.path.join(minivolume, 'Ch2')

        for file in glob.glob(minivolume+'\\**.tif'):
            if 'Ch1' in file:
                if not os.path.exists(channel1_path):
                    os.mkdir(channel1_path)
                shutil.move(file, channel1_path)
            if 'Ch2' in file:
                if not os.path.exists(channel2_path):
                    os.mkdir(channel2_path)
                shutil.move(file, channel2_path)

        for file in sorted(glob.glob(minivolume+'\\**')):
            if 'Ch' in file:
                channel=file
                numbers=[]
                tiles_numbers=[]
                for file in glob.glob(channel+'\\**'):
                    numbers.append(file[file.find('.ome')-2:file.find('.ome')])
                    tiles_numbers.append(file[file.find('_Ch')-2:file.find('_Ch')])
                planes=len(set(numbers))
                tiles_number=len(set(tiles_numbers))
                for i in range(1,planes+1):
                    plane_path=os.path.join(channel, 'Plane_{}'.format(f"{i:02}"))
                    if not os.path.exists(plane_path):
                        os.mkdir(plane_path)
                    for file in glob.glob(channel+'\\**'):
                        if f"{i:02}" in file[file.find('.ome')-2:file.find('.ome')]:
                            shutil.move(file, plane_path)

                for planes in glob.glob(channel+'\\**'):
                    tiles=sorted(glob.glob(planes+'\\**.tif'))
                    if not os.path.exists( os.path.join(os.path.split(tiles[i])[0],'Corrected')):
                        os.mkdir(os.path.join(os.path.split(tiles[i])[0],'Corrected'))

                    if len(glob.glob(os.path.join(os.path.split(tiles[i])[0],'Corrected')+'\\**.tif'))<tiles_number:
                        # input_arr=cm.load(tiles)
                        # if input_arr.shape.index(min(input_arr.shape))==0:
                        #     input_arr=np.reshape(input_arr, (input_arr.shape[1],input_arr.shape[2],input_arr.shape[0]), order='F')
                        for file in tiles:
                            input_arr=np.zeros((512,512,len(tiles)))
                            for i, file_name in enumerate(tiles):
                                with tifffile.TiffFile(file_name) as tffl:
                                    input_image= tffl.asarray()
                                    if len(input_image.shape)==4:
                                          input_image=input_image[0,0,:,:]
                                    if len(input_image.shape)==3:
                                          input_image=input_image[0,:,:]
                                    input_arr[:,:,i] = input_image

                        shifted_images=np.zeros(input_arr.shape).astype('float32')
                        for i in range(min(input_arr.shape)):
                            frame=input_arr[:,:,i]
                            BiDiPhase=biDiPhaseOffsets(frame)

                            shifted_image=shiftBiDi(BiDiPhase, frame)
                            shifted_images[:,:,i]=shifted_image
                            tifffile.imsave( os.path.join(os.path.split(tiles[i])[0],'Corrected\\Tile_shifted_{}.tif'.format(f"{i:04}")),shifted_image)

    #%% one image

# image_path=r'C:\Users\sp3660\Desktop\Volumes\20210917_SPGT_Atlas_920_50024_607_without_175_205z_5z_1ol-008\Ch2\Plane_04\20210917_SPGT_Atlas_920_50024_607_without_175_205z_5z_1ol-008_Cycle00011_Ch2_000004.ome.tif'
# with tifffile.TiffFile(image_path) as tffl:
#     input_image= tffl.asarray()
#     tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_ORIGINAL_{}.tif'.format(f"{i:04}")),input_image)
#     BiDiPhase=biDiPhaseOffsets(input_image)
#     shifted_image=np.zeros(input_image.shape).astype('float32')
#     shifted_image=shiftBiDi(BiDiPhase, input_image)
#     i=11
#     tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_shifted_{}.tif'.format(f"{i:04}")),shifted_image)
#     if BiDiPhase!=0:
#         double_shifted_image=shiftBiDi(BiDiPhase*2, input_image)
#         tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_double_shifted_{}.tif'.format(f"{i:04}")),double_shifted_image)

#     else:
#         negative_shifted_image=shiftBiDi(-1, input_image)
#         positive_shifted_image=shiftBiDi(1, input_image)
#         double_negative_shifted_image=shiftBiDi(-2, input_image)
#         double_positive_shifted_image=shiftBiDi(2, input_image)
#         tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_negative_shifted_{}.tif'.format(f"{i:04}")),negative_shifted_image)
#         tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_positive_shifted_{}.tif'.format(f"{i:04}")),positive_shifted_image)
#         tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_double_negative_shifted_{}.tif'.format(f"{i:04}")),double_negative_shifted_image)
#         tifffile.imsave(os.path.join(os.path.split(image_path)[0],'Corrected\\Tile_double_positive_shifted_{}.tif'.format(f"{i:04}")),double_positive_shifted_image)

# pre_figs={}
# pre_axs={}
# post_figs={}
# post_axs={}
# for i in range(shifted_images.shape[-1]):
#         pre_figs[i]=plt.figure()
#         pre_axs[i]=pre_figs[i].add_subplot()
#         pre_axs[i].imshow(input_arr[:,:,i])
#         pre_axs[i].set_title('Pre')
#         post_figs[i]=plt.figure()th
#         post_axs[i]=post_figs[i].add_subplot()
#         post_axs[i].imshow(shifted_images[:,:,i])
#         post_axs[i].set_title('Post')

import imagej
from scyjava import jimport
import glob
import os
ij = imagej.init('sc.fiji:fiji')
FileSaver = jimport('ij.io.FileSaver')
#%%
plugin = 'Grid/Collection stitching'
volumes=r'C:/Users/sp3660/Desktop/Volumes'
for directory in glob.glob(volumes+'\\**\\Ch**\\PLane**\\Corrected'):
    args = {
        'type':['Grid: snake by rows'],
        'order':['Right & Down']   ,
        'grid_size_x':5 ,
        'grid_size_y':3 ,
        'tile_overlap':1 ,
        'first_file_index_i':0 ,
        'directory':directory,
        'file_names':'Tile_shifted_00{ii}.tif' ,
        'output_textfile_name':'TileConfiguration.txt' ,
        'fusion_method':['Linear Blending'] ,
        'regression_threshold':0.30 ,
        'max/avg_displacement_threshold':2.50 ,
        'absolute_displacement_threshold':3.50 ,
        'compute_overlap':False,
        'subpixel_accuracy':True,
        'computation_parameters':['Save memory (but be slower)'],
        'image_output':['Fuse and display'],
    }

    ij.py.run_plugin(plugin, args)
    imp = ij.py.active_image_plus()
    fs = FileSaver(imp)
    fs.saveAsTiff( os.path.join(directory,'Mosaic_1%'))


"""
Welfare Call dataset
"""
from dataclasses import dataclass, field
from typing import List, Literal, Optional
import os
import logging

import jsonlines

from utils.utils import PUNCTUATION_LABELS

logger = logging.getLogger(__name__)

@dataclass
class InputExampleJSON:
    text: str
    metadata: dict
    audio_path: str
    label: str

@dataclass
class InputExample:
    """
    A single training/test example for simple sequence classification.

    Args:
        guid: Unique id for the example.
        words: list. The words of the sequence.
        labels: (Optional) list. The slot labels of the example.
    """

    guid: str
    words: List[str] = field(default_factory=list)
    labels: List[str] = field(default_factory=list)
    audio_path: Optional[str] = None

class WelfareCallDatasetProcessor(object):
    """Processor for welfare call data set"""

    def __init__(self, args):
        self.args = args
        self.labels_lst = PUNCTUATION_LABELS

    @classmethod
    def _read_file(cls, input_file: str) -> List[InputExampleJSON]:
        """Read jsonl file, and return words and label as list"""
        with jsonlines.open(input_file) as f:
            lines: List[InputExampleJSON] = []
            for line in f.iter():
                lines.append(InputExampleJSON(**line))
            return lines

    def _create_examples(
        self, dataset: List[InputExampleJSON], set_type: str
    ) -> List[InputExample]:
        """Creates examples for the training and dev sets."""
        examples: List[InputExample] = []
        for i, data in enumerate(dataset):
            words = data.text.split()
            labels = data.label.split()

            labels_idx = [
                self.labels_lst.index(label)
                if label in self.labels_lst
                else self.labels_lst.index("UNK")
                for label in labels
            ]

            assert len(words) == len(labels_idx)

            example = {
                "guid": f"{set_type}-{i}",
                "words": words,
                "labels": labels_idx,
                "audio_path": data.audio_path,
            }

            if i % 10000 == 0:
                logger.info(f"CREATING :: EX :: {data}")

            examples.append(InputExample(**example))

        return examples

    def get_examples(self, mode: Literal["train", "dev", "test"]) -> List[InputExample]:
        file_map = {
            "train": self.args.train_file,
            "dev": self.args.dev_file,
            "test": self.args.test_file,
        }

        example_file_path = os.path.join(self.args.data_dir, file_map[mode])
        logger.info("LOOKING AT {}".format(example_file_path))

        return self._create_examples(self._read_file(example_file_path), mode)


from rest_framework.permissions import IsAuthenticated
from rest_framework import status, generics
from photos.serializers import PhotoSerializer

class AddPost(generics.GenericAPIView):
    permission_classes = (IsAuthenticated,)
    serializer_class = PhotoSerializer

    def post(self, request, *args, **kwargs):
        data = {**request.data, 'user': request.user.id}
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_200_OK)


from __future__ import print_function
import argparse
import tools.find_mxnet
import mxnet as mx
import os
import importlib
import sys

def parse_args():
    parser = argparse.ArgumentParser(description='Convert a trained model to deploy model')
    parser.add_argument('--network', dest='network', type=str, default='vgg16_ssd_300',
                        choices=['vgg16_ssd_300', 'vgg16_ssd_512'], help='which network to use')
    parser.add_argument('--epoch', dest='epoch', help='epoch of trained model',
                        default=0, type=int)
    parser.add_argument('--prefix', dest='prefix', help='trained model prefix',
                        default=os.path.join(os.getcwd(), 'model', 'ssd_300'), type=str)
    parser.add_argument('--num-class', dest='num_classes', help='number of classes',
                        default=20, type=int)
    parser.add_argument('--nms', dest='nms_thresh', type=float, default=0.5,
                        help='non-maximum suppression threshold, default 0.5')
    parser.add_argument('--force', dest='force_nms', type=bool, default=True,
                        help='force non-maximum suppression on different class')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()
    sys.path.append(os.path.join(os.getcwd(), 'symbol'))
    net = importlib.import_module("symbol_" + args.network) \
        .get_symbol(args.num_classes, args.nms_thresh, args.force_nms)
    _, arg_params, aux_params = mx.model.load_checkpoint(args.prefix, args.epoch)
    # new name
    tmp = args.prefix.rsplit('/', 1)
    save_prefix = '/deploy_'.join(tmp)
    mx.model.save_checkpoint(save_prefix, args.epoch, net, arg_params, aux_params)
    print("Saved model: {}-{:04d}.param".format(save_prefix, args.epoch))
    print("Saved symbol: {}-symbol.json".format(save_prefix))


import sys
import os

sys.path.append(
    os.path.dirname(
        os.path.abspath(
            os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
        )
    )
)

from rabbitpy import Queue, Connection, Message
from config.config_rabbitpy import URL
import time
from exchanges import utils

conn = Connection(URL)
channel = conn.channel()

queue_name = f"response-queue-{os.getpid()}"
response_queue = Queue(
    channel=channel, name=queue_name, exclusive=True, auto_delete=False
)

if response_queue.declare():
    print("Response queue declared.")

if response_queue.bind("rpc-replies", queue_name):
    print("Response queue bound.")

for img_id, filename in enumerate(utils.get_images()):
    print(f"Sending request for image {img_id}: {filename}")

    message = Message(
        channel=channel,
        body_value=utils.read_image(filename),
        properties={
            "content_type": utils.mime_type(filename),
            "correlation_id": str(img_id),
            "reply_to": queue_name,
        },
        opinionated=True,
    )

    message.publish(exchange="direct-rpc-requests", routing_key="detect-faces")

    message = None
    while not message:
        time.sleep(0.5)
        message = response_queue.get()

    message.ack()

    duration = time.time() - time.mktime(
        message.properties["headers"]["first_publish"]
    )

    print(
        f"""
        Facial detection RPC call for image...
        {message.properties['correlation_id']} total duration: {duration}
        """
    )

print("RPC requests processed.")
channel.close()
conn.close()


import os
import logging

def logger_setup() -> None:
    logging.basicConfig(filename='logfile.log', encoding='utf-8',level=logging.DEBUG)

def read_secrets() -> dict[str,str]: # Returns a dict of all secrets?
    secrets: dict[str,str] = {}

    with open('secrets/secrets.txt','r') as f:
        lines = f.readlines()

    for line in lines:
        secrets[line[:line.find('=')]]=line[line.find('=')+1:-1]

    return secrets

# Set environment variables using secrets file
def set_env_variables() -> None:
    secrets: dict[str,str] = read_secrets()

    if 'SPOTIPY_CLIENT_ID' in os.environ:
        logging.info('SPOTIPY_CLIENT_ID already set')
    if 'SPOTIPY_CLIENT_ID' not in os.environ:
        os.environ['SPOTIPY_CLIENT_ID']=secrets['SPOTIPY_CLIENT_ID']
        logging.info('Set SPOTIPY_CLIENT_ID')

    if 'SPOTIPY_CLIENT_SECRET' in os.environ:
        logging.info('SPOTIPY_CLIENT_SECRET alreay set')
    if 'SPOTIPY_CLIENT_SECRET' not in os.environ:
        os.environ['SPOTIPY_CLIENT_SECRET']=secrets['SPOTIPY_CLIENT_SECRET']
        logging.info('Set SPOTIPY_CLIENT_SECRET')

    if 'SPOTIPY_REDIRECT_URI' in os.environ:
        logging.info('SPOTIPY_REDIRECT_URI already set')
    if 'SPOTIPY_REDIRECT_URI' not in os.environ:
        os.environ['SPOTIPY_REDIRECT_URI']=secrets['SPOTIPY_REDIRECT_URI']
        logging.info('Set SPOTIPY_REDIRECT_URI')


# import all the dependecies
import streamlit as st
import pandas as pd
import plotly.express as px
from streamlit_lottie import st_lottie
import joblib
import json

st.set_page_config(
    layout="wide"
)
from pages.housing_price_prediction.components.Layout import Layout
root = "./pages/housing_price_prediction"

# load the required data
@st.cache_data
def load_data():
    dataframe = pd.read_csv('./pages/housing_price_prediction/dataset/Housing.csv')
    return dataframe

dataframe = load_data()

# Design the sidebar
st.sidebar.title("Moreen-Ai")

charts, predictions = st.tabs(['ðŸ“‰Charts', 'âœ¨ Predictions'])

with charts:
    # data management
    bedrooms = st.sidebar.multiselect(
        "Number of Bedrooms",
        options=dataframe['bedrooms'].unique(),
        default=dataframe['bedrooms'].unique()
    )

    bathrooms = st.sidebar.multiselect(
        "Number of Bathrooms",
        options=dataframe['bathrooms'].unique(),
        default=dataframe['bathrooms'].unique()
    )

    stories = st.sidebar.multiselect(
        "Number of stories",
        options=dataframe['stories'].unique(),
        default=dataframe['stories'].unique()
    )

    mainroad = st.sidebar.multiselect(
        "Near the mainroad",
        options=dataframe['mainroad'].unique(),
        default=dataframe['mainroad'].unique()
    )

    guestroom = st.sidebar.multiselect(
        "Has a guest room",
        options=dataframe['guestroom'].unique(),
        default=dataframe['guestroom'].unique()
    )

    basement = st.sidebar.multiselect(
        "Has a basement",
        options=dataframe['basement'].unique(),
        default=dataframe['basement'].unique()
    )

    hotwaterheating = st.sidebar.multiselect(
        "Has Hot-water heating",
        options=dataframe['hotwaterheating'].unique(),
        default=dataframe['hotwaterheating'].unique()
    )

    airconditioning = st.sidebar.multiselect(
        "Has air conditioning system ",
        options=dataframe['airconditioning'].unique(),
        default=dataframe['airconditioning'].unique()
    )

    parking = st.sidebar.multiselect(
        "Has parking area",
        options=dataframe['parking'].unique(),
        default=dataframe['parking'].unique()
    )

    prefarea = st.sidebar.multiselect(
        "Is a prefered areae",
        options=dataframe['prefarea'].unique(),
        default=dataframe['prefarea'].unique()
    )

    furnishingstatus = st.sidebar.multiselect(
        "Furnishing Status",
        options=dataframe['furnishingstatus'].unique(),
        default=dataframe['furnishingstatus'].unique()
    )

    # query the data
    df_selections = dataframe.query(
        "bedrooms == @bedrooms & bathrooms == @bathrooms & stories == @stories & mainroad == @mainroad & guestroom == @guestroom & basement == @basement & hotwaterheating == @hotwaterheating & airconditioning == @airconditioning & parking == @parking & prefarea == @prefarea & furnishingstatus == @furnishingstatus"
    )

    # Design the main page
    title = "Housing Price Analytics"
    introduction = '''
    **Dataset source:** [kaggle.com](https://www.kaggle.com/datasets/harishkumardatalab/housing-price-prediction)

    '''

    image = ""
    description=""

    # Insights
    total_houses = len(df_selections)
    average_price = df_selections['price'].mean()
    average__area = df_selections['area'].mean()

    min_cost = df_selections['price'].min()
    max_const = df_selections['price'].max()

    layout = Layout(title, introduction, image, description)
    layout.render()

    # Insights
    total_houses = len(df_selections)

    if(len(df_selections != 0)):
        average_price = df_selections['price'].mean().round()
        average_area = df_selections['area'].mean().round()

    else:
        average_area = 0
        average_price = 0

    # first row in the ui
    left1, middle1, right1 = st.columns(3)

    with left1:
        st.subheader('Houses Found')
        st.subheader('{}'.format(total_houses))

    with middle1:
        st.subheader('Average area(sq. feet)')
        st.subheader('{} USD'.format(average_area))
    with right1:
        st.subheader('Average Price (USD)')
        st.subheader(average_price)

    # first rowset
    row1col1, row1col2,row1col3, row1col4 = st.columns((1,1,1,2))
    with row1col1:
        mainroad_df = df_selections.groupby('mainroad')['mainroad'].count().reset_index(name='count')
        fig1 = px.pie(mainroad_df, values='count', names='mainroad', title='Mainroad Distribution')
        st.plotly_chart(fig1, use_container_width=True)

    with row1col2:
        guestroom_df = df_selections.groupby('guestroom')['guestroom'].count().reset_index(name='count')
        fig2 = px.pie(guestroom_df, values='count', names='guestroom', title='Guestroom Distribution')
        st.plotly_chart(fig2, use_container_width=True)

    with row1col3:
        basement_df = df_selections.groupby('basement')['basement'].count().reset_index(name='count')
        fig3 = px.pie(basement_df, values='count', names='basement', title='basement Distribution')
        st.plotly_chart(fig3, use_container_width=True)

    with row1col4:
        with open("./animations/animation_ll5nx88j.json", 'r') as file:
            animation1 = json.load(file)
        st_lottie(
            animation1,
            width=360
        )
    # second rowse""t
    row2col1, row2col2, row2col3= st.columns((2, 1, 1))
    with row2col1:
        st.dataframe(df_selections)

    with row2col2:
        airconditioning_df = df_selections.groupby('airconditioning')['airconditioning'].count().reset_index(name='count')
        fig5 = px.pie(airconditioning_df, values='count', names='airconditioning', title='Airconditioning Distribution')
        st.plotly_chart(fig5, use_container_width=True)

    with row2col3:
        hotwaterheating_df = df_selections.groupby('hotwaterheating')['hotwaterheating'].count().reset_index(name='count')
        fig4 = px.pie(hotwaterheating_df, values='count', names='hotwaterheating', title='Hot-water heating Distribution')
        st.plotly_chart(fig4, use_container_width=True)

    # third rowse""t
    row3col1, row3col2, row3col3= st.columns((1, 1, 2))
    with row3col1:
        prefarea_df = df_selections.groupby('prefarea')['prefarea'].count().reset_index(name='count')
        fig6 = px.pie(prefarea_df, values='count', names='prefarea', title='prefarea Distribution')
        st.plotly_chart(fig6, use_container_width=True)

    with row3col2:
        bedrooms_df = df_selections.groupby('bedrooms')['bedrooms'].count().reset_index(name='count')
        fig7 = px.pie(bedrooms_df, values='bedrooms', names='bedrooms', title='Bedrooms Distributions')
        st.plotly_chart(fig7, use_container_width=True)

    with row3col3:
        stories_df = df_selections.groupby('stories')['stories'].count().reset_index(name='count')
        fig8 = px.bar(stories_df, y='stories', x='count', orientation='h', title='stories Distributions')
        st.plotly_chart(fig8, use_container_width=True)

with predictions:
    st.title('House Price Prediction, with Decision Tree Regressor')
    st.success('Fill in the forms below to get the price eastimation of your dream home')

    inpcol1, inpcol2, inpcol3, inpcol4, inpcol5  = st.columns(5)
    with inpcol1:
        bedrooms_input = st.slider("Bedrooms", min_value=1, max_value=6, value=3)

    with inpcol2:
        bathrooms_input = st.slider("Bathrooms", min_value=1, max_value=4, value=1)

    with inpcol3:
        stories_input = st.slider("Stories", min_value=1, max_value=4, value=2)

    with inpcol4:
        parking_input = st.slider("Parking", min_value=1, max_value=6, value=3)

    with inpcol5:
        furnishingstatus_input = st.selectbox('Furnishing Status',dataframe.furnishingstatus.unique())

    check1, check2,check3,check4,check5, check6 = st.columns(6)
    with check1:
        mainroad_input = st.checkbox("Mainroad", value=True)
    with check2:
        guestroom_input = st.checkbox('Guestroom', value=True)
    with check3:
        basement_input = st.checkbox('Basement', value=True)
    with check4:
        hotwaterheating_input = st.checkbox('Hot water Heating', value=True)
    with check5:
        airconditioning_input = st.checkbox('Air Conditioning', value=True)
    with check6:
        prefarea_input = st.checkbox('Prefered Area', value=True)

    # process the user input
    if mainroad_input:
        mainroad_input = 1
    else:
        mainroad_input = 0

    if guestroom_input:
        guestroom_input = 1
    else:
        guestroom_input = 0

    if basement_input:
        basement_input = 1
    else:
        basement_input = 0

    if hotwaterheating_input:
        hotwaterheating_input = 1
    else:
        hotwaterheating_input = 0

    if airconditioning_input:
        airconditioning_input = 1
    else:
        airconditioning_input = 0

    if prefarea_input:
        prefarea_input = 1
    else:
        prefarea_input = 0

    if furnishingstatus_input == 'furnished':
        furnishingstatus_input = 0
    elif furnishingstatus_input == 'semi-furnished':
        furnishingstatus_input = 1
    elif furnishingstatus_input == 'unfurnished':
        furnishingstatus_input = 2

    model = joblib.load('{}/DecisionTreeRegressor'.format(root))

    prediction = model.predict([[furnishingstatus_input,parking_input, bedrooms_input,stories_input,bathrooms_input, mainroad_input, guestroom_input, basement_input,hotwaterheating_input, prefarea_input,airconditioning_input]])

    pred1, pred2 = st.columns(2)

    with pred1:
        with open('./animations/cool.json', 'r') as file:
            animation2 = json.load(file)

            st_lottie(
                animation2,
                width=360
            )

    with pred2:
        st.header('Predicted House Value')
        st.header('{:,} USD'.format(prediction[0]))


# -*- coding:utf-8 -*-

from bs4 import BeautifulSoup
from touyantong.settings import IMAGE_SERVICE, TAGS
import re

def is_standard_image(img):
    previous_deep = 0
    while previous_deep < 3:
        img = img.find_previous("p")
        if not img:
            return [False, None]
        if re.match(r"图\d", img.text.replace(" ", "")):
            img = re.sub(r"[\s+\.\!\/_,$%:^*(+\"\']+|[表图\d+——：！，。？、~@#￥%……&*（）]+", "", img.text.replace(" ", ""))
            return [True, img]
        else:
            previous_deep += 1
            continue
    return [False, None]

def extract_images(soup, hash):
    all_img = soup.find_all("img")
    r = [{"caption": is_standard_image(img)[1],
          "src": IMAGE_SERVICE + hash + "/" + img.get("src")} for img in all_img if is_standard_image(img)[0]]
    if not all_img:
        return []
    return r

def main(origin, category, hash):
    soup = BeautifulSoup(origin, "lxml")
    try:
        type_id = [tag["type_id"] for tag in TAGS if tag["abbreviation"] == category][0]
    except IndexError:
        return []
    if type_id != 3:
        return []
    return extract_images(soup, hash)


# -*- coding: utf-8 -*-
"""
title: CartPole-v0 example
subtitle:
keywords: [Gym, OpenAI, Deep Learning, Reinforcement Learning, AI/ML]
source:
    - link: https://gym.openai.com/envs/CartPole-v1/
    - title: Using Keras Reinforcement Learning API with OPENAI GYM
      link: https://medium.com/@abhishek.bn93/using-keras-reinforcement-learning-api-with-openai-gym-6c2a35036c83
      date: 2019-02-10
      author:
          - nick:
            fullname: Abhishek Nagaraja
            email:
file:
    name:
    path:
    date: 2019-03-14 Thu 10:30:00
    author:
        - nick: kasprark
          fullname: Arkadiusz Kasprzyk
          email:
              - arkadiusz.kasprzyk@tieto.com
              - akasp@interia.pl
              - akasp666@gmail.com
              - arek@staart.pl
"""

#%%

import gym
import random
import numpy as np
import rl   # pip install keras-rl
from keras.layers import Dense, Flatten
from keras.models import Sequential
from keras.optimizers import Adam

#%%

env = gym.make('CartPole-v1')

dir(env.observation_space)
env.observation_space.shape  # dimension of observation space
env.observation_space.high
env.observation_space.low
env.observation_space.sample()
env.observation_space.np_random  # ?

env.action_space.n  # nr of possible actions

#%%

episodes = range(20)

for _ in episodes:
    state = env.reset()
    done = False
    score = 0
    i = 0
    while not done:
        env.render()
        action = random.choice([0, 1])
        state, reward, done, info = env.step(action)
        score += reward
        i += 1
    print("episode {:2} - iters {:2} - score {:4.1f}".format(_, i, score))

#%%
#%% Q-table
def agent(states, actions):
    model = Sequential()
    model.add(Flatten(input_shape = (1, states)))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(actions, activation='relu'))
    return model

model = agent(env.observation_space.shape[0], env.action_space.n)

model.summary()

#%%
from rl.agents import SARSAAgent
from rl.policy import EpsGreedyQPolicy

policy = EpsGreedyQPolicy()

sarsa = SARSAAgent(model=model,
                   policy=policy,
                   nb_actions=env.action_space.n   # from env !
                  )

sarsa.compile('adam', metrics=['mse'])   # just model.compile(...)

sarsa.fit(env, nb_steps=5e4, visualize=False, verbose=1)

#%%
scores = sarsa.test(env, nb_episodes=100, visualize=False)
mean_score = np.mean(scores.history['episode_reward'])
print('Average score over 100 test games: {}'.format(mean_score))

#%%
sarsa.save_weights('sarsa_weights.h5f', overwrite=True)

#%%
sarsa.load_weights('sarsa_weights.h5f')

#%%
# how the trained agent works

sarsa.test(env, nb_episodes=2, visualize=True)

#%%

sarsa.get_config()


import cv2
import os
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

window_height = 300
window_width = 400

canvas = tk.Tk()
canvas.geometry("400x300")
canvas.title("PNG Converter")
canvas.iconbitmap('images/icon.ico')
canvas.configure(bg='gray')
canvas.resizable(False, False)
screen_width = canvas.winfo_screenwidth()
screen_height = canvas.winfo_screenheight()
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))
canvas.geometry("{}x{}+{}+{}".format(window_width,
                window_height, x_cordinate, y_cordinate))

canvas.filename = ""

def jpeg_maker(input_path, target_format):
    if input_path == "":
        return

    output_path = os.path.splitext(input_path)[0] + target_format
    print(output_path)
    image = cv2.imread(input_path)
    cv2.imwrite(output_path, image, [
        int(cv2.IMWRITE_JPEG_QUALITY), 100])
    messagebox.showinfo(
        "Information", "File Conversion Successful! Output placed in Source folder")

def open_directory():
    canvas.filename = filedialog.askopenfilename(initialdir="C:/Users/Infinity/Documents", title="Select A File",
                                                 filetypes=(("PNG Files", "*.png"), ("all files", "*.*")))
    jpeg_maker(canvas.filename, '.jpg')

browse_button = tk.Button(canvas, text="Browse File",
                          command=open_directory, font=("SAN_SERIF", 20, "bold"))
browse_button.pack(pady=100)

canvas.mainloop()


import discord
from discord.ext import commands

import os, dotenv

bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())

@bot.event
async def on_ready():
    print(f'Bot is ready!')

# When we want to create a command that will take a string as an argument, we need to add the * before the argument. Like this :
# @bot.command
# async def command_name(ctx : commands.Context, *, string):
#     ...

# For example, if we want to create a command that will send a message, we can do it like this:
@bot.command()
async def send_message(ctx : commands.Context, *, message : str):
    await ctx.send(message)

# Now we can call the command with the prefix we have set. Like this : !send_message Hello World!
# If we did not add the * before the argument, the command would not work because it would take only the first word of the message.
# We need to know that we can not add any other argument after the string argument .
# But we can add as many arguments as we want before the string argument.

if __name__ == '__main__':
    """
    For all the courses, do not worry about the token it is just for personnal security purposes
    """
    dotenv.load_dotenv()
    token = os.getenv('token')

    if token is None:
        raise ValueError('Token not found')

    bot.run(token)

chapter 2 - commands/2.4 - string_command.py


import sys
import xml.etree.ElementTree as ET

try:
    # Py2.7 and newer
    from xml.etree.ElementTree import ParseError as StdlibParseError
except ImportError:
    # Py2.6 and older
    from xml.parsers.expat import ExpatError as StdlibParseError

from appstream.errors import ParseError, ValidationError

if sys.version_info[0] == 2:
    # Python2 has a nice basestring base class
    string_types = (basestring,)
else:
    # But python3 has distinct types
    string_types = (str, bytes)

def _join_lines(txt):
    """ Remove whitespace from XML input """
    txt = txt or ''  # Handle NoneType input values
    val = ''
    lines = txt.split('\n')
    for line in lines:
        stripped = line.strip()
        if len(stripped) == 0:
            continue
        val += stripped + ' '
    return val.strip()

def _parse_desc(node):
    """ A quick'n'dirty description parser """
    desc = ''
    for n in node:
        if n.tag == 'p':
            desc += '
' + _join_lines(n.text) + '
'
        elif n.tag == 'ol' or n.tag == 'ul':
            desc += '
'
            for c in n:
                if c.tag == 'li':
                    desc += '
' + _join_lines(c.text) + '
'
                else:
                    raise ParseError('Expected
 in <%s>, got <%s>', n.tag, c.tag)
            desc += '
'
        else:
            raise ParseError('Expected
,
,
 in <%s>, got <%s>', node.tag, n.tag)
    return desc

class Checksum(object):
    def __init__(self):
        """ Set defaults """
        self.kind = 'sha1'
        self.target = None
        self.value = None
        self.filename = None
    def to_xml(self):
        return '        <checksum filename="%s" target="%s" type="sha1">%s</checksum>\n' % (self.filename, self.target, self.value)
    def _parse_tree(self, node):
        """ Parse a <checksum> object """
        if 'filename' in node.attrib:
            self.filename = node.attrib['filename']
        if 'type' in node.attrib:
            self.kind = node.attrib['type']
        if 'target' in node.attrib:
            self.target = node.attrib['target']
        self.value = node.text

class Release(object):
    def __init__(self):
        """ Set defaults """
        self.version = None
        self.description = None
        self.timestamp = 0
        self.checksums = []
        self.location = None
        self.size_installed = 0
        self.size_download = 0

    def get_checksum_by_target(self, target):
        """ returns a checksum of a specific kind """
        for csum in self.checksums:
            if csum.target == target:
                return csum
        return None

    def add_checksum(self, csum):
        """ Add a checksum to a release object """
        for csum_tmp in self.checksums:
            if csum_tmp.target == csum.target:
                self.checksums.remove(csum_tmp)
                break
        self.checksums.append(csum)

    def _parse_tree(self, node):
        """ Parse a <release> object """
        if 'timestamp' in node.attrib:
            self.timestamp = int(node.attrib['timestamp'])
        if 'version' in node.attrib:
            self.version = node.attrib['version']
            # fix up hex value
            if self.version.startswith('0x'):
                self.version = str(int(self.version[2:], 16))
        for c3 in node:
            if c3.tag == 'description':
                self.description = _parse_desc(c3)
            if c3.tag == 'size':
                if 'type' not in c3.attrib:
                    continue
                if c3.attrib['type'] == 'installed':
                    self.size_installed = int(c3.text)
                if c3.attrib['type'] == 'download':
                    self.size_download = int(c3.text)
            elif c3.tag == 'checksum':
                csum = Checksum()
                csum._parse_tree(c3)
                self.add_checksum(csum)

    def to_xml(self):
        xml = '      <release version="%s" timestamp="%i">\n' % (self.version, self.timestamp)
        if self.size_installed > 0:
            xml += '        <size type="installed">%i</size>\n' % self.size_installed
        if self.size_download > 0:
            xml += '        <size type="download">%i</size>\n' % self.size_download
        if self.location:
            xml += '        <location>%s</location>\n' % self.location
        for csum in self.checksums:
            xml += csum.to_xml()
        if self.description:
            xml += '        <description>%s</description>\n' % self.description
        xml += '      </release>\n'
        return xml

class Provide(object):
    def __init__(self):
        """ Set defaults """
        self.kind = None
        self.value = None
    def _parse_tree(self, node):
        """ Parse a <provide> object """
        if node.tag == 'firmware':
            if 'type' in node.attrib and node.attrib['type'] == 'flashed':
                self.kind = 'firmware-flashed'
            self.value = node.text.lower()

class Component(object):
    """ A quick'n'dirty MetaInfo parser """

    def __init__(self):
        """ Set defaults """
        self.id = None
        self.update_contact = None
        self.kind = None
        self.provides = []
        self.name = None
        self.pkgname = None
        self.summary = None
        self.description = None
        self.urls = {}
        self.icons = {}
        self.metadata_license = None
        self.project_license = None
        self.developer_name = None
        self.releases = []
        self.kudos = []

    def to_xml(self):
        xml = '  <component type="firmware">\n'
        if self.id:
            xml += '    <id>%s</id>\n' % self.id
        if self.pkgname:
            xml += '    <pkgname>%s</pkgname>\n' % self.pkgname
        if self.name:
            xml += '    <name>%s</name>\n' % self.name
        if self.summary:
            xml += '    %s\n' % self.summary
        if self.developer_name:
            xml += '    <developer_name>%s</developer_name>\n' % self.developer_name
        if self.description:
            xml += '    <description>%s</description>\n' % self.description
        for key in self.urls:
            xml += '    <url type="%s">%s</url>\n' % (key, self.urls[key])
        for key in self.icons:
            xml += '    <icon type="%s">%s</icon>\n' % (key, self.icons[key]['value'])
        if len(self.releases) > 0:
            xml += '    <releases>\n'
            for rel in self.releases:
                xml += rel.to_xml()
            xml += '    </releases>\n'
        if len(self.kudos) > 0:
            xml += '    <kudos>\n'
            for kudo in self.kudos:
                xml += '      <kudo>%s</kudo>\n' % kudo
            xml += '    </kudos>\n'
        if len(self.provides) > 0:
            xml += '    <provides>\n'
            for p in self.provides:
                xml += '      <firmware type="flashed">%s</firmware>\n' % p.value
            xml += '    </provides>\n'
        xml += '  </component>\n'
        return xml

    def add_release(self, release):
        """ Add a release object if it does not already exist """
        for r in self.releases:
            if r.version == release.version:
                return
        self.releases.append(release)

    def add_provide(self, provide):
        """ Add a provide object if it does not already exist """
        for p in self.provides:
            if p.value == provide.value:
                return
        self.provides.append(provide)

    def get_provides_by_kind(self, kind):
        """ Returns an array of provides of a certain kind """
        provs = []
        for p in self.provides:
            if p.kind == kind:
                provs.append(p)
        return provs

    def validate(self):
        """ Parse XML data """
        if not self.id or len(self.id) == 0:
            raise ValidationError('No <id> tag')
        if not self.name or len(self.name) == 0:
            raise ValidationError('No <name> tag')
        if not self.summary or len(self.summary) == 0:
            raise ValidationError('No  tag')
        if not self.description or len(self.description) == 0:
            raise ValidationError('No <description> tag')
        if self.kind == 'firmware':
            if len(self.provides) == 0:
                raise ValidationError('No <provides> tag')
            if len(self.releases) == 0:
                raise ValidationError('No <release> tag')
        if not self.metadata_license or len(self.metadata_license) == 0:
            raise ValidationError('No <metadata_license> tag')
        if self.metadata_license not in ['CC0-1.0']:
            raise ValidationError('Invalid <metadata_license> tag')
        if not self.project_license or len(self.project_license) == 0:
            raise ValidationError('No <project_license> tag')
        if not self.developer_name or len(self.developer_name) == 0:
            raise ValidationError('No <developer_name> tag')

        # verify release objects
        for rel in self.releases:
            if not rel.version or len(rel.version) == 0:
                raise ValidationError('No version in <release> tag')
            if rel.timestamp == 0:
                raise ValidationError('No timestamp in <release> tag')

    def parse(self, xml_data):
        """ Parse XML data """

        # parse tree
        if isinstance(xml_data, string_types):
            # Presumably, this is textual xml data.
            try:
                root = ET.fromstring(xml_data)
            except StdlibParseError as e:
                raise ParseError(str(e))
        else:
            # Otherwise, assume it has already been parsed into a tree
            root = xml_data

        # get type
        if 'type' in root.attrib:
            self.kind = root.attrib['type']

        # parse component
        for c1 in root:

            # <id>
            if c1.tag == 'id':
                self.id = c1.text

            # <updatecontact>
            elif c1.tag == 'updatecontact' or c1.tag == 'update_contact':
                self.update_contact = c1.text

            # <metadata_license>
            elif c1.tag == 'metadata_license':
                self.metadata_license = c1.text

            # <releases>
            elif c1.tag == 'releases':
                for c2 in c1:
                    if c2.tag == 'release':
                        rel = Release()
                        rel._parse_tree(c2)
                        self.add_release(rel)

            # <provides>
            elif c1.tag == 'provides':
                for c2 in c1:
                    prov = Provide()
                    prov._parse_tree(c2)
                    self.add_provide(prov)

            # <kudos>
            elif c1.tag == 'kudos':
                for c2 in c1:
                    if not c2.tag == 'kudo':
                        continue
                    self.kudos.append(c2.text)

            # <project_license>
            elif c1.tag == 'project_license' or c1.tag == 'licence':
                self.project_license = c1.text

            # <developer_name>
            elif c1.tag == 'developer_name':
                self.developer_name = _join_lines(c1.text)

            # <name>
            elif c1.tag == 'name' and not self.name:
                self.name = _join_lines(c1.text)

            # <pkgname>
            elif c1.tag == 'pkgname' and not self.pkgname:
                self.pkgname = _join_lines(c1.text)

            #
            elif c1.tag == 'summary' and not self.summary:
                self.summary = _join_lines(c1.text)

            # <description>
            elif c1.tag == 'description' and not self.description:
                self.description = _parse_desc(c1)

            # <url>
            elif c1.tag == 'url':
                key = 'homepage'
                if 'type' in c1.attrib:
                    key = c1.attrib['type']
                self.urls[key] = c1.text

            elif c1.tag == 'icon':
                key = c1.attrib.pop('type', 'unknown')
                c1.attrib['value'] = c1.text
                self.icons[key] = self.icons.get(key, []) + [c1.attrib]


# samplesetごとのSTRINGDBのH, L、Mの数を表形式でまとめる
# %%
import os

import matplotlib.pyplot as plt
import pandas as pd
from dotenv import load_dotenv

from src.util.metrics import Metrics
from src.util.similarity_strategy import (
    INTERACTION_SIMILARITY_STRATEGIES,
    DirectStringScore,
)
from thesis.utils.matplotlib_format import MATPLOTLIB_CONFIG
from thesis.utils.reportset import COMPARE_REPORT_SET

load_dotenv()

THESIS_FIG_PATH = os.environ["THESIS_FIG_PATH"]
THESIS_TB_PATH = os.environ["THESIS_TB_PATH"]

TB_SAVEDIR = os.path.join(THESIS_TB_PATH, "results", "stringdb")

if not os.path.exists(TB_SAVEDIR):
    os.makedirs(TB_SAVEDIR)

for key, value in MATPLOTLIB_CONFIG.items():
    plt.rcParams[key] = value

# %%
sampledata_num_table = pd.DataFrame(
    index=pd.MultiIndex.from_product(
        [COMPARE_REPORT_SET.keys(), COMPARE_REPORT_SET["HepG2"].keys()]
    ),
    columns=["high-confidence", "midium-confidence", "low-confidence"],
)

for biosample in COMPARE_REPORT_SET:
    for condition in COMPARE_REPORT_SET[biosample]:
        sampleset = COMPARE_REPORT_SET[biosample][condition]
        data = Metrics(sampleset.report)(
            [
                *INTERACTION_SIMILARITY_STRATEGIES,
                DirectStringScore(metrics="score", qcut=True),
            ],
            add_description=False,
        )

        assert isinstance(data, pd.DataFrame)

        sampledata_num_table.loc[(biosample, condition)] = data.replace(
            {
                "STRING Score": {
                    "h": "high-confidence",
                    "m": "midium-confidence",
                    "l": "low-confidence",
                }
            },
            inplace=False,
        )["STRING Score"].value_counts()
# %%

sampledata_num_table.style.format("{:,d}").format_index(
    escape="latex", axis=1
).format_index(escape="latex", axis=0).to_latex(
    os.path.join(TB_SAVEDIR, "stringdb_compareset_count.tex"),
    column_format="llrrr",
    position="tbp",
    position_float="centering",
    hrules=True,
    caption="比較セットごとのタンパク質相互作用のペアの数",
    label="tab:stringdb_compareset_count",
)

# %%


import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
import sys, math

Filename = None
# Crit_code = None
Data = None
Exp_val = None
Dev = None

def get_data_from_file() -> list:
  try:
    f = open(Filename, 'r')
    data = list(map(int, f.read().split(',')))
  except:
    print_help()
    exit(0)
  else:
    return data

def print_help():
  print('В данной программе определены следующие параметры:\n')
  print('\t/f:<имя_файла> - с помощью данной команды ')

# def choose_mode() -> str:
#   if Crit_code == 'hi':
#     print('hi-square')
#   elif Crit_code == 'se':
#     print('series')
#   elif Crit_code == 'in':
#     print('intervals')
#   elif Crit_code == 'di':
#     print('disintegration')
#   elif Crit_code == 'mo':
#     print('monotomy')
#   elif Crit_code == 'co':
#     print('conflict')

#   return ''

def get_probability_of_slice(seq : list) -> dict:
  probs = {}
  for el in seq:
    if el not in probs:
      probs[el] = 0
    probs[el] += 1
  for k, val in probs.items():
    probs[k] = val / len(seq)
  return probs

def get_expected_value(probs : dict) -> int:
  mu = 0
  for el, p in probs.items():
    mu += el * p
  return mu

def get_exp_val_and_dev() -> None:
  global Exp_val, Dev
  n = len(Data)
  step = (n // 100, 1)[n <= 100]
  l = n // step
  Exp_val = []
  posr = step
  for _ in range(l):
    Exp_val.append(get_expected_value(get_probability_of_slice(Data[0:posr])))
    posr += step
  Dev = []
  posr = step
  for l in range(len(Exp_val)):
    s = 0
    for el in Data[0:posr]:
      s += (el - Exp_val[l]) * (el - Exp_val[l])
    posr += step
    Dev.append(math.sqrt(s / step))
  return

def choose_mode() -> None:
  while True:
    print('\n1. Построить график зависимости математического ожидания от объема выборки')
    print('2. Построить график зависимости среднеквадратического отклонения от объема выборки')
    print('3. Вывести информацию о текущей ППСЧ')
    print('4. Выход')
    bl = input('Выберите опцию: ')
    if bl == '1':
      xs = [i for i in range(len(Exp_val))]
      xd = Exp_val[len(Exp_val) // 2]
      rel_err = round(abs(xd - Exp_val[-1]) / xd * 100, 2)
      df = pd.DataFrame({'Объём выборки': xs, 'Математическое ожидание': Exp_val})
      plt.figure(figsize=(10, 6), constrained_layout=True)
      t = sns.scatterplot(data=df, x='Объём выборки', y='Математическое ожидание', color='red')
      t.set(xlabel='Относительная погрешность измерения мат. ожидания ' +
                   f'для выборки из {len(Data)} элементов: {rel_err}%')
      plt.show()
    elif bl == '2':
      xs = [i for i in range(len(Dev))]
      xd = Dev[len(Dev) // 2]
      rel_err = round(abs(xd - Dev[-1]) / xd * 100, 2)
      df = pd.DataFrame({'Объём выборки': xs, 'Cреднеквадратичное отклонение': Dev})
      plt.figure(figsize=(10, 6), constrained_layout=True)
      t = sns.scatterplot(data=df, x='Объём выборки', y='Cреднеквадратичное отклонение', color='blue')
      t.set(xlabel='Относительная погрешность измерения средн. отклонения'
                   f' для выборки из {len(Data)} элементов: {rel_err}%')
      plt.show()
    elif bl == '4':
      break
  return

if __name__ == '__main__':
  for el in sys.argv:
    if '/f:' in el:
      Filename = el[3:]
      Data = get_data_from_file()
      mx = max(Data) + 1
      Data = [i / mx for i in Data]
    elif '/h' in el:
      print_help()
  if Filename == None or Data == None:
    print_help()
  else:
    get_exp_val_and_dev()
    choose_mode()


from Tkinter import *
from PIL import Image, ImageDraw

class canvasUI(object):
    def __init__(self):
        self.master = Tk()
        self.canvas_width = 300
        self.canvas_height = 300
        self.win = None
        self.img = None
        self.draw = None
        self.initCanvas()
    def initCanvas(self):
        self.master.title( "Handwritten Digit Recognizer" )
        self.win = Canvas(self.master, width=self.canvas_width, height=self.canvas_height, bg='white')
        self.win.pack(expand = YES, fill = BOTH)
        self.win.bind( "<B1-Motion>", self.paint )

        self.img = Image.new('RGB', (self.canvas_width, self.canvas_height), 'white')
        self.draw = ImageDraw.Draw(self.img)

        message = Label( self.master, text = "Press and Drag the mouse to draw" )
        message.pack( side = BOTTOM )

        button = Button(self.master, text='Predict', command=self.save, anchor = SW)
        button.pack()

        clr = Button(self.master, text='Clear', command=self.clear, anchor = NW)
        clr.pack()

        mainloop()
        self.img.close()

        img = Image.open('utils/temp.jpg')
        img = img.resize((28,28))
        img.save('utils/temp.jpg')
        img.close()

    def clear(self):
        self.win.delete("all")
        self.img = Image.new('RGB', (self.canvas_width, self.canvas_height), 'white')
        self.draw = ImageDraw.Draw(self.img)

    def save(self):
        filename="utils/temp.jpg"
        self.img.save(filename)
        self.master.destroy()

    def paint(self, event):
        x1, y1 = event.x, event.y
        x2, y2 = ( event.x + 20), ( event.y + 20 )
        self.win.create_oval( x1, y1, x2, y2, fill = "#000000", width=5 )
        self.draw.ellipse([x1,y1,x2,y2], fill='black')


from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from rest_framework import serializers, status
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import Feedback

class FeedbackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Feedback
        fields = '__all__'

@method_decorator(csrf_exempt, name='dispatch')
class FeedbackView(APIView):
    @csrf_exempt
    def post(self, request, format=None):
        if self.request.user.is_authenticated():
            user = self.request.user
        else:
            user = None
        if 'user' in request.data:
            del request.data['user']

        user_agent = request.data.get('user_agent')
        if not user_agent:
            user_agent = request.META.get('HTTP_USER_AGENT', None)

        serializer = FeedbackSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(user=user, user_agent=user_agent)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


from django.conf import settings
from django.urls import re_path, path

from .views import (
    LoadQuestions,
    LoadScore,
    ListQuizes,
    LoadPreferences,
    SaveUserCustomQuiz,
)

app_name = 'quiz'

urlpatterns = [
    re_path(
        route=r'^question(?:/(?P<pk>\d+))?/?$',
        view=LoadQuestions.as_view(),
        name='questions',
    ),
    re_path(
        route=r'^score(?:/(?P<pk>\d+))?/?$',
        view=LoadScore.as_view(),
        name='score',
    ),
    re_path(
        route=r'^quiz-list(?:/(?P<pk>\d+))?/?$',
        view=ListQuizes.as_view(),
        name='quiz',
    ),
    re_path(
        route=r'^preferences(?:/(?P<pk>\d+))?/?$',
        view=LoadPreferences.as_view(),
        name='preferences',
    ),
    re_path(
        route=r'^preferences/update(?:/(?P<pk>\d+))?/?$',
        view=LoadPreferences.as_view(),
        name='preferences_update',
    ),
    re_path(
        route=r'^save-custom-quiz/?$',
        view=SaveUserCustomQuiz.as_view(),
        name='save_custom_quiz',
    )
]

if settings.DEBUG:
    from .admin import router
    urlpatterns += router.urls

    from django.http import HttpRequest

    from rest_framework.reverse import reverse as api_reverse
    from rest_framework.views import APIView
    from rest_framework.response import Response

    class RedirectAPI(APIView):
        def get(self, request: HttpRequest, format=None):
            quiz_urls = {
                'quiz': f"http://127.0.0.1:8000{api_reverse(viewname='quiz:api-quiz-list')}?page=1",
                'questions': f"http://127.0.0.1:8000{api_reverse(viewname='quiz:api-question-list')}?page=1",
                'answers': f"http://127.0.0.1:8000{api_reverse(viewname='quiz:api-answer-list')}?page=1",
                'score': f"http://127.0.0.1:8000{api_reverse(viewname='quiz:api-score-list')}?page=1",
                'preferences': f"http://127.0.0.1:8000{api_reverse(viewname='quiz:api-preferences-list')}?page=1",
            }
            return Response(data=quiz_urls)

    urlpatterns += [
        re_path(
            route=r'^quiz-viewsets$',
            view=RedirectAPI.as_view(),
            name='quiz_viewsets'
        ),
    ]


from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from ip_app.models import IpLogModel

class IplogTests(APITestCase):

    def test_create_log_record(self):
        url = reverse('ip-list')
        response = self.client.post(url, {}, format='json', HTTP_X_REAL_IP='127.0.0.1')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(IpLogModel.objects.count(), 1)
        self.assertEqual(IpLogModel.objects.get().ip, '127.0.0.1')

    def test_create_log_record_fail(self):
        url = reverse('ip-list')
        response = self.client.post(url, {}, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(IpLogModel.objects.count(), 0)

    def test_get_list(self):
        url = reverse('ip-list')
        self.client.post(url, {}, format='json', HTTP_X_REAL_IP='127.0.0.1')
        self.client.post(url, {}, format='json', HTTP_X_REAL_IP='172.10.0.2')
        self.assertEqual(IpLogModel.objects.count(), 2)
        response = self.client.get(url)
        self.assertEqual(2, len(response.data))
        self.assertEqual('127.0.0.1', response.data[0]['ip'])
        response = self.client.get(url + '?limit=1&offset=1')
        self.assertEqual('172.10.0.2', response.data['results'][0]['ip'])


import functools

from absl.testing import absltest
import numpy as np

from trax.layers import test_utils
from trax.supervised import decoding

def arithmetic_sequence(input_seq, limit=10):
  # Increment the last symbol. Wrap to [0, 10).
  return (input_seq[-1] + 1) % limit

class TestUtilsTest(absltest.TestCase):

  def test_mock_transformer_lm_eval_equals_predict(self):
    model_fn = functools.partial(
        test_utils.MockTransformerLM,
        sequence_fn=arithmetic_sequence,
        vocab_size=10,
    )
    test_utils.test_eval_equals_predict_discrete(model_fn, vocab_size=10)

  def test_mock_transformer_lm_decodes_arithmetic_sequence(self):
    model = test_utils.MockTransformerLM(
        sequence_fn=arithmetic_sequence,
        vocab_size=10,
        mode='predict',
    )
    output = decoding.autoregressive_sample(
        model, max_length=5, start_id=0, eos_id=-1, accelerate=False
    )

    # Sequence including the leading 0 and the last predicted symbol.
    full_seq = list(range(6))
    # decoding.autoregressive_sample doesn't return the leading 0.
    np.testing.assert_array_equal(output, [full_seq[1:]])
    # The prediction buffers don't include the last predicted symbol.
    model.assert_prediction_buffers_equal([full_seq[:-1]])

  def test_mock_transformer_lm_rewinds(self):
    model = test_utils.MockTransformerLM(
        sequence_fn=arithmetic_sequence,
        vocab_size=10,
        mode='predict',
    )
    sample_3 = functools.partial(
        decoding.autoregressive_sample,
        max_length=3,
        eos_id=-1,
        accelerate=False,
    )

    # Generate the 3 initial symbols.
    init_output = sample_3(model, start_id=0)
    np.testing.assert_array_equal(init_output, [[1, 2, 3]])
    state = model.state

    # Generate the next 3 symbols.
    next_output = sample_3(model, start_id=init_output[0, -1])
    np.testing.assert_array_equal(next_output, [[4, 5, 6]])

    # Rewind and generate the last 3 symbols again.
    model.state = state
    next_output = sample_3(model, start_id=init_output[0, -1])
    np.testing.assert_array_equal(next_output, [[4, 5, 6]])

    # Check the buffers.
    model.assert_prediction_buffers_equal([[0, 1, 2, 3, 4, 5]])

if __name__ == '__main__':
  absltest.main()


#!/usr/bin/env python3.5
# -*- encoding: utf-8  -*-
"""
@Version: 1.0
@Author: tianbaorenjie
@License:
@Contact: tianbaorenjie@163.com
@Site: http://
@Software: PyCharm
@Project: AngularPy_dev
@File: asset.py
@Time: 9/27/17 8:11 PM
"""
from users.utils import *
from ..models import ServerAsset
from .. import forms

from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView

from django.urls import reverse_lazy

import json

class ServerAssetListView(AdminUserRequiredMixin, TemplateView):
    template_name = "cmdb/asset_list.html"

    def get_context_data(self, **kwargs):
        context = {
            "app": "资产管理",
            "action": "资产信息",
        }
        kwargs.update(context)
        return super(ServerAssetListView, self).get_context_data(**kwargs)

class ServerAssetCreateView(AdminUserRequiredMixin, CreateView):
    model = ServerAsset
    form_class = forms.ServerAssetCreateForm
    template_name = "cmdb/asset_create_update.html"
    success_url = reverse_lazy("cmdb:asset-list")

    def form_valid(self, form):
        self.asset = asset = form.save()
        asset.create_by = self.request.user.username or "Admin"
        asset.save()
        return super(ServerAssetCreateView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = {
            "app": "资产管理",
            "action": "创建资产",
        }
        kwargs.update(context)
        return super(ServerAssetCreateView, self).get_context_data(**kwargs)

class ServerAssetUpdateView(AdminUserRequiredMixin, UpdateView):
    model = ServerAsset
    form_class = forms.ServerAssetUpdateForm
    template_name = "cmdb/asset_create_update.html"

    def get_context_data(self, **kwargs):
        context = {
            "app": "资产管理",
            "action": "资产信息更新",
        }
        kwargs.update(context)
        return super(ServerAssetUpdateView, self).get_context_data(**kwargs)

    def form_invalid(self, form):
        print(form.errors)
        return super(ServerAssetUpdateView, self).form_invalid(form)

    def get_success_url(self):
        success_url = reverse_lazy("cmdb:asset-detail", kwargs={"pk": self.object.pk})
        return success_url

class ServerAssetDetailView(DeleteView):
    model = ServerAsset
    context_object_name = "asset"
    template_name = "cmdb/asset_detail.html"

    def get_context_data(self, **kwargs):
        networks = self.get_networks(),
        disks = self.get_disks(),
        mounts = self.get_mounts(),
        context = {
            "app": "资产信息",
            "action": "资产状态详情",
            "networks": networks,
            "disks": disks,
            "mounts": mounts,
        }
        kwargs.update(context)
        return super(ServerAssetDetailView, self).get_context_data(**kwargs)

    def get_networks(self):
        if self.object.networks is None:
            return "None"
        return json.loads(self.object.networks)

    def get_disks(self):
        if self.object.disks is None:
            return "None"
        return json.loads(self.object.disks)

    def get_mounts(self):
        if self.object.mounts is None:
            return "None"
        return json.loads(self.object.mounts)


import logging
import pickle
from collections import OrderedDict
import traceback
import hashlib

import torch
from lightning import Trainer
from lightning.pytorch.callbacks import RichProgressBar, RichModelSummary
from lightning.pytorch.callbacks.progress.rich_progress import RichProgressBarTheme
import copy

from fedstellar.learning.exceptions import DecodingParamsError, ModelNotMatchingError
from fedstellar.learning.learner import NodeLearner
from torch.nn import functional as F

###########################
#    LightningLearner     #
###########################

class LightningLearner(NodeLearner):
    """
    Learner with PyTorch Lightning.

    Atributes:
        model: Model to train.
        data: Data to train the model.
        epochs: Number of epochs to train.
        logger: Logger.
    """

    def __init__(self, model, data, config=None, logger=None):
        self.model = model
        # self.model = torch.compile(model)  # PyTorch 2.0
        self.data = data
        self.config = config
        self.logger = logger
        self.__trainer = None
        self.epochs = 1
        logging.getLogger("lightning.pytorch").setLevel(logging.INFO)

        # FL information
        self.round = 0
        # self.local_step = 0
        # self.global_step = 0

        self.logger.log_metrics({"Round": self.round}, step=self.logger.global_step)

    def get_round(self):
        return self.round

    def set_model(self, model):
        self.model = model

    def set_data(self, data):
        self.data = data

    ####
    # Model weights
    ####
    def encode_parameters(self, params=None, contributors=None, weight=None):
        if params is None:
            params = self.model.state_dict()
        array = [val.cpu().numpy() for _, val in params.items()]
        return pickle.dumps(array)

    def decode_parameters(self, data):
        try:
            params_dict = zip(self.model.state_dict().keys(), pickle.loads(data))
            return OrderedDict({k: torch.tensor(v) for k, v in params_dict})
        except:
            raise DecodingParamsError("Error decoding parameters")

    def check_parameters(self, params):
        # Check ordered dict keys
        if set(params.keys()) != set(self.model.state_dict().keys()):
            return False
        # Check tensor shapes
        for key, value in params.items():
            if value.shape != self.model.state_dict()[key].shape:
                return False
        return True

    def set_parameters(self, params):
        try:
            self.model.load_state_dict(params)
        except:
            raise ModelNotMatchingError("Not matching models")

    def get_parameters(self):
        return self.model.state_dict()

    def get_hash_model(self):
        '''
        Returns:
            str: SHA256 hash of model parameters
        '''
        return hashlib.sha256(self.encode_parameters()).hexdigest()

    def set_epochs(self, epochs):
        self.epochs = epochs

    def fit(self):
        try:
            if self.epochs > 0:
                self.create_trainer()
                # torch.autograd.set_detect_anomaly(True)
                self.__trainer.fit(self.model, self.data)
                self.__trainer = None
        except Exception as e:
            logging.error("Something went wrong with pytorch lightning. {}".format(e))
            # Log full traceback
            logging.error(traceback.format_exc())

    def interrupt_fit(self):
        if self.__trainer is not None:
            self.__trainer.should_stop = True
            self.__trainer = None

    def evaluate(self):
        try:
            if self.epochs > 0:
                self.create_trainer()
                self.__trainer.test(self.model, self.data, verbose=True)
                self.__trainer = None
                # results = self.__trainer.test(self.model, self.data, verbose=True)
                # loss = results[0]["Test/Loss"]
                # metric = results[0]["Test/Accuracy"]
                # self.__trainer = None
                # self.log_validation_metrics(loss, metric, self.round)
                # return loss, metric
            else:
                return None
        except Exception as e:
            logging.error("Something went wrong with pytorch lightning. {}".format(e))
            # Log full traceback
            logging.error(traceback.format_exc())
            return None

    def log_validation_metrics(self, loss, metric, round=None, name=None):
        self.logger.log_metrics({"Test/Loss": loss, "Test/Accuracy": metric}, step=self.logger.global_step)
        pass

    def get_num_samples(self):
        return (
            len(self.data.train_dataloader().dataset),
            len(self.data.test_dataloader().dataset),
        )

    def finalize_round(self):
        self.logger.global_step = self.logger.global_step + self.logger.local_step
        self.logger.local_step = 0
        self.round += 1
        self.logger.log_metrics({"Round": self.round}, step=self.logger.global_step)
        pass

    def create_trainer(self):
        logging.debug("[Learner] Creating trainer with accelerator: {}".format(self.config.participant["device_args"]["accelerator"]))
        progress_bar = RichProgressBar(
            theme=RichProgressBarTheme(
                description="green_yellow",
                progress_bar="green1",
                progress_bar_finished="green1",
                progress_bar_pulse="#6206E0",
                batch_progress="green_yellow",
                time="grey82",
                processing_speed="grey82",
                metrics="grey82",
            ),
            leave=True,
        )
        self.__trainer = Trainer(
            callbacks=[RichModelSummary(max_depth=1), progress_bar],
            max_epochs=self.epochs,
            accelerator=self.config.participant["device_args"]["accelerator"],
            devices="auto" if self.config.participant["device_args"]["accelerator"] == "cpu" else "1",  # TODO: only one GPU for now
            # strategy="ddp" if self.config.participant["device_args"]["accelerator"] != "auto" else None,
            # strategy=self.config.participant["device_args"]["strategy"] if self.config.participant["device_args"]["accelerator"] != "auto" else None,
            logger=self.logger,
            log_every_n_steps=20,
            enable_checkpointing=False,
            enable_model_summary=False,
            enable_progress_bar=True
        )

    def validate_neighbour_model(self, neighbour_model_param):
        avg_loss = 0
        running_loss = 0
        bootstrap_dataloader = self.data.bootstrap_dataloader()
        num_samples = 0
        neighbour_model = copy.deepcopy(self.model)
        neighbour_model.load_state_dict(neighbour_model_param)

        # enable evaluation mode, prevent memory leaks.
        # no need to switch back to training since model is not further used.
        if torch.cuda.is_available():
            neighbour_model = neighbour_model.to('cuda')
        neighbour_model.eval()

        # bootstrap_dataloader = bootstrap_dataloader.to('cuda')

        with torch.no_grad():
            for inputs, labels in bootstrap_dataloader:
                if torch.cuda.is_available():
                    inputs = inputs.to('cuda')
                    labels = labels.to('cuda')
                outputs = neighbour_model(inputs)
                loss = F.cross_entropy(outputs, labels)
                running_loss += loss.item()
                num_samples += inputs.size(0)

        avg_loss = running_loss / len(bootstrap_dataloader)
        logging.debug("[Learner.validate_neighbour]: Computed neighbor loss over {} data samples".format(num_samples))
        return avg_loss


from logging import getLogger
from qtpy.QtCore import (Qt, QModelIndex, QAbstractTableModel,
                         QSortFilterProxyModel)
from .enums import Statuses
from models.all_messages_model import AllMessagesModel
from datetime import datetime
from mps_constants import FROM_1970_TO_1990_IN_SECONDS

class MessageTableModel(QAbstractTableModel):
    """
    author: Evren Keskin
    ===================================================================
    This is a table model that holds the message information for display.
    It is used as a connection between the UI display in history_ui,
    and the models of retrieved data from AllMessagesModel.
    It holds the rows of data that is used for table display in '_data'.
    For now, it is hard coded for LCLS use
    but can easily be adapted for more flexibility
    ===================================================================

    The most important functions are
    __init__: used to create the table header, the hidden columns
    and initialize what the user first sees.
    set_data: used to set the table row data based on MPSFaultModel info.
    This is what the user sees
    set_accepted: sets the shown column based on accepted checkboxes
    """
    logger = getLogger(__name__)

    def __init__(self, parent, messages_model: AllMessagesModel):
        super(MessageTableModel, self).__init__(parent)
        self.message_model = messages_model

        self.hdr_lst = (["Date", "Message"])

        self._data = []  # table data, which will hold inputs from database
        self.status = []
        self.filteringThatOneMessage = False

    def rowCount(self, index: QModelIndex = QModelIndex()):
        """Return the number of rows in the model."""
        return 0 if index.isValid() else len(self._data)

    def columnCount(self, index: QModelIndex = QModelIndex()):
        """Return the number of columns in the model."""
        return len(self.hdr_lst)

    def insertRows(self, new_row, position, rows, parent=QModelIndex()):
        """
        Insert a new message row at the top or bottom based on position negativity
        Negative inserts are from the top, and positives are from the bottom
        """
        if position >= 0:
            self.beginInsertRows(parent, 0, 0)
            self._data.insert(position, new_row)
            self.endInsertRows()
        else:
            self.beginInsertRows(parent, 0, 0)
            self._data.append(new_row)
            self.endInsertRows()
        return True

    def removeRows(self, position, rows, parent=QModelIndex()):
        start, end = position, rows
        self.beginRemoveRows(parent, start, end)
        del self._data[start:end + 1]
        self.endRemoveRows()
        return True

    def data(self, index: QModelIndex, role: Qt.ItemDataRole):
        """Return the index's text, alignment, background color,
        OR foreground color."""
        if not index.isValid():
            return
        elif role == Qt.DisplayRole:
            return str(self._data[index.row()][index.column()])
        elif role == Qt.TextAlignmentRole and 0 < index.column():
            return Qt.AlignCenter
        # elif role == Qt.BackgroundRole:
        #     return Statuses.WHT.brush()
        elif role == Qt.ForegroundRole:
            row = index.row()
            col = index.column()
            if col != 0:
                return self.status[row].brush()
            return Qt.black

    def headerData(self, section: int, orientation: Qt.Orientation,
                   role: Qt.ItemDataRole):
        """Set the horizontal header's text."""
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.hdr_lst[section]

    def initialize_live(self):
        self.lastLiveIndex = 0
        liveMessages = self.message_model.liveMessages
        for index, messageObj in enumerate(liveMessages):
            lst = [messageObj.message] * len(self.hdr_lst)
            lst[0] = datetime.fromtimestamp(messageObj.seconds_from_1990
                                            + FROM_1970_TO_1990_IN_SECONDS)
            lst[1] = messageObj.message

            self.status.append(self.get_status_from_type(messageObj.message_type))

            self.insertRows(lst, self.lastLiveIndex, 1)
            self.lastLiveIndex += 1

    def update_live(self):
        self.message_model.update_live_messages()
        liveMessages = self.message_model.liveMessages[self.lastLiveIndex:]
        for index, messageObj in enumerate(liveMessages):
            lst = [messageObj.message] * len(self.hdr_lst)
            lst[0] = datetime.fromtimestamp(messageObj.seconds_from_1990
                                            + FROM_1970_TO_1990_IN_SECONDS)
            lst[1] = messageObj.message

            self.status.append(self.get_status_from_type(messageObj.message_type))

            self.insertRows(lst, index, 1)
            self.lastLiveIndex += 1

    def update_interactive(self, start, end):
        self.removeRows(0, self.rowCount())

        self.message_model.update_interval_messages(start, end)

        interactiveMessages = self.message_model.interactiveMessages
        for index, messageObj in enumerate(interactiveMessages):
            lst = [messageObj.message] * len(self.hdr_lst)
            lst[0] = datetime.fromtimestamp(messageObj.seconds_from_1990
                                            + FROM_1970_TO_1990_IN_SECONDS)
            lst[1] = messageObj.message

            self.status.append(self.get_status_from_type(messageObj.message_type))

            self.insertRows(lst, -1, 1)

    def get_status_from_type(self, message_type):
        status = Statuses.BGD
        if message_type == 'BD':
            status = Statuses.CYN  # here for comedic effect, this type of message is unused since 2009
        elif message_type == 'BR':
            status = Statuses.BLU
        elif message_type == 'BT':
            status = Statuses.GRN
        elif message_type == 'BV':
            status = Statuses.MAG
        else:  # if messageObj.message_type == 'F':
            status = Statuses.BGD
        return status

    def filter_accepts_row(self, row: int, parent: QModelIndex, filters: dict):
        """Called by MPSSortFilterProxyModel to filter out rows based on
        the table's needs."""
        for col, text in filters.items():
            if text not in str(self._data[row][col]).lower():
                return False
        return True

class MPSSortFilterModel(QSortFilterProxyModel):
    """Customized QSortFilterProxyModel to allow the user to sort and
    filter the customized QAbstractTableModel. Allows for functionality
    for the logic table, summary table,
    bypassed items table, and recent fault table"""
    def __init__(self, parent):
        super(MPSSortFilterModel, self).__init__(parent)
        self.filters = {}

    def setFilterByColumn(self, column: int, text: str):
        """Sets the filters to be used on individual columns."""
        self.filters[column] = text.lower()
        self.invalidateFilter()

    def removeFilterByColumn(self, column: int):
        """Removes the filters from a given column."""
        if column in self.filters:
            del self.filters[column]
            self.invalidateFilter()

    def filterAcceptsRow(self, source_row: int, source_parent: QModelIndex):
        """Override QSortFilterProxyModel's filterAcceptsRow method to
        filter out rows based on the table's needs."""
        return self.sourceModel().filter_accepts_row(source_row,
                                                     source_parent,
                                                     self.filters)