# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors:
#     Alvaro del Castillo <acs@bitergia.com>
#

import json
import logging
import os.path

import requests

from ..backend import Backend, BackendCommand, metadata
from ..cache import Cache
from ..errors import CacheError
from ..utils import (DEFAULT_DATETIME,
                     datetime_to_utc,
                     str_to_datetime,
                     urljoin)

logger = logging.getLogger(__name__)

class Jenkins(Backend):
    """Jenkins backend for Perceval.

    This class retrieves the builds from a
    Jenkins site. To initialize this class the
    URL must be provided.

    :param url: Jenkins url
    :param cache: cache object to store raw data
    :param origin: identifier of the repository; when `None` or an
        empty string are given, it will be set to `url` value
    """
    version = '0.1.0'

    def __init__(self, url, cache=None, origin=None):
        origin = origin if origin else url

        super().__init__(origin, cache=cache)
        self.url = url
        self.client = JenkinsClient(url)

    @metadata
    def fetch(self):
        """Fetch the builds from the url.

        The method retrieves, from a Jenkins url, the
        builds updated since the given date.

        :returns: a generator of builds
        """

        logger.info("Looking for projects at url '%s'", self.url)

        self._purge_cache_queue()
        nbuilds = 0  # number of builds processed
        njobs = 0 # number of jobs processed

        projects = json.loads(self.client.get_jobs())
        jobs = projects['jobs']

        for job in jobs:
            njobs += 1
            logger.debug("Adding builds from %s (%i/%i)" % (job['url'], njobs, len(jobs)))
            raw_builds = self.client.get_builds(job['name'])
            self._push_cache_queue(raw_builds)
            self._flush_cache_queue()
            builds = json.loads(raw_builds)
            builds = builds['builds']
            for build in builds:
                yield build
                nbuilds += 1

        logger.info("Total number of jobs: %i" % (njobs))
        logger.info("Total number of builds: %i" % (nbuilds))

    @metadata
    def fetch_from_cache(self):
        """Fetch the builds from the cache.

        :returns: a generator of builds

        :raises CacheError: raised when an error occurs accessing the
            cache
        """
        if not self.cache:
            raise CacheError(cause="cache instance was not provided")

        cache_items = self.cache.retrieve()

        for items in cache_items:
            builds = json.loads(items)['builds']
            for build in builds:
                yield build

    @staticmethod
    def metadata_id(item):
        """Extracts the identifier from a Build item."""
        return str(item['url'])

    @staticmethod
    def metadata_updated_on(item):
        """Extracts the update time from a Jenkins item.

        The timestamp is extracted from 'timestamp' field.
        This date is a UNIX timestamp but needs to be converted to
        a float value.

        :param item: item generated by the backend

        :returns: a UNIX timestamp
        """
        return float(item['timestamp']/1000)

class JenkinsClient:
    """Jenkins API client.

    This class implements a simple client to retrieve builds from
    projects in a Jenkins node.

    :param url: URL of jenkins node: https://build.opnfv.org/ci

    :raises HTTPError: when an error occurs doing the request
    """

    def __init__(self, url):
        self.url = url

    def get_jobs(self):
        """ Retrieve all jobs
        """
        url_jenkins = self.url + "/view/All/api/json"

        req = requests.get(url_jenkins)
        req.raise_for_status()
        return req.text

    def get_builds(self, job_name):
        """ Retrieve all builds from a job
        """
        # depth=2 to get builds details
        job_url = self.url + "/job/%s/" % (job_name)
        url_jenkins = job_url + "api/json?depth=2"

        req = requests.get(url_jenkins)
        req.raise_for_status()
        return req.text

class JenkinsCommand(BackendCommand):
    """Class to run Jenkins backend from the command line."""

    def __init__(self, *args):
        super().__init__(*args)
        self.url = self.parsed_args.url
        self.origin = self.parsed_args.origin
        self.outfile = self.parsed_args.outfile

        if not self.parsed_args.no_cache:
            if not self.parsed_args.cache_path:
                base_path = os.path.expanduser('~/.perceval/cache/')
            else:
                base_path = self.parsed_args.cache_path

            cache_path = os.path.join(base_path, self.url)

            cache = Cache(cache_path)

            if self.parsed_args.clean_cache:
                cache.clean()
            else:
                cache.backup()
        else:
            cache = None

        self.backend = Jenkins(self.url, cache=cache, origin=self.origin)

    def run(self):
        """Fetch and print the Builds.

        This method runs the backend to fetch the Builds of a given url.
        Builds are converted to JSON objects and printed to the
        defined output.
        """
        if self.parsed_args.fetch_cache:
            builds = self.backend.fetch_from_cache()
        else:
            builds = self.backend.fetch()

        try:
            for build in builds:
                obj = json.dumps(build, indent=4, sort_keys=True)
                self.outfile.write(obj)
                self.outfile.write('\n')
        except requests.exceptions.HTTPError as e:
            raise requests.exceptions.HTTPError(str(e.response.json()))
        except IOError as e:
            raise RuntimeError(str(e))
        except Exception as e:
            if self.backend.cache:
                self.backend.cache.recover()
            raise RuntimeError(str(e))

    @classmethod
    def create_argument_parser(cls):
        """Returns the Jenkins argument parser."""

        parser = super().create_argument_parser()

        # Jenkins options
        group = parser.add_argument_group('Jenkins arguments')

        # Required arguments
        parser.add_argument('url',
                            help="URL of the Jenkins server")

        return parser


# code-checked
# server-checked

import torch
import torch.utils.data
import torch.nn.functional as F
from torch.autograd import Variable

import numpy as np

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

import pickle

class ToyDataset(torch.utils.data.Dataset):
    def __init__(self):
        self.examples = []

        with open("/root/evaluating_bdl/toyClassification/x.pkl", "rb") as file: # (needed for python3)
            x = pickle.load(file) # (shape: (2000, 2))

        with open("/root/evaluating_bdl/toyClassification/y.pkl", "rb") as file: # (needed for python3)
            y = pickle.load(file) #  (shape: (2000, ))

        x_1_train = []
        x_2_train = []
        y_train = []
        for i in range(x.shape[0]):
            if x[i, 0] > 0:
                x_1_train.append(x[i, 0])
                x_2_train.append(x[i, 1])
                y_train.append(y[i])

        y_train = np.array(y_train)
        x_train = np.zeros((len(y_train), 2), dtype=np.float32)
        x_train[:, 0] = np.array(x_1_train)
        x_train[:, 1] = np.array(x_2_train)

        x_train_false = x_train[y_train == 0] # (shape: (num_false, 2))
        x_train_true = x_train[y_train == 1] # (shape: (num_true, 2))
        print ("num_false: %d" % x_train_false.shape[0])
        print ("num_true: %d" % x_train_true.shape[0])
        plt.figure(1)
        plt.plot(x_train_false[:, 0], x_train_false[:, 1], "r.")
        plt.plot(x_train_true[:, 0], x_train_true[:, 1], "b.")
        plt.ylabel("x_2")
        plt.xlabel("x_1")
        plt.xlim([-3, 3])
        plt.ylim([-3, 3])
        plt.savefig("/root/evaluating_bdl/toyClassification/HMC/training_data.png")
        plt.close(1)

        for i in range(x_train.shape[0]):
            example = {}
            example["x"] = x_train[i]
            example["y"] = y_train[i]
            self.examples.append(example)

        self.num_examples = len(self.examples)

    def __getitem__(self, index):
        example = self.examples[index]

        x = example["x"]
        y = example["y"]

        return (x, y)

    def __len__(self):
        return self.num_examples

#_ = ToyDataset()


#! /usr/bin/env python3

import argparse
import random
import sys
import os
import math

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument( '-n', '--num-samples', default = 1, type = int, help = 'Number of samples to be generated.' )
    parser.add_argument( '-l', '--length', default = 16, type = int, help = 'Length of a sample in bytes.' )
    parser.add_argument( '-v', '--verbose', action = 'store_true' )
    parser.add_argument( '-b', '--bits', default = 8, type = int, help = 'Bits per unit item in sample.' )
    parser.add_argument( 'FILE' )
    args = parser.parse_args()

    if args.verbose == True:
        print( args )

    handle = open( args.FILE, 'w' ) if args.FILE != '-' else sys.stdout

    random.seed()

    unit_format = "%%0%dx" % math.ceil( args.bits / 4 )
    total_bits = args.length * args.bits
    max_value   = 1 << total_bits - 1

    for n in range( args.num_samples ):
        value = random.randint( 0, max_value )
        random_list = [ ( value >> ( i * 8 ) ) & 0xff for i in range( args.length ) ]
        handle.write( " ".join( [ unit_format  % value for value in random_list ] ) + os.linesep )

    if handle != sys.stdout:
        handle.close()

if __name__ == '__main__':
    main()


from PyQt5 import QtWidgets, QtGui, QtCore
import sys, signal, os
import AlarmEventHandler
import AudioModule
########################## START BUILDING THE CLASS##########################

