import json
import os
from typing import TextIO

from ctranslate2.converters.transformers import TransformersConverter

def model_converter(model, model_output):
    converter = TransformersConverter("openai/whisper-" + model)
    try:
        converter.convert(model_output, None, "float16", False)
    except Exception as e:
        print(e)

def format_timestamp(seconds: float, always_include_hours: bool = False, decimal_marker: str = '.'):
    assert seconds >= 0, "non-negative timestamp expected"
    milliseconds = round(seconds * 1000.0)

    hours = milliseconds // 3_600_000
    milliseconds -= hours * 3_600_000

    minutes = milliseconds // 60_000
    milliseconds -= minutes * 60_000

    seconds = milliseconds // 1_000
    milliseconds -= seconds * 1_000

    hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
    return f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"

class ResultWriter:
    extension: str

    def __init__(self, output_dir: str):
        self.output_dir = output_dir

    def __call__(self, result: dict, audio_path: str):
        audio_basename = os.path.basename(audio_path)
        output_path = os.path.join(self.output_dir, audio_basename + "." + self.extension)

        with open(output_path, "w", encoding="utf-8") as f:
            self.write_result(result, file=f)

    def write_result(self, result: dict, file: TextIO):
        raise NotImplementedError

class WriteTXT(ResultWriter):
    extension: str = "txt"

    def write_result(self, result: dict, file: TextIO):
        for segment in result["segments"]:
            print(segment.text.strip(), file=file, flush=True)

class WriteVTT(ResultWriter):
    extension: str = "vtt"

    def write_result(self, result: dict, file: TextIO):
        print("WEBVTT\n", file=file)
        for segment in result["segments"]:
            print(
                f"{format_timestamp(segment.start)} --> {format_timestamp(segment.end)}\n"
                f"{segment.text.strip().replace('-->', '->')}\n",
                file=file,
                flush=True,
            )

class WriteSRT(ResultWriter):
    extension: str = "srt"

    def write_result(self, result: dict, file: TextIO):
        for i, segment in enumerate(result["segments"], start=1):
            # write srt lines
            print(
                f"{i}\n"
                f"{format_timestamp(segment.start, always_include_hours=True, decimal_marker=',')} --> "
                f"{format_timestamp(segment.end, always_include_hours=True, decimal_marker=',')}\n"
                f"{segment.text.strip().replace('-->', '->')}\n",
                file=file,
                flush=True,
            )

class WriteTSV(ResultWriter):
    """
    Write a transcript to a file in TSV (tab-separated values) format containing lines like:
    <start time in integer milliseconds>\t<end time in integer milliseconds>\t<transcript text>

    Using integer milliseconds as start and end times means there's no chance of interference from
    an environment setting a language encoding that causes the decimal in a floating point number
    to appear as a comma; also is faster and more efficient to parse & store, e.g., in C++.
    """
    extension: str = "tsv"

    def write_result(self, result: dict, file: TextIO):
        print("start", "end", "text", sep="\t", file=file)
        for segment in result["segments"]:
            print(round(1000 * segment.start), file=file, end="\t")
            print(round(1000 * segment.end), file=file, end="\t")
            print(segment.text.strip().replace("\t", " "), file=file, flush=True)

class WriteJSON(ResultWriter):
    extension: str = "json"

    def write_result(self, result: dict, file: TextIO):
        json.dump(result, file)


# Python 3 program to
# compute sum of digits in
# number.

# Function to get sum of digits

def getSum(n):

	sum = 0
	while (n != 0):

		sum = sum + int(n % 10)
		n = int(n/10)

	return sum

# Driver code
if __name__ == "__main__":
	n = 687

	# Function call
	print(getSum(n))


from flask import Flask, render_template, Response, jsonify, request
import settings
from flask import abort

app = Flask(__name__,
            static_url_path='',
            static_folder='static',
            template_folder='templates')

log = settings.logging

@app.route('/')
def index():
    return render_template('index.html')

options = [
    {
        'id': 1,
        'title': u'Add new device',
        'description': u'More description for add device option',
        'status': True
    },
    {
        'id': 2,
        'title': u'View device',
        'description': u'More description for view device option',
        'status': False
    }
]

@app.route('/options/api/v1.0/options', methods=['GET'])
def get_options():
    return jsonify({'options': options})

@app.route('/options/api/v1.0/options/<int:option_id>', methods=['GET'])
def get_task(option_id):
    option = [option for option in options if option['id'] == option_id]
    if len(option) == 0:
        abort(404)
    return jsonify({'task': option[0]})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, threaded=True, debug=True)
    # app.run(host='0.0.0.0', port=443, threaded=True, ssl_context=(
    #     '/etc/letsencrypt/live/cq.jarzebak.eu/cert.pem', '/etc/letsencrypt/live/cq.jarzebak.eu/privkey.pem'))
    log.debug("Started up cq app")


# list comprehension = a way to create a new list with less syntax
# can mimic certain lambda functions, easier to read
# list = [expression for item in iterable]

squares = [] # empty list
for i in range(1, 11): # for loop
    squares.append(i*i)  # append() method adds an item to the end of the list
print(squares)  # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

squares = [i*i for i in range(1, 11)] # list comprehension
print(squares)  # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


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

'''
Created on Jan 10, 2013

@author: daniel

This utility file provides a method for moving information between Sugar
instances.
'''

from collections import defaultdict
from sugarcrm import Sugarcrm
from code import interact
from sugarcrm.sugarentry import SugarEntry

com_url = 'http://<host>:<port>/service/v4_1/rest.php'
com_usr = '<user>'
com_pwd = '<pass>'

pro_url = 'http://<host>:<port>/service/v4_1/rest.php'
pro_usr = '<user>'
pro_pwd = '<pass>'

modules = ['Accounts', 'Contacts', 'Opportunities', 'Leads', 'Notes',
           'Prospects', 'Tasks']

cache = defaultdict(dict)
# Fill in user values from one system to the other here.
#   The mapping is From -> To.  Ie: Old System -> New System.
cache['Users']['1'] = '1'

# This map holds fields that need to pull from other cached values.
relate = {
  'Contacts': {
    'account_id': 'Accounts',
    'assigned_user_id': 'Users'
  },
  'Opportunities': {
    'account_id': 'Accounts',
    'assigned_user_id': 'Users'
  },
  'Leads': {
    'account_id': 'Accounts',
    'assigned_user_id': 'Users',
    'contact_id': 'Contacts',
    'opportunity_id': 'Opportunities',
  },
  'Prospects': {
    'assigned_user_id': 'Users',
    'lead_id': 'Leads'
  },
  'Tasks': {
    'assigned_user_id': 'Users',
    'contact_id': 'Contacts'
  },
  'Calls': {
    'assigned_user_id': 'Users',
    'contact_id': 'Contacts'
  },
  'Notes': {
    'account_id': 'Accounts',
    'assigned_user_id': 'Users',
    'contact_id': 'Contacts',
    'lead_id': 'Leads',
    'opportunity_id': 'Opportunities'
  },
  'Accounts': {
    'assigned_user_id': 'Users'
  }
}

SPro = Sugarcrm(pro_url, pro_usr, pro_pwd)
SCom = Sugarcrm(com_url, com_usr, com_pwd)

# A second lookup, this one for required module level connections that
# must be generated.
mod_links = {
  'Tasks': [SCom.modules['Leads'],
            SCom.modules['Notes'],
            SCom.modules['Opportunities'],
            SCom.modules['Accounts']],
  'Notes': [SCom.modules['Opportunities'],
            SCom.modules['Leads'], ]
}

def makeProEntry(oldEntry, oldID = None):
  module = oldEntry._module
  mod_name = module._name
  newEntry = SugarEntry(SPro, mod_name)
  for field in module._fields.keys():
    if field == 'id':
      oldID = oldEntry[field]
      continue
    if field in relate[mod_name]:
      ref_mod = relate[mod_name][field]
      newEntry[field] = cache[ref_mod].get(oldEntry[field], '')
      continue
    newEntry[field] = oldEntry[field]
  newEntry.save()
  for relmod in mod_links.get(mod_name, []):
    for relentry in oldEntry.get_related(relmod):
      if relentry['id'] in cache[relmod._name]:
        newrelentry = SPro.newSugarEntry(relmod._name)
        newrelentry['id'] = cache[relmod._name][relentry['id']]
        newEntry.relate(newrelentry)
  if oldID is not None:
    cache[mod_name][oldID] = newEntry['id']

if __name__ == '__main__':
  interact(local = globals())


import numpy as np

def loadDataSet(filename):
    """数据加载函数,但只返回整个,无单独的label"""
    dataMat=[]
    fr=open(filename)
    for line in fr.readlines():
        currentLine=line.strip().split("\t")
        fltLine=list(map(float,currentLine))
        dataMat.append(fltLine)

    return dataMat

def binSplitDataSet(dataSet,feature,value):
    """选择dataset的某个特征feature,将整个数据集按照feature与value的大小关系进行二分"""
    mat0=dataSet[np.nonzero(dataSet[:,feature]>value)[0],:]
    mat1=dataSet[np.nonzero(dataSet[:,feature]<=value)[0],:]
    return mat0,mat1

def regLeaf(dataSet):
    """负责生成叶结点的函数
    当函数chooseBestSplit确定不再对数据进行切分时,就会调用该函数,
    来得到叶结点的模型
    回归树中,该模型其实就是目标变量的均值
    """
    return np.mean(dataSet[:,-1])

def regErr(dataSet):
    """返回给定数据上的目标变量的平方误差,可理解为计算分类问题的基尼系数(实际上这是基于最小二乘偏差)"""
    return np.var(dataSet[:,-1])*dataSet.shape[0]

def chooseBestSplit(dataSet,leafType=regLeaf,errType=regErr,ops=(1,4)):
    """找到数据集的最佳二分切分方式
    如果找不到,则返回None并且同时调用creatTree来产生叶结点,叶结点也返回None
    ops中,第一个元素为容许下降的误差值,第二个元素是切分的最小样本数
    注意该函数有三个提前结束条件"""
    m,n=dataSet.shape
    S=errType(dataSet)    # 总体混乱程度
    tolS=ops[0]    # 能够容忍的最小方差变化量
    tolN=ops[1]    # 能够容忍的最小矩阵样本数量
    if len(set(dataSet[:,-1].T.tolist()[0]))==1:    # 如果数据集中的目标值全部相等，则创建叶结点并退出
        return None,leafType(dataSet)

    # 初始化最优方差，最优特征，最优特征值
    bestS=np.inf
    bestIndex=0    # 最优特征的索引
    bestValue=0
    for featIndex in range(n-1):    # 遍历每个特征
        for splitVal in set(dataSet[:,featIndex].T.tolist()[0]):
            mat0,mat1=binSplitDataSet(dataSet,featIndex,splitVal)
            if mat0.shape[0]<tolN or mat1.shape[0]<tolN:    # 划分得到的子数据集的尺寸过小 则重新划分
                continue
            # 子数据集的方差和
            newS=errType(mat0)+errType(mat1)    # 新的混乱程度

            # 更新
            if newS<bestS:
                bestS=newS
                bestIndex=featIndex
                bestValue=splitVal

    if S-bestS<tolS:    # 如果误差减少不大，则退出
        return None,leafType(dataSet)

    # 使用最优特征和特征值切分
    mat0,mat1=binSplitDataSet(dataSet,bestIndex,bestValue)
    if mat0.shape[0]<tolN or mat1.shape[0]<tolN:
        return None,leafType(dataSet)

    return bestIndex,bestValue

