#!/usr/bin/env python
#
# -----------------------------------------------------------------------------
# Copyright (c) 2016 The Regents of the University of California
#
# This file is part of kevlar (http://github.com/dib-lab/kevlar) and is
# licensed under the MIT license: see LICENSE.
# -----------------------------------------------------------------------------

from collections import defaultdict
from itertools import combinations, product
from networkx import Graph, connected_components
from sys import stdout, stderr, exit
import re
import khmer
import screed
import kevlar
from kevlar.sequence import Record, KmerOfInterest
from kevlar.sequence import write_record, parse_augmented_fastx

class KevlarPartitionLabelError(ValueError):
    pass

def parse_fasta(data):
    """Load sequences in Fasta format.

    This generator function yields a tuple containing a defline and a sequence
    for each record in the Fasta data. Stolen shamelessly from
    http://stackoverflow.com/a/7655072/459780.
    """
    name, seq = None, []
    for line in data:
        line = line.rstrip()
        if line.startswith('>'):
            if name:
                yield (name, ''.join(seq))
            name, seq = line, []
        else:
            seq.append(line)
    if name:  # pragma: no cover
        yield (name, ''.join(seq))

def parse_seq_dict(data):
    """Load sequences from a Fasta file into a dictionary."""
    seqs = dict()
    for defline, sequence in parse_fasta(data):
        seqid = defline[1:].replace('\t', ' ').split(' ')[0]
        assert seqid not in seqs, seqid
        seqs[seqid] = sequence
    return seqs

def afxstream(filelist):
    for infile in filelist:
        fh = kevlar.open(infile, 'r')
        for record in parse_augmented_fastx(fh):
            yield record

def partition_id(readname):
    partmatch = re.search(r'kvcc=(\d+)', readname)
    if not partmatch:
        return None
    return partmatch.group(1)

def parse_partitioned_reads(readstream):
    current_part = None
    reads = list()
    for read in readstream:
        part = partition_id(read.name)
        if part is None:
            reads.append(read)
            current_part = False
            continue

        if current_part is False:
            message = 'reads with and without partition labels (kvcc=#)'
            raise KevlarPartitionLabelError(message)

        if part != current_part:
            if current_part:
                yield reads
                reads = list()
            current_part = part
        reads.append(read)

    yield reads

def parse_single_partition(readstream, partid):
    """
    Retrieve a single partition (by label) from a stream of partitioned reads.
    """
    for partition in parse_partitioned_reads(readstream):
        readname = partition[0].name
        partmatch = re.search(r'kvcc=(\d+)', readname)
        if not partmatch:
            continue
        if partmatch.group(1) == partid:
            yield partition

class AnnotatedReadSet(object):
    """Data structure for de-duplicating reads and combining annotated k-mers.

    The `kevlar novel` command produces output in an "augmented Fastq" format,
    with "interesting" (potentially novel) k-mers annotated like so.

            @read1
            TTAACTCTAGATTAGGGGCGTGACTTAATAAGGTGTGGGCCTAAGCGTCT
            +
            IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
                         AGGGGCGTGACTTAATAAG          8 0 0#
                           GGGCGTGACTTAATAAGGT          8 0 0#

    Each line ending with a # shows an interesting k-mer and its abundances
    in the case and control samples. If `kevlar novel` is run in "k-mer
    banding" mode (with the `--batch` flag), the same read will typically show
    up in multiple output files, with different "interesting" k-mers annotated
    in each. This data structure supports de-duplicating reads that appear in
    multiple augmented Fastq files and combining their annotated k-mers.
    """

    def __init__(self, ksize, abundmem, mask=None):
        self._reads = dict()
        self._counts = khmer.Counttable(ksize, abundmem / 4, 4)
        self._mask = mask
        self._readcounts = defaultdict(int)
        self._ikmercounts = defaultdict(int)

        self._masked = defaultdict(int)
        self._abundfilt = defaultdict(int)
        self._valid = defaultdict(int)

        self._novalidkmers_count = 0

    def __len__(self):
        return len(self._reads)

    def __iter__(self):
        for readid in self._reads:
            record = self._reads[readid]
            if len(record.annotations) == 0:
                continue
            yield record

    @property
    def masked(self):
        return len(self._masked), sum(self._masked.values())

    @property
    def abundfilt(self):
        return len(self._abundfilt), sum(self._abundfilt.values())

    @property
    def valid(self):
        return len(self._valid), sum(self._valid.values())

    @property
    def discarded(self):
        return self._novalidkmers_count

    @property
    def distinct_reads(self):
        return len(self._readcounts)

    @property
    def read_instances(self):
        return sum(self._readcounts.values())

    @property
    def distinct_ikmers(self):
        return len(self._ikmercounts)

    @property
    def ikmer_instances(self):
        return sum(self._ikmercounts.values())

    def add(self, newrecord):
        self._readcounts[newrecord.name] += 1
        if newrecord.name in self._reads:
            record = self._reads[newrecord.name]
            assert record.sequence == newrecord.sequence
            record.annotations.extend(newrecord.annotations)
        else:
            self._reads[newrecord.name] = newrecord

        for kmer in newrecord.annotations:
            kmerhash = self._counts.hash(newrecord.ikmerseq(kmer))
            self._ikmercounts[kmerhash] += 1
            if self._mask and self._mask.get(kmerhash) > 0:
                self._masked[kmerhash] += 1
            else:
                self._counts.add(kmerhash)

    def validate(self, casemin=6, ctrlmax=1):
        for readid in self._reads:
            record = self._reads[readid]
            validated_kmers = list()
            for kmer in record.annotations:
                kmerhash = self._counts.hash(record.ikmerseq(kmer))
                kmercount = self._counts.get(kmerhash)
                if kmercount < casemin:
                    self._abundfilt[kmerhash] += 1
                elif sum([1 for a in kmer.abund[1:] if a > ctrlmax]):
                    self._abundfilt[kmerhash] += 1
                else:
                    newabund = tuple([kmercount] + list(kmer.abund[1:]))
                    newkmer = KmerOfInterest(kmer.ksize, kmer.offset, newabund)
                    validated_kmers.append(newkmer)
                    self._valid[kmerhash] += 1
            record.annotations = validated_kmers
            if len(validated_kmers) == 0:
                self._novalidkmers_count += 1

def bam_filter_suppl(bam):
    for record in bam:
        if record.is_secondary or record.is_supplementary:
            continue
        yield record

def bam_paired_reader(bam):
    prev = None
    for record in bam_filter_suppl(bam):
        if not record.is_paired:
            assert prev is None
            yield record, None
            continue
        if prev is None:
            prev = record
            continue
        if prev.query_name == record.query_name:
            pr = prev.is_read1 and record.is_read2
            rp = prev.is_read2 and record.is_read1
            assert pr or rp
            if pr:
                yield prev, record
            else:
                yield record, prev
            prev = None
        else:
            yield prev, None
            if record.is_paired:
                prev = record
            else:
                yield record
                prev = None


#!/usr/bin/env python2
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
#from vaderSentiment import sentiment as vaderSentiment
#from vaderSentiment.vaderSentiment import sentiment as vaderSentiment
import sys

err, emo, pos, neu, neg = 0, 0, 0, 0, 0

with open (sys.argv[1], 'r') as f:
    analyzer = SentimentIntensityAnalyzer()
    for line in f:
        try:
            tweetId, text = line.strip().split('\t')
            #vs = vaderSentiment(text)
            vs = analyzer.polarity_scores(text)
            vpos, vneg, vneu = vs["pos"], vs["neg"], vs["neu"]
            # if pos score is highest then this tweet's classified as pos
            if(vpos > vneu and vpos > vneg):
                pos = pos + 1
                print ("%s\t%s"%('P', text))
		#print "pos\t", text
            # if neg score is highest
            if (vneg > vpos and vneg > vneu):
                neg = neg + 1
                print ("%s\t%s" %('N', text))
		#print "neg\t", text
            # this tweet is classified as emotional if
            if(vpos > vneu or vneg > vneu):
                emo = emo +1
            # otherwise it's neutral
            elif(vneu > vneg and vneu > vpos):
                neu = neu + 1
                print ("%s\t%s"% ('NONE', text))
		#print "neu\t", text

        except:
            err = err + 1
            pass

print ("emo: %d \tpos: %d\t neu: %d\t neg: %d\t err: %d" % (emo, pos, neu, neg, err))


import cv2
import sys
import os
import argparse
import shutil
import torch

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from vision.ssd.vgg_ssd import create_vgg_ssd, create_vgg_ssd_predictor
from vision.ssd.mobilenetv1_ssd import create_mobilenetv1_ssd, create_mobilenetv1_ssd_predictor
from vision.ssd.mobilenetv1_ssd_lite import create_mobilenetv1_ssd_lite, create_mobilenetv1_ssd_lite_predictor
from vision.ssd.squeezenet_ssd_lite import create_squeezenet_ssd_lite, create_squeezenet_ssd_lite_predictor
from vision.ssd.mobilenet_v2_ssd_lite import create_mobilenetv2_ssd_lite, create_mobilenetv2_ssd_lite_predictor
from vision.utils.misc import Timer

