import gc
import re
import nltk
import string
import numpy as np
import pandas as pd
from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from sklearn.svm import LinearSVC
from distributed import LocalCluster
from sklearn.pipeline import Pipeline
from sklearn.ensemble import VotingClassifier, RandomForestClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.decomposition import TruncatedSVD
from nltk.stem.snowball import SnowballStemmer
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score, confusion_matrix
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.tokenize import WordPunctTokenizer, RegexpTokenizer, TreebankWordTokenizer

STOP=set(stopwords.words('english') + list(string.punctuation))

# cluster = LocalCluster()

def plot_confusion_matrix(cm, target_names,title='Confusion matrix',cmap=None, normalize=True):

    import matplotlib.pyplot as plt
    import numpy as np
    import itertools

    accuracy = np.trace(cm) / float(np.sum(cm))
    misclass = 1 - accuracy

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(8, 6))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()

    if target_names is not None:
        tick_marks = np.arange(len(target_names))
        plt.xticks(tick_marks, target_names, rotation=45)
        plt.yticks(tick_marks, target_names)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.4f}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
    plt.show()

# function to parse the titles for html data
def parse_title(x):
    soup = BeautifulSoup(x,'html.parser')
    val = soup.find('title')
    if val is not None:
        val = val.text
        val = val.strip()
        return val
    else:
        return ''

# function to write titles to a csv file
def extract_text(file,fn,outfile,chunk_size=10**3):
    for chunk in pd.read_csv(file, chunksize=chunk_size, encoding='utf-8', engine='c'):
        df = chunk['Webpage_id']
        title = chunk['Html'].apply(lambda x: fn(x))
        df = pd.concat([df,title], axis=1)
        df.to_csv(outfile, mode='a',encoding='utf-8',index=False)
        del df,title

# function to preprocess the text data
def process_data(df, col):
    df[col]=df[col].apply(lambda x: re.sub(r'www|http',"", str(x)))
    df[col]=df[col].apply(lambda x: re.sub(r'\d+',"", str(x)))
    df[col]=df[col].apply(lambda x: re.sub(r'-'," ", str(x)))

    return df[col]

# custom tokenizer
def tokenize(text):
    tokens = [word for word in wpt.tokenize(text) if len(word) > 1]
    stems = [stemmer.stem(item) for item in tokens]

    return stems

# function to prepare submission file
def prepare_sub(prediction, filename):
    sub = pd.read_csv('./data/sample_submission_poy1UIu.csv')
    sub.Tag = prediction
    sub.to_csv(filename, index=False)

# extract the titles. This will take some time
# extract_text('./data/train/html_data.csv',parse_title, 'w_id_title.csv', chunk_size=10**4)

labels=['news', 'clinicalTrials', 'conferences', 'profile', 'forum','publication', 'thesis', 'guidelines', 'others']

tr = pd.read_csv('./data/train/train.csv')
ts = pd.read_csv('./data/test_nvPHrOx.csv')

titles = pd.read_csv(open('w_id_title.csv','rU'), encoding='utf-8', engine='c')
titles.columns = ['Webpage_id', 'Title']
tr.Webpage_id=tr.Webpage_id.astype('str')
ts.Webpage_id=ts.Webpage_id.astype('str')

# merge the titles with train and test files
tr = pd.merge(tr,titles)
ts = pd.merge(ts, titles)

# tr['full_text']
tr['full_text']=tr.Domain + ' ' + tr.Url + ' ' + tr.Title
ts['full_text']=ts.Domain + ' ' + ts.Url + ' ' + ts.Title

# only domain field
tr_dom= process_data(tr,'full_text')
ts_dom= process_data(ts,'full_text')

y=tr.loc[:,'Tag']
X=tr_dom

del tr, ts
gc.collect()

stemmer = SnowballStemmer("english", ignore_stopwords=True)
wpt=RegexpTokenizer(r'\w+')
# wpt=WordPunctTokenizer()

# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

# SGD Classifier
text_clf_svm = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='word', min_df=0.003)),
                         ('clf-svm', SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, max_iter=10, random_state=42))])

text_clf_svm_w = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='char', min_df=0.003)),
                         ('clf-svm', SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, max_iter=10, random_state=23))])

text_clf_svm_wn = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer=tokenize, min_df=0.003)),
                         ('clf-svm', SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, max_iter=10, random_state=90))])

# Multinomial Naive bayes
text_clf_mnb = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='word', min_df=0.003)),
                         ('clf-nb', MultinomialNB(alpha=1e-2,fit_prior=True, class_prior=None))])

text_clf_mnb_w = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='char', min_df=0.003)),
                         ('clf-nb', MultinomialNB(alpha=1e-2,fit_prior=True, class_prior=None))])

text_clf_mnb_wn = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer=tokenize, min_df=0.003)),
                         ('clf-nb', MultinomialNB(alpha=1e-2,fit_prior=True, class_prior=None))])

# LinearSVC model
text_clf_svc = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='word', min_df=0.003)),
                         ('clf-svc', LinearSVC(C=1e-2,class_weight='balanced',random_state=84))])

text_clf_svc_w = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='char', min_df=0.003)),
                         ('clf-svc', LinearSVC(C=1e-2,class_weight='balanced',random_state=102))])

text_clf_svc_wn = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer=tokenize, min_df=0.003)),
                         ('clf-svc', LinearSVC(C=1e-2,class_weight='balanced',random_state=101))])

# logistic Regression
text_clf_log = Pipeline([('vect', TfidfVectorizer(stop_words=STOP, ngram_range=(1,3), analyzer='char', min_df=0.003)),
                        #  ('scaler',StandardScaler()),
                         ('clf-log', LogisticRegression(random_state=200, multi_class='multinomial', solver='newton-cg'))])

estm = [('sgd', text_clf_svm),('sgd_w', text_clf_svm_w), ('sgd_wn', text_clf_svm_wn),
        ('mnb', text_clf_mnb), ('mnb_w', text_clf_mnb_w), ('mnb_wn', text_clf_mnb_wn),
        ('svc', text_clf_svc), ('svc_w', text_clf_svc_w), ('svc_wn', text_clf_svc_wn),
        ('log', text_clf_log)]

clfens = VotingClassifier(estimators=estm, voting='hard')

mdlfit = clfens.fit(tr_dom.values, y.values)
pred = mdlfit.predict(ts_dom)
prepare_sub(pred,'ensemble_tfidf_svd.csv')


'''
readline handling for mavproxy
'''

import sys, glob

rline_mpstate = None

class rline(object):
    '''async readline abstraction'''
    def __init__(self, prompt, mpstate):
        import threading
        global rline_mpstate
        self.prompt = prompt
        self.line = None
        rline_mpstate = mpstate

    def set_prompt(self, prompt):
        if prompt != self.prompt:
            self.prompt = prompt
            sys.stdout.write(prompt)

def complete_alias():
    '''return list of aliases'''
    global rline_mpstate
    return rline_mpstate.aliases.keys()

def complete_command():
    '''return list of commands'''
    global rline_mpstate
    return rline_mpstate.command_map.keys()

def rule_expand(component, text):
    '''expand one rule component'''
    global rline_mpstate
    if component[0] == '<' and component[-1] == '>':
        return component[1:-1].split('|')
    if component == '(FILENAME)':
        return glob.glob(text+'*')
    if component == '(PARAMETER)':
        return rline_mpstate.mav_param.keys()
    if component == '(SETTING)':
        return rline_mpstate.settings.list()
    return [component]

def rule_match(component, cmd):
    '''see if one rule component matches'''
    if component == cmd:
        return True
    expanded = rule_expand(component, cmd)
    if cmd in expanded:
        return True
    return False

def complete_rule(rule, cmd):
    '''complete using one rule'''
    global rline_mpstate
    rule_components = rule.split(' ')

    # check it matches so far
    for i in range(len(cmd)-1):
        if not rule_match(rule_components[i], cmd[i]):
            return []

    # expand the next rule component
    expanded = rule_expand(rule_components[len(cmd)-1], cmd[-1])
    return expanded

def complete_rules(rules, cmd):
    '''complete using a list of completion rules'''
    if not isinstance(rules, list):
        rules = [rules]
    ret = []
    for r in rules:
        ret += complete_rule(r, cmd)
    return ret

last_clist = None

def complete(text, state):
    '''completion routine for when user presses tab'''
    global last_clist
    global rline_mpstate
    if state != 0 and last_clist is not None:
        return last_clist[state]

    # split the command so far
    cmd = readline.get_line_buffer().split(' ')

    if len(cmd) == 1:
        # if on first part then complete on commands and aliases
        last_clist = complete_command() + complete_alias()
    elif cmd[0] in rline_mpstate.completions:
        # we have a completion rule for this command
        last_clist = complete_rules(rline_mpstate.completions[cmd[0]], cmd[1:])
    else:
        # assume completion by filename
        last_clist = glob.glob(text+'*')
    ret = []
    for c in last_clist:
        if c.startswith(text):
            ret.append(c)
    ret.append(None)
    last_clist = ret
    return last_clist[state]

# some python distributions don't have readline, so handle that case
# with a try/except
try:
    import readline
    readline.set_completer_delims(' \t\n;')
    readline.parse_and_bind("tab: complete")
    readline.set_completer(complete)
except Exception:
    pass


def isvalid(line):
    words = line.split()
    words = list(map(''.join, map(sorted, words)))

    for i, n in enumerate(words[:-1]):
        for j in words[i+1:]:
            if words[i] == j:
                return False
    return True

with open('day04.in', 'r') as fi:
    sumOfValid = 0
    for line in fi:
        if isvalid(line):
            sumOfValid += 1
    print(sumOfValid)


# -*- coding: utf-8 -*-
# @Time    : 2021/8/15 17:13
# @File    : 343. 整数拆分.py
from leetcode import *

