from __future__ import print_function
from gunpowder import *
from gunpowder.tensorflow import *
from gunpowder.contrib import ZeroOutConstSections, AddDistance
import tensorflow as tf
import os
import math
import json
import sys
import logging
print("syspath", sys.path)
import z5py

class Label(object):
    def __init__(self, labelname, labelid, scale_loss=True, scale_key=None):
        self.labelname= labelname
        self.labelid = labelid

        self.gt_dist_key = ArrayKey('GT_DIST_'+self.labelname.upper())
        self.pred_dist_key = ArrayKey('PRED_DIST_'+self.labelname.upper())
        self.scale_loss = scale_loss
        if self.scale_loss:
            self.scale_key = ArrayKey('SCALE_'+self.labelname.upper())
        if scale_key is not None:
            self.scale_key = scale_key

def train_until(max_iteration, data_sources, input_shape, output_shape, dt_scaling_factor, loss_name, labels):
    ArrayKey('RAW')
    ArrayKey('ALPHA_MASK')
    ArrayKey('GT_LABELS')
    ArrayKey('MASK')

    data_providers = []
    data_dir = "/groups/saalfeld/saalfeldlab/larissa/data/cell/{0:}.n5"
    voxel_size = Coordinate((2, 2, 2))
    input_size = Coordinate(input_shape) * voxel_size
    output_size = Coordinate(output_shape) * voxel_size

    if tf.train.latest_checkpoint('.'):
        trained_until = int(tf.train.latest_checkpoint('.').split('_')[-1])
        print('Resuming training from', trained_until)
    else:
        trained_until = 0
        print('Starting fresh training')
    for src in data_sources:
        n5_source = N5Source(
            os.path.join(data_dir.format(src)),
            datasets={
                ArrayKeys.RAW: 'volumes/raw',
                ArrayKeys.GT_LABELS: 'volumes/labels/all',
                ArrayKeys.MASK: 'volumes/mask'
            },
            array_specs={
                ArrayKeys.MASK: ArraySpec(interpolatable=False)
            }
        )
        data_providers.append(n5_source)

    with open('net_io_names.json', 'r') as f:
        net_io_names = json.load(f)

    inputs = dict()
    inputs[net_io_names['raw']] = ArrayKeys.RAW
    outputs = dict()
    snapshot = dict()
    snapshot[ArrayKeys.RAW] = 'volumes/raw'
    snapshot[ArrayKeys.GT_LABELS] = 'volumes/labels/gt_labels'
    for label in labels:
        inputs[net_io_names['gt_'+label.labelname]] = label.gt_dist_key
        if label.scale_loss or label.scale_key is not None:
            inputs[net_io_names['w_'+label.labelname]] = label.scale_key
        outputs[net_io_names[label.labelname]] = label.pred_dist_key
        snapshot[label.gt_dist_key] = 'volumes/labels/gt_dist_'+label.labelname
        snapshot[label.pred_dist_key] = 'volumes/labels/pred_dist_'+label.labelname

    # specifiy which Arrays should be requested for each batch
    request = BatchRequest()
    snapshot_request = BatchRequest()

    request.add(ArrayKeys.RAW, input_size, voxel_size=voxel_size)
    request.add(ArrayKeys.GT_LABELS, output_size,  voxel_size=voxel_size)
    request.add(ArrayKeys.MASK, output_size, voxel_size=voxel_size)

    for label in labels:
        request.add(label.gt_dist_key, output_size, voxel_size=voxel_size)
        snapshot_request.add(label.pred_dist_key, output_size, voxel_size=voxel_size)
        if label.scale_loss:
            request.add(label.scale_key, output_size, voxel_size=voxel_size)

    # create a tuple of data sources, one for each HDF file
    data_sources = tuple(
        provider +
        Normalize(ArrayKeys.RAW) + # ensures RAW is in float in [0, 1]

        # zero-pad provided RAW and MASK to be able to draw batches close to
        # the boundary of the available data
        # size more or less irrelevant as followed by Reject Node
        Pad(ArrayKeys.RAW, None) +
        RandomLocation(min_masked=0.5, mask=ArrayKeys.MASK) # chose a random location inside the provided arrays
        #Reject(ArrayKeys.MASK) # reject batches wich do contain less than 50% labelled data

        for provider in data_providers)

    train_pipeline = (
        data_sources +
        RandomProvider() +
        ElasticAugment((100, 100, 100), (10., 10., 10.), (0, math.pi/2.0),
                       prob_slip=0, prob_shift=0, max_misalign=0,
                       subsample=8) +
        SimpleAugment() +
        #ElasticAugment((40, 1000, 1000), (10., 0., 0.), (0, 0), subsample=8) +
        IntensityAugment(ArrayKeys.RAW, 0.95, 1.05, -0.05, 0.05) +
        IntensityScaleShift(ArrayKeys.RAW, 2, -1) +
        ZeroOutConstSections(ArrayKeys.RAW))

    for label in labels:
        train_pipeline += AddDistance(label_array_key=ArrayKeys.GT_LABELS,
                        distance_array_key=label.gt_dist_key,
                        normalize='tanh',
                        normalize_args=dt_scaling_factor,
                        label_id=label.labelid)

    train_pipeline = (train_pipeline)
    for label in labels:
        if label.scale_loss:
            train_pipeline += BalanceByThreshold(label.gt_dist_key, label.scale_key, mask=ArrayKeys.MASK)
    train_pipeline = (
        train_pipeline +
        PreCache(
            cache_size=40,
            num_workers=10)+

        Train(
            'build',
            optimizer=net_io_names['optimizer'],
            loss=net_io_names[loss_name],
            inputs=inputs,
            summary=net_io_names['summary'],
            log_dir='log',
            outputs=outputs,
            gradients={}
        ) +
        Snapshot(snapshot,
            every=500,
            output_filename='batch_{iteration}.hdf',
            output_dir='snapshots/',
            additional_request=snapshot_request) +

        PrintProfilingStats(every=50))

    print("Starting training...")
    with build(train_pipeline) as b:
        for i in range(max_iteration):
            b.request_batch(request)

    print("Training finished")

if __name__ == "__main__":
    #set_verbose(False)
    logging.basicConfig(level=logging.INFO)
    data_sources = ['gt_cell2_v1', ]
    input_shape = (196, 196, 196)
    output_shape = (92, 92, 92)
    dt_scaling_factor = 50
    max_iteration = 400000
    loss_name = 'loss_total_unbalanced'
    labels = []
    labels.append(Label('ECS', (6,7)))
    labels.append(Label('cell', (1,2,3,4,5,8,9,10,11,12,13,14)))
    labels.append(Label('plasma_membrane', 5))
    labels.append(Label('ERES', (12,13)))
    labels.append(Label('ERES_membrane', 12, scale_loss=False, scale_key=labels[-1].scale_key))
    labels.append(Label('MVB', (3,9)))
    labels.append(Label('MVB_membrane', 3, scale_loss=False, scale_key=labels[-1].scale_key))
    labels.append(Label('er', (4,8)))
    labels.append(Label('er_membrane', 4, scale_loss=False, scale_key=labels[-1].scale_key))
    labels.append(Label('mito', (1,2)))
    labels.append(Label('mito_membrane', 2, scale_loss=False, scale_key=labels[-1].scale_key))
    labels.append(Label('vesicles', 10))
    labels.append(Label('microtubules', 11))
    train_until(max_iteration, data_sources, input_shape, output_shape, dt_scaling_factor, loss_name, labels)


#!/usr/bin/python3
"""
Starts Flask web app
"""
from flask import Flask, render_template
from models import storage
from models.state import State

app = Flask(__name__)
app.url_map.strict_slashes = False

@app.teardown_appcontext
def tear_down(self):
    """Removes the current SQLAlchemy session"""
    storage.close()

@app.route('/states')
def states():
    """Displays html page for states"""
    return render_template('9-states.html', state_list=storage.all(State))

@app.route('/states/<id>')
def states_ids(id):
    """Displays html for states and id"""
    try:
        state = storage.all()["State.{}".format(id)]
        return render_template('9-states.html', state=state)
    except KeyError:
        return render_template('9-states.html')

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)


from django.shortcuts import render
from django.http import HttpResponse
from .forms import post_create_form
from .models import *
from django.contrib import messages
from django.utils import timezone
from django.core.paginator import Paginator

def posts_list(request):
	posts = Post.objects.all()
	paginated_posts = Paginator(posts, 5)

	paginator = Paginator(posts, 3)
	page_number = request.GET.get('page', 1)
	page = paginator.get_page(page_number)
	is_paginated = page.has_other_pages()

	if page.has_previous():
		prev_url = '?page={}'.format(page.previous_page_number())
	else:
		prev_url = ''

	if page.has_next():
		next_url = '?page={}'.format(page.next_page_number())
	else:
		next_url = ''

	context = {
		'page_object': page,
		'is_paginated': is_paginated,
		'next_url': next_url,
		'prev_url': prev_url
	}
	return render(request, 'mainblog/posts_list.html', context=context)

def post_detail(request, slug):
	post = Post.objects.get(slug__iexact=slug)
	return render(request, 'mainblog/post_detail.html', context={'post': post})

def post_create(request):
	if request.method == "POST":
		if post_create_form(request.POST).is_valid():
			for slug in Post.objects.all():
				if slug.slug == request.POST.get("post_slug"):
					return render(request, 'mainblog/error_slug.html', context={"slug":request.POST.get("post_slug")})
					break
				else:
					post=Post.objects.create(title=request.POST.get("post_title"), body=request.POST.get("post_body"), slug=request.POST.get("post_slug"))
					posts = Post.objects.all()
					return render(request, 'mainblog/posts_list.html', context={'posts': posts})
		else:
			return HttpResponse("Invalid form")
	else:
		return render(request, 'mainblog/post_create.html', context={'form':post_create_form})

def post_edit(request, slug):
	edit_form=Post.objects.get(slug=slug)
	if request.method=="POST":
		edit_form.title = request.POST.get("post_title")+"  (edited)"
		edit_form.slug = request.POST.get("post_slug")
		edit_form.body = request.POST.get("post_body")
		edit_form.date_pub = timezone.now()
		edit_form.save()
		return render(request, 'mainblog/post_detail.html', context={'post': edit_form})
	else:
		return render(request, 'mainblog/post_edit.html', context={"post": edit_form})


import logging
from build_datasets_old import load_dataset
from lib.log import logger
from lib.dataset import target_price_variation, target_discrete_price_variation, target_binned_price_variation, discretize_ta_features
import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier, AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.decomposition import PCA
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import SimpleImputer, KNNImputer, IterativeImputer
from sklearn.feature_selection import SelectFromModel, SelectKBest, VarianceThreshold, f_classif, RFECV
from sklearn.neural_network import MLPClassifier
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC, LinearSVC
from sklearn.metrics import classification_report, confusion_matrix, plot_roc_curve
from collections import Counter
from sklearn.preprocessing import LabelEncoder, MinMaxScaler, OneHotEncoder, RobustScaler
from imblearn.pipeline import Pipeline
from matplotlib import pyplot as plt
from sklearn.metrics import mean_squared_error, accuracy_score
import numpy as np
import os
import pickle
import json, math

PARAM_GRID = {
    'c__hidden_layer_sizes':[(2,), (4,), (2,4), (4,8)],
    'c__solver':['adam'],
    'c__activation':['logistic','tanh','relu'],
    'c__alpha':[0.0001, 0.001, 0.01],
    'c__learning_rate':['constant','adaptive'],
    'c__random_state':[0],
    'c__max_iter':[2000]
}