def createTree(dataSet, leafType=regLeaf, errType=regErr, ops=(1, 4)):
    """
    常见决策树的递归函数
    leafType:给出建立叶结点的函数
    errType:给出误差计算函数
    """
    feat, val = chooseBestSplit(
        dataSet, leafType, errType, ops)    # 选择使得信息增益最大的特征及其分界值val

    if feat==None:    # 递归到没有特征可以划分了，则返回该特征对应的最佳划分值(这里不可以if not None 代替该语句！)
        return val
    retTree = {}
    retTree['spInd'] = feat
    retTree['spVal'] = val
    # 根据最优特征及其划分值,划分子集
    lSet, rSet = binSplitDataSet(dataSet, feat, val)
    retTree['left'] = createTree(
        lSet, leafType, errType, ops)     # 利用划分后的左子集,递归创建左子树
    retTree['right'] = createTree(rSet, leafType, errType, ops)

    return retTree

def linearSolve(dataSet):
    m, n = dataSet.shape
    x = np.mat(np.ones((m, n)))
    y = np.mat(np.ones((m, 1)))
    x[:, 1:n] = dataSet[:, :n-1]
    y = dataSet[:, -1]

    xTx = x.T*x
    if np.linalg.det(xTx) == 0:
        print('the det of xTx is 0,None has been returned.')
        return np.array([0,0,0])
    ws = xTx.I*(x.T*y)
    return ws, x, y

def modelLeaf(dataSet):
    ws, X, y = linearSolve(dataSet)
    return ws

def modelErr(dataSet):
    """误差计算函数,基于线性模型的预测误差"""
    ws, X, y = linearSolve(dataSet)
    yHat = X*ws
    return np.sum(np.power(y-yHat, 2))

def regTreeEval(model, inDat):
    return float(model)

def modelTreeEval(model, inDat):
    n = inDat.shape[1]
    # 构造X
    X = np.mat(np.ones((1, n+1)))
    X[:, 1:n+1] = inDat
    return float(X*model)

def treeForeCast(tree, inDat, modelEval=regTreeEval):
    if not isTree(tree):    # 如果tree不是一棵树,而只是一个叶子结点
        return regTreeEval(tree, inDat)
    # 如果tree是树，则需要递归预测
    if inDat[tree['spInd']] > tree['spVal']:    # 输入数据的特征值与树的分裂界限值比较，决定进入左子树还是右子树
        if isTree(tree['left']):    # 如果tree的左子节点为树，则递归预测
            return treeForeCast(tree['left'], inDat, modelEval)
        else:
            return modelEval(tree['left'], inDat)
    else:
        if isTree(tree['right']):
            return treeForeCast(tree['right'], inDat, modelEval)
        else:
            return modelEval(tree['right'], inDat)

def createForeCast(tree, testData, modelEval=regTreeEval):
    m = len(testData)
    yHat = np.mat(np.zeros((m, 1)))
    for i in range(m):
        yHat[i, 0] = treeForeCast(tree, testData[i], modelEval)    # 每次预测一条数据
    return yHat

def isTree(obj):
    """判断输入数据是否为一棵树(这里为字典)"""
    return isinstance(obj,dict)


import os
import glob
import imageio.v2 as imageio
from wand.image import Image
import PySimpleGUI as sg
from moviepy.editor import ImageSequenceClip

# Function to create an MP4 movie from images.
def create_mp4(input_folder, output_path, fps):
    # Get a sorted list of image paths.
    image_paths = sorted(
        [
            os.path.join(input_folder, file)
            for file in os.listdir(input_folder)
            if file.lower().endswith((".png", ".jpg", ".jpeg"))
        ]
    )
    # Read in images.
    images = [imageio.imread(path) for path in image_paths]
    if images:
        # Create and write video file.
        clip = ImageSequenceClip(images, fps=fps)
        clip.write_videofile(output_path, codec="mpeg4")

# Function to create a GIF from images.
def create_gif(input_folder, output_path, duration, loop):
    # Ensure output path ends with .gif.
    if not output_path.lower().endswith(".gif"):
        output_path += ".gif"
    # Get a sorted list of image paths.
    image_paths = sorted(
        [
            os.path.join(input_folder, file)
            for file in os.listdir(input_folder)
            if file.lower().endswith((".png", ".jpg", ".jpeg", ".tiff", ".exr"))
        ]
    )
    # Read in images.
    images = [imageio.imread(path) for path in image_paths]
    if images:
        # Create and write GIF file.
        imageio.mimsave(output_path, images, duration=duration, loop=loop)

# Function to process images with various effects.
def process_images(
    input_folder,
    output_folder,
    dither,
    num_colors,
    pixelate,
    pixelate_factor,
    resize,
    width,
    height,
    rotate,
    angle,
    blur,
    radius,
    mirror,
):
    processing_done = False
    for img_path in glob.glob(os.path.join(input_folder, "*")):
        if img_path.lower().endswith((".png", ".jpg", ".jpeg", ".tiff", ".exr")):
            # Apply requested image processing operations.
            with Image(filename=img_path) as img:
                if dither:
                    img.quantize(number_colors=int(num_colors), dither="riemersma")
                    processing_done = True
                if pixelate:
                    img.resize(
                        int(img.width // pixelate_factor),
                        int(img.height // pixelate_factor),
                    )
                    img.resize(
                        img.width * pixelate_factor, img.height * pixelate_factor
                    )
                    processing_done = True
                if resize:
                    img.resize(width, height)
                    processing_done = True
                if rotate:
                    img.rotate(angle)
                    processing_done = True
                if blur:
                    img.gaussian_blur(radius=radius)
                    processing_done = True
                if mirror:
                    img.flop()
                    processing_done = True
                img.save(
                    filename=os.path.join(output_folder, os.path.basename(img_path))
                )
    return processing_done

# Set up PySimpleGUI.
sg.theme("DarkBlue")

# Define the layout of the GUI.
layout = [
    [
        sg.Text(
            "Process Images bakes input folder images to output folder with selected effects"
        )
    ],
    [
        sg.Text("Input Folder", size=(15, 1)),
        sg.InputText(key="-IN-"),
        sg.FolderBrowse(),
    ],
    [
        sg.Text("Output Folder", size=(15, 1)),
        sg.InputText(key="-OUT-"),
        sg.FolderBrowse(),
    ],
    [
        sg.Checkbox("Dither", key="-DITHER-"),
        sg.InputText(key="-NUM_COLORS-", default_text="256"),
    ],
    [
        sg.Checkbox("Pixelate", key="-PIXELATE-"),
        sg.InputText(key="-PIXELATE_FACTOR-", default_text="1"),
    ],
    [
        sg.Checkbox("Resize", key="-RESIZE-"),
        sg.InputText(key="-WIDTH-", default_text="512"),
        sg.InputText(key="-HEIGHT-", default_text="512"),
    ],
    [
        sg.Checkbox("Rotate", key="-ROTATE-"),
        sg.InputText(key="-ANGLE-", default_text="90"),
    ],
    [sg.Checkbox("Blur", key="-BLUR-"), sg.InputText(key="-RADIUS-", default_text="0")],
    [sg.Checkbox("Mirror", key="-MIRROR-", default=False)],
    [
        sg.Text("MP4 output path"),
        sg.InputText(key="-MP4-"),
        sg.FileSaveAs(file_types=(("MP4 Files", "*.mp4"),)),
    ],
    [sg.Text("MP4 FPS"), sg.InputText(key="-FPS-", default_text="24")],
    [
        sg.Text("GIF output path"),
        sg.InputText(key="-GIF-"),
        sg.FileSaveAs(file_types=(("GIF Files", "*.gif"),)),
    ],
    [sg.Text("GIF delay time in ms"), sg.InputText(key="-DURATION-", default_text="5")],
    [sg.Text("GIF loop count"), sg.InputText(key="-LOOP-", default_text="0")],
    [
        sg.Button("Process Images"),
        sg.Button("Create MP4"),
        sg.Button("Create GIF"),
        sg.Button("Exit"),
    ],
]

# Define icon path.
icon_path = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), "data_set_icon_246682.ico"
)
# Create the GUI window.
window = sg.Window("ImageChef", layout, background_color="", icon=icon_path)

# Main event loop.
while True:
    event, values = window.read()
    if event in (None, "Exit"):
        break
    elif event == "Process Images":
        try:
            pixelate_factor = int(values["-PIXELATE_FACTOR-"])
            width = int(values["-WIDTH-"])
            height = int(values["-HEIGHT-"])
            angle = float(values["-ANGLE-"])
            radius = float(values["-RADIUS-"])
            if not values["-IN-"] or not os.path.isdir(values["-IN-"]):
                raise ValueError("Invalid or non-existent input folder")
            if not values["-OUT-"]:
                sg.popup(
                    "No output folder specified, processed images will be saved in the input folder."
                )
        except ValueError as e:
            sg.popup(f"Invalid input: {e}")
            continue

        processing_done = process_images(
            values["-IN-"],
            values["-OUT-"],
            values["-DITHER-"],
            values["-NUM_COLORS-"],
            values["-PIXELATE-"],
            pixelate_factor,
            values["-RESIZE-"],
            width,
            height,
            values["-ROTATE-"],
            angle,
            values["-BLUR-"],
            radius,
            values["-MIRROR-"],
        )
        if processing_done:
            sg.popup("Processing Done")
        else:
            sg.popup("No Processing Done")

    elif event == "Create MP4":
        try:
            fps = int(values["-FPS-"])
            if not values["-IN-"] or not os.path.isdir(values["-IN-"]):
                raise ValueError("Invalid or non-existent input folder")
            if not values["-MP4-"]:
                raise ValueError("Output path for MP4 is empty")
            create_mp4(values["-IN-"], values["-MP4-"], fps)
        except ValueError as e:
            sg.popup(f"Invalid input for MP4 creation: {e}")

    elif event == "Create GIF":
        try:
            duration = float(values["-DURATION-"])
            loop = int(values["-LOOP-"])
            if not values["-OUT-"] or not os.path.isdir(values["-OUT-"]):
                raise ValueError("Invalid or non-existent output folder")
            if not values["-GIF-"]:
                raise ValueError("Output path for GIF is empty")
            create_gif(values["-OUT-"], values["-GIF-"], duration, loop)
        except ValueError as e:
            sg.popup(f"Invalid input for GIF creation: {e}")

window.close()


from VendApi import *
from VendApi2 import *
import CsvUtil as cu

domain = ''
token = ''

 #the product_id to search for in the inventory endpoint
prod_id = '2598c236-c76c-2a64-4aee-a410a54af7d2'

####################################

api = VendApi(domain, token)
api2 = VendApi2(domain, token)
inventories = api2.getInventories()
outlets = api.getOutlets()
onametoout = api.getKeyToObjs(outlets, 'name')
oidtoout = api.getKeyToObjs(outlets, 'id')

# print(oidtoout)

outtoinventory = {}
onametoinventory ={}

raw_inventory = []

#outlet to inventory record
for inv in inventories:
    curr_pid = inv['product_id']

    if curr_pid != prod_id:
        continue

    raw_inventory.append(inv)

    curr_oid = inv['outlet_id']
    curr_out_name = oidtoout[curr_oid]['name']

    # outtoinventory[f'{curr_out_name}({curr_oid})'] = inv['inventory_level']
    # onametoinventory[curr_out_name] = inv['inventory_level']
    onametoinventory[f'{curr_out_name}({curr_oid})'] = inv['inventory_level']

print("\n\n######################  OUTLET TO INVENTORY MAPPED JSON ######################\n")
print(onametoinventory)
print('\n-------------------------------------\n')
print("\n######################  RAW INVENTORY RECORDS FROM THE /api/2.0/inventory ENDPOINT ######################\n")
print(raw_inventory)


from ...key import Address
from ...common import Int, concatBytes, _hint
from ...hint import MNFT_COLLECTION_POLICY, MNFT_COLLECTION_REGISTER_FORM, MNFT_MINT_FORM, MNFT_NFT_ID, MNFT_SIGNER, MNFT_SIGNERS

