import numpy
import scipy.special
import matplotlib.pyplot

class neuralNetwork():

    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        # 学习率
        self.lr = learningrate

        # 链接权重矩阵
        self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
        self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)

        # 使用正态分布采样权重
        # self.wih = numpy.random.normal(0.0,pow(self.hnodes,-0.5),(self.hnodes,self.inodes))
        # self.who = numpy.random.normal(0.0,pow(self.onodes,-0.5),(self.onodes,self.hnodes))

        # 激活函数
        self.activation_function = lambda x: scipy.special.expit(x)

    def train(self,inputs_list,targets_list):

        # 初始化为array
        inputs = numpy.array(inputs_list, ndmin=2).T
        targets = numpy.array(targets_list, ndmin=2).T

        hidden_inputs = numpy.dot(self.wih, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = numpy.dot(self.who, hidden_outputs)
        final_outputs = self.activation_function(final_inputs)

        # 误差
        output_errors = targets - final_outputs
        hidden_errors = numpy.dot(self.who.T,output_errors)

        # 更新权重
        # 隐藏层与输出层
        self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)),numpy.transpose(hidden_outputs))
        # 输入层与隐藏层
        self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)),numpy.transpose(inputs))

    def query(self,inputs_list):

        inputs = numpy.array(inputs_list,ndmin=2).T
        hidden_inputs = numpy.dot(self.wih,inputs)
        hidden_outputs = self.activation_function(hidden_inputs)
        final_inputs = numpy.dot(self.who,hidden_outputs)
        final_outputs = self.activation_function(final_inputs)
        return final_outputs

if __name__ == '__main__':

    input_nodes = 784 # 28x28
    hidden_nodes = 100
    output_nodes = 10
    learning_rate = 0.3

    n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)

    with open('mnist_train_100.csv','r') as f:
        train_list = f.readlines()

    for record in train_list:

        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:])/255.0 * 0.99)+0.01
        targets = numpy.zeros(output_nodes) + 0.01
        targets[int(all_values[0])] = 0.99
        n.train(inputs,targets)

    with open('mnist_test_10.csv', 'r') as f:
        test_list = f.readlines()

    for record in test_list:

        all_values = record.split(',')
        image_array = numpy.asfarray(all_values[1:]).reshape((28,28))
        matplotlib.pyplot.imshow(image_array,cmap='Greys',interpolation='None')
        matplotlib.pyplot.show()

        result = n.query((numpy.asfarray(all_values[1:])/255.0 * 0.99)+0.01)
        print(str(numpy.argmax(result)),str(all_values[0]))


#! python3

''' The program will prompt for a URL,
read the JSON data from that URL using urllib
and then parse and extract the comment counts from the JSON data,
compute the sum of the numbers in the file

Sample data: http://python-data.dr-chuck.net/comments_42.json (Sum=2553)

Assignment #2, using JSON

Developed by Kyle DuBois, Version 1.0 '''

import urllib.request, urllib.parse, urllib.error
import json

numList = list()

while True:
    address = input('Enter location: ')
    if len(address) < 1:
        break

    print('Retrieving', address)

    # This section opens the URL and parses through JSON data
    url = urllib.request.urlopen(address)
    data = url.read().decode()
    headers = dict(url.getheaders())
    print('Retrieved', len(data),'characters')
    info = json.loads(data)

    #print(json.dumps(info, indent=4))

    # This section looks for and extracts the count numbers from the JSON data and creates a list
    for u in info["comments"]:
        #print(u['count'])
        numList.append(u['count'])

    # This section runs through the list and creates a new list of integers
    nunuList = list()
    for num in numList:
        newNum = int(num)
        nunuList.append(newNum)

    # This section sums all the integers and prints the total
    tot = sum(nunuList)
    print(tot)
    break


"""Mixins for review request views."""

from __future__ import annotations

import logging
from datetime import datetime
from typing import List, Optional, TYPE_CHECKING

from django.db.models import Q
from django.http import Http404, HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404, render
from django.template.defaultfilters import date
from django.utils.formats import localize
from django.utils.html import format_html
from django.utils.safestring import SafeString, mark_safe
from django.utils.timezone import localtime
from django.utils.translation import gettext
from djblets.views.generic.base import (CheckRequestMethodViewMixin,
                                        PrePostDispatchViewMixin)
from typing_extensions import TypedDict

from reviewboard.accounts.mixins import CheckLoginRequiredViewMixin
from reviewboard.diffviewer.models import DiffSet
from reviewboard.reviews.models import ReviewRequest, ReviewRequestDraft
from reviewboard.reviews.models.base_review_request_details import \
    BaseReviewRequestDetails
from reviewboard.reviews.models.review_request import ReviewRequestCloseInfo
from reviewboard.site.mixins import CheckLocalSiteAccessViewMixin
from reviewboard.site.models import LocalSite

if TYPE_CHECKING:
    from reviewboard.attachments.models import FileAttachmentSequence

logger = logging.getLogger(__name__)

class ReviewRequestViewMixin(CheckRequestMethodViewMixin,
                             CheckLoginRequiredViewMixin,
                             CheckLocalSiteAccessViewMixin,
                             PrePostDispatchViewMixin):
    """Common functionality for all review request-related pages.

    This performs checks to ensure that the user has access to the page,
    returning an error page if not. It also provides common functionality
    for fetching a review request for the given page, returning suitable
    context for the template, and generating an image used to represent
    the site when posting to social media sites.
    """

    permission_denied_template_name = \
        'reviews/review_request_permission_denied.html'

    class StatusExtraInfo(TypedDict):
        """Extra info to include in the status rendering.

        This is used in :py:meth:`get_review_request_status_html`.
        """

        #: A text string to display to the user.
        #:
        #: Type:
        #:     str
        text: str

        #: The timestamp associated with this info block.
        #:
        #: Type:
        #:     datetime.datetime
        timestamp: Optional[datetime]

        #: Extra info to format into the ``text`` string.
        #:
        #: Type:
        #:     dict
        extra_vars: dict

    def pre_dispatch(
        self,
        request: HttpRequest,
        review_request_id: int,
        *args,
        **kwargs,
    ) -> Optional[HttpResponse]:
        """Look up objects and permissions before dispatching the request.

        This will first look up the review request, returning an error page
        if it's not accessible. It will then store the review request before
        calling the handler for the HTTP request.

        Args:
            request (django.http.HttpRequest):
                The HTTP request from the client.

            review_request_id (int):
                The ID of the review request being accessed.

            *args (tuple):
                Positional arguments to pass to the handler.

            **kwargs (dict):
                Keyword arguments to pass to the handler.

                These will be arguments provided by the URL pattern.

        Returns:
            django.http.HttpResponse:
            The resulting HTTP response to send to the client, if there's
            a Permission Denied.
        """
        self.review_request = self.get_review_request(
            review_request_id=review_request_id,
            local_site=self.local_site)

        if not self.review_request.is_accessible_by(request.user):
            return self.render_permission_denied(request)

        return None

    def render_permission_denied(
        self,
        request: HttpRequest,
    ) -> HttpResponse:
        """Render a Permission Denied page.

        This will be shown to the user if they're not able to view the
        review request.

        Args:
            request (django.http.HttpRequest):
                The HTTP request from the client.

        Returns:
            django.http.HttpResponse:
            The resulting HTTP response to send to the client.
        """
        return render(request,
                      self.permission_denied_template_name,
                      status=403)

    def get_review_request(
        self,
        review_request_id: int,
        local_site: Optional[LocalSite] = None,
    ) -> ReviewRequest:
        """Return the review request for the given display ID.

        Args:
            review_request_id (int):
                The review request's display ID.

            local_site (reviewboard.site.models.LocalSite):
                The Local Site the review request is on.

        Returns:
            reviewboard.reviews.models.review_request.ReviewRequest:
            The review request for the given display ID and Local Site.

        Raises:
            django.http.Http404:
                The review request could not be found.
        """
        q = ReviewRequest.objects.all()

        if local_site:
            q = q.filter(local_site=local_site,
                         local_id=review_request_id)
        else:
            q = q.filter(pk=review_request_id)

        q = q.select_related('submitter', 'repository')

        return get_object_or_404(q)

    def get_diff(
        self,
        revision: Optional[int] = None,
        draft: Optional[ReviewRequestDraft] = None,
    ) -> DiffSet:
        """Return a diff on the review request matching the given criteria.

        If a draft is provided, and ``revision`` is either ``None`` or matches
        the revision on the draft's DiffSet, that DiffSet will be returned.

        Args:
            revision (int, optional):
                The revision of the diff to retrieve. If not provided, the
                latest DiffSet will be returned.

            draft (reviewboard.reviews.models.review_request_draft.
                   ReviewRequestDraft, optional):
                The draft of the review request.

        Returns:
            reviewboard.diffviewer.models.diffset.DiffSet:
            The resulting DiffSet.

        Raises:
            django.http.Http404:
                The diff does not exist.
        """
        # Normalize the revision, since it might come in as a string.
        if revision:
            revision = int(revision)

        # This will try to grab the diff associated with a draft if the review
        # request has an associated draft and is either the revision being
        # requested or no revision is being requested.
        if (draft and draft.diffset_id and
            (revision is None or draft.diffset.revision == revision)):
            return draft.diffset

        query = Q(history=self.review_request.diffset_history_id)

        # Grab a revision if requested.
        if revision is not None:
            query = query & Q(revision=revision)

        try:
            return DiffSet.objects.filter(query).latest()
        except DiffSet.DoesNotExist:
            raise Http404

    def get_social_page_image_url(
        self,
        file_attachments: FileAttachmentSequence,
    ) -> Optional[str]:
        """Return the URL to an image used for social media sharing.

        This will look for the first attachment in a list of attachments that
        can be used to represent the review request on social media sites and
        chat services. If a suitable attachment is found, its URL will be
        returned.

        Args:
            file_attachments (list of reviewboard.attachments.models.
                              FileAttachment):
                A list of file attachments used on a review request.

        Returns:
            str:
            The URL to the first image file attachment, if found, or ``None``
            if no suitable attachments were found.
        """
        for file_attachment in file_attachments:
            if file_attachment.mimetype.startswith('image/'):
                return file_attachment.get_absolute_url()

        return None

    def get_review_request_status_html(
        self,
        review_request_details: BaseReviewRequestDetails,
        close_info: ReviewRequestCloseInfo,
        extra_info: List[ReviewRequestViewMixin.StatusExtraInfo] = [],
    ) -> SafeString:
        """Return HTML describing the current status of a review request.

        This will return a description of the submitted, discarded, or open
        state for the review request, for use in the rendering of the page.

        Args:
            review_request_details (reviewboard.reviews.models
                                    .base_review_request_details
                                    .BaseReviewRequestDetails):
                The review request or draft being viewed.

            close_info (dict):
                A dictionary of information on the closed state of the
                review request.

            extra_info (list of dict):
                A list of dictionaries showing additional status information.
                Each must have a ``text`` field containing a format string
                using ``{keyword}``-formatted variables, a ``timestamp`` field
                (which will be normalized to the local timestamp), and an
                optional ``extra_vars`` for the format string.

        Returns:
            django.utils.safestring.SafeString:
            The status text as HTML for the page.
        """
        review_request = self.review_request
        status = review_request.status
        review_request_details = review_request_details

        if status == ReviewRequest.SUBMITTED:
            timestamp = close_info['timestamp']

            if timestamp:
                text = gettext('Created {created_time} and submitted '
                               '{timestamp}')
            else:
                text = gettext('Created {created_time} and submitted')
        elif status == ReviewRequest.DISCARDED:
            timestamp = close_info['timestamp']

            if timestamp:
                text = gettext('Created {created_time} and discarded '
                               '{timestamp}')
            else:
                text = gettext('Created {created_time} and discarded')
        elif status == ReviewRequest.PENDING_REVIEW:
            text = gettext('Created {created_time} and updated {timestamp}')
            timestamp = review_request_details.last_updated
        else:
            logger.error('Unexpected review request status %r for '
                         'review request %s',
                         status, review_request.display_id,
                         extra={'request': request})

            return mark_safe('')

        parts = [
            {
                'text': text,
                'timestamp': timestamp,
                'extra_vars': {
                    'created_time': date(localtime(review_request.time_added)),
                },
            },
        ] + extra_info

        html_parts = []

        for part in parts:
            if part['timestamp']:
                timestamp = localtime(part['timestamp'])
                timestamp_html = format_html(
                    '<time class="timesince" datetime="{0}">{1}</time>',
                    timestamp.isoformat(),
                    localize(timestamp))
            else:
                timestamp_html = ''

            html_parts.append(format_html(
                part['text'],
                timestamp=timestamp_html,
                **part.get('extra_vars', {})))

        return mark_safe(' — '.join(html_parts))