def test_gains(close, y_pred, initial_balance=100, position_size=0.1):
    position_amount = initial_balance*position_size
    balance=initial_balance
    coins = 0
    last_price = None
    for price, y in zip(close, y_pred):
        if not price or np.isnan(price):
            continue
        if y not in [0, 1]:
            continue
        if not y: # Sell if y == 0
            amount = position_amount/price
            if coins < amount:
                amount = coins
            coins -= amount
            balance += amount*price
        else:# Buy if y == 1
            amount = position_amount/price
            if balance < position_amount:
                amount = balance/price
            balance -= amount*price
            coins += amount
        last_price = price
    if coins and last_price:
        balance += coins*last_price
        coins = 0
    return balance

def plot_class_distribution(dataset, _sym, y):
    counter = Counter(y)
    print('Dataset: {} Symbol: {}'.format(dataset, _sym))
    for k, v in counter.items():
        per = v / len(y) * 100
        print('Class=%d, n=%d (%.3f%%)' % (k, v, per))
    # plot the distribution
    #plt.title(_sym)
    #plt.bar(counter.keys(), counter.values())
    #plt.show()

def get_symbol_features(index, sym):
    data = index[sym]
    features = pd.read_csv(data['csv'], sep=',', encoding='utf-8', index_col='Date', parse_dates=True)
    # Replace nan with infinity so that it can later be imputed to a finite value
    features = features.replace([np.inf, -np.inf], np.nan)

    # Derive target classes from closing price
    target_pct = target_price_variation(features['close'])
    target = target_binned_price_variation(target_pct, n_bins=2)
    return features, target

def main():
    index = load_dataset('all_merged', return_index=True)
    for _sym, data in index.items():
        features, target = get_symbol_features(index, _sym)

        features_p = features[data['features']['ohlcv']].pct_change().replace([np.inf, -np.inf], np.nan)
        features_p.columns = [c + '_p1' for c in features_p.columns]
        features_1 = features_p.shift(1)
        features_1.columns = [c+'_lag1' for c in features_1.columns]
        features_2 = features_p.shift(2)
        features_2.columns = [c+'_lag2' for c in features_2.columns]

        features_mean = features_p.rolling(3).mean()
        features_mean.columns = [c + '_mean_3' for c in features_mean.columns]

        ta = features[data['features']['ta'] + data['features']['ta_7d'] + data['features']['ta_30d']]

        features = pd.concat([features['close'], ta, features_p, features_1, features_2, features_mean], axis=1)[30:]
        target = target[30:]
        # Split data in train and blind test set with 70:30 ratio,
        #  most ML models don't take sequentiality into account, but our pipeline
        #  uses a SimpleImputer with mean strategy, so it's best not to shuffle the data.
        X_train, X_test, y_train, y_test = train_test_split(features.values, target.values, shuffle=False,
                                                            test_size=0.3)
        logger.info("Start Feature Selection")
        imp = SimpleImputer()
        values = imp.fit_transform(X_train)
        #sel = SelectKBest(score_func=f_classif, k=min(10, X_train.shape[1]))
        feature_count = int(0.3*X_train.shape[1])
        sel = RFECV(
            estimator=RandomForestClassifier(),
            cv=5,
            verbose=0,
            n_jobs=4,
            min_features_to_select=feature_count,
            scoring='neg_mean_squared_error'
        )
        sel.fit(values, y_train)
        logger.info("End Feature Selection")
        bestfeatures = [c for c, f in zip(features.columns, sel.get_support()) if f]
        if not 'close' in bestfeatures:
            bestfeatures += ['close']
        print("Using features:\n{}".format(bestfeatures, len(bestfeatures)))

        train_features = pd.DataFrame(X_train, columns=features.columns)
        test_features = pd.DataFrame(X_test, columns=features.columns)
        X_train = train_features[bestfeatures].values
        X_test = test_features[bestfeatures].values

        # Summarize distribution
        print("Training set: # Features {}, # Samples {}".format(X_train.shape[1], X_train.shape[0]))
        plot_class_distribution("Training set", _sym, y_train)
        print("Test set: # Features {}, # Samples {}".format(X_test.shape[1], X_test.shape[0]))
        plot_class_distribution("Test set", _sym, y_test)
        if not np.isfinite(X_train).all():
            logger.warning("Training x is not finite!")
        if not np.isfinite(y_train).all():
            logger.warning("Training y is not finite!")
        if not np.isfinite(X_test).all():
            logger.warning("Test x is not finite!")
        if not np.isfinite(y_test).all():
            logger.warning("Test y is not finite!")

        # Build pipeline to be used as estimator in grid search
        #  so that each subset of the data is transformed independently
        #  to avoid contamination between folds.
        pipeline = Pipeline([
            ('i', IterativeImputer()),  # Replace nan's with the median value between previous and next observation
            ('s', MinMaxScaler(feature_range=(-1,1))),
            ('c', MLPClassifier()),
        ])

        # Perform hyperparameter tuning of the ensemble with 5-fold cross validation
        logger.info("Start Grid search")
        CV_rfc = GridSearchCV(
            estimator=pipeline,
            param_grid=PARAM_GRID,
            cv=5,
            n_jobs=4,
            scoring='neg_mean_squared_error',
            verbose=1
        )
        CV_rfc.fit(X_train, y_train)
        logger.info("End Grid search")

        # Take the fitted ensemble with tuned hyperparameters
        clf = CV_rfc.best_estimator_
        # Test ensemble's performance on training and test sets
        logger.info("Classification report on train set")
        predictions1 = clf.predict(X_train)
        train_report = classification_report(y_train, predictions1, output_dict=True)
        print(classification_report(y_train, predictions1))
        logger.info("Classification report on test set")
        predictions2 = clf.predict(X_test)
        test_report = classification_report(y_test, predictions2, output_dict=True)
        print(classification_report(y_test, predictions2))
        stats = {
            'score': accuracy_score(y_train, predictions1),
            'mse': mean_squared_error(y_train, predictions1),
            'test_score': accuracy_score(y_test, predictions2),
            'test_mse': mean_squared_error(y_test, predictions2),
            'train_report': train_report,
            'test_report': test_report,
        }
        print(CV_rfc.best_params_)
        num_samples = min(y_train.shape[0], y_test.shape[0], 30)
        print("Gains calculated on {} samples only!".format(num_samples))
        print("Train Accuracy: {}\nTrain MSE: {}\nGains on train preds: 100 -> {}".format(
            accuracy_score(y_train, predictions1),
            mean_squared_error(y_train, predictions1),
            test_gains(train_features['close'][0:num_samples], predictions1[0:num_samples], initial_balance=100, position_size=0.1)
        ))
        print("Test Accuracy: {}\nTest MSE: {}\nGains on test preds: 100 -> {}".format(
            accuracy_score(y_test, predictions2),
            mean_squared_error(y_test, predictions2),
            test_gains(test_features['close'][0:num_samples], predictions2[0:num_samples], initial_balance=100, position_size=0.1)
        ))
        print("--- end ---")

if __name__ == '__main__':
    logger.setup(
        filename='../build_model.log',
        filemode='w',
        root_level=logging.DEBUG,
        log_level=logging.DEBUG,
        logger='build_model'
    )
    main()


"""
The Sims 4 Community Library is licensed under the Creative Commons Attribution 4.0 International public license (CC BY 4.0).
https://creativecommons.org/licenses/by/4.0/
https://creativecommons.org/licenses/by/4.0/legalcode

Copyright (c) COLONOLNUTTY
"""
from typing import List, Set, Tuple

from sims4communitylib.modinfo import ModInfo
from sims4communitylib.utils.common_collection_utils import CommonCollectionUtils
from sims4communitylib.testing.common_assertion_utils import CommonAssertionUtils
from sims4communitylib.testing.common_test_service import CommonTestService

# noinspection PyMissingOrEmptyDocstring
@CommonTestService.test_class(ModInfo.get_identity())
class CommonCollectionUtilsTests:
    @staticmethod
    @CommonTestService.test((1, 2, 3), (2,))
    @CommonTestService.test((1, 2, 3), (4,), (2,))
    @CommonTestService.test((1, 2, 3), (4, 7), (5, 6), (3,))
    def _should_intersect_true(list_one, *list_items) -> None:
        result = CommonCollectionUtils.intersects(list_one, *list_items)
        CommonAssertionUtils.is_true(result)

    @staticmethod
    @CommonTestService.test((1, 2, 3), (4, 8))
    @CommonTestService.test((1, 2, 3), (5, 9,), (10, 4))
    def _should_intersect_false(list_one: List[int], *list_items: int) -> None:
        result = CommonCollectionUtils.intersects(list_one, *list_items)
        CommonAssertionUtils.is_false(result)

    @staticmethod
    @CommonTestService.test([1, 2, 3], 2, {(1, 2), (1, 3), (2, 3)})
    def _should_combine(items: List[int], combination_length: int, expected_outcome: Set[Tuple[int]]) -> None:
        result = CommonCollectionUtils.create_possible_combinations(items, combination_length)
        CommonAssertionUtils.are_equal(result, expected_outcome)


import networkx as nx
import numpy as np
from random import randint
from scipy import linalg
from scipy import sparse

