#! /usr/bin/env python3
#
# Collect information from the system and prepare a data packet to be sent to another system.
#
# This was originally designed for UDP datagrams, so the data packet is small with
# data integrity built in, but no error correction.
#
# The host specific configuration information is in a YAML file.
#
# June-2022, Pat Welch, pat@mousebrains.com

from argparse import ArgumentParser
from Hosts import Hosts, Host
from Hash import hash8, hash8Integer
import logging
import socket
import os

class Packet:
    def __init__(self, args:ArgumentParser) -> None:
        self.__args = args

    @staticmethod
    def addArgs(parser:ArgumentParser) -> None:
        grp = parser.add_argument_group("Data packet related options")
        grp.add_argument("--tempDevice", type=str, default="/sys/class/thermal/thermal_zone0/temp",
                help="Name of thermal device to read temperature from")
        grp.add_argument("--fs", type=str, default="/",
                help="Filesystem to send information about")

    def implode(self, host:Host) -> bytes:
        temp = None
        total = None
        free = None
        try:
            args = self.__args
            # First collect file system information
            info = os.statvfs(args.fs) # Get the filesystem information
            norm = info.f_frsize / 2**30 # tens of gigabytes
            total = info.f_blocks * norm
            free = info.f_bavail * norm
            if os.path.isfile(args.tempDevice):
                with open(args.tempDevice, "r") as fp:
                    temp = int(fp.read())# temperature in milliC
        except:
            logging.exception("Unexpected exception")
            return None

        data = bytearray()
        data+= host.hostNumber
        data+= host.encodeTemperature(temp)
        data+= host.encodeSpace(free)
        data+= host.encodeFraction(free, total)
        data+= hash8(data)
        return data

    def explode(self, data:bytes, hosts:Hosts) -> tuple[str, float, float, float]:
        if len(data) != 8:
            logging.warning("Data packet, %s, not 8 bytes long, %s", len(data), data)
            return (None, None, None, None)
        hash = hash8Integer(data[:-1])
        if hash != data[-1]:
            logging.warning("Data packet hash, %s, does not agree with calculation, %s, for %s",
                    data[-1], hash, data)
            return (None, None, None, None)

        host = hosts.hostFromNumber(data[0])
        if host is None:
            return (None, None, None, None)
        return (
                host.name,
                host.decodeTemperature(data[1:3]),
                host.decodeSpace(data[3:5]),
                host.decodeFraction(data[5:7]),
                )

if __name__ == "__main__":
    parser = ArgumentParser()
    Packet.addArgs(parser)
    Hosts.addArgs(parser)
    args = parser.parse_args()

    hosts = Hosts(args)
    host = hosts.getHost()
    packet = Packet(args)
    data = packet.implode(host)
    print("Packet", data, len(data))
    (host, temp, free, frac) = packet.explode(data, hosts)
    print("Host", host, "temp", temp, "free", free, "frac", frac)


import cv2
import pandas as pd
import os
from vision.visualization.drawer import draw_rectangle, draw_text, draw_highlighted_test, get_color
import numpy as np
from vision.misc.help_func import get_repo_dir, load_json, validate_output_path

def draw_dets(frame, dets, t_index=6):
    """
    Draws bounding boxes and track IDs on the input frame for each detection.

    Args:
        frame (numpy.ndarray): The input frame to draw on.
        dets (numpy.ndarray): A 2D array of detections, where each row is a detection and the columns contains the
            coordinates of the bounding box and the track ID.
        t_index (int, optional): The index of the track ID column in the detections array. Defaults to 6.

    Returns:
        numpy.ndarray: The input frame with bounding boxes and track IDs drawn on it.
    """
    for det in dets:
        track_id = det[t_index]
        color_id = int(track_id) % 15  # 15 is the number of colors in list
        color = get_color(color_id)
        text_color = get_color(-1)
        frame = draw_rectangle(frame, (int(det[0]), int(det[1])), (int(det[2]), int(det[3])), color, 3)
        frame = draw_highlighted_test(frame, f'ID:{int(track_id)}', (det[0], det[1]), frame.shape[1], color, text_color,
                                      True, 10, 3)
    return frame

def write_vid(fsi, tracks_df, s_frame=0, max_frame=np.inf, t_index=6, frame_save_directory=""):
    """
    Writes a new video file with the input tracks_df overlaid on top of the input video file.

    Args:
        fsi (cv2.VideoCapture): The input video file to overlay tracks on top of.
        tracks_df (pandas.DataFrame): A DataFrame containing track data with columns "frame", "x1", "y1", "x2",
            "y2", and "track_id".
        s_frame (int, optional): The starting frame of the output video. Defaults to 0.
        max_frame (int, optional): The maximum number of frames to write to the output video. Defaults to np.inf.
        t_index (int, optional): The index of the track ID column in the detections array. Defaults to 6.
        frame_save_directory (str, optional): path for saving frame by frame images
    """
    i = s_frame
    n = min(cap_fsi.get(cv2.CAP_PROP_FRAME_COUNT), max_frame)
    while cap_fsi.isOpened() and i < max_frame:
        print(f"\r{i}/{n - 1} ({i / (n - s_frame) * 100: .2f}%) frames", end="")
        _, frame_fsi = cap_fsi.read()
        if isinstance(frame_fsi, type(None)):
            break
        if rotate:
            frame_fsi = cv2.rotate(frame_fsi, cv2.ROTATE_90_COUNTERCLOCKWISE)
        dets = tracks_df[tracks_df["frame"] == i].to_numpy()
        if len(dets):
            frame_fsi = draw_dets(frame_fsi, dets, t_index=t_index)
        if frame_save_directory != "":
            cv2.imwrite(os.path.join(frame_save_directory, f"frame_{i}.png"), frame_fsi)
        fsi.write(frame_fsi)
        i += 1
    fsi.release()

def make_vid_with_dets(cap_fsi, tracks_df, rotate, fps=0, new_vid_name="", s_frame=0, max_frame=np.inf
                       , frame_save_directory=""):
    """
    Creates a new video file with detected object tracks overlaid on each frame.

    Args:
        cap_fsi (cv2.VideoCapture): The input video capture object.
        tracks_df (pd.DataFrame): A DataFrame containing object tracks with columns "frame", "x1", "y1", "x2", "y2",
         and "track_id".
        rotate (bool): If True, rotate the video frames by 90 degrees counterclockwise before processing.
        fps (int): The frames per second to use for the output video. If 0, use the same frame rate as the input video.
        new_vid_name (str): The filename to use for the output video. If empty, create a default filename.
        s_frame (int): The index of the starting frame.
        max_frame (int): The maximum number of frames to process.
        frame_save_directory (str, optional): path for saving frame by frame images

    Returns:
        None
    """
    width = int(cap_fsi.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap_fsi.get(cv2.CAP_PROP_FRAME_HEIGHT))
    if rotate:
        width, height = height, width
    if not fps:
        fps = int(cap_fsi.get(cv2.CAP_PROP_FPS))
    if new_vid_name == "":
        new_vid_name = os.path.join(row_path, f'Result_FSI_{side}_with_dets.mkv')
    else:
        new_vid_name = os.path.join(row_path, new_vid_name)
    if frame_save_directory != "":
        validate_output_path(frame_save_directory)
    fsi = cv2.VideoWriter(new_vid_name, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, (width, height))
    cap_fsi.set(cv2.CAP_PROP_POS_FRAMES, s_frame)
    write_vid(fsi, tracks_df, s_frame, max_frame, frame_save_directory=frame_save_directory)

if __name__ == "__main__":
    row_path = r"/media/fruitspec-lab/cam175/APPLECHILE04_test/290323/APPCALIB/R1A"
    rotate = True
    s_frame = 0
    max_frame = 300
    fps = 15
    new_vid_name = ""
    frame_save_directory = "/media/fruitspec-lab/easystore/debugging/APPLECHILE04_test_R1A"

    side = 1 if row_path.endswith("A") else 2
    jai_fp = os.path.join(row_path, f'Result_FSI_{side}.mkv')
    tracks_path = os.path.join(row_path, f'tracks.csv')
    tracks_df = pd.read_csv(tracks_path)
    cap_fsi = cv2.VideoCapture(jai_fp)

    make_vid_with_dets(cap_fsi, tracks_df, rotate, fps=fps, new_vid_name=new_vid_name, s_frame=s_frame,
                       max_frame=max_frame, frame_save_directory=frame_save_directory)


import time
import uuid

from BluenetTestSuite.testframework.framework import *
from BluenetTestSuite.firmwarecontrol.datatransport import *
from crownstone_core.protocol.BluenetTypes import ControlType

import datetime

def test_norelayflickeringonboot_loopbody(FW, minimal_relay_half_period_ms):
    """
    Assumes that a state change is reported at the exact moment the relay will be switched.

    Resets the device, then waits for some time to record any state changes. Validates all such
    changes are no closer than [minimal_relay_half_period_ms]
    """
    print("##### setup test_bootloadsflashintooverride_loopbody #####")
    # print("reset firmwarestate recorder")
    FW.clear()

    # print("restart test subject")
    sendCommandToCrownstone(ControlType.RESET, [])

    # print("allow device to reset and startup")
    time.sleep(5)

    # filter relevant history
    relay_history_time_value_pairs = [
        [logentry.time, logentry.value]
        for logentry in FW.historylist if logentry.classname == "Relay" and logentry.valuename == "on"
    ]

    min_time_diff_between_relay_switches = datetime.timedelta(microseconds=minimal_relay_half_period_ms * 1000)

    num_relay_state_changes = len(relay_history_time_value_pairs)
    if num_relay_state_changes < 2:
        # print("too few relay state changes reported during flick test to check period lengths ({0})".format(
        # num_relay_state_changes))
        return TestFramework.success()

    failures = []

    first = relay_history_time_value_pairs[0]
    for second in relay_history_time_value_pairs[1:]:
        if first[1] == second[1]:
            # no state change yet.
            pass
        else:
            state_unchanged_time = second[0] - first[0]
            if state_unchanged_time < min_time_diff_between_relay_switches:
                failures += [TestFramework.failure("found too short relay flick: " + repr(state_unchanged_time))]
            # move to next relay state half-period
            first = second

    return failures if failures else TestFramework.success()

def test_norelayflickeringonboot(FW):
    """
    It's not allowed to see/hear any sudden relay state changes after a reboot.
    """
    print("##### test_bootloadsflashintooverride #####")
    print("override switch state to be sure it is set to fully on, and pause 15 sec to enforce persistence")
    sendCommandToCrownstone(ControlType.SWITCH, [100])

    # need time for persistence of switch state to be pushed.
    time.sleep(15)

    result = []
    for i in range(3):
        result += [test_norelayflickeringonboot_loopbody(FW, 1000)]
    return result

if __name__ == "__main__":
    with TestFramework(test_norelayflickeringonboot) as frame:
        if frame != None:
            for result in frame.test_run():
                print(result)
        else:
            print(TestFramework.failure())


import pytest

from app.models import Post
from django.urls import reverse

def test_get_posts_list(db, api_client, post_factory):
    post = post_factory()
    url = reverse('posts_list')
    response = api_client.get(url)
    print(response.json())
    assert response.status_code == 200
    assert response.json() == [
        {
            'id': post.id,
            'title': post.title,
            'content': post.content,
            'user': post.user.id
        }
    ]

# def test_new_post_factory(db, post_factory):
#     post = post_factory.create()
#     print(post.user.__dict__)
#     assert True