#  dp[i] 表示 i 的组合最大乘积
class Solution:
    def integerBreak(self, n: int) -> int:
        dp = [0] * (n + 1)
        for i in range(1, n + 1):
            for j in range(i):
                dp[i] = max(dp[i], j * (i - j), j * dp[i - j])
        return dp[-1]


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""This python script contains the classes and methods for detecting the regions of interest (ROIs)
    the analysis will be focused on"""

__author__ = 'martino lorusso'

# Import the necessary packages
import random
import math

import numpy as np
import cv2

class Frame:
    """
    A class for representing the upper metal chassis of a mold

    Frame class methods implement the necessary image processing techniques for frame analysis.
    See OpenCV documentation for reference

    Attributes
    ----------
    frame_params : dict
        A dictionary of parameters to initialize a Frame object. Parameters are passed to class
        methods. The full list is available and editable in config.py

    Methods
    -------
    detect_frame_lines(src, params, num_frame_lines=10)
        Detect the frame image main lines
    get_bounding_boxes_from_contours(src)
        Compute the bounding boxes from contours
    select_n_largest_boxes(boxes, dst, num_boxes=4)
        Select the largest n boxes in a list of bounding boxes
    set_mean_box_coords_from_box_collection(box_collection)
        Compute a bounding box mean coordinates given a collection of boxes
    get_rectangle_sides_ratio(box_coords)
        Given a box, compute the ratio of its sides
    compute_percent_change(x, x_ref)
        Compute the percentage change between two numeric values
    get_rect_coords_from_box(box_coords)
        Compute top left and bottom right rectangle coordinates from a bounding box
    set_inner_frame_rect(box_coords, ref_box_coords, diff_thresh=2)
        Set the bounding box coordinates of the inner frame comparing the computed coordinates
        to a reference bounding box coordinates
    compute_outer_frame_coords(inner_frame_rect)
        Determine the outer frame rectangle coordinates given the frame thickness
    get_frame_roi(src, frame_coords, remove_holes=True)
        Extract the frame region of interest from the computed coordinates
    draw_hough_lines(lines, dst, src, show_val=False, show_lines=False, show_single=False)
        Draw the detected frame lines
    remove_frame_holes_from_mask(src, in_top_left, in_bottom_right, out_top_left, out_bottom_right)
        Remove the frame holes from the frame mask

    """

    def __init__(self, frame_params):
        # A dictionary of parameters can be passed using a configuration file like config.py
        self.frame_params = frame_params

    def detect_frame_lines(self, src, params, num_frame_lines=10):
        """Detect the frame image main lines

        Parameters
        ----------
        src : ndarray
            3D array of `int` type representing the source image
        params : dict
            The dictionary containing the parameters requested by the image processing pipeline
            functions. Refer to OpenCV documentation for a thorough description of each one.
            Parameters need to be stored in config.py
        num_frame_lines : int
            The number of lines to be extracted among all the lines that are detected in order
            of likelihood (default 10). See OpenCV's HoughLines method documentation for reference

        Returns
        -------
        lines : list
            A nested list of lines with length <= num_frame_lines parameter

        """
        preproc = src.copy()
        # Perform image denoising using Non-local Means Denoising algorithm --> OpenCV doc. for ref.
        den = cv2.fastNlMeansDenoising(preproc, h=params['h'],
                                       templateWindowSize=params['template_window_size'],
                                       searchWindowSize=params['search_window_size'])
        gray = cv2.cvtColor(den, cv2.COLOR_BGR2GRAY)
        # Apply a bilateral filter to grayscale image. Bilateral filter can reduce unwanted noise
        # very well while keeping edges fairly sharp
        gray = cv2.bilateralFilter(gray, params['d'],
                                   params['sigma_color'],
                                   params['sigma_space'])
        # Apply an adaptive threshold
        # Switch comments of next two lines to choose between Gaussian_C and Mean_C method
        # ada_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        ada_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                           cv2.THRESH_BINARY_INV,
                                           blockSize=params['block_size'],
                                           C=params['C'])
        # Use the morphological erode and dilate operations with specified kernel sizes
        ada_thresh = cv2.erode(ada_thresh, kernel=(3, 3), iterations=1)
        ada_thresh = cv2.dilate(ada_thresh, kernel=(5, 5), iterations=1)
        img_to_detect_lines = ada_thresh.copy()

        # Set angle resolution and minimum line length for line detection
        angle_res = params['angle_res']
        min_line_length = params['min_line_length']
        # Apply HoughLines method to detect lines --> OpenCV doc. for ref.
        lines = cv2.HoughLines(img_to_detect_lines, 1, np.pi * angle_res / 180, min_line_length)
        if lines is not None:
            # Select the first num_frame_lines detected lines
            if len(lines) >= num_frame_lines:
                lines = lines[:num_frame_lines]

        return lines

    def get_bounding_boxes_from_contours(self, src):
        """Compute the bounding boxes from contours

        Parameters
        ----------
        src : ndarray
            3D array of `int` type representing the source image

        Returns
        -------
        bound_boxes : list
            A nested list of the minimal bounding boxes for the specified contours

        """
        cnt_img = src.copy()
        # If source is a color image, convert it to grayscale
        if src.ndim == 3:
            cnt_img = cv2.cvtColor(cnt_img, cv2.COLOR_BGR2GRAY)
        # Retrieve contours from the image using the algorithm [Suzuki85] --> OpenCV doc. for ref.
        contours, hierarchy = cv2.findContours(cnt_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        # Remove parent contour, drawn from hierarchy, that corresponds to outer frame bounding box
        contours = [contours[j] for j in range(len(contours)) if hierarchy[0][j][3] >= 0]
        # Initialize contour approximating polygons' and bounding boxes' lists
        contours_poly = [None] * len(contours)
        bound_boxes = [None] * len(contours)

        # Loop over the contours list
        for i, cnt in enumerate(contours):
            # Approximate the polygonal curves of the contour
            perimeter = cv2.arcLength(cnt, True)
            # Set approximation precision to 1.0% of perimeter
            contours_poly[i] = cv2.approxPolyDP(cnt, 0.010 * perimeter, True)
            # Compute a bounding rectangle from the approximated contour, as the tuple (x, y, w, h)
            # letting (x, y) be the top-left coordinate of the box rectangle and (w, h) be its
            # width and height
            bound_boxes[i] = cv2.boundingRect(contours_poly[i])

        return bound_boxes

    def select_n_largest_boxes(self, boxes, dst, num_boxes=4):
        """Select the n largest boxes in a list of bounding boxes

        Parameters
        ----------
        boxes : list
            A list of bounding boxes, each one defined by coordinates (x, y, w, h)
        dst : ndarray
            3D array of `int` type representing the destination image for drawing boxes
        num_boxes : int
            The number of boxes to be selected from all detected boxes (default 4)

        Returns
        -------
        drawn_boxes : ndarray
            3D array of `int` type representing the drawn boxes image
        selected_boxes : list
            A list of selected bounding boxes

        """
        drawn_boxes = dst.copy()

        box_areas = []
        # Loop over the boxes list
        for i in range(len(boxes)):
            # Compute the area of each box and then append it to box_areas list
            box_area = int(boxes[i][2]) * int(boxes[i][3])
            box_areas.append(box_area)
        # Sort the boxes by area size, from largest to smallest
        area_idx = [idx for idx, area in enumerate(box_areas)]
        areas = zip(area_idx, box_areas)
        sorted_areas = sorted(areas, key=lambda x: x[1], reverse=True)
        # Extract the n boxes with the largest area as tuples (x, y, w, h), letting (x, y) be the
        # top-left coordinate of the box rectangle and (w, h) be its width and height
        largest_n_areas = sorted_areas[:num_boxes]
        selected_boxes = [boxes[area[0]] for area in largest_n_areas][0]

        # Loop over the largest boxes list
        for area in largest_n_areas:
            # Draw box rectangles on the dst image
            i = area[0]
            color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256))
            cv2.rectangle(drawn_boxes, (int(boxes[i][0]), int(boxes[i][1])),
                          (int(boxes[i][0] + boxes[i][2]), int(boxes[i][1] + boxes[i][3])),
                          color, 3)

        return drawn_boxes, selected_boxes

    def set_mean_box_coords_from_box_collection(self, box_collection):
        """Compute a bounding box mean coordinates given a collection of boxes

        Parameters
        ----------
        box_collection : list
            A list of bounding boxes, each one defined by coordinates (x, y, w, h)

        Returns
        -------
        mean_box : tuple
            The bounding box computed by averaging the respective coordinates

        """
        # Get the mean bounding box by computing the average coordinates in a given collection
        # of boxes. Such box may be set as the default detected box for the most difficult molds
        mean_box = tuple(np.mean([box[0] for box in box_collection], axis=0, dtype='int'))

        return mean_box

    def get_rectangle_sides_ratio(self, box_coords):
        """Given a box, compute the ratio of its sides

        Parameters
        ----------
        box_coords : tuple
            A tuple representing a box coordinates (x, y, w, h)

        Returns
        -------
        box_ratio : float
            The ratio between the width (w) and height (h) of the box rectangle

        """
        width = box_coords[2]
        height = box_coords[3]
        box_ratio = round(width / height, 4)

        return box_ratio

    def compute_percent_change(self, x, x_ref):
        """Compute the percentage change between two numeric values

        Parameters
        ----------
        x : float
            New value
        x_ref : float
            Old or reference value

        Returns
        -------
        pct_change : float
            The percentage increase or decrease of the new value compared to the old value

        """
        pct_change = round(abs((x - x_ref) / x_ref) * 100, 2)

        return pct_change

    def get_rect_coords_from_box(self, box_coords):
        """Compute top left and bottom right rectangle coordinates from a bounding box coordinates

        Parameters
        ----------
        box_coords : tuple
            A tuple representing a box coordinates (x, y, w, h)

        Returns
        -------
        rect_from_box : list
            A list of top -left and bottom-right coordinates tuples of the corresponding rectangle

        """
        # Upper left vertex in OpenCV cv2.rectangle() method
        rect_start_point = (box_coords[0], box_coords[1])
        # Lower right vertex in OpenCV cv2.rectangle() method
        rect_end_point = (int(box_coords[0] + box_coords[2]), int(box_coords[1] + box_coords[3]))
        rect_from_box = [rect_start_point, rect_end_point]

        return rect_from_box

    def set_inner_frame_rect(self, box_coords, ref_box_coords, diff_thresh=2.0):
        """Set the bounding box coordinates of the inner frame by comparing the computed coordinates
        to a reference bounding box coordinates

        Parameters
        ----------
        box_coords : tuple
            A tuple representing a box coordinates (x, y, w, h)
        ref_box_coords : tuple
            A tuple representing a reference box coordinates (x, y, w, h)
        diff_thresh : float
            A difference threshold (in percent) between the sides ratio of the input box and
            the sides ratio of the reference box (default 2.0)

        Returns
        -------
        coords : tuple
            The coordinates of the chosen box
        diff_from_ratio : float
            The difference (in percent) between the sides ratio of the input (computed) box and
            the sides ratio of the reference box

        """
        # Get the sides ratio of both input box and reference box, compute their difference
        box_ratio = self.get_rectangle_sides_ratio(box_coords)
        ref_box_ratio = self.get_rectangle_sides_ratio(ref_box_coords)
        diff_from_ratio = self.compute_percent_change(box_ratio, ref_box_ratio)
        # Compare the computed difference to the difference threshold to set the inner frame box
        if diff_from_ratio < diff_thresh:
            coords = box_coords
        else:
            coords = ref_box_coords

        return coords, diff_from_ratio

    def compute_outer_frame_coords(self, inner_frame_rect):
        """Determine the outer frame rectangle coordinates given the frame thickness

        Parameters
        ----------
        inner_frame_rect : list
            A list of top -left and bottom-right coordinates tuples of the inner frame rectangle

        Returns
        -------
        outer_frame_rect : list
            A list of top -left and bottom-right coordinates tuples of the outer frame rectangle

        """
        pt1 = inner_frame_rect[0]
        pt2 = inner_frame_rect[1]
        # Given the frame thickness that is an attribute of Frame instance set in config.py,
        # compute the outer frame rectangle coordinates
        top_left = self.frame_params['frame_thickness'][0]
        bottom_right = self.frame_params['frame_thickness'][1]
        pt1_out = tuple(map(lambda x, y: x - y, pt1, top_left))
        pt2_out = tuple(map(lambda x, y: x + y, pt2, bottom_right))
        outer_frame_rect = [pt1_out, pt2_out]

        return outer_frame_rect

    def get_frame_roi(self, src, frame_coords, remove_holes=True):
        """Extract the frame region of interest from the computed coordinates

        Parameters
        ----------
        src : ndarray
            3D array of `int` type representing the pre-processed image
        frame_coords : list
            A list of points representing the inner and outer frame rectangles coordinates
        remove_holes : bool
            Allow to remove the chassis holes from source as they could affect the casting analysis

        Returns
        -------
        frame_roi = ndarray
            3D array of `int` type representing the image of the frame ROI

        """
        # Frame_coords is made up of [pt1, pt2, pt1_out, pt2_out]
        in_top_left, in_bottom_right = frame_coords[0], frame_coords[1]
        out_top_left, out_bottom_right = frame_coords[2], frame_coords[3]
        blank = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8)
        # Get a frame mask based on the inner and outer boxes coordinates
        cv2.rectangle(blank, out_top_left, out_bottom_right, (255, 255, 255), -1)
        cv2.rectangle(blank, in_top_left, in_bottom_right, (0, 0, 0), -1)

        # Remove frame holes, if requested
        if remove_holes:
            blank = self.remove_frame_holes_from_mask(blank, in_top_left, in_bottom_right,
                                                      out_top_left, out_bottom_right)
        # Use a bitwise AND operation to get the frame ROI image
        blank = cv2.cvtColor(blank, cv2.COLOR_BGR2GRAY)
        frame_roi = cv2.bitwise_and(src, src, mask=blank)

        return frame_roi

    def draw_hough_lines(self, lines, dst, src,
                         show_val=False, show_lines=False, show_single=False):
        """Draw the detected frame lines

        Parameters
        ----------
        lines : list
            A nested list of lines
        dst : ndarray
            3D array of `int` type representing the destination image for drawing lines
        src : ndarray
            3D array of `int` type representing the source image for showing the original image
        show_val : bool
            Allow to show parametric coordinates values
        show_lines : bool
            Allow to show an image of the lines
        show_single : bool
            Allow to show the lines one at a time

        Returns
        -------
        dst : ndarray
            3D array of `int` type representing the destination image with the drawn lines

        """
        # Draw the lines detected through the Hough Line Transform --> OpenCV doc. for ref.
        if lines is not None:
            # Loop over lines list
            for i in range(0, len(lines)):
                # Extract rho and theta parametric coordinates from each line
                rho = lines[i][0][0]
                theta = lines[i][0][1]
                # Show rho and theta values, if requested
                if show_val:
                    print("rho: ", rho, "\ttheta: ", theta)
                # Compute cartesian coordinates from parametric ones
                a = math.cos(theta)
                b = math.sin(theta)
                x0 = a * rho
                y0 = b * rho
                pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
                pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))
                # Draw the line on dst image
                cv2.line(dst, pt1, pt2, (0, 0, 255), 2, cv2.LINE_AA)
                # Show the lines one at a time, if requested
                if show_single:
                    cv2.imshow("Detected Lines - Standard Hough Line Transform",
                               np.hstack([src, dst]))
                # Press `q` to stop the drawing
                if cv2.waitKey() & 0xFF == ord("q"):
                    break
            # Show the lines, if requested
            if show_lines:
                cv2.imshow("Frame", dst)
                cv2.waitKey()
                cv2.destroyAllWindows()

        return dst

    def remove_frame_holes_from_mask(self, src,
                                     in_top_left, in_bottom_right, out_top_left, out_bottom_right):
        """Remove the frame holes from the frame mask

        Parameters
        ----------
        src : ndarray
            3D array of `int` type representing the source image
        in_top_left : tuple
            top-left coordinates of inner frame
        in_bottom_right : tuple
            bottom-right coordinates of inner frame
        out_top_left : tuple
            top-left coordinates of outer frame
        out_bottom_right : tuple
            bottom-right coordinates of outer frame

        Returns
        -------
        dst : ndarray
            3D array of `int` type representing the destination image with the drawn lines
        """
        dst = src.copy()
        # Get center of top and bottom frame holes
        pt1_x, pt1_y = (in_bottom_right[0]-in_top_left[0])//2, (in_top_left[1]-out_top_left[1])//2
        pt2_x, pt2_y = pt1_x, (out_bottom_right[1]-in_bottom_right[1])//2
        # Set holes radius based on the image size
        radius = src.shape[1] // 8
        # Draw two black filled circles on the destination image using above parameters
        cv2.circle(dst, (pt1_x, pt1_y), radius, (0, 0, 0), -1)
        cv2.circle(dst, (pt2_x, pt2_y), radius, (0, 0, 0), -1)

        return dst

class Sand:
    """
    A class for representing the central sandy area of a mold where the casting process occurs

    Sand class methods implement the necessary image processing techniques for sandy area analysis.
    See OpenCV documentation for reference

    Attributes
    ----------
    sand_area_coords : list
        A list of coordinate tuples that are obtained from frame roi detection step and are used to
        initialize a Sand object

    Methods
    -------
    get_sand_roi(src)
        Extract the sand region of interest from the coordinates computed in frame detection step

    """

    def __init__(self, sand_area_coords):
        # sand_area_coords is made up of [pt1, pt2]
        self.top_left = sand_area_coords[0]
        self.bottom_right = sand_area_coords[1]

    def get_sand_roi(self, src):
        """Extract the sand region of interest from the coordinates computed in frame detection step

        Parameters
        ----------
        src : ndarray
            3D array of `int` type representing the pre-processed image

        Returns
        -------
        sand_roi : ndarray
            3D array of `int` type representing the image of the sand ROI

        """
        # Get the top-left and bottom-right ROI coordinate tuples from Sand instance attributes,
        # note that they correspond to the inner frame rectangle coordinates
        top_left = self.top_left
        bottom_right = self.bottom_right
        blank = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8)
        # Get a sand mask based on the above coordinates
        cv2.rectangle(blank, top_left, bottom_right, (255, 255, 255), -1)
        mask = cv2.cvtColor(blank, cv2.COLOR_BGR2GRAY)
        # Use a bitwise AND operation to get the sand ROI image
        sand_roi = cv2.bitwise_and(src, src, mask=mask)

        return sand_roi


import sys

def chars():
    counter = 0
    f = open(sys.argv[2], 'r')
    content = f.read()
    f.close()
    for char in content:
        counter += 1
    return counter

def words():
    counter = 0
    file_words = []
    f = open(sys.argv[2], 'r')
    file_words = f.read().replace("\n", " ").split(" ")
    f.close()
    for word in file_words:
        if word != "":
            counter += 1
    return counter

def lines():
    f = open(sys.argv[2], 'r')
    counter = sum(1 for line in f)
    f.close()
    return counter

def main():
    if sys.argv[1] == "words":
        print(words())
    elif sys.argv[1] == "chars":
        print(chars())
    elif sys.argv[1] == "lines":
        print(lines())

if __name__ == '__main__':
    main()


EXECUTE = "Применить"
REPLY_TO_HLS_AUTHENTICATION = "Ответ на проверку подлинности"
CHANGE_HLS_SECRET = "Изменение пароля"
ADD_OBJECT = "Добавление объекта"
REMOVE_OBJECT = "Удаление объекта"
ACTIVATE_PASSIVE_CALENDAR = "Активировать пассивный календарь"
ADJUST_TO_QUARTER = "Подстройка к ¼ часа"
ADJUST_TO_MEASURING_PERIOD = "Подстройка к измерительному периоду"
ADJUST_TO_MINUTE = "Подстройка к минуте"
ADJUST_TO_PRESET_TIME = "Подстройка к уставке времени"
PRESET_ADJUSTING_TIME = "Задание уставки времени"
SHIFT_TIME = "Сдвиг времени"
REMOTE_DISCONNECT = "Отключение"  # in origin "Удаленное отключение"
REMOTE_RECONNECT = "Подключение"  # in origin "Удаленное включение"
RESET = "Сброс"
IMAGE_TRANSFER_INITIATE = "Инициализация передачи"
IMAGE_BLOCK_TRANSFER = "Передача блока данных"
IMAGE_VERIFY = "Проверка файла"
IMAGE_ACTIVATE = "Активирование файла"
CAPTURE = "Захват"
ENABLE_DISABLE = "Разрешить/запретить (номера)"
INSERT = "Ввести (номер)"
DELETE = "Стереть (номер)"
SECURITY_ACTIVATE = "Усиление безопасности"
GLOBAL_KEY_TRANSFER = "Передача общего ключа"
KEY_TRANSFER = "Передача ключа"
KEY_AGREEMENT = "Ключевые соглашения"
GENERATE_KEY_PAIR = "Генерация пары для ключа"
GENERATE_CERTIFICATE_REQUEST = "Запрос генерации сертификата"
IMPORT_CERTIFICATE = "Импорт сертификата"
EXPORT_CERTIFICATE = "Экспорт сертификата"
REMOVE_CERTIFICATE = "Удаление сертификата"
REQUEST_ACTION = "Запрос действия"
PUSH = "Протолнуть"
# demand register
NEXT_PERIOD = "Следующий период"


from fastapi import APIRouter, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel
from dependencies import get_token_header

router = APIRouter(
    prefix="/users",
    tags=["users"],
    dependencies=[Depends(get_token_header)],
    responses={404: {"description": "Not found"}},
)

fake_users_db = {"plumbus": {"name": "Plumbus"}, "gun": {"name": "Portal Gun"}}

@router.get("/")
async def read_users():
    return fake_users_db

@router.get("/{item_id}")
async def read_item(item_id: str):
    if item_id not in fake_users_db:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"name": fake_users_db[item_id]["name"], "item_id": item_id}

@router.put(
    "/{item_id}",
    tags=["custom"],
    responses={403: {"description": "Operation forbidden"}},
)
async def update_item(item_id: str):
    if item_id != "plumbus":
        raise HTTPException(
            status_code=403, detail="You can only update the item: plumbus"
        )
    return {"item_id": item_id, "name": "The great Plumbus"}

# @router.get("/me/", response_model=User)
# async def read_users_me(current_user: User = Depends(get_current_active_user)):
#     return current_user

# @router.get("/me/items/")
# async def read_own_items(current_user: User = Depends(get_current_active_user)):
#     return [{"item_id": "Foo", "owner": current_user.username}]


import math

def solution(n):

    # 파이썬에서는 long형을 따로 두지 않고 모든 정수는 int형 하나로 표시한다.
    tmp = int(math.sqrt(n))

    if math.sqrt(n) - tmp == 0:
        return (tmp+1) ** 2 # n ** m 이면 n의 m제곱
    else:
        return -1

print(solution(121))

025_정수 제곱근 판별.py


from clean_dfs import *
import pandas as pd
import boto3

def get_person_ids(applicants):
    applicants = applicants.reset_index().reset_index().drop(['index', 'id'], axis=1).rename(
        columns={'level_0': 'person_id'})
    return applicants

def generate_candidates_df(applicants, talents, sparta_day_results):
    applicants_and_talents = applicants.merge(talents,
                                              left_on=['name', 'invite_date'],
                                              right_on=['name', 'date'],
                                              how='left')

    candidates = applicants_and_talents.merge(sparta_day_results,
                                              left_on=['name', 'invite_date'],
                                              right_on=['name', 'date'],
                                              how='left')

    candidates = candidates.drop(candidates.index[candidates.person_id.duplicated().tolist()].tolist())
    candidates.reset_index(drop=True, inplace=True)
    candidates['person_id'] = candidates.index + 1
    return candidates

# makes an address table dataframe
def generate_address_df(df):
    addresses = list()
    # for each row in the talent csv files, add each unique address, city and postcode to a list
    for index, row in df.iterrows():
        address_list = list()
        address_list.append(row["address"])
        address_list.append(row["city"])
        address_list.append(row["postcode"])
        if address_list not in addresses:
            addresses.append(address_list)

    address_table = pd.DataFrame(addresses, columns=["address", "city", "postcode"])
    address_table.index = address_table.index + 1
    address_table['address_id'] = address_table.index
    return address_table

def address_id_in_candidates(candidates, address_table):
    merged = pd.merge(candidates, address_table, on='address')
    merged = merged.drop(
        columns=['city_x', 'address', 'postcode_x', 'city_y', 'postcode_y', 'date_y', 'weaknesses', 'strengths',
                 'tech_self_score'])
    merged = merged.rename(columns={"date_x": "date"})
    merged = merged.drop_duplicates(subset='person_id', keep='first')
    return merged

def generate_courses_table(dataframe):
    courses_df = dataframe
    courses_df = pd.DataFrame().assign(course=courses_df['course'], date=courses_df['date'])
    courses_df['course_id'] = courses_df.groupby(['course']).ngroup()
    courses_df = courses_df[['course', 'course_id', 'date']].copy()
    courses_df.set_index('course_id', inplace=True)
    courses_df.groupby(['course', 'date'])
    courses_df = courses_df.drop_duplicates()
    courses_df = courses_df.reset_index()
    courses_df.index = courses_df.index + 1
    courses_df['course_id'] = courses_df.index
    courses_df = courses_df.rename(columns={"course": "course_name"})

    return courses_df

# makes a weakness table dataframe
def generate_weakness_df(df):
    weaknesses_list = list()

    # for each row in the dataframe add distinct weaknesses to a list
    for index, row in df.iterrows():
        for weakness in row["weaknesses"]:
            if weakness not in weaknesses_list:
                weaknesses_list.append(weakness)

    # turn weaknesses list into a dataframe and add a column for the weakness id
    weakness_table = pd.DataFrame(weaknesses_list, columns=['weakness'])
    weakness_table.index = weakness_table.index + 1
    weakness_table['weakness_id'] = weakness_table.index
    return weakness_table

# takes talent jsons, talent csv, weakness dataframe as arguments
def generate_weakness_junc_df(candidates, weakness_df):
    weakness_junc_list = list()

    # for each row in the merged dataframe add (id, weakness) to a new dataframe for each weakness in weaknesses list
    for index, row in candidates.iterrows():
        person_id = row['person_id']
        weaknesses = row['weaknesses']

        if type(weaknesses) == list:
            for weakness in weaknesses:
                weakness_junc_list.append([person_id, weakness])

    weakness_junc = pd.DataFrame(weakness_junc_list, columns=['person_id', 'weakness'])

    weakness_junc = pd.merge(weakness_junc, weakness_df, on='weakness')[['person_id', 'weakness_id']]

    return weakness_junc

def generate_strengths_df(df):
    # Create a new DataFrame for strengths column only
    column_names = ['name', 'strengths']
    strengths = df[column_names]

    # Initialize an empty list to store row data
    strengths_list = []

    # Iterate over all rows
    for index, row in strengths.iterrows():
        strengths_list_row = row['strengths']

        # Iterate over each strength in the list
        for strength in strengths_list_row:
            if strength not in strengths_list:
                # Append row data to the list
                strengths_list.append(strength)

    # Create a new DataFrame from the list
    strengths_df = pd.DataFrame({'strength': strengths_list})
    strengths_df.index = strengths_df.index + 1
    strengths_df['strength_id'] = strengths_df.index
    # Return the new DataFrame
    return strengths_df

# takes talent jsons, talent csv, weakness dataframe as arguments
def generate_strengths_junc_df(candidates, strengths_df):
    strengths_junc_list = list()

    for index, row in candidates.iterrows():
        person_id = row['person_id']
        strengths = row['strengths']

        if type(strengths) == list:
            for strength in strengths:
                strengths_junc_list.append([person_id, strength])

    strengths_junc = pd.DataFrame(strengths_junc_list, columns=['person_id', 'strength'])

    strengths_junc = pd.merge(strengths_junc, strengths_df, on='strength')[['person_id', 'strength_id']]

    return strengths_junc

def generate_tss_df(df, candidates):
    # Create names lists
    names = []
    technologies = []
    self_scores = []

    # Loop through dataframe
    for index, row in df.iterrows():
        try:
            techs = list(row['tech_self_score'].keys())
            for tech in techs:
                names.append(row['name'].upper())
                technologies.append(tech)
                self_scores.append(row['tech_self_score'][tech])
        except AttributeError:
            continue

    tech_self_scores = pd.DataFrame({'name': names, 'technology': technologies, 'self_score': self_scores})
    merged = pd.merge(tech_self_scores, candidates, on='name')[['person_id', 'technology', 'self_score']]

    return merged

def generate_academy_table(df_academy_csv, courses_table, candidates):
    merged = pd.merge(df_academy_csv, courses_table, left_on='course', right_on='course_name')[['name', 'course_id']]
    academy_table = pd.merge(merged, candidates, on='name')[['person_id', 'course_id']]
    return academy_table

def generate_trainers_table(df):
    old_name = "Ely Kely"
    new_name = "Elly Kelly"
    df.loc[:, ["trainer"]] = df.loc[:, ["trainer"]].replace(old_name, new_name)
    trainers_course = df[["trainer", "course", "date"]]
    trainer_course = trainers_course.drop_duplicates(subset = ["date"], keep= "first")
    trainer_course['trainer_id'] = range(1, len(trainer_course) + 1)
    trainers = trainer_course.set_index('trainer_id')
    trainers_table = trainers.drop(['course', 'date'], axis=1).drop_duplicates()
    trainers = trainers.rename(columns={"course": "course_name"})
    trainers['trainer_id'] = trainers.index
    return trainers, trainers_table

def generate_trainers_junc(df_trainers, df_courses):
    trainers_junc = pd.merge(df_trainers, df_courses, on='course_name')[['course_id', 'trainer_id']]
    return trainers_junc

def use(key: str):
    s3 = boto3.client('s3')
    obj = s3.get_object(Bucket='data-eng-228-final-project', Key=key)
    filetype = key[key.index('.'):]
    if filetype == '.csv':
        data = pd.read_csv(obj['Body'])
    elif filetype == '.json':
        data = json.load(obj['Body'])
    elif filetype == '.txt':
        data = obj['Body'].read().decode('utf-8')
    return data

def make_wide(table):
    academy_results_wide = table.pivot(index=['name', 'week_no'],
                                       columns='attribute',
                                       values='score')
    academy_results = academy_results_wide.reset_index()
    return academy_results

def make_upper(df, attribute: str='name'):
    return df[attribute].transform(lambda attr: attr.upper())

def transform_acares(keystrings: list):
    tables = []

    for keystring in keystrings:
        current_ar = use(keystring)
        current_ar['name'] = make_upper(current_ar, 'name')
        raw_attributes = current_ar.columns[2:]

        names = []
        week_numbers = []
        attributes = []
        scores = []

        for name in current_ar['name']:
            for attribute in raw_attributes:
                names.append(name)
                week_numbers.append(attribute[attribute.index('W') + 1:])
                attributes.append(attribute[:attribute.index('_')])
                scores.append(float(current_ar[current_ar['name'] == name][attribute].tolist()[0]))
        academy_results_long = pd.DataFrame({'name': names,
                                             'week_no': week_numbers,
                                             'attribute': attributes,
                                             'score': scores})
        tables.append(academy_results_long)
    return pd.concat(tables)

def join_person_id_to_acares(candidates, academy_results):
    passed_candidates = candidates[(candidates.result == 'PASS').tolist()]

    academy_results = academy_results.merge(passed_candidates[['person_id', 'name']], left_on='name', right_on='name')
    return academy_results

# should return two lists first one with tables second with
def transform_to_tables(dataframes: dict, academy_csvs_file_names: list) -> (list, list):
    df_academy_csv = dataframes['academy_csv']
    df_academy_csv = clean_academy_csv(df_academy_csv)

    df_csv = dataframes['csv']
    df_csv = clean_talent_csv(df_csv)

    df_json = dataframes['json']
    df_json = clean_talent_json(df_json)

    df_txt = dataframes['txt']
    df_txt = clean_talent_txt(df_txt)

    tables = list()
    table_names = list()

    # generate all the tables to put into the database and add them to a list along with a list of the table names
    candidates_temp = generate_candidates_df(get_person_ids(df_csv), df_json, df_txt)
    address = generate_address_df(df_csv)
    tables.append(address)
    table_names.append('address')

    candidates = address_id_in_candidates(candidates_temp, address)
    tables.append(candidates)
    table_names.append('candidates')

    courses = generate_courses_table(df_academy_csv)
    tables.append(courses)
    table_names.append('courses')

    weaknesses = generate_weakness_df(df_json)
    tables.append(weaknesses)
    table_names.append('weaknesses')

    weakness_junc = generate_weakness_junc_df(candidates_temp, weaknesses)
    tables.append(weakness_junc)
    table_names.append('weakness_junc')

    strengths = generate_strengths_df(df_json)
    tables.append(strengths)
    table_names.append('strengths')

    strengths_junc = generate_strengths_junc_df(candidates_temp, strengths)
    tables.append(strengths_junc)
    table_names.append('strengths_junc')

    tech_scores = generate_tss_df(df_json, candidates)
    tables.append(tech_scores)
    table_names.append('tech_scores')

    academy = generate_academy_table(df_academy_csv, courses, candidates)
    tables.append(academy)
    table_names.append('academy')

    trainers_table, trainers = generate_trainers_table(df_academy_csv)
    tables.append(trainers)
    table_names.append('trainers')

    trainers_junc = generate_trainers_junc(trainers_table, courses)
    tables.append(trainers_junc)
    table_names.append('trainers_junc')

    df = transform_acares(academy_csvs_file_names)
    academy_results = make_wide(df)
    academy_results = join_person_id_to_acares(candidates, academy_results)
    tables.append(academy_results)
    table_names.append('academy_results')

    return tables, table_names


import cv2
import numpy as np
import numpy.linalg as la
import mv3dpose.geometry.geometry as gm
from mv3dpose.geometry.stereo import get_fundamental_matrix

def get_single_human3d(humans3d):
    J = len(humans3d[0])
    human3d = [None] * J  # single 3d person
    for jid in range(J):
        pts3d = []
        for person3d in humans3d:
            if person3d[jid] is not None:
                pts3d.append(person3d[jid])

        if len(pts3d) > 0:
            pt3d = np.mean(pts3d, axis=0)
            human3d[jid] = pt3d
    return human3d

def get_distance3d(person1, person2):
    J = len(person1)
    assert len(person2) == J
    result = []
    for jid in range(J):
        if person1[jid] is None or person2[jid] is None:
            continue
        d = la.norm(person1[jid] - person2[jid])
        result.append(d)
    return np.array(result)

def merge3d(persons3d, weights):
    """
    :param person3d:
    :param weights:
    :return:
    """
    assert len(persons3d) == len(weights)
    assert 1.001 > np.sum(weights) > 0.99
    J = len(persons3d[0])
    n = len(persons3d)
    result = [None] * J
    for jid in range(J):
        w_acc = 0
        never_hit = True
        pt3d = np.array([0, 0, 0], np.float32)
        for i in range(n):
            if persons3d[i][jid] is not None:
                w = weights[i]
                w_acc += w
                pt3d += w * persons3d[i][jid]
                never_hit = False
        if not never_hit:
            pt3d = pt3d / w_acc
            result[jid] = pt3d
    return result

def calculate_cost(cam1, person1, cam2, person2):
    """ calculate the epipolar distance between two humans
    :param cam1:
    :param person1:
    :param cam2:
    :param person2:
    :return:
    """
    F = get_fundamental_matrix(cam1.P, cam2.P)
    J = len(person1)
    assert J == len(person2)

    # drop all points that are -1 -1 (not visible)
    pts1 = []
    pts2 = []
    weights1 = []
    weights2 = []
    for jid in range(J):
        x1, y1, w1 = person1[jid]
        x2, y2, w2 = person2[jid]
        if x1 >= 0 and x2 >= 0:
            pts1.append((x1, y1))
            weights1.append(w1)
            pts2.append((x2, y2))
            weights2.append(w2)
    weights1 = np.clip(weights1, a_min=0, a_max=1)
    weights2 = np.clip(weights2, a_min=0, a_max=1)

    if len(pts1) == 0:
        return np.finfo(np.float32).max

    pts1 = np.array(pts1)
    pts2 = np.array(pts2)

    epilines_1to2 = np.squeeze(
        cv2.computeCorrespondEpilines(pts1, 1, F))

    epilines_2to1 = np.squeeze(
        cv2.computeCorrespondEpilines(pts2, 2, F))

    total = 0

    n_pairs = len(pts1)
    assert n_pairs == len(pts2)

    if n_pairs == 1:
        epilines_1to2 = np.expand_dims(epilines_1to2, axis=0)
        epilines_2to1 = np.expand_dims(epilines_2to1, axis=0)

    for p1, l1to2, w1, p2, l2to1, w2 in zip(
            pts1, epilines_1to2, weights1,
            pts2, epilines_2to1, weights2):
        d1 = gm.line_to_point_distance(*l1to2, *p2)
        d2 = gm.line_to_point_distance(*l2to1, *p1)
        total += d1 + d2
    return total / n_pairs  # normalize

class Hypothesis:

    def __init__(self, pts, cam, threshold,
                 scale_to_mm,
                 distance_threshold,
                 debug_2d_id=None):
        """
        :param pts: [ (x, y, w), ... ] * J
        :param cam: ProjectiveCamera
        :param threshold: if cost is larger then this
            value then the 'other' must not be merged
        :param scale_to_mm: d * scale_to_mm = d_in_mm
            that means: if our scale is in [m] we need to set
            scale_to_mm = 1000
        :param variance_threshold: in [mm]. When exceeded we try
            to drop the joint that causes the problem
        """
        self.scale_to_mm = scale_to_mm
        self.distance_threshold = distance_threshold
        self.points = [pts]
        self.cams = [cam]
        self.threshold = threshold
        if debug_2d_id is not None:  # only for debugging
            self.debug_2d_ids = [debug_2d_id]

    def size(self):
        return len(self.points)

    def get_3d_person(self):
        assert self.size() > 1
        distance_threshold = self.distance_threshold
        scale_to_mm = self.scale_to_mm

        humans2d = []
        for cid, (cam, human) in enumerate(zip(self.cams, self.points)):
            human2d = Person2d(cid, cam, human)
            humans2d.append(human2d)

        strong_humans2d = []
        weak_humans2d = []
        for person in humans2d:
            if person.believe > 0.35:  # .45
                strong_humans2d.append(person)
            else:
                weak_humans2d.append(person)

        if len(strong_humans2d) < 2:
            strong_humans2d = humans2d
            weak_humans2d = []

        strong_humans3d = []
        n = len(strong_humans2d)
        for pid1 in range(n - 1):
            for pid2 in range(pid1 + 1, n):
                person1 = strong_humans2d[pid1]
                person2 = strong_humans2d[pid2]
                person3d, _ = person1.triangulate(person2)
                strong_humans3d.append(person3d)

        strong_human3d = get_single_human3d(strong_humans3d)

        merge_targets = []
        for weak_human2d in weak_humans2d:
            humans3d_normal = []
            for strong_human2d in strong_humans2d:
                person3d, _ = weak_human2d.triangulate(strong_human2d)
                humans3d_normal.append(person3d)
            human3d_normal = get_single_human3d(humans3d_normal)

            humans3d_mirror = []
            weak_human2d_mirror = Person2d.flip_lr(weak_human2d)
            for strong_human2d in strong_humans2d:
                person3d, _ = weak_human2d_mirror.triangulate(strong_human2d)
                humans3d_mirror.append(person3d)
            human3d_mirror = get_single_human3d(humans3d_mirror)

            d_normal = np.mean(
                get_distance3d(strong_human3d, human3d_normal) * scale_to_mm)
            d_mirror = np.mean(
                get_distance3d(strong_human3d, human3d_mirror) * scale_to_mm)

            human3d_select = human3d_normal
            if d_normal > d_mirror:
                human3d_select = human3d_mirror

            if distance_threshold > 0:
                if min(d_normal, d_mirror) < distance_threshold:
                    merge_targets.append(human3d_select)
            else:
                merge_targets.append(human3d_select)  # always choose

        n = len(merge_targets)
        if n > 0:
            weights = [1] + [1/(n+1)] * n
            weights = np.array(weights)
            weights = weights/np.sum(weights)

            human3d = merge3d([strong_human3d] + merge_targets, weights)
        else:
            human3d = strong_human3d

        return human3d

    def calculate_cost(self, o_points, o_cam):
        """
        :param o_points: other points * J
        :param o_cam: other camera
        :return:
        """
        veto = False  # if true we cannot join {other} with this
        total_cost = 0
        for person, cam in zip(self.points, self.cams):
            cost = calculate_cost(cam, person,
                                  o_cam, o_points)
            total_cost += cost
            if cost > self.threshold and get_believe(person) > 0.5:
                veto = True

        return total_cost / len(self.points), veto

    def merge(self, o_points, o_cam):
        """ integrate {other} into our hypothesis
        :param o_points:
        :param o_cam:
        :return:
        """
        self.cams.append(o_cam)
        self.points.append(o_points)

class HypothesisList:

    def __init__(self, hypothesis_list):
        """
        :param hypothesis_list: list of hypothesis'
        """
        self.hypothesis_list = hypothesis_list

    def get_3d_person(self):
        """
        :return:
        """
        poses = []
        for hyp in self.hypothesis_list:
            pose = hyp.get_3d_person()
            poses.append(pose)

        J = len(poses[0])
        result = [None] * J
        for jid in range(J):
            valid_points = []
            for pose in poses:
                if pose[jid] is not None:
                    valid_points.append(pose[jid])
            if len(valid_points) > 0:
                result[jid] = np.mean(valid_points, axis=0)
            else:
                result[jid] = None

        return result

def get_believe(points2d):
    believe = []
    J = len(points2d)
    for jid in range(J):
        w = points2d[jid, 2]
        if w >= 0:
            believe.append(w)
    return np.mean(believe)

class Person2d:

    @staticmethod
    def flip_lr(person):
        """ creates a new person with left and right flipped
        :param person: {Person2d}
        :return:
        """
        left  = [5, 6, 7, 11, 12, 13, 15, 17, 18, 19, 20]
        right = [2, 3, 4,  8,  9, 10, 14, 16, 21, 22, 23]
        lr = left + right
        rl = right + left

        points2d = person.points2d.copy()
        points2d[lr] = points2d[rl]

        new_person = Person2d(person.cid, person.cam, points2d)
        return new_person

    def __init__(self, cid, cam, points2d, noundistort=False):
        """
        :param cid
        :param cam: {Camera}
        :param points2d: distorted points
        :param noundistort: if True do not undistort
        """
        self.cid = cid
        self.cam = cam
        self.believe = get_believe(points2d)

        if noundistort:
            self.points2d = points2d
        else:
            # ~~~ undistort ~~~
            valid_points2d = []
            jids = []
            for jid, pt2d in enumerate(points2d):
                if pt2d[0] < 0:
                    continue
                jids.append(jid)
                valid_points2d.append(pt2d)
            valid_points2d = np.array(valid_points2d, np.float32)
            points2d_undist = cam.undistort_points(valid_points2d)
            self.points2d = points2d.copy()
            for idx, jid in enumerate(jids):
                self.points2d[jid] = points2d_undist[idx]
            # ~~~~~~~~~~~~~~~~~~~~

    def __len__(self):
        return 24

    def triangulate(self, other):
        """
        :param other: {Person2d}
        :return:
        """
        Pts1 = []
        Pts2 = []
        jids = []
        W1 = []
        W2 = []

        J = len(other)
        assert J == len(self.points2d)
        assert J == len(self)

        for jid in range(J):
            if self.points2d[jid, 2] > 0 and \
                    other.points2d[jid, 2] > 0:
                Pts1.append(self.points2d[jid, 0:2])
                Pts2.append(other.points2d[jid, 0:2])
                jids.append(jid)
                W1.append(self.points2d[jid, 2])
                W2.append(other.points2d[jid, 2])

        Pts1 = np.transpose(Pts1)
        Pts2 = np.transpose(Pts2)

        Points3d = [None] * J
        w = [-1] * J
        if len(Pts1) > 0:
            Pts3d = gm.from_homogeneous(
                np.transpose(cv2.triangulatePoints(
                    self.cam.P, other.cam.P, Pts1, Pts2)))

            for jid, pt3d, w1, w2 in zip(jids, Pts3d, W1, W2):
                Points3d[jid] = pt3d
                w[jid] = min(w1, w2)

        return Points3d, np.array(w)


from django import forms
from django.forms import ModelChoiceField

from dashboard.models import Campaigns, DestinationLists, Bots, CommentsSources

class ToolsCommentsSourcesForm(forms.Form):
    def __init__(self, *args, **kwargs):
        dst_id = kwargs.pop('dst_id')
        super(ToolsCommentsSourcesForm, self).__init__(*args, **kwargs)
        if dst_id is not None and dst_id != '':
            try:
                all_fields = CommentsSources.objects.get(dst_id=dst_id)
                self.fields['comment_like_in_pages'].initial = all_fields.comment_like_in_pages
                self.fields['comment_like_in_walls'].initial = all_fields.comment_like_in_walls
                self.fields['comment_like_in_searches'].initial = all_fields.comment_like_in_searches
                self.fields['keywords'].initial = ",".join(all_fields.keywords)
                self.fields['exclude_keywords'].initial = ",".join(all_fields.exclude_keywords)
                self.fields['comment_like_in_groups'].initial = all_fields.comment_like_in_groups
                self.fields['comment_like_in_permalinks'].initial = all_fields.comment_like_in_permalinks
                self.fields['url_text_area'].initial = all_fields.url_text_area
            except CommentsSources.DoesNotExist as e:
                print(e)
    comment_like_in_pages = forms.BooleanField(required=False)
    comment_like_in_walls = forms.BooleanField(required=False)
    comment_like_in_searches = forms.BooleanField(required=False)
    keywords = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': "form-control"}))
    exclude_keywords = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': "form-control"}))
    comment_like_in_groups = forms.BooleanField(required=False)
    comment_like_in_permalinks = forms.BooleanField(required=False)
    url_text_area = forms.CharField(widget=forms.Textarea(attrs={'cols': '60', 'rows': 5, 'class': "form-control"}))


"""Contains routes and views(?) to handle templates (.jinja2 files)."""

import os
import base64

from flask import Flask, render_template, request, redirect, url_for, session

from model import SavedTotal

app = Flask(__name__)
# app.secret_key = b'\xb6x(\xd67\x1f\xa7\x15\x92\xf1VqU\xe9|\xbcqu\xac\xf6\x16\xa8\x8f\xe5'
app.secret_key = os.environ.get('SECRET_KEY').encode()

@app.route('/')
def home():
    return redirect(url_for('add'))

@app.route('/retrieve')
def retrieve():
    # Retrieve code argument from the user's code sumbission
    # Compare to request.form method because retrieve form uses GET method
    code = request.args.get('code', None)

    # If the user is visiting the retrieve page (did not submit form yet):
        # Then just render the retrieve.jinja2 template
    # But if they did submit the form:
        # Then attempt to retrieve the SavedTotal that has the provided code
        # Then save the total from that SavedTotal into session["total"]
        # Then redirect the user back to the main 'add' page

    if code is None:
        return render_template("retrieve.jinja2")
    try:
        saved_total = SavedTotal.get(SavedTotal.code == code)
    except SavedTotal.DoesNotExist:
        return render_template("retrieve.jinja2", error="Code not found")

    session['total'] = saved_total.value

    return redirect(url_for('add'))

@app.route('/add', methods=['GET', 'POST'])
def add():
    if 'total' not in session:
        session['total'] = 0
    if request.method == 'POST':
        # Handle the form submission - which uses POST method
        number = int(request.form['number'])
        session['total'] += number

    return render_template('add.jinja2', session=session)

@app.route('/save', methods=['POST'])
def save():
    total = session.get('total', 0)
    code = base64.b32encode(os.urandom(8)).decode().strip('=')

    saved_total = SavedTotal(value=total, code=code)
    saved_total.save()

    return render_template('save.jinja2', code=code)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 6738))
    # port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)


from copy import deepcopy, copy
from typing import Dict, Any, Generic, List
from ..lexer import Token, LexerThread
from ..common import ParserCallbacks

from .lalr_analysis import Shift, ParseTableBase, StateT
from lark.exceptions import UnexpectedToken

###{standalone

class ParseConf(Generic[StateT]):
    __slots__ = 'parse_table', 'callbacks', 'start', 'start_state', 'end_state', 'states'

    parse_table: ParseTableBase[StateT]
    callbacks: ParserCallbacks
    start: str

    start_state: StateT
    end_state: StateT
    states: Dict[StateT, Dict[str, tuple]]

    def __init__(self, parse_table: ParseTableBase[StateT], callbacks: ParserCallbacks, start: str):
        self.parse_table = parse_table

        self.start_state = self.parse_table.start_states[start]
        self.end_state = self.parse_table.end_states[start]
        self.states = self.parse_table.states

        self.callbacks = callbacks
        self.start = start

class ParserState(Generic[StateT]):
    __slots__ = 'parse_conf', 'lexer', 'state_stack', 'value_stack'

    parse_conf: ParseConf[StateT]
    lexer: LexerThread
    state_stack: List[StateT]
    value_stack: list

    def __init__(self, parse_conf: ParseConf[StateT], lexer: LexerThread, state_stack=None, value_stack=None):
        self.parse_conf = parse_conf
        self.lexer = lexer
        self.state_stack = state_stack or [self.parse_conf.start_state]
        self.value_stack = value_stack or []

    @property
    def position(self) -> StateT:
        return self.state_stack[-1]

    # Necessary for match_examples() to work
    def __eq__(self, other) -> bool:
        if not isinstance(other, ParserState):
            return NotImplemented
        return len(self.state_stack) == len(other.state_stack) and self.position == other.position

    def __copy__(self):
        return type(self)(
            self.parse_conf,
            self.lexer, # XXX copy
            copy(self.state_stack),
            deepcopy(self.value_stack),
        )

    def copy(self) -> 'ParserState[StateT]':
        return copy(self)

    def feed_token(self, token: Token, is_end=False) -> Any:
        state_stack = self.state_stack
        value_stack = self.value_stack
        states = self.parse_conf.states
        end_state = self.parse_conf.end_state
        callbacks = self.parse_conf.callbacks

        while True:
            state = state_stack[-1]
            try:
                action, arg = states[state][token.type]
            except KeyError:
                expected = {s for s in states[state].keys() if s.isupper()}
                raise UnexpectedToken(token, expected, state=self, interactive_parser=None)

            assert arg != end_state

            if action is Shift:
                # shift once and return
                assert not is_end
                state_stack.append(arg)
                value_stack.append(token if token.type not in callbacks else callbacks[token.type](token))
                return
            else:
                # reduce+shift as many times as necessary
                rule = arg
                size = len(rule.expansion)
                if size:
                    s = value_stack[-size:]
                    del state_stack[-size:]
                    del value_stack[-size:]
                else:
                    s = []

                value = callbacks[rule](s) if callbacks else s

                _action, new_state = states[state_stack[-1]][rule.origin.name]
                assert _action is Shift
                state_stack.append(new_state)
                value_stack.append(value)

                if is_end and state_stack[-1] == end_state:
                    return value_stack[-1]
###}


from pprint import pprint

class Router(object):

    def __init__(self):

        self.shortest_router = 100
        self.longest_router = 0

    def read_router_file(self, filename):
        router_dict = {}
        f = open(filename)
        for line in f:
            line = line.strip().split(',')
            router_dict[line[0]] = line[1]
            if self.shortest_router > len(line[0]):
                self.shortest_router = len(line[0])
            if self.longest_router < len(line[0]):
                self.longest_router = len(line[0])

        f.close()
        return router_dict

    def read_phone_file(self, filename):
        return [line.strip() for line in open(filename)]

    def get_longest_prefix(self, dict, number):
        if len(number) > self.longest_router:
            chop = len(number) - self.longest_router
            number = number[:-chop]

        while True:
            if len(number) < self.shortest_router:
                break

            if number in dict:
                return dict[number]
            else:
                number = number[:-1]

# recursion
# set initial phone # length to longest router
# chop off phone # digits until length < shortest_router
def main():

    router = Router()
    price_list = []
    router_dict = router.read_router_file('route-costs-10000000.txt')
    phone_list = router.read_phone_file('phone-numbers-1000.txt')
    for number in phone_list:
        price_list.append([number, router.get_longest_prefix(router_dict, number)])
    print(price_list)

if __name__ == '__main__':
    main()


import time
import webbrowser
from datetime import datetime

def CreateTimerApp():
    print("Enter number of mins : ")
    min_c = int(input())
    seconds_c = min_c * 60
    print("Your time start now : ")
    for i in range(seconds_c,1,-1):
        time.sleep(1)
        print("Time remaining : %3d" % i, end = "\r")
    webbrowser.open("https://www.youtube.com/watch?v=ADYP32VV-ak&t=1781s")

currentDateAndTime = datetime.now()
print("The current date and time is", currentDateAndTime)
currentTime = currentDateAndTime.strftime("%H:%M:%S")
print("Time Now : ", currentTime)
print(datetime.today().strftime("%I:%M %p"))
#CreateTimerApp()


import numpy as np
import pylab as pl
from os import path

data_dir = path.abspath('/data/ndhuang/cryodump/scan')

UC_head = np.fromfile(path.join(data_dir, 'current_rms_00.dat'))
UC_stage = np.fromfile(path.join(data_dir, 'current_rms_11.dat'))

freq = 39
fig = pl.figure()
pl.subplot(211, title = 'UC head: scan')
head_spect = np.real(np.fft.rfft(UC_head))
head_freqs = np.fft.fftfreq(head_spect.size, d = 1.0 / freq)
ind = np.argwhere([s > 0 and f > 0 for (f, s) in zip(head_freqs, head_spect)])
pl.loglog(head_freqs[ind], head_spect[ind])

pl.subplot(212, title = 'UC stage: scan')
stage_spect = np.real(np.fft.rfft(UC_stage))
stage_freqs = np.fft.fftfreq(stage_spect.size, d = 1.0 / freq)
ind = np.argwhere([s > 0 and f > 0 for (f, s) in zip(stage_freqs, stage_spect)])
pl.loglog(stage_freqs[ind], stage_spect[ind])
pl.savefig('scan.png')

pl.show()


# 5 最长回文子串 https://leetcode-cn.com/problems/longest-palindromic-substring/

# 回文串是一个正读和反读都一样的字符串，"level", "noon"

class Solution:
    def longestPalindrome(self, s):
        """
        babad 和 dabab (正序和倒序的字符串) 形成网格，网格中的值为最长公共子串长度
        正序为行，返回值， a[i-n:i+1]
        if a[i] = b[j]:
            cell[i][j] = cell[i-1][j-1] +1
        else:
            cell[i][j]= 0
        aacdefcaa
        需要进一步判断子串的索引和逆序子串中的原始索引是否是同一个
        :param s:
        :return:
        """
        if not s:
            return ""
        a = s
        # 逆序
        b = s[::-1]
        cell = [[0]*len(s) for i in range(len(s))]

        for i in range(len(a)):
            for j in range(len(b)):
                if a[i] == b[j]:
                    if i>=1 and j>=1:
                        cell[i][j] = cell[i-1][j-1] + 1
                    else:
                        cell[i][j] = 1
                else:
                    cell[i][j] = 0
        max_value = 0
        idx_arr = [0, 0]
        for i in range(len(cell)):
            for j in range(len(cell[i])):
                if cell[i][j] > max_value:
                    n = cell[i][j]
                    sub_str_s = i - n +1
                    re_sub_str_s = len(s) - j -1
                    # 子串的原始索引和逆序子串中的原始索引是同一个，才是回文子串
                    if sub_str_s == re_sub_str_s:
                        idx_arr = [i, j]
                        max_value = cell[i][j]
        n = cell[idx_arr[0]][idx_arr[-1]]
        res = a[idx_arr[0]-n+1: idx_arr[0]+1]
        return res

s = "babad"
# s = "cbbd"
# s = "aacdefcaa"
sl = Solution()
print(sl.longestPalindrome(s))


import numpy as np
from falass import dataformat
import matplotlib.pyplot as plt
import MDAnalysis as mda

class Files:
    """File parsing.

    Definition of the files to be used in the analysis; such as the .pdb file, .lgt file (is one exists), .dat file
    (is one exists). Also defined at this time are the resolution (this is ignored if the .dat file has 4 columns)
    and the percentage error in the intensity (this is also ignored if the .dat file has >2 columns), and whether
    the simulation cell should be flipped in the xy-plane.

    Parameters
    ----------
    pdbfile: str
        Path and name of the .pdb file to be analysed.
    lgtfile: str, optional
        Path and name of the .lgt file (which contains the scattering lengths of each of the atom types in the
        pdbfile). If a lgtfile is not defined falass will help in the creation of one.
        Currently the .lgt file style that is supported is a 3 column space separated txt file where the columns are
        atom_type, real_scattering_length, and imaginary_scattering_length respectively.
    datfile: str, optional
        Path and name of the .dat file (from which the analysis q vectors are drawn and also for subsequent
        comparison between theory and experiment). If a datfile is not defined falass will allow the user to
        define a range of q vectors to calculate the reflectometry over.
        Currently the .dat file style that is supported is a 2, 3, and 4 column space separated txt files where the
        columns are q, i, di, and dq respectively.
    resolution: float, optional
        Percentage of the q vector for the width of the resolution Gaussian function to be used in the data
        smearing, if a 4 column datfile is given this is ignored.
    ierror: float, optional
        Percentage error of the intensity to be assumed, if a >2 column datfile is used this is ignored.
    flip: bool, optional
        False if the system should be read as is, true is the simulation cell should be rotated through the
        xy-plane -- note that falass treats the first side that the neutron or X-ray interacts with as that at z=0.
    xray: bool, optional
        True if the scattering length of the particles should be scaled by the classical radius of an electron.
    """
    def __init__(self, pdbfile=None, lgtfile=None, datfile=None, resolution=5., ierror=5., flip=False, xray=False):
        self.pdbfile = pdbfile
        self.cell = []
        self.atoms = []
        self.number_of_timesteps = 0
        self.times = []
        self.lgtfile = lgtfile
        self.scat_lens = []
        self.datfile = datfile
        self.expdata = []
        self.ierror = ierror
        self.resolution = resolution
        self.flip = flip
        self.xray = xray
        return

    def set_file(self, pdbfile=None, lgtfile=None, datfile=None):
        """Edits files.

        Let the subsequent definition, or redefinition of the pdbfile, lgtfile or datfile.

        Parameters
        ----------
        pdbfile: str
            Path and name of the .pdb file to be analysed.
        lgtfile: str, optional
            Path and name of the .lgt file (which contains the scattering lengths of each of the atom types in the
            pdbfile). If a lgtfile is not defined falass will help in the creation of one.
            Currently the .lgt file style that is supported is a 3 column space separated txt file where the columns are
            atom_type, real_scattering_length, and imaginary_scattering_length respectively.
        datfile: str, optional
            Path and name of the .dat file (from which the /home/arm61/progs/refnxanalysis q vectors are drawn and also for subsequent
            comparison between theory and experiment). If a datfile is not defined falass will allow the user to
            define a range of q vectors to calculate the reflectometry over.
            Currently the .dat file style that is supported is a 2, 3, and 4 column space separated txt files where the
            columns are q, i, di, and dq respectively.
        """
        if pdbfile:
            self.pdbfile = pdbfile
        if lgtfile:
            self.lgtfile = lgtfile
        if datfile:
            self.datfile = datfile
        return

    def read_pdb(self):
        """Parse .pdb.

        Reads the .pdb file into memory. Currently the atoms must have the title 'ATOM', the timestep time needs to
        be the last text in the 'TITLE' line, and the cell dimensions are taken from the 'CRYST1' line, and assumed to
        be orthorhomic. Non-orthorhomic cells are not necessarily supported.
        """
        print("Reading PDB file")
        self.u = u = mda.Universe(self.pdbfile)

        self.cell = []
        self.atoms = []

        for ts in u.trajectory:
            self.cell.append(u.dimensions[:3].copy())
            # grab z positions
            pos = u.atoms.positions[:, 2]
            # flip?
            if self.flip:
                pos = flip_zpos(u.dimensions[2], pos)
            self.atoms.append(
                [dataformat.AtomPositions(at.name, p) for at, p in zip(u.atoms, pos)]
            )

        lines = line_count(self.pdbfile)
        with open(self.pdbfile, 'r') as f:
            for i, line in enumerate(f):
                if "TITLE  " in line:
                    self.number_of_timesteps, new_time = iterate_time(self.number_of_timesteps, line)
                    self.times.append(new_time)

        return

    def read_lgt(self):
        """Parses .lgt.

        Parses the lgtfile. If no lgtfile is defined falass will help the user to build one by working through the
        atom types in the pdb file and requesting input of the real and imaginary scattering lengths. This will also
        occur if a atom type if found in the pdbfile but not in the given lgts file. falass will write the lgtfile
        to disk if atom types do not feature in the given lgtfile or one is written from scratch.
        """
        if self.lgtfile:
            lines = line_count(self.lgtfile)
            print("Reading LGT file")
            percentage = 0
            print_update(percentage)
            file = open(self.lgtfile, 'r')
            for i, line in enumerate(file):
                percentage_new = np.floor(i / lines * 100)
                percentage = check_update(percentage, percentage_new)
                line_list = line.split()
                duplicate = check_duplicates(self.scat_lens, line_list[0])
                if not duplicate:
                    i = 1
                    if self.xray:
                        i *= 2.817940
                    self.scat_lens.append(dataformat.ScatLens(line_list[0], float(line_list[1])*i, float(line_list[2])*i))
            print_update(100)
            file.close()
        else:
            raise ValueError("No lgtfile has been defined.")
        return

    def read_dat(self):
        """Parses .dat.

        Parses the .dat file, supporting 2, 3, and 4 column files consisting of q, i, di, and dq with comments in lines
        where the first character is a '#'. If there is no .dat file the get_qs() function should be used to generate
        q vectors to allow for the calculation of the reflectometry profile.
        """
        self.expdata=[]
        if self.datfile:
            lines = line_count(self.datfile)
            print("Reading DAT file")
            percentage = 0
            print_update(percentage)
            file = open(self.datfile, 'r')
            for i, line in enumerate(file):
                percentage_new = np.floor(i / lines * 100)
                percentage = check_update(percentage, percentage_new)
                if line[0] != '#':
                    line_list = line.split()
                    if len(line_list) == 2:
                        self.expdata.append(dataformat.QData(float(line_list[0]), float(line_list[1]),
                                                             float(line_list[1]) * (self.ierror / 100),
                                                             float(line_list[0]) * (self.resolution / 100)))
                    if len(line_list) == 3:
                        self.expdata.append(dataformat.QData(float(line_list[0]), float(line_list[1]),
                                                             float(line_list[2]),
                                                             float(line_list[0]) * (self.resolution / 100)))
                    if len(line_list) == 4:
                        self.expdata.append(dataformat.QData(float(line_list[0]), float(line_list[1]),
                                                             float(line_list[2]), float(line_list[3])))
            print_update(100)
            file.close()
        else:
            print('No DAT file has been given, therefore no comparison will be conducted, please use the get_qs '
                  'function. Alternatively the DAT file can be added using the setFile function.')
        return

    def get_qs(self, start=0.005, end=0.5, number=50):
        """Make custom q-vectors.

        If no datfile exists this function should be used to generate a linear-spaced range of q-vector for the
        calculation of the reflectometry over.

        Parameters
        ----------
        start: float, optional
            The first q-vector for which the reflectometry should be calculated.
        end: float, optional
            The last q-vector for which the reflectometry should be calculated.
        number: int, optional
            The number of q-vectors.
        """
        q_values = np.linspace(start, end, number)
        for i in range(0, len(q_values)):
            self.expdata.append(dataformat.QData(q_values[i], None, None, q_values[i] * (self.resolution / 100)))
        return

    def plot_dat(self, rq4=True):
        """Plot the experimental data file that has been read in.

        Parameters
        ----------
        rq4: bool, optional
            Should the plot be created with a y-axis of Rq^4
        """
        if self.datfile:
            x = []
            y = []
            dy = []
            plt.rc('text')
            plt.rc('font', family='serif')
            if rq4:
                for i in range(0, len(self.expdata)):
                    x.append(self.expdata[i].q)
                    y.append(np.log10(self.expdata[i].i * self.expdata[i].q ** 4))
                    dy.append((self.expdata[i].di * self.expdata[i].q ** 4) /
                              (self.expdata[i].i * np.log(10)))
                    plt.ylabel('log($Rq^4$) (\AA$^4$)')
            else:
                for i in range(0, len(self.expdata)):
                    x.append(self.expdata[i].q)
                    y.append(np.log10(self.expdata[i].i))
                    dy.append(self.expdata[i].di / (self.expdata[i].i * np.log(10)))
                    plt.ylabel('log($R$)')
            x = np.asarray(x)
            y = np.asarray(y)
            dy = np.asarray(dy)
            plt.errorbar(x, y, yerr=dy, marker='o', ls='')
            plt.xlabel('$q$ (\AA)')
            plt.show()
        else:
            raise ValueError("No data file is defined.")
        return plt

def check_duplicates(array, check):
    """Stops duplicate atom types.

    Checks if an atom type has already been added to an array.

    Parameters
    ----------
    array: array-type ScatLens
        The array to check.
    check: str
        The atom type to try and find.

    Returns
    -------
    bool
        True if the atom type is already present in the scatlen type array, false if not.
    """
    for i in range(0, len(array)):
        if array[i].atom == check:
            return True
    return False

def line_count(filename):
    """File length.

    Quickly counts the number of lines in a file

    Parameters
    ----------
    filename: str
        Name of the file that the number of lines is desired for.

    Returns
    -------
    int
        Number of lines in the file
    """
    i = 0
    with open(filename) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

def flip_zpos(cell, zpos):
    """Flip the z-position.

    Flips the z-position through the xy-plane.

    Parameters
    ----------
    cell: float
        z-cell dimension.
    zpos: float
        z-position for an atom.

    Returns
    -------
    float
        z-position after the flipping
    """
    return np.sqrt(np.square(cell - zpos))

def iterate_time(number_of_timesteps, line):
    """Increases the number of timesteps

    Iterates the number of timesteps and finds the new timestep value.

    Parameters
    ----------
    number_of_timesteps: int
        Number of timesteps found so far.
    line: str
        'TITLE' line from the pdb file which has timestep information.

    Returns
    -------
    int
        Number of timesteps iterated by 1.
    float
        Time of newest timestep.
    """
    number_of_timesteps += 1
    new_time = float(line.split()[-1])
    return number_of_timesteps, new_time

def print_update(percentage):
    """Print percentage read-in.

    Prints a percentage of how much as been read in at a given time.

    Parameters
    ----------
    string: int
        Percentage read-in complete.
    """
    print("[{} {} % ]".format('#' * int(percentage / 10), int(percentage / 10) * 10))

def check_update(percentage, percentage_new):
    """Check if an update string should be printed.

    Assess if an update string should be printed to the screen.

    Parameters
    ----------
    string: int
        Percentage of way through reading.

    Returns
    -------
    int
        New percentage of way through reading.
    """
    if percentage_new > percentage + 9:
        percentage = percentage_new
        print_update(percentage)
    return percentage


""" Array
A generic array data structure.
Used as a base class for vector and matrix classes.
Example:
    $ from linear_algebra.array import Array
    $ array = Array([[1,2],[3,4]])