class Handler:

    #get exact node list from graph partition
    def indexArray(nodeList,k,lables):
        array = []
        for r in range(0,k):
            array.append([])
            count = 0
            for i in lables:
                count+=1
                graph_count = 0
                for a in nodeList:
                    graph_count+=1
                    if(r==i and count==graph_count):
                        array[r].append(a)
                        break
        return array
    def alphaCut(A):
        #define zero matrix
        degree = np.zeros((A.shape[0],A.shape[0]),dtype=np.float)
        #define ones matrix
        oneMatrix = np.ones((A.shape[0],1),dtype=np.float)
        #get sum of row
        rowsum = A.sum(axis=0)
        #create degree matrix
        for j in range(0, A.shape[0]):
            degree[j,j] = rowsum[j]
        #Get alpha cut matrix
        maltiply = np.matmul(oneMatrix.transpose(), degree)

        numerator = np.matmul(maltiply.transpose(),maltiply)
        deno = np.matmul(maltiply,oneMatrix)
        del maltiply
        del oneMatrix
        del degree
        del rowsum
        denominator = np.linalg.inv(deno)
        value = denominator*numerator
        # value = numerator*denominator
        return value-A
    def conectivityMatrix(partitionArray,G,data):
        matrix = np.zeros((len(partitionArray),len(partitionArray)),dtype=np.float)
        i = 0
        edgecut = 0
        for r in partitionArray:
            for k in range(0,len(partitionArray)):
                if(k>i):
                    value = 0.0
                    count = 0.0
                    for c in r:
                        for d in partitionArray[k]:
                            #value+=float(G.get_edge_data(d,c)['weight'])
                            if (G.get_edge_data(str(d), str(c)) is not None):
                                # value += abs(G.get_edge_data(d, c)[data])
                                if (len(G.get_edge_data(str(d), str(c))) > 1):
                                    count+=1
                                    value += abs(G.get_edge_data(str(d), str(c))[1][data])
                    if(count!=0):
                        edgecut+=value
                        matrix[i][k] = value/count
                        matrix[k][i] = value/count

            i+=1;
        return matrix,edgecut
    def adjecencyMatrixes(partitionArray,G,data):
        matrix = np.zeros((len(partitionArray), len(partitionArray)), dtype=np.float)
        pickMatrix = np.zeros((len(partitionArray), len(partitionArray)), dtype=np.float)
        i = 0
        edgecut = 0
        for r in partitionArray:
            for k in range(0, len(partitionArray)):
                if (k > i):
                    value = 0.0
                    pickValue = 0
                    for c in r:
                        for d in partitionArray[k]:
                            # value+=float(G.get_edge_data(d,c)['weight'])
                            if (G.get_edge_data(str(d), str(c)) is not None):
                                # value += abs(G.get_edge_data(d, c)[data])
                                value += 1
                                if (len(G.get_edge_data(str(d), str(c))) > 1):
                                    value += abs(G.get_edge_data(str(d), str(c))[1][data])
                                    pickValue += abs(G.get_edge_data(str(d), str(c))[1][data])
                    matrix[i][k] = value
                    matrix[k][i] = value
                    pickMatrix[i][k] = pickValue
                    pickMatrix[k][i] = pickValue
                    if(value>0):
                        print(r,value)
            i += 1;
        return matrix ,pickMatrix
    def edgeConectivity(matrix,part,index):
        value = 0
        for q in part:
            for r in q:
                value = matrix[r][index]

        return value

    def degreeMatrix(G,p):
        matrix = np.zeros((len(p), len(p)), dtype=np.float)
        i=0
        for r in p:
            degreeCount = 0
            for c in r:
                degreeCount+=G.degree(c)
            matrix[i][i] = degreeCount
            i+=1
        return matrix

    def partitionSize(partitionArray,indexer):
        max, id = 100000, 0
        for i in range(0, len(partitionArray)):
            if (len(partitionArray[i]) < max)&(i not in indexer):
                max = len(partitionArray[i])
                id = i
        return id

    def interPartitionConectivity(matrix,p1,index):

        value = 0
        for r in p1:
            value += matrix[r][index]
        return value

    def partitionRandom(adjecencyMatrix,indexer,partitionSize):
        value =0
        while(True):
            max = 0
            for z in range(0,int(len(adjecencyMatrix)/partitionSize)):
                value = randint(0,len(adjecencyMatrix)-1)
                if (value not in indexer)&(adjecencyMatrix[max][max]<adjecencyMatrix[value][value]):
                    max = value
            if max not in indexer:
                value = max
                break

        return value
    def partitionRandomNew(adjecencyMatrix,indexer,partitionSize):
        value =0

        for z in range(0,int(len(adjecencyMatrix)/partitionSize)):
            value = randint(0,len(adjecencyMatrix)-1)
            if value not in indexer:
                break

        return value

    def adjecencyMatrix(partitionArray,G,data):
        matrix = np.zeros((len(partitionArray),len(partitionArray)),dtype=np.float)
        pickMatrix = np.zeros((len(partitionArray),len(partitionArray)),dtype=np.float)
        i = 0
        edgecut = 0
        for r in partitionArray:
            for k in range(0,len(partitionArray)):
                if(k>i):
                    value = 0.0
                    pickValue = 0
                    for c in r:
                        for d in partitionArray[k]:
                            #value+=float(G.get_edge_data(d,c)['weight'])
                            if (G.get_edge_data(str(d), str(c)) is not None):
                                # value += abs(G.get_edge_data(d, c)[data])
                                value+=1
                                if (len(G.get_edge_data(str(d), str(c))) > 1):
                                    value += abs(G.get_edge_data(str(d), str(c))[1][data])
                                    pickValue+=abs(G.get_edge_data(str(d), str(c))[1][data])
                    matrix[i][k] = value
                    matrix[k][i] = value
                    pickMatrix[i][k]=pickValue
                    pickMatrix[k][i] = pickValue
                elif(i==k):
                    value = 0
                    pickValue = 0
                    for x in range(0,len(r)):
                        for y in range(x,len(r)):
                            if (G.get_edge_data(str(r[x]), str(r[y])) is not None):
                                value+=1
                                if (len(G.get_edge_data(str(r[x]), str(r[y]))) > 1):
                                    value += abs(G.get_edge_data(str(r[x]), str(r[y]))[1][data])
                                    pickValue += abs(G.get_edge_data(str(r[x]), str(r[y]))[1][data])
                    matrix[i][k] = value
                    pickMatrix[i][k] = pickValue

            i+=1;
        return matrix , pickMatrix

    def partCon(G,p1,p2,data):

        value = 0
        for r in p1:
            for d in p2:
                # value+=float(G.get_edge_data(d,c)['weight'])

                if (G.get_edge_data(str(r), str(d)) is not None):
                    # value += abs(G.get_edge_data(d, c)[data])
                    if (len(G.get_edge_data(str(r), str(d))) > 1):
                        value += abs(G.get_edge_data(str(r), str(d))[1][data])
        return value


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:    xurongzhong#126.com wechat:pythontesting qq:37391319
# CreateDate: 2018-1-19 pandas_concat_rows_from_multiple_files.py
import pandas as pd
import glob
import os

input_path = os.path.dirname(__file__)           # current file path
output_file = r"output_files\12output.csv"

all_files = glob.glob(os.path.join(input_path, 'sales_*'))    #
all_data_frames = []
for file in all_files:
    print(file.replace("\\", "/"))
    data_frame = pd.read_csv(file.replace("\\", "/"), index_col=None)
    all_data_frames.append(data_frame)
data_frame_concat = pd.concat(all_data_frames, axis=0, ignore_index=True)
data_frame_concat.to_csv(output_file, index=False)


import numpy as np

import mlalgorithms.checks as checks

from . import model

class MostPopular(model.IModel):

    def __init__(self, num_popular_ids=5):
        super().__init__()
        self.num_popular_ids = num_popular_ids
        self.most_popular_goods = dict()

    def train(self, train_samples, train_labels, **kwargs):
        checks.check_equality(len(train_samples), len(train_labels),
                              message="Samples and labels have different "
                                      "sizes")
        self.most_popular_goods = kwargs["most_popular_goods"]

    def predict(self, samples, **kwargs):
        predictions = []
        for _ in samples:
            prediction = np.array(self.most_popular_goods)
            predictions.append(prediction)
        return predictions

class SameAsBefore(model.IModel):

    def __init__(self, num_popular_ids=5):
        super().__init__()
        self.num_popular_ids = num_popular_ids
        self.latest_orders = dict()
        self.most_popular_goods = dict()

    def train(self, train_samples, train_labels, **kwargs):
        checks.check_equality(len(train_samples), len(train_labels),
                              message="Samples and labels have different "
                                      "sizes")

        self.most_popular_goods = kwargs["most_popular_goods"]

        # Get person ids from train samples, samples format:
        # [[person_id, month, day], [person_id, month, day], ...].
        persons_ids = [person_data[0] for person_data in train_samples]
        self.latest_orders = dict(zip(persons_ids, train_labels))

    def predict(self, samples, **kwargs):
        predictions = []
        for sample in samples:
            if self.latest_orders.get(sample[0]) is None:
                prediction = np.array(self.most_popular_goods)
            else:
                prediction = np.array(self.latest_orders[sample[0]])
            predictions.append(prediction)
        return predictions

class MostPopularFromOwnOrders(model.IModel):

    def __init__(self, num_popular_ids):
        super().__init__()
        self.num_popular_ids = num_popular_ids

        checks.check_types(self.num_popular_ids, int,
                           var_name="num_popular_ids")
        checks.check_value(self.num_popular_ids, 0, 100, True, False,
                           var_name="num_popular_ids")

        self.orders = dict()
        self.most_popular_goods = dict()
        self.most_popular_good_ids = list()
        self.max_good_id = 0

    def process_orders(self):
        """
        Find most popular goods, then set popular good identifiers equal to
        one but other good identifiers equal to zero.
        """
        for person_orders in self.orders.values():
            non_zero_count = np.count_nonzero(person_orders)

            if non_zero_count < self.num_popular_ids:
                non_zero_ind = person_orders.argsort()[::-1][:non_zero_count]
                sub_index = []

                for index in self.most_popular_good_ids:
                    if index not in non_zero_ind:
                        sub_index.append(index)
                        non_zero_count += 1
                        if non_zero_count == self.max_good_id:
                            break

                if non_zero_count < self.num_popular_ids:
                    sub_index.extend(
                        np.random.randint(self.max_good_id + 1,
                                          size=(self.num_popular_ids -
                                                non_zero_count)).tolist()
                    )
                person_orders[sub_index] = 1
            else:
                indices = person_orders.argsort()[::-1][:self.num_popular_ids]
                not_in_indices = [x for x in range(len(person_orders))
                                  if x not in indices]
                person_orders[not_in_indices] = 0
                person_orders[indices] = 1

    def train(self, train_samples, train_labels, **kwargs):
        checks.check_equality(len(train_samples), len(train_labels),
                              message="Samples and labels have different "
                                      "sizes")

        self.most_popular_goods = kwargs["most_popular_goods"]
        self.most_popular_good_ids = kwargs["most_popular_good_ids"]

        checks.check_value(len(self.most_popular_good_ids),
                           lower=self.num_popular_ids, strict_less=False,
                           var_name="most_popular_good_ids")

        self.max_good_id = kwargs["max_good_id"]

        # Get person ids from train samples, samples format:
        # [[person_id, month, day], [person_id, month, day], ...].
        persons_ids = [person_data[0] for person_data in train_samples]
        for persons_id, label in zip(persons_ids, train_labels):
            if self.orders.get(persons_id) is None:
                self.orders[persons_id] = np.array(label)
            else:
                self.orders[persons_id] += np.array(label)

        self.process_orders()

    def predict(self, samples, **kwargs):
        predictions = []
        for sample in samples:
            if self.orders.get(sample[0]) is None:
                prediction = np.array(self.most_popular_goods)
            else:
                prediction = self.orders[sample[0]]
            predictions.append(prediction)
        return predictions


# -*- coding: utf-8 -*-
from cmsauth.models import CmsUser

def CmsContextProcessor(request):
    user = request.user
    # 给user添加一个avatar属性
    # avatar属性存储在CmsUser里面
    # 相当于把CmsUser的avatar属性添加到user当中

    # 如果user没有avatar属性,就需要添加\
    if not hasattr(user, 'avatar'):
        cmsuser = CmsUser.objects.filter(user__pk=user.pk).first()
        if cmsuser:
            setattr(user, 'avatar', cmsuser.avatar)

    return {'user': user}


import wpilib

import subsystems
import oi
#from commands.autonomous import AutonomousProgram

class Robot(wpilib.IterativeRobot):

    def robotInit(self):
        self.drivetrain = subsytems.Drivetrain()
        self.oi = OI()
        self.autonomousCommand = commands.Auto()

    def autonomousInit(self):
        try:
            self.autonomousProgram.start()
        except NameError:
            print("Autonomous command not set")

    def teleopInit(self):
        try:
            self.autonomousProgram.cancel()
        except NameError:
            print("Autonomous command not set")

if __name__ == '__main__':
    wpilib.run(Robot)


# Copyright (c) 2017-2019 Carnegie Mellon University. All rights reserved.
# Use of this source code is governed by BSD 3-clause license.

import numpy as np

def frame(data, window_length, hop_length):
    num_samples = data.shape[0]
    num_frames = 1 + int(np.floor((num_samples - window_length) / hop_length))
    shape = (num_frames, window_length) + data.shape[1:]
    strides = (data.strides[0] * hop_length,) + data.strides
    return np.lib.stride_tricks.as_strided(data, shape=shape, strides=strides)

