"""Representational similarity analysis."""

import os
import numpy as np
from scipy import stats
from statsmodels.stats.multitest import multipletests
from scipy import optimize
import scipy.spatial.distance as sd
import pandas as pd

from mindstorm import prsa
from wikisim import task
from wikisim import model

def rank_dsm(dsm):
    """Rank transform a dissimilarity matrix."""
    res = sd.squareform(stats.rankdata(sd.squareform(dsm)))
    return res

def define_network(roi_names, roi_labels, net_ids, net_names, net_labels):
    """Define ROIs in a network."""
    nn = [net_names[i] for i in net_ids]
    nl = [net_labels[i] for i in net_ids]
    df = pd.DataFrame({'roi_label': roi_labels, 'net': nn, 'net_label': nl,
                       'net_id': net_ids}, index=roi_names)
    return df

def get_rois(roi_group):
    """Get a list of ROI names for a given ROI group."""
    if isinstance(roi_group, list):
        rois = roi_group
        labels = roi_group
        net_ids = [0] * len(roi_group)
        net_names = ['roi'] * len(roi_group)
        net_labels = ['roi'] * len(roi_group)
    elif roi_group == 'func':
        rois = ['r_ofa', 'b_ffa', 'b_pat', 'b_opa', 'b_rsc', 'b_ppa']
        labels = ['OFA', 'FFA', 'ATFA', 'OPA', 'RSC', 'PPA']
        net_ids = [0, 0, 0, 1, 1, 1]
        net_names = ['face', 'scene']
        net_labels = ['Face', 'Scene']
    elif roi_group == 'pmat':
        rois = ['b_hopa_ang', 'b_hopa_prec', 'b_hopa_rscpcc', 'b_phc_jl_mni',
                'b_fshs_phpc', 'b_fshs_ahpc',
                'b_prc_jl_mni', 'b_hopa_amyg', 'b_hopa_fus', 'b_hopa_itc',
                'b_hopa_tpo', 'b_hopa_pope', 'b_hopa_ptri', 'b_hopa_ofc']
        labels = ['ANG', 'PREC', 'PCC', 'PHC',
                  'pHPC', 'aHPC',
                  'PRC', 'AMYG', 'FUS', 'ITC', 'TPO', 'OPER', 'TRIA', 'OFC']
        net_ids = [0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
        net_names = ['pm', 'hpc', 'at']
        net_labels = ['PM', 'HPC', 'AT']
    elif roi_group == 'pm-at':
        rois = ['b_hopa_ang', 'b_hopa_prec', 'b_hopa_rscpcc', 'b_phc_jl_mni',
                'b_prc_jl_mni', 'b_hopa_amyg', 'b_hopa_fus', 'b_hopa_itc',
                'b_hopa_tpo', 'b_hopa_pope', 'b_hopa_ptri', 'b_hopa_ofc']
        labels = ['ANG', 'PREC', 'PCC', 'PHC',
                  'PRC', 'AMYG', 'FUS', 'ITC', 'TPO', 'OPER', 'TRIA', 'OFC']
        net_ids = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
        net_names = ['pm', 'at']
        net_labels = ['PM', 'AT']
    else:
        rois = [roi_group]
        labels = [roi_group]
        net_ids = [0]
        net_names = ['roi']
        net_labels = ['ROI']

    nn = [net_names[i] for i in net_ids]
    nl = [net_labels[i] for i in net_ids]
    df = pd.DataFrame({'roi_label': labels, 'net': nn, 'net_label': nl,
                       'net_id': net_ids}, index=rois)
    return df

def load_roi_pattern(roi_dir, subject):
    """Load ROI patterns from exported data set."""
    mat_file = os.path.join(roi_dir, f'pattern_{subject}.txt')
    tab_file = os.path.join(roi_dir, f'pattern_{subject}.csv')
    vols = pd.read_csv(tab_file, index_col=0)
    patterns = np.loadtxt(mat_file)
    return patterns, vols

def load_roi_rdm(roi_dir, subject, item_groups=None, rank=False):
    """Load item group RDMs for an ROI."""
    if item_groups is None:
        item_groups = {'face': slice(None, 60), 'scene': slice(60, None)}

    patterns, vols = load_roi_pattern(roi_dir, subject)
    rdm = {}
    for group, ind in item_groups.items():
        rdv = sd.pdist(patterns[ind, :], 'correlation')
        if rank:
            rdv = stats.rankdata(rdv)
        rdm[group] = sd.squareform(rdv)
    return rdm

def load_roi_corr(roi_dir, rois, subj_ids=None, item_groups=None):
    """Load activity correlations between ROIs."""
    if subj_ids is None:
        subj_ids = task.get_subjects()

    if item_groups is None:
        item_groups = {'face': slice(None, 60), 'scene': slice(60, None)}

    n_roi = len(rois)
    n_subj = len(subj_ids)
    corr = {}
    for group in item_groups.keys():
        corr[group] = np.zeros((n_subj, n_roi, n_roi))

    for i, subj_id in enumerate(subj_ids):
        roi_list = []
        for roi in rois:
            patterns, vols = load_roi_pattern(os.path.join(roi_dir, roi), subj_id)
            roi_list.append(np.mean(patterns, 1)[:, None])
        roi_mat = np.hstack(roi_list)

        for group, ind in item_groups.items():
            x = roi_mat[ind, :].T
            corr_mat = 1 - sd.squareform(sd.pdist(x, 'correlation'))
            corr[group][i, :, :] = corr_mat
    return corr

def load_roi_zstat(res_dir, roi, subj_ids=None):
    """Load z-statistic from permutation test results."""
    if subj_ids is None:
        subj_ids = task.get_subjects()

    z = []
    for subj_id in subj_ids:
        subj_file = os.path.join(res_dir, 'roi', roi, f'zstat_{subj_id}.csv')
        zdf = pd.read_csv(subj_file, index_col=0).T
        zdf.index = [subj_id]
        z.append(zdf)
    df = pd.concat(z)
    return df

def load_net_zstat(rsa_dir, model_set, category, rois, subj_ids=None, suffix=None):
    """Load z-statistics for a model for a set of ROIs."""
    # get the directory with results for this model set and category
    res_dir = os.path.join(rsa_dir, f'prex_{model_set}_{category}')
    if suffix is not None:
        res_dir += suffix
    if not os.path.exists(res_dir):
        raise IOError(f'Results directory not found: {res_dir}')

    # get subjects to load
    if subj_ids is None:
        subj_ids = task.get_subjects()

    # load each ROI
    df_list = []
    for name, roi in rois.iterrows():
        rdf = load_roi_zstat(res_dir, name, subj_ids)
        mdf = pd.DataFrame({'subj_id': rdf.index, 'roi': roi.roi_label,
                            'net': roi.net_label, 'net_id': roi.net_id},
                           index=rdf.index)
        full = pd.concat((mdf, rdf), axis=1)

        df_list.append(full)
    df = pd.concat(df_list, ignore_index=True)
    return df

def load_set_zstat(rsa_dir, model_sets, category, rois, subj_ids=None):
    """Load z-statistics for multiple model sets."""
    df_list = []
    for name, models in model_sets.items():
        dfs = load_net_zstat(rsa_dir, name, category, rois, subj_ids)
        dfs = dfs.rename(columns=models)
        dfs.loc[:, 'set'] = name
        df_list.append(dfs)
    df = pd.concat(df_list, ignore_index=True)
    return df

def resid_rdm(rdm, control_rdms, rank=False):
    """Residualize an RDM after controlling for other RDMs."""
    # get vector forms of the rdms
    if rank:
        rdv = stats.rankdata(sd.squareform(rdm))
        control_rdv = [stats.rankdata(sd.squareform(control_rdm))
                       for control_rdm in control_rdms]
    else:
        rdv = sd.squareform(rdm)
        control_rdv = [sd.squareform(control_rdm)
                       for control_rdm in control_rdms]

    # prepare model
    X = np.vstack((*control_rdv, np.ones(rdv.shape))).T
    y = rdv
    beta, rnorm = optimize.nnls(X, y)
    res = y - X.dot(beta)
    rdm_res = sd.squareform(res)
    return rdm_res

def model_set_prsa(model_set, ref_name, model_names, n_perm=10000):
    """Run partial RSA on a model set."""
    # unpack into reference and other models
    ref_rdm = model_set[ref_name]
    other_set = {name: model_set[name] for name in model_names}

    # create permuted models
    model_rdms = list(other_set.values())
    model_names = list(other_set.keys())
    spec = prsa.init_pRSA(n_perm, model_rdms)

    # run partial correlation analysis
    results = {}
    ref_rdv = stats.rankdata(sd.squareform(ref_rdm))
    for i in range(len(model_rdms)):
        mat = spec['model_mats'][i]
        resid = spec['model_resid'][i]
        stat = prsa.perm_partial(ref_rdv, mat, resid)
        z = prsa.perm_z(stat)
        p = 1 - stats.norm.cdf(z)
        results[model_names[i]] = {'z': z, 'p': p, 'stat': stat[0]}
    return pd.DataFrame(results).T

def sign_perm(mat, n_perm, method='fdr', tail='right'):
    """Test significance and control for family-wise error."""
    # generate random sign flips
    n_samp, n_test = mat.shape
    rand_sign = np.hstack((np.ones((n_samp, 1)),
                           np.random.choice([-1, 1], (n_samp, n_perm))))

    # apply random sign to all conditions
    mat_perm = mat[:, :, None] * rand_sign[:, None, :]
    stat_perm = np.mean(mat_perm, 0)
    if tail == 'both':
        stat_perm = np.abs(stat_perm)
    elif tail == 'left':
        stat_perm = -stat_perm

    # compare each condition to sign flip distribution
    p = np.mean(stat_perm >= stat_perm[:, :1], 1)

    # compare to pooled distribution
    if method == 'fdr':
        p_cor = np.mean(stat_perm.flatten() >= stat_perm[:, :1], 1)
    elif method == 'fdr_bh':
        out = multipletests(p, alpha=0.05, method='fdr_bh')
        p_cor = out[1]
    elif method == 'fwe':
        stat_max = np.max(stat_perm, 0)
        p_cor = np.mean(stat_max[None, :] >= stat_perm[:, :1], 1)
    elif method == 'none':
        p_cor = p.copy()
    else:
        raise ValueError(f'Invalid method: {method}')
    return p, p_cor

def sign_perm_t(mat, n_perm, beta, test='t'):
    """Test for significantly positive values with FDR correction."""
    # generate random sign flips
    n_samp, n_test = mat.shape
    rand_sign = np.hstack(
        (np.ones((n_samp, 1)), np.random.choice([-1, 1], (n_samp, n_perm)))
    )

    # apply random sign to all conditions
    mat_perm = mat[:, :, None] * rand_sign[:, None, :]

    # ttest vs zero for permuted and actual stats
    if test == 't':
        perm_t, perm_p = stats.ttest_1samp(mat_perm, 0, axis=0)
        t, p = stats.ttest_1samp(mat_perm[:, :, 0], 0, axis=0)

        # convert to right-tailed test
        perm_p /= 2
        perm_p[perm_t < 0] = 1 - perm_p[perm_t < 0]
        p /= 2
        p[t < 0] = 1 - p[t < 0]
    else:
        raise ValueError(f'Invalid test type: {test}.')

    # q-value at each actual p-value
    ERstar = np.zeros(p.shape)
    r = np.zeros(p.shape)
    rstarb = np.zeros(p.shape)
    m = n_test
    q = np.zeros(p.shape)
    for i, a in enumerate(p):
        # expected value of number of rejections under the null
        Rstar = np.sum(perm_p <= a, 0)
        ERstar[i] = np.mean(Rstar)

        # 1 - beta quantile of Rstar
        rstarb[i] = np.quantile(Rstar, 1 - beta)

        # number of actual rejections
        r[i] = np.sum(p <= a)

        # FDR local estimator
        if (r[i] - rstarb[i]) >= (a * m):
            q[i] = ERstar[i] / (ERstar[i] + r[i] - (a * m))
        else:
            q[i] = np.mean(Rstar >= 1)

    # package results
    std = np.std(mat, axis=0, ddof=1)
    mean = np.mean(mat, axis=0)
    sem = stats.sem(mat, axis=0)
    df = pd.DataFrame({
        't': t, 'p': p, 'q': q, 'ERstar': ERstar, 'rstarb': rstarb, 'r': r,
        'mean': mean, 'std': std, 'sem': sem, 'd': np.abs(mean) / std
    })
    return df

def roi_zstat_perm(
    df, model, n_perm=100000, method='fdr', beta=0.05, by_network=False
):
    """Test ROI correlations using a permutation test."""
    # shape into matrix format
    rois = df.roi.unique()
    nets = df.net.unique()
    mat = df.pivot(index='subj_id', columns='roi', values=model)
    mat = mat.reindex(columns=rois)

    # run sign flipping test
    if method == 't':
        if by_network:
            # correct within each network
            list_results = []
            for net in nets:
                inc_rois = df.loc[df['net'] == net, 'roi'].unique()
                net_mat = mat.filter(inc_rois)
                net_results = sign_perm_t(
                    net_mat.to_numpy(), n_perm, beta, test=method
                )
                net_results.index = net_mat.columns
                list_results.append(net_results)
            results = pd.concat(list_results, axis=0)
        else:
            # correct across all ROIs
            results = sign_perm_t(mat.to_numpy(), n_perm, beta, test=method)
            results.index = mat.columns
    else:
        p, p_cor = sign_perm(mat.to_numpy(), n_perm, method=method)
        results = pd.DataFrame({'p': p, 'p_cor': p_cor}, index=rois)

        # add stats for mean
        zstat = mat.agg(['mean', 'sem', 'std']).T
        results = pd.concat((zstat, results), axis=1)
        results['d'] = results['mean'].abs() / results['std']
    return results

def net_zstat_perm(df, model, n_perm=100000, method='fdr'):
    """Test ROI correlations separately by network."""
    res_list = []
    for idx, zstat in df.groupby('net'):
        res = roi_zstat_perm(zstat, model, n_perm, method)
        res_list.append(res)
    results = pd.concat(res_list, 0)
    return results

def roi_resid_geo(
    roi_dir, model_dir, control_models, geo_edges, geo_model='geo', subj_ids=None,
    rank=False
):
    """Calculate ROI residuals within geography distance bins."""
    if subj_ids is None:
        subj_ids = task.get_subjects()

    # vector representation of dissimilarity for geo and control models
    model_names = control_models + [geo_model]
    model_rdms = model.load_category_rdms(model_dir, 'scene', model_names)
    control_rdms = [model_rdms[name] for name in control_models]
    geo_rdm = model_rdms[geo_model]
    geo_rdv = sd.squareform(geo_rdm)

    # residualize roi rdms
    resid_list = []
    for subj_id in subj_ids:
        roi_rdm = load_roi_rdm(roi_dir, subj_id)['scene']
        roi_resid = resid_rdm(roi_rdm, control_rdms, rank=rank)

        # z-score dissimilarity within subject
        resid_list.append(stats.zscore(sd.squareform(roi_resid)))

    # calculate neural dissimilarity as a function of distance bin
    means, edges, number = stats.binned_statistic(geo_rdv, resid_list, 'mean',
                                                  bins=geo_edges)
    centers = geo_edges[:-1] + np.diff(geo_edges) / 2

    df = pd.DataFrame(means, index=subj_ids, columns=centers).reset_index()
    df = df.melt(id_vars='index', var_name='distance',
                 value_name='dissimilarity')
    return df

def dependent_corr(xy, xz, yz, n, twotailed=True):
    """
    Test difference between two dependent correlation coefficients.

    Test if x is more correlated with y than with z, given that y and z
    may be correlated themselves.

    See Williams (1959) Journal of the Royal Statistical Society,
    Series B and Steiger (1980) Psychological Bulletin.

    Taken from: https://github.com/psinger/CorrelationStats

    @param xy: correlation coefficient between x and y
    @param xz: correlation coefficient between x and z
    @param yz: correlation coefficient between y and z
    @param n: number of elements in x, y and z
    @param twotailed: whether to calculate a one or two tailed test
    @return: t and p-val
    """
    d = xy - xz
    determ = 1 - xy * xy - xz * xz - yz * yz + 2 * xy * xz * yz
    av = (xy + xz) / 2
    cube = (1 - yz) * (1 - yz) * (1 - yz)

    t2 = d * np.sqrt(
        (n - 1) * (1 + yz) / ((2 * (n - 1) / (n - 3)) * determ + av * av * cube)
    )
    p = 1 - stats.t.cdf(abs(t2), n - 3)
    if twotailed:
        p *= 2
    return t2, p

def compare_rdm_corr(x, y, z):
    """Compare correlation between one RDM and two others."""
    xy = stats.spearmanr(sd.squareform(x), sd.squareform(y))[0]
    xz = stats.spearmanr(sd.squareform(x), sd.squareform(z))[0]
    yz = stats.spearmanr(sd.squareform(y), sd.squareform(z))[0]
    n = len(sd.squareform(x))
    t, p = dependent_corr(xy, xz, yz, n)
    df = n - 3
    return t, p, df


from django.shortcuts import (
    render, redirect, get_object_or_404
)
from django.urls import reverse
from django.conf import settings
from decimal import Decimal
from orders.models import Order
from django.http import (
    HttpRequest, HttpResponseRedirect, HttpResponse
)
import stripe

# Stripe API key
stripe.api_key: str = settings.STRIPE_SK
stripe.api_version: str = settings.STRIPE_API_VERSION

def payment_process(request: HttpRequest) -> HttpResponse | HttpResponseRedirect:
    """Redirect to stripe payment process modal"""

    # Retrieve the order ID set in django session (payment init I.e. "order_create view").
    order: Order = get_object_or_404(Order, id=request.session.get('order_id', None))

    if request.method == 'POST':
        # Callback URLs
        cancel_url: str = request.build_absolute_uri(reverse('payment:pay_canceled'))
        callback_url: str = request.build_absolute_uri(reverse('payment:pay_completed'))

        # Checkout session data.
        metadata: dict = {
            'mode': 'payment',
            'client_reference_id': order.id,
            'success_url': callback_url,
            'cancel_url': cancel_url,
            'line_items': [],
        }
        # Add order items to checkout session.
        for item in order.items.all():
            metadata['line_items'].append({
            'price_data': {
                'unit_amount': int(item.price * Decimal('100')),
                'currency': 'usd',
                'product_data': {
                    'name': item.product.name,
                },
            },
            'quantity': item.quantity,
            })

        # Create checkout session.
        session = stripe.checkout.Session.create(**metadata)
        if session:
            try:
                return redirect(session.url, code=303)
            except:
                raise stripe.max_network_retries
        return render(request, 'payment/pay_process.html', locals())
    return render(request, 'payment/pay_process.html', locals())

def payment_completed(request: HttpRequest) -> HttpResponse:
    return render(request, 'payment/pay_completed.html')

def payment_canceled(request: HttpRequest) -> HttpResponse:
    return render(request, 'payment/pay_canceled.html')


import argparse
import os
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
import scipy.io
import scipy.misc
import numpy as np
import pandas as pd
import PIL
import tensorflow as tf
from keras import backend as K
from keras.layers import Input, Lambda, Conv2D
from keras.models import load_model, Model
from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes
from models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body
import cv2

def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6):

    box_scores = box_confidence*box_class_probs
    box_classes = K.argmax(box_scores,-1)
    box_class_scores = K.max(box_scores,-1)

    filtering_mask = box_class_scores>threshold
    scores = tf.boolean_mask(box_class_scores,filtering_mask)
    boxes = tf.boolean_mask(boxes,filtering_mask)
    classes = tf.boolean_mask(box_classes,filtering_mask)

    return scores, boxes, classes

def yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5):
    """
    Applies Non-max suppression (NMS) to set of boxes
    """

    max_boxes_tensor = K.variable(max_boxes, dtype='int32')     # tensor to be used in tf.image.non_max_suppression()
    K.get_session().run(tf.variables_initializer([max_boxes_tensor])) # initialize variable max_boxes_tensor

    nms_indices = tf.image.non_max_suppression(boxes,scores,max_boxes,iou_threshold)

    scores = K.gather(scores,nms_indices)
    boxes = K.gather(boxes,nms_indices)
    classes = K.gather(classes,nms_indices)

    return scores, boxes, classes

def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):
    """
    Converts the output of YOLO encoding (a lot of boxes) to your predicted boxes along with their scores, box coordinates and classes.

    """
    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs

    boxes = yolo_boxes_to_corners(box_xy, box_wh)

    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = score_threshold)

    boxes = scale_boxes(boxes, image_shape)

    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes, max_boxes, iou_threshold)

    return scores, boxes, classes

def predict(sess, image_file):
    """
    Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions.
    """

    image, image_data = preprocess_image(image_file, model_image_size = (608, 608))

    out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],feed_dict={yolo_model.input:image_data,K.learning_phase(): 0})

    # Generate colors for drawing bounding boxes.
    colors = generate_colors(class_names)

    # Draw bounding boxes on the image file
    draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)

    image = image.convert('RGB')
    image = np.array(image)

    return image

def main():
    sess = K.get_session()
    global yolo_model, class_names, anchors
    global scores, boxes, classes

    class_names = read_classes("model_data/coco_classes.txt")
    anchors = read_anchors("model_data/yolo_anchors.txt")
    image_shape = (720., 1280.)

    yolo_model = load_model("model_data/yolo.h5")
    yolo_model.summary()
    yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
    scores, boxes, classes = yolo_eval(yolo_outputs, image_shape)

    cap = cv2.VideoCapture(0)
    i=0
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
        if not ret:
            break

        height , width , layers =  frame.shape
        new_h=720
        new_w=1280
        frame = cv2.resize(frame, (new_w, new_h))
        i += 1

        # Display the resulting frame
        image = predict(sess, frame)
        cv2.imshow('frame',image)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

main()


from collections import deque

from shoehorn import logging