from sqlalchemy import Session

from . import keygen, models, schemas

def create_db_url(db: Session, url: schemas.BaseURL) -> models.URL:
    key = keygen.create_unique_random_key(db)
    secret_key = f"{key}_{keygen.create_random_key(length=8)}"
    db_url = models.URL(
        target_url=url.target_url, key=key, secret_key=secret_key
    )
    db.add(db_url)
    db.commit()
    db.refresh(db_url)
    return db_url

# This function either returns the URL object or returns a None object.
def get_db_url_by_key(db: Session, url_key: str) -> models.URL:
    return db.query(models.URL).filter(models.URL.key == url_key, models.URL.is_active).first()


import pandas as pd
import numpy as np
from sklearn.naive_bayes import BernoulliNB
import math

from apps import db_onnections

def anomality_model(history,predict):
    '''
    model used: Bernoulli Naive Bayse
    :param history: the dataframe with which fit the model
    :param predict: the element to be used for the prediction
    :return: the probability of the user for the given array.

    '''
    user = predict["display_name"]
    history = history.drop(['timestamp'], axis=1)

    history["display_name"] = np.where(history["display_name"] == user, 1, 0)

    df_target = history[history["display_name"] == 1]
    df_core = history[history["display_name"] == 0]

    df_core = df_core.drop_duplicates() # Reduce the "No-events"

    df = df_target.append(df_core)

    model = BernoulliNB()
    label = df['display_name']
    main = df.drop(['display_name'], axis=1)

    model.fit(main, label)

    #print(user)
    predict = predict.drop(['timestamp', 'display_name'])
    #print("### predict #####")

    output = model.predict_proba(np.array(predict).reshape(1,-1))

    return output[0][1]

def checker_anomalitics():#from_data='2018-01-01', border_data= "2018-06-18", end_date= "2018-06-25"

    history, predict = db_onnections.anomalityDB() #from_data= from_data, border_data= border_data, end_date= end_date)

    dataframe = pd.DataFrame()
    for index, item in predict.iterrows():
        ser = pd.Series()
        ser["Display Name"] = item["display_name"]
        ser['Hostname'] = item['Hostname_']
        ser['Protocol'] = item['Protocol_']
        ser['Subnet'] = item['Subnet_']

        item=item.drop(['Hostname_','Protocol_','Subnet_'])

        proba = anomality_model(history=history, predict=item)

        ser["Probability"] = math.sqrt(proba)

        ser["Time"] = pd.to_datetime(item['timestamp'])
        ser["Time"] = pd.to_datetime(ser["Time"])
        ser['Time'] = ser['Time'].strftime("%Y-%m-%d %H:%M:%S")

        dataframe = dataframe.append(ser, ignore_index=True)

        dataframe = dataframe.sort_values("Probability")
        dataframe = dataframe[["Time", "Display Name", "Subnet", "Protocol", "Hostname", "Probability"]]

        dataframe['Time'] = pd.to_datetime(dataframe['Time'])

    return dataframe


import pandas as pd
import streamlit as st
import plotly.graph_objects as go
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification,
)
import altair as alt

cols = [
    "stereotype",
    "anti-stereotype",
    "unrelated",
    "profession",
    "race",
    "gender",
    "religion",
]

def inference(inputs: str):
    tokenized = tokenizer(
        inputs,
        return_tensors="pt",
        max_length=512,
        padding="max_length",
        truncation=True,
    ).to(device)
    outputs = model(**tokenized)
    return outputs.logits.tolist()[0]

def write_header():
    st.title("Korean Stereotype Detector")
    st.markdown(
        """
        - Write any sentence containing stereotypes and click the Run button.
        - This application is made with TUNiB-Electra and K-StereoSet.
        - Using CPU, the result might be slow
    """
    )

def get_json_str(lists):
    string = "{\n"
    for i, v in enumerate(lists):
        string += f'\t"{cols[i]}": {v},\n'
    return string + "}"

def write_textbox():
    input_text = st.text_area(label="Write your sentence", key=1, height=40)
    button = st.button(label="Run")
    output = inference(input_text)

    st.markdown(
        """
    #
    ## result
    ***
    """
    )

    if button:
        col1, col2 = st.columns([5, 5])

        with col1:
            st.code(get_json_str(output))

        with col2:
            st.write(
                alt.Chart(
                    pd.DataFrame({"Class": cols, "Logits": output}),
                    width=490,
                    height=360,
                )
                .mark_bar()
                .encode(x="Class", y="Logits")
            )

if __name__ == "__main__":
    st.set_page_config(
        page_title="Korean stereotype detector", page_icon="☮", layout="wide"
    )
    tokenizer = AutoTokenizer.from_pretrained(
        "dhtocks/tunib-electra-stereotype-classifier"
    )
    model = AutoModelForSequenceClassification.from_pretrained(
        "dhtocks/tunib-electra-stereotype-classifier"
    )
    device = "cpu"
    write_header()
    write_textbox()


import matplotlib.pyplot as plt
import os
import csv
import numpy as np
x_list = []
y_list = []
x, y = [], []
with open('mean_std.csv', 'r') as f:
    rdr = csv.reader(f)
    mylist = list(rdr)
    # print(mylist[1])
    # print(len(mylist))
    for i in range(0, len(mylist)):
        if (i % 2) == 0:
            x.append(mylist[i])
        else:
            y.append(mylist[i])
    for t in x:
        t = float(t[0])
        x_list.append(t)
    for i in y:
        i = float(i[0])
        y_list.append(i)
    print(x_list, y_list)

x_mean = np.mean(x_list)
x_std = np.std(x_list)
y_mean = np.mean(y_list)
y_std = np.std(y_list)
print(x_mean)
print(y_mean)
for i in range(0, len(x_list)): # Regularization
    x_list[i] = (x_list[i] - x_mean)/x_std

for t in range(0, len(y_list)):
    y_list[t] = (y_list[t] - y_mean)/y_std

print(x_list)
print(y_list)

plt.scatter(x_list, y_list, c="black") # 산포도 그려서 저장
plt.title("Mean, Std Distribution")
plt.xlabel("mean")
plt.ylabel("std")
plt.savefig("mean_std.png")
plt.show()


from googleapiclient.discovery import build
import os
from dotenv import load_dotenv
from youtube_transcript_api import YouTubeTranscriptApi
from preprocessing.objects import video
from preprocessing.objects import query

# Calls Youtube API and retrieves transcript
def youtube_request(query, link =''):
    try:
        load_dotenv()
        youtube = build('youtube', 'v3', developerKey=os.getenv('GOOGLE_APPLICATION_CREDENTIALS'))

        if query.get_select():
            request_id = youtube.search().list(part='snippet', q=link, maxResults = query.get_max_results())
            response = request_ids.execute()
            print(response)

        else:
            request_ids = youtube.search().list(part='snippet', q=query.get_search(), maxResults = query.get_max_results(), type=query.get_type(), videoCaption='closedCaption')
            response = request_ids.execute()

        return response

    except:
        print('Error retrieving video information')

def transcript_request(url):
    try:
        srt = YouTubeTranscriptApi.get_transcript(url, languages=['en'])
        for text in srt:
            transcript = transcript + text['text'] + ' '

        return transcript

    except:
        print('Error retrieving transcript')

def run_youtube_query(query):

    # If manual selections made, retrieves transcript and specific video information from youtube API:
    if query.get_selection():
        for link in query.get_selection():
            response = youtube_request(query, link)
            for items in response['items']:
                new_video = video(items['id']['videoId'], items['snippet']['channelTitle'],'',items['snippet']['publishedAt'], items['snippet']['title'], transcript_request(link), '')
                query.add_video_object(new_video)

            return query

    # Calls Youtbue API if no manual selection made:
    else:
        query_response = youtube_request(query)
        # Iterates through JSON response and adds video object to query list
        try:
            for items in query_response['items']:
                new_video = video(items['id']['videoId'], items['snippet']['channelTitle'],'',items['snippet']['publishedAt'], items['snippet']['title'], transcript_request(items['id']['videoId'], ''))
                query.add_video_object(new_video)
        except:
            print("Error creating video object")

    return query


"""
常用的认证以及DRF的认证
"""
import logging

import jwt
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.utils.six import text_type
from django.utils.translation import ugettext as _
from rest_framework import exceptions
from rest_framework_jwt.utils import jwt_decode_handler

from .jwt_util import jwt_get_session_id
from ..permission.models.users import UserProfile

logger = logging.getLogger(__name__)
User = get_user_model()

class OpAuthJwtAuthentication(object):
    """
    统一JWT认证(环境允许情况下, 推荐使用RedisOpAuthJwtAuthentication)
    """

    def authenticate(self, request):
        token = self.get_header_authorization(request) or self.get_cookie_authorization(request)
        if not token:
            return None
        try:
            payload = jwt_decode_handler(token)
        except jwt.ExpiredSignature:
            msg = _('Signature has expired.')
            raise exceptions.AuthenticationFailed(msg)
        except jwt.DecodeError:
            msg = _('Error decoding signature.')
            raise exceptions.AuthenticationFailed(msg)
        except jwt.InvalidTokenError:
            raise exceptions.AuthenticationFailed()
        except UserProfile.DoesNotExist:
            raise exceptions.AuthenticationFailed()

        username = payload.get('username', None)
        if not username:
            return None
        username_field = settings.USERNAME_FIELD or 'username'
        user = User.objects.filter(**{username_field: username}).first()
        if not user or not user.is_active:
            return None
        return user, token

    def authenticate_header(self, request):
        pass

    @classmethod
    def get_header_authorization(cls, request):
        """
        获取header里的认证信息, 通常用于跨域携带请求
        :param request:
        :return:
        """
        auth = request.META.get('HTTP_AUTHORIZATION', b'')
        if isinstance(auth, text_type):
            auth = auth.encode(settings.JWT_AUTH.get('HTTP_HEADER_ENCODING', 'iso-8859-1'))
        if not auth:
            return ''
        auth = str(auth, encoding='utf-8').split()
        if len(auth) != 2 or auth[0].upper() != settings.JWT_AUTH.get('JWT_AUTH_HEADER_PREFIX', 'JWT').upper():
            return ''
        return auth[1]

    @classmethod
    def get_cookie_authorization(cls, request):
        """
        获取cookie里JWT认证信息
        :param request:
        :return:
        """
        auth = request.COOKIES.get(settings.JWT_AUTH.get('JWT_AUTH_COOKIE', 'AUTH_JWT'), '')
        auth = auth.split()
        if len(auth) != 2 or auth[0].upper() != settings.JWT_AUTH.get('JWT_AUTH_HEADER_PREFIX', 'JWT'):
            return ''
        return auth[1]

class RedisOpAuthJwtAuthentication(OpAuthJwtAuthentication):
    """
    基于Redis的统一JWT认证(推荐使用)
    """
    prefix = settings.JWT_AUTH.get('JWT_AUTH_HEADER_PREFIX', 'JWT')

    def authenticate(self, request):
        res = super().authenticate(request)
        if res:
            user, token = res
            session_id = jwt_get_session_id(token)
            key = f"{self.prefix}_{session_id}_{user.username}"
            redis_token = cache.get(key)
            if redis_token == token:
                return user, token
            else:
                raise exceptions.AuthenticationFailed("登录信息失效，请重新登录！")
        return None