# -*- coding: utf-8 -*-
import os
import sys
import math
import logging
import numpy as np
from numpy import exp
from copy import deepcopy

import rmgpy.constants as constants
from rmgpy.statmech.vibration import HarmonicOscillator

from ape.sampling import SamplingJob
from ape.FitPES import from_sampling_result, cubic_spline_interpolations
from ape.schrodinger import SetAnharmonicH

class Statmech(object):
    """
    A class to solve shrodinger equation, evaluate partition function and related properties of 1-D PES by using statistical thermodynamics
    """
    def __init__(self, label, input_file, output_directory, Tlist=[298.15], P=100000, frequency_scale_factor=1, ncpus=None):
        self.label = label
        self.input_file = input_file
        self.output_directory = output_directory
        self.Tlist = Tlist
        self.P = P
        self.frequency_scale_factor = frequency_scale_factor
        self.ncpus = ncpus
        self.result_info = list()

    def load_save(self):
        self.csv_path = os.path.join(self.output_directory, '{}_samping_result.csv'.format(self.label))
        self.mode_dict, self.energy_dict, self.min_elect, self.rotors, job_keys = from_sampling_result(self.csv_path)
        n_rotors, n_vibs = 0, 0
        for mode in self.mode_dict.values():
            if mode['mode'] == 'tors': n_rotors += 1
            else: n_vibs += 1
        self.sampling = SamplingJob(self.label, self.input_file, output_directory=self.output_directory, ncpus=self.ncpus, rotors=self.rotors, **job_keys)
        logging.disable(50)
        self.sampling.parse()
        self.sampling.sampling(save_result=False)
        logging.disable(logging.NOTSET)
        self.is_QM_MM_INTERFACE = self.sampling.is_QM_MM_INTERFACE
        self.conformer = self.sampling.conformer
        self.raw_conformer = self.sampling.raw_conformer
        self.only_vib_conformer = self.sampling.only_vib_conformer
        self.zpe_of_Hohf = self.sampling.zpe
        e0 = self.min_elect * constants.E_h * constants.Na + self.sampling.zpe
        self.conformer.E0 = (e0, "J/mol")
        for mode in self.conformer.modes:
            if isinstance(mode, HarmonicOscillator):
                frequencies = mode.frequencies.value_si
                mode.frequencies = (frequencies * self.frequency_scale_factor, "cm^-1")
        self.spin_multiplicity = self.conformer.spin_multiplicity
        self.optical_isomers =self.conformer.optical_isomers
        self.symbols = self.sampling.symbols

        # Solve SE of 1-D PES and calculate E S G Cp
        self.polynomial_dict = cubic_spline_interpolations(self.energy_dict, self.mode_dict)

        # Extract whether this system is QM/MM system or not
        self.is_QM_MM_INTERFACE = self.sampling.is_QM_MM_INTERFACE

    def calcThermoOfEachMode(self, eig, N, mode, T):
        beta = 1/(constants.kB*T) * constants.E_h
        Q = 0
        Q_vib = 0
        E = 0
        dQ = 0
        ddQ = 0
        for i in range(N):
            Ei = eig[i]
            Q += exp(-beta*Ei)
            dQ += Ei*exp(-beta*Ei)*beta/T
            ddQ += -2*Ei*exp(-beta*Ei)*beta/pow(T,2) + pow(Ei,2)*exp(-beta*Ei)*pow(beta,2)/pow(T,2)
            E += Ei*exp(-beta*Ei)
            if i == 0:
                zpve = Ei
            # Measuring energy relative to the zero point vibration frequency
            dE = Ei - zpve
            Q_vib += exp(-beta*dE)
        E /= Q
        is_tors = True if self.mode_dict[mode]['mode'] == 'tors' else False
        if is_tors:
            omega = self.mode_dict[mode]['symmetry_number']
            Q /= omega
            Q_vib /= omega
            dQ /= omega
            ddQ /= omega

        E0 = eig[0]
        v = (eig[1]-eig[0]) * constants.E_h / constants.h / (constants.c * 100)
        #print(Q)

        F = -math.log(Q)/beta
        S = (E - F)/T
        Cv = (2/Q*dQ - T*pow(dQ/Q,2) + T/Q*ddQ)/beta

        return v, E0, E, S, F, Q, Q_vib, Cv

    def SolvEig(self, mode, T):
        Nbasis = 50
        Nbasis_prev = 0
        H_prev = None
        Qold =  np.log(sys.float_info[0])
        vold =  np.log(sys.float_info[0])
        converge = False
        while not converge:
            Nbasis += 1
            H = SetAnharmonicH(self.polynomial_dict, self.mode_dict, self.energy_dict, mode, Nbasis, N_prev=Nbasis_prev, H_prev=H_prev)
            Nbasis_prev = Nbasis
            H_prev = deepcopy(H)
            eig, v =np.linalg.eigh(H)
            v, E0, E, S, F, Q, Q_vib, Cv = self.calcThermoOfEachMode(eig, Nbasis, mode, T)

            if Qold == np.log(sys.float_info[0]):
                self.result_info.append("# \n# \t %d \t\t-\t\t-" % Nbasis) #first run
                logging.debug("# \t {} \t\t-\t\t-".format(Nbasis))
            else:
                self.result_info.append("# \n# \t %d \t\t %.10f \t\t %.10f" % (Nbasis, abs(Q-Qold), abs(v-vold)))
                logging.debug("# \t {:d} \t\t {:.10f} \t\t {:.10f}".format(Nbasis, abs(Q-Qold), abs(v-vold)))

            if ((abs(Q-Qold)<1e-4) and (abs(v-vold)<1e-2)):
                self.result_info.append("# Convergence criterion met")
                self.result_info.append("# ------------------------------------")
                converge = True
                self.result_info.append("# Frequency (cm-1): %.10f" % v)
                self.result_info.append("# Zero point vibrational energy (hartree): %.10f" % E0)
                self.result_info.append("# Energy (hartree): %.10f" % E )
                self.result_info.append("# Entropy (hartree/K): %.10f" % S)
                self.result_info.append("# Free energy (hartree): %.10f" % F)
                self.result_info.append("# Partition function: %.10f" % Q)
                hartree2kcalmol = constants.E_h * constants.Na / 4184
                E0 *= hartree2kcalmol
                E *= hartree2kcalmol
                S *= hartree2kcalmol * 1000
                F *= hartree2kcalmol
                Cv *= hartree2kcalmol * 1000
                '''
                print("Frequency (cm-1): ",v)
                print("Zero point vibrational energy (kcal/mol): ",E0)
                print("Energy (kcal/mol): ",E )
                print("Entropy (cal/mol/K): ",S)
                print("Free energy (kcal/mol): ",F)
                print("Partition function: ",Q)
                '''

            Qold = Q
            vold = v
        return v, E0, E, S, F, Q, Q_vib, Cv


import sqlite3
from script import Goals, teams, Games
import time

#Separates the output of script.py
print("\nOutput from script.py.")
print("\n" * 10)
print("Output now is from Query.py.\n\n")

def SortByGroup():
    Test = True
    while Test:
        search = input("Grupo (A/B/C/D/E/F/G/H): ").upper()
        if search in ("A", "B", "C", "D", "E", "F", "G", "H"):
            search = "Group " + search
            Test = False
        else:
            print("Enter a valid letter!\n")
    #Print games by user group input
    for row in cursor.execute("SELECT * FROM FifaWorldCup WHERE MatchGroup = ?", (search,)):
        print('{0}: {1} {2} {3} on {4} in {5}'.format(row[1], row[2], row[3], row[4], row[5], row[6]))

def SortByAllGroups():
    #Print all  rows sorted by group
    for row in cursor.execute('SELECT * FROM FifaWorldCup ORDER BY MatchGroup'):
        print('{0} {1} {2}, {3} in {4}'.format(row[1], row[2], row[3], row[4], row[5]))

def SortByGamesTeam(Teams):
    Test = True
    print("\n\n")
    #Print games by user group input
    while Test:
        print("List of teams: ")
        print(Teams)
        search = input("Name of team (ex: Portugal, be sure the name has Capitalize letters "
                        + "and must be in english): ")
        if search in Teams:
            print("\n\n")
            for row in cursor.execute("SELECT * FROM FifaWorldCup WHERE TeamHome = ?", (search,)):
                print('{0}: {1} {2} {3} on {4} in {5}'.format(row[1], row[2], row[3], row[4], row[5], row[6]))
            for row in cursor.execute("SELECT * FROM FifaWorldCup WHERE TeamAway = ?", (search,)):
                print('{0}: {1} {2} {3} on {4} in {5}'.format(row[1], row[2], row[3], row[4], row[5], row[6]))
            Test = False
        else:
            print("\n" * 5 + "Enter a valid team name!")

def SortByLocation(Locals):
    Test = True
    while Test:
        print("All locations: ")
        print(Locals)
        search = input("\nLocation name (Please Copy & Paste from the list above without "
                        + "\'\' to search the name of location): ")
        if search in Locals:
            #Code that is executed when is valid input
            print("\n\n")
            for row in cursor.execute("SELECT * FROM FifaWorldCup WHERE Location = ?", (search,)):
                print('{0}: {1} {2} {3} on {4} - {5}'.format(row[6], row[2], row[3], row[4], row[5], row[1]))
            Test = False
        else:
            print("\n" * 5 + "Enter a valid location!")

def SortByAllLocation():
    for row in cursor.execute("SELECT * FROM FifaWorldCup ORDER BY Location"):
        print('{0}: {1} {2} {3} on {4} - {5}'.format(row[6], row[2], row[3], row[4], row[5], row[1]))

def GoalsByTeam(Teams, Goals):
    Test = True
    golos = 0
    print("\n\n")
    #Print games by user group input
    while Test:
        print("List of teams: ")
        print(Teams)
        search = input("Name of team (ex: Portugal, be sure the name has Capitalize letters "
                        + "and must be in english): ")
        if search in Teams:
            #Code that is executed when is valid input
            j = [i for i, x in enumerate(teams) if x == search]
            try:
                for i in range(len(j)):
                    golos += Goals[j[i]]
            except IndexError:
                print(search + ": {0}".format(golos) + " goals")
            Test = False
        else:
            print("\n" * 5 + "Enter a valid team name!")

def AvGoalsPerGame(Golos, Games):
    av = Golos / Games
    print("Average goals per game:", av)

def AvGoalsPerDay(Golos):
    av = Golos / 14
    print("Average goals per day:", av)