class GUI(QtWidgets.QWidget):
    def __init__(self):
        super(GUI, self).__init__()
        self.setWindowTitle('Alarm Clock')
        self.setGeometry(100, 100, 300, 210)
        self.setWindowIcon(QtGui.QIcon('alarm-clock-icon.png'))
        self.setStyleSheet('background-color: #00b300')
        self.setFixedSize(300, 210)
        self.show()
        self.all_widgets = []

    def add_time(self, from_time, to_time, x, y):
        time_ = QtWidgets.QComboBox(self)
        time_.setGeometry(x, y, 50, 50)
        time_.setStyleSheet('border: 2px solid black;'
                            'background-color: #33ccff;'
                            'border-radius: 5px;'
                            'font-size: 18px')
        for i in range(from_time, to_time):
            time_.addItem(format_with_zero(i))
        time_.show()
        self.all_widgets.append(time_)

    def add_label(self, name_, x, y, width=50):
        label = QtWidgets.QLabel(name_, self)
        label.setGeometry(x, y, width, 20)
        label.setStyleSheet('border: 2px solid black;'
                            'background-color: lightgreen;'
                            'border-radius: 5px;'
                            'font-size: 15px')
        label.show()
        label.setAlignment(QtCore.Qt.AlignCenter)
        self.all_widgets.append(label)

    def add_button(self,icon_path, text, x, y):
        btn = QtWidgets.QPushButton(self)
        btn.setGeometry(x, y, 50, 50)
        btn.setIcon(QtGui.QIcon(icon_path))
        btn.setIconSize(QtCore.QSize(40, 40))
        btn.show()
        btn.setStyleSheet('border-radius: 10px;\
                           border: 2px solid black;')

        self.all_widgets.append(btn)

    def add_button_event(self, btn, signal):
        btn.clicked.connect(signal)

    def add_ringtone_list(self):
        dd = QtWidgets.QComboBox(self)
        dd.setGeometry(95, 180, 110, 20)
        dd.addItem('Bird')
        dd.addItem('Bird 2')
        dd.addItem('Mario')
        dd.addItem('Rooster')
        dd.addItem('None')
        dd.setStyleSheet('border: 2px solid black;'
                            'background-color: #33ccff;'
                            'border-radius: 5px;'
                            'font-size: 15px')
        dd.show()
        self.all_widgets.append(dd)

    def closeEvent(self, QCloseEvent):
        pid = os.getpid()
        os.kill(pid, signal.SIGKILL)

def format_with_zero(str_):
    result = str(str_)
    if len(result) is 1:
        result = '0' + result
    return result

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    a_clock = GUI()
    a_clock.add_time(from_time=0, to_time=24, x=95, y=35)
    a_clock.add_time(from_time=0, to_time=60, x=155, y=35)

    a_clock.add_label(name_='HH' , x=95, y=10)
    a_clock.add_label(name_='MM', x=155, y=10)

    a_clock.add_button(icon_path='./Icon/play.png', text='Start', x=95, y=90)
    a_clock.add_button(icon_path='./Icon/stop.png', text='Stop', x=155, y=90)

    a_clock.add_label('Alarm Tone', 95, 150, width=110)
    a_clock.add_ringtone_list()

    a_clock.all_widgets[5].setEnabled(False)

    am = AudioModule.PlayTone(a_clock.all_widgets[7])
    a_clock.all_widgets[7].currentIndexChanged.connect(am.play)

    # Create Signals
    cm = AlarmEventHandler.ControllerMenu(a_clock)
    play = cm.play
    pause = cm.stop

    a_clock.add_button_event(a_clock.all_widgets[4], play)
    a_clock.add_button_event(a_clock.all_widgets[5], pause)

    sys.exit(app.exec_())


# import libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# change the default plotting style to something nicer
plt.style.use('ggplot')

# import csv file
# data from https://fred.stlouisfed.org/series/DCOILWTICO/downloaddata
df = pd.read_csv('DCOILWTICO.csv')

# convert date column to datetime
df['DATE'] = pd.to_datetime(df['DATE'])

# setup basic line plot, change line color from red to green
ax = df.plot(x='DATE', y='VALUE', color = '#31a354', linewidth = .5)

# change the legend lable from "VALUE" to "Oil Price ($)"
L = plt.legend(loc = 'upper left', fontsize = 'x-large')
L.get_texts()[0].set_text('Oil Price ($)')

# edit axis labels and font size
ax.set_title("Price of Oil for last 30 years", fontsize = 18)
ax.set_xlabel("Time", fontsize = 14)
ax.set_ylabel("Oil Price ($)", fontsize = 14)

# save the figure at a good resolution
fig = ax.get_figure()
fig.savefig('oilprice.png', dpi=600)

plt.show()


# -*- coding: utf-8 -*-
# author: Dabin Cheng time: 2020/4/30

import torch
import torch.nn as nn

class C3D(nn.Module):

# input 128*128，b,c,t,h,w

    def __init__(self):
        super(C3D, self).__init__()

        self.conv1 = nn.Conv3d(3, 64, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.pool1 = nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))

        self.conv2 = nn.Conv3d(64, 128, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.pool2 = nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))

        self.conv3a = nn.Conv3d(128, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.conv3b = nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.pool3 = nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))

        self.conv4a = nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.conv4b = nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.pool4 = nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))

        self.conv5a = nn.Conv3d(512, 512, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.conv5b = nn.Conv3d(512, 512, kernel_size=(3, 3, 3), padding=(1, 1, 1))
        self.pool5 = nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))
        self.conv6 = nn.Conv3d(256, 32, kernel_size=(1, 1, 1))

        # self.fc6 = nn.Linear(8192, 4096)
        # self.fc7 = nn.Linear(4096, 4096)
        # self.fc8 = nn.Linear(4096, 487)
        #
        # self.dropout = nn.Dropout(p=0.5)

        self.relu = nn.ReLU()
        # self.softmax = nn.Softmax()

    def forward(self, x):

        h = self.relu(self.conv1(x))
        h = self.pool1(h)

        h = self.relu(self.conv2(h))
        h = self.pool2(h)

        h = self.relu(self.conv3a(h))
        h = self.relu(self.conv3b(h))
        h = self.pool3(h)

        h = self.relu(self.conv4a(h))
        h = self.relu(self.conv4b(h))
        h = self.pool4(h)

        # h = self.relu(self.conv5a(h))
        # h = self.relu(self.conv5b(h))
        # h = self.pool5(h)
        h = self.conv6(h)

        # h = h.view(-1, 8192)
        # h = self.relu(self.fc6(h))
        # h = self.dropout(h)
        # h = self.relu(self.fc7(h))
        # h = self.dropout(h)
        #
        # logits = self.fc8(h)
        # probs = self.softmax(logits)
        #
        return h

class ResBlock(nn.Module):
    def __init__(self, input_channel, output_channel, stride=1, init_method='kaiming_normal_'):
        super(ResBlock, self).__init__()
        self.in_channel = input_channel
        self.out_channel = output_channel
        self.init_method = init_method
        self.stride = stride

        self.w_1 = nn.Conv3d(self.in_channel, self.out_channel, kernel_size=1, \
                             stride=self.stride, padding=0, bias=False)
        self.bn_1 = nn.BatchNorm3d(self.out_channel)
        self.w_2 = nn.Conv3d(self.out_channel, self.out_channel, kernel_size=3, \
                             stride=1, padding=1, bias=False)
        self.bn_2 = nn.BatchNorm3d(self.out_channel)
        self.w_3 = nn.Conv3d(self.out_channel, self.out_channel, kernel_size=3, \
                             stride=1, padding=1, bias=False)
        self.bn_3 = nn.BatchNorm3d(self.out_channel)
        self.w_4 = nn.Conv3d(self.out_channel, self.out_channel, kernel_size=3, \
                             stride=1, padding=1, bias=False)
        self.bn_4 = nn.BatchNorm3d(self.out_channel)
        self.w_5 = nn.Conv3d(self.out_channel, self.out_channel, kernel_size=3, \
                             stride=1, padding=1, bias=False)
        self.bn_5 = nn.BatchNorm3d(self.out_channel)
        self.relu = nn.ReLU()

        for w in self.modules():
            if isinstance(w, nn.Conv3d):
                getattr(nn.init, self.init_method)(w.weight)

    def forward(self, input):
        step_1 = self.bn_1(self.w_1(input))
        step_2 = self.relu(self.bn_2(self.w_2(step_1)))
        step_3 = self.bn_3(self.w_3(step_2))
        step_3 = self.relu(step_3 + step_1)
        step_4 = self.relu(self.bn_4(self.w_4(step_3)))
        step_5 = self.bn_5(self.w_5(step_4))
        step_5 = self.relu(step_5 + step_3)
        return step_5

class Res3D(nn.Module):
    def __init__(self, input_channels, output_channels, init_method='kaiming_normal_'):
        super(Res3D, self).__init__()

        assert len(output_channels) == 4
        self.input_channels = input_channels
        self.output_channels_1 = output_channels[0]
        self.output_channels_2 = output_channels[1]
        self.output_channels_3 = output_channels[2]
        self.output_channels_4 = output_channels[3]
        self.fc = nn.Linear()
        self.init_method = init_method

        self.w_1 = nn.Conv3d(self.input_channels, self.output_channels_1, (3, 7, 7), \
                             stride=(1, 2, 2), padding=(1, 3, 3), bias=False)  # why false
        self.bn = nn.BatchNorm3d(self.output_channels_1)
        self.relu = nn.ReLU()
        self.res_2 = ResBlock(self.output_channels_1, self.output_channels_2, stride=1, \
                              init_method='kaiming_normal_')
        self.res_3 = ResBlock(self.output_channels_2, self.output_channels_3, stride=2, \
                              init_method='kaiming_normal_')
        self.res_4 = ResBlock(self.output_channels_3, self.output_channels_4, stride=1, \
                              init_method='kaiming_normal_')

        for w in self.modules():
            if isinstance(w, nn.Conv3d):
                getattr(nn.init, self.init_method)(w.weight)

    def forward(self, input):
        block1 = self.relu(self.bn(self.w_1(input)))

        block2 = self.res_2(block1)
        block3 = self.res_3(block2)
        block4 = self.res_4(block3)
        out = torch.flatten(block4, 1)

        return out

class ConvLSTMCell(nn.Module):

    def __init__(self, input_dim, hidden_dim, kernel_size, bias):
        """
        Initialize ConvLSTM cell.
        Parameters
        ----------
        input_dim: int
            Number of channels of input tensor.
        hidden_dim: int
            Number of channels of hidden state.
        kernel_size: (int, int)
            Size of the convolutional kernel.
        bias: bool
            Whether or not to add the bias.
        """

        super(ConvLSTMCell, self).__init__()

        self.input_dim = input_dim
        self.hidden_dim = hidden_dim

        self.kernel_size = kernel_size
        self.padding = kernel_size[0] // 2, kernel_size[1] // 2
        self.bias = bias

        self.conv = nn.Conv2d(in_channels=self.input_dim + self.hidden_dim,
                              out_channels=4 * self.hidden_dim,
                              kernel_size=self.kernel_size,
                              padding=self.padding,
                              bias=self.bias)

    def forward(self, input_tensor, cur_state):
        h_cur, c_cur = cur_state

        combined = torch.cat([input_tensor, h_cur], dim=1)  # concatenate along channel axis

        combined_conv = self.conv(combined)
        cc_i, cc_f, cc_o, cc_g = torch.split(combined_conv, self.hidden_dim, dim=1)
        i = torch.sigmoid(cc_i)
        f = torch.sigmoid(cc_f)
        o = torch.sigmoid(cc_o)
        g = torch.tanh(cc_g)

        c_next = f * c_cur + i * g
        h_next = o * torch.tanh(c_next)

        return h_next, c_next

    def init_hidden(self, batch_size, image_size):
        height, width = image_size
        return (torch.zeros(batch_size, self.hidden_dim, height, width, device=self.conv.weight.device),
                torch.zeros(batch_size, self.hidden_dim, height, width, device=self.conv.weight.device))

