from django.shortcuts import render, get_object_or_404
from django.http import Http404

from .models import Question

# Create your views here.

def index(request):
    try:
        latest_question_list = Question.objects.order_by('pub_date')[:5]
    except Question.DoesNotExist:
        raise Http404('Question does not exist')
    context = {
        'latest_question_list': latest_question_list,
    }
    return render(request, 'pools/index.html', context)

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    context = {
        'question': question
    }
    return render(request, 'pools/detail.html', context)

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    context = {
        'question': question
    }
    return render(request, 'pools/results.html', context)

def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    context = {
        'question': question
    }
    return render(request, 'pools/vote.html', context)


from functools import partial
from random import shuffle
from kivy.clock import Clock
from kivy.app import App
from kivy.lang import Builder

import ani_property
ani_property.install()

KV_CODE = r'''
GridLayout:
    rows: 4
    cols: 4
    padding: 20
    spacing: 20
'''

def shuffle_children(widget, dt):
    children = widget.children[:]
    widget.clear_widgets()
    shuffle(children)
    for c in children:
        widget.add_widget(c)

class SampleApp(App):
    def build(self):
        return Builder.load_string(KV_CODE)

    def on_start(self):
        from kivy.uix.button import Button
        from ani_property import AniMagnet

        grid = self.root
        for i in range(grid.rows * grid.cols):
            label = Button(text=str(i), font_size=50, opacity=0.5)
            magnet = AniMagnet()
            magnet.add_widget(label)
            grid.add_widget(magnet)

        Clock.schedule_interval(partial(shuffle_children, grid), 3)

if __name__ == '__main__':
    SampleApp().run()


# -*- encoding: utf-8 -*-

import requests
from scdl import CLIENT_ID

class Client():

    def get_collection(self, url, token, maxpage):
        params = {
            'client_id': CLIENT_ID,
            'linked_partitioning': '1',
        }
        if token:
            params['oauth_token'] = token
        resources = list()
        count = 0
        while url and count < maxpage:
            response = requests.get(url,
                headers={
                "Sec-Fetch-Mode":"cors",
                "Origin": "https://soundcloud.com",
                "Authorization": "OAuth {}".format(token),
                "Content-Type": "application/json",
                "Accept": "application/json, text/javascript, */*; q=0.1",
                "Referer": "https://soundcloud.com/",
                "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
                "DNT": "1",
                })
            response.raise_for_status()
            json_data = response.json()
            if 'collection' in json_data:
                resources.extend(json_data['collection'])
            else:
                resources.extend(json_data)
            if 'next_href' in json_data:
                url = json_data['next_href']
                count += 1
            else:
                url = None
        return resources


# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****

bl_info = {
    "name": "Surface Heat Diffuse Skinning",
    "author": "mesh online",
    "version": (3, 4, 2),
    "blender": (2, 80, 0),
    "location": "View3D > UI > Mesh Online",
    "description": "Surface Heat Diffuse Skinning",
    "warning": "",
    "wiki_url": "http://www.mesh-online.net/vhd.html",
    "category": "Object"
}

import bpy
import sys
import os
import time
import platform
from subprocess import PIPE, Popen
from threading  import Thread
from bpy.props import *
from queue import Queue, Empty

class SFC_OT_ModalTimerOperator(bpy.types.Operator):
    """Operator which runs its self from a timer"""
    bl_idname = "wm.surface_heat_diffuse"
    bl_label = "Surface Heat Diffuse Skinning"
    bl_options = {'REGISTER', 'UNDO'}

    _timer = None
    _pid = None
    _queue = None

    _objs = []
    _permulation = []
    _selected_indices = []
    _selected_group_index_weights = []

    _start_time = None

    def write_bone_data(self, obj, filepath):
        f = open(filepath, 'w', encoding='utf-8')

        f.write("# surface heat diffuse bone export.\n")

        amt = obj.data
        bpy.ops.object.mode_set(mode='EDIT')
        for bone in amt.edit_bones:
            if bone.use_deform:
                world_bone_head = obj.matrix_world @ bone.head
                world_bone_tail = obj.matrix_world @ bone.tail
                f.write("b,{},{},{},{},{},{},{}\n".format(
                bone.name.replace(",", "\\;"), world_bone_head[0], world_bone_head[1], world_bone_head[2],
                world_bone_tail[0], world_bone_tail[1], world_bone_tail[2]))
        bpy.ops.object.mode_set(mode='OBJECT')

        f.close()

    def write_mesh_data(self, objs, filepath):
        f = open(filepath, 'w', encoding='utf-8')

        f.write("# surface heat diffuse mesh export.\n")

        vertex_offset = 0
        for obj in objs:
            for v in obj.data.vertices:
                world_v_co = obj.matrix_world @ v.co
                f.write("v,{},{},{}\n".format(world_v_co[0], world_v_co[1], world_v_co[2]))

            for poly in obj.data.polygons:
                f.write("f");
                for loop_ind in poly.loop_indices:
                    vert_ind = obj.data.loops[loop_ind].vertex_index
                    f.write(",{}".format(vertex_offset + vert_ind))
                f.write("\n")

            vertex_offset += len(obj.data.vertices)

        f.close()

    def read_weight_data(self, objs, filepath):
        # make permulation for all vertices
        vertex_offset = 0;
        for obj in objs:
            for index in range(len(obj.data.vertices)):
                self._permulation.append((vertex_offset + index, index, obj))
            vertex_offset += len(obj.data.vertices)

        if bpy.context.scene.surface_protect:
            for index in range(len(objs)):
                obj = objs[index]
                # get selected vertex indices
                self._selected_indices.append([i.index for i in obj.data.vertices if i.select])
                self._selected_group_index_weights.append([])

                # push protected vertices weight
                for vert_ind in self._selected_indices[index]:
                    for g in obj.data.vertices[vert_ind].groups:
                        self._selected_group_index_weights[index].append((obj.vertex_groups[g.group].name, vert_ind, g.weight))

        f = open(filepath, 'r', encoding='utf-8')

        bones = []
        for line in f:
            if len(line) == 0:
                continue
            tokens = line.strip("\r\n").split(",")
            if tokens[0] == "b":
                group_name = tokens[1].replace("\\;", ",")
                bones.append(group_name)
                for obj in objs:
                    #check for existing group with the same name
                    if None != obj.vertex_groups.get(group_name):
                        group = obj.vertex_groups[group_name]
                        obj.vertex_groups.remove(group)
                    obj.vertex_groups.new(name = group_name)
            if tokens[0] == "w":
                group_name = bones[int(tokens[2])]
                index = int(tokens[1])
                vert_ind = self._permulation[index][1]
                weight = float(tokens[3])
                obj = self._permulation[index][2]
                # protect vertices weight
                if bpy.context.scene.surface_protect and vert_ind in self._selected_indices[objs.index(obj)]:
                    continue
                obj.vertex_groups[group_name].add([vert_ind], weight, 'REPLACE')

        f.close()

        if bpy.context.scene.surface_protect:
            for index in range(len(objs)):
                obj = objs[index]
                # pop protected vertices weight
                for (group_name, vert_ind, weight) in self._selected_group_index_weights[index]:
                    obj.vertex_groups[group_name].add([vert_ind], weight, 'REPLACE')

    def modal(self, context, event):
        if event.type == 'ESC':
            self._pid.terminate()
            return self.cancel(context)

        if event.type == 'TIMER':
            # background task is still running
            if None == self._pid.poll():
                # read line without blocking
                try:  rawline = self._queue.get_nowait()
                except Empty:
                    pass
                else:
                    line = rawline.decode().strip("\r\n")
                    self.report({'INFO'}, line)
            else:
                # background task finished running
                self.read_weight_data(self._objs, os.path.join(os.path.dirname(__file__), "data", "untitled-weight.txt"))
                running_time = time.time() - self._start_time
                self.report({'INFO'}, "".join(("Complete, ", "running time: ", \
                str(int(running_time / 60))," minutes ", str(int(running_time % 60)), " seconds")))
                # bind meshes to the armature
                bpy.ops.object.parent_set(type='ARMATURE')
                return self.cancel(context)

        return {'RUNNING_MODAL'}

    def execute(self, context):
        arm_count = 0
        obj_count = 0
        for ob in bpy.context.selected_objects:
            if 'ARMATURE' == ob.type:
                arm_count += 1
            if 'MESH' == ob.type:
                obj_count += 1
        if not (context.mode == 'OBJECT' and arm_count == 1 and obj_count >= 1):
            self.report({'ERROR'}, "Please select one armature and at least one mesh in 'OBJECT' mode, then try again.")
            return {'CANCELLED'}

        self._objs = []
        self._permulation = []
        self._selected_indices = []
        self._selected_group_index_weights = []

        arm = None
        objs = []

        # get armature and mesh
        for ob in bpy.context.selected_objects:
            if 'ARMATURE' == ob.type:
                arm = ob
            if 'MESH' == ob.type:
                objs.append(ob)

        # sort meshes by name
        objs.sort(key=lambda obj:obj.name);
        # save the reference for later use
        self._objs = objs

        for obj in objs:
            # focus on the mesh
            bpy.context.view_layer.objects.active = obj
            # synchronize data
            bpy.ops.object.mode_set(mode='OBJECT')

        # write mesh data
        self.write_mesh_data(objs, os.path.join(os.path.dirname(__file__), "data", "untitled-mesh.txt"))

        # we must focus on the armature before we can write bone data
        bpy.context.view_layer.objects.active = arm
        # synchronize data
        bpy.ops.object.mode_set(mode='OBJECT')

        # write bone data
        self.write_bone_data(arm, os.path.join(os.path.dirname(__file__), "data", "untitled-bone.txt"))

        # do voxel skinning in background
        ON_POSIX = 'posix' in sys.builtin_module_names

        # chmod
        if ON_POSIX:
            os.chmod(os.path.join(os.path.dirname(__file__), "bin", platform.system(), "shd"), 0o755)

        def enqueue_output(out, queue):
            for line in iter(out.readline, b''):
                queue.put(line)
            out.close()

        executable_path = None
        if platform.system() == 'Windows':
            if platform.machine().endswith('64'):
                executable_path = os.path.join(os.path.dirname(__file__), "bin", platform.system(), "x64", "shd")
            else:
                executable_path = os.path.join(os.path.dirname(__file__), "bin", platform.system(), "x86", "shd")
        else:
            executable_path = os.path.join(os.path.dirname(__file__), "bin", platform.system(), "shd")

        self._pid = Popen([executable_path,
                        "untitled-mesh.txt",
                        "untitled-bone.txt",
                        "untitled-weight.txt",
                        str(context.scene.surface_resolution),
                        str(context.scene.surface_loops),
                        str(context.scene.surface_samples),
                        str(context.scene.surface_influence),
                        str(context.scene.surface_falloff),
                        context.scene.surface_sharpness,
                        "y" if context.scene.detect_surface_solidify else "n"],
                        cwd = os.path.join(os.path.dirname(__file__), "data"),
                        stdout = PIPE,
                        bufsize = 1,
                        close_fds = ON_POSIX)

        self._queue = Queue()
        t = Thread(target=enqueue_output, args=(self._pid.stdout, self._queue))
        t.daemon = True
        t.start()

        self._start_time = time.time()
        # start timer to poll data
        self._timer = context.window_manager.event_timer_add(0.1, window=context.window)
        context.window_manager.modal_handler_add(self)

        return {'RUNNING_MODAL'}

    def cancel(self, context):
        # remove timer
        context.window_manager.event_timer_remove(self._timer)
        self._objs = []
        self._permulation = []
        self._selected_indices = []
        self._selected_group_index_weights = []
        return {'CANCELLED'}