class CollectionRegisterForm:
    def __init__(self, target, symbol, name, royalty, uri, whites):
        assert royalty >= 0 and royalty < 100, 'Invalid royalty; CollectionRegisterForm'
        self.hint = _hint(MNFT_COLLECTION_REGISTER_FORM)
        self.target = Address(target)
        self.symbol = symbol
        self.name = name
        self.royalty = Int(royalty)
        self.uri = uri
        self.whites = []
        for w in whites:
            self.whites.append(Address(w))

    def bytes(self):
        bTarget = self.target.bytes()
        bSymbol = self.symbol.encode()
        bName = self.name.encode()
        bRoyalty = self.royalty.bytes()
        bUri = self.uri.encode()

        _whites = bytearray()
        for w in self.whites:
            _whites += w.bytes()
        bWhites = bytes(_whites)

        return concatBytes(bTarget, bSymbol, bName, bRoyalty, bUri, bWhites)

    def dict(self):
        form = {}
        form['_hint'] = self.hint.hint
        form['target'] = self.target.address
        form['symbol'] = self.symbol
        form['name'] = self.name
        form['royalty'] = self.royalty.value
        form['uri'] = self.uri

        whites = []
        for w in self.whites:
            whites.append(w.address)
        form['whites'] = whites

        return form

class CollectionPolicy:
    def __init__(self, name, royalty, uri, whites):
        assert royalty >= 0 and royalty < 100, 'Invalid royalty; CollectionPolicy'
        self.hint = _hint(MNFT_COLLECTION_POLICY)
        self.name = name
        self.royalty = Int(royalty)
        self.uri = uri
        self.whites = []
        for w in whites:
            self.whites.append(Address(w))

    def bytes(self):
        bName = self.name.encode()
        bRoyalty = self.royalty.bytes()
        bUri = self.uri.encode()

        _whites = bytearray()
        for w in self.whites:
            _whites += w.bytes()
        bWhites = bytes(_whites)

        return concatBytes(bName, bRoyalty, bUri, bWhites)

    def dict(self):
        policy = {}
        policy['_hint'] = self.hint.hint
        policy['name'] = self.name
        policy['royalty'] = self.royalty.value
        policy['uri'] = self.uri

        whites = []
        for w in self.whites:
            whites.append(w.address)
        policy['whites'] = whites

        return policy

class NFTSigner:
    def __init__(self, account, share, signed):
        assert share >= 0 and share <= 100, 'Invalid share; NFTSigner'
        self.hint = _hint(MNFT_SIGNER)
        self.account = Address(account)
        self.share = Int(share)
        self.signed = signed

    def bytes(self):
        bAccount = self.account.bytes()
        bShare = self.share.bytes()
        bSigned = bytes([0])
        if self.signed:
            bSigned = bytes([1])
        return concatBytes(bAccount, bShare, bSigned)

    def dict(self):
        signer = {}
        signer['_hint'] = self.hint.hint
        signer['account'] = self.account.address
        signer['share'] = self.share.value
        signer['signed'] = self.signed
        return signer

class NFTSigners:
    def __init__(self, total, signers):
        assert total >= 0 and total <= 100, 'Invalid total share; NFTSigners'
        self.hint = _hint(MNFT_SIGNERS)
        self.total = Int(total)
        self.signers = signers

    def bytes(self):
        bTotal = self.total.bytes()
        _signers = bytearray()
        for s in self.signers:
            _signers += s.bytes()
        bSigners = _signers

        return concatBytes(bTotal, bSigners)

    def dict(self):
        signers = {}
        signers['_hint'] = self.hint.hint
        signers['total'] = self.total.value
        _signers = []
        for s in self.signers:
            _signers.append(s.dict())
        signers['signers'] = _signers

        return signers

class MintForm:
    def __init__(self, hash, uri, creators, copyrighters):
        self.hint = _hint(MNFT_MINT_FORM)
        self.hash = hash
        self.uri = uri
        self.creators = creators
        self.copyrighters = copyrighters

    def bytes(self):
        bHash = self.hash.encode()
        bUri = self.uri.encode()
        bCreators = self.creators.bytes()
        bCopyrighters = self.copyrighters.bytes()

        return concatBytes(bHash, bUri, bCreators, bCopyrighters)

    def dict(self):
        form = {}
        form['_hint'] = self.hint.hint
        form['hash'] = self.hash
        form['uri'] = self.uri
        form['creators'] = self.creators.dict()
        form['copyrighters'] = self.copyrighters.dict()
        return form

class NFTID:
    def __init__(self, collection, idx):
        assert idx.value > 0, 'idx must be over zero; NFTID'
        self.hint = _hint(MNFT_NFT_ID)
        self.collection = collection
        self.idx = idx

    def bytes(self):
        bCollection = self.collection.encode()
        bIdx = self.idx.bytes()
        return concatBytes(bCollection, bIdx)

    def dict(self):
        id = {}
        id['_hint'] = self.hint.hint
        id['collection'] = self.collection
        id['idx'] = self.idx.value
        return id


### Add fixed time effects and controls for infection levels and national lockdown

# Initial imports
import pandas as pd
import statsmodels.api as sm
import numpy as np
from scipy import stats
import statsmodels.formula.api as smf
import seaborn as sns
import matplotlib.pyplot as plt
import seaborn as sns

#Import household control data
data = pd.read_csv('household_regression_data_weeks.csv')

# Calculate the national lockdown variable for each week
lockdown_weekly = data.groupby('Week')['National_lockdown'].mean().reset_index()

#Import the y_weekly data
y_weekly = pd.read_csv('y_weekly_inf.csv')

#Merge on the national lockdown control and save the file
y_weekly = y_weekly.merge(lockdown_weekly, on='Week')

y_weekly.to_csv('y_weekly_full.csv')

# Fit the OLS regression model
model = smf.ols(formula='Y ~ Treatment + Week + Student_infection_lag + National_lockdown', data=y_weekly).fit()

print(model.summary())

# Plot the residuals
sns.residplot(x=model.predict(), y=y_weekly['Y'], lowess=True, line_kws={'color': 'red'})
plt.title('Residual Plot')
plt.xlabel('Predicted Values')
plt.ylabel('Residuals')
plt.show()

# Plot the real data and model over time
plt.plot(y_weekly['Week'], y_weekly['Y'], label='Real Data')
plt.plot(y_weekly['Week'], model.predict(), label='Model')
plt.axvline(x=10, linestyle='--', color='black')
plt.title('Real Data vs. Model Over Time')
plt.xlabel('Week of Study')
plt.ylabel('% Participation')
plt.ylim((0,1))
plt.legend()
plt.show()


import calendar
import unittest
from datetime import date, datetime, timedelta

from codenotes import parse_args
from codenotes.util.args import date_args_empty, dates_to_search

class TestDateArgsNeededEmpty(unittest.TestCase):
    def test_no_args(self):
        args = parse_args(["task", "search"])

        self.assertTrue(date_args_empty(args))

    def test_only_date(self):
        args = parse_args(["task", "search", "--today"])

        self.assertFalse(date_args_empty(args))

    def test_only_text(self):
        args = parse_args(["task", "search", "New", "task", "added"])

        self.assertFalse(date_args_empty(args))

    def test_text_and_date(self):
        args = parse_args(["task", "search", "New", "task", "added", "--today"])

        self.assertFalse(date_args_empty(args))

class TestDateToSearch(unittest.TestCase):
    def test_today(self):
        search_date = datetime.now().date()
        args = parse_args(["task", "search", "--today"])

        self.assertEqual(dates_to_search(args), search_date)

    def test_yesterday(self):
        search_date = datetime.now().date() - timedelta(days=1)
        args = parse_args(["task", "search", "--yesterday"])

        self.assertEqual(dates_to_search(args), search_date)

    def test_month(self):
        now = datetime.now()
        num_days = calendar.monthrange(now.year, now.month)[1]
        days = [date(now.year, now.month, 1), date(now.year, now.month, num_days)]

        args = parse_args(["task", "search", "--month"])

        self.assertListEqual(dates_to_search(args), days)

    def test_week(self):
        now = datetime.now().date()
        first_day = now - timedelta(days=now.weekday())
        last_day = first_day + timedelta(days=6)
        days = [first_day, last_day]

        args = parse_args(["task", "search", "--week"])

        self.assertListEqual(dates_to_search(args), days)

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


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 24 06:56:45 2018

