# -*- coding: utf-8 -*-
"""
Created on Fri Aug  7 19:03:44 2020

@author: Frank
"""

##Repeat with the cleaned test/training dataset
import tensorflow as tf
import os
import tensorflow.keras as keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
import math

#(x_train, y_train),(x_test, y_test) = mnist.load_data()

# Extract dataset from folder:
train_datagen = ImageDataGenerator(rescale = 1/255)
test_datagen = ImageDataGenerator(rescale = 1/255)

# Batch Gradient Descent. Batch Size = Size of Training Set
# Stochastic Gradient Descent. Batch Size = 1
# Mini-Batch Gradient Descent. 1 < Batch Size < Size of Training Set

# get training images
train_gen = train_datagen.flow_from_directory(
    r'.\cleaned_data\train',
    target_size = (32, 32),
    batch_size = 128,
    class_mode='binary'
    #classes = ['normal','viral']
)

# get testing images
test_gen = test_datagen.flow_from_directory(
    r'.\cleaned_data\test',
    target_size = (32, 32), #FK: Target size is the height and width of the images...
    batch_size  = 128,
    class_mode='binary'
    #classes = ['normal','viral']
)

imgs, labels = next(train_gen)

import matplotlib.pyplot as plt
print(imgs[0])
plt.imshow(imgs[0],cmap=plt.cm.binary)
plt.show()

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(train_gen,
        steps_per_epoch=10,
        epochs=5,
        validation_data=test_gen
    )

#val_loss, val_acc = model.evaluate(train_gen, test_gen)
#print(val_loss)
#print(val_acc)

# predictions = model.predict(x_test)
# print(predictions)
# import numpy as np
# print(np.argmax(predictions[0]))
# plt.imshow(x_test[0],cmap=plt.cm.binary)
# plt.show()

###########################################################Number dataset...
# import tensorflow as tf

# mnist = tf.keras.datasets.mnist
# (x_train, y_train),(x_test, y_test) = mnist.load_data()

# print(x_train[0])

# import matplotlib.pyplot as plt

# plt.imshow(x_train[0],cmap=plt.cm.binary)
# plt.show()
# print(y_train[0])

# x_train = tf.keras.utils.normalize(x_train, axis=1)
# x_test = tf.keras.utils.normalize(x_test, axis=1)

# print(x_train[0])

# plt.imshow(x_train[0],cmap=plt.cm.binary)
# plt.show()

# model = tf.keras.models.Sequential()

# model.add(tf.keras.layers.Flatten())
# model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
# model.compile(optimizer='adam',
#               loss='sparse_categorical_crossentropy',
#               metrics=['accuracy'])

# model.fit(x_train, y_train, epochs=3)

# val_loss, val_acc = model.evaluate(x_test, y_test)
# print(val_loss)
# print(val_acc)

# predictions = model.predict(x_test)
# print(predictions)
# import numpy as np
# print(np.argmax(predictions[0]))
# plt.imshow(x_test[0],cmap=plt.cm.binary)
# plt.show()
###################################################END OF NUMBER DATASET


from xai.brain.wordbase.nouns._pinafore import _PINAFORE

#calss header
class _PINAFORES(_PINAFORE, ):
	def __init__(self,):
		_PINAFORE.__init__(self)
		self.name = "PINAFORES"
		self.specie = 'nouns'
		self.basic = "pinafore"
		self.jsondata = {}


"""

Interface layer to the database on the Java side.

"""

import sqlite3, re, math

