import pyb, sensor, image, math, time
from machine import Timer

enable_lens_corr = False # turn on for straighter lines...
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()

f_x = (2.8 / 3.984) * 160 # find_apriltags defaults to this if not set
f_y = (2.8 / 2.952) * 120 # find_apriltags defaults to this if not set
c_x = 160 * 0.5 # find_apriltags defaults to this if not set (the image.w * 0.5)
c_y = 120 * 0.5 # find_apriltags defaults to this if not set (the image.h * 0.5)

# initial UART
uart = pyb.UART(3,9600,timeout_char=1000)
uart.init(9600,bits=8,parity = None, stop=1, timeout_char=1000)
send = "initial"

# flag
color_det_active = False
line_det_active = False
april_det_active = False

trigger = 0
color_trigger = 0

# color Threshold
red = (0, 100, 11, 127, -128, 127)
green = (38, 100, -128, 29, 32, 127)

# All lines also have x1(), y1(), x2(), and y2() methods to get their end-points
# and a line() method to get all the above as one 4 value tuple for draw_line().
def degrees(radians):
    return (180 * radians) / math.pi

def uart_detect(s):
    global color_det_active
    global line_det_active
    global april_det_active
    if(s == b'color'):
        color_det_active = True
        line_det_active = False
        april_det_active = False
    elif(s == b'line'):
        color_det_active = False
        line_det_active = True
        april_det_active = False
    elif (s == b'calib'):
        color_det_active = False
        line_det_active = False
        april_det_active = True
    elif (s == b'stop'):
        color_det_active = False
        line_det_active = False
        april_det_active = False
        uart.write(("/stop/run\n").encode())
    else:
        color_det_active = color_det_active
        line_det_active = line_det_active
        april_det_active = april_det_active

def color_detection():
    global color_trigger
    green_blobs = img.find_blobs([green], roi = (70, 50, 20, 20))
    if(green_blobs):
        print("find green")
        color_trigger = color_trigger + 1
    else:
        color_trigger = 0

def line_detection():
    global trigger
    trigger = trigger + 1
    for l in img.find_line_segments(merge_distance = 200, max_theta_diff = 200, roi = (20, 0 , 120, 20)):
        img.draw_line(l.line(), color = (255, 255, 0))
        print_args = (l.x1(), l.y1(), l.x2(), l.y2())
        uart.write(("/line_det/run %f %f %f %f\n" % print_args).encode())

def tag():
    for tag in img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y): # defaults to TAG36H11
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        # The conversion is nearly 6.2cm to 1 -> translation
        print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
        # Translation units are unknown. Rotation units are in degrees.
        uart.write(("/calib/run %f %f %f %f %f %f\n" % print_args).encode())
        time.sleep(8)

while(True):
    send = uart.readline()
    uart_detect(send)
    #if(send):
    #    print(send)
    clock.tick()
    img = sensor.snapshot()

    if enable_lens_corr:
        img.lens_corr(1.8) # for 2.8mm lens...

    if(color_det_active):
        time.sleep_ms(1000)
        color_detection()
        if(color_trigger > 3):
            uart_detect(b'stop')
            color_trigger = 0
    elif (line_det_active):
        time.sleep_ms(100)
        line_detection()
        if(trigger > 40):
            uart_detect(b'stop')
            trigger = 0
    elif (april_det_active):
        tag()


from queue import PriorityQueue

q = PriorityQueue()

n = 0
string = ""

for x in range (0, 3):
    n = input()
    string = input()
    q.put((-n, string))

while not q.empty():
    prox_item = q.get()
    print(prox_item)


import requests
import json
import gw_config

URL = gw_config.URL + 'setonoff.php'

def run():
    data = {'cmd': 'set', 'value': '1'}
    ret = requests.post(URL, json=data)
    ret = ret.json()
    print(ret['errno'], ret['msg'])

if __name__ == '__main__':
    run()


from odoo import fields, models, api, _