class ConvLSTM(nn.Module):

    """
    Parameters:
        input_dim: Number of channels in input
        hidden_dim: Number of hidden channels
        kernel_size: Size of kernel in convolutions
        num_layers: Number of LSTM layers stacked on each other
        batch_first: Whether or not dimension 0 is the batch or not
        bias: Bias or no bias in Convolution
        return_all_layers: Return the list of computations for all layers
        Note: Will do same padding.
    Input:
        A tensor of size B, T, C, H, W or T, B, C, H, W
    Output:
        A tuple of two lists of length num_layers (or length 1 if return_all_layers is False).
            0 - layer_output_list is the list of lists of length T of each output
            1 - last_state_list is the list of last states
                    each element of the list is a tuple (h, c) for hidden state and memory
    Example:
        >> x = torch.rand((32, 10, 64, 128, 128))
        >> convlstm = ConvLSTM(64, 16, 3, 1, True, True, False)
        >> _, last_states = convlstm(x)
        >> h = last_states[0][0]  # 0 for layer index, 0 for h index
    """

    def __init__(self, input_dim, hidden_dim, kernel_size, num_layers,
                 time_first=False, bias=True, return_all_layers=False):
        super(ConvLSTM, self).__init__()

        self._check_kernel_size_consistency(kernel_size)

        # Make sure that both `kernel_size` and `hidden_dim` are lists having len == num_layers
        kernel_size = self._extend_for_multilayer(kernel_size, num_layers)
        hidden_dim = self._extend_for_multilayer(hidden_dim, num_layers)
        if not len(kernel_size) == len(hidden_dim) == num_layers:
            raise ValueError('Inconsistent list length.')

        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.kernel_size = kernel_size
        self.num_layers = num_layers
        self.time_first = time_first
        self.bias = bias
        self.return_all_layers = return_all_layers

        cell_list = []
        for i in range(0, self.num_layers):
            cur_input_dim = self.input_dim if i == 0 else self.hidden_dim[i - 1]

            cell_list.append(ConvLSTMCell(input_dim=cur_input_dim,
                                          hidden_dim=self.hidden_dim[i],
                                          kernel_size=self.kernel_size[i],
                                          bias=self.bias))

        self.cell_list = nn.ModuleList(cell_list)

    def forward(self, input_tensor, hidden_state=None):
        """
        Parameters
        ----------
        input_tensor: todo
            5-D Tensor either of shape (t, b, c, h, w) or (b, t, c, h, w)
        hidden_state: todo
            None. todo implement stateful
        Returns
        -------
        last_state_list, layer_output
        """
        if not self.time_first:
            # (b, c, t, h, w) -> (b, t, c, h, w)
            input_tensor = input_tensor.permute(0, 2, 1, 3, 4)

        b, _, _, h, w = input_tensor.size()

        # Implement stateful ConvLSTM
        if hidden_state is not None:
            raise NotImplementedError()
        else:
            # Since the init is done in forward. Can send image size here
            hidden_state = self._init_hidden(batch_size=b,
                                             image_size=(h, w))

        layer_output_list = []
        last_state_list = []

        seq_len = input_tensor.size(1)
        cur_layer_input = input_tensor

        for layer_idx in range(self.num_layers):

            h, c = hidden_state[layer_idx]
            output_inner = []
            for t in range(seq_len):
                h, c = self.cell_list[layer_idx](input_tensor=cur_layer_input[:, t, :, :, :],
                                                 cur_state=[h, c])
                output_inner.append(h)

            layer_output = torch.stack(output_inner, dim=1)
            cur_layer_input = layer_output

            layer_output_list.append(layer_output)
            last_state_list.append([h, c])

        if not self.return_all_layers:
            layer_output_list = layer_output_list[-1:]
            last_state_list = last_state_list[-1:]

        return layer_output_list, last_state_list

    def _init_hidden(self, batch_size, image_size):
        init_states = []
        for i in range(self.num_layers):
            init_states.append(self.cell_list[i].init_hidden(batch_size, image_size))
        return init_states

    @staticmethod
    def _check_kernel_size_consistency(kernel_size):
        if not (isinstance(kernel_size, tuple) or
                (isinstance(kernel_size, list) and all([isinstance(elem, tuple) for elem in kernel_size]))):
            raise ValueError('`kernel_size` must be tuple or list of tuples')

    @staticmethod
    def _extend_for_multilayer(param, num_layers):
        if not isinstance(param, list):
            param = [param] * num_layers
        return param