from django.http import response
from django.shortcuts import render,redirect
from django.conf import settings
from django.http.response import JsonResponse
from django.contrib.auth.decorators import login_required
import razorpay
from .models import *
from datetime import datetime
import json
from django.views.decorators.csrf import csrf_exempt
from django.contrib import messages
from django.http import HttpResponseBadRequest
from django.contrib.auth.models import User

razorpay_client = razorpay.Client(auth=(settings.RAZOR_KEY_ID, settings.RAZOR_KEY_SECRET))

@login_required
def payment_page(request):
    if request.method == "POST":
        print('request.GET.get("amount")')
    return render(request,'payment_page.html')

def user_payment(request):
    if request.GET.get('event') == "pay" :
        amount = int(request.GET.get("amount")) * 100
        currency = 'INR'
        razorpay_order = razorpay_client.order.create(dict(amount=amount,
                                                        currency=currency,
                                                        payment_capture='0'))
        razorpay_order_id = razorpay_order['id']
        callback_url = '/payment/payment-details/'
        # user_payment=UserPayment(Username=request.user,Order_id=razorpay_order_id)
        # user_payment.save()
        context = {}
        context['razorpay_order_id'] = razorpay_order_id
        context['razorpay_merchant_key'] = settings.RAZOR_KEY_ID
        context['razorpay_amount'] = amount
        context['currency'] = currency
        context['callback_url'] = callback_url
        return JsonResponse(context)

from django.utils.decorators import method_decorator

@csrf_exempt
@login_required
def payment_details(request):
    if request.method == "POST":
        print(request.body)
        payment_id = request.POST.get("razorpay_payment_id", "")
        payment_client = razorpay_client.payment.fetch(payment_id)
        create_at=datetime.fromtimestamp(payment_client['created_at']).strftime("%A, %B %d, %Y %I:%M:%S")

        payment_detail=Payment_Detail(Username=request.user,Order_id=payment_client['order_id'],
                                    Payment_id=payment_client['id'],Status=payment_client['status'],
                                    Method=payment_client['method'],
                                    Upi_id=payment_client['vpa'],Email=payment_client['email'],
                                    Contact=payment_client['contact'],
                                    Upi_Transaction_id=payment_client['acquirer_data']['upi_transaction_id'],Create_at=create_at,
                                    Amount=payment_client['amount']/100,Currency=payment_client['currency'],
                                    International=payment_client['international'],
                                    Amount_refunded=payment_client['amount_refunded'],
                                    )
        payment_detail.save()

        messages.success(request,"your payment has successful")
        return redirect('/')
    else:
        return HttpResponseBadRequest()


import copy
from functools import partial
from inspect import Parameter, signature
from multiprocessing import get_logger
import traceback
from typing import Any, Tuple

from ice.llutil.argparser import as_list, isa
from ice.llutil.utils import auto_freeze_enabled

def _inplace_surrogate(cls, funcname):
    backupname = "__backup" + funcname
    setattr(cls, backupname, getattr(cls, funcname, None))
    def newfunc(self, *a, **k):
        try:
            cfg = object.__getattribute__(self, "_builder")
            if not cfg._frozen:
                try:
                    value = getattr(cfg, funcname)(*a, **k)
                except AttributeError as e:
                    get_logger().info(f"Try to automatically freeze due to AttributeError:\n{e.args[0]}")
                    self.auto_freeze()
                    if not cfg._frozen: raise
            if cfg._frozen:
                fn = getattr(cls, backupname)  # will get None instead of raise AttributeError if not hasattr(original_class, funcname), so
                if fn is None: raise AttributeError(funcname) # we raise it manually.
                value = fn(self, *a, **k)
        except AttributeError:
            # TODO: rephrase the exception handling part
            if funcname == "__getattribute__":
                if len(a) and a[0] in ('freeze', 'clone', 'update_params'):
                    return object.__getattribute__(cfg, *a)
                else:
                    try:
                        value = object.__getattribute__(cls, *a)
                    except AttributeError:
                        value = object.__getattribute__(self, *a)
            else:
                # assert False, f"{cls.__name__}.{funcname}({a}, {k}) frozen={cfg._frozen}\nAttributeError:{e}"
                raise
        return value
    setattr(cls, funcname, newfunc)

def configurable(cls):
    """This decorator delays the initialization of ``cls`` until ``freeze()``.

    Returns:
        decorated class which is now configurable.

    Example:

    >>> import ice
    >>>
    >>> @ice.configurable
    >>> class AClass:
    >>>     def __init__(self, a, b, c, d):
    >>>         self.a = a
    >>>         self.b = b
    >>>         self.c = c
    >>>         self.d = d
    >>>
    >>> # partial initialization.
    >>> i = AClass(b=0)
    >>>
    >>> # alter positional and keyword arguments afterwards.
    >>> i[0] = 2
    >>> i['b'] = 1
    >>> i.update({'c': 3, 'd': 4})
    >>> i.update(d=5)
    >>>
    >>> # unfrozen configurable can be printed as a legal construction python statement.
    >>> assert repr(i) == "AClass(a=2, b=1, c=3, d=5)"
    >>>
    >>> # real initialization of original object.
    >>> i.freeze()
    >>> assert i.a == 2 and i.b == 1
    """

    # do not wrap the same cls more than once.
    if is_configurable(cls):
        return cls

    # mark the cls so that is becomes a configurable class.
    cls.configurable_class_id = id(cls)

    try:
        object.__getattribute__(cls, "__freeze__")
    except AttributeError:
        # use the original initialization function as __freeze__.
        cls.__freeze__ = getattr(cls, "__init__")

    # substitute the original initialization function.
    def __new_init__(obj, *args, **kwds):
        if obj.configurable_class_id == cls.configurable_class_id:
            _Builder(cls, obj, *args, **kwds)
        else:  # called from subclass instance
            cls.__freeze__(obj, *args, **kwds)

    cls.__init__ = __new_init__

    # surrogate other functions
    _inplace_surrogate(cls, "__getattribute__")
    _inplace_surrogate(cls, "__getattr__")
    _inplace_surrogate(cls, "__str__")
    _inplace_surrogate(cls, "__getitem__")
    _inplace_surrogate(cls, "__setitem__")
    _inplace_surrogate(cls, "__call__")
    _inplace_surrogate(cls, "__repr__")
    _inplace_surrogate(cls, "extra_repr")
    _inplace_surrogate(cls, "__contains__")

    # return the modified cls
    return cls

def is_configurable(cls) -> bool:
    """check if a class or an object is configurable.

    Returns:
        bool

    Example:

    >>> import ice
    >>> import torch.nn as nn
    >>> ice.make_configurable(nn.Conv2d, nn.Linear)
    >>> assert ice.is_configurable(nn.Conv2d)

    """
    return getattr(cls, "configurable_class_id", None) in (id(cls), -1)

def has_builder(obj) -> bool:
    try:
        object.__getattribute__(obj, "_builder")
        return True
    except:
        return False

def frozen(obj):
    if is_configurable(obj):
        if has_builder(obj):
            builder = object.__getattribute__(obj, "_builder")
            return builder._frozen
        else:
            return obj._frozen
    else:
        return True  # view normal objects as a frozen version

def make_configurable(*classes):
    """This function converts multiple existing classes to configurables.

    Note:
        This have exactly the same effects of decorate each class with `@configurable` when defining the class.
        Each class only need to be decorated once, extra calling of conversion is ignored and has no side effects.

    Example:

    >>> import ice
    >>> import torch.nn as nn
    >>> ice.make_configurable(nn.Conv2d, nn.Linear)
    >>> assert ice.is_configurable(nn.Conv2d)

    """
    for cls in classes:
        configurable(cls)

def clone(obj, deepcopy=True):
    """clone configurables, containers, and ordinary objects recursively.

    Args:
        obj (configurable or list/dict of configurables): the configurable object to be cloned.
        deepcopy (bool, optional): copy resources by value. Defaults to True.

    Returns:
        Unfrozen copy of the original configurable.

    >>> import ice
    >>> import torch.nn as nn
    >>> ice.make_configurable(nn.Conv2d, nn.Linear)
    >>>
    >>> convcfg = nn.Conv2d(16, 8)
    >>>
    >>> conv1x1 = convcfg.clone()  # or ice.clone(convcfg)
    >>> conv1x1['kernel_size'] = 1
    >>> conv1x1.freeze()  # or ice.freeze(conv1x1)
    >>> assert conv1x1.kernel_size == (1, 1)
    >>>
    >>> conv3x3 = convcfg.clone()
    >>> conv3x3['kernel_size'] = 3
    >>> conv3x3.freeze()
    >>> assert conv3x3.kernel_size == (3, 3)
    """
    import torch
    if isinstance(obj, list):
        obj = [clone(x, deepcopy=deepcopy) for x in obj]
    elif isinstance(obj, tuple):
        obj = tuple(clone(x, deepcopy=deepcopy) for x in obj)
    elif isinstance(obj, dict):
        obj = obj.__class__({k: clone(v, deepcopy=deepcopy) for k, v in obj.items()})
    elif has_builder(obj):
        obj = objattr(obj, "_builder").clone(deepcopy=deepcopy)
    elif isinstance(obj, Configurable):
        obj = obj.clone()
    elif isinstance(obj, torch.Tensor):
        obj = obj.clone()
    elif deepcopy:
        obj = copy.deepcopy(obj)
    else:
        obj = copy.copy(obj)
    return obj

def freeze(obj):
    """freeze configurables recursively.

    **Freezing** is the process of building the configuration into real objects.
    Original `__init__()` functions of configurable classes declared by ``configurable``
     or ``make_configurable`` now will be called recursively to initialize the real instance,
     also known as the frozen version of a configurable.

    Args:
        obj (configurable or list/dict of configurables): the configurable object to be freeze.

    Returns:
        Frozen version of the original configurable.

    Note:
        Freezing happens in-place, ignoring the returned value is safe.
        If a user wants to reuse the configuration feature, he can clone() the
        object before or after frozen with the same effect.

    Example:
        See examples for ``configurable`` and ``clone``.
    """
    import torch
    if isinstance(obj, list):
        obj = [freeze(x) for x in obj]
    elif isinstance(obj, tuple):
        obj = tuple(freeze(x) for x in obj)
    elif isinstance(obj, dict):
        obj = obj.__class__({k: freeze(v) for k, v in obj.items()})
    elif has_builder(obj):
        obj = objattr(obj, "_builder").freeze()
    elif isinstance(obj, Configurable):
        obj = obj.freeze()
    elif isinstance(obj, torch.Tensor):
        obj = obj.clone()
    return obj

def objattr(obj, attrname):
    return object.__getattribute__(obj, attrname)

