# -*- coding: utf-8 -*-
import pandas as pd
import pyodbc
import datetime
import re
import numpy as np
import codecs
import configparser as cp

_numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64', 'bool']

def _col_profile(col):
    """Convenience function for profiling a column"""
    count = len(col)
    if col.dtype in _numerics:
        total = col.sum()
        mean = col.mean()
    else:
        total = None
        mean = None
    nulls = len(col) - col.count()
    return count, total, mean, nulls

def _num_to_str(num):
    """Convenience function for converting a non-percent numeric to a nicely formatted string"""
    return '{0:,.0f}'.format(num)

def _div_zero_str(numer,denom):
    """Convenience function for dividing by zero and formatting as a nice string"""
    if pd.isnull(denom):
        return 'nan'
    elif denom == 0:
        return '0%'
    else:
        return '{0:.2f}%'.format(numer/denom*100)

def _report_diff(x):
    """Returns difference from values in a 2-element list with left and right values delimited by pipe"""
    return x[0] if x[0] == x[1] else '{} | {}'.format(*x)

def get_file_paths(path):
    """ Creates dictionary of paths to files in a specific directory where keys are the file name (before first period).
    Paths can then be passed to DataComp initialization like sqls["left"]

    Parameters
    ----------
    path : Directory path where scripts are stored

    Returns
    -------
    get_file_paths : dict
        Dictionary of file paths in directory with file names (before first period)
        as keys.

    Examples
    --------
    >>> sqls = "C:/mysqlfiles/"
    """
    import os
    file_paths = {}
    file_list = os.listdir(path)
    for file in file_list:
        file_name = file.rsplit(".",1)
        file_paths[file_name[0]] = path + file
    return file_paths

## DataComp class
class DataComp(object):
    """ Container for comparison of two of data-sets a left and right, from SQL, txt or DataFrame.
    On initialization loads left data from either sql or txt. To complete comparison use add_right_data and compare_data methods.

    Parameters
    ----------
    cnxn_path : Directory path to an ini file with connection information. See cnxn_example.ini in github repo for example.
    left_cnxn_name : The connection name of the left data set in the ini file
    left_script_path: Directory path to SQL script. If None, then expected is txt file and therefore no script
        default None
    datetofrom: Two-element tuple containing date from and date to, to insert into SQL script. Will be ignored for txt source. Must be YYYY-MM-DD format.
        default ("2015-04-01","2015-04-02")
    sql_timeout: Number of seconds after which a timeout will occur and the query will be cancelled
        default None

    Examples
    --------
    >>> dc1 = DataComp("C:/cnxn.ini","sales",sqls["left"],("2015-04-01","2015-05-01"))
    """

    def __init__(self, cnxn_path,left_cnxn_name,left_script_path = None,datetofrom=("2015-04-01","2015-04-02"),sql_timeout = None,**kwargs):
        """Gets connection information from ini file and loads left data into class attribute"""
        self.cnxn_path = cnxn_path
        self.sql_timeout = sql_timeout
        self.left_cnxn_name = left_cnxn_name
        self.left_script_path = left_script_path
        self.datetofrom = datetofrom
        self.left_data, self.left_sql = self._get_data(cnxn_name = self.left_cnxn_name, script_path = self.left_script_path,**kwargs)
        self.right_cnxn_name = None
        self.right_script_path = None
        self.right_sql = None
        self.right_data = None

    def add_right_data(self,right_cnxn_name,right_script_path = None, DataFrame = None,**kwargs):
        """Add right data for comparison, unlike left data, right can be from DataFrame.
        For DataFrame set right_cnxn_name = None and set DataFrame to a named DataFrame object in scope

        Parameters
        ----------
        right_cnxn_name : The connection name of the right data set in the ini file
            set right_cnxn_name = None for DataFrame
        right_script_path: Directory path to SQL script. If None, then expected is txt file and therefore no script
            default None
        DataFrame: A pandas DataFrame, can be used if right_cnxn_name = None

        Examples
        --------
        >>> dc1.add_right_data(right_cnxn_name="sales_sql",right_script_path = sqls["sales"])
        >>> dc1.add_right_data(right_cnxn_name="sales_txt")
        >>> dc1.add_right_data(right_cnxn_name=None,DataFrame = mydf)

        """
        self.right_cnxn_name = right_cnxn_name
        self.right_script_path = right_script_path
        if right_cnxn_name is None:
            self.right_data = self._index_data(df=DataFrame)
        else:
            self.right_data, self.right_sql = self._get_data(cnxn_name = self.right_cnxn_name, script_path = self.right_script_path,**kwargs)

    def _get_data(self,cnxn_name,script_path,**kwargs):
        """Gets data from either text source or SQL source based on what is stored in ini file for specified name
        Also returns the actual sql for inspection, troubleshooting
        """
        if script_path is None:
            expected_type = 'txt'
        else:
            expected_type = 'sql'
        cnxn_info = self._get_cnxn_info(cnxn_name = cnxn_name,expected_type = expected_type)

        if script_path is None:
            sql_script = None
            data = self._populate_dataframe_from_txt(txt_path = cnxn_info[1],**kwargs)
        else:
            sql_script = self._load_sql_script(path=script_path,datetofrom=self.datetofrom)
            data = self._populate_dataframe_from_sql(cnxn_string = cnxn_info[1],cnxn_name = cnxn_name, sql_script = sql_script)

        return data, sql_script

    def _get_cnxn_info(self,cnxn_name,expected_type):
        """Gets sql connection string or txt path from ini file"""
        parser = cp.ConfigParser()
        parser.read(self.cnxn_path)
        assert parser.has_section(cnxn_name), "The cnxn_name provided is not in the ini file specified." + self.cnxn_path
        cnxn_type = parser.get(cnxn_name,'cnxn_type')
        assert cnxn_type == expected_type, "The expected type, " + expected_type + ", is not the same as the cnxn_type, " + cnxn_type +\
            ".\nExpected type is txt if script_path is None, otherwise sql"
        cnxn_string =  parser.get(cnxn_name,'cnxn_string')
        return (cnxn_type,cnxn_string)

    def _load_sql_script(self,path,datetofrom=("2015-04-01","2015-04-02")):
        '''Load SQL and changes all date occurrence pairs to to and from in datetofrom parameter'''
        if not isinstance(path,str):
            raise TypeError('sql path must be string')
        if not isinstance(datetofrom,tuple) or not len(datetofrom) == 2:
            raise TypeError('dates to and from must be in tuple of length 2')
        for d in datetofrom:
            try:
                datetime.datetime.strptime(d,'%Y-%m-%d')
            except ValueError:
                raise ValueError("incorrect date format, should be YYYY-MM-DD")
        # SQL scripts worked with in SSMS may be saved with utf-8 BOM mark which must be replaced with empty string for pyodbc execution
        with open(path,'rb') as f:
            sql_script_bytes = f.read()
        sql_script_bytes = sql_script_bytes.replace(codecs.BOM_UTF8,b'')
        sql_script = sql_script_bytes.decode(encoding='utf-8')
        # Find all occurrences of a date in YYYY-MM-DD format
        ms = list(re.finditer('\\d{4}-\\d{2}-\\d{2}',sql_script))
        # For each pair of date occurrences, insert in first date of tuple then second
        for i, m in enumerate(ms):
            sql_script = sql_script[:m.start()] + datetofrom[i%2] + sql_script[m.end():]
        return sql_script

    def _populate_dataframe_from_sql(self,cnxn_string,cnxn_name,sql_script):
        '''With a valid SQL script and connection, loads SQL data into dataframe'''
        with pyodbc.connect(cnxn_string) as cnxn:
            try:
                if self.sql_timeout is not None:
                    cnxn.timeout = self.sql_timeout
                df = pd.read_sql(sql_script,cnxn)
                assert df.shape[0] > 0, "The SQL executed does not return any rows, check " + sql_script
                ## Basic assumption is left most column is primary key for comparison. This can be set later
                df = self._index_data(df)
                return df
            except TypeError:
                raise AssertionError("SQL script loaded but could not return a dataframe, check to make sure a result-set is actually returned")

    def _populate_dataframe_from_txt(self,txt_path,**kwargs):
        """Creates dataframe from txt file, **kwargs for read_table keyword arguments such as non-tab sep"""
        df = pd.read_table(txt_path,**kwargs)
        df = self._index_data(df)
        return df

    def compare_data(self):
        """ Completely compare the loaded left and right data sets.
        Displaying both testing message and returning six item dictionary for exploration including:
            left_not_right_data: If any, rows in left data not found in right based on shared primary key (default is left most in each data set, unless set_key method used)
            right_not_left_data: If any, rows in right data not found in left based on shared primary key
            diff_summary: Summary level difference at a data-set/column level
            diff_values: Matched rows where at least one value is different, columns with at least one different are moved to left of PK, with different values delimited by pipe

        Returns
        -------
        compare_data : dict
            Dictionary of data frames described above

        Examples
        --------
        >>> dict_data = dc1.compare_data()
        >>> dict_data["diff_summary"]
                         LeftRowCount  RightRowCount  CommonRowCount  DiffValCount  \
             NumericCol         10            11             9           3
             StringCol          10            11             9           1

                         LeftSums  RightSums  TotalDiff  TotalPctDiff  LeftMeans  RightMeans
             NumericCol     470        410        60            0.1463414  47.0       41.0
             StringCol      NaN        NaN        NaN           NaN        NaN         NaN

        """

        ## Can only compare once data has been added to right
        assert self.right_data is not None, "right data has not yet been added, use add_right_data function"

        ## Can only compare data where each set's PK is unique
        assert self.left_data.index.duplicated().sum() == 0, "left PK is not unique, use set_key method on unique columns. \n Example: " + \
            str(self.left_data.index[self.left_data.index.duplicated()].values[0])

        assert self.right_data.index.duplicated().sum() == 0, "right PK is not unique, use set_key method on unique columns. \n Example: " + \
            str(self.right_data.index[self.right_data.index.duplicated()].values[0])

        ## Also protect against duplicate columns, as potentially a mistake in aliasing formulas within the SQL
        assert self.left_data.columns.duplicated().sum() == 0, "left column names are not unique, edit query or text file. \n Example: " + \
            str(self.left_data.columns[self.left_data.columns.duplicated()].values[0])

        assert self.right_data.columns.duplicated().sum() == 0, "right column names are not unique, edit query or text files. \n Example: " + \
            str(self.right_data.columns[self.right_data.columns.duplicated()].values[0])

        ## Compare rows in two sets based on common key and report on differences
        self._compare_row_counts()
        ## Compare values for matched rows
        self._compare_values()
        ## Return data for inspection as dictionary as this can be easily explored in spyder IDE (and likely others...)
        d = {}

        d["left_not_right_data"] = self.left_not_right_data
        d["right_not_left_data"] = self.right_not_left_data
        d["diff_values"] = self.diff_values

        ## Format summary data and order in meaningful way
        diff_summary_final = self.diff_summary.select_dtypes(include = _numerics).applymap(_num_to_str)
        diff_summary_final = pd.concat([diff_summary_final,self.diff_summary.select_dtypes(exclude = _numerics)],axis=1)
        cols_ordered = ["left_count","left_nulls","right_count","right_nulls","common_row_count","diff_val_count","left_total","right_total","total_diff","total_diff_pct","left_mean","right_mean"]
        d["diff_summary"] = diff_summary_final[cols_ordered]

        return d

    def _compare_row_counts(self):
        """ Find rows not common to both sets, report on them, and store these findings"""

        ## First identify common columns
        left_cols = self.left_data.columns
        right_cols = self.right_data.columns
        common_cols = left_cols[left_cols.isin(right_cols)]

        left_cols_not_right = left_cols[~left_cols.isin(common_cols)]
        right_cols_not_left = right_cols[~right_cols.isin(common_cols)]

        print("\nData Comparison Results:\nColumns common to both data sets compared for equality:\n\t",common_cols.values,\
            "\n\nColumns in left but not in right:\n\t",left_cols_not_right.values,\
            "\n\nColumns in right but not in left\n\t",right_cols_not_left.values)

        assert len(common_cols) >= 2, "Require at least one common column in data sets for comparison"

        self.common_cols = common_cols

        left_pks = self.left_data.index
        right_pks = self.right_data.index

        common_pks = left_pks[left_pks.isin(right_pks)]
        self.common_pks = common_pks

        ## Want to calculate summary statistics before subsetting datasets on common PKs, note bool is considered numeric as well for database comparison
        summary_headers = ['left_count','left_total','left_mean','left_nulls','right_count','right_total','right_mean','right_nulls']

        combined_summary = [list(_col_profile(self.left_data[col])) + \
            list(_col_profile(self.right_data[col])) for col in common_cols]

        self.diff_summary = pd.DataFrame(data = combined_summary , columns=summary_headers,index=common_cols)
        self.diff_summary["total_diff"] = self.diff_summary["left_total"] - self.diff_summary["right_total"]

        self.diff_summary["total_diff_pct"] = [_div_zero_str(x,y) for x,y in zip(self.diff_summary["total_diff"],self.diff_summary["left_total"])]

        self.diff_summary["common_row_count"] = common_pks.shape[0]

        ## Convert all to string for ease of comparison and as means to address numpy nan triggers spyder IDE issue 2991
        self.left_data = self.left_data.applymap(lambda x: str(x))
        self.right_data = self.right_data.applymap(lambda x: str(x))

        self.left_not_right_data = self.left_data[~left_pks.isin(right_pks)]
        self.right_not_left_data = self.right_data[~right_pks.isin(left_pks)]

        print("\nRows matched in both sets: ",str(common_pks.shape[0]),\
        "\n\nRows in left set not in right: ",str(self.left_not_right_data.shape[0]),\
        "\n\nRows in right set not in left: ",str(self.right_not_left_data.shape[0]))

    def _compare_values(self):
        """ Compare the values of those rows commont to both sets"""

        ## Can only compare common cols and row pks
        left_data_comp = self.left_data.loc[self.common_pks][self.common_cols]
        right_data_comp = self.right_data.loc[self.common_pks][self.common_cols]

        ## Return the position of the values which are not equal
        diff_array = np.where(left_data_comp.values != right_data_comp.values)

        ## Count up the number of difference by column using positional index
        colsdiff = diff_array[1]
        colsdiff_counts = np.bincount(colsdiff)

        ## Need to pad val diff summary to total length of columns
        ## as colsdiff_counts will only be as long as the position of final column with any errors

        colsdiff_length = self.diff_summary.shape[0]
        colsdiff_counts = np.lib.pad(colsdiff_counts, (0,colsdiff_length-len(colsdiff_counts)), 'constant', constant_values=(0))

        self.diff_summary["diff_val_count"] = ['{:,}'.format(x) for x in colsdiff_counts]

        if len(diff_array[0]) == 0:
            print("\nFor matched rows, all values match")
            self.diff_values = None

        else:
            rows_with_diff = np.unique(diff_array[0])
            cols_with_diff = np.unique(diff_array[1])

            ## Pass only columns and rows which have at least one difference
            diff_panel = pd.Panel({"left":left_data_comp.iloc[rows_with_diff,cols_with_diff],\
                "right":right_data_comp.iloc[rows_with_diff,cols_with_diff]})
            diff_values = diff_panel.apply(_report_diff, axis=0)
            print("\nFor matched rows,",str(len(rows_with_diff)),"of the rows have at least one different value among",str(len(cols_with_diff)),"columns flagged with differences")

            ## What to see all columns for contextual info for rows with at least some differences, but columns with differences at front
            diff_rows = left_data_comp.iloc[rows_with_diff,:]
            diff_rows = diff_rows[diff_rows.columns.delete(cols_with_diff)]
            ## Include delim columns to easily distinguish columns with any differences
            diff_values["cols_with_diffs_on_left"] = "|"
            diff_values = pd.concat([diff_values,diff_rows],axis=1)
            self.diff_values = diff_values

    def set_key(self,col,side="left"):
        """ Sets the primary key on both, left or right data set to another named column for comparison

        Parameters
        ----------
        side : the data set to set key on, either "left", "right" or "both"
            default "left"
        col : the column name to set as a key
        Examples
        --------
        >>> dc1.set_key("both","Employee")
        """
        if side == "both":
            assert self.right_data is not None, "Right data set must exist"
            assert col in self.left_data.columns, "Column specified as key must be column in left data set, check spelling and case"
            assert col in self.right_data.columns, "Column specified as key must be column in right data set, confirm right exists, check spelling and case"

            self.left_data = self._index_data(df = self.left_data,col = col)
            self.right_data = self._index_data(df = self.right_data,col = col)

        elif side == "left":
            assert col in self.left_data.columns, "Column specified as key must be column in left data set, check spelling and case"
            self.left_data = self._index_data(df = self.left_data,col = col)
        elif side == "right":
            assert self.right_data is not None, "Right data set must exist"
            assert col in self.right_data.columns, "Column specified as key must be column in right data set, confirm right exists, or check spelling and case"
            self.right_data = self._index_data(df = self.right_data,col = col)
        else:
            raise AssertionError('side must be both, left or right')

    def _index_data(self,df,col=None):
        """PK for comparison is left-most (position 0) by default unless specified (e.g. via the set_key function)"""
        if col is None:
            df.index = df.iloc[:,0]
        else:
            df.index = df[col]
        df = df.sort_index()
        return df


