# -*- coding: utf-8 -*-
from odoo import models, fields, api, _ # type: ignore
import logging

logger = logging.getLogger(__name__)

class SubmittedTestVariable(models.Model):
    _name = 'terralab.submittedtestvariable'
    _inherit = ['mail.thread']
    _description = 'TerraLab Submitted Test Variable'

    submitted_sample = fields.Many2one('terralab.submittedsample', 'Submitted Sample', track_visibility='onchange') # Every Submitted Test Variable is attached to a specific Submitted Sample
    submitted_test = fields.Many2one('terralab.submittedtest', 'Submitted Test', track_visibility='onchange') # Every Submitted Test Variable is attached to a specific Submitted Test
    test_variable_type = fields.Many2one('terralab.testvariabletype', 'Test Variable Type', track_visibility='onchange') # Every Submitted Test Variable is a specific Test Variable Type
    order = fields.Many2one('sale.order', 'Order', track_visibility='onchange') # Submitted Test Variable is always attached to an Order
    order_name = fields.Char(compute='_get_order_fields', store=True, track_visibility='onchange')
    order_terralab_status = fields.Char(compute='_get_order_fields', store=True, track_visibility='onchange')
    num = fields.Integer(compute='_get_num', store=True, track_visibility='onchange')
    value = fields.Char(track_visibility='onchange')
    name = fields.Char(compute='_get_name', store=True, translate=True, track_visibility='onchange')

    @api.depends('test_variable_type')
    def _get_num(self):
        for item in self:
            if item.test_variable_type:
                item.num = item.test_variable_type.num
            else:
                item.num = None

    @api.depends('submitted_test', 'test_variable_type', 'value')
    def _get_name(self):
        for item in self:
            if item.submitted_test and item.test_variable_type:
                # This is ugly, sorry.
                for submitted_test in item.submitted_test:
                    for submitted_test_id, submitted_test_name in submitted_test.name_get():
                        item.name = '%s %s' % (submitted_test_name, item.test_variable_type.name)
            else:
                item.name = ''

    @api.depends('order', 'order.name', 'order.terralab_status')
    def _get_order_fields(self):
        for item in self:
            if item.order:
                item.order_name = item.order.name
                item.terralab_status = item.order.terralab_status
            else:
                item.order_name = ''
                item.terralab_status = ''

    # What's the next required action for this submitted_test variable?
    def compute_terralab_next_action(self, order_terralab_status):
        if order_terralab_status in ('submitted', 'accepted', 'rejected'):
            if self.value == '' or self.value == False:
                return _('Add value for submitted test variable %s') % (self.name)
        # No action required for this test variable
        return ''

    # When this variable is modified, pass on the chatter message to the Order object
    def _message_log(self, *args, **kwargs):
        ret = super(SubmittedTestVariable, self)._message_log(*args, **kwargs)
        logger.info('MESSAGE LOG VARIABLE %s %s %s' % (ret, args, kwargs))

        # Add more info about the variable to the chatter message (by default, field_desc is just "Value")
        submitted_sample = None
        submitted_test = None
        submitted_test_variable = None
        for item in self:
            submitted_test_variable = item
            submitted_sample = item.submitted_sample
            submitted_test = item.submitted_test

        newargs = dict(kwargs)
        if submitted_sample and submitted_test and submitted_test_variable:
            if 'tracking_value_ids' in newargs:
                for tracking_value in newargs['tracking_value_ids']:
                    if len(tracking_value) >= 3:
                        attrs = tracking_value[2]
                        if 'field_desc' in attrs:
                            attrs['field_desc'] = '%s / %s / Variable %s %s' % (submitted_sample.name, submitted_test.test_type.name, submitted_test_variable.num, attrs['field_desc'])
            logger.info('MESSAGE LOG NEWARGS %s' % (newargs))

        for item in self:
            if hasattr(item.order, '_message_log'):
                item.order._message_log(*args, **newargs)

        return ret


from django.http import HttpResponse, Http404
from django.shortcuts import render, get_object_or_404
from .models import Movie  # importando os models

# Create your views here.

def index(request):
    # SELECT * FROM movies_movie
    movies = Movie.objects.all()

    render(request, 'index.html', {'movies': movies})

    # SELECT * FROM movies_movie WHERE
    # Movie.objects.filter(release_year=1984)

    # return HttpResponse("Hello World")

    return render(request, 'index.html', {'movies': movies})

def detail(request, movie_id):
    # try:
    #     movie = Movie.objects.get(pk=movie_id)
    #     return render(request, 'movies/detail.html', {'movie': movie})
    # except Movie.DoesNotExist:
    #     raise Http404()

    movie = get_object_or_404(Movie, pk=movie_id)
    return render(request, 'movies/detail.html', {'movie': movie})


import threading
import mysql.connector
import datetime
import urllib.request as urllib2
import time

db = mysql.connector.connect(
    host="127.0.0.1",
    user="root",
    passwd="",
    database="brofistool"
)
conn = db.cursor()
hasil_test = []