class AccountPayment(models.Model):
    _inherit = "account.payment"

    @api.onchange('amount', 'currency_id')
    def _onchange_amount(self):
        jrnl_filters = self._compute_journal_domain_and_types()
        journal_types = jrnl_filters['journal_types']
        domain_on_types = [('type', 'in', list(journal_types))]
        if self.invoice_ids:
            domain_on_types.append(('company_id', '=', self.invoice_ids[0].company_id.id))
            domain_on_types.append(('fal_business_type', '=', self.invoice_ids[0].fal_business_type.id))
            if self.invoice_ids[0].type in ['out_invoice', 'in_refund']:
                domain_on_types.append(('x_studio_type_bon', '=', 'm'))
            elif self.invoice_ids[0].type in ['in_invoice', 'out_refund']:
                domain_on_types.append(('x_studio_type_bon', '=', 'h'))
        if self.journal_id.type not in journal_types or (self.invoice_ids and self.journal_id.company_id != self.invoice_ids[0].company_id):
            self.journal_id = self.env['account.journal'].search(domain_on_types, limit=1)
        return {'domain': {'journal_id': jrnl_filters['domain'] + domain_on_types}}

    fal_business_type = fields.Many2one('fal.business.type', related='journal_id.fal_business_type', string='Business Type', readonly=True)
    journal_id = fields.Many2one('account.journal', string='Journal', required=True, readonly=True, states={'draft': [('readonly', False)]}, tracking=True, domain="[('type', 'in', ('bank', 'cash')), ('company_id', '=', company_id), ('fal_business_type', '=', fal_business_type)]")
    writeoff_account_id = fields.Many2one('account.account', string="Difference Account", domain="[('deprecated', '=', False), ('company_id', '=', company_id), ('fal_business_type', '=', fal_business_type)]", copy=False)

    def _prepare_payment_moves(self):
        ''' Prepare the creation of journal entries (account.move) by creating a list of python dictionary to be passed
        to the 'create' method.

        Example 1: outbound with write-off:

        Account             | Debit     | Credit
        ---------------------------------------------------------
        BANK                |   900.0   |
        RECEIVABLE          |           |   1000.0
        WRITE-OFF ACCOUNT   |   100.0   |

        Example 2: internal transfer from BANK to CASH:

        Account             | Debit     | Credit
        ---------------------------------------------------------
        BANK                |           |   1000.0
        TRANSFER            |   1000.0  |
        CASH                |   1000.0  |
        TRANSFER            |           |   1000.0

        :return: A list of Python dictionary to be passed to env['account.move'].create.
        '''
        all_move_vals = []
        for payment in self:
            company_currency = payment.company_id.currency_id
            move_names = payment.move_name.split(payment._get_move_name_transfer_separator()) if payment.move_name else None

            # Compute amounts.
            write_off_amount = payment.payment_difference_handling == 'reconcile' and -payment.payment_difference or 0.0
            if payment.payment_type in ('outbound', 'transfer'):
                counterpart_amount = payment.amount
                liquidity_line_account = payment.journal_id.default_debit_account_id
            else:
                counterpart_amount = -payment.amount
                liquidity_line_account = payment.journal_id.default_credit_account_id

            # Manage currency.
            if payment.currency_id == company_currency:
                # Single-currency.
                balance = counterpart_amount
                write_off_balance = write_off_amount
                counterpart_amount = write_off_amount = 0.0
                currency_id = False
            else:
                # Multi-currencies.
                balance = payment.currency_id._convert(counterpart_amount, company_currency, payment.company_id, payment.payment_date)
                write_off_balance = payment.currency_id._convert(write_off_amount, company_currency, payment.company_id, payment.payment_date)
                currency_id = payment.currency_id.id

            # Manage custom currency on journal for liquidity line.
            if payment.journal_id.currency_id and payment.currency_id != payment.journal_id.currency_id:
                # Custom currency on journal.
                if payment.journal_id.currency_id == company_currency:
                    # Single-currency
                    liquidity_line_currency_id = False
                else:
                    liquidity_line_currency_id = payment.journal_id.currency_id.id
                liquidity_amount = company_currency._convert(
                    balance, payment.journal_id.currency_id, payment.company_id, payment.payment_date)
            else:
                # Use the payment currency.
                liquidity_line_currency_id = currency_id
                liquidity_amount = counterpart_amount

            # Compute 'name' to be used in receivable/payable line.
            rec_pay_line_name = ''
            if payment.payment_type == 'transfer':
                rec_pay_line_name = payment.name
            else:
                if payment.partner_type == 'customer':
                    if payment.payment_type == 'inbound':
                        rec_pay_line_name += _("Customer Payment")
                    elif payment.payment_type == 'outbound':
                        rec_pay_line_name += _("Customer Credit Note")
                elif payment.partner_type == 'supplier':
                    if payment.payment_type == 'inbound':
                        rec_pay_line_name += _("Vendor Credit Note")
                    elif payment.payment_type == 'outbound':
                        rec_pay_line_name += _("Vendor Payment")
                if payment.invoice_ids:
                    rec_pay_line_name += ': %s' % ', '.join(payment.invoice_ids.mapped('name'))

            # Compute 'name' to be used in liquidity line.
            if payment.payment_type == 'transfer':
                liquidity_line_name = _('Transfer to %s') % payment.destination_journal_id.name
            else:
                liquidity_line_name = payment.name

            # ==== 'inbound' / 'outbound' ====
            per_line_dmsrefnum = '/'
            if payment.partner_type == 'customer':
                per_line_dmsrefnum = payment.invoice_ids[0] and payment.invoice_ids[0].x_studio_dmsrefnumber or payment.invoice_ids[0].invoice_origin
            elif payment.partner_type == 'supplier':
                per_line_dmsrefnum = payment.invoice_ids[0] and payment.invoice_ids[0].x_studio_dmsrefnumber or payment.invoice_ids[0].name
            move_vals = {
                'date': payment.payment_date,
                'ref': payment.communication,
                'journal_id': payment.journal_id.id,
                'currency_id': payment.journal_id.currency_id.id or payment.company_id.currency_id.id,
                'partner_id': payment.partner_id.id,
                'line_ids': [
                    # Receivable / Payable / Transfer line.
                    (0, 0, {
                        'name': rec_pay_line_name,
                        'amount_currency': counterpart_amount + write_off_amount if currency_id else 0.0,
                        'currency_id': currency_id,
                        'debit': balance + write_off_balance > 0.0 and balance + write_off_balance or 0.0,
                        'credit': balance + write_off_balance < 0.0 and -balance - write_off_balance or 0.0,
                        'date_maturity': payment.payment_date,
                        'partner_id': payment.partner_id.commercial_partner_id.id,
                        'account_id': payment.destination_account_id.id,
                        'payment_id': payment.id,
                        'x_studio_per_line_dmsrefnum': per_line_dmsrefnum or "/"
                    }),
                    # Liquidity line.
                    (0, 0, {
                        'name': liquidity_line_name,
                        'amount_currency': -liquidity_amount if liquidity_line_currency_id else 0.0,
                        'currency_id': liquidity_line_currency_id,
                        'debit': balance < 0.0 and -balance or 0.0,
                        'credit': balance > 0.0 and balance or 0.0,
                        'date_maturity': payment.payment_date,
                        'partner_id': payment.partner_id.commercial_partner_id.id,
                        'account_id': liquidity_line_account.id,
                        'payment_id': payment.id,
                    }),
                ],
            }
            if write_off_balance:
                # Write-off line.
                move_vals['line_ids'].append((0, 0, {
                    'name': payment.writeoff_label,
                    'amount_currency': -write_off_amount,
                    'currency_id': currency_id,
                    'debit': write_off_balance < 0.0 and -write_off_balance or 0.0,
                    'credit': write_off_balance > 0.0 and write_off_balance or 0.0,
                    'date_maturity': payment.payment_date,
                    'partner_id': payment.partner_id.commercial_partner_id.id,
                    'account_id': payment.writeoff_account_id.id,
                    'payment_id': payment.id,
                }))

            if move_names:
                move_vals['name'] = move_names[0]

            all_move_vals.append(move_vals)

            # ==== 'transfer' ====
            if payment.payment_type == 'transfer':
                journal = payment.destination_journal_id

                # Manage custom currency on journal for liquidity line.
                if journal.currency_id and payment.currency_id != journal.currency_id:
                    # Custom currency on journal.
                    liquidity_line_currency_id = journal.currency_id.id
                    transfer_amount = company_currency._convert(balance, journal.currency_id, payment.company_id, payment.payment_date)
                else:
                    # Use the payment currency.
                    liquidity_line_currency_id = currency_id
                    transfer_amount = counterpart_amount

                transfer_move_vals = {
                    'date': payment.payment_date,
                    'ref': payment.communication,
                    'partner_id': payment.partner_id.id,
                    'journal_id': payment.destination_journal_id.id,
                    'line_ids': [
                        # Transfer debit line.
                        (0, 0, {
                            'name': payment.name,
                            'amount_currency': -counterpart_amount if currency_id else 0.0,
                            'currency_id': currency_id,
                            'debit': balance < 0.0 and -balance or 0.0,
                            'credit': balance > 0.0 and balance or 0.0,
                            'date_maturity': payment.payment_date,
                            'partner_id': payment.partner_id.commercial_partner_id.id,
                            'account_id': payment.company_id.transfer_account_id.id,
                            'payment_id': payment.id,
                        }),
                        # Liquidity credit line.
                        (0, 0, {
                            'name': _('Transfer from %s') % payment.journal_id.name,
                            'amount_currency': transfer_amount if liquidity_line_currency_id else 0.0,
                            'currency_id': liquidity_line_currency_id,
                            'debit': balance > 0.0 and balance or 0.0,
                            'credit': balance < 0.0 and -balance or 0.0,
                            'date_maturity': payment.payment_date,
                            'partner_id': payment.partner_id.commercial_partner_id.id,
                            'account_id': payment.destination_journal_id.default_credit_account_id.id,
                            'payment_id': payment.id,
                        }),
                    ],
                }

                if move_names and len(move_names) == 2:
                    transfer_move_vals['name'] = move_names[1]

                all_move_vals.append(transfer_move_vals)
        return all_move_vals


from .unit_base import UnitBase
import os

from .raw_unit_em import RawUnitEM1

def get_emotion_by_file_name_1(file_name):
    assert file_name.endswith('.mid')
    file_name = file_name[:-4]
    r = file_name.split('_')[1]
    return r

em1_funcs = {
    'file_name_1': get_emotion_by_file_name_1,
}

class UnitEM1(UnitBase):
    """
    所有的ts种类
    """
    @classmethod
    def get_raw_unit_class(cls):
        return RawUnitEM1

    @classmethod
    def convert_raw_to_value(
        cls, raw_data, encoder, midi_dir, midi_path,
        pos_info, bars_positions, bars_chords, bars_insts, bar_begin, bar_end, **kwargs
    ):
        emo_label = raw_data['EM1']
        emo_label = emo_label['emotion']
        return emo_label

    def get_vector(self, use=True, use_info=None):
        value = self.value
        vector = [0] * 5
        if value is None or not use:
            vector[-1] = 1
            return vector
        emo_id = int(value[1]) - 1
        assert 0 <= emo_id < 4
        vector[emo_id] = 1
        return vector

    @property
    def vector_dim(self) -> int:
        return 5


import numpy as np
import cv2
def normalize (x):
    x = np.array(x)
    sum_x = sum(x)
    if(sum_x==0):
        sum_x = 0.001
    return x/sum_x/40