import os
from typing import List

import cv2
import numpy as np
from mtcnn.mtcnn import MTCNN

# running this more than once in same console throws an exception, hence this guard:
try:
    detector = MTCNN()
except ValueError as ve:
    # doing the job of this silly library:
    if not ve.args[0].startswith("Variable pnet/conv1/weights already exists"):
        raise ve

PADDING = 32

def cropped_face(image: np.ndarray, bounding_box: List[int]) -> np.ndarray:
    x, y, w, h = bounding_box
    top = max(0, y - h)
    bottom = y + 2 * h
    left = max(0, x - w)
    right = x + 2 * w
    return image[top:bottom, left:right]
#
#
#
# img_array = cv2.imread("datasets/videos/manual_zuck_screenshot.png")
# faces = detector.detect_faces(img_array)
# cv2.imwrite("datasets/videos/uncropped_face.jpg", img_array)
# cv2.imwrite("datasets/videos/cropped_face.jpg", cropped_face(img_array, faces[0]['box']))

videos_dir = "datasets/videos/"
filename = "how_to_build_the_future.mkv"

video_path = videos_dir + filename
filename_w_o_extension = filename[:-4]
faces_dir = "datasets/videos/faces/%s_better_crop/MZ/" % filename_w_o_extension
os.makedirs(faces_dir, exist_ok=True)

capture = cv2.VideoCapture(video_path)

while capture.isOpened():
    frame_number = capture.get(cv2.CAP_PROP_POS_FRAMES)

    ret, frame = capture.read()

    # proxy for end of video:
    if not ret:
        break

    # only detect faces every FPS frames (i.e. every second):
    if not (0 == int(frame_number % capture.get(cv2.CAP_PROP_FPS))):
        continue

    # just for testing purposes, don't want to do entire video
    # if frame_number > 30 * 10:
    #     break

    faces = detector.detect_faces(frame)

    for face_num, face in enumerate(faces):
        cropped = cropped_face(frame, face['box'])
        filename = faces_dir + "frame_%s_face_%s.jpg" % (frame_number, face_num)
        cv2.imwrite(filename, cropped)

# releasing for now, because it seems to prevent me from opening the file in VLC (?!)
capture.release()


import numpy as np
from scipy import misc
import os

test_file = "/home/didi/Documents/Codes/test.txt"
valid_file = "/home/didi/Documents/Codes/valid.txt"
train_file = "/home/didi/Documents/Codes/train.txt"
def read_data(filename):
    thefile = open(filename,"r")
    lines = thefile.readlines()
    #print lines
    content = [x.strip() for x in lines]
    images = []
    labels = []
    for item in content:
        #print eval(item)[0]
        image_name = "/home/didi/Documents/Codes/data/" + eval(item)[0]
        #print image_name
        image = misc.imread(image_name)
        image = np.array(image,dtype = np.float32)
        image = image.flatten()
        images.append(image)

        labels.append(int(eval(item)[1]))

    images = np.array(images,dtype = np.float32)
    labels = np.array(labels, dtype  = np.int32)
    return images, labels

def load_train_data():
    train_data, train_label = read_data(train_file)
    return train_data, train_label

def load_test_data():
    test_data, test_label = read_data(test_file)
    return test_data, test_label

def load_valid_data():
    valid_data, valid_label = read_data(valid_file)
    return valid_data, valid_label


import Defines
from pygame.locals import *
from models.Models import *
from views.View import *
import pygame, sys, os
class MasterController:
    def __init__(self):
        self.map = Map(MAP_SIZE_W, MAP_SIZE_H, BLOCKSIZE)
        pygame.init()
        window = pygame.display.set_mode((WIDTH,HEIGHT))
        pygame.display.set_caption('def')
        self.screen = pygame.display.get_surface()
        self.model = Model( self.map)
        self.camera = Camera(self.screen.get_width(),self.screen.get_height())

        self.entity_controller = EntityController(self.map)
        self.entity_view = EntityView(self.entity_controller)

        self.view = View(self.screen, self.camera, self.entity_view)

        self.camera_lock = True
        self.camera_follow_player = False
        #music here atm

     #   pygame.mixer.music.load("src/music/04.ogg")
     #   pygame.mixer.music.play()

    def menu_mode(self):
        self.menu = Menu()
        while 1:
            self.screen.fill((0,0,0))
            self.menu.draw(self.screen)

    def run(self):
        self.map.load_map("map1.map")
        #self.map.load_map("test.out")
      #  self.map.init_adjacent()
        self.entity_controller.update_enemy_path()
        self.entity_controller.add_entity()

        while 1:
            self.screen.fill((0,0,50))
            self.update()
            self.handle_events(pygame.event.get())
            self.view.draw_level(self.screen, self.map)

            self.entity_controller.update()
            pygame.display.update()
            pygame.display.flip()

    def update(self):
        # CAMERA MOUSE
        self.update_camera()
        if not self.camera_lock and not self.camera_follow_player:
            self.camera.update()
        elif self.camera_follow_player:
            self.camera.set_pos(
                                ((WIDTH/2)-(self.entity_controller.get_player_pos().get_pos()[0]*BLOCKSIZE*self.camera.get_zoom()),
                                 (HEIGHT/2)-(self.entity_controller.get_player_pos().get_pos()[1]*BLOCKSIZE*self.camera.get_zoom())
                                 )
                                )
    def update_camera(self):
       #PREVENT ouside window mouse scrolling
      #  if not pygame.mouse.get_focused():
       #     self.camera.set_speed((0,0))
        #    return
        if self.view.freeze_camera_movement_areas(pygame.mouse.get_pos()):
            self.camera.set_speed((0,0))
            return

        if pygame.mouse.get_pos()[0] < self.screen.get_width()/10:
            self.camera.set_speed((CAMERA_SPEED,self.camera.get_speed()[1]))
        if pygame.mouse.get_pos()[0] > self.screen.get_width()-(self.screen.get_width()/10):
            self.camera.set_speed((-1*CAMERA_SPEED,self.camera.get_speed()[1]))

        if self.camera.get_speed()[0] != 0 and pygame.mouse.get_pos()[0] > self.screen.get_width()/10 and pygame.mouse.get_pos()[0] < self.screen.get_width() -(self.screen.get_width()/10):
            self.camera.set_speed((0,self.camera.get_speed()[1]))
        # UP N DOWN
        if pygame.mouse.get_pos()[1] < self.screen.get_height()/10:
            self.camera.set_speed((self.camera.get_speed()[0],CAMERA_SPEED))
        # dont scroll on hud

        if pygame.mouse.get_pos()[1] > self.screen.get_height()-200:
            self.camera.set_speed((self.camera.get_speed()[0],-1*CAMERA_SPEED))

        if self.camera.get_speed()[1] != 0 and pygame.mouse.get_pos()[1] > self.screen.get_height()/10 and pygame.mouse.get_pos()[1] <  self.screen.get_height()-300:#self.screen.get_height() -(self.screen.get_height()/10):
            self.camera.set_speed((self.camera.get_speed()[0],0))

    def handle_events(self, events):
        for event in events:
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                      # HANDLE SAVEINPUT N STUFF
                if event.key == K_UP:
                    self.map.save_map("test.out")
                if event.key == K_DOWN:
                    self.map.load_map()
                    self.entity_controller.set_map(self.map)
                if event.key == K_SPACE: # add entity(enemy)..
                    self.entity_controller.add_entity()
                if event.key == K_c:
                    self.camera.center_map()
                if event.key == K_F1:
                    self.view.toggle_lines()
                if event.key == K_F2:
                    if self.camera_lock:
                        self.camera_lock = False
                    else:
                        self.camera_lock = True

                if event.key == K_F3:
                    if self.camera_follow_player:
                        self.camera_follow_player = False
                    else:
                        self.camera_follow_player = True

            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            self.view.handle_event(event,self.map)

            if event.type == pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed()[0] == 1:
                    self.map.change_color_at((pygame.mouse.get_pos()[0]-self.camera.get_x()
                                              ,pygame.mouse.get_pos()[1]-self.camera.get_y())
                                             , self.view.get_selected_color(), self.camera.get_zoom())

                if event.button == 5:
                    self.camera.set_zoom(self.camera.get_zoom()/2.0)

                if event.button == 4:
                    self.camera.set_zoom(self.camera.get_zoom()*2.0)

            if pygame.mouse.get_pressed()[2] == 1:
                self.entity_controller.add_tower(self.map.tile_at(pygame.mouse.get_pos()))
                #path = PathFinder()
                #path.calc_path(self.map.get_tile_list()[0], self.map.tile_at((pygame.mouse.get_pos()[0]-self.camera.get_x(), pygame.mouse.get_pos()[1]-self.camera.get_y())), self.map.get_tile_list())