def periodic_hann(window_length):
    return 0.5 - (0.5 * np.cos(2 * np.pi / window_length * np.arange(window_length)))

def stft_magnitude(signal, fft_length,
                   hop_length=None,
                   window_length=None):

    frames = frame(signal, window_length, hop_length)
    # Apply frame window to each frame. We use a periodic Hann (cosine of period
    # window_length) instead of the symmetric Hann of np.hanning (period
    # window_length-1).
    window = periodic_hann(window_length)
    windowed_frames = frames * window
    return np.abs(np.fft.rfft(windowed_frames, int(fft_length)))

# Mel spectrum constants and functions.
_MEL_BREAK_FREQUENCY_HERTZ = 700.0
_MEL_HIGH_FREQUENCY_Q = 1127.0

def hertz_to_mel(frequencies_hertz):
    return _MEL_HIGH_FREQUENCY_Q * np.log(
        1.0 + (frequencies_hertz / _MEL_BREAK_FREQUENCY_HERTZ))

def spectrogram_to_mel_matrix(num_mel_bins=20,
                              num_spectrogram_bins=129,
                              audio_sample_rate=8000,
                              lower_edge_hertz=125.0,
                              upper_edge_hertz=3800.0):

    nyquist_hertz = audio_sample_rate / 2.
    if lower_edge_hertz >= upper_edge_hertz:
        raise ValueError("lower_edge_hertz %.1f >= upper_edge_hertz %.1f" %
                         (lower_edge_hertz, upper_edge_hertz))
    spectrogram_bins_hertz = np.linspace(
        0.0, nyquist_hertz, num_spectrogram_bins)
    spectrogram_bins_mel = hertz_to_mel(spectrogram_bins_hertz)
    # The i'th mel band (starting from i=1) has center frequency
    # band_edges_mel[i], lower edge band_edges_mel[i-1], and higher edge
    # band_edges_mel[i+1].  Thus, we need num_mel_bins + 2 values in
    # the band_edges_mel arrays.
    band_edges_mel = np.linspace(hertz_to_mel(lower_edge_hertz),
                                 hertz_to_mel(upper_edge_hertz), num_mel_bins + 2)
    # Matrix to post-multiply feature arrays whose rows are num_spectrogram_bins
    # of spectrogram values.
    mel_weights_matrix = np.empty((num_spectrogram_bins, num_mel_bins))
    for i in range(num_mel_bins):
        lower_edge_mel, center_mel, upper_edge_mel = band_edges_mel[i:i + 3]
        # Calculate lower and upper slopes for every spectrogram bin.
        # Line segments are linear in the *mel* domain, not hertz.
        lower_slope = ((spectrogram_bins_mel - lower_edge_mel) /
                       (center_mel - lower_edge_mel))
        upper_slope = ((upper_edge_mel - spectrogram_bins_mel) /
                       (upper_edge_mel - center_mel))
        # .. then intersect them with each other and zero.
        mel_weights_matrix[:, i] = np.maximum(0.0, np.minimum(lower_slope,
                                                              upper_slope))
    # HTK excludes the spectrogram DC bin; make sure it always gets a zero
    # coefficient.
    mel_weights_matrix[0, :] = 0.0
    return mel_weights_matrix

def log_mel_spectrogram(data,
                        audio_sample_rate=8000,
                        log_offset=0.0,
                        window_length_secs=0.025,
                        hop_length_secs=0.010,
                        **kwargs):

    window_length_samples = int(round(audio_sample_rate * window_length_secs))
    hop_length_samples = int(round(audio_sample_rate * hop_length_secs))
    fft_length = 2 ** int(np.ceil(np.log(window_length_samples) / np.log(2.0)))

    spectrogram = stft_magnitude(
        data,
        fft_length=fft_length,
        hop_length=hop_length_samples,
        window_length=window_length_samples)

    #print("fft_length", fft_length, "hop_length", hop_length_samples, "window_length", window_length_samples)
    #print("spectrogram_length", spectrogram.shape)

    mel_spectrogram = np.dot(spectrogram, spectrogram_to_mel_matrix(
        num_spectrogram_bins=spectrogram.shape[1],
        audio_sample_rate=audio_sample_rate, **kwargs))
    return np.log(mel_spectrogram + log_offset)

def log_mel_spectrogram_subtract_bg(data,
                                    background=None,
                                    audio_sample_rate=8000,
                                    log_offset=0.0,
                                    window_length_secs=0.025,
                                    hop_length_secs=0.010,
                                    **kwargs):

    window_length_samples = int(round(audio_sample_rate * window_length_secs))
    hop_length_samples = int(round(audio_sample_rate * hop_length_secs))
    fft_length = 2 ** int(np.ceil(np.log(window_length_samples) / np.log(2.0)))

    spectrogram = stft_magnitude(
        data,
        fft_length=fft_length,
        hop_length=hop_length_samples,
        window_length=window_length_samples)

    if (background is not None):
        spectrogram = spectrogram - background
        zeros = np.where(spectrogram == 0)
        negatives = np.where(spectrogram < 0)
        spectrogram[zeros] = 0.001
        spectrogram[negatives] = 0.001

    mel_spectrogram = np.dot(spectrogram, spectrogram_to_mel_matrix(
        num_spectrogram_bins=spectrogram.shape[1],
        audio_sample_rate=audio_sample_rate, **kwargs))
    return np.log(mel_spectrogram + log_offset)


import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
import pandas as pd
f, ax = plt.subplots(1, 2, figsize=(6, 3))
for ii, typ in enumerate(['go', 'OUU']):
  dat = pd.read_csv('./%sHist.dat' % typ, sep=r'\s+')
  print(dat.columns)
  ax[ii].plot(dat.obj_fn / -1e6, c='k', lw=3, zorder=0)
  if typ == 'go': ax[ii].set_ylabel('Deterministic Power Production (MW)')
  else: ax[ii].set_ylabel('Expecteded Power Production (MW)')
  dat.drop(dat.columns[:2], inplace=True, axis=1)
  ax2 = np.rad2deg(dat).drop('obj_fn', axis=1).plot(ax=ax[ii], legend=None, secondary_y=True)
#ax2 = ax.twinx()
  if ii > 0: ax2.set_ylabel("Turbine Yaw Position ($^\circ$)")
  ax[ii].set_xlabel('Optimization Iteration')

legend_elements = [
                   Line2D([0], [0], marker='o', color='k', lw=3, label='Power',
                          markerfacecolor=None, markeredgecolor='k'),
                   Line2D([0], [0], color='g', label='Yaw Position',
                          markerfacecolor=None, markeredgecolor='k')]

#ax2.legend(handles=legend_elements, loc='lower center', bbox_to_anchor=[.5, 1.])
#plt.tight_layout()
plt.subplots_adjust(wspace=.4)
plt.savefig('hists')
#plt.savefig('hists', bbox_inches='tight')


import copy
import time
import torch
from tqdm import tqdm
from sklearn.metrics import f1_score

def train_model(model,
    dataloaders,
    criterion,
    optimizer,
    scheduler,
    device,
    num_epochs=25):

    since = time.time()
    total = len(dataloaders['train'])

    val_acc_history = []
    f1_histroy = []

    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0

    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        with tqdm(total) as t:

            for phase in ['train', 'val']:
                if phase == 'train':
                    scheduler.step()
                    model.train()
                else:
                    model.eval()

                running_loss = 0.0
                running_corrects = 0

                for inputs, labels in dataloaders[phase]:

                    inputs = inputs.to(device)
                    labels = labels.to(device)

                    optimizer.zero_grad()

                    with torch.set_grad_enabled(phase == 'train'):

                        outputs  = model(inputs)
                        _, preds = torch.max(outputs, 0)

                        loss = criterion(outputs, labels)

                        if phase == 'train':
                            loss.backward()
                            optimizer.step()

                    #r = (predicts == labels.byte())
                    #acc = r.float().sum().data[0]
                    #print(acc)
                    running_loss += loss.item() * inputs.size(0)
                    running_corrects += torch.sum(preds == labels.data)

                epoch_loss = running_loss / len(dataloaders[phase].dataset)
                epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)

                print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

                if phase == 'val' and epoch_acc > best_acc:
                    best_acc = epoch_acc
                    best_model_wts = copy.deepcopy(model.state_dict())

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
    print('Best val Acc: {:4f}'.format(best_acc))

    # load best model weights
    model.load_state_dict(best_model_wts)
    return model


# Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

from jstest.common import utils
from jstest.testrunner import utils as testrunner_utils
from jstest.testrunner.devices.device_base import RemoteDevice
from jstest.testrunner.devices.connections.serialcom import SerialConnection

class STM32F4Device(RemoteDevice):
    '''
    Device of the STM32F4-Discovery target.
    '''
    def __init__(self, env):
        self.os = 'nuttx'
        self.stlink = env['modules']['stlink']

        RemoteDevice.__init__(self, env)

        data = {
            'dev-id': env['info']['device_id'],
            'baud': env['info']['baud'],
            'timeout': env['info']['timeout'],
            'prompt': 'nsh> '
        }

        self.channel = SerialConnection(data)

    def initialize(self):
        '''
        Flash the device.
        '''
        if self.env['info']['no_flash']:
            return

        stlink = self.env['modules']['stlink']
        nuttx = self.env['modules']['nuttx']

        flash_flags = ['write', nuttx['paths']['image'], '0x8000000']

        utils.execute(stlink['src'], 'build/Release/st-flash', flash_flags)

    def reset(self):
        '''
        Reset the device to create clean environment.
        '''
        flasher = self.stlink['paths']['st-flash']

        utils.execute('.', flasher, ['reset'], quiet=True)
        # Wait a moment to boot the device.
        time.sleep(5)

    def execute(self, testset, test):
        '''
        Execute the given test.
        '''
        self.reset()
        self.login()

        # Absolute path to the test file on the device.
        testfile = '/test/%s/%s' % (testset, test['name'])

        args = []
        if not self.env['info']['no_memstat']:
            args = ['--mem-stats']

        command = {
            'iotjs': 'iotjs %s %s' % (' '.join(args), testfile),
            'jerryscript': 'jerry %s %s' % (testfile, ' '.join(args))
        }

        # Run the test on the device.
        output = self.channel.exec_command(command[self.app])

        stdout, memstat, _ = testrunner_utils.process_output(output)
        # Process the exitcode of the last command.
        exitcode = self.channel.exec_command('echo $?')

        self.logout()

        return {
            'output': stdout.rstrip('\n').replace('\n', '<br>'),
            'memstat': memstat,
            'exitcode': exitcode
        }


from Func_Web_Scrape_NBA import get_perGame_2018, get_perGame_header
from Func_redefine_pos import multiclass_visulization_PCA, multiclass_visulization_LDA, multiclass_visulization_Kmeans
import numpy as np
import csv
import matplotlib.pyplot as plt