def data():
    print("Proses Dimulai")
    sql_apn = "SELECT apn FROM t_fraud_test_progress_detail WHERE suspect_fraud IS NULL GROUP BY apn"
    conn.execute(sql_apn)
    apns = conn.fetchall()
    for apn in apns:
        sql_proxy = "SELECT id,ip_address,url,port " \
                    "FROM t_fraud_test_progress_detail " \
                    "WHERE suspect_fraud IS NULL " \
                    "AND modem = 'modem-1' " \
                    "AND test_type = 'proxy tunneling' " \
                    "AND apn = '%s' " \
                    "LIMIT 10" % apn
        conn.execute(sql_proxy)
        proxys = conn.fetchall()
        t = threading.Thread(target=proxy_check, args=[proxys])
        t.start()
        t.join()
    print("Proses Selesai")

    for hasil in hasil_test:
        update = "UPDATE t_fraud_test_progress_detail " \
                 "SET suspect_fraud = '%s', " \
                 "started_at = '%s', " \
                 "finished_at = '%s' " \
                 "WHERE id = %s" % (hasil[3],hasil[1],hasil[2],hasil[0])
        conn.execute(update)
    db.commit()

def proxy_check(proxy):
    print(threading.currentThread().getName(), 'Starting')
    for data in proxy:
        id = data[0]
        ip = data[2]
        if ip is None:
            ip = data[1]
        port = data[3]
        if port is None:
            port = 1080

        address = str(ip) + ":" + str(port)
        # print(address)
        now = datetime.datetime.now()
        started = now.strftime("%Y-%m-%d %H:%M:%S")
        try:
            proxy_handler = urllib2.ProxyHandler({'http': address})
            opener = urllib2.build_opener(proxy_handler)
            opener.addheaders = [('User-agent', 'Mozilla/5.0')]
            urllib2.install_opener(opener)
            req = urllib2.Request("http://103.41.204.195/ping.php")
            sock = urllib2.urlopen(req, timeout=10)
            rs = sock.read(1000)
            if rs is None:
                proxy_result = "No"
            else:
                proxy_result = "Yes"
            now = datetime.datetime.now()
            finished = now.strftime("%Y-%m-%d %H:%M:%S")
        except:
            proxy_result = "No"
            now = datetime.datetime.now()
            finished = now.strftime("%Y-%m-%d %H:%M:%S")

        isi_test = [id, started, finished, proxy_result]
        hasil_test.append(isi_test)
        print(threading.currentThread().getName(), 'Exiting')
data()


# -*- coding: utf-8 -*-
import logging
from functools import partial
from os import getenv
from urllib.parse import quote

import pytest
import requests
from asynctest import MagicMock
from requests.auth import HTTPBasicAuth

from cabbage import AmqpConnection, AsyncAmqpRpc

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(name)s: %(message)s')

TEST_RABBITMQ_HOST = getenv('TEST_RABBITMQ_HOST', 'localhost')

class Management:
    def __init__(self, url, vhost):
        self.base_url = f'{url}/api/'
        self.vhost = vhost
        self.request_params = dict(
            auth=HTTPBasicAuth('guest', 'guest'),
            headers={'content-type': 'application/json'},
        )

    def _call(self, http_method, *args, **kwargs):
        args = map(partial(quote, safe=''), args)
        result = requests.request(http_method,
                                  self.base_url + '/'.join(args),
                                  **{**self.request_params, **kwargs})
        if result.text:
            return result.json()

    def get_queue(self, name):
        return self._call('get', 'queues', self.vhost, name)

    def get_consumers(self):
        return self._call('get', 'consumers', self.vhost)

    def get_exchanges(self):
        return self._call('get', 'exchanges', self.vhost)

    def put_vhost(self):
        self._call('put', 'vhosts', self.vhost)
        self._call('put', 'permissions', self.vhost, 'guest', json={'configure': '.*', 'write': '.*', 'read': '.*'})

    def delete_vhost(self):
        self._call('delete', 'vhosts', self.vhost)

    def publish(self, exchange, routing_key, data):
        self._call('post', 'exchanges', self.vhost, exchange or 'amq.default', 'publish',
                   json={'properties': {}, 'routing_key': routing_key, 'payload': data, 'payload_encoding': 'string'})

TEST_VHOST = 'cabbage_test'

@pytest.fixture(scope='session')
def management():
    """Wrapper for RabbitMQ Management Plugin API."""
    return Management(f'http://{TEST_RABBITMQ_HOST}:15672', vhost=TEST_VHOST)

@pytest.yield_fixture(scope='function', autouse=True)
def vhost_environment(management: Management):
    management.put_vhost()
    yield
    management.delete_vhost()

@pytest.fixture
async def rpc(event_loop):
    """Ready-to-work RPC connected to RabbitMQ in Docker."""
    connection = AmqpConnection(hosts=[(TEST_RABBITMQ_HOST, 5672)], virtualhost=TEST_VHOST,
                                loop=event_loop)
    rpc = AsyncAmqpRpc(connection=connection)
    await rpc.connect()
    yield rpc
    await rpc.stop()

@pytest.fixture
async def rpc_with_specified_callback_exchange(event_loop, request):
    """Ready-to-work RPC connected to RabbitMQ with specified callback exchange"""
    connection = AmqpConnection(hosts=[(TEST_RABBITMQ_HOST, 5672)], virtualhost=TEST_VHOST,
                                loop=event_loop)
    rpc = AsyncAmqpRpc(connection=connection, callback_exchange=request.param)
    rpc._on_response = MagicMock()
    await rpc.connect()
    yield rpc
    await rpc.stop()


# Add the root path of the dmrgpy library
import os ; import sys ; sys.path.append(os.getcwd()+'/../../../src')