import unittest
from contextlib import redirect_stdout
from io import StringIO

from ukoly.kapitola2.quotes import *

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.out = StringIO()

    def test_double_quotes(self):
        with redirect_stdout(self.out):
            double_quotes()
        self.assertEqual(self.out.getvalue(), 'Alenka rekla: "Bez mi koupit rohliky, Honzo."\n')

    def test_single_quotes(self):
        with redirect_stdout(self.out):
            single_quotes()
        self.assertEqual(self.out.getvalue(), "Alenka rekla: 'Bez mi koupit rohliky, Honzo.'\n")

    def test_mixed_quotes(self):
        with redirect_stdout(self.out):
            mixed_quotes()
        self.assertEqual(self.out.getvalue(), '"You\'ll be an IT specialist!\", that\'s what the guys at Viet Code said.\n')

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


import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class newDissector(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Dissector Script")

        mainBox = self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,spacing=10)
        self.add(mainBox)

        topBox = self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
        topBox.set_homogeneous(False)

        midOneBox = self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
        midOneBox.set_homogeneous(False)

        midTwoBox = self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
        midTwoBox.set_homogeneous(False)

        midThreeBox = self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
        midThreeBox.set_homogeneous(False)

        botBox = self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,spacing=10)
        botBox.set_homogeneous(False)

        mainBox.pack_start(topBox, True, True, 10)
        mainBox.pack_start(midOneBox, True, True, 10)
        mainBox.pack_start(midTwoBox, True, True, 10)
        mainBox.pack_start(midThreeBox, True, True, 10)
        mainBox.pack_start(botBox, True, True, 10)

        label = Gtk.Label("Generate a custom dissector script from a selected project.")
        label.set_justify(Gtk.Justification.CENTER)
        topBox.pack_start(label, True, True, 10)

        nameLabel = Gtk.Label("Project")
        nameLabel.set_justify(Gtk.Justification.CENTER)
        midOneBox.pack_start(nameLabel, True, True, 10)

        nameEntry = Gtk.Entry()
        nameEntry.set_text("Name")
        midOneBox.pack_start(nameEntry, True, True, 10)

        self.browse = Gtk.Button(label="Browse")
        self.browse.connect("clicked", self.browse_clicked)
        midOneBox.pack_start(self.browse, True, True, 10)

        nameLabel = Gtk.Label("Dissector Format")
        nameLabel.set_justify(Gtk.Justification.LEFT)
        midTwoBox.pack_start(nameLabel, True, True, 10)

        format_store = Gtk.ListStore(int,str)
        format_store.append([1, "format1"])
        format_store.append([2, "format2"])
        format_store.append([3, "format3"])
        format_store.append([4, "format4"])
        format_store.append([5, "format5"])

        format_combo = Gtk.ComboBox.new_with_model_and_entry(format_store)
        format_combo.connect("changed", self.on_name_combo_changed)
        format_combo.set_entry_text_column(1)
        midTwoBox.pack_start(format_combo, False, False, 20)

        nameLabel = Gtk.Label("Save Location")
        nameLabel.set_justify(Gtk.Justification.CENTER)
        midThreeBox.pack_start(nameLabel, True, True, 10)

        nameEntry = Gtk.Entry()
        nameEntry.set_text("System Path")
        midThreeBox.pack_start(nameEntry, True, True, 10)

        self.browse2 = Gtk.Button(label="Browse")
        self.browse2.connect("clicked", self.browse2_clicked)
        midThreeBox.pack_start(self.browse2, True, True, 10)

        self.generate = Gtk.Button(label="Generate")
        self.generate.connect("clicked", self.generate_clicked)
        botBox.pack_start(self.generate, True, True, 10)

        self.cancel = Gtk.Button(label="Cancel")
        self.cancel.connect("clicked", self.cancel_clicked)
        botBox.pack_start(self.cancel, True, True, 10)

    def browse_clicked(self,widget):
        print("Browsing n shit")
        dialog = Gtk.FileChooserDialog("Please choose a file", self,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK))

        self.add_filters(dialog)

        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print("Open clicked")
            print("File selected: " + dialog.get_filename())
        elif response == Gtk.ResponseType.CANCEL:
            print("Cancel clicked")

        dialog.destroy()

    def browse2_clicked(self,widget):
        print("Browsing n shit")
        dialog = Gtk.FileChooserDialog("Please choose a folder", self,
            Gtk.FileChooserAction.SELECT_FOLDER,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             "Select", Gtk.ResponseType.OK))
        dialog.set_default_size(800, 400)

        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print("Select clicked")
            print("Folder selected: " + dialog.get_filename())
        elif response == Gtk.ResponseType.CANCEL:
            print("Cancel clicked")

        dialog.destroy()

    def generate_clicked(self,widget):
        print("generating")
        self.destroy()

    def cancel_clicked(self,widget):
        print("canceling")
        self.destroy()

    def on_name_combo_changed(self, combo):
        tree_iter = combo.get_active_iter()
        if tree_iter is not None:
            model = combo.get_model()
            row_id, name = model[tree_iter][:2]
            print("Selected: ID=%d, name=%s" % (row_id, name))
        else:
            entry = combo.get_child()
            print("Entered: %s" % entry.get_text())

    def add_filters(self, dialog):
        filter_text = Gtk.FileFilter()
        filter_text.set_name("Text files")
        filter_text.add_mime_type("text/plain")
        dialog.add_filter(filter_text)

        filter_py = Gtk.FileFilter()
        filter_py.set_name("Python files")
        filter_py.add_mime_type("text/x-python")
        dialog.add_filter(filter_py)

        filter_any = Gtk.FileFilter()
        filter_any.set_name("Any files")
        filter_any.add_pattern("*")
        dialog.add_filter(filter_any)


import cv2
import numpy as np
import os
import dlib

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

detector = dlib.get_frontal_face_detector()

name = input("Enter your name: ")

frames = []
outputs = []

while True:

    ret, frame = cap.read()
    gray = []

    if ret:

        faces = detector(frame)

        for face in faces:
            x = face.left()
            y = face.top()
            w = face.right() - x
            h = face.bottom() - y

            cut = frame[y:y+h, x:x+w]
            fix = cv2.resize(cut, (100, 100))
            gray = cv2.cvtColor(fix, cv2.COLOR_BGR2GRAY)
            cv2.imshow("My Face", gray)

        cv2.imshow("My Screen", frame)

    key = cv2.waitKey(1)
    if key == ord("q"):
        break
    if key == ord("c"):
        frames.append(np.array(gray).flatten())
        outputs.append([name])

X = np.array(frames)
y = np.array(outputs)

data = np.hstack([y, X])

f_name = "face_data.npy"

if os.path.exists(f_name):
    old = np.load(f_name)
    data = np.vstack([old, data])

np.save(f_name, data)

cap.release()
cv2.destroyAllWindows()


from nltk.tokenize import word_tokenize

def read_file (entity, query):

    f=open("known_people_data/" + entity +".txt", 'r')
    file_data = f.readlines()
    text=""
    found = 0

    for i in file_data:

        line = word_tokenize(i)

        if line[0] == query:
            for j in range (2, len(line) ):
                text = text + " " + line[j]
                found = 1

    f.close()

    if found ==0:

        text = "i don't know"

    return(text)

def write_file(name, query, entity ) :

    f= open("known_people_data/" +name+".txt","a+")
    f.write(query + " " + ":" + " " + entity + "\n")

    f.close()


import numpy as np
import math
import time
import sys
from matplotlib import rc
rc('text', usetex=True)
from matplotlib import pyplot as plt

#
tex_fonts = {
    # Use LaTeX to write all text
    "text.usetex": True,
    "font.family": "serif",
    "font.serif": "Times",
    "mathtext.fontset": "custom",
    "mathtext.rm": "Times New Roman",
    # "font.serif": "ntx-Regular-tlf-t1",
    # Use 8pt font in plots, to match 8pt font in document
    "axes.labelsize": 8,
    "font.size": 8,
    # Make the legend/label fonts a little smaller
    "legend.fontsize": 7,
    "xtick.labelsize": 7,
    "ytick.labelsize": 7
}

#plt.rc("text.latex", preamble=r'''
#       \usepackage{newtxmath}
#       ''')

plt.rcParams.update(tex_fonts)

# set up parameters
data_root_path = "/rds/user/dc-bamb1/rds-dirac-dp131/dc-bamb1/GRChombo_data/KerrSF"
home_path="/home/dc-bamb1/GRChombo/Analysis/"

output_dir = "data/flux_data"

half_box = True

KS_or_cartesian_r=True
phi0 = 0.1
R_min = 5
R_max = 10
average_time = False
av_n = 1
plot_mass=False
cumulative=True
diff_from_alpha0=0
log_y=0
Nphi=64
Ntheta=18
Theta_max="1.0"

# appropriate \int Ylm Ylm^* cos(2 theta) sin(theta) dtheta dphi factor for 0 <= l <= 10
cos2theta_integrals = [[-(1/3)],[1/5,-(3/5)],[1/21,-(1/7),-(5/7)],\
[1/45,-(1/15),-(1/3),-(7/9)],[1/77,-(3/77),-(15/77),-(5/11),-(9/11)],\
[1/117,-(1/39),-(5/39),-(35/117),-(7/13),-(11/13)],\
[1/165,-(1/55),-(1/11),-(7/33),-(21/55),-(3/5),-(13/15)],\
[1/221,-(3/221),-(15/221),-(35/221),-(63/221),-(99/221),-(11/17),-(15/17)],\
[1/285,-(1/95),-(1/19),-(7/57),-(21/95),-(33/95),-(143/285),-(13/19),-(17/19)],\
[1/357,-(1/119),-(5/119),-(5/51),-(3/17),-(33/119),-(143/357),-(65/119),-(5/7),-(19/21)],\
[1/437,-(3/437),-(15/437),-(35/437),-(63/437),-(99/437),-(143/437),-(195/437),-(255/437),-(17/23),-(21/23)]]