def GoalsPerGroup(Goals):
    golosG = 0
    Test = True
    for row in cursor.execute("SELECT MatchGroup, GH, GA FROM FifaWorldCup ORDER BY MatchGroup"):
        if (row[1] and row [2]) > (-1):
            golosG += row[1] + row[2]
            Test = True
        elif Test:
            print('{0}: {1}'.format(row[0], golosG))
            golosG = 0
            Test = False

def EveryGoalRegistered(Golos):
    print("Total number of goals so far:", Golos)

#Connects to db
db = sqlite3.connect('db.sqlite3')
cursor = db.cursor()

Golos = 0
for i in range(len(Goals)):
    Golos += Goals[i]
Teams = []
for row in cursor.execute('''SELECT DISTINCT TeamHome FROM FifaWorldCup ORDER BY TeamHome'''):
    Teams.append(row[0])
Locals = []
for row in cursor.execute('''SELECT DISTINCT Location FROM FifaWorldCup ORDER BY Location'''):
    Locals.append(row[0])

Loop = True
while Loop:
    time.sleep(2)
    print("\n")
    print("1 - User Group Info | 2 - All groups by order | 3 - User Team Info | 4 - User Location Info "
            + "| 5 - All locations by order | 6 - User team goals | 7 - Average goals per game "
            + "| 8 - Average goals per day | 9 - User Group Goals | 10 - Every goal so far | 0 - Exit")
    op = input("Opção: ")
    if op == "0":
        exit()
    elif op == "1":
        print("\n" * 5)
        SortByGroup()
    elif op == "2":
        print("\n" * 5)
        SortByAllGroups()
    elif op == "3":
        print("\n" * 5)
        SortByGamesTeam(Teams)
    elif op == "4":
        print("\n" * 5)
        SortByLocation(Locals)
    elif op == "5":
        print("\n" * 5)
        SortByAllLocation()
    elif op == "6":
        print("\n" * 5)
        GoalsByTeam(Teams, Goals)
    elif op == "7":
        print("\n" * 5)
        AvGoalsPerGame(Golos, Games)
    elif op == "8":
        print("\n" * 5)
        AvGoalsPerDay(Golos)
    elif op == "9":
        print("\n" * 5)
        GoalsPerGroup(Goals)
    elif op == "10":
        print("\n" * 5)
        EveryGoalRegistered(Golos)

db.close()


'''
Collection of functions to drawing synthetic corpora based on Dirichlet distribution.
'''

# <<< Add the src path to sys.path
import sys
import os

containing_folder = os.path.dirname(os.path.abspath(__file__))
src_dir = os.path.abspath(os.path.join(containing_folder, os.pardir))
if src_dir not in sys.path:
    sys.path.insert(0, src_dir)
# >>>

import numpy as np

from corpora.synthetic import draw_dwz_from_ptd_pwt, get_pw_pt, draw_dirichlet
from corpora.structure import deltaI_from_nwd
from common.convert_states import state_nwjd, nwd_to_texts

def make_pwt_ptd_dirichlet(vec_alpha, vec_beta, D, seed=None):
    '''From Dirichlet hyperparameters, generate the conditional probabilities
    - p(t|d)
    - p(w|t)
    IN:
    - vec_alpha: hyperparameter for topic-doc distribution, len=K...number of topics
    - vec_beta: hyperparameter for word-topic distribution, len=V...number of word-types
    - D: number of documents, int
    OUT:
    - p_td:  p(t|d), shape=KxD
    - p_wt:  p(w|t), shape=VxK
    '''
    K = len(vec_alpha)
    V = len(vec_beta)

    # get seed_doc_list and seed_topic_list
    if seed is not None:
        np.random.seed(seed=seed)
        seed_doc_list = np.random.choice(D * D, size=D, replace=False)
        np.random.seed(seed=seed + 100)
        seed_topic_list = np.random.choice(K * K, size=K, replace=False)

    # # p_t_d from samples of alpha_vec
    p_td = np.zeros((K, D))
    for i_d in range(D):
        if seed is None:
            seed_doc = None
        else:
            seed_doc = seed_doc_list[i_d]
        p_td_tmp = draw_dirichlet(vec_alpha, seed=seed_doc)
        p_td[:, i_d] = 1.0 * p_td_tmp
    # # p_w_t from samples of beta_vec
    p_wt = np.zeros((V, K))
    for i_K in range(K):
        if seed is None:
            seed_topic = None
        else:
            seed_topic = seed_topic_list[i_K]
        p_wt_tmp = draw_dirichlet(vec_beta, seed=seed_topic)
        p_wt[:, i_K] = 1.0 * p_wt_tmp

    return p_td, p_wt

def make_hyper_vec(alpha, vec_p):
    '''make the hyperparameter vector in our notation.
    The resulting dirichlet-hyperparameter is given by:
    vec{alpha} = (alpha_1,alpha_2,...,alpha_S) with S = len(vec_p) with alpha_i = alpha*S*p_i.
    If we draw random variables vec{x}, the marginal expectation value is <x_i> = p_i

    IN:
    - alpha, float
    - vec_p = (p_i) with i=1,..,S
    OUT:
    - vec_alpha = (alpha_i) with i =1,...,S
    '''
    return alpha * len(vec_p) * vec_p

def synthetic_dirichlet_wrapper(dict_input, if_info=True):
    '''
    Wrapper for drawing synthetic corpus: DIRICHLET

    Input:
        dict_input = {
            ## choose topic model
            'corpustype': 'dirichlet', str

            ## corpus-size parameters
            , 'D':D, int  ;#of docs
              'V':V, int  ;#of word-types
              'm':m, int  ;textlength
              'K':K, int  ;#of topics

            ## structure parameters
              'alpha':alpha, float   ; concentration parameter for topic-doc distribution p_td
              'beta':beta, float   ; concentration parameter for word-topic distribution p_wt
            ## external contraints parameters
            'dist_w': dist_w, str ; global distribution of word-frequencies
                                    'uni','zipf'
            'dist_z': dist_z, str ; global distribution of topic-frequencies
                                    'uni','zipf'
        }

    Output:
        dict_output = {
            'state_dwz':[(d,w,z)]; list of 3-tuples for each drawn token (doc,word-type,topic-label)
            list_t_d_true:[ t_d]; topic-membership of each document
            'p_wt':p_wt; word-topic dist, arr.shape=VxK
            'p_td':p_td; topic-doc dist, arr.shape=KxD
        }
    '''

    # # get the size-parameters

    V = dict_input['V']
    K = dict_input['K']

    # # external contraints parameters
    dist_w = dict_input['dist_w']

    # # get the structure parameters
    alpha = dict_input['alpha']

    # # Parameters for document
    D = dict_input['D']
    m = dict_input['m']

    # # optional parameter
    beta = dict_input.get('beta', None)
    dist_t = dict_input.get('dist_t', None)
    seed = dict_input.get('seed', None)
    burstiness = dict_input.get('burstiness', None)

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

    dict_output = synthetic_dirichlet_terminal(V, K, dist_w, D, m, alpha, beta, dist_t=dist_t, seed=seed, burstiness=burstiness, if_info=if_info)

    return dict_output

def synthetic_dirichlet_terminal(V, K, dist_w, D, m, alpha, beta=None, dist_t=None, seed=None, burstiness=None, if_info=True):

    if dist_t is None:
        dist_t = dist_w
    if beta is None:
        beta = 1.0 * alpha

    # # global distribution of topic-size
    p_t = get_pw_pt(K, dist=dist_t)
    # # global distribution of word frequencies
    p_w = get_pw_pt(V, dist=dist_w)

    # # get vector-hyperparameters
    vec_alpha = make_hyper_vec(alpha, p_t)
    vec_beta = make_hyper_vec(beta, p_w)

    # # create the mixture-matrices p_wt (word-topic) and p_td (topic-doc)
    p_td, p_wt = make_pwt_ptd_dirichlet(vec_alpha, vec_beta, D, seed=seed)

    # # draw the dwz-state
    state_dwz, p_w_td = draw_dwz_from_ptd_pwt(p_td, p_wt, m, burstiness=burstiness)
    n_wd, n_wj, n_jd = state_nwjd(state_dwz, D, V, K)
    texts = nwd_to_texts(n_wd)
    # # infer the topic-membership of each doc:
    # # choose topic with largest contribution from p(t|d)
    list_t_d_true = np.argmax(p_td, axis=0)

    # # empirical p_t and p_w; otherwise p_tw is not normalized
    p_t_emp = 1.0 / D * np.sum(p_td, axis=1)
    p_w_emp = np.sum(p_wt * p_t_emp)

    # # infer the topic-membership of each word:
    # # choose topic with largest contribution from p(t|w) = p(w|t)*p(t)/p(w)
    # p_tw = (p_wt*(p_w[:,np.newaxis]/p_t)).T
    p_tw = (p_wt.T * (p_t_emp[:, np.newaxis] / p_w_emp))
    list_t_w_true = np.argmax(p_tw, axis=0)

    # # Get the structure
    if if_info:
        DeltaI, I_alpha = deltaI_from_nwd(n_wd)
    else:
        DeltaI = 0
        I_alpha = 0

    dict_out = {}
    dict_out['p_w'] = p_w
    # dict_out['V_t'] =V_t
    dict_out['word_topic_assign_list'] = list_t_w_true
    dict_out['p_t'] = p_t
    dict_out['p_wt'] = p_wt
    dict_out['document_topic_assign_list'] = list_t_d_true
    dict_out['p_td'] = p_td
    dict_out['state_dwz'] = state_dwz
    dict_out['n_wd'] = n_wd
    dict_out['n_wj'] = n_wj
    dict_out['n_jd'] = n_jd
    dict_out['texts'] = texts
    dict_out['p_tw'] = p_tw

    dict_out['DeltaI'] = DeltaI
    dict_out['I_alpha'] = I_alpha

    return dict_out


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

# # Prepare

import json
import re
from convlab2.util.multiwoz.multiwoz_slot_trans import *
en_pattern = re.compile('[a-zA-Z]')

vocab_dict_path = 'vocab_dict.json'
vocab_dict = json.load(open(vocab_dict_path))

# # MultiWOZ ontology translate

def capitalize(s):
    return s[0].upper() + s[1:]

def intent_translate(intent):
    return vocab_dict['intent_set'][intent.lower()]

def domain_translate(domain):
    return vocab_dict['domain_set'][domain.lower()]

def slot_translate(domain, slot):
    if slot == 'taxi_phone':
        slot = 'phone'
    elif slot == 'taxi_types':
        slot = 'car'

    try:
        if slot.lower() in vocab_dict['slot_set']:
            return vocab_dict['slot_set'][slot.lower()]
        elif capitalize(domain) in REF_USR_DA and slot.lower() in REF_USR_DA[capitalize(domain)]:
            return vocab_dict['slot_set'][REF_USR_DA[capitalize(domain)][slot.lower()].lower()]
        elif capitalize(domain) in REF_SYS_DA and capitalize(slot) in REF_SYS_DA[capitalize(domain)]:
            return vocab_dict['slot_set'][REF_SYS_DA[capitalize(domain)][capitalize(slot)].lower()]

    except:
        print(domain, slot)
        raise