def init_properties():
    bpy.types.Scene.surface_resolution = IntProperty(
        name = "Voxel Resolution",
        description = "Maximum voxel grid size",
        default = 128,
        min = 32,
        max = 1024)

    bpy.types.Scene.surface_loops = IntProperty(
        name = "Diffuse Loops",
        description = "Heat diffuse pass = Voxel Resolution * Diffuse Loops",
        default = 5,
        min = 1,
        max = 9)

    bpy.types.Scene.surface_samples = IntProperty(
        name = "Sample Rays",
        description = "Ray samples count",
        default = 64,
        min = 32,
        max = 128)

    bpy.types.Scene.surface_influence = IntProperty(
        name = "Influence Bones",
        description = "Max influence bones per vertex, please decrease the value (such as 4) for mobile devices",
        default = 8,
        min = 1,
        max = 128)

    bpy.types.Scene.surface_falloff = FloatProperty(
        name = "Diffuse Falloff",
        description = "Heat diffuse falloff",
        default = 0.2,
        min = 0.01,
        max = 0.99)

    bpy.types.Scene.surface_protect = BoolProperty(
        name = "Protect Selected Vertex Weight",
        description = "Protect selected vertex weight",
        default = False)

    bpy.types.Scene.surface_sharpness = EnumProperty(
        name = "Edges",
        description = "Edges",
        items = [
	('1','Soft','Soft Curvature'),
	('2','Normal','Normal Curvature'),
	('3','Sharp','Sharp Curvature'),
	('4','Sharpest','Sharpest Curvature')],
	default = '3')

    bpy.types.Scene.detect_surface_solidify = BoolProperty(
        name = "Detect Solidify",
        description = "Detect solidified clothes, if you enable this option, make sure that all bones are in the charecter's volume, otherwise, the result may be wrong",
        default = False)

def clear_properties():
    props = ["surface_resolution",
    "surface_samples",
    "surface_falloff",
    "surface_loops",
    "surface_influence",
    "surface_protect"]

    for p in props:
        if p in bpy.types.Scene.bl_rna.properties:
            exec("del bpy.types.Scene." + p)

class SFC_PT_SurfaceHeatDiffuseSkinningPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Surface Heat Diffuse Skinning"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Mesh Online'

    @classmethod
    def poll(self, context):
        return True

    def draw(self, context):
        layout = self.layout

        layout.prop(context.scene, 'surface_resolution', icon='BLENDER', toggle=True)
        layout.prop(context.scene, 'surface_loops', icon='BLENDER', toggle=True)
        layout.prop(context.scene, 'surface_samples', icon='BLENDER', toggle=True)
        layout.prop(context.scene, 'surface_influence', icon='BLENDER', toggle=True)
        layout.prop(context.scene, 'surface_falloff', icon='BLENDER', toggle=True)
        layout.prop(context.scene, 'surface_sharpness')
        layout.prop(context.scene, 'surface_protect')
        layout.prop(context.scene, 'detect_surface_solidify')

        row = layout.row()
        row.operator("wm.surface_heat_diffuse")

def register():
    bpy.utils.register_class(SFC_PT_SurfaceHeatDiffuseSkinningPanel)
    bpy.utils.register_class(SFC_OT_ModalTimerOperator)
    init_properties()

def unregister():
    bpy.utils.unregister_class(SFC_PT_SurfaceHeatDiffuseSkinningPanel)
    bpy.utils.unregister_class(SFC_OT_ModalTimerOperator)
    clear_properties()

if __name__ == "__main__":
    register()


# -*- coding: utf-8 -*-
import os
from sys import path

from django.conf.global_settings import (TEMPLATE_CONTEXT_PROCESSORS,
                                         STATICFILES_FINDERS)

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
path.append(os.path.join(BASE_DIR, 'apps'))

SECRET_KEY = '_c#^&2xzwd@xt@i2b5kftn+*-9$t&l+bg9&zb3@^jq)&^s38*d'

DEBUG = False

TEMPLATE_DEBUG = True

# Application definition
DJANGO_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
)

THIRD_PARTY_APPS = (
    'compressor',
    # 'south',
    'typogrify',
    'bourbon',
    'meta',
)

LOCAL_APPS = (
    'usrs',
    'wknd',
)

INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS += (
    'django.core.context_processors.request',
)

ROOT_URLCONF = 'wknd_project.urls'

# Internationalization
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Australia/Adelaide'

USE_I18N = False

USE_L10N = True

USE_TZ = False

STATIC_URL = '/s/'