def analytic_flux(t, R, l, m, a, mu, cumulative):
	## calculate the Boyer Lindquist r
	## assume M = 1
	r_plus = 1 + np.sqrt(1 - a*a)
	r = R*(1 + r_plus/(4.0*R))**2
	## calculate the perturbative flux at large radius to order
	cos2theta = cos2theta_integrals[l][int(np.abs(m))]
	L = l*(l+1)/(mu**2)
	tau = mu*t
	""" #
	Integrating factor of
		\int Ylm Ylm^* cos(2 theta) dtheta dphi
	assuming the spherical harmonics are normalised so that
		\int Ylm Ylm^* cos(2 theta) dtheta dphi = 1
	# """
	if (m!=0):
		if not cumulative:
			F0=(mu*tau)/2-1/2*mu*np.sin(tau)*np.cos(tau)
			F1=1/4*(L-1)*mu*np.sin(2*tau)-1/2*mu*tau*(L-2*np.cos(2*tau)+1)
			F2=1/4*tau*(-3*a**2*mu*cos2theta-7*a**2*mu-12*a*m+3*(1-2*L)*mu*np.cos(2*tau)+7*L*mu)+1/8*np.sin(2*tau)*(3*a**2*mu*cos2theta+7*a**2*mu+12*a*m-(L+3)*mu)+3*mu*tau**2*np.sin(tau)*np.cos(tau)
			F3=1/4*tau*(2*a**2*mu*cos2theta*(5-2*np.cos(2*tau))-mu*(2*a**2*(L-16)+L*(L+4)+20)+np.cos(2*tau)*(-8*a**2*mu-16*a*m+(L*(L+10)-10)*mu)+40*a*m)-1/4*np.sin(2*tau)*(3*a**2*mu*cos2theta+a**2*(-(L-12))*mu+12*a*m+3*(L-5)*mu)-1/6*mu*tau**3*(3*L+7*np.cos(2*tau)-4)+1/4*(1-9*L)*mu*tau**2*np.sin(2*tau)
			F4=1/24*tau**3*(3*a**2*mu*cos2theta+mu*(9*a**2+L*(3*L+35)-59)+12*a*m+5*(8*L+1)*mu*np.cos(2*tau))-1/16*tau**2*np.sin(2*tau)*(15*a**2*mu*cos2theta+mu*(21*a**2-L*(15*L+41)+52)+60*a*m)+3/8*a**2*np.sin(2*tau)*(a**2*mu*cos2theta+mu*(3*a**2+L-5)+4*a*m)+1/8*tau*(a**2*mu*cos2theta*(-6*a**2+(5*L+9)*np.cos(2*tau)-5*L-9)+np.cos(2*tau)*(mu*(a**2*(3*L+49)-6*L**2+L-35)+4*a*(5*L+9)*m)-4*a*m*(6*a**2+5*L+9)-mu*(18*a**4+a**2*(9*L+19)-6*L**2+L-35))+(mu*tau**5)/24-25/48*mu*tau**4*np.sin(2*tau)
		elif cumulative:
			F0=(tau**2)/4-1/4*(np.sin(tau)**2)
			F1=-1/4*(L+1)*tau**2+1/4*(L-3)*(np.sin(tau)**2)+tau*np.sin(tau)*np.cos(tau)
			F2=1/8*tau**2*(-3*a**2*cos2theta-7*a**2-12*a*m+7*L-6*np.cos(2*tau))+1/8*(np.sin(tau)**2)*(3*a**2*cos2theta+(7*a**2+5*L-12)+12*a*m)+3/8*(3-2*L)*tau*np.sin(2*tau)
			F3=-1/8*tau*np.sin(2*tau)*(4*a**2*cos2theta+8*a**2+16*a*m-(L**2+L-2))+1/8*tau**2*(10*a**2*cos2theta-(2*a**2*(L-16)+L*(L+4)+20)+40*a*m+(9*L-8)*np.cos(2*tau))-1/8*(np.sin(tau)**2)*(2*a**2*cos2theta-2*a**2*(L-8)+8*a*m+(L*(L+7)-32))+1/24*(4-3*L)*tau**4-7/6*tau**3*np.sin(tau)*np.cos(tau)
			F4=1/32*tau*np.sin(2*tau)*(a**2*(10*L+3)*cos2theta+a**2*(6*L+77)+4*a*(10*L+3)*m+3*(L**2+L-34))+1/96*tau**4*(3*a**2*cos2theta+(9*a**2+L*(3*L+35)-59)+12*a*m+25*np.cos(2*tau))+1/32*tau**2*(a**2*(-mu)*cos2theta*(2*(6*a**2+5*L+9)-15*np.cos(2*tau))+np.cos(2*tau)*((21*a**2-L*(15*L+1)+32)+60*a*m)-8*a*m*(6*a**2+5*L+9)-2*(18*a**4+a**2*(9*L+19)-6*L**2+L-35))+1/32*(np.sin(tau)**2)*(36*a**4+48*a**3*m+a**2*(12*a**2-10*L-3)*cos2theta+a**2*(6*L-137)-4*a*(10*L+3)*m-3*(L**2+L-34))+5/6*(2*L-1)*tau**3*np.sin(tau)*np.cos(tau)+(tau**6)/144
	elif (m==0):
		if not cumulative:
			F0=mu*tau*(np.sin(tau)**2)
			F1=-(L+2)*mu*tau*(np.sin(tau)**2)-mu*tau**2*np.sin(2*tau)
			F2=-1/2*mu*tau*(np.sin(tau)**2)*(3*a**2*cos2theta+7*a**2-10*L)-6*a**2*mu*np.sin(tau)*(np.cos(tau)-1)+3*(L+1)*mu*tau**2*np.sin(tau)*np.cos(tau)+mu*tau**3*np.cos(2*tau)
			F3=1/4*mu*tau**2*np.sin(2*tau)*(4*a**2*cos2theta+8*a**2-L*(L+22)+7)-mu*tau*(np.sin(tau/2)**2)*(-10*a**2*cos2theta*(np.cos(tau)+1)+(2*a**2*(L-7)+L*(L+14)+5)*np.cos(tau)+2*a**2*(L-13)+L*(L+14)+5)+9*a**2*mu*(np.sin(2*tau)-2*np.sin(tau))-1/2*mu*tau**3*(3*(L+1)*np.cos(2*tau)+L-1)+7/6*mu*tau**4*np.sin(tau)*np.cos(tau)
			F4=1/8*mu*tau**3*(a**2*cos2theta*(1-5*np.cos(2*tau))+(-7*a**2+L*(5*L+32)-11)*np.cos(2*tau)+3*a**2+L*(L+12)-15)-1/4*mu*tau**2*np.sin(tau)*(np.cos(tau)*(a**2*(5*L+19)*cos2theta+a**2*(3*L+41)-2*L*(8*L+17)+2)+12*a**2)+6*a**2*mu*(2*a**2+L+3)*(np.sin(tau/2)**2)*np.sin(tau)+1/2*mu*tau*(np.sin(tau/2)**2)*(-18*a**4-2*a**2*(6*a**2+5*L+11)*cos2theta*(np.cos(tau/2)**2)+5*a**2*(L-5)+(-18*a**4+a**2*(17*L+35)+L*(9*L+8)+15)*np.cos(tau)+L*(9*L+8)+15)-5/6*(L+1)*mu*tau**4*np.sin(2*tau)+1/24*tau**5*(mu-5*mu*np.cos(2*tau))
		elif cumulative:
			F0=tau**2/4+(np.sin(tau)**2)/4-1/4*tau*np.sin(2*tau)
			F1=1/4*tau**2*(-L+2*np.cos(2*tau)-2)-1/4*L*(np.sin(tau)**2)+1/4*L*tau*np.sin(2*tau)
			F2=1/8*tau**2*(-3*a**2*cos2theta-7*a**2-6*L*np.cos(2*tau)+10*L)+1/8*((np.sin(tau/2)**2)*((8*L-62*a**2)*np.cos(tau)+34*a**2+8*L)-3*a**2*cos2theta*(np.sin(tau)**2))+1/8*tau*np.sin(2*tau)*(3*a**2*cos2theta+7*a**2-4*L)+tau**3*np.sin(tau)*np.cos(tau)
			F3=1/8*tau**2*(2*a**2*cos2theta*(5-2*np.cos(2*tau))+(-8*a**2+L*(L+13)-9)*np.cos(2*tau)-2*a**2*(L-19)-L*(L+14)-5)+tau*(1/8*np.sin(2*tau)*(-6*a**2*cos2theta+2*a**2*(L-3)+L+14)-6*a**2*np.sin(tau))+3/4*a**2*cos2theta*(np.sin(tau)**2)-1/4*(np.sin(tau/2)**2)*((2*a**2*(L-39)+L+14)*np.cos(tau)+2*a**2*(L+9)+L+14)+1/24*tau**4*(-3*L-7*np.cos(2*tau)+3)-1/6*(9*L+2)*tau**3*np.sin(tau)*np.cos(tau)
			F4=1/96*tau**4*(3*a**2*cos2theta+9*a**2+5*(8*L+3)*np.cos(2*tau)+3*L*(L+12)-45)-1/48*tau**3*np.sin(2*tau)*(15*a**2*cos2theta+21*a**2-L*(15*L+56)+48)+1/32*tau**2*(-36*a**4+a**2*cos2theta*((10*L+23)*np.cos(2*tau)-2*(6*a**2+5*L+11))+(a**2*(6*L+61)-L*(17*L+12)-44)*np.cos(2*tau)-2*a**2*(7*L+85)+96*a**2*np.cos(tau)+2*L*(9*L+8)+30)+1/16*(np.sin(tau/2)**2)*(60*a**4+2*(1-12*a**2)*a**2*cos2theta*(np.cos(tau/2)**2)-a**2*(8*L+13)+(-132*a**4-a**2*(8*L+13)+L*(L+4)-14)*np.cos(tau)+L*(L+4)-14)+1/16*tau*np.sin(tau)*(48*a**2*(L+3)+np.cos(tau)*(36*a**4+(12*a**2-1)*a**2*cos2theta-a**2*(40*L+131)-L*(L+4)+14))+tau**6/144-5/48*tau**5*np.sin(2*tau)
	Flux = F0 + F1/r + F2/r**2 + F3/r**3 + F4/r**4
	return Flux

def misaligned_analytic_flux(alpha, t, R, a, mu, cumulative):
	# only for l=m=1 at the moment
	# |Y11(alpha)|^2 = 0.25*(1 + cos\alpha)^2|Y11|^2 + 0.25*(1 - cos\alpha)^2|Y1m1|^2 + 0.5*\sin^2(\alpha)|Y10|^2
	Y11 = analytic_flux(t, R, 1, 1, a, mu, cumulative)
	Y1m1 = analytic_flux(t, R, 1, -1, a, mu, cumulative)
	Y10 = analytic_flux(t, R, 1, 0, a, mu, cumulative)
	result = 0.25*(1 + np.cos(alpha*np.pi))**2*Y11 + 0.25*(1 - np.cos(alpha*np.pi))**2*Y1m1 + 0.5*(np.sin(alpha)**2)*Y10
	return result

def misaligned_analytic_flux_v2(Alpha, t, R, a, mu, cumulative):
	## calculate the Boyer Lindquist r
	## assume M = 1
	r_plus = 1 + np.sqrt(1 - a*a)
	r = R*(1 + r_plus/(4.0*R))**2
	## calculate the perturbative flux at large radius to order
	l=1
	m=1
	L = l*(l+1)/(mu**2)
	tau = mu*t
	alpha = np.pi*Alpha
	#
	if cumulative:
		F0=tau**2/4-(np.sin(tau)**2)/4
		F1=1/4*(-L-1)*tau**2+1/4*(L-3)*(np.sin(tau)**2)+tau*np.sin(tau)*np.cos(tau)
		F2=3/40*tau*np.sin(2*tau)*(a**2*np.cos(2*alpha)-a**2-10*L+15)+1/40*tau**2*(3*a**2*np.cos(2*alpha)-29*a**2-(60*a*m*np.cos(alpha))/mu+35*L-30*np.cos(2*tau))+((np.sin(tau)**2)*(mu*(-9*a**2*np.cos(2*alpha)+35*a**2+25*L-60)+60*a*m*np.cos(alpha)))/(40*mu)
		F3=(tau*np.sin(2*tau)*(2*a**2*mu*np.cos(2*alpha)+5*mu*(-6*a**2+L**2+L-2)-80*a*m*np.cos(alpha)))/(40*mu)+tau**2*(1/40*(-2*a**2*np.cos(2*alpha)*(2*np.cos(2*tau)+5)+(4*a**2+45*L-40)*np.cos(2*tau)-5*(2*a**2*(L-14)+L*(L+4)+20))+(5*a*m*np.cos(alpha))/mu)+((np.sin(tau)**2)*(mu*(10*a**2*np.cos(2*alpha)+2*a**2*(5*L-42)-5*L*(L+7)+160)-40*a*m*np.cos(alpha)))/(40*mu)+(1/6-L/8)*tau**4-7/6*tau**3*np.sin(tau)*np.cos(tau)
		F4=1/48*tau**3*np.sin(2*tau)*(-3*a**2*np.cos(2*alpha)+3*a**2+40*L-20)+1/480*tau**4*(-3*a**2*np.cos(2*alpha)+39*a**2+(60*a*m*np.cos(alpha))/mu+5*L*(3*L+35)+125*np.cos(2*tau)-295)+1/160*tau**2*(np.cos(2*tau)*(a**2*(10*L-7)*np.cos(2*alpha)+a**2*(67-10*L)-5*(15*L**2+L-32))-(20*a*m*np.cos(alpha)*(2*(6*a**2+5*L+9)-15*np.cos(2*tau)))/mu+2*(-78*a**4+a**2*(6*a**2+5*L+7)*np.cos(2*alpha)-5*a**2*(7*L+15)+5*L*(6*L-1)+175))+((np.sin(tau)**2)*(20*a*m*(12*a**2-10*L-3)*np.cos(alpha)+mu*(180*a**4+a**2*(-36*a**2+20*L+7)*np.cos(2*alpha)+a**2*(40*L-683)-15*(L**2+L-34))))/(160*mu)+(tau*np.sin(2*tau)*(mu*(-12*a**4+a**2*(12*a**2-20*L-7)*np.cos(2*alpha)+a**2*(20*L+383)+15*(L**2+L-34))+20*a*(10*L+3)*m*np.cos(alpha)))/(160*mu)+tau**6/144
	elif not cumulative:
		F0=(mu*tau)/2-1/4*mu*np.sin(2*tau)
		F1=1/4*(L-1)*mu*np.sin(2*tau)-1/2*mu*tau*(L-2*np.cos(2*tau)+1)
		F2=tau*(1/20*mu*(6*a**2*np.cos(2*alpha)*(np.cos(tau)**2)-3*(a**2+10*L-5)*np.cos(2*tau)-29*a**2+35*L)-3*a*m*np.cos(alpha))+1/40*np.sin(2*tau)*(-6*a**2*mu*np.cos(2*alpha)+32*a**2*mu+60*a*m*np.cos(alpha)-5*(L+3)*mu)+3*mu*tau**2*np.sin(tau)*np.cos(tau)
		F3=1/20*mu*tau**2*np.sin(2*tau)*(4*a**2*np.cos(2*alpha)-4*a**2-45*L+5)+tau*(1/20*mu*(-2*a**2*np.cos(2*alpha)*(np.cos(2*tau)+5)+(-26*a**2+5*L*(L+10)-50)*np.cos(2*tau)-5*(2*a**2*(L-14)+L*(L+4)+20))+2*a*m*np.cos(alpha)*(5-2*np.cos(2*tau)))+1/20*np.sin(2*tau)*(mu*(6*a**2*np.cos(2*alpha)+a**2*(5*L-57)-15*(L-5))-60*a*m*np.cos(alpha))-1/6*mu*tau**3*(3*L+7*np.cos(2*tau)-4)
		F4=1/120*tau**3*(mu*(-3*a**2*np.cos(2*alpha)*(5*np.cos(2*tau)+1)+5*(3*a**2+40*L+5)*np.cos(2*tau)+39*a**2+5*L*(3*L+35)-295)+60*a*m*np.cos(alpha))-1/80*tau**2*np.sin(2*tau)*(mu*(2*a**2*(5*L+4)*np.cos(2*alpha)+a**2*(52-10*L)-5*L*(15*L+41)+260)+300*a*m*np.cos(alpha))+3/40*a**2*np.sin(2*tau)*(mu*(-2*a**2*np.cos(2*alpha)+14*a**2+5*L-25)+20*a*m*np.cos(alpha))+1/40*tau*(mu*(-78*a**4+a**2*np.cos(2*alpha)*((6*a**2-5*L-7)*np.cos(2*tau)+6*a**2+5*L+7)-5*a**2*(7*L+15)+(-6*a**4+5*a**2*(L+45)+5*(-6*L**2+L-35))*np.cos(2*tau)+5*L*(6*L-1)+175)-20*a*m*np.cos(alpha)*(6*a**2-(5*L+9)*np.cos(2*tau)+5*L+9))+(mu*tau**5)/24-25/48*mu*tau**4*np.sin(2*tau)
	Flux = F0 + F1/r + F2/r**2 + F3/r**3 + F4/r**4
	return Flux