@author: Javier Alejandro Acevedo Barroso
"""

import numpy as np
import matplotlib.pyplot as plt

x_obs = np.array([-2.0,1.3,0.4,5.0,0.1, -4.7, 3.0, -3.5,-1.1])
y_obs = np.array([ -1.931,   2.38,   1.88,  -24.22,   3.31, -21.9,  -5.18, -12.23,   0.822])
sigma_y_obs = ([ 2.63,  6.23, -1.461, 1.376, -4.72,  1.313, -4.886, -1.091,  0.8054])
plt.errorbar(x_obs, y_obs, yerr=sigma_y_obs, fmt='o')

def model(x,a,b,c):
    return a*x*x  + b * x + c

def loglikelihood(x_obs, y_obs, sigma_y_obs, a, b, c):
    d = y_obs -  model(x_obs, a,b,c)
    d = d/sigma_y_obs
    d = -0.5 * np.sum(d**2)
    return d

def logprior(a, b, c):
#    p = -np.inf
#    if a < 2 and a >-2 and b >-10 and b<10 and c > -10 and c < 10:
#        p = 0.0
#    return p
    return 0

N= 50000
lista_a = [np.random.random()]
lista_b = [np.random.random()]
lista_c = [np.random.random()]
logposterior = [loglikelihood(x_obs, y_obs, sigma_y_obs, lista_a[0], lista_b[0], lista_c[0]) + logprior(lista_a[0], lista_b[0], lista_c[0])]

sigma_delta_a = 0.2
sigma_delta_b = 1
sigma_delta_c = 1.0

for i in range(1,N):
    propuesta_a  = lista_a[i-1] + np.random.normal(loc=0.0, scale=sigma_delta_a)
    propuesta_b  = lista_b[i-1] + np.random.normal(loc=0.0, scale=sigma_delta_b)
    propuesta_c  = lista_b[i-1] + np.random.normal(loc=0.0, scale=sigma_delta_c)

    logposterior_viejo = loglikelihood(x_obs, y_obs, sigma_y_obs, lista_a[i-1], lista_b[i-1], lista_c[i-1]) + logprior(lista_a[i-1], lista_b[i-1], lista_c[i-1])
    logposterior_nuevo = loglikelihood(x_obs, y_obs, sigma_y_obs, propuesta_a, propuesta_b, propuesta_c) + logprior(propuesta_a, propuesta_b,propuesta_c)

    r = min(1,np.exp(logposterior_nuevo-logposterior_viejo))
    alpha = np.random.random()
    if(alpha<r):
        lista_a.append(propuesta_a)
        lista_b.append(propuesta_b)
        lista_c.append(propuesta_c)
        logposterior.append(logposterior_nuevo)
    else:
        lista_a.append(lista_a[i-1])
        lista_b.append(lista_b[i-1])
        lista_c.append(lista_c[i-1])
        logposterior.append(logposterior_viejo)
lista_a = np.array(lista_a)
lista_b = np.array(lista_b)
lista_c = np.array(lista_c)
logposterior = np.array(logposterior)

realx = np.linspace(-5,5,100)
#rta = (lista_a.argmax(),lista_b.argmax(),lista_c.argmax())
rta = (lista_a.mean(),lista_b.mean(),lista_c.mean())
plt.plot(realx, model(realx,rta[0],rta[1],rta[2]))
plt.title("a = %.3f  b = %.3f  c = %.3f" % (rta))

h2 = plt.figure()

#plt.plot(lista_a[100:], label='pendiente')
#plt.plot(lista_b[100:], label='intercepto')
#plt.plot(lista_c[100:], label='c')
#plt.plot(logposterior[100:], label='loglikelihood')
#plt.legend()

h2 = plt.figure()
plt.plot(lista_a, lista_b, alpha=0.5)
plt.scatter(lista_a, lista_b, alpha=0.4, c=np.exp(logposterior))
plt.colorbar()

h2 = plt.figure()
plt.plot(lista_b, lista_c, alpha=0.5)
plt.scatter(lista_a, lista_b, alpha=0.4, c=np.exp(logposterior))
plt.colorbar()

h2 = plt.figure()
plt.plot(lista_a, lista_c, alpha=0.5)
plt.scatter(lista_a, lista_b, alpha=0.4, c=np.exp(logposterior))
plt.colorbar()


# Time Complexity of Solution:
# Best O(n); Average O(n^2); Worst O(n^2).
#
# Approach:
# Insertion sort is good for collections that are very small or nearly sorted.
# Otherwise it's not a good sorting algorithm:
# it moves data around too much.
# Each time an insertion is made, all elements in a greater position are shifted

def insertion_sort(arr):
    # since we want to swap an item with previous one, start from 1
    for i in range(1, len(arr)):

        # current_value will be used for comparison with previous items,
        # and sent to the place it belongs
        current_value = arr[i]

        # reducing i directly will mess loop, so reduce its 'position' instead
        position = i

        # position > 0 bcoz no point going till arr[0] since there is
        # no seat available on its left, for current_value

        while position > 0 and arr[position - 1] > current_value:
            # Move the bigger item one step right to make room for current_value
            arr[position] = arr[position - 1]
            position -= 1

        arr[position] = current_value

alist = [5, 2, 1, 9, 0, 4, 6]
print('Before', alist)
insertion_sort(alist)
print('After ', alist)


import copy
from pathlib import Path
from collections import defaultdict

import pandas as pd
import numpy as np
import torch
from torch.utils.data import Dataset

try:
    import datasets as hf_datasets
except ImportError:
    pass

def get_texts_df(dir_path):
    paths = [x for x in dir_path.iterdir() if x.is_file() and x.suffix == ".txt"]
    paths.sort(key=lambda path: path.stem)
    texts = []
    for path in paths:
        with open(path) as f:
            texts.append((path.stem, f.read().rstrip()))
    return pd.DataFrame(texts, columns=["id", "text"])

def get_dfs(path):
    path = Path(path)
    test_texts = get_texts_df(path / "test")
    train_df = pd.read_csv(path / "train.csv")
    train_texts = get_texts_df(path / "train")
    return train_texts, train_df, test_texts

def get_block_dataset(df, text_column, tokenizer, max_len, seed):
    if max_len is None:
        max_len = tokenizer.model_max_length
    dataset = hf_datasets.Dataset.from_pandas(df)
    dataset = dataset.shuffle(seed)

    def tokenize(examples):
        tokenized = tokenizer(
            examples[text_column], add_special_tokens=False, return_attention_mask=False
        )
        for tokens in tokenized.input_ids:
            tokens.append(tokenizer.sep_token_id)
        return tokenized

    dataset = dataset.map(
        tokenize,
        batched=True,
        remove_columns=dataset.column_names,
        desc="Tokenizing examples...",
    )

    def blockify(examples):
        all = []
        for sub in examples["input_ids"]:
            all.extend(sub)
        sub_max = max_len - 2
        block_starts = range(0, len(all) - sub_max + 1, sub_max)
        blocks = [
            tokenizer.build_inputs_with_special_tokens(all[i : i + sub_max])
            for i in block_starts
        ]
        examples = {"input_ids": blocks}
        return examples

    dataset = dataset.map(
        blockify,
        batched=True,
        desc="Chunking examples...",
    )
    return dataset

def split_offsets(line):
    words = line.split()
    offsets = []
    offset = 0
    for word in words:
        begin_offset = line.index(word, offset)
        offset = begin_offset + len(word)
        offsets.append((begin_offset, offset))
    return offsets

labels = [
    "Lead",
    "Position",
    "Evidence",
    "Claim",
    "Concluding Statement",
    "Counterclaim",
    "Rebuttal",
]

def get_label_ids(labels):
    label_to_id = {(None, 0): 0}
    id_to_label = {0: (None, 0)}
    for i, t in enumerate(labels):
        label_to_id[(t, True)] = i * 2 + 1
        label_to_id[(t, False)] = i * 2 + 2
        id_to_label[i * 2 + 1] = (t, True)
        id_to_label[i * 2 + 2] = (t, False)
    return label_to_id, id_to_label

label_to_id, id_to_label = get_label_ids(labels)
max_labels = len(label_to_id)

def get_answer_dict(df):
    answers = defaultdict(list)
    for row in df.itertuples():
        words = row.predictionstring.split()
        words = [int(word) for word in words]
        answers[row.id].append((words, row.discourse_type))

    for answer in answers.values():
        answer.sort()
    return answers

def get_clean_answers(answers):
    answers = copy.deepcopy(answers)
    for _, answer in answers.items():
        prev_words = [-1]
        cleaned_answer = []
        for words, label in answer:
            if prev_words[-1] >= words[0]:
                if len(prev_words) == 1:
                    if len(words) == 1:
                        continue
                    words.pop(0)
                else:
                    prev_words.pop()
            cleaned_answer.append((words, label))
            prev_words = words
        answer.clear()
        answer.extend(cleaned_answer)
    return answers

def check_answer_dict(answers):
    for answer in answers.values():
        prev = -1
        for words, _ in answer:
            if len(words) == 0:
                return False
            for word_id in words:
                if prev >= word_id:
                    return False
                prev = word_id
    return True

def get_word_dict(texts):
    offsets = {}
    for row in texts.itertuples():
        offsets[row.id] = split_offsets(row.text)
    return offsets

def overlap(a, b, c, d):
    return a < d and c < b

def intersect_ranges(ranges, items):
    # Given sorted ranges (non-overlapping) and sorted items (non-overlapping)
    # Collect items that fall into these ranges and return the indices.
    groups = []
    index = 0
    for r, s in ranges:
        group = []
        while index < len(items) and items[index][0] < s:
            if r < items[index][1]:
                group.append(index)
            index += 1
        groups.append(group)
    return groups

def get_target(token_offsets, answers, word_offsets, overflow_to_sample):
    answer_ranges = [
        [(word_offset[words[0]][0], word_offset[words[-1]][1]) for words, _ in answer]
        for answer, word_offset in zip(answers, word_offsets)
    ]
    answer_seen = set()
    target = torch.zeros(token_offsets.size()[:-1], dtype=torch.long)
    for i, token_offset in enumerate(token_offsets.tolist()):
        j = overflow_to_sample[i].item()
        answer_tokens = intersect_ranges(answer_ranges[j], token_offset)
        for k, answer_token in enumerate(answer_tokens):
            label = answers[j][k][1]
            label1 = label_to_id[(label, False)]
            label0 = label_to_id[(label, True)] if (j, k) not in answer_seen else label1
            target[i, answer_token[0:1]] = label0
            target[i, answer_token[1:]] = label1

            if len(answer_token) > 0:
                answer_seen.add((j, k))
    return target

class FeedbackDataset(Dataset):
    def __init__(self, texts, df, tokenizer, max_len, stride, pad_to_multiple_of):
        self.texts = texts
        self.answers = get_answer_dict(df)
        self.answers = get_clean_answers(self.answers)
        self.words = get_word_dict(texts)
        self.tokenizer = tokenizer
        self.max_len = max_len
        self.stride = stride
        self.pad_to_multiple_of = pad_to_multiple_of

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

    def __getitem__(self, idx):
        text = self.texts.loc[idx, "text"]
        text_id = self.texts.loc[idx, "id"]
        answer = self.answers[text_id]
        words = self.words[text_id]
        return text, answer, words

    def get_collate_fn(self):
        def collate_fn(examples):
            text, answer, words = [list(a) for a in zip(*examples)]
            inputs = self.tokenizer(
                text,
                add_special_tokens=True,
                padding=True,
                truncation=True,
                return_overflowing_tokens=True,
                return_offsets_mapping=True,
                max_length=self.max_len,
                stride=self.stride,
                return_tensors="pt",
                pad_to_multiple_of=self.pad_to_multiple_of,
            )
            target = get_target(
                inputs.offset_mapping,
                answer,
                words,
                inputs.overflow_to_sample_mapping,
            )
            return len(examples), inputs, target, words, answer

        return collate_fn

def get_matches(preds, golds):
    pred_sets = [set(pred) for pred in preds]
    gold_sets = [set(gold) for gold in golds]

    seen = set()
    matches = []
    for i, pred_set in enumerate(pred_sets):
        for j, gold_set in enumerate(gold_sets):
            if j in seen:
                continue
            intersection = len(pred_set.intersection(gold_set))
            if intersection <= 0.5 * len(gold_set):
                continue
            if intersection <= 0.5 * len(pred_set):
                continue
            seen.add(j)
            matches.append((i, j))
            break
    return matches

def _score_single(tp, fp, fn, pred_words, pred_labels, answer_words, answer_labels):
    matches = get_matches(pred_words, answer_words)
    for l in pred_labels:
        fp[l] += 1
    for l in answer_labels:
        fn[l] += 1
    for i, j in matches:
        l = pred_labels[i]
        if l != answer_labels[j]:
            continue
        tp[l] += 1
        fp[l] -= 1
        fn[l] -= 1

def score(preds_batch, words_batch, answers_batch):
    return score_words(pred_to_words(preds_batch, words_batch), answers_batch)

def score_words(preds_batch, answers_batch):
    tp = defaultdict(int)
    fp = defaultdict(int)
    fn = defaultdict(int)
    for preds, answers in zip(preds_batch, answers_batch):
        pred_words, pred_labels = zip(*preds) if preds else ([], [])
        answer_words, answer_labels = zip(*answers)
        _score_single(tp, fp, fn, pred_words, pred_labels, answer_words, answer_labels)

    return {l: (tp[l], fp[l], fn[l]) for l in labels}

def pred_to_words(preds_batch, words_batch):
    pred_words_batch = []
    for preds, words in zip(preds_batch, words_batch):
        if not preds:
            pred_words_batch.append([])
            continue

        pred_ranges, pred_labels = zip(*preds)
        pred_words = intersect_ranges(pred_ranges, words)
        pred_words = [(a, b) for a, b in list(zip(pred_words, pred_labels)) if a]
        pred_words_batch.append(pred_words)

    return pred_words_batch

def _confusion_matrix_single(
    matrix, pred_words, pred_labels, answer_words, answer_labels, to_id
):
    matches = get_matches(pred_words, answer_words)
    pred_seen = [False for _ in range(len(pred_labels))]
    answer_seen = [False for _ in range(len(answer_labels))]

    for i, j in matches:
        pred_seen[i] = True
        answer_seen[j] = True
        p = to_id[pred_labels[i]]
        a = to_id[answer_labels[j]]
        matrix[p, a] += 1

    for seen, p in zip(pred_seen, pred_labels):
        if seen:
            continue
        matrix[to_id[p], to_id[None]] += 1

    for seen, a in zip(answer_seen, answer_labels):
        if seen:
            continue
        matrix[to_id[None], to_id[a]] += 1

def confusion_matrix_words(preds_batch, answers_batch):
    full_labels = labels + [None]
    to_id = {l: i for i, l in enumerate(full_labels)}

    matrix = np.zeros((len(full_labels), len(full_labels)), dtype=np.int64)
    for preds, answers in zip(preds_batch, answers_batch):
        pred_words, pred_labels = zip(*preds) if preds else ([], [])
        answer_words, answer_labels = zip(*answers)
        _confusion_matrix_single(
            matrix, pred_words, pred_labels, answer_words, answer_labels, to_id
        )

    full_labels[-1] = "Unmatched"
    df = pd.DataFrame(matrix, index=full_labels, columns=full_labels)
    return df

def confusion_matrix(preds_batch, words_batch, answers_batch):
    return confusion_matrix_words(
        pred_to_words(preds_batch, words_batch), answers_batch
    )


'''
    Fig1. Cascade Matrix heatmap
'''

import numpy as np
import matplotlib.pyplot as plt
# from numba import jit
import matplotlib.colors as colors

# @jit(nopython = True)
def random_cascade_matrix(mu_L, mu_U, sigma_L, sigma_U, gamma, N = 250):
    J = np.zeros((N, N))

    for i in range(N):
        for j in range(i):
            z1 = np.random.normal(0, 1)
            z2 = np.random.normal(0, 1)

            J[i, j] = mu_L/N + sigma_L*z1/np.sqrt(N)
            J[j, i] = mu_U/N + sigma_U*(gamma*z1 + np.sqrt(1 - gamma**2)*z2)/np.sqrt(N)

    return J
fig, axs = plt.subplots(1, 3, figsize = (10, 3))
plt.rc('text', usetex=True)
plt.rcParams.update({'font.size': 12})
# for display
def block_matrix(mu, sizes):
    N = np.sum(sizes)
    c = np.hstack((np.array([0]), np.cumsum(sizes)[:-1]))

    J = np.zeros((N, N))

    for i in range(np.size(sizes)):
        for j in range(np.size(sizes)):
            for u in range(sizes[i]):
                for v in range(sizes[j]):
                    J[c[i] + u, c[j] + v] = mu[i, j]

    return J

def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
    new_cmap = colors.LinearSegmentedColormap.from_list(
        'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
        cmap(np.linspace(minval, maxval, n)))
    return new_cmap

arr = np.linspace(0, 50, 100).reshape((10, 10))

cmap = plt.get_cmap('viridis')
new_cmap = truncate_colormap(cmap, 0.12, 0.98)

mu1 = np.random.uniform(0, 1, 16).reshape((4, 4))
sizes1 = np.array([5, 4, 7, 3])

mu_cascade = np.array([[1, 2, 2, 2], [0, 1, 2, 2], [0, 0, 1, 2], [0, 0, 0, 1]])
sizes_cascade = np.array([7, 5, 6, 4])
J_cascade = block_matrix(mu_cascade, sizes_cascade)

J1 = block_matrix(mu1, sizes1)
axs[0].matshow(J1, cmap = "viridis")
axs[1].matshow(J_cascade, cmap = new_cmap)
axs[2].matshow(random_cascade_matrix(-1, 1, 0.0, 0.0, 0.0, 500), cmap = new_cmap)

# print(random_cascade_matrix)
# for i in range(3):
    # Turn off tick labels
axs[0].set_xticklabels([])
axs[0].set_xticks([])

axs[1].set_xticklabels([])
axs[1].set_xticks([])

axs[2].set_xticklabels([])
axs[2].set_xticks([])

axs[0].get_yaxis().set_visible(False)
axs[1].get_yaxis().set_visible(False)
axs[2].get_yaxis().set_visible(False)

axs[0].set_xlabel("Block Structured")
axs[1].set_xlabel("Cascade Model, Finite B")
axs[2].set_xlabel("Cascade Model, infinite B")
plt.tight_layout()

# panel labels
axs[0].annotate('(a)', xy=(0.05, 0.05), xycoords='axes fraction', zorder = 10, ma = 'center', bbox=dict(facecolor='white', alpha=0.6, boxstyle='round'))
axs[1].annotate('(b)', xy=(0.05, 0.05), xycoords='axes fraction', zorder = 10, ma = 'center', bbox=dict(facecolor='white', alpha=0.6, boxstyle='round'))
axs[2].annotate('(c)', xy=(0.05, 0.05), xycoords='axes fraction', zorder = 10, ma = 'center', bbox=dict(facecolor='white', alpha=0.6, boxstyle='round'))
fig.tight_layout()
plt.savefig("Interaction Matrix Cartoon.pdf")


from django.shortcuts import render

from custom_model_field_app.forms import PersonForm

# Create your views here.

def customview(request):
    form = PersonForm()

    if request.method == 'POST':

        if form.is_valid():
            form.save()

    return render(request,'custom.html',{'form':form})


def swap_case(s):
    count_str = len(s)
    modified_str = ''
    for i in range(count_str):
        if s[i].isupper():
            a = s[i].lower()
        elif s[i].islower():
            a = s[i].upper()
        else:
            a = s[i]
        modified_str = modified_str + a
    return modified_str

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)


from __future__ import (nested_scopes, generators, division, absolute_import, with_statement,
                        print_function, unicode_literals)

from pants.backend.core.tasks.check_exclusives import ExclusivesMapping
from pants.backend.jvm.tasks.jvm_task import JvmTask
from pants.base.exceptions import TaskError
from pants.util.dirutil import safe_mkdtemp, safe_rmtree
from pants_test.task_test_base import TaskTestBase

class DummyJvmTask(JvmTask):
  def execute(self):
    pass

class JvmTaskTest(TaskTestBase):
  """Test some base functionality in JvmTask."""

  @classmethod
  def task_type(cls):
    return DummyJvmTask

  def setUp(self):
    super(JvmTaskTest, self).setUp()
    self.workdir = safe_mkdtemp()

    self.t1 = self.make_target('t1', exclusives={'foo': 'a'})
    self.t2 = self.make_target('t2', exclusives={'foo': 'a'})
    self.t3 = self.make_target('t3', exclusives={'foo': 'b'})
    # Force exclusive propagation on the targets.
    self.t1.get_all_exclusives()
    self.t2.get_all_exclusives()
    self.t3.get_all_exclusives()
    context = self.context(target_roots=[self.t1, self.t2, self.t3])

    # Create the exclusives mapping.
    exclusives_mapping = ExclusivesMapping(context)
    exclusives_mapping.add_conflict('foo', ['a', 'b'])
    exclusives_mapping._populate_target_maps(context.targets())
    context.products.safe_create_data('exclusives_groups', lambda: exclusives_mapping)

    self.task = self.create_task(context, self.workdir)

  def tearDown(self):
    super(JvmTaskTest, self).tearDown()
    safe_rmtree(self.workdir)

  def test_get_base_classpath_for_compatible_targets(self):
    self.task.get_base_classpath_for_compatible_targets([self.t1, self.t2])

  def test_get_base_classpath_for_incompatible_targets(self):
    with self.assertRaises(TaskError):
      self.task.get_base_classpath_for_compatible_targets([self.t1, self.t3])


infile = open("input/in22_real.txt","r")
# infile = open("input/in22_test.txt","r")

p1 = []
next(infile)
for line in infile:
	if line == '\n':
		break
	p1.append(int(line.strip()))

p2 = []
next(infile)
for line in infile:
	if line == '\n':
		break
	p2.append(int(line.strip()))

def score(p):
	return sum([n*(i+1) for i,n in enumerate(reversed(p))])

def day22_part1(p1,p2):
	while len(p1) and len(p2):
		c1 = p1.pop(0)
		c2 = p2.pop(0)
		if c1 > c2:
			p1.extend([c1,c2])
		else:
			p2.extend([c2,c1])
	if len(p1):
		return score(p1)
	else:
		return score(p2)

def day22_part2(p1,p2,depth,Nmax):
	Nturns = 0
	while len(p1) and len(p2):
		Nturns += 1
		if Nturns > Nmax: #probably repeating, call it quits
			return True
		c1 = p1.pop(0)
		c2 = p2.pop(0)
		if len(p1) >= c1 and len(p2) >= c2:
			if day22_part2(p1[:c1],p2[:c2],depth + 1,Nmax):
				p1.extend([c1,c2])
			else:
				p2.extend([c2,c1])
		else:
			if c1 > c2:
				p1.extend([c1,c2])
			else:
				p2.extend([c2,c1])
	if depth:
		if len(p1):
			return True
		else:
			return False
	else:
		if len(p1):
			return score(p1)
		else:
			return score(p2)

print(day22_part1(p1[:],p2[:]))
print(day22_part2(p1[:],p2[:],0,600))


from fastexcel import read_excel
from openpyxl import load_workbook
from xlrd import open_workbook

def pyxl_read(test_file_path: str):
    wb = load_workbook(test_file_path, read_only=True, keep_links=False, data_only=True)
    for ws in wb:
        rows = ws.iter_rows()
        rows = ws.values
        for row in rows:
            for value in row:
                value

def xlrd_read(test_file_path: str):
    wb = open_workbook(test_file_path)
    for ws in wb.sheets():
        for idx in range(ws.nrows):
            for value in ws.row_values(idx):
                value

def fastexcel_read(test_file_path: str):
    reader = read_excel(test_file_path)
    for sheet_name in reader.sheet_names:
        sheet = reader.load_sheet_by_name(sheet_name)
        sheet.to_arrow()


# Ignas Kleveckas S2095960

import sys
import time
import os
import select
from socket import *

class Sender(object):
    def __init__(self, remote_host, port, file_name, retry_timeout, window_size):
        # receive input
        self.remote_host = remote_host
        self.port = port
        self.file_name = file_name
        self.retry_timeout = retry_timeout / 1000
        self.window_size = window_size

        # configure socket
        self.so = socket(AF_INET, SOCK_DGRAM)

        # initialise remaining values
        self.sequence_no = 1
        self.window_base = 1
        self.transfer_time = 0
        self.file_size = os.path.getsize(file_name)
        self.buffer_length = 1024
        self.total_packets = 1 + (self.file_size / self.buffer_length)
        self.timeout = False
        self.timer = False
        self.packets = [bytearray((0).to_bytes(1, 'big'))] #dummy value in 0 position

    def packet_monitor(self):
        if not self.timeout:
            time_left = self.retry_timeout - (time.time() - self.time_sent)
            if time_left >= 0:

                # check if there is an incoming packet
                ready = select.select([self.so], [], [], 0)
                if ready[0]:
                    self.receive()
            else:
                self.timeout = True
        else:
            self.process_timeout()

    def receive(self):
        response, addr = self.so.recvfrom(self.buffer_length)
        ack_no = int.from_bytes(response, 'big')
        #print("ACK received " + str(ack_no))
        self.window_base = ack_no + 1

        # if the ack for the base packet was received, stop the timer
        if self.window_base == self.sequence_no:
            self.timer = False

        # otherwise reset the timer
        else:
            self.time_sent = time.time()
            self.timer = True

    def process_timeout(self):
        self.timer = True
        self.timeout = False
        self.time_sent = time.time()

        # resend the packets in the window (after window base)
        for i in range(self.window_base, self.sequence_no):
            packet = self.packets[i]
            self.so.sendto(packet, (self.remote_host, self.port))
            #print("Packet retransmitted " + str(i))

    def send_file(self):
        myfile = open(self.file_name, "rb")
        data = myfile.read(self.buffer_length)
        end_of_file = 0
        start = time.time()
        while self.window_base < self.total_packets or data:
            #if time.time() - start > 180: # TESTING
                #break
            if self.sequence_no < self.window_base + self.window_size and data:

                # prepare header
                if len(data) < self.buffer_length:
                    end_of_file = 1
                else:
                    end_of_file = 0
                packet = bytearray(self.sequence_no.to_bytes(2, 'big'))
                packet.extend(end_of_file.to_bytes(1, 'big'))
                packet.extend(data)

                # send packet and add it to the packet list
                self.packets.append(packet)
                self.so.sendto(packet, (self.remote_host, self.port))
                #print("Packet sent " + str(self.sequence_no))

                # if sending the base packet, start the timer
                if self.window_base == self.sequence_no:
                    self.time_sent = time.time()
                    self.timer = True

                self.sequence_no += 1

                # read data for the next packet
                data = myfile.read(self.buffer_length)
                if len(data) < self.buffer_length:
                    end_of_file = 1

            if self.timer:
                self.packet_monitor()

        total_time = time.time() - start
        throughput = self.file_size / (1000 * total_time)
        print(throughput)

        # close the file and socket
        self.so.close()
        myfile.close()

        return throughput

if __name__ == '__main__':
    sender = Sender(sys.argv[1], int(sys.argv[2]), sys.argv[3], int(sys.argv[4]), int(sys.argv[5]))
    sender.send_file()


# https://pypi.org/project/RPi.bme280/
import smbus2
import bme280

import syslog
import threading
def logmsg(level, msg):
    syslog.syslog(level, 'station: {}: {}'.format(threading.currentThread().getName(), msg))

def logdbg(msg):
    logmsg(syslog.LOG_DEBUG, msg)

def loginf(msg):
    logmsg(syslog.LOG_INFO, msg)

def logerr(msg):
    logmsg(syslog.LOG_ERR, msg)

port = 1
address = 0x76
bus = smbus2.SMBus(port)

calibration_params = bme280.load_calibration_params(bus, address)

import numpy as np
import time as tm
import os
#with open('station.log', 'w') as f:
#	pass
target_t_f = 85.
t_safe = 90.
from importlib import reload
cadence = 5 # seconds
last_time_s = None
last_error = None
while True:
	data = bme280.sample(bus, address, calibration_params)

	t_f = data.temperature * 9. / 5 + 32.
	with open('status', 'r') as f:
		status = f.readline()
	output = '{:30} {:10s} {:5.2f} {:8.2f} {:5.2f}'.format(str(data.timestamp).replace(' ','_'), status, t_f, data.pressure, data.humidity)
	with open('station.log', 'a') as f:
		#f.write('{} error={:.3f} dt={:6.3f}s p={:.3f} i={:.3f} d={:.3f} pid={:.3f}'.format(output, error, dt, p, i, d, pid))
		f.write('{}\n'.format(output))
	time_s = tm.mktime(data.timestamp.timetuple())

	if t_f > t_safe:
		print('temperature {:.2f} > safe temperature {:.2f}; safe mode'.format(t_f, t_safe))
		os.system('./safe')
	#elif t_f < target_t_f:
	#	os.system('./on')
	#else:
	#	os.system('./off')
	else:
		error = t_f - target_t_f
		kp = 1
		ki = kp / 300 # 5-minute integration time
		kd = kp * 300 # 5-minute derivative time
		p = kp * error
		if last_time_s:
			dt = time_s - last_time_s
			i += ki * 0.5 * (error + last_error) * dt
			d = kd * (error - last_error) / dt
		else:
			dt = 0
			i = 0
			d = 0
		# now instead of comparing t_f to target_t_f, compare p+i+d to zero
		last_time_s = time_s
		last_error = error

		pid = p + i + d
		full_output = '{} error={:.3f} dt={:6.3f}s p={:.3f} i={:.3f} d={:.3f} pid={:.3f}'.format(output, error, dt, p, i, d, pid)
		print(full_output)
		loginf(full_output)
		if pid > 0:
			try:
				os.system('./off')
			except Exception as e:
				logerr(str(e.args[0]))
				with open('station.e', 'a') as ferr:
					ferr.write('{} {}\n'.format(str(e), full_output))
		elif pid < 0:
			try:
				os.system('./on')
			except Exception as e:
				logerr(str(e.args[0]))
				with open('station.e', 'a') as ferr:
					ferr.write('{} {}\n'.format(str(e), full_output))
		else:
			logerr('error {} not a number'.format(pid))
			raise ValueError('error not a number')

	tm.sleep(cadence)


def solution(nums: list[int]) -> int:
    slow, fast = 0, 0
    while True:
        slow = nums[slow]
        fast = nums[nums[fast]]
        if slow == fast:
            break

    slow2 = 0
    while slow != slow2:
        slow, slow2 = nums[slow], nums[slow2]

    return slow2

nums = [1, 3, 2, 2, 4]
print(solution(nums))


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

# Change default path
# sys.path.append('PROGMOD_')

# Function for formatting file to array of specific variables
# data = txt file
# sh_index = state history index, i.e. controller or environment
# var_index = variable index, i.e. timestamp, joint_angle etc...
def txt_to_variable_array (data, sh_index, var_index):
    sh = data.split("|")[sh_index]
    vars = sh.split("/")[var_index]
    seperated = vars.split("&")

    # Remove blank spaces
    index_to_remove = []
    for i, e in enumerate(seperated):
        if (e == ""):
            index_to_remove.append(i)
    for e in reversed(index_to_remove):
        seperated.pop(e)

    # Convert from strings to float
    in_numbers = [float(numerical_string) for numerical_string in seperated]

    return (in_numbers, seperated, vars)

def plotxy (x, y, y2, xlab, ylab, xunit, yunit):

    # PLOT XY DATA
    plt.plot(x, y, "ro-", color="blue", label="Original")
    plt.plot(x, y2, "ro-", color="red", label="Improved")
    plt.xlabel(xlab)
    plt.ylabel(ylab)

    # SET UNITS
    plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter(f'%.1f {xunit}'))
    plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter(f'%.1f {yunit}'))

    plt.grid()
    plt.legend()
    plt.show()

# Main
def main():
    # Changing default matplotlib font: https://jonathansoma.com/lede/data-studio/matplotlib/changing-fonts-in-matplotlib/
    matplotlib.rcParams['font.serif'] = "Palatino Linotype" # Change default serif font
    matplotlib.rcParams['font.family'] = "serif" # Set default family to serif

    x_num =          [2, 5, 10, 15, 17, 20, 22, 25, 30, 32]
    y_num_original = [3 ,24, 99, 224, 288, 399, 483, 624, 899, 1023]
    y_num_improved = [4, 13, 28, 43, 49, 58, 64, 73, 88, 94]

    plotxy(x_num, y_num_original, y_num_improved, "No. of processes", "No. of messages", "", "")

if __name__ == "__main__":
    main()


'''--------------SANKE,WATER AND GUN GAME------------------
-------------DEVELOPED BY : RANNJEET PRAJAPATI--------'''''

import random
def game(comp,your):
    if comp=='s':
        if your=='w':
            return False
        elif your=='g':
            return True
    elif comp=='w':
        if your=='g':
            return False
        elif your=='s':
            return True
    elif comp=='g':
        if your=='s':
            return False
        elif your=='w':
            return True
    elif comp==your:
        return None

print("computer's turn: Snake(s),water(w),Gun(g)")
randno= random.randint(1,3)
if randno==1:
    comp='s'
elif randno==2:
    comp='w'
elif randno==3:
    comp='g'

print("Your turn: Snake(s),water(w),Gun(g)")
your=input()
print("computer chooses:",comp)
print("you chooses:",your)
result=game(comp,your)
if result==True:
    print ("You win!")
elif result==False:
    print("You lose!")
elif result==None:
    print("Game is tie!")


from PyQt5 import QtCore
from PyQt5.QtCore import QObject, QThreadPool, pyqtSignal
from PyQt5.QtWidgets import QWidget, QScrollArea

from cvstudio.util import GUIUtilities
from cvstudio.view.widgets import ImageButton
from cvstudio.view.widgets.loading_dialog import QLoadingDialog
from cvstudio.view.widgets.response_grid import ResponseGridLayout, GridCard
from cvstudio.view.wizard import ModelWizard

class ModelsGridWidget(QWidget, QObject):
    new_item_action = pyqtSignal()

    def __init__(self, parent=None):
        super(ModelsGridWidget, self).__init__(parent)
        self.grid_layout = ResponseGridLayout()
        self.grid_layout.setAlignment(QtCore.Qt.AlignTop)
        self.grid_layout.cols = 8
        self.setLayout(self.grid_layout)
        self._entries = None

    def build_new_button(self):
        new_item_widget: GridCard = GridCard(with_actions=False, with_title=False)
        btn_new_item = ImageButton(GUIUtilities.get_icon("new_folder.png"))
        btn_new_item.clicked.connect(lambda: self.new_item_action.emit())
        new_item_widget.body = btn_new_item
        return new_item_widget

    def bind(self):
        cards_list = []
        new_item_button = self.build_new_button()
        cards_list.append(new_item_button)
        self.grid_layout.widgets = cards_list
        super(ModelsGridWidget, self).update()

class ModelsTabWidget(QScrollArea):
    def __init__(self, parent=None):
        super(ModelsTabWidget, self).__init__(parent)
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.data_grid = ModelsGridWidget()
        self.data_grid.new_item_action.connect(self.data_grid_new_item_action_slot)
        self.setWidget(self.data_grid)
        self.setWidgetResizable(True)
        self._thread_pool = QThreadPool()
        self._loading_dialog = QLoadingDialog()
        self.load()

    def data_grid_new_item_action_slot(self):
        new_model_wizard = ModelWizard()
        new_model_wizard.exec_()

    def load(self):
        self.data_grid.bind()


import pandas as pd
import numpy as np
import time
import pylab as pl
import math
import os
import pickle
import gzip
from operator import itemgetter
from matplotlib import collections  as mc
import copy
from random import randint
from itertools import *

def save_in_file_fast(arr, file_name):
    pickle.dump(arr, open(file_name, 'wb'))

def load_from_file_fast(file_name):
    return pickle.load(open(file_name, 'rb'))

def SieveOfEratosthenes(n):
    # Create a boolean array "prime[0..n]" and initialize
    #  all entries it as true. A value in prime[i] will
    # finally be false if i is Not a prime, else true.
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):

        # If prime[p] is not changed, then it is a prime
        if (prime[p] == True):

            # Update all multiples of p
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    return prime

def get_primes():
    cache_path = OUTPUT_PATH + 'prime_list.pkl'
    if not os.path.isfile(cache_path):
        n = 200000
        prime = SieveOfEratosthenes(n)
        plist = []
        for p in range(2, n):
            if prime[p]:
                plist.append(p)
        save_in_file_fast(set(plist), cache_path)
    else:
        plist = load_from_file_fast(cache_path)

    return plist

# Globals
INPUT_PATH = 'input/'
OUTPUT_PATH = './'
CITIES = pd.read_csv('input/cities.csv')
PRIMES = get_primes()

all_ids = CITIES['CityId'].values
all_x = CITIES['X'].values
all_y = CITIES['Y'].values

CITIES_HASH = dict()
for i, id in enumerate(all_ids):
    CITIES_HASH[id] = (all_x[i], all_y[i])

def isPrime(num):
    return num in PRIMES

ORIGINAL = pd.read_csv('optimized_submission.csv')['Path'].values

def get_complete_score(tour):

    score = 0.0
    for i in range(0, len(tour)-1):
        p1 = CITIES_HASH[tour[i]]
        p2 = CITIES_HASH[tour[i+1]]
        stepSize = math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]))
        if ((i + 1) % 10 == 0) and (tour[i] not in PRIMES):
            stepSize *= 1.1
        score += stepSize
    return score

def get_score(tour, start, end):
    score = 0.0
    for i in range(start, end):
        p1 = CITIES_HASH[tour[i]]
        p2 = CITIES_HASH[tour[i+1]]
        stepSize = math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]))
        if ((i + 1) % 10 == 0) and (tour[i] not in PRIMES):
            stepSize *= 1.1
        score += stepSize
    return score

def swap(arr, i, j):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp

def save_to_csv(arr):
    sub = pd.DataFrame(np.array(arr), columns = ["Path"])
    sub.to_csv('optimized_submission.csv', index=None)

def optimization2():
    numIter = 50000000
    iteration = 0
    modified = copy.deepcopy(ORIGINAL)
    total_reduction = 0.0
    last_saved_reduction = 0.0
    radius = 0
    while True:
        radius += 1
        print("radius increase to: " + str(radius))
        for step in range(9, len(ORIGINAL) - 1, 10):
            best_reduction = 0
            for i in [-radius, radius]:
                # save every 100000 iterations
                if iteration % 100000 == 0:
                    print("iteration: " + str(iteration))
                    if last_saved_reduction > total_reduction:
                        last_saved_reduction = total_reduction
                        print("saving to csv")
                        print("total reduction so far: " + str(total_reduction))
                        save_to_csv(modified)
                        print("score so far: " + str(get_complete_score(modified)))
                    else:
                        print("no improvement")
                iteration += 1
                index = step + i
                if (index < len(ORIGINAL) and index > 0):
                    start = min(step, index) - 1
                    end = min(max(step, index) + 1, len(ORIGINAL) - 1)
                    original_score = get_score(modified, start, end)
                    swap(modified, step, index)
                    modified_score = get_score(modified, start, end)
                    score_diff = modified_score - original_score
                    if score_diff < best_reduction:
                        best_reduction = score_diff
                        best_swap = [step, index]
                    # swap back
                    swap(modified,  step, index)
            if (best_reduction < 0):
                swap(modified, best_swap[0], best_swap[1])
                total_reduction += best_reduction
    return modified, total_reduction

def optimization3():
    numIter = 50000000
    iteration = 0
    modified = copy.deepcopy(ORIGINAL)
    total_reduction = 0.0
    last_saved_reduction = 0.0
    radius = 9
    last_checked_iteration = 0
    while True:
        radius += 1
        print("radius increase to: " + str(radius))
        for origin in range(9, len(ORIGINAL) - 1, 10):
            best_reduction = 0
            start = max(origin - radius, 0)
            end = min(origin + radius, len(ORIGINAL) - 1)
            path_to_check = copy.deepcopy(modified[start:end])
            original_score = get_score(modified, max(start - 1, 0), min(end + 1, len(ORIGINAL) - 1))
            best_combo = copy.deepcopy(path_to_check)
            for combo in permutations(path_to_check, len(path_to_check)):
                iteration += 1
                for i in range(len(combo)):
                    modified[origin - radius + i] = combo[i]
                modified_score = get_score(modified, max(start - 1, 0), min(end + 1, len(ORIGINAL) - 1))
                score_diff = modified_score - original_score
                if score_diff < best_reduction:
                    best_reduction = score_diff
                    best_combo = copy.deepcopy(combo)
            total_reduction += best_reduction
            for j in range(len(best_combo)):
                modified[origin - radius + j] = best_combo[j]
            # save appx. every 100000 iterations
            if iteration > last_checked_iteration + 100000:
                last_checked_iteration = iteration
                print("iteration: " + str(iteration))
                if last_saved_reduction > total_reduction:
                    last_saved_reduction = total_reduction
                    print("saving to csv")
                    print("total reduction so far: " + str(total_reduction))
                    save_to_csv(modified)
                    print("score so far: " + str(get_complete_score(modified)))
                else:
                    print("no improvement")
    return modified, total_reduction

print("starting ...")
naive_modified3, total_reduction = optimization3()
print("end result")
print("improve score by: " + str(-total_reduction))
print("end!")


from langchain.chat_models import ChatVertexAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage

chat = ChatVertexAI()

messages = [
    SystemMessage(content="You are a helpful assistant that answer questions."),
    HumanMessage(content="Why is the sky blue?"),
]
print(chat(messages))

template = """
You are a helpful assistant that answer questions.
"""
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

chat_prompt = ChatPromptTemplate.from_messages(
    [system_message_prompt, human_message_prompt]
)

# get a chat completion from the formatted messages
resp = chat(chat_prompt.format_prompt(text="why is the sky blue.").to_messages())
print(resp)


import abc
import numpy as np
from nn.utils.label_mapper import LabelMapper
from datetime import datetime

class LearningAlgorithmTypes(object):
    SGD = "stochastic gradient descent"

class LearningAlgorithmFactory(object):
    @staticmethod
    def create_learning_algorithm_from_type(learning_algorithm_type):
        if learning_algorithm_type == LearningAlgorithmTypes.SGD:
            return SGD()
        else:
            raise NotImplementedError("Requested learning algorithm type not yet implemented")

class AbstractLearningAlgorithm(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def learn(self, *args, **kwargs):
        raise NotImplementedError()

class SGD(AbstractLearningAlgorithm):
    def __init__(self):
        super(SGD, self).__init__()

    def learn(self, network, training_data_set, number_of_epochs, learning_rate, size_of_batch, **kwargs):
        for epoch in range(number_of_epochs):
            print("Epoch: " + str(epoch) + " Start time: " + str(datetime.now()))
            np.random.shuffle(training_data_set.data_instances)

            for batch in np.array_split(training_data_set.data_instances,
                                        len(training_data_set.data_instances) / size_of_batch):
                self.__update_weights_and_bias(network, batch, learning_rate)

    def __update_weights_and_bias(self, network, batch, learning_rate):
        number_of_training_instances = len(batch)
        updated_biases = map(lambda layer_biases: np.zeros(layer_biases.shape), network.biases)
        updated_weights = map(lambda layer_weights: np.zeros(layer_weights.shape), network.weights)

        for data_instance in batch:
            # Computing the partial derivatives of the function cost w.r.t. each weight and bias. These partial
            # derivatives are the gradient's components.
            delta_biases, delta_weights = self.__back_propagate(network, data_instance.features_values_vector,
                                                                data_instance.label)

            # Accumulating the delta of weights and biases for each training sample of the batch in order to adjust
            # the network's weights and biases in the opposite direction of the gradient.
            updated_biases = [new_bias + delta for new_bias, delta in zip(updated_biases, delta_biases)]
            updated_weights = [new_weight + delta for new_weight, delta in zip(updated_weights, delta_weights)]

        # Updating the network's weights and biases in the opposite direction of the cost function's gradient
        network.weights = [current_weight - (learning_rate / number_of_training_instances) * new_weight
                           for current_weight, new_weight in zip(network.weights, updated_weights)]
        network.biases = [current_bias - (learning_rate / number_of_training_instances) * new_bias
                          for current_bias, new_bias in zip(network.biases, updated_biases)]

    def __back_propagate(self, network, output_vector, expected_output_label):
        last_layer = -1
        updated_biases = map(lambda layer_biases: np.zeros(layer_biases.shape), network.biases)
        updated_weights = map(lambda layer_weights: np.zeros(layer_weights.shape), network.weights)

        output_vectors_by_layer = [output_vector.reshape(1, len(output_vector))]
        input_vectors_by_layer = []

        for bias, weights in zip(network.biases, network.weights):
            next_layer_input = np.dot(output_vector, weights) + bias.T
            input_vectors_by_layer.append(next_layer_input)
            output_vector = network.neuron.compute(next_layer_input)
            output_vectors_by_layer.append(output_vector)

        delta = network.cost_computer.compute_cost_derivative(output_vector=output_vectors_by_layer[last_layer],
                                                              expected_output_vector=LabelMapper().map_label_to_vector(
                                                                      expected_output_label)) * \
                network.neuron.compute_derivative(input_vectors_by_layer[last_layer])
        updated_biases[last_layer] = delta.T
        updated_weights[last_layer] = np.dot(output_vectors_by_layer[last_layer - 1].T, delta)

        for layer_index in xrange(2, network.number_of_layers):
            z = input_vectors_by_layer[-layer_index]
            sp = network.neuron.compute_derivative(z)
            delta = np.dot(delta, network.weights[-layer_index + 1].T) * sp
            updated_biases[-layer_index] = delta.T
            updated_weights[-layer_index] = np.dot(output_vectors_by_layer[-layer_index - 1].T, delta)

        return updated_biases, updated_weights


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

import os, sys, datetime, json
from core import info_collection
from conf import settings

# import check python version model
from plugins.detector import check_version

class ArgvHandler(object):
    def __init__(self, argvs):
        self.argvs = argvs
        self.parse_argv()

    def parse_argv(self):
        if len(self.argvs) > 1:
            if hasattr(self, self.argvs[1]):
                func = getattr(self, self.argvs[1])
                func()
            else:
                self.help()
        else:
            self.help()

    def help(self):
        msg = '''
        collect
        report
        '''
        print(msg)

    def collect(self):
        obj = info_collection.InfoCollection()
        asset_data = obj.collect()
        print(asset_data)

    def __load_asset_id(self):
        '''
        deal local asset_id
        :return: asset_id
        '''
        asset_id_file = settings.Params["asset_id"]
        has_asset_id = False
        if os.path.isfile(asset_id_file):
            asset_id = open(asset_id_file).read().strip()
            asset_id = int(asset_id)
            if asset_id.isdigit():
                return asset_id
            else:
                has_asset_id = False

    def __import_model(self, cmd1, cmd2):
        '''
        deal urllib request command
        :param cmd1: urllib py3 or urllib2 py2 request comand
        :param cmd2: urllib.parse py3 or urllib py2  comand
        :return: return request command and urlencode command
        '''
        cmd_urllib1 = __import__(cmd1)
        cmd_urllib2 = __import__(cmd2)
        return cmd_urllib1, cmd_urllib2

    def __deal_urllib(self, cmd_urllib1, cmd_urllib2, url, data=None, pyversion=None, method=None):
        '''
        deal python2 or python3 urllib request
        :param cmd_urllib1: urllib request model
        :param cmd_urllib2: urllib urlencode model
        :param url: antOps server url
        :param data: system info data
        :param pyversion: python version
        :param method: get or post
        :return: return server callback info
        '''
        if method == "get":
            req = cmd_urllib1.Request(url)
            res_data = cmd_urllib1.urlOpen(req, timeout=settings.Params["request_timeout"])
            callback = res_data.read()
            print("--->server response: ", callback)
            return callback
        elif method =="post":
            data_encode = cmd_urllib2.urlencode(data, encoding='utf-8')
            if pyversion == 3:
                # 解决 POST data should be bytes or an iterable of bytes. It cannot be of type str.
                #  # 这里需要测试，存在bug
                req = cmd_urllib1.request.Request(url=url, data=bytes(data_encode, encoding='utf-8')) # python 3.x version
                res_data = cmd_urllib1.urlOpen(req, timeout=settings.Params["request_timeout"])
                callback = res_data.read()
                callback = str(callback, encoding='utf-8')
            elif pyversion == 2:
                req = cmd_urllib1.Request(url=url, data=data_encode)
                res_data = cmd_urllib1.urlOpen(req, timeout=settings.Params["request_timeout"])
                callback = res_data.read()
            callback = json.load(callback)
            print("\033[31;1m[%s]:[%s]\033[0m response:\n%s" % (method, url, callback))
            return callback

    def __submit_data(self, url, data, method):
        '''
        This model is compability python2 and python3
        :param url: antOps server url
        :param data: system info data
        :param method: get or post
        :return: return server callback info
        '''
        if url in settings.Params["urls"]:
            if type(settings.Params["port"]) is int:
                url = "http://%s:%s%s" % (
                    settings.Params["server"], settings.Params["port"], settings.Params["urls"][url])
            else:
                url = "http://%s/%s" % (settings.Params["server"], settings.Params["urls"][url])
            print("Connectins.. \n \033[32;2m[%s] \033[0m, it may take a minute..." % url)
            if method == "get":
                args = ""
                for k, v in data.items():
                    args += "&%s=%s" % (k, v)
                args = args[1:]
                url_with_args = "%s?%s" % (url, args)
                try:
                    pversion = check_version.check_python()
                    if pversion == 3:
                        cmd_urllib1, cmd_urllib2 = self.__import_model("urllib.request", "urllib.parse")
                        callback = self.__deal_urllib(cmd_urllib1, cmd_urllib2, url_with_args, method="get")
                        return callback
                    elif pversion == 2:
                        cmd_urllib1, cmd_urllib2 = self.__import_model("urllib2", "urllib")
                        callback = self.__deal_urllib(cmd_urllib1, cmd_urllib2, url_with_args, method="get")
                        return callback
                except cmd_urllib1.request.URLError:
                    sys.exit("\033[31;1m%s\033[0m" % cmd_urllib1.request.URLError)
            elif method == "post":
                try:
                    pversion = check_version.check_python()
                    if pversion == 3:
                        cmd_urllib1, cmd_urllib2 = self.__import_model("urllib.request", "urllib.parse")
                        callback = self.__deal_urllib(cmd_urllib1, cmd_urllib2, url, data=data, pyversion=pversion, method="get")
                        return callback
                    elif pversion == 2:
                        cmd_urllib1, cmd_urllib2 = self.__import_model("urllib2", "urllib")
                        callback = self.__deal_urllib(cmd_urllib1, cmd_urllib2, url, data=data, pyversion=pversion, method="get")
                        return callback
                except Exception:
                    sys.exit("\033[31;1m%s\033[0m" % Exception)
            else:
                raise KeyError

    def __update_asset_id(self, asset_id):
        asset_id_file = settings.Params["asset_id"]
        f = open(asset_id_file, "w")
        f.write(str(asset_id))
        f.close()

    def report(self):
        obj = info_collection.InfoCollection()
        asset_data = obj.collect()
        asset_id = self.__load_asset_id() # load from asset_id file
        if asset_id:
            asset_data["asset_id"] = asset_id
            post_url = "asset_update"
        else:
            asset_data["asset_id"] = None
            post_url = "asset_report"
        data = {"asset_data": json.dumps(asset_data)}
        response = self.__submit_data(post_url, data, method="post")
        if "asset_id" in response:
            self.__update_asset_id(response["asset_id"])
        self.log_record(response)

    def log_record(self, log_mesg):
        '''
        log deal model
        :param log_mesg: server callback info
        :return: None
        '''
        f = open(settings.Params["log_file"], "a")
        if log_mesg is str:
            pass
        if type(log_mesg) is dict:
            if "info" in log_mesg:
                for msg in log_mesg["info"]:
                    log_format = "%s\tINFO\t%s\n" % (datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S"), msg)
                    f.write(log_format)
            if "error" in log_mesg:
                for msg in log_mesg["error"]:
                    log_format = "%s\tERROR\t%s\n" %(datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S"),msg)
                    f.write(log_format)
            if "warning" in log_mesg:
                for msg in log_mesg["warning"]:
                    log_format = "%s\tWARNING\t%s\n" %(datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S"),msg)
                    f.write(log_format)
        f.close()


class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        memo = [[0] * (n+1) for _ in range(m+1)]
        return self.dfs(m , n , memo)

    def dfs(self, m, n, memo):  # methods of postion m, n

        if m < 0 or n < 0:
            return 0
        if m == 1 and n == 1:
            return 1
        if memo[m][n]:
            return memo[m][n]
        up = self.dfs(m - 1, n, memo)
        left = self.dfs(m, n - 1, memo)
        memo[m][n] = up + left
        return memo[m][n]

class Solution2(object):
    def uniquePaths(self, m, n):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """

        if m < 0 or n < 0:
            return 0

        dp = [[0] * (n + 1) for _ in range(m + 1)]

        # m, n = len(obstacleGrid), len(obstacleGrid[0])
        # if obstacleGrid[0][0] == 0:
        dp[1][1] = 1
        for i in range(1, m + 1):
            for j in range(1, n + 1):
                if i == 1 and j == 1:
                    continue
                else:
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
        return dp[m][n]