if __name__ == '__main__':
    # Part A. Load data
    # ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== =====
    X = []
    y = []
    player_names = []
    i = 0
    with open('data_2018.csv', 'r') as file:
        reader = csv.reader(file)
        for line in reader:
            if i == 0:
                i += 1
            else:
                data = []
                for i in range(2, 26):
                    data.append(float(line[i]))
                X.append(data)
                y.append(float(line[26]))
                player_names.append(line[0])
    X = np.array(X)
    y = np.array(y)

    # Part B. Machine Learning
    # ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== ===== =====
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import confusion_matrix
    n_samples, n_features = X.shape
    n_labels = 5

    # 1. Dimensionality Reduction
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    from sklearn.preprocessing import StandardScaler
    X_norm = StandardScaler().fit_transform(X)

    # <1> PCA
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    from sklearn.decomposition import PCA
    pca = PCA(n_components=2)
    X_pca = pca.fit_transform(X_norm)
    multiclass_visulization_PCA(X_pca, y)

    # <2> LDA
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
    lda = LinearDiscriminantAnalysis(n_components=2)
    X_lda = lda.fit_transform(X, y)
    multiclass_visulization_LDA(X_lda, y, player_names)
    '''
    # 2. Classification
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    # <1> Decision Tree
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    from sklearn.tree import DecisionTreeClassifier
    # LDA
    X_train, X_test, y_train, y_test = train_test_split(X_lda, y, random_state = 0)

    clf_dtree = DecisionTreeClassifier(max_depth = 2).fit(X_train, y_train)
    pred_dtree = clf_dtree.predict(X_test)
    score_dtree_lda = clf_dtree.score(X=X_test, y=y_test)

    # PCA
    X_train, X_test, y_train, y_test = train_test_split(X_pca, y, random_state = 0)

    clf_dtree_pca = DecisionTreeClassifier(max_depth = 2).fit(X_train, y_train)
    pred_dtree = clf_dtree_pca.predict(X_test)
    score_dtree_pca = clf_dtree_pca.score(X=X_test, y=y_test)
    print("score_dtree: ", score_dtree_lda)

    # <2> SVM
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    from sklearn.svm import SVC
    X_train, X_test, y_train, y_test = train_test_split(X_lda, y, random_state = 0)

    clf_svm_linear = SVC(kernel = 'linear', C = 1).fit(X_train, y_train)
    pred_svm = clf_svm_linear.predict(X_test)

    # model accuracy for X_test
    score_svm_lda = clf_svm_linear.score(X_test, y_test)
    print("score_svm: ", score_svm_lda)

    # <3>. KNN
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    from sklearn.neighbors import KNeighborsClassifier
    X_train, X_test, y_train, y_test = train_test_split(X_lda, y, random_state = 0)

    knn = KNeighborsClassifier(n_neighbors = 7).fit(X_train, y_train)
    # accuracy on X_test
    score_knn_lda = knn.score(X_test, y_test)
    print("score_knn: ", score_knn_lda)

    '''
    # 2. Clustering
    # ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
    # K means
    from sklearn.cluster import KMeans
    n_clusters = 8
    kmeans = KMeans(n_clusters, random_state=0).fit(X_lda)
    y_labels = kmeans.labels_

    for i in range(n_clusters):
        X_tmp = X_lda[y_labels == i]
        player_indexes = [idx for idx, label in enumerate(y_labels) if label == i]
        plt.figure(figsize=(10, 10))
        plt.scatter(X_tmp[:, 0], X_tmp[:, 1], c = 'r', s = 20)
        # Annotate samples
        for idx in player_indexes[0:10]:
            name = player_names[idx]
            plt.annotate(name, (X_lda[idx, 0], X_lda[idx, 1]))
        plt.show()

    multiclass_visulization_Kmeans(X_lda, y_labels)


from os.path import abspath, dirname
import pytest
import os

from firedrake import *
cwd = abspath(dirname(__file__))

exp1="x[0]*(1-x[0])"
exp2="sin(2*pi *(x[0]-x[1]))"
exp3="(x[0]*x[0])+(x[1]*x[1])"
exp4="1- (((0.5-x[0])*(0.5-x[0]))+((0.5-x[1])*(0.5-x[1])))"
imgpath='diderot/tmp/'

def quantize(namenrrd,namepng):
    os.system('unu quantize -b 8 -i ' +namenrrd+ ' -o '+ namepng)
    os.system('open ' + namepng)

# ex0|ex1 use lerp
# ex0 calls quantize
# ex1 diderot creates png file

def test_tmp0():
    mesh = UnitSquareMesh(2, 2)
    V = FunctionSpace(mesh, "P", 4)
    f = Function(V).interpolate(Expression(exp1))
    vis_diderot.tmp(f)

def test_ex0():
    mesh = UnitSquareMesh(2, 2)
    V = FunctionSpace(mesh, "P", 4)
    f = Function(V).interpolate(Expression(exp1))
    namenrrd=imgpath+'ex0.nrrd'
    namepng=imgpath+'ex0.png'
    res=200
    lower_range=0
    upper_range=1
    type=0  # creates nrrd file
    vis_diderot.basic_d2s_lerp(namenrrd,f, res,res, lower_range, upper_range, lower_range, upper_range,type)
    quantize(namenrrd,namepng)

# c program calls quantize
def atest_ex1():
    mesh = UnitSquareMesh(2, 2)
    V = FunctionSpace(mesh, "P", 4)
    f = Function(V).interpolate(Expression(exp2))
    namepng=imgpath+'ex1.png'
    res=200
    lower_range=0
    upper_range=1
    type=1  # creates png file file
    vis_diderot.basic_d2s_lerp(namepng,f, res,res, lower_range, upper_range, lower_range, upper_range,type)
    os.system('open ' + namepng)

if __name__ == '__main__':
    import os
    pytest.main(os.path.abspath(__file__))


import csv
from park_at_dcu.models import Facility

with open('../data/A3/Facility.csv') as f:
   reader = csv.reader(f)
   i = 0
   for row in reader:
      facility = Facility(i,row[0],int(row[1]))
      facility.save()
      i = i+1


import numpy as np
import pandas as pd
import pandas_datareader as web
from datetime import date
from dateutil.relativedelta import relativedelta
import scipy.optimize as optimize
import plotly
import plotly.graph_objects as go
import json