class TestTarget(object):

    __test__ = False

    #: .. note:: This isn't used during handling of events,
    #            it's only here for testing setting and un-setting
    #            error handlers.
    error_target = None

    def __init__(self, propagate=False):
        self.events = []
        self.propagate = propagate

    def __call__(self, event):
        self.events.append(event)
        if self.propagate:
            return event

class Capture(object):

    def __init__(self, stack):
        self._targets = deque()
        self.events = []
        self.stack = stack

    @property
    def targets(self):
        # don't include our own test target
        return list(self._targets)[:-1]

    def error_target(self, event):
        raise

    def start(self):
        # vars is just a view, so need to take a copy:
        self.existing = dict(vars(self.stack))
        self.stack.targets = self._targets
        self.stack.error_target = self.error_target
        self.stack.error_target_installed = []
        target = TestTarget()
        self.stack.push(target)
        self.events = target.events

    def stop(self):
        targets = []
        while self._targets:
            targets.append(self.stack.pop())
        for attr, value in self.existing.items():
            setattr(self.stack, attr, value)
        # for later inspection
        self._targets = targets

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop()

try:
    import pytest
except ImportError:  # pragma: no cover
    pass
else:
    @pytest.fixture()
    def capture():
        with Capture(logging) as capture:
            yield capture


from django.contrib import messages

from homepage.templatetags.has_group import has_group
from announcements.utils.messages import SuccessMessage, ErrorMessages

def announcement_delete(request, announcements_obj, member_obj, logged_user_id):

    # Get the announcement_id from the request
    announcement_id = request.POST.get('delete_announcement_id')

    # Get the announcement record from db
    this_announcement = announcements_obj.get(id=announcement_id)

    # Check if the user is admin, system_admin or manager
    if has_group(request.user, ['admin', 'system_admin']):

        # Delete the announcement record
        this_announcement.delete()

        # Return success message
        return messages.success(request, SuccessMessage.SUCCESSFUL_DELETION)

    elif has_group(request.user, ['manager']):

        # Get member record from db
        member = member_obj.get(id=logged_user_id)

        # If manager belongs to the portal that tries to delete
        if member.portal_id == this_announcement.portal_id:

            # Delete the record from db
            this_announcement.delete()

            # Return success message
            return messages.success(request, SuccessMessage.SUCCESSFUL_DELETION)
        else:

            # Return error message
            return messages.error(request, ErrorMessages.CANNOT_DELETE_ADMIN_ANNOUNCEMENTS)


import dj_database_url, os
import logging

# TODO(nikolai) straighten this shit out
DEBUG = bool(os.environ.get('ISDEVENV', False))
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Nikolai Mushegian', 'nikolai.mushegian+logging@gmail.com'),
    ('Andrey Kostov', 'kostov.andrey@gmail.com')
)

MANAGERS = ADMINS

# sets up DB config for Heroku
DATABASES = {}
DATABASES['default'] = dj_database_url.config(default='postgres://localhost')

if (os.environ.get('ISDEVENV', True)):
    logger = logging.getLogger(__name__)
    logger.info("Using DEVDB (sqlite)")
    DATABASES['default'] = {
        'ENGINE' : 'django.db.backends.sqlite3',
        'NAME' : 'DEVDB'
    }

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = False

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles/')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

LOGIN_URL = '/signin/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates/css/'),
    os.path.join(PROJECT_ROOT, 'templates/js/'),
    os.path.join(PROJECT_ROOT, 'templates/img/'),
    os.path.join(PROJECT_ROOT, 'templates/fonts/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '##09(!5=cs2nqpez&x)*8k2i#=fwd95wn7zg4fm$$=dx#kxsi4'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'webapp.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'webapp.wsgi.application'

TEMPLATE_DIRS = (
    # this is so that we can use the same relative paths on heroku
    os.path.join(PROJECT_ROOT, 'templates')

    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.db.backends.sqlite3',
    'webapp',
    'gunicorn',
    'django.contrib.admin',
    'django.contrib.admindocs',
)

AUTH_PROFILE_MODULE = "UserProfile"

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        #TODO this does not support multiple processes, figure out how to incorporate ConcurrentLogHandler from PyPi
        'file_info_writer': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'formatter': 'verbose',
            'filename': os.path.join(PROJECT_ROOT, 'log', 'general_info.log')
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        'file_info': {
            'handlers': ['file_info_writer'],
            'level': 'INFO',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
    }
}


"""Utility functions to display the pose detection results."""

from typing import List, Tuple

import cv2
import numpy as np
from numpy import ndarray
import configSim

from object_detector import Detection

_MARGIN = 10  # pixels
_ROW_SIZE = 10  # pixels
_FONT_SIZE = 1
_FONT_THICKNESS = 1
_TEXT_COLOR = (0, 0, 255)  # red

rpx = configSim.RPX
rpy = configSim.RPY
cx = None
cy = None

def visualize(
    image: np.ndarray,
    detections: List[Detection],
    cx=cx,
    cy=cy,
) -> Tuple[ndarray, int, int]:
  """Draws bounding boxes on the input image and return it.

  Args:
    image: The input RGB image.
    detections: The list of all "Detection" entities to be visualize.

  Returns:
    Image with bounding boxes and cx, cy (center of bounding box)
    :param cy:
    :param cx:
  """

  # draw coordinates origin
  cv2.circle(image, (rpx, rpy), 5, (0, 0, 255), -1)
  cv2.circle(image, (rpx, rpy), 15, (0, 0, 255), 2)

  for detection in detections:
    # Draw bounding_box
    start_point = detection.bounding_box.left, detection.bounding_box.top
    end_point = detection.bounding_box.right, detection.bounding_box.bottom
    cv2.rectangle(image, start_point, end_point, _TEXT_COLOR, 3)

    # calculate center of bounding box
    cx = int(start_point[0] + (end_point[0] - start_point[0]) / 2)
    cy = int(start_point[1] + (end_point[1] - start_point[1]) / 2)

    # Draw label and score
    category = detection.categories[0]
    class_name = category.label
    probability = round(category.score, 2)
    result_text = class_name + ' (' + str(probability) + ')'
    text_location = (_MARGIN + detection.bounding_box.left, _MARGIN + _ROW_SIZE + detection.bounding_box.top)
    cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN, _FONT_SIZE, _TEXT_COLOR, _FONT_THICKNESS)
    cv2.circle(image, (cx, cy), 5, (255, 0, 0), -1)
    cv2.line(image, (cx, cy), (rpx, rpy), (255, 255, 0), 2)

  return image, cx, cy


import Game_classes
import config
import telebot
import threading
import time

types = telebot.types
bot = telebot.TeleBot(config.token)

'''
def get_game_from_chat(cid):
    try:
        return Main_classes.existing_games[cid]
    except KeyError:
        return None

def get_game_from_player(cid):
    try:
        return Main_classes.dict_players[cid]
    except KeyError:
        print('Игрок не найден!')
        return None

def send_inventory(player):
    keyboard = types.InlineKeyboardMarkup()
    for p in player.itemlist:
        Aviable = True
        if p.id[0:5] == 'iteme' and player.energy < 2:
            Aviable = False
        if Aviable:
            keyboard.add(types.InlineKeyboardButton(text=p.name, callback_data=str(p.id + str(player.fight.round))))
    keyboard.add(types.InlineKeyboardButton(text='Отмена', callback_data=str('cancel')))
    bot.send_message(player.chat_id, 'Выберите предмет.', reply_markup=keyboard)

def send_skills(player):
    keyboard = types.InlineKeyboardMarkup()
    for p in player.itemlist:
        if not p.standart:
            keyboard.add(types.InlineKeyboardButton(text=p.name, callback_data=str(p.id + str(player.fight.round))))
    keyboard.add(types.InlineKeyboardButton(text='Отмена', callback_data=str('cancel')))
    bot.send_message(player.chat_id, 'Выберите навык.', reply_markup=keyboard) '''

def delete_game(Game):
    for p in Game.pending_players:
        try:
            del Game_classes.players_list[p.chat_id]
        except KeyError:
            pass
    del Game_classes.games_list[Game.cid]
    del Game

# Закончить набор игроков и начать сражение
def start_fight(cid):
    game = Game_classes.games_list[cid]
    game.waitingtimer.cancel()
    game.currentstate = game.gamestate[1]
    game.waitingtimer.cancel()
    t = threading.Thread(target=prepare_fight, args=[game])
    t.daemon = True
    t.start()

def prepare_fight(game):
    # Организация словаря
    game.player_dict = {p.chat_id: p for p in game.players}
    #game.currentstate = 'weapon'
    bot.send_message(game.cid, 'Время убивать!\n' \
                    '\n\n _В битве на выживание нельзя оставаться безучастным. Любой, кто не будет сражаться на твоей стороне - враг, которого ты должен сокрушить. \nСкрипторус Мунификантус_' \
        , parse_mode = 'markdown')
    for p in game.players:
        game.fight.activeplayers.append(p)
        p.team.actors.append(p)
    #какой нибудь раздатчик
    '''
    game.weaponcounter = len(game.players)
    game.waitings = True
    for p in game.players:
        get_weapon(p)
    timer = threading.Timer(90.0, game.change)
    timer.start()
    while game.weaponcounter > 0 and game.waitings is True:
        time.sleep(3)
    if game.weaponcounter == 0:
        bot.send_message(game.cid, 'Оружие выбрано.')
    else:
        for p in game.players:
            if p.weapon is None:
                p.weapon = Weapon_list.weaponlist[random.randint(0, len(Weapon_list.weaponlist) - 1)]
        bot.send_message(game.cid, 'Оружие выбрано или случайно распределено.')
    timer.cancel()
    '''
    game.currentstate = 'fighting'
    # Последняя подготовка
#    for p in game.players:
#        p.fight.string.add('Оружие ' + p.name + ' - ' + p.weapon.name)

    print('Красные - ' + ', '.join([p.name for p in game.team1.players]))
    print('Синие - ' + ', '.join([p.name for p in game.team2.players]))
    #game.fight.string.post(bot, 'Выбор оружия')

    game.startfight()

def fight_loop(game, fight):

    fight.team1 = game.team1
    fight.team2 = game.team2
    fight.team1.leader = game.team1.actors[0]
    fight.team2.leader = game.team2.actors[0]
    fight.actors = fight.aiplayers + fight.activeplayers
    for p in game.players:
        #if p.chat_id == 86190439:
            #p.abilities.append(special_abilities.Isaev)
            #special_abilities.Isaev.aquare(p.abilities, p)
        p.hp = p.maxhp
        p.Alive = True
        p.team.participators.append(p)
    while fight.team1.actors != [] and fight.team2.actors != [] and fight.round != 50:
        fight.string.add('Красные => array of Players(' + ', '.join([p.name for p in game.team1.actors]) + ')')
        fight.string.add('Синие => array of Players(' + ', '.join([p.name for p in game.team2.actors]) + ')')
        get_playerpool(fight)
        send_actions(fight)
        wait_response(fight)
        #manifest_actions(fight)
        #get_results(fight)
        #refresh_turn(fight)
        #kill_players(fight)
        fight.string.post(bot, 'Жалкая попытка одолеть соперника №' + str(fight.round))
    end(fight, game)
    delete_game(game)