class Configurable:

    configurable_class_id = -1

    def __init__(self, *args, **kwds) -> None:
        try:
            objattr(self, "_cls")
        except AttributeError:
            self._obj = self
            self._cls = self.__class__

        param_signs = list(signature(self._cls.__freeze__).parameters.items())[1:]
        self._args_only = []
        self._args_only_count = 0
        self._args_only_names = {}
        self._args_or_kwds = {}
        self._argnames = []
        self._var_args = []
        self._var_args_name = None
        self._kwds_only = {}
        self._var_kwds = {}
        self._var_kwds_name = None
        for name, param in param_signs:
            if param.kind == Parameter.POSITIONAL_ONLY:
                self._args_only.append(param.default)
                self._args_only_names[name] = self._args_only_count
                self._args_only_count += 1
            elif param.kind == Parameter.POSITIONAL_OR_KEYWORD:
                self._args_or_kwds[name] = param.default
                self._argnames.append(name)
            elif param.kind == Parameter.VAR_POSITIONAL:
                self._var_args_name = name
            elif param.kind == Parameter.KEYWORD_ONLY:
                self._kwds_only[name] = param.default
            elif param.kind == Parameter.VAR_KEYWORD:
                self._var_kwds_name = name
            else:
                assert False, "unknown param kind"

        self._all_args_count = self._args_only_count + len(self._args_or_kwds)
        self._all_kwds_names = list(self._args_only_names.keys()) + self._argnames + [self._var_args_name, self._var_kwds_name] + list(self._kwds_only.keys())

        self._frozen = False
        self.update_params(*args, **kwds)

    def update_params(self, *args, **kwds):
        # if provided args for position-only params, all of them should be provided.
        if self._args_only_count and len(args):
            self._args_only = [None] * self._args_only_count
            for idx in range(self._args_only_count):
                try:
                    self._args_only[idx] = args[idx]
                except IndexError:
                    raise TypeError(f" {self._cls.__name__} missing 1 required position-only argument '{self._args_only_names[idx]}'.")
            args = args[idx+1:]
        # args or kwds
        if len(self._args_or_kwds) and len(args):
            for idx, arg in enumerate(args):
                self[self._args_only_count + idx] = arg
            args = args[idx+1:]
        # var_args
        if self._var_args_name is not None and len(args):
            self._var_args = args
            args = []
        if len(args):
            raise TypeError(f"{self._cls.__name__} doesn't accept variable position-only arguments.")
        # kwds
        for name, arg in kwds.items():
            self[name] = arg
        return self._obj

    def __getitem__(self, key):
        if isinstance(key, int):
            if key < 0:
                raise KeyError(key)
            elif key < self._args_only_count:
                return self._args_only[key]
            elif key < self._all_args_count:
                key = self._argnames[key - self._args_only_count]
                return self._args_or_kwds[key]
            else:
                raise KeyError(key)
        else:
            if key in self._args_or_kwds:
                return self._args_or_kwds[key]
            elif key in self._kwds_only:
                return self._kwds_only[key]
            elif key == self._var_args_name:
                return self._var_args
            elif key == self._var_kwds_name:
                return self._var_kwds
            elif key in self._args_only_names:
                idx = self._args_only_names[key]
                return self._args_only[idx]
            elif key in self._var_kwds:
                return self._var_kwds[key]
            else:
                raise KeyError(key)

    def __setitem__(self, key, value):
        if self._frozen:
            # traceback.print_stack()
            raise RuntimeError(f"Frozen configuration {self._cls.__name__} can not be altered, please use clone() at proper time.")
        if isa(key, tuple):
            for i in key:
                if i in self:
                    self.__setitem__(i, value)
                    break
            else:
                raise KeyError(key)
        else:
            if isinstance(key, int):
                if key < 0:
                    raise KeyError(key, value)
                elif key < self._args_only_count:
                    self._args_only[key] = value
                elif key < self._all_args_count:
                    key = self._argnames[key - self._args_only_count]
                    self._args_or_kwds[key] = value
                else:
                    raise KeyError(key, value)
            else:
                if key in self._args_or_kwds:
                    self._args_or_kwds[key] = value
                elif key in self._kwds_only:
                    self._kwds_only[key] = value
                elif key == self._var_args_name:
                    self._var_args = as_list(value)
                elif key == self._var_kwds_name:
                    if not isa(value, dict):
                        raise TypeError(f"direct assignment of '**{key}' argument of '{self._cls.__name__}' type should be a dict.")
                    self._var_kwds = value
                elif key in self._args_only_names:
                    idx = self._args_only_names[key]
                    self._args_only[idx] = value
                elif self._var_kwds_name is not None:
                    self._var_kwds[key] = value
                else:
                    raise KeyError(key, f"'{self._cls.__name__}' does not accept variable keywords arguments.")

    def __contains__(self, key):
        if isa(key, int):
            return key >= 0 and key < self._all_args_count
        elif isa(key, str):
            return key in self._all_kwds_names
        else:
            raise TypeError(key)

    def __str__(self):
        args = []
        if self._args_only_count:
            args.append(', '.join([str(arg) for arg in self._args_only]))
        if len(self._args_or_kwds):
            args.append(', '.join([f"{k}={v}" for k, v in self._args_or_kwds.items() if v != Parameter.empty]))
        if len(self._var_args):
            args.append(', '.join([str(arg) for arg in self._var_args]))
        if len(self._kwds_only):
            args.append(', '.join([f"{k}={v}" for k, v in self._kwds_only.items() if v != Parameter.empty]))
        if len(self._var_kwds):
            args.append(', '.join([f"{k}={v}" for k, v in self._var_kwds.items() if v != Parameter.empty]))
        args = ', '.join(args)
        return f"{objattr(self, '_cls').__name__}({args})"

    def extra_repr(self):
        return ''

    def __getattr__(self, attrname):
        if objattr(self, "_frozen"):
            raise AttributeError(f"\"{self._cls.__name__}\" does not have attribute \"{attrname}\".")
        else:
            raise AttributeError(f"Configurable \"{self._cls.__name__}\" is not frozen, which may be the reason of not having attribute \"{attrname}\".")

    def clone(self, deepcopy=True):
        return self._cls(
            *clone(self._args_only, deepcopy=deepcopy),
            *clone(self._args_or_kwds, deepcopy=deepcopy).values(),
            *clone(self._var_args, deepcopy=deepcopy),
            **clone(self._kwds_only, deepcopy=deepcopy),
            **clone(self._var_kwds, deepcopy=deepcopy)
            )

    def freeze(self):
        # make twice freezing legal.
        if self._frozen: return self._obj
        # mark as frozen so that orig__init__ will be able to manipulate the original instance via __getattribute__.
        self._frozen = True
        # initialize the class instance
        self._cls.__freeze__(self._obj, *self._args_only, *self._args_or_kwds.values(), *self._var_args, **self._kwds_only, **self._var_kwds)
        return self._obj

    def auto_freeze(self):

        if not auto_freeze_enabled(): return self._obj

        def all_args():
            for arg in self._args_only: yield arg
            for arg in self._args_or_kwds.values(): yield arg
            for arg in self._var_args: yield arg
            for arg in self._kwds_only.values(): yield arg
            for arg in self._var_kwds.values(): yield arg

        for arg in all_args():
            if arg is Parameter.empty:
                break
        else:
            self.freeze()

        return self._obj

    def __call__(self, *args: Any, **kwds: Any) -> Any:
        return self.clone().update_params(*args, **kwds).auto_freeze()

class _Builder(Configurable):
    """This class stores the arguments and meta informations that is needed by ``configurable`` decorator.

    This class is not intended to be visited directly by end user. See ``configurable`` for usage guide instead.
    """

    def __init__(self, cls, obj, *args, **kwds) -> None:
        self._cls = cls
        self._obj = obj
        super().__init__(*args, **kwds)
        object.__setattr__(obj, "_builder", self)
        self.auto_freeze()

    def __reduce__(self) -> Tuple[Any, ...]:
        assert not self._frozen, f"{self}"
        return (partial(self._cls, *self._args_only, *self._args_or_kwds.values(), *self._var_args, **self._kwds_only, **self._var_kwds), ())

    def __repr__(self):
        return str(self)


# SELECT
# 	id,
# 	rok,
# 	tytul_oryginalny,
# 	informacje,
# 	opis_bibliograficzny_cache,
# 	opis_bibliograficzny_zapisani_autorzy_cache
# FROM
# 	bpp_rekord_mat
# WHERE
# 	opis_bibliograficzny_cache LIKE '%W: pod red%';

# -*- encoding: utf-8 -*-
import argparse
import csv
import logging

import Levenshtein
from django.core.management import BaseCommand
from django.db import transaction
from openpyxl import load_workbook

from bpp.models import (
    Zrodlo,
    Rodzaj_Zrodla,
    Wydawnictwo_Zwarte,
    Wydawnictwo_Ciagle,
    TO_REDAKTOR,
)

class Command(BaseCommand):
    help = "Wyrzuca 'pod red.' z pola informacje, gdy jest już redaktor"

    def wyrzuc_pod_red_z_informacji(self, klass):
        """
        :type klass: bpp.models.Wydawnictwo_Ciagle
        """

        for elem in klass.objects.filter(
            informacje__startswith="pod red.",
            opis_bibliograficzny_cache__contains="W: pod red.",
        ):
            parsed_informacje = [
                x.strip()
                for x in elem.informacje.replace("pod red.", "").strip().split(",")
            ]

            zapisani_autorzy = [
                x.zapisany_jako
                for x in elem.autorzy_set.filter(
                    typ_odpowiedzialnosci__typ_ogolny=TO_REDAKTOR
                )
            ]

            odleglosci = [
                Levenshtein.distance(za, pi)
                for za, pi in zip(zapisani_autorzy, parsed_informacje)
            ]

            yield (
                [
                    elem.pk,
                    elem.tytul_oryginalny,
                    elem.rok,
                    elem.informacje,
                    parsed_informacje,
                    zapisani_autorzy,
                    odleglosci,
                ]
            )

    @transaction.atomic
    def handle(self, *args, **options):
        f = open("res.csv", "w")
        writer = csv.writer(f)
        writer.writerow(
            [
                "PK",
                "Tytul",
                "Rok",
                "Informacje",
                "Informacje przeanalizowane",
                "zapisani autorzy",
                "odleglosci",
            ]
        )
        for elem in Wydawnictwo_Zwarte, Wydawnictwo_Ciagle:
            for res in self.wyrzuc_pod_red_z_informacji(elem):
                writer.writerow(res)

        f.close()


from datetime import datetime

import pytest

import pcapi.core.offers.factories as offers_factories
from pcapi.core.offers.models import OfferValidationStatus
from pcapi.models import Offer
from pcapi.utils.human_ids import humanize

from tests.conftest import TestClient