class DB:
	def __init__(self, path):
		if hasattr(path,'execute'):
			self.conn = path
			self.conn.row_factory = sqlite3.Row
		else:
			self.conn = sqlite3.connect(path, isolation_level=None)
			self.conn.execute('pragma journal_mode=wal')
			self.conn.row_factory = sqlite3.Row
		self.debug = False

		self.addMoreFunctions()

	def __enter__(self):
		return self

	def __exit__(self, type, value, tb):
		self.conn.close()

	def addMoreFunctions(self):
		self.conn.create_function('_ln', 1, math.log)
		self.conn.create_function('_exp', 1, math.exp)

		class Prod:
			def __init__(self):
				self.total = 1
			def step(self, value):
				self.total *= value
			def finalize(self):
				return self.total
		self.conn.create_aggregate('_product', 1, Prod)

	def escapeIdentifier(self, id):
		if id.find("\x00") >= 0:
			raise "NUL characters not allowed in SQL identifiers"

		return "\"" + id.replace("\"", "\"\"") + "\""

	def query(self, sql, params = None, ignoreErrors = False, keyType = "name"):
		if keyType == "index":
			self.conn.row_factory = None
		else:
			self.conn.row_factory = sqlite3.Row

		if params is None: params = []
		if self.debug:
			print("query:",sql,"
")
			print("params:",params,"
")

		if ignoreErrors:
			rs = None
			try:
				rs = self.conn.execute(sql, params)
			except: pass
		else:
			rs = self.conn.execute(sql, params)

		# Always commit
		self.conn.commit()

		return rs

	def handleValType(self, val):
		return bautils.convertNumber(val)
		'''try:
			val = int(val)
		except:
			try:
				val = float(val)
			except: pass

		return val'''

	# This will be efficient, simple and protect against injections
	def intParams(self, lst):
		sqlFrag = "("
		sep = ""
		for item in lst:
			int(item) # If not int, throw error
			sqlFrag += sep + str(item)
			sep = ","

		sqlFrag += ")"

		return sqlFrag

	# Creates the placeholders string. More general than above function, but involves
	# duplication and is less efficient
	def placeholders(self, lst):
		return "("+("?,"*len(lst))[:-1]+")"

	def queryValue(self, sql, params = None, typeHandling = True, ignoreErrors = False):
		rs = self.query(sql, params, ignoreErrors)
		if rs is None: return None
		row = rs.fetchone()
		if row is None:  return None

		return row[0]

	# keyType can be "name" or "index"
	def queryRow(self, sql, params = None, keyType = "name", typeHandling = True, ignoreErrors = False):
		rs = self.query(sql, params, ignoreErrors, keyType)
		row = rs.fetchone()
		if row is None:  return None

		if keyType == "name":
			return dict(row)
		elif keyType == "index":
			return list(row)

	# keyType can be "name" or "index"
	def queryRows(self, sql, params = None, keyType = "name", typeHandling = True, oneD = False, ignoreErrors = False):
		rs = self.query(sql, params, ignoreErrors, keyType)

		if oneD:
			data = []
			for row in rs:
				data.extend(row)
			return data

		rows = rs.fetchall()
		if rows is None:  return None

		if keyType == "name":
			return [dict(r) for r in rows]
		elif keyType == "index":
			return [list(r) for r in rows]
		elif keyType == "indexWithHeaders":
			return [[list(r) for r in rows],rows[0].keys()]

	# Returns a map of field1 to field2 (which are the first and second fields, respectively, by default)
	def queryMap(self, sql, params = None, ignoreErrors = False, field1 = 0, field2 = 1):
		rs = self.query(sql, params, ignoreErrors)

		map = {}
		for row in rs:
			map[row[field1]] = row[field2]

		return map

	def update(self, table, upds, condition, params, ignore = []):
		if not params: params = []

		updStr = ""
		sep = ""
		i = 0
		for field,val in upds.items():
			if field in ignore:  continue
			params.insert(i, val)
			updStr += sep + field + "= ?"
			sep = ", "
			i += 1

		if condition:
			condition = " where "+condition

		sql = "update "+table+" set "+updStr+condition

		if self.debug:
			print("query:",sql,"
")
			print("params:",params,"
")

		self.query(sql, params)

	# pk can be string (name of primary key), or an array of strings (names) for a "composite" key
	# replace always returns the row id, even for composite keys
	def replace(self, table, upds, pk, valid = None):
		if isinstance(pk, list):
			pks = pk
		else:
			pks = [pk]
		for pk in pks:
			if pk not in upds:
				print("Primary key '{}' not in |upds|. Use None or -1 if inserting.", pk)
		pkWheres = ' and '.join('{} = ?'.format(pk) for pk in pks)
		recordExists = self.queryValue("select 1 from {} where {}".format(table, pkWheres), [upds[pk] for pk in pks])

		# Filter down to just those that are in valid (if valid specified)
		if valid:
			oldUpds = upds
			upds = {}
			for k in valid:
				if k in oldUpds:
					v = oldUpds[k]
					upds[k] = v

		if not recordExists:
			insertFields = [k for k in upds.keys() if k not in pks] if (len(pks)==1 and isinstance(upds[pk],int) and upds[pk]<0) else upds.keys()
			fieldStr = ", ".join(self.escapeIdentifier(f) for f in insertFields)
			placeholdersStr = ("?,"*len(insertFields))[:-1]
			params = [upds[f] for f in insertFields]
			sql = "insert into {} ({}) values ({})".format(self.escapeIdentifier(table), fieldStr, placeholdersStr)
			if self.debug: print(sql, params)
			self.query(sql, params)

			return self.queryValue("select last_insert_rowid()")
		else:
			updateFields = upds.keys()
			fieldSetStr = ", ".join(self.escapeIdentifier(k)+" = ?" for k in updateFields)
			params = [upds[f] for f in updateFields] + [upds[pk] for pk in pks]
			sql = "update {} set {} where {}".format(self.escapeIdentifier(table), fieldSetStr, pkWheres)
			if self.debug: print(sql, params)
			self.query(sql, params)

			if len(pks)==1:
				return upds[pk]
			else:
				return self.queryValue('select rowid from {} where {}'.format(self.escapeIdentifier(table), pkWheres), [upds[pk] for pk in pks])

	'''
	changeSet structure should be: array(
		"changed" => array(
			<pkValue> => array("field1" => val, etc.),
			<pkValue2> etc.
		),
		"inserted" => array(
			array("field1" => val, etc.),
			array("field1" => val, etc.),
			etc.
		),
		"deleted" => array(<pkValue>, <pkValue2>, etc.)
	)
	'''
	def applyTableChanges(self, table, changeSet, pkName):
		newPkMap = {}

		for pk, record in changeSet["changed"].items():
			record[pkName] = int(pk)
			self.replace(table, record, pkName)

		for record in changeSet["inserted"]:
			oldPk = record[pkName]
			record[pkName] = None
			newPk = self.replace(table, record, pkName)
			newPkMap[oldPk] = newPk

		for pk in changeSet["deleted"]:
			self.query("delete from "+table+" where "+pkName+" = ?", [pk])

		return newPkMap

	def tableExists(self, table):
		return (self.queryValue("select 1 from sqlite_master where type='table' and name = ?", [table]) is not None)

	def fieldNames(self, rs):
		return [r[0] for r in rs.description]

	# inTable is either a string name of a table, or a rs
	def writeCsv(self, inTable, outCsvFn, fields = "*", where = None):
		import csv
		with open(outCsvFn, "w", newline='') as outCsvFile:

			if type(inTable) == str:
				rs = self.query("select "+",".join(fields)+" from "+inTable+((" where "+where) if where else ""))
			else:
				rs = inTable

			# Write headers
			if fields == "*":
				headers = self.fieldNames(rs)
			else:
				headers = fields
			outCsv = csv.DictWriter(outCsvFile, headers)
			outCsv.writeheader()

			for row in rs:
				row = dict(row)
				outCsv.writerow(dict((k,row[k]) for k in headers if k in row))

			rs.close()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct  5 10:50:27 2020

@author: haodale
"""

import os
import math
import re
import sys

X = []
Y = []
W = {}
W_new = {}

lamda = 0.01
mu = 0.01 #learning reate
iterate_num = 10
voc = set()

# read training files and prepare the data for training, denote spam Y = 0, ham Y = 1
def readFile(path, isSpam):
    files = os.listdir(path)
    for file in files:
        f = open(os.path.join(path, file), 'r', encoding = "ISO-8859-1")
        content = f.read()

        #only count the meaningful words
        content = re.sub('[^a-zA-Z]', ' ', content)
        content = content.strip().lower()

        row = {}
        row["0"] = 1.0
        for word in content.split():
            # get distinct words, put them in voc set
            voc.add(word)

            # as the voc sequence to form the X and Y vectors
            if row.get(word) is None:
                row[word] = 0
            row[word] += 1

        X.append(row)
        if isSpam:
            Y.append(0)
        else:
            Y.append(1)

# calculate the prob P(Y=1|Xl,W)
def calProb(xl):
    global W
    WX = W["0"]
    for word in xl:
        if word in W:
            WX += W[word] * xl[word]

    #avoid overflow
    if WX >= 700:
        return 1

    exp_WX = math.exp(WX)
    prob = exp_WX / (1 + exp_WX)
    return prob

# train LR to get w vector
def trainLR():
    global W, W_new
    W_new = W.copy()
    print('running iteration: ', end=" ", flush = True)
    for i in range(iterate_num):
        print((i + 1), end=" ", flush = True)
        for word in W:
            sum = 0.0
            for l in range(len(Y)):
                yl = Y[l]
                xl = X[l]
                if word in xl:
                    prob = calProb(xl)
                    sum += xl[word] * (yl - prob)
            W_new[word] = W[word] + mu * sum - lamda * mu * W[word]
        W = W_new.copy()
    print()

# test the logistic regression with test files
def testLR(path, isSpam):
    correct = 0
    total = 0

    files = os.listdir(path)
    # get distinct words, put them in voc set
    for file in files:
        total += 1
        f = open(os.path.join(path, file), 'r', encoding = "ISO-8859-1")
        content = f.read()

        #only count the meaningful words
        content = re.sub('[^a-zA-Z]', ' ', content)
        content = content.strip().lower()

        row = {}
        for word in content.split():
            if word in W:
                if row.get(word) is None:
                    row[word] = 0
                row[word] += 1

        WX = W["0"]
        for word in row:
            WX += W[word] * row[word]

        if isSpam:
            if WX <= 0:
                correct += 1
        else:
            if WX > 0:
                correct += 1

    return correct, total

#main function
if __name__ == '__main__':
    #parameter 1 for lamda, parameter 2 for iteration times
    lamda = float(sys.argv[1])
    iterate_num = int(sys.argv[2])

    trainSpamPath = 'train/spam'
    trainHamPath = 'train/ham'
    testSpamPath = 'test/spam'
    testHamPath = 'test/ham'

    # read training files and prepare the data for training
    readFile(trainSpamPath, True)
    readFile(trainHamPath, False)

    # initial W vector all item to be 0
    W["0"] = 0.0
    for word in voc:
        W[word] = 0.0

    # train LR
    trainLR()

    #test for spam and ham test data seperatelly
    spam_correct, spam_total = testLR(testSpamPath, True)
    ham_correct, ham_total = testLR(testHamPath, False)

    print("spam accuracy:", spam_correct,"/", spam_total,"=", spam_correct/spam_total)
    print("ham accuracy:", ham_correct,"/", ham_total,"=", ham_correct/ham_total)

    # final result for accuracy
    accuracy = (spam_correct + ham_correct) / (spam_total + ham_total)
    print ("total accuracy:", accuracy)


from datetime import datetime
import sys
sys.path.insert(0, '../../../src/')
from models import Locussummary, LocussummaryReference, Source
sys.path.insert(0, '../')
from database_session import get_dev_session as get_session
from config import CREATED_BY, EMAIL
from util import sendmail

__author__ = 'sweng66'

'''
* Retrieve the summary data and put it into dictionary (memory)
* Read in the summary text for each gene from the upload file and compare the
  text with the info in the memory.
  * The summary for this gene for the given type (eg, Regulation or Phenotype)
    is not in the database,
       * insert the summay text into the LOCUSSUMMARY table
       * insert any associated reference(s) into LOCUSSUMMARY_REFERENCE table
         (eg, for regulation summaries)
  * The summary for this gene for the given type is in the database.
       * if the summary text is updated, update the LOCUSSUMMARY.text/html;
         otherwise noneed todo anything to theLOCUSSUMMARY table
       * check to see if there is any referenceupdate, if yes, updatethe
         LOCUSSUMMARY_REFERENCE table
'''

def load_summaries(summary_type, summary_file, log_file):

    nex_session = get_session()

    fw = open(log_file,"w")

    fw.write(str(datetime.now()) + "\n")
    fw.write("reading data from summary_file...\n")

    data = read_summary_file(nex_session, fw, summary_type, summary_file, log_file)

    fw.write(str(datetime.now()) + "\n")
    fw.write("retriveing data from database and store the data in dictionary...\n")

    key_to_summary = dict([((x.locus_id, x.summary_type, x.summary_order), x) for x in nex_session.query(Locussummary).all()])
    key_to_summaryref = dict([((x.summary_id, x.reference_id, x.reference_order), x) for x in nex_session.query(LocussummaryReference).all()])

    source_to_id = dict([(x.display_name, x.source_id) for x in nex_session.query(Source).all()])
    source_id = source_to_id.get('SGD')

    summary_id_to_references = {}
    for x in nex_session.query(LocussummaryReference).all():
        references = []
        if x.summary_id in summary_id_to_references:
            references = summary_id_to_references[x.summary_id]
        references.append(x)
        summary_id_to_references[x.summary_id] = references

    load_summary_holder = { "summary_added": 0,
                            "summary_updated": 0,
                            "summary_reference_added": 0 }

    fw.write(str(datetime.now()) + "\n")
    fw.write("updating the database...\n")

    for x in data:
        key = (x['locus_id'], x['summary_type'], x['summary_order'])
        summary_id = None
        if key in key_to_summary:
            if x['text'] != key_to_summary[key].text.strip():
                fw.write("OLD:" + key_to_summary[key].text + ":\n")
                fw.write("NEW:" + x['text'] + ":\n")
                nex_session.query(Locussummary).filter_by(summary_id=key_to_summary[key].summary_id).update({'text': x['text'], 'html': x['html']})
                nex_session.commit()
                load_summary_holder['summary_updated'] = load_summary_holder['summary_updated'] + 1
            else:
                fw.write("SUMMARY is in DB\n")
            summary_id = key_to_summary[key].summary_id
            update_references(nex_session,
                              fw,
                              load_summary_holder,
                              source_id,
                              summary_id,
                              summary_id_to_references.get(summary_id),
                              x.get('references'))
        else:
            summary_id = insert_summary(nex_session, fw, load_summary_holder, source_id, x)
            if x.get('references'):
                for y in x['references']:
                    insert_summary_reference(nex_session, fw, load_summary_holder, source_id, summary_id, y)

    nex_session.commit()
    nex_session.close()

    fw.write(str(datetime.now()) + "\n")
    fw.write("writing summary and sending an email to curators...\n")

    write_summary_and_send_email(load_summary_holder, fw, summary_type)

    fw.close()

def insert_summary_reference(nex_session, fw, load_summary_holder, source_id, summary_id, y):

    x = LocussummaryReference(summary_id = summary_id,
                              reference_id = y['reference_id'],
                              reference_order = y['reference_order'],
                              source_id = source_id,
                              created_by = CREATED_BY)
    nex_session.add(x)

    load_summary_holder['summary_reference_added'] = load_summary_holder['summary_reference_added'] + 1

    fw.write("insert new summary reference:" + str(summary_id) + ", " + str(y['reference_id']) + ", " + str(y['reference_order']))

def insert_summary(nex_session, fw, load_summary_holder, source_id, y):

    x = Locussummary(locus_id = y['locus_id'],
                     summary_type = y['summary_type'],
                     summary_order = y['summary_order'],
                     text = y['text'],
                     html = y['html'],
                     source_id = source_id,
                     created_by = CREATED_BY)

    nex_session.add(x)
    nex_session.commit()

    load_summary_holder['summary_added'] = load_summary_holder['summary_added'] + 1

    fw.write("insert summary:" + str(y['locus_id']) + ", " + y['summary_type'] + ", " + str(y['summary_order']) + ", " + y['text'] + ", " + y['html']+"\n")

    return x.summary_id

def update_references(nex_session, fw, load_summary_holder, source_id, summary_id, old_references, new_references):
    if old_references is None and new_references is None:
        return
    if old_references is None:
        for y in new_references:
            insert_summary_reference(nex_session, fw, load_summary_holder, source_id, summary_id, y)
    elif new_references is None:
        for y in old_references:
            nex_session.delete(y)
    else:
        ref_old = {}
        for x in old_references:
            ref_old[x.reference_id] = x

        ref_new = {}
        for y in new_references:
            if y['reference_id'] in ref_old:
                x = ref_old[y['reference_id']]
                if y['reference_order'] == x.reference_order:
                    continue
                else:
                    nex_session.query(LocussummaryReference).filter_by(summary_id=summary_id,reference_id=y['reference_id']).update({'reference_order': y['reference_order']})
            else:
                insert_summary_reference(nex_session, fw, load_summary_holder, source_id, summary_id, y)
            ref_new[y['reference_id']] = 1

        ## clean up old refs
        # for x in old_references:
        #    if x.reference_id in ref_new:
        #        print "The REFERENCE is in the file"
        #        continue
        #    # nex_session.delete(x)
        #    # print "The LOCUSSUMMARY_REFERENCE row for summary_id=", summary_id, " and reference_id=", x.reference_id, " has been deleted from the database."

def read_summary_file(nex_session, fw, summary_type, summary_file, log_file):

    from util import link_gene_names
    from models import Locusdbentity, Referencedbentity

    name_to_dbentity = dict([(x.systematic_name, x) for x in nex_session.query(Locusdbentity).all()])
    sgdid_to_dbentity_id = dict([(x.sgdid, x.dbentity_id) for x in nex_session.query(Locusdbentity).all()])
    pmid_to_reference_id = dict([(x.pmid, x.dbentity_id) for x in nex_session.query(Referencedbentity).all()])

    data = []

    if summary_type == 'Phenotype':
        f = open(summary_file, 'U')
        for line in f:
            pieces = line.strip().split('\t')
            dbentity = name_to_dbentity.get(pieces[0])
            if dbentity is None:
                continue
            data.append({'locus_id': dbentity.dbentity_id,
                         'text': pieces[1],
                         'html': link_gene_names(pieces[1], {dbentity.display_name, dbentity.format_name, dbentity.display_name + 'P', dbentity.format_name + 'P'}, nex_session),
                         'summary_type': summary_type,
                         'summary_order': 1 })

    elif summary_type == 'Regulation':
        f = open(summary_file, 'U')
        for line in f:
            pieces = line.strip().split('\t')
            dbentity = name_to_dbentity.get(pieces[0])
            if dbentity is None:
                continue

            references = []

            pmid_list = pieces[3].replace(' ', '')
            pmids = pmid_list.split('|')
            order = 0

            for pmid in pmids:
                reference_id = pmid_to_reference_id.get(int(pmid))
                if reference_id is None:
                    print("PMID=", pmid, " is not in the database")
                    continue
                order = order + 1
                references.append({'reference_id': reference_id, 'reference_order': order})

            data.append({'locus_id': dbentity.dbentity_id,
                         'text': pieces[2],
                         'html': link_gene_names(pieces[2], {dbentity.display_name, dbentity.format_name, dbentity.display_name + 'P', dbentity.format_name + 'P'}, nex_session),
                         'summary_type': summary_type,
                         'summary_order': 1,
                         'references': references})
    elif summary_type == 'Function':
        f = open(summary_file, 'U')
        for line in f:
            pieces = line.split('\t')
            if len(pieces) >= 8:
                sgdid = pieces[8]
                if sgdid.startswith('SGD:'):
                    dbentity_id = sgdid_to_dbentity_id.get(sgdid[4:])
                    if dbentity_id is None:
                        continue
                    functionSummary = [x[22:].strip() for x in pieces[9].split('|') if x.startswith('go_annotation_summary')]
                    if len(functionSummary) == 1:
                        data.append({'locus_id': dbentity_id,
                                     'text': functionSummary[0],
                                     'html': functionSummary[0],
                                     'summary_type': summary_type,
                                     'summary_order': 1})
    else:
        fw.write("Unknown summary_type: " + summary_type+ "\n")
        exit()

    return data

def write_summary_and_send_email(load_summary_holder, fw, summary_type):

    summary = ''
    summary = summary + "In total " + str(load_summary_holder['summary_added']) + " " + summary_type + " summaries added.\n"
    summary = summary + "In total " + str(load_summary_holder['summary_updated']) + " " + summary_type + " summaries updated.\n"
    summary = summary + "In total " + str(load_summary_holder['summary_reference_added']) + " " + summary_type + " summary references addedd.\n\n"

    fw.write(summary)

    # email_subject = summary_type + " Summaries Loading"
    ## send email here
    # sendmail(email_subject, summary, EMAIL)

    print(summary)

if __name__ == "__main__":

    log_file = "logs/summary_loading.log"

    summary_file = ""
    summary_type = ""
    if len(sys.argv) >= 3:
        summary_file = sys.argv[1]
        summary_type = sys.argv[2]
    elif len(sys.argv) >= 2:
        summary_file = sys.argv[1]
        summary_type = "Phenotype"
    else:
        print("Usage: load_function_summaries.py summary_file_name_with_path summary_type[Phenotype|Regulation|Function]")
        print("Example: load_function_summaries.py 16-11phenoSummaries.txt Phenotype")
        print("Example: load_function_summaries.py 16-5regulationSummaries.txt Regulation")
        print("Example: load_function_summaries.py gp_information.559292_sgd Function")
        exit()

    log_file = "logs/" + summary_type + "summary_loading.log"

    load_summaries(summary_type, summary_file, log_file)


import tushare as ts
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = ts.get_k_data('510050',start='2008-01-01',end='2018-01-01',ktype='D',index=False)
li =np.array(data['close'].values)
t1 = np.diff(li)
result = []
temp = 0
for i in range(len(t1)):
    if t1[i]>0:
        temp+=1
        continue
    if t1[i]<0:
        result.append(temp)
        temp = 0
result = np.array(result)
result = result[result>0]
plt.hist(result)
plt.show()


import torch
from torch.nn.functional import conv_transpose1d, conv_transpose2d, conv_transpose3d

from backpack.utils.ein import eingroup

def get_convtranspose1d_weight_gradient_factors(input, grad_out, module):
    N, C_in = input.shape[0], input.shape[1]
    kernel_size = module.kernel_size
    kernel_size_numel = int(torch.prod(torch.Tensor(kernel_size)))

    X = unfold_by_conv_transpose(input, module).reshape(N, C_in * kernel_size_numel, -1)
    return X, grad_out

def get_convtranspose2d_weight_gradient_factors(input, grad_out, module):
    N, C_in = input.shape[0], input.shape[1]
    kernel_size = module.kernel_size
    kernel_size_numel = int(torch.prod(torch.Tensor(kernel_size)))

    X = unfold_by_conv_transpose(input, module).reshape(N, C_in * kernel_size_numel, -1)
    dE_dY = eingroup("n,c,h,w->n,c,hw", grad_out)
    return X, dE_dY

def get_convtranspose3d_weight_gradient_factors(input, grad_out, module):
    N, C_in = input.shape[0], input.shape[1]
    kernel_size = module.kernel_size
    kernel_size_numel = int(torch.prod(torch.Tensor(kernel_size)))

    X = unfold_by_conv_transpose(input, module).reshape(N, C_in * kernel_size_numel, -1)
    dE_dY = eingroup("n,c,d,h,w->n,c,dhw", grad_out)
    return X, dE_dY

def unfold_by_conv_transpose(input, module):
    """Return the unfolded input using transpose convolution."""
    N, C_in = input.shape[0], input.shape[1]
    kernel_size = module.kernel_size
    kernel_size_numel = int(torch.prod(torch.Tensor(kernel_size)))

    def make_weight():
        weight = torch.zeros(1, kernel_size_numel, *kernel_size)

        for i in range(kernel_size_numel):
            extraction = torch.zeros(kernel_size_numel)
            extraction[i] = 1.0
            weight[0, i] = extraction.reshape(*kernel_size)

        repeat = [C_in, 1] + [1 for _ in kernel_size]
        weight = weight.repeat(*repeat)
        return weight.to(module.weight.device)

    def get_conv_transpose():
        functional_for_module_cls = {
            torch.nn.ConvTranspose1d: conv_transpose1d,
            torch.nn.ConvTranspose2d: conv_transpose2d,
            torch.nn.ConvTranspose3d: conv_transpose3d,
        }
        return functional_for_module_cls[module.__class__]

    conv_transpose = get_conv_transpose()
    unfold = conv_transpose(
        input,
        make_weight().to(module.weight.device),
        bias=None,
        stride=module.stride,
        padding=module.padding,
        dilation=module.dilation,
        groups=C_in,
    )

    return unfold.reshape(N, -1, kernel_size_numel)


# importing the library
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from nltk.stem import WordNetLemmatizer
import re
import pandas as pd

# importing the data set
messages1 = pd.read_csv('spam.csv',encoding='latin-1')
messages =messages1.iloc[:,[0,1]]

# changing cols with rename()
messages = messages.rename(columns = {"v1": "Result",
                                      "v2":"Message"})

# cleaning the text
ps = PorterStemmer()
wordnet = WordNetLemmatizer()
corpus = []

#sing stemming
for i in range(len(messages)):
    review = re.sub('[^a-zA-Z]',' ' , messages['Message'][i])
    review = review.lower()
    review = review.split()
    review = [ps.stem(word) for word in review if not word in set(stopwords.words('english'))]
    review = ' '.join(review)
    corpus.append(review)

'''
# using lematization
for i in range(len(messages)):
    review = re.sub('[^a-zA-Z]',' ' , messages['Message'][i])
    review = review.lower()
    review = review.split()
    review = [wordnet.lemmatize(word) for word in review if not word in set(stopwords.words('english'))] # here we can use lematization or stemming
    review = ' '.join(review)
    corpus.append(review)
'''

# creating the TF-IDF Model
from sklearn.feature_extraction.text import TfidfVectorizer
TF_IDF = TfidfVectorizer()   # in tf-idf seperate craetes the seperate precision to the words i.e 0.5,0.75,0.3612
X = TF_IDF.fit_transform(corpus).toarray()

'''
# creating the BOW model
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features=6000)     # in BOW model it creates the same priority to all the words i.e 0 or 1
X = cv.fit_transform(corpus).toarray()
'''

y = pd.get_dummies(messages['Result'])
y= y.iloc[:,0].values

# for visulization  and exporting to excel
visulize =pd.DataFrame(X,y)
export_excel = visulize.to_excel (r'C:\Users\dell\Desktop\NLP_export_dataframe.xlsx', index = None, header=True) # Dont forget to add .xlsx at the end of the path

# train test split
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2 , random_state=0)

'''
# fitting the model
# for NLP naive bayes is the most used algorithm
# Fitting Naive Bayes to the Training set
from sklearn.naive_bayes import GaussianNB # here accuracy is 88%
classifier = GaussianNB()
classifier.fit(X_train, y_train)
'''

# fitting the multinomial naive bayes
from sklearn.naive_bayes import MultinomialNB # this multinomial naive bayes accuracy is more comapred to gaussian i.e 96%
classifier = MultinomialNB()
classifier.fit(X_train,y_train)

# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

#checking the acuracy score
from sklearn.metrics import accuracy_score
accuracy= accuracy_score(y_test,y_pred) * 100


import random
import math

class FitnessCalc:
    def __init__(self, cMatrix, priorProbVector, priorProbVectorReverse):
        self.__cMatrixRowNum = 10
        self.__cMatrixColNum = 25
        self.cMatrix = cMatrix
        self.priorProbVector = priorProbVector
        self.priorProbVectorReverse = priorProbVectorReverse

    # calculate population fitness
    def popFit(theGen, manVector):
        rowNum = len(theGen);
        indFitVector = [0]*rowNum
        for i in range(rowNum):
            individual = theGen[i]
            indFitVector[i] = self.indFit(individual, manVector);
        return indFitVector

    # calculate individual fitness
    def indFit(self, disVector, manVector):
        fitValue = self.fit(disVector, manVector)
        return fitValue

    def fit(self, disVector, manVector):
        fitValue = self.fit1(disVector, manVector) * \
                self.fit2(disVector, manVector) * \
                self.fit3(disVector);
        return fitValue;

    # fit1 function
    def fit1(self, disVector, manVector):
        fitValue = 1.0;
        for i in range(self.__cMatrixRowNum):
            if (manVector[i] != 0):
                fitValue = fitValue * (1 - self.fitHelper(i, disVector));
        return fitValue;

    # helper function for fit1
    def fitHelper(self, theRowNum, disVector):
        result = 1.0;
        for i in range(self.__cMatrixColNum):
            if (disVector[i] != 0):
                result = result * (1 - self.cMatrix[theRowNum][i]);
        return result;

    # fit2 function
    def fit2(self, disVector, manVector):
        fitValue = 1.0;
        for i in range(self.__cMatrixRowNum):
            if (manVector[i] == 0):
                for j in range(self.__cMatrixColNum):
                    if (disVector[j] != 0):
                        fitValue = fitValue* (1 - self.cMatrix[i][j]);
        return fitValue;

    # fit3 function
    def fit3(self, disVector):
        fitValue = 1.0;
        for i in range(self.__cMatrixColNum):
            if (disVector[i] != 0):
                fitValue = fitValue * (self.priorProbVector[i] / self.priorProbVectorReverse[i]);
        return fitValue;

class ManGenerator:
    def getBit(self, num):
        bitVec = [int(x) for x in bin(num)[2:]];
        aManVector = [0] * 10;

        count = -1;
        for i in range(len(bitVec)):
            aManVector[count] = bitVec[-(i+1)];
            count -= 1;
        return aManVector;

    def getInt(self, theVector):
        for i in range(10):
            theVector[i] = str(theVector[i]);
        theStr = ''.join(theVector);
        return int(theStr, 2)


import pygame
import time
import random as rd

pygame.init()

food_point=2
width = 600
height = 400
h1=470
dis = pygame.display.set_mode((width,h1))
pygame.display.set_caption("Snake Game")

def gen_food():
    global foodx,foody,food_point
    dis.fill(black)
    for a in range(width):
        pygame.draw.rect(dis,red,[a,height,snakeblock,snakeblock])
    font = pygame.font.SysFont("comicsansms",35)
    val = font.render("Score = "+str(snake_length-1), True, red)
    dis.blit(val,[snakeblock,height+snakeblock])
    if snake_length%10==1:
        food_point = 5
    else:
        food_point = 1
    foodx = round(rd.randrange(0,width-snakeblock)/snakeblock)*snakeblock
    foody = round(rd.randrange(0,height-snakeblock)/snakeblock)*snakeblock

def snake():
    global foodx,foody,snake_list,food_point
    for x1 in snake_list:
        pygame.draw.rect(dis,orange,[x1[0],x1[1],snakeblock,snakeblock])
    if food_point>1:
        font = pygame.font.SysFont("comicsansms",35)
        val = font.render("Bonus!!!", True, red)
        dis.blit(val,[width*3/7,height+snakeblock])
        pygame.draw.rect(dis,green,[foodx-snakeblock,foody-snakeblock,snakeblock*3,snakeblock*3])
    else:
        pygame.draw.rect(dis,green,[foodx,foody,snakeblock,snakeblock])

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
orange = (240,50,0)
gameover=False
snakeblock = 10
snake_list = []
snake_length=1
x=width/2
x_change=0
y=height/2
y_change=0
dis.fill(black)
foodx=0
foody=0
clock = pygame.time.Clock()
speed= 10
gameend=False

while not gameover:
    while gameend:
        x_change=0
        y_change=0
        dis.fill(black)
        for a in range(width):
            pygame.draw.rect(dis,red,[a,height,snakeblock,snakeblock])
        score=snake_length-1
        font = pygame.font.SysFont("comicsansms",35)
        val = font.render("Score = "+str(score)+". Press 'r' to retry", True, red)
        dis.blit(val,[width/7,height+snakeblock])
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameend=False
                gameover = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                    gen_food()
                    x_change=-snakeblock
                    gameend=False
                    dis.fill(black)
                    x=width/2
                    y=height/2
                    snake_length=1
                    snake_list = []
                    gen_food()
                    for a in range(width):
                         pygame.draw.rect(dis,red,[a,height,snakeblock,snakeblock])

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameover = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_s:
                print("space pressed")
                dis.fill(black)
                gen_food()
                for a in range(width):
                    pygame.draw.rect(dis,red,[a,height,snakeblock,snakeblock])
                x_change=-snakeblock
            if event.key==pygame.K_DOWN and x_change!=0:
                print("down pressed")
                x_change = 0
                y_change = snakeblock
            if event.key == pygame.K_UP and x_change!=0:
                print("up pressed")
                x_change = 0
                y_change = -snakeblock
            if event.key == pygame.K_RIGHT and y_change!=0:
                print("right pressed")
                x_change = snakeblock
                y_change = 0
            if event.key == pygame.K_LEFT and y_change!=0:
                print("left pressed")
                x_change = -snakeblock
                y_change = 0

    head=[]
    x=x+x_change
    y=y+y_change
    x%=width
    y=y%height
    head.append(x)
    head.append(y)
    snake_list.append(head)
    if x>=width or x<0 or y>height or y<0:
        gameend=True
    if len(snake_list) > snake_length:
        #print("reduce",len(snake_list))
        pygame.draw.rect(dis,black,[snake_list[0][0],snake_list[0][1],snakeblock,snakeblock])
        del snake_list[0]
    if x_change==0 and y_change==0:
        font = pygame.font.SysFont("comicsansms",35)
        val = font.render("Press 's' to start", True, red)
        dis.blit(val,[width*2/7,height+snakeblock])
        pygame.display.update()
        continue;

    snake()
    for x1 in snake_list[0:-1]:
        if x1[0]==x and x1[1]==y:
            print("here")
            gameend=True

    if x==foodx and y==foody:
        snake_length+=food_point
        gen_food()

    clock.tick(speed)
    pygame.display.update()

pygame.quit()


##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Yaoyao Liu
## NUS School of Computing
## Email: yaoyao.liu@mail.m2i.ac.cn
## Copyright (c) 2019
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import numpy as np
import sys
import tensorflow as tf
from tensorflow.python.platform import flags
from utils.misc import mse, softmaxloss, xent, resnet_conv_block, resnet_nob_conv_block, normalize

FLAGS = flags.FLAGS

class Models:
    def __init__(self):
        self.dim_input = FLAGS.img_size * FLAGS.img_size * 3
        self.dim_output = FLAGS.way_num
        self.update_lr = FLAGS.base_lr
        self.pretrain_class_num = FLAGS.pretrain_class_num
        self.meta_lr = tf.placeholder_with_default(FLAGS.meta_lr, ())
        self.swn_lr = tf.placeholder_with_default(FLAGS.swn_lr, ())

        self.pretrain_lr = tf.placeholder_with_default(FLAGS.pre_lr, ())

        self.loss_func = xent
        self.pretrain_loss_func = softmaxloss

        self.channels = 3
        self.img_size = FLAGS.img_size

    def process_ss_weights(self, weights, ss_weights, label):
        [dim0, dim1] = weights[label].get_shape().as_list()[0:2]
        this_ss_weights = tf.tile(ss_weights[label], multiples=[dim0, dim1, 1, 1])
        return tf.multiply(weights[label], this_ss_weights)

    def forward_pretrain_resnet(self, inp, weights, reuse=False, scope=''):
        inp = tf.reshape(inp, [-1, self.img_size, self.img_size, self.channels])
        net = self.pretrain_block_forward(inp, weights, 'block1', reuse, scope)
        net = self.pretrain_block_forward(net, weights, 'block2', reuse, scope)
        net = self.pretrain_block_forward(net, weights, 'block3', reuse, scope)
        net = self.pretrain_block_forward(net, weights, 'block4', reuse, scope)
        net = tf.nn.avg_pool(net, [1,5,5,1], [1,5,5,1], 'VALID')
        net = tf.reshape(net, [-1, np.prod([int(dim) for dim in net.get_shape()[1:]])])
        return net

    def forward_resnet(self, inp, weights, ss_weights, reuse=False, scope=''):
        inp = tf.reshape(inp, [-1, self.img_size, self.img_size, self.channels])
        net = self.block_forward(inp, weights, ss_weights, 'block1', reuse, scope)
        net = self.block_forward(net, weights, ss_weights, 'block2', reuse, scope)
        net = self.block_forward(net, weights, ss_weights, 'block3', reuse, scope)
        net = self.block_forward(net, weights, ss_weights, 'block4', reuse, scope)
        net = tf.nn.avg_pool(net, [1,5,5,1], [1,5,5,1], 'VALID')
        net = tf.reshape(net, [-1, np.prod([int(dim) for dim in net.get_shape()[1:]])])
        return net

    def forward_resnet_2(self, inp, weights, ss_weights, reuse=False, scope=''):
        '''Also outputing the conv-feature map before average pooling.'''
        inp = tf.reshape(inp, [-1, self.img_size, self.img_size, self.channels])
        net = self.block_forward(inp, weights, ss_weights, 'block1', reuse, scope)
        net = self.block_forward(net, weights, ss_weights, 'block2', reuse, scope)
        net = self.block_forward(net, weights, ss_weights, 'block3', reuse, scope)
        net = self.block_forward(net, weights, ss_weights, 'block4', reuse, scope)
        net1 = tf.nn.avg_pool(net, [1,5,5,1], [1,5,5,1], 'VALID')
        net1 = tf.reshape(net1, [-1, np.prod([int(dim) for dim in net1.get_shape()[1:]])])
        return net1, net

    def forward_fc(self, inp, fc_weights):
        net = tf.matmul(inp, fc_weights['w5']) + fc_weights['b5']
        return net

    def pretrain_block_forward(self, inp, weights, block, reuse, scope):
        net = resnet_conv_block(inp, weights[block + '_conv1'], weights[block + '_bias1'], reuse, scope+block+'0')
        net = resnet_conv_block(net, weights[block + '_conv2'], weights[block + '_bias2'], reuse, scope+block+'1')
        net = resnet_conv_block(net, weights[block + '_conv3'], weights[block + '_bias3'], reuse, scope+block+'2')
        res = resnet_nob_conv_block(inp, weights[block + '_conv_res'], reuse, scope+block+'res')
        net = net + res
        net = tf.nn.max_pool(net, [1,2,2,1], [1,2,2,1], 'VALID')
        net = tf.nn.dropout(net, keep_prob=FLAGS.pretrain_dropout_keep)
        return net

    def block_forward(self, inp, weights, ss_weights, block, reuse, scope):
        net = resnet_conv_block(inp, self.process_ss_weights(weights, ss_weights, block + '_conv1'), ss_weights[block + '_bias1'], reuse, scope+block+'0')
        net = resnet_conv_block(net, self.process_ss_weights(weights, ss_weights, block + '_conv2'), ss_weights[block + '_bias2'], reuse, scope+block+'1')
        net = resnet_conv_block(net, self.process_ss_weights(weights, ss_weights, block + '_conv3'), ss_weights[block + '_bias3'], reuse, scope+block+'2')
        res = resnet_nob_conv_block(inp, weights[block + '_conv_res'], reuse, scope+block+'res')
        net = net + res
        net = tf.nn.max_pool(net, [1,2,2,1], [1,2,2,1], 'VALID')
        net = tf.nn.dropout(net, keep_prob=1)
        return net

    def construct_fc_weights(self):
        dtype = tf.float32
        fc_weights = {}
        fc_initializer =  tf.contrib.layers.xavier_initializer(dtype=dtype)
        if FLAGS.phase=='pre':
            fc_weights['w5'] = tf.get_variable('fc_w5', [512, FLAGS.pretrain_class_num], initializer=fc_initializer)
            fc_weights['b5'] = tf.Variable(tf.zeros([FLAGS.pretrain_class_num]), name='fc_b5')
        else:
            fc_weights['w5'] = tf.get_variable('fc_w5', [512, self.dim_output], initializer=fc_initializer)
            fc_weights['b5'] = tf.Variable(tf.zeros([self.dim_output]), name='fc_b5')
        return fc_weights

    def construct_resnet_weights(self):
        weights = {}
        dtype = tf.float32
        conv_initializer =  tf.contrib.layers.xavier_initializer_conv2d(dtype=dtype)
        fc_initializer =  tf.contrib.layers.xavier_initializer(dtype=dtype)
        weights = self.construct_residual_block_weights(weights, 3, 3, 64, conv_initializer, dtype, 'block1')
        weights = self.construct_residual_block_weights(weights, 3, 64, 128, conv_initializer, dtype, 'block2')
        weights = self.construct_residual_block_weights(weights, 3, 128, 256, conv_initializer, dtype, 'block3')
        weights = self.construct_residual_block_weights(weights, 3, 256, 512, conv_initializer, dtype, 'block4')
        weights['w5'] = tf.get_variable('w5', [512, FLAGS.pretrain_class_num], initializer=fc_initializer)
        weights['b5'] = tf.Variable(tf.zeros([FLAGS.pretrain_class_num]), name='b5')
        return weights

    def construct_residual_block_weights(self, weights, k, last_dim_hidden, dim_hidden, conv_initializer, dtype, scope='block0'):
        weights[scope + '_conv1'] = tf.get_variable(scope + '_conv1', [k, k, last_dim_hidden, dim_hidden], initializer=conv_initializer, dtype=dtype)
        weights[scope + '_bias1'] = tf.Variable(tf.zeros([dim_hidden]), name=scope + '_bias1')
        weights[scope + '_conv2'] = tf.get_variable(scope + '_conv2', [k, k, dim_hidden, dim_hidden], initializer=conv_initializer, dtype=dtype)
        weights[scope + '_bias2'] = tf.Variable(tf.zeros([dim_hidden]), name=scope + '_bias2')
        weights[scope + '_conv3'] = tf.get_variable(scope + '_conv3', [k, k, dim_hidden, dim_hidden], initializer=conv_initializer, dtype=dtype)
        weights[scope + '_bias3'] = tf.Variable(tf.zeros([dim_hidden]), name=scope + '_bias3')
        weights[scope + '_conv_res'] = tf.get_variable(scope + '_conv_res', [1, 1, last_dim_hidden, dim_hidden], initializer=conv_initializer, dtype=dtype)
        return weights

    def construct_resnet_ss_weights(self):
        ss_weights = {}
        ss_weights = self.construct_residual_block_ss_weights(ss_weights, 3, 64, 'block1')
        ss_weights = self.construct_residual_block_ss_weights(ss_weights, 64, 128, 'block2')
        ss_weights = self.construct_residual_block_ss_weights(ss_weights, 128, 256, 'block3')
        ss_weights = self.construct_residual_block_ss_weights(ss_weights, 256, 512, 'block4')
        return ss_weights

    def construct_residual_block_ss_weights(self, ss_weights, last_dim_hidden, dim_hidden, scope='block0'):
        ss_weights[scope + '_conv1'] = tf.Variable(tf.ones([1, 1, last_dim_hidden, dim_hidden]), name=scope + '_conv1')
        ss_weights[scope + '_bias1'] = tf.Variable(tf.zeros([dim_hidden]), name=scope + '_bias1')
        ss_weights[scope + '_conv2'] = tf.Variable(tf.ones([1, 1, dim_hidden, dim_hidden]), name=scope + '_conv2')
        ss_weights[scope + '_bias2'] = tf.Variable(tf.zeros([dim_hidden]), name=scope + '_bias2')
        ss_weights[scope + '_conv3'] = tf.Variable(tf.ones([1, 1, dim_hidden, dim_hidden]), name=scope + '_conv3')
        ss_weights[scope + '_bias3'] = tf.Variable(tf.zeros([dim_hidden]), name=scope + '_bias3')
        return ss_weights


def likes(names):
    def ans(names):
        if len(names) == 0:
            return "no one likes this"
        elif len(names) == 1:
            return "{0} likes this".format(names[0])
        elif len(names) == 2:
            return "{0} and {1} like this".format(names[0], names[1])
        elif len(names) == 3:
            return "{0}, {1} and {2} like this".format(names[0], names[1], names[2])
        else:
            return "{0}, {1} and {s} others like this".format(names[0], names[1], s = (len(names) - 2))

    return ans(names)

def isValidWalk(walk):
    from collections import Counter
    counter = Counter(walk)
    return len(walk) == 10 and counter['n'] == counter['s'] and counter['e'] == counter['w']

def find_nb(m):
    n = 0
    s = 0
    while s < m:
        s = n ** 3 + s
        n = n + 1
    if (s == m):
        return n - 1
    else:
        return -1

def filter_list(l):
    return [x for x in l if isinstance(x, int)]

print(filter_list([1,2,'a','b']))

def  to_weird_case(string):
    def word(w):
        return "".join([x.upper() if ind % 2 == 0 else x.lower() for ind, x in enumerate(w)])

    return " ".join([word(w) for w in string.split()])

print(to_weird_case('This is a test'))

def isPower(n, x):
    num = n
    while num >= x:
        if (num % x != 0 or x == 1):
            return False
        if num == x:
            return True
        num = num / x
    return False

def digitsSum(n):
    return sum(map(int, str(n)))

greatNum = lambda n: n > 10 and isPower(n, digitsSum(n))

def power_sumDigTerm(n):
    nums = range(2, 99)
    acc = set()
    pq = [[pow(p, 2), p, 2] for p in nums]

    while len(acc) < n:
        smallest = pq[0]

        if greatNum(smallest[0]):
            acc.add(smallest[0])

        power = smallest[2]
        new_base = smallest[1] + 1
        new_value = pow(new_base, power)

        pq[0] = [new_value, new_base, power + 1]
        pq.sort()

    return sorted(acc)[n - 1]


import copy

#给传入的字符串打分,0表示空格，1表示己方棋子，2表示敌方棋子，未考虑跳眠三，跳眠二，大跳活二
def point(str1):
    str2=str1[::-1]
    if '11111' in str1 or '11111' in str2:#五连
        return 1000
    elif '011110' in str1 or '011110' in str2:#活四
        return 100
    elif '011101' in str1 or '011101' in str2:#跳活四情形1
        return 16
    else:
        for x in ('011112','101112','110112','111012'):#冲四
            if x in str1 or x in str2:
                return 15
        if '011100' in str1 or '011100' in str2:#连活三
            return 15
        if '010110' in str1 or '010110' in str2:#跳活三
            return 10
        if '001112' in str1 or '001112' in str2:#眠三
            return 2
        for x in ('011000','001100'):#连活2
            if x in str1 or x in str2:
                return 2
        if '010100' in str1 or '010100' in str2:#跳活二
            return 1
        if '000112' in str1 or '000112' in str2:#眠二
            return 0.2
    return 0

#将传入的字符串进行处理，等于number的数字转化为1,不等于的转化为2，其他转化为0
def str_deal(str1,number):
    result=''
    for x in str1:
        if x=='1' or x=='2':
            if x==str(number):
                result+='1'
            else:
                result+='2'
        else:
            result+='0'
    return result

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

    #ai
def ai(board,player):
    if player.number==1:
        enemy=2
    else:
        enemy=1
    my_tuple=find_best(board,player.number)#自己的分数
    enemy_tuple=find_best(board,enemy)#对手的分数
    if my_tuple[2]*2.5>=enemy_tuple[2]:#进攻
        player.down(board,my_tuple[0],my_tuple[1])
    else:#防守
        player.down(board,enemy_tuple[0],enemy_tuple[1])

#在棋盘所有空位模拟落子并寻找得分最大的空位,返回（x,y,max_score)
def find_best(board,player_number):
    max_score=0
    for i in range(board.row):
        for j in range(board.column):
            if board.boa[i][j]!=1 and board.boa[i][j]!=2:
                score=0
                fake=copy.deepcopy(board.boa)#复制棋盘
                fake[i][j]=player_number#模拟落子
                #限定这次落子可能影响的范围
                up=max(i-5,0)
                down=min(i+5,board.row-1)
                left=max(j-5,0)
                right=min(j+5,board.column-1)
                #往4个角的长度
                leftup=min(j-left,i-up)
                leftdown=min(j-left,down-i)
                rightup=min(right-j,i-up)
                rightdown=min(right-j,down-i)

                u_d=l_r=lu_rd=ru_ld=''
                #将四个方向的棋子序列转化为字符串
                for k in range(up,down+1):#竖直方向
                    u_d+=str(fake[k][j])
                for k in range(left,right+1):#水平方向
                    l_r+=str(fake[i][k])
                for k in range(leftup,0,-1):#左上到右下
                    lu_rd+=str(fake[i-k][j-k])
                for k in range(rightdown+1):
                    lu_rd+=str(fake[i+k][j+k])
                for k in range(rightup,0,-1):
                    ru_ld+=str(fake[i-k][j+k])#右上到左下
                for k in range(leftdown+1):
                    ru_ld+=str(fake[i+k][j-k])

                #转化为point函数能处理的格式
                u_d=str_deal(u_d,player_number)
                l_r=str_deal(l_r,player_number)
                lu_rd=str_deal(lu_rd,player_number)
                ru_ld=str_deal(ru_ld,player_number)

                #计算总分数
                for k in (u_d,l_r,lu_rd,ru_ld):
                    score+=point(k)

                if score>=max_score:
                    max_score=score
                    x=j
                    y=i
    return (x,y,max_score)


# binarizes the image and does some cropping, uses erosion for edge detection. Does not analyze the bars

from PIL import Image, ImageFilter, ImageEnhance
import numpy as np
import matplotlib.pyplot as plt
import cv2
from collections import Counter

# General Approach and Methods:
## Binarize the image, increase / decrease contrast, increase / decrease brightness
## shape detection
## use the above shape detection to 'estimate' width value

# binarizeImg takes in an image and converts it to binary using a passed in threshold
def binarizeImg(thresh, baseImg, basePix):
  newImg = Image.new('L', (baseImg.width, baseImg.height)) # make a copy as to not change our original image
  newPix = newImg.load()
  for i in range(baseImg.width):
    for j in range(baseImg.height):
      newPix[i, j] = basePix[i, j] # copy our image

  for i in range(newImg.width):
    for j in range(newImg.height):
      if newPix[i, j] < thresh: # check against the threshold value
        newPix[i, j] = 0
      else:
        newPix[i, j] = 1
  return newImg

# function that increases the contrast of an image given an intensity
def adjustContrast(baseImg, intensity):
  contrast = ImageEnhance.Contrast(baseImg)
  contrast = contrast.enhance(intensity) # set FACTOR > 1 to enhance contrast, < 1 to decrease
  return contrast

# using a modifier, returns an image with a changed brightness setting
def adjustBrightness(baseImg, modifier):
  pixels = baseImg.load()
  for i in range(baseImg.width):
    for j in range(baseImg.height):
      newValue = pixels[i, j] + modifier
      if newValue > 255:
        pixels[i, j] = 255
      elif newValue < 0:
        pixels[i, j] = 0
      else:
        pixels[i, j] = newValue

  return baseImg

# calculates the min and max pxiels in an image, uses the difference to
# determine whether the quality of the image is good or poor
def imgIsGood(img, thresh):
  npArray = np.asarray(img) # convert to numoy array for easier calculation
  min = np.amin(npArray) # get the minimum pixel value
  max = np.amax(npArray) # get the maximum pixel value

  if min == 0 and max == 255: # if both min and max is 0 and 255, probably a digital barcode image
    return True
  else: # otherwise likely a photo of a bracode
    if (max - min) > thresh:
      return False
    else:
      return True

# SMOOTH AND SHARPEN KERNELS GRABBED FROM: https://www.taylorpetrick.com/blog/post/convolution-part3
def smoothImage(img):
  kernel = np.ones((4,4),np.float32)/25 # smooth kernel
  adjusted = cv2.filter2D(np.asarray(img),-1,kernel)
  return Image.fromarray(adjusted)

def sharpenImage(img):
  kernel = np.array(([0, -1, 0],[-1, 5, -1],[0, -1, 0]), dtype="int")
  adjusted = cv2.filter2D(np.asarray(img),-1,kernel)
  return Image.fromarray(adjusted)

def rotateImage(image, angle):
  imageCenter = tuple(np.array(image.shape[1::-1]) / 2)
  rotationMatrix = cv2.getRotationMatrix2D(imageCenter, angle, 1.0)
  result = cv2.warpAffine(image, rotationMatrix, image.shape[1::-1], flags=cv2.INTER_LINEAR)
  return result

# reverses the colors of a binary image
def inverseImage(arr):
  #return np.invert(arr)
  for i in range(len(arr)):
    for j in range(len(arr[i])):
      if arr[i, j] == 0:
        arr[i, j] = 1
      else:
        arr[i, j] = 0
  return arr

def detectBarCode(origImg, binImg):
  # finds all of the contours of the image of all detected shapes
  contours, _ = cv2.findContours(binImg,1,2)
  img = binImg # copy our binImg into a new variable to not change the original binarized version
  validBar = [] # valid bar consists of the coordinates of all rectangles detected if it is assumed to be a bar in the barcode
  maxAngle = 0
  for cnt in contours:
    (x, y, w, h) = cv2.boundingRect(cnt) # gets the x, y coords and size of the contour
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    if len(approx)==4: # if approx is 4, this contour is a rectangle / square

      # with x and y > 0, skip the edge of the image being considered a contour.
      # h and w calculations help assure that we only grab contours that are rectangles
      # in the barcode and not from anywhere else in the image
      if x > 0 and y > 0 and h > 30 and w < 70:
        # If all above are true, then this is probably a barcode rectangle. Add it as a valid contour.
        rotatedRect = cv2.minAreaRect(cnt)
        validBar.append((x, y, x + w, y + h))
        angle = rotatedRect[2] * -1 # stores the angle of the contour
        if angle > maxAngle and angle != -90:
          maxAngle = angle

  validNp = np.asarray(validBar) # make validBars into a numpy array
  left = np.min(validNp[:,0])
  top = np.min(validNp[:,1])
  right = np.max(validNp[:,2])
  bottom = np.max(validNp[:,3])

  drawBoundingBox(np.asarray(origImg), contours, (left, top, right, bottom))
  rotated = rotateImage(binImg, (maxAngle * -1) // 2) # use our maxAngle // 2 as the amount to rotate

  # convert to pillow image for simple cropping, then return back the numpy array value of the cropped image
  image = Image.fromarray(rotated)
  image = image.crop((left, top, right, bottom))
  return np.asarray(image)

def drawBoundingBox(image, contours, points):
  # following gets coordinates needed for cropping
  rectImage = np.asarray(image.copy())
  contImage = np.asarray(image.copy())

  cv2.drawContours(contImage, contours, -1, (255, 0, 0), 2)
  print("DETECTED CONTOURS")
  saveArrayAsImage("contours.jpg", contImage)

  cv2.rectangle(rectImage, (points[0], points[1]), (points[2], points[3]), (255, 0, 0), 3)
  print("DETECTED BARCODE REGION")
  saveArrayAsImage("region.jpg", rectImage * 255)

def saveArrayAsImage(filename, array):
  arrayAsImg = Image.fromarray(array)
  arrayAsImg.save('./results/' + filename)

# from: https://stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-list
def remove_values_from_list(the_list, val):
   return [value for value in the_list if value != val]

def get_four_most_common_list_values(my_list):
  most_common = Counter(my_list)
  most_common = most_common.most_common(4)
  to_return = []
  for i in most_common:
    to_return.append(i[0])
  return to_return

def convert_common_to_widths(my_list, most_common):
  new_list = []
  for item in range(len(my_list)):
    for common in range(len(most_common)):
      if my_list[item] == most_common[common]:
        new_list.append(common + 1) # bar width values can be 1, 2, 3, 4.
  return new_list

# follows the standard of 4 bar width values equaling one bar code digit. Sets this idea up.
def convert_widths_to_tuple(widths):
  tuple_list = []
  single_item = []
  counter = 0
  print(len(widths))
  for item in range(len(widths)):
    if item == counter + 4:
      tuple_list.append(single_item)
      single_item = []
      counter = item + 1
    else:
      single_item.append(widths[item])

  return tuple_list

# Uses the barcode values seen here: https://www.wikihow.com/Read-12-Digit-UPC-Barcodes
# converts the width values taken in the image and assign a numeric value
# uses the margin of error to assign a value even if a width value is slightly off
def assign_values_to_widths(widths, margin):
  actual_values = []
  width_values =	{
    3211: 0,
    2221: 1,
    2122: 2,
    1411: 3,
    1132: 4,
    1231: 5,
    1114: 6,
    1312: 7,
    1213: 8,
    3112: 9,
  }

  for width in widths:
    number = ''.join(map(str, width)) # convert the array of digits into a single number as a string
    number = int(number) # use a cast to convert our number string into an integer
    for value in width_values.keys():
      if number == value:
        actual_values.append(width_values[value])
      else:
        for m in range(number, number + margin): # account for margin of error in positive direction
          if m == value:
            actual_values.append(width_values[value])
            continue
        for m in range(number - margin, number): # account for margin of error in negative direction
          if m == value:
            actual_values.append(width_values[value])
            continue

  return actual_values

# takes in a numpy array representation of an image and attempts to analyze the barcode
def analyzeBarCode(image):
  middle = len(image) // 2
  values = [] # used to store the perceived numerical value of the barcodes
  count = 0
  currentBar = 0
  for pixel in range(len(image[middle])):
    pixelVal = image[middle][pixel]
    if pixel == 0: # if this is the first iteration
      currentBar = pixelVal
    else:
      if pixelVal == currentBar:
        count += 1
      else:
        currentBar = pixelVal
        values.append(count)
        count = 0

  values = remove_values_from_list(values, 0)
  common = get_four_most_common_list_values(values)
  print("common:", common)
  only_most_common = []
  #for ctr in range(len(values)):
   # if values[ctr] in common:
    #  only_most_common.append(values[ctr])

  widths = convert_common_to_widths(values, common)
  tuples = convert_widths_to_tuple(widths)
  print("tuples:", tuples)

  new_values = assign_values_to_widths(tuples, 20)
  print("new values:", new_values)

  return values

#### BEGIN MAIN ####

# load the barcode
imOrig = Image.open('barcode.png')
im = imOrig.convert('L') # convert to grayscale
im.save("./results/original.jpg")

erosionFactor = 0 # will change based on whether it is a photo or digital scan
# if image is not a digital scan of a barcode, meaning it is probably a photo, perform some cleanup.
if not imgIsGood(im, 100):
  print("This image seems to be a photo of a barcode, attempting to clean up and binarize.")
  im = adjustBrightness(im, -40) # increasing brightness seems to make fabrizzio's picture worse
  #im = adjustContrast(im, -100)
  #im = smoothImage(im)
  im = sharpenImage(im)
  saveArrayAsImage("enhanced.jpg", np.asarray(im))
  erosionFactor = 6
else:
  print("Image seems to be a digital scan. Going to binarize without cleaning up")
  erosionFactor = 3

# convert the image to binary, im will now hold the binarized pixels
im = binarizeImg(127, im, im.load())

# store im as a numpy array, makes it easier to perform operations
npBin = np.asarray(im)
# attempt to draw and crop the image to where the barcode is
npBin = detectBarCode(im, npBin)

# build a convolution kernel, and use it to generate the difference between the image and its eroded version
kernel = np.ones((erosionFactor, erosionFactor),np.uint8)
erosion = cv2.erode(npBin, kernel)

difference = npBin - erosion # use erosion for edge detection

## Save our final image to be used for analysis

difference = inverseImage(difference)

saveArrayAsImage("final.jpg", difference * 255)

data = analyzeBarCode(difference)
print("data:", data)


import discord

from config import DiscordConfig
from discord.ext import commands as cmds
from discord.utils import get
from typing import Union
import re

registers = ['zero','at','v0','v1','a0','a1','a2','a3','t0','t1','t2','t3','t4','t5','t6','t7','s0','s1','s2','s3','s4','s5','s6','s7','t8','t9','k0','k1','gp','sp','fp','ra']
formats = ['r','i1','i2','i3','j']

# Reusable regex patterns
comment_pattern = re.compile('\#.*')
breakpoint_inline_pattern = re.compile('^\s*(\w+)\:\s*\w+.*$') # Breakpoint is on the same line as the instruction
breakpoint_nextline_pattern = re.compile('^\s*(\w+)\:\s*$') # Breakpoint is only thing on the current line

r_pattern = lambda x: re.compile('^\s*' + x + '\s*\$(\w+)\s*,\s*\$(\w+)\s*,\s*\$(\w+)\s*$')
i1_pattern = lambda x: re.compile('^\s*' + x + '\s*\$(\w+)\s*,\s*(\-?\d+)\s*\(\s*\$(\w+)\s*\)\s*$')
i2_pattern = lambda x: re.compile('^\s*' + x + '\s*\$(\w+)\s*,\s*\$(\w+)\s*,\s*(\w+|\-?\d+)\s*$')
i3_pattern = lambda x: re.compile('^\s*' + x + '\s*\$(\w+)\s*,\s*\$(\w+)\s*,\s*(\w+|\-?\d+)\s*$')
j_pattern = lambda x: re.compile('^\s*' + x + '\s*(\w+|\d+)\s*$')

r_type_opcode = '000000'

commands = {
	'add': {
		'opcode': r_type_opcode,
		'funct': '100000',
		'shampt': '00000',
		'format': 'r'
	},
	'addi': {
		'opcode': '001000',
		'format': 'i3'
	},
	'and': {
		'opcode': r_type_opcode,
		'funct': '100100',
		'shampt': '00000',
		'format': 'r'
	},
	'beq': {
		'opcode': '000100',
		'format': 'i2'
	},
	'bne': {
		'opcode': '000101',
		'format': 'i2'
	},
	'j': {
		'opcode': '000010',
		'format': 'j'
	},
	'lw': {
		'opcode': '100011',
		'format': 'i1'
	},
	'or': {
		'opcode': r_type_opcode,
		'funct': '100101',
		'shampt': '00000',
		'format': 'r'
	},
	'slt': {
		'opcode': r_type_opcode,
		'funct': '101010',
		'shampt': '00000',
		'format': 'r'
	},
	'sw': {
		'opcode': '101011',
		'format': 'i1'
	},
	'sub': {
		'opcode': r_type_opcode,
		'funct': '100010',
		'shampt': '00000',
		'format': 'r'
	}
}

def to_binary(num: int, n: int = 5) -> str:
    '''
    Just convert an integer to binary. Unfortunately, converting a negative number
    invloves a slightly more complicated process since bin() doesn't work on
    negative numbers.
    '''

    if num < 0:
        # bin() of a negative number returns -thesignednum (bin(-5) => -0b101 instead of 1011)
        # Run the process for multiplying a positive number by -1
        result = bin(num)[3:].zfill(n)

        for i in range(len(result)): # Flip the bits
            if result[i] == '0':
                result = result[:i] + '1' + result[i+1:]
            else:
                result = result[:i] + '0' + result[i+1:]

        # Add 1
        carry = 0
        for i in range(len(result)-1,-1,-1):
            if result[i] == '0' and carry == 0:
                result = result[:i] + '1' + result[i+1:]
                break
            elif result[i] == '1' and carry == 0:
                carry = 1
                result = result[:i] + '0' + result[i+1:]
            elif result[i] == '0' and carry == 1:
                result = result[:i] + '1' + result[i+1:]
                break
            elif result[i] == '1' and carry == 1:
                result = result[:i] + '0' + result[i+1:]
        return result
    else:
        return f'{num:b}'.zfill(n) + ' '

def is_int(s):
    '''
    Check if a string is an integer
    '''

    if len(s) < 1:
        return False
    if s[0] == "-":
        return s[1:].isdigit()
    return s.isdigit()

def translate(instructions):
    '''
    Converts a list of mips instructions into a list of binary strings.
    Returns a list of size 2. The first element is either a 0 or 1.
    0 indicates the program worked and the correct output is in the second element.
    1 indicates the code wasn't properly formated and an error message is the second element.
    '''

    lines = []
    line_map = {} # Map the original instructions to the new lines in the lines list
    line_num = 1 # Variable for keeping track of errors
    current_line = 0 # The index of the current instruction being parsed

    # Find all breakpoints before parsing the commands
    for line in instructions:
        line = re.sub(comment_pattern, '', line) # Remove comments

        # Ignore empty lines
        if line.strip() == '':
            continue

        inline = re.search(breakpoint_inline_pattern, line)

        if inline:
            breakpoint = inline.groups()[0]
            line_map[breakpoint] = current_line
        else:
            nextline = re.search(breakpoint_nextline_pattern, line)
            if nextline:
                breakpoint = nextline.groups()[0]
                line_map[breakpoint] = current_line
                continue

        current_line += 1

    current_line = 0
    for line in instructions:
        unknown_command = True
        line = re.sub(comment_pattern, '', line)

        if line.strip() == '':
            line_num += 1
            continue

        inline = re.search(breakpoint_inline_pattern, line)

        if inline:
            line = re.sub(f'{breakpoint}:', '', line)
        else:
            nextline = re.search(breakpoint_nextline_pattern, line)
            if nextline:
                line_num += 1
                continue

        for key in commands:
            command = commands[key]

            f = command['format'] # format is a Python3 keyword

            if f == 'r':
                pattern = r_pattern(key)
            elif f == 'i1':
                pattern = i1_pattern(key)
            elif f == 'i2':
                pattern = i2_pattern(key)
            elif f == 'i3':
                pattern = i3_pattern(key)
            elif f == 'j':
                pattern = j_pattern(key)

            r = re.search(pattern, line) # Check for an existing pattern
            if r:
                result = command['opcode'] # Line always starts with the opcode
                args = r.groups() # Get the arguments

                if f == 'r':
                    for arg in args:
                        if not arg in registers:
                            return 1, f'Unknown register {arg} on line {line_num}'

                    rs = registers.index(args[1])
                    rt = registers.index(args[2])
                    rd = registers.index(args[0])

                    result += to_binary(rs) + to_binary(rt) + to_binary(rd) + command['shampt'] + command['funct']
                    lines.append(result)

                elif f == 'i1':
                    if not args[0] in registers:
                        return 1, f'unknown register {args[0]} on line {line_num}'
                    if not args[2] in registers:
                        return 1, f'unknown register {args[2]} on line {line_num}'

                    rs = registers.index(args[2])
                    rt = registers.index(args[0])

                    immed = int(args[1])
                    result += to_binary(rs) + to_binary(rt) + to_binary(immed, 16)
                    lines.append(result)

                elif f == 'i2':
                    if not args[0] in registers:
                        return 1, f'unknown register {args[0]} on line {line_num}'
                    if not args[1] in registers:
                        return 1, f'unknown register {args[1]} on line {line_num}'

                    rs = registers.index(args[0])
                    rt = registers.index(args[1])

                    immed = args[2]

                    result += to_binary(rs) + to_binary(rt)

                    if is_int(immed): # Check if integer
                        result += to_binary(int(immed), 16)
                    elif immed in line_map:
                        result += to_binary(line_map[immed]-current_line-1, 16) # -1 index because the PC automatically increments (+1 index)
                    else:
                        return 1, 'breakpoint {immed} on line {line_num} does not exist'

                    lines.append(result)

                elif f == 'i3':
                    if not args[0] in registers:
                        return 1, f'unknown register {args[0]} on line {line_num}'
                    if not args[1] in registers:
                        return 1, f'unknown register {args[1]} on line {line_num}'

                    rs = registers.index(args[1])
                    rt = registers.index(args[0])
                    immed = args[2]

                    result += to_binary(rs) + to_binary(rt)

                    if is_int(immed):
                        result += to_binary(int(immed), 16)
                    elif immed in line_map:
                        result += to_binary(line_map[immed] - current_line - 1, 16)
                    else:
                        return 1, f'breakpoint {immed} on line {line_num} does not exist'

                    lines.append(result)

                elif f == 'j':
                    addr = args[0]

                    if is_int(addr):
                        result += to_binary(int(addr), 26)
                    elif addr in line_map:
                        result += to_binary(line_map[addr], 26)
                    else:
                        return 1, f'breakpoint {addr} on line {line_num} does not exist'

                    lines.append(result)

                unknown_command = False
                break

        if unknown_command:
            return 1, f'Unknown pattern on line {line_num}'

        line_num += 1
        current_line += 1

    return 0, lines

def setup(bot: object) -> None:
    bot.add_cog(FunThings(bot))

class FunThings(cmds.Cog, name = 'FunThings'):
    def __init__(self, bot):
        self.bot = bot

    @cmds.command(name = 'mips_to_bin')
    async def mips_to_bin(self, ctx, *mips: str):
        mips_string = ' '.join(mips)

        error, result = translate([mips_string])

        if not error:
            result = result[0] # only the first line

            embed = discord.Embed(title = 'Converted MIPS', colour = discord.Colour.green())
            embed.add_field(name = 'Binary', value = result)

            binary_string = result.replace(' ', '')
            binary = int(binary_string, 2)
            h = hex(binary)

            embed.add_field(name = 'Hex', value = str(h))

            await ctx.send(embed = embed)

        else:
            await ctx.send(result)


import pykube
api = pykube.HTTPClient(pykube.KubeConfig.from_file("./kubconfig"))
obj = {
    "apiVersion": "v1",
    "kind": "ReplicationController",
    "metadata": {
        "name": "my-rc",
        "namespace": "default"
    },
    "spec": {
        "replicas": 3,
        "selector": {
            "app": "nginx"
        },
        "template": {
            "metadata": {
                "labels": {
                    "app": "nginx"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "nginx",
                        "image": "nginx",
                        "ports": [
                            {"containerPort": 80}
                        ]
                    }
                ]
            }
        }
    }
}
#创建rc
#pykube.ReplicationController(api, obj).create()

obj = {
    "apiVersion": "v1",
    "kind": "ReplicationController",
    "metadata": {
        "name": "my-rc",
        "namespace": "default"
    }
}
#删除rc
pykube.ReplicationController(api, obj).delete()


import sklearn
from sklearn import svm
from sklearn import neural_network
import numpy as np
from sklearn.model_selection import KFold
from sklearn import datasets
import matplotlib.pyplot as plt

# X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# y = np.array([0, 1, 1, 0])

iris = datasets.load_iris()
X = iris.data
y = iris.target

# model = svm.SVC()
model   = sklearn.neural_network.MLPClassifier()

# def calc_accuracy(y, y_):

def run(model, X, y, title="", n_splits=10, random_state=None):
    print("====== %s ======" % title)
    kf = KFold(n_splits, random_state=random_state, shuffle=True)
    train_accuracy_sum = 0.0
    test_accuracy_sum = 0.0
    sum_count    = 0
    for train_index, test_index in kf.split(X):
        sum_count += 1
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
        model.fit(X_train, y_train)
        train_pred      = model.predict(X_train)
        test_pred       = model.predict(X_test)
        train_accuracy  = sklearn.metrics.accuracy_score(y_train, train_pred)
        test_accuracy   = sklearn.metrics.accuracy_score(y_test, test_pred)
        train_accuracy_sum += train_accuracy
        test_accuracy_sum  += test_accuracy
        # print("true y: %s " % y_test)
        # print("predict y: %s " % y_)
        try:
            print("n_support_:", model.n_support_)
        except:
            pass
        print("Train Accuracy: %f" % train_accuracy, "Test Accuracy: %f" % test_accuracy)

    train_average_accuracy = train_accuracy_sum / sum_count
    test_average_accuracy  = test_accuracy_sum / sum_count
    print("[%s] Average Train Accuracy: %f" % (title, train_average_accuracy))
    print("[%s] Average Test Accuracy: %f" % (title, test_average_accuracy))
    return test_average_accuracy

if False:
    iris = datasets.load_iris()
    run(
        svm.SVC(random_state=931),
        iris.data,
        iris.target,
        title="Non-Linear MLP",
        random_state = 101
    )

    run(
        sklearn.neural_network.MLPClassifier(random_state=3290),
        iris.data,
        iris.target,
        title="MLP",
        random_state=101
    )

if False:
    run(
        sklearn.neural_network.MLPClassifier(random_state=3290),
        features,
        labels,
        title="MLP",
        random_state=101
    )

if True:
    # print('./multiplication-reference-feature/f5_reference_all_mult_%.1f.csv' % 0.9)
    # exit()

    algorithms = ["f5", "jstego", "outguess"]
    for algorithm in algorithms:
        embed_rates       = []
        average_accuracys = []
        take_num          = -1

        for embed_rate in np.arange(1, 10 +1) / 10:
            print("Loading(%.1f)..." % embed_rate)
            features = []
            labels   = []
            with open('./%s-multiplication-reference-feature/%s_reference_all_mult_0.0.csv' % (algorithm, algorithm), 'r') as f:
                for line in f:
                    feature = np.array([float(e) for e in line.split(',')])
                    features.append(feature[:take_num])
                    labels.append(0)

            with open('./%s-multiplication-reference-feature/%s_reference_all_mult_%.1f.csv' % (algorithm, algorithm, embed_rate), 'r') as f:
                for line in f:
                    feature = np.array([float(e) for e in line.split(',')])
                    features.append(feature[:take_num])
                    labels.append(1)

            features = np.array([f / np.linalg.norm(f) for f in features])
            # features = features / np.max(np.abs(features))
            labels   = np.array(labels)
            print("Loaded!")

            print('len(features):', len(features))
            average_accuracy = run(
                # svm.SVC(random_state=3290),
                sklearn.neural_network.MLPClassifier(hidden_layer_sizes = (100,)),
                features,
                labels,
                title="MLP - %s" % algorithm,
                random_state=101
            )
            embed_rates.append(embed_rate)
            average_accuracys.append(average_accuracy)

        print(embed_rates)
        print(average_accuracys)

        plt.plot(embed_rates, average_accuracys, label = algorithm)

    plt.legend()
    plt.title("MLP h5")
    plt.xlabel("embed rate(%)")
    plt.ylabel("Accuracy(%)")
    plt.show()


# -*- coding: utf-8 -*-
"""
Created on Sat May 29 18:20:21 2021

@author: User
"""

import socket
import dataCollection as dc
import time

host_ip = '192.168.1.3'
socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('localhost', 10000)
bufferSize = 4096

while True:
    time.sleep(10)
    try:
        now = time.time()
        file = open("Device_3.txt", "w+")
        file.write(dc.generateDatagram(dc.collectData(), host_ip))
        file.seek(0)
        sent = socket.sendto(file.read().encode(), server_address)
        file.close()
        print("Size of transmission buffer: %d" %bufferSize)
        data, server = socket.recvfrom(bufferSize)
        print ('received message "%s"' % data.decode('utf8'))
        print("Transmission took %g seconds" %(time.time() - now))
    except Exception as info :
        print(info)


# one two three four five six seven eight nine ten
# eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
# twenty thirty forty fifty sixty seventy eighty ninety
# hundred

under_20 = (0, 3, 3, 5, 4, 4, 3, 5, 5, 4, 3, 6, 6, 8, 8, 7, 7, 9, 8, 8)
tys = (0, 0, 6, 6, 5, 5, 5, 7, 6, 6)
def cal(n):
    if n == 0:
        return 0
    if n < 20:
        return under_20[n]
    if n == 1000:
        return 11
    elif n >= 100:
        t = int(n / 100)
        w = cal(n % 100)
        if w == 0:
            return under_20[t] + 7
        else:
            return under_20[t] + 10 + w
    else:
        t = int(n / 10)
        return tys[t] + cal(n % 10)

sum = 0
for i in range(1, 1001):
    sum += cal(i)

print(sum)


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

import cv2
import os
from time import time

import torch
import torch.nn as nn
import torch.utils.data as data
from torch.autograd import Variable as V

import torchvision.transforms.functional as F

import torchvision
from torchvision import transforms

from networks.cenet import CE_Net_
from framework import MyFrame
from loss import dice_bce_loss
from data import ImageFolder
from Visualizer import Visualizer
from PIL import Image

import Constants
import image_utils

# Please specify the ID of graphics cards that you want to use
os.environ['CUDA_VISIBLE_DEVICES'] = "0"

def CE_Net_Train():
    NAME = 'CE-Net' + Constants.ROOT.split('/')[-1]

    # run the Visdom
    viz = Visualizer(env=NAME)

    solver = MyFrame(CE_Net_, dice_bce_loss, 2e-4)
    print("count",Constants.BATCHSIZE_PER_CARD)
    batchsize = torch.cuda.device_count() * Constants.BATCHSIZE_PER_CARD
    print("batchsize",batchsize)

    # For different 2D medical image segmentation tasks, please specify the dataset which you use
    # for examples: you could specify "dataset = 'DRIVE' " for retinal vessel detection.

    dataset = ImageFolder(root_path=Constants.ROOT, datasets='DRIVE')
    data_loader = torch.utils.data.DataLoader(
        dataset,
        batch_size=batchsize,
        shuffle=True,
        num_workers=4)

    # start the logging files
    mylog = open('logs/' + NAME + '.log', 'w')
    tic = time()

    no_optim = 0
    total_epoch = Constants.TOTAL_EPOCH
    train_epoch_best_loss = Constants.INITAL_EPOCH_LOSS
    for epoch in range(1, total_epoch + 1):
        data_loader_iter = iter(data_loader)
        train_epoch_loss = 0
        index = 0

        for img, mask in data_loader_iter:
            solver.set_input(img, mask)
            train_loss, pred = solver.optimize()
            train_epoch_loss += train_loss
            index = index + 1

        # show the original images, predication and ground truth on the visdom.
        show_image = (img + 1.6) / 3.2 * 255.
        viz.img(name='images', img_=show_image[0, :, :, :])
        viz.img(name='labels', img_=mask[0, :, :, :])
        viz.img(name='prediction', img_=pred[0, :, :, :])

        torchvision.utils.save_image(img[0, :, :, :], "test_data/results2/image_"+str(epoch) + ".jpg", nrow=1, padding=2, normalize=True, range=None, scale_each=False, pad_value=0)
        torchvision.utils.save_image(mask[0, :, :, :], "test_data/results2/mask_"+str(epoch) + ".jpg", nrow=1, padding=2, normalize=True, range=None, scale_each=False, pad_value=0)
        torchvision.utils.save_image(pred[0, :, :, :], "test_data/results2/pred_"+str(epoch) + ".jpg", nrow=1, padding=2, normalize=True, range=None, scale_each=False, pad_value=0)

        # x = torch.tensor([[1,2,3],[4,5,6]], dtype = torch.uint8)
        # x = show_image[0,:,:,:]
        # print(x.shape)
        # pil_im = transforms.ToPILImage(mode = 'RGB')(x)
        # pil_im.save('/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/image_' + str(epoch) +  '.jpg')

        # x = mask[0,:,:,:]
        # print(x.shape)
        # pil_im = transforms.ToPILImage(mode = 'L')(x)
        # pil_im.save('/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/mask_' + str(epoch) +  '.jpg')

        # x = pred[0,:,:,:]
        # print(x.shape)
        # pil_im = transforms.ToPILImage(mode = 'HSV')(x.detach().cpu().numpy())
        # pil_im.save('/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/prediction_' + str(epoch) +  '.jpg')
        # (x.detach().numpy()).save("/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/image_" + str(epoch) + ".png")
        # cv2.imwrite('imagename.jpg', x.detach().numpy().astype('uint8')).transpose(2,1,0)
        # x = mask[0,:,:,:]
        # # F.to_pil_image(x.detach().numpy()).save("/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/mask_" + str(epoch) + ".png")
        # x = pred[0,:,:,:]
        # print(x.shape)
        # cv2.imwrite('imagename2.jpg', x.detach().numpy().astype('uint8'))

        # F.to_pil_image(x.detach().numpy()).save("/home/videsh/Downloads/Chandan/paper_implementation/CE-Net-master/images/prediction_" + str(epoch) + ".png")
        print("saving images")

        train_epoch_loss = train_epoch_loss/len(data_loader_iter)
        print(mylog, '********')
        print(mylog, 'epoch:', epoch, '    time:', int(time() - tic))
        print(mylog, 'train_loss:', train_epoch_loss)
        print(mylog, 'SHAPE:', Constants.Image_size)
        print('********')
        print('epoch:', epoch, '    time:', int(time() - tic))
        print('train_loss:', train_epoch_loss)
        print('SHAPE:', Constants.Image_size)

        # if train_epoch_loss >= train_epoch_best_loss:
        #     no_optim += 1
        # else:
        #     no_optim = 0
        #     train_epoch_best_loss = train_epoch_loss
        #     solver.save('./weights/' + NAME + '.th')
        # if no_optim > Constants.NUM_EARLY_STOP:
        #     print(mylog, 'early stop at %d epoch' % epoch)
        #     print('early stop at %d epoch' % epoch)
        #     break
        if no_optim > Constants.NUM_UPDATE_LR:
            if solver.old_lr < 5e-7:
                break
            solver.load('./weights/' + NAME + '.th')
            print("loading the weights")
            solver.update_lr(2.0, factor=True, mylog=mylog)
        mylog.flush()

    print(mylog, 'Finish!')
    print('Finish!')
    mylog.close()

if __name__ == '__main__':
    print(torch.__version__)
    CE_Net_Train()


from django.conf.urls import url
from .views import (ItemListView, item_list_view, ItemDetailView, item_detail_view,ItemFeaturedView,
ItemFeaturedDetailView,ItemDetailSlugView)

"""
urlpatterns = [
    url(r'^$', ItemListView.as_view()),
    url(r'^featureditems/$', ItemFeaturedView.as_view()),
    url(r'^featureditems/(?P<pk>\d+)/$', ItemFeaturedDetailView.as_view()),
    url(r'^items-fbv/$', item_list_view),
    url(r'^(?P<pk>\d+)/$', ItemDetailView.as_view()),
    url(r'items-fbv/(?P<id>\d+)/$', item_detail_view),
    url(r'^(?P<slug>[\w-]+)/$', ItemDetailSlugView.as_view()),

]
"""
urlpatterns = [
    url(r'^$', ItemListView.as_view(), name='list'),
    url(r'^(?P<slug>[\w-]+)/$', ItemDetailSlugView.as_view(), name='detail'),
]


import numpy as np

def yaw_pitch2vec(yp):
    yaw, pitch = yp[0], yp[1]
    y = -np.sin(pitch)
    proj_zox = np.cos(pitch)

    z = -np.cos(yaw) * proj_zox
    x = -np.sin(yaw) * proj_zox

    return np.array([x, y, z])


# coding: utf-8

#input
N = int(input())
index = [i for i in range(N)]
mat = []
for i in index:
    tmp = input().split(" ")
    tmp = list(map(float, tmp))
    mat.append(tmp)

#前進消去
for i in index:
    inv = 1 / mat[i][i]
    for j in range(N + 1):
        mat[i][j] *= inv
    for h in range(i + 1, N):
        p = 0 - mat[h][i]
        for w in range(N + 1):
            mat[h][w] += p * mat[i][w]

#後退代入
inv_i = list(index) #リストの値渡し
inv_i.reverse()
x = []
x.append(mat[N - 1][N])
for i in range(N - 1):
    x.append(mat[inv_i[i + 1]][N])
    for j in range(i + 1):
        x[i + 1] -= mat[inv_i[i + 1]][N - j - 1] * x[j]

x.reverse()
for i in range(N):
    print("x" + str(i + 1) + " = " + str(x[i]))


from kzpy3.vis import *
import rospy
import rosbag
import cv2
from cv_bridge import CvBridge,CvBridgeError
import threading

bridge = CvBridge()

image_topics = ['left_image','right_image']
single_value_topics = ['steer','state','motor','encoder','GPS2_lat']
vector3_topics = ['acc','gyro','gyro_heading']#,'gps']
camera_sides = ['left','right']

import kzpy3.data_analysis.aruco_annotator as ann

def multi_preprocess(A,bag_folder_path,bagfile_range=[]):

    bag_files = sorted(gg(opj(bag_folder_path,'*.bag')))
    if len(bagfile_range) > 0:
        bag_files = bag_files[bagfile_range[0]:(bagfile_range[1]+1)]

    threading.Thread(target=multi_preprocess_thread,args=[A,bag_files]).start()

def multi_preprocess_thread(A,bag_files):
    for b in bag_files:
        if A['STOP_LOADER_THREAD']:
            A['STOP_LOADER_THREAD'] = False
            print('Stopping multi_preprocess_thread.')
            break
        preprocess(A,b)

def get_new_A(_=None):
    A = {}
    A['current_img_index'] = 0
    A['STOP_LOADER_THREAD'] = False
    A['STOP_ANIMATOR_THREAD'] = False
    A['STOP_GRAPH_THREAD'] = False
    A['d_indx'] = 1.0
    A['t_previous'] = 0
    A['left_deltas'] = []
    return A

def preprocess(A,path):
    timer = Timer(0)

    for topic in image_topics + single_value_topics:
        if topic not in A:

            A[topic] = []
    for topic in vector3_topics:
        if topic+'_x' not in A:
            A[topic+'_x'] = []
            A[topic+'_y'] = []
            A[topic+'_z'] = []

    if True:#try:
        cprint('Loading bagfile '+path,'yellow')

        bag = rosbag.Bag(path)

        for topic in single_value_topics:
            for m in bag.read_messages(topics=['/bair_car/'+topic]):
                t = round(m.timestamp.to_time(),3) # millisecond resolution
                if not isinstance(m[1].data,(int,long,float)):
                    print("if not isinstance(m[1].data,(int,long,float)):")
                    print(d2s("m[1].data = ",m[1].data))
                    assert(False)
                #A[topic][t] = m[1].data
                A[topic].append([t,m[1].data])

        for topic in vector3_topics:
            if topic != 'gps':
                for m in bag.read_messages(topics=['/bair_car/'+topic]):
                    t = round(m.timestamp.to_time(),3)
                    if not isinstance(m[1].x,(int,long,float)):
                        print("if not isinstance(m[1].x,(int,long,float)):")
                        print(d2s("m[1].x = ",m[1].x))
                        assert(False)
                    if not isinstance(m[1].y,(int,long,float)):
                        print("if not isinstance(m[1].y,(int,long,float)):")
                        print(d2s("m[1].y = ",m[1].y))
                        assert(False)
                    if not isinstance(m[1].z,(int,long,float)):
                        print("if not isinstance(m[1].x,(int,long,float)):")
                        print(d2s("m[1].z = ",m[1].z))
                        assert(False)
                    A[topic+'_x'].append([t,m[1].x])
                    A[topic+'_y'].append([t,m[1].y])
                    A[topic+'_z'].append([t,m[1].z])
        """
        try:
            topic = 'gps'
            for m in bag.read_messages(topics=['/bair_car/'+topic]):
                t = round(m.timestamp.to_time(),3)
                A[topic].append([t,[m[1].latitude,m[1].longitude,m[1].altitude]])
        except:
            print 'gps problem'
        """
        color_mode = "rgb8"

        for s in camera_sides:
            for m in bag.read_messages(topics=['/bair_car/zed/'+s+'/image_rect_color']):
                t = round(m.timestamp.to_time(),3)
                if A['t_previous'] > 0:
                    if s == 'left':
                        A['left_deltas'].append([t,t-A['t_previous']])
                A['t_previous'] = t

                A[s+'_image'].append([t,bridge.imgmsg_to_cv2(m[1],color_mode)])

    #except Exception as e:
    #    print e.message, e.args

    print(d2s('Done in',timer.time(),'seconds'))
    """
    A['state'] = array(A['state'])
    A['steer'] = array(A['steer'])
    A['motor'] = array(A['motor'])
    A['gyro_x'] = array(A['gyro_x'])
    A['gyro_y'] = array(A['gyro_y'])
    A['gyro_z'] = array(A['gyro_z'])
    A['acc_x'] = array(A['acc_x'])
    A['acc_y'] = array(A['acc_y'])
    A['acc_z'] = array(A['acc_z'])
    """

"""
import kzpy3.teg9.data.bag as bag
reload(bag)
A = {}
A['current_img_index'] = 0
bag.preprocess(A,'/media/karlzipser/ExtraDrive3/from_Mr_Yellow/Mr_Yellow_Fern_14April2017/direct_Fern_aruco_1_14Apr17_22h19m52s_Mr_Yellow/bair_car_2017-04-14-22-23-05_6.bag' )

bag.animate(A)
"""

hist_timer = Timer(5)

def graph_thread(A):

    figure('MSE')
    clf()
    indx = int(A['current_img_index'])
    if indx < 0:
        indx = 0
    elif indx >= len(A['left_image']):
        indx = len(A['left_image'])-1
    while True:
        while A['STOP_GRAPH_THREAD'] == False:
            if len(A['left_image']) < 30:
                #print("""'if len(A['left_image']) < 30:'""")
                pause(0.1)
                continue
            A_len = len(A['left_image'])
            clf()

            steer = array(A['steer'])
            plot(steer[:,0]-steer[0,0],steer[:,1])
            t = A['left_image'][indx][0]
            xlim(t-20-steer[0,0],t-steer[0,0])
            motor = array(A['motor'])
            state = array(A['state'])*10
            plot(motor[:,0]-motor[0,0],motor[:,1])
            plot(state[:,0]-state[0,0],state[:,1])
            ylim(-1,100)
            left_deltas = array(A['left_deltas'])
            states = array(A['state'])[:,1]

            if hist_timer.check():
                figure('left_deltas')
                hist(left_deltas[:,1])
                figure('states')
                hist(states)
                hist_timer.reset()

            figure('MSE')

            #plot(left_deltas[:,0]-left_deltas[0,0],100*left_deltas[:,1])

            """
            plot(A['motor'][:,0]-A['motor'][0,0],A['motor'][:,1])
            plot(A['steer'][:,0]-A['steer'][0,0],A['steer'][:,1])

            figure('gyro')
            clf()
            plot(A['gyro_x'][:,0]-A['gyro_x'][0,0],A['gyro_x'][:,1])
            plot(A['gyro_y'][:,0]-A['gyro_y'][0,0],A['gyro_y'][:,1])
            plot(A['gyro_z'][:,0]-A['gyro_z'][0,0],A['gyro_z'][:,1])

            figure('acc')
            clf()
            plot(A['acc_x'][:,0]-A['acc_x'][0,0],A['acc_x'][:,1])
            plot(A['acc_y'][:,0]-A['acc_y'][0,0],A['acc_y'][:,1])
            plot(A['acc_z'][:,0]-A['acc_z'][0,0],A['acc_z'][:,1])
            """
            pause(0.001)
            while A_len == len(A['left_image']):
                indx = int(A['current_img_index'])
                if indx < 0:
                    indx = 0
                elif indx >= len(A['left_image']):
                    indx = len(A['left_image'])-1
                t = A['left_image'][indx][0]
                xlim(t-20-steer[0,0],t-steer[0,0])
                pause(0.2)
        pause(0.2)

def animator_thread(A):
    while True:
        while A['STOP_ANIMATOR_THREAD'] == False:
            if len(A['left_image']) < 30:
                time.sleep(0.1)
                continue

            indx = int(A['current_img_index'])
            if indx < 0:
                indx = 0
            elif indx >= len(A['left_image']):
                indx = len(A['left_image'])-1

            a = A['left_image'][indx]
            img = a[1]#ann.get_aruco_image(a[1],filled=True)
            mi_or_cv2(img,cv=True,delay=30,title='animate')

            A['current_img_index'] += A['d_indx']
            if A['current_img_index'] >= len(A['left_image']):
                A['current_img_index'] = len(A['left_image'])-1
        time.sleep(0.2)

def start_graph(A):
    A['STOP_GRAPH_THREAD'] = False

def stop_graph(A):
    A['STOP_GRAPH_THREAD'] = True
def start_animation(A):
    A['STOP_ANIMATOR_THREAD'] = False

def stop_animation(A):
    A['STOP_ANIMATOR_THREAD'] = True
def stop_loader(A):
    A['STOP_LOADER_THREAD'] = True

def menu(A):
    menu_functions = ['exit_menu','start_animation','start_graph','stop_animation','stop_graph','stop_loader','get_new_A']
    while True:
        for i in range(len(menu_functions)):
            print(d2n(i,') ',menu_functions[i]))
        try:
            choice = input('> ')
            if type(choice) == int:
                if choice == 0:
                    return
                if choice >-1 and choice < len(menu_functions):
                    exec_str = d2n(menu_functions[choice],'(A)')
                    exec(exec_str)
        except:
            pass

A = {}
A = get_new_A(A)
A['d_indx']=4.0

#import kzpy3.teg9.data.bag as bag
#reload(bag)

#bag_folder_path='/media/karlzipser/ExtraDrive1/bair_car_data_10/caffe_direct_Smyth_tape_16Jan17_16h52m24s_Mr_Silver'
#bag_folder_path='/media/karlzipser/ExtraDrive3/from_Mr_Blue/Mr_Blue_Fern_14April2017/processsed/direct_Fern_aruco_1_14Apr17_13h45m40s_Mr_Blue'

bag_folder_path = sys.argv[1]
multi_preprocess(A,bag_folder_path)#,[1,3])

threading.Thread(target=animator_thread,args=[A]).start()
threading.Thread(target=graph_thread,args=[A]).start()

#menu(A)

"""
import kzpy3.teg9.data.bag as bag
#reload(bag)
A = bag.get_new_A()
bag_folder_path='/media/karlzipser/ExtraDrive1/bair_car_data_10/caffe_direct_Smyth_tape_16Jan17_16h52m24s_Mr_Silver'
bag_folder_path='/media/karlzipser/ExtraDrive1/bair_car_data_10/direct_Smyth_tape_25Jan17_19h24m14s_Mr_Yellow'
bag.multi_preprocess(A,bag_folder_path,[1,3])

bag.menu(A)
"""