from pirc522 import RFID
import signal
import time

class Rfid_Oku:
    def oku():
        rdr = RFID()
        util = rdr.util()
        util.debug = True

        rdr.wait_for_tag()
        (error, data) = rdr.request()

        if not error:
            #print("Kart Algilandi!")
            (error, uid) = rdr.anticoll()
            if not error:
                kart_uid = str(uid[0])+" "+str(uid[1])+" "+str(uid[2])+" "+str(uid[3])+" "+str(uid[4])
                return (kart_uid)

    def result(self,sonuc):
        return sonuc

Raspberry Pi RFID uygulaması/Rfid_Oku.py


#http://www.pythonchallenge.com/pc/def/ocr.html
__author__ = 'chihchieh.sun'

s = ''.join([line.rstrip() for line in open('level2_ocr.txt')])

OCCURRENCES = {}

for c in s:
    OCCURRENCES[c] = OCCURRENCES.get(c, 0) + 1

avgOC = len(s) // len(OCCURRENCES)

print(''.join([c for c in s if OCCURRENCES[c] < avgOC]))


import numpy as np
from utils import plot_output

# Defining parameters
N = 500
L = 5  # Topological charge number
A3 = np.zeros((N, N), dtype='complex_')
# Constructing SPP
x = np.array([i for i in range(N)])
y = np.array([i for i in range(N)])