STATIC_ROOT = os.path.join(BASE_DIR, '../static_root')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_FINDERS += (
    'compressor.finders.CompressorFinder',
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

# Django compressor
COMPRESS_PRECOMPILERS = (
    ('text/x-scss', 'django_libsass.SassCompiler'),
)

# Django extensions.
GRAPH_MODELS = {
    'all_applications': True,
    'group_models': True,
}

# WKND defaults
#AUTH_PROFILE_MODULE = 'usrs.Profile'
APPLICATION_PER_DAY_LIMIT = 2
LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'

# Django Meta
META_SITE_PROTOCOL = 'http'
META_SITE_DOMAIN = 'wkndcrew.com'
META_SITE_TYPE = 'website'
META_DEFAULT_KEYWORDS = META_INCLUDE_KEYWORDS = ['events', 'South Australia', 'Adelaide', 'WKND crew']
META_USE_OG_PROPERTIES = True
META_USE_TWITTER_PROPERTIES = True


"""DeviceManagement URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url,patterns
from django.contrib import admin
from Dmanage import views

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^borrowDeviceForm/$',views.borrowDeviceForm,name='borrowDeviceForm'),
    url(r'^return_device/$',views.return_device,name='returnDevice'),
    url(r'^list/$',views.list,name='list'),
    url(r'^list/(?P<device_sn_slug>\w+)/history/$',views.device_history,name='device_history'),
    url(r'^list/data$',views.list_data,name='list_data'),
    url(r'^list/(?P<device_sn_slug>\w+)/history/data$',views.device_history_data,name='device_history_data'),
    url(r'^login/$', views.user_login, name='login'),
    url(r'^logout/$', views.user_logout, name='logout'),
]


import h5py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from mpl_toolkits.mplot3d import Axes3D

from sklearn.metrics import roc_auc_score, roc_curve
import numpy as np
from tensorflow.keras import datasets, layers, models
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report

from ocnn import OneClassNeuralNetwork

def main():
    data = h5py.File('Data/http.mat', 'r')

    (x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()

    X = np.array(x_train, dtype=np.float32).T

    """
    Mapping derived from http://odds.cs.stonybrook.edu/smtp-kddcup99-dataset/ and http://odds.cs.stonybrook.edu/http-kddcup99-dataset/
    """
    feature_index_to_name = {0: "duration",
                             1: "src_bytes",
                             2: "dst_bytes"}

    num_features = 32
    num_hidden = 32
    r = 1.0
    epochs = 10
    nu = 0.001

    oc_nn = OneClassNeuralNetwork(num_features, num_hidden, r)
    model, history = oc_nn.train_model(x_train, epochs=epochs, nu=nu)

    plt.style.use("ggplot")
    plt.figure()
    plt.plot(history.epoch, history.history["loss"], label="train_loss")
    plt.plot(history.epoch, history.history["quantile_loss"], label="quantile_loss")
    plt.plot(history.epoch, history.history["r"], label="r")

    plt.title("OCNN Training Loss")
    plt.xlabel("Epoch")
    plt.ylabel("Loss")
    plt.legend(loc="upper right")

    y_pred = model.predict(x_test)

    roc_auc_score(y_test, y_pred)

if __name__ == "__main__":
    main()
    exit()


from flask_app.config.mysqlconnection import connectToMySQL
from flask import flash
from flask import re	# the regex module
# create a regular expression object that we'll use later
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$')

class User:
    def __init__( self , data ):
        self.id = data['id']
        self.name = data['name']
        self.location = data['location']
        self.language = data['language']
        self.comment = data['comment']
        self.created_at = data['created_at']
        self.updated_at = data['updated_at']
    # Now we use class methods to query our database

    # Now we use class methods to query our database
    @classmethod
    def get_all(cls):
        query = "SELECT * FROM dojo_survey;"
        # make sure to call the connectToMySQL function with the schema you are targeting.
        results = connectToMySQL('dojo_survey_schema').query_db(query)
        # Create an empty list to append our instances of friends
        users = []
        # Iterate over the db results and create instances of friends with cls.
        for user in results:
            users.append( cls(user) )
        return users

    # GET A SPECIFIC USER
    @classmethod
    def get_user_by_id(cls, data):
        query = "SELECT * FROM dojo_survey WHERE dojo_survey.id = %(id)s;"
        results = connectToMySQL('dojo_survey_schema').query_db(query, data)
        if results:
            return results[0]
        return False

    #CREATE
    @classmethod
    def save(cls, data ):
        query = "INSERT INTO dojo_survey ( name , location , language , comment, created_at, updated_at ) VALUES ( %(name)s , %(location)s , %(language)s ,%(comment)s , NOW() , NOW() );"
        # data is a dictionary that will be passed into the save method from server.py
        return connectToMySQL('dojo_survey_schema').query_db( query, data )

    @staticmethod
    def validate_user(user):
        is_valid = True # we assume this is true
        if len(user['name']) < 3:
            flash("Name must be at least 3 characters.", "firstNameRegister")
            is_valid = False
        if not user.get('location'):
              flash("You must select a location.", "locationRegister")
              is_valid = False
        if not user.get('language'):
             flash("You must select a language.", "languageRegister")
             is_valid = False
        if len(user['comment']) < 3:
            flash("Comment must be at least 3 characters.", "commentRegister")
            is_valid = False

        # test whether a field matches the pattern
        if not EMAIL_REGEX.match(user['email']):
            flash("Invalid email address!")
            is_valid = False

        return is_valid


#!/usr/bin/python3

import numpy, tqdm, json, os, random
import scipy.signal

import util

import speech_features

RATE = 8000
NUMCEP = 16
CLASSES = 45

LENGTH = 4000
DELTA = 2000
LABEL_SCALE = 100

LABEL_SAVE_JSON = "switchboard-labels.json"
OUT_FILE = "melspecs-switchboard.npy"
EMPTY = "<EMPTY>"
SIL = "SIL"
SIL_DROPOUT = 0.5

def load(specf):
    specf = os.path.join(specf, OUT_FILE)
    fragfile = util.FragmentedFile(specf)
    return fragfile.load()

def view(specf):
    SAMPLES = 5

    import matplotlib
    matplotlib.use("agg")
    from matplotlib import pyplot

    data = load(specf)

    fig, axes = pyplot.subplots(nrows=2, ncols=SAMPLES)
    fig.set_size_inches(18, 6)

    kmap, imap = load_label_map(os.path.join(specf, LABEL_SAVE_JSON))
    size = len(kmap)

    for i, (x, y, l) in zip(range(SAMPLES), data):
        axes[0, i].imshow(x.T, cmap="hot", interpolation="bicubic", aspect="auto")
        y = util.onehot(y, size).T
        for j in range(len(y)):
            axes[1, i].plot(y[j])
        title = [imap[i] for i in l if i]
        axes[0, i].set_title(", ".join(title))

    pyplot.savefig("switchboard-mfcc-samples.png", bbox_inches="tight")

def create_spectrograms(dataf):
    data = list(_load(dataf))

    Xa = []
    Xb = []
    ya = []
    yb = []
    la = []
    lb = []
    all_labels = set()

    for num, rate, waveA, waveB, pA, pB, sA, sB in tqdm.tqdm(data, desc="Processing data", ncols=80):
        assert rate == RATE
        waveA = remove_noise(waveA)
        waveB = remove_noise(waveB)
        yA = match_labels(waveA, pA)
        yB = match_labels(waveB, pB)

        for wavA, slcA, slcy in slice_step(waveA, yA, pA, LENGTH, DELTA):
            if keep_slice(slcA):
                melA = convert_spectrogram(wavA)
                Xa.append(melA)
                ya.append(slcA)
                la.append(slcy)
                all_labels.update(slcA)

        for wavB, slcB, slcy in slice_step(waveB, yB, pB, LENGTH, DELTA):
            if keep_slice(slcB):
                melB = convert_spectrogram(wavB)
                Xb.append(melB)
                yb.append(slcB)
                lb.append(slcy)
                all_labels.update(slcB)

    print('''
    ***

    Skipped %d files because they were shorter than 1 second.

    ***
    ''' % SKIPPED)

    all_labels = sorted(all_labels)
    assert all_labels[0] == EMPTY
    assert len(all_labels) == CLASSES + 1

    DATA_DIR = os.path.dirname(dataf)

    label_file = os.path.join(DATA_DIR, LABEL_SAVE_JSON)
    save_label_map(all_labels, label_file)
    keymap, idxmap = load_label_map(label_file)
    ya = convert_key2idx(keymap, ya)
    yb = convert_key2idx(keymap, yb)
    la = convert_key2idx(keymap, la)
    lb = convert_key2idx(keymap, lb)
    assert len(Xa) == len(ya) == len(la)
    assert len(Xb) == len(yb) == len(lb)

    out_file = os.path.join(DATA_DIR, OUT_FILE)
    X = Xa + Xb
    Y = ya + yb
    L = la + lb
    assert len(X) == len(Y) == len(L)
    L = pad_fitlargest(L)

    fragfile = util.FragmentedFile(out_file)
    fragfile.dump(len(X), zip(X, Y, L))

# === PRIVATE ===

SKIPPED = 0

def pad_fitlargest(labels):
    "Because the first class is EMPTY, we can use that as padding."
    longest = max(map(len, labels))
    print("Labels padded to length: %d" % longest)
    def pad(arr):
        out = numpy.zeros(longest).astype(numpy.int32)
        out[:len(arr)] = arr
        return out
    return list(map(pad, labels))

def keep_slice(slc):
    if all([v==SIL for v in slc]):
        return random.random() > SIL_DROPOUT
    else:
        return True

def slice_step(wav, lab, phns, length, step):
    if len(wav) == len(lab) and len(wav) > length:

        def locate_phns(i):
            out = []
            for name, start, end, pid in phns:
                if start > end:
                    start, end = end, start
                if start >= i+length:
                    break
                elif end < i:
                    continue
                else:
                    out.append(name)
            return out

        d, r = divmod(len(wav)-length, step)
        for i in range(0, d*step, step):
            yield wav[i:i+length], lab[i:i+length][::LABEL_SCALE], locate_phns(i)
        if r:
            yield wav[-length:], lab[-length:][::LABEL_SCALE], locate_phns(len(wav)-length)
    else:
        global SKIPPED
        SKIPPED += 1

def remove_noise(data):
    b, a = scipy.signal.butter(2, 40/(8000/2), btype="highpass")
    data = scipy.signal.lfilter(b, a, data)
    return data

def convert_key2idx(keymap, y):
    out = []
    for arr in tqdm.tqdm(y, desc="Converting labels to ints", ncols=80):
        out.append(numpy.array([keymap[v] for v in arr]))
    return out

def save_label_map(labels, fname):
    with open(fname, "w") as f:
        json.dump(labels, f)

def load_label_map(fname):
    with open(fname, "r") as f:
        labels = json.load(f)
        print("Classes: %d" % (len(labels)-1))
        assert CLASSES + 1 == len(labels)
        idxmap = dict(enumerate(labels))
        keymap = {k:i for i, k in idxmap.items()}
        return keymap, idxmap

def _load(dataf):
    with open(dataf, "rb") as f:
        with tqdm.tqdm(desc="Loading %s" % dataf, ncols=80) as bar:
            while True:
                try:
                    yield numpy.load(f)
                    bar.update()
                except OSError:
                    break

def convert_spectrogram(wav):
    return speech_features.mfcc(wav, samplerate=RATE, numcep=NUMCEP)

def match_labels(wav, phns):
    y = [EMPTY] * len(wav)
    for name, start, end, pid in phns:
        if start > end:
            start, end = end, start
        y[start:end] = [name] * (end-start)
    return y

@util.main(__name__)
def main(fname, sample=0):
    sample = int(sample)
    if sample:
        view(fname)
    else:
        create_spectrograms(fname)


import unittest
import matplotlib.pyplot as plt

class TestCreateParticles(unittest.TestCase):
    def test_create_particles(self):
        from src.dataio import GridIO, FlowIO
        from src.create_particles import Particle, LaserSheet, CreateParticles

        # Read-in the grid and flow file
        grid = GridIO('../data/shocks/shock_test.sb.sp.x')
        grid.read_grid()
        grid.compute_metrics()
        flow = FlowIO('../data/shocks/shock_test.sb.sp.q')
        flow.read_flow()

        # Set particle data
        p = Particle()
        p.min_dia = 144e-9
        p.max_dia = 573e-9
        p.mean_dia = 281e-9
        p.std_dia = 97e-9
        p.density = 810
        p.n_concentration = 5000
        p.compute_distribution()
        # print(p.particle_field)

        # Read-in the laser sheet
        laser = LaserSheet(grid)
        laser.position = 0.0009
        laser.thickness = 0.0001  # Adjusted based on grid thickness
        laser.pulse_time = 1e-7
        laser.compute_bounds()
        # print(laser.width)

        # Create particle locations array
        ia_bounds = [None, None, None, None]
        loc = CreateParticles(grid, flow, p, laser, ia_bounds)
        loc.ia_bounds = [0, 0.003, 0, 0.001]
        loc.in_plane = 90
        loc.compute_locations()
        loc.compute_locations2()

        # Sample code to plot particle locations and relative diameters
        _in_plane = int(p.n_concentration * loc.in_plane * 0.01)
        # plot in-plane particle locations
        plt.scatter(loc.locations[:_in_plane, 0], loc.locations[:_in_plane, 1],
                    s=10*loc.locations[:_in_plane, 3]/p.min_dia, c='g')
        # plot out-of-plane locations
        plt.scatter(loc.locations[_in_plane:, 0], loc.locations[_in_plane:, 1],
                    s=10*loc.locations[_in_plane:, 3]/p.min_dia, c='r')
        plt.xlim([-0.0001, 0.004])
        plt.ylim([0, 0.0019])

        # plt.show()

        # plot in-plane particle locations
        plt.figure()
        plt.scatter(loc.locations2[:_in_plane, 0], loc.locations2[:_in_plane, 1],
                    s=10 * loc.locations2[:_in_plane, 3] / p.min_dia, c='g')
        # plot out-of-plane locations
        plt.scatter(loc.locations2[_in_plane:, 0], loc.locations2[_in_plane:, 1],
                    s=10 * loc.locations2[_in_plane:, 3] / p.min_dia, c='r')
        plt.xlim([-0.0001, 0.004])
        plt.ylim([0, 0.0019])

        plt.show()

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


# 8*8 + 입력좌표에 대해 검사하는 기능 만들기

n, m = map(int, input().split());

arr = list();
for i in range(0, n):
    arr.append(input());

rtn2 = 64;
flag = 0;

for y0 in range(0, n - 8 + 1):
	for x0 in range(0, m - 8 + 1):
		rtn = 0;
		for y in range (0, 8):
			for x in range(0, 8):
				if arr[y0 + y][x0 + x] == 'W' and flag == 0:
					rtn += 1;
				elif arr[y0 + y][x0 + x] == 'B' and flag == 1:
					rtn += 1;
				flag = flag ^ 1;
			flag = flag ^ 1;
		rtn = min(rtn, 64 - rtn);
		rtn2 = min(rtn, rtn2);
print(rtn2);


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

# In[1]:

get_ipython().run_line_magic('pylab', 'inline')
import pandas
import seaborn

# # Load CSV file into memory

# In[2]:

data = pandas.read_csv('~/Documents/python/uber-raw-data-apr14.csv')

# In[3]:

data.tail()

# #convert datetime and insert coloumn

# In[4]:

dt = '4/30/2014 23:22:00'

# In[5]:

d, t = dt.split (' ')
print (d)
print (t)

# In[6]:

m, d, y = dt.split('/')

# In[7]:

d

# In[8]:

int(d)

# In[9]:

dt = pandas.to_datetime(dt)

# In[10]:

data['Date/Time'] = data['Date/Time'].map(pandas.to_datetime)

# In[11]:

data.tail()

# In[12]:

def get_dom(dt):
    return dt.day
data ['dom'] = data['Date/Time'].map(get_dom)

# In[13]:

data.tail()

# In[14]:

def get_weekday(dt):
    return dt.weekday()

data['weekday'] = data['Date/Time'].map(get_weekday)

def get_hour(dt):
    return dt.hour

data['hour'] = data['Date/Time'].map(get_hour)

data.tail()

# # Analisa

# ## Analisa berdasar tanggal

# In[15]:

hist(data.dom, bins = 30, rwidth=.8, range=(0.5, 30.5))
xlabel('Date of the month')
ylabel('frequency')
title('Frequency Data Uber 2014')

# In[16]:

#for k, rows in data.groupby('dom'):
#    print((k, rows))
#    print((k, len(rows)))

def count_rows(rows):
    return len(rows)

by_date = data.groupby('dom').apply(count_rows)
by_date

# In[17]:

plot(by_date)

# In[18]:

bar(range(1, 31), by_date)

# In[19]:

by_date_sorted = by_date.sort_values()
by_date_sorted

# In[20]:

bar(range(1, 31), by_date_sorted)

# In[21]:

bar(range(1, 31), by_date_sorted)
xticks(range(1, 31), by_date_sorted.index)
xlabel('Date of the month')
ylabel('frequency')
title('Frequency Data Uber 2014')
("")

# ## Analisa berdasar jam(hour)

# In[30]:

hist(data.hour, bins=24, range=(.5, 24))

# In[ ]:

# ## analisa weekday

# In[33]:

hist(data.weekday, bins=7, range =(-.5, 6.5), rwidth=.8, color='#AA6666', alpha=.4)
xticks(range(7), 'Mon Tue Wed Thue Fri Satur Sun'.split())

# In[35]:

data.groupby('hour weekday'.split()).apply(count_rows)

# In[36]:

data.groupby('hour weekday'.split()).apply(count_rows).unstack()

# In[40]:

data.groupby('weekday hour'.split()).apply(count_rows).unstack()

# In[41]:

by_cross = data.groupby('weekday hour'.split()).apply(count_rows).unstack()

# In[42]:

seaborn.heatmap(by_cross)

# #by Lat and lon

# In[44]:

hist(data['Lat'])

# In[47]:

hist(data['Lat'], bins=100, range=(40.5, 41))
("")

# In[49]:

hist(data['Lon'], bins=100)
("")

# In[54]:

hist(data['Lon'], bins=100, range=(-74.1, -73.9))
("")

# In[60]:

hist(data['Lat'], bins=100, range=(40.5, 41), color= 'g', alpha=.5)
twiny()
hist(data['Lon'], bins=100, range=(-74.1, -73.9), color='r', alpha=.5)
("")

# In[66]:

hist(data['Lat'], bins=100, range=(40.5, 41), color= 'g', alpha=.5, label ='latitude')
grid()
legend(loc='upper left')
twiny()
hist(data['Lon'], bins=100, range=(-74.1, -73.9), color='r', alpha=.5, label ='longitude')
grid()
legend(loc='best')
("")

# In[79]:

plot(data['Lat'], data['Lon'], '.', ms=3, color='green', alpha=.5)

# In[84]:

figure(figsize=(20, 20))
plot(data['Lon'], data['Lat'], '.', ms=1, color='green', alpha=.5)
xlim(-74.2, -73.7)
ylim(40.7, 41 )

# In[ ]:

Data Visualisation/Uber/data analysis uber.py


import time

t1 = time.time()

def divn(num):
    l = []
    arr = [1,2,3,4,5,6,7,8,9]
    for i in range(2,int(num**0.5)+1):
        b = num%i
        if b == 0:
            rem = int(num/i)
            g = [int(i) for i in list(str(num) + str(rem) + str(i))]
            if len(g) == 9:
                res = [x for x in arr if x in g]
                if len(res) == 9:
                    if num not in l:
                        l.append(num)
    return l

result = []
for i in range(1000,10000):
    req = divn(i)
    if len(req)>0:
        print(req)
        result+= req

print(sum(result))

t2 = time.time()
print(t2-t1)

'''def factors(n):
   return sorted(list(reduce(list.__add__,
               ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))'''


from serre_biblio import *
from toit import Toit
from uselect import poll, POLLIN
from machine import Pin, ADC
from time import sleep, ticks_ms

try:
  import usocket as socket
except:
  import socket

toit = Toit( )

# ============================================================================= b_poussoir (...)
bp_flag = 0 # Attention : Variable globale

def Set_BP_flag ( Pin ):
    global bp_flag
    bp_flag +=1

def b_poussoir():
    """ Si appui sur le BP -> interruption -> ouverture ou fermeture du toit """
    global bp_flag
    if bp_flag > 0:
        if not toit.OPEN :
            toit.ouvrir()
        elif not toit.CLOSE :
            toit.fermer()
        else :
            toit.fermer()
        bp_flag = 0

bp.irq ( trigger=Pin.IRQ_RISING, handler=Set_BP_flag )

# ============================================================================= web_page (...)
def web_page( )->str:
    # ----------------------------------------- État du toit
    if toit.CLOSE :
        toit_html="FERMÉ"
    elif toit.OPEN :
        toit_html="OUVERT"
    else :
        toit_html="## INCONNU ##"

    # ----------------------------------------- Température
    temperature_html = str(capteurBME.temperature)

    # ----------------------------------------- Lecture code HTML / CSS / et JS
    page_html = lire_fichier ( "index.html" )
    fichier_css = lire_fichier ( "style.css" )
    fichier_js = lire_fichier ( "script.js" )

    # ----------------------------------------- Incorporer JS et CSS dans HTML
    page_html = page_html.replace("<fichier_css>",fichier_css)
    page_html = page_html.replace("<fichier_js>",fichier_js)

    page_html = page_html.replace("<variable_temperature>", temperature_html)
    page_html = page_html.replace("<variable_toit>", toit_html)

    return page_html #Affiche la page HTML

# ==================================================================================== main
if not toit.CLOSE :
    toit.fermer ( )

if toit.CLOSE and Wifi_Connected : # Tout est OK => Attendre une requête

    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_socket.bind(('', 80))
    my_socket.listen(3)
    print("En attente de connexion ...")
    repondre = False

    while True : # En mode AP_IF
        connexion = []
        poller = poll()
        poller.register(my_socket, POLLIN)
        i = 0
        while connexion==[] :
            connexion = poller.poll(1000)
            b_poussoir() # Vérifier si appui sur le BP toutes les secondes

        conn, addr = my_socket.accept()
        print("\nRéception d'une requête depuis : "+ str(addr))
        request = conn.recv(1024)
        request = str(request)
        ouverture = request.find('/?open_toit=1') # Récupérer les arguments dans l'URL
        fermeture = request.find('/?close_toit=1')
        requete = request.find('GET / HTTP')

        if requete == 2 : # Eviter de gérer des connexions parasites
            repondre = True

        if ouverture == 6:
            if not toit.OPEN :
                toit.ouvrir ( )
            repondre = True

        if fermeture == 6:
            if not toit.CLOSE :
                toit.fermer ( )
            repondre = True

        if repondre :

            reponse_html = web_page( ) # Construction de la page Web

            conn.send('HTTP/1.1 200 OK\n')
            conn.send('Content-Type: text/html\n')
            conn.send('Connection: close\n\n')
            conn.sendall(reponse_html)
            conn.close()
            repondre = False


import requests
import os
from requests_oauthlib import OAuth2Session
from dotenv import load_dotenv
load_dotenv()

from config import twitter_scopes
from mongo_db import mongo

redirect_uri = os.environ.get("REDIRECT_URI")
client_id = os.environ.get("CLIENT_ID")

class Token_Manager():
    def __init__(self):
        self.client_id = os.environ.get("CLIENT_ID")
        self.client_secret = os.environ.get("CLIENT_SECselfRET")
        self.token_url = "https://api.twitter.com/2/oauth2/token"

    def get_userid(self, token):
        url = "https://api.twitter.com/2/users/me"
        user_id = requests.request(
            "GET",
            url,
            headers={
                "Authorization": "Bearer {}".format(token["access_token"]),
                "Content-Type": "application/json",
            }
        )
        print(user_id.json())
        print()
        self.user_id = user_id.json()['data']['id']
        return self.user_id

    def save_token(self, refreshed_token):
        collection_name = 'user_tokens'
        user_id = self.get_userid(refreshed_token)
        new_token_entry = {"user_id": user_id, "token": refreshed_token}
        mongo.save_to_collection(new_token_entry, collection_name, user_id)

    def refresh_token(self, token):
        twitter = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=twitter_scopes)
        refreshed_token = twitter.refresh_token(
            client_id=self.client_id,
            client_secret=self.client_secret,
            token_url=self.token_url,
            refresh_token=token["refresh_token"],
        )
        self.save_token(refreshed_token)
        return refreshed_token

tm = Token_Manager()

if __name__ == "__main__":
    print("Executing TokenManager as main file")


from django import forms
from .models import Order, ProductReview

PRODUCT_QUANTITY_CHOICES = [(i, str(i)) for i in range(1, 21)]

class CustomSelectWidget(forms.Select):
    template_name = 'store/tst.html'

class OrderForm(forms.Form):

    '''in the single_product view, we pass the product instance to the form.'''
    def __init__(self, instance, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)
        self.instance = instance

        self.fields['size'] = forms.ModelChoiceField(
            queryset=self.instance.sizes.all(),
            widget=forms.RadioSelect())

        self.fields['quantity'] = forms.IntegerField(widget=forms.NumberInput(attrs={'class': 'form-control'}), initial=1)

class ReviewForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ReviewForm, self).__init__(*args, **kwargs)

        #remove help text from form fields
        for field in self.fields:
            self.fields[field].label = ''

    class Meta:
        model = ProductReview
        fields = ['name', 'email', 'comment',]
        widgets = {'name': forms.widgets.TextInput(attrs={
                   'placeholder': 'Name'}),
                'email': forms.widgets.TextInput(attrs={
                    "placeholder": 'Email'}),
                'comment': forms.widgets.Textarea(attrs={
                    "placeholder": 'Review', "rows": "5"}),
               }

class OrderAddressForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ['first_name', 'last_name', 'country', 'city', 'address', 'postal_code']


import os
import numpy as np
import pandas as pd

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
plt.ioff()

import seaborn

def compare_abs_auroc_differences(results_dir,
                                  model_a_path, model_a_descriptor,
                                  model_b_path, model_b_descriptor):
    """Take the results of eval_by_scan_attr.calculate_model_perf_by_scan_attr()
    for two different models and make comparison plots.

    <results_dir> is the path to the directory in which to save the results
    <model_a_path> is the path to the directory in which the results of
        eval_by_scan_attr.py are stored for Model A
    <model_b_path> is the path to the directory in which the results of
        eval_by_scan_attr.py are stored for Model B
    <model_a_descriptor> and <model_b_descriptor> are descriptive strings
        that will be used in generating the plots

    For each scan attribute, a df will be loaded that has the following format:
        the columns are different scan attribute options. For example if the
            attribute is StationName, then the options (the columns) can
            include 'DMPRAD3FORCE', 'DMP_CT1', 'DMP_CT2', 'CCCT3Revo',
            'IPCT1', 'CaryCT1',...,'CTC1'.
        the rows are different abnormalities, such as 'lung_nodule',
            'heart_cardiomegaly', and 'h_great_vessel_atherosclerosis'
        the values are AUROCs calculated for that particular scan attribute
            option and abnormality."""
    if not os.path.exists(results_dir):
        os.mkdir(results_dir)

    #Create plots for all the attributes
    for attribute in ['SliceThickness','PatientAgeYears',
        'orig_square','orig_numslices','orig_slope','orig_inter',
        'orig_yxspacing','orig_zdiff',

        'Manufacturer','ManufacturerModelName','InstitutionName',
        'StationName','SoftwareVersions','ConvolutionKernel',
        'PatientSex','EthnicGroup','IterativeReconAnnotation',
        'IterativeReconConfiguration','IterativeReconLevel',
        'ProtocolName','ReconAlgo','ReconAlgoManuf']:
        model_a_df = pd.read_csv(os.path.join(model_a_path, attribute+'_AUROC.csv'),header=0,index_col=0)
        model_b_df = pd.read_csv(os.path.join(model_b_path,attribute+'_AUROC.csv'),header=0,index_col=0)

        model_a_df_w_diff = add_diff_column(model_a_df,model_a_descriptor)
        model_b_df_w_diff = add_diff_column(model_b_df,model_b_descriptor)

        #Combine the dataframes
        combined = pd.concat([model_a_df_w_diff, model_b_df_w_diff],axis=0, ignore_index=True)

        #sort by model and then by difference
        combined = combined.sort_values(by=['Model','Max AUROC Difference'],ascending=[False,True])

        #make plots
        make_bar_plot_per_abnormality(combined, attribute, results_dir)
        make_boxplot_agg_abnormalities(combined, attribute, results_dir)
        make_boxplot_agg_abnormality_groups(combined, attribute, results_dir)

def add_diff_column(df, model_descriptor):
    """Calculate the maximum AUROC difference between the different scan
    attribute options for each abnormality, and reformat the df for subsequent
    seaborn plotting.

    A bigger Max AUROC Difference is worse, because it means the performance
    varies a lot by that scan attribute.

    A smaller difference is good, because it means the performance for
    that abnormality is consistent across the different scan attribute
    options.

    For example, if the AUROC difference is very large for 'cardiomegaly'
    depending on different StationName (CT scanner) attribute options, that
    suggests the model might be cheating and using information about the
    CT scanner to predict 'cardiomegly.'

    <df> is a pandas dataframe with the format described in the
        docstring for compare_abs_auroc_differences()
    <model_descriptor> is a descriptive string"""
    df['Maximum'] = df.max(axis=1)
    df['Minimum'] = df.min(axis=1)
    df['Max AUROC Difference'] = (df['Maximum']-df['Minimum'])

    #drop 'Count' row
    df = df.drop(index='Count')

    #add a column indicating the model so you can use seaborn barplot easily
    df['Model'] = model_descriptor

    #make the abnormality index into a column so you can use seaborn barplot easily
    df.reset_index(inplace=True)
    df = df.rename(columns = {'index':'Abnormality'})

    #keep only the 3 columns needed for plotting
    df = df[['Abnormality','Max AUROC Difference','Model']]

    return df

def make_bar_plot_per_abnormality(combined, attribute, results_dir):
    """Make bar plot where each abnormality has two bars, one bar for Model A
    and one bar for Model B. The y axis shows the Max AUROC Difference, so
    lower is better."""
    fig, ax = plt.subplots(figsize=(16,8))
    seaborn.barplot(x = 'Abnormality', y = 'Max AUROC Difference', data = combined,
                hue = 'Model', hue_order = ['Base','Mask'], ax = ax)
    plt.xticks(rotation=90, fontsize='x-small')
    plt.savefig(os.path.join(results_dir,attribute+'_BarPerAbn.png'))
    plt.close()

def make_boxplot_agg_abnormalities(combined, attribute, results_dir):
    """Boxplot where different abnormalities are aggregated for each model,
    and the y axis shows the Max AUROC Difference, so a lower overall
    boxplot is better"""
    fig, ax = plt.subplots(figsize=(6,6))
    seaborn.boxplot(x = 'Model', y = 'Max AUROC Difference', data = combined, ax = ax, order=['Base','Mask'])
    plt.title('Max AUROC Difference \nAcross Abnormalities',fontsize='xx-large')
    increase_label_sizes(plt)
    plt.savefig(os.path.join(results_dir,attribute+'_BoxAggAbns.png'))
    plt.close()

def make_boxplot_agg_abnormality_groups(combined, attribute, results_dir):
    """Grouped boxplot where different abnormalities are aggregated for each
    model, but abnormalities are split up according to their organ: lung,
    heart, great_vessel, or mediastinum. The y axis shows the Max AUROC
    Difference, so a lower overall boxplot is better."""
    #Assign an organ to each abnormality
    combined['Organ']=''
    for idx in combined.index.values.tolist():
        abnormality = combined.at[idx,'Abnormality']
        if 'lung' in abnormality:
            combined.at[idx,'Organ'] = 'lung'
        elif 'heart' in abnormality:
            combined.at[idx,'Organ'] = 'heart'
        elif 'vessel' in abnormality:
            combined.at[idx,'Organ'] = 'great_vessel'
        elif 'mediastinum' in abnormality:
            combined.at[idx,'Organ'] = 'mediastinum'

    #Sanity check: make sure every abnormality has an organ assigned
    assert combined[combined['Organ']==''].shape[0]==0

    #Make plot
    fig, ax = plt.subplots(figsize=(8,8))
    seaborn.boxplot(x = 'Model', y = 'Max AUROC Difference', order = ['Base','Mask'],
                hue = 'Organ', data = combined, ax = ax, palette = 'mako')
    plt.title('Max AUROC Difference\nAcross Grouped Abnormalities',fontsize='xx-large')
    increase_label_sizes(plt)
    plt.savefig(os.path.join(results_dir,attribute+'_BoxAggAbnsByOrgan.png'))
    plt.close()

def increase_label_sizes(plt):
    """Increase the axis label sizes for a seaborn plot"""
    #https://stackoverflow.com/questions/43670164/font-size-of-axis-labels-in-seaborn?rq=1
    for ax in plt.gcf().axes:
        current_xlabels = ax.get_xlabel()
        ax.set_xlabel(current_xlabels, fontsize='x-large')
        current_ylabels = ax.get_ylabel()
        ax.set_ylabel(current_ylabels, fontsize='x-large')

    plt.xticks(fontsize='x-large')
    plt.yticks(fontsize='x-large')


import torch
import torchvision
from torchvision.io import read_image
import os
from torch.utils.data import Dataset

"""
This part of the script is an easy API to load and get the datasets
"""

def get_dataset(dataset: str, c_angle=30, new_size=[32, 32], batch_size=300):
    """
    :param new_size:
    :param c_angle:
    :param dataset: chosen dataset
    :return: train loader ,test loader and input size
    """
    if dataset == 'FASHION_MNIST':
        train_set = FASHION_MNIST('./data/' + dataset + '/', download=True, train=True,
                                  transform=torchvision.transforms.ToTensor())
        test_set = FASHION_MNIST('./data/' + dataset + '/', download=True, train=False,
                                 transform=torchvision.transforms.ToTensor())
        input_size = (28, 28, 1)
    elif dataset == 'Rotate FASHION_MNIST':
        rotate_tran_fun = lambda x: rotate_tran(x, angle=c_angle)
        train_set = FASHION_MNIST('./data/' + dataset + f'_Rotate_{c_angle}/', download=True, train=True,
                                  transform=rotate_tran_fun)
        test_set = FASHION_MNIST('./data/' + dataset + f'_Rotate_{c_angle}/', download=True, train=False,
                                 transform=rotate_tran_fun)
        input_size = (28, 28, 1)
    elif dataset == 'LFW':
        train_set = LFW('./data/' + dataset + '/', split='train',
                        transform=torchvision.transforms.ToTensor(), download=True)
        test_set = LFW('./data/' + dataset + '/', split='test',
                       transform=torchvision.transforms.ToTensor(), download=True)
        input_size = (250, 250)
    elif dataset == 'LFW_resize':
        resize_tran_fun = lambda x: resize_tran(x, new_size=new_size)
        train_set = LFW('./data/' + dataset + f'_{new_size}/', split='train', transform=resize_tran_fun, download=True)
        test_set = LFW('./data/' + dataset + f'_{new_size}/', split='test', transform=resize_tran_fun, download=True)
        input_size = (new_size[0], new_size[1], 3)

    train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True, drop_last=True)
    test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=True, drop_last=True)
    return train_loader, test_loader, input_size, batch_size

def rotate_tran(img, angle):
    tensor_img = torchvision.transforms.ToTensor(img)
    return torchvision.transforms.functional.rotate(img=tensor_img, angle=angle)

def resize_tran(img, new_size=[32, 32]):
    tensor_img = torchvision.transforms.ToTensor(img)
    return torchvision.transforms.functional.resize(img=tensor_img, size=new_size)

class FASHION_MNIST(torchvision.datasets.FashionMNIST):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __getitem__(self, index):
        return super().__getitem__(index)[0]

class LFW(torchvision.datasets.LFWPeople):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def __getitem__(self, index):
        return super().__getitem__(index)[0]

class Paining(Dataset):
    def __init__(self, img_dir, transform=None, target_transform=None):
        self.img_dir = img_dir
        self.transform = transform
        self.target_transform = target_transform

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

    def __getitem__(self, idx):
        img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0])
        image = read_image(img_path)
        label = self.img_labels.iloc[idx, 1]
        if self.transform:
            image = self.transform(image)
        if self.target_transform:
            label = self.target_transform(label)
        return image


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_openml
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import train_test_split
from matplotlib.colors import LinearSegmentedColormap
from sklearn.preprocessing import LabelEncoder

def prep_data(data):

    # Assuming y is your target labels
    y = data['target'].values

    # Normalize the data
    data_normalized = data.iloc[:, :-1].values / 255.0

    # Convert data to PyTorch tensors
    X_tensor = torch.tensor(data_normalized, dtype=torch.float32)
    y_tensor = torch.tensor(y, dtype=torch.long)

    # Split the data into training and testing sets
    X_train_tensor, X_test_tensor, y_train_tensor, y_test_tensor = train_test_split(X_tensor, y_tensor, test_size=0.2, random_state=42)
    num_classes = len(set(y))
    print("Unique classes in target labels:", num_classes)

    return X_train_tensor,y_train_tensor,X_test_tensor,y_test_tensor,num_classes

def create_model(MaxoutNetworkWithSoftmax):
    input_size = 784
    num_classes = 10
    device = torch.device("cpu")

    # Define the model
    model = MaxoutNetworkWithSoftmax(input_size, num_classes)

    model = model.to(device)

    optimizer = optim.Adam(model.parameters(), lr=0.001)

    # Define the loss function
    criterion = nn.CrossEntropyLoss()
    return model,criterion,optimizer

def create_model_2(DeepMaxoutNetwork,X_train_tensor):

        # Hyperparameters
    num_epochs = 100
    Batch_size =  100
    # Create an instance of the DeepMaxoutNetwork
    deep_maxout_model = DeepMaxoutNetwork(input_dim=784, hidden_dim=100, output_dim=10, num_units=2, num_layers=3)
    input_size = X_train_tensor.shape[1]
    criterion = nn.CrossEntropyLoss()
    sgd =  optim.SGD(deep_maxout_model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4)
    adam = optim.Adam(deep_maxout_model.parameters(), lr=0.01,weight_decay=1e-2)
    optimizer = adam

    return deep_maxout_model,criterion,optimizer, num_epochs, Batch_size

def create_model_3(ShallowRBF,X_train_tensor,centers):
    # Hyperparameters
    num_epochs = 100
    learning_rate = 0.01
    momentum = 0.9
    Batch_size =  100
    num_classes = 10
    input_size = 784
    n_channels = 3
    num_units = 2
    # Create an instance of the DeepMaxoutNetwork
    RBF_model = ShallowRBF(input_dim=784, num_classes=10, num_centers=centers.shape[0])
    input_size = X_train_tensor.shape[1]
    criterion = nn.CrossEntropyLoss()
    sgd =  optim.SGD(RBF_model.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4)
    adam = optim.Adam(RBF_model.parameters(), lr=0.01)
    optimizer = adam

    return RBF_model,criterion,optimizer, num_epochs, Batch_size

def create_model_4(ShallowSoftmaxNetBN,X_train_tensor):
    # Hyperparameters
    num_epochs = 100
    learning_rate = 0.01
    momentum = 0.9
    Batch_size = 100
    num_classes = 10
    input_size = 784
    n_channels = 3
    num_units = 2
    # Create an instance of the DeepMaxoutNetwork
    ShallowSoftmax_model = ShallowSoftmaxNetBN(input_dim=784, output_dim=10)

    input_size = X_train_tensor.shape[1]
    criterion = nn.CrossEntropyLoss()
    sgd =  optim.SGD(ShallowSoftmax_model.parameters(), lr=0.01,weight_decay=1e-2)
    adam = optim.Adam(ShallowSoftmax_model.parameters(), lr=0.01,weight_decay=1e-4)
    optimizer = adam
    return ShallowSoftmax_model,criterion,optimizer, num_epochs, Batch_size

def test_eps(test_Maxout,model,device,test_dataloader):
    # Run test for each epsilon

    accuracies = []
    args = []
    epsilons = [-15,-14,-13,-12,-11, -10,-9,-8,-7,-6, -5,-4,-3,-2,-1, 0,1,2,3,4, 5,6,7,8,9, 10,11,12,13,14, 15]

    for eps in epsilons:
        accuracy, arg = test_Maxout(model, device, test_dataloader, eps)
        accuracies.append(accuracy)
        args.append(arg)
    return args, epsilons

def plot_eps(args,epsilons):
    for i in range(len(args)):
        args[i] = args[i].detach().numpy()
        args[i] = np.log(args[i]) - np.log(np.sum(np.exp(args[i]), axis=1, keepdims=True))
        args[i] = args[i].mean(axis=0)
    # Plot the average values as a function of epsilon
    #plt.figure(figsize=(6, 18))  # Increase the height by a factor of 3
    plt.plot(epsilons, [i[:] for i in args])
    plt.xlabel('Epsilon')
    plt.ylabel('softmax output')

    plt.title('softmax output for each class vs Epsilon')
    plt.legend(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
    plt.show()

def training_loop(optimizer, model, criterion, X_train_tensor, y_train_tensor, num_epochs=200, batch_size=128):
    train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
    train_loader = DataLoader(train_dataset, batch_size, shuffle=True)

    train_losses = []  # Store training losses for each epoch

    for epoch in range(num_epochs):
        for batch_X, batch_y in train_loader:
            outputs = model(batch_X)
            loss = criterion(outputs, batch_y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        train_losses.append(loss.item())  # Store the loss after each epoch
        print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')

    return train_losses,model

def plot_losses(train_losses):
    import matplotlib.pyplot as plt
    plt.plot(train_losses, label='Training Loss')
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.title('Training Curve')
    plt.legend()
    plt.show()

def eval_train(X_train_tensor,y_train_tensor,model):

    model.eval()  # Set the model to evaluation mode
    with torch.no_grad():
        train_outputs = model(X_train_tensor)
        _, predicted = torch.max(train_outputs, 1)
        correct = (predicted == y_train_tensor).sum().item()
        total = y_train_tensor.size(0)
        train_accuracy = correct / total

    print(f'Training Accuracy: {train_accuracy * 100:.2f}%')

def visualize_weights_and_signs(model):
    # Get the weights from the first layer (assuming it's the layer you're interested in)
    weights = model.linear.weight.data

    # Extract the signs of the weights
    weight_signs = torch.sign(weights)

    # Check if the weights are 1-dimensional
    if weights.dim() == 1:
        # Reshape the weights to be a 2D tensor with one row
        weights = weights.view(1, -1)

    # Reshape the weights to match the original image dimensions (assuming 28x28)
    weight_images = weights.view(-1, 28, 28)

    # Reshape the weight signs to match the original image dimensions (assuming 28x28)
    weight_sign_images = weight_signs.view(-1, 28, 28)

    # Plot each set of weights and weight signs in a separate subplot
    num_classes = weight_images.size(0)
    fig, axes = plt.subplots(num_classes, 2, figsize=(16, 8 * num_classes))

    for i in range(num_classes):
        # Plot weights
        axes[i, 0].imshow(weight_images[i].cpu().numpy(), cmap='gray')
        axes[i, 0].set_title(f'Class {i} - Weight')
        axes[i, 0].axis('off')

        # Plot weight signs
        axes[i, 1].imshow(weight_sign_images[i].cpu().numpy(), cmap='gray', vmin=-1, vmax=1)
        axes[i, 1].set_title(f'Class {i} - Sign')
        axes[i, 1].axis('off')

    # Show the plot
    plt.show()

def eval_test(X_test_tensor, y_test_tensor, model):
    # Convert test data to DataLoader for batching
    test_dataset = TensorDataset(X_test_tensor, y_test_tensor)
    test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)

    model.eval()  # Set the model to evaluation mode
    total = 0
    correct = 0
    total_mean_confidence = 0.0
    total_samples = 0

    incorrect_mean_confidence = 0.0
    incorrect_samples = 0

    wrong_predictions = []
    correct_predictions = []
    all_predictions = []  # List to store all predictions

    with torch.no_grad():
        for batch_X, batch_y in test_loader:
            outputs = model(batch_X)
            _, predicted = torch.max(outputs.data, 1)
            total += batch_y.size(0)
            correct += (predicted == batch_y).sum().item()

            # Store all predictions
            all_predictions.extend(predicted.tolist())

            # Calculate mean confidence for all predictions
            probabilities = nn.functional.softmax(outputs, dim=1)
            confidences, _ = torch.max(probabilities, dim=1)
            total_mean_confidence += confidences.sum().item()
            total_samples += batch_y.size(0)

            # Calculate mean confidence for incorrect predictions
            incorrect_mask = predicted != batch_y
            if incorrect_mask.sum().item() > 0:
                incorrect_mean_confidence += confidences[incorrect_mask].sum().item()
                incorrect_samples += incorrect_mask.sum().item()

                # Store the wrong predictions
                wrong_predictions.extend(predicted[incorrect_mask].tolist())

            # Store the correct predictions
            correct_predictions.extend(predicted[~incorrect_mask].tolist())

    # Calculate mean confidence for all examples
    if total_samples > 0:
        total_mean_confidence /= total_samples

    # Calculate mean confidence for incorrect predictions
    if incorrect_samples > 0:
        incorrect_mean_confidence /= incorrect_samples

    accuracy = correct / total
    print(f'Accuracy: {accuracy * 100:.2f}%')
    print(f'Mean Confidence for All Examples: {total_mean_confidence:.4f}')
    print(f'Mean Confidence for Incorrect Predictions: {incorrect_mean_confidence:.4f}')

    return wrong_predictions, correct_predictions, all_predictions  # Return all predictions


from org.eclipse.swt import SWT
from org.eclipse.swt.widgets import Shell, Menu, MenuItem, ExpandBar, ExpandItem, Label, Composite, Button, Listener, Text
from org.eclipse.swt.layout import FillLayout, GridLayout

shell = Shell()
display = shell.getDisplay()
shell.setLayout(FillLayout())
shell.setText("ExpandBar Example")

menubar = Menu(shell, SWT.BAR)
shell.setMenuBar(menubar)
fileItem = MenuItem(menubar, SWT.CASCADE)
fileItem.setText("&File")
submenu = Menu(shell, SWT.DROP_DOWN)
fileItem.setMenu(submenu)
item = MenuItem(submenu, SWT.PUSH)
item.setText("New ExpandItem")

bar = ExpandBar(shell, SWT.V_SCROLL)
image = display.getSystemImage(SWT.ICON_QUESTION)

# First item
composite = Composite(bar, SWT.NONE)
# Add a context menu, check we describe it properly
popupmenu = Menu(shell, SWT.POP_UP)
popupitem = MenuItem(popupmenu, SWT.PUSH)
popupitem.setText("Popup")
composite.setMenu(popupmenu)
layout = GridLayout(2, False)
layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10
layout.verticalSpacing = 10
composite.setLayout(layout)
label = Label(composite, SWT.NONE)
label.setImage(display.getSystemImage(SWT.ICON_ERROR))
label = Label(composite, SWT.NONE)
label.setText("SWT.ICON_ERROR")
label = Label(composite, SWT.NONE)
label.setImage(display.getSystemImage(SWT.ICON_INFORMATION))
label = Label(composite, SWT.NONE)
label.setText("SWT.ICON_INFORMATION")
label = Label(composite, SWT.NONE)
label.setImage(display.getSystemImage(SWT.ICON_QUESTION))
label = Label(composite, SWT.NONE)
label.setText("SWT.ICON_QUESTION")
item1 = ExpandItem(bar, SWT.NONE)
item1.setText("What is your favorite icon")
item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y)
item1.setControl(composite)
item1.setImage(image)
item1.setExpanded(True)

def createButton(composite, text):
    # Add a context menu, check we describe it properly
    button = Button(composite, SWT.PUSH)
    button.setText(text)
    popupmenu = Menu(shell, SWT.POP_UP)
    popupitem = MenuItem(popupmenu, SWT.PUSH)
    popupitem.setText("Button Popup")
    button.setMenu(popupmenu)

# Second item
composite = Composite(bar, SWT.NONE)
layout = GridLayout()
layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10
layout.verticalSpacing = 10
composite.setLayout(layout)
button = createButton(composite, "Button1")
button = createButton(composite, "Button2")
item0 = ExpandItem(bar, SWT.NONE)
item0.setText("What is your favorite button")
item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y)
item0.setControl(composite)
item0.setImage(image)

class AddListener(Listener):
    def handleEvent(self, e):
        item2 = ExpandItem(bar, SWT.NONE)
        composite = Composite(bar, SWT.NONE)
        layout = GridLayout(2, False)
        composite.setLayout(layout)

        label = Label(composite, SWT.NONE)
        label.setText("What is your name?")
        # Just to see if this fools the text-finding algorithm
        pointlessComposite = Composite(composite, SWT.NONE)
        text = Text(pointlessComposite, SWT.NONE)
        item2.setText("New Question")
        text.pack()
        composite.pack()
        size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)
        item2.setHeight(size.y)
        item2.setControl(composite)
        item2.setImage(image)
        item2.setExpanded(True)

item.addListener(SWT.Selection, AddListener())

bar.setSpacing(8)
shell.setSize(400, 550)
shell.open()
while not shell.isDisposed():
    if not display.readAndDispatch():
        display.sleep()

display.dispose()


import datetime
import json
import math
import threading
import traceback

import asyncio
import pandas as pd

from constants import (
    MAP_MARKET_ID_TO_NAME as MARKET_MAP,
    AVAILABLE_MARKETS,
    ALL_MARKET_LABEL,
    QUERY_INTERVAL,
    MINT_DIVISOR,
    CONTRACT_ADDRESS
)
from prometheus_metrics import metrics
from blockchain.client import ResourceClient as BlockchainClient
from subgraph.client import ResourceClient as SubgraphClient

# Contract addresses
CONTRACT_ADDRESS = CONTRACT_ADDRESS

def write_to_json(data, filename):
    with open(filename, 'w') as json_file:
        json.dump({"data": data}, json_file, indent=4)

async def process_live_positions(blockchain_client, live_positions):
    """
    Asynchronously process live positions data.

    Args:
        live_positions (list): List of live position data, where each element is a dictionary
            containing information about a live position.

    Returns:
        pandas.DataFrame: DataFrame containing processed live position information.

    This asynchronous function processes live position data by filtering out positions not in available markets,
    retrieving their current values, and calculating UPNL (Unrealized Profit and Loss) metrics.

    Args Details:
        - `live_positions`: List of live position data.

    Note:
        - `AVAILABLE_MARKETS`, `get_current_value_of_live_positions`, and `MINT_DIVISOR` are assumed to be defined.
        - This function utilizes asynchronous operations for improved performance.

    """
    live_positions_df = pd.DataFrame(live_positions)
    live_positions_df.drop(
        live_positions_df[~live_positions_df['market'].isin(AVAILABLE_MARKETS)].index,
        inplace = True
    )
    # values = await get_current_value_of_live_positions(blockchain_client, live_positions_df)
    positions = live_positions_df[['market', 'owner.id', 'position_id']].values.tolist()
    values = await blockchain_client.get_value_of_positions(positions)
    values = [v / MINT_DIVISOR for v in values]
    live_positions_df['value'] = values
    live_positions_df['upnl'] = live_positions_df['value'] - live_positions_df['collateral_rem']
    live_positions_df['upnl_pct'] = live_positions_df['upnl'] / live_positions_df['collateral_rem']
    return live_positions_df

def set_metrics_to_nan():
    """
    Set metrics values to NaN to indicate a query error.

    This function updates the 'mint_gauge' metrics labels for all markets, setting their values to NaN.
    This is typically used to indicate that there was an issue with the query or data retrieval.

    Note:
        - `metrics` is a global object representing a metrics collector.
        - `AVAILABLE_MARKETS` is a global variable.
        - `MARKET_MAP` is a global variable.
        - `ALL_MARKET_LABEL` is a global variable.

    Returns:
        None
    """
    # Set metric to NaN to indicate that something went wrong with the query
    metrics['upnl_gauge'].labels(market=ALL_MARKET_LABEL).set(math.nan)
    metrics['collateral_rem_gauge'].labels(market=ALL_MARKET_LABEL).set(math.nan)
    metrics['upnl_pct_gauge'].labels(market=ALL_MARKET_LABEL).set(math.nan)
    for market in AVAILABLE_MARKETS:
        metrics['upnl_gauge'].labels(market=MARKET_MAP[market]).set(math.nan)
        metrics['collateral_rem_gauge'].labels(market=MARKET_MAP[market]).set(math.nan)
        metrics['upnl_pct_gauge'].labels(market=MARKET_MAP[market]).set(math.nan)

def set_metrics(live_positions_df_with_curr_values):
    """
    Set metrics based on processed live positions data.

    Args:
        live_positions_df_with_curr_values (pandas.DataFrame): DataFrame containing processed live position information.

    Returns:
        None

    This function sets various metrics based on the processed live position data, including UPNL (Unrealized Profit and Loss),
    collateral, and UPNL percentage metrics.

    Args Details:
        - `live_positions_df_with_curr_values`: DataFrame containing processed live position information.

    Note:
        - `set_metrics_to_nan`, `metrics`, `AVAILABLE_MARKETS`, `MARKET_MAP`, and `ALL_MARKET_LABEL` are assumed to be defined.
        - This function updates metrics based on the provided live position data.

    """
    if not len(live_positions_df_with_curr_values):
        set_metrics_to_nan()
        return

    # Calculate current value of each live position
    live_positions_df = live_positions_df_with_curr_values

    # Set initial value of upnl metric so far
    upnl_total = live_positions_df['upnl'].sum()
    upnl_total_per_market_df = live_positions_df.groupby(by='market')['upnl'].sum().reset_index()
    upnl_total_per_market = dict(zip(upnl_total_per_market_df['market'], upnl_total_per_market_df['upnl']))
    metrics['upnl_gauge'].labels(market=ALL_MARKET_LABEL).set(upnl_total)
    for market_id in upnl_total_per_market:
        metrics['upnl_gauge'].labels(market=MARKET_MAP[market_id]).set(upnl_total_per_market[market_id])

    # Set initial value for collateral metric so far
    collateral_total = live_positions_df['collateral_rem'].sum()
    collateral_total_per_market_df = live_positions_df.groupby(by='market')['collateral_rem'].sum().reset_index()
    collateral_total_per_market = dict(zip(collateral_total_per_market_df['market'], collateral_total_per_market_df['collateral_rem']))
    metrics['collateral_rem_gauge'].labels(market=ALL_MARKET_LABEL).set(collateral_total)
    for market_id in collateral_total_per_market:
        metrics['collateral_rem_gauge'].labels(market=MARKET_MAP[market_id]).set(collateral_total_per_market[market_id])
        metrics['upnl_pct_gauge'].labels(market=MARKET_MAP[market_id]).set(
            upnl_total_per_market[market_id] / collateral_total_per_market[market_id]
        )

    # live_positions_df['upnl_pct'] = live_positions_df['upnl'] / live_positions_df['collateral_rem']
    metrics['upnl_pct_gauge'].labels(market=ALL_MARKET_LABEL).set(upnl_total / collateral_total)

async def query_upnl(subgraph_client, blockchain_client, stop_at_iteration=math.inf):
    """
    Asynchronously query unrealized profit and loss (UPNL) metrics from the subgraph.

    Args:
        subgraph_client: An instance of the subgraph client used for querying data.
        blockchain_client: An instance of the blockchain client used for querying data.
        stop_at_iteration (int, optional): The maximum number of iterations to run the query. Default is math.inf.

    Returns:
        None

    This asynchronous function queries UPNL metrics from the provided subgraph client, connects to the Arbitrum network,
    and handles exceptions.

    It performs the following steps:
        1. Connects to the Arbitrum network.
        2. Initializes metrics and sets them to NaN.
        3. Fetches live positions from the subgraph and calculates current values.
        4. Sets UPNL metrics based on the live positions and current values.
        5. Runs iterations to update UPNL metrics.
        6. Handles exceptions and resets metrics if an error occurs.

    Note:
        - `process_live_positions`, `set_metrics`, and `set_metrics_to_nan` are defined functions.
        - `QUERY_INTERVAL` is a global variable.
        - `network` is a global object representing network connectivity.

    """
    print('[upnl] Starting query...')
    blockchain_client.connect_to_network()
    set_metrics_to_nan()
    try:
        iteration = 0

        # Fetch all live positions so far from the subgraph
        print('[upnl] Getting live positions from subgraph...')
        live_positions = subgraph_client.get_all_live_positions()
        print('live_positions', len(live_positions))
        # write_to_json(live_positions, 'live_positions.json')
        print('[upnl] Getting live positions current value from blockchain...')
        live_positions_df_with_curr_values = await process_live_positions(blockchain_client, live_positions)
        # write_to_json(live_positions_df_with_curr_values.to_dict(orient="records"), 'live_positions_with_current_values.json')
        print('[upnl] Calculating upnl metrics...')
        set_metrics(live_positions_df_with_curr_values)

        await asyncio.sleep(QUERY_INTERVAL)

        while iteration < stop_at_iteration:
            try:
                print('===================================')
                print(f'[upnl] Running iteration #{iteration}...')
                timestamp_start = math.ceil(datetime.datetime.now().timestamp())
                print('[upnl] timestamp_start', datetime.datetime.utcfromtimestamp(timestamp_start).strftime('%Y-%m-%d %H:%M:%S'))

                # Fetch all live positions so far from the subgraph
                live_positions = subgraph_client.get_all_live_positions()
                live_positions_df_with_curr_values = await process_live_positions(blockchain_client, live_positions)
                set_metrics(live_positions_df_with_curr_values)

                # Increment iteration
                iteration += 1

                # Wait for the next iteration
                await asyncio.sleep(QUERY_INTERVAL)
                # if iteration == 10:
                #     1 / 0
            except Exception as e:
                print(
                    f"[upnl] An error occurred on iteration "
                    f"{iteration} timestamp_start "
                    f"{datetime.datetime.utcfromtimestamp(timestamp_start).strftime('%Y-%m-%d %H:%M:%S')}:", e)
                traceback.print_exc()
    except Exception as e:
        print(f"[upnl] An error occurred:", e)
        traceback.print_exc()
        set_metrics_to_nan()

subgraph_client = SubgraphClient()
blockchain_client = BlockchainClient()
thread = threading.Thread(target=asyncio.run, args=(query_upnl(subgraph_client, blockchain_client),))

if __name__ == '__main__':
    asyncio.run(query_upnl)


class Board:
    """ Defines the game board """

    def __init__(self, COL, ROW, BINDS, COUNT):
        # self.board = [[0]*COL]*ROW  # problematic echoing...!
        self._cols, self._rows = COL, ROW
        self._board = [[0 for i in range(ROW)] for j in range(COL)]  # The COLxROW large board, all zeroes, i.e. unset
        self._binds = dict()
        for bind in BINDS:
            c, r, v = bind
            if not c in self._binds.keys():
                self._binds[c] = dict()
            if not r in self._binds[c].keys():
                self._binds[c][r] = v
                self._board[c][r] = v  # Also set the board, while we are in the loop...
            else:
                print(f"Conflict in Binding: {bind[0]}, {bind[1]} seems to defined more than once: {BINDS}")
        self._cntc = COUNT[0]
        self._cntr = COUNT[1]

        # Impliment BINDS

    def set(self, c, r, v):
        """ Set bord cell x,y to value v """
        self._board[c][r] = v

    def get(self, c, r):
        """ So banale that it's hardly relevant """
        return self._board[c][r]

    def get_col(self, i):
        return self._board[i]

    def get_row(self, i):
        vrow = [col[i] for col in self._board]
        return vrow

    def count(self, mode, i):
        """ Count number of 'ship' celle, per coll or row
            mode: 'c': coll mode, 'r': row mode
            i: the number of the coll/row to count """
        if mode == 'c':
            return self.get_col(i).count(1)
        elif mode == 'r':
            return self.get_row(i).count(1)
        else:
            raise ValueError(f"Illegal mode: {mode}, should be 'c' or 'r'")

    def full(self):
        """ Is all celles, on the board, filled with a non-zero value """
        return any([[v != 0 for v in inner] for inner in self._board])

    def satisfied(self):
        """ All bindings and counts satisfied """
        if all([[self._board[i][j] == self._binds[i][j] for j in self._binds[i].keys()] for i in self._binds.keys()]):
            if any([self._count('c', i) != self._cntc[i] for i in range(self._cntc)]):
                return False
        else:
            print(f"BIND seems to be violates! {self._binds} in {self.board_astext()}")
            return False

    def board_asrawtxt(self):
        str_ou = str()
        for r in reversed(range(self._rows)):  # Screen print top-down
            for c in range(self._cols):
                str_ou += ' ' + self._board[c][r]
            str_ou += "\n"
        return str_ou

    def board_astext(self):
        str_ou = str()
        for r in reversed(range(self._rows)):  # Screen print top-down
            for c in range(self._cols):
                v = self._board[c][r]
                if v == -1:  # No-ship
                    p = ' O'
                elif v == 0:  # unknown
                    p = ' .'
                elif v == 1:  # Ship
                    p = ' X'
                else:
                    raise ValueError(f"Illigal value in board: {self._board}")
                str_ou += p
            str_ou += "\n"
        return str_ou


from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import roc_auc_score

import os
import dill as dpickle
import numpy as np
import pandas as pd
import logging

class MLPWrapper:
    """Wrapper for Multi-Layer Perceptron classifier"""
    def __init__(self,
                 clf,
                 model_file="model.dpkl",
                 precision_threshold=0.7,
                 recall_threshold=0.5,
                 load_from_model=False):
        """Initialize parameters of the MLP classifier
        Args:
          clf: a sklearn.neural_network.MLPClassifier object
          model_file: the local path to save or load model
          precision_threshold: the threshold that the precision of one label must meet in order to be predicted
          recall_threshold: the threshold that the recall of one label must meet in order to be predicted
          load_from_model: load classifier from model file or not
        """
        if clf:
            self.clf = clf
        elif load_from_model:
            self.load_model(model_file=model_file)
        else:
            raise Exception("You need to pass a MLPClassifier object to the wrapper")
        self.model_file = model_file
        self.precision_threshold = precision_threshold
        self.recall_threshold = recall_threshold

        # precisions/probability_thresholds/recalls are dict
        # {label_index: number or None}
        self.precisions = None
        self.probability_thresholds = None
        self.recalls = None
        # count of labels
        self.total_labels_count = None

    def fit(self, X, y):
        """Train the classifier
        Args:
          X: features, numpy.array
          y: labels, numpy.array
        """
        self.clf.fit(X, y)

    def predict_probabilities(self, X):
        """Predict probabilities of all labels for data
        Args:
          X: features, numpy.array

        Return: a list, shape (n_samples, n_classes)
        """
        return self.clf.predict_proba(X)

    def find_probability_thresholds(self, X, y, test_size=0.3):
        """Split the dataset into training and testing to find probability thresholds for all labels
        Args:
          X: features, numpy.array
          y: labels, numpy.array
        """
        # split data
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=1234)
        self.fit(X_train, y_train)
        y_pred = self.predict_probabilities(X_test)

        self.probability_thresholds = {}
        self.precisions = {}
        self.recalls = {}
        self.total_labels_count = len(y_test[0])
        for label in range(self.total_labels_count):
            # find the probability for each label
            best_precision, best_recall, best_threshold = 0.0, 0.0, None
            precision, recall, threshold = precision_recall_curve(np.array(y_test)[:, label], y_pred[:, label])
            for prec, reca, thre in zip(precision[:-1], recall[:-1], threshold):
                # precision, recall must meet two thresholds respecitively
                if prec >= self.precision_threshold and reca >= self.recall_threshold:
                    # choose the threshold with the higher precision
                    if prec > best_precision:
                        best_precision = prec
                        best_recall = reca
                        best_threshold = thre
            # self.probability_thresholds is a dict {label_index: probability_threshold}
            # If probability_thresholds[label] is None, do not predict this label always, which
            # means this label is in the excluded list because it does not satisfy
            # both of the precision and recall thresholds
            self.probability_thresholds[label] = best_threshold
            self.precisions[label] = best_precision
            self.recalls[label] = best_recall

    def grid_search(self, params=None, cv=5, n_jobs=-1):
        """Grid search to find the parameters for the best classifier
        Args:
          params: parameter settings to try
                  a dict with param names as keys and lists of settings as values
          cv: cross-validation splitting strategy, int
          n_jobs: number of jobs to run in parallel, int or None
        """
        if not params:
            # default parameters to try
            params = {'hidden_layer_sizes': [(100,), (200,), (400, ), (50, 50), (100, 100), (200, 200)],
                      'alpha': [.001, .01, .1, 1, 10],
                      'learning_rate': ['constant', 'adaptive'],
                      'learning_rate_init': [.001, .01, .1]}
        self.clf = GridSearchCV(self.clf, params, cv=cv, n_jobs=n_jobs)

    def save_model(self, model_file=None):
        """Save the model to the local path
        Args:
          model_file: The local path to save the model, str or None
                      if None, use the property of this class.
        """
        if model_file:
            self.model_file = model_file
        with open(self.model_file, 'wb') as f:
            dpickle.dump(self.clf, f)

    def load_model(self, model_file=None):
        """Load the model from the local path
        Args:
          model_file: The local path to load the model, str or None
                      if None, use the property of this class.
        """
        if model_file:
            self.model_file = model_file
        if not os.path.exists(self.model_file):
            raise Exception("Model path {self.model_file} does not exist")
        with open(self.model_file, 'rb') as f:
            self.clf = dpickle.load(f)