class MainNet(nn.Module):

    def __init__(self, input_dim, hidden_dim, lstm_kernel_size, num_layers, batch_size, num_class):
        super(MainNet, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.lstm_kernel_size = lstm_kernel_size
        self.num_layers = num_layers
        self.batch_size = batch_size
        self.num_class = num_class

        c3d = C3D()
        convlstm = ConvLSTM(input_dim=self.input_dim,
                            hidden_dim=self.hidden_dim,
                            kernel_size=self.lstm_kernel_size,
                            num_layers=self.num_layers)
        self.c3d = c3d
        self.convlstm = convlstm
        self.fc = nn.Linear(512,self.num_class)

    def forward(self, input_tensor):
        x = self.c3d(input_tensor)
        # x_dim = list(x.size())
        # x_channel = x_dim[1]
        # x_squence = x_dim[2]
        # x_high = x_dim[3]
        # x_width = x_dim[4]
        # x_total = x_channel * x_squence * x_high * x_width

        x, _ = self.convlstm(x)
        x = x[0][:, 15, :, :, :]
        x = torch.flatten(x, start_dim=1, end_dim=-1)
        x = self.fc(x)

        return x


import socket
import json
import sys

def dns(input):
    flag=0
    ##data stored locally
    file1 = open("data.json","r")
    localdata = json.loads(file1.read())
    file1.close()
    ##checking if the requested site's ip exists locally
    for key,val in localdata.items():
        if(input==key):
            addr=val
            flag=1
    ##if data not found locally geting the ip from dns server
    if(flag==0):
        addr = socket.gethostbyname(input)
        localdata[input] = addr            ##adding data to local data
    file1 = open("data.json","w")
    file1.write(json.dumps(localdata))
    file1.close()
    return(addr)

s = socket.socket()
port = int(sys.argv[1])
print("Socket created")
s.bind(('',port))
print("socket binded to port",port)
s.listen(5)
print("Socket is listening")

while True:
    c,addr = s.accept()
    print("Connection received")
    data = c.recv(1024).decode()
    print(data)
    result=str(dns(data))
    print(result)
    c.send(bytes("".join(result),"utf-8"))
    c.close()
exit()


import yaml
from typing import Dict, Tuple, Union
from pprint import pprint
import os

def load_configuration(config_file: str) -> Dict[str,
                                                 Dict[str,
                                                      Union[str, int, float, bool, Tuple[int, int, int]]]]:
    """Load the default.yaml and override specific instance from the config_file.yaml

    Args:
        config_file: The config_file to load.

    Returns:
        Dictionary with 'model' and 'data' keys that both contain dictionaries of parameters.
    """
    parameters = yaml.load(open("/home/kay/projects/lumbar/configurations/default.yaml", "r"))
    parameters_experiment = yaml.load(open("/home/kay/projects/lumbar/configurations/%s.yaml" % config_file, "r"))

    for sub_key in parameters_experiment:
        for key in parameters_experiment[sub_key]:
            parameters[sub_key][key] = parameters_experiment[sub_key][key]

    return parameters

def load_model(config_file: str):
    from models.lfunetmulti import LfUNetMulti
    from models.lfunetsingle import LfUNetSingle
    import tensorflow as tf

    tf.reset_default_graph()
    parameters = load_configuration(config_file)

    model_parameters = parameters["model"]
    data_parameters = parameters["data"]

    model_type = model_parameters["type"]
    if model_type == "multi":
        return LfUNetMulti(name_ext=model_parameters["name_ext"],
                           resolution=data_parameters["resolution"],
                           nb_blocks=model_parameters["nb_blocks"],
                           nb_channels=model_parameters["nb_channels"],
                           learning_rate=model_parameters["learning_rate"],
                           keep_prob=model_parameters["keep_prob"],
                           bn_decay=model_parameters["bn_decay"],
                           init_schema=model_parameters["init_schema"],
                           optimizer=model_parameters["optimizer"],
                           learning_decay=model_parameters["learning_decay"],
                           supervision=model_parameters["supervision"],
                           activation=model_parameters["activation"],
                           activation_output=model_parameters["activation_output"],
                           batch_normalization=model_parameters["batch_normalization"],
                           channel_inc=model_parameters["channel_inc"],
                           res_skip=model_parameters["res_skip"],
                           long_skip=model_parameters["long_skip"],
                           max_dec_channels=model_parameters["max_dec_channels"],
                           channel_dec=model_parameters["channel_dec"],
                           loss_metric=model_parameters["loss_metric"],
                           loss_metric_weight=model_parameters["loss_metric_weight"],
                           prediction_dropout_hack=model_parameters["prediction_dropout_hack"])

    if model_type == "single":
        return LfUNetSingle(name_ext=model_parameters["name_ext"],
                            resolution=data_parameters["resolution"],
                            nb_blocks=model_parameters["nb_blocks"],
                            nb_channels=model_parameters["nb_channels"],
                            learning_rate=model_parameters["learning_rate"],
                            keep_prob=model_parameters["keep_prob"],
                            bn_decay=model_parameters["bn_decay"],
                            init_schema=model_parameters["init_schema"],
                            learning_decay=model_parameters["learning_decay"],
                            supervision=model_parameters["supervision"],
                            activation=model_parameters["activation"],
                            batch_normalization=model_parameters["batch_normalization"],
                            channel_inc=model_parameters["channel_inc"],
                            res_skip=model_parameters["res_skip"],
                            long_skip=model_parameters["long_skip"])

    else:
        raise ValueError("Unknown model type %s" % model_type)

def load_data_parameters(config_file: str):
    parameters = load_configuration(config_file)
    return parameters["data"]

def _get_configurations(path: str, net_type: str):
    return [(net_type + file_name, None) for file_name in [f.split('.')[0] for f in os.listdir(path)]]

def get_single_configurations():
    return _get_configurations(path="/home/kay/projects/lumbar/configurations/single/", net_type="single/")

def get_multi_configurations():
    return _get_configurations(path="/home/kay/projects/lumbar/configurations/multi/", net_type="multi/")

def get_all_configurations():
    return get_single_configurations() + get_multi_configurations()


from django.contrib import admin
from django.utils.html import format_html

from . import models

def bust_cache(modeladmin, request, queryset):
    for assessment in queryset:
        assessment.bust_cache()
    message = f"Cache for {queryset.count()} assessment(s) busted!"
    modeladmin.message_user(request, message)

bust_cache.short_description = "Clear cache for selected assessments"

@admin.register(models.Assessment)
class AssessmentAdmin(admin.ModelAdmin):
    list_display = ("__str__", "get_managers", "get_team_members", "get_reviewers")
    list_per_page = 10
    list_filter = (
        "editable",
        "public",
    )

    search_fields = (
        "name",
        "project_manager__last_name",
        "team_members__last_name",
        "reviewers__last_name",
    )

    actions = (bust_cache,)

    def queryset(self, request):
        qs = super().queryset(request)
        return qs.prefetch_related("project_manager", "team_members", "reviewers")

    def get_staff_ul(self, mgr):
        ul = ["
"]
        for user in mgr.all():
            ul.append(f"
{user.first_name} {user.last_name}
")

        ul.append("
")
        return format_html(" ".join(ul))

    def get_managers(self, obj):
        return self.get_staff_ul(obj.project_manager)

    def get_team_members(self, obj):
        return self.get_staff_ul(obj.team_members)

    def get_reviewers(self, obj):
        return self.get_staff_ul(obj.reviewers)

    get_managers.short_description = "Managers"
    get_managers.allow_tags = True

    get_team_members.short_description = "Team"
    get_team_members.allow_tags = True

    get_reviewers.short_description = "Reviewers"
    get_reviewers.allow_tags = True

@admin.register(models.DoseUnits)
class DoseUnitsAdmin(admin.ModelAdmin):
    list_display = (
        "name",
        "animal_dose_group_count",
        "epi_exposure_count",
        "invitro_experiment_count",
    )

@admin.register(models.Species)
class SpeciesAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
    )
    list_display_links = ("name",)

@admin.register(models.Strain)
class StrainAdmin(admin.ModelAdmin):
    list_select_related = ("species",)
    list_display = ("id", "name", "species")
    list_display_links = ("name",)
    list_filter = ("species",)

@admin.register(models.EffectTag)
class EffectTagAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "id")
    search_fields = ("name",)

@admin.register(models.TimeSpentEditing)
class TimeSpentEditingAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "seconds",
        "assessment",
        "content_type",
        "object_id",
        "content_object",
        "created",
    )
    search_fields = (
        "assessment",
        "content_type",
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.list_display_links = []


#!/home/zkfr/.local/share/virtualenvs/xf-5EfV3Nly/bin/python
#-*- coding:utf-8 -*-
# @author : MaLei
# @datetime : 2018-12-06 13:54
# @file : get_shopinfo.py
# @software : PyCharm

from datetime import datetime as dt
from dianping import *
import re
from bs4 import  BeautifulSoup

def get_id():
    # 从数据库获取店铺id
    cursor,conn=connect_mysql()
    shopid_sql='SELECT shopId FROM food_rank;'
    cursor.execute(shopid_sql)
    shopid_list=list(cursor.fetchall())
    for each in shopid_list[:]:
        shopid_list.append(each[0])
        shopid_list.remove(each)
    shopid_list=set(shopid_list)
    cursor.close()
    conn.close()
    return shopid_list
    # print(shopid_list,len(shopid_list))

def get_shop_info():
    shopid_list = get_id()
    base_url = 'http://www.dianping.com/shop/'
    for shop_id in shopid_list:
        info_url=base_url+shop_id+'/review_all'
        try:
            res=request_set(info_url)
            good_count = res.xpath('//label[@class="filter-item filter-good"]/span/text()')[0][1:-1]
            middle_count = res.xpath('//label[@class="filter-item filter-middle"]/span/text()')[0][1:-1]
            bad_count = res.xpath('//label[@class="filter-item filter-bad"]/span/text()')[0][1:-1]
            re_num = res.xpath('//span[@class="active"]/em/text()')[0][1:-1]
            pages = res.xpath('//div[@class="reviews-pages"]/a[last()-1]/text()')[0]
            tags=res.xpath('//div[@class="reviews-tags"]/div[@class="content"]//span/a/text()')
            for each in tags[:]:
                re_each=clear_text(each)
                tags.append(re_each)
                tags.remove(each)
            print(good_count,middle_count,bad_count,re_num,pages,tags)

            get_rewiew_info(res)

            if pages>=2:
                for i in range(2,pages+1):
                    rewiew_url=info_url+'/p'+str(i)
                    res = request_set(rewiew_url)
                    get_rewiew_info(res)
        except:
            print('请求异常')

def get_rewiew_info(res):
    texts = []
    try:
        soup = BeautifulSoup(res.text(), 'lxml')
        reviews = soup.find_all(class_='review-words Hide')
        for each in reviews:
            review = each.get_text()[:-7]
            texts.append(review)
        texts = clear_text(texts)
        print(texts)

    except:
        # 后期添加tenserflow识别验证码
        # if text.xpath('//div[@_slider__sliderTitle___119tD]/p') or text.xpath('yodaModuleWrapper'):
        #     print('需要验证码')
        #     sleep(10)
        #     # 如果是滑动验证，模拟滑动
        # else:
        print('页面爬取错误')

from lxml import etree
# get_shop_info()
text=open('test.txt',encoding='utf-8')
text1=etree.HTML(text.read())
get_rewiew_info(text1)


import pygame
import random
import sys

# INITIALIZE PYGAME.
pygame.init()

# CREATE THE GAME SCREEN.
SCREEN_WIDTH_IN_PIXELS = 400
SCREEN_HEIGHT_IN_PIXELS = 400
screen = pygame.display.set_mode((SCREEN_WIDTH_IN_PIXELS, SCREEN_HEIGHT_IN_PIXELS))

# DEFINE THE PLAYER'S INITIAL SCORE.
score = 0

# DEFINE THE PLAYER'S POSITION.
player_x = 200
player_y = 200

# DEFINE INFORMATION ABOUT THE COLLECTIBLE.
collectible_x = 100
collectible_y = 100
collectible_width_in_pixels = 20
collectible_height_in_pixels = 20
collectible_color = pygame.Color(255, 0, 0)

# RUN THE GAME UNTIL THE USER CHOOSES TO EXIT.
while True:
    # HANDLE ANY EVENTS SENT TO THE GAME PROGRAM.
    for event in pygame.event.get():
        # EXIT THE GAME IF THE USER CHOSE TO QUIT.
        if pygame.QUIT == event.type:
            # pygame.quit() may still leave the window up,
            # so sys.exit() is needed to fully close the program.
            pygame.quit()
            sys.exit()

        # MOVE THE PLAYER BASED ON KEYBOARD INPUT.
        PLAYER_MOVEMENT_AMOUNT_IN_PIXELS = 10
        if pygame.KEYDOWN == event.type:
            if pygame.K_UP == event.key:
                player_y -= PLAYER_MOVEMENT_AMOUNT_IN_PIXELS
            if pygame.K_DOWN == event.key:
                player_y += PLAYER_MOVEMENT_AMOUNT_IN_PIXELS
            if pygame.K_LEFT == event.key:
                player_x -= PLAYER_MOVEMENT_AMOUNT_IN_PIXELS
            if pygame.K_RIGHT == event.key:
                player_x += PLAYER_MOVEMENT_AMOUNT_IN_PIXELS

    # UPDATE THE POSITIONS OF OBJECTS IN THE GAME WORLD.
    # These need to be updated each frame because some objects might move.
    PLAYER_SIZE_IN_PIXELS = 20
    player_rectangle = pygame.Rect(
        player_x,
        player_y,
        PLAYER_SIZE_IN_PIXELS,
        PLAYER_SIZE_IN_PIXELS)
    collectible_rectangle = pygame.Rect(
        collectible_x,
        collectible_y,
        collectible_width_in_pixels,
        collectible_height_in_pixels)

    # COLLECT THE COLLECTIBLE IF THE PLAYER TOUCHES IT.
    player_touched_collectible = player_rectangle.colliderect(collectible_rectangle)
    if player_touched_collectible:
        # COUNT THE COLLECTIBLE IN THE PLAYER'S SCORE.
        score += 1
        print("Score: " + str(score))

        # RANDOMLY GENERATE A NEW COLLECTIBLE.
        MIN_COLLECTIBLE_SIZE_IN_PIXELS = 5
        MAX_COLLECTIBLE_SIZE_IN_PIXELS = 50
        collectible_width_in_pixels = random.randrange(
            MIN_COLLECTIBLE_SIZE_IN_PIXELS,
            MAX_COLLECTIBLE_SIZE_IN_PIXELS)
        collectible_height_in_pixels = random.randrange(
            MIN_COLLECTIBLE_SIZE_IN_PIXELS,
            MAX_COLLECTIBLE_SIZE_IN_PIXELS)

        MAX_COLLECTIBLE_X = SCREEN_WIDTH_IN_PIXELS - collectible_width_in_pixels
        collectible_x = random.randrange(MAX_COLLECTIBLE_X + 1)
        MAX_COLLECTIBLE_Y = SCREEN_HEIGHT_IN_PIXELS - collectible_height_in_pixels
        collectible_y = random.randrange(MAX_COLLECTIBLE_Y + 1)

        MAX_COLOR_COMPONENT = 255
        collectible_color.r = random.randrange(MAX_COLOR_COMPONENT + 1)
        collectible_color.g = random.randrange(MAX_COLOR_COMPONENT + 1)
        collectible_color.b = random.randrange(MAX_COLOR_COMPONENT + 1)

    # CLEAR THE SCREEN FOR THE NEW FRAME.
    BACKGROUND_COLOR = pygame.Color(0, 0, 0)
    screen.fill(BACKGROUND_COLOR)

    # DRAW THE PLAYER.
    PLAYER_COLOR = pygame.Color(255, 255, 255)
    pygame.draw.rect(
        screen,
        PLAYER_COLOR,
        player_rectangle)

    # DRAW THE COLLECTIBLE.
    pygame.draw.rect(
        screen,
        collectible_color,
        collectible_rectangle)

    # DISPLAY THE UPDATED FRAME.
    pygame.display.update()


import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def page():
    driver = webdriver.Chrome('/Users/jorge.rios/Downloads/chromedriver')  # Optional argument, if not specified will search path.
    driver.get("http://127.0.0.1:5000/")
    texto = ""
    with open('unonuevo.txt', 'r') as f:
        texto = f.readline()
    #assert "Python" in driver.title
    elem = driver.find_element_by_name("nombre")
    elem.clear()
    elem.send_keys(texto)
    time.sleep(5)
    elem = driver.find_element_by_name("edad")
    elem.send_keys("20")
    elem = driver.find_element_by_name("submit")
    elem.send_keys(Keys.RETURN)
    assert "No results found." not in driver.page_source
    #driver.close()
    time.sleep(5)
    driver.quit()

if __name__ == "__main__":
    page()


import os

import sys
import numpy as np
import cPickle as pickle
import argparse
import time
from subprocess import Popen, PIPE

from multiprocessing import Process, Queue

sys.path.append('./')
sys.path.append('../')

from environments import env_utils

import socket

import gc

def deleteIfExists(filename):
    if os.path.exists(filename):
        os.remove(filename)

def validSoln(state,soln,Environment):
    solnState = state
    for move in soln:
        solnState = Environment.next_state(solnState,move)

    return(Environment.checkSolved(solnState))

def fileListener(sock,heuristicFn,Environment):
    sock.listen(1)
    exampleState = np.expand_dims(Environment.generate_envs(1, [0, 0])[0][0],0)
    stateDim = exampleState.shape[1]

    maxBytes = 4096
    connection, client_address = sock.accept()
    while True:
        dataRec = connection.recv(8)
        while not dataRec:
            connection, client_address = sock.accept()
            dataRec = connection.recv(8)

        numBytesRecv = np.frombuffer(dataRec,dtype=np.int64)[0]

        #startTime = time.time()
        numBytesSeen = 0
        dataRec = ""
        while numBytesSeen < numBytesRecv:
            conRec = connection.recv(maxBytes)
            dataRec = dataRec + conRec
            numBytesSeen = numBytesSeen + len(conRec)

        states = np.frombuffer(dataRec,dtype=Environment.dtype)
        states = states.reshape(len(states)/stateDim,stateDim)
        #print("Rec Time: %s" % (time.time()-startTime))

        ### Run nnet
        #startTime = time.time()
        results = heuristicFn(states)
        #print("Heur Time: %s" % (time.time()-startTime))

        ### Write file
        #startTime = time.time()
        connection.sendall(results.astype(np.float32))
        #print("Write Time: %s" % (time.time()-startTime))

def dataListener(dataQueue,resQueue,gpuNum=None):
    nnet = nnet_utils.loadNnet(args.model_loc,args.model_name,useGPU,Environment,gpuNum=gpuNum)
    while True:
        data = dataQueue.get()
        nnetResult = nnet(data)
        resQueue.put(nnetResult)

parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, required=True, help="Name of input file")

parser.add_argument('--env', type=str, default='cube3', help="Environment: cube3, cube4")

parser.add_argument('--methods', type=str, default="kociemba,nnet", help="Which methods to use. Comma separated list")

parser.add_argument('--combine_outputs', action='store_true')

parser.add_argument('--model_loc', type=str, default="", help="Location of model")
parser.add_argument('--model_name', type=str, default="model.meta", help="Which model to load")

parser.add_argument('--nnet_parallel', type=int, default=200, help="How many to look at, at one time for nnet")
parser.add_argument('--depth_penalty', type=float, default=0.1, help="Coefficient for depth")
parser.add_argument('--bfs', type=int, default=0, help="Depth of breadth-first search to improve heuristicFn")

parser.add_argument('--startIdx', type=int, default=0, help="")
parser.add_argument('--endIdx', type=int, default=-1, help="")

parser.add_argument('--use_gpu', type=int, default=1, help="1 if using GPU")

parser.add_argument('--verbose', action='store_true', default=False, help="Print status to screen if switch is on")

parser.add_argument('--name', type=str, default="", help="Special name to append to file name")

args = parser.parse_args()

Environment = env_utils.getEnvironment(args.env)

if args.env.upper() == 'CUBE3':
    sys.path.append('./solvers/cube3/')
    from solver_algs import Kociemba
    from solver_algs import Optimal
elif args.env.upper() == 'CUBE4':
    sys.path.append('./solvers/cube4/')
    from solver_algs import Optimal
elif args.env.upper() == 'PUZZLE15':
    sys.path.append('./solvers/puzzle15/')
    from solver_algs import Optimal
elif args.env.upper() == 'PUZZLE24':
    sys.path.append('./solvers/puzzle24/')
    from solver_algs import Optimal

useGPU = bool(args.use_gpu)

methods = [x.lower() for x in args.methods.split(",")]
print("Methods are: %s" % (",".join(methods)))

### Load starting states
filename = args.input
outFileLoc_pre = "../testData/%s/%s_nnetPar%s_depthP%s_bfs%s_%s_solved" % (args.env.lower(),filename.split("/")[-1].split(".")[0],args.nnet_parallel,args.depth_penalty,args.bfs,"_".join(methods))

if args.endIdx != -1:
    outFileLoc_pre = "%s_%i_%i" % (outFileLoc_pre,args.startIdx,args.endIdx)

if args.name != "":
    outFileLoc_pre = "%s_%s" % (outFileLoc_pre,args.name)

outFileLoc = "%s.pkl" % (outFileLoc_pre)
print >> sys.stderr, "Input File: %s, Output File: %s" % (filename,outFileLoc)

inputData = pickle.load(open(filename,"rb"))

states = inputData['states']

if args.endIdx == -1:
    args.endIdx = len(states)

states = states[args.startIdx:args.endIdx]

### Load nnet if needed
if "nnet" in methods:
    from ml_utils import nnet_utils
    from ml_utils import search_utils

    os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
    if len(os.environ['CUDA_VISIBLE_DEVICES']) > 1:
        gpuNums = [int(x) for x in os.environ['CUDA_VISIBLE_DEVICES'].split(",")]
    else:
        gpuNums = [None]
    numParallel = len(gpuNums)

    ### Initialize files
    dataQueues = []
    resQueues = []
    for num in range(numParallel):
        dataQueues.append(Queue(1))
        resQueues.append(Queue(1))

        dataListenerProc = Process(target=dataListener, args=(dataQueues[num],resQueues[num],gpuNums[num],))
        dataListenerProc.daemon = True
        dataListenerProc.start()

    def heuristicFn_nnet(x):
        ### Write data
        parallelNums = range(min(numParallel,x.shape[0]))
        splitIdxs = np.array_split(np.arange(x.shape[0]),len(parallelNums))
        for num in parallelNums:
            dataQueues[num].put(x[splitIdxs[num]])

        ### Check until all data is obtaied
        results = [None]*len(parallelNums)
        for num in parallelNums:
            results[num] = resQueues[num].get()

        results = np.concatenate(results)

        return(results)

    socketName = "%s_socket" % (outFileLoc_pre)
    try:
        os.unlink(socketName)
    except OSError:
        if os.path.exists(socketName):
            raise

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(socketName)

    fileListenerProc = Process(target=fileListener, args=(sock,heuristicFn_nnet,Environment,))
    fileListenerProc.daemon = True
    fileListenerProc.start()

### Get solutions
data = dict()
data["states"] = states
data["solutions"] = dict()
data["times"] = dict()
data["nodesGenerated_num"] = dict()
for method in methods:
    data["solutions"][method] = [None]*states.shape[0]
    data["times"][method] = [None]*states.shape[0]
    data["nodesGenerated_num"][method] = [None]*states.shape[0]

def runMethods(idx_state):
    idx, state = idx_state
    stateStr = " ".join([str(x) for x in state])
    #print(stateStr)
    for method in methods:
        start_time = time.time()
        if method == "kociemba":
            soln = Kociemba.solve(state)
            nodesGenerated_num = 0

            elapsedTime = time.time() - start_time
        elif method == "nnet":
            runType = "python"
            #if (args.env.upper() in ['CUBE3','PUZZLE15','PUZZLE24','PUZZLE35']) or ('LIGHTSOUT' in args.env.upper()):
            #    runType = "cpp"
            #else:
            #    runType = "python"
            if runType == "cpp":
                popen = Popen(['./ml_utils/parallel_weighted_astar',stateStr,str(args.depth_penalty),str(args.nnet_parallel),socketName,args.env], stdout=PIPE, stderr=PIPE, bufsize=1, universal_newlines=True)
                lines = []
                for stdout_line in iter(popen.stdout.readline, ""):
                    stdout_line = stdout_line.strip('\n')
                    lines.append(stdout_line)
                    if args.verbose:
                        sys.stdout.write("%s\n" % (stdout_line))
                        sys.stdout.flush()

                moves = [int(x) for x in lines[-3].split(" ")[:-1]]
                soln = [Environment.legalPlays[x] for x in moves][::-1]
                nodesGenerated_num = int(lines[-1])
            else:
                BestFS_solve = search_utils.BestFS_solve([state],heuristicFn_nnet,Environment,bfs=args.bfs)
                isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(numParallel=args.nnet_parallel,depthPenalty=args.depth_penalty,verbose=args.verbose)
                BestFS_solve = []
                del BestFS_solve
                gc.collect()

                soln = solveSteps[0]
                nodesGenerated_num = nodesGenerated_num[0]

            elapsedTime = time.time() - start_time
        elif method == "optimal":
            soln, nodesGenerated_num, elapsedTime = Optimal.solve(state)
        else:
            continue

        data["times"][method][idx] = elapsedTime
        data["nodesGenerated_num"][method][idx] = nodesGenerated_num

        assert(validSoln(state,soln,Environment))

        data["solutions"][method][idx] = soln

print("%i total states" % (states.shape[0]))
if 'optimal' in methods:
    solutions, nodesGenerated_num, times = Optimal.solve(states,args.startIdx,args.endIdx,args.combine_outputs)
    data["solutions"]['optimal'] = solutions
    data["times"]['optimal'] = times
    data["nodesGenerated_num"]['optimal'] = nodesGenerated_num
    for state,soln in zip(states,data["solutions"]['optimal']):
        if soln is not None:
            assert(validSoln(state,soln,Environment))
else:
    for idx,state in enumerate(states):
        runMethods((idx,state))

        solveStr = ", ".join(["len/time/#nodes - %s: %i/%.2f/%i" % (method,len(data["solutions"][method][idx]),data["times"][method][idx],data["nodesGenerated_num"][method][idx]) for method in methods])
        print >> sys.stderr, "State: %i, %s" % (idx,solveStr)

### Save data
pickle.dump(data, open(outFileLoc, "wb"), protocol=1)

### Print stats
for method in methods:
    solnLens = np.array([len(soln) for soln in data["solutions"][method]])
    times = np.array([solveTime for solveTime in data["times"][method]])
    nodesGenerated_num = np.array([solveTime for solveTime in data["nodesGenerated_num"][method]])
    print("%s: Soln len - %f(%f), Time - %f(%f), # Nodes Gen - %f(%f)" % (method,np.mean(solnLens),np.std(solnLens),np.mean(times),np.std(times),np.mean(nodesGenerated_num),np.std(nodesGenerated_num)))


#!/usr/bin/env python
#
# setup.py -- Installation for djblets
#
# Copyright (C) 2008 Christian Hammond
# Copyright (C) 2007-2008 David Trowbridge
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import subprocess
import sys
import os

try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

from distutils.core import Command
from setuptools.command.egg_info import egg_info
from setuptools.command.test import test

from djblets import django_version, get_package_version, is_release, VERSION

# Make sure this is a version of Python we are compatible with. This should
# prevent people on older versions from unintentionally trying to install
# the source tarball, and failing.
if sys.hexversion < 0x02060000:
    sys.stderr.write('This version of Djblets is incompatible with your '
                     'version of Python.\n')
    sys.exit(1)

class BuildEggInfo(egg_info):
    def run(self):
        if ('sdist' in sys.argv or
            'bdist_egg' in sys.argv or
            'install' in sys.argv):
            self.run_command('build_media')
            self.run_command('build_i18n')

        egg_info.run(self)

class BuildMedia(Command):
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        retcode = subprocess.call([
            sys.executable, 'contrib/internal/build-media.py'])

        if retcode != 0:
            raise RuntimeError('Failed to build media files')

class BuildI18n(Command):
    description = 'Compile message catalogs to .mo'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        # If we are attempting to build on a system without an
        # existing copy of Djblets installed in a reachable
        # location (such as distribution packaging), we need to
        # ensure that the source directory is in the PYTHONPATH
        # or the import of djblets.util.filesystem will fail.
        current_path = os.getenv("PYTHONPATH")
        if current_path:
            os.putenv("PYTHONPATH", "%s:%s" % (current_path, os.getcwd()))
        else:
            os.putenv("PYTHONPATH", os.getcwd())

        retcode = subprocess.call([
            sys.executable, 'contrib/internal/build-i18n.py'])

        if retcode != 0:
            raise RuntimeError('Failed to build i18n files')

def run_tests(*args):
    import os
    os.system("tests/runtests.py")

test.run_tests = run_tests

cmdclasses = {
    'egg_info': BuildEggInfo,
    'build_media': BuildMedia,
    'build_i18n': BuildI18n,
}

PACKAGE_NAME = 'Djblets'

if is_release():
    download_url = 'http://downloads.reviewboard.org/releases/%s/%s.%s/' % \
                   (PACKAGE_NAME, VERSION[0], VERSION[1])
else:
    download_url = 'http://downloads.reviewboard.org/nightlies/'

setup(name=PACKAGE_NAME,
      version=get_package_version(),
      test_suite="dummy",
      license="MIT",
      description="A collection of useful classes and functions for Django",
      packages=find_packages(exclude=["tests"]),
      cmdclass=cmdclasses,
      install_requires=[
          django_version,
          'django-pipeline>=1.3.23,<1.3.9999',
          'feedparser>=5.1.2',
          'pillowfight',
          'pytz',
      ],
      dependency_links=[
          "http://downloads.reviewboard.org/mirror/",
          download_url,
      ],
      include_package_data=True,
      zip_safe=False,
      maintainer="Christian Hammond",
      maintainer_email="christian@beanbaginc.com",
      url="https://www.reviewboard.org/downloads/djblets/",
      download_url=download_url,
      classifiers=[
          "Development Status :: 4 - Beta",
          "Environment :: Web Environment",
          "Framework :: Django",
          "Intended Audience :: Developers",
          "License :: OSI Approved :: MIT License",
          "Operating System :: OS Independent",
          "Programming Language :: Python",
          "Topic :: Software Development",
          "Topic :: Software Development :: Libraries :: Python Modules",
      ])


from __future__ import print_function

import re
import numpy as np
import pandas as pd

from scipy import sparse
from functools import lru_cache
from collections import Counter
from nltk.corpus import stopwords
from pymorphy2 import MorphAnalyzer
from gensim.models import KeyedVectors, Word2Vec
from sklearn.feature_extraction.text import CountVectorizer

STOP_WORDS = list(set(stopwords.words("russian")) | set(stopwords.words("english")))
morph = MorphAnalyzer()
rgx_remove_sym = re.compile(r'[^\w]')

P = "paragraph"  # text
Q = "question"  # text

class FeatureBuilder(object):
    def __init__(self, args):
        # self.w2v_model = KeyedVectors.load_word2vec_format(args.w2v, encoding="utf8", unicode_errors="ignore", binary=True)
        self.w2v_model = None
        self.w2v_pos = ["VERB", "NOUN", "NUMR", "INFN", "GRND", "ADJS", "ADJF", "COMP"]
        self.undefined_pos = Counter()

    def normalize(self, sentence):
        """
        remove symbols -> split by word -> lowercase -> lemmatize -> remove stop words
        :param sentence:
        :return: normalized sentence
        """
        tokens = []
        for word in rgx_remove_sym.sub(" ", sentence).split():
            try:
                token = morph.parse(word.lower())[0]
                if token.normal_form in STOP_WORDS:
                    continue
                tokens.append(token)
            except Exception as ex:
                print("[-] Failed to normalize {} word. Error is \n{}".format(word, ex))
                continue
        return tokens

    @lru_cache()
    def get_norm_pos(self, pos):
        if pos in self.w2v_pos:
            return pos
        elif pos == "NUM":
            return "NUMR"
        else:
            self.undefined_pos.update(pos)
            return None

    def get_log_tf_sum(self, s1, s2):
        tf = Counter()
        [tf.update(x) for x in s1]
        [tf.update(x) for x in s2]

        tf_scores = []
        for w in set(s1) & set(s2):
            w_max = tf.most_common(1)[0][1]
            tf_scores.append(0.5 + 0.5 * (tf.get(w, 0.0) / w_max))

        return np.sum(tf_scores, dtype=np.float32)

    def compute_sim(self, row):
        p_tokens = self.normalize(row[P])
        q_tokens = self.normalize(row[Q])

        q_tokens_size = len(q_tokens)
        cosine_similarities = []

        skipped = 0
        normalized_p = []
        normalized_q = []

        for index, p_token in enumerate(p_tokens):
            try:
                normalized_p.append(p_token.normal_form)

                if index > (q_tokens_size - 1):
                    break
                else:
                    q_token = q_tokens[index]
                    q_pos = self.get_norm_pos(q_token.tag.POS)

                p_pos = self.get_norm_pos(p_token.tag.POS)

                if not p_pos or not q_pos:
                    skipped += 1
                    continue

                p_lemma_poc = "%s_%s" % (p_token.normal_form, p_pos)
                q_lemma_poc = "%s_%s" % (q_token.normal_form, q_pos)

                normalized_q.append(q_token.normal_form)

                w2v_sim_score = self.w2v_model.wv.similarity(p_lemma_poc, q_lemma_poc)
                cosine_similarities.append(w2v_sim_score)
            except Exception as ex:
                skipped += 1
                continue

        cos_sim_sum = np.sum(cosine_similarities, dtype=np.float32)
        idfs_intersection_sum = self.get_log_tf_sum(normalized_p, normalized_q)

        return pd.Series([cos_sim_sum, idfs_intersection_sum], dtype=np.float32)

    def prepare_w2v_model(self, df):
        all_sentences = []
        self.w2v_model = Word2Vec()

    def cosine_sim(self, row):
        pass


# coding: utf-8

# In[12]:

import pandas as pd
import numpy as np
from scipy.stats import chi2_contingency

# In[45]:

def load_data():
    df = pd.DataFrame([
        ['Amphibian',7,72,0.911392],
        ['Bird',75,	413,0.846311],
        ['Fish',11,	115,0.912698],
        ['Mammal',30,146,0.829545],
        ['Nonvascular Plant',5,328,0.984985],
        ['Reptile',	5,73,0.935897],
        ['Vascular Plant',46,4216,0.989207] ],
        columns=['category','not_protected','protected','percent_protected'])
    return df

# In[46]:

def chi(cat1, cat2,contingency):
    chi2, pval, dof, expected = chi2_contingency(contingency)
    if pval<0.05:
        print("significant difference exists between", cat1," and ", cat2)

# In[47]:

#_main_chi_study
df=load_data()
for row in range (0,len(df)-1):
    cat1=df.loc[row][0]
    for row2 in range(1,len(df)):
        cat2=df.loc[row2][0]
        # calculating contingency
        contingency = [df.loc[row][1],df.loc[row][2]],[df.loc[row+1][1],df.loc[row+1][2]]
        chi(cat1,cat2,contingency)

# In[ ]:


from collections import defaultdict

with open("../inputs/01.txt") as f_in:
    freqs = list(int(line.rstrip()) for line in f_in.readlines())
    d = defaultdict(int)
    freq = 0
    d[freq] += 1
    found = False
    while not found:
        for frq in freqs:
            freq += frq
            d[freq] += 1
            if d[freq] >= 2:
                print(freq)
                found = True
                break


import os
import contextlib
import tensorflow as tf
import os
import codecs
import numpy as np
from tqdm import tqdm
import tensorflow as tf
from tensorflow.python import keras
import tensorflow.keras.backend as K
from keras_bert import Tokenizer
from keras_bert import get_custom_objects
from keras_bert import load_trained_model_from_checkpoint
from sklearn.metrics import accuracy_score, f1_score

USE_TPU = False

if USE_TPU:
  TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
  resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)
  tf.contrib.distribute.initialize_tpu_system(resolver)
  strategy = tf.contrib.distribute.TPUStrategy(resolver)