X, Y = np.meshgrid(x, y)
theta = np.arctan2((X - N/2), (Y - N/2))
r = np.sqrt((X - N/2) * (X - N/2) + (Y - N/2) * (Y - N/2))
A1 = L * (theta + np.pi)
A2 = np.fmod(A1, 2*np.pi)

for p in range(N):
    for q in range(N):
        if np.fmod(A2[p, q], 2*np.pi) <= np.pi:
            A3[p, q] = np.exp(1j * np.pi)
        else:
            A3[p, q] = np.exp(1j * 0)
A3[r > 30] = 0

plot_output(A3, N, angle=True, spiral=True)


import numpy as np
from gc_utils import dictionary_to_vector
from gc_utils import gradients_to_vector
from gc_utils import relu
from gc_utils import sigmoid
from gc_utils import vector_to_dictionary
from public_tests import *
from testCases import *

# GRADED FUNCTION: forward_propagation

def forward_propagation(x, theta):
    """
    Implement the linear forward propagation (compute J) presented in Figure 1 (J(theta) = theta * x)

    Arguments:
    x -- a real-valued input
    theta -- our parameter, a real number as well

    Returns:
    J -- the value of function J, computed using the formula J(theta) = theta * x
    """

    J = theta * x

    return J

def backward_propagation(x, theta):
    """
    Computes the derivative of J with respect to theta (see Figure 1).

    Arguments:
    x -- a real-valued input
    theta -- our parameter, a real number as well

    Returns:
    dtheta -- the gradient of the cost with respect to theta
    """

    dtheta = x

    return dtheta