"""

from __future__ import annotations
from typing import Union

NumericType = Union[complex, float]
ArrayType = Union[
    list[list[list[NumericType]]],  # 3D tensor
    list[list[NumericType]],  # matrix
    list[NumericType],  # vector
    NumericType  # scalar
]

class Array:
    """Generic array data structure"""

    def __init__(self, data: ArrayType) -> None:
        if not self._valid_shape(data):
            raise ValueError("Dimension mismatch within array")
        self.data = data
        self.shape = self._shape(data, [])

    def __repr__(self) -> str:
        if len(str(self.data)) < 64:
            representation = f"Array {self.data} of shape {self.shape}"
        else:
            representation = f"Array of shape {self.shape}"
        return representation

    def __len__(self) -> int:
        length = self.shape[0]
        return length

    def __getitem__(self, index) -> ArrayType:
        if len(self.shape) >= 1:
            result = self.data[index]
        else:
            raise IndexError("Array is a scalar value")
        return result

    def __setitem__(self, index, value: ArrayType) -> None:
        if len(self.shape) >= 1:
            prior_data = self.data
            prior_data[index] = value
            if not self._valid_shape(prior_data):
                raise ValueError("Dimension mismatch within array")
            self.data = prior_data
            self.shape = self._shape(prior_data, [])
        else:
            raise IndexError("Array is a scalar value")

    def __add__(self, other: Array) -> Array:
        if self.shape != other.shape:
            raise ValueError("Array dimensions do not match")
        result = self._add(self.data, other.data)
        return Array(result)

    def __sub__(self, other: Array) -> Array:
        if self.shape != other.shape:
            raise ValueError("Array dimensions do not match")
        result = self._sub(self.data, other.data)
        return Array(result)

    def _add(self, array: ArrayType, other: ArrayType) -> ArrayType:
        """Add the elements of two arrays"""
        if isinstance(array, list):
            results = []
            for array_item, other_item in zip(array, other):
                if isinstance(array_item, list):
                    result = self._add(array_item, other_item)
                    results.append(result)
                else:
                    result = array_item + other_item
                    results.append(result)
        else:
            results = array + other
        return results

    def _sub(self, array: ArrayType, other: ArrayType) -> ArrayType:
        """Subtract the elements of two arrays"""
        if isinstance(array, list):
            results = []
            for array_item, other_item in zip(array, other):
                if isinstance(array_item, list):
                    result = self._sub(array_item, other_item)
                    results.append(result)
                else:
                    result = array_item - other_item
                    results.append(result)
        else:
            results = array - other
        return results

    def _valid_shape(self, data: ArrayType) -> bool:
        """Check whether an array has a valid shape"""
        if isinstance(data, list):
            prior_length = None
            for item in data:
                if isinstance(item, list):
                    item_length = len(item)
                else:
                    item_length = 0
                if prior_length is None:
                    prior_length = item_length
                if item_length != prior_length:
                    return False
                result = self._valid_shape(item)
                if not result:
                    return False
        return True

    def _shape(self, data: ArrayType, shape: list[int]) -> tuple[int, ...]:
        """Get the shape of an array"""
        if isinstance(data, list):
            outer_length = len(data)
            shape.append(outer_length)
            if outer_length == 0:
                return tuple(shape)
            result = self._shape(data[0], shape)
            if isinstance(result, tuple):
                return result
        return tuple(shape)


class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.

        runtime is O(mn)
        space complexity O(m+n)
        """

        row_index= set()
        col_index= set()

        row= len(matrix)
        col= len(matrix[0])

        for r in range(row):

            for c in range(col):

                if matrix[r][c]==0:
                    row_index.add(r)
                    col_index.add(c)
        for Row in row_index:

            for c in range(col):

                matrix[Row][c]=0
        for r in range(row):

            for Col in col_index:

                matrix[r][Col]=0

        return matrix