dataset = tf.keras.utils.get_file(
    fname="20news-18828.tar.gz",
    origin="http://qwone.com/~jason/20Newsgroups/20news-18828.tar.gz",
    extract=True,
)

SEQ_LEN = 128
BATCH_SIZE = 16
EPOCHS = 4
LR = 2e-5

pretrained_path = "D:/data/uncased_L-12_H-768_A-12"
config_path = os.path.join(pretrained_path, 'bert_config.json')
checkpoint_path = os.path.join(pretrained_path, 'bert_model.ckpt')
vocab_path = os.path.join(pretrained_path, 'vocab.txt')

token_dict = {}
with codecs.open(vocab_path, 'r', 'utf8') as reader:
    for line in reader:
        token = line.strip()
        token_dict[token] = len(token_dict)

tokenizer = Tokenizer(token_dict)

def load_data(path, tagset):
    """
    Input:
      path: Root directory where the categorical data sit in folders.
      tagset: List of folder-name, category tuples.
    Output:
      train_x / test_x: List with two items (viz token_input, seg_input)
      test_x / test_y: Output labels corresponding to trainx / train_y.
    """
    global tokenizer
    indices, labels = [], []
    for folder, label in tagset:
        folder = os.path.join(path, folder)
        for name in tqdm(os.listdir(folder)):
            with open(os.path.join(folder, name), 'r', encoding="utf-8", errors='ignore') as reader:
                text = reader.read()
            ids, segments = tokenizer.encode(text, max_len=SEQ_LEN)
            indices.append(ids)
            labels.append(label)

    items = list(zip(indices, labels))
    np.random.shuffle(items)
    indices, labels = zip(*items)
    indices = np.array(indices)
    mod = indices.shape[0] % BATCH_SIZE
    if mod > 0:
        indices, labels = indices[:-mod], labels[:-mod]
    return [indices, np.zeros_like(indices)], np.array(labels)