def main(args):
    net_type = args.net_type
    model_path = args.weights_path
    label_path = args.label_path
    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)

    if args.live:
        cap = cv2.VideoCapture(0)
        cap.set(3,640)
        cap.set(4,480)
    else:
        cap=cv2.VideoCapture(args.video_path)

    Fourcc=cv2.VideoWriter_fourcc('M','P','4','V')
    writer=cv2.VideoWriter('result.mp4',fourcc=Fourcc,fps=15,frameSize=(640,480))

    num_gpus=torch.cuda.device_count()
    device='cuda' if num_gpus else 'cpu'

    if net_type == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif net_type == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
    elif net_type == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    #elif net_type == 'mb3-ssd-lite':
    #    net = create_mobilenetv3_ssd_lite(len(class_names), is_test=True)
    else:
        print("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
        sys.exit(1)

    net.load(model_path)

    if net_type == 'vgg16-ssd':
        predictor = create_vgg_ssd_predictor(net, candidate_size=20,device=device)
    elif net_type == 'mb1-ssd':
        predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=20,device=device)
    elif net_type == 'mb1-ssd-lite':
        predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=20,device=device)
    elif net_type == 'mb2-ssd-lite':
        predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=20,device=device)
    elif net_type == 'sq-ssd-lite':
        predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=20,device=device)
    #elif net_type == 'mb3-ssd-lite':
    #    predictor = create_mobilenetv3_ssd_lite_predictor(net, candidate_size=10)
    else:
        print("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
        sys.exit(1)

    timer = Timer()

    while True:
        _, orig_image = cap.read()
        if orig_image is None:
            print('END')
            break

        image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
        timer.start()
        boxes, labels, probs = predictor.predict(image, 10, 0.4)
        interval = timer.end()
        print('Time: {:.2f}s, Detect Objects: {:d}.'.format(interval, labels.size(0)))
        for i in range(boxes.size(0)):
            box = boxes[i, :]
            label = f"{class_names[labels[i]]}: {probs[i]:.2f}"
            cv2.rectangle(orig_image, (box[0], box[1]), (box[2], box[3]), (255, 255, 0), 4)

            cv2.putText(orig_image, label,
                        (box[0]+20, box[1]+40),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        1,  # font scale
                        (255, 0, 255),
                        2)  # line type
        writer.write(orig_image)
        cv2.imshow('annotated', orig_image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    writer.release()
    cv2.destroyAllWindows()
    if args.out_video:
        shutil.move('result.mp4',args.out_video)
    else:
        os.remove('result.mp4')

if __name__=='__main__':
    parser=argparse.ArgumentParser(description='Live Detection')
    parser.add_argument('--net-type',default='mb2-ssd-lite',choices=['mb2-ssd-lite','mb3-ssd-lite'],type=str)
    parser.add_argument('--weights-path',help='path to trained weights')
    parser.add_argument('--label-path',help='path to label file')
    parser.add_argument('--live',action='store_true',default=False)
    parser.add_argument('--video-path',help='path to video')
    parser.add_argument('--out-video',help='output video names')

    args=parser.parse_args()

    main(args)


class MyClass():              #定义类
    def printMethod(self):       #定义方法(self类似于Java中的this，类方法和静态方法不需要)
        print("hello world")

mycls=MyClass()
mycls.printMethod()

#创建类
class AddrBookEntry(object):
    'address book entry class'

    def __init__(self,nm,ph):
        self.name=nm
        self.phone=ph
        print("create instance for",self.name)

    def updatePhone(self,newph):
        self.phone=newph
        print("updated phone# for",self.phone)

john=AddrBookEntry("john",13078587303)
jane=AddrBookEntry("jane",111)

#创建子类
class EmplAddrBookEntry(AddrBookEntry):

    'employee address book entry class'#员工地址簿

    def __init__(self,nm,ph,id,em):
        AddrBookEntry.__init__(self,nm,ph)
        self.emid=id
        self.email=em

    'employee address'

    def updateEmail(self,newem):
        self.email=newem
        print("updated e-mail address for:"+self.name)

john1=EmplAddrBookEntry("john doe","400-800-901",42,"jhon@john.com")
print(john1.__doc__)


import os
from flask import Flask, request, render_template
import requests
import pymongo
import Models.LicensePlate as LicensePlate
import json
from flask_socketio import SocketIO, send
import AddToQueue
import uuid
import DBHelper

app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins='*')
app.config['SECRET_KEY'] = 'secret!'

LicensePlate = LicensePlate.LicensePlate()

@app.route('/webhook', methods=['POST'])
def receiveInfo():
    header = request.headers["Content-Type"]
    dataReceived = request.values.get("json") # data is received as Form-Data. json holds the data we want
    PlateInfo = json.loads(dataReceived)
    LicensePlate = buildLicensePlate(PlateInfo) #Build LicensePlate Object with data received
    inserted_id = str(DBHelper.insertToDB(LicensePlate)) #Insert into DB
    LicensePlate.inserted_id = inserted_id
    AddToQueue.sendLicensePlateToQueue(LicensePlate) #Send to Queue
    return "Ok"

def buildLicensePlate(PlateInfo):
    LicensePlate.plate = PlateInfo["data"]["results"][0]["plate"]
    LicensePlate.dscore = PlateInfo["data"]["results"][0]["dscore"]
    LicensePlate.score = PlateInfo["data"]["results"][0]["score"]
    LicensePlate.vehicle_type = PlateInfo["data"]["results"][0]["vehicle"]["type"]
    LicensePlate.vehicle_type_score = PlateInfo["data"]["results"][0]["vehicle"]["score"]
    LicensePlate.camera_id = PlateInfo["data"]["camera_id"]
    LicensePlate.filename = PlateInfo["data"]["filename"]
    LicensePlate.timestamp = PlateInfo["data"]["timestamp"]
    return LicensePlate

@app.route("/")
def index():
    return render_template("index.html")

if __name__ == "__main__":
    socketio.run(app, host='0.0.0.0', port=80)


from a00_common_functions import *
from pandas import read_csv
import pickle
import numpy as np
from os.path import isfile

def subblock(x, filter, block_number, **kwargs):
    from keras import backend as K
    from keras.layers import Activation, Add, BatchNormalization, Conv2D

    x = BatchNormalization(name='bm_bn_1_sub_{}'.format(block_number))(x)
    y = x
    y = Conv2D(filter, (1, 1), activation='relu', name='bm_conv2d_1_sub_{}'.format(block_number), **kwargs)(y)  # Reduce the number of features to 'filter'
    y = BatchNormalization(name='bm_bn_2_sub_{}'.format(block_number))(y)
    y = Conv2D(filter, (3, 3), activation='relu', name='bm_conv2d_2_sub_{}'.format(block_number), **kwargs)(y)  # Extend the feature field
    y = BatchNormalization(name='bm_bn_3_sub_{}'.format(block_number))(y)
    y = Conv2D(K.int_shape(x)[-1], (1, 1), name='bm_conv2d_3_sub_{}'.format(block_number), **kwargs)(y)  # no activation # Restore the number of original features
    y = Add(name='bm_add_{}'.format(block_number))([x, y])  # Add the bypass connection
    y = Activation('relu', name='bm_relu_sub_{}'.format(block_number))(y)
    return y

def build_model(lr, l2, img_shape=(384, 384, 1), activation='sigmoid'):
    from keras import backend as K
    from keras import regularizers
    from keras.optimizers import Adam
    from keras.engine.topology import Input
    from keras.layers import Activation, Add, BatchNormalization, Concatenate, Conv2D, Dense, Flatten, \
        GlobalMaxPooling2D, \
        Lambda, MaxPooling2D, Reshape
    from keras.models import Model, load_model

    ##############
    # BRANCH MODEL
    ##############
    regul = regularizers.l2(l2)
    optim = Adam(lr=lr)
    kwargs = {'padding': 'same', 'kernel_regularizer': regul}

    inp = Input(shape=img_shape, name='bm_input_1')  # 384x384x1
    x = Conv2D(64, (9, 9), strides=2, activation='relu', name='bm_conv2d_1', **kwargs)(inp)

    x = MaxPooling2D((2, 2), strides=(2, 2), name='bm_mp_2')(x)  # 96x96x64
    for _ in range(2):
        x = BatchNormalization(name='bm_bn_2_{}'.format(_))(x)
        x = Conv2D(64, (3, 3), activation='relu', name='bm_relu_2_{}'.format(_), **kwargs)(x)

    x = MaxPooling2D((2, 2), strides=(2, 2), name='bm_mp_3')(x)  # 48x48x64
    x = BatchNormalization(name='bm_bn_3')(x)
    x = Conv2D(128, (1, 1), activation='relu', name='bm_conv2d_3', **kwargs)(x)  # 48x48x128
    for _ in range(4):
        x = subblock(x, 64, 0 + _, **kwargs)

    x = MaxPooling2D((2, 2), strides=(2, 2), name='bm_mp_4')(x)  # 24x24x128
    x = BatchNormalization(name='bm_bn_4')(x)
    x = Conv2D(256, (1, 1), activation='relu', name='bm_conv2d_4', **kwargs)(x)  # 24x24x256
    for _ in range(4):
        x = subblock(x, 64, 4 + _, **kwargs)

    x = MaxPooling2D((2, 2), strides=(2, 2), name='bm_mp_5')(x)  # 12x12x256
    x = BatchNormalization(name='bm_bn_5')(x)
    x = Conv2D(384, (1, 1), activation='relu', name='bm_conv2d_5', **kwargs)(x)  # 12x12x384
    for _ in range(4):
        x = subblock(x, 96, 8 + _, **kwargs)

    x = MaxPooling2D((2, 2), strides=(2, 2), name='bm_mp_6')(x)  # 6x6x384
    x = BatchNormalization(name='bm_bn_6')(x)
    x = Conv2D(512, (1, 1), activation='relu', name='bm_conv2d_6', **kwargs)(x)  # 6x6x512
    for _ in range(4):
        x = subblock(x, 128, 12 + _, **kwargs)

    x = GlobalMaxPooling2D(name='gmp_2d')(x)  # 512
    branch_model = Model(inp, x, name='branch_model')

    ############
    # HEAD MODEL
    ############
    mid = 32
    xa_inp = Input(shape=branch_model.output_shape[1:], name='hm_inp_a')
    xb_inp = Input(shape=branch_model.output_shape[1:], name='hm_inp_b')
    x1 = Lambda(lambda x: x[0] * x[1], name='lambda_1')([xa_inp, xb_inp])
    x2 = Lambda(lambda x: x[0] + x[1], name='lambda_2')([xa_inp, xb_inp])
    x3 = Lambda(lambda x: K.abs(x[0] - x[1]), name='lambda_3')([xa_inp, xb_inp])
    x4 = Lambda(lambda x: K.square(x), name='lambda_4')(x3)
    x = Concatenate(name='concat_1')([x1, x2, x3, x4])
    x = Reshape((4, branch_model.output_shape[1], 1), name='reshape1')(x)

    # Per feature NN with shared weight is implemented using CONV2D with appropriate stride.
    x = Conv2D(mid, (4, 1), activation='relu', padding='valid', name='hm_conv_2d_1')(x)
    x = Reshape((branch_model.output_shape[1], mid, 1), name='hm_reshape_2')(x)
    x = Conv2D(1, (1, mid), activation='linear', padding='valid', name='hm_conv_2d_2')(x)
    x = Flatten(name='flatten')(x)

    # Weighted sum implemented as a Dense layer.
    x = Dense(1, use_bias=True, activation=activation, name='weighted-average')(x)
    head_model = Model([xa_inp, xb_inp], x, name='head_model')

    ########################
    # SIAMESE NEURAL NETWORK
    ########################
    # Complete model is constructed by calling the branch model on each input image,
    # and then the head model on the resulting 512-vectors.
    img_a = Input(shape=img_shape)
    img_b = Input(shape=img_shape)
    xa = branch_model(img_a)
    xb = branch_model(img_b)
    x = head_model([xa, xb])
    model = Model([img_a, img_b], x, name='full_model')
    model.compile(optim, loss='binary_crossentropy', metrics=['binary_crossentropy', 'acc'])
    return model, branch_model, head_model

def build_model_multich(channels, pretrained_weights, img_shape):

    model_multi_ch, branch_model_3ch, head_model_3ch = build_model(64e-5, 0, img_shape=img_shape)

    if pretrained_weights is not None:
        model, branch_model, head_model = build_model(64e-5, 0, img_shape=(img_shape[0], img_shape[1], 1))
        print("\nLoading weights from {} ".format(pretrained_weights))
        model.load_weights(pretrained_weights, by_name=False, skip_mismatch=False)

        print(branch_model.summary())
        for layer in branch_model_3ch.layers:
            print('Go for: {}'.format(layer.name))
            if layer.name == 'bm_conv2d_1':
                layer_old = branch_model.get_layer(layer.name)
                weights, bias = layer_old.get_weights()
                print('Old weights shape: {}'.format(weights.shape))
                print('Old bias shape: {}'.format(bias.shape))
                new_weights = np.zeros((9, 9, 3, 64), dtype=np.float32)
                new_weights[:, :, 0:0+1, :] = weights / channels
                new_weights[:, :, 1:1+1, :] = weights / channels
                new_weights[:, :, 2:2+1, :] = weights / channels
                layer.set_weights([new_weights, bias])
                continue
            layer_old = branch_model.get_layer(layer.name)
            weights = layer_old.get_weights()
            layer.set_weights(weights)
        for layer in head_model_3ch.layers:
            print('Go for: {}'.format(layer.name))
            layer_old = head_model.get_layer(layer.name)
            weights = layer_old.get_weights()
            layer.set_weights(weights)
        print(model_multi_ch.summary())

    return model_multi_ch, branch_model_3ch, head_model_3ch

def build_model_with_other_shape(pretrained_weights, img_shape=(512, 512, 1)):
    model, branch_model, head_model = build_model(64e-5, 0, img_shape=img_shape)
    print("\nLoading weights from {} ".format(pretrained_weights))
    model.load_weights(pretrained_weights, by_name=False, skip_mismatch=False)
    print(model.summary())
    return model, branch_model, head_model

def freeze_unfreeze_model(model, freeze=True, layer_name='bm_conv2d_1'):
    for layer in model.layers:
        if layer.name == layer_name:
            print("\nSetting layer {} trainable. \n".format(layer_name))
            layer.trainable = True
            continue
        if freeze:
            layer.trainable = False
        else:
            layer.trainable = True
    return model

def generate_train_val_4_fold_split(RUN_FOLD):

    def _find_train_val_split_4_fold(l, run_fold):
        assert len(l) > 1
        kf = KFold(n_splits=4)

        if len(l) == 2:
            return l, []
        elif len(l) == 3:
            if run_fold == 3:
                return l, []
            else:
                return l[:run_fold]+l[run_fold+1:], [l[run_fold]]
        else:
            tr, val = list(kf.split(l))[run_fold]
            return [l[i] for i in tr], [l[i] for i in val]

    def _expand_path_RGB(p):
        if isfile('../data/train_array_RGB/' + p): return '../data/train_array_RGB/' + p
        if isfile('../data/test_array_RGB/' + p): return '../data/test_array_RGB/' + p
        if isfile('../data/old/train_array/' + p): return '../data/old/train_array/' + p
        return None

    print("\n", "*"*70)
    print("Started generating train val splits ... \n")

    ### using zft bbox
    temp_p2bb = pickle.load(open('../modified_data/p2bb_averaged_v1.pkl', 'rb'))
    p2bb1 = {}
    for k in temp_p2bb:
        p2bb1[k+'.npy'] = temp_p2bb[k]
    temp_p2bb = pickle.load(open('../modified_data/p2bb_averaged_playground_v1.pkl', 'rb'))
    p2bb2 = {}
    for k in temp_p2bb:
        p2bb2[k+'.npy'] = temp_p2bb[k]
    p2bb = {**p2bb1, **p2bb2}

    # load tagged data
    tagged = dict([(p[:-4]+'.npy',w) for _,p,w in read_csv('../data/train.csv').to_records()])
    newdata = dict([(k[:-4]+'.npy', v) for k,v in pickle.load(open('../modified_data/new_train_data.pkl', 'rb')).items()])
    newdata_bootstrap_Dec17 = dict([(k[:-4]+'.npy', v) for k,v in pickle.load(open('../modified_data/bootstrap_data/bootstrap_data_model_LB891.pkl', 'rb')).items()])
    tagged = dict(**tagged, **newdata)
    for ndk in newdata_bootstrap_Dec17:
        if ndk not in tagged:
            tagged[ndk] = newdata_bootstrap_Dec17[ndk]
        elif tagged[ndk] == newdata_bootstrap_Dec17[ndk]:
            if 0:
                print("Ignored duplicate keys with same values: {} -> {}".format(ndk, tagged[ndk]))
        else:
            print("Error. Found duplicate keys with different values: {} -> {}, {}".format(ndk, tagged[ndk], newdata_bootstrap_Dec17[ndk]))
            assert 0
    print("Total tagged images: ", len(tagged))

    # whale ID -> photo
    w2p = {}
    for img, lb in tagged.items():
        if img in p2bb:      # remove bad images!
            if lb not in w2p:
                w2p[lb] = [img]
            else:
                w2p[lb].append(img)

    # test
    submit = [p for _,p,_ in read_csv('../data/sample_submission.csv').to_records()]
    submit = [f[:-4]+'.npy' for f in submit]
    join   = set(list(tagged.keys()) + submit)
    print(len(tagged),len(submit),len(join),list(tagged.items())[:5],submit[:5])

    # shuffle all data
    np.random.seed(12321)
    for k in w2p:
        np.random.shuffle(w2p[k])

    valid_neg = []
    for k in w2p:
        if k == 'new_whale':
            valid_neg += w2p[k]
    valid_neg_batch = len(valid_neg) // 4
    print("Total new whale images: ", len(valid_neg))
    valid_neg = valid_neg[RUN_FOLD*valid_neg_batch:(RUN_FOLD+1)*valid_neg_batch][:1000]  # only select 1000 negative val images
    print("Total neg images in val for this fold: ", len(valid_neg))

    train, val = [], []
    w2ts = {} # valid whale label to photos

    for k in w2p:
        if len(w2p[k]) > 1 and k != 'new_whale':
            train_split, val_split = _find_train_val_split_4_fold(w2p[k], RUN_FOLD)
            train += train_split
            val += val_split
            w2ts[k] = np.array(train_split).astype(object)

    train = sorted(train)
    val += valid_neg
    print("\nTotal train images: ", len(train))
    print("\nTotal val images: {}\n".format(len(val)))

    # photo -> image shape
    p2size = {}
    for p in join:
        size      = np.load(_expand_path_RGB(p)).shape
        p2size[p] = size
    print(len(p2size), list(p2size.items())[:5])

    print("Done generating train val splits for fold {}! \n".format(RUN_FOLD))
    print("*"*70)

    return train, val, tagged, p2size


#coding:utf-8

import MeCab

tagger = MeCab.Tagger("-Owakati")
patrain = open('train.sent', 'w')
patest = open('test.sent', 'w')
padev = open('dev.sent', 'w')
antrain = open('train.ans', 'w')
antest = open('test.ans', 'w')
andev = open('dev.ans', 'w')

nc = 0
pc = 0

with open('train.txt') as f:
    for l in f:
        l = l.split("\n")[0]
        sent = l.split("\t")[0]
        score = l.split("\t")[1]
        if float(score) == -1:
            score = str(0)
        res = tagger.parse(sent).split("\n")[0]
        if nc < 1000 and score == '0':
            padev.write(res+"\n")
            andev.write(score+"\n")
            nc += 1
        elif pc < 1000 and score == '1':
            padev.write(res+"\n")
            andev.write(score+"\n")
            pc += 1
        else:
            patrain.write(res+"\n")
            antrain.write(score+"\n")

patrain.close()
antrain.close()
padev.close()
andev.close()

with open('test.txt') as f:
    for l in f:
        l = l.split("\n")[0]
        sent = l.split("\t")[0]
        score = l.split("\t")[1]
        if float(score) > 3:
            score = str(1)
        else:
            score = str(0)
        res = tagger.parse(sent).split("\n")[0]
        patest.write(res+"\n")
        antest.write(score+"\n")

patest.close()
antest.close()


from  flask import Flask,render_template
app=Flask(__name__)
@app.route("/")
def index():
    name="张三"
    age=18
    gender="男"
    return render_template("index.html",**locals())
if __name__ == '__main__':
    app.run(host="192.168.11.12",port=6789,debug=True)


"""
Driver for the Si7021 humidity and temperature sensor
requires SMBus
"""
from smbus import SMBus
from time import sleep

class RHTS:
    """ Driver for Si7021 temperatuer and humidity sensor
    """
    meas_RH = 0xf5
    read_temp = 0xe0
    reset = 0xfe
    def __init__(self, i2cbus, addr):
        self.i2cbus = i2cbus
        self.bus = SMBus(self.i2cbus)
        self.addr = addr
    def measure(self):
        self.bus.write_byte(self.addr, self.meas_RH)
        sleep(0.3)
    def readall(self):
        """Units of temperature in  deg C """
        self.measure()
        # Pull the MSB of the RH data
        data0 = self.bus.read_byte(self.addr)
        # Pull the LSB from the RH data
        data1 = self.bus.read_byte(self.addr)
        # Calculate the RH from user manual section 5.1.1
        RH = ((data0*256 + data1)*125 / 65535.0)-6
        self.RH = max(0, min(100, RH))
        # Pull the two byte temperature dats - needs byte shifting
        data3 = self.bus.read_word_data(self.addr, self.read_temp)
        # Shift the bytes
        data4 = ((data3 & 0xff) << 8) | (data3 >> 8)
        # Calculate the temperature in deg C from manual section 5.1.1
        self.tempC = 175.72 * data4 / 65536. - 46.85
        # return the RH and Temperature data
        return [self.tempC, self.RH]


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

# In[ ]:

import pandas as pd
import numpy as np
import scipy.optimize as optimize
import matplotlib.pyplot as plt
from tqdm import tqdm

# In[ ]:

from google.colab import drive
drive.mount('/content/drive')

# In[ ]:

path = '/content/drive/MyDrive/problem_set_2_sample/'

# # problem 1

# In[ ]:

def logfun(X, y, network, sigma, lambda_, beta1, beta2, alpha):
    mg = len(X)
    I = np.eye(mg)
    epsilon = np.dot((I - lambda_*network),y) - np.dot(X, beta1).reshape(-1,1) - np.dot(network,np.dot(X, beta2)).reshape(-1,1) - alpha
    return (mg/2*np.log(2*np.pi)) + (mg/2*np.log(sigma**2)) - np.log(np.linalg.det(I-lambda_*network)) + (1/(2*sigma**2)*np.dot(epsilon.T,epsilon))

def data_loadding(i):
    group = np.loadtxt(path + 'group/group%s.dat'%i)
    network = np.loadtxt(path + 'network/network%s.dat'%i)
    data = pd.DataFrame(group)
    X = data[[0,1,2,3,4,5,7,8,9,10,11,12,13]].values
    y = data[[17]].values
    return X, y, network

def total_logfun(para):
    for i in range(1,77):
        sigma = para[0]
        lambda_ = para[1]
        beta1 = para[2:15]
        beta2 = para[15:28]
        alpha = para[i+27]
        value = logfun(load_X['group%s'%i], load_y['group%s'%i], load_network['group%s'%i], sigma, lambda_, beta1, beta2, alpha)
        value += value
    return value[0][0]

# In[ ]:

load_X = {}
load_y = {}
load_network = {}
for i in range(1,77):
    X, y, network = data_loadding(i)
    load_X['group%s'%i] = X
    load_y['group%s'%i] = y
    load_network['group%s'%i] = network

# In[ ]:

def estimator(sig, lamb, beta1, beta2):
        alpha = np.zeros(76)+5
        beta1 = np.zeros(13)+beta1
        beta2 = np.zeros(13)+beta2
        sig_lamb = np.array([sig, lamb])
        value = 0
        para = np.concatenate((sig_lamb, beta1, beta2, alpha), axis=0)

        buds = [(None,None), (-0.1, 0.1)]
        for i in range(len(para)-2):
            buds.append((None,None))

        result = optimize.minimize(total_logfun, para, bounds=buds, method='SLSQP',options={'disp': True, 'maxiter' : 200})
        #result = optimize.minimize(total_logfun, para, bounds=buds,options={'disp': True, 'maxiter' : 1000})
        return result

# In[ ]:

from datetime import datetime
start=datetime.now()
value = estimator(0.706, 0.068, 0.499, 0.491) #teacher table1 SAR modle
print(datetime.now()-start)

# In[ ]:

para = value.x
std = value.jac
print('sigma :', para[0])
print('sigma standard error:', (1/(std[0]*std[0]))**0.5)
print('-----------------------------------------------------------------------------------')
print('Lambda :', para[1])
print('Lambda standard error:', (1/(std[1]*std[1]))**0.5)
print('-----------------------------------------------------------------------------------')
print('Beta1 :', para[2:16])
print('Beta1 standard error:', (1/(std[2:16]*std[2:16]))**0.5)
print('-----------------------------------------------------------------------------------')
print('Beta2 :', para[16:30])
print('Beta2 standard error:', (1/(std[16:30]*std[16:30]))**0.5)
print('-----------------------------------------------------------------------------------')
print('Alpha :', para[30:])
print('Alpha standard error:', (1/(std[30:]*std[30:]))**0.5)

# # problem 2-3

# In[ ]:

def logfun(X, y, network, sigma, lambda_, beta1, beta2, alpha):
    mg = len(X)
    I = np.eye(mg)
    epsilon = np.dot((I - lambda_*network),y) - np.dot(X, beta1).reshape(-1,1) - np.dot(network,np.dot(X, beta2)).reshape(-1,1) - alpha
    return -(mg/2*np.log(2*np.pi)) - (mg/2*np.log(sigma**2)) + np.log(np.linalg.det(I-lambda_*network)) - (1/(2*sigma**2)*np.dot(epsilon.T,epsilon))

def data_loadding(i):
    group = np.loadtxt(path + 'group/group%s.dat'%i)
    network = np.loadtxt(path + 'network/network%s.dat'%i)
    data = pd.DataFrame(group)
    X = data[[0,1,2,3,4,5,7,8,9,10,11,12,13]].values
    y = data[[17]].values
    return X, y, network

def total_logfun(sigma, lambda_, beta1, beta2, alpha):
    sigma = sigma
    lambda_ = lambda_
    beta1 = beta1
    beta2 = beta2
    for i in range(1,77):
        alpha_ = alpha[i-1]
        value = logfun(load_X['group%s'%i], load_y['group%s'%i], load_network['group%s'%i], sigma, lambda_, beta1, beta2, alpha_)
        value += value
    return value[0][0]

# In[ ]:

load_X = {}
load_y = {}
load_network = {}
for i in range(1,77):
    X, y, network = data_loadding(i)
    load_X['group%s'%i] = X
    load_y['group%s'%i] = y
    load_network['group%s'%i] = network

# In[ ]:

# Lambda

def lambda_(t):
    lambda_t = np.random.multivariate_normal([lambda_T[t-1]], np.eye(1)*0.1**2)
    like_2 = total_logfun(sigma2_T[t-1], lambda_T[t-1], beta1_T[t-1], beta2_T[t-1], alpha_T[t-1])
    like_1 = total_logfun(sigma2_T[t-1], lambda_t, beta1_T[t-1], beta2_T[t-1], alpha_T[t-1])
    pp_l = like_1 - like_2
    pp_l = min([np.exp(pp_l), 1])
    if np.random.rand(1)[0] <= pp_l:
        lambda_T[t] = lambda_t
    else:
        lambda_T[t] = lambda_T[t-1]
    return lambda_T

# In[ ]:

# Beta

def beta_(t):
    zvx1, zvx2 = 0, 0
    zvy1, zvy2 = 0, 0
    for i in range(1,77):
        mg = len(load_X['group%s'%i])
        I = np.eye(mg)
        ss = I - lambda_T[t]*load_network['group%s'%i] # mg*mg
        yy = np.dot(ss,load_y['group%s'%i]) - alpha_T[t-1][i-1]
        zz_x1 = load_X['group%s'%i] # mg*13
        zz_x2 = np.dot(load_network['group%s'%i], load_X['group%s'%i]) # mg*13

        zvx1 = zvx1 + np.dot(zz_x1.T, zz_x1)/(sigma2_T[t-1]**2) # 13*13
        zvx2 = zvx2 + np.dot(zz_x2.T, zz_x2)/(sigma2_T[t-1]**2)

        zvy1 = zvy1 + np.dot(zz_x1.T, yy)/(sigma2_T[t-1]**2) # mg*1
        zvy2 = zvy2 + np.dot(zz_x2.T, yy)/(sigma2_T[t-1]**2)

    B1 = np.linalg.inv((np.linalg.inv(B_0)+zvx1))
    B2 = np.linalg.inv((np.linalg.inv(B_0)+zvx2))
    mean_beta1 = np.ravel((np.dot(B1, (np.dot(np.linalg.inv(B_0), beta1_0).reshape(-1, 1)+zvy1))))
    mean_beta2 = np.ravel((np.dot(B2, (np.dot(np.linalg.inv(B_0), beta2_0).reshape(-1, 1)+zvy2))))
    beta1_hat = np.random.multivariate_normal(mean_beta1, B1, 1)
    beta2_hat = np.random.multivariate_normal(mean_beta2, B2, 1)

    beta1_T[t], beta2_T[t]= beta1_hat, beta2_hat
    return beta1_T, beta2_T

# In[ ]:

# Sigma^2

def sigma2_(t):
    ep_ = np.zeros(1)
    for i in range(1, 77):
        y = load_y['group%s'%i]
        X = load_X['group%s'%i]
        network = load_network['group%s'%i]
        mg = len(load_X['group%s'%i])
        I = np.eye(mg)
        epsilon = np.dot((I - lambda_T[t]*network),y) - np.dot(X, beta1_T[t]).reshape(-1,1)
        - np.dot(network,np.dot(X, beta2_T[t])).reshape(-1,1) - alpha_T[t-1][i-1]
        ep_ = np.append(ep_, epsilon)
    ep_ = ep_[1:]
    rho_1 = rho_0 + len(ep_)
    chi = np.random.chisquare(rho_1)
    sigma2_T[t] = (np.dot(ep_.reshape(1,-1), ep_.reshape(-1,1))[0][0] + eta_0)/chi
    return sigma2_T

# In[ ]:

# Alpha

def alpha_(t):
    dd = 1/(1/alpha_0 + 1/sigma2_T[t])
    for i in range(1,77):
        mg = len(load_X['group%s'%i])
        I = np.eye(mg)
        ss = I - lambda_T[t]*load_network['group%s'%i] # mg*mg
        yy = np.dot(ss,load_y['group%s'%i]) # mg*1
        zz_x1 = load_X['group%s'%i] # mg*13
        zz_x2 = np.dot(load_network['group%s'%i], load_X['group%s'%i]) # mg*13
        alpha_T[t][i-1] = (dd/sigma2_T[t]*sum(yy - np.dot(load_X['group%s'%i], beta1_T[t]).reshape(-1,1)
            - np.dot(load_network['group%s'%i],np.dot(load_X['group%s'%i], beta2_T[t])).reshape(-1,1))[0]) + np.random.normal(0, dd**0.5, 1)
    return alpha_T

# In[ ]:

_lambda, _beta1, _beta2, _sigma2, _alpha = [], [], [], [], []

# In[ ]:

T = 50000
beta1_T = np.zeros([T, 13])
beta2_T = np.zeros([T, 13])
lambda_T = np.zeros(T)
sigma2_T = np.zeros(T) + 1
alpha_T = np.zeros([T, 76])
rho_0 = 2.2
eta_0 = 0.1

alpha_0 = 1
beta1_0 = np.zeros(13)
beta2_0 = np.zeros(13)
B_0 = np.eye(13) + 3

zvx1, zvx2 = 0, 0
zvy1, zvy2 = 0, 0

for t in tqdm(range(1,T)):
    lambda_(t)
    beta_(t)
    sigma2_(t)
    alpha_(t)

# In[ ]:

plt.figure(figsize=(20,10))
plt.title('Lambda')
plt.plot(lambda_T)

# In[ ]:

plt.figure(figsize=(20,10))
plt.title('Sigma^2')
plt.plot(sigma2_T)

# In[ ]:

plt.figure(figsize=(20,10))
plt.title('Beta1 - age')
plt.plot(beta1_T.T[0])

# In[ ]:

plt.figure(figsize=(20,10))
plt.title('Beta1')
plt.plot(beta1_T)

# In[ ]:

plt.figure(figsize=(20,10))
plt.title('Beta2')
plt.plot(beta2_T)

# In[ ]:

print('lambda :', np.mean(lambda_T, axis=0))
print('-----------------------------------------------------------------------------------')
print('sigma^2 :', np.mean(sigma2_T, axis=0))
print('-----------------------------------------------------------------------------------')
print('Beta1 :', np.mean(beta1_T, axis=0))
print('-----------------------------------------------------------------------------------')
print('Beta2 :', np.mean(beta2_T, axis=0))
print('-----------------------------------------------------------------------------------')
print('Alpha :', np.mean(alpha_T, axis=0))

# In[ ]:


#!/usr/bin/env python

#-----------------------------------------------------------------------------
# Copyright (c) 2013--, scikit-bio development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

__version__ = '0.0.0-dev'

from setuptools import find_packages, setup
from distutils.command.build_py import build_py

classes = """
    Development Status :: 1 - Planning
    License :: OSI Approved :: BSD License
    Topic :: Software Development :: Libraries
    Topic :: Scientific/Engineering
    Topic :: Scientific/Engineering :: Bio-Informatics
    Programming Language :: Python
    Programming Language :: Python :: 2.7
    Operating System :: Unix
    Operating System :: POSIX
    Operating System :: MacOS :: MacOS X
"""
classifiers = [s.strip() for s in classes.split('\n') if s]

long_description = """The brokit project"""

setup(name='brokit',
      cmdclass={'build_py': build_py},
      version=__version__,
      license='BSD',
      description='brokit',
      long_description=long_description,
      author="scikit-bio development team",
      author_email="gregcaporaso@gmail.com",
      maintainer="scikit-bio development team",
      maintainer_email="gregcaporaso@gmail.com",
      url='https://github.com/biocore/brokit',
      packages=find_packages(),
      install_requires=['scikit-bio == 0.0.0-dev'],
      dependency_links=[
        'https://github.com/biocore/scikit-bio/archive/master.zip#egg=scikit-bio-0.0.0-dev'
      ],
      extras_require={},
      classifiers=classifiers
      )


import cv2
import time
import math
import sys
import os
import gym
import numpy as np
from gym import error, spaces
from gym import utils
from gym.utils import seeding
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import collections
import itertools as IT
from shapely.geometry import Point, asPoint, asPolygon
from shapely.geometry.polygon import Polygon
from shapely import speedups
import shapely.vectorized as sv
from shapely.ops import cascaded_union
from shapely.prepared import prep
import copy

from uav_enviroment.env_utils import *
from uav_enviroment.env_plotting import *
# import Box2D
# from Box2D.b2 import (edgeShape, circleShape, fixtureDef, polygonShape, revoluteJointDef, contactListener)

import gym
from gym import spaces
from gym.utils import seeding, EzPickle

FPS = 50
SCALE = 30.0  # affects how fast-paced the game is, forces should be adjusted as well

INITIAL_RANDOM = 1000.0  # Set 1500 to make game harder

VIEWPORT_W = 600
VIEWPORT_H = 400

"""
HARDCODINGS in HERE, change it to define other problem.

Target Point: the target point you try to reach
threshold_dist = the distance at which we consider the game done
MAX_TIMESTEP = constant with maximum amount of steps to reach the target
TIME_NEGATIVE_REWARD = When you exceed the Max timestamp you get a negative reward
(this number was 130(max_x)^2 + 230(max_y)^2)
OUT_OF_BOUNDS_REWARD = when you get out of bounds you get a small negative reward
POSITIVE_REWARD = reward when you reach the goal

inecuations for reward shaping:
    POSITIVE_REWARD > 2*MAX_TIMESTEP
    OUT_OF_BOUNDS_REWARD > 6 *MAX_TIMESTEP + 60(this numbers depend on the scaling factor)

ENV_LOWS and ENV_HIGHS are the constants to define the space, the first 2 components are the range in which we can move
the last 2 are the min and max speed.
    Args:
        OBS:
        [pos_x,pos_y,vel_x,vel_y,target_x,target_y]
        ACT:
        [accel_x,accel_y]

REWARD_NORMALIZATION_FACTOR = 1/this is the reward
FRAME_RATE = the fps at which the game will run
SCALING_FACTOR = the speed increase for the drone for each action
"""

max_timestep = 1000
REWARD_NORMALIZATION_FACTOR = 10 * max_timestep
OUT_OF_BOUNDS_REWARD = 1.2 * (10 * max_timestep)
# TIME_NEGATIVE_REWARD = OUT_OF_BOUNDS_REWARD/4
TIME_NEGATIVE_REWARD = 0

POSITIVE_REWARD = 1.2 * (10 * max_timestep)

EMPTY_TRAJECTORY = {'x': [],  # Position.x
                    'y': [],  # Position y
                    'vel': [],  # Velocity
                    'dist': [],  # Distance
                    'reward': []  # Reward
                    }

# OBS:
# [distance_to_target/MAX_DIST , orientation_to_target, speed_norm/MAX_VEL, p_crash, theta_crash]
# ACT:
# [thrust,steering_angle]
MAX_DIST = 100
MAX_VEL = 50
ENV_OBS_LOWS = np.array([0, -1, 0, 0, -1])
ENV_OBS_HIGH = np.array([1, 1, 1, 1, 1])
ENV_ACT_LOWS = np.array([0, -1])
ENV_ACT_HIGH = np.array([10, 1])

NUM_DISCRETE_ACTIONS = 5

STATE_SIZE = 6

threshold_distance = 40
threshold_velocity = 4

N_OBSTACLES = 0  # Better keep it under 75 to speed up
PERCEPTION_DISTANCE = 60
IMAGE_WIDTH = 300
IMAGE_HEIGTH = 300

# # TODO go back to 64 it
OBS_IMAGE_WIDTH = 600
OBS_IMAGE_HEIGTH = 600

STEERING_VEL = 0.1

FRAMES_PER_SECOND = 5
SCALING_FACTOR = 4
GAMMA = 0.99
RENDER_EPISODE = 5

# Helper Classes if needed

class UAVEnv(gym.Env):
    """ Class UAVEnv that models the AIRsim simulator dynamics.
    The main API methods that users of this class need to know are:
        step
        reset
        render
        close
        seed
    And set the following attributes:
        action_space: The Space object corresponding to valid actions
        observation_space: The Space object corresponding to valid observations
        reward_range: A tuple corresponding to the min and max possible rewards
    Note: The methods are accessed publicly as "step", "reset", etc.. The
    non-underscored versions are wrapper methods to which we may add
    functionality over time.
    """
    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second': FPS
    }

    continuous = True  # Denotes if the action space is continuous
    dense_reward = True  # True if the reard is dense, else it will be +1 if success -1 if not
    angular_movement = True  # When True dynamics are modeled with steer and thrust, else they are modeled with dx, dy
    observation_with_image = False  # When True return an image alongside the observation
    reset_always = True  # When True change the environment origin, target and obstacles every time it resets.
    controlled_speed = True  # When True the task is to reach the target with a controlled speed, else is just reach the target

    logging_episodes = 1000

    map_size_x = 200
    map_size_y = 200
    map_min_x = - map_size_x * 0.1
    map_min_y = - map_size_y * 0.1
    map_max_x = map_size_x * 1.1
    map_max_y = map_size_y * 1.1
    threshold_dist = 40
    threshold_vel = 4

    n_obstacles = 0

    max_timestep = 1000

    def __init__(self, continuous=True, angular_movement=True, observation_with_image=False, reset_always=True,
                 controlled_speed=True):
        self.seed()
        self.viewer = None

        self.continuous = continuous  # Denotes if the action space is continuous
        # dense_reward = True  # True if the reard is dense, else it will be +1 if success -1 if not
        self.angular_movement = angular_movement  # When True dynamics are modeled with steer and thrust, else they are modeled with dx, dy
        self.observation_with_image = observation_with_image  # When True return an image alongside the observation
        self.reset_always = reset_always  # When True change the environment origin, target and obstacles every time it resets.
        self.controlled_speed = controlled_speed  # When True the task is to reach the target with a controlled speed, else is just reach the target
        # Shapely Speedups
        # if speedups.available:
        speedups.enable()

        # TODO things that shouldn't be here
        self.render_episode = False
        self.num_envs = 1

        # # move this outside the env (check if they serve the resetalways flag)
        # self.latest_results = collections.deque(maxlen=150)

        self.done = [False]
        self.reward_range = (-OUT_OF_BOUNDS_REWARD, POSITIVE_REWARD)
        self.spec = None

        # Counters
        self.episode = 0
        self.total_timestep = 0
        self.episode_success = True
        self.n_done = 0
        self.oo_time = 0
        self.oob = 0
        self.crashes = 0

        # Rendering variables
        self.trajectory = copy.deepcopy(EMPTY_TRAJECTORY)
        self.RENDER_FLAG = False

        # Simulation parameters
        self.time_step = 0.1  # To obtain trajectories

        u_max = MAX_VEL / np.sqrt(2)
        v_max = MAX_VEL / np.sqrt(2)
        u_min = MAX_VEL / np.sqrt(2)
        v_min = MAX_VEL / np.sqrt(2)

        # Dynamic characteristics
        um = max([abs(u_max), abs(u_min)])
        vm = max([abs(v_max), abs(v_min)])
        self.w = np.sqrt(um ** 2 + vm ** 2)
        self.F = SCALING_FACTOR  # It was 20 in airsim i think
        self.k = self.F / self.w

        # Obstacles Variables
        self.obstacles = []
        self.prep_obstacles = []
        self.obstacle_centers = np.array([])
        self.culled = []

        # IMPLEMENTED THE IMAGE AS PARTIAL OBSERVABILITY
        perception_shape = (IMAGE_HEIGTH, IMAGE_WIDTH, 3)
        observation_shape = (OBS_IMAGE_HEIGTH, OBS_IMAGE_WIDTH, 3)
        self.field_of_view = np.zeros(perception_shape, np.uint8)

        # State definition
        self.s = {'x': [0.0],  # Position.x
                  'y': [0.0],  # Position y
                  'u': [0.0],  # Velocity x
                  'v': [0.0],  # Velocity y
                  'target_x': [0.0],
                  'target_y': [0.0],
                  'origin_x': [0.0],
                  'origin_y': [0.0]
                  }

        # Model the angular_perception according to the dynamics: either ANGULAR or CARTESIAN
        if self.angular_movement:
            n_discrete_actions = 4  # Thrust, Brake,  Steer Left, Steer Right
            env_act_lows = np.array([0, -1])  # Thrust and Steer
            env_act_high = np.array([1, 1])
        else:
            # Perception matrix initialization, only with cartesian(dx,dy) dynamics.
            self.perception_matrix = create_perception_matrix(dist=PERCEPTION_DISTANCE, n_radius=16, pts_p_radius=6)
            self.perception_ray_points = create_perception_distance_points(max_dist=PERCEPTION_DISTANCE, n_radius=16)
            n_discrete_actions = 4  # +dx, -dx, +dy, -dy
            env_act_lows = np.array([-1, -1])  # dx and dy
            env_act_high = np.array([1, 1])

        # Define the action space either CONTINUOUS or DISCRETE.
        if self.continuous:
            self.action_space = spaces.Box(low=env_act_lows, high=env_act_high)
        else:
            self.action_space = spaces.Discrete(n_discrete_actions)

        # Define the action space either CONTINUOUS or DISCRETE.
        if self.angular_movement:
            ENV_OBS_LOWS = np.zeros(shape=self.get_angular_observation().shape)
            ENV_OBS_HIGH = np.ones(shape=self.get_angular_observation().shape)
        else:
            ENV_OBS_LOWS = np.zeros(shape=self.get_cartesian_observation().shape)
            ENV_OBS_HIGH = np.ones(shape=self.get_cartesian_observation().shape)
            # ENV_OBS_HIGH[0:6] = [self.map_max_x, self.map_max_y, MAX_VEL, MAX_VEL, self.map_max_x, self.map_max_y]

        # self.observation_space = spaces.Box(low=ENV_OBS_LOWS, high=ENV_OBS_HIGH, dtype=np.float32)

        # Define the observation space with or without an image
        if self.observation_with_image:
            self.observation_space = spaces.Box(low=0, high=255, dtype=np.uint8, shape=observation_shape)
        else:
            self.observation_space = spaces.Box(low=ENV_OBS_LOWS, high=ENV_OBS_HIGH, dtype=np.float32)

        if self.controlled_speed:
            self.reward_function = self._controlled_speed_reward()  # controlled speed reward
        else:
            self.reward_function = self._no_speed_reward()  # no speed reward

    def setup(self, *, map_size_x=map_size_x, map_size_y=map_size_y, n_obstacles=0, reset_always=True,
              max_timestep=1000, threshold_dist=40, threshold_vel=4, reward_sparsity=False):

        self.map_size_x = map_size_x
        self.map_size_y = map_size_y
        self.map_min_x = - map_size_x * 0.1
        self.map_min_y = - map_size_y * 0.1
        self.map_max_x = map_size_x * 1.1
        self.map_max_y = map_size_y * 1.1
        self.n_obstacles = n_obstacles
        self.reset_always = reset_always
        self.max_timestep = max_timestep
        self.threshold_dist = threshold_dist
        self.threshold_vel = threshold_vel
        # True is sparse reward, False is a dense reward
        self.reward_sparsity = reward_sparsity
        self.dense_reward = not reward_sparsity

        if self.angular_movement:  # set the appropriate methods for the observation
            self.get_observation = self.get_angular_observation
        else:
            self.get_observation = self.get_cartesian_observation

        if self.observation_with_image:
            self.get_observation = self.get_image_observation

        if self.continuous:  # For continuous actions
            if self.angular_movement:
                self._take_action = self._cont_angular_action  # Continuous angular
            else:
                self._take_action = self._cont_cartesian_action  # Continuous cartesian
        else:  # For discrete actions
            if self.angular_movement:
                self._take_action = self._disc_angular_action  # Discrete angular
            else:
                self._take_action = self._disc_cartesian_action  # Discrete cartesian

        self.generate_map()

    def reset(self):
        """Resets the state of the environment and returns an initial observation.
        Returns: observation (object): the initial observation of the
            space.
        """
        # print('Reset, episode', self.episode, ' next target:', self.target)
        self.episode += 1
        self.total_timestep = 0
        self.trajectory = copy.deepcopy(EMPTY_TRAJECTORY)

        # # TODO remove after
        # self.output_video = None
        # if self.RENDER_FLAG:
        #     if self.output_video is not None:
        #         self.output_video.release()
        #     # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
        #     self.output_video = cv2.VideoWriter('/home/daniel/Videos/DQN_training/DQN_ep{}.avi'.format(self.episode),
        #                                         cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 60,
        #                                         (IMAGE_WIDTH, IMAGE_HEIGTH))
        #

        # NEW ENVIRONMENT
        if self.reset_always:
            # Reset the initial position and the target ALWAYS
            self.generate_map()
            self.episode_success = False
        else:
            # Reset the initial position and the target WHEN SUCCESSFUL
            if self.episode_success:
                self.generate_map()
                self.episode_success = False

        # Origin and target are modified in the generate map function
        self.s['x'] = self.s['origin_x']
        self.s['y'] = self.s['origin_y']
        self.s['u'] = [np.multiply(np.random.uniform(size=1), 0.1)[0] - 0.05]
        self.s['v'] = [np.multiply(np.random.uniform(size=1), 0.1)[0] - 0.05]

        observation = self.get_observation()
        self.done = np.array([False])

        return observation

    # @profile
    def step(self, action):
        """Run one timestep of the environment's dynamics. When end of
        episode is reached, you are responsible for calling `reset()`
        to reset this environment's state.
        Accepts an action and returns a tuple (observation, reward, done, info).
        Args:
            action (object): an action provided by the environment
        Returns:
            observation (object): agent's observation of the current environment
            reward (float) : amount of reward returned after previous action
            done (boolean): whether the episode has ended, in which case further step() calls will return undefined results
            info (dict): contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)
        """
        self.total_timestep += 1
        # if self.total_timestep % 20 == 0:
        #     print('TTL = ', self.total_timestep)
        # print( 'TTL = ', self.total_timestep)
        # print('action = ',action)

        # Making sure action has the right size
        action = np.reshape(action, (action.size,))
        # Take the action
        x, y, u, v, t_x, t_y = self.get_state()

        x_next, y_next, u_next, v_next = self._take_action(action, x, y, u, v)

        self.s['x'] = [x_next]
        self.s['y'] = [y_next]
        self.s['u'] = [u_next]
        self.s['v'] = [v_next]

        # Select some obstacles so that the computations are easier
        point = np.array([self.s['x'], self.s['y']]).reshape((-1, 2))
        if self.obstacles:
            self.culled = fast_distance(point, self.obstacle_centers) < 3 * PERCEPTION_DISTANCE

        # Get Observations
        observation = self.get_observation()

        # # TODO remove after
        # if self.RENDER_FLAG:
        #     img = self.get_image_observation()
        #     cv2.imshow('observation', img)
        #     cv2.waitKey(1)
        #     if self.output_video is not None:
        #         self.output_video.write(img)

        # compute the reward
        reward = self._reward_function()

        # Result console logging
        if self.episode % self.logging_episodes == 0 and self.total_timestep == 1:
            print('Episode ', self.episode)
            print('good = ', self.n_done, ', timeouts = ', self.oo_time,
                  ', OoBounds = ', self.oob, ', Crashes = ', self.crashes)

            self.n_done = 0
            self.oo_time = 0
            self.oob = 0
            self.crashes = 0

        # has the game finished
        done = self.done

        # Render functions
        self.trajectory['x'].append(self.s['x'])
        self.trajectory['y'].append(self.s['y'])

        info = {}

        return observation, reward, np.array(done), info

    def get_observation(self):
        """ method called to get the observation, override with the appropriate one """
        return self.get_angular_observation()

    def get_state(self):
        x = self.s['x'][0]
        y = self.s['y'][0]
        u = self.s['u'][0]
        v = self.s['v'][0]
        t_x = self.s['target_x'][0]
        t_y = self.s['target_y'][0]

        return x, y, u, v, t_x, t_y

    # @profile
    def get_cartesian_observation(self):
        x, y, u, v, t_x, t_y = self.get_state()

        dist = np.linalg.norm((t_x - x, t_y - y))
        clipped_dist = min(MAX_DIST, dist) / MAX_DIST
        vel = np.linalg.norm(np.array([u, v]))
        clipped_vel = min(MAX_VEL, vel) / MAX_VEL

        dir_x = np.nan_to_num(((t_x - x) / (dist + 1e-6)) * clipped_dist)
        dir_y = np.nan_to_num(((t_y - y) / (dist + 1e-6)) * clipped_dist)

        vel_x = np.nan_to_num((u / (vel + 1e-6)) * clipped_vel)
        vel_y = np.nan_to_num((v / (vel + 1e-6)) * clipped_vel)

        # p_vector = self.grid_collisions_perception(x, y)
        p_vector = self.ray_collision_perception((x, y))
        # print('rays: {}'.format(p_vector))

        # if self.total_timestep % 20 == 0:
        #     print('state =        {0:.2f}, {1:.2f}, {2:.2f}, {3:.2f}, {4:.2f}, {5:.2f}'.format(x, y, u, v, t_x, t_y))
        #     print('observations = {0:.2f}, {1:.2f}, {2:.2f}, {3:.2f}'.format(dir_x, dir_y, vel_x, vel_y))

        return np.concatenate(([dir_x, dir_y, vel_x, vel_y], p_vector))

    def grid_collisions_perception(self, x, y):
        point = np.array([x, y]).reshape((-1, 2))
        PER = self.perception_matrix + point
        p_vector = np.zeros(PER.size // 2)

        # get only close objects to check the perception
        # culled = [as_point.distance(obstacle) < 1.5*PERCEPTION_DISTANCE for obstacle in self.obstacles]
        if self.obstacles:
            self.culled = fast_distance(point, self.obstacle_centers) < 3 * PERCEPTION_DISTANCE
            for obstacle in IT.compress(self.prep_obstacles, self.culled):
                # Vectorized
                p_i = sv.contains(obstacle, x=PER[:, 0], y=PER[:, 1])
                p_vector = np.logical_or(p_vector, p_i)

        return p_vector

    def ray_collision_perception(self, position):
        """

        :param position: numpy array with the position of the agent
        :return: a list with distances to the collision of each one of the rays
        """
        rays = create_perception_rays(position=position, points=self.perception_ray_points)
        position_point = np.array(position).reshape((-1, 2))
        # get only close objects to check the perception
        if self.obstacles:
            self.culled = fast_distance(position_point, self.obstacle_centers) < 3 * PERCEPTION_DISTANCE
            for obstacle in IT.compress(self.prep_obstacles, self.culled):
                # Vectorized
                rays = [ray.difference(obstacle) for ray in rays]
        # TODO test

        # print('rays: {}'.format(rays))
        return [(ray.length / PERCEPTION_DISTANCE) for ray in rays]

    def get_angular_observation(self):
        x, y, u, v, t_x, t_y = self.get_state()
        # This gets the angle between the direction of your speed and the direction of the target
        dist = min(MAX_DIST, np.linalg.norm((t_x - x, t_y - y))) / MAX_DIST
        theta = np.nan_to_num(angle_difference([x, y], [u, v], [t_x, t_y]))
        vel = min(MAX_VEL, np.sqrt(u ** 2 + v ** 2)) / MAX_VEL
        p_choque, theta_crash = self.angular_perception()

        return np.array([dist, theta, vel, p_choque, theta_crash])

    def angular_perception(self):
        point = np.array([self.s['x'], self.s['y']])
        vel = np.array([self.s['u'], self.s['v']])

        # get only close objects to check the angular_perception
        if len(self.obstacles) > 0:
            self.culled = fast_distance(point, self.obstacle_centers) < 3 * PERCEPTION_DISTANCE

        crash_angle = 0
        p_crash = 0
        left_angle = 0
        right_angle = 0
        for obstacle in IT.compress(self.prep_obstacles, self.culled):
            exteriors = np.array(obstacle.exterior.coords.xy)
            # Angles between our speed and the obstacles borders
            obstacle_angles = angle_difference(point, vel, exteriors)
            if not (all(a < 0 for a in obstacle_angles[0:4]) or
                    all(a > 0 for a in obstacle_angles[0:4]) or
                    all(a > np.pi / 2 for a in np.abs(obstacle_angles[0:4]))):
                # print('crush')
                # We care for the maximum angle of each side
                # Left angles are the ones with a positive difference
                candidate_left_angle = max([x for x in obstacle_angles if x > 0])
                # Right angles are the ones with a negative difference
                candidate_right_angle = min([x for x in obstacle_angles if x <= 0])

                if candidate_left_angle > left_angle: left_angle = candidate_left_angle
                if candidate_right_angle < right_angle: right_angle = candidate_right_angle
                p_crash = max(p_crash,
                              1 - (min(fast_distance(point.reshape(2, 1), exteriors[0:4])) / (2 * PERCEPTION_DISTANCE)))

            candidate_angles = np.array([left_angle, right_angle])
            crash_angle = candidate_angles[np.argmin(np.abs(candidate_angles))]
            # sigue siendo un poco naive la implementacion pero coges el angulo mas grande para desviarte, dando igual izquierda o derecha
            # dará problemas pero hay que encontrar una forma de manejar los angulos positivos de una forma y los negativos de otra.
        # print('crash angle: ', crash_angle, ' p_crash: ', p_crash)

        return p_crash, crash_angle

    # @profile
    def get_image_observation(self):
        x, y, u, v, t_x, t_y = self.get_state()

        img = self.field_of_view.copy()
        pos = np.array((x, y))
        half_window = np.array([IMAGE_WIDTH // 2, IMAGE_HEIGTH // 2])

        # Plot target
        target = np.array((t_x, t_y)) - pos + half_window
        cv2.circle(img, tuple((target).astype(np.int)), self.threshold_dist, (0, 0, 255), thickness=-1)

        # Plot obstacles

        # for obstacle in self.obstacles:
        for obstacle in IT.compress(self.prep_obstacles, self.culled):
            obstacle_in_image(img, obstacle, pos, half_window)

        # Plot the ship and velocity
        u = u * 4
        v = v * 4
        cv2.circle(img, tuple(half_window), 4, (0, 255, 0), thickness=-1)
        cv2.line(img, tuple(half_window), (int(IMAGE_WIDTH // 2 + u), int(IMAGE_HEIGTH // 2 + v)), (0, 255, 0), 2)

        # # Print Equivalent
        # cv2.imshow('observation', img)
        # cv2.waitKey(1)
        #

        img = cv2.resize(img, (OBS_IMAGE_HEIGTH, OBS_IMAGE_WIDTH))
        # print('img')
        return img

    def _take_action(self, action):
        """ method called to modify the state with each action, override with the appropriate one"""
        return self._cont_angular_action(action)

    def _cont_angular_action(self, action, x, y, u, v):
        """ continuous angular dynamics"""

        x_next = x + u * self.time_step
        y_next = y + v * self.time_step
        r = np.sqrt(u ** 2 + v ** 2)
        theta = np.arctan2(v, u)
        r_next = min(MAX_VEL, max(0, r + (self.F * action[0] - self.k * r)))
        theta_next = theta + action[1] * np.pi * STEERING_VEL
        u_next = r_next * np.cos(theta_next)
        v_next = r_next * np.sin(theta_next)

        return x_next, y_next, u_next, v_next

    def _cont_cartesian_action(self, action, x, y, u, v):
        """ continuous cartesian dynamics"""
        vel = np.linalg.norm(np.array([u, v]))
        lim_u = np.abs(u / vel) * MAX_VEL
        lim_v = np.abs(v / vel) * MAX_VEL
        x_next = x + u * self.time_step
        y_next = y + v * self.time_step
        u_next = (self.F * action[0] - self.k * u) * self.time_step + u
        v_next = (self.F * action[1] - self.k * v) * self.time_step + v
        u_next = np.clip(u_next, -lim_u, lim_u)  # Limit the max speed
        v_next = np.clip(v_next, -lim_v, lim_v)

        return x_next, y_next, u_next, v_next

    def _disc_angular_action(self, action, x, y, u, v):
        """ discrete angular dynamics"""
        offset = self._interpret_action(action)

        x_next = x + u * self.time_step
        y_next = y + v * self.time_step
        r = np.sqrt(u ** 2 + v ** 2)
        theta = np.arctan2(v, u)
        r_next = min(MAX_VEL, max(0, r + (self.F * offset[0] - self.k * r)))
        theta_next = theta + offset[1] * np.pi * STEERING_VEL
        u_next = r_next * np.cos(theta_next)
        v_next = r_next * np.sin(theta_next)

        return x_next, y_next, u_next, v_next

    def _disc_cartesian_action(self, action, x, y, u, v):
        """ discrete cartesian dynamics"""
        offset = self._interpret_action(action)

        vel = np.linalg.norm(np.array([u, v]))
        lim_u = np.abs(u / vel) * MAX_VEL
        lim_v = np.abs(v / vel) * MAX_VEL
        x_next = x + u * self.time_step
        y_next = y + v * self.time_step
        u_next = (self.F * offset[0] - self.k * u) * self.time_step + u
        v_next = (self.F * offset[1] - self.k * v) * self.time_step + v
        u_next = np.clip(u_next, -lim_u, lim_u)  # Limit the max speed
        v_next = np.clip(v_next, -lim_v, lim_v)

        return x_next, y_next, u_next, v_next

    def _interpret_action(self, action):
        # print(action)
        scaling_factor = 1
        if action == 0:
            quad_offset = (scaling_factor, 0)
        elif action == 1:
            quad_offset = (-scaling_factor, 0)
        elif action == 2:
            quad_offset = (0, -scaling_factor)
        elif action == 3:
            quad_offset = (0, scaling_factor)
        else:
            quad_offset = (0, 0)
        return quad_offset

    def _reward_function(self):
        """ method called to compute the reward based on which type of task it is:
                eg: reach the target or reach it with controlled speed
        """
        return self._controlled_speed_reward()

    # @profile
    def _controlled_speed_reward(self):
        # k1 = 0.01
        # k2 = 1
        # k3 = 0.01
        # k4 = 1
        # k5 = -80
        # k6 = - k5
        # k7 = 3
        # k8 = 0.03
        # # reward = -k1 * ((d2 + k4) * (1 + k2 * (min(k7 * v2 + k5, 0) / (k8 * d2 + k3)))) - k6

        x, y, u, v, t_x, t_y = self.get_state()

        dist = np.float64(10000000)
        dist = min(dist, np.linalg.norm([x - t_x, y - t_y]))
        vel = np.linalg.norm(np.array([u, v]))

        # Reward in base of distance
        # if self.total_timestep % 20 == 0:
        #     print('dist: ', np.array([dist]), ' vel: ', np.array([vel]))
        reward = 0
        if dist < self.threshold_dist and vel < self.threshold_vel:
            reward = + POSITIVE_REWARD
            self.done = np.array([True])
            # print('Target Found')
            self.n_done += 1
            self.episode_success = True
        else:
            d2 = np.square(dist)
            v2 = np.square(vel)
            reward = (-0.01 * (
                        (d2 + 1) * (1 + 1 * (min(3 * v2 - 80, 0) / (0.03 * d2 + 0.01)))) - 80) * self.dense_reward
        # Max Timestep
        if self.total_timestep >= self.max_timestep:
            reward -= TIME_NEGATIVE_REWARD
            self.oo_time += 1
            self.done = np.array([True])
            # print('Timeout')

        # Out of bounds check
        if x < self.map_min_x or y < self.map_min_y or x > self.map_max_x or y > self.map_max_y:
            reward += -OUT_OF_BOUNDS_REWARD
            self.oob += 1
            self.done = np.array([True])
            # print('Out of Bounds')

        # COLLISIONS check
        for obstacle in IT.compress(self.prep_obstacles, self.culled):
            if sv.contains(obstacle, x=x, y=y):
                reward += -OUT_OF_BOUNDS_REWARD
                # print('Crash')
                self.crashes += 1
                self.done = np.array([True])
                break

        reward = reward / REWARD_NORMALIZATION_FACTOR
        # print(reward)

        # Printing functions

        self.trajectory['vel'].append(vel)
        self.trajectory['dist'].append(dist)
        self.trajectory['reward'].append(reward)

        return reward

    def _no_speed_reward(self):

        x, y, u, v, t_x, t_y = self.get_state()

        dist = np.float64(10000000)
        dist = min(dist, np.linalg.norm([x - t_x, y - t_y]))
        vel = np.linalg.norm(np.array([u, v]))

        # Reward in base of the distance
        # if self.total_timestep % 20 == 0:
        #     print('dist: ', np.array([dist]), ' vel: ', np.array([vel]))
        reward = 0
        if dist < self.threshold_dist:
            reward = + POSITIVE_REWARD
            self.done = np.array([True])
            # print('Target Found')
            self.n_done += 1
            self.episode_success = True
        else:
            d2 = np.square(dist)
            reward = (- 0.01 * (d2 + 1) - 80) * self.dense_reward
        # Max Timestep
        if self.total_timestep >= self.max_timestep:
            reward -= TIME_NEGATIVE_REWARD
            self.oo_time += 1
            self.done = np.array([True])
            # print('Timeout')

        # Out of bounds check
        if x < self.map_min_x or y < self.map_min_y or x > self.map_max_x or y > self.map_max_y:
            reward += -OUT_OF_BOUNDS_REWARD
            self.oob += 1
            self.done = np.array([True])
            # print('Out of Bounds')

        # COLLISIONS check
        for obstacle in IT.compress(self.prep_obstacles, self.culled):
            if sv.contains(obstacle, x=x, y=y):
                reward += -OUT_OF_BOUNDS_REWARD
                # print('Crash')
                self.crashes += 1
                self.done = np.array([True])
                break

        reward = reward / REWARD_NORMALIZATION_FACTOR
        # print(reward)

        # Printing functions

        self.trajectory['vel'].append(vel)
        self.trajectory['dist'].append(dist)
        self.trajectory['reward'].append(reward)

        return reward

    def render(self, mode='human', close=False):
        """Renders the environment.
        The set of supported modes varies per environment. (And some
        environments do not support rendering at all.) By convention,
        if mode is:
        - human: render to the current display or terminal and
          return nothing. Usually for human consumption.
        - rgb_array: Return an numpy.ndarray with shape (x, y, 3),
          representing RGB values for an x-by-y pixel image, suitable
          for turning into a video.
        - ansi: Return a string (str) or StringIO.StringIO containing a
          terminal-style text representation. The text can include newlines
          and ANSI escape sequences (e.g. for colors).
        Note:
            Make sure that your class's metadata 'render.modes' key includes
              the list of supported modes. It's recommended to call super()
              in implementations to use the functionality of this method.
        Args:
            mode (str): the mode to render with
            close (bool): close all open renderings
        Example:
        class MyEnv(Env):
            metadata = {'render.modes': ['human', 'rgb_array']}
            def render(self, mode='human'):
                if mode == 'rgb_array':
                    return np.array(...) # return RGB frame suitable for video
                elif mode is 'human':
                    ... # pop up a window and render
                else:
                    super(MyEnv, self).render(mode=mode) # just raise an exception
        """
        if mode == 'human':
            render_trajectory(self)
        if mode == 'rgb_array':
            return self.get_image_observation()
        pass

    def close(self):
        """Override _close in your subclass to perform any necessary cleanup.
        Environments will automatically close() themselves when
        garbage collected or when the program exits.
        """
        super().close()

        cv2.destroyAllWindows()

    def seed(self, seed=None):
        """Sets the seed for this env's random number generator(s).
        Note:
            Some environments use multiple pseudorandom number generators.
            We want to capture all such seeds used in order to ensure that
            there aren't accidental correlations between multiple generators.
        Returns:
            list<bigint>: Returns the list of seeds used in this env's random
              number generators. The first value in the list should be the
              "main" seed, or the value which a reproducer should pass to
              'seed'. Often, the main seed equals the provided 'seed', but
              this won't be true if seed=None, for example.
        """
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def generate_map(self):
        self.obstacles = []
        for _ in range(self.n_obstacles):
            self.add_obstacle(self.generate_obstacle())
        # Added these 2 lines to reduce number of polys and increase performance
        # self.obstacles = cascaded_union(self.obstacles)
        # self.prep_obstacles = [prep(polygon) for polygon in self.obstacles]
        self.prep_obstacles = self.obstacles
        # self.obstacle_centers = np.array([obstacle.centroid.coords[0] for obstacle in self.obstacles])
        self.s['origin_x'], self.s['origin_y'] = self.generate_start_point()
        self.s['target_x'], self.s['target_y'] = self.generate_target_point()

    def generate_start_point(self):
        intersections = [True]  # We initialize it with something to run the loop
        origin = [0, 0]

        while any(intersections):
            origin = np.multiply(np.random.uniform(size=2), np.array([self.map_size_x, self.map_size_y]))
            point = asPoint(origin)
            intersections = []
            [intersections.append(obstacle.contains(point)) for obstacle in self.obstacles]

        return [origin[0]], [origin[1]]

    def generate_target_point(self):
        intersections = [True]  # We initialize it with something to run the loop
        target = [0, 0]
        while any(intersections):
            dist = 0
            while dist < 2 * self.threshold_dist:
                target = np.multiply(np.random.uniform(size=2), np.array([self.map_size_x, self.map_size_y]))
                dist = np.linalg.norm([self.s['origin_x'] - target[0], self.s['origin_y'] - target[1]])

            point = asPoint(target)
            intersections = []
            [intersections.append(obstacle.contains(point)) for obstacle in self.obstacles]

        return [target[0]], [target[1]]

    def generate_obstacle(self):
        """
        :return: a rectangle obstacle
        """
        x0 = np.random.rand() * self.map_size_x * 0.9
        y0 = np.random.rand() * self.map_size_y * 0.9
        w = (np.random.rand() + 0.2) * self.map_size_x * 0.2
        h = (np.random.rand() + 0.2) * self.map_size_y * 0.2
        p1, p2, p3, p4 = [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)]
        # print([(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)])
        return asPolygon([p1, p2, p3, p4])

    def add_obstacle(self, obstacle: Polygon):
        self.obstacles.append(obstacle)
        self.obstacle_centers = np.array([obstacle.centroid.coords[0] for obstacle in self.obstacles])
        self.prep_obstacles = self.obstacles


import ctypes
import string
import sys

def get_sensor_id_device(pSpecDevice):
    """get sensor id

    This function will get the sensor ID from Apollo

    * \brief Get sensor ID of physical device(sensor)
    *
    * This function returns a sensor ID of currently activated physical device(sensor).
    * Current sensor is an activated one by duActivateDevice() function.
    *
    * \code
    *   char sensor_id[SENSOR_ID_STRING_LENGTH];
    *   int ret_value = duGetSensorID(sensor_id);
    * \endcode
    *
    * \param pointer to char array for sensor ID data [OUT]
    *
    * \return
    * Returns one numeric value of:
    * - NSP_RETURN_VALUE_SUCCESS (1)
    * - NSP_RETURN_VALUE_FAILURE (-1)
    """

    sensorIDBuffer = ctypes.c_char * 26
    sensorID = sensorIDBuffer()

    ret = pSpecDevice.duGetSensorID(sensorID)

    if ret <=0:
        print ("[PythonPrismError] UnKnown Sensor ID From Device" , sensorID.value)
        return (-1,-1)
    else:
        print ("[PythonPrism] SensorID From Device: ", sensorID.value)
        return (ret,sensorID.value)


import re
from testBot.constants import SCRAPY_FAILURE_HTML
from testBot.exceptions import HTMLParsingException

class Utils(object):
    error_type = 0
    error_description = ""

    def __init__(self, config, hotel, not_scraped_items, not_scraped_items_optional):
        self.not_scraped_items_optional = not_scraped_items_optional
        self.hotel = hotel
        self.config = config
        self.not_scraped_items = not_scraped_items

    def evaluate_tag(self, parent_tag, parent_selector, target):
        try:
            children = 0
            for child_tag in self.config["BotInfo"]["HtmlTags"]:
                if self.config["BotInfo"]["HtmlTags"][child_tag]["parent"] == parent_tag:
                    children += 1
                    child_selector = parent_selector.xpath(self.config["BotInfo"]["HtmlTags"][parent_tag]["ByXpath"])
                    self.evaluate_tag(child_tag, child_selector, target)
            if children == 0:
                value = self.get_element_value(parent_tag, parent_selector)
                if value is None or len(value) == 0:
                    if not self.config["BotInfo"]["HtmlTags"][parent_tag]["isOptional"]:
                        if parent_tag not in self.not_scraped_items:
                            self.not_scraped_items.append(parent_tag)
                    else:
                        if parent_tag not in self.not_scraped_items_optional:
                            self.not_scraped_items_optional.append(parent_tag)
                else:
                    if parent_tag == "hotelTaxDetailsSelector":
                        target = self.get_tax_details(target, value)
                    else:
                        target = self.save(target, self.config["BotInfo"]
                                           ["HtmlTags"][parent_tag]["SavePath"].split(";"), value)
            return target
        except HTMLParsingException as e:
            if not self.config["BotInfo"]["HtmlTags"][parent_tag]["isOptional"]:
                if parent_tag not in self.not_scraped_items:
                    self.not_scraped_items.append(parent_tag)
            else:
                if parent_tag not in self.not_scraped_items_optional:
                    self.not_scraped_items_optional.append(parent_tag)
            self.error_type = SCRAPY_FAILURE_HTML
            self.error_description = str(e)
            raise e
        except Exception as e:
            self.error_type = SCRAPY_FAILURE_HTML
            self.error_description = str(e)

    def get_room_details(self, selector):
        try:
            rooms_table_div = self.get_element_value("hotelRoomsTableDivSelector", selector)
            rooms_div = self.get_element_value("hotelRoomsRowDivSelector", rooms_table_div)
            room_id = 0
            for room_div in rooms_div:
                room = self.get_room()
                availability_div = self.get_element_value("roomAvailabilityDivSelector", room_div)
                if availability_div is not None and len(availability_div) == 0:
                    check = self.get_element_value("roomCheckSelector", room_div)
                    if check is None or len(check) == 0:
                        room = self.hotel["Rooms"][room_id-1]
                        for tag in self.config["BotInfo"]["HtmlTags"]:
                            if tag == "hotelPriceDivSelector":
                                room = self.get_price_details(room, room_div)
                            elif tag == "hotelBedroomTypeDivSelector":
                                room = self.get_bedroom_details(room, room_div)
                            elif self.config["BotInfo"]["HtmlTags"][tag]["parent"] == "hotelRoomsRowSelector" and\
                                    tag != "roomCheckSelector" and tag != "roomAvailabilityDivSelector" and not\
                                    self.config["BotInfo"]["HtmlTags"][tag]["filter"]:
                                room = self.evaluate_tag(tag, room_div, room)
                    else:
                        room_id += 1
                        room["Availability"] = "O"
                        room["Error_Description"] = None
                        for tag in self.config["BotInfo"]["HtmlTags"]:
                            if tag == "hotelPriceDivSelector":
                                room = self.get_price_details(room, room_div)
                            elif tag == "hotelBedroomTypeDivSelector":
                                room = self.get_bedroom_details(room, room_div)
                            elif self.config["BotInfo"]["HtmlTags"][tag]["parent"] == "hotelRoomsRowSelector" and\
                                    tag != "roomCheckSelector" and tag != "roomAvailabilityDivSelector":
                                room = self.evaluate_tag(tag, room_div, room)
                else:
                    room = self.get_room()
                    room_id += 1
                    for tag in self.config["BotInfo"]["HtmlTags"]:
                        if self.config["BotInfo"]["HtmlTags"][tag]["parent"] == "hotelRoomsRowSelector" and \
                                tag != "roomCheckSelector" and tag != "hotelPriceDivSelector"\
                                and tag != "roomAvailabilityDivSelector":
                            room = self.evaluate_tag(tag, room_div, room)
                    room["Error_Description"] = self.get_element_value("hotelRoomStatusSelector", availability_div)
                    room["Availability"] = "AVL01"
                self.hotel["Rooms"].append(room)
        except Exception as e:
            raise e

    def get_room(self):
        room = {
            "Url": "",
            "PagePosition": "",
            "PageRank": "",
            "OverallRank": "",
            "OutBoundFlightDetails": "",
            "ReturnFlightDetails": "",
            "Discount": 0,
            "Transferincluded": "",
            "TransferType": "",
            "CheapestRateIndicator": "",
            "FilePath": "",
            "Extra2": "",
            "Extra3": "",
            "Extra5": "",
            "Extra4": "",
            "HitCount": "",
            "Facilities": [],
            "DailyRate": {"TaxInfo": {}},
            "StayAmount": {"TaxInfo": {}}
        }
        return room

    def get_hotel_details(self):
        # adding hotel details
        try:
            self.hotel["WebsiteName"] = self.config["BotInfo"]["WebsiteName"]
            self.hotel["SiteId"] = self.config["BotInfo"]["WebsiteID"]
            self.hotel["rateRequestId"] = ""
            self.hotel["LOS"] = self.config["CrawlParams"]["minimumLengthofStay"]
            self.hotel["HotelId"] = self.config["CrawlParams"]["propertyID"]
            self.hotel["ServerResponseTime"] = ""
            self.hotel["PageContentSize_MD"] = None
            self.hotel["RequestSegmentId"] = self.config["MetaData"]["RequestSegmentId"]
            self.hotel["ReportID"] = self.config["MetaData"]["ReportId"]
            self.hotel["CityId"] = self.config["CrawlParams"]["cityId"]
            self.hotel["isRequestFailed"] = None
            self.hotel["RequestType"] = self.config["CrawlParams"]["type"]
            self.hotel["CheckInDate"] = self.config["CrawlParams"]["checkInDate"]
            self.hotel["CheckOutDate"] = self.config["CrawlParams"]["checkOutDate"]
            self.hotel["City"] = self.config["CrawlParams"]["city"]
            self.hotel["RequestCurrency"] = self.config["CrawlParams"]["currency"]
            self.hotel["Rooms"] = []
        except Exception as e:
            raise e

    def save(self, target, save_path, value):
        try:
            if len(save_path) == 0:
                return value
            else:
                if save_path[0] == "add":
                    if isinstance(value, str):
                        target.append(value)
                    else:
                        target += value
                else:
                    if save_path[0] not in target:
                        target[save_path[0]] = self.save({}, save_path[1:], value)
                    else:
                        target[save_path[0]] = self.save(target[save_path[0]], save_path[1:], value)
                return target
        except Exception as e:
            raise e

    def get_element_value(self, tag, selector):
        try:
            if self.config["BotInfo"]["HtmlTags"][tag]["isValue"]:
                output = selector.xpath(self.config["BotInfo"]["HtmlTags"][tag]["ByXpath"]+"/text()")
                if output is not None and len(output) != 0:
                    if tag == "hotelOtherFacilitiesSelector":
                        output = [x.strip("\n") for x in output.extract()]
                    else:
                        output = output.extract_first().strip("\n").strip()
            else:
                output = selector.xpath(self.config["BotInfo"]["HtmlTags"][tag]["ByXpath"])
                if "string(" in self.config["BotInfo"]["HtmlTags"][tag]["ByXpath"] and\
                        "Div" not in tag:
                    if output is not None and len(output) != 0:
                        if tag == "hotelTaxDetailsSelector":
                            output = output.extract()
                        else:
                            output = output.extract_first().strip("\n").strip()
            return output
        except Exception as e:
            raise HTMLParsingException(str(e))

    def get_price_details(self, room, selector):
        price_div = self.get_element_value("hotelPriceDivSelector", selector)
        price_check = self.get_element_value("hotelRoomDiscountPriceSelector", price_div)
        if price_check is not None and len(price_check) != 0:
            discount_price = self.get_element_value("hotelRoomDiscountPriceSelector", price_div)
            original_price = self.get_element_value("hotelRoomOriginalPriceSelector", price_div)
            original_price = re.findall('\d+', original_price)
            discount_price = re.findall('\d+', discount_price)
            if len(original_price) == 2:
                original_price = float(original_price[0]) + float(original_price[1]) / 100
            elif len(original_price) == 1:
                original_price = float(original_price[0])
            if len(discount_price) == 2:
                discount_price = float(discount_price[0]) + float(discount_price[1]) / 100
            elif len(discount_price) == 1:
                discount_price = float(discount_price[0])
            room["DailyRate"]["Price"] = discount_price / int(self.config["CrawlParams"]["minimumLengthofStay"])
            room["StayAmount"]["Price"] = discount_price
            room["Discount"] = (original_price - discount_price) * 100 / float(original_price)
            room["RateCategory"] = "O"
        else:
            standard_price = self.get_element_value("hotelRoomStandardPriceSelector", price_div)
            standard_price = re.findall('\d+', standard_price)
            if len(standard_price) == 2:
                standard_price = float(standard_price[0]) + float(standard_price[1]) / 100
            elif len(standard_price) == 1:
                standard_price = float(standard_price[0])
            room["DailyRate"]["Price"] = standard_price / int(self.config["CrawlParams"]["minimumLengthofStay"])
            room["StayAmount"]["Price"] = standard_price
            room["RateCategory"] = "O"
        return room

    def get_tax_details(self, target, tax_info):
        tax = "".join(tax_info)
        tax = tax.strip("\n")
        tax = tax.split("\n")
        tax = [i for i in tax if i != ""]
        tax_included = []
        tax_not_included = []
        inclusion = True
        for i in range(len(tax)):
            if "Not included" in tax[i]:
                inclusion = False
            elif "Included" in tax[i]:
                inclusion = True
            elif '%' in tax[i]:
                if inclusion:
                    tax_included.append(tax[i])
                else:
                    tax_not_included.append(tax[i])
        daily_rate = target["DailyRate"]
        stay_amount = target["StayAmount"]
        if len(tax_included) == 0:
            daily_rate["isTaxIncluded"] = False
            stay_amount["isTaxIncluded"] = False
            for i in tax_not_included:
                temp = i.strip(",").split("%")
                daily_rate["TaxInfo"][temp[1]] = temp[0]
                stay_amount["TaxInfo"][temp[1]] = temp[0]
        else:
            daily_rate["isTaxIncluded"] = True
            stay_amount["isTaxIncluded"] = True
            for i in tax_included:
                temp = i.strip(",").split("%")
                daily_rate["TaxInfo"][temp[1]] = temp[0]
                stay_amount["TaxInfo"][temp[1]] = temp[0]
        target["DailyRate"] = daily_rate
        target["StayAmount"] = stay_amount
        return target

    def get_bedroom_details(self, room, room_div):
        try:
            bedroom = self.get_element_value("hotelBedroomTypeDivSelector", room_div)
            check = self.get_element_value("hotelBedtypeFixedSelector", bedroom)
            if check is not None and len(check) != 0:
                room = self.evaluate_tag("hotelBedtypeFixedSelector", room_div, room)
            else:
                room = self.evaluate_tag("hotelBedtypeOptionsSelector", room_div, room)
            return room
        except Exception as e:
            raise e


import random
from urllib.parse import urlparse
from datetime import datetime, timedelta

import pytz
import requests
from bs4 import BeautifulSoup
from django.core.management import BaseCommand
from django.utils import timezone

class Command(BaseCommand):
    help = '국민일보 최신기사 리스트를 DB에 추가한다'
    press = None
    name = '국민일보'
    url_base = 'https://kmib.co.kr'

    headers = {'User-Agent': 'Mozilla/5.0 ' + '(Windows NT 10.0; Win64; x64) ' +
                             'AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/60.0.3112.113 Safari/537.36'}

    # def add_arguments(self, parser):
    #     parser.add_argument('press_keyword', type=str)
    #

    def handle(self, *args, **options):
        """
        우선 연합 뉴스만 예시로 작성한다.
        """
        urls = list()

        now = datetime.now()
        dates = [now]
        if now.hour < 6:
            dates.append(now - timedelta(1)) # requirement: 6시간 이상 지나지 않았으면 전날도 크롤링

        print(dates)
        for adate in dates:
            for page in range(10, 0, -1):
                url = f'http://news.kmib.co.kr/article/list.asp?sid1=all&sid2=&page={page}&sdate={adate:%Y%m%d}&st='
                urls.append(url)
                print(url)

        for url in urls:
            try:
                _response = requests.get(url, headers=self.headers)
                _response.encoding = 'cp949'
                _response.close()

            except requests.exceptions.ConnectionError as e:
                _second = random.randrange(5 * 60, 15 * 60)
                # sleep_with_message(_second, '리스트가 읽히지 않습니다. 차단을 피하기 위해서 긴 대기 시간을 가집니다.')
                exit()

            soup = BeautifulSoup(_response.text, 'lxml')
            soup_list = soup.select_one('.nws_list')

            for item in soup_list.select('div.nws'):
                dt = item.select_one('dt').select_one('a')
                url = dt['href']
                title = dt.string
                datetime_str = item.select_one('dd.date').string
                datetime_obj = datetime.strptime(datetime_str, '%Y-%m-%d %H:%M')
                summary = item.select_one('dd.tx').string.strip()
                thumbnail_obj = item.select_one('p.pic a img')
                if thumbnail_obj: thumbnail_src = thumbnail_obj['src']

                print(f'국민일보: {datetime_obj}: {title}: {url}')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.core.validators

class Migration(migrations.Migration):

    dependencies = [
        ('curation', '0017_video_country'),
    ]

    operations = [
        migrations.AddField(
            model_name='video',
            name='url',
            field=models.TextField(default=None, null=True, validators=[django.core.validators.URLValidator()]),
        ),
        migrations.AlterField(
            model_name='video',
            name='country',
            field=models.CharField(blank=True, max_length=70, null=True, choices=[('FR', 'France'), ('DE', 'Germany'), ('IT', 'Italy'), ('FI', 'Finland'), ('NL', 'Netherlands'), ('UK', 'UK'), ('LU', 'Luxembourg')]),
        ),
    ]


def game():
    num = 42
    guess = input("im thinking of a number.\
try to guess it: ")
    while(guess != num):
        guess = int(guess)
        if(guess < num):
            print('too low')
        else:
            print('too high')
        guess = input("try guess again?")
    choice=input("Want to play again (y/n)")
    if(choice == "yes"):
        game()
    else:
        print('thank you for playing')

game()


import tensorflow as tf
import cv2
import time

print(tf.__version__)

cascade_path = 'cashaarcascade_frontalcatface_extended.xml'
cascade = cv2.CascadeClassifier(cascade_path)
#cascade.load('/root/PycharmProjects/untitled/cashaarcascade_frontalcatface_extended.xml')
#cascade.load('./haarcascades/cashaarcascade_frontalcatface_extended.xml')
cascade.load('/root/PycharmProjects/untitled/opencv-master/data/haarcascades/haarcascade_frontalcatface.xml')

catpic = '/root/PycharmProjects/untitled/Data_CatFace/Caicai/Caicai_3.jpg'
img = cv2.imread(catpic)
#print(len(img))
# cv2.imshow('catpic', img)
# cv2.waitKey(0)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# cv2.imshow('gray', img_gray)
# cv2.waitKey(0)
catfaces = cascade.detectMultiScale(img_gray, scaleFactor=1.03, minNeighbors=5, minSize=(10, 10))

print(len(catfaces))
if len(catfaces) > 0:
    print('cat face detected')
    for(i, (x, y, w, h)) in enumerate(catfaces):
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), thickness=2)
        roiImg = img[y:y + h, x:x + w]
        cv2.imwrite('face.jpg', roiImg)
        cv2.imshow('facedected', roiImg)
        cv2.waitKey(0)
    cv2.imwrite('cat.jpg', img)

time.sleep(10)


# import necessary libraries
import matplotlib.pyplot as plt
import numpy as np

# define the functino to plot
def plotter(ax, data1, data2, param_dict):
    out = ax.plot(data1, data2, **param_dict)
    return out

# use the defined plotter function
data1, data2, data3, data4 = np.random.randn(4, 100)
fig, ax = plt.subplots(1, 1)
plotter(ax, data1, data2, {'marker': 'x'})
plt.show()

# using two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)
plotter(ax1, data1, data2, {'marker': 'x'})
plotter(ax2, data3, data4, {'marker': 'o'})
plt.show()