def gradient_check(
    x,
    theta,
    epsilon=1e-7,
    print_msg=False,
):
    """
    Implement the gradient checking presented in Figure 1.

    Arguments:
    x -- a float input
    theta -- our parameter, a float as well
    epsilon -- tiny shift to the input to compute approximated gradient with formula(1)

    Returns:
    difference -- difference (2) between the approximated gradient and the backward propagation gradient. Float output
    """

    theta_plus = theta + epsilon
    theta_minus = theta - epsilon
    J_plus = forward_propagation(x, theta_plus)
    J_minus = forward_propagation(x, theta_minus)
    gradapprox = (J_plus - J_minus) / (2 * epsilon)

    grad = backward_propagation(x, theta)

    numerator = np.linalg.norm(grad - gradapprox)
    denominator = np.linalg.norm(grad) + np.linalg.norm(gradapprox)
    difference = numerator / denominator

    if print_msg:
        if difference > 2e-7:
            print(
                "\033[93m"
                + "There is a mistake in the backward propagation! difference = "
                + str(difference)
                + "\033[0m"
            )
        else:
            print(
                "\033[92m"
                + "Your backward propagation works perfectly fine! difference = "
                + str(difference)
                + "\033[0m"
            )

    return difference

def forward_propagation_n(X, Y, parameters):
    """
    Implements the forward propagation (and computes the cost) presented in Figure 3.

    Arguments:
    X -- training set for m examples
    Y -- labels for m examples
    parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3":
                    W1 -- weight matrix of shape (5, 4)
                    b1 -- bias vector of shape (5, 1)
                    W2 -- weight matrix of shape (3, 5)
                    b2 -- bias vector of shape (3, 1)
                    W3 -- weight matrix of shape (1, 3)
                    b3 -- bias vector of shape (1, 1)

    Returns:
    cost -- the cost function (logistic cost for m examples)
    cache -- a tuple with the intermediate values (Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3)

    """

    # retrieve parameters
    m = X.shape[1]

    W1 = parameters["W1"]
    b1 = parameters["b1"]

    W2 = parameters["W2"]
    b2 = parameters["b2"]

    W3 = parameters["W3"]
    b3 = parameters["b3"]

    # LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
    Z1 = np.dot(W1, X) + b1
    A1 = relu(Z1)

    Z2 = np.dot(W2, A1) + b2
    A2 = relu(Z2)

    Z3 = np.dot(W3, A2) + b3
    A3 = sigmoid(Z3)

    # Cost
    log_probs = np.multiply(-np.log(A3), Y) + np.multiply(-np.log(1 - A3), 1 - Y)

    cost = 1.0 / m * np.sum(log_probs)

    cache = (Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3)

    return cost, cache