def calculate_auc(predictions, y_holdout, label_columns):
    """Calculate AUC.

    Args:
      Predictions: num_samples x num_features array
      y_holdout: Labels "one" hot encoded; num_samples x num_labels
      label_columns: List of labels
    """
    auc_scores = []
    counts = []

    for i, l in enumerate(label_columns):
        y_hat = predictions[:, i]
        y = y_holdout[:, i]
        auc = roc_auc_score(y_true=y, y_score=y_hat)
        auc_scores.append(auc)

    counts = y_holdout.sum(axis=0)

    df = pd.DataFrame({'label': label_columns, 'auc': auc_scores, 'count': counts})
    display(df)
    weightedavg_auc = df.apply(lambda x: x.auc * x['count'], axis=1).sum() / df['count'].sum()
    print(f'Weighted Average AUC: {weightedavg_auc}')
    return df, weightedavg_auc


from itertools import groupby
from datetime import datetime, timedelta

from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError

class website(models.Model):
	_inherit = 'website'

	def get_dynamic_count(self,prods):
		filters = self.env['product.filter.value'].sudo().search([])
		for fltr in filters:
			count = 0
			for product in prods:
				for flt in product.filter_ids:
					if fltr in flt.filter_value_ids:
						count += 1
			fltr.sudo().write({
				'dynamic_count' : count,
			})