def value_translate(domain, slot, value):
    value = value.strip().lower()
    error_tup = []
    if (domain, slot, value) in error_tup:
        print(f'    Skip mismatching tuple: {domain}-{slot}-{value}')
        return ''

    if domain.lower() == 'general':
        return value

    if domain not in vocab_dict['value_set']:
        domain = domain.lower()

    if slot not in vocab_dict['value_set'][domain]:
        slot = slot.lower()

    if slot not in vocab_dict['value_set'][domain] and slot.lower() in REF_USR_DA[capitalize(domain)]:
        slot = REF_USR_DA[capitalize(domain)][slot.lower()]

    if slot not in vocab_dict['value_set'][domain] and capitalize(slot) in REF_SYS_DA[capitalize(domain)]:
        slot = REF_SYS_DA[capitalize(domain)][capitalize(slot)]

    if slot not in vocab_dict['value_set'][domain]:
        if slot is None:
            slot = 'none'
    if slot not in vocab_dict['value_set'][domain]:
        print(f'{domain}-{slot} has no vocab dict')
        raise Exception

    if not en_pattern.findall(value):
        return value

    if not vocab_dict['value_set'][domain][slot]:
        return value

    if slot in ['post', 'postcode', 'phone', 'Ref', 'reference']:
        return value

    if value not in vocab_dict['value_set'][domain][slot]:
        if '|' in value:
            return '或'.join([value_translate(domain, slot, v) for v in value.split('|')])

        trans_value = {
            "king's college": "king 's college",
            "intermediate dependency area": "intermediate dependancy area"
            # "saint john's college": "saint johns college",
            # "kettle's yard": "kettles yard",
            # "christ's college": "christ college",
            # "little saint mary's church": "little saint marys church",
            # "people's portraits exhibition at girton college": "peoples portraits exhibition at girton college",
        }
        if value in trans_value:
            value = trans_value[value]
        elif value.replace("'", "") in vocab_dict['value_set'][domain][slot]:
            value = value.replace("'", "")
        elif value.replace("'s", "") in vocab_dict['value_set'][domain][slot]:
            value = value.replace("'s", "")
        elif value.replace('the ', '') in vocab_dict['value_set'][domain][slot]:
            value = value.replace('the ', '')

    try:
        return vocab_dict['value_set'][domain][slot][value]
    except:
        print(domain, slot, value)
        raise Exception

def ontology_translate(typ, *args):
    assert typ in ['intent', 'domain', 'slot', 'value'], 'Function translate() requires 1st argument: domain|slot|value.'
    if typ == 'intent':
        assert len(args) == 1, 'Needs 1 argument: intent.'
        return intent_translate(args[0])
    elif typ == 'domain':
        assert len(args) == 1, 'Needs 1 argument: domain.'
        return domain_translate(args[0])
    elif typ == 'slot':
        assert len(args) == 2, 'Needs 2 argument: domain, slot.'
        return slot_translate(args[0], args[1])
    elif typ == 'value':
        assert len(args) == 3, 'Needs 3 argument: domain, slot, value.'
        return value_translate(args[0], args[1], args[2])

if __name__ == '__main__':
    print(ontology_translate('intent', 'request'))
    print(ontology_translate('domain', 'police'))
    print(ontology_translate('slot', 'attraction', 'phone'))
    print(ontology_translate('value', 'attraction', 'area', 'central district'))


import re
import requests

from metgem.workers.base import BaseWorker

class CheckUpdatesWorker(BaseWorker):
    URL = "https://api.github.com/repos/metgem/metgem/releases/latest"

    def __init__(self, current_version, **kwargs):
        super().__init__(**kwargs)
        self._current_version = current_version

    def run(self):
        if self.isStopped():
            self.canceled.emit()
            return False

        if not self._current_version:
            return False

        try:
            r = requests.get(CheckUpdatesWorker.URL, timeout=.5)
        except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
            return False

        try:
            r.raise_for_status()
        except requests.HTTPError:
            return False

        if self.isStopped():
            self.canceled.emit()
            return False

        if r:
            json = r.json()
            tag_name = json.get("tag_name")
            if not tag_name:
                return False

            new_version = re.sub("[^0-9.]", "", tag_name)
            if not new_version or new_version <= self._current_version:
                return False

            release_notes = json.get("body")
            url = json.get("html_url")
            assets = json.get("assets")

            if not self.isStopped():
                if assets:
                    return new_version, release_notes, url
                else:
                    return False
            else:
                self.canceled.emit()


import os
import re
import time
from urllib import request
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#  获得某一个关键词下的所有页面的链接的url
def get_sub_page_url(browser, url):
    browser.get(url)
    time.sleep(2)
    # input_box = WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.XPATH,
    #                                                                             '/html/body/div/form/div[2]/input[1]')))
    input_box = browser.find_element_by_xpath('/html/body/div/form/div[2]/input[1]')
    print(input_box)

    # 输入搜索内容
    try:
        input_box.send_keys('21世纪马克思主义')
        browser.find_element_by_xpath('/html/body/div/form/div[2]/input[2]').click()
    except:
        pass

    next = 1
    all_url = []
    while next:
        time.sleep(1)
        page_element_list = browser.find_elements_by_xpath('//td/div/a[contains(@target, "_blank")]')
        for item in page_element_list:
            all_url.append(item.get_attribute("href"))

        # 换页操作
        try:
            next_page = browser.find_element_by_link_text('下页')
            next_page.click()
            time.sleep(3)
        except:
            next_page = None
            break

    return all_url

#  获得子页的所有文本内容
def get_text_from_page(browser, all_url):
    # try:
    i = 0
    for item in all_url:
        browser.get(item)
        title = browser.find_elements_by_xpath('//h1[contains(@class, "js-title")]')
        i += 1
        print(i)
        text_page = browser.find_elements_by_xpath('//p')
        try:
            if title[0].text == '':
                with open(r'E:\21世纪马克思主义\中国青年报\{}.txt'.format(all_url[i]), 'w', encoding="utf-8") as f:
                    for it in text_page:
                        f.write('    ')
                        f.write(it.text)
                        f.write('\n')
            else:
                with open(r'E:\21世纪马克思主义\中国青年报\{}.txt'.format(title[0].text), 'w', encoding="utf-8") as f:
                    for it in text_page:
                        f.write('    ')
                        f.write(it.text)
                        f.write('\n')
        except:
            pass
        time.sleep(3)

if __name__ == "__main__":
    # 需要先print出所有页面的url后，再将url放到页面中，再查找页面内容
    url = "http://sou.cyol.com/servlet/SearchServlet.do"
    browser = webdriver.Chrome()
    options = webdriver.ChromeOptions()
    options.binary_location = r"D:\Google Chrome\Chrome\Application\chrome.exe"
    all_url = get_sub_page_url(browser, url)
    all_url = set(all_url)
    print(all_url)
    print(len(all_url))
    get_text_from_page(browser, all_url)
    browser.close()


import argparse
from my_dataset import MyDataSet as data
from model import UFUformer as net
import cv2
import os
import random
import numpy as np
from tensorboardX import SummaryWriter
from torch.optim.lr_scheduler import StepLR
import torch
import torch.nn as nn
import torchvision.utils as vutils
from torch.utils.data import DataLoader
from tqdm import tqdm

ap=argparse.ArgumentParser()
ap.add_argument('-b','--batch',help='the number of batch',type=int,default='4')
ap.add_argument('-e','--epoch',help='the number of training',type=int,default='500')
ap.add_argument('-r','--resume',help='the choice of resume',type=bool,default=False)

args=vars(ap.parse_args())

def log_images(writer, img, out,ll256,gt, it):

    images_array = vutils.make_grid(img).to('cpu')
    out_array = vutils.make_grid(out * 255).to('cpu').detach()
    ll256_array = vutils.make_grid(ll256 * 255).to('cpu').detach()
    gt = vutils.make_grid(gt).to('cpu')

    writer.add_image('input', images_array, it)
    writer.add_image('out', out_array, it)
    writer.add_image('ll256',ll256_array,it)
    writer.add_image('gt',gt,it)

net=net()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

if args['resume']:
    files = '.\\checkpoint\\checkpoint_255_epoch.pkl'
    assert os.path.isfile(files), "{} is not a file.".format(args['resume'])
    state = torch.load(files)
    net.load_state_dict(state['model'])
    iteration = state['epoch'] + 1
    optimizer = state['optimizer']
    print("Checkpoint is loaded at {} | epochs: {}".format(args['resume'], iteration))
else:
    iteration = 0

da=data('D:\\SZ\\UIEB')
dataloder=DataLoader(da,batch_size=args['batch'],shuffle=True)

optimizer=torch.optim.Adam(lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0,params=net.parameters())
scheduler = StepLR(optimizer, step_size=150, gamma=0.5)

def proimage(im):
    images = im[image_idx, :, :, :].clone().detach().requires_grad_(False)
    image = torch.transpose(images, 0, 1)
    image = torch.transpose(image, 1, 2).cpu().numpy() * 255
    return image

writer = SummaryWriter(comment='test_comment', filename_suffix="test_suffix")
j = 0
h = 0
for iter in range(iteration,args['epoch']):
    print(iter)
    prob = tqdm(enumerate(dataloder),total=len(dataloder))
    if iter < 1000:
        L1 = nn.MSELoss()
    else:
        L1 = nn.L1Loss()
    for i,data in prob:
        gt=torch.tensor(data[0].numpy(),requires_grad=True,device='cuda')
        raw=torch.tensor(data[1].numpy(),requires_grad=True,device='cuda')
        net.to('cuda')
        a=net(raw)
        L1loss = L1(a,gt)
        loss = L1loss
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        prob.set_postfix(Loss1=L1loss)
        h = h+1
        writer.add_scalar('loss',loss,h)

        c = a
        if i % 100 ==0:
            j += 100
            image_idx =random.randint(0, 0)
            predi=c[image_idx,:,:,:].clone().detach().requires_grad_(False)
            predi=torch.transpose(predi,0,1)
            predi=torch.transpose(predi,1,2).cpu().numpy()*255
            gti=proimage(gt)
            rawi=proimage(raw)
            image=np.concatenate((rawi,predi,gti),axis=1)
            image_name='sample12' + "/out" + str(iter)+'_'+str(i) + ".png"
            cv2.imwrite(image_name,image)

    if (iter + 1) % 1 == 0:
        checkpoint = {"model": net.state_dict(),
                      "optimizer": optimizer,
                      "epoch": iter}

        if not os.path.exists('checkpoint'):
            os.mkdir('checkpoint')
        path_checkpoint = "checkpoint/checkpoint_{}_epoch.pkl".format(iter)
        torch.save(checkpoint, path_checkpoint)

    scheduler.step()
    print(optimizer.param_groups[0]['lr'])


import pygame.camera
import pygame.image
import sys
import cv2
import time
from VideoCapture import Device

cam = Device()
cam.saveSnapshot('image.jpg')
cam.setResolution(160,120)

# grab first frame
colorimg = cam.getImage()
img = colorimg.convert('L')
WIDTH = img.size[0]
HEIGHT = img.size[1]
screen = pygame.display.set_mode( ( WIDTH*2, HEIGHT*2 ) )
pygame.display.set_caption("pyGame Camera View")
average = img
diff = img

while True :
    time.sleep(0.05)
    for e in pygame.event.get() :
        if e.type == pygame.QUIT :
            sys.exit()
    rgbimg = img.convert("RGB")
    mode = rgbimg.mode
    size = rgbimg.size
    data = rgbimg.tostring()
    #print mode
    #assert mode in "RGB", "RGBA"
    surface = pygame.image.fromstring(data, size, mode)

    rgbimg = average.convert("RGB")
    mode = rgbimg.mode
    size = rgbimg.size
    data = rgbimg.tostring()
    #print mode
    #assert mode in "RGB", "RGBA"
    surface2 = pygame.image.fromstring(data, size, mode)

    rgbimg = diff.convert("RGB")
    mode = rgbimg.mode
    size = rgbimg.size
    data = rgbimg.tostring()
    #print mode
    #assert mode in "RGB", "RGBA"
    surface3 = pygame.image.fromstring(data, size, mode)

    rgbimg = colorimg
    mode = rgbimg.mode
    size = rgbimg.size
    data = rgbimg.tostring()
    #print mode
    #assert mode in "RGB", "RGBA"
    surface4 = pygame.image.fromstring(data, size, mode)

    # draw frame
    screen.blit(surface, (0,0))
    screen.blit(surface2, (WIDTH,HEIGHT))
    screen.blit(surface3, (0,HEIGHT))
    screen.blit(surface4, (WIDTH,0))

    pygame.display.flip()
    # grab next frame
    lastimg = img
    diff = lastimg
    colorimg = cam.getImage()
    img = colorimg.convert('L')
    for x in range(0, WIDTH):
        for y in range(0,HEIGHT):
            average.putpixel((x,y),(average.getpixel((x,y))*8 +  img.getpixel((x,y))*2)/10.0)

    for x in range(0, WIDTH):
        for y in range(0,HEIGHT):
            diff.putpixel( (x,y),  abs(lastimg.getpixel((x,y)) - img.getpixel((x,y))))