import numpy as np
import matplotlib.pyplot as plt
from dmrgpy import parafermionchain
n = 3
pc = parafermionchain.Parafermionic_Chain(n,Z=3) # create the chain
pc.test_ED()

h = 0
for i in range(n):
  for j in range(n):
      h = h + pc.N[i]*pc.N[j]*np.random.random()
      h = h + pc.Sig[i]*pc.Sig[j]*np.random.random()
      h = h + 1j*pc.Sig[i]*pc.Sigd[j]*np.random.random()
      h = h + pc.Tau[i]*pc.Tau[j]*np.random.random()
      h = h + 1j*pc.Tau[i]*pc.Taud[j]*np.random.random()

h = h + h.get_dagger() # Make the Hamiltonian Hermitian
pc.set_hamiltonian(h) # set the Hamiltonian
pc.maxm = 60
pc.kpmmaxm = 60
print(pc.gs_energy(mode="DMRG"))
print(pc.gs_energy(mode="ED"))

print(pc.get_excited(mode="ED",n=10))

ops = pc.Chi + pc.Psi
no = 10

def rando():
    o = ops[np.random.randint(len(ops))]
    return o #+ o.get_dagger()

name = [rando(),rando()]
delta = 1e-1
(e1,d1) = pc.get_dynamical_correlator(name=name,mode="ED",submode="INV",
        delta=delta)
(e2,d2) = pc.get_dynamical_correlator(name=name,mode="ED",submode="KPM",
        delta=delta)
(e0,d0) = pc.get_dynamical_correlator(name=name,mode="DMRG",submode="KPM",
        delta=delta)

plt.plot(e0,d0.real,color="red",label="DMRG")
plt.scatter(e2,d2.real,color="green",label="ED-KPM")
plt.plot(e1,d1.real,color="blue",label="ED-INV")
plt.legend()
plt.show()


# ##### BEGIN LICENSE BLOCK #####
#
# This program is licensed under Creative Commons CC0
# https://creativecommons.org/publicdomain/zero/1.0/
#
# Note: All Blender API calls are licensed under GNU GPL according to www.blender.org
#
# ##### END LICENSE BLOCK #####