class FilterProductTemplate(models.Model):
	_inherit = 'product.template'

	filter_ids = fields.One2many('filter.product.line','product_tmpl_id','Product Filter')

class ProductFilters(models.Model):
	_name = 'product.filter'
	_description = 'Filter Product'
	_order = 'group_id'

	name = fields.Char('Name')
	type = fields.Selection([
		('radio', 'Radio'),
		('select', 'Select'),
		('color', 'Color')], default='radio', required=True)
	group_id = fields.Many2one('group.filter','Group Filter',required=True,default=lambda self: self.env['group.filter'].search([('name','=','Other Filters')],limit=1))
	filter_value_ids = fields.One2many('product.filter.value','filter_id',string="Filter Values",)
	filter_ids = fields.One2many('filter.product.line','filter_name_id','Product Filter')

class FilterProductValue(models.Model):
	_name = 'product.filter.value'
	_description = 'Filter Product Value'

	@api.depends('filter_id','name','html_color')
	def _attribute_count(self):
		for filters in self:
			product_obj = self.env['product.template']
			product_ids = product_obj.search([])
			count = 0
			if product_ids:
				for product in product_ids:
					if product.filter_ids:
						for flt in product.filter_ids:
							if filters in flt.filter_value_ids:
								count += 1
			filters.product_count = count

	name = fields.Char('Filter Values Name')
	filter_id = fields.Many2one('product.filter','Filter Name')

	html_color = fields.Char(
		string='HTML Color Index', oldname='color',
		help="""Here you can set a
		specific HTML color index (e.g. #ff0000) to display the color if the
		filterer type is 'Color'.""")
	product_count = fields.Integer('Count',compute='_attribute_count',)
	dynamic_count = fields.Integer("Dynamic Count")