def end(fight, game):
    bot.send_message(game.cid, "Игра окончена! Здесь будет проверка условий победы.")

# Собираем пул активных игроков
def get_playerpool(fight):
    fight.round += 1
    fight.fightstate = 'playerpool'
    for p in fight.activeplayers:
        if p.disabled:
            p.turn = 'disabled'
        elif p.alive:
            fight.playerpool.append(p)
        elif 'Zombie' in p.passive:
            if p.zombiecounter > 0:
                fight.playerpool.append(p)

# Рассылаем варианты действий
def send_actions(fight):
    for p in fight.actors:
        account_targets(p)
    for p in fight.playerpool:
        send_action(p, fight)
        print('Послан список действий для ' + p.name)

# Определяем список возможных целей
def account_targets(player):
    if player.team == player.fight.team1:
        player.targets = player.game.team2.actors
    elif player.team == player.game.team2:
        player.targets = player.game.team1.actors

# Описание вариантов действий
def send_action(p, fight):
    keyboard = types.InlineKeyboardMarkup()

    if not p.castready:
        keyboard.add(*[types.InlineKeyboardButton(text=power.name, callback_data=str(power.id) + str(fight.round))
                        for power in Game_classes.powerlist])
        callback_button2 = types.InlineKeyboardButton(text=u'\U0001F52E Каст', callback_data=str('start_magic' + str(fight.round)))
        keyboard.add(callback_button2)
    #else:
        callback_button_target = types.InlineKeyboardButton(text='Цель', callback_data=str('target' + str(fight.round)))
        keyboard.add(callback_button_target)
        callback_button_area = types.InlineKeyboardButton(text='Область', callback_data=str('area' + str(fight.round)))
        keyboard.add(callback_button_area)
        callback_button_selfcast = types.InlineKeyboardButton(text='На себя', callback_data=str('selfcast' + str(fight.round)))
        keyboard.add(callback_button_selfcast)
        callback_button_cancel = types.InlineKeyboardButton(text='Отмена', callback_data=str('cancel' + str(fight.round)))
        keyboard.add(callback_button_cancel)
    p.choicemessage = bot.send_message(p.chat_id, player_turn_info(p).string, reply_markup=keyboard)
    p.info.clear()

def player_turn_info(player):
    player.info.add(str(player.fight.round) + ' попытка')
    player.info.add(str(player.hp) + u'\U00002665' + '/ ' + str(player.maxhp) + ' ')
    return player.info

# Ожидание ответа
def wait_response(fight):
    fight.done = False
    fight.fightstate = 'waiting'
    print('Ждем хода: ')
    for n in fight.playerpool:
        print(n.name)
    timer = threading.Timer(120.0, timerd, [fight])
    timer.start()
    i = 1
    while fight.playerpool and fight.done is False:
        if len(fight.playerpool) == 1 and i == 1:
            i += 1
        time.sleep(5)
    if fight.done:
        for p in fight.playerpool:
            print('Удаляем ход ' + p.name)
            p.turn = 'skip' + str(fight.round)
            bot.edit_message_text(chat_id=p.chat_id, message_id=p.choicemessage.message_id,
                                  text=str(fight.round) + " попытка... ¯\_(ツ)_/¯ ")
        fight.playerpool = []
    timer.cancel()
    del timer

# Переключение счетчика
def timerd(fight):
    fight.done = True


from setuptools import setup, find_packages

with open('README.rst') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='Forwarder App',
    version='0.1.0',
    description='Telegram Forwarder App based on TDLib',
    long_description=readme,
    author='Álvaro Fernández (Alvhix)',
    author_email='alvhix@gmail.com',
    url='https://github.com/Alvhix/ForwarderApp',
    license=license,
    packages=find_packages(exclude=('tests', 'docs'))
)


import logging

import gobject
import gtk

from jarabe.model import shell

_RAISE_DELAY = 250

class TabbingHandler(object):
    def __init__(self, frame, modifier):
        self._frame = frame
        self._tabbing = False
        self._modifier = modifier
        self._timeout = None

    def _start_tabbing(self):
        if not self._tabbing:
            logging.debug('Grabing the input.')

            screen = gtk.gdk.screen_get_default()
            window = screen.get_root_window()
            keyboard_grab_result = gtk.gdk.keyboard_grab(window)
            pointer_grab_result = gtk.gdk.pointer_grab(window)

            self._tabbing = (keyboard_grab_result == gtk.gdk.GRAB_SUCCESS and
                             pointer_grab_result == gtk.gdk.GRAB_SUCCESS)

            # Now test that the modifier is still active to prevent race
            # conditions. We also test if one of the grabs failed.
            mask = window.get_pointer()[2]
            if not self._tabbing or not (mask & self._modifier):
                logging.debug('Releasing grabs again.')

                # ungrab keyboard/pointer if the grab was successfull.
                if keyboard_grab_result == gtk.gdk.GRAB_SUCCESS:
                    gtk.gdk.keyboard_ungrab()
                if pointer_grab_result == gtk.gdk.GRAB_SUCCESS:
                    gtk.gdk.pointer_ungrab()

                self._tabbing = False
            else:
                self._frame.show(self._frame.MODE_NON_INTERACTIVE)

    def __timeout_cb(self, event_time):
        self._activate_current(event_time)
        self._timeout = None
        return False

    def _start_timeout(self, event_time):
        self._cancel_timeout()
        self._timeout = gobject.timeout_add(_RAISE_DELAY,
                lambda: self.__timeout_cb(event_time))

    def _cancel_timeout(self):
        if self._timeout:
            gobject.source_remove(self._timeout)
            self._timeout = None

    def _activate_current(self, event_time):
        home_model = shell.get_model()
        activity = home_model.get_tabbing_activity()
        if activity and activity.get_window():
            activity.get_window().activate(event_time)

    def next_activity(self, event_time):
        if not self._tabbing:
            first_switch = True
            self._start_tabbing()
        else:
            first_switch = False

        if self._tabbing:
            shell_model = shell.get_model()
            zoom_level = shell_model.zoom_level
            zoom_activity = (zoom_level == shell.ShellModel.ZOOM_ACTIVITY)

            if not zoom_activity and first_switch:
                activity = shell_model.get_active_activity()
            else:
                activity = shell_model.get_tabbing_activity()
                activity = shell_model.get_next_activity(current=activity)

            shell_model.set_tabbing_activity(activity)
            self._start_timeout(event_time)
        else:
            self._activate_next_activity(event_time)

    def previous_activity(self, event_time):
        if not self._tabbing:
            first_switch = True
            self._start_tabbing()
        else:
            first_switch = False

        if self._tabbing:
            shell_model = shell.get_model()
            zoom_level = shell_model.zoom_level
            zoom_activity = (zoom_level == shell.ShellModel.ZOOM_ACTIVITY)

            if not zoom_activity and first_switch:
                activity = shell_model.get_active_activity()
            else:
                activity = shell_model.get_tabbing_activity()
                activity = shell_model.get_previous_activity(current=activity)

            shell_model.set_tabbing_activity(activity)
            self._start_timeout(event_time)
        else:
            self._activate_next_activity(event_time)

    def _activate_next_activity(self, event_time):
        next_activity = shell.get_model().get_next_activity()
        if next_activity:
            next_activity.get_window().activate(event_time)

    def stop(self, event_time):
        gtk.gdk.keyboard_ungrab()
        gtk.gdk.pointer_ungrab()
        self._tabbing = False

        self._frame.hide()

        self._cancel_timeout()
        self._activate_current(event_time)

        home_model = shell.get_model()
        home_model.set_tabbing_activity(None)

    def is_tabbing(self):
        return self._tabbing


import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.callbacks import EarlyStopping
from keras_preprocessing.text import Tokenizer
from sklearn.feature_extraction.text import TfidfVectorizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from keras.layers import Input, LSTM, Dense, Embedding

''''''''''''''''''''' Read Preprocessed File '''''''''''''''''''''
csv_read_neutral = 'D:/study/python/UROP/analyzing/analyzed_naverNews.csv'
data = pd.read_csv(csv_read_neutral)

''''''''''''''''''''' Add SOS, EOS Token in Decoder Input, Output '''''''''''''''''''''
data['decoder_input'] = data['title'].apply(lambda x: 'sostoken ' + x)
data['decoder_target'] = data['title'].apply(lambda x: x + ' eostoken')

''''''''''''''''''''' Split Encoder, Decoder '''''''''''''''''''''
encoder_input = np.array(data['contents'])
decoder_input = np.array(data['decoder_input'])
decoder_target = np.array(data['decoder_target'])

''''''''''''''''''''' Make Random Index for Shuffle Data '''''''''''''''''''''
indices = np.arange(encoder_input.shape[0])
np.random.shuffle(indices)

encoder_input = encoder_input[indices]
decoder_input = decoder_input[indices]
decoder_target = decoder_target[indices]

''''''''''''''''''''' Train : Test = 8 : 2 '''''''''''''''''''''
test_val = int(len(encoder_input) * 0.2)

encoder_input_train = encoder_input[:-test_val]
decoder_input_train = decoder_input[:-test_val]
decoder_target_train = decoder_target[:-test_val]

encoder_input_test = encoder_input[-test_val:]
decoder_input_test = decoder_input[-test_val:]
decoder_target_test = decoder_target[-test_val:]

''''''''''''''''''''' Tokenize : Contents '''''''''''''''''''''
contents_tfidf_tokenizer = TfidfVectorizer()  # Contents : TF-IDF, Title : Simple Tokenizer
contents_tfidf_tokenizer.fit(encoder_input_train)
total_cnt = len(contents_tfidf_tokenizer.vocabulary_)

print('=========================================')
print('Analyze Contents Token')
print('Total Word Num:', total_cnt)
print('=========================================')

''''''''''''''''''''' TF-IDF : Contents (Train) '''''''''''''''''''''
encoder_input_train_tfidf = contents_tfidf_tokenizer.transform(encoder_input_train).toarray()

contents_train_word_to_index_tfidf = contents_tfidf_tokenizer.vocabulary_
contents_train_index_to_word_tfidf = dict(map(reversed, contents_train_word_to_index_tfidf.items()))

''''''''''''''''''''' Filtering TF-IDF > 0 and Save with Map Structure : Contents (Train) '''''''''''''''''''''
encoder_input_train_tfidf_list = []
for sentence_tfidf_idx, sentence_tfidf in enumerate(encoder_input_train_tfidf):
    encoder_input_train_tfidf_map = {}
    print('contents train tfidf > 0 :', sentence_tfidf_idx, '/', len(encoder_input_train_tfidf))
    for word_idx, word_tfidf in enumerate(sentence_tfidf):
        if word_tfidf > 0:
            encoder_input_train_tfidf_map[word_idx] = word_tfidf
    encoder_input_train_tfidf_list.append(encoder_input_train_tfidf_map)

# print(encoder_input_train_tfidf_list)
print('contents train filtering tfidf > 0 done')