weights = [
        np.random.normal(size=(4,3)),
        np.zeros((3,2))
        #np.random.normal(size=(3,2)),
    ]

SIZE = 100
prev_out = np.zeros((SIZE,SIZE,3))
prev_out_factor = 1
input_factor = 1
distance_factor = 1
saved_idx = 0
error = np.zeros((SIZE,SIZE,2))
result = cv2.resize(cv2.imread("../download.jpeg"),(SIZE,SIZE))/255
#cv2.imshow("mona",result)
OMODE = True
PMODE = True
amount = 0.1

while(True):
    out = np.zeros((SIZE,SIZE,3))
    for i in range(SIZE):
        for j in range(SIZE):
            recur = normalize(np.matmul(prev_out[i][j],weights[1]))
            inputx = [result[i][j][0],result[i][j][1],recur[0],recur[1]]
            #inputx = [result[i][j][0],result[i][j][1],recur[0],recur[1]]
            out[i][j] += normalize(np.matmul(inputx,weights[0]))
            prev_out[i][j] = out[i][j]
    out = cv2.resize(out,(512,512),interpolation=cv2.INTER_AREA)

    #out = cv2.resize(out,(512,512))
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(out,"z: "+str(weights[1][0][0]),(40,40) ,cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255))

    cv2.imshow('test',out)
    k = cv2.waitKey(1)
    if(k==ord('l')):
        weights[0] = np.random.normal(size=(4,3))
    if(k==ord('q')):
        cv2.destroyAllwindows()
        break
    elif(k==ord("y")):
        with open("weird.txt","r") as file:
            weights[1] = np.array(eval(file.read()))
        with open("drugs.txt","r") as file:
            weights[0] = np.array(eval(file.read()))
    elif(k==ord("t")):
        #with open("weird.txt","w") as file:
        #    file.write(str(weights[1]))
        with open("weird2.txt","w") as file:
            file.write(str(weights[0]))
    elif(k==ord('p')):
        if(PMODE):
            PMODE = False
            amount = -0.1
        else:
            PMODE = True
            amount = 0.1
    elif(k==ord('o')):
        if(OMODE):
            OMODE = False
        else:
            OMODE = True

    elif(k==ord('1')):
        if(OMODE==True):
            weights[0][0][0] += amount
        else:
            weights[0][2][0] += amount
    elif(k==ord('2')):
        if(OMODE==True):
            weights[0][0][1] += amount
        else:
            weights[0][2][1] += amount

    elif(k==ord('3')):
        if(OMODE==True):
            weights[0][0][2] += amount
        else:
            weights[0][2][2] += amount
    elif(k==ord('4')):
        if(OMODE==True):
            weights[0][1][0] += amount
        else:
            weights[0][3][0] += amount
    elif(k==ord('5')):
        if(OMODE==True):
            weights[0][1][1] += amount
        else:
            weights[0][3][1] += amount
            print(weights[0][3][1])
    elif(k==ord('6')):
        if(OMODE==True):
            weights[0][1][2] += amount
        else:
            weights[0][3][2] += amount
    elif(k==ord('a')):
        weights[1][0][0] +=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('s')):
        weights[1][0][1] +=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('d')):
        weights[1][1][0] +=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('f')):
        weights[1][1][1] +=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('g')):
        weights[1][2][0] +=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('h')):
        weights[1][2][1]+=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('z')):
        weights[1][0][0] -=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('x')):
        weights[1][0][1] -=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('c')):
        weights[1][1][0]-=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('v')):
        weights[1][1][1] -=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('b')):
        weights[1][2][0] -=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))
    elif(k==ord('n')):
        weights[1][2][1] -=0.1
        cv2.imshow("hi",cv2.resize(weights[1],(512,512)))


from sciwing.models.simpleclassifier import SimpleClassifier
from sciwing.datasets.classification.generic_sect_dataset import GenericSectDataset
from sciwing.modules.lstm2vecencoder import LSTM2VecEncoder
from sciwing.modules.embedders.vanilla_embedder import VanillaEmbedder
import sciwing.constants as constants
import os
import torch.nn as nn
from sciwing.metrics.precision_recall_fmeasure import PrecisionRecallFMeasure
import pathlib

import torch.optim as optim
from sciwing.engine.engine import Engine
import json
import argparse
import torch

FILES = constants.FILES
PATHS = constants.PATHS

OUTPUT_DIR = PATHS["OUTPUT_DIR"]
CONFIGS_DIR = PATHS["CONFIGS_DIR"]