from triton_client import TritonClient
from time import sleep, time
import numpy as np
from concurrent.futures import ThreadPoolExecutor

def working():
    count = 0
    client = TritonClient(url='localhost:8001', model_name='extractor_onnx')
    executor = ThreadPoolExecutor(max_workers=1)
    message = None
    future = None
    while 1:
        print(f'count is {count}')
        image = np.random.random((120, 120, 3))
        if image is not None and future is None:
            message = image
            future = executor.submit(client.gen_response, message)
        sleep(1)
        if future is not None:
            if future.running():
                print('future still running...')
            if future.done():
                print('-------FUTURE IS DONE-------')
                print('GOT VECTOR WHICH HAS SHAPE', future.result().shape)
                future.cancel()
                future = None
                diff = np.subtract(message, image)
                diff = np.mean(diff)
                if diff:
                    message = image
                    future = executor.submit(client.gen_response, message)
        count += 1
        if count == 50:
            break

if __name__ == '__main__':
    working()


from __future__ import annotations

import textwrap
from typing import List, Tuple, Union

from aiogram import Router, html
from aiogram.filters import Command, CommandObject
from aiogram.types import Message
from likeinterface import Interface, methods
from likeinterface.exceptions import LikeInterfaceError
from likeinterface.types import Cards

from exc import CommandUsageError