from django.shortcuts import render

from api.serializers import BookSerializer
from api.models import Books
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from rest_framework.decorators import action
from api.serializers import UserSerializer
from django.contrib.auth.models import User
from rest_framework import authentication,permissions

class BooksView(ModelViewSet):
    # authentication_classes=[authentication.TokenAuthentication]
    permission_classes=[permissions.IsAuthenticated]

    serializer_class=BookSerializer
    model=Books
    queryset=Books.objects.all()

    @action(methods=["GET"],detail=False)
    def genre(self,request,*args,**kwargs):
        qs=Books.objects.all().values_list("genre",flat=True).distinct()
        return Response(data=qs)

    def get_queryset(self):
        qs=Books.objects.all()

        if "genre" in self.request.query_params:
            cat=self.request.query_params.get("genre")
            qs=qs.filter(genre=cat)
            return qs

        if "price_gt" in self.request.query_params:
            price=self.request.query_params.get("price_gt")
            qs=qs.filter(price__gt=price)
            return qs

        return qs

class UserView(ModelViewSet):
    serializer_class=UserSerializer
    queryset=User.objects.all()

    # def create(self, request, *args, **kwargs):
    #     serializer=UserSerializer(data=request.data)
    #     if serializer.is_valid():
    #         usr=User.objects.create_user(**serializer.validated_data)
    #         serializer=UserSerializer(usr,many=False)
    #         return Response(data=serializer.data)
    #     else:
    #         return Response(data=serializer.errors)


import os
import logging
import requests
import re

from sys import exit

URL_DUCK_DNS = 'https://www.duckdns.org/update?domains=${sub_domains}.duckdns.org&token=${token}&ip=${ip}'
PAGE_MY_IP ='https://www.cual-es-mi-ip.net/'
try:
    PATH_LOGS=os.environ["UPDATE_DUCK_DNS_LOGS"]
except KeyError:
    print('log path not found creating in default folder')

try:
    logging.basicConfig(
        filename=PATH_LOGS+'/updateDuckDNS.log',
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s',
        datefmt='%d/%m/%Y %I:%M:%S %p')
except:
    logging.basicConfig(
        filename='/tmp/updateDuckDNS.log',
        level=logging.DEBUG,
        format='%(asctime)s - %(levelname)s - %(message)s',
        datefmt='%d/%m/%Y %I:%M:%S %p')

logging.info('Started')

try:
    TOKEN = os.environ["UPDATE_DUCK_DNS_TOKEN"]
    SUB_DOMAINS = os.environ["UPDATE_DUCK_DNS_SUB_DOMAIN"]
    URL_DUCK_DNS = URL_DUCK_DNS.replace('${sub_domains}', SUB_DOMAINS)
    URL_DUCK_DNS = URL_DUCK_DNS.replace('${token}', TOKEN)
except KeyError:
    logging.warning('there are no environment variables')
    exit()

page_link ='https://domains.google.com/checkip'
page_response = requests.get(page_link, timeout=5)
my_ip = page_response.text

logging.info("My IP: " + my_ip)

try:
    PATH_INSTALL=os.environ["PATH_INSTALL_SCRIPT_PYTHON_DUCK_DNS"]
except Exception:
    logging.warning('not found enviroment PATH_INSTALL_SCRIPT_PYTHON')

replace_ip = True
try:
    if(os.path.isfile(PATH_INSTALL+"/my_ip.txt") == True):
        file = open(PATH_INSTALL+"/my_ip.txt", "r+")
        ip_file = file.read()
        logging.info('my_ip.txt: ' + ip_file.strip())
        if(ip_file.strip() != my_ip.strip()):
            ip_file = re.sub(ip_file.strip(), my_ip.strip(), ip_file)
            file.seek(0)
            file.write(ip_file)
            file.close()
            logging.info('update ip')
        else:
            replace_ip = False
            logging.info('not update ip')
    else:
        logging.info('create file my_ip.txt')
        file = open(PATH_INSTALL+"/my_ip.txt","w")
        file.write(my_ip)
        file.close()

except Exception as ex:
    logging.warning(ex)
    logging.warning('not found file my_ip.txt')
    exit()

if (replace_ip):
    URL_DUCK_DNS = URL_DUCK_DNS.replace('${ip}', my_ip)
    logging.info("DUCK_URL: " + URL_DUCK_DNS)

    page_response = requests.get(URL_DUCK_DNS, timeout=5)

    logging.info("Response: " + page_response.text)

logging.info('Finished')


import logging
import os
import shutil
from pathlib import Path

from PIL import Image

# import statistics
from scipy import spatial
from tqdm import tqdm

from ..models.slide_classifier import inference
from .helpers import make_dir_if_not_exist

logger = logging.getLogger(__name__)

class SegmentCluster:
    """Iterates through frames in order and splits based on large visual differences
    (measured by the cosine difference between the feature vectors from the slide classifier)"""

    def __init__(self, slides_dir, model_path="model_best.ckpt"):
        self.slides_dir = Path(slides_dir)
        self.slides_list = sorted(os.listdir(self.slides_dir))
        self.model = inference.load_model(model_path)
        self.change_indexes = None

    def extract_and_add_features(self, gamma=1.08):
        """Extracts features from the images in ``slides_dir`` and saves feature vectors"""
        num_slides = len(self.slides_list)

        all_features = []
        logger.info("Extracting features from " + str(num_slides) + " slides")
        for idx, slide in tqdm(
            enumerate(self.slides_list),
            total=num_slides,
            desc="> AI Segmenting Engine: Feature extraction",
        ):
            current_slide_path = os.path.join(self.slides_dir, slide)
            _, _, _, extracted_features = inference.get_prediction(
                self.model,
                Image.open(current_slide_path),
            )
            all_features.append(extracted_features)

        # spatial.distance.cosine computes the cosine difference, a larger value means more different
        # and a smaller value means more similar
        similarities = [
            spatial.distance.cosine(all_features[i - 1], all_features[i])
            for i in range(1, len(all_features))
        ]
        mean_similarities = sum(similarities) / len(similarities)
        # std_similarities = statistics.pstdev(similarities)

        # A larger `sim_compare_value` = less segments
        # A larger divisor = more segments
        sim_compare_value = mean_similarities * gamma

        change_indexes = [
            i
            for i in range(0, len(similarities))
            if similarities[i] > sim_compare_value
        ]

        self.change_indexes = change_indexes
        return change_indexes

    def transfer_to_filesystem(self, copy=True, create_best_samples_folder=True):
        """Takes all images in directory ``slides_dir`` and saves each cluster to a subfolder in ``cluster_dir`` (directory in parent of ``slides_dir``)"""
        cluster_dir = (
            self.slides_dir.parents[0] / "slide_clusters"
        )  # cluster_dir = up one directory from slides_dir then into "slide_clusters"

        if create_best_samples_folder:
            best_samples_path = cluster_dir / "best_samples"
            make_dir_if_not_exist(best_samples_path)

        current_folder = 0
        current_cluster_path = cluster_dir / str(current_folder)
        for idx, slide in enumerate(self.slides_list):
            current_slide_path = os.path.join(self.slides_dir, slide)
            if idx in self.change_indexes:
                current_folder += 1
                if create_best_samples_folder:
                    if copy:
                        shutil.copy(str(current_slide_path), str(best_samples_path))
                    else:
                        shutil.move(str(current_slide_path), str(best_samples_path))
            current_cluster_path = cluster_dir / str(current_folder)

            make_dir_if_not_exist(current_cluster_path)

            if copy:
                shutil.copy(str(current_slide_path), str(current_cluster_path))
            else:
                shutil.move(str(current_slide_path), str(current_cluster_path))

        return cluster_dir, best_samples_path

# seg = SegmentCluster("slide")
# seg.extract_and_add_features()
# seg.transfer_to_filesystem()

# TODO
# 1. Matrix of similarity/difference scores to remove the most similar images (in the case that the presenter switches back and forth between two things, thus producing duplicates)
# 2. Test if model features for same slide as screen capture vs camera are the same
# 3. If in the above step the model features are difference then change the dataset so that all screen captures are in the slides class and all cameras are in the presenter_slide class


# -*- coding: utf-8 -*-
import itertools

from cabbage.utils import ExponentialBackoff, FibonaccianBackoff

class TestExponentialBackoff:

    def test_double(self):
        backoff = ExponentialBackoff(1, 2, 2048)
        assert list(itertools.islice(backoff, 15)) == [
            1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 2048, 2048, 2048
        ]

class TestFibonaccianBackoff:

    def test_maxprecision(self):
        backoff = FibonaccianBackoff(1000)
        assert list(itertools.islice(backoff, 15)) == [
            1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
        ]

    def test_1min(self):
        backoff = FibonaccianBackoff(60)
        assert list(itertools.islice(backoff, 15)) == [
            1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 60, 60, 60, 60, 60
        ]


from django import template
from eulfedora.util import RequestFailed, PermissionDenied

register = template.Library()

class CatchFedoraErrorsNode(template.Node):
    """Template render node for catching fedora access errors and providing
    fallback content."""
    def __init__(self, fedora_access, permission_denied=None,
                 fedora_failed=None):
        self.fedora_access = fedora_access
        self.permission_denied = permission_denied
        self.fedora_failed = fedora_failed

    def render(self, context):
        try:
            return self.fedora_access.render(context)
        except PermissionDenied:
            if self.permission_denied is not None:
                return self.permission_denied.render(context)
            elif self.fedora_failed is not None:
                return self.fedora_failed.render(context)
            else:
                return ''
        except RequestFailed:
            if self.fedora_failed is not None:
                return self.fedora_failed.render(context)
            else:
                return ''

@register.tag(name='fedora_access')
def do_catch_fedora_errors(parser, token):
    """Catches fedora errors between ``{% fedora_access %}`` and
    ``{% end_fedora_access %}``. Template designers may specify
    optional ``{% permission_denied %}`` and ``{% fedora_failed %}``
    sections with fallback content in case of permission or other errors
    while rendering the main block.

    Note that when Django's ``TEMPLATE_DEBUG`` setting is on, it precludes
    all error handling and displays the Django exception screen for all
    errors, including fedora errors, even if you use this template tag. Turn
    off ``TEMPLATE_DEBUG`` if that debug screen is getting in the way of the
    use of {% fedora_access %}.
    """

    END_TAGS = ('end_fedora_access',
                'permission_denied', 'fedora_failed')

    blocks = {}
    blocks['fedora_access'] = parser.parse(END_TAGS)
    token = parser.next_token()
    while token.contents != 'end_fedora_access':
        # need to convert token.contents manually to a str. django gives us
        # a unicode. we use it below in **blocks, but python 2.6.2 and
        # earlier can't use **kwargs with unicode keys. (2.6.5 is ok with
        # it; not sure about intervening versions.) in any case, direct
        # conversion to string is safe here (i.e., no encoding needed)
        # because the parser guarantees it's one of our END_TAGS, which are
        # all ascii.
        current_block = str(token.contents)
        if current_block in blocks:
            raise template.TemplateSyntaxError(
                current_block + ' may appear only once in a fedora_access block')

        blocks[current_block] = parser.parse(END_TAGS)
        token = parser.next_token()

    return CatchFedoraErrorsNode(**blocks)