if __name__ == "__main__":
    # read the hyperparams from config file
    parser = argparse.ArgumentParser(
        description="LSTM encoder with linear classifier"
        "with initial random word embeddings"
    )

    parser.add_argument("--exp_name", help="Specify an experiment name", type=str)
    parser.add_argument(
        "--device", help="Adding which device to run the experiment on", type=str
    )
    parser.add_argument(
        "--max_num_words",
        help="Maximum number of words to be considered " "in the vocab",
        type=int,
    )

    parser.add_argument(
        "--debug",
        help="Specify whether this is run on a debug options. The "
        "dataset considered will be small",
        action="store_true",
    )
    parser.add_argument(
        "--debug_dataset_proportion",
        help="The proportion of the dataset " "that will be used if debug is true",
        type=float,
    )
    parser.add_argument("--bs", help="batch size", type=int)
    parser.add_argument("--lr", help="learning rate", type=float)
    parser.add_argument("--epochs", help="number of epochs", type=int)
    parser.add_argument(
        "--save_every", help="Save the model every few epochs", type=int
    )
    parser.add_argument(
        "--log_train_metrics_every",
        help="Log training metrics every few iterations",
        type=int,
    )
    parser.add_argument("--emb_dim", help="embedding dimension", type=int)
    parser.add_argument(
        "--emb_type",
        help="The type of glove embedding you want. The allowed types are glove_6B_50, glove_6B_100, "
        "glove_6B_200, glove_6B_300, random",
    )
    parser.add_argument(
        "--hidden_dim", help="Hidden dimension of the LSTM network", type=int
    )
    parser.add_argument(
        "--max_length", help="Maximum length of the inputs to the encoder", type=int
    )
    parser.add_argument(
        "--bidirectional",
        help="Specify Whether the lstm is bidirectional or uni-directional",
        action="store_true",
    )
    parser.add_argument(
        "--combine_strategy",
        help="How do you want to combine the hidden dimensions of the two "
        "combinations",
    )

    parser.add_argument(
        "--exp_dir_path", help="Directory to store all experiment related information"
    )
    parser.add_argument(
        "--model_save_dir",
        help="Directory where the checkpoints during model training are stored.",
    )
    parser.add_argument(
        "--vocab_store_location", help="File in which the vocab is stored"
    )

    args = parser.parse_args()
    config = {
        "EXP_NAME": args.exp_name,
        "DEVICE": args.device,
        "MAX_NUM_WORDS": args.max_num_words,
        "MAX_LENGTH": args.max_length,
        "DEBUG": args.debug,
        "DEBUG_DATASET_PROPORTION": args.debug_dataset_proportion,
        "BATCH_SIZE": args.bs,
        "EMBEDDING_TYPE": args.emb_type,
        "EMBEDDING_DIMENSION": args.emb_dim,
        "HIDDEN_DIMENSION": args.hidden_dim,
        "LEARNING_RATE": args.lr,
        "NUM_EPOCHS": args.epochs,
        "SAVE_EVERY": args.save_every,
        "LOG_TRAIN_METRICS_EVERY": args.log_train_metrics_every,
        "BIDIRECTIONAL": bool(args.bidirectional),
        "COMBINE_STRATEGY": args.combine_strategy,
        "EXP_DIR_PATH": args.exp_dir_path,
        "MODEL_SAVE_DIR": args.model_save_dir,
        "VOCAB_STORE_LOCATION": args.vocab_store_location,
    }

    DEVICE = config["DEVICE"]
    EXP_NAME = config["EXP_NAME"]
    EXP_DIR_PATH = config["EXP_DIR_PATH"]
    EXP_DIR_PATH = pathlib.Path(EXP_DIR_PATH)
    VOCAB_STORE_LOCATION = config["VOCAB_STORE_LOCATION"]
    MAX_NUM_WORDS = config["MAX_NUM_WORDS"]
    MAX_LENGTH = config["MAX_LENGTH"]
    EMBEDDING_DIMENSION = config["EMBEDDING_DIMENSION"]
    EMBEDDING_TYPE = config["EMBEDDING_TYPE"]
    DEBUG = config["DEBUG"]
    DEBUG_DATASET_PROPORTION = config["DEBUG_DATASET_PROPORTION"]
    BATCH_SIZE = config["BATCH_SIZE"]
    HIDDEN_DIMENSION = config["HIDDEN_DIMENSION"]
    LEARNING_RATE = config["LEARNING_RATE"]
    NUM_EPOCHS = config["NUM_EPOCHS"]
    SAVE_EVERY = config["SAVE_EVERY"]
    LOG_TRAIN_METRICS_EVERY = config["LOG_TRAIN_METRICS_EVERY"]
    BIDIRECTIONAL = config["BIDIRECTIONAL"]
    COMBINE_STRATEGY = config["COMBINE_STRATEGY"]
    MODEL_SAVE_DIR = config["MODEL_SAVE_DIR"]

    GENERIC_SECTION_TRAIN_FILE = "genericSEct.train.data"
    train_dataset = GenericSectDataset(
        filename=GENERIC_SECTION_TRAIN_FILE,
        dataset_type="train",
        max_num_words=MAX_NUM_WORDS,
        max_instance_length=MAX_LENGTH,
        word_vocab_store_location=VOCAB_STORE_LOCATION,
        debug=DEBUG,
        debug_dataset_proportion=DEBUG_DATASET_PROPORTION,
        word_embedding_type=EMBEDDING_TYPE,
        word_embedding_dimension=EMBEDDING_DIMENSION,
    )

    validation_dataset = GenericSectDataset(
        filename=GENERIC_SECTION_TRAIN_FILE,
        dataset_type="valid",
        max_num_words=MAX_NUM_WORDS,
        max_instance_length=MAX_LENGTH,
        word_vocab_store_location=VOCAB_STORE_LOCATION,
        debug=DEBUG,
        debug_dataset_proportion=DEBUG_DATASET_PROPORTION,
        word_embedding_type=EMBEDDING_TYPE,
        word_embedding_dimension=EMBEDDING_DIMENSION,
    )

    test_dataset = GenericSectDataset(
        filename=GENERIC_SECTION_TRAIN_FILE,
        dataset_type="test",
        max_num_words=MAX_NUM_WORDS,
        max_instance_length=MAX_LENGTH,
        word_vocab_store_location=VOCAB_STORE_LOCATION,
        debug=DEBUG,
        debug_dataset_proportion=DEBUG_DATASET_PROPORTION,
        word_embedding_type=EMBEDDING_TYPE,
        word_embedding_dimension=EMBEDDING_DIMENSION,
    )

    test_dataset_params = {
        "filename": GENERIC_SECTION_TRAIN_FILE,
        "dataset_type": "test",
        "max_num_words": MAX_NUM_WORDS,
        "max_instance_length": MAX_LENGTH,
        "word_vocab_store_location": VOCAB_STORE_LOCATION,
        "debug": DEBUG,
        "debug_dataset_proportion": DEBUG_DATASET_PROPORTION,
        "word_embedding_type": EMBEDDING_TYPE,
        "word_embedding_dimension": EMBEDDING_DIMENSION,
    }

    # saving the test dataset params
    # lets save the test dataset params for the experiment
    if not EXP_DIR_PATH.is_dir():
        EXP_DIR_PATH.mkdir()

    with open(os.path.join(EXP_DIR_PATH, "config.json"), "w") as fp:
        json.dump(config, fp)

    VOCAB_SIZE = train_dataset.word_vocab.get_vocab_len()
    NUM_CLASSES = train_dataset.get_num_classes()

    config["VOCAB_SIZE"] = VOCAB_SIZE
    config["NUM_CLASSES"] = NUM_CLASSES

    with open(os.path.join(EXP_DIR_PATH, "config.json"), "w") as fp:
        json.dump(config, fp)

    with open(os.path.join(EXP_DIR_PATH, "test_dataset_params.json"), "w") as fp:
        json.dump(test_dataset_params, fp)

    random_embeddings = train_dataset.word_vocab.load_embedding()
    random_embeddings = nn.Embedding.from_pretrained(random_embeddings, freeze=False)

    embedder = VanillaEmbedder(
        embedding_dim=EMBEDDING_DIMENSION, embedding=random_embeddings
    )
    encoder = LSTM2VecEncoder(
        emb_dim=EMBEDDING_DIMENSION,
        embedder=embedder,
        dropout_value=0.0,
        hidden_dim=HIDDEN_DIMENSION,
        combine_strategy=COMBINE_STRATEGY,
        bidirectional=BIDIRECTIONAL,
        device=torch.device(DEVICE),
    )

    classiier_encoding_dim = 2 * HIDDEN_DIMENSION if BIDIRECTIONAL else HIDDEN_DIMENSION
    model = SimpleClassifier(
        encoder=encoder,
        encoding_dim=classiier_encoding_dim,
        num_classes=NUM_CLASSES,
        classification_layer_bias=True,
    )

    optimizer = optim.Adam(params=model.parameters(), lr=LEARNING_RATE)
    metric = PrecisionRecallFMeasure(idx2labelname_mapping=train_dataset.idx2classname)

    engine = Engine(
        model=model,
        train_dataset=train_dataset,
        validation_dataset=validation_dataset,
        test_dataset=test_dataset,
        optimizer=optimizer,
        batch_size=BATCH_SIZE,
        save_dir=MODEL_SAVE_DIR,
        num_epochs=NUM_EPOCHS,
        save_every=SAVE_EVERY,
        log_train_metrics_every=LOG_TRAIN_METRICS_EVERY,
        device=torch.device(DEVICE),
        metric=metric,
        use_wandb=True,
        experiment_name=EXP_NAME,
        experiment_hyperparams=config,
        track_for_best="macro_fscore",
    )

    engine.run()


from xai.brain.wordbase.nouns._gulp import _GULP

#calss header
class _GULPED(_GULP, ):
	def __init__(self,):
		_GULP.__init__(self)
		self.name = "GULPED"
		self.specie = 'nouns'
		self.basic = "gulp"
		self.jsondata = {}


from decord import VideoReader
import numpy as np
from decord import VideoReader
from decord import cpu, gpu
from utils.datasets import  letterbox
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size
import multiprocessing
import cv2
# from yolov5_original.detect import YOLOV5
from multiprocessing import Pool
import time