''''''''''''''''''''' Sort TF-IDF Descending Order, Filtering Rank < 40 : Contents (Train) '''''''''''''''''''''
contents_tfidf_len = 40

contents_train_tfidf_token_list = []
for sentence_idx, encoder_input_train_tfidf_map in enumerate(encoder_input_train_tfidf_list):
    contents_train_tfidf_token = []
    encoder_input_train_tfidf_map = sorted(encoder_input_train_tfidf_map.items(), key=lambda x: x[1], reverse=True)
    print('contents train tfidf sort, rank < %d :' % contents_tfidf_len, sentence_idx, '/', len(encoder_input_train))
    for key, value in encoder_input_train_tfidf_map:
        if len(contents_train_tfidf_token) >= contents_tfidf_len:
            break
        contents_train_tfidf_token.append(contents_train_index_to_word_tfidf[key])
    contents_train_tfidf_token_list.append(contents_train_tfidf_token)

print('contents train filtering rank done')

''''''''''''''''''''' Make New Sentence by Using Tokenizer and Filtered TF-IDF '''''''''''''''''''''
contents_tokenizer = Tokenizer()
contents_tokenizer.fit_on_texts(encoder_input_train)

encoder_input_train = contents_tokenizer.texts_to_sequences(encoder_input_train)
contents_index_to_word = contents_tokenizer.index_word

new_encoder_input_train = []
for sentence_idx, sentence in enumerate(encoder_input_train):
    new_encoder_input_train_word = []
    new_encoder_input_train_sentence = ''

    for word_idx, word in enumerate(sentence):
        if contents_index_to_word[word] in contents_train_tfidf_token_list[sentence_idx] \
                and contents_index_to_word[word] not in new_encoder_input_train_word:
            new_encoder_input_train_word.append(contents_index_to_word[word])
            new_encoder_input_train_sentence += contents_index_to_word[word] + ' '
            print(contents_index_to_word[word], end=' ')

    new_encoder_input_train.append(new_encoder_input_train_sentence)
    print('\n')

print(new_encoder_input_train)

''''''''''''''''''''' TF-IDF : Contents (Test) '''''''''''''''''''''
encoder_input_test_tfidf = contents_tfidf_tokenizer.transform(encoder_input_test).toarray()

contents_test_word_to_index_tfidf = contents_tfidf_tokenizer.vocabulary_
contents_test_index_to_word_tfidf = dict(map(reversed, contents_test_word_to_index_tfidf.items()))

''''''''''''''''''''' Filtering TF-IDF > 0 and Save with Map Structure : Contents (Test) '''''''''''''''''''''
encoder_input_test_tfidf_list = []
for sentence_tfidf_idx, sentence_tfidf in enumerate(encoder_input_test_tfidf):
    encoder_input_test_tfidf_map = {}
    print('contents test tfidf > 0 :', sentence_tfidf_idx, '/', len(encoder_input_test_tfidf))
    for word_idx, word_tfidf in enumerate(sentence_tfidf):
        if word_tfidf > 0:
            encoder_input_test_tfidf_map[word_idx] = word_tfidf
    encoder_input_test_tfidf_list.append(encoder_input_test_tfidf_map)

# print(encoder_input_test_tfidf_list)
print('contents test filtering tfidf > 0 done')

''''''''''''''''''''' Sort TF-IDF Descending Order, Filtering Rank < 40 : Contents (Test) '''''''''''''''''''''
contents_tfidf_len = 40

contents_test_tfidf_token_list = []
for sentence_idx, encoder_input_test_tfidf_map in enumerate(encoder_input_test_tfidf_list):
    contents_test_tfidf_token = []
    encoder_input_test_tfidf_map = sorted(encoder_input_test_tfidf_map.items(), key=lambda x: x[1], reverse=True)
    print('contents test tfidf sort, rank < %d :' % contents_tfidf_len, sentence_idx, '/', len(encoder_input_test))
    for key, value in encoder_input_test_tfidf_map:
        if len(contents_test_tfidf_token) >= contents_tfidf_len:
            break
        contents_test_tfidf_token.append(contents_test_index_to_word_tfidf[key])
    contents_test_tfidf_token_list.append(contents_test_tfidf_token)

print('contents test filtering rank done')

''''''''''''''''''''' Make New Sentence by Using Tokenizer and Filtered TF-IDF '''''''''''''''''''''
contents_tokenizer = Tokenizer()
contents_tokenizer.fit_on_texts(encoder_input_test)

encoder_input_test = contents_tokenizer.texts_to_sequences(encoder_input_test)
contents_index_to_word = contents_tokenizer.index_word

new_encoder_input_test = []
for sentence_idx, sentence in enumerate(encoder_input_test):
    new_encoder_input_test_word = []
    new_encoder_input_test_sentence = ''

    for word_idx, word in enumerate(sentence):
        if contents_index_to_word[word] in contents_test_tfidf_token_list[sentence_idx] \
                and contents_index_to_word[word] not in new_encoder_input_test_word:
            new_encoder_input_test_word.append(contents_index_to_word[word])
            new_encoder_input_test_sentence += contents_index_to_word[word] + ' '
            print(contents_index_to_word[word], end=' ')

    new_encoder_input_test.append(new_encoder_input_test_sentence)
    print('\n')

print(new_encoder_input_test)

''''''''''''''''''''' ReTokenize with New Sentence '''''''''''''''''''''
contents_tokenizer = Tokenizer()
contents_tokenizer.fit_on_texts(new_encoder_input_train)
contents_tokenizer.fit_on_texts(new_encoder_input_test)

new_encoder_input_train = contents_tokenizer.texts_to_sequences(new_encoder_input_train)
new_encoder_input_test = contents_tokenizer.texts_to_sequences(new_encoder_input_test)
contents_vocab = len(contents_tokenizer.word_index) + 1

''''''''''''''''''''' Tokenize : Title '''''''''''''''''''''
title_tokenizer = Tokenizer()
title_tokenizer.fit_on_texts(decoder_input_train)
title_vocab = len(title_tokenizer.word_index) + 1

#
# total_cnt = len(title_tokenizer.vocabulary_)
#
# print('=========================================')
# print('Analyze Title Token')
# print('Total Word Num:', total_cnt)
# print('=========================================')
#
# ''''''''''''''''''''' Re-Tokenize by Checking Contents Word Frequency : Title '''''''''''''''''''''
# title_vocab = 10300
# title_tokenizer = TfidfVectorizer(max_features=title_vocab)
# title_tokenizer.fit(decoder_input_train + decoder_target_train)

''''''''''''''''''''' Text to Sequence : Title '''''''''''''''''''''
decoder_input_train = title_tokenizer.texts_to_sequences(decoder_input_train)
decoder_target_train = title_tokenizer.texts_to_sequences(decoder_target_train)
decoder_input_test = title_tokenizer.texts_to_sequences(decoder_input_test)
decoder_target_test = title_tokenizer.texts_to_sequences(decoder_target_test)

''''''''''''''''''''' Pad Sequences '''''''''''''''''''''
contents_pad_len = contents_tfidf_len
title_pad_len = 10

encoder_input_train = pad_sequences(new_encoder_input_train, maxlen=contents_pad_len)
decoder_input_train = pad_sequences(decoder_input_train, maxlen=title_pad_len)
decoder_target_train = pad_sequences(decoder_target_train, maxlen=title_pad_len)

encoder_input_test = pad_sequences(new_encoder_input_test, maxlen=contents_pad_len)
decoder_input_test = pad_sequences(decoder_input_test, maxlen=title_pad_len)
decoder_target_test = pad_sequences(decoder_target_test, maxlen=title_pad_len)

print(new_encoder_input_train)
print(new_encoder_input_test)

''''''''''''''''''''' Build Model '''''''''''''''''''''
embedding_dim_list = [32, 64, 128, 256]
hidden_size_list = [32, 64, 128, 256]