@pytest.mark.usefixtures("db_session")
class Returns204Test:
    def when_activating_all_existing_offers(self, app):
        # Given
        offer1 = offers_factories.OfferFactory(isActive=False)
        venue = offer1.venue
        offer2 = offers_factories.OfferFactory(venue=venue, isActive=False)
        offerer = venue.managingOfferer
        offers_factories.UserOffererFactory(user__email="pro@example.com", offerer=offerer)

        # When
        client = TestClient(app.test_client()).with_session_auth("pro@example.com")
        data = {"isActive": True, "page": 1, "venueId": humanize(venue.id)}
        response = client.patch("/offers/all-active-status", json=data)

        # Then
        assert response.status_code == 202
        assert Offer.query.get(offer1.id).isActive
        assert Offer.query.get(offer2.id).isActive

    def when_deactivating_all_existing_offers(self, app):
        # Given
        offer1 = offers_factories.OfferFactory()
        venue = offer1.venue
        offer2 = offers_factories.OfferFactory(venue=venue)
        offerer = venue.managingOfferer
        offers_factories.UserOffererFactory(user__email="pro@example.com", offerer=offerer)

        # When
        client = TestClient(app.test_client()).with_session_auth("pro@example.com")
        data = {"isActive": False}
        response = client.patch("/offers/all-active-status", json=data)

        # Then
        assert response.status_code == 202
        assert not Offer.query.get(offer1.id).isActive
        assert not Offer.query.get(offer2.id).isActive

    def should_update_offers_by_given_filters(self, app):
        # Given
        user_offerer = offers_factories.UserOffererFactory()
        venue = offers_factories.VenueFactory(managingOfferer=user_offerer.offerer)
        matching_offer1 = offers_factories.OfferFactory(name="OKAY 1", venue=venue)
        offers_factories.StockFactory(offer=matching_offer1, beginningDatetime=datetime(2020, 10, 10, 12, 0, 0))
        matching_offer2 = offers_factories.OfferFactory(name="OKAY 2", venue=venue)
        offers_factories.StockFactory(offer=matching_offer2, beginningDatetime=datetime(2020, 10, 10, 12, 0, 0))

        offer_out_of_date_range = offers_factories.OfferFactory(name="OKAY 3", venue=venue)
        offers_factories.StockFactory(
            offer=offer_out_of_date_range,
            beginningDatetime=datetime(2020, 10, 12, 10, 0, 0),
        )
        offer_on_other_venue = offers_factories.OfferFactory(name="OKAY 4")
        offer_with_not_matching_name = offers_factories.OfferFactory(name="Pas celle-ci", venue=venue)

        data = {
            "isActive": False,
            "offererId": humanize(user_offerer.offerer.id),
            "venueId": humanize(venue.id),
            "name": "OKAY",
            "periodBeginningDate": "2020-10-09T00:00:00Z",
            "periodEndingDate": "2020-10-11T23:59:59Z",
        }
        client = TestClient(app.test_client()).with_session_auth(user_offerer.user.email)

        # When
        response = client.patch("/offers/all-active-status", json=data)

        # Then
        assert response.status_code == 202
        assert not Offer.query.get(matching_offer1.id).isActive
        assert not Offer.query.get(matching_offer2.id).isActive
        assert Offer.query.get(offer_out_of_date_range.id).isActive
        assert Offer.query.get(offer_on_other_venue.id).isActive
        assert Offer.query.get(offer_with_not_matching_name.id).isActive

    def test_only_approved_offers_patch(self, app):
        approved_offer = offers_factories.OfferFactory(isActive=False)
        venue = approved_offer.venue
        pending_offer = offers_factories.OfferFactory(venue=venue, validation=OfferValidationStatus.PENDING)
        rejected_offer = offers_factories.OfferFactory(venue=venue, validation=OfferValidationStatus.REJECTED)
        offerer = venue.managingOfferer
        offers_factories.UserOffererFactory(user__email="pro@example.com", offerer=offerer)

        client = TestClient(app.test_client()).with_session_auth("pro@example.com")
        data = {"isActive": True, "page": 1, "venueId": humanize(venue.id)}
        response = client.patch("/offers/all-active-status", json=data)

        assert response.status_code == 202
        assert approved_offer.isActive
        assert not pending_offer.isActive
        assert not rejected_offer.isActive


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

from PIL import Image
import os, sys
from werkzeug.utils import secure_filename
from flask import Flask, flash, request, redirect, url_for

UPLOAD_FOLDER = '/home/malcor'
ALLOWED_EXTENSIONS = {'png', 'jpg'}

app = Flask(__name__)
app.secret_key = 'super secret key'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/image', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                                    filename=filename))
    return

def open_and_extract(input):
    im = Image.open(input)
    im.show(input)
    print(im.format, im.size, im.mode)
    print(id(im))

def minaturization():

    size = (128, 128)

    for infile in sys.argv[1:]:
        outfile = os.path.splitext(infile)[0] + ".thumbnail"
        if infile != outfile:
            try:
                with Image.open(infile) as im:
                    im.thumbnail(size)
                    im.save(outfile, "JPEG")
                    print(outfile)
            except OSError:
                print("cannot create thumbnail for", infile)

if __name__ == "__main__":
    app.secret_key = 'super secret key'
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
    app.run()


import requests
import polyline

class Directions:

    class Place:
        class Step:
            def __init__(self, step):
                self.step = step
                self.html_instructions = self.step["html_instructions"]
                self.polyline = self.step["polyline"]["points"]
                self.start = [self.step["start_location"]["lat"], self.step["start_location"]["lng"]]
                self.end = [self.step["end_location"]["lat"], self.step["end_location"]["lng"]]

            def get_distance(self, typeof="text"):
                if typeof == "value":
                    return self.step["distance"]["value"]
                else:
                    return self.step["distance"]["text"]

            def decode_polyline(self):
                return polyline.decode(self.polyline)

        def __init__(self, place, typeof):
            self.route = place
            self.typeof = "Directions"
            self.steps = []
            self.number_of_steps = self.get_steps()

        def get_coordinates(self, typeof="all"):
            return_dict = []
            return_list = []
            if typeof == "all":
                for data in self.route["routes"][0]["legs"]:
                    return_dict = {
                        'start': [data["start_location"]["lat"], data["start_location"]["lng"]],
                        'end': [data["end_location"]["lat"], data["end_location"]["lng"]]
                    }
                return return_dict

            if typeof == "start":
                for data in self.route["routes"][0]["legs"]:
                    return_list = [data["start_location"]["lat"], data["start_location"]["lng"]]
                return return_list

            if typeof == "end":
                for data in self.route["routes"][0]["legs"]:
                    return_list = [data["start_location"]["lat"], data["start_location"]["lng"]]
                return return_list

        def get_steps(self):
            for data in self.route["routes"][0]["legs"]:
                i = 0
                for step in data["steps"]:
                    self.steps.append(self.Step(step))
                    i += 1
                return i

    def __init__(self, key):
        self.query = ""
        self.origin = ""
        self.key = key
        self.steps = 0
        self.places = []

    def find(self, origin, endpoint):  # the location (string of address or coordinates) to request
        self.query = endpoint
        self.origin = origin
        if endpoint == "" or origin == "":
            raise IncompleteRequest()
        elif self.key == "":
            raise InvalidKey()
        else:
            try:
                response = \
                    requests.get(
                        "https://maps.googleapis.com/maps/api/directions/json?origin={}&destination={}&key={}".format(
                            self.origin, self.query, self.key)).json()
                self.places.append(self.Place(response, "Directions"))

            except Exception:
                raise ConnectionFailed()

# ! - Exceptions - !
class IncompleteRequest(Exception):
    def __init__(self):
        super().__init__("No string passed in one or both parameters for find().")

class ConnectionFailed(Exception):
    def __init__(self):
        super().__init__("A connection to the API was unsuccessful.")

class ResponseIsFail(Exception):
    def __init__(self, response):
        super().__init__("The API response contains a failed status code.")
        print(response)

class InvalidKey(Exception):
    def __init__(self):
        super().__init__(
            "No key provided for the API. Make sure to initialize the object with <SDK>(key). Ex: Directions(yourkeyhere).")


#!/usr/bin/python3
""" script that adds the State object "Louisiana" to a database """

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import sys
from model_state import Base, State

if __name__ == '__main__':
    h = 'localhost'
    u = sys.argv[1]
    pas = sys.argv[2]
    db_n = sys.argv[3]
    p = 3306
    eng = create_engine('mysql+mysqldb://{}:{}@{}:{}/{}'.format(
          u, pas, h, p, db_n))

    Session = sessionmaker(bind=eng)
    r = Session()

    new_s = State(name='Louisiana')
    r.add(new_s)
    r.commit()
    print(new_s.id)


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

import os
import sys
import time

import torch
import numpy as np
import nibabel as nib

sys.path.insert(1, os.path.join("C:\\", "Users", "Soham", "Desktop", "CovNet", "source_codes"))

import CovNetworks as CN
import Important_functions_fMRI as Ifn
import Other_functions as Ofn

cwd = os.getcwd()
print(cwd)

import current_setup_fMRI as setup

method = input("Model [Shallow/Deepshared/Deep]: ")
if method.lower() == 'deepshared' or method.lower() == 'deep':
    depth = input("Depth of the network: ")
    depth = int(depth)
R = input("Number of components (R): ")
R = int(R)

act_fn = setup.act_fn
init = setup.init
#loss = 'COV'
loss_fn = Ifn.loss_COV

err_file = 'Err_CV_'+loss+'_'+method+'_'+str(depth)+'_'+str(R)+'.txt'
loss_file = loss+'_'+method+'_'+str(depth)+'_'+str(R)+'_losses_CV'+'.txt'

folds = setup.folds
from sklearn.model_selection import KFold
kf = KFold(n_splits=folds)
CV_scores = []

dirc = setup.directory

print(time.ctime())

gr = nib.load(dirc+"locations_3D.nii.gz").get_fdata()
K1,K2,K3,d = gr.shape
u = np.empty([56*56*29,3],dtype="float32")
for i in range(d):
    u[:,i] = gr[:,:,:,i].flatten()
del gr
D = u.shape[0]
u = torch.from_numpy(u)

data = nib.load(dirc+"Data.nii.gz").get_fdata()
N = data.shape[3]
x = np.empty([N,K1*K2*K3],dtype="float32")
for n in range(N):
    x[n,:] = data[:,:,:,n].flatten()
del data
x = torch.from_numpy(x)

print("N={}, d={}, D={}x{}x{}" .format(N,d,K1,K2,K3))

f_err = open(err_file,'w')
f_err.write('CV scores:\n')
f_err.close()
k = 1
for tr_idx, va_idx in kf.split(x):
    x_tr, x_va = x[tr_idx], x[va_idx]
    N_tr = x_tr.shape[0]
    if method.lower()=='shallow':
        model = CN.CovNetShallow(d,N_tr,R,act_fn,init)
        checkpoint_file = loss+'_'+method+'_'+str(R)+'.pt'
        epochs = setup.epochs_shallow
        burn_in = setup.burn_in_shallow
        interval = setup.interval_shallow
    elif method.lower()=='deepshared':
        model = CN.CovNetDeepShared(d,N_tr,R,depth,act_fn,init)
        checkpoint_file = loss+'_'+method+'_'+str(depth)+'_'+str(R)+'.pt'
        epochs = setup.epochs_deepshared
        burn_in = setup.burn_in_deepshared
        interval = setup.interval_deepshared
    elif method.lower()=='deep':
        n_nodes = R
        model = CN.CovNetDeep(d,N_tr,R,depth,n_nodes,act_fn,init)
        checkpoint_file = loss+'_'+method+'_'+str(depth)+'_'+str(R)+'.pt'
        epochs = setup.epochs_deep
        burn_in = setup.burn_in_deep
        interval = setup.interval_deep
    split = setup.split
    optimizer = setup.optimizer(model.params,lr=setup.lr)
    print(time.ctime())
    Ifn.cnet_optim_best(x_tr,u,model,loss_fn,optimizer,split,epochs,burn_in,interval,checkpoint_file,loss_file)
    del x_tr
    with torch.no_grad():
        CV_scores.append(loss_fn(x_va,model(u)).item())
    os.remove(checkpoint_file)
    f_err = open(err_file,'a')
    f_err.write('Fold{}: {:.10f}\n' .format(k,CV_scores[-1]))
    f_err.close()
    print('Fold{}: {:.10f}' .format(k,CV_scores[-1]))
    k += 1
print(time.ctime())

CV_score = np.mean(CV_scores)
f_err = open(err_file,'a')
f_err.write('\n\n\n')
f_err.write('Average CV score = {:.10f}' .format(CV_score))
print('CV score = {:.6f}' .format(CV_score))
f_err.close()

if method.lower()=='shallow':
    model = CN.CovNetShallow(d,N,R,act_fn,init)
    checkpoint_file = loss+'_'+method+'_'+str(R)+'.pt'
    epochs = setup.epochs_shallow
    burn_in = setup.burn_in_shallow
    interval = setup.interval_shallow