class EF:

    def __init__(self, duration, pl_data, pl_tickers, pl_weights, num_ports):
        self.pl_data = pl_data
        self.tickers = pl_tickers
        self.weights = pl_weights
        self.end = date.today()
        self.start = self.end - relativedelta(years=duration)
        self.num_ports = num_ports
        self.num_portfolios = 100
        self.risk_free_rate = 0.0152

    def get_data(self):
        data = {}
        for ticker in self.tickers:
            data[ticker] = web.DataReader(ticker, 'yahoo',
                                          self.start.strftime('%Y-%m-%d'),
                                          self.end.strftime('%Y-%m-%d'))[
                'Adj Close']
        df = pd.DataFrame.from_dict(data)
        return df

    def get_return(self):
        df = self.get_data()
        returns = df.pct_change()
        mean_returns = returns.mean()
        cov_matrix = returns.cov()

        return returns, mean_returns, cov_matrix

    def my_current_portfolio(self):
        df = self.get_data()
        # Re-balance - normalization
        weights = np.array(self.weights / np.sum(self.weights))

        ## Expected Return
        exp_ret = np.sum(df.pct_change().mean() * weights) * 252

        ## Expected Volatility
        exp_vol = np.sqrt(
            np.dot(weights.T, np.dot(df.pct_change().cov() * 252, weights)))

        ## Sharpe Ratio
        SR = exp_ret / exp_vol

        output = ["Current Portfolio", round(exp_ret * 100, 2),
                  round(exp_vol * 100, 2), round(SR, 4)]
        return output

    def generate_ef_viz(self):
        my_pl_output = self.my_current_portfolio()
        df = self.get_data()
        returns, mean_returns, cov_matrix = self.get_return()
        annual_volatility, annual_return, expected_volatility, expected_return, \
        min_expected_volatility, min_expected_return, max_sharpe, min_vol, \
        frontier_vol, frontier_y, ef_df, \
        max_sharpe_allocation, min_vol_allocation = self.display_ef(
            mean_returns,
            cov_matrix,
            self.risk_free_rate,
            self.num_portfolios,
            returns, df,
            self.tickers)

        tbl_contents = \
            [list(e) for e in zip(*[my_pl_output,
                                    [
                                        "Maximum Sharpe Ratio Portfolio",
                                        round(expected_return * 100,
                                              2),
                                        round(
                                            expected_volatility * 100,
                                            2),
                                        round(
                                            expected_return / expected_volatility,
                                            4)],
                                    ["Minimum Volatility Portfolio",
                                     round(
                                         min_expected_return * 100,
                                         2),
                                     round(
                                         min_expected_volatility * 100,
                                         2),
                                     round(
                                         min_expected_return / min_expected_volatility,
                                         4)],
                                    ])]

        # PLOT 1 PORTFOLIO COMPARISON =========================================
        tbl_fig = go.Figure()
        tbl_fig.add_trace(go.Table(
            header=dict(
                values=["Portfolio", "Expected Return", "Expected Volatility",
                        "Sharpe Ratio"],
                font=dict(size=15),
                align="left"
            ),
            cells=dict(
                values=tbl_contents,
                align="left")
        ))
        tbl_fig.update_layout(
            title={'text': "Portfolio Comparison"},
            titlefont=dict(size=36),
            height=250
        )

        TBLgraphJSON = json.dumps(tbl_fig, cls=plotly.utils.PlotlyJSONEncoder)

        # PLOT 2 ALLOCATION TABLE ===========================================
        weights_contents = \
            [list(e) for e in zip(*[
                ["Maximum Sharpe Ratio Portfolio"] + max_sharpe.x.tolist(),
                ["Minimum Volatility Portfolio"] + min_vol.x.tolist()
                                    ])]
        alloc_fig = go.Figure()
        alloc_fig.add_trace(go.Table(
            header=dict(
                values=['Portfolio'] + max_sharpe_allocation.columns.to_list(),
                font=dict(size=15),
                align="left"
            ),
            cells=dict(
                values=weights_contents,
                align="left")
        ))
        alloc_fig.update_layout(
            title={'text': "Allocation Weights"},
            titlefont=dict(size=36),
            height=250
        )

        ALLOCgraphJSON = json.dumps(alloc_fig, cls=plotly.utils.PlotlyJSONEncoder)

        # PLOT 3 EF PLOT ======================================================
        vol_arr, ret_arr, sharpe_arr = self.run_simulation(df)
        ef_fig = go.Figure()

        # Mark the tickers position
        ef_fig.add_trace(go.Scatter(
            x=annual_volatility * 100,
            y=annual_return * 100,
            mode="markers",
            marker=dict(
                size=16,
                symbol='diamond',
                color='orange'
            ),
            name="Ticker",
            text=self.tickers,
            # TODO check if this worked https://plotly.com/python/text-and-annotations/
            # textposition="bottom center",
            hovertemplate=
            'Ticker: %{text}' +
            '
Expected Return: %{y:.2f}%
' +
            'Risk: %{x:.2f}%'
        ))

        # Simulation
        ef_fig.add_trace(go.Scatter(
            x=vol_arr * 100,
            y=ret_arr * 100,
            mode="markers",
            marker=dict(
                color=sharpe_arr,
            ),
            name="EF Simulation",
            text=sharpe_arr,
            hovertemplate=
            'Simulated Allocation' +
            '
Sharpe Ratio: %{text:.2f}' +
            '
Expected Return: %{y:.2f}%
' +
            'Risk: %{x:.2f}%'

        ))

        # Frontier line
        ef_fig.add_trace(go.Scatter(
            x=ef_df['Standard Deviation'] * 100,
            y=ef_df['Expected Return'] * 100,
            mode='lines+markers',
            line=dict(color="black"),
            name="EF Line",
            text=ef_df['Sharpe Ratio'],
            hovertemplate=
            'Efficient Frontier Line' +
            '
Sharpe Ratio: %{text:.2f}' +
            '
Expected Return: %{y:.2f}%
' +
            'Risk: %{x:.2f}%'
        ))
        # Maximum Sharpe Ratio Portfolio Allocation
        ef_fig.add_trace(go.Scatter(
            x=[expected_volatility * 100],
            y=[expected_return * 100],
            mode="markers",
            name="Max. SR. ALLOC.",
            marker=dict(
                size=16,
                symbol='star',
                color='green'
            ),
            text=[expected_return / expected_volatility],
            hovertemplate=
            'Maximum Sharpe Ratio Portfolio ALLOC' +
            '
Sharpe Ratio: %{text:.2f}' +
            '
Expected Return: %{y:.2f}%
' +
            'Risk: %{x:.2f}%'
        ))

        # Minimum Volatility Portfolio Allocation
        ef_fig.add_trace(go.Scatter(
            x=[min_expected_volatility * 100],
            y=[min_expected_return * 100],
            mode="markers",
            name="Min. Risk ALLOC.",
            marker=dict(
                size=16,
                symbol='x-dot',
                color='blue'
            ),
            text=[min_expected_volatility / min_expected_return],
            hovertemplate=
            'Minimum Volatility Portfolio ALLOC' +
            '
Sharpe Ratio: %{text:.2f}' +
            '
Expected Return: %{y:.2f}%
' +
            'Risk: %{x:.2f}%'
        ))
        tbl_fig.update_layout(
            title="Portfolio Comparison",

        )
        ef_fig.update_layout(
            xaxis_title="Standard Deviation",
            yaxis_title="Expected Return",
            title="Efficient Frontier",
            titlefont=dict(size=36),
            # legend_title='Tickers',
            showlegend=True,
            height=700,

            xaxis=dict(
                # type='linear',
                ticksuffix='%'),
            yaxis=dict(
                # type='linear',
                # range=[1, 100],
                ticksuffix='%'),
            # hovermode="x"
        )

        EFgraphJSON = json.dumps(ef_fig, cls=plotly.utils.PlotlyJSONEncoder)

        # PLOT 4 Random Allocation Simulation =================================
        traces = {}

        for col in self.tickers:
            traces[col] = go.Scatter(x=ef_df['Standard Deviation'] * 100,
                                     y=ef_df[col] * 100,
                                     name=col,
                                     # text=[round(c,2) for c in col],
                                     # mode='lines',
                                     # line=dict(width=0.5),
                                     stackgroup='one'
                                     )
        sim_data = list(traces.values())
        sim_fig = go.Figure(sim_data)
        sim_fig.update_traces(
            mode="lines",
            # hovertemplate=None
            hovertemplate=
            'Allocation: %{y:.2f}%'
            # 'Risk: %{x:.2f}%'
            # hovertemplate="%{y}"
        )
        sim_fig.update_layout(
            xaxis_title="Standard Deviation",
            yaxis_title="Allocation",
            title="Random Allocation Simulation",
            titlefont=dict(size=36),
            legend_title='Tickers',
            showlegend=True,
            xaxis=dict(
                type='linear',
                ticksuffix='%'),
            yaxis=dict(
                type='linear',
                range=[1, 100],
                ticksuffix='%'),
            hovermode="x")
        SIMgraphJSON = json.dumps(sim_fig, cls=plotly.utils.PlotlyJSONEncoder)

        return TBLgraphJSON, EFgraphJSON, SIMgraphJSON, ALLOCgraphJSON

    # bring EF.py

    def portfolio_annualised_performance(self, weights, mean_returns,
                                         cov_matrix):
        """
        :param weights:
        :param mean_returns:
        :param cov_matrix:
        :return: expected volatility, expected return
        """
        # Expected Return
        returns = np.sum(mean_returns * weights) * 252
        # Expected Volatility
        # Denominator of the sharpe ratio
        # Use Linear Algebra here
        # transpose the weights
        # dot product of log_returns covariance multiplied by 252 with weights
        # sqrt of whole thing gives the expected volatility
        std = np.sqrt(
            np.dot(weights.T, np.dot(cov_matrix, weights))) * np.sqrt(
            252)
        return std, returns

    def random_portfolios(self, num_portfolios, mean_returns, cov_matrix,
                          risk_free_rate, tickers):
        # results for vol, return, sharpe ratio
        results = np.zeros((3, num_portfolios))
        # allocation ratio
        weights_record = []
        for i in range(num_portfolios):
            # create random weights for number of tickers
            weights = np.random.random(len(tickers))
            # rebalance - normalization
            weights = weights / np.sum(weights)
            weights_record.append(weights)
            portfolio_std_dev, portfolio_return = self.portfolio_annualised_performance(
                weights, mean_returns, cov_matrix)
            # Standard Deviation
            results[0, i] = portfolio_std_dev
            # Expected Return
            results[1, i] = portfolio_return
            # Sharpe Ratio
            results[2, i] = (
                                    portfolio_return - risk_free_rate) / portfolio_std_dev

        weights_df = pd.DataFrame.from_records(weights_record, columns=tickers)
        result_df = pd.DataFrame.from_records(results.T,
                                              columns=['Standard Deviation',
                                                       'Expected Return',
                                                       'Sharpe Ratio'])
        portfolio_allocation_df = pd.concat([weights_df, result_df],
                                            axis=1).sort_values(
            by=['Expected Return'], ascending=False)
        return portfolio_allocation_df

    def neg_sharpe_ratio(self, weights, mean_returns, cov_matrix,
                         risk_free_rate):
        p_var, p_ret = self.portfolio_annualised_performance(weights,
                                                             mean_returns,
                                                             cov_matrix)
        return -(p_ret - risk_free_rate) / p_var

    def max_sharpe_ratio(self, mean_returns, cov_matrix, risk_free_rate):
        num_assets = len(mean_returns)
        args = (mean_returns, cov_matrix, risk_free_rate)
        constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
        bound = (0.0, 1.0)
        bounds = tuple(bound for asset in range(num_assets))
        result = optimize.minimize(self.neg_sharpe_ratio,
                                   num_assets * [1. / num_assets, ],
                                   args=args,
                                   method='SLSQP', bounds=bounds,
                                   constraints=constraints,
                                   options={"disp": True})
        return result

    def portfolio_volatility(self, weights, mean_returns, cov_matrix):
        return self.portfolio_annualised_performance(weights, mean_returns,
                                                     cov_matrix)[
            0]

    def min_variance(self, mean_returns, cov_matrix):
        num_assets = len(mean_returns)
        args = (mean_returns, cov_matrix)
        constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
        bound = (0.0, 1.0)
        bounds = tuple(bound for asset in range(num_assets))

        result = optimize.minimize(self.portfolio_volatility,
                                   num_assets * [1. / num_assets, ], args=args,
                                   method='SLSQP', bounds=bounds,
                                   constraints=constraints)

        return result

    def efficient_return(self, mean_returns, cov_matrix, target):
        num_assets = len(mean_returns)
        args = (mean_returns, cov_matrix)

        def portfolio_return(weights):
            return self.portfolio_annualised_performance(weights, mean_returns,
                                                         cov_matrix)[1]

        constraints = (
            {'type': 'eq', 'fun': lambda x: portfolio_return(x) - target},
            {'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
        bounds = tuple((0, 1) for asset in range(num_assets))
        result = optimize.minimize(self.portfolio_volatility,
                                   num_assets * [1. / num_assets, ], args=args,
                                   method='SLSQP', bounds=bounds,
                                   constraints=constraints)
        return result

    def efficient_frontier(self, mean_returns, cov_matrix, returns_range):
        efficients = []
        for ret in returns_range:
            efficients.append(
                self.efficient_return(mean_returns, cov_matrix, ret))
        return efficients

    def display_ef(self, mean_returns, cov_matrix, risk_free_rate,
                   num_portfolios,
                   returns, df, tickers):
        port_alloc_df = self.random_portfolios(num_portfolios, mean_returns,
                                               cov_matrix,
                                               risk_free_rate, tickers)

        max_sharpe = self.max_sharpe_ratio(mean_returns, cov_matrix,
                                           risk_free_rate)
        # expected volatility, expected return
        expected_volatility, expected_return = self.portfolio_annualised_performance(
            max_sharpe['x'],
            mean_returns,
            cov_matrix)  # weights,
        max_sharpe_allocation = pd.DataFrame(max_sharpe.x, index=df.columns,
                                             columns=['allocation'])
        max_sharpe_allocation.allocation = [round(i * 100, 2) for i in
                                            max_sharpe_allocation.allocation]
        max_sharpe_allocation = max_sharpe_allocation.T

        # Minimum volatility, minimum expected return
        min_vol = self.min_variance(mean_returns, cov_matrix)
        min_expected_volatility, min_expected_return = self.portfolio_annualised_performance(
            min_vol['x'],
            mean_returns,
            cov_matrix)
        min_vol_allocation = pd.DataFrame(min_vol.x, index=df.columns,
                                          columns=['allocation'])
        min_vol_allocation.allocation = [round(i * 100, 2) for i in
                                         min_vol_allocation.allocation]
        min_vol_allocation = min_vol_allocation.T

        annual_volatility = np.std(returns) * np.sqrt(252)
        annual_return = mean_returns * 252

        print("-" * 80)
        print("Maximum Sharpe Ratio Portfolio Allocation\n")
        print("Annualised Return:", expected_return)
        print("Annualised Volatility:", expected_volatility)
        print(max_sharpe_allocation)  # red star
        print("-" * 80)

        print("Minimum Volatility Portfolio Allocation\n")
        print("Annualised Return:", min_expected_return)
        print("Annualised Volatility:", min_expected_volatility)
        print(min_vol_allocation)  # green star
        print("-" * 80)
        print("Individual Stock Returns and Volatility\n")
        for i, txt in enumerate(df.columns):
            print(txt, ":", "annualised return", annual_return[i],
                  "| annualised volatility:", annual_volatility[i])
        print("-" * 80)

        frontier_y = np.linspace(min_expected_return, expected_return,
                                 100)  # This is frontier_y, expected return
        efficient_portfolios = self.efficient_frontier(mean_returns,
                                                       cov_matrix,
                                                       frontier_y)

        # fun is Volatility, x is Allocation, frontier_y=target is expected Return / sharpe ratio? ret / vol

        frontier_vol = [p['fun'] for p in efficient_portfolios]
        ef_alloc = [p['x'] for p in efficient_portfolios]
        ef_sr = frontier_y / np.array(frontier_vol)

        ef_alloc_df = pd.DataFrame.from_records(ef_alloc,
                                                columns=tickers)  # 10,000
        ef_vol_df = pd.DataFrame(frontier_vol,
                                 columns=['Standard Deviation'])  # 10,000
        ef_ret_df = pd.DataFrame(frontier_y,
                                 columns=['Expected Return'])  # 100
        ef_sr_df = pd.DataFrame(ef_sr, columns=['Sharpe Ratio'])  # 100

        ef_df = pd.concat([ef_alloc_df, ef_vol_df, ef_sr_df, ef_ret_df],
                          axis=1).sort_values(by=['Expected Return'])
        return annual_volatility, annual_return, expected_volatility, \
               expected_return, min_expected_volatility, min_expected_return, \
               max_sharpe, min_vol, frontier_vol, frontier_y, ef_df, \
               max_sharpe_allocation, min_vol_allocation

    def run_simulation(self, df):
        all_weights = np.zeros((self.num_portfolios, len(df.columns)))
        ret_arr = np.zeros(self.num_portfolios)
        vol_arr = np.zeros(self.num_portfolios)
        sharpe_arr = np.zeros(self.num_portfolios)

        for ind in range(self.num_portfolios):
            # Create Random Weights
            weights = np.array(np.random.random(len(self.tickers)))

            # Rebalance Weights
            weights = weights / np.sum(weights)

            # Save Weights
            all_weights[ind, :] = weights

            # Expected Return
            ret_arr[ind] = np.sum((df.pct_change().mean() * weights) * 252)

            # Expected Variance
            vol_arr[ind] = np.sqrt(
                np.dot(weights.T,
                       np.dot(df.pct_change().cov() * 252, weights)))

            # Sharpe Ratio
            sharpe_arr[ind] = ret_arr[ind] / vol_arr[ind]

        return vol_arr, ret_arr, sharpe_arr


#!/usr/bin/env python

"""照片模拟水的波动效果"""

import pygame, os
from pygame.locals import *
from math import sin  # 导入正弦函数的工具箱
import time

main_dir = os.path.split(os.path.abspath(__file__))[0]  # 获取当前文件所在路径

def main():
    pygame.init()  # 初始化
    screen = pygame.display.set_mode((640, 480), HWSURFACE|DOUBLEBUF)  # 生成一个窗口

    imagename = os.path.join(main_dir, 'data', 'liquid.bmp')  # 拼接图片路径
    bitmap = pygame.image.load(imagename)    # 加载图片
    bitmap = pygame.transform.scale2x(bitmap) # 将图像放大两倍
    bitmap = pygame.transform.scale2x(bitmap) # 将图像放大两倍

    if screen.get_bitsize() == 8: # 以相同的格式获取图像和屏幕
        screen.set_palette(bitmap.get_palette())
    else:
        bitmap = bitmap.convert()

    anim = 0.0

    while 1:
        for e in pygame.event.get():   # 处理事件
            if e.type in [QUIT, KEYDOWN, MOUSEBUTTONDOWN]:
                return

        anim = anim + 0.02
        for x in range(0,640,20):   # 模拟水的波动效果
            xpos = (x + (sin(anim + x * .01) * 15)) + 20
            for y in range(0, 480, 20):
                ypos = (y + (sin(anim + y * .01) * 15)) + 20
                screen.blit(bitmap, (x, y), (xpos, ypos, 20, 20))

        pygame.display.flip()  # 更新
        time.sleep(0.01)       # 等待0.01s

if __name__ == '__main__':
    main()


"""
Projeto de Implementação – Sistema Recuperação de Artigos

Teylor Moreto Guaitolini - 20191CECA70271
Matheus Corteletti Delfino - 20191CECA70212
"""

import sqlite3

class Banco:
    def __init__(self):
        self.conn = sqlite3.connect("banco.db")

    def __del__(self):
        self.conn.close()

    def create(self):
        """Cria as tabelas no banco de dados"""
        self.__cursor = self.conn.cursor()
        try:
            sqlstatement = "CREATE TABLE usuarios(PK_CPF VARCHAR(11) NOT NULL PRIMARY KEY, Senha VARCHAR(32) NOT NULL);"
            self.__cursor.execute(sqlstatement)

            sqlstatement = "CREATE TABLE artigos(PK_ID TEXT(255), Titulo TEXT(255), Resumo TEXT(255), Link TEXT(255), Consulta TEXT(255), FK_CPF VARCHAR(11), FOREIGN KEY (FK_CPF) REFERENCES usuarios(PK_CPF));"
            self.__cursor.execute(sqlstatement)
        except sqlite3.OperationalError:
            # print Exception.
            print("Erro. Verifique seu SQL")

        self.conn.commit()

    def insert(self, sqlstatement, dados):
        """Insere registros no banco de dados"""
        self.__cursor = self.conn.cursor()
        try:
            self.__cursor.execute(sqlstatement, dados)
        except sqlite3.OperationalError:
            print("Erro. Verifique seu SQL")

        self.conn.commit()

    def select(self, sqlstatement):
        """Seleciona registros do banco de dados"""
        self.__cursor = self.conn.cursor()

        try:
            self.__cursor.execute(sqlstatement)
        except sqlite3.OperationalError:
            print("Erro. Verifique seu SQL")

        self.dados = self.__cursor.fetchall()


import Menus
import os
import Autentificar

def criar_tab_Estoque( conexao ):
    cursor = conexao.cursor()

    sql = '''
      CREATE TABLE IF NOT EXISTS estoque(
        estq_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        estq_nome TEXT NOT NULL,
        estq_descricao TEXT NOT NULL,
        estq_quant INTEGER NOT NULL,
        estq_preco INTEGER NOT NULL
      );'''
    cursor.execute(sql)
    print('Tabela Estoque criada com sucesso!')

def verificar_preco(string):
    virgula = 0
    string = string.replace(' ','')
    for i in range(0,len(string)):
        # Tem virgula?
        if(string[i] == ','):
            virgula += 1

    # tem virgula!
    if(virgula == 1):
        string = string.split(',')
        if(len(string[1]) == 2):
            # tudo certo
            string = string[0]+string[1]
            return string

        elif(len(string[1]) == 1):
            # esta faltando um zero
            string = string[0] + string[1]+'0'
            return string

        elif (len(string[1]) == 0):
            string = string[0] + string[1] +'00'
            return string

        else:
            # Aceitar apenas 2 numeros após a virgula
            return 'Não decimal!'

    elif (virgula == 0):
        # foi digitado um numero inteiro! add as duas casas após a virgula
        string = string + '00'
        return string

    else:
        # se tiver mais de 1 virgula ou menos de 0 --> erro! Solicitar novamente!
        return 'Não decimal!'

def executar_sql(cursor, conexao, sql):

    x = input('\nSalvar dados?\n1 - SIM\n2 - NÃO\n\nOpção: ')
    if(x == '1'):

        cursor.execute(sql)
        conexao.commit()
        input('\nMercadoria atualizado com sucesso!\n\nPressione enter para continuar!')
        os.system('cls' if os.name == 'nt' else 'clear')

        Menus.menu_mercadoria()

        print('Opção: 2')

    elif(x == '2'):

        print('\nCancelado!')
        input('\nPressione enter para continuar!')
        os.system('cls' if os.name == 'nt' else 'clear')

        Menus.menu_mercadoria()

        print('Opção: 2')

def inserir_produtos(conexao):
    cursor = conexao.cursor()

    print('\n*********CADASTRAR NOVO PRODUTO*********\n')
    estq_nome = input('Nome: ')
    estq_descricao = input('Descrição: ')
    while(True):

        estq_quant = input('Quantidade: ')

        if(estq_quant.isdecimal()):
            break
        else:
            print('\nQuantidade deve ser um número INTEIRO!')
            input('\nPressione enter para continuar!')
            os.system('cls' if os.name == 'nt' else 'clear')
            Menus.menu_mercadoria()
            print('Opção: 1')
            print('\n*********CADASTRAR NOVO PRODUTO*********\n')
            print('Nome: {}'.format(estq_nome))
            print('Descrição: {}'.format(estq_descricao))

    while(True):
        estq_preco = input('Preço da mercadoria: ')
        # preço sera armazenado como inteiro pois SQLite nao tem opção float ou decimal com escala

        estq_preco = verificar_preco(estq_preco)

        if(estq_preco.isdecimal()):
            break
        else:

            print('\nPreço incorretamente informado!')
            input('\nPressione enter para continuar!')
            os.system('cls' if os.name == 'nt' else 'clear')
            Menus.menu_mercadoria()
            print('Opção: 1')
            print('\n*********CADASTRAR NOVO PRODUTO*********\n')
            print('Nome: {}'.format(estq_nome))
            print('Descrição: {}'.format(estq_descricao))
            print('Quantidade: {}'.format(estq_quant))

    try:
        if(estq_nome and estq_descricao and estq_quant and estq_preco):
            x = input('\nSalvar dados?\n1 - SIM\n2 - NÃO\n\nOpção:  ')

            if(x == '1'):
                sql = '''
                INSERT INTO estoque (estq_nome, estq_descricao, estq_quant, estq_preco) VALUES
                ('{}','{}',{},{})
                '''.format(estq_nome, estq_descricao, estq_quant, estq_preco)

                cursor.execute(sql)
                conexao.commit()

                print('\nMercadoria inserida com sucesso!')
                input('\nPressione enter para continuar!')

            elif (x == '2'):
                print('\nCancelado!')
                input('\nPressione enter para continuar!')

            else:
                print('\nOpção Inválida!\nCancelado!')
                input('\nPressione enter para continuar!')

        else:

            print('\nDados invalidos! Tente novamente!')
            input('Pressione enter para continuar!')

    except:

        print('\nAlgo deu errado!\nTenha certeza que todos os dados foram preenchidos corretamente.\nTente novamente!')
        input('\nPressione enter para continuar!')

def atualizar_produtos(conexao):

    cursor = conexao.cursor()

    while(True):

        Menus.menu_atualizar_estq()

        op = input('Opção: ')

        if(op == '1'):

            estq_id = input('\nId do produto: ')

            nome = Autentificar.validar_id_prod(conexao, estq_id)

            if(nome):
                print('\nProduto: {}'.format(nome))

                estq_nome = input('\nNovo nome: ')

                sql = '''
                UPDATE estoque
                SET estq_nome = '{}'
                WHERE estq_id = {}
                '''.format(estq_nome, estq_id)

                executar_sql(cursor, conexao, sql)

            else:
                print('\nProduto não encontrado!')
                input('\nPressione enter para continuar!')

                os.system('cls' if os.name == 'nt' else 'clear')

                Menus.menu_mercadoria()

                print('Opção: 2')

        elif (op == '2'):

            estq_id = input('\nId do produto: ')

            nome = Autentificar.validar_id_prod(conexao, estq_id)

            if(nome):
                print('\nProduto: {}'.format(nome))

                estq_descricao = input('\nNova descrição: ')

                sql = '''
                UPDATE estoque
                SET estq_descricao = '{}'
                WHERE estq_id = {}
                '''.format(estq_descricao, estq_id)

                executar_sql(cursor,conexao,sql)

            else:
                print('\nProduto não encontrado!')
                input('\nPressione enter para continuar!')

                os.system('cls' if os.name == 'nt' else 'clear')

                Menus.menu_mercadoria()

                print('Opção: 2')

        elif(op == '3'):
            estq_id = input('\nId produto: ')

            nome = Autentificar.validar_id_prod(conexao, estq_id)

            if(nome):
                print('\nProduto: {}'.format(nome))

                while(True):

                    estq_quant = input('\nAtualizar quantidade: ')

                    if(estq_quant.isdecimal()):
                        sql = '''
                        UPDATE estoque
                        SET estq_quant = {}
                        WHERE estq_id = {}
                        '''.format(estq_quant, estq_id)

                        executar_sql(cursor, conexao, sql)

                        break

                    else:
                        print('\nQuantidade deve ser um número INTEIRO!')
                        input('\nPressione enter para continuar!')
                        os.system('cls' if os.name == 'nt' else 'clear')
                        Menus.menu_mercadoria()
                        print('Opção: 2')
                        Menus.menu_atualizar_estq()
                        print('Opção: 3')
                        print('\nId do Produto: {}'.format(estq_id))
                        print('\nProduto: {}'.format(nome))

            else:
                print('\nProduto não encontrado!')
                input('\nPressione enter para continuar!')

                os.system('cls' if os.name == 'nt' else 'clear')

                Menus.menu_mercadoria()

                print('Opção: 2')

        elif(op == '4'):
            estq_id = input('\nId produto: ')

            nome = Autentificar.validar_id_prod(conexao, estq_id)

            if(nome):
                print('\nProduto: {}'.format(nome))

                while(True):

                    estq_preco_u = input('\nAtualizar preço/unidade aquisição: ')

                    estq_preco_u = verificar_preco(estq_preco_u)

                    if(estq_preco_u.isdecimal()):
                        sql = '''
                        UPDATE estoque
                        SET estq_preco = {}
                        WHERE estq_id = {}
                        '''.format(estq_preco_u, estq_id)

                        executar_sql(cursor,conexao,sql)

                        break
                    else:

                        print('\nPreço incorretamente informado!')
                        input('\nPressione enter para continuar!')
                        os.system('cls' if os.name == 'nt' else 'clear')
                        Menus.menu_mercadoria()
                        print('Opção: 2')
                        Menus.menu_atualizar_estq()
                        print('Opção: 4')
                        print('\nId produto: {}'.format(estq_id))
                        print('\nProduto: {}'.format(nome))

            else:
                print('\nProduto não encontrado!')
                input('\nPressione enter para continuar!')

                os.system('cls' if os.name == 'nt' else 'clear')

                Menus.menu_mercadoria()

                print('Opção: 2')

        elif (op == '5'):
            break

        else:
            print('\nOpção invalida!')
            input('\nPressione enter para continuar!')

            os.system('cls' if os.name == 'nt' else 'clear')

            Menus.menu_mercadoria()

            print('Opção: 2')

def deletar_produtos(conexao):
    cursor = conexao.cursor()
    estq_id = input('\nId produto: ')
    nome = Autentificar.validar_id_prod(conexao, estq_id)

    if(nome):

        print('\nProduto: {}'.format(nome))

        sql = '''
            DELETE FROM estoque
            WHERE estq_id = {}
        '''.format(estq_id)

        x = input('\nTem certeza que deseja excluir?\n1 - SIM\n2 - NÃO\n\nOpção: ')

        if(x == '1'):
            cursor.execute(sql)
            conexao.commit()

            input('\nProduto deletado com sucesso!\n\nPressione enter para continuar!')

        elif(x == '2'):
            input('\nExclusão cancelada!\n\nPressione enter para continuar!')

        else:
            input(
                '\nOpção invalida!\nExclusão cancelada!\n\nPressione enter para continuar!')

    else:
        input('\nProduto não encontrado!\n\nPressione enter para continuar!')

def listar_produtos(conexao):
    cursor = conexao.cursor()

    sql = '''
    SELECT * FROM estoque
    '''
    cursor.execute(sql)
    lista = cursor.fetchall()

    print('\n________________ Estoque _______________\n')
    for u in lista:
        x = '{}'.format(int(u[4])/100)
        vet = x.split('.')
        if(len(vet[1]) == 1):
            x = 'R$' + vet[0] + ',' + vet[1] + '0'
        else:
            x = 'R$' + vet[0] + ',' + vet[1]

        print('''
    Id: {}
    Produto: {}
    Descrição: {}
    Qntd estoque: {}
    Preço: {}
    '''.format(u[0], u[1], u[2], u[3], x))

    input('\n________________________________________\n\nPressione enter para continuar!')

	# print('\n________________ Estoque _______________\n', lista)
	# for u in lista:
    #   u[4] = int(u[4])/100
	# 	print('''
	# ID: {}
	# Produto: {}
	# Descrição: {}
	# Qntd Estoque: {}
	# Preço Unitário: {}
    #     Porcentagem lucro(%): {}
	# '''.format(u[0], u[1], u[2], u[3], u[4], u[5] ))
	# input('\n________________________________________\n\nPressione enter para continuar!')


#!/usr/bin/env python

# uppercase one letter
def __upper_case(word, letter):
	return word[0:letter] + word[letter].upper() + word[letter+1:]

# single letter case alterator
def single_case_alterator(word):
	yield word
	for i in xrange(0, len(word)):
		yield __upper_case(word, i)

# all letter case alterator (recursive generator, first time I've made one of those)
def all_case_alterator(word, letter_num=0):
	if len(word) == 1:
		if word.isdigit():
			yield word
		else:
			yield word.lower()
			yield word.upper()
	else:
		recursed_gen = all_case_alterator(word[1:])
		try:
			while True:
				recursed_ret1 = recursed_gen.next()
				recursed_ret2 = recursed_gen.next()
				if word[0].isdigit():
					yield word[0] + recursed_ret1
					yield word[0] + recursed_ret2
				else:
					yield word[0].lower() + recursed_ret1
					yield word[0].upper() + recursed_ret1
					yield word[0].lower() + recursed_ret2
					yield word[0].upper() + recursed_ret2
		except StopIteration:
			pass


from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponseRedirect
from django.contrib import messages
from instructors.forms import RatingForm
from instructors.models import Instructor, Rating
from django.contrib.auth.decorators import login_required
import datetime

@login_required
def AddRatingView(request, instructor_name):
    print(instructor_name)
    instructor = get_object_or_404(Instructor, username=instructor_name)
    form = RatingForm(request.POST)
    rating_qr = Rating.objects.filter(user=request.user, instructor=instructor)
    if rating_qr.exists():
        messages.warning(request, 'You already rated the instructor')
        return redirect('instructors:instructor-detail', instructor_name)
    else:
        if form.is_valid():
            userrating = form.cleaned_data['rating']
            review = form.cleaned_data['review']
            rating = Rating()
            rating.instructor = instructor
            rating.pub_date = datetime.datetime.now()
            rating.user = request.user
            rating.review = review
            rating.rating = userrating
            rating.save()
            messages.success(request, 'Successfully reviewed')
            return redirect('instructors:instructor-detail', instructor_name)
        return render(request, 'instructors/instructor_detail.html', {'form': form, 'instructor': instructor})


#!/usr/bin/env python3

# Reading Bytes From a File
with open('test_file.dat', 'rb') as binary_file:
    # Read the whole file at once
    data = binary_file.read()
    print(data)

    # Seek positions and read N bytes
    binary_file.seek(0) # Go to the beggining
    couple_bytes = bynary_file.read(2)
    print(couple_bytes)


import argparse
import os

from a_e_model import *

if __name__ == "__main__":
    # Inputs for the main function
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--data_name",
        choices=[
            "fault_free_training",
            "faulty_training",
            "faulty_testing",
            "fault_free_training_test",
        ],
        default="fault_free_training_test",
        type=str,
    )
    parser.add_argument(
        "--skip_rows",
        help="how many rows to skip in the data file",
        default=20,
        type=int,
    )
    parser.add_argument("--batch_size", help="batch size", default=512, type=int)
    parser.add_argument("--lags", help="pnumber of lags", default=1, type=int)
    parser.add_argument("--loss_threshold", help="loss_threshold", default=0.113, type=float)
    parser.add_argument(
        "--perc_of_data_withing_bound",
        help="perc_of_data_withing_bound",
        default=0.95,
        type=float,
    )
    parser.add_argument("--perc_of_outputs", help="perc_of_outputs", default=0.75, type=float)
    args = parser.parse_args()

    data_name = str(args.data_name)
    skip_rows = int(args.skip_rows)
    batch_size = int(args.batch_size)
    lags = int(args.lags)
    loss_threshold = float(args.loss_threshold)
    perc_of_data_withing_bound = float(args.perc_of_data_withing_bound)
    perc_of_outputs = float(args.perc_of_outputs)

    ## **Defining folder paths**
    model_dir = "model/weights/"
    normalization_params_dir = "model/normalization_parameters/"
    debug_dir = "debug/"
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)
    if not os.path.exists(normalization_params_dir):
        os.makedirs(normalization_params_dir)
    if not os.path.exists(debug_dir):
        os.makedirs(debug_dir)

    # Load data
    # norm_data, norm_parameters = data_loader_autoencoder(data_name, lags, skip_rows, normalization_params_dir)

    # train autoencoder
    model, norm_parameters = build_model(model_dir, normalization_params_dir)

    model_parameters = {
        "batch_size": batch_size,
        "norm_parameters": norm_parameters,
        "lags": lags,
    }

    perc_of_data_withing_bound = [i for i in np.arange(0.98, 0, -0.05)]
    perc_of_outputs = [i for i in np.arange(0, 0.98, 0.05)]
    for perc_of_data_withing_bound_i in perc_of_data_withing_bound:
        loss_threshold_arr = train_loss_fast2(model, model_parameters, perc_of_data_withing_bound_i)

        excluded_faults = [3, 9, 15]
        faults_list = [0, 1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20]
        false_alarm_ratio = []
        for perc_of_outputs_i in perc_of_outputs:
            for fault_no in faults_list:
                if fault_no in excluded_faults:
                    continue
                if fault_no == 0:
                    print(
                        perc_of_data_withing_bound_i,
                        perc_of_outputs_i,
                        0,
                        detect_anomalies2(
                            model,
                            model_parameters,
                            fault_no,
                            loss_threshold_arr,
                            perc_of_outputs_i,
                        ),
                    )
                else:
                    false_alarm_ratio.append(
                        detect_anomalies2(
                            model,
                            model_parameters,
                            fault_no,
                            loss_threshold_arr,
                            perc_of_outputs_i,
                        )
                    )
            print(
                perc_of_data_withing_bound_i,
                perc_of_outputs_i,
                np.mean(false_alarm_ratio),
            )
            # print(false_alarm_ratio)
            print("**********************")