def gradient_check_n(
    parameters,
    gradients,
    X,
    Y,
    epsilon=1e-7,
    print_msg=False,
):
    """
    Checks if backward_propagation_n computes correctly the gradient of the cost output by forward_propagation_n

    Arguments:
    parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
    grad -- output of backward_propagation_n, contains gradients of the cost with respect to the parameters
    X -- input datapoint, of shape (input size, number of examples)
    Y -- true "label"
    epsilon -- tiny shift to the input to compute approximated gradient with formula(1)

    Returns:
    difference -- difference (2) between the approximated gradient and the backward propagation gradient
    """

    # Set-up variables
    parameters_values, _ = dictionary_to_vector(parameters)

    grad = gradients_to_vector(gradients)
    num_parameters = parameters_values.shape[0]
    J_plus = np.zeros((num_parameters, 1))
    J_minus = np.zeros((num_parameters, 1))
    gradapprox = np.zeros((num_parameters, 1))

    # Compute gradapprox
    for i in range(num_parameters):
        # Compute J_plus[i]. Inputs: "parameters_values, epsilon". Output = "J_plus[i]".
        # "_" is used because the function you have outputs two parameters but we only
        # care about the first one

        theta_plus = np.copy(parameters_values)
        theta_plus[i] = theta_plus[i] + epsilon
        J_plus[i], _ = forward_propagation_n(X, Y, vector_to_dictionary(theta_plus))

        theta_minus = np.copy(parameters_values)
        theta_minus[i] = theta_minus[i] - epsilon
        J_minus[i], _ = forward_propagation_n(X, Y, vector_to_dictionary(theta_minus))

        gradapprox[i] = (J_plus[i] - J_minus[i]) / (2 * epsilon)

    numerator = np.linalg.norm(grad - gradapprox)
    denominator = np.linalg.norm(grad) + np.linalg.norm(gradapprox)
    difference = numerator / denominator

    if print_msg:
        if difference > 2e-7:
            print(
                "\033[93m"
                + "There is a mistake in the backward propagation! difference = "
                + str(difference)
                + "\033[0m"
            )
        else:
            print(
                "\033[92m"
                + "Your backward propagation works perfectly fine! difference = "
                + str(difference)
                + "\033[0m"
            )

    return difference

Course 5, Improving Deep Neural Networks/Week 1/gradient_checking.py