from django.shortcuts import render
# Create your views here.

from django.core.files.storage import FileSystemStorage

from keras.models import load_model
from keras.preprocessing import image
import tensorflow as tf
import json
from tensorflow import Graph
import numpy as np

img_height, img_width=224,224
with open('./models/imagenet_classes.json','r') as model:
    modelLabel=model.read()

modelLabel=json.loads(modelLabel)

model_graph = Graph()
with model_graph.as_default():
    tf_session = tf.compat.v1.Session()
    with tf_session.as_default():
        model=load_model('./models/MobileNetModelImagenet.h5')

def index(request):
    context={'a':1}
    return render(request,'index.html',context)

def predictImage(request):
    print (request)
    print (request.POST.dict())
    file=request.FILES['filePath']
    fileSave=FileSystemStorage()
    filePathName=fileSave.save(file.name,file)
    filePathName=fileSave.url(filePathName)
    imageSetup ='.'+filePathName
    img = image.load_img(imageSetup, target_size=(img_height, img_width))
    imgOutput  = image.img_to_array(img)
    imgOutput=imgOutput/255
    imgOutput=imgOutput.reshape(1,img_height, img_width,3)
    with model_graph.as_default():
        with tf_session.as_default():
            prediction=model.predict(imgOutput)

    predictedImage=modelLabel[str(np.argmax(prediction[0]))]

    context={'filePathName':filePathName,'predictedLabel':predictedImage[1]}
    return render(request,'index.html',context)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 10:03:14 2017