router = Router()

def validate_command_args(args: str) -> Union[Tuple[List[str], List[str]], bool]:
    if not args:
        return False

    try:
        board, *hands = args.split()
    except ValueError:
        return False

    board = textwrap.fill(board, 2).split("\n")
    return board, hands

@router.message(Command(commands="evaluate"))
async def evaluate_command_handler(
    message: Message,
    command: CommandObject,
    interface: Interface,
) -> None:
    args = validate_command_args(command.args)
    if not args:
        raise CommandUsageError(
            html.quote(
                f"Usage: {command.prefix}{command.command} {html.bold('[board]')} {html.bold('[hand]')} {html.bold('[hand]')}\n"
                f"Example {html.code(f'{command.prefix}{command.command} AcAdAhAsKc 2c2d 2h2s')}"
            ),
        )

    board, hands = args

    try:
        text = "\n".join(
            f"{hand.hand.title()} - {html.bold(hands[hand.id])}"
            for hand in await interface.request(
                method=methods.GetEvaluationResult(cards=Cards(board=board, hands=hands))
            )
        )
    except LikeInterfaceError:
        await message.answer(text="Invalid arguments")
    else:
        await message.answer(text=html.quote(text))


from django.db import models
from .NonStructuralElement import NonStructuralElement
'''
The 'RoofAssembly' class represents the roof assembly in a building.
'''
class RoofAssembly(NonStructuralElement):
	isAccessible = models.BooleanField(blank=True)
	isLightweightConstruction = models.BooleanField(blank=True)

	def serialize(self):
		return {
			'isAccessible': self.isAccessible,
			'isLightweightConstruction': self.isLightweightConstruction,
		} | super().serialize()