class data_dir:
	def __init__(self, num, l, m, a, mu, Al, nphi, ntheta, L, N):
		self.num = num
		self.l = l
		self.m = m
		self.a = float(a)
		self.mu = float(mu)
		self.nphi = nphi
		self.ntheta = ntheta
		self.theta_max = Theta_max
		self.Al = float(Al)
		self.N = N
		self.L = L
		if N==128:
			Nfix=""
		else:
			Nfix = "_N{:d}".format(N)
		if L==1024:
		        Lfix=""
		else:
		     	Lfix="_L{:d}".format(L)
		self.name = "run{:04d}_l{:d}_m{:d}_a{:s}_Al{:s}_mu{:s}_M1_IsoKerr{:s}{:s}".format(num, l, m, a, Al, mu, Lfix, Nfix)
	#
	def load_data(self):
		# load flux and mass data from csv files
		file_name = home_path + output_dir + "/" + self.name + "_J_R_linear_n000000_r_plus_to_{:d}_nphi{:d}_ntheta{:d}_theta_max{:s}.dat".format(R_max, self.nphi, self.ntheta, self.theta_max)
		flux_data = np.genfromtxt(file_name, skip_header=1)
		print("loaded " + file_name)
		mu = float(self.mu)
		self.tflux = flux_data[1:,0]
		self.r_min = flux_data[0,1]
		self.r_max = flux_data[0,2]
		E0 = 0.5*(4*np.pi*(self.r_max**3)/3)*(phi0*mu)**2
		self.inner_mass_flux = -flux_data[1:,1]/E0
		self.outer_mass_flux = -flux_data[1:,2]/E0
		self.analytic_outer_flux = misaligned_analytic_flux_v2(self.Al+0.5*(1-self.m), self.tflux, self.r_max, self.a, mu, cumulative)*(4*np.pi)*phi0**2/E0
		if cumulative:
			dt = self.tflux[2] - self.tflux[1]
			#inner_mass_flux = np.cumsum(inner_mass_flux)*dt
			self.outer_mass_flux = np.cumsum(self.outer_mass_flux)*dt
			pass
		if plot_mass:
			file_name = home_path + "data/mass_data" + "/" + "{:s}_mass_r_plus_to_{:d}.dat".format(self.name, R_max)
			self.mass_data = np.genfromtxt(file_name, skip_header=1)
			print("loaded " + file_name)
			if cumulative:
				self.tmass = mass_data[1:,0]
				self.dmass = (mass_data[1:,1] - mass_data[0,1])/E0
			elif not cumulative:
				self.tmass = mass_line_data[:-1,0]
				dt = tmass[1] - tmass[0]
				self.tmass_mean = 0.5*(self.tmass[1:]+self.tmass[:-1])
				self.dmass = (mass_line_data[1:,1] - mass_line_data[:-1,1])/(E0*dt)

data_dirs = []
def add_data_dir(num, l, m, a, mu, Al, nphi=Nphi, ntheta=Ntheta, L=1024, N=128):
        x = data_dir(num, l, m, a, mu, Al, nphi, ntheta, L, N)
        data_dirs.append(x)

# choose datasets to compare

"""run0002_l0_m0_a0.7_Al0_mu0.4_M1_IsoKerr
run0005_l1_m1_a0.7_Al0_mu0.4_M1_IsoKerr
run0006_l1_m1_a0.99_Al0_mu0.4_M1_IsoKerr
run0007_l2_m2_a0.7_Al0_mu0.4_M1_IsoKerr
run0008_l4_m4_a0.7_Al0_mu0.4_M1_IsoKerr
run0009_l1_m-1_a0.7_Al0_mu0.4_M1_IsoKerr
run0010_l8_m8_a0.7_Al0_mu0.4_M1_IsoKerr
run0011_l1_m1_a0.7_Al0_mu2.0_M1_IsoKerr
run0012_l1_m1_a0.7_Al0_mu0.01_M1_IsoKerr
run0013_l2_m2_a0.7_Al0_mu0.8_M1_IsoKerr
run0014_l8_m8_a0.7_Al0_mu3.2_M1_IsoKerr
run0015_l1_m1_a0.7_Al0.5_mu0.4_M1_IsoKerr
run0016_l1_m-1_a0.99_Al0_mu0.4_M1_IsoKerr
run0017_l1_m1_a0.99_Al0.5_mu0.4_M1_IsoKerr
run0018_l1_m1_a0.99_Al0.25_mu0.4_M1_IsoKerr"""

#add_data_dir(2, 0, 0, "0.7", "0.4", "0", 64, 64, "_theta_max0.99")
#add_data_dir(5, 1, 1, "0.7", "0.4", "0", 64, 64, "_theta_max0.99")
#add_data_dir(7, 2, 2, "0.7", "0.4", "0", 64, 64, "_theta_max0.99")
#add_data_dir(8, 4, 4, "0.7", "0.4", "0", 64, 64, "_theta_max0.99")
#add_data_dir(9, 1, -1, "0.7", "0.4", "0", 64, 64, "_theta_max0.99")
#add_data_dir(15, 1, 1, "0.7", "0.4", "0.5", 64, 64, "_theta_max0.99")

add_data_dir(6, 1, 1, "0.99", "0.4", "0")
add_data_dir(18, 1, 1, "0.99", "0.4", "0.25", 64, 64)
add_data_dir(17, 1, 1, "0.99", "0.4", "0.5", 64, 64)
add_data_dir(16, 1, -1, "0.99", "0.4", "0" , Nphi, Ntheta, 2048, 256)

def alpha_text(m, alpha):
        if m == 1:
                if alpha == 0:
                        return "0"
                elif alpha == 0.25:
                        return "\\pi/4"
                elif alpha == 0.5:
                        return "\\pi/2"
                else:
                     	return "nan"
        elif m == -1:
                return "\\pi"

def plot_graph():
	# plot setup
	ax1 = plt.axes()
	fig = plt.gcf()
	fig.set_size_inches(3.375,3)
	font_size = 10
	title_font_size = 10
	label_size = 10
	legend_font_size = 10
	rc('xtick',labelsize=font_size)
	rc('ytick',labelsize=font_size)
	#
	colours = ['r', 'b', 'g', 'm', 'c', 'y']
	colours2 = ['k', 'm', 'c']
	i = 0
	dd0 = data_dirs[0]
	dd0.load_data()
	for i in range(0,len(data_dirs)):
		dd = data_dirs[i]
		dd.load_data()
		mu = float(dd.mu)
		#net_flux = outer_mass_flux - inner_mass_flux
		#label_ = "$\\mu$={:.2f}".format(mu)
		#label_ = "$l$={:d} $m$={:d}".format(dd.l, dd.m)
		label_ = "$\\alpha={:s}$".format(alpha_text(dd.m, dd.Al))
		#ax1.plot(tflux,inner_mass_flux,colours[i]+"--", label="flux into R={:.1f} ".format(r_min)+label_)
		#ax1.plot(tflux,outer_mass_flux,colours[i]+"-", label="flux into R={:.1f} ".format(r_max)+label_)
		ds_length = min(len(dd.outer_mass_flux), len(dd0.outer_mass_flux))
		tau = dd.tflux[:ds_length]*mu
		flux=(10**(4*diff_from_alpha0))*(dd.outer_mass_flux[:ds_length]-dd0.outer_mass_flux[:ds_length]*diff_from_alpha0)
		aflux=(10**(4*diff_from_alpha0))*(dd.analytic_outer_flux[:ds_length]-dd0.analytic_outer_flux[:ds_length]*diff_from_alpha0)
		if log_y:
			ax1.plot(tau,np.log10(np.abs(flux)),colours[i]+"-", label=label_, linewidth=1)
		#	ax1.plot(tau,np.log10(np.abs(aflux)),colours[i]+"--", label="_4th order t$\\mu$/r analytic flux into R={:.1f} ".format(R_max)+label_, linewidth=1)
		else:
			ax1.plot(tau,flux,colours[i]+"-", label=label_, linewidth=1)
			#ax1.plot(tau,aflux,colours[i]+"--", label="_4th order t$\\mu$/r analytic flux into R={:.1f} ".format(R_max)+label_, linewidth=1)
		#ax1.plot(tflux,net_flux,colours[i]+":", label="net flux " + label_)
		#
		if plot_mass:
			mass_line_data = mass_data[dd.num]
			#print(mass_line_data[0:,1])
			#ax1.plot(tmass,delta_mass/E0,colours[i]+"-", label="change in mass {:.1f}$<r<${:.1f} ".format(r_min,r_max)+label_)
			if cumulative:
				ax1.plot(dd.tmass*mu,dd.dmass,colours[i]+"-.", label="_change in mass $R_+<R<${:.1f} ".format(R_max)+label_, linewidth=1)
			elif not cumulative:
				ax1.plot(dd.tmass*mu,dd.dmass,colours[i]+"-.", label="_rate of change in mass $R_+<R<${:.1f} ".format(R_max)+label_, linewidth=1)
		i = i + 1
	ax1.set_xlabel("$\\tau$", fontsize=label_size)
	if diff_from_alpha0:
		ax1.set_xlim((0, 400))
	else:
		ax1.set_xlim((0, 500))
		#ax1.set_ylim((0.0, 0.1))
	if cumulative:
		if diff_from_alpha0:
			if log_y:
				ax1.set_ylim((-5.0, 3.0))
				ax1.set_ylabel("$\\log_{10}(|$diff. in cumulative flux$| \\times 10^{4} / E_0)$", fontsize=label_size)
				save_path = home_path + "plots/mass_flux_in_R{:.0f}_IsoKerr_compare_Al_cumulative_difference_from_Al0_m1_log_y.pdf".format(R_max)
			else:
				ax1.set_ylim((-0.5, 5.0))
				ax1.set_ylabel("difference in cumulative flux $\\times 10^{4} / E_0$", fontsize=label_size)
				save_path = home_path + "plots/mass_flux_in_R{:.0f}_IsoKerr_compare_Al_cumulative_difference_from_Al0_m1.pdf".format(R_max)
			ax1.set_title("Cumulative mass flux, $M=1,\\mu=0.4,\\chi=0.99$ \n $l=|m|=1$; diff from $\\alpha=0$", wrap=True, fontsize=title_font_size)
		else:
			ax1.set_ylabel("cumulative flux / $E_0$", fontsize=label_size)
			ax1.set_title("Cumulative mass flux, $M=1,\\mu=0.4,\\chi=0.99$,\n$l=|m|=1$", wrap=True, fontsize=title_font_size)
			save_path = home_path + "plots/plots_for_first_paper/Fig_16_mass_flux_in_R{:.0f}_IsoKerr_compare_Al_cumulative.pdf".format(R_max)
	else:
		if diff_from_alpha0:
			if log_y:
				ax1.set_ylim((-6.0, 2.5))
				ax1.set_ylabel("$\\log_{10}(|$diff. in flux$| \\times 10^{4} / E_0)$", fontsize=label_size)
				save_path = home_path + "plots/mass_flux_in_R{:.0f}_IsoKerr_compare_Al_difference_from_Al0_m1_log_y.pdf".format(R_max)
			else:
				ax1.set_ylim((-0.05, 0.1))
				ax1.set_ylabel("difference in flux $\\times 10^{4} / E_0 $", fontsize=label_size)
				save_path = home_path + "plots/mass_flux_in_R{:.0f}_IsoKerr_compare_Al_difference_from_Al0_m1.pdf".format(R_max)
			plt.title("Mass flux, $M=1,\\mu=0.4,\\chi=0.99$ \n$l=|m|=1$; diff. from $\\alpha=0$")

		else:
			plt.title("Mass flux, $M=1,\\mu=0.4,\\chi=0.99$, $l=|m|=1$")
			ax1.set_ylabel("flux / $E_0$", fontsize=label_size)
			save_path = home_path + "plots/mass_flux_in_R{:.0f}_IsoKerr_compare_Al.pdf".format(R_max)
	ax1.legend(loc='best', fontsize=legend_font_size)
	plt.xticks(fontsize=font_size)
	plt.yticks(fontsize=font_size)
	plt.tight_layout()
	plt.savefig(save_path)
	print("saved plot as " + str(save_path))
	plt.clf()