path = os.path.join(os.path.dirname(dataset), '20news-18828')
tagset = [(x, i) for i, x in enumerate(os.listdir(path))]
id_to_labels = {id_: label for label, id_ in tagset}

all_x, all_y = load_data(path, tagset)

train_perc = 0.8
total = len(all_y)

n_train = int(train_perc * total)
n_test = (total - n_train)

test_x = [all_x[0][n_train:], all_x[1][n_train:]]
train_x = [all_x[0][:n_train], all_x[1][:n_train]]

train_y, test_y = all_y[:n_train], all_y[n_train:]

print("# Total: %s, # Train: %s, # Test: %s" % (total, n_train, n_test))

# Load pretrained model
with strategy.scope() if USE_TPU else contextlib.suppress():
    model = load_trained_model_from_checkpoint(
        config_path,
        checkpoint_path,
        training=True,
        trainable=True,
        seq_len=SEQ_LEN,
    )

    # Add dense layer for classification
    inputs = model.inputs[:2]
    dense = model.get_layer('NSP-Dense').output
    outputs = keras.layers.Dense(units=20, activation='softmax')(dense)
    model = keras.models.Model(inputs, outputs)

    model.compile(
        optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['sparse_categorical_accuracy']
    )

print(model.summary())

# sess = K.get_session()
# uninitialized_variables = set([i.decode('ascii') for i in sess.run(tf.report_uninitialized_variables())])
# init_op = tf.variables_initializer(
#     [v for v in tf.global_variables() if v.name.split(':')[0] in uninitialized_variables]
# )
# sess.run(init_op)