bl_info = {
    "name": "Flip Y and Z",
    "author": "Mazay",
    "version": (0, 2),
    "blender": (2, 80, 0),
    "location": "View3D > Object > Flip Y and Z",
    "description": "Adds Flip Y and Z option to object menu.",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Object"}

import bpy
import bmesh
import mathutils

class FlipYZ(bpy.types.Operator):
    """Flip Y and Z of object"""
    bl_idname = "object.flip_yz"
    bl_label = "Flip Y and Z"
    bl_options = {'REGISTER', 'UNDO'} # Add to F3 search, Add Undo step

    @classmethod
    def poll(cls, context):
        return len(bpy.context.selected_objects) > 0

    def execute(self, context):
        for ob in bpy.context.selected_objects:

            # Flip Y and Z in 4x4 transform matrix:
            m = ob.matrix_world
            x, y, z, w = m[0], m[1], m[2], m[3]
            # swapping third and second row, and third and second column
            ob.matrix_world = mathutils.Matrix([
                (x.x, x.z, x.y, x.w),
                (z.x, z.z, z.y, z.w),
                (y.x, y.z, y.y, y.w),
                (w.x, w.z, w.y, w.w),
            ])

            # Flip Y and Z of vertices:
            if ob.type == 'MESH':
                mesh = ob.data
                bm = bmesh.new() # Empty BMesh
                bm.from_mesh(mesh) # Fill with data

                for v in bm.verts:
                    y = v.co.y
                    z = v.co.z
                    v.co.y = z
                    v.co.z = y

                bm.to_mesh(mesh) # Overwrite original mesh
                mesh.update() # Update viewport after mesh change
                bm.free() # Free temporary mesh from memory

        return {'FINISHED'}

# Only needed if you want to add into a dynamic menu.
def menu_func(self, context):
    self.layout.separator()
    self.layout.operator(FlipYZ.bl_idname, text="Flip Y and Z")

def register():
    bpy.utils.register_class(FlipYZ)
    bpy.types.VIEW3D_MT_object.append(menu_func)

def unregister():
    bpy.types.VIEW3D_MT_object.remove(menu_func)
    bpy.utils.unregister_class(FlipYZ)

if __name__ == "__main__":
    register()


from __future__ import print_function
from dvn.utils import get_itk_array,write_itk_imageArray
import numpy as np
from dvn import FCN, VNET, UNET
from dvn import Network as NET
from dvn import losses as ls
import argparse
import os
import keras as K

# Defined global variables here .................................
BLOCK_SIZE = (64, 64, 64)
OVERLAP_SIZE = (5, 5, 5)
SEL_TOP = 50

def norm_data(data):
    data = data - np.min(data)
    data = data * 1.0 / np.max(data)
    return data

def histinfo(data, cfreq):
    bins = np.arange(np.min(data), np.max(data), 10)
    vals, bins = np.histogram(data, bins, density=True)
    acc = 0
    cutoff = np.max(bins)
    cfreq *= sum(vals)
    for i, v in enumerate(vals):
        acc = acc+v
        if acc >= cfreq:
            cutoff = bins[i]
            break

    return data

def processdata(X, hist_cutoff, n_in):
    if n_in == 1:
        data = histinfo(data=X, cfreq=float(hist_cutoff))
        data = norm_data(data=data)
        return data
    else:
        cfs = hist_cutoff.split(' ')
        if len(cfs) == 1:
            cfs = cfs * len(X)
        assert len(X) == len(cfs), 'Number of channels must match with number of histogram cutoffs'
        data = np.zeros(X.shape, dtype=X.dtype)
        for i in range(len(X)):
            data[i] = norm_data(histinfo(data=X[i], cfreq=float(cfs[i])))

        return data

def load_model(filename=None, n_in=1, n_out=2, modelType=0, use_crosshair=False):
    if filename is None:
        modelType = [FCN, VNET, UNET][modelType]
        model = modelType(nlabels=n_out, nchannels=n_in, cross_hair=use_crosshair)
    else:
        filename = os.path.abspath(filename)
        model = NET.load(filename)

    return model

def train_model(model,data_x,data_y,data_mask,batch_size,n_epochs,lr,weighted_cost,initial_epoch=0):
    k = []
    if weighted_cost:
        for l in np.unique(data_y):
            if l != 0:
                k.append(l)
    sgd = K.optimizers.SGD(lr=lr, decay=0, momentum=0, nesterov=False)
    loss = ls.weighted_categorical_crossentropy_with_fpr(classes=len(k)) if weighted_cost else ls.categorical_crossentropy(axis=1)
    model.compile(optimizer=sgd, loss=loss)
    model.fit(x=data_x,y=data_y,epochs=n_epochs,batch_size=batch_size,initial_epoch=initial_epoch)
    return model, lr

def generate_data(inputFn,labelFn,maskFn,preprocess=False,hist_cutoff=[],n_in=1, cube_size=64):
    iFn = []
    lFn = []
    mFn = []
    with open(os.path.abspath(inputFn)) as f:
        iFn = f.readlines()
    iFn = [x.strip() for x in iFn]

    with open(os.path.abspath(labelFn)) as f:
        lFn = f.readlines()
    lFn = [x.strip() for x in lFn]

    if not maskFn is None:
        with open(os.path.abspath(maskFn)) as f:
            mFn = f.readlines()
        mFn = [x.strip() for x in mFn]

    data_x = []
    data_y = []
    data_mask = []

    if not maskFn is None:
        for ifn,lfn,mfn in zip(iFn,lFn,mFn):
            if n_in == 1:
                X = get_itk_array(ifn)
            else:
                ifns = ifn.split('\t')
                assert len(ifns) >= n_in, 'Number of Input files per line should match the number of channels (tab separated!)'
                X = []
                sh = None
                for ii in ifns[:n_in]:
                    x = get_itk_array(ii)
                    assert (sh is None) or sh == x.shape, 'Input files should have the same shape'
                    X.append(x)

        X = np.array(X).astype('float32')
        Y = get_itk_array(lfn)
        M = get_itk_array(mfn)

        assert X.shape[-3:] == Y.shape
        assert X.shape[-3:] == M.shape

        if preprocess:
            X = processdata(X=X, hist_cutoff=hist_cutoff, n_in=n_in)
            data_x.append(X)
        else:
            data_x.append(X)
        data_y.append(Y)
        data_mask.append(M)
    else:
        for ifn,lfn in zip(iFn,lFn):
            if n_in == 1:
                X = get_itk_array(ifn)
            else:
                ifns = ifn.split('\t')
                assert len(ifns) >= n_in, 'Number of Input files per line should match the number of channels (tab separated!)'
                X = []
                sh = None
                for ii in ifns[:n_in]:
                    x = get_itk_array(ii)
                    assert (sh is None) or sh == x.shape, 'Input files should have the same shape'
                    X.append(x)

        X = np.array(X).astype('float32')
        Y = get_itk_array(lfn)

        assert X.shape[-3:] == Y.shape

        if preprocess:
            X = processdata(X=X, hist_cutoff=hist_cutoff, n_in=n_in)
            data_x.append(X)
        else:
            data_x.append(X)

        data_y.append(Y)

    if not maskFn is None:
        return get_bounding_blocks(np.array(data_x, dtype='float32'),np.array(data_y, dtype='int32'),np.array(data_mask, dtype='int32'),cube_size=cube_size)
    else:
        return get_bounding_blocks(np.array(data_x, dtype='float32'),np.array(data_y, dtype='int32'), None, cube_size=cube_size)

def get_bounding_blocks(feats,grd_truth,mask=None,gmt=False,cube_size=64):
    feat_stack = np.array([])
    grd_truth_stack=np.array([])
    mask_stack = np.array([])
    cnt = 0
    BLOCK_SIZE=[cube_size if s > cube_size else s for s in grd_truth.shape[-3:]]

    for vol_ind in range(grd_truth.shape[0]):
        values = []
        indices = []

    for x in np.arange(0,grd_truth.shape[1]-BLOCK_SIZE[0],BLOCK_SIZE[0]-OVERLAP_SIZE[0]):
        for y in np.arange(0,grd_truth.shape[2]-BLOCK_SIZE[1],BLOCK_SIZE[1]-OVERLAP_SIZE[1]):
            for z in np.arange(0,grd_truth.shape[3]-BLOCK_SIZE[2],BLOCK_SIZE[2]-OVERLAP_SIZE[2]):
                data = grd_truth[vol_ind][x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]

                if gmt:
                    val = np.sum(data==1) + np.sum(data==2)
                else:
                    val = np.sum(data==1)

                values.append(val)
                indices.append((x,y,z))

    values = np.argsort(np.asarray(values))
    fsh = len(feats.shape) == 5
    for ind in values[values.shape[0]-SEL_TOP:]:
        x,y,z = indices[ind]
        if cnt==0:
            if fsh:
                feat_stack = feats[vol_ind:vol_ind+1,:,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]
            else:
                feat_stack = feats[vol_ind:vol_ind+1,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]

            grd_truth_stack = grd_truth[vol_ind:vol_ind+1,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]

            if not mask is None:
                mask_stack = mask[vol_ind:vol_ind+1,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]

        else:
            if fsh:
                fs = feats[vol_ind:vol_ind+1,:,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]
            else:
                fs = feats[vol_ind:vol_ind+1,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]

            feat_stack = np.concatenate([feat_stack,fs])
            grd_truth_stack = np.concatenate([grd_truth_stack,grd_truth[vol_ind:vol_ind+1,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]])

            if not mask is None:
                mask_stack = np.concatenate([mask_stack,mask[vol_ind:vol_ind+1,x:x+BLOCK_SIZE[0],y:y+BLOCK_SIZE[1],z:z+BLOCK_SIZE[2]]])

        cnt += 1

    if mask is None:
        return feat_stack,grd_truth_stack
    else:
        return feat_stack,grd_truth_stack,mask_stack

def parse_args():
    parser = argparse.ArgumentParser(description='Train/Finetune a cross-hair filter based FCN on NIFTI volumes')
    parser.add_argument('--inputFns', dest='inputFns', type=str, default='inputs.txt',
                   help='a text file containing a list of names/path of input data for the traning (one example per line) (default: inputs.txt)')
    parser.add_argument('--labelFns', dest='labelFns', type=str, default='labels.txt',
                   help='a text file containing a list of names/path of data label for the traning (one example per line) (default: labels.txt)')
    parser.add_argument('--maskFns', dest='maskFns', type=str, default=None,
                   help='a text file containing a list of names/path of mask data for the traning (one example per line)')
    parser.add_argument('--preprocess', dest='preprocess', action='store_true',
                   help='Whether to apply preprocessing or not (default: False)')
    parser.add_argument('--hist-cutoff', dest='hist_cutoff', type=str, default="0.99",
                   help='Cutoff to use when applying histogram cutoff (default: 0.99)')
    parser.add_argument('--initModel', dest='model', type=str, default=None,
                   help='a path to a model which should be used as a base for the training (default: None)')
    parser.add_argument('--modelType', dest='modelType', type=int, default=0,
                   help='the model type to train (FCN=0, VNET=1, UNET=2) (default: 0)')
    parser.add_argument('--use_crosshair', dest='usecrosshair', action='store_true',
                   help='Whether to use crosshair filters or not (default: False)')
    parser.add_argument('--n_in', dest='n_in', type=int, default=1,
                   help='number of input channels (default: 1)')
    parser.add_argument('--n_out', dest='n_out', type=int, default=2,
                   help='number of prediction classes (default: 2)')
    parser.add_argument('--batch-size', dest='batch_size', type=int, default=1,
                   help='batch size for training (default: None)')
    parser.add_argument('--cs', dest='cube_size', type=int, default=64,
                   help='Size of cube to be used during training (default: 64)')
    parser.add_argument('--epochs', dest='epochs', type=int, default=1,
                   help='number of training epochs (default: 1)')
    parser.add_argument('--save-after', dest='save_after', type=int, default=1,
                   help='number of training epochs after which the model should be saved (default: 1)')
    parser.add_argument('--modelFn', dest='modelFn', type=str, default='model',
                   help='filename for saving trained models. Note .dat will be appended autmatically (default: model)')
    parser.add_argument('--modelFolder', dest='model_folder', type=str, default='',
                   help='folder where models will be saved (default: current working directory)')
    parser.add_argument('--lr', dest='learning_rate', type=float, default=0.01,
                   help='learning rate (default: 0.01)')
    parser.add_argument('--decay', dest='decay', type=float, default=0.99,
                   help='learning rate decay per epoch (default: 0.99)')
    parser.add_argument('--weighted-cost', dest='weighted_cost', action='store_true',
                   help='Whether to use weighted cost or not (default: False)')
    args = parser.parse_args()

    return args

def run():
    args = parse_args()
    n_in = args.n_in
    model = load_model(args.model, args.n_in, args.n_out, args.modelType, args.usecrosshair)
    n_epochs = args.epochs
    batch_size = args.batch_size
    lr = args.learning_rate
    decay = args.decay
    weighted_cost = args.weighted_cost
    model_folder = args.model_folder
    modelFn = args.modelFn
    save_after = args.save_after

    if args.maskFns is None:
        data_x, data_y = generate_data(args.inputFns, args.labelFns, args.maskFns, preprocess=args.preprocess, hist_cutoff=args.hist_cutoff, n_in=args.n_in,cube_size=args.cube_size)
        sh = data_y.shape
        data_y = data_y.reshape((sh[0], 1) + sh[1:])
        if n_in == 1:
            data_x = data_x.reshape((sh[0], 1) + sh[1:])
        data_mask = None
    else:
        data_x, data_y, data_mask = generate_data(args.inputFns, args.labelFns, args.maskFns, preprocess=args.preprocess, hist_cutoff=args.hist_cutoff, n_in=args.n_in, cube_size=args.cube_size)
        sh = data_y.shape
        data_y = data_y.reshape((sh[0], 1) + sh[1:])
        data_mask = data_mask.reshape((sh[0], 1) + sh[1:])
        if n_in == 1:
            data_x = data_x.reshape((sh[0], 1) + sh[1:])

    n_iters = int(n_epochs / save_after)
    iters = [save_after for i in range(n_iters)]

    if n_epochs % save_after > 0:
        iters.append(n_epochs % save_after)

    sh = data_x.shape
    print('..............................')
    print('Training Parameters')
    print('..............................')
    print('learning-rate:',lr)
    print('decay:', decay)
    print('Number of epochs:', n_epochs)
    print('Batch size:', batch_size)
    print('Weighted cost:',weighted_cost)
    print('Base Model:',args.model)
    print('Model save folder:', model_folder)
    print('Model save filename:',modelFn)
    print('save model after every', save_after, 'epoch(s)')
    print('Apply preprocessing:', args.preprocess)
    print('Histogram cutoff:', args.hist_cutoff)
    print('Number of input channels:', args.n_in)
    print('Number of classes:', args.n_out)
    print('Number of Examples Extracted:', sh[0])
    print('Training cube size:', sh[-3:])
    print('...................................................\n \n')

    for i, this_epochs in enumerate(iters):
        model, lr = train_model(model=model,data_x=data_x,data_y=data_y,data_mask=data_mask,batch_size=batch_size,n_epochs=this_epochs,lr=lr,weighted_cost=weighted_cost)
        this_model_fn = os.path.abspath(os.path.join(model_folder,'model'+str(i+1)+'.dat'))
        print('saving model......')
        model.save(this_model_fn)
        lr = lr * decay
        print('.....................................................................')

if __name__ == "__main__":
    run()


# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 11:11:59 2018

@author: Administrator
"""
from urllib import parse,request
import re
import requests
import json

def getTInfo(key):
    # 通过抓包的方式获取的url，并不是浏览器上显示的url
    url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"

    # 完整的headers
    headers = {
            "Accept" : "application/json, text/javascript, */*; q=0.01",
            "X-Requested-With" : "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0",
            "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
        }

    formdata = {
    "i":key,
    "from":"auto",
    "to":"auto",
    "smartresult":"dict",
    "client":"fanyideskweb",
    "salt":"1511219405946",
    "sign":"f8965f67a1d3eee8a69ddf8ccc5f582b",
    "doctype":"json",
    "version":"2.1",
    "keyfrom":"fanyi.web",
    "action":"FY_BY_REALTIME",
    "typoResult":"false"
    }

    # 由于urllib在做post时需要data是bytes，所以这里需要对
    #formdata做一次类型的处理，转成bytes
    # data=parse.urlencode(formdata).encode("utf-8")
    data=bytes(parse.urlencode(formdata),encoding='utf-8')
    #  做一次urlencode
    #利用Request将headers，dict，data整合成一个对象传入urlopen
#    req = request.Request(url,data,headers,method='POST')
#    response=request.urlopen(req)
#    info = response.read().decode('utf-8')
    #print(info)
    req = requests.post(url, headers=headers, data=data)
    info = req.text
    #print(req.text)

    #使用正则表达式取出翻译之后的信息
#    strRule = re.compile('"tgt":"(.*?)"}')
#    info2 = strRule.findall(info)
#    for i in info2:
#               i = i.replace('"',"")
#    return info2[0]

    # 使用json来提取翻译的信息
    jsonloads = json.loads(info) # json encode
    return jsonloads["translateResult"][0][0]["tgt"]

#print(getTInfo("hello"))
if __name__ == "__main__":
    info = input("请输入您要查询的单词：")
    print(getTInfo(info))


from typing import Callable, Union

import matplotlib as mpl
import numpy as np
import torch as to
import torch.nn as nn
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import pyrado
from pyrado.utils.data_types import merge_dicts

def draw_surface(
    x: np.ndarray,
    y: np.ndarray,
    z_fcn: Union[Callable[[np.ndarray], np.ndarray], nn.Module],
    x_label: str,
    y_label: str,
    z_label: str,
    data_format="numpy",
    fig: plt.Figure = None,
    title: str = None,
    plot_kwargs: dict = None,
) -> plt.Figure:
    """
    Render a 3-dim surface plot by providing a 1-dim array of x and y points and a function to calculate the z values.

    .. note::
        If you want to have a tight layout, it is best to pass axes of a figure with `tight_layout=True` or
        `constrained_layout=True`.

    :param x: x-axis 1-dim grid for constructing the 2-dim mesh grid
    :param y: y-axis 1-dim grid for constructing the 2-dim mesh grid
    :param z_fcn: function that defines the surface, takes a 2-dim vector as input
    :param x_label: label for the x-axis
    :param y_label: label for the y-axis
    :param z_label: label for the z-axis
    :param data_format: data format, 'numpy' or 'torch'
    :param fig: handle to figure, pass None to create a new figure
    :param title: title displayed above the figure, set to None to suppress the title
    :param plot_kwargs: keyword arguments forwarded to pyplot's `plot_surface()` function
    :return: handle to figure
    """
    plot_kwargs = merge_dicts([dict(cmap=mpl.rcParams["image.cmap"]), plot_kwargs])

    if fig is None:
        fig = plt.figure()
    ax = Axes3D(fig, auto_add_to_figure=False)
    fig.add_axes(ax)

    # Create mesh grid matrices from x and y vectors
    xx, yy = np.meshgrid(x, y)

    # Check which version to use based on the output of the function
    if data_format == "numpy":
        # Operate on ndarrays
        zz = np.array([z_fcn(np.stack((x, y), axis=0)) for x, y in zip(xx, yy)])

    elif data_format == "torch":
        # Operate on Tensors
        xx_tensor = to.from_numpy(xx)
        yy_tensor = to.from_numpy(yy)

        if hasattr(z_fcn, "_fcn"):
            # Passed function was wrapped (e.g. by functools)
            check_fcn = z_fcn._fcn
        else:
            check_fcn = z_fcn

        if isinstance(check_fcn, nn.Module):
            # Adapt for batch-first behavior of NN-based policies
            zz = to.stack(
                [
                    z_fcn(to.stack((x, y), dim=1).view(-1, 1, 2).to(to.get_default_dtype()))
                    for x, y in zip(xx_tensor, yy_tensor)
                ]
            )
        else:
            zz = to.stack(
                [
                    z_fcn(to.stack((x, y), dim=1).transpose(0, 1).to(to.get_default_dtype()))
                    for x, y in zip(xx_tensor, yy_tensor)
                ]
            )
        zz = zz.squeeze().detach().cpu().numpy()

    else:
        raise pyrado.ValueErr(given=data_format, eq_constraint="'numpy' or 'torch'")

    # Generate the plot
    ax.plot_surface(xx, yy, zz, **plot_kwargs)

    # Add labels
    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    ax.set_zlabel(z_label)
    if title is not None:
        ax.set_title(title)
    return fig


import requests
from bs4 import BeautifulSoup
from plugins.Craw_baidu import getxml
import time
import xlwt
import os

class Baidu(object):
	# 参考：https://www.jianshu.com/p/b9068070785a
	# 当前时间
	t = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
	# 构建CRID
	CRID = 'CRA' + t
	# path = getxml.rxi.getpropertypath()
	# propertypath = path + '/baidujs.xls'

	def __init__(self, filepath=None):
		# 初始网页
		self.start_url = 'https://news.baidu.com/widget?id=InternationalMil&channel=mil'
		self.t = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
		# 构建CRID
		self.CRID = 'CRA' + self.t
		self.num = 0
		self.filepath = filepath
		self.configfilePath = os.getcwd() + '/' + 'plugins/' + 'Craw_baidu' + '/' + 'Craw_baidu' + '.xml'
		self.xml = getxml.read_xml_info(self.configfilePath)
		if self.filepath is None:
			self.filepath = self.xml.getfilepath()
		# 定义保存Excel的位置
		self.workbook = xlwt.Workbook()
		self.sheet = self.workbook.add_sheet('百度军事')
		# 设置Excel表头
		# head = ['标题', '发布者', 'CRID', 'DOCID', '内容', 'url']
		head = ['标志', '序号', '题名', '作者', '单位', '关键字', '摘要', '来源', '发表时间', '下载地址', '后缀']
		for h in range(len(head)):
			self.sheet.write(0, h, head[h])  # 把表头写到Excel里面去

	# 获取每条新闻的url
	def geturls(self):
		html = requests.get(self.start_url).text
		soup = BeautifulSoup(html, 'html.parser')
		url_list = []
		for i in soup.find_all('a'):
			ul = i.get('href')
			url_list.append(ul)
		return url_list

	# 获取页面具体信息
	def getdetail(self, news_url):
		print(news_url)
		response = requests.get(news_url)
		html = response.text
		bf = BeautifulSoup(html, 'html.parser')
		# print("开始爬取。。。。。。。。。。。。")
		# url
		self.newsurl = news_url
		# print("url:"+self.newsurl)

		# 标题
		# self.title = bf.select("#detail-page > div.title_border > div > div.article-title > h2")[0].get_text()
		try:
			self.title = bf.select("#detail-page > div.title_border > div > div.article-title > h2")[0].get_text()
		except:
			# self.title = 'hhh'
			pass
		# print("title:"+self.title)

		# 发布者
		try:
			self.poster = bf.select(
				"#detail-page > div.title_border > div > div.article-desc.clearfix > div.author-txt > p")[0].get_text()
			# print("poster:"+self.poster)
		except:
			self.poster = 'hhhhhh'
			pass

		# 时间
		try:
			self.time1 = bf.select(
				"#detail-page > div.title_border > div > div.article-desc.clearfix > div.author-txt > div > span.date")[0].\
				get_text()
			self.time = str(time.localtime().tm_year)+'-'+self.time1.strip("发布时间：")
		except:
			pass

		# 内容
		self.content_list = []
		content = bf.select("#article > div > p > span")
		# print("content:")
		for c in content:
			# print(c.get_text())
			self.content_list.append(c.get_text())
		# print(self.content_list)

		# DOCID
		self.num += 1
		self.DOCID = self.CRID + "%04d" % self.num
		# print("DOCID:"+self.DOCID)
		# print("CRID:"+self.CRID)

		self.write_excel()
		self.write_txt()

	def write_txt(self):
		txtname = self.DOCID + self.title
		txtpath = self.filepath.replace('\\', '/') + "/" + txtname + ".txt"
		# print("写入txt：" + txtpath)
		writetxt = open(txtpath, "w")
		for i in range(len(self.content_list)):
			writetxt.write(self.content_list[i])
		writetxt.close()

		# head = ['标志', '序号', '题名', '作者', '单位', '关键字', '摘要', '来源', '发表时间', '下载地址', '后缀']
	def write_excel(self):
		self.sheet.write(self.num, 0, self.CRID)
		self.sheet.write(self.num, 1, self.DOCID)
		self.sheet.write(self.num, 2, self.DOCID+self.title)
		self.sheet.write(self.num, 3, self.poster)
		self.sheet.write(self.num, 8, self.time)
		self.sheet.write(self.num, 9, self.newsurl)
		self.sheet.write(self.num, 10, 'txt')
	# 	# 追加写入 参考：https://blog.csdn.net/lA6Nf/article/details/79352112?utm_source=blogxgwz6
	# 	rb = xlrd.open_workbook(self.propertypath, formatting_info=True)
	# 	rn = rb.sheets()[0].nrows
	# 	# r = rn
	# 	# print(rn)
	# 	wb = copy(rb)
	# 	st = wb.get_sheet(0)
	# 	self.write_excel(st, rn)
	# 	os.remove(self.propertypath)
	# 	wb.save(self.propertypath)

		# return self.num, self.title

	# 写入excel
	# def write_excel(self, sheet, n):
	# 	sheet.write(n, 0, self.title)
	# 	sheet.write(n, 1, self.poster)
	# 	sheet.write(n, 2, self.CRID)
	# 	sheet.write(n, 3, self.DOCID)
	# 	sheet.write(n, 4, self.content_list)
	# 	sheet.write(n, 5, self.newsurl)

# if __name__ == "__main__":
	# bd = Baidu()
	# urls = []
	# urls = bd.geturls()
	# # c = []
	# for url in urls:
	# 	if int(bd.num) < 10:
	# 		bd.getdetail(url)
	# 	else:
	# 		break
	# 	# print(int(bd.num)+1)
	# 	# bd.getdetail(url)
	# 	# c.append(bd.getdetail(url))
	# # bd.workbook.save('E:/Pythonworkspace/bdjs_crawl/baidujs.xls')
	# # print(c)
	# t = time.strftime('%Y', time.localtime(time.time()))
	# print(time.localtime().tm_year)
	# print()


import tensorflow as tf
from keras.datasets import mnist
import numpy as np
import pandas as pd
from loader import Batch

dataset=mnist.load_data()
(X_train,y_train),(X_test,y_test)=dataset

X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)

from keras.utils import to_categorical
#one-hot encode target column
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32, kernel_size=3, activation='relu'))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=3)

model.predict(X_test[:4])
y_test[:4]
from prep import Preprocessor

from sklearn.ensemble import AdaBoostClassifier
from sklearn.naive_bayes import MultinomialNB
X=[]
y=[]
n=0
with open('trainingdata.txt', 'rt') as training_data:
    n=int(training_data.readline())
    for line in training_data:
        y.append(int(line[0]))
        X.append(line[2:])

from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.pipeline import Pipeline
clf=MultinomialNB()
classifier=Pipeline([('vect', CountVectorizer(stop_words='english',max_features=1000)),
                     ('tfidf',TfidfTransformer()),
                     ('clf',AdaBoostClassifier(base_estimator=clf))])
classifier.fit(X,y)
x=[]
for i in range(int(input())):
    x.append(input())

from typing import Tuple, List
from nn_lite import *

from model import Model, DecoderType

def solve(img):
    decoder_type = DecoderType.BestPath
    model = Model(list(open(FilePaths.fn_char_list).read()), decoder_type, must_restore=True)
    return infer(model, img)

def get_img_height() -> int:
    return 32

def get_img_size(line_mode: bool = False) -> Tuple[int, int]:
    if line_mode:
        return 256, get_img_height()
    return 128, get_img_height()

def infer(model, fn_img):
    img = fn_img
    assert img is not None

    preprocessor = Preprocessor(get_img_size(), dynamic_width=True, padding=16)
    img = preprocessor.process_img(img)

    batch = Batch([img], None, 1)
    recognized, probability = model.infer_batch(batch, True)

    return recognized


from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from helpers import render_to_json, jsonResponse, serializeUser
from django.forms.models import modelformset_factory
from django.template import RequestContext
from django.contrib.auth.models import User

@login_required
@render_to_json
def get(req, user_id):
    if user_id:
        user = User.objects.get(id=user_id)
        return serializeUser(user)
    else:
        return [serializeUser(user)
                for user in User.objects.all()]

@login_required
def create(req):
    UserFormSet = modelformset_factory(User)
    if req.method == 'POST':
        formset = UserFormSet(req.POST)
        if formset.is_valid():
            user = formset.save()[0]
            user.set_password(user.password)
            user.save()
            # do something.
            return jsonResponse(serializeUser(user))
    else:
        formset = UserFormSet(queryset=User.objects.none())
    return render_to_response("user.html",
            {"formset": formset},
            context_instance=RequestContext(req))

@login_required
@render_to_json
def update(req, user_id):
    pass

@login_required
@render_to_json
def delete(req, user_id):
    user = User.objects.get(id=user_id)
    user.is_active = False
    user.save()

    return {'success': True}