plot_graph()


import numpy as np
import re
import itertools
from collections import Counter
from os import listdir
from os.path import isfile, join
import  json

def clean_str(string):
    """
    Tokenization/string cleaning for all datasets except for SST.
    Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
    """
    string = re.sub(r"[()\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " comma", string)
    string = re.sub(r"!", " exclamationmark ", string)
    string = re.sub(r"\(", "", string)
    string = re.sub(r"\)", "", string)
    string = re.sub(r"\?", " questionmark ", string)
    string = re.sub(r"\.\.\.", " dotdotdot ", string)
    string = re.sub(r"\.", "", string)
    return string.strip().lower()

def match_text(ID):
    json_path=""
    mypath="rumoureval-data/"

    for d in listdir(mypath):
        if ID in listdir(mypath+d):
            dir=d
            json_path=mypath+dir+"/"+ID+"/source-tweet/"+ID+".json"
            break

    for d in listdir(mypath):#event
        for thread in listdir(mypath+d):
            reply_path=mypath+d+"/"+thread+"/replies/"
            if ID+".json" in listdir(reply_path):
                json_path=reply_path+ID+".json"
                break
        if json_path!="":#break outer loop
            break
    if (isfile(json_path)):
        source = json.load(open(json_path, 'r'))
    else:
        return False
    return source['text']

def load_data_and_labels(task,jsonfile):
    """
    Loads rumorID and stance from file
    """
    ID1=[]#these list for memory ID order
    ID2=[]
    ID3=[]
    ID4=[]
    truth_values = json.load(open(jsonfile, 'r'))
    category=0
    agreed_examples=[]
    disagreed_examples=[]
    appeal_examples=[]
    comment_examples=[]
    if (task=="taskA"):
        category=4#SDQC
        for id in truth_values.keys():
            if(match_text(id)):
                if (truth_values[id]=="support"):
                    agreed_examples.append(match_text(id))
                    ID1.append(id)
                if (truth_values[id]=="deny"):
                    disagreed_examples.append(match_text(id))
                    ID2.append(id)
                if (truth_values[id]=="query"):
                    appeal_examples.append(match_text(id))
                    ID3.append(id)
                if (truth_values[id]=="comment"):
                    comment_examples.append(match_text(id))
                    ID4.append(id)
    else:
        category=3#rumor or not

    #strip special char
    agreed_examples = [s.strip() for s in agreed_examples]
    disagreed_examples = [s.strip() for s in disagreed_examples]
    appeal_examples = [s.strip() for s in appeal_examples]
    comment_examples = [s.strip() for s in comment_examples]

    IDlist=ID1+ID2+ID3+ID4
    # Split by words
    x_text = agreed_examples + disagreed_examples+appeal_examples+comment_examples
    x_text = [clean_str(sent) for sent in x_text]
    # Generate labels
    agreed_labels = [[0, 0,0,1] for _ in agreed_examples]
    disagreed_labels = [[0,0,1,0] for _ in disagreed_examples]
    appeal_labels = [[0, 1,0,0] for _ in appeal_examples]
    comment_labels = [[1, 0,0,0] for _ in comment_examples]
    y = np.concatenate([agreed_labels,disagreed_labels,appeal_labels,comment_labels], 0)

    return [x_text, y,IDlist]

def batch_iter(data, batch_size, num_epochs, shuffle=True):
    """
    Generates a batch iterator for a dataset.
    """
    data = np.array(data)
    data_size = len(data)
    num_batches_per_epoch = int(len(data)/batch_size) + 1

    for epoch in range(num_epochs):
        # Shuffle the data at each epoch

        for batch_num in range(num_batches_per_epoch):
            shuffle_indices = np.random.permutation(np.arange(data_size))
            shuffled_data = data[shuffle_indices]
            y_=[]
            for batch in shuffled_data:
                #print(batch)
                x_batch, y_batch = zip(batch)
                y_.append(y_batch)

            new_data=[]#new data
            a1=0
            a2=0
            a3=0
            a4=0
            i=0

            for y in y_:

                if ((y[0][0]==1) and (a1<batch_size/4)):
                    a1+=1
                    new_data.append(shuffled_data[i])
                if ((y[0][1]==1) and (a2<batch_size/4)):
                    a2+=1
                    new_data.append(shuffled_data[i])
                if ((y[0][2]==1) and (a3<batch_size/4)):
                    a3+=1
                    new_data.append(shuffled_data[i])
                if ((y[0][3]==1) and (a4<batch_size/4)):
                    a4+=1
                    new_data.append(shuffled_data[i])
                i+=1
            # print(len(new_data))
            new_data=np.array(new_data)
            indices = np.random.permutation(np.arange(len(new_data)))
            out_data = new_data[indices]
            yield out_data


from collections import defaultdict, OrderedDict
import re

from pycldf import iter_datasets, Sources
from clldutils.misc import lazyproperty, nfilter
from clld.db.models import common
from clld.db.fts import tsvector
from clld.db.meta import DBSession

from dictionaria.lib.ingest import BaseDictionary
from dictionaria import models

def read_media_table(cldf):
    media = {}

    if cldf.get('MediaTable'):
        tablename = 'MediaTable'
    elif cldf.get('media.csv'):
        tablename = 'media.csv'
    else:
        return media

    property_map = {
        cldf[tablename, prop].name: fallback
        for prop, fallback in (
            ('id', 'ID'),
            ('name', 'Filename'),
            ('description', 'Description'),
            ('languageReference', 'Language_ID'),
            ('mediaType', 'mimetype'),
            ('downloadUrl', 'URL'))
        if cldf.get((tablename, prop))}

    for row in cldf[tablename]:
        media_item = {property_map.get(k) or k: v for k, v in row.items()}
        media[media_item['ID']] = media_item

    return media

def get_foreign_keys(ds, from_table):
    """
    :param ds: A CLDF Dataset object.
    :param from_table: A csvw.metadata.Table object.
    :return: a `dict` mapping CLDF component names to `list`s of column names in `from_table`,
    which are foreign keys into the component.
    """
    res = defaultdict(list)
    for component in ['EntryTable', 'SenseTable', 'ExampleTable']:
        ref = ds.get(component)
        if not ref:
            continue
        for fk in from_table.tableSchema.foreignKeys:
            if fk.reference.resource == ref.url and \
                    fk.reference.columnReference == ref.tableSchema.primaryKey and \
                    len(fk.columnReference) == 1:
                res[component].append(fk.columnReference[0])
    return res

def get_labels(type_, table, colmap, submission, exclude=None):
    labels = OrderedDict(submission.props.get('labels', []))
    exclude = exclude or []
    exclude.extend([colmap.get('mediaReference', 'Media_IDs'), 'ZCom1'])
    res = OrderedDict()
    for col in table.tableSchema.columns:
        if col.name not in exclude and col.name not in colmap.values():
            res[col.name] = (col.titles.getfirst() if col.titles else col.name, False)
    map_name = '{0}_map'.format(type_)
    if submission.props.get(map_name):
        for key, label in labels.items():
            if key in submission.props[map_name]:
                res[submission.props[map_name][key]] = (
                    label, key in submission.props.get('process_links_in_labels', []))
    order_name = '{0}_custom_order'.format(type_)
    if submission.props.get(order_name):
        res = OrderedDict([(k, res[k]) for k in submission.props[order_name] if k in res])
    return res

class Dictionary(BaseDictionary):
    @lazyproperty
    def cldf(self):
        try:
            return next(iter_datasets(self.dir))
        except StopIteration:
            raise ValueError('no cldf metadata found in {}'.format(self.dir))

    def add_refs(self, data, table, row, obj, labels):
        if table == 'EntryTable':
            model, kw = models.WordReference, dict(word=obj)
        elif table == 'SenseTable':
            model, kw = models.MeaningReference, dict(meaning=obj)
        else:
            raise ValueError(table)
        refs_col = self.cldf.get((table, 'source'))
        if refs_col:
            for sid, context in map(self.cldf.sources.parse, row.get(refs_col.name, [])):
                if sid in data['DictionarySource']:
                    DBSession.add(model(
                        source=data['DictionarySource'][sid],
                        description=labels.get(context, (context, None))[0], **kw))

    def load(self,
             submission,
             data,
             vocab,
             lang,
             comparison_meanings,
             labels):
        def id_(oid):
            return '%s-%s' % (submission.id, oid)

        print('######\n a CLDF dict! \n####')

        media = read_media_table(self.cldf)

        metalanguages = submission.props.get('metalanguages', {})
        entries = self.cldf['EntryTable']
        colmap = {k: self.cldf['EntryTable', k].name
                  for k in [
                      'id', 'headword', 'partOfSpeech', 'languageReference',
                      'mediaReference', 'source']
                  if self.cldf.get(('EntryTable', k))}
        fks = get_foreign_keys(self.cldf, entries)
        elabels = get_labels('entry', entries, colmap, submission, exclude=fks['EntryTable'][:])

        for lemma in entries:
            if not lemma[colmap['headword']]:
                continue
            oid = lemma.pop(colmap['id'])
            word = data.add(
                models.Word,
                oid,
                id=id_(oid),
                name=lemma.pop(colmap['headword']),
                pos=lemma.pop(colmap['partOfSpeech']),
                dictionary=vocab,
                language=lang)
            DBSession.flush()

            media_ids = set(
                lemma.get(colmap.get('mediaReference', 'Media_IDs'))
                or [])
            files = [(md5, media[md5]) for md5 in media_ids if md5 in media]
            for md5, spec in sorted(
                files,
                key=lambda i: i[1].get(submission.props.get('media_order', 'Description')) or i[1]['ID']
            ):
                submission.add_file(None, md5, common.Unit_files, word, spec)

            self.add_refs(data, 'EntryTable', lemma, word, elabels)

            for index, (key, label) in enumerate(elabels.items()):
                label, with_links = label
                if lemma.get(key):
                    DBSession.add(common.Unit_data(
                        object_pk=word.pk,
                        key=label.replace('_', ' '),
                        value=lemma[key],
                        ord=index,
                        jsondata=dict(with_links=with_links)))

        DBSession.flush()

        #
        # Now that all entries are in the DB and have primary keys, we can create the
        # self-referential links:
        #
        fullentries = defaultdict(list)
        for lemma in entries:
            fullentries[lemma[colmap['id']]].extend(list(lemma.items()))
            word = data['Word'][lemma[colmap['id']]]
            for col in fks['EntryTable']:
                col = self.cldf['EntryTable', col]
                label = col.titles.getfirst() if col.titles else col.name
                if label == 'Entry_IDs':
                    label = 'See also'
                label = label.replace('_', ' ')
                for lid in lemma[col.name] or []:
                    if lid not in data['Word']:
                        print('missing entry ID: {0}'.format(lid))
                    else:
                        DBSession.add(models.SeeAlso(
                            source_pk=word.pk, target_pk=data['Word'][lid].pk, description=label))

        sense2word = {}
        colmap = {k: self.cldf['SenseTable', k].name
                  for k in [
                      'id', 'entryReference', 'description', 'mediaReference',
                      'source']
                  if self.cldf.get(('SenseTable', k))}
        fks = get_foreign_keys(self.cldf, self.cldf['SenseTable'])

        slabels = get_labels(
            'sense',
            self.cldf['SenseTable'],
            colmap,
            submission,
            exclude=['alt_translation1', 'alt_translation2'] + fks['EntryTable'][:])

        for sense_index, sense in enumerate(self.cldf['SenseTable']):
            fullentries[sense[colmap['entryReference']]].extend(list(sense.items()))
            sense2word[sense[colmap['id']]] = sense[colmap['entryReference']]
            try:
                w = data['Word'][sense[colmap['entryReference']]]
            except KeyError:
                print('missing entry: {0}'.format(sense[colmap['entryReference']]))
                continue
            dsc = sense[colmap['description']]
            if not isinstance(dsc, list):
                dsc = [dsc]
            kw = dict(
                id=id_(sense[colmap['id']]),
                name='; '.join(nfilter(dsc)),
                semantic_domain=sense.pop('Semantic_Domain', None),
                ord=sense_index,
                word=w)
            if 'alt_translation1' in sense and metalanguages.get('gxx'):
                kw['alt_translation1'] = sense['alt_translation1']
                kw['alt_translation_language1'] = metalanguages.get('gxx')
            if 'alt_translation2' in sense and metalanguages.get('gxy'):
                kw['alt_translation2'] = sense['alt_translation2']
                kw['alt_translation_language2'] = metalanguages.get('gxy')
            m = data.add(models.Meaning, sense[colmap['id']], **kw)
            DBSession.flush()

            self.add_refs(data, 'SenseTable', sense, m, slabels)

            for index, (key, label) in enumerate(slabels.items()):
                label, with_links = label
                if sense.get(key):
                    DBSession.add(models.Meaning_data(
                        object_pk=m.pk,
                        key=label.replace('_', ' '),
                        value=sense[key],
                        ord=index,
                        jsondata=dict(with_links=with_links)))

            concepticon_field = sense.get('Concepticon_ID') or ''
            concepticon_ids = [
                elem.strip()
                for elem in concepticon_field.split(';')
                if elem.strip()]
            for i, concepticon_id in enumerate(concepticon_ids):
                match = re.fullmatch(r'([^]]*)\s*\[(\d+)\]', concepticon_id)
                if not match:
                    continue
                _, cid = match.groups()
                if cid not in comparison_meanings:
                    continue
                concept = comparison_meanings[cid]

                vsid = '%s-%s' % (lang.id, concept)
                vs = data['ValueSet'].get(vsid)
                if not vs:
                    vs = data.add(
                        common.ValueSet, vsid,
                        id=vsid,
                        language=lang,
                        contribution=vocab,
                        parameter_pk=concept)

                DBSession.add(models.Counterpart(
                    id='{0}-{1}'.format(m.id, i), name=w.name, valueset=vs, word=w))

            DBSession.flush()
            media_ids = set(
                sense.get(colmap.get('mediaReference', 'Media_IDs'))
                or [])
            files = [(md5, media[md5]) for md5 in media_ids if md5 in media]
            for md5, spec in sorted(
                files,
                key=lambda i: i[1].get(submission.props.get('media_order', 'Description')) or i[1]['ID']
            ):
                submission.add_file(None, md5, models.Meaning_files, m, spec)

            for col in fks['EntryTable']:
                col = self.cldf['SenseTable', col]
                if col.name == colmap['entryReference']:
                    continue
                label = col.titles.getfirst() if col.titles else col.name
                label = label.replace('_', ' ')
                entry_ids = sense[col.name]
                if entry_ids:
                    if not isinstance(entry_ids, list):
                        entry_ids = [entry_ids]
                    for eid in entry_ids:
                        if eid not in data['Word']:
                            print('missing entry ID: {0}'.format(eid))
                        else:
                            DBSession.add(models.Nym(
                                source_pk=m.pk, target_pk=data['Word'][eid].pk, description=label))

        colmap = {k: self.cldf['ExampleTable', k].name
                  for k in ['id', 'primaryText', 'translatedText']
                  if self.cldf.get(('ExampleTable', k))}
        for ex in self.cldf.get('ExampleTable', ()):
            #
            # FIXME: Detect the column with sense IDs by looking at the foreign keys!
            #
            mids = ex.get('Senses') or ex.get('Sense_IDs', [])
            if not isinstance(mids, list):
                mids = mids.split(' ; ')
            for mid in mids:
                if mid not in data['Meaning']:
                    continue
                if mid in sense2word:
                    fullentries[sense2word[mid]].extend(list(ex.items()))
                    models.MeaningSentence(
                        meaning=data['Meaning'][mid],
                        sentence=data['Example'][ex[colmap['id']]])
                else:
                    print('missing sense: {0}'.format(mid))

        for wid, d in fullentries.items():
            if wid in data['Word']:
                data['Word'][wid].fts = tsvector(
                    '; '.join('{0}: {1}'.format(k, v) for k, v in d if v))


from uuid import uuid4
from sqlalchemy.orm import relationship

from app.extensions import db
from app.services.github import GitHub

class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.String(255), primary_key=True)
    username = db.Column(db.String(255), unique=True, nullable=False)
    avatar_url = db.Column(db.String(255), nullable=True)
    github_id = db.Column(db.Integer(), nullable=True)
    photos = db.relationship(
        'Photo',
        cascade='all, delete',
        backref='owner',
        lazy=True
    )

    def __init__(self, id, username, avatar_url, github_id, photos):
        self.id = id
        self.username = username
        self.avatar_url = avatar_url
        self.github_id = github_id
        self.photos = photos

    @staticmethod
    def find_or_create_from_token(access_token):
        data = GitHub.get_user_from_token(access_token)

        """Find existing user or create new User instance"""
        instance = User.query.filter_by(github_id=data['id']).first()

        if not instance:
            instance = User(
                str(uuid4()),
                data['login'],
                data['avatar_url'],
                data['id'],
                []
            )
            db.session.add(instance)
            db.session.commit()

        return instance

    def __repr__(self):
        return "<User: {}>".format(self.username)