history = model.fit(
    train_x,
    train_y,
    epochs=EPOCHS,
    batch_size=BATCH_SIZE,
    validation_split=0.20,
    shuffle=True,
    verbose=1
)

predicts = model.predict(test_x, verbose=True).argmax(axis=-1)
accuracy = accuracy_score(test_y, predicts)
macro_f1 = f1_score(test_y, predicts, average='macro')
micro_f1 = f1_score(test_y, predicts, average='micro')
weighted_f1 = f1_score(test_y, predicts, average='weighted')

print("Accuracy: %s" % accuracy)
print ('macro_f1: %s\nmicro_f1:%s\nweighted_f1:%s' %(
    macro_f1, micro_f1, weighted_f1)
)


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

from flask import Blueprint, render_template, abort, session, redirect
from jinja2 import TemplateNotFound

from main import app, db

index_page = Blueprint('index_page', __name__,
                        template_folder='templates',
                      )

@index_page.route('/', defaults={'page': 'index'})
@index_page.route('/<string:page>')
def index(page):
    try:
        return render_template('%s.html' % page)
    except TemplateNotFound:
        abort(404)

@index_page.route('/news')
def news():
    videos = map(lambda x: x[2], os.walk(os.path.join(app.root_path, 'static/video')))
    videos = map(lambda x: x.split("."), videos[0])
    return render_template('news.html', videos=videos)

@index_page.route('/videos/<string:ids>/<string:types>')
def videos(ids, types):
    return render_template('video.html', ids=ids, types=types)

@index_page.route('/work')
def work():
    imgs0 = os.listdir(os.path.join(app.root_path, 'static/img/portfolio/0'))
    imgs1 = os.listdir(os.path.join(app.root_path, 'static/img/portfolio/1'))
    imgs2 = os.listdir(os.path.join(app.root_path, 'static/img/portfolio/2'))
    imgs3 = os.listdir(os.path.join(app.root_path, 'static/img/portfolio/3'))
    return render_template('work.html', imgs=[imgs0, imgs1, imgs2, imgs3], enumerate=enumerate)

@index_page.route('/subscribe/1')
def subscribe1():
    cur = db.connection.cursor()
    cur.execute("""select type,description from product_type;""")
    info = cur.fetchall()
    try:
        return render_template('subscribe/subscribe1.html', info=info)
    except TemplateNotFound:
        abort(404)

@index_page.route('/subscribe/2')
def subscribe2():
    if not session.get('subscribe_type', None):
        return redirect('/subscribe/1')
    try:
        return render_template('subscribe/subscribe2.html')
    except TemplateNotFound:
        abort(404)

app.register_blueprint(index_page)


import jieba
import pandas as pd
from gensim import corpora
from tensorflow import keras
from sklearn.preprocessing import LabelEncoder
import numpy as np
import tensorflow as tf
from gensim.models.doc2vec import Doc2Vec,LabeledSentence

file_path="C:\Python\spce\yask\gongdan.csv"
stopwords_path="C:\Python\spce\yask\stopword.txt"
model_path=""

#分词方法
def chinese_word_cut(text):
    return ' '.join(jieba.cut(text))
#获取停止词
def load_stopword(stopwords_path):
    f_stop=open(stopwords_path)
    sw=[line.strip() for line in f_stop]
    f_stop.close()
    return sw
#处理并获取训练数据
def get_train_data(X,Y,Vocab_lenth,n_classes):
    texts=X
    encoded_docs = [keras.preprocessing.text.one_hot(str(d), Vocab_lenth) for d in texts]
    max_length = 600
    x_train =  keras.preprocessing.sequence.pad_sequences(encoded_docs, maxlen=max_length, padding='post')
    encoder = LabelEncoder()
    y_train = encoder.fit_transform(Y)
    # y_train=keras.utils.to_categorical(encoded_Y,num_classes=n_classes)
    # y_train = tf.one_hot(indices=tf.cast(encoded_Y, tf.int32), depth=n_classes)
    return x_train,y_train