@author: tkoller
"""

import argparse

from safe_exploration.utils_config import loadConfig, create_env, create_solver, get_model_options_from_conf
from safe_exploration.exploration_runner import run_exploration
from safe_exploration.episode_runner import run_episodic
from safe_exploration.uncertainty_propagation_runner import run_uncertainty_propagation

def create_parser():
    """ Create the argparser """

    parser = argparse.ArgumentParser(description=""" Library for MPC-base Safe Exploration
    of unknown Dynamic Systems using Gaussian Processes \n\n

    Default configurations for the following scenarios exist:

    """)
    parser.add_argument("--scenario_config",
                        default= "journal_experiment_configs/dynamic_expl_inv_pend_nsafe=2_nperf=0_r=1_beta_safe=2.py", type = str,
                        help= """ Create your own scenario by copying one of the scenarios
                        in the example_configs/ directory and changing the default options.\n
                        Default scenario is dynamic_expl_inv_pend_nsafe=2_nperf=0_r=1_beta_safe=2.py (see above for explanation) """)
    return parser

def check_config_conflicts(conf):
    """ Check if there are conflicting options in the Config

    Parameters
    ----------
    conf: Config
        The config file

    Returns
    -------
    has_conflict: Bool
        True, if there is a conflict in the config
    conflict_str: String
        The error message

    """

    has_conflict = False
    conflict_str = ""
    if conf.task == "exploration" and not conf.solver_type == "safempc":
        return True, "Exploration task only allowed with safempc solver"
    elif conf.task == "uncertainty_propagation" and not conf.solver_type == "safempc":
        return True, "Uncertainty propagation task only allowed with safempc solver"

    return has_conflict, conflict_str

def run_scenario(args):
    """ Run the specified scenario

    Parameters
    ----------
    args:
        The parsed arguments (see create_parser for details)
    """
    config_path = args.scenario_config
    conf = loadConfig(config_path)

    conflict, conflict_str = check_config_conflicts(conf)
    if conflict:
        raise ValueError("There are conflicting settings: {}".format(conflict_str))

    env = create_env(conf.env_name, conf.env_options)

    solver, safe_policy = create_solver(conf, env)

    task = conf.task
    if task == "exploration":
        run_exploration(conf, conf.visualize)#,conf.static_exploration,conf.n_iterations,
                        #conf.n_restarts_optimizer,conf.visualize,conf.save_vis,conf.save_path,conf.verify_safety,conf.n_experiments)
    elif task == "episode_setting":
        run_episodic(conf)

    elif task == "uncertainty_propagation":
        run_uncertainty_propagation(env, solver, conf)

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


import json
import os
import base64
import string
import random
import boto3

QUEUE_URL = os.environ['QUEUE_URL']

def lambda_handler(event,context):
    content = "
Hello there "+event['headers']['x-forwarded-for']+" using "+event['headers']['user-agent']+"

... my favorite food is "+os.environ['MY_CONSTANT']+"
"
    print("Hello there", format(event['headers']['x-forwarded-for']), "using ", format(event['headers']['user-agent']), "... my favorite food is ", os.environ['MY_CONSTANT'])
    post_content = event['headers']['x-forwarded-for']+" /// "+event['headers']['user-agent']
    sqs = boto3.client('sqs')

    try:
        response = sqs.send_message(
            QueueUrl=QUEUE_URL,
            MessageBody=str(post_content),
            DelaySeconds=0
        )
        print (response)
    except Exception as e:
        raise IOError(e)

    return {
        'body': content,
        'headers': {
            'Content-Type': 'text/html'
        },

        'statusCode': 200
    }


from tkinter import *
from tkinter import ttk
from tkcalendar import DateEntry
import ghasedakpack
import datetime
def send_sms():
    # create api key
        sms = ghasedakpack.Ghasedak("39a79a573ba0e90ee353b76acb178b8d33ee0626ed96cd7cb9ec984cf2612ac6")
        date_today=datetime.date.today()
        # create formate date
        date_today = str(int(date_today.strftime("%d")))+"/"+str(int(date_today.strftime("%m")))+"/"+date_today.strftime("%Y")

        date_user=edate.get()
        if date_user==date_today:

            sms.send({ 'message':etext.get(),  'receptor' :eadress.get() })
            # زمان‌بندی فراخوانی تابع send_sms() هر یک دقیقه
        window.after(60000, send_sms)

window=Tk()
window.configure(bg="green")
window.geometry("500x600")
window.title("sms panel")
window.resizable(width=False,height=False)
Label(window,text="Sms Panel", fg="black",bg="yellow",cursor="arrow",width=71,height=2).place(x='5',y='1')
adress=Label(window,bg="green" ,text="  number phone    :  ")
adress.place(x='80',y='100')
eadress=Entry(window)
eadress.place(x='200',y='100')

text=Label(window,bg="green",text=" Enter your Message :  ")
text.place(x='80',y='180')
etext=Entry(window)
etext.place(x='200',y='180')

text=Label(window,text=" Enter your date :  ",bg="green")
text.place(x='80',y='260')
edate = DateEntry(window)
edate.place(x='200',y='260')

Button(window,text="send",command=send_sms,width=5,height=2).place(x='240',y='350')
window.after(60000, send_sms)  # زمان‌بندی اولیه فراخوانی تابع send_sms() هر یک دقی
window.mainloop()


import PySimpleGUI as sg
import cv2
from ImageEditing import ChangeBrightness,ChangeSaturation,ImageSharpen,ImageFlip
from ImageFilters import OilPainting, Water , BlackAndWhite,GrayScale
'''
Requirements
    - PySimpleGUI
    - OpenCV (specifically pip install opencv-contrib-python, if error persists uninstall both opencv-contrib-python  and opencv-python(if installed) and "pip install --no-cache-dir opencv-contrib-python")

'''

def main():

    # sg.theme("Reddit")
    sg.theme("DarkBrown2")
    column = [
        sg.Column([
            [
            sg.Radio("Oil Painting", "Radio", size=(10, 1), key="-OIL-",font=("Cascadia Code SemiBold", 12)),
            sg.Slider(
                (1, 50),
                # add 50 ticks to the slider
                tick_interval=0.1,
                orientation="h",
                size=(20, 15),
                key="-OIL SLIDER SIZE-",
            ),
            sg.Slider(
                (1, 100),
                tick_interval=0.1,
                orientation="h",
                size=(20, 15),
                key="-OIL SLIDER LEVEL-",
            )
            ],
            [
                sg.Radio("Water Color", "Radio", size=(10, 1), key="-WATER-",font=("Cascadia Code SemiBold", 12)),
                sg.Slider(
                    (1, 400),
                    orientation="h",
                    size=(20, 15),
                    key="-WATER SLIDER SIZE-",
                ),
                sg.Slider(
                    (1, 100),
                    orientation="h",
                    size=(20, 15),
                    key="-WATER SLIDER LEVEL-",
                )
            ],
            [
                sg.Radio("Brightness", "Radio", size=(10, 1), key="-BRIGHTNESS-",font=("Cascadia Code SemiBold", 12)),
                sg.Slider(
                    (0, 100),
                    orientation="h",
                    size=(40, 15),
                    key="-BRIGHTNESS SLIDER-",
                ),
            ],
            [
                sg.Radio("Saturation", "Radio", size=(10, 1), key="-SATURATION-",font=("Cascadia Code SemiBold", 12)),
                sg.Slider(
                    (0, 100),
                    orientation="h",
                    size=(20, 15),
                    key="-SATURATION SLIDER-",
                )
            ],
            [
                sg.Radio("Blur", "Radio", size=(10, 1), key="-BLUR-",font=("Cascadia Code SemiBold", 12)),
                sg.Slider(
                    (1, 100),
                    orientation="h",
                    size=(40, 15),
                    key="-BLUR SLIDER-",
                ),
            ],
            [
                sg.Radio("Hue", "Radio", size=(10, 1), key="-HUE-",font=("Cascadia Code SemiBold", 12)),
                sg.Slider(
                    (0, 225),
                    orientation="h",
                    size=(40, 15),
                    key="-HUE SLIDER-",
                ),
            ],
            [
                sg.Radio("enhance", "Radio", size=(10, 1), key="-ENHANCE-",font=("Cascadia Code SemiBold", 12)),
                sg.Slider(
                    (1, 255),
                    orientation="h",
                    size=(40, 15),
                    key="-ENHANCE SLIDER-",
                ),
            ],
            [
                sg.Radio("GrayScale", "Radio", size=(10, 1), key="-GRAYSCALE-", font=("Cascadia Code SemiBold", 12)),
            ],

            [sg.Button("Exit", size=(10, 1), font=("Cascadia Code SemiBold", 12))],
    ],scrollable=True,vertical_scroll_only=True)
]

    # Define the window layout
    layout = [
        [
        sg.Column(
            [
                [sg.Text("Image Editing", font=("Cascadia Code SemiBold", 25))],
                [sg.Image(filename="", key="-IMAGE-", size=(800, 800))]],element_justification='center')
            ]
            ,
            column
        ]

    # Create the window and show it without the plot
    window = sg.Window("OpenCV Integration", layout, location=(400, 0),grab_anywhere=True,resizable=True,size=(600, 1000) )

    cap = cv2.VideoCapture(0)

    while True:
        event, values = window.read(timeout=20)

        if event == "Exit" or event == sg.WIN_CLOSED:
            break

        ret, frame = cap.read()

        if values["-BRIGHTNESS-"]:
            frame = ChangeBrightness(frame, values["-BRIGHTNESS SLIDER-"])
        elif values["-SATURATION-"]:
            frame = ChangeSaturation(frame, values["-SATURATION SLIDER-"])
        elif values["-BLUR-"]:
            frame = ImageSharpen(frame, values["-BLUR SLIDER-"])
        elif values["-HUE-"]:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            frame[:, :, 0] += int(values["-HUE SLIDER-"])
            frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
        elif values["-ENHANCE-"]:
            enh_val = values["-ENHANCE SLIDER-"] / 40
            clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8))
            lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
            lab[:, :, 0] = clahe.apply(lab[:, :, 0])
            frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
        elif values["-OIL-"]:
            frame = OilPainting(
                frame,
                values["-OIL SLIDER SIZE-"],
                values["-OIL SLIDER LEVEL-"],
            )
        elif values["-WATER-"]:
            frame = Water(
                frame,
                NeighbourhoodSize = values["-WATER SLIDER SIZE-"],
                Range = values["-WATER SLIDER LEVEL-"]
            )
        elif values["-GRAYSCALE-"]:
            frame = GrayScale(frame)
        imgbytes = cv2.imencode(".png", frame)[1].tobytes()
        window["-IMAGE-"].update(data=imgbytes)

    window.close()

if __name__ == "__main__":
    main()


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

import cv2 as cv

#读取图像
src=cv.imread('example.png')

#namedWindow函数，用于创建一个窗口
cv.namedWindow('input_image')

#指定窗口显示图像，‘input_image'：窗口名；src：要显示的图像
cv.imshow('input_image', src)

#翻转
h_flip = cv.flip(src, 1)#水平翻转
v_flip = cv.flip(src, 0)#垂直翻转
hv_flip = cv.flip(src, -1)#水平垂直翻转

#保存图像
cv.imwrite("v_flip.jpg", v_flip)
cv.imwrite("h_flip.jpg", h_flip)
cv.imwrite("hv_flip.jpg", hv_flip)

#绑定键盘，0指永远
cv.waitKey(0)

#删除所有窗口，可用窗口名做参数表示删除指定窗口
cv.destroyAllWindows()


# Input data paths

data_dir = "./data"
covariates = f"{data_dir}/Covariates_simulation.csv"
thickness = f"{data_dir}/CorticalMeasuresENIGMA_ThickAvg.csv"
volume = f"{data_dir}/SubcorticalMeasuresENIGMA_VolAvg.csv"
output_dir = f"./output"

nj = -1

#-----------------------------------------------------------------------------------------------------------------

# Import dependencies

import pandas as pd
import numpy as np
from joblib import Parallel, delayed
import warnings
import networkx as nx
from scipy import stats
from sklearn.linear_model import LinearRegression
import logging
import argparse

logging.basicConfig(filename=f"{output_dir}/log.txt",
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.DEBUG,
    datefmt='%Y-%m-%d %H:%M:%S %Z')
existing = []
def log_func(existing, var_dict):
	new = {key:type(value) for key, value in var_dict.items() if key not in existing}
	existing.extend([key for key in var_dict if key not in existing])
	return new, existing
#-----------------------------------------------------------------------------------------------------------------

# Assemble dataframe for analyses and screen subjects

demographics = pd.read_csv(covariates, index_col="SubjID")
dashless_columns = pd.Series(demographics.columns.str.split('_')).apply(lambda x: ''.join(str.capitalize(s) for s in x))
demographics.rename(columns=dict(zip(demographics.columns, dashless_columns)), inplace=True)

demographics.loc[demographics.Subtype.isna(), 'Subtype'] = 0
demographics['Minor'] = np.int32(demographics.Age<18.)
demographics['Durill3'] = np.int32(demographics.Durill<3.)
demographics['Age2'] = demographics['Age']**2

thickness_volume = pd.read_csv(thickness, index_col="SubjID", usecols=lambda x: x!='ICV').merge(pd.read_csv(volume, index_col="SubjID"), left_index=True, right_index=True)
dashless_columns = pd.Series(thickness_volume.columns.str.split('_')).apply(lambda x: ''.join(str.capitalize(s) for s in x))
thickness_volume.rename(columns=dict(zip(thickness_volume.columns, dashless_columns)), inplace=True)

macroscale_indices = ['Lthickness', 'Rthickness', 'Lsurfarea', 'Rsurfarea', 'Icv']
demographics = demographics.merge(thickness_volume[macroscale_indices], left_index=True, right_index=True)
thickness_volume = thickness_volume.drop(macroscale_indices, axis=1)

covar_regressors = ['Age', 'Hand']
to_keep = np.all(~demographics[covar_regressors].isna(), axis=1)
demographics = demographics.loc[to_keep, :]
thickness_volume = thickness_volume.loc[to_keep, :]

demographics.to_csv(f"{data_dir}/Demographics.csv")
thickness_volume.to_csv(f"{data_dir}/CT_Volume.csv")

logging.debug(f"DATAFRAME CREATED\n{log_func(existing, locals())[0]}\n\n")
#-----------------------------------------------------------------------------------------------------------------

# Correct for covariates

def covar_correct(X, Y, data):

    def get_resid(X, y, data):
        r = data[y] - LinearRegression(n_jobs=1).fit(data[X], data[y]).predict(data[X])
        return r

    R = Parallel(n_jobs=nj)(delayed(get_resid)(X, y, data) for y in Y)
    return np.asanyarray(R).T

demographics = pd.read_csv(f"{data_dir}/Demographics.csv", index_col="SubjID")
thickness_volume = pd.read_csv(f"{data_dir}/CT_Volume.csv", index_col="SubjID")
brain_regions = thickness_volume.columns

data = demographics[['Age', 'Hand']].merge(thickness_volume, left_index=True, right_index=True)

X = ['Age', 'Hand']
Y = brain_regions

residuals = thickness_volume.copy()
residuals[brain_regions] = covar_correct(X, Y, data)

residuals.to_csv(f"{data_dir}/Data_residuals.csv")

logging.debug(f"DATA CORRECTED FOR CONFOUNDS\n{log_func(existing, locals())[0]}\n\n")

#-----------------------------------------------------------------------------------------------------------------

# Normalize on healthy subjects

thickness_volume = pd.read_csv(f"{data_dir}/CT_Volume.csv", index_col="SubjID")
demographics = pd.read_csv(f"{data_dir}/Demographics.csv", index_col="SubjID")
residuals = pd.read_csv(f"{data_dir}/Data_residuals.csv", index_col="SubjID")
residuals = thickness_volume.merge(demographics['Dx'], left_index=True, right_index=True)

mu = residuals.groupby('Dx').mean()
sd = residuals.groupby('Dx').std()

zscores = ((residuals - mu.loc[0]) / sd.loc[0]).drop('Dx', axis=1)

zscores.to_csv(f"{data_dir}/Data_zscores.csv")

logging.debug(f"DATA ZSCORED\n{log_func(existing, locals())[0]}\n\n")
#-----------------------------------------------------------------------------------------------------------------

# Generate graphs and extract metrics

thresholds = np.arange(95,101, 1)

demographics = pd.read_csv(f"{data_dir}/Demographics.csv", index_col="SubjID")
zscores = pd.read_csv(f"{data_dir}/Data_zscores.csv", index_col="SubjID")
demo_columns = demographics.columns
brain_regions = zscores.columns

def joint_variation(v):
    warnings.filterwarnings('ignore')
    if ~isinstance(v, np.ndarray):
        v = np.asanyarray(v)
    JV = np.array([ 1 / np.exp((v - i) ** 2) for i in v])
    np.fill_diagonal(JV, 0)
    return JV

def get_metrics(M, thr, nodes):
    M[M < np.percentile(M, thr)] = 0
    M[M > 0] = 1

    B = nx.from_numpy_matrix(M)
    B = nx.relabel_nodes(B, dict(zip(B, nodes)))
    B = nx.algorithms.full_diagnostics(B, swi=False, swi_niter=100, swi_nrand=10, swi_seed=None, n_jobs=nj, prefer=None)

    attributes = B.nodes[nodes[0]].keys()
    metric_dict = {metric: nx.get_node_attributes(B, metric) for metric in attributes}
    metric_dict.update({metric: {"global": value} for metric, value in B.metrics.items()})
    return metric_dict

results = {}
logging.debug(f"\n\nCOMPUTED METRICS FOR K:")
for thr in thresholds:
    r = Parallel(n_jobs=nj)(delayed(get_metrics)(joint_variation(v), thr, brain_regions) for _, v in zscores.iterrows())
    results.update({thr: dict(zip(zscores.index, r))})
    logging.debug(f"\t{thr}")

output_df = pd.DataFrame([[subj, thr] for subj in results[thr].keys() for thr in results.keys()], columns=['SubjID', 'Density']).set_index('SubjID')

metrics = results[thresholds[0]][zscores.index[0]].keys()
for metric in metrics:
    columns = [(node,
               metric,
              [results[density][subj][metric][node] for subj, density in output_df['Density'].iteritems()])
              for node in results[thresholds[0]][zscores.index[0]][metric].keys()]

    for node, metric, values in columns:
        output_df[f"{node}_{metric}"] = np.float64(values)

output_df.to_csv(f"{output_dir}/Graph_metrics.csv")

logging.debug(f"GRAPH METRICS COMPUTED\n{log_func(existing, locals())[0]}\n\n")
#-----------------------------------------------------------------------------------------------------------------

# Group stats

demographics = pd.read_csv(f"{data_dir}/Demographics.csv", index_col="SubjID")
gmetrics = pd.read_csv(f"{output_dir}/Graph_metrics.csv", index_col="SubjID")
metrics = gmetrics.columns.to_list()
stat_list = [np.nanmean, np.nanstd, np.nanmedian, stats.iqr, 'count']
groups = ['Dx', 'Dx3', 'Subtype', 'Minor', 'Durill3']

stat_list = ['count', np.nanvar, np.nanmean, np.nanstd, np.nanmedian, stats.iqr]
contrasts = [['Dx', 'Dx3'],
             ['Dx', 'Dx3','Minor'],
             ['Dx', 'Dx3', 'Subtype'],
             ['Dx', 'Dx3', 'Durill3']]

Ks = np.arange(gmetrics.Density.min(), gmetrics.Density.max()+1, 1)
K_ranges = np.array([(Kmin, Kmax) for Kmin in Ks for Kmax in Ks[Ks>=Kmin+3]])

for Kmin, Kmax in K_ranges:
    AUC = gmetrics[(gmetrics.Density >= Kmin) & (gmetrics.Density <= Kmax)].groupby('SubjID').sum()
    AUC = AUC.merge(demographics, left_index=True, right_index=True)

    group_stats = [AUC.groupby(groups).agg(stat_list).stack().reset_index().rename(columns={f'level_{len(groups)}':'Stat'})
                   for i, groups in enumerate(contrasts)]

    for i, data in enumerate(group_stats):
        data.sort_values(contrasts[i] + ['Stat'])
        data['Contrast'] = i

    aggregation_table = pd.concat(group_stats).set_index(['Contrast', 'Dx', 'Dx3', 'Subtype', 'Minor', 'Durill3']).reset_index()
    aggregation_table['Kmin'] = Kmin
    aggregation_table['Kmax'] = Kmax

    aggregation_table.to_csv(f"{output_dir}/Group_stats_K{Kmin}-{Kmax}.csv")

logging.debug(f"GROUP STATS COMPUTED\n{log_func(existing, locals())[0]}\n\n")

#-----------------------------------------------------------------------------------------------------------------

# Correlation coefficients

demographics = pd.read_csv(f"{data_dir}/Demographics.csv", index_col="SubjID")
gmetrics = pd.read_csv(f"{output_dir}/Graph_metrics.csv", index_col="SubjID")
metrics = gmetrics.columns.to_list()

Ks = np.arange(gmetrics.Density.min(), gmetrics.Density.max()+1, 1)
K_ranges = np.array([(Kmin, Kmax) for Kmin in Ks for Kmax in Ks[Ks>=Kmin+3]])

for Kmin, Kmax in K_ranges:
    AUC = gmetrics[(gmetrics.Density >= Kmin) & (gmetrics.Density <= Kmax)].groupby('SubjID').sum()
    AUC = AUC.merge(demographics, left_index=True, right_index=True)

    group_Rs = []
    for group in demographics.Dx3.unique():
        R = AUC[AUC.Dx3==group].corr(method='spearman')[demographics.columns]
        R.Dx3 = group
        group_Rs.append(R)

    R_table = pd.concat(group_Rs).sort_values('Dx3')
    R_table['Kmin'] = Kmin
    R_table['Kmax'] = Kmax

    R_table.to_csv(f"{output_dir}/Correlations_K{Kmin}-{Kmax}.csv")

logging.debug(f"CORRELATIONS COMPUTED\n{log_func(existing, locals())[0]}\n\n")
#-----------------------------------------------------------------------------------------------------------------

# Regression models

import statsmodels.formula.api as sm

demographics = pd.read_csv(f"{data_dir}/Demographics.csv", index_col="SubjID")
gmetrics = pd.read_csv(f"{output_dir}/Graph_metrics.csv", index_col="SubjID")[demographics.Dx3==2]
global_metrics = gmetrics.columns[gmetrics.columns.str.startswith('global')]

def modeldata(formula, data):
    model = sm.gls(formula, data=data)
    fitted = model.fit()

    endog = pd.Series('_'.join(model.endog_names.split('_')[1:]), index=['endog'])
    params = fitted.params.rename(index=dict(zip(fitted.params.index, 'param_' + fitted.params.index)))
    errors = fitted.bse.rename(index=dict(zip(fitted.params.index, 'bse_' + fitted.bse.index)))
    pvalues = fitted.pvalues.rename(index=dict(zip(fitted.pvalues.index, 'pvalues_' + fitted.bse.index)))
    rsquared = pd.Series(fitted.rsquared, index=['rsquared'])
    rsquared_adj = pd.Series(fitted.rsquared_adj, index=['rsquared_adj'])

    results = pd.concat([endog, params, errors, pvalues, rsquared, rsquared_adj])
    return results

Ks = np.arange(gmetrics.Density.min(), gmetrics.Density.max()+1, 1)
K_ranges = np.array([(Kmin, Kmax) for Kmin in Ks for Kmax in Ks[Ks>=Kmin+3]])

for Kmin, Kmax in K_ranges:
    AUC = gmetrics[(gmetrics.Density >= Kmin) & (gmetrics.Density <= Kmax)].groupby('SubjID').sum()
    AUC = AUC.merge(demographics, left_index=True, right_index=True)

    regression_results = Parallel(n_jobs=nj)(delayed
                                             (modeldata)
                                             (f"{metric} ~ Age * Bmi", data=AUC)
                                             for metric in global_metrics)

    regression_table = pd.concat([res for res in regression_results], axis=1).T.set_index('endog')

    regression_table['Kmin'] = Kmin
    regression_table['Kmax'] = Kmax

    regression_table.to_csv(f"{output_dir}/Regressions_K{Kmin}-{Kmax}.csv")

logging.debug(f"REGRESSION MODELS COMPUTED\n{log_func(existing, locals())[0]}\n\n\n\n")
#-----------------------------------------------------------------------------------------------------------------


import numpy as np
import matplotlib.pyplot as plt
import ising as ising
from sklearn import linear_model
from sklearn.model_selection import train_test_split

#------------------#
# DATA PREPARATION #
#------------------#

# Ising states parameters
nsample = 1000   # number of data samples
nsites = 20       # number of ising sites
rndseed = 12      # random number generator seed

# generate random ising configurations
ising_states = ising.generate_states(nsample, nsites, rand_seed=rndseed)
# generate ising interaction matrix
ising_intmat = ising.interaction_matrix(ising_states)
# calculate total energy for ising states (y)
energy = ising.total_energy(ising_states)

# prepare data for model training
# features (X) : pair interactions for each configuration with size (nsample, nsite*nsite)
# labels (y) : total energy for each configuration (nsample, 1)
X = ising_intmat.reshape(1, -1, nsample).swapaxes(0, 2).squeeze()
y = energy

#--------------------------------#
# MODEL SELECTION AND EVALUATION #
#--------------------------------#

# split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=.33, random_state=rndseed)

# instantiate models
linreg_ols = linear_model.LinearRegression()    # ordinary least square regression
linreg_rdg = linear_model.Ridge()               # Ridge regression
linreg_las = linear_model.Lasso()               # Lasso regression

# train old model
linreg_ols.fit(X_train, y_train)
err_train_ols = linreg_ols.score(X_train, y_train)
err_test_ols = linreg_ols.score(X_test, y_test)
coef_ols = linreg_ols.coef_

# train ridge and lasso models
err_train_las = []
err_train_reg = []
err_test_las = []
err_test_reg = []
coef_las = []
coef_reg = []
regpara = np.logspace(-4, 5, 10)    # regularization parameter

for alpha in regpara:
    # ridge
    linreg_rdg.set_params(alpha=alpha)
    linreg_rdg.fit(X_train, y_train)
    # errors and coeffs
    err_train_reg.append(linreg_rdg.score(X_train, y_train))
    err_test_reg.append(linreg_rdg.score(X_test, y_test))
    coef_reg.append(linreg_rdg.coef_)

    # lasso
    linreg_las.set_params(alpha=alpha)
    linreg_las.fit(X_train, y_train)
    # errors and coeffs
    err_train_las.append(linreg_las.score(X_train, y_train))
    err_test_las.append(linreg_las.score(X_test, y_test))
    coef_las.append(linreg_las.coef_)

#-----------------------#
# ANALYZE MODEL OUTPUTS #
#-----------------------#

# plot train and test errors
plt.hlines(err_train_ols, min(regpara), max(regpara), colors='k', linestyles='dotted', label='Train OLS')
plt.hlines(err_test_ols, min(regpara), max(regpara), colors='k', linestyles='solid', label='Test OLS')
plt.semilogx(regpara, err_train_reg, '--ro', label="Train Ridge")
plt.semilogx(regpara, err_test_reg, 'ro', label="Test Ridge")
plt.semilogx(regpara, err_train_las, '--bo', label="Train Lasso")
plt.semilogx(regpara, err_test_las, 'bo', label="Test Lasso")
plt.xlim(min(regpara), max(regpara))
plt.xlabel("Regularization parameter")
plt.ylabel("Model score")
plt.legend()

# linear regression coefficients (interaction strength of ising pairs)
npara = regpara.size
fig, ax = plt.subplots(nrows=npara, ncols=3)

coef_reg = np.array(coef_reg).reshape(npara, nsites, nsites)
coef_las = np.array(coef_las).reshape(npara, nsites, nsites)
coef_ols = coef_ols.reshape(nsites, nsites)

for iax in range(npara):
    # Ridge
    ax[iax, 0].imshow(coef_reg[iax, :, :])
    # Lasso
    ax[iax, 1].imshow(coef_las[iax, :, :])
    # Ols
    ax[iax, 2].imshow(coef_ols)

plt.show()

# train and test dataset scores
# print("linreg_ols train set score:{:2.2f}".format(linreg_ols.score(X_train, y_train)))
# print("linreg_ols test set score:{:2.2f}".format(linreg_ols.score(X_test, y_test)))

# linear regression coefficients (interaction strength of ising pairs)
# coef_ols = linreg_ols.coef_
# coef_ols = coef_ols.reshape(nsites, nsites)

# imgplt = plt.imshow(coef_ols)
# imgplt.set_clim(-1, 1)
# plt.colorbar()
# plt.show()

# new sample
# new_state = ising.generate_states(1,nsites)
# new_energy = ising.total_energy(new_state)
# new_X = new_state.reshape(nsites, 1) * new_state
# new_X = new_X.reshape(1,-1)
# linreg_ols.predict(new_X)

# paper example
# X_train_exmpl = X[:400, :]
# y_train_exmpl = y[:400]
# X_test_exmpl = X[400:3*400//2, :]
# y_test_exmpl = y[400:3*400//2]

# linreg_ols_exmpl = linear_model.LinearRegression()
# linreg_ols_exmpl.fit(X_train_exmpl, y_train_exmpl)
# linreg_ols_exmpl.score(X_train_exmpl, y_train_exmpl)
# linreg_ols_exmpl.score(X_test_exmpl, y_test_exmpl)

# print scores
# print("model 1 train score: {:4.2f}".format(linreg_ols.score(X_train, y_train)))
# print("model 1 test score: {:4.2f}".format(linreg_ols.score(X_test, y_test)))
# print("model 2 train score: {:4.2f}".format(linreg_ols_exmpl.score(X_train_exmpl, y_train_exmpl)))
# print("model 2 test score: {:4.2f}".format(linreg_ols_exmpl.score(X_test_exmpl, y_test_exmpl)))
#


from django.urls import path

from apps.task2.views import ListFeedbackAPIView, CreateFeedbackAPIView, DetailServiceAPIView, DeleteFeedbackAPIView

urlpatterns = [
    path('list/feedbacks/', ListFeedbackAPIView.as_view(), name='list_feedback'),
    path('create/feedback/', CreateFeedbackAPIView.as_view(), name='create_feedback'),
    path('detail/service/<int:pk>/', DetailServiceAPIView.as_view(), name='detail_service'),
    path('delete/feedback/<int:pk>/', DeleteFeedbackAPIView.as_view(), name='delete_feedback')

]


# solution to Advent of Code 2018, day 7 part one and two
# https://adventofcode.com/2018/day/7
#
# Usage: sleigh.py [inputfile]

from collections import defaultdict
import fileinput
import re

def main():
    steplist = set([re.findall(r'\s([A-Z]).*([A-Z])', line).pop() for line in fileinput.input()])

    # graph is a dict of tuples
    # dict key is the name of the step ('A', 'B', 'C', etc)
    # tuple is (set(), set()) where the sets contain inbound
    # and outbound nodes relating to the dict key
    graph = defaultdict(tuplesets)
    INBOUND = 0
    OUTBOUND = 1
    for a, b in steplist:
        graph[a][OUTBOUND].add(b)
        graph[b][INBOUND].add(a)

    # solve part one
    open_nodes = set()
    visited = set()
    steps = ''
    [open_nodes.add(node) for node, related in graph.items() if not related[INBOUND]]

    while open_nodes:
        next_node = min([node for node in open_nodes if graph[node][INBOUND] <= visited])
        open_nodes |= graph[next_node][OUTBOUND]
        steps += next_node
        visited.add(next_node)
        open_nodes.remove(next_node)

    print('Step order:', steps)

    # solve part two
    workers = [None] * 5
    open_nodes = set()
    done = set()
    [open_nodes.add(node) for node, related in graph.items() if not related[INBOUND]]

    seconds = 0
    finished = False
    while not finished:
        finishing_workers = [i for i,worker in enumerate(workers) if not worker == None and worker[0] == seconds]
        for i in finishing_workers:
            finished_step = workers[i][1]
            done.add(finished_step)
            open_nodes |= graph[finished_step][OUTBOUND]
            workers[i] = None

        if len(done) == len(graph.keys()):
            finished = True
            continue

        available_workers = [i for i,worker in enumerate(workers) if worker == None]
        if not available_workers:
            seconds += 1
            continue
        for i in available_workers:
            potential_next = [node for node in open_nodes if graph[node][INBOUND] <= done]
            if not potential_next:
                continue
            next_node = min(potential_next)
            workers[i] = ((seconds + ord(next_node) - 4), next_node)
            open_nodes.remove(next_node)
        seconds += 1

    print('Seconds to completion with 5 workers:', seconds)

def tuplesets():
    return (set(), set())

if __name__ == '__main__':
    main()