import pickle as pkl
import  pdb
from collections import Counter

def substr(x, y):
    # remove list without duplicates
    xn = [elem for elem in x]
    for yelem in y:
        if yelem in xn:
            xn.remove(yelem)
    return xn
def get_prec_recall_measures(gt, pred):
    curr_pred = substr(pred, gt)
    curr_gt = substr(gt, pred)
    tp =  len(pred) - len(curr_pred)
    fp = len(curr_pred)
    fn = len(curr_gt)
    # if fp > 0. and tp > 0.:
    #    pdb.set_trace()
    return tp,fp,fn
files = ['../dataset/watch_pred.p']

it = 0
for it in range(len(files)):
    file_name = files[it]

    with open(file_name, 'rb') as f:
        ct = pkl.load(f)
    cand_files = ct.keys()

    tps, fps, fns, correct  = 0, 0, 0, 0
    for cand_file in cand_files:
        content_pred = ct[cand_file]
        gt = [x for x in content_pred['ground_truth'] if x not in [None, 'None']]
        pred = [x for x in content_pred['prediction'] if x not in [None, 'None']]
        tp, fp, fn = get_prec_recall_measures(gt, pred)
        tps += tp
        fps += fp
        fns += fn
        if (fp + fn) == 0:
            correct += 1
    precision = 1.0 * tps / (tps + fps)
    recall = 1.0 * tps / (tps + fns)
    f1 = 2 * precision * recall / (precision + recall)
    print('Precision. {}, Recall. {}, F1. {}, Accuracy: {}'.format(precision, recall, f1, correct*1./len(cand_files)))


import math
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

def plot_yt_ypred(test_pred,lim,subplots = 1, caption = [],rang =[]):
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(1,subplots, figsize = (20,10))
    for x  in range(subplots):
        if rang == []:
            ax[x].plot(test_pred[x][0],'g*', label = "Test")
            ax[x].set_ylim(lim[0],lim[1])
            if len(caption) > 0:
                ax[x].set_xlabel(caption[x])
            ax[x].plot(test_pred[x][1], 'ro', label = "Predict")
            ax[x].legend()
        else:
            ax[x].set_ylim(lim[0],lim[1])
            ax[x].plot(test_pred[x][0][rang[0]:rang[1]],'g*', label = "Test")
            if len(caption) > 0:
                ax[x].set_xlabel(caption[x])
            ax[x].plot(test_pred[x][1][rang[0]:rang[1]], 'ro', label = "Predict")
            ax[x].legend()

    plt.show()

def bplot(df,x,y,be=-1,end=-1):
    '''Simple plot x vs y'''
    if(be!=-1 and end!=-1):
        xpoints = df[x][be:end]
        ypoints = df[y][be:end]

    else:
        xpoints = df[x]
        ypoints = df[y]
    plt.scatter(xpoints, ypoints)
    plt.show()