class Preprocess_Video(object):
    def __init__(self,url,num_threads=1,batch=64):
        self.num_threads= multiprocessing.cpu_count()
        print("cpu count ",self.num_threads)
        self.vr= VideoReader(url, ctx=cpu(0))
        self.img_size=640
        # self.detection = YOLOV5()
        self.n=len(self.vr)
        self.batch = batch

    def letterbox(self,begin, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
        end=min(begin + self.batch,self.n)
        img=self.vr[begin].asnumpy()
        print("A")
        shape = img.shape[:2]  # current shape [height, width]
        if isinstance(new_shape, int):
            new_shape = (new_shape, new_shape)

        r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
        if not scaleup:
            r = min(r, 1.0)
        ratio = r, r
        new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
        dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
        if auto:
            dw, dh = np.mod(dw, 32), np.mod(dh, 32)
        elif scaleFill:
            dw, dh = 0.0, 0.0
            new_unpad = (new_shape[1], new_shape[0])
            ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]
        dw /= 2
        dh /= 2
        res_imgs=[]
        top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
        left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
        for i in range(begin,end):
            print(i)
            if shape[::-1] != new_unpad:  # resize
                img=self.vr[i].asnumpy()
                img=cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
                res_imgs.append(cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)[:, :, ::-1].transpose(2, 0, 1))  # add border
        return res_imgs, ratio, (dw, dh)

    def process_frame(self):
        arg_lists = list(range(0,self.n,self.batch))
        print(arg_lists)
        pool = Pool(processes=self.num_threads)
        result = pool.map(call_method, arg_lists)
        print(result)
        #res=detection.detect(imgs)

def call_method(i):
    # Implicitly use global copy of my_instance, not one passed as an argument
    return my_instance.letterbox(i)

my_instance = Preprocess_Video("town.avi")

if __name__ == '__main__':
    t1=time.time()
    my_instance.process_frame()
    print("time : ",time.time()-t1)


import os
import pathlib
import numpy as np
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile

from collections import namedtuple
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display

import cv2
import time
import queue
import threading

from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

import triangulate
import decision_making
import socket_client

# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1
# Patch the location of gfile
tf.gfile = tf.io.gfile

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  try:
    # Currently, memory growth needs to be the same across GPUs
    for gpu in gpus:
      tf.config.experimental.set_memory_growth(gpu, True)
    logical_gpus = tf.config.experimental.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Memory growth must be set before GPUs have been initialized
    print(e)

def load_model(model_name):
  base_url = 'http://download.tensorflow.org/models/object_detection/'
  model_file = model_name + '.tar.gz'
  model_dir = tf.keras.utils.get_file(
    fname=model_name,
    origin=base_url + model_file,
    untar=True)

  model_dir = pathlib.Path(model_dir)/"saved_model"

  # model_dir = os.path.join('data', 'bottle_chair')
  model = tf.saved_model.load(str(model_dir))
  model = model.signatures['serving_default']

  return model

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
# PATH_TO_LABELS = os.path.join('data', 'bottle_chair_label_map.pbtxt')
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

def run_inference_for_single_image(model, image, filter_person=True):
  image = np.asarray(image)
  # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
  input_tensor = tf.convert_to_tensor(image)
  # The model expects a batch of images, so add an axis with `tf.newaxis`.
  input_tensor = input_tensor[tf.newaxis,...]

  # Run inference
  output_dict = model(input_tensor)

  # All outputs are batches tensors.
  # Convert to numpy arrays, and take index [0] to remove the batch dimension.
  # We're only interested in the first num_detections.
  num_detections = int(output_dict.pop('num_detections'))
  # output_dict = {key:value[0, :num_detections].numpy()
  #                for key,value in output_dict.items()}
  indices = np.where(output_dict['detection_classes'][0, :num_detections].numpy() == 44)

  for key,value in output_dict.items():
    value_np = value[0, :num_detections].numpy()
    output_dict[key] = value_np[indices]
  output_dict['num_detections'] = num_detections

  # detection_classes should be ints.
  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'], output_dict['detection_boxes'],
               image.shape[0], image.shape[1])
    detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                       tf.uint8)
    output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()

  return output_dict

def show_inference(model, image_np):
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks_reframed', None),
      use_normalized_coordinates=True,
      line_thickness=8)

  return image_np, output_dict

class VideoCapture:

  def __init__(self, name):
    self.cap = cv2.VideoCapture(name)
    # self.cap.set(6, cv2.VideoWriter_fourcc('H', '2', '6', '4')) # for reading raspivid
    self.q = queue.Queue()
    t = threading.Thread(target=self._reader)
    t.daemon = True
    t.start()

  # read frames as soon as they are available, keeping only most recent one
  def _reader(self):
    while True:
      ret, frame = self.cap.read()
      if not ret:
        break
      if not self.q.empty():
        try:
          self.q.get_nowait()   # discard previous (unprocessed) frame
        except Queue.Empty:
          pass
      self.q.put(frame)

  def read(self):
    return self.q.get()

# cv2 display preferences
font = cv2.FONT_HERSHEY_SIMPLEX
fpsPosition = (10, 100)
fontScale = 0.5
fontColor = (255, 255, 255)
lineType = 2

def displayFps(image, start_time):
  fps = 1 / (time.time() - start_time)
  info = f"{fps} fps"

  cv2.putText(image, info,
              fpsPosition,
              font,
              fontScale,
              fontColor,
              lineType)
  return fps

def getMiddleCoordinates(image, output_dict, width, height, min_score_thresh=.5, display=True):
  boxes = output_dict['detection_boxes']
  classes = output_dict['detection_classes']
  scores = output_dict['detection_scores']
  coordinatesList = []

  TextYCoordinate = 150
  for i in range(boxes.shape[0]):
    if scores is None or scores[i] > min_score_thresh:
      ymin = round(boxes[i,0]*height, 2)
      xmin = round(boxes[i,1]*width, 2)
      ymax = round(boxes[i,2]*height, 2)
      xmax = round(boxes[i,3]*width, 2)
      xmid = int((xmin + xmax) / 2)
      ymid = int((ymin + ymax) / 2)
      midCoordinate = (xmid, ymid)
      coordinatesList.append(midCoordinate)

      belowTimeText = (10,TextYCoordinate)
      cv2.putText(image, f'{midCoordinate}',
                  belowTimeText,
                  font,
                  fontScale,
                  fontColor,
                  lineType)
      cv2.putText(image, "X",
                  midCoordinate,
                  font,
                  fontScale,
                  (0,0,255),
                  lineType)
      TextYCoordinate += 50

  return coordinatesList

# distance calculation parameters initialization
width = 640
height = 480
angle_width=62.2
angler = triangulate.Frame_Angles(width,height,angle_width)
angler.build_frame()
camera_distance = 0.15

# calculate distance from phone to detected object
def distance_calculation(left_coordinate, right_coordinate):
  (xlp, ylp) = left_coordinate
  (xrp, yrp) = right_coordinate
  xlangle,ylangle = angler.angles_from_center(xlp,ylp,top_left=True,degrees=True)
  xrangle,yrangle = angler.angles_from_center(xrp,yrp,top_left=True,degrees=True)
  X,Y,Z,D = angler.location(camera_distance,(xlangle,ylangle),(xrangle,yrangle),center=True,degrees=True)

  return D

# initializing rc car control
transmission_delay=0.02
slow_dist=1
brake_dist=0.6
max_speed=0.83
frames_to_skip=0
waiting_time=1
driver = decision_making.Drive(
    transmission_delay=transmission_delay,
    slow_dist=slow_dist,
    brake_dist=brake_dist,
    max_speed=max_speed,
    frames_to_skip=frames_to_skip,
    waiting_time=waiting_time)

def speed_calculation(fps, distance):
  speed = driver.accelerate(fps, distance)
  return speed

# initializing socket client
host = '192.168.31.207'
port = 9999
socket = socket_client.Socket(host,port)

def getAvgTimeDelay(start_time, avg_delay=0, acc=0):
  # elapsed time is in ms
  elapsed_time = (time.time() - start_time) * 1000
  new_acc = acc + 1
  new_avg_delay = (( avg_delay*(acc) ) + elapsed_time) / new_acc

  return new_avg_delay, new_acc