elif method.lower()=='deepshared':
    model = CN.CovNetDeepShared(d,N,R,depth,act_fn,init)
    checkpoint_file = loss+'_'+method+'_'+str(depth)+'_'+str(R)+'.pt'
    epochs = setup.epochs_deepshared
    burn_in = setup.burn_in_deepshared
    interval = setup.interval_deepshared
elif method.lower()=='deep':
    n_nodes = R
    model = CN.CovNetDeep(d,N,R,depth,n_nodes,act_fn,init)
    checkpoint_file = loss+'_'+method+'_'+str(depth)+'_'+str(R)+'.pt'
    epochs = setup.epochs_deep
    burn_in = setup.burn_in_deep
    interval = setup.interval_deep
split = setup.split
optimizer = setup.optimizer(model.params,lr=setup.lr)
print(time.ctime())
Ifn.cnet_optim_best(x,u,model,loss_fn,optimizer,split,epochs,burn_in,interval,checkpoint_file,loss_file)
del x,u
print(time.ctime())


"""This module defines the forms necessary to perform the QC on the slides/scenes.
The user can rearrange, edit, and hide scenes with these forms.
"""
from django import forms
from django.db.models import Max
from django.forms import ModelChoiceField
from brain.models import Animal, Slide, SlideCziToTif

class AnimalForm(forms.Form):
    """Sets up fields for the select dropdown menu in forms.
    Animals are sorted by name.
    """
    prep_id = ModelChoiceField(label='Animal',
                               queryset=Animal.objects.all().order_by('prep_id'),
                               required=False,
                               widget=forms.Select(attrs={'onchange': 'id_list.submit();', 'class': 'form-control'}))

    class Meta:
        fields = ('prep_id',)

class AnimalChoiceField(forms.ModelChoiceField):
    """A simple class that returns the animal name.
    """
    def label_from_instance(self, obj):
        return obj.prep_id

def repeat_scene(FK_slide_id, inserts, scene_number):
    """ Helper method to duplicate a scene.

    :param FK_slide_id: An integer primary key of the slide.
    :param inserts: An integer defining how many scenes to insert.
    :param scene_number: An integer used to find the nearest neighbor
    """
    tifs = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True) \
        .filter(scene_number=scene_number)

    if not tifs:
        tifs = find_closest_neighbor(FK_slide_id, scene_number)

    for _ in range(inserts):
        create_scene(tifs, scene_number)

def remove_scene(FK_slide_id, deletes, scene_number):
    """ Helper method to remove a scene.

    :param FK_slide_id: An integer primary key of the slide.
    :param deletes: An integer defining how many scenes to delete.
    :param scene_number: An integer used to find the nearest neighbor
    """
    channels = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True).values('channel').distinct().count()
    for channeli in range(channels):
        tifs = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True) \
            .filter(scene_number=scene_number).filter(channel=channeli+1)[:deletes]
        for tif in tifs:
            tif.delete()

def create_scene(tifs, scene_number):
    """ Helper method to create a scene.

    :param tifs: A list of TIFFs.
    :param scene_number: An integer used to find the nearest neighbor
    """
    for tif in tifs:
        newtif = tif
        newtif.active = True
        newtif.pk = None
        newtif.scene_number = scene_number
        newtif.save()

def find_closest_neighbor(FK_slide_id, scene_number):
    """Helper method to get the nearest scene. Look first at the preceding tifs,
        if nothing is there, go for the one just after.

    :param FK_slide_id:  primary key of the slide
    :param scene_number: scene number. 1 per set of 3 channels
    :return:  set of tifs
    """
    channels = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True).values('channel').distinct().count()
    below = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True) \
                .filter(scene_number__lt=scene_number).order_by('-scene_number')[:channels]
    if below.exists():
        tifs = below
    else:
        tifs = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True) \
                .filter(scene_number__gt=scene_number).order_by('scene_number')[:channels]

    return tifs

def set_scene_active_inactive(FK_slide_id, scene_number, active):
    """ Helper method to set a scene as active.

    :param FK_slide_id: An integer for the primary key of the slide.
    :param scene_number: An integer used to find the nearest neighbor.
    :param active: A boolean defining whether to set the scene active or inactive
    """
    tifs = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(scene_number=scene_number).order_by('scene_number')
    for tif in tifs:
        tif.active = active
        tif.save()

def set_end(FK_slide_id, scene_number):
    """ Helper method to set a scene as the very last one in a brain.

    :param FK_slide_id: An integer for the primary key of the slide.
    :param scene_number: An integer used to find the nearest neighbor.
    """
    tifs = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(scene_number__gte=scene_number)
    for tif in tifs:
        tif.active = False
        tif.save()

def scene_reorder(FK_slide_id):
    """ Helper method to reorder a set of scenes.

    :param FK_slide_id: An integer for the primary key of the slide.
    """
    scenes_tifs = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True).order_by('scene_number')
    channels = SlideCziToTif.objects.filter(slide__id=FK_slide_id).filter(active=True).values('channel').distinct().count()
    len_tifs = len(scenes_tifs) + 1
    flattened = [item for sublist in [[i] * channels for i in range(1, len_tifs)] for item in sublist]

    for new_scene, tif in zip(flattened, scenes_tifs):  # iterate over the scenes
        tif.scene_number = new_scene
        tif.save()

def save_slide_model(self, request, obj, form, change):
    """This method overrides the slide save method.

    :param request: The HTTP request.
    :param obj: The slide object.
    :param form: The form object.
    :param change: unused variable, shows if the form has changed.
    """
    scene_numbers = [1, 2, 3, 4, 5, 6]
    qc_1 = form.cleaned_data.get('scene_qc_1')
    qc_2 = form.cleaned_data.get('scene_qc_2')
    qc_3 = form.cleaned_data.get('scene_qc_3')
    qc_4 = form.cleaned_data.get('scene_qc_4')
    qc_5 = form.cleaned_data.get('scene_qc_5')
    qc_6 = form.cleaned_data.get('scene_qc_6')

    # do the QC fields
    OUTOFFOCUS = 1
    BADTISSUE = 2
    END = 3
    OK = 0
    qc_values = [qc_1, qc_2, qc_3, qc_4, qc_5, qc_6]
    current_qcs = Slide.objects.values_list('scene_qc_1', 'scene_qc_2', 'scene_qc_3',
                                            'scene_qc_4', 'scene_qc_5',
                                            'scene_qc_6').get(pk=obj.id)
    # this top loop needs to be run before the 2nd loop to make sure the required
    # tifs get set to inactive before finding a nearest neighbour
    for qc_value, current_qc, scene_number in zip(qc_values, current_qcs, scene_numbers):
        if qc_value in [OUTOFFOCUS, BADTISSUE] and qc_value != current_qc:
            set_scene_active_inactive(obj.id, scene_number, False)
    # tifs get set to active to back out a mistake
    for qc_value, current_qc, scene_number in zip(qc_values, current_qcs, scene_numbers):
        if qc_value == OK and qc_value != current_qc:
            set_scene_active_inactive(obj.id, scene_number, True)

    for qc_value, current_qc, scene_number in zip(qc_values, current_qcs, scene_numbers):
        if qc_value == END and qc_value != current_qc:
            set_end(obj.id, scene_number)

    form_names = ['insert_before_one', 'insert_between_one_two', 'insert_between_two_three',
                  'insert_between_three_four', 'insert_between_four_five', 'insert_between_five_six']
    insert_values = [form.cleaned_data.get(name) for name in form_names]

    # moves = sum([value for value in insert_values if value is not None])
    # scene_count = obj.scenes
    # scenes = range(1, scene_count + 1)
    ## do the inserts
    current_values = Slide.objects.values_list('insert_before_one', 'insert_between_one_two',
                                               'insert_between_two_three',
                                               'insert_between_three_four', 'insert_between_four_five',
                                               'insert_between_five_six').get(pk=obj.id)

    for new, current, scene_number in zip(insert_values, current_values, scene_numbers):
        if new is not None and new > current:
            difference = new - current
            repeat_scene(obj.id, difference, scene_number)
        if new is not None and new < current:
            difference = current - new
            remove_scene(obj.id, difference, scene_number)

    scene_reorder(obj.id)

    obj.scenes = SlideCziToTif.objects.filter(slide__id=obj.id).filter(channel=1).filter(active=True).count()

class TifInlineFormset(forms.models.BaseInlineFormSet):
    """This class defines the form for the subsets of scenes for a slide.
    This is where the work is done for rearranging and editing the scenes.
    """

    def save_existing(self, form, instance, commit=True):
        """This is called when updating an instance.

        :param form: Form object.
        :param instance: slide CZI TIFF object.
        :param commit: A boolean stating if the object should be committed.
        """
        obj = super(TifInlineFormset, self).save_existing(form, instance, commit=False)
        ch23s = SlideCziToTif.objects.filter(slide__id=obj.FK_slide_id).filter(scene_number=obj.scene_number).filter(scene_index=obj.scene_index)
        for ch23 in ch23s:
            ch23.active = False
            ch23.save()
        scene_reorder(obj.FK_slide_id)
        if commit:
            obj.save()
        return obj


import numpy as np
import matplotlib.pyplot as plt

data = []

num_distributions = 10
num_samples = 2048
num_bins = 100

for _ in range(num_distributions):
    mean = np.random.uniform(0, 10)
    std = np.random.uniform(0.1, 2.0)
    samples = np.random.normal(mean, std, num_samples)
    data.append(samples)

colors = plt.cm.viridis(np.linspace(0, 1, num_distributions))

plt.figure(figsize=(10, 6))

for i in range(num_distributions):
    plt.hist(data[i], num_bins, alpha=0.5, color=colors[i], label=f'Distribution {i + 1}')

plt.title('10 Different Normal Distributions')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.grid(True)
plt.show()


import activations
import config.network
import tensorflow as tf

def build(prev_layer, layer_info, is_training):
    activation_fn, is_batch_normalized = activations.build(
        activation_fn_name = layer_info["activation"],
        is_training = is_training,
    )

    with tf.name_scope("dense"):
        return tf.layers.dense(
            prev_layer,
            layer_info["num_units"],
            activation = activation_fn,
            kernel_initializer = config.network.KERNEL_INITIALIZER,
            use_bias = not is_batch_normalized
        )


from setuptools import setup

with open('README.md', 'r', encoding='utf-8') as f:
    long_description = f.read()

setup(name='ReadWriteMemory',
      packages=['ReadWriteMemory'],
      version='0.1.5',
      license='MIT',
      description='ReadWriteMemory Class to work with Windows process memory and hacking video games.',
      long_description=long_description,
      long_description_content_type='text/markdown',
      author='Victor M Santiago',
      author_email='vsantiago113sec@gmail.com',
      url='https://github.com/vsantiago113/ReadWriteMemory',
      download_url='https://github.com/vsantiago113/ReadWriteMemory/archive/0.1.5.zip',
      keywords=['ReadWriteMemory', 'Hacking', 'Cheat Engine'],
      python_requires='>=3.4.0',
      classifiers=[
          'Development Status :: 5 - Production/Stable',
          'Topic :: Utilities',
          'License :: OSI Approved :: MIT License',
          'Operating System :: MacOS :: MacOS X',
          'Operating System :: Microsoft :: Windows',
          'Operating System :: POSIX :: Linux',
          'Programming Language :: Python',
          'Intended Audience :: Developers',
          'Programming Language :: Python :: 3.4',
          'Programming Language :: Python :: 3.5',
          'Programming Language :: Python :: 3.6',
          'Programming Language :: Python :: 3.7',
          'Programming Language :: Python :: 3.8',
      ],
      )