def cnn_model_fn(features, labels, mode):
  """Model function for CNN."""
  # Input Layer
  input_layer = tf.reshape(features["x"], [-1, 600])
  embedding = tf.get_variable('embedding', [17171, 64])
  embedding_inputs = tf.nn.embedding_lookup(embedding, input_layer)
  # Convolutional Layer #1
  conv1 = tf.layers.conv1d(
      inputs=embedding_inputs,
      filters=256,
      kernel_size=5,
      padding="same",
      activation=tf.nn.relu)

  # Pooling Layer #1
  pool1=tf.reduce_max(conv1, reduction_indices=[1], name='gmp')

  # Dense Layer
  dense = tf.layers.dense(pool1, 128, name='fc1')
  dropout = tf.layers.dropout(
      inputs=dense, rate=0.5, training=mode == tf.estimator.ModeKeys.TRAIN)

  # Logits Layer
  logits = tf.layers.dense(inputs=dropout, units=n_classes)
  predictions = {
      # Generate predictions (for PREDICT and EVAL mode)
      "classes": tf.argmax(input=logits, axis=1),
      # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
      # `logging_hook`.
      "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
  }

  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

  # Calculate Loss (for both TRAIN and EVAL modes)
  # labels = tf.reshape(labels, [-1,1])
  # loss = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits)
  loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

  # Configure the Training Op (for TRAIN mode)
  if mode == tf.estimator.ModeKeys.TRAIN:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
    train_op = optimizer.minimize(
        loss=loss,
        global_step=tf.train.get_global_step())
    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

  # Add evaluation metrics (for EVAL mode)
  eval_metric_ops = {
      "accuracy": tf.metrics.accuracy(
          labels=labels, predictions=predictions["classes"])}
  return tf.estimator.EstimatorSpec(
      mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

if __name__ == '__main__':
    #原始文本处理
    stop_words = load_stopword(stopwords_path)
    data=pd.read_csv(file_path,encoding='gb18030',nrows=10000)
    data['content_cuted']=data['工单详情'].apply(chinese_word_cut)
    data['content_cuted'] = [[word for word in line.strip().lower().split() if word not in stop_words] for line in data['content_cuted']]

    M = len(data['content_cuted'])

    print('文本数目：%d个' % M)
    #获取训练文本的字典和长度
    dictionary = corpora.Dictionary(data['content_cuted'])
    Vocab_lenth = len(dictionary)
    # print('词的个数：', V)
    # corpus = [dictionary.doc2bow(text) for text in data['content_cuted']]

#DOC2VEC的向量表示
    # class LabeledLineSentence(object):
    #     def __init__(self, filename):
    #         self.filename = filename
    #     def __iter__(self):
    #         for uid, line in enumerate(data['content_cuted']):
    #             yield LabeledSentence(words=line, tags=['SENT_%s' % uid])
    #
    # sentences=LabeledLineSentence('')
    # model=Doc2Vec(sentences,size=100,window=3,min_count=1)
    # # model.save(model_path)
    # model.delete_temporary_training_data(keep_doctags_vectors=True, keep_inference=True)
    #获取文本和标签
    n_classes=len(data.groupby('工单大类'))
    x_train,y_train=get_train_data(data['content_cuted'],data['工单大类'],Vocab_lenth,n_classes)
    print('获取训练数据')
    #构建模型并训练
    # model=keras.models.Sequential()
    # model.add(keras.layers.Embedding(Vocab_lenth, 64, input_length=100))
    # model.add(keras.layers.LSTM(128,return_sequences=True))
    # model.add(keras.layers.LSTM(128))
    # model.add(keras.layers.Dropout(0.2))
    # model.add(keras.layers.Dense(128,activation='relu'))
    # model.add(keras.layers.Dropout(0.2))
    # model.add(keras.layers.Dense(n_classes,activation='softmax'))
    # # sgd=keras.optimizers.SGD(lr=0.01,nesterov=True)
    #
    # model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
    # model.fit(x_train, y_train, batch_size=64, epochs=5)

# Create the Estimator
    mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="C:\Python\spce\yask/text-model/cnn")
    tensors_to_log = {"probabilities": "softmax_tensor"}
    logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=2)
    train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": x_train},y=y_train,batch_size=100,num_epochs=None,shuffle=False)
    mnist_classifier.train(input_fn=train_input_fn,steps=200,hooks=[logging_hook])

    eval_input_fn = tf.estimator.inputs.numpy_input_fn( x={"x": x_train},y=y_train, num_epochs=1,shuffle=False)
    eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
    print(eval_results)
    print('完成')


import time

import pygame
import random

WIDTH = 1000
HEIGHT = 500
FPS = 30

#Инициализация

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rock-Paper-Scissors")
clock = pygame.time.Clock()

#Создание цветов

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

#Создание сущностей

#кнопка

button = pygame.image.load('button.jpg')
buttonrect = button.get_rect()

#текст

font = pygame.font.SysFont('serif', 50)
user_text  = font.render('Вы выиграли', True,  GREEN)
bot_text = font.render('Вы проиграли', True, RED)
tie_text = font.render('Ничья', True, BLUE)

#бумага

paper_img = pygame.image.load('paper.jpg')
paper = pygame.transform.scale(paper_img, (220, 220))
paperrect = paper.get_rect()

paper_dubl = pygame.transform.scale(paper_img, (220, 220))
paperrect_dubl = paper_dubl.get_rect()

#камень

rock_img = pygame.image.load('rock.jpg')
rock = pygame.transform.scale(rock_img, (220, 220))
rockrect = rock.get_rect()

rock_dubl = pygame.transform.scale(rock_img, (220, 220))
rockrect_dubl = rock_dubl.get_rect()

#ножницы

scissors_img = pygame.image.load('scissors2.jpg')
scissors = pygame.transform.scale(scissors_img, (220, 220))
scissorsrect = scissors.get_rect()

scissors_dubl = pygame.transform.scale(scissors_img, (220, 220))
scissorsrect_dubl = scissors_dubl.get_rect()

#результат

def game(user_choice, bot_choice) :
    running = True
    while running :

        clock.tick(FPS)
        screen.fill(WHITE)

        #цикл событий

        for event in pygame.event.get() :
            if event.type == pygame.QUIT :
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN :
                if event.button == 1 and 424 <= event.pos[0] <= 600 and 200 <= event.pos[1] <= 244 :
                    running = False

        #отрисовка результата

        if user_choice == bot_choice - 1 or user_choice == bot_choice + 2:
            screen.blit(user_text, (357.5, 50))
        elif user_choice == bot_choice :
            screen.blit(tie_text, (433, 50))
        else :
            screen.blit(bot_text, (348, 50))

        #отрисовка (default)

        buttonrect.x = 424
        buttonrect.y = 200
        screen.blit(button, buttonrect)
        if user_choice == 1 :
            paperrect.x = 180
            screen.blit(paper, paperrect)
        if user_choice == 2 :
            rockrect.x = 180
            screen.blit(rock, rockrect)
        if user_choice == 3 :
            scissorsrect.x = 180
            screen.blit(scissors, scissorsrect)
        if bot_choice == 1 :
            paperrect_dubl.x = 600
            screen.blit(paper_dubl, paperrect_dubl)
        if bot_choice == 2:
            rockrect_dubl.x = 600
            screen.blit(rock_dubl, rockrect_dubl)
        if bot_choice == 3 :
            scissorsrect_dubl.x = 600
            screen.blit(scissors_dubl, scissorsrect_dubl)
        pygame.display.flip()

# Начало игры

running = True
while running :

    #позиционирование обьектов

    paperrect.x = 85
    paperrect.y = 140
    rockrect.x = 390
    rockrect.y = 130
    scissorsrect.x = 695
    scissorsrect.y = 140

    paperrect_dubl.y = 140
    rockrect_dubl.y = 130
    scissorsrect_dubl.y = 140

    #цикл событий

    clock.tick(FPS)
    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN :
            if event.button == 1 :
                if 85 <= event.pos[0] <= 305 and 140 <= event.pos[1] <= 360 :
                    #paper.set_alpha(100)
                    game(1, random.randint(1, 3))
                if 390 <= event.pos[0] <= 610 and 140 <= event.pos[1] <= 360:
                    #rock.set_alpha(100)
                    game(2, random.randint(1, 3))
                if 695 <= event.pos[0] <= 915 and 140 <= event.pos[1] <= 360:
                    #scissors.set_alpha(100)
                    game(3, random.randint(1, 3))

    #отрисовка

    screen.fill(WHITE)
    screen.blit(paper, paperrect)
    screen.blit(rock, rockrect)
    screen.blit(scissors, scissorsrect)
    pygame.display.flip()

pygame.quit()


# Importing libraries
import tensorflow as tf
import tensorflow.keras.backend as K
import numpy as np

from ResNet50 import ResNet50
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

K.set_image_data_format('channels_last')
K.set_learning_phase(1)

gpu_list = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpu_list[0], True)
print("Number of  GPUs: ", len(gpu_list))

# Model initialization
filters = np.array([64,64,256])
input_img, outputs =  ResNet50(filters = filters, classes = 2, input_shape = (256, 256, 3))
model = Model(inputs = [input_img], outputs = [outputs])

# Compiling the model
model.compile(optimizer = Adam(lr=0.002, epsilon=1e-08, decay=0.0),
              loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Setting up checkpoint callback to save the model during training
checkpoint = ModelCheckpoint('C:/Keras_Tutorial/Casia/Entire_Casia_2/Saved_Models/ResNet-50/model_RNN_256by256input_BS32_Cr_LBP_Lr_point004_sparsecross_binarylabels_New_140_Epochs.model',
                                     verbose=2,
                                     monitor='val_loss',
		                             mode='min',
                                     save_weights_only=False,
                                     save_best_only=True)

# Train and Test data paths
train_path = 'C:/Keras_Tutorial/Casia/train_valid_combined/Cr_LBP'
test_path = 'C:/Keras_Tutorial/Casia/test/Cr_LBP'

# Batch size and target size
batch_size = 32
target_size = (256,256)

# Loading and augmenting data using ImageData Generator
trainloader = ImageDataGenerator(dtype = 'uint8', horizontal_flip=True,
    vertical_flip=True).flow_from_directory(directory = train_path, target_size = (256,256), classes = ['Fake', 'Real'], batch_size = 32, class_mode = 'binary')

# Augmentations are only applied to the train data

test_loader = ImageDataGenerator(dtype = 'uint8', ).flow_from_directory(directory = test_path, target_size = (256,256), classes = ['Fake', 'Real'], batch_size =32, class_mode = 'binary')
# Total number of epochs for training
epochs = 40

# Training the model
results =  model.fit_generator(trainloader, validation_data = test_loader, steps_per_epoch = trainloader.n//trainloader.batch_size, validation_steps = test_loader.n//test_loader.batch_size, epochs = epochs) #callbacks = [checkpoint]

# Accuracy and Loss graphs

figure, axes = plt.subplots(2, 1, figsize=(6, 6))
axes[0].plot(results.history['loss'], label="TrainLoss")
axes[0].plot(results.history['val_loss'], label="ValLoss")
axes[0].legend(loc='best', shadow=True)
axes[1].plot(results.history['acc'], label="TrainAcc")
axes[1].plot(results.history['val_acc'], label="ValAcc")
axes[1].legend(loc='best', shadow=True)
plt.show()