import urllib.request
import json
import dml
import prov.model
import datetime
import uuid
from math import *

def greatCircleDistance(coord1,coord2):
  def haversin(x):
    return sin(x/2)**2
  return 2 * asin(sqrt(
      haversin(radians(coord2[0])-radians(coord1[0])) +
      cos(radians(coord1[0])) * cos(radians(coord2[0])) * haversin(radians(coord2[1])-radians(coord1[1]))))*6371

client = dml.pymongo.MongoClient()
repo = client.repo
repo.authenticate('ashleyyu_bzwtong', 'ashleyyu_bzwtong')
url = 'http://datamechanics.io/data/ashleyyu_bzwtong/cityOfBostonPolice.json'
response = urllib.request.urlopen(url).read().decode("utf-8")
police = json.loads(response)
policeStation = police['data']['fields'][3]['statistics']['values']
coordinates = []
for i in policeStation:
	coordinates.append((i['lat'],i['long']))

print (type(police))
print (coordinates)

print (greatCircleDistance((42.334445,-71.089922),(42.371228,-71.038591)))


import pandas as pd
import numpy as np
import os
import datetime
import time
from collections import Counter

np.set_printoptions(suppress=True)

def get_now_time():
    nowBJT = datetime.datetime.today()
    nowBJTStr = nowBJT.strftime('%Y%m%d%H')
    nowHour = int(nowBJTStr[-2:])  # 字符串中获取小时并转为整数
    if nowHour >= 8 and nowHour < 20:
        nowTimeStr = nowBJTStr[:-2] + '08'
        timeTemp = datetime.datetime.strptime(nowTimeStr, '%Y%m%d%H')
        previousTime = timeTemp - datetime.timedelta(days=1)
        previousTimeS = previousTime.strftime('%Y%m%d%H')
        return previousTimeS
    elif nowHour >= 20:
        nowTimeStr = nowBJTStr[:-2] + '20'
        timeTemp = datetime.datetime.strptime(nowTimeStr, '%Y%m%d%H')
        previousTime = timeTemp - datetime.timedelta(days=1)
        previousTimeS = previousTime.strftime('%Y%m%d%H')
        return previousTimeS

def get_correct_verification(pathO, pathM, pathZ, element, nowTime):
    '''
    用预报结果和实况计算各站多日平均绝对误差和每日全站平均绝对误差
    :param pathO: 实况资料基本路径
    :param pathM: 预报结果基本路径
    :param element: 检验要素
    :param listOfForcastDate: get_file_list获取的所需检验文件名列表
    :param stDf: 打底DF
    :return:各站多日平均绝对误差和每日全站平均绝对误差
    '''
    # print(listOfForcastDate)
    listTemp1, listTemp2 = [], []

    pathOb = pathO + nowTime + '_' + element + '.txt'
    pathZd = pathZ + nowTime + '_' + element + '.txt'
    pathFc = pathM + element + '\\' + nowTime[2:] + '.024'

    arFc = np.loadtxt(pathFc, skiprows=3)
    arOb = np.loadtxt(pathOb)
    arZd = np.loadtxt(pathZd)
    diffAryFcAbs = abs(arFc - arOb)
    diffAryZdAbs = abs(arZd - arOb)
    FcAbsTemp = diffAryFcAbs.reshape(44114)
    ZdAbsTemp = diffAryZdAbs.reshape(44114)

    ZdNum, FcNum = 0, 0
    for i in FcAbsTemp:
        if i <= 2:
            FcNum += 1
    print('客观预报准确率为{:.3f}'.format(FcNum / 44114))
    listTemp2.append(FcNum / 44114)
    for j in ZdAbsTemp:
        if j <= 2:
            ZdNum += 1
    print('指导预报准确率为{:.3f}'.format(ZdNum / 44114))
    listTemp2.append(ZdNum / 44114)

    diffAryZdAbs[diffAryZdAbs == 0] = np.nan
    skillPoint = (diffAryZdAbs - diffAryFcAbs) / diffAryZdAbs
    diffAryFcAllPointAVE = np.nanmean(diffAryFcAbs)
    diffAryZdAllPointAVE = np.nanmean(diffAryZdAbs)
    skillAllPointAVE = (diffAryZdAllPointAVE - diffAryFcAllPointAVE) / diffAryZdAllPointAVE
    listTemp1.append(skillAllPointAVE)
    return diffAryFcAbs, diffAryZdAbs, skillPoint, listTemp1, listTemp2

if __name__ == '__main__':
    timeStart = time.time()
    element = ['TMAX', 'TMIN']  # ,'TMIN'
    pathO = 'F:\\work\\2020Correct\\data\\TM_ob_24h_Grid\\'
    pathM = 'F:\\work\\2020Correct\\data\\TM_Result_Grid_20\\'
    pathS = 'F:\\work\\2020Correct\\data\\verificationResultG_20\\'
    pathZ = 'F:\\work\\2020Correct\\data\\TM_md_24h_Grid\\'
    nowTime = get_now_time()
    print(nowTime)
    ###############################################################
    for eEle in element:
        print(eEle)
        aa = get_correct_verification(pathO, pathM, pathZ, eEle, nowTime)
        diffFceDayforePoint, diffZdeDayforePoint, skilleDayforePoint, skilleDayforePointAVE, Rate = aa

        np.savetxt(pathS + 'diffFceDayforePoint_' + nowTime + eEle + '.txt', diffFceDayforePoint, fmt='%.4f')
        np.savetxt(pathS + 'diffZdeDayforePoint_' + nowTime + eEle + '.txt', diffZdeDayforePoint, fmt='%.4f')
        np.savetxt(pathS + 'skilleDayforePoint_' + nowTime + eEle + '.txt', skilleDayforePoint, fmt='%.4f')
        np.savetxt(pathS + 'skilleDayforePointAVE_' + nowTime + eEle + '.txt', skilleDayforePointAVE, fmt='%.4f')
        # with open(pathS + 'precision_rate' + nowTime + eEle + '.txt','w') as f1:
        #     f1.writelines(str(Rate))
        np.savetxt(pathS + 'precision_rate' + nowTime + eEle + '.txt', Rate, fmt='%.4f')
    timeEnd = time.time()
    timeCost = timeEnd - timeStart
    print(timeCost)
    #########################################################
    print('DONE')


from setuptools import setup, find_packages
from os import path