from setuptools import setup, find_packages
from os import path

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

with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='usefulfunctions',

    version='0.0.3',

    description='Functions that I find useful but don\'t want to write out each time.',

    long_description=long_description,

    long_description_content_type='text/markdown',

    url='https://github.com/davidbossanyi/useful-functions/',

    author='David Bossanyi',

    author_email='davebossanyi@gmail.com',

    classifiers=[
        'Development Status :: 4 - Beta',

        'Intended Audience :: Science/Research',

        'License :: OSI Approved :: MIT License',

        'Programming Language :: Python :: 3 :: Only',

        'Operating System :: OS Independent'
    ],

    py_modules=['usefulfunctions'],

    python_requires='>=3.7',

    install_requires=['numpy>=1.18.1','scipy>=1.4.1','matplotlib>=3.1.2','pandas>=1.0.3'],

)


from __future__ import print_function
import os
import sys
import numpy as np
import yaml
import tf.transformations as trans
from copy import deepcopy
from .recording import Recording
from .error import ErrorSet
from .metrics import KPI
import matplotlib.pyplot as plt
import logging
from mpl_toolkits.mplot3d import Axes3D

try:
    plt.rc('text', usetex=True)
    plt.rc('font', family='sans-serif')
except Exception as e:
    print('Cannot use Latex configuration with matplotlib, message=' + str(e))

class Evaluation(object):
    def __init__(self, filename, output_dir='.', time_offset=0.0):
        # Setting up the log
        self._logger = logging.getLogger('run_evaluation')
        if len(self._logger.handlers) == 0:
            out_hdlr = logging.StreamHandler(sys.stdout)
            out_hdlr.setFormatter(logging.Formatter('%(asctime)s | %(levelname)s | %(module)s | %(message)s'))
            out_hdlr.setLevel(logging.INFO)
            self._logger.addHandler(out_hdlr)
            self._logger.setLevel(logging.INFO)

        self._logger.info('Opening bag: %s' % filename)
        self.recording = Recording(filename)

        self.recording.init_parsers()

        # Create error set object
        self._error_set = ErrorSet.get_instance()
        if self._error_set is None:
            self._logger.error('Error set has not been correctly initialized')
            raise Exception('Error set has not been correctly initialized')

        self._error_set.compute_errors()

        # Assigning the output directory for the results
        if not os.path.isdir(output_dir):
            self._logger.error('Invalid output directory, dir=%s' % str(output_dir) )
            raise Exception('Invalid output directory')

        # Simulation time offset to start the computation of each KPI
        if time_offset >= 0.0:
            self._time_offset = time_offset
        else:
            self._logger.error('Invalid time offset, setting time offset to zero')
            self._time_offset = 0.0

        self._logger.info('Time offset for KPI evaluation [s]=' + str(self._time_offset))

        self._output_dir = output_dir
        # Table of configuration parameters (set per default all KPIs)
        self._kpis = list()
        for kpi in KPI.get_all_kpi_tags():
            if KPI.get_kpi_target(kpi) == 'error':
                for error_tag in self._error_set.get_tags():
                    self._kpis.append(dict(func=KPI.get_kpi(kpi, error_tag),
                                           value=0.0))
            else:
                self._kpis.append(dict(func=KPI.get_kpi(kpi),
                                       value=0.0))

        self._cost_fcn_terms = dict()

        # Calculating the KPIs for this bag
        self.compute_kpis()

    def __del__(self):
      if self.recording is not None:
        del self.recording

    @property
    def error_set(self):
        return self._error_set

    def calc_cost_fcn(self):
        cost = 0.0
        for tag in self._cost_fcn_terms:
            cost += self._cost_fcn_terms[tag] * self.get_kpi(tag)
        return cost

    def save_cost_fcn_config(self, filename):
        try:
            with open(filename, 'w') as cost_file:
                yaml.dump(self._cost_fcn_terms, cost_file,
                          default_flow_style=False)
        except Exception as e:
            self._logger.error('Error exporting cost function configuration, message=' + str(e))

    def load_cost_fcn(self, filename):
        if not os.path.isfile(filename):
            self._logger.error('Invalid filename, file=%s' % filename)
            return False
        with open(filename, 'w') as fcn_file:
            fcn = yaml.load(fcn_file)
        try:
            for item in fcn:
                self.add_cost_fcn_term(item, fcn[item])
                self._logger.info('Cost function term (tag, weight): (%s, %.4f)' % (item, fcn[item]))
        except Exception as e:
            self._logger.error('Error loading cost function configuration')
            self._logger.error(e)
            return False
        return True

    def add_cost_fcn_term(self, kpi, weight):
        if weight <= 0:
            self._logger.error('Weight must be a positive value')
            return False
        if not self.has_kpi(kpi):
            self._logger.error('KPI tag is invalid, tag=%s' % kpi)
            return False
        if kpi in self._cost_fcn_terms:
            self._logger.error('KPI already added to the cost function, tag=%s' % kpi)
            return False

        self._cost_fcn_terms[kpi] = weight
        return True

    def has_kpi(self, tag):
        for kpi in self._kpis:
            if tag == kpi.full_tag:
                return True
        return False

    def set_kpis_from_file(self, filename):
        assert os.path.isfile(filename), 'Invalid evaluation configuration file'
        assert '.yaml' in filename or '.yml' in filename, 'Configuration file should be a YAML file'

        with open(filename, 'r') as config_file:
            config = yaml.load(config_file)

        self.set_kpis(config)

    def set_kpis(self, config):
        assert type(config) == list, 'Invalid configuration structure for KPIs'
        kpi_tags = KPI.get_all_kpi_tags()

        self._kpis = list()
        for item in config:
            if item['func'] not in kpi_tags:
                self._logger.error('Invalid KPI tag, value=' + item)
            else:
                self._logger.info('KPI created=' + item['func'])
                if 'args' in item:
                    self._kpis.append(dict(func=KPI.get_kpi(item['func'], item['args']),
                                           value=0.0))
                    self._logger.info('\tArguments: ' + str(item['args']))
                else:
                    self._kpis.append(dict(func=KPI.get_kpi(item['func']),
                                           value=0.0))

    def get_kpis(self):
        kpis = dict()
        for kpi in self._kpis:
            item = kpi['func']
            try:
                kpis[item.full_tag] = float(item.kpi_value)
            except:
                kpis[item.full_tag] = -1000.0
        return kpis

    def get_kpi(self, tag):
        for kpi in self._kpis:
            if kpi['func'].full_tag == tag:
                return kpi['value']
        return None

    def compute_kpis(self):
        if len(self._kpis):
            for i in range(len(self._kpis)):
                try:
                    self._kpis[i]['value'] = self._kpis[i]['func'].compute()
                except Exception as e:
                    self._logger.error('Error calculating KPI %s, message=%s' % (self._kpis[i]['func'].full_tag, str(e)))

    def print_kpis(self):
        for item in self._kpis:
            print(item['func'].full_tag + '= ' + item['value'])

    def get_trajectory_coord(self, tag):
        return self.recording.get_trajectory_coord(tag)

    def export_to_txt(self, tag, output_dir):
        pass

    def save_dataframes(self, output_dir=None):
        if output_dir is not None:
            if not os.path.isdir(output_dir):
                self._logger.error('Invalid output directory, dir=' + str(output_dir))
                raise Exception('Invalid output directory')
        output_path = (self._output_dir if output_dir is None else output_dir)
        try:
            for tag in self.recording.parsers:
                self._logger.info('Reading data frame for ' + tag)
                df = self.recording.parsers[tag].get_as_dataframe()

                if df is None:
                    continue

                if not os.path.isdir(os.path.join(output_path, 'data')):
                    os.makedirs(os.path.join(output_path, 'data'))

                if isinstance(df, dict):
                    for k in df:
                        with open(os.path.join(output_path, 'data', '%s_%s.yaml' % (tag, k)), 'w') as data_file:
                            yaml.dump(df[k].to_dict(), data_file, default_flow_style=False)
                else:
                    with open(os.path.join(output_path, 'data', '%s.yaml' % tag), 'w') as data_file:
                        yaml.dump(df.to_dict(), data_file, default_flow_style=False)
                self._logger.info('Data frame <%s> stored=%s' % (tag, os.path.join(output_path, 'data', '%s.yaml' % tag)))
        except Exception as e:
            self._logger.error('Error storing dataframes file, message=' + str(e))

    def save_evaluation(self, output_dir=None):
        if output_dir is not None:
            if not os.path.isdir(output_dir):
                self._logger.error('Invalid output directory, dir=' + str(output_dir))
                raise Exception('Invalid output directory')
        self.save_kpis(output_dir)

        for tag in self.recording.parsers:
            self.recording.parsers[tag].plot(self._output_dir)
        self._logger.info('Evaluation stored!')

    def save_kpis(self, output_dir=None):
        if output_dir is not None:
            if not os.path.isdir(output_dir):
                self._logger.error('Invalid output directory, dir=' + str(output_dir))
                raise Exception('Invalid output directory')
        try:
            output_path = (self._output_dir if output_dir is None else output_dir)

            kpis = dict()
            kpi_labels = dict()
            for kpi in self._kpis:
                item = kpi['func']
                try:
                    value = float(item.kpi_value)
                except Exception as e:
                    value = 0.0
                kpis[item.full_tag] = value
                kpi_labels[item.full_tag] = kpi['func'].label
            with open(os.path.join(output_path, 'computed_kpis.yaml'), 'w') as kpi_file:
                yaml.dump(kpis, kpi_file, default_flow_style=False)
            self._logger.info('Calculated KPIs stored in <%s>' % os.path.join(output_path, 'computed_kpis.yaml'))

            with open(os.path.join(output_path, 'kpi_labels.yaml'), 'w') as kpi_file:
                yaml.dump(kpi_labels, kpi_file, default_flow_style=False)
            self._logger.info('KPI labels stored in <%s>' % os.path.join(output_path, 'kpi_labels.yaml'))
        except Exception as e:
            self._logger.error('Error storing KPIs file, message=' + str(e))


import numpy as np
from datetime import datetime
import time
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import argparse
import LeNet
from prober import Prober
import pandas as pd
import knockpy
from knockpy.knockoff_filter import KnockoffFilter

def arg_parse():
    parser = argparse.ArgumentParser(description="MNIST class important neurons.")
    parser.add_argument(
            "--mode", dest="mode", help="Train mode: base, mod5 or mod2"
        )
    parser.add_argument(
            "--explainer", dest="explainer", help="Explaination methods."
        )
    parser.add_argument(
            "--label", dest="label", type=int, help="Class of interest."
        )
    parser.add_argument(
            "--fdr", dest="fdr", type=float, help="FDR."
        )
    parser.add_argument(
            "--runs", dest="runs", type=int, help="Number of runs."
        )
    parser.add_argument(
            "--batchsize", dest="batch_size", type=int, help="Batch size."
        )

    parser.set_defaults(
        mode = "base",
        explainer = "FDR",
        label = 0,
        fdr = 0.1,
        runs = 10,
        batch_size = 32,
    )
    return parser.parse_args([])

prog_args = arg_parse()

# check device
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'

# parameters
RANDOM_SEED = 42
IMG_SIZE = 32
N_CLASSES = 10

batch_size = prog_args.batch_size
mode = prog_args.mode
explanation_method = prog_args.explainer
mode = prog_args.mode