def test(n):
 print("here you go from Python Application 2")
 a = [1,2]
 a = [1,2] + [3,4]
 print(len(a))
 print(n)
 num = 0
 for b in a:
     num += b
     print(num)


import torch

#three functions for federated average
def add_model(dst_model, src_model, dst_no_data, src_no_data):
    """Add the parameters of two models.
        Args:
            dst_model (torch.nn.Module): the model to which the src_model will be added.
            src_model (torch.nn.Module): the model to be added to dst_model.
        Returns:
            torch.nn.Module: the resulting model of the addition.
        """
    if (dst_model==None):
        return src_model
    params1 = src_model.named_parameters()
    params2 = dst_model.named_parameters()
    dict_params2 = dict(params2)
    with torch.no_grad():
        for name1, param1 in params1:
            if name1 in dict_params2:
                dict_params2[name1].set_(param1.data*src_no_data + dict_params2[name1].data*dst_no_data)
    return dst_model

def scale_model(model, scale):
    """Scale the parameters of a model.
    Args:
        model (torch.nn.Module): the models whose parameters will be scaled.
        scale (float): the scaling factor.
    Returns:
        torch.nn.Module: the module with scaled parameters.
    """
    params = model.named_parameters()
    dict_params = dict(params)
    with torch.no_grad():
        for name, param in dict_params.items():
            dict_params[name].set_(dict_params[name].data * scale)
    return model

def federated_avg(models,data_num):
    """Calculate the federated average of a list of models.
    Args:
        models: the dictionary of models of which the federated average is calculated.
    Returns:
        torch.nn.Module: the module with averaged parameters.
    """

    total_no_data=0
    model=None
    for i in models.keys():
        model = add_model(model, models[i],total_no_data,data_num[i])
        model = scale_model(model, 1.0 / (total_no_data+data_num[i]))
        total_no_data=total_no_data+data_num[i]
    return model