# ip_left = 0  # Use this only if you have one webcam for testing
ip_left = "http://192.168.31.167/?action=stream"
ip_right = "http://192.168.31.207/?action=stream"
# ip_left = "http://192.168.43.1:8080/video"
# ip_right = "http://192.168.43.41:8080/video"
cap_left = VideoCapture(ip_left)
cap_right = VideoCapture(ip_right)
# cap_right = cap_left

# Models can be found here: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
model_name = 'ssdlite_mobilenet_v2_coco_2018_05_09'
detection_model = load_model(model_name)
avg_delay = 0
acc = 0

while True:
    start_time = time.time()

    # Read frame from camera
    image_np_left = cap_left.read()
    image_np_right = cap_right.read()

    image_np_left, output_dict_left = show_inference(detection_model, np.array(image_np_left))
    image_np_left = cv2.resize(image_np_left, (width, height))
    coords_left = getMiddleCoordinates(image_np_left, output_dict_left, width, height)

    image_np_right, output_dict_right = show_inference(detection_model, np.array(image_np_right))
    image_np_right = cv2.resize(image_np_right, (width, height))
    fps = displayFps(image_np_right, start_time)
    coords_right = getMiddleCoordinates(image_np_right, output_dict_right, width, height)

    distance = None
    if coords_left and coords_right:
      distance = distance_calculation(coords_left[0], coords_right[0])
      print(f"distance: {distance}")

    speed = speed_calculation(fps, distance)
    print(f"speed: {speed}")

    # send speed to socket server
    socket.send(speed)

    # Display output
    results = np.concatenate((image_np_left, image_np_right), axis=1)
    cv2.imshow('object detection', results)

    # avg_delay, acc = getAvgTimeDelay(start_time, avg_delay, acc)
    # print(avg_delay)

    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        socket.close()
        break


from __future__ import print_function
import unittest
from pytraj.base import *
from pytraj import adict
from pytraj import io as mdio
from pytraj.utils import eq, aa_eq
from pytraj.decorators import no_test, test_if_having, test_if_path_exists
from pytraj.testing import cpptraj_test_dir
import pytraj.common_actions as pyca
from pytraj import api
from pytraj.compat import zip

class Test(unittest.TestCase):
    def test_0(self):
        traj = mdio.iterload("./data/tz2.ortho.nc", "./data/tz2.ortho.parm7")
        api_traj = api.Trajectory(traj)

        # test xyz
        aa_eq(api_traj.xyz, traj.xyz)

        # test object lifetime
        aa_eq(api_traj[0].xyz, api_traj.xyz[0])

        # test Box
        assert (api_traj.has_box() == True)
        boxes = traj.box_to_ndarray()
        for i, frame in enumerate(api_traj):
            assert (frame.has_box() == True)
            f_blist = frame.box.tolist()
            aa_eq(f_blist, boxes[i].tolist())

        # test join
        api_traj += traj
        assert api_traj.n_frames == traj.n_frames * 2
        aa_eq(api_traj.xyz[traj.n_frames:], api_traj.xyz[:traj.n_frames])

        # test autoimage
        # make Trajectory from TrajectoryIterator
        fa = traj[:]
        t_api2 = api.Trajectory(fa)
        t_api2.autoimage()
        fa.autoimage()
        saved_traj = mdio.iterload("./data/tz2.autoimage.nc", "./data/tz2.ortho.parm7")
        for f1, f2 in zip(fa, t_api2):
            aa_eq(f1.box.tolist(), f2.box.tolist())
            print (f1.rmsd(f2))
        aa_eq(t_api2.xyz, fa.xyz, decimal=1)

        # make sure to reproduce cpptraj's output too
        aa_eq(saved_traj.xyz, fa.xyz)

        # test slicing to get correct Box
        t_api2_sliced = t_api2[:10]
        aa_eq(t_api2_sliced._boxes, t_api2._boxes[:10])

if __name__ == "__main__":
    unittest.main()


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

"""
Pygame Learning Environment Integration: https://github.com/ntasfi/PyGame-Learning-Environment
"""

#to do:
#finish rest of args write up
#test pong: for lives/score issue (ie if 1 live, reset after every score conceded
#work my way though the functions on this list
    #verify that I fill out all the necessary environments that need to be filled out

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

from ple import PLE
from ple.games.catcher import Catcher
from ple.games.pixelcopter import Pixelcopter
from ple.games.pong import Pong
from ple.games.puckworld import PuckWorld
from ple.games.raycastmaze import RaycastMaze
from ple.games.snake import Snake
from ple.games.waterworld import WaterWorld
from ple.games.monsterkong import MonsterKong
from ple.games.flappybird import FlappyBird

import pygame
import numpy as np
from tensorforce import TensorForceError
from tensorforce.environments import Environment