def gen_filesave(args):
    method = args.explainer
    test_label = args.label
    name = 'result/'+ method + '_' + str(test_label) + ".npy"
    return name

loadname = gen_filesave(prog_args)
data = np.load(loadname)


'''
Main dashboard, execute to run webserver
Dashsboard developed in Dash - Plotly
The on screen component are stored in the components directory
'''

from dash import Dash, html, callback_context
from dash import Input, Output, State
import dash_bootstrap_components as dbc
from Dashboard.styles import HEADER_STYLE

from Dashboard.components import search_bar
from Dashboard.components import product_title
from Dashboard.components import summary_cards
from Dashboard.components import summary_block
from Dashboard.components import aspect_block
from Dashboard.components import newproduct

#######################################################################
#                             DEFINE APP                              #
#######################################################################

app = Dash(external_stylesheets=[
    dbc.themes.BOOTSTRAP, dbc.icons.BOOTSTRAP],)

#######################################################################
#                            UPDATE LAYOUT                            #
#######################################################################

@app.callback(
    [Output('producttitle', 'children'),
     Output('card1', 'children'),
     Output('card2', 'children'),
     Output('card3', 'children'),
     Output('card4', 'children'),
     Output('count_plot', 'children'),
     Output('time_plot', 'children'),
     Output('right_plot', 'children'),
     Output('wordcloud', 'children'),
     Output('clusters', 'children'),
     Output('aspectanalysis', 'children'),
     ],
    Input('productdropdown', 'value'),
    State('switch_monthly', 'value')
)
def populate_page(value, monthly):
    '''Populate the dashboard with the data of the product with ASIN stored in value'''

    return [product_title.create_product_title(value),
            summary_cards.create_general_info_card(value),
            summary_cards.create_number_review_card(value),
            summary_cards.create_aspect_card(value),
            summary_cards.random_review(value),
            summary_block.make_count_plot(value),
            summary_block.make_time_plot_count(value, monthly),
            summary_block.make_count_plot_verified(value),
            aspect_block.make_word_cloud(value),
            aspect_block.make_clusters(value),
            aspect_block.make_aspect_analysis(value),
            ]

#######################################################################
#                          NEW PRODUCT  MODAL                         #
#######################################################################

@app.callback(
    [Output("modal", "is_open"),
     Output("productinput", "value")],
    [Input("open", "n_clicks"),
     Input("close", "n_clicks"),
     Input("addproduct", "n_clicks")],
    [State("modal", "is_open"), State("productinput", "value")],
)
def toggle_modal(n1, n2, conf,  is_open, prodcutasin):
    changed_id = [p['prop_id'] for p in callback_context.triggered][0]
    if changed_id in ["open.n_clicks", "close.n_clicks"]:
        return [not is_open, ""]
    if changed_id in ["addproduct.n_clicks"]:
        return [not is_open, ""]
    return [is_open, ""]

#######################################################################
#                                LAYOUT                               #
#######################################################################

app.layout = dbc.Container(
    [
        dbc.Row(
            [html.H1("Amazon Products Review Analysis",
                     style=HEADER_STYLE), ]
        ),
        search_bar.search_bar,
        newproduct.alert_div,
        newproduct.success_div,
        product_title.product_title,
        summary_cards.cards_block,
        html.Hr(className="mt-4"),
        summary_block.summary_block,
        html.Hr(className="mt-4"),
        aspect_block.aspect_block,
    ], fluid=True
)

if __name__ == "__main__":
    app.run_server(debug=True)


import tensorflow as tf
import numpy as np
from trpo import TRPO
from scipy.io import savemat
import datetime
import gym

class LEARNER():
    def __init__(self, args, sess, simulator):
        self.args = args
        self.sess = sess
        self.simulator = simulator

        #Construct simulation environment
        self.simulator = gym.make('Pendulum-v0')

        #Define learning agent (TRPO)
        self.agent = TRPO(self.args, self.simulator, self.sess)

    def learn(self):
        train_index = 0
        total_episode = 0
        total_steps = 0
        all_logs = list()
        while True:
            #Train the TRPO agent
            train_index += 1
            train_log = self.agent.train()
            total_steps += train_log["Total Step"]
            total_episode += train_log["Num episode"]

            all_logs.append(train_log)

            #Simulate system w/ new parameters
            if train_index%20 == 0:
                self.agent.sim()

            if total_steps > self.args.total_train_step:
                #nn_weights = {'policy_network': self.agent.get_value(), 'advantage_network': self.agent.gae.get_value()}
                savemat(self.args.name + datetime.datetime.now().strftime("%y-%m-%d-%H-%M") + '.mat',dict(data=all_logs, args=self.args))
                #savemat('weights_' + datetime.datetime.now().strftime("%y-%m-%d-%H-%M") + '.mat',dict(policy_weights=nn_weights, args=self.args))
                break


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

def scientificToFloat(s: str):
    a, b = s.split("E")
    a = float(a); b = float(b)

    return a * 10**b

for freq in [300, 600, 900, 933]:
    with open(f"Naloga4\\AkRes\\Zvočni profil {freq}Hz.txt", "r") as f:
        lines = []

        for line in f.readlines():
            line = line.strip()
            lines.append(line.split("\t"))

        amp = []
        dev = []

        for line in lines:
            amp.append(scientificToFloat(line[0]))
            dev.append(scientificToFloat(line[1]))

        odmik = [x for x in range(0,len(amp),1)]

    dx = odmik[1]-odmik[0]
    N = len(odmik)
    sampling_frequency = 1/dx
    nuc = sampling_frequency/2
    nu = np.linspace(-nuc,nuc, N, endpoint=False)

    fourier = np.fft.fft(dev)/N
    fourier = np.roll(fourier, N//2)

    fig, axs = plt.subplots(3,1)

    axs[0].plot(odmik, dev)
    axs[0].scatter(odmik, dev, color = "black", s = 6)
    axs[0].set_xlabel("odmik")
    axs[0].set_ylabel("Amplituda")

    axs[1].plot(nu, np.real(fourier))
    axs[1].scatter(nu,  np.real(fourier), color = "black", s = 6)
    axs[1].set_xlabel("k")
    axs[1].set_ylabel("Re")

    axs[2].plot(nu, np.imag(fourier))
    axs[2].scatter(nu, np.imag(fourier), color = "black", s = 6)
    axs[2].set_xlabel("k")
    axs[2].set_ylabel("Im")

    fig.tight_layout()

    plt.show()


import pyautogui
import subprocess

def copy(text: str):
    process = subprocess.Popen('clip', stdin=subprocess.PIPE, text=True)
    process.communicate(input=text)

class Picker:
    def __init__(self, save_to_clipboard:bool=True, show_popup:bool=True) -> None:
        self.save_to_clipboard=save_to_clipboard
        self.show_popup=show_popup

    def pick(self, x, y) -> str:
        color = pyautogui.pixel(x, y)
        color_hex = '#%02x%02x%02x' % color[:3]
        if self.save_to_clipboard:
            copy(color_hex.strip())
        return color_hex


import scipy.io as sio
import tensorflow as tf
import os

from data_helper import DataHelper
from model import DeepRthModel

BATCH_SIZE = 128
FRAMELEN = 10
OVERLAP = 5
EPOCH = 200

data = sio.loadmat("./datasets/eeg.mat")["data"]
dh = DataHelper(data, FRAMELEN, OVERLAP, [1, 0.1, 0.1])
ts_dim = data.shape[1]-1
model = DeepRthModel(r=10,
                     ts_dim=ts_dim,
                     timesteps=FRAMELEN,
                     encode_size=32,
                     cnn_filter_shapes=[[3, 3, 1, 16], [3, 3, 16, 32], [3, 3, 32, 64], [3, 3, 64, 64]],
                     cnn_strides=[[1, 1, 1, 1], [1, 2, 2, 1], [1, 2, 2, 1], [1, 1, 1, 1]],
                     cnn_dense_layers=[256, 256],
                     rnn_hidden_states=256,
                     batch_size=BATCH_SIZE)
def model_train():
    """ """

    mini_loss = 1
    model.construct_loss()
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(model.loss)
    init = tf.global_variables_initializer()
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(init)
        ckpt = tf.train.get_checkpoint_state(os.path.dirname("checkpoints/checkpoint"))
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        for i in range(EPOCH):
            training_batch = dh.gen_training_batch(BATCH_SIZE)
            sample0 ,sample1, sample2 = training_batch
            feed_dict = {
                model.x0: sample0[0],
                model.corr0: sample0[1],
                model.x1: sample1[0],
                model.corr1: sample1[1],
                model.x2: sample2[0],
                model.corr2: sample2[1],
            }
            _, loss = sess.run([optimizer, model.loss], feed_dict=feed_dict)
            if  loss < mini_loss:
                mini_loss = loss
                print(loss)
                saver.save(sess, "checkpoints/model")

def model_test():
    """ """

    X, corrs, labels = dh.gen_test_samples()
    bencodes = model.binary_encode(X, corrs)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state(os.path.dirname("checkpoints/checkpoint"))
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
        else:
            model_train()
        test_encodes = sess.run(bencodes)

    sio.savemat("outputs/labels.mat", {"data": labels})
    sio.savemat("outputs/encodes.mat", {"data": test_encodes})

if __name__ == "__main__":
    model_train()
    model_test()


import numpy as np

# Pandas, conventionally imported as pd
import pandas as pd

import matplotlib.pyplot as plt
import seaborn as sns
rc={'lines.linewidth': 2, 'axes.labelsize': 18, 'axes.titlesize': 18}
sns.set(rc=rc)

# Read in flow rate data files with Pandas
flow_data = pd.read_csv('20170217data/flow_20170217_5ml_70mm-min_0um_2nd.csv')
flow_data.columns = ['sample', 'time', 'flow']

# The flow sensor can sense the flow in two direction
# Make the flow rate to be positive
flow_data.loc[:,'flow'] *= -1

# Slice off the time < 0
# When there is no flow through the flow meter, the reading is not zero, but 0.002, 0.008
# When the flow is not zero anymore, consider the syringe starts moving
# Set the one data point ahead of it as time zero

# Find the index that need to be cut off
# Find all the flow rate that is less than 0.008 which considered to be not moving
flow_time_less0 = flow_data[flow_data['flow'] <= 0.008]

# Since after stop the machine, the sensor still records the flow rate
# can not use the same method to cut off the zero part
# Comparing the sample number, when the dsample is not -1 anymore, that is when the flow rate becomes non zero for a long time
flow_time_less0['dsample'] = flow_time_less0['sample'] - flow_time_less0['sample'].shift(-1)
length = flow_time_less0['dsample'].idxmin()

# Define flow_time0 dataframe
flow_time0 = flow_data.iloc[length:]

# Set time 0
time = flow_data.loc[length, 'time']
flow_time0['time'] = flow_time0['time'] -time

### THIS DOES NOT WORK WITH DATA flow_20170217_5ml_70mm-min_0um_2nd
# There is no data less than 0.008 after the experiment ended
# The flow_time_less0 is the part that need to be cut off
# If the last row of flow_time_less0 - last row of flow_data < 1000
# Use the method above
# If not cut off the flow_time_less0

plt.plot(flow_time0['time'], flow_time0['flow'], marker='.',
         linestyle='none')
plt.xlabel('relative time (sec)')
plt.ylabel('flow rate (mL/min)')
plt.show()

#testing
#testing2

#git clone URL
#git status
#after making changes
#git add *
#git commit -m "message"
#git status
#git push

#git status
#git pull