def model_encoder_1_decoder_1():
    ''''''''''''''''''''' Encoder : LSTM X 1 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm1(enc_emb)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 1 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm(dec_emb, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e1d1 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e1d1

def model_encoder_1_decoder_3():
    ''''''''''''''''''''' Encoder : LSTM X 1 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm1(enc_emb)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 3 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output1, _, _ = decoder_lstm1(dec_emb, initial_state=[state_h, state_c])

    decoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output2, _, _ = decoder_lstm2(decoder_output1, initial_state=[state_h, state_c])

    decoder_lstm3 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm3(decoder_output2, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e1d3 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e1d3

def model_encoder_3_decoder_1():
    ''''''''''''''''''''' Encoder : LSTM X 3 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output1, state_h1, state_c1 = encoder_lstm1(enc_emb)

    encoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output2, state_h2, state_c2 = encoder_lstm2(encoder_output1)

    encoder_lstm3 = LSTM(hidden_size, return_state=True, return_sequences=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm3(encoder_output2)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 1 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm(dec_emb, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e3d1 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e3d1

def model_encoder_3_decoder_3():
    ''''''''''''''''''''' Encoder : LSTM X 3 '''''''''''''''''''''
    encoder_inputs = Input(shape=(contents_pad_len,))
    enc_emb = Embedding(contents_vocab, embedding_dim)(encoder_inputs)

    encoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output1, state_h1, state_c1 = encoder_lstm1(enc_emb)

    encoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    encoder_output2, state_h2, state_c2 = encoder_lstm2(encoder_output1)

    encoder_lstm3 = LSTM(hidden_size, return_state=True, return_sequences=True, dropout=0.4)
    encoder_outputs, state_h, state_c = encoder_lstm3(encoder_output2)

    decoder_inputs = Input(shape=(None,))

    ''''''''''''''''''''' Decoder : LSTM X 3 '''''''''''''''''''''
    dec_emb_layer = Embedding(title_vocab, embedding_dim)
    dec_emb = dec_emb_layer(decoder_inputs)

    decoder_lstm1 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output1, _, _ = decoder_lstm1(dec_emb, initial_state=[state_h, state_c])

    decoder_lstm2 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_output2, _, _ = decoder_lstm2(decoder_output1, initial_state=[state_h, state_c])

    decoder_lstm3 = LSTM(hidden_size, return_sequences=True, return_state=True, dropout=0.4)
    decoder_outputs, _, _ = decoder_lstm3(decoder_output2, initial_state=[state_h, state_c])

    decoder_softmax_layer = Dense(title_vocab, activation='softmax')
    decoder_softmax_outputs = decoder_softmax_layer(decoder_outputs)

    ''''''''''''''''''''' Encoder + Decoder Model '''''''''''''''''''''
    model = Model([encoder_inputs, decoder_inputs], decoder_softmax_outputs)
    model.summary()

    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')

    early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)

    history_e3d3 = model.fit(x=[encoder_input_train, decoder_input_train], y=decoder_target_train,
                             validation_data=([encoder_input_test, decoder_input_test], decoder_target_test),
                             batch_size=256, callbacks=[early_stopping_callback], epochs=1000)

    return history_e3d3

if __name__ == "__main__":
    info_list = []
    loss_e1d1_list = []
    loss_e1d3_list = []
    loss_e3d1_list = []
    loss_e3d3_list = []
    DROPOUT = 0.5

    plot_dir = 'D:/study/python/UROP/model/neutral/plot/plot_TFIDF_LSTM_dropout%d' % (DROPOUT * 100)
    if not os.path.exists(plot_dir):
        os.makedirs(plot_dir)

    for embedding_dim in embedding_dim_list:
        for hidden_size in hidden_size_list:
            info_list.append('emb: ' + str(embedding_dim) + ', hidden: ' + str(hidden_size))

            print('\n=========   e1 d1 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e1d1 = model_encoder_1_decoder_1()
            loss_e1d1_list.append(history_e1d1.history['val_loss'])
            print('==============   e1 d1 End    ================\n')

            print('\n=========   e1 d3 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e1d3 = model_encoder_1_decoder_3()
            loss_e1d3_list.append(history_e1d3.history['val_loss'])
            print('==============   e1 d3 End    ================\n')

            print('\n=========   e3 d1 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e3d1 = model_encoder_3_decoder_1()
            loss_e3d1_list.append(history_e3d1.history['val_loss'])
            print('==============   e3 d1 End    ================\n')

            print('\n=========   e3 d3 Start, Emb: %d Hid: %d    ===========' % (embedding_dim, hidden_size))
            history_e3d3 = model_encoder_3_decoder_3()
            loss_e3d3_list.append(history_e3d3.history['val_loss'])
            print('==============   e3 d3 End    ================\n')

            plt.figure()
            plt.plot(history_e1d1.history['val_loss'], label='test_e1d1')
            plt.plot(history_e1d3.history['val_loss'], label='test_e1d3')
            plt.plot(history_e3d1.history['val_loss'], label='test_e3d1')
            plt.plot(history_e3d3.history['val_loss'], label='test_e3d3')

            plt.ylim([0, 4])
            plt.legend()
            plt.title('Loss Graph (Embedding Dim: %d, Hidden Size: %d)' % (embedding_dim, hidden_size))
            plt.savefig(plot_dir + '/emb%d_hid%d.png' % (embedding_dim, hidden_size))

        loss_dataframe = []
        loss_dataframe = pd.DataFrame(loss_dataframe, columns=['info', 'e1d1', 'e1d3', 'e3d1', 'e3d3'])
        loss_dataframe['info'] = info_list
        loss_dataframe['e1d1'] = loss_e1d1_list
        loss_dataframe['e1d3'] = loss_e1d3_list
        loss_dataframe['e3d1'] = loss_e3d1_list
        loss_dataframe['e3d3'] = loss_e3d3_list
        loss_dataframe.to_csv('D:/study/python/UROP/model/neutral/loss/loss_TFIDF_LSTM_dropout%d.csv' % (DROPOUT * 100),
                              encoding='utf-8-sig', index=True)


"""
The :py:mod:`vc2_conformance.decoder.assertions` module contains functions
which perform checks which are used in multiple places within a bitstream and
have been factored-out to avoid repetition.
"""

from collections import OrderedDict

from vc2_data_tables import (
    ParseCodes,
    PictureCodingModes,
)

from vc2_conformance.level_constraints import LEVEL_CONSTRAINTS

from vc2_conformance.version_constraints import MINIMUM_MAJOR_VERSION

from vc2_conformance.symbol_re import WILDCARD, END_OF_SEQUENCE

from vc2_conformance.constraint_table import allowed_values_for

from vc2_conformance.decoder.exceptions import (
    ValueNotAllowedInLevel,
    NonConsecutivePictureNumbers,
    EarliestFieldHasOddPictureNumber,
    MajorVersionTooHigh,
)

def assert_in(value, collection, exception_type, *args):
    """
    Check to see if a value is a member of the provided collection using
    ``in``. If it is not, throws an exception of the given type with the value
    and collection passed as as arguments.
    """
    if value not in collection:
        raise exception_type(value, collection, *args)

def assert_in_enum(value, enum, exception_type):
    """
    Check to see if a value is a member of the provided assertion type. If it
    is not, throws an exception of the given type with the value as argument.
    """
    try:
        enum(value)
    except ValueError:
        raise exception_type(value)

def assert_parse_code_in_sequence(parse_code, matcher, exception_type, *args):
    """
    Check that the specified parse code's name matches the next value in the
    sequence defined by :py:class:`vc2_conformance.symbol_re.Matcher`.

    If it does not, ``exception_type`` will be raised with the following
    arguments:

    * The ``parse_code`` (as a :py:class:`~vc2_data_tables.ParseCodes`)
    * A list of parse codes (as a
      :py:class:`~vc2_data_tables.ParseCodes`) which would have been
      valid (or ``None`` if any parse code would be allowed.
    * A boolean which is True if it would have been valid to end the sequence
      at this point.
    * Any additional arguments passed to this function
    """
    parse_code = ParseCodes(parse_code)

    if not matcher.match_symbol(parse_code.name):
        expected_parse_codes = []
        expected_end = False
        for parse_code_name in matcher.valid_next_symbols():
            if parse_code_name == WILDCARD:
                expected_parse_codes = None
                break
            elif parse_code_name == END_OF_SEQUENCE:
                expected_end = True
            elif expected_parse_codes is not None:
                expected_parse_codes.append(getattr(ParseCodes, parse_code_name))

        raise exception_type(parse_code, expected_parse_codes, expected_end, *args)

def assert_parse_code_sequence_ended(matcher, exception_type, *args):
    """
    Check that the specified :py:class:`vc2_conformance.symbol_re.Matcher` has
    reached a valid end for the sequence of parse codes specified.

    If the matcher still expects more parse codes, ``exception_type`` will be
    raised with the following arguments:

    * The ``parse_code`` will be None
    * A list of parse codes (as a
      :py:class:`~vc2_data_tables.ParseCodes`) which would have been
      valid (or ``None`` if any parse code would be allowed.
    * A boolean which is True if it would have been valid to end the sequence
      at this point. (Always False, in this case)
    * Any additional arguments passed to this function
    """
    if not matcher.is_complete():
        expected_parse_codes = []
        for parse_code_name in matcher.valid_next_symbols():
            if parse_code_name == WILDCARD:
                expected_parse_codes = None
                break
            elif expected_parse_codes is not None:
                expected_parse_codes.append(getattr(ParseCodes, parse_code_name))

        raise exception_type(None, expected_parse_codes, False, *args)

def assert_level_constraint(state, key, value):
    """
    Check that the given key and value is allowed according to the
    :py:data:`vc2_conformance.level_constraints.LEVEL_CONSTRAINTS`. Throws a
    :py;exc:`vc2_conformance.decoder.exceptions.ValueNotAllowedInLevel`
    exception on failure.

    Takes the current :py:class:`~vc2_conformance.pseudocode.state.State` instance from
    which the current
    :py:attr:`~vc2_conformance.pseudocode.state.State._level_constrained_values` will be
    created/updated.
    """
    state.setdefault("_level_constrained_values", OrderedDict())

    allowed_values = allowed_values_for(
        LEVEL_CONSTRAINTS,
        key,
        state["_level_constrained_values"],
    )

    if value not in allowed_values:
        raise ValueNotAllowedInLevel(
            state["_level_constrained_values"], key, value, allowed_values
        )
    else:
        state["_level_constrained_values"][key] = value

def assert_picture_number_incremented_as_expected(state, picture_number_offset):
    """
    Check that ``state["picture_number"]`` has been set to an appropriate value
    by (12.2) picture_header or (14.2) fragment_header.  It should be called
    after the picture number has been read from the stream into
    ``state["picture_number"]`` and with the
    :py:func:`~vc2_conformance.decoder.io.tell` value just after the value was
    read.

    This assertion will check that:

    * (12.2), (14.2) Picture numbers in a sequence must increment by 1 and wrap after
      2^32-1 back to zero, throwing :py:exc:`NonConsecutivePictureNumbers` if
      this is not the case.
    * (12.2), (14.2) When coded as fields, the first field in the sequence must
      have an even picture number, throwing
      :py:exc:`EarliestFieldHasOddPictureNumber` if this is not the case.

    This assertion also has the following side-effects:

    * Sets ``state["_last_picture_number"]`` to ``state["picture_number"]``
    * Sets ``state["_last_picture_number_offset"]`` to the value passed in the
      picture_number_offset argument.
    * Increments ``state["_num_pictures_in_sequence"]``

    In the case of fragments (14.2), this assertion should be called only for
    the first fragment in a picture (with fragment_slice_count==0).
    """
    # (12.2), (14.4) Picture numbers in a sequence must increment by 1 and wrap
    # after 2^32-1 back to zero
    if "_last_picture_number" in state:
        expected_picture_number = (state["_last_picture_number"] + 1) & 0xFFFFFFFF
        if state["picture_number"] != expected_picture_number:
            raise NonConsecutivePictureNumbers(
                state["_last_picture_number_offset"],
                state["_last_picture_number"],
                picture_number_offset,
                state["picture_number"],
            )
    state["_last_picture_number"] = state["picture_number"]
    state["_last_picture_number_offset"] = picture_number_offset

    # (12.2), (14.4) When coded as fields, the first field in the sequence must
    # have an even picture number.
    if state["picture_coding_mode"] == PictureCodingModes.pictures_are_fields:
        early_field = state["_num_pictures_in_sequence"] % 2 == 0
        even_number = state["picture_number"] % 2 == 0
        if early_field and not even_number:
            raise EarliestFieldHasOddPictureNumber(state["picture_number"])
    state["_num_pictures_in_sequence"] += 1

def log_version_lower_bound(state, minimum_major_version):
    """
    Records an instance where a value in the stream requires that
    major_version have a certain value. These will later be checked by
    :py:func:`assert_major_version_is_minimal`. Bounds are logged in
    ``state["_expected_major_version"]``

    Parameters
    ==========
    state : :py:class:`~vc2_conformance.pseudocode.state.State`
    minimum_major_version : int
        The minimum major version number allowed to support the feature being
        logged.
    """
    state["_expected_major_version"] = max(
        state.get("_expected_major_version", MINIMUM_MAJOR_VERSION),
        minimum_major_version,
    )

def assert_major_version_is_minimal(state):
    """
    Checks whether the major_version number supplied is the lowest possible
    major_version number for this stream (as required by (11.2.2)). Should be
    called once at the end of a stream.
    """
    major_version = state["major_version"]
    expected_major_version = state.get("_expected_major_version", MINIMUM_MAJOR_VERSION)

    # Errata: The following special case was missing from the spec
    #
    # (11.2.2) Special case for empty sequences of fragmented pictures: When an
    # encoder is configured to produce fragmented pictures in streams the
    # major_version needs to be set to '3' to allow fragmented picture data
    # units. In the case of an empty sequence, major_version may not have to be
    # '3' since no data units with a fragment parse code will be present. To
    # make the life of encoders developers easier in this special case, the
    # spec permits them to set major_version to '3' even when no fragmented
    # picture data units appear in the stream enabling them to use the same
    # sequence header for all sequences, regardless of length.
    if state["_num_pictures_in_sequence"] == 0 and major_version == 3:
        return

    if major_version > expected_major_version:
        raise MajorVersionTooHigh(major_version, expected_major_version)


from zope import interface
from zope.traversing.browser import absoluteURL
from zojax.ownership.interfaces import IOwnership
from zojax.principal.profile.interfaces import IPersonalProfile

class ContentOwner(object):

    def update(self):
        context = self.context
        request = self.request

        principal = None

        ownership = IOwnership(context, None)
        if ownership is not None:
            principal = ownership.owner

        profile = IPersonalProfile(principal, None)
        if profile is not None:
            self.owner = profile.title

            space = profile.space
            if space is not None:
                self.profile = '%s/'%absoluteURL(space, request)

    def isAvailable(self):
        return True


import numpy as np
import os, sys
import scipy.ndimage as ndi
import matplotlib.pyplot as plt
from scipy.cluster.vq import kmeans, vq
no_dementia_base_path = sys.path[0]+os.sep+'Non_dementia'
dementia_base_path = sys.path[0]+os.sep+'dementia'

base_path = dementia_base_path
for id in range(1,13):
    os.mkdir(base_path+os.sep+str(id))
    #f, axes = plt.subplots(nrows=2, ncols=1, figsize=(15, 10))
    sarraster = plt.imread(base_path+os.sep+'subj'+os.sep+str(id)+'.gif')
    # Removing speckles
    sarraster = ndi.median_filter(sarraster , size=2)
    # Flatten image to get line of values
    flatsarraster = sarraster.flatten().astype(float)

    print(flatsarraster.shape)

    # In remaining subplots add k-means classified images
    for i in range(2,6):
        #This scipy code classifies k-mean, code has same length as flattened
        #SAR raster and defines which class the SAR value corresponds to
        centroids, variance = kmeans(flatsarraster, i)
        code, distance = vq(flatsarraster, centroids)
        fig = plt.figure()
        fig.suptitle('K-Means Classification')

        # In first subplot add original SAR image
        ax = plt.subplot(241)
        plt.axis('off')
        ax.set_title('Original Image')
        plt.imshow(sarraster, cmap = 'gray')
        print(sarraster.shape)
        #Since code contains the classified values, reshape into SAR dimensions
        codeim = code.reshape(sarraster.shape[0], -1)
        print(codeim.shape)
        #codeim = ndi.median_filter(codeim , size=4)
        for j in range(i):
            #Plot the subplot with (i+2)th k-means
            ax = plt.subplot(2,4,j+2)
            c = codeim == j
            c = ndi.median_filter(c , size=10)
            plt.axis('off')
            xlabel = str(j+1) , ' clusters'
            ax.set_title(xlabel)
            plt.imsave(base_path+os.sep+str(id)+'/gradient'+str(i)+str(j)+'.png', c, cmap='gray')

    #plt.show()


import numpy as np
from argparse import Namespace

from utils.get_arguments import get_params
from utils.infomation_exporter import InformationExporter
import benchmarks
import algorithms
from algorithms import Algorithm
from benchmarks import MultiModalProblem

def get_benchmark(benchmark_name: str, n_var: int)-> MultiModalProblem:
    return getattr(benchmarks, benchmark_name)(n_var)

def get_algorithm(algorithm_name: str, benchmark: MultiModalProblem, n_init:int, n_iter: int, n_batch: int) -> Algorithm:
    return getattr(algorithms, algorithm_name)(benchmark, n_init, n_iter, n_batch)

def run_experiment(parameters: Namespace) -> None:
    """The actual experiment code."""
    for seed in parameters.seeds:
        for benchmark_name in parameters.problems:
            for algorithm_name in parameters.algorithms:
                for n_var in parameters.n_vars:
                    parameters.n_var = n_var
                    parameters.algorithm = algorithm_name
                    parameters.problem = benchmark_name
                    parameters.seed = seed
                    np.random.seed(parameters.seed)
                    n_init, n_iter, n_batch = parameters.n_init, parameters.n_iter, parameters.n_batch

                    exporter = InformationExporter(parameters)
                    benchmark = get_benchmark(benchmark_name, n_var)

                    algorithm = get_algorithm(algorithm_name,benchmark, n_init, n_iter, n_batch)

                    X, Y = algorithm.run()

                    xfmt = '%lf' + '\t%lf' * (parameters.n_var - 1)
                    yfmt = '%lf'
                    np.savetxt(exporter.folder_name['data'] + 'X.txt', X, xfmt)
                    np.savetxt(exporter.folder_name['data'] + 'Y.txt', Y, yfmt)

if __name__ == "__main__":
    params = get_params()
    run_experiment(params)


import os
import sys
import numpy as np
import pickle
import torch
from random import randint, shuffle
from torch.utils.data import DataLoader, Dataset

def selected_frame(data, num_frame):
    """This function uniformly samples data to num_frame frames.
    Not suitable for online recognition model
    """
    frame, joint, dim = data.shape
    if frame == num_frame:
        return data
    interval = frame / num_frame
    uniform_list = [int(i * interval) for i in range(num_frame)]
    return data[uniform_list, :]

def data_aug(data, cls, state):
    def scale(dt):
        for point in range(dt.shape[0]):
            ratio = 0.1
            factor = np.random.uniform(1 - ratio, 1 + ratio)
            dt[point][:][:3] *= factor  # scale each joint
        return dt

    def noise(dt):
        ratio = 0.1
        # select 4 joints
        all_joint = list(range(1, dt.shape[2]))
        shuffle(all_joint)
        selected_joint = all_joint[:5]  # 5 noisy joints

        for point in range(dt.shape[0]):
            for j_id in selected_joint:
                # noise_offset = np.random.uniform(low, high, 7)
                factor = np.random.uniform(1 - ratio, 1 + ratio)
                for t in range(dt.shape[1]):
                    dt[point][t][j_id] *= factor
        return dt

    skeleton_aug = data[:]
    cls_aug = cls[:]
    state_aug = state[:]

    x = np.random.randint(5)
    # skeleton_aug = np.append(skeleton_aug, noise(data[x::5]), axis=0)
    skeleton_aug = np.append(skeleton_aug, scale(data[x::5]), axis=0)
    cls_aug = np.append(cls_aug, cls[x::5], axis=0)
    state_aug = np.append(state_aug, state[x::5], axis=0)

    return skeleton_aug, cls_aug, state_aug
    # return noise(data), cls, state

class SkeletonData(Dataset):
    def __init__(self, data, cls_labels, state_labels, mode="train", use_data_aug=False):
        if use_data_aug and mode == "train":
            data, cls_labels, state_labels = data_aug(data, cls_labels, state_labels)
            self.data = torch.tensor(data, dtype=torch.float)
            self.cls_labels = torch.tensor(cls_labels, dtype=torch.float)
            self.state_labels = torch.tensor(state_labels, dtype=torch.float)
            self.mode = mode
        else:
            self.data = torch.tensor(data, dtype=torch.float)
            self.cls_labels = torch.tensor(cls_labels, dtype=torch.float)
            self.state_labels = torch.tensor(state_labels, dtype=torch.float)
            self.mode = mode

    def __len__(self):
        return len(self.cls_labels)

    def __getitem__(self, item):
        skeleton_data = self.data[item]
        cls_label = int(self.cls_labels[item])
        stt_label = self.state_labels[item]

        return skeleton_data, cls_label, stt_label


from PIL import Image
import pytesseract
from pdf2image import convert_from_path

# PDF_file = "/home/shiva/Sirius/PDF/Division orders/19049.pdf"
# pages = convert_from_path(PDF_file, 500)
# image_counter = 1
# for page in pages:
# 	filename = "page_"+str(image_counter)+".jpg"
# 	page.save(filename, 'JPEG')
# 	image_counter = image_counter + 1
# filelimit = image_counter-1
# outfile = "/home/shiva/Sirius/Text/Division orders/19049.txt"
# f = open(outfile, "a")
# for i in range(1, filelimit + 1):
# 	filename = "page_"+str(i)+".jpg"
# 	text = str(((pytesseract.image_to_string(Image.open(filename)))))
# 	text = text.replace('-\n', '')
# 	f.write(text)
# f.close()

import os,glob

def convertMultiple(pdfDir, txtDir):
    if pdfDir == "": pdfDir = os.getcwd() + "\\"
    for pdf in os.listdir(pdfDir):
        fileExtension = pdf.split(".")[-1]
        if fileExtension == "pdf":
            pdfFilename = pdfDir + pdf
            text = convert_from_path(pdfFilename)
            image_counter = 1
            for page in text:
                filename = "page_"+str(image_counter) + ".jpg"
                page.save(filename,'JPEG')
                image_counter = image_counter + 1
            filelimit = image_counter - 1
            for i in range(1, filelimit +1):
                filename = "page_"+str(i)+".jpg"
                text = str(((pytesseract.image_to_string(Image.open(filename)))))
                text = text.replace('-\n','')
            textFilename = txtDir + pdf[-9:-4] + ".txt"
            textFile = open(textFilename, "w") #make text file
            textFile.write(str(text)) #write text to text file

pdfDir = "/home/shiva/Sirius/PDF/Division orders/"
txtDir = "/home/shiva/Sirius/Text/Division orders/"
str(convertMultiple(pdfDir, txtDir))


import collections
import logging
import regex
import string
import unicodedata
from functools import partial
from multiprocessing import Pool as ProcessPool
from typing import Tuple, List, Dict
import numpy as np

"""
Evaluation code from DPR: https://github.com/facebookresearch/DPR
"""

class SimpleTokenizer(object):
    ALPHA_NUM = r'[\p{L}\p{N}\p{M}]+'
    NON_WS = r'[^\p{Z}\p{C}]'

    def __init__(self):
        """
        Args:
            annotators: None or empty set (only tokenizes).
        """
        self._regexp = regex.compile(
            '(%s)|(%s)' % (self.ALPHA_NUM, self.NON_WS),
            flags=regex.IGNORECASE + regex.UNICODE + regex.MULTILINE
        )

    def tokenize(self, text, uncased=False):
        matches = [m for m in self._regexp.finditer(text)]
        if uncased:
            tokens = [m.group().lower() for m in matches]
        else:
            tokens = [m.group() for m in matches]
        return tokens

logger = logging.getLogger(__name__)

QAMatchStats = collections.namedtuple('QAMatchStats', ['top_k_hits', 'questions_doc_hits'])

def calculate_matches(data: List, workers_num: int):
    """
    Evaluates answers presence in the set of documents. This function is supposed to be used with a large collection of
    documents and results. It internally forks multiple sub-processes for evaluation and then merges results
    :param all_docs: dictionary of the entire documents database. doc_id -> (doc_text, title)
    :param answers: list of answers's list. One list per question
    :param closest_docs: document ids of the top results along with their scores
    :param workers_num: amount of parallel threads to process data
    :param match_type: type of answer matching. Refer to has_answer code for available options
    :return: matching information tuple.
    top_k_hits - a list where the index is the amount of top documents retrieved and the value is the total amount of
    valid matches across an entire dataset.
    questions_doc_hits - more detailed info with answer matches for every question and every retrieved document
    """

    logger.info('Matching answers in top docs...')

    tokenizer = SimpleTokenizer()
    get_score_partial = partial(check_answer, tokenizer=tokenizer)

    processes = ProcessPool(processes=workers_num)
    scores = processes.map(get_score_partial, data)

    logger.info('Per question validation results len=%d', len(scores))

    n_docs = len(data[0]['ctxs'])
    top_k_hits = [0] * n_docs
    for question_hits in scores:
        best_hit = next((i for i, x in enumerate(question_hits) if x), None)
        if best_hit is not None:
            top_k_hits[best_hit:] = [v + 1 for v in top_k_hits[best_hit:]]

    return QAMatchStats(top_k_hits, scores)

def check_answer(example, tokenizer) -> List[bool]:
    """Search through all the top docs to see if they have any of the answers."""
    answers = example['answers']
    ctxs = example['ctxs']

    hits = []

    for i, doc in enumerate(ctxs):
        text = doc['text']

        if text is None:  # cannot find the document for some reason
            logger.warning("no doc in db")
            hits.append(False)
            continue

        hits.append(has_answer(answers, text, tokenizer))

    return hits

def has_answer(answers, text, tokenizer) -> bool:
    """Check if a document contains an answer string."""
    text = _normalize(text)
    text = tokenizer.tokenize(text, uncased=True)

    for answer in answers:
        answer = _normalize(answer)
        answer = tokenizer.tokenize(answer, uncased=True)
        for i in range(0, len(text) - len(answer) + 1):
            if answer == text[i: i + len(answer)]:
                return True
    return False

#################################################
########        READER EVALUATION        ########
#################################################

def _normalize(text):
    return unicodedata.normalize('NFD', text)

#Normalization and score functions from SQuAD evaluation script https://worksheets.codalab.org/rest/bundles/0x6b567e1cf2e041ec80d7098f031c5c9e/contents/blob/
def normalize_answer(s):
    def remove_articles(text):
        return regex.sub(r'\b(a|an|the)\b', ' ', text)

    def white_space_fix(text):
        return ' '.join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return ''.join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))

def em(prediction, ground_truth):
    return normalize_answer(prediction) == normalize_answer(ground_truth)

def f1(prediction, ground_truth):
    prediction_tokens = normalize_answer(prediction).split()
    ground_truth_tokens = normalize_answer(ground_truth).split()
    common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
    num_same = sum(common.values())
    if num_same == 0:
        return 0
    precision = 1.0 * num_same / len(prediction_tokens)
    recall = 1.0 * num_same / len(ground_truth_tokens)
    f1 = (2 * precision * recall) / (precision + recall)
    return f1

def f1_score(prediction, ground_truths):
    return max([f1(prediction, gt) for gt in ground_truths])

def exact_match_score(prediction, ground_truths):
    return max([em(prediction, gt) for gt in ground_truths])

####################################################
########        RETRIEVER EVALUATION        ########
####################################################

def eval_batch(scores, inversions, avg_topk, idx_topk):
    for k, s in enumerate(scores):
        s = s.cpu().numpy()
        sorted_idx = np.argsort(-s)
        score(sorted_idx, inversions, avg_topk, idx_topk)

def count_inversions(arr):
    inv_count = 0
    lenarr = len(arr)
    for i in range(lenarr):
        for j in range(i + 1, lenarr):
            if (arr[i] > arr[j]):
                inv_count += 1
    return inv_count

def score(x, inversions, avg_topk, idx_topk):
    x = np.array(x)
    inversions.append(count_inversions(x))
    for k in avg_topk:
        # ratio of passages in the predicted top-k that are
        # also in the topk given by gold score
        avg_pred_topk = (x[:k]<k).mean()
        avg_topk[k].append(avg_pred_topk)
    for k in idx_topk:
        below_k = (x<k)
        # number of passages required to obtain all passages from gold top-k
        idx_gold_topk = len(x) - np.argmax(below_k[::-1])
        idx_topk[k].append(idx_gold_topk)


"""This program seeks to compare the prediction results of two or more machine learning models,
when they are trained and used on the same dataset. It is the first step in guiding the choice of
an optimal model for the dataset of interest. """

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

def class_probs(labels, estimators, data_test):

    preds = []
    prob0s = []
    prob1s = []
    for clf in estimators:
        pred = clf.predict_proba(data_test)
        preds.append(pred)

        prob0 = np.mean(pred[:, 0])
        prob0s.append(prob0)

        prob1 = np.mean(pred[:, 1])
        prob1s.append(prob1)

    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars

    fig, ax = plt.subplots(figsize=(6, 7))
    rects1 = ax.bar(x - width / 2, prob0s, width, label="Class0")
    rects2 = ax.bar(x + width / 2, prob1s, width, label="Class1")

    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel("Mean probability")
    ax.set_title("Probability by estimator and class")
    ax.set_xticks(x)
    ax.set_xticklabels(labels)
    ax.legend()

    def autolabel(rects):
        """Attach a text label above each bar in *rects*, displaying its height."""
        for rect in rects:
            height = rect.get_height()
            height = round(height, 2)
            ax.annotate(
                "{}".format(height),
                xy=(rect.get_x() + rect.get_width() / 2, height),
                xytext=(0, 3),  # 3 points vertical offset
                textcoords="offset points",
                ha="center",
                va="bottom",
            )

    autolabel(rects1)
    autolabel(rects2)

    fig.tight_layout()

    plt.show()


# # # # # # # # # # # # # # #
#                           #
#   BPC-ZKR  Assignment 06  #
#   Author: Hung Ngo Quang  #
#                           #
# # # # # # # # # # # # # # #

from math import gcd
from sympy import isprime

def pollard_algorithm(n):
    """
    Utility function that generates prime factors of a given number n using a Pollard p-1 algorithm.
    """
    base = 2
    i = 2

    while True:
        base = (base ** i) % n
        d = gcd((base-1), n)

        if d > 1:
            return d

        i += 1

def find_prime_factors(n):
    """
    Utility function that finds both of the prime factors of the given number N.
    """
    num = n
    ans = []

    while True:
        d = pollard_algorithm(num)
        ans.append(d)
        r = int(num / d)

        if isprime(r):
            ans.append(r)
            break
        else:
            num = r

    return ans

def find_private_key(n, public_key):
    """
    Driver function that finds the private key using a brute-force method.
    """
    factors = find_prime_factors(n)
    print(f"Factors of {n} are: {factors}")

    phi_n = (factors[0] - 1) * (factors[1] - 1)
    print(f"Φ(n) = {phi_n}")

    private_key = pow(public_key, -1, phi_n)

    return private_key

def decypher_rsa(cryptogram, n, private_key):
    """
    Decyphers a given RSA cryptogram with the knowledge of a given private key.
    """
    message = square_and_multiply_always(cryptogram, private_key, n)
    str_message = str(message)
    letters = []

    for i in range(len(str_message) // 2):
        letter = str_message[2*i:(2*i)+2]
        letters.append(letter)

    decyphered_message = ""

    for i in range(len(letters)):
        decyphered_message += chr((int(letters[i]) + 65 - 1))   # We have to subtract one because the alphabet mentioned in the assignment starts with 1 and not 0.

    print(f"Numerical values of the letters: {letters}")

    return decyphered_message

def square_and_multiply_always(base, exp, mod):
    """
    Utility function that calculates base^exp % mod using a Square and multiply algorithm.
    """
    R0 = 1
    R1 = base
    c = '{0:b}'.format(exp)
    i = len(c)-1
    t = 0
    c = c[::-1]
    while(i >= 0):
        if(t == 0):
            Rt = R0
        elif(t == 1):
            Rt = R1
        else:
            print("t != 0 or 1")
        R0 = (R0*Rt) % mod
        d = int(c[i])
        t = (t^d)
        i = i-1 + t
    return R0

def basic_euclidean_algorithm(a, b):
    """
    Recursive function that calculates the GCD of given numbers a and b using a basic version of Euclidean algorithm. Returns the value of GCD.
    """
    if a == 0:
        return b

    return basic_euclidean_algorithm(b % a, a)

def extended_euclidean_algorithm(a, b):
    """
    Calculate the inverse element using an Extended Euclidean algorithm.
    """
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = extended_euclidean_algorithm(b % a, a)
        return (g, x - (b // a) * y, y)

def main():
    # First task
    print("1)")
    print(f"GCD(10547, 1651) = {basic_euclidean_algorithm(10547, 1651)}\n")

    # Second task
    print("2)")
    _, x, _ = extended_euclidean_algorithm(345, 577)
    print(f"345^(-1) mod 577 = {x % 577}\n")

    # Third task
    print("3)")
    _, x, _ = extended_euclidean_algorithm(16, 357)
    print(f"16^(-1) mod 357 = {x % 357}\n")

    # Fourth task
    print("4)")
    print(f"Private key: {find_private_key(129621733357, 9)}\n")

    # Fifth task
    print("5)")
    print(f"Decyphered text: {decypher_rsa(605710893992533, 3324847060925479, 1813552871415011)}")

if __name__ == "__main__":
    main()


import matplotlib.pyplot as plt
import numpy as np
import math as m

def spirSum(c0):
    c1=np.zeros(len(c0)+1)+np.zeros(len(c0)+1)*1j
    c1[0]=0
    for i in range(len(c0)):
        c1[i+1]=c1[i]+c0[i]/n
    return c1

D=0.3         #[m] távcső átmérője
lmbda=380e-9    #[m] hullámhossz
f=2.5           #[m] fókusztáv
alpha=D/2/f *180/m.pi /1000     #[rad] max látómező az apertúra és a fókusztáv összefüggéséven

n=100
d=np.arange(0,D,D/n)

# print(C)

# for i in range(-80,80):
#     C=np.exp(1j*2*m.pi/lmbda*d*np.sin(0.5*i*1e-7))
#     C2=spirSum(C)
#     plt.plot(np.real(C2), np.imag(C2),'.-', color=(0.2, 0.4, 0.6))
#     plt.plot(np.real(C2[-1]), np.imag(C2[-1]),'o', color=(0., 0., 0.))
N=80
for i in range(-N,N):
    C=np.exp(1j*2*m.pi/lmbda*d*np.sin(0.5*i*1e-7))
    C2=spirSum(C)
    plt.plot(np.real(C2[-1]), np.imag(C2[-1]),'.', color=(0., 0., 0.))
# C2=np.zeros(len(C)+1)+np.zeros(len(C)+1)*1j
# C2[0]=0
# for i in range(len(C)):
#     C2[i+1]=C2[i]+C[i]/n

# print(C2)

plt.plot(np.real(C), np.imag(C),'.')

plt.grid()
ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
plt.xlim([-1.5, 1.5])
plt.ylim([-1.5, 1.5])

plt.show()

x=np.arange(-alpha,alpha,alpha/100)

beta=D/lmbda*np.sin(x*m.pi/180)

I=D*np.sin(beta/2)/(beta/2)*np.cos(x)

plt.plot(x,I)
plt.xlabel("szögérték [fok]")
plt.show()