class PygameLearningEnvironment(Environment):

    def __init__(self, game_name, rewards, state_as_image = True, fps = 30, force_fps=True, frame_skip=2,
                 hold_action=2, visualize=False, width=84, height=84, lives=1):
        """
        Initialize Pygame Learning Environment
        https://github.com/ntasfi/PyGame-Learning-Environment

        Args:
            env_name: PLE environment

            fps: frames per second
            force_fps: False for slower speeds
            frame_skip: number of env frames to skip
            hold_action: number of env frames to hold each action for
            isRGB: get color or greyscale version of statespace #isRGB = False,
            game_height,game_width: height and width of environment
            visualize: If set True, the program will visualize the trainings, will slow down training
            lives: number of lives in game. Game resets on game over (ie lives = 0). only in Catcher and Pong (score)

        """

        self.env_name = game_name
        self.rewards = rewards
        self.lives = lives
        self.state_as_image = state_as_image
        self.fps = fps #30  # frames per second
        self.force_fps = force_fps #True  # False for slower speeds
        self.frame_skip = frame_skip  # frames to skip
        self.ple_num_steps = hold_action  # frames to continue action for
        #self.isRGB = isRGB #always returns color, lets tensorforce due the processing
        self.visualize = visualize
        self.width = width
        self.height = height
        #testing
        self.reached_terminal = 0
        self.episode_time_steps = 0
        self.episode_reward = 0
        self.total_time_steps = 0

        if self.env_name == 'catcher':
            self.game = Catcher(width=self.width, height=self.height,init_lives=self.lives)
        elif self.env_name == 'pixelcopter':
            self.game = Pixelcopter(width=self.width, height=self.height)
        elif self.env_name == 'pong':
            self.game = Pong(width=self.width, height=self.height,MAX_SCORE=self.lives)
        elif self.env_name == 'puckworld':
            self.game = PuckWorld(width=self.width, height=self.height)
        elif self.env_name == 'raycastmaze':
            self.game = RaycastMaze(width=self.width, height=self.height)
        elif self.env_name == 'snake':
            self.game = Snake(width=self.width, height=self.height)
        elif self.env_name == 'waterworld':
            self.game = WaterWorld(width=self.width, height=self.height)
        elif self.env_name == 'monsterkong':
            self.game = MonsterKong()
        elif self.env_name == 'flappybird':
            self.game = FlappyBird(width=144, height=256)  # limitations on height and width for flappy bird
        else:
            raise TensorForceError('Unknown Game Environement.')

        if self.state_as_image:
           process_state = None
        else:
            #create a preprocessor to read the state dictionary as a numpy array
            def process_state(state):
                # ret_value = np.fromiter(state.values(),dtype=float,count=len(state))
                ret_value = np.array(list(state.values()), dtype=np.float32)
                return ret_value

        # make a PLE instance
        self.env = PLE(self.game,reward_values=self.rewards,fps=self.fps, frame_skip=self.frame_skip,
                       num_steps=self.ple_num_steps,force_fps=self.force_fps,display_screen=self.visualize,
                       state_preprocessor = process_state)
        #self.env.init()
        #self.env.act(self.env.NOOP) #game starts on black screen
        #self.env.reset_game()
        #self.env.act(self.env.NOOP)
        #self.env.act(self.env.NOOP)
        #self.env.act(self.env.NOOP)
        #self.env.act(self.env.NOOP)
        #self.env.reset_game()

        # setup gamescreen object
        if state_as_image:
            w, h = self.env.getScreenDims()
            self.gamescreen = np.empty((h, w, 3), dtype=np.uint8)
        else:
            self.gamescreen = np.empty(self.env.getGameStateDims(), dtype=np.float32)
        # if isRGB:
        #     self.gamescreen = np.empty((h, w, 3), dtype=np.uint8)
        # else:
        #     self.gamescreen = np.empty((h, w), dtype=np.uint8)

        # setup action converter
        # PLE returns legal action indexes, convert these to just numbers
        self.action_list = self.env.getActionSet()
        self.action_list = sorted(self.action_list, key=lambda x: (x is None, x))

    def __str__(self):
        return 'PygameLearningEnvironment({})'.format(self.env_name)

    def close(self):
        pygame.quit()
        self.env = None

    def reset(self):
        # if isinstance(self.gym, gym.wrappers.Monitor):
        #     self.gym.stats_recorder.done = True
        #env.act(env.NOOP) # need to take an action or screen is black
        # clear gamescreen
        if self.state_as_image:
            self.gamescreen = np.empty(self.gamescreen.shape, dtype=np.uint8)
        else:
            self.gamescreen = np.empty(self.gamescreen.shape, dtype=np.float32)
        self.env.reset_game()
        return self.current_state

    def execute(self, actions):

        #print("lives check in ple {}".format(self.env.lives()))
        #self.env.saveScreen("test_screen_capture_before_{}.png".format(self.total_time_steps))
        #lives_check = self.env.lives() #testing code

        ple_actions = self.action_list[actions]
        reward = self.env.act(ple_actions)
        state = self.current_state
        # testing code
        # self.env.saveScreen("test_screen_capture_after_{}.png".format(self.total_time_steps))
        # self.episode_time_steps += 1
        # self.episode_reward += reward
        # self.total_time_steps += 1
        # print("reward is {}".format(reward))
        # #if self.env.lives() != lives_check:
        # #    print('lives are different is game over? {}'.format(self.env.game_over()))
        # print('lives {}, game over {}, old lives {}'.format(self.env.lives(),self.env.game_over(),lives_check))

        if self.env.game_over():
            terminal = True
            # testing code
            self.reached_terminal += 1
            # print("GAME OVER reached terminal {}".format(self.reached_terminal))
            # print("episode time steps {}, episode reward {}".format(self.episode_time_steps,self.episode_reward))
            # self.episode_reward = 0
            # self.episode_time_steps = 0
            # print("total timesteps {}".format(self.total_time_steps))
        else:
            terminal = False

        return state, terminal, reward

    @property
    def actions(self):
        return dict(type='int', num_actions=len(self.action_list), names=self.action_list)

    # @property
    # def actions(self):
    #     return OpenAIGym.action_from_space(space=self.gym.action_space)

    #ALE implementation
    # @property
    # def actions(self):
    #     return dict(type='int', num_actions=len(self.action_inds), names=self.action_names)

    @property
    def states(self):
        return dict(shape=self.gamescreen.shape, type=float)

    @property
    def current_state(self):
        #returned state can either be an image or an np array of key components
        if self.state_as_image:
            self.gamescreen = self.env.getScreenRGB()
            # if isRGB:
            #     self.gamescreen = self.env.getScreenRGB()
            # else:
            #     self.gamescreen = self.env.getScreenGrayscale()
        else:
            self.gamescreen = self.env.getGameState()

        return np.copy(self.gamescreen)

    #ALE implementation
    # @property
    # def states(self):
    #     return dict(shape=self.gamescreen.shape, type=float)

    # @property
    # def current_state(self):
    #     self.gamescreen = self.ale.getScreenRGB(self.gamescreen)
    #     return np.copy(self.gamescreen)

    # @property
    # def is_terminal(self):
    #     if self.loss_of_life_termination and self.life_lost:
    #         return True
    #     else:
    #         return self.ale.game_over()


# SUMMARY:      collect_pt_data.py
# USAGE:        collect the pickle files
# ORG:          Pacific Northwest National Laboratory
# AUTHOR:       Xuehang Song
# E-MAIL:       xuehang.song@pnnl.gov
# ORIG-DATE:    July-2017
# DESCRIPTION:
# DESCRIPTION-END

import glob
import subprocess as sp
import os
import numpy as np
import pickle

nbatch = 500
prefix = "path_pickle_"

simu_dir = "/global/cscratch1/sd/renh686/Xuehang/npt/simu_homo/"
# simu_dir = "/global/cscratch1/sd/chenxy/Xuehang/npt/simu_base/"
# simu_dir = '/global/cscratch1/sd/hd09/Xuehang/npt/simu_base_smooth/'

list_file = "unsaved.list"

pk_dir = simu_dir + "shared_latest/"

unsaved = []
for ibatch in range(1, (nbatch+1)):
    print(ibatch)
    pks = glob.glob(pk_dir+"path_pickle_"+str(ibatch)+"*")
    if len(pks) == 0:
        unsaved.append(str(ibatch))

fname = open(simu_dir+list_file, "w")
fname.write("\n".join(unsaved))
fname.close()


from os import system

import login
import globalstuff
import sheetaccess
import commands

system('clear')

user = login.login()

while True:
	files = globalstuff.professorfiles
	for i in files:
		key = files[i]
		sheet = sheetaccess.opensheet(key,user)
		worksheet = sheetaccess.openworksheet(sheet,'Overview')
		globalstuff.i(user,sheet,worksheet,i)

		try:
			[row,cell] = commands.findcommandsprof(worksheet,i)
		except:
			continue


import pygame
import cores
import entidades

class Bola(entidades.Entidade):
	#Atributos
	def __init__(self, x, y, largura, altura, cor):
		entidades.Entidade.__init__(self, x, y, largura, altura, cor)
		self.velocidadeXMaxima = 5
		self.velocidadeYMaxima = 3
		self.velocidadeX = 0
		self.velocidadeY = -3
		self.xInicial = x
		self.yInicial = y

	def atualizarPosicao(self, largura, altura):
		if(self.x < 0 or self.x > (largura - self.largura)):
			self.velocidadeX *= -1
		if(self.y < 0):
			self.velocidadeY *= -1
		if(self.y > (altura - self.altura)):
			self.velocidadeY = -3
			self.velocidadeX = 0
			self.x = self.xInicial
			self.y = self.yInicial

		self.x += self.velocidadeX
		self.y += self.velocidadeY

	def colidiu(self, x, y, largura):
		if((self.x + self.largura/2) < (x + largura/2)):
			self.velocidadeX = -(self.velocidadeXMaxima * (((x + (largura/2.0)) - (self.x + (self.largura/2.0))) / (largura/2.0)))
			print(self.velocidadeX)

		elif((self.x + self.largura/2) > (x + largura/2)):
			self.velocidadeX = -(self.velocidadeXMaxima * (((x + (largura/2.0)) - (self.x + (self.largura/2.0))) / (largura/2.0)))
			print(self.velocidadeX)

		elif((self.x + self.largura/2) == (x + largura/2)):
			self.velocidadeX = 0
			print("No Centro!")

		self.velocidadeY *= -1
		self.y = y - 10


from coin_api import CoinApi
import asyncio
import sys
import traceback
import time
import emailconnect

