#!/user/bin/env python
import hashlib, logging, urllib, datetime, sys, base64, os, string
from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.api import memcache
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson
import models
from common import *
import cgi
import generalcounter

# auto retry for random timeout
from gae_retry import autoretry_datastore_timeouts
autoretry_datastore_timeouts()

# Add our custom Django template filters to the built in filters
template.register_template_library('templatetags.basetags')

class MainHandler(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
          appUser = models.get_or_create_appUser()
          self.response.out.write(respond_to_request(self.request, user, 'app_base.html'))
        else:
         self.response.out.write(respond_to_request(self.request, user, 'app_base.html'))

class CreateHandler(webapp.RequestHandler):
    def get(self):
        recaptcha_challenge = cgi.escape(self.request.get('recaptcha_challenge_field'))
        recaptcha_response = cgi.escape(self.request.get('recaptcha_response_field'))
        tags = cgi.escape(self.request.get('tags').lower()).split(',')
        tags = [tag.strip() for tag in tags]
        name = cgi.escape(self.request.get('name'))
        if recaptcha_challenge != '' and recaptcha_response != '' and name != '':
            recaptchaResponse = RecaptchaSubmit(recaptcha_challenge, recaptcha_response,
                                                 '6LeqlggAAAAAAFcYAzZ1fn6zwNKHlZRuoKLnBrPu', os.environ['REMOTE_ADDR'])

            if recaptchaResponse.is_valid or DEBUG:
                appUser = models.get_or_create_appUser()
                self.create(appUser, name, tags)
            else:
                self.failed()
        else:
            self.failed()

    def create(self, appUser, name, tags=None):
        generalcounter.increment(PAGE_CREATED_COUNTER)
        p = models.create_page(appUser, name, getNewPageTemplate(name), tags)
        #json = createPageJson(p)
        json = {}
        json['edit'] = 'http://' + os.environ['HTTP_HOST'] + '/a/edit/' + str( p.key() )
        self.response.out.write(simplejson.dumps(json))

    def failed(self):
        json = {}
        json['error'] = True
        json['message'] = 'Failed to create new page.'
        self.response.out.write(simplejson.dumps(json))

class EditHandler(webapp.RequestHandler):
    def get(self, page_key):
        p = models.Page.get(page_key)
        if p:
            #appUser = models.get_or_create_appUser()
            json = createPageJson(p, False)
            params = {}
            params['page'] = json
            self.response.out.write(respond_to_request(self.request, users.get_current_user(), 'app_edit.html', params))
        else:
            self.error(403)

class SaveHandler(webapp.RequestHandler):
    def post(self):
        page_key = cgi.escape(self.request.get('pk'))
        save_key = cgi.escape(self.request.get('spk'))
        sjson = self.request.get('__saveHidden')
        saved = False
        if sjson != '' and page_key and save_key:
            #json = simplejson.loads(sjson)
            if canSavePage(page_key, save_key):
                page = models.Page.get(page_key)
                if page:
                    if page.content.content != sjson:
                        page.content.content = sjson
                        memcache.delete(PAGE_MEMCACHE_HTML + str(page.key().id()))
                        #update updated property
                        db.put([page.content, page])
                    saved = True
                    self.response.out.write('save')
        if not saved:
            self.error(403)

class LoggerHandler(webapp.RequestHandler):
    def get(self):
        page_key = cgi.escape(self.request.get('pk'))
        save_key = cgi.escape(self.request.get('spk'))
        message = cgi.escape(self.request.get('message'))
        if message and canSavePage(page_key, save_key):
            logging.debug(message)
        self.response.out.write('save')

class JsonPageHandler(webapp.RequestHandler):
    def get(self, page_key):
        page_key = page_key.split('.')[0]
        p = models.Page.get(page_key)
        #appUser = models.get_or_create_appUser()
        if p:
            json = createPageJson(p)
            self.response.out.write('JSON_PAGE=' + simplejson.dumps(json))

class DebugHandler(webapp.RequestHandler):
    def get(self):
        if users.is_current_user_admin() or DEBUG:
            appUser = models.get_or_create_appUser()
            params = {}
            params['pages_set'] = models.get_all_pages_for_user(appUser)
            params['pages_none_user'] = models.get_pages_for_nonuser()
            params['PATH_INFO'] = os.environ['PATH_INFO']
            params['HTTP_HOST'] = os.environ['HTTP_HOST']
            params['pages_new_ordered'], next = models.get_pages_newest()
            self.response.out.write(respond_to_request(self.request, users.get_current_user(), 'debug_base.html', params))
        else:
            self.error(403)

class DeleteHandler(webapp.RequestHandler):
    def get(self):
        page_key = cgi.escape(self.request.get('pk'))
        save_key = cgi.escape(self.request.get('spk'))
        redirect = self.request.get('redirect')
        if page_key and save_key and canSavePage(page_key, save_key):
            appUser = models.get_or_create_appUser()
            models.del_page(page_key, appUser)
            if redirect:
                self.redirect(redirect)
            else:
                json = {}
                json['success'] = True
                self.response.out.write(simplejson.dumps(json))
        else:
            self.error(403)

class GetUserPagesHandler(webapp.RequestHandler):
    def get(self):
        pageIndex = int(self.request.get('p', '0'))
        appUser = models.get_or_create_appUser()
        if appUser:
          pages, next = models.get_pages_for_user(appUser, pageIndex, 20)
          if next:
           nexturi = 'http://' + os.environ['HTTP_HOST'] + '/a/get/?p=%d' % (pageIndex + 1)
          else:
           nexturi = None
          if pageIndex > 1:
           prevuri = 'http://' + os.environ['HTTP_HOST'] + '/a/get/?p=%d' % (pageIndex - 1)
          elif pageIndex == 1:
           prevuri = 'http://' + os.environ['HTTP_HOST'] + '/a/get/'
          else:
           prevuri = None

          params = {}
          jsonPages=[]
          for page in pages:
              json = {}
              json['edit'] = 'http://' + os.environ['HTTP_HOST'] + '/a/edit/' + str(page.key())
              json['name'] = page.name
              json['id'] = str(page.key().id())
              json['updated'] = page.updated
              jsonPages.append(json)
          if nexturi:
              params['nextIndex'] = str((pageIndex + 1))
          if prevuri:
              params['prevIndex'] = str((pageIndex - 1))
          params['pages'] = jsonPages
          self.response.out.write(respond_to_request(self.request, users.get_current_user(), 'open_page.html', params))
        else:
         self.error(403)

class ImageSharingUploadImage(webapp.RequestHandler):
  """Handler for uploading images."""
  def post(self):
    """Process the image upload form.
    """
    page_key = cgi.escape(self.request.get('pk'))
    save_key = cgi.escape(self.request.get('spk'))
    if canSavePage(page_key, save_key) == False:
      self.error(400)
      self.response.out.write('Access Denied')
    else:
        page = db.get(page_key)
        # Get the actual data for the picture
        img_data = self.request.POST.get('the_file').file.read()
        json = {}
        params = {}
        try:
          img = images.Image(img_data)
          # Basically, we just want to make sure it's a PNG
          # since we don't have a good way to determine image type
          # through the API, but the API throws an exception
          # if you don't do any transforms, so go ahead and use im_feeling_lucky.
          #img.im_feeling_lucky()
          #png_data = img.execute_transforms(images.PNG)

          #img.resize(60, 100)
          #thumbnail_data = img.execute_transforms(images.PNG)
          params['json'] = json
          if page.image_upload_count > 10:
            json['error'] = 'Sorry, we only allow 10 uploads per page. try using \'Add Image URL\' tab.'
          else:
            pic = models.create_image(models.get_or_create_appUser(), page, img_data)
            page.image_upload_count = page.image_upload_count + 1
            page.put()
            json['url'] = 'http://' + os.environ['HTTP_HOST'] + '/a/i/' + str( pic.key().id() )
        except images.BadImageError:
          json['error'] = 'Sorry, we had a problem processing the image provided. Try again later.'
        except images.NotImageError:
          json['error'] = 'Sorry, we don\'t recognize that image format. We can process JPEG, GIF, PNG, BMP, TIFF, and ICO files.'
        except images.LargeImageError:
         json['error'] = 'Sorry, the image provided was too large for us to process. 1 MEG is the size limit, try using \'Add Image URL\' tab or resizing your image and upload again.'
        except RequestTooLargeError:
          json['error'] = 'Sorry, the image provided was too large for us to process. 1 MEG is the size limit, try using \'Add Image URL\' tab or resizing your image and upload again.'
        finally:
          self.response.out.write(respond_to_request(self.request, None, 'common/iframeJsonResponse.html', params))

class ServeImage(webapp.RequestHandler):
    def get(self, image_id):
        if image_id:
            image_id = long(image_id)
            imageModel = models.Picture.get_by_id(image_id)
            if imageModel:
                self.response.out.write(imageModel.image.img_data)
            else:
                self.error(403)
        else:
            self.error(403)
class TestDataHandler(webapp.RequestHandler):
    def get(self):
        if users.is_current_user_admin() or DEBUG:
            appUser = models.get_or_create_appUser()
            for i in range(50):
                models.create_page(appUser, 'Test Data:' + str(i), '{}', 'Car,Porsche,vorun,jaydn,kreal'.split(','))
            self.response.out.write('done')
        else:
            self.error(403)

class RenameHandler(webapp.RequestHandler):
    def get(self):
        page_key = cgi.escape(self.request.get('pk'))
        save_key = cgi.escape(self.request.get('spk'))
        name = cgi.escape(self.request.get('name'))
        if name != '' and page_key and save_key and canSavePage(page_key, save_key):
            page = models.Page.get(page_key)
            if page:
                page.name = name
                page.put()
            json = {}
            json['success'] = True
            self.response.out.write(simplejson.dumps(json))
        else:
            self.error(403)

class ShortenUrlHandler(webapp.RequestHandler):
    def get(self):
        url = cgi.escape(self.request.get('url'))
        s = shortenUrl(url)
        json = {}
        json['shorten'] = s
        self.response.out.write(simplejson.dumps(json))

class PreviewImageHandler(webapp.RequestHandler):
    def get(self):
        url = cgi.escape(self.request.get('url'))
        self.response.out.write(respond_to_request(self.request, None, 'common/previewImage.html', {'imageurl':url}))

application = webapp.WSGIApplication([
    #these paths are relative to app.yaml
    ('/a/', MainHandler),
    ('/a/i/([-\w]+)', ServeImage),
    ('/a/edit/([-\w]+)', EditHandler),
    ('/a/delete/.*', DeleteHandler),
    ('/a/create/.*', CreateHandler),
    ('/a/save/.*', SaveHandler),
    ('/a/get/.*', GetUserPagesHandler),
    ('/a/jsonpage/(.*)', JsonPageHandler),
    ('/a/upload/.*', ImageSharingUploadImage),
    ('/a/debug/.*', DebugHandler),
    ('/a/rename/.*', RenameHandler),
    ('/a/logger/.*', LoggerHandler),
    ('/a/shortenurl/.*', ShortenUrlHandler),
    ('/a/previewimage/.*', PreviewImageHandler),
    ], debug=DEBUG)

def main():
    wsgiref.handlers.CGIHandler().run(application)

if __name__=='__main__':
     main()


import sys
import os
import astropy.io.fits as pyfits
import numpy
from astLib import astWCS

if __name__ == "__main__":

    catfile = sys.argv[1]
    data = numpy.loadtxt(catfile)

    fitsfile = sys.argv[2]
    hdu = pyfits.open(fitsfile)

    for i in range(1, len(hdu)):
        wcs = astWCS.WCS(hdu[i].header, mode="pyfits")

        sel = (data[:,4] == i)

        data_sel = data[sel]

        for j in range(data_sel.shape[0]):
            x, y = data_sel[j,0], data_sel[j,1]
            _ra, _dec = wcs.pix2wcs(x-1, y-1)

            print(x, y, i, data_sel[j, 2], data_sel[j, 3], _ra, _dec)


from source.utilities.graphStringMuxer import GraphStringMuxer
from source.utilities.trajectoryCounts import TrajectoryCounts
from typing import List
from source.dataStructures import NoOpCompare, Vector

class BranchingGraphError(Exception):
    pass

class Graph(NoOpCompare):

    def __init__(self, graphVectors: List[Vector], vectorList: List[Vector], skipSpinUp=False):
        if skipSpinUp:
            return
        self.graphData = []
        self.weight = 0
        self.offendingIndex = 0
        self.lastChange = None
        self.vectorList = vectorList
        # self.trajectoryRegister = TrajectoryCounts.fillRegister(graphVectors)
        charList = ''
        for v in graphVectors:
            vector = vectorList[v]
            self.graphData.append(self.toKey(vector))
            self.weight += vector[2]
            if charList.find(vector[0]) == -1:
                charList += vector[0]
            else:
                raise BranchingGraphError

    def copy(self):
        g = Graph([], [], skipSpinUp=True)
        g.graphData = list(self.graphData)
        g.weight = self.weight
        g.lastChange = self.lastChange
        g.vectorList = self.vectorList
        # g.trajectoryRegister = self.trajectoryRegister
        g.offendingIndex = self.offendingIndex
        return g

    def goAcross(self, vector: Vector, weight: int) -> int:
        i = GraphStringMuxer.translate(self.lastChange[0])
        oldWeight = self.vectorList[self.graphData[i]][2]
        self.swapOutLastChange()
        self.replace(vector)
        return  weight - oldWeight + vector[2]

    def swapOutLastChange(self) -> None:
        self.replace(self.vectorList[self.lastChange])

    def goDeeper(self, vector, weight) -> int:
        self.replace(vector)
        return weight + vector[2]

    def replace(self, vector) -> None:
        i = GraphStringMuxer.translate(vector[0])
        v = self.graphData[i]
        self.lastChange = v
        self.weight += vector[2] - self.vectorList[v][2]
        # self.trajectoryRegister = TrajectoryCounts.createDeltaRegister(self.trajectoryRegister, v, vector)
        self.__assignVector(vector)

    def isValid(self) -> bool:
        # [consistent, offendingIndex] = TrajectoryCounts.demonstratesConsistency(self.trajectoryRegister)
        # self.offendingIndex = offendingIndex
        # if not consistent:
        #     return False
        visited = {}
        start = self.graphData[0][0]
        beginning = start
        while True:
            if visited.get(start, False):
                break
            visited[start] = True
            i = GraphStringMuxer.translate(start)
            start = self.graphData[i][3]
        return len(visited) == len(self.graphData) and start == beginning

    def getWeight(self):
        return self.weight

    def toVectorListString(self):
        s = "{"
        for v in self.graphData:
            s += str(self.vectorList[v])
        return s + "}"

    def __assignVector(self, vector):
        i = GraphStringMuxer.translate(vector[0])
        self.graphData[i] = self.toKey(vector)

    def __str__(self):
        visited = {}
        start = self.graphData[0][0]
        literal = "(" + start
        while True:
            if visited.get(start, False):
                break
            literal += GraphStringMuxer.arrowString
            visited[start] = True
            i = GraphStringMuxer.translate(start)
            start = self.destintationAt(i)
            literal += start
        return literal + "): " + str(self.weight)

    def __unicode__(self):
        return u"" + self.__str__()

    def toKey(self, v: Vector) -> str:
        return v[0] + GraphStringMuxer.arrowString + v[1]

    def destintationAt(self, i: int) -> str:
        return GraphStringMuxer.destintationCharacter(self.graphData[i])


# -*- coding: utf-8 -*-
"""
Created on Thu May 25 15:50:34 2023

@author: gabri
"""
import numpy as np
import matplotlib.pyplot as plt
from hamiltonians import Hamiltonian_soliton_A1u_sparse, charge_conjugation_operator_sparse
from functions import get_components
import scipy

L_x = 200
L_y = 200
t = 1
Delta = 1
mu = -2  #-2
Phi = 0.1*np.pi  #height of the phase soliton around flux pi
t_J = 1   #t
L = 30      #L_y//2
k = 4   #number of eigenvalues
Delta_Z = 0
theta = np.pi/2
phi = 0

H = Hamiltonian_soliton_A1u_sparse(t=t, mu=mu, L_x=L_x, L_y=L_y, Delta=Delta, t_J=t_J, Phi=Phi, L=L)
eigenvalues_sparse, eigenvectors_sparse = scipy.sparse.linalg.eigsh(H, k=k, sigma=0)
idx = eigenvalues_sparse.argsort()
eigenvalues_sparse = eigenvalues_sparse[idx]
eigenvectors_sparse = eigenvectors_sparse[:, idx]

from scipy.linalg import orth
eigenvectors_negative = scipy.linalg.orth(eigenvectors_sparse[:,:2]) #for negative eigenvalues
#eigenvectors_negative = eigenvectors_sparse[:,:2]
C = charge_conjugation_operator_sparse(L_x, L_y)
A = C.dot(eigenvectors_negative[:,0].conj()) #positive eigenvector
is_charge_conjugation_A = np.allclose(H @ A, -eigenvalues_sparse[0]*A)
B = C.dot(eigenvectors_negative[:,1].conj()) #positive eigenvector
is_charge_conjugation_B = np.allclose(H @ B, -eigenvalues_sparse[1]*B)
eigenvectors = np.append(eigenvectors_negative, A.reshape((len(A), 1)), axis=1)
eigenvectors = np.append(eigenvectors, B.reshape((len(B), 1)), axis=1)

eigenvector_up_negative = 1/np.sqrt(2)*(eigenvectors[:,0]+eigenvectors[:,1])
eigenvector_down_negative = 1/np.sqrt(2)*(eigenvectors[:,0]-eigenvectors[:,1])
eigenvector_up_positive = 1/np.sqrt(2)*(eigenvectors[:,2]+eigenvectors[:,3])
eigenvector_down_positive = 1/np.sqrt(2)*(eigenvectors[:,2]-eigenvectors[:,3])

eigenvectors_polarized = np.array([eigenvector_up_negative, eigenvector_down_negative, eigenvector_up_positive, eigenvector_down_positive]).T

index = np.arange(k)   #which zero mode (less than k)
probability_density = []
zero_state = []
localized_state_upper_left = [] # it is the site (L_x/2-1, (L_y+L)/2)
localized_state_upper_right = [] # it is the site (L_x/2, (L_y+L)/2)
localized_state_bottom_left = [] # it is the site (L_x/2-1, (L_y-L)/2)
localized_state_bottom_right = [] # it is the site (L_x/2, (L_y-L)/2)
localized_state_left = []
localized_state_right = []

for i in index:
    destruction_up, destruction_down, creation_down, creation_up = get_components(eigenvectors_sparse[:,i], L_x, L_y)
    probability_density.append((np.abs(destruction_up)**2 + np.abs(destruction_down)**2 + np.abs(creation_down)**2 + np.abs(creation_up)**2)/(np.linalg.norm(np.abs(destruction_up)**2 + np.abs(destruction_down)**2 + np.abs(creation_down)**2 + np.abs(creation_up)**2)))
    zero_state.append(np.stack((destruction_up, destruction_down, creation_down, creation_up), axis=2)) #positive energy eigenvector splitted in components
    localized_state_upper_left.append(zero_state[i][(L_y+L)//2, L_x//2-1,:])
    localized_state_upper_right.append(zero_state[i][(L_y+L)//2, L_x//2,:])
    localized_state_bottom_left.append(zero_state[i][(L_y-L)//2, L_x//2-1,:])
    localized_state_bottom_right.append(zero_state[i][(L_y-L)//2, L_x//2,:])
    localized_state_left.append(zero_state[i][:, L_x//2-1,:])
    localized_state_right.append(zero_state[i][:, L_x//2,:])


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

import Sniper
import os

def get_parser():
    import argparse
    parser=argparse.ArgumentParser(description='run SN PSDTools')

    #---------------log level-----------------
    parser.add_argument("--evtmax", type=int, default=-1, help='events to be processed')
    parser.add_argument("--loglevel", default="Debug", choices=["Test", "Debug", "Info", "Warn", "Error", "Fatal"],help="Set the Log Level")

    #---------------input and output-----------------
    parser.add_argument("--input", help="input file name")
    parser.add_argument("--inputList", action="append", help="input file list name, this file list contains the names of input files")
    parser.add_argument("--output", default="", help="The user output file")
    parser.add_argument("--user-output", default="", help="The user output file")
    parser.add_argument("--truthFile", default="", help="the truth file")
    parser.add_argument("--weightFile", default="", help="the truth file")

    #--------------------For PSDTools--------------------------
    parser.add_argument("--method", default="TestPSDTool", choices=["TestPSDTool", "SNMLTool"], help="The PSDTools method")
    parser.add_argument("--enablePredict", dest="usePredict", action="store_true")
    parser.add_argument("--disablePredict", dest="usePredict", action="store_false")
    parser.set_defaults(usePredict = True)

    return parser

DATA_LOG_MAP = {"Test":0, "Debug":2, "Info":3, "Warn":4, "Error":5, "Fatal":6}

if __name__ == "__main__":
    parser=get_parser()
    args=parser.parse_args()

    topTask=Sniper.TopTask("TopTask")
    topTask.setLogLevel(DATA_LOG_MAP[args.loglevel])
    topTask.setEvtMax(args.evtmax)

    #=======================Input using framework======================
    filelist=[]
    if args.input:
        filelist.append(args.input)
    if args.inputList:
        for fname in args.inputList:
            with open(fname) as hxf:
                for line in hxf:
                    line=line.strip()
                    filelist.append(line)
    import BufferMemMgr
    bufmgr=topTask.createSvc("BufferMemMgr")
    bufmgr.property("StopTaskOpt").set(1)
    #bufmgr.property("TimeWindow").set([-10,10])
    import RootIOSvc
    readin=topTask.createSvc("RootInputSvc/InputSvc")
    readin.property("InputFile").set(filelist)

    #=======================Output using framwork======================
    ##########################################################
    ###For the output of data model, PSDTools data model is not implemented yet
    ###readout = topTask.createSvc("RootOutputSvc/OutputSvc")
    ###readout.property("OutputStreams").set({"/Event/PSDTools":args.output})
    ##########################################################
    import RootWriter
    rootwriter = topTask.createSvc("RootWriter")
    userOutputMap = {"USER_OUTPUT":args.user_output, "PSD_OUTPUT":args.user_output}
    rootwriter.property("Output").set(userOutputMap)

    #=======================PSDTools related======================
    import PSD
    psdalg = topTask.createAlg("PSDAlg")
    if args.method == 'SNMLTool':
        import SNPSD
        psdtool = psdalg.createTool(args.method)
        psdtool.property("truthFile").set(args.truthFile)
        psdtool.property("weightFile").set(args.weightFile)
    psdalg.property("Method").set(args.method)
    psdalg.property("UsePredict").set(args.usePredict)

    topTask.show()
    topTask.run()


'''
    @author = Akshay Kale
'''
import json

from uni_db.order_management.models import (
    Order, OrderItem, OrderStatusHistory
)
from uni_db.client_erp.models import ClientMobile
#from DBLayer.inventory.models import Delivery
from django.core.exceptions import ObjectDoesNotExist
from ordermanagement.utils.address_utils import address_as_json
from flask import current_app as app
from ordermanagement.utils.auth import get_user
from ordermanagement.utils.JsonSerializer import JSONSerializer
from ordermanagement.utils.utility_functions import (
    get_str_datetime, get_str_date
)

def create_response(order_instance):
    '''
     This method returns the JSON response for the Order.
     Keyword arguments
     order_instance -- Order object
    '''

    serializer = JSONSerializer()
    response = {}
    billing_address = address_as_json(order_instance.billing_address)
    shipping_address = address_as_json(order_instance.shipping_address)
    serialized_sale_order = serializer.serialize(order_instance)
    seriaized_sale_order_item = serializer.serialize([
        json.loads(serializer.serialize(ob)) for ob in
        OrderItem.objects.filter(order=order_instance)
    ])

    serialized_sale_order_history = serializer.serialize([
        json.loads(serializer.serialize(oh)) for oh in
        OrderStatusHistory.objects.filter(order=order_instance)
    ])

#     if(order_instance.status=='DISPATCHED' or order_instance.status=='ORDER COMPLETED' or order_instance.status=='RETURN COMPLETED'):
#         try:
#             response['IndiaPostBarcode']=str(Delivery.objects.get(order=order_instance).indiaPost_barcode_no)
#         except ObjectDoesNotExist:
#             response['IndiaPostBarcode']="Delivery Object Not Created."
#     else:
#         response['IndiaPostBarcode']='Yet To Dispatch'
    response['SaleOrder'] = json.loads(serialized_sale_order)
    if order_instance.advance_order_date:
        response['SaleOrder']['advance_order_date'] = get_str_date(
            order_instance.advance_order_date)

    response['SaleOrder']['created_on'] = get_str_datetime(
        order_instance.created_on)
    response['SaleOrder']['updated_on'] = get_str_datetime(
        order_instance.updated_on)

    if order_instance.entered_by:
        response['SaleOrder']['entered_by_csr'] = order_instance.entered_by.get_full_name()
    if order_instance.modified_by:
        response['SaleOrder']['modified_by_csr'] = order_instance.modified_by.get_full_name()

    response['SaleOrder']['billing_address'] = billing_address
    response['SaleOrder']['shipping_address'] = shipping_address
    response['SaleOrder']['notification_mobile'] = [str(mob.mobile) for mob
                                                    in ClientMobile.objects.filter(client=order_instance.owner)]
    response['SaleOrder']['owner'] = {
        'Name': order_instance.owner.client_name,
        'ClientId': order_instance.owner.client_id
    }

    #response['SaleOrder']['coupon_name'] = order_instance.coupon.name if order_instance.coupon else 'None'
    response['SaleOrderItem'] = json.loads(seriaized_sale_order_item)
    response['SaleOrderHistory'] = json.loads(serialized_sale_order_history)
    return response

def handle_request(code):
    '''
    This method handles the  get request,
    Keyword argments
    code -- Sales Order Code
    '''
    try:
        order = Order.objects.get(sales_order_id=code)
        return create_response(order)

    except ObjectDoesNotExist as o:
        app.logger.info(o)
        return {
            "responseCode": 200,
            "Message": "Invalid Order Code"
        }

    except Exception as e:
        app.logger.info(e)
        return {
            "responseCode": 500,
            "Message": "Internal Server Error"
        }


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auther : Jianghao
# Date : 2017/11/3

import json
#
# s1 = '{"key1":"value1"}'      #字符串只能是这个格式的,才能被json转换   通过loads进行反序列化时,必须使用双引号
# s2 = json.loads(s1)           #使用loads反序列化
#
# print(s1,type(s1))
# print(s2,type(s2))
#
# d1 = {'key2':'value2'}
# d2 = json.dumps(d1)
#
# print(d1,type(d1))
# print(d2,type(d2))   #经dumps处理之后，dict变为str

# import json
#
# d1 = {'key2':'value2'}
# json.dump(d1,open('序列化.json','w'))    #将d1序列化，并写入文件

# import json
#
# e1 = json.load(open('序列化.json','r'))      #读取json文件,反序列化
# print(e1,type(e1))

d = [1,2,3,4,5]

print(d,type(d))

json.dump(d,open('1.json','w'))

print(json.load(open('1.json','r')))


# coding: utf-8
'''
Created on 2014��9��21��

直方图： 描述了图像中某一灰度级别的像素占总像素的比例，从而描绘了图像的灰度分布，直方图可以单独计算图像中某一区域
的直方图，但是计算时分母必须是整个图像的像素总数。也就是必须正则化于整幅图像。

像素数值必定  0=< pixel <=255,即在区间【0，255】之间。
可见：直方图可以叠加。

@author: xhj
'''

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

def calcAndDrawHist(image, color):
    hist= cv2.calcHist([image], [0], None, [256], [0.0,255.0])
    minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(hist)
    histImg = np.zeros([256,256,3], np.uint8)
    hpt = int(0.9* 256);

    for h in range(256):
        intensity = int(hist[h]*hpt/maxVal)
        cv2.line(histImg,(h,256), (h,256-intensity), color)

    return histImg;

def calcDrawHistChannel(img):
    '''
       根据直方图含义自己所写的函数，和上边的calcAndDrawHist函数所显示的图像完全相同。
    '''
    if img.ndim == 3:
        x = [i for i in range(256)]
        for i in range(3):
            hist = cv2.calcHist([img],[i],None, [256],[0.0,255.0])
            plt.plot(x,hist/hist.sum())   #使用matplotlib进行折线图绘制

        plt.show()

if __name__ == '__main__':
    img = cv2.imread(r'E:\pyopencv\imgs\fruits.jpg')
    calcDrawHistChannel(img)

    img = cv2.imread(r"E:\pyopencv\imgs\fruits.jpg")
    b, g, r = cv2.split(img)

    histImgB = calcAndDrawHist(b, [255, 0, 0])
    histImgG = calcAndDrawHist(g, [0, 255, 0])
    histImgR = calcAndDrawHist(r, [0, 0, 255])

    cv2.imshow("histImgB", histImgB)
    cv2.imshow("histImgG", histImgG)
    cv2.imshow("histImgR", histImgR)
    cv2.imshow("Img", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


import pop_util
import requests

# access token has validity of 1 hour
accessTok = pop_util.access_token()

url = 'https://tmobilea-sb02.apigee.net/api/asset-portfolio/v2/business-application/APM0224114'

popTok = pop_util.create_pop(url)

# access api endpoints using python request lib
headers = {
'X-Authorization': popTok,
'Authorization': 'Bearer '+ accessTok,
}
url = url
payload={}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)


import asyncio
import time
import pandas as pd
import jdatetime as jd
from datetime import timedelta
from tqdm import tqdm

from requests_html import HTMLSession, AsyncHTMLSession

# number of workers to run in parallel
WORKERS = 64
# number of pages to scrap
THRESHOLD = 5
# default URL template
URL_TEMPLATE = 'https://fidibo.com/category/literature/short-story?page='
DATE_FORMAT = '%Y/%m/%d'
HEADERS = {'X-Requested-With': 'XMLHttpRequest'}

session = HTMLSession()
session.headers.update(HEADERS)  # send HEADERS with all requests
r = session.get('https://fidibo.com/category/literature/short-story')

# get number of pages available
TOTAL_PAGES = r.json()['total_pages']
SIZE_PER_PAGE = r.json()['size']

# books that are going to be scraped
TOTAL_BOOKS = SIZE_PER_PAGE * THRESHOLD

# choose smaller between total_pages and threshold for threshold value
THRESHOLD = min(THRESHOLD, TOTAL_PAGES)

print(f'[i] Number of workers {WORKERS}')
print(f'[i] Total pages is {TOTAL_PAGES}')
print(f'[i] Number of pages to be scrapped {THRESHOLD}')

# dataset columns
all_books = []
all_comments = []

async def worker(job_queue: asyncio.Queue, response_queue: asyncio.Queue, pbar):
    """
    this function run asynchronously and GET the book pages
    of the provided URL and put() the Response object
    to the responses_queue.
    """
    while True:
        # get() the page number of the page to scrap
        page_number = await job_queue.get()
        # print(f'[i] getting page {page_number}')
        # create an session object which runs asynchronously
        asession = AsyncHTMLSession()
        asession.headers.update(HEADERS)
        response = await asession.get(URL_TEMPLATE + page_number)
        response_queue.put_nowait(response)
        job_queue.task_done()

        # update progress bar
        pbar.update(1)

async def comment_worker(job_queue: asyncio.Queue, response_queue: asyncio.Queue, p_bar):
    """
    this function run asynchronously and GET the comments of books
    of the provided URL and put() the Response object
    to the responses_queue.
    """
    while True:
        # get() the page number of the page to scrap
        data = await job_queue.get()
        # print(f'[i] getting page {page_number}')
        # create an session object which runs asynchronously
        asession = AsyncHTMLSession()
        asession.headers.update(HEADERS)
        response = await asession.post('https://fidibo.com/book/comment', data)
        response_queue.put_nowait((response, data))
        job_queue.task_done()
        # print(f'[i] got page {page_number}')

        # update progress bar
        p_bar.update(1)

async def parse(res_q: asyncio.Queue, comments_job: asyncio.Queue, book_q):
    """
    this function get book attributes and queue job for
    getting comment total pages to be scraped and then
    create jobs for comments to be scraped
    """
    job_queue = asyncio.Queue()
    response_queue = asyncio.Queue()

    # while the is still a result available in the Queue
    while not res_q.empty():
        res = await res_q.get()
        data = res.json()
        [
            book_q.put_nowait({'id': _['id'], 'title': _['title'], 'authors': _['authors'], 'url': _['url']})
            for _ in data['books']
        ]

        for _ in data['books']:
            book_id = _['id']
            page = 1
            post_data = {'book_id': book_id, 'page': page}

            job_queue.put_nowait(post_data)

    pbar = tqdm(total=job_queue.qsize())
    tasks = []
    for i in range(WORKERS):
        task = asyncio.create_task(comment_worker(job_queue, response_queue, pbar))
        tasks.append(task)

    # for progress bar
    await job_queue.join()
    pbar.close()

    # create jobs for comments to be scraped
    while not response_queue.empty():
        response, post_data = response_queue.get_nowait()
        total_comments_page = response.json()['comment_paging']['pageCount']

        for page in range(1, total_comments_page + 1):
            post_data['page'] = page
            await comments_job.put(post_data)

    for task in tasks:
        task.cancel()

async def get_books_comments(comments_res: asyncio.Queue, comments_q: asyncio.Queue):
    """
    create comment dict from previous results
    """
    while not comments_res.empty():
        cms, post_data = comments_res.get_nowait()
        comments = cms.json()
        for comment in comments.get('Comments'):
            comments_q.put_nowait({
                'id': comment.get('id'),
                'book_id': comment.get('book_id'),
                'comment': comment.get('comment')
            })

async def main():
    # create FIFO Queue for both jobs to be done
    # and the result of the jobs
    job_queue = asyncio.Queue()
    response_queue = asyncio.Queue()
    book_queue = asyncio.Queue()

    comments_jobs = asyncio.Queue()
    comments_response = asyncio.Queue()
    comments_queue = asyncio.Queue()

    # create all page numbers that need to be scraped
    for _ in range(THRESHOLD):
        job_queue.put_nowait(str(_ + 1))  # to start from 1

    pbar = tqdm(total=THRESHOLD)
    # Create WORKER number of worker tasks to process the queue concurrently.
    tasks = []
    for i in range(WORKERS):
        task = asyncio.create_task(worker(job_queue, response_queue, pbar))
        tasks.append(task)

    # Wait until the queue is fully processed.
    started_at = time.monotonic()
    await job_queue.join()
    total_slept_for = time.monotonic() - started_at
    # close progressbar for the worker()
    pbar.close()

    print('\n[i] getting book URLs...')
    await parse(response_queue, comments_jobs, book_queue)
    print('[i] got all book URLs.')

    while not book_queue.empty():
        all_books.append(book_queue.get_nowait())

    # run all jobs for comments of books to be scraped
    comment_pbar = tqdm(total=comments_jobs.qsize())
    comment_tasks = []
    for i in range(WORKERS):
        task = asyncio.create_task(comment_worker(comments_jobs, comments_response, comment_pbar))
        comment_tasks.append(task)

    await comments_jobs.join()
    comment_pbar.close()

    print('\n[i] getting book comments...')
    await get_books_comments(comments_response, comments_queue)
    print('[i] got all book comments.')

    while not comments_queue.empty():
        all_comments.append(comments_queue.get_nowait())

    # Cancel our worker tasks.
    for task in tasks:
        task.cancel()

    for task in comment_tasks:
        task.cancel()
    # Wait until all worker tasks are cancelled.
    await asyncio.gather(*tasks, return_exceptions=True)

    # Wait until all worker tasks are cancelled.
    await asyncio.gather(*comment_tasks, return_exceptions=True)

asyncio.run(main())

# create dataframe and save them
df = pd.DataFrame(all_comments)
df.to_excel('comments.xlsx')
df.to_csv('comments.csv')

df = pd.DataFrame(all_books)
df.to_excel('books.xlsx')
df.to_csv('books.csv')

# saved dataframes will be analysed using score_comments.py


import timeit
from functools import reduce

def main():
    with open('input.txt') as f:
        inputs = [[int(c) for c in line.strip()] for line in f.readlines()]

    counts = reduce(iter_add, inputs)
    gamma_rate_arr = [1 if c > len(inputs) / 2 else 0 for c in counts]

    gamma_rate = int(''.join(map(str, gamma_rate_arr)), base=2)
    epsilon_rate = (~gamma_rate) & int('1' * len(inputs[0]), base=2)

    power_consumption = gamma_rate * epsilon_rate

    print(f"part1: gamma: {gamma_rate}, epsilon: {epsilon_rate}, power_consumption: {power_consumption}")

    o2_filtered = inputs
    co2_filtered = inputs
    for index in range(len(inputs[0])):
        if len(o2_filtered) > 1:
            o2_counts = reduce(iter_add, o2_filtered)
            o2_most_common = [1 if c >= len(o2_filtered) / 2 else 0 for c in o2_counts]
            o2_target = o2_most_common[index]

            o2_filtered = list(filter(lambda line: line[index] == o2_target, o2_filtered))
        if len(co2_filtered) > 1:
            co2_counts = reduce(iter_add, co2_filtered)
            co2_least_common = [0 if c >= len(co2_filtered) / 2 else 1 for c in co2_counts]
            co2_target = co2_least_common[index]

            co2_filtered = list(filter(lambda line: line[index] == co2_target, co2_filtered))

    assert len(o2_filtered) == 1
    assert len(co2_filtered) == 1

    o2_rating = int(''.join(map(str, o2_filtered[0])), base=2)
    co2_rating = int(''.join(map(str, co2_filtered[0])), base=2)

    print(f"o2 rating: {o2_rating}, co2 rating: {co2_rating}, life support rating: {o2_rating * co2_rating}")

def iter_add(iter1, iter2):
    return map(sum, zip(iter1, iter2))

if __name__ == '__main__':
    #timeit.timeit('main()', number=5)
    main()


# -*- coding: utf-8 -*-

# Usage:
#
# {% user_avatar_url user [size_in_px|"small"|"medium"|"large"] %}
#
# user:
# django.contrib.auth.models.User object
#
# size_in_px:
# size of the result avatar (size_in_px x size_in_px)
#
# Examples:
# {% user_avatar_url request.user "18" %}
# {% user_avatar_url request.user "small" %}
# {% user_avatar_url request.user "medium" %}
# {% user_avatar_url request.user "large" %}

import urllib
from django import template
from django.conf import settings
from journalmanager.models import UserProfile

SIZE_LARGE = 64
SIZE_MEDIUM = 32
SIZE_SMALL = 18

register = template.Library()
def user_avatar_url(user, size):
    size = size.lower()
    if size == 'large':
        size = SIZE_LARGE
    elif size == 'medium':
        size = SIZE_MEDIUM
    elif size == 'small':
        size = SIZE_SMALL
    elif size.isdigit() and int(size) > 0:
        size = int(size)
    else:
        return '' # unknow size, no photo

    if not user or not user.is_authenticated() or not user.email:
        return ''

    try:
        user_profile = user.get_profile()
    except UserProfile.DoesNotExist:
        return ''
    else:
        params = urllib.urlencode({'s': size, 'd': 'mm'})
        gravatar_url = getattr(settings, 'GRAVATAR_BASE_URL', 'https://secure.gravatar.com')
        avartar_url = '{0}/avatar/{1}?{2}'.format(gravatar_url, user_profile.gravatar_id, params)
        return avartar_url

register.simple_tag(user_avatar_url)


# Requestes ( get, post )
from Comunication import comunication

# Get configs
from File import read

# To handle jsons
import json

# Make a get request to the server requiring the next animation
# Code of the request: 0x0001
# ARGUMENTS ( none )
# RETURN:
#	-byte[]: the server response
def board_server_next_animation():
	# Get the board configurations
	board_config = read.read_conf_file( "board.json" )

	# Make the request
	response = comunication.get( comunication.get_server_url(),
					  { "board_id": board_config[ "board_id" ], "code": 0x0001, "token": board_config[ "token" ] } )

	# Check the response
	if response.status_code != 200:
		return 0 # There was an error

	# Return the response content
	return response.content

# Sync the local data to the one of the board
# Code of the request: 0x0003
def board_server_send_sync():
	# Get the board configs
	board_config = read.read_conf_file( "board.json" )

	# Make the request
	response = comunication.get( comunication.get_server_url(),
								 { "board_id": board_config[ "board_id" ], "leds": board_config[ "leds_number" ], "code": 0x0003, "token": board_config[ "token" ] } )

	# Check the response
	if response.status_code != 200:
		return 0 # There was an error

	# Return the response content
	return response.content

# Require a specif animation to the server
# Code of the request: 0x0005
def board_server_specific_animation( default = False ):
	# Get the board configs
	board_config = read.read_conf_file( "board.json" )

	# Is the animation to get the ofline or the online
	if default:
		default = "remote_animation"
	else:
		default = "actual_animation"

	# Make the request
	response = comunication.get( comunication.get_server_url(),
								 { "board_id": board_config[ "board_id" ], "animation_name": board_config[ default ], "code": 0x0005, "token": board_config[ "token" ] } )

	# Check the response
	if response.status_code != 200:
		return 0 # There was an error

	# Return the response content
	return response.content

# The board requeires it's configuration to the server
def board_server_get_sync():
	return

# Checks if in a response there are some informations about the server asking something
def check_if_server_asking( response ):
	# Check the response dictionary passed

	# Asking should always be in position 1 of "Content"
	if "Asking" in response.contain[ "Content" ][ 1 ]:
		return True
	return False

def handle_server_hasking( server_demands ):
	# Check the demands dictionary passed

	# Broadcast the message
	comunication.broadcast( json.dumps( server_demands ) )

requests_codes = { 0x0001: board_server_next_animation, 0x0003: board_server_send_sync, 0x0005: board_server_specific_animation, 0x0007: board_server_get_sync }


import json
from threading import active_count
import matplotlib.pyplot as plt
import numpy as np
import PIL
import os
import datetime
import random
import tensorflow as tf
import tensorflow.python as tfp
from tensorflow.python.keras import layers
from tensorflow.python.keras.layers import Flatten, Input, Softmax
from tensorflow.python.keras.callbacks import TensorBoard, Callback
# from tf..keras.utils.vis_utils import plot_model

instance = int(datetime.datetime.now().timestamp())

print("instance is: ", instance)

class LossAndErrorPrintingCallback(Callback):
    def on_train_batch_end(self, batch, logs=None):
        print(
            "On batch {}, avg. loss {:7.2f}.".format(batch, logs["loss"])
        )

    def on_test_batch_end(self, batch, logs=None):
        print(
            "On batch {}, avg. loss {:7.2f}.".format(batch, logs["loss"])
        )

    def on_epoch_end(self, epoch, logs=None):
        cols = list(logs.keys())
        cols.sort()
        hcols = f"epoch,{','.join(cols)},\n"
        vcols = f"{epoch},"

        for c in cols:
            vcols += f"{logs[c]},"
        vcols += "\n"

        if f"epoch_training_log_{instance}.csv" not in os.listdir('logs/'):
            with open(f"logs/epoch_training_log_{instance}.csv", "w+") as touch:
                touch.write(hcols)

        with open(f"logs/epoch_training_log_{instance}.csv", "a+") as fp:
            fp.write(vcols)

print(tf.__version__, "\n")

def acacia_load_data(train=18000, test=2000, per_img=400):

    images = {"Acacia Species": [], "Other": []}
    class_names = []

    for i, species in enumerate(os.listdir('Sorted')):
        class_names.append(species)
        lst = list(os.listdir(f'Sorted/{species}'))
        random.shuffle(lst)

        for j, file in enumerate(lst):
            if len(images[species]) >= round(train / 2):
                break

            try:
                data = json.load(open(f'Sorted/{species}/{file}', 'r'))
                random.shuffle(data)
                for img in data[:per_img]:
                    images[species].append((img, i))
            except:
                continue

            print(
                f'loaded file {file} totals {len(images["Acacia Species"])} Acacia and {len(images["Other"])} Other')

    print(f"Acacia {len(images['Acacia Species'])} Other {len(images['Other'])}")

    random.shuffle(images["Acacia Species"])
    random.shuffle(images["Other"])

    combined = images["Acacia Species"][:(
        train + test)//2] + images["Other"][:(train + test)//2]
    random.shuffle(combined)

    train_images = np.array([x[0] for x in combined[:train]])
    train_labels = np.array([x[1] for x in combined[:train]])
    test_images = np.array([x[0] for x in combined[train:train+test]])
    test_labels = np.array([x[1] for x in combined[train:train+test]])

    return ((train_images, train_labels), (test_images, test_labels), class_names)

(train_images, train_labels), (test_images, test_labels), class_names = acacia_load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0

def createModel_tfp():
    model = tfp.keras.Sequential([
        tfp.keras.layers.InputLayer(input_shape=(96, 96, 3)),
        tfp.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
        tfp.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        tfp.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
        tfp.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        tfp.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
        tfp.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        tfp.keras.layers.Conv2D(128, 3, padding='same', activation='relu'),
        # tfp.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        # tfp.keras.layers.Conv2D(256, 3, padding='same', activation='relu'),
        tfp.keras.layers.AveragePooling2D(),
        tfp.keras.layers.Flatten(),
        tfp.keras.layers.Dense(128, activation='relu'),
        tfp.keras.layers.Dense(2)
    ])
    return model

# def createModel_top():
#     model = tf.keras.Sequential([
#         tf.keras.layers.InputLayer(input_shape=(96, 96, 3)),
#         tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
#         tf.keras.layers.MaxPooling2D(),
#         # tfp.keras.layers.AveragePooling2D(),
#         tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
#         tf.keras.layers.MaxPooling2D(),
#         # tfp.keras.layers.AveragePooling2D(),
#         tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
#         tf.keras.layers.MaxPooling2D(),
#         # tfp.keras.layers.AveragePooling2D(),
#         tf.keras.layers.Conv2D(128, 3, padding='same', activation='relu'),
#         tf.keras.layers.MaxPooling2D(),
#         # tf.keras.layers.AveragePooling2D(),
#         tf.keras.layers.Conv2D(256, 3, padding='same', activation='relu'),
#         tf.keras.layers.AveragePooling2D(),
#         tf.keras.layers.Flatten(),
#         tf.keras.layers.Dense(128, activation='relu'),
#         tf.keras.layers.Dense(2)
#     ])
#     return model

def createModel():
    model = tf.keras.Sequential([
        tf.keras.layers.InputLayer(input_shape=(96, 96, 3)),
        tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        # tfp.keras.layers.AveragePooling2D(),
        tf.keras.layers.Conv2D(128, 3, padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(),
        # tf.keras.layers.AveragePooling2D(),
        tf.keras.layers.Conv2D(256, 3, padding='same', activation='relu'),
        tf.keras.layers.AveragePooling2D(),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(2)
    ])
    return model

model = createModel_tfp()

# model = createModel()
# tf.keras.utils.plot_model(model, to_file=f'net_model_{instance}.png',
#                           show_shapes=True, show_layer_names=False)
# print("saved model arch to [net_model.png] [enter] proceeding to training")

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(
                  from_logits=True),
              metrics=['accuracy', 'mae', 'mse', 'RootMeanSquaredError'])

model.fit(train_images, train_labels, epochs=50,
          callbacks=[LossAndErrorPrintingCallback()])

print('#############')
print(model.evaluate(test_images, test_labels, verbose=2))
print('#############')

test_loss, test_acc, test_mae, test_mse, test_rmse = model.evaluate(test_images, test_labels, verbose=2)

print('\nTest accuracy:', test_acc)

mfile = F'Models/AcaciaNorthBearing_{round(test_acc, 2)}_{instance}.h5'
model.save(mfile)
print("saved model to ", mfile)


import sys

sys.path.append("../../../Asset Wealth")

import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from osgeo import osr
from osgeo import gdal
from src.data_utils import calc_mean
from src.data_utils import calc_std

from src.config import clipping_values

def slice_to_input_size(array: np.ndarray, input_height: int, input_width: int):
    '''Slice image array to desired input shape.

    Args:
        array (np.ndarray): Numpy array containing image data
        input_height (int): Uniform image height to slice to
        input_width (int):  Uniform image width to slice to

    Returns:
        array (np.array): Numpy array containing image data in shape of input height/width and Bandwidth
    '''
    if len(array.shape) == 3:
        outer_pixels_y = int((array.shape[1] - input_height) / 2)
        outer_pixels_x = int((array.shape[2] - input_width) / 2)
        array = array[:, outer_pixels_y:-outer_pixels_y, outer_pixels_x:-outer_pixels_x]
        if array.shape[1] == input_height + 1:
            array = array[:, :-1, :]
        if array.shape[2] == input_width + 1:
            array = array[:, :, :-1]
    elif len(array.shape) == 2:
        outer_pixels_y = int((array.shape[0] - input_height) / 2)
        outer_pixels_x = int((array.shape[1] - input_width) / 2)
        array = array[outer_pixels_y:-outer_pixels_y, outer_pixels_x:-outer_pixels_x]
        if array.shape[0] == input_height + 1:
            array = array[:-1, :]
        if array.shape[1] == input_width + 1:
            array = array[:, :-1]
    return array

def standardize_resize(img: str, img_path: str,  input_height: str, input_width: str, clipping_values: list,
                     means=False, stds=False, add_img_path=False, standardize=False):
    '''Standardize and resize GeoTIFFs.
    Standardization is performed per band using Standard Scaler.
    Resizing is performed by slicing to the center of the image in shape of provided input Size.
    For VIIRS images, the band is tripled to fit RGB input shape of common CNNs.
    Standardized and resized images are stored in a new GeoTIFF.

    Args:
        img (str):                  Filename of image to normalize and resize
        img_path (str):             Path to image data
        input_height (int):         Desired input height
        input_width (int):          Desired input width
        clipping_values (list):     Interval of min and max values for clipping
        means (bool/np.ndarray):         Optional: Result of calc_mean: Mean of pixel values for each channel
        stds (bool/np.ndarray):          Optional: Result of calc_mean: Standard deviation of pixel values for each channel
        add_img_path (bool/str):    Optional: Path to image data to add (eg. for combining Sentinel-2 and VIIRS)
        standardize (bool):                Optional: Whether or not to standardize image data (e.g. standardization is not needed when already normalized Sentinel-2 and VIIRS data is merged)

    '''
    ds = gdal.Open(os.path.join(img_path, img))
    geotransform = ds.GetGeoTransform()
    array = ds.ReadAsArray()

    array[np.isnan(array)] = 0
    assert not np.any(np.isnan(array)), "Float"

    if add_img_path:
        ds = gdal.Open(os.path.join(add_img_path, img))
        geotransform = ds.GetGeoTransform()
        array_to_add = ds.ReadAsArray()

        array_to_add[np.isnan(array_to_add)] = 0
        assert not np.any(np.isnan(array_to_add)), "Float"
    # Clipping
    array = np.clip(array, a_min=clipping_values[0], a_max=clipping_values[1])

    assert not np.any(np.isnan(array)), "After clipping"
    # Normalize the array
    if standardize:
        if 'Sentinel' in img_path:
            array = ((array.transpose(1, 2, 0) - means) / stds).transpose(2, 0, 1)
        elif 'VIIRS' in img_path:
            array = ((array.transpose(0, 1) - means) / stds).transpose(1, 0)
        assert not np.any(np.isnan(array)), "Normalize"

    # Resize the array to ensure that all image arrays have the same size
    if len(array.shape) == 3:
        if array.shape != (array.shape[0], input_height, input_width):
            if array.shape[1] > input_height:
                array = slice_to_input_size(array, input_height, input_width)

        assert array.shape == (array.shape[0], input_height, input_width)
    elif len(array.shape) == 2:
        if array.shape != (input_height, input_width):
            if array.shape[0] > input_height:
                array = slice_to_input_size(array, input_height, input_width)

        assert array.shape == (input_height, input_width)
    # return normalized and resized image array

    # Create new geotiff
    if add_img_path:
        dst_ds = gdal.GetDriverByName('GTiff').Create(
            os.path.join('/mnt/datadisk/data/VIIRS_Sentinel2/preprocessed/asset/',
                         img_path.split('/')[-2],
                         img_path.split('/')[-1],
                         img), input_height, input_width,len(array) + 1, gdal.GDT_Byte)
    if 'Sentinel' in img_path:
        dst_ds = gdal.GetDriverByName('GTiff').Create(os.path.join(img_path, img), input_height, input_width,
                                                      len(array), gdal.GDT_Byte)
    elif 'VIIRS' in img_path:
        dst_ds = gdal.GetDriverByName('GTiff').Create(os.path.join(img_path, img), input_height, input_width, 3,
                                                      gdal.GDT_Byte)
    dst_ds.SetGeoTransform(geotransform)  # specify coords
    srs = osr.SpatialReference()  # establish encoding
    srs.ImportFromEPSG(4326)  # WGS84 lat/long
    dst_ds.SetProjection(srs.ExportToWkt())  # export coords to file
    if 'Sentinel' in img_path:
        for i in range(len(array)):
            dst_ds.GetRasterBand(i + 1).WriteArray(array[i])  # write band to the raster
    elif 'VIIRS' in img_path:
        array = np.concatenate((np.array([array]),) * 3, axis=0)

        for i in range(len(array)):
            dst_ds.GetRasterBand(i + 1).WriteArray(array[i])  # write band to the raster
    if add_img_path:
        for i in range(len(array)):
            dst_ds.GetRasterBand(i + 1).WriteArray(array[i])  # write band to the raster
        dst_ds.GetRasterBand(i + 2).WriteArray(array_to_add[0])  # write (viirs) band to the raster
    dst_ds.FlushCache()  # write to disk
    dst_ds = None

def main(img_path: str, ur: str, year: str, input_height: str, input_width: str,
         clipping_values: list, channels: list, add_img_path=False, standardize=False):
    '''Preprocessing of GeoTIFFs.
    Preprocessing includes slicing to desired input size and standardizing the image data.

    Args:
        img_path (str):             Path to image data
        ur (str):                   'u' for urban, 'r' for rural
        year (str):                 timespan (2012_2014 / 2016_2020) or all data
        input_height (int):         Desired input height
        input_width (int):          Desired input width
        clipping_values (list):     Interval of min and max values for clipping
        channels (list):            List of channels to use. [] to use all channels.
        add_img_path (bool/str):    Optional: Path to image data to add (eg. for combining Sentinel2 and VIIRS)
        standardize:                Optional: Whether or not to standardize image data (e.g. standardization is not needed when already
                                    normalized Sentinel-2 and VIIRS data is merged)
    '''

    # Get Splits to Normalize Train/Val and Test separately
    X_train_val, X_test = train_test_split([i for i in os.listdir(img_path) if i.endswith('.tif')], test_size=0.20,
                                           random_state=42)
    ds = gdal.Open(os.path.join(img_path, X_train_val[0]))
    geotransform = ds.GetGeoTransform()

    array = ds.ReadAsArray()
    array[np.isnan(array)] = 0
    assert not np.any(np.isnan(array)), "Float"

    # Clipping
    array = np.clip(array, a_min=clipping_values[0], a_max=clipping_values[1])

    assert not np.any(np.isnan(array)), "After clipping"
    # Normalize the array

    if standardize:
        # Calculate Means and Standard Deviations per Band
        if 'Sentinel' in img_path:
            if os.path.exists('./s2_means_stds.csv'):
                means_stds_df = pd.read_csv('./s2_means_stds.csv', )

                if ur + '_train_val_means' in means_stds_df.columns:
                    print('Mean and Standard Deviation Values found:')
                    train_val_means = means_stds_df[ur + '_train_val_means'].to_numpy()
                    train_val_stds = means_stds_df[ur + '_train_val_stds'].to_numpy()

                    test_means = means_stds_df[ur + '_test_means'].to_numpy()
                    test_stds = means_stds_df[ur + '_test_stds'].to_numpy()
                    print(train_val_means.shape, train_val_stds.shape, test_means.shape, test_stds.shape)
                else:
                    train_val_means = calc_mean(img_path, X_train_val, input_height, input_width, clipping_values, channels)
                    train_val_stds = calc_std(train_val_means, img_path, X_train_val, input_height, input_width,
                                              clipping_values, channels)

                    test_means = calc_mean(img_path, X_test, input_height, input_width, clipping_values, channels)
                    test_stds = calc_std(train_val_means, img_path, X_test, input_height, input_width, clipping_values,
                                         channels)

                    means_stds_df[ur + '_train_val_means'] = train_val_means
                    means_stds_df[ur + '_train_val_stds'] = train_val_stds
                    means_stds_df[ur + '_test_means'] = test_means
                    means_stds_df[ur + '_test_stds'] = test_stds
                    means_stds_df.to_csv('./s2_means_stds.csv')
            else:
                train_val_means = calc_mean(img_path, X_train_val, input_height, input_width, clipping_values, channels)
                train_val_stds = calc_std(train_val_means, img_path, X_train_val, input_height, input_width,
                                          clipping_values, channels)

                test_means = calc_mean(img_path, X_test, input_height, input_width, clipping_values, channels)
                test_stds = calc_std(train_val_means, img_path, X_test, input_height, input_width, clipping_values,
                                     channels)
                means_stds_df = pd.DataFrame(list(zip(train_val_means, train_val_stds, test_means, test_stds)),
                                             columns=[ur + '_train_val_means', ur + '_train_val_stds', ur + '_test_means',
                                                      ur + '_test_stds'])
                means_stds_df.to_csv('./s2_means_stds.csv')
        elif 'VIIRS' in img_path:
            if os.path.exists('./viirs_means_stds.csv'):
                means_stds_df = pd.read_csv('./viirs_means_stds.csv', )

                if ur + '_' + '_train_val_means' in means_stds_df.columns:
                    print('Mean and Standard Deviation Values found:')
                    train_val_means = means_stds_df[ur + '_' + year + '_train_val_means'].to_numpy()
                    train_val_stds = means_stds_df[ur + '_' + year + '_train_val_stds'].to_numpy()

                    test_means = means_stds_df[ur + '_' + year + '_test_means'].to_numpy()
                    test_stds = means_stds_df[ur + '_' + year + '_test_stds'].to_numpy()
                    print(train_val_means.shape, train_val_stds.shape, test_means.shape, test_stds.shape)
                else:
                    train_val_means = calc_mean(img_path, X_train_val, input_height, input_width, clipping_values, channels)
                    train_val_stds = calc_std(train_val_means, img_path, X_train_val, input_height, input_width,
                                              clipping_values, channels)

                    test_means = calc_mean(img_path, X_test, input_height, input_width, clipping_values, channels)
                    test_stds = calc_std(test_means, img_path, X_test, input_height, input_width, clipping_values, channels)

                    means_stds_df[ur + '_' + year + '_train_val_means'] = train_val_means
                    means_stds_df[ur + '_' + year + '_train_val_stds'] = train_val_stds
                    means_stds_df[ur + '_' + year + '_test_means'] = test_means
                    means_stds_df[ur + '_' + year + '_test_stds'] = test_stds
                    means_stds_df.to_csv('./viirs_means_stds.csv')
            else:
                train_val_means = calc_mean(img_path, X_train_val, input_height, input_width, clipping_values, channels)
                train_val_stds = calc_std(train_val_means, img_path, X_train_val, input_height, input_width,
                                          clipping_values, channels)

                test_means = calc_mean(img_path, X_test, input_height, input_width, clipping_values, channels)
                test_stds = calc_std(test_means, img_path, X_test, input_height, input_width, clipping_values, channels)
                means_stds_df = pd.DataFrame(list(zip(train_val_means, train_val_stds, test_means, test_stds)),
                                             columns=[ur + '_' + year + '_train_val_means',
                                                      ur + '_' + year + '_train_val_stds',
                                                      ur + '_' + year + '_test_means',
                                                      ur + '_' + year + '_test_stds'])
                means_stds_df.to_csv('./viirs_means_stds.csv')

    # Normalize and Resize Images and save the preprocessed Images for Training
    for image in tqdm(X_train_val):
        standardize_resize(img=image, img_path=img_path, input_height=input_height, input_width=input_width,
                           clipping_values=clipping_values, means=train_val_means, stds=train_val_stds,
                           standardize=True)

    for image in tqdm(X_test):
        standardize_resize(img=image, img_path=img_path, input_height=input_height, input_width=input_width,
                           clipping_values=clipping_values, means=test_means, stds=test_stds,
                           standardize=True)

if __name__ == '__main__':

    main(
        img_path='/mnt/datadisk/data/VIIRS/preprocessed/asset/urban/mozambique_2016_2021',
        ur='u',
        year='all',
        input_height=200,
        input_width=200,
        clipping_values=clipping_values,
        channels=[],
        standardize = True
    )


### This is a python script to merge 2 files. This code is used to merge ORFs file and a contaminants list.

import argparse

parser = parser = argparse.ArgumentParser(description='This is a python script to merge two files.')
parser.add_argument("-1", "--file1", help="full path of file1", metavar="File path")
parser.add_argument("-2", "--file2", help="full path of file2", metavar="File path")
parser.add_argument("-o", "--out", help="full path of of output file", metavar="File path")

args = parser.parse_args()
merge(args.file1, args.file2, args.out)

def merge(file1, file2, out):
    with open(file1,'r') as f1, open(file2,'r') as f2, open(out, 'w') as o:
        for line in f1:
            o.write(line)
        for line in f2:
            o.write(line)


"""Blender benchmark test script"""
from argparse import ArgumentParser
import json
import logging
import os.path
import subprocess
from zipfile import ZipFile
import requests

# omit the first arg which is the script name
parser = ArgumentParser()
parser.add_argument("-s", "--scene", dest="scene",
                    help="blender scene", metavar="scene", required=True)
parser.add_argument("-d", "--device", dest="device",
                    help="device", metavar="device", required=True)
parser.add_argument("-v", "--version", dest="version",
                    help="version", metavar="version", required=True)

args = parser.parse_args()

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
log_dir = os.path.join(SCRIPT_DIR, "run")
if not os.path.isdir(log_dir):
    os.mkdir(log_dir)
LOGGING_FORMAT = '%(asctime)s %(levelname)-s %(message)s'
logging.basicConfig(filename=f'{log_dir}/harness.log',
                    format=LOGGING_FORMAT,
                    datefmt='%m-%d %H:%M',
                    level=logging.DEBUG)
console = logging.StreamHandler()
formatter = logging.Formatter(LOGGING_FORMAT)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

EXECUTABLE = "benchmark-launcher-cli.exe"
ABS_EXECUTABLE_PATH = os.path.join(SCRIPT_DIR, EXECUTABLE)

def download_and_extract_cli():
    """Downloads and extracts blender benchmark CLI"""
    download_url = "https://download.blender.org/release/BlenderBenchmark2.0/launcher/benchmark-launcher-cli-3.1.0-windows.zip"
    zip_path = os.path.join(
        SCRIPT_DIR, "benchmark-launcher-cli-3.1.0-windows.zip")
    response = requests.get(download_url, allow_redirects=True, timeout=120)
    with open(zip_path, 'wb') as zip_file:
        zip_file.write(response.content)
    with ZipFile(zip_path, 'r') as zip_object:
        zip_object.extractall(path=SCRIPT_DIR)

if os.path.isfile(ABS_EXECUTABLE_PATH) is False:
    logging.info("Downloading blender benchmark CLI")
    download_and_extract_cli()

VERSION = ""
SCENE = []
DEVICE_TYPE = ""

run_array = [ABS_EXECUTABLE_PATH, "blender", "download", args.version]
process = subprocess.run(
    run_array, capture_output=True, text=True, check=False)

if process.returncode > 0:
    logging.info(process.stdout)
    logging.info(process.stderr)
    logging.error("Test failed!")

if args.scene.lower() == "all":
    SCENE = ["monster", "classroom", "junkshop"]
else:
    SCENE = [args.scene]

if args.device.lower() == "cpu":
    DEVICE_TYPE = "CPU"
else:
    run_array = [ABS_EXECUTABLE_PATH, "devices", "-b", args.version]
    process = subprocess.run(
        run_array, capture_output=True, text=True, check=False)

    if process.returncode > 0:
        logging.info(process.stdout)
        logging.info(process.stderr)
        logging.error("Test failed!")
    else:
        logging.info(process.stdout)
        logging.info(process.stderr)
        if "OPTIX" in process.stdout or "OPTIX" in process.stderr:
            DEVICE_TYPE = "OPTIX"  # nvidia
        if "CUDA" in process.stdout or "CUDA" in process.stderr:
            DEVICE_TYPE = "CUDA"  # older non rtx nvidia
        elif "HIP" in process.stdout or "HIP" in process.stderr:
            DEVICE_TYPE = "HIP"  # amd
        elif "oneAPI" in process.stdout or "oneAPI" in process.stderr:
            DEVICE_TYPE = "oneAPI"  # intel

arg_string = ["blender", "list"]
run_array = [ABS_EXECUTABLE_PATH, "benchmark"] + SCENE + \
    ["-b", args.version, "--device-type", DEVICE_TYPE, "--json"]
logging.info("Running with arguments %s", run_array)
process = subprocess.run(
    run_array, capture_output=True, text=True, check=False)

if process.returncode > 0:
    logging.error(process.stderr)
    logging.error("Test failed!")

json_array = json.loads(process.stdout)

json_report = []
for report in json_array:
    scene_report = {
        "timestamp": report['timestamp'],
        "version": report['blender_version']['version'],
        "scene": report['scene']['label'],
        "score": round(report['stats']['samples_per_minute'], 2),
        "device": report['device_info']['compute_devices'][0]['name']
    }

    logging.info(json.dumps(scene_report, indent=2))
    scene_report['version_json'] = report['blender_version']
    scene_report['results_json'] = report['stats']
    json_report.append(scene_report)

with open(os.path.join(log_dir, "report.json"), "w", encoding="utf-8") as file:
    file.write(json.dumps(json_report))

logging.info('Test finished!')


import json
import logging
import time
import datetime

from dateutil import parser
from psycopg2._psycopg import OperationalError as PsycopOperationalError, InterfaceError as PsycopInterfaceError
from django.db import connection
from django.db.utils import OperationalError as DjangoOperationalError, InterfaceError as DjangoInterfaceError
from django.conf import settings
from attribution.models import attribution_new as mdl_attribution_new
from attribution.models.attribution_new import AttributionNew

DELETE_OPERATION = "delete"
UPDATE_OPERATION = "update"
ERROR_EPC_FIELD = "error"
LEARNING_CONTAINER_YEAR_PREFIX_EXTERNAL_ID = "osis.learning_container_year_"
TUTOR_PREFIX_EXTERNAL_ID = "osis.tutor_"

logger = logging.getLogger(settings.DEFAULT_LOGGER)
queue_exception_logger = logging.getLogger(settings.QUEUE_EXCEPTION_LOGGER)

def process_message(body):
    """
        Callback of APPLICATION_RESPONSE queue
    :param json_data:
    :return:
    """
    try:
        json_data = body.decode("utf-8")
        new_applications = json.loads(json_data)
        _update_applications_list(new_applications)
    except (PsycopOperationalError, PsycopInterfaceError, DjangoOperationalError, DjangoInterfaceError):
        queue_exception_logger.exception('Postgres Error during process tutor application message => retried')
        connection.close()
        time.sleep(1)
        process_message(body)
    except Exception:
        logger.exception('(Not PostgresError) during process tutor application message. Cannot update ')

def _update_applications_list(new_applications):
    for new_application in new_applications:
        global_id = new_application.get('global_id')
        attribution_new = mdl_attribution_new.find_by_global_id(global_id)
        if attribution_new:
            applications_list = []
            if not attribution_new.applications:
                _merge_applications_list(new_application.get('tutor_applications'), attribution_new)
            else:
                for application in new_application['tutor_applications']:
                    existing_application = next((data for data in attribution_new.applications if data.get("year") == application.get("year") and data.get("acronym") == application.get("acronym")), None)
                    if existing_application:
                        if _check_if_update(application, existing_application):
                            applications_list.append(application)
                            attribution_new.applications.remove(existing_application)
                        else:
                            applications_list.append(existing_application)
                            attribution_new.applications.remove(existing_application)
                    else:
                        applications_list.append(application)
                _merge_applications_list(applications_list, attribution_new)
        else:
            attribution_new = AttributionNew(global_id=global_id, applications=new_application.get('tutor_applications'))
            attribution_new.save()

def _check_if_update(application, existing_application):
    if "pending" not in existing_application or (existing_application["pending"] == UPDATE_OPERATION and parser.parse(
            existing_application.get("last_changed")) < parser.parse(application.get("last_changed"))):
        return True

def _merge_applications_list(applications_list, attribution_new):
    attribution_new.applications = applications_list
    attribution_new.save()


"""
플라스크에서 @app.before_request 데코레이터는 요청이 만들어지고 뷰에 전달하기 직전에 호출할 함수가 있을 때 사용

before_request를 사용하는 일반적인 경우는 전역 공간에 값을 저장할 때다.
이렇게 하면 요청 컨텍스트 안에서 호출되는 모든 함수는 g변수를 사용해서 데이터를 얻을 수 있다.

다음예제에서 HTTP 기본인증을 처리할 때 요청 컨텍스트의 username을 g변수의 user 속성에 저장한다.

"""

from flask import Flask , jsonify, g,request

app = Flask(__name__)

@app.before_request
def authenticate():
    if request.authorization:
        g.user = request.authorization['username']
    else:
        g.user = 'Anonymous'

@app.route('/api')
def my_microservice():
    return jsonify({'Hello':g.user})

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

#클라이언트가 /api 엔드포인트를 호출하면 제공된 헤더 정보에 맞는 g.user 값이 authenticate()함수에서 설정된다.


#Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LogisticRegression, SGDClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from mlxtend.plotting import plot_decision_regions
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
import ipywidgets as widgets
from JaverianaML.classification import LDA
from IPython.display import display
import scipy.stats as stats
import sklearn as skl

##Normal distribution for iris dataset
def Binary_Gaussian_Plot(DB, shared_variance=False):
    '''
    Density plot of P(petal_width|y=c) assuming Gaussin pdfs for each class
    '''
    fig, ax = plt.subplots(1,2,figsize=(20,7))
    iris = sns.load_dataset('iris')
    iris = iris.loc[iris.species != 'setosa', :]
    plt.figure(figsize=(15,7))

    mu1 = iris.groupby('species')['petal_width'].mean()['virginica']
    std1 = iris.groupby('species')['petal_width'].std()['virginica']

    mu2 = iris.groupby('species')['petal_width'].mean()['versicolor']
    std2 = iris.groupby('species')['petal_width'].std()['versicolor']

    if shared_variance:
        std =np.sqrt( 0.5*std2**2 +  0.5*std1**2 )
        std1 = std
        std2 = std

    x1 = np.linspace(mu1 - 3*std1, mu1 + 3*std1, 100)
    ax[0].fill_between(x1, stats.norm.pdf(x1, mu1, std1)*0.5, alpha=0.5, label="virginica")

    x2 = np.linspace(mu2 - 3*std2, mu2 + 3*std2, 100)
    ax[0].fill_between(x2, stats.norm.pdf(x2, mu2, std2)*0.5, alpha=0.5, label="versicolor")
    sns.scatterplot(data=iris, x='petal_width', y='species', hue='species', hue_order =['virginica', 'versicolor'], ax=ax[1])

    ax[0].vlines(DB,0,1)
    ax[1].vlines(DB,0,1)
    ax[0].text(0.7,0.9, r'$P(X|C_1)P(C_1)$', fontsize=20)
    ax[0].text(1.9,0.9, r'$P(X|C_2)P(C_2)$', fontsize=20)
    ax[0].set_xlabel("petal_width")
    ax[0].set_ylabel("Density")
    ax[0].legend()

def Multiclass_Gaussian_Plot(DB1, DB2, shared_variance=False):
    '''
    Density plot of P(petal_width|y=c) assuming Gaussin pdfs for each class
    '''
    fig, ax = plt.subplots(1,2,figsize=(25,7))
    iris = sns.load_dataset('iris')
    plt.figure(figsize=(25,7))

    mu1 = iris.groupby('species')['petal_width'].mean()['virginica']
    std1 = iris.groupby('species')['petal_width'].std()['virginica']

    mu2 = iris.groupby('species')['petal_width'].mean()['versicolor']
    std2 = iris.groupby('species')['petal_width'].std()['versicolor']

    mu3 = iris.groupby('species')['petal_width'].mean()['setosa']
    std3 = iris.groupby('species')['petal_width'].std()['setosa']

    if shared_variance:
        std =np.sqrt( (50/150)*std2**2 +  (50/150)*std1**2 + (50/150)*std3**2 )
        std1 = std
        std2 = std
        std3 = std

    x1 = np.linspace(mu1 - 3*std1, mu1 + 3*std1, 100)
    ax[0].fill_between(x1, stats.norm.pdf(x1, mu1, std1)*0.5, alpha=0.5, label="virginica")

    x2 = np.linspace(mu2 - 3*std2, mu2 + 3*std2, 100)
    ax[0].fill_between(x2, stats.norm.pdf(x2, mu2, std2)*0.5, alpha=0.5, label="versicolor")

    x3 = np.linspace(mu3 - 3*std3, mu3 + 3*std3, 100)
    ax[0].fill_between(x3, stats.norm.pdf(x3, mu3, std3)*0.5, alpha=0.5, label="versicolor")

    sns.scatterplot(data=iris, x='petal_width', y='species', hue='species', hue_order =['virginica', 'versicolor', 'setosa'], ax=ax[1])

    ax[0].vlines(DB1,0,1.8)
    ax[1].vlines(DB1,0,2)

    ax[0].vlines(DB2,0,1.8)
    ax[1].vlines(DB2,0,2)

    ax[0].text(0.9,1.0, r'$P(X|C_1)P(C_1)$', fontsize=15)
    ax[0].text(1.9,1.0, r'$P(X|C_2)P(C_2)$', fontsize=15)
    ax[0].text(0.0, 1.0, r'$P(X|C_3)P(C_3)$', fontsize=15)
    ax[0].set_xlabel("petal_width")
    ax[0].set_ylabel("Density")
    ax[0].legend()

def Gaussian_3d_plot(w0, w):

    from plotly.subplots import make_subplots
    import scipy.stats as stats
    import plotly.graph_objects as go
    import plotly.express as px
    iris = sns.load_dataset('iris')
    iris = iris.loc[iris.species != 'setosa', :]

    means = iris.groupby("species", as_index=False).mean()[['species','petal_width', 'petal_length']]
    mu1 = means.loc[means.species=='versicolor', ['petal_width', 'petal_length']].to_numpy().ravel()
    mu2 = means.loc[means.species=='virginica', ['petal_width', 'petal_length']].to_numpy().ravel()

    cov1 = iris.loc[iris.species=='versicolor' ,['petal_width', 'petal_length']].cov().to_numpy()
    cov2 = iris.loc[iris.species=='virginica' ,['petal_width', 'petal_length']].cov().to_numpy()

    cov = 0.5*cov1 + 0.5*cov2

    xs = np.linspace(0.5, 3.0, 100)
    zs = np.linspace(0, 1.4, 100)

    X, Z = np.meshgrid(xs, zs)
    Y = w*X + w0

    x1, y1 = np.mgrid[0.5:1.9:200j, 2.5:5.3:200j]
    x2, y2 = np.mgrid[1.3:2.6:200j, 4.5:7:200j]

    xy1 = np.column_stack([x1.flat, y1.flat])
    xy2 = np.column_stack([x2.flat, y2.flat])

    cmap = plt.get_cmap("tab10")
    colorscale = [[0, 'rgb' + str(cmap(1)[0:3])], [1, 'rgb' + str(cmap(2)[0:3])]]

    z1 = stats.multivariate_normal.pdf(xy1, mean=mu1, cov=cov)
    z1 = z1.reshape(x1.shape)

    z2 = stats.multivariate_normal.pdf(xy2, mean=mu2, cov=cov)
    z2 = z2.reshape(x2.shape)

    fig = make_subplots(rows=1, cols=2, specs=[[{'is_3d': True}, {'is_3d': False}]])
    trace_a = go.Surface(x=x1, y=y1, z=z1, cmin=0, cmax=1, surfacecolor=np.zeros(shape=z1.shape), colorscale= colorscale, showscale=False, name="versicolor", opacity=0.8)
    trace_b = go.Surface(x=x2, y=y2,z=z2, cmin=0, cmax=1, surfacecolor=np.ones(shape=z2.shape) , colorscale= colorscale, showscale=False, name="virginica", opacity=0.8)
    trace_c = go.Surface(x=X, y=Y,z=Z, cmin=0, cmax=1, surfacecolor=np.ones(shape=z2.shape)*0.5 , colorscale= colorscale, showscale=False, name="virginica")
    fig.add_traces([trace_a, trace_b, trace_c], rows=1, cols=1)
    fig.update_scenes(xaxis_title_text="petal_width", yaxis_title_text="petal_length")

    scatter_1 = go.Scatter(x=iris[iris.species=='versicolor'].petal_width, y=iris[iris.species=='versicolor'].petal_length,  mode='markers', name='versicolor', marker_color='red')
    scatter_2 = go.Scatter(x=iris[iris.species=='virginica'].petal_width, y=iris[iris.species=='virginica'].petal_length,  mode='markers', name='virginica', marker_color='green')

    x=np.linspace(1,2.5,100)
    y=w*x+w0
    line = go.Scatter(x=x, y=y,  mode='lines', name='virginica', marker_color='black')

    fig.add_traces([scatter_1, scatter_2, line], rows=1, cols=2)
    fig.update_yaxes(title_text="petal_length", row=1, col=2)
    fig.update_xaxes(title_text="petal_width", row=1, col=2)
    fig.update_layout(width=900*2, height=600)
    fig.show()

def LDA_on_different_datasets(dataset='blobs'):

    """
    This function fits LDA model using different synthetic datasets

    Arguments:
        dataset: string
            One of 'blobs', 'circle' or 'moons' which indicates the
            sythetic dataset to be created.
    """

    #Create sinthetic dataset
    if dataset == 'blobs':
        X, y = skl.datasets.make_blobs(n_samples=1000, cluster_std=3, centers=2, random_state=42)
    elif dataset == 'circle':
        X, y = skl.datasets.make_circles(n_samples=1000, noise=0.1, factor=0.5, random_state=42)
    elif dataset == 'moons':
        X, y = skl.datasets.make_moons(n_samples=1000, noise=0.1, random_state=42)

    #Fit linear discriminanat analysis model
    plt.figure(figsize=(10,8))
    plot_decision_regions(X, y, clf=LinearDiscriminantAnalysis().fit(X,y))
    plt.xlabel(r'$x_1$', fontsize=20)
    plt.ylabel(r'$x_1$', fontsize=20)

def QDA_on_different_datasets(dataset='blobs'):

    """
    This function fits QDA model using different synthetic datasets

    Arguments:
        dataset: string
            One of 'blobs', 'circle' or 'moons' which indicates the
            sythetic dataset to be created.
    """

    #Create sinthetic dataset
    if dataset == 'blobs':
        X, y = skl.datasets.make_blobs(n_samples=1000, cluster_std=3, centers=2, random_state=42)
    elif dataset == 'circle':
        X, y = skl.datasets.make_circles(n_samples=1000, noise=0.1, factor=0.5, random_state=42)
    elif dataset == 'moons':
        X, y = skl.datasets.make_moons(n_samples=1000, noise=0.1, random_state=42)

    #Fit linear discriminanat analysis model
    plt.figure(figsize=(10,8))
    plot_decision_regions(X, y, clf=QuadraticDiscriminantAnalysis().fit(X,y))
    plt.xlabel(r'$x_1$', fontsize=20)


import configparser
import random

import pygame as pg
from PIL import Image, ImageEnhance
from pygame.math import Vector2

from game.base.base import DynamicObject, StaticObject

class Background(StaticObject):

    def __init__(self, display):
        config = configparser.ConfigParser()
        config.read('config.ini')
        super().__init__(display)

        self._display_size = self._display.get_size()
        self._sprite = "./data/background.jpg"
        self._preloaded_image = pg.image.load(self._sprite).convert()  # предзагруженный исходный спрайт

        self.image = pg.transform.scale(self._preloaded_image, self._display_size)
        self.rect = self.image.get_rect(center=(self._display_size[0] // 2, self._display_size[1] // 2))

        nums = int(config["Objects"]["n_bcg_ast"])
        self._asteroids = [Asteroid(display=self._display) for _ in range(nums)]

    def _load_image(self):
        """Сброс состояния картинки"""
        self.image = self._preloaded_image.copy()

    def _resize(self):
        """
        Изменение размера фона
        :return: None
        """
        x_factor = self._display.get_size()[0] / self._display_size[0]  # коэфф. масштабирования по x
        y_factor = self._display.get_size()[1] / self._display_size[1]  # коэфф. масштабирования по y

        self._load_image()
        self.image = pg.transform.scale(self.image, self._display.get_size())
        self.rect = self.image.get_rect(center=(round(self.rect.centerx * x_factor),
                                                round(self.rect.centery * y_factor)))

        self._display_size = self._display.get_size()

    def update(self):
        """
        Обновление фона
        :return: None
        """
        if self._display_size != self._display.get_size():
            self._resize()

        for asteroid in self._asteroids:
            asteroid.update()

    def draw(self, surface=None):
        """
        Зарисовка фона
        :param surface: где рисуем
        :return: None
        """
        super().draw(surface)

        for asteroid in self._asteroids:
            asteroid.draw()

class Asteroid(DynamicObject):

    def __init__(self, display, pos=None, *groups):
        config = configparser.ConfigParser()
        config.read('config.ini')
        pos = pos or (random.randint(0, display.get_full_size()[0]),
                      random.randint(0, display.get_full_size()[1]))
        super().__init__(display, pos, *groups)
        min_ast = int(config["Objects"]["min_bcg_ast"])
        max_ast = int(config["Objects"]["min_bcg_ast"])

        self._display_size = self._display.get_size()

        self._speed = Vector2(random.random(), 0)
        self._size = (random.randint(min_ast, max_ast),) * 2

        self._sprite = random.choice(["./data/ast5.png", "./data/ast3.png",
                                      "./data/ast1.png", "./data/ast4.png",
                                      "./data/ast2.png", "./data/ast6.png"])
        # предобработка изоображения
        image = Image.open(self._sprite)
        image = ImageEnhance.Sharpness(image).enhance(0.4)
        image = ImageEnhance.Brightness(image).enhance(0.7)
        image = ImageEnhance.Contrast(image).enhance(0.85)
        color_key = image.getpixel((0, 0))
        self._preloaded_image = pg.image.fromstring(image.tobytes(), image.size, image.mode).convert_alpha()
        self._preloaded_image.set_alpha(150)
        self._preloaded_image.set_colorkey(color_key)

        self._load_image()
        self.image = pg.transform.scale(self.image, self._size)
        self.rect = self.image.get_rect(center=self._pos)

    def _load_image(self):
        """Сброс состояния картинки"""
        self.image = self._preloaded_image.copy()

    def _resize(self):
        """
        Изменение размера
        :return: None
        """
        x_factor = self._display.get_size()[0] / self._display_size[0]  # коэфф. масштабирования по x
        y_factor = self._display.get_size()[1] / self._display_size[1]  # коэфф. масштабирования по y

        self._load_image()
        self._size = (round(self._size[0] * x_factor), round(self._size[1] * y_factor))

        self.image = pg.transform.scale(self.image, self._size)
        self.rect = self.image.get_rect(center=(round(self.rect.centerx * x_factor),
                                                round(self.rect.centery * y_factor)))
        self._pos = Vector2(self.rect.center)

        self._display_size = self._display.get_size()

    def _move(self):
        self._pos -= self._speed
        super()._move()

    def _toroidal_geometry(self):
        """
        Функция тороидальной геометрии экрана
        :return: None
        """
        if self.rect.left > self._display.get_width():
            self._pos.x = 0
        elif self.rect.right < 0:
            self._pos.x = self._display.get_width()
        self.rect.center = self._pos

    def update(self):
        """
        Обновление состояния фоновых астероидов
        :return: None
        """
        if self._display_size != self._display.get_size():
            self._resize()

        eps = self._size[1] / 2
        if 0 - eps < self._pos.y < self._display.get_height() + eps:
            super().update()


import os
import cv2
import PIL
from pathlib import Path
from PIL import Image
import  glob

def listDirectory(directory, fileExtList):
    fileList = [os.path.normcase(f)
            for f in os.listdir(directory)]
    fileList = [os.path.join(directory, f)
            for f in fileList if os.path.splitext(f)[1] in fileExtList]
    return fileList

def processImage(imageName):
    img = Image.open(imageName)

    saveExt = '.jpg'

    rgb_img = img.convert('RGB')
    rgb_img = rgb_img.resize((60,100),Image.ANTIALIAS)
    print("Boyutlandırıldı...")
    rgb_img.save(os.path.splitext(imageName)[0] + saveExt)
    if os.path.splitext(imageName)[1] != saveExt:
        os.remove(imageName)

if __name__ == '__main__':
    directory = 'Klasör Yolu...'
    exts = ['.jpg','.png']
    imageNames = listDirectory(directory, exts)
    for imageName in imageNames:
        processImage(imageName)


from urllib.request import Request, urlopen
from urllib.parse import urlencode
import json

class RequestHandler:
    def __request(self) -> None:
        try:
            query = {
                'search_query': self.query,
                'page': self.page,
                'sp': self.searchPreferences,
                'persist_gl': 1,
                'gl': self.region
            } if self.searchPreferences is not None else {
                'search_query': self.query,
                'page': self.page,
                'persist_gl': 1,
                'gl': self.region
            }
            request = Request(
                'https://www.youtube.com/results' + '?' + urlencode(query),
                headers = {'Accept-Language': self.language + ',en;q=0.9'}
            )
            self.response = urlopen(request).read().decode('utf_8')
        except:
            self.exception = True

    def __makeSource(self) -> None:
        try:
            start = True
            for index in range(self.response.index('ytInitialData'), len(self.response)):
                if self.response[index] == '{' and start:
                    startIndex = index
                    start = False
                elif self.response[index: index + 9] == '</script>' and not start:
                    endIndex = index - 1
                    break
            self.responseSource = json.loads(self.response[startIndex: endIndex])['contents']['twoColumnSearchResultsRenderer']['primaryContents']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents']
        except:
            self.exception = True


import sys
from itertools import combinations
#from math import factorial
input = sys.stdin.readline

n, m = map(int, input().split())
towns = [0] * (n + 1)

for _ in range(m):
    a, b = map(int, input().split())
    towns[a] |= (1 << b)

count = 0
for i, j in combinations(range(1, n + 1), 2):
    to_billage = towns[i] & towns[j]  # 비트 and로 교집합 구하기
    n = bin(to_billage).count("1")
    count += n * (n - 1) // 2  # nC2

print(count)


import streamlit as st
import os
import hashlib
from utils.cert_utils import extract_certificate
from utils.streamlit_utils import view_certificate
from connection import contract
from utils.streamlit_utils import displayPDF, hide_icons, hide_sidebar, remove_whitespaces

st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
hide_icons()
hide_sidebar()
remove_whitespaces()

options = ("Verify Certificate using PDF", "View/Verify Certificate using Certificate ID")
selected = st.selectbox("", options, label_visibility="hidden")

if selected == options[0]:
    uploaded_file = st.file_uploader("Upload the PDF version of the certificate")
    if uploaded_file is not None:
        bytes_data = uploaded_file.getvalue()
        with open("certificate.pdf", "wb") as file:
            file.write(bytes_data)
        try:
            (uid, candidate_name, course_name, org_name) = extract_certificate("certificate.pdf")
            displayPDF("certificate.pdf")
            os.remove("certificate.pdf")

            # Calculating hash
            data_to_hash = f"{uid}{candidate_name}{course_name}{org_name}".encode('utf-8')
            certificate_id = hashlib.sha256(data_to_hash).hexdigest()

            # Smart Contract Call
            result = contract.functions.isVerified(certificate_id).call()
            if result:
                st.success("Certificated validated successfully!")
            else:
                st.error("Invalid Certificate! Certificate might be tampered")
        except Exception as e:
            st.error("Invalid Certificate! Certificate might be tampered")

elif selected == options[1]:
    form = st.form("Validate-Certificate")
    certificate_id = form.text_input("Enter the Certificate ID")
    submit = form.form_submit_button("Validate")
    if submit:
        try:
            view_certificate(certificate_id)
            # Smart Contract Call
            result = contract.functions.isVerified(certificate_id).call()
            if result:
                st.success("Certificated validated successfully!")
            else:
                st.error("Invalid Certificate ID!")
        except Exception as e:
            st.error("Invalid Certificate ID!")


import os
import json

import maya.app.renderSetup.model.renderSetup as renderSetup
from openpype.pipeline import publish

class ExtractRenderSetup(publish.Extractor):
    """
    Produce renderSetup template file

    This will save whole renderSetup to json file for later use.
    """

    label = "Extract RenderSetup"
    hosts = ["maya"]
    families = ["rendersetup"]

    def process(self, instance):
        parent_dir = self.staging_dir(instance)
        json_filename = "{}.json".format(instance.name)
        json_path = os.path.join(parent_dir, json_filename)

        with open(json_path, "w+") as file:
            json.dump(
                renderSetup.instance().encode(None),
                fp=file, indent=2, sort_keys=True)

        if "representations" not in instance.data:
            instance.data["representations"] = []

        representation = {
            'name': 'json',
            'ext': 'json',
            'files': json_filename,
            "stagingDir": parent_dir,
        }
        instance.data["representations"].append(representation)

        self.log.debug(
            "Extracted instance '%s' to: %s" % (instance.name, json_path))