HERE = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(HERE, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

with open(path.join(HERE, 'requirements.txt')) as fp:
    install_requires = fp.read()
# Arguments marked as "Required" below must be included for upload to PyPI.
# Fields marked as "Optional" may be commented out.

setup(
    name='lux-api',  # PyPI Name (pip install [name])
    version='0.1.2',  # Required
    description='A Python API for Intelligent Data Discovery',  # Project description (Optional)
    long_description=long_description,  # Optional
    long_description_content_type='text/markdown',  # Optional (see note above)
    url='https://github.com/lux-org/lux',  # Optional
    maintainer='Doris Jung-Lin Lee',  # Optional
    maintainer_email='dorisjunglinlee@gmail.com',  # Optional
    classifiers=[  # Optional
        'Development Status :: 1 - Planning',
        'Intended Audience :: Developers',
        'Intended Audience :: Science/Research',
        'Intended Audience :: Other Audience',
        'Topic :: Scientific/Engineering :: Information Analysis',
        'Topic :: Scientific/Engineering :: Visualization',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python :: 3'
    ],
    keywords='visualization analytics data-science insight discovery',  # Optional
    include_data_package=True,
    packages=find_packages(),  # Required
    python_requires='>=3.5',
    install_requires=install_requires,
    extras_require={  # Optional
        'test': ['pytest']
    }
)


import torch
import torch.nn as nn
import torch.nn.functional as F

from attention import AttentionConv, AttentionStem

class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, out_channels, stride=1, groups=1, base_width=64):
        super(Bottleneck, self).__init__()
        self.stride = stride
        width = int(out_channels * (base_width / 64.)) * groups

        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels, width, kernel_size=1, bias=False),
            nn.BatchNorm2d(width),
            nn.ReLU(),
        )
        self.conv2 = nn.Sequential(
            AttentionConv(width, width, kernel_size=7, padding=3, groups=8),
            nn.BatchNorm2d(width),
            nn.ReLU(),
        )
        self.conv3 = nn.Sequential(
            nn.Conv2d(width, self.expansion * out_channels, kernel_size=1, bias=False),
            nn.BatchNorm2d(self.expansion * out_channels),
        )

        self.shortcut = nn.Sequential()
        if stride != 1 or in_channels != self.expansion * out_channels:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_channels, self.expansion * out_channels, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion * out_channels)
            )

    def forward(self, x):
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.conv3(out)
        if self.stride >= 2:
            out = F.avg_pool2d(out, (self.stride, self.stride))

        out += self.shortcut(x)
        out = F.relu(out)

        return out

class Model(nn.Module):
    def __init__(self, block, num_blocks, dataset, num_classes=1000, stem=False):
        super(Model, self).__init__()
        self.in_places = 64

        if dataset in ['CIFAR10', 'CIFAR100']:
            if stem:
                self.init = nn.Sequential(
                    # CIFAR10
                    AttentionStem(in_channels=3, out_channels=64, kernel_size=4, stride=1, padding=2, groups=1),
                    nn.BatchNorm2d(64),
                    nn.ReLU(),
                )
            else:
                self.init = nn.Sequential(
                    # CIFAR10
                    nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False),
                    nn.BatchNorm2d(64),
                    nn.ReLU(),
                )
        elif dataset == 'IMAGENET':
            if stem:
                self.init = nn.Sequential(
                    # For ImageNet
                    AttentionStem(in_channels=3, out_channels=64, kernel_size=4, stride=1, padding=2, groups=1),
                    nn.BatchNorm2d(64),
                    nn.ReLU(),
                    nn.MaxPool2d(4, 4)
                )
            else:
                self.init = nn.Sequential(
                    # For ImageNet
                    nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
                    nn.BatchNorm2d(64),
                    nn.ReLU(),
                    nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
                )

        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)
        self.num_blocks = num_blocks
        self._norm_mask_dict = {}

    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1] * (num_blocks - 1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_places, planes, stride))
            self.in_places = planes * block.expansion
        return nn.Sequential(*layers)

    def get_attention(self, layer, aggregated=True, norm=True, unpad=True):
        """Get attention map of an attention layer.
        `norm` and `unpad` do not matter if `aggregated` is False

        Ref: https://discuss.pytorch.org/t/how-to-unfold-a-tensor-into-sliding-windows-and-then-fold-it-back/55890/7

        Args:
            layer: attention layer
            aggregated: whether to aggregate across footprints by folding
            norm: whether to normalize to eliminate cumulative effect
            unpad: whether to unpad the attention

        Return:
            attn: retrived attention map
        """
        assert hasattr(layer, 'attn_raw'), 'Layer does not have attention'
        assert layer.attn_raw is not None, 'Attention not computed yet'

        attn = layer.attn_raw
        if not aggregated:
            return attn

        # Prepare the dimension
        kernel_size = layer.kernel_size
        stride = layer.stride
        B, N, C, H, W, K = attn.shape
        padding = 2 * (kernel_size//2)

        # (B, N, C, H, W, K) -> (B*N*C, K, H*W)
        attn = attn.reshape([B*N*C, H*W, K]).permute(0, 2, 1)

        # Fold the attenion to aggregate
        attn = F.fold(attn, (H+padding, W+padding),
                      kernel_size=kernel_size, stride=stride)

        # (B*N*C, 1, H_pad, W_pad) -> (B, N*C, H_pad, W_pad)
        attn = attn.reshape([B, N*C, H+padding, W+padding])

        # Normalize the cummulative effect of sliding
        if norm:
            if (H, kernel_size) not in self._norm_mask_dict:
                # Compute norm_mask if not precomputed
                norm_mask = F.conv2d(torch.ones(1, 1, H, H),
                                     torch.ones(1, 1, kernel_size, kernel_size),
                                     padding=padding).to(attn.device)
                self._norm_mask_dict[(H, kernel_size)] = norm_mask
            else:
                norm_mask = self._norm_mask_dict[(H, kernel_size)]
            attn = attn / norm_mask

        # Unpad the attention
        if unpad:
            attn = attn[:, :, padding//2:padding//2+H, padding//2:padding//2+W]
        return attn

    def get_all_attention(self, aggregated=True, norm=True, unpad=True):
        """Get attention of all layers

        Return:
            attn_dict: dictionary of attention with the structure:
                attn_dict[l_name][m_name], where l_name is layer name and
                m_name is module name
        """
        attn_dict = {}
        for l in range(len(self.num_blocks)):
            l_name = 'layer{}'.format(l+1)
            attn_dict[l_name] = {}
            layer = getattr(self, l_name)
            for m in range(self.num_blocks[l]):
                attn = self.get_attention(
                    layer[m].conv2[0], aggregated, norm, unpad)
                attn_dict[l_name][str(m)] = attn
        return attn_dict

    def forward(self, x):
        out = self.init(x)
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.layer4(out)
        # out = F.avg_pool2d(out, 4)
        out = self.avgpool(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)

        # attn_dict = self.get_all_attention()
        return out

def ResNet26(num_classes=1000, stem=False, dataset='CIFAR10'):
    return Model(Bottleneck, [1, 2, 4, 1], num_classes=num_classes, stem=stem, dataset=dataset)

def ResNet38(num_classes=1000, stem=False, dataset='CIFAR10'):
    return Model(Bottleneck, [2, 3, 5, 2], num_classes=num_classes, stem=stem, dataset=dataset)

def ResNet50(num_classes=1000, stem=False, dataset='CIFAR10'):
    return Model(Bottleneck, [3, 4, 6, 3], num_classes=num_classes, stem=stem, dataset=dataset)

def get_model_parameters(model):
    total_parameters = 0
    for layer in list(model.parameters()):
        layer_parameter = 1
        for l in list(layer.size()):
            layer_parameter *= l
        total_parameters += layer_parameter
    return total_parameters

# temp = torch.randn((2, 3, 224, 224))
# model = ResNet38(num_classes=1000, stem=True)
# print(get_model_parameters(model))


# USO: Passar como argumento o diretório de imagens para treinamento e teste.
# python predict.py --training images/training --testing images/testing

# Importa as bibliotecas necessárias
from patterns.localbinarypatterns import LocalBinaryPatterns
from sklearn.svm import LinearSVC
from imutils import paths
import argparse
import cv2
import os

# Parseia os argumentos da linha de comando.
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--training", required=True,
	help="path to the training images")
ap.add_argument("-e", "--testing", required=True,
	help="path to the tesitng images")
args = vars(ap.parse_args())

# Inicializa o descritor LBP (Local Binary Patterns)
# com os dados e a lista de labels
desc = LocalBinaryPatterns(24, 8)
data = [] # será armazedado para cada imagem sua descrição LBP.
labels = [] # será armazedado os rótulos para classficação.

# Itera sobre o conjunto de treinamento de imagens (training images)
for imagePath in paths.list_images(args["training"]):
	# Carrega a imagem, converte para escala cinza e descreve isso com LBP.
	image = cv2.imread(imagePath)
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

	# Gera as imagens LBP e Histograma
	hist = desc.describe(gray) # Retorna o resultado do descritor LBP.

	# Extrai os labels dos paths da imagem, depois atualiza a
	# lista de dados e labels inicializada anteriormente.
	labels.append(imagePath.split(os.path.sep)[-2])
	data.append(hist)

# Resultado
# data = [hist_1, ... , ..., hist_8]
# labels = ['epidural', ..., 'intraparenchymal']

# Treina um classificador linear SVM nos dados
# Para maiores informações: https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html
model = LinearSVC(C=50.0, random_state=42, max_iter=1500) # Cria modelo com os parâmetros citados.
model.fit(data, labels) # Treina o modelo, como parâmetro recebe os dados e os labels (rótulos das classes)

# Resultado
# Modelo está treinado

# Para cada descriotor previamente extraido dos dados deve existir um label que os identifique.
# Descritor da imagem 1 será classificado como rótulo e assim por diante.

# Itera sobre as imagens de teste
for imagePath in paths.list_images(args["testing"]):
	# Carrega imagem, converte isso para escala cinza e descreve isso com LBP.
	image = cv2.imread(imagePath)
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	hist = desc.describe(gray)

	# Classifica a imagem entre os labels existentes.
	prediction = model.predict(hist.reshape(1, -1))

	# Mostra a imagem e a previsão
	cv2.putText(image, prediction[0], (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
		0.4, (0, 0, 255), 1)
	cv2.imshow("Prediction", image)
	cv2.waitKey(0)


"""
    recordFile.py records audio from the default microphone in a background thread using pyaudio
    Copyright (C) 2016 coderman64

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
import pyaudio
import wave
import threading
import time
import subprocess

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "tmp/tmp.wav"

class recorder:
    def __init__(self):
        self.going = False
        self.process = None
        self.filename = "ScreenCapture.mpg"
    def record(self,filename):
        try:
            if self.process.is_alive():
                self.going = False
        except AttributeError:
            print("test")
        self.process = threading.Thread(target=self._record)
        self.process.start()
        self.filename = filename
    def _record(self):
        p = pyaudio.PyAudio()
        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)

        print("* recording")

        frames = []

        self.going = True

        while self.going:
            data = stream.read(CHUNK)
            frames.append(data)

        print("* done recording")

        stream.stop_stream()
        stream.close()
        p.terminate()

        wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b''.join(frames))
        wf.close()

    def stop_recording(self):
        self.going = False


from __future__ import absolute_import, unicode_literals
# from __future__ import absolute_import, unicode_literals
# https://stackabuse.com/asynchronous-tasks-in-django-with-redis-and-celery/
# https://ruddra.com/posts/docker-do-stuff-using-celery-using-redis-as-broker/   * main ref
# https://www.revsys.com/tidbits/celery-and-django-and-docker-oh-my/
# https://mopitz199.github.io/docs/celery-redis-django-docker.html

import os
from celery import Celery
import logging
from .customized import train
# from models.customized import train
from django.conf import settings
from django.apps import apps
import datetime

# https://ruddra.com/posts/docker-do-stuff-using-celery-using-redis-as-broker/

logger = logging.getLogger("Celery")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AlphaProject.settings')

app = Celery('celery_tasks.tasks')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def train_mode(param1, param2):
    project_folder = getProjectFolder(param2)
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S") + "-"
    train(project_folder, timestamp)

    print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    print(param2)

    # https://stackoverflow.com/questions/57666355/cannot-import-models-into-celery-tasks-in-django
    # It works well!
    Model = apps.get_model(app_label='models', model_name='model')
    Project = apps.get_model(app_label='projects', model_name='project')
    User = apps.get_model(app_label='auth', model_name='User')
    user = User.objects.get(id=param2[0])
    project = Project.objects.get(user=user, title=param2[1])
    print(user.id)
    print(user.username)
    print(project.title)

    model = Model.objects.create(title=timestamp + "-keras.h5",
                                 location=project.location + "models/" + timestamp + "-keras.h5",
                                 label_location=project.location + "models/" + timestamp + "-classLabel.txt",
                                 url=settings.MEDIA_URL_DATADASE + "models/" + timestamp + "-keras.h5",
                                 description="default",
                                 type=param2[2],
                                 user=user,
                                 project=project,
                                 isPublic=True)
    model.save()

    locationOfModel = os.path.abspath(os.path.dirname(__file__))
    print(locationOfModel)

    return "success"

def getProjectFolder(param2):
    project_folder = settings.MEDIA_ROOT + str(param2[0]) + "/" + param2[1] + "/"
    return project_folder