class FilterProductGroup(models.Model):
	_name = 'group.filter'
	_description = 'Filter Group'

	name = fields.Char('Filter Group Name')

class FilterProductLine(models.Model):
	_name = 'filter.product.line'
	_description = 'Filter Product'

	product_tmpl_id = fields.Many2one('product.template','Filter View')
	filter_name_id = fields.Many2one('product.filter','Filter Name')
	filter_value_ids = fields.Many2many('product.filter.value',string="Filter Values")

	@api.onchange('filter_name_id')
	def onchange_filter_name_id(self):
		for flt in self:
			if flt.filter_name_id:
				if flt.filter_name_id != flt.filter_value_ids.filter_id :
					# reset task when changing project
					flt.filter_value_ids = False
				return {'domain': {
					'filter_value_ids': [('filter_id', '=', flt.filter_name_id.id)]
				}}

	@api.model
	def create(self,vals):
		res = super(FilterProductLine, self).create(vals)
		if 'filter_value_ids' in vals:
			for value in res.filter_value_ids:
				value._attribute_count()
		return res

	def write(self,vals):
		if 'filter_value_ids' in vals:
			for value in vals['filter_value_ids']:
				for flt_value in value[2]:
					flt = self.env['product.filter.value'].browse(flt_value)
					flt._attribute_count()
			for value in self.filter_value_ids:
				value._attribute_count()
		return super(FilterProductLine, self).write(vals)


'''
input number and print whether even or odd.
'''

while True :
    inputNum = int(input("Enter the number: "))

    if (inputNum == 0) :
        print("Good-bye!")
        break

    if (inputNum % 2 == 0) :
        print("Even!")
    else :
        print("Odd!")


from userbot.events import register
from userbot import CMD_HELP, bot

PENIS_TEMPLATE = """
╔╦╦
╠╬╬╬╣
╠╬╬╬╣ I ♥
╠╬╬╬╣ Chocolate
╚╩╩╩╝ CeteUserBot
"""

@register(outgoing=True, pattern=r"^\.(?:cho)\s?(.)?")
async def emoji_nah(e):
    emoji = e.pattern_match.group(1)

    await e.edit("Cete...")
    message = PENIS_TEMPLATE
    if emoji:
        message = message.replace('🍆', emoji)

    await e.edit(message)

CMD_HELP.update({
    "cho":
    ".cho\
    \nKullanım: cho yaratır :o\n"
})