async def main(loop):
	""" main class that starts the event loop controlling the API and the display """
	usage()
	# display = CoinDisplay(loop)
	api = CoinApi(loop, get_coin(), get_market(), get_currency())
	log('using api ' + api.get_endpoint())
	calls = 0
	calls2 = 0
	calls3 = 0
	while 1:

		# display.on_load()
		try:

			log('updating value of ' + api.get_coin() + ' at ' + api.get_market())
			percent_change = await api.percent_change()
			log(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%')

			if percent_change >= 3:
				if calls in range(0, 99999, 100):
					logEmail(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%')
				#email = threading.Thread(target=logEmail(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%'))
				#email.start()
				calls = 1
			else:
				calls = 0

			if percent_change >= -5:
				if calls2 in range(0, 99999, 100):
				#email = threading.Thread(target=logEmail(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%'))
				#email.start()
					logEmail(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%')
					calls2 = 1
			else:
				calls2 = 0

			if calls3 in range(0, 99999, 100):

				#email = threading.Thread(target=logEmail(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%'))
				#email.start()

				logEmail(api.get_coin() + 'market change in last 24hrs is ' + str(percent_change) + '%')
				calls3 = 1

			# display.on_data(percent_change)
		except KeyError:
			log('no api data available for coin:market:currency combination.')
			# display.on_error()
		except Exception:
			print(traceback.format_exc())
			# display.on_error()
		await asyncio.sleep(api.throttle())
		calls += 1
		calls2 += 1
		calls3 += 1
def usage():
	print('================================================')
	print('|              <!> CoinEmail 0.2 <!>           |')
	print('---------with code from ethermeter 1.0----------------')
	print('-------------------------------------------------')
	print('usage: python coinEmail.py <coin> <market> <currency>')
	print('example: python coinEmail.py ETH Coinbase USD')
	print('-------------------------------------------------\n')

def get_coin():
	return get_arg(1, 'LTC')

def get_market():
	return get_arg(2, 'Coinbase')

def get_currency():
	return get_arg(3, 'BTC')

def get_arg(index, default):
	params = ['coin', 'market', 'currency']
	if (len(sys.argv) > index):
		log('using ' + sys.argv[index] + ' as ' + params[index - 1])
		return sys.argv[index]
	else:
		log('no ' + params[index - 1] + ' specified, using ' + default)
		return default

def logEmail(line):

	print(time.strftime('%H:%M:%S') + ' > ' + line + ' ..')
	email = emailconnect.SendMail()
	email.send(line)
	asyncio.sleep(240)

def log(line):
	print(time.strftime('%H:%M:%S') + ' > ' + line + ' ..')

try:
	loop = asyncio.get_event_loop()
	loop.run_until_complete(main(loop))
	loop.run_forever()
except KeyboardInterrupt:
	log('blockchains is the future, goodnight')
	pass


#!/usr/bin/env python
"""

Date : November 20, 2017

Author : Heather Landry Drexler

Count reads from a bam file that map to gene features: genes, exons, and introns. The length of each feature
is also calculated as well as RPKM calculations for the whole gene, only  exons, and only introns.

This script is design for paired end Illumina sequencing reads.

use : python featureCount_PE.py     iMerge (input file with
                                    iBAM (input BAM file with only unique alignments) [1]
                                    oBAM (output BAM file containing only non duplicated reads) [2]

"""
import sys, pysam, os, numpy, re

# open files from command line arguments
iMerge = open(sys.argv[1], 'r')
iBAM = pysam.Samfile(sys.argv[2], 'rb')
oTest = open(sys.argv[3], 'w')

# save the gene name in a set if it's not already in it
gene_name = set()

# calculate million mapped reads for RPKM analysis
mmr = float(iBAM.mapped)/float(2)/float(1000000)

# loop through a file with feature coordinates sorted by gene name
# record information about each feature
for line in iMerge:
    chrom = line.split('\t')[0]                     # chromosome
    start = int(line.split('\t')[1])                # start coordinate of feature
    end = int(line.split('\t')[2])                  # end coordinate of feature
    gene = line.split('\t')[3]                      # gene name
    feature = line.split('\t')[4].split('_')[0]     # feature: gene, exon, or intron
    strand = line.split('\t')[5][0]                 # strand of gene with intron
    feature_length = end-start                      # length of feature
    read_length = 80

    # check if gene name is in the set, if not save it
    # start variables for counting the number of reads in features
    if gene not in gene_name:

        # print header if this is the first line
        if len(gene_name)==0:
            oTest.write('\t'.join(['gene','gene_count','exon_count','intron_count','gene_RPKM','exon_RPKM','intron_RPKM','exon_intron_ratio'])+'\n')

        elif gene_length <= 10 or exon_length <= 10 or intron_length <= 10:
            print(str(gene_output)+' thrown out')
            oTest.write('\t'.join([str(gene_output),str(gene_count),str(exon_count),str(intron_count),'NULL','NULL','NULL','NULL'])+'\n')

        else:
            gene_RPKM=float(gene_count)/float(gene_length)*1000 / float(mmr)
            exon_RPKM=float(exon_count)/float(exon_length)*1000 / float(mmr)
            intron_RPKM=float(intron_count)/float(intron_length)*1000 / float(mmr)

            if intron_RPKM==0:
                ratio="Inf"
            else:
                ratio=exon_RPKM/intron_RPKM
            oTest.write('\t'.join([str(gene_output),str(gene_count),str(exon_count),str(intron_count),str(gene_RPKM),str(exon_RPKM),str(intron_RPKM),str(ratio)])+'\n')

        # set variables for the next gene
        found = set()
        gene_name.add(gene)
        gene_count=0
        exon_count=0
        intron_count=0
        gene_length=0
        exon_length=0
        intron_length=0

    # count reads that span the gene
    if feature=='gene' and feature_length>10:
        gene_length+=feature_length
        for read in iBAM.fetch(chrom, int(start)+3, int(end)-3):
            if (strand=='+' and read.is_reverse and read.is_read2
                or (strand=='+' and not read.is_reverse and read.is_read1)
                or (strand=='-' and read.is_reverse and read.is_read1)
                or (strand=='-' and not read.is_reverse and read.is_read2)):
                if (start-read_length)<read.aend<(end+read_length) and (start-read_length)<read.pos<(end+read_length):
                    key = str(read.qname)+"_gene_"+str(gene)
                    if key not in found:
                        found.add(key)
                        gene_count+=1

    # count the number of reads that span exons in this gene
    # the code counts per exon, but does not double count over multiple exons in the same gene
    if feature=='exon' and feature_length>10:
        exon_length+=feature_length
        for read in iBAM.fetch(chrom, int(start)+3, int(end)-3):
            if (strand=='+' and read.is_reverse and read.is_read2
                or (strand=='+' and not read.is_reverse and read.is_read1)
                or (strand=='-' and read.is_reverse and read.is_read1)
                or (strand=='-' and not read.is_reverse and read.is_read2)):
                if (start<read.aend<end or start<read.pos<end):
                    key = str(read.qname)+"_exon_"+str(gene)
                    if key not in found:
                        found.add(key)
                        exon_count+=1

    # count the number of reads that span introns in this gene
    # the code counts per intron, but does not double count over multiple introns in the same gene
    if feature=='intron' and feature_length>10:
        intron_length+=feature_length
        for read in iBAM.fetch(chrom, int(start)+3, int(end)-3):
            if (strand=='+' and read.is_reverse and read.is_read2
                or (strand=='+' and not read.is_reverse and read.is_read1)
                or (strand=='-' and read.is_reverse and read.is_read1)
                or (strand=='-' and not read.is_reverse and read.is_read2)):
                if (start<read.aend<end or start<read.pos<end):
                    key = str(read.qname)+"_intron_"+str(gene)
                    if key not in found:
                        found.add(key)
                        intron_count+=1

    # store gene name before reading the next one
    gene_output=gene

iMerge.close()
iBAM.close()
oTest.close()