import discord
import requests
from discord.ext import commands

WILSIM_BASE_URL = 'https://lineagenode-tz18.c9users.io/WILSIM/'

WILSIM_ADOPT = '/adopt'

WILSIM_STATUS = '/status'

WILSIM_PAINT = '/paint'

WILSIM_FEED = '/feed'

WILSIM_PLAY = '/play'

async def interact(ctx, action):
    """Generic interaction function"""

    wilsim_interaction_url = WILSIM_BASE_URL + str(ctx.guild.id) + "/1" + action

    wilsim_interaction = requests.get(wilsim_interaction_url)

    print(wilsim_interaction_url)

    if wilsim_interaction.status_code is 200:
        await ctx.send(wilsim_interaction.json()['flavortext'])

async def check_status(ctx):
    """Checks WilSim's status. Returns a Requests object."""
    status_url = WILSIM_BASE_URL + str(ctx.guild.id) + "/1" + WILSIM_STATUS
    wilsim_status = requests.get(status_url)

    print(status_url)

    return wilsim_status

async def format_status(ctx):
    """Creates a Discord formatted status for WilSim."""

    wilsim_status = await check_status(ctx)

    if wilsim_status.status_code is 200:

        name = wilsim_status.json()['name']
        hunger = wilsim_status.json()['hunger']
        thirst = wilsim_status.json()['thirst']
        painted = wilsim_status.json()['painted']

        message = """```Name: {0}\nHunger: {1}\nThirst: {2}\nPaint Level: {3}/100```""".\
            format(name, hunger, thirst, painted)

        return message
    else:
        return "We could not find a registered pet!"

class WilSimCore(commands.Cog):
    """TranslateCog"""

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

    @commands.command(name='adopt')
    async def adopt(self, ctx):

        wilsim_status = await check_status(ctx)

        if wilsim_status.status_code is 200:
            await ctx.send("You have already registered WilSim to this server!")
            return

        if wilsim_status.status_code is not 200:
            register_url = WILSIM_BASE_URL + str(ctx.guild.id) + "/1" + WILSIM_ADOPT

            wilsim_register = requests.get(register_url)
            print(register_url)

            if wilsim_register.status_code is 200:
                await ctx.send("Congratulations, {0}! You have successfully adopted {1}!".format(
                                ctx.guild.name, wilsim_register.json()['name']))

    @commands.command(name='status')
    async def status(self, ctx):

        wilsim_status_message = await format_status(ctx)

        await ctx.send(wilsim_status_message)

    @commands.command(name='paint')
    async def paint(self, ctx):
        """Paint Wilsim"""
        await interact(ctx, WILSIM_PAINT)

    @commands.command(name='feed')
    async def feed(self, ctx):
        """Feed Wilsim"""
        await interact(ctx, WILSIM_FEED)

    @commands.command(name='play')
    async def play(self, ctx):
        """Play with Wilsim"""
        await interact(ctx, WILSIM_PLAY)

def setup(bot):
    bot.add_cog(WilSimCore(bot))
    print("WilSimCore cog loaded.")


"""
Constraint Satisfaction Problem

Artificial Intellegence
Orebro University

Tobias Lans (lanstobias@gmail.com)
Bobo Lillqvist (bobo.lillqvist@gmail.com)

Last modified
"""

class CSP:
    """ Constraint satisfaction class. """
    wordlist = ["ADD", "ADO", "AGE", "AGO", "AID", "AIL", "AIM", "AIR", "AND", "ANY", "APE", "APT",
                "ARC", "ARE", "ARK", "ARM", "ART", "ASH", "ASK", "AUK", "AWE", "AWL", "AYE", "BAD",
                "BAG", "BAN", "BAT", "BEE", "BOA", "EAR", "EEL", "EFT", "FAR", "FAT", "FIT", "LEE",
                "OAF", "RAT", "TAR", "TIE"]

    # a1 = wordlist
    # a2 = wordlist
    # a3 = wordlist
    # d1 = wordlist
    # d2 = wordlist
    # d3 = wordlist

def main():
    """ Main function. """

    print(CSP.wordlist[0][0])

main()


import timeit

# find the sum of all numbers which are multiples of 3 or 5 below 1000
def mult_3_or_5():
    ret = 0
    for i in range(1, 1000):
        if not i%5 or not i%3:
            ret += i
    return ret

# this function performs nearly twice as fast as the first
def mult_3_or_5_alt():
    set3 = set()
    set5 = set()
    count3 = 3
    count5 = 5
    while count5 < 1000:
        set3.add(count3)
        set5.add(count5)
        count3 += 3
        count5 += 5
    while count3 < 1000:
        set3.add(count3)
        count3 += 3
    return sum(set3 - set5) + sum(set5)

if __name__ == '__main__':
    print(timeit.Timer(stmt=mult_3_or_5).timeit(number=10))
    print(timeit.Timer(stmt=mult_3_or_5_alt).timeit(number=10))


#!/bin/python
# -*- coding: utf-8 -*-

"""
https://www.hackerrank.com/challenges/python-loops
"""

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = input()
for i in range(0, n):
    print (i * i)


import sys
import operator
import random

import monster
import monster_template
#import item
#import item_template
import level_template

import map as _map

import term
import dice
import cells

OPAQUE_FLAG   = (1<<0)
WALKABLE_FLAG = (1<<1)
MONSTER_FLAG  = (1<<2)

class Level(object):

  def __init__(self, template):
    if isinstance(template, str):
      template = level_template.db[template]
    self.template = template

    self.monsters = []

    # initial level generation
    self.generate_cells()
    while self.num_monsters < self.max_spawn_monsters:
      self.spawn_random_monster()

  def __getattr__(self, name):
    t = self.__dict__.get("template", None)
    if name == "flags":
      return self._rebuild_map()
    elif name == "monsters_map":
      return self._rebuild_monsters_map()
    else:
      try:
        return getattr(t, name)
      except:
        raise AttributeError(name)

  def turn(self):
    # update and spawn monsters
    self.remove_dead_monsters()
    if self.num_monsters < self.max_spawn_monsters:
      if dice.chance(self.spawn_rate):
        self.spawn_random_monster()

  ################################################################################
  # Derived mappings                                                             #
  ################################################################################

  # Mappings:
  #   cells_map (contains level cells)
  #   explored_map (cells as last seen by pc: (ch,col) pairs)
  #
  # Derived:
  #   monsters_map (derived from self.monsters)

  def _make_mapping(self, default_entry=None):
    return [[default_entry for x in range(self.width)] for y in range(self.height)]

  def _rebuild_map(self):
    self.flags = _map.Map(self.width, self.height)
    for y in range(self.height):
      for x in range(self.width):
        self.set_cell(x, y, self.cells_map[y][x])
    for mon in self.monsters:
      if mon.is_alive: self.flags.set(x, y, MONSTER_FLAG, True)
    return self.flags

  def _rebuild_monsters_map(self):
    self.monsters_map = self._make_mapping()
    for mon in self.monsters:
      x, y = self.get_monster_pos(mon)
      self.monsters_map[y][x] = mon
    return self.monsters_map

  def __getstate__(self):
    data = dict(self.__dict__)
    data.pop("flags", None)
    data.pop("monsters_map", None)
    return data

  ################################################################################
  # Level generation                                                             #
  ################################################################################

  def generate_cells(self):
    self.flags = _map.Map(self.width, self.height)
    self.cells_map = self._make_mapping()
    self.explored_map = self._make_mapping((' ', 8))

    ch_map = self.map.split()

    # parse cell_alias
    cell_alias = {}
    for ch, info in dict(getattr(self, 'cell_alias', [])).items():
      if isinstance(info, str):
        cell_class, cell_kwargs = cells.db[info], {}
      else:
        cell_class, cell_kwargs = cells.db[info[0]], info[1]
      cell_alias[ch] = (cell_class, cell_kwargs)

    # create all cells
    for y in range(self.height):
      for x in range(self.width):
        ch = ch_map[y][x]
        if ch in cell_alias:
          cell_class, cell_kwargs = cell_alias[ch]
        else:
          cell_class, cell_kwargs = cells.db_alias[ch], {}
        self.set_cell(x, y, cell_class(**cell_kwargs))

  def spawn_random_monster(self):
    mon = monster.Monster(monster_template.random_select(1, self.danger_level))
    x, y = self.find_empty_cell()
    self.add_monster(mon, x, y)

  ################################################################################
  # Cells                                                                        #
  ################################################################################

  def iter_cells(self, x_range=None, y_range=None):
    x_range = x_range or (0, self.width)
    y_range = y_range or (0, self.height)
    for y in range(*y_range):
      row = self.cells_map[y]
      for x in range(*x_range):
        yield x, y, row[x]

  def find_empty_cell(self):
    """Return (x,y) cell position of an empty cell without a monster"""
    while True:
      x, y = random.randint(0, self.width-1), random.randint(0, self.height-1)
      if cells.is_walkable(self.cells_map[y][x]) and not self.get_monster_at(x, y):
        return x, y

  def get_cell(self, x, y):
    return self.cells_map[y][x]

  def set_cell(self, x, y, cell):
    self.cells_map[y][x] = cell
    self.flags.set(x, y, OPAQUE_FLAG, cells.is_opaque(cell))
    self.flags.set(x, y, WALKABLE_FLAG, cells.is_walkable(cell))

  def cell_gfx(self, x, y):
    return cells.gfx(self.cells_map[y][x])

  ################################################################################
  # Field of vision (FOV) and pc distance map                                    #
  ################################################################################

  def iter_seen_monsters(self, x_range=None, y_range=None):
    (x0, x1), (y0, y1) = x_range, y_range
    for mon in self.seen_monsters:
      x, y = self.get_monster_pos(mon)
      if x < x0 or y < y0 or x >= x1 or y >= y1: continue
      yield x, y, mon

  def calculate_fov(self, x, y, radius):
    seen_cells = self.flags.calculate_fov(x, y, radius, 0, OPAQUE_FLAG)

    monsters_map = self.monsters_map
    cells_map = self.cells_map
    explored_map = self.explored_map

    self.seen_monsters = []
    for x, y in seen_cells:
      explored_map[y][x] = cells.gfx(cells_map[y][x])
      mon = monsters_map[y][x]
      if mon and mon.is_alive:
        self.seen_monsters.append(mon)

  def calculate_distance_map(self, points):
    self.dist_map = self.flags.calculate_distance_map(points, WALKABLE_FLAG, WALKABLE_FLAG)
    return self.dist_map

  ################################################################################
  # Monsters                                                                     #
  ################################################################################

  def add_monster(self, mon, x, y):
    assert not mon.location
    assert not self.monsters_map[y][x]
    mon.location = monster.Location(self, x, y)
    self.monsters.append(mon)
    self.monsters_map[y][x] = mon
    self.flags.set(x, y, MONSTER_FLAG, True)

  def remove_monster(self, mon):
    loc = mon.location
    assert loc.level == self
    self.monsters.remove(mon)
    self.monsters_map[loc.y][loc.x] = None
    self.flags.set(loc.x, loc.y, MONSTER_FLAG, False)
    mon.location = None
    # Gotcha: Also remove from seen_monsters list!
    if mon in self.seen_monsters: self.seen_monsters.remove(mon)

  def remove_dead_monsters(self):
    dead_monsters = filter(operator.attrgetter("is_dead"), self.monsters)
    for mon in dead_monsters:
      self.remove_monster(mon)

  def move_monster(self, mon, x, y):
    loc = mon.location
    assert loc.level == self
    assert not self.monsters_map[y][x]
    self.monsters_map[loc.y][loc.x] = None; self.flags.set(loc.x, loc.y, MONSTER_FLAG, False)
    loc.x, loc.y = x, y
    self.monsters_map[loc.y][loc.x] = mon; self.flags.set(loc.x, loc.y, MONSTER_FLAG, True)

  def nudge_monster_at(self, x, y):
    """Make sure that any monster at (x,y) is moved onto another cell."""
    mon = self.get_monster_at(x, y)
    if mon:
      # TODO: Prefer adjacent free cells...
      x0, y0 = self.find_empty_cell()
      self.move_monster(mon, x0, y0)

  def get_monster_pos(self, mon):
    loc = mon.location
    assert loc.level == self
    return loc.x, loc.y

  def next_monster(self):
    assert len(self.monsters) > 0
    while True:
      mon = max(self.monsters, key=lambda mon: mon.location.move_pts)
      if mon.location.move_pts > 0:
        mon.location.move_pts -= 100
        return mon
      else:
        for mon in self.monsters:
          mon.location.move_pts += mon.speed

  def iter_monsters(self, x_range=None, y_range=None):
    if not x_range and not y_range:
      for mon in self.monsters:
        yield mon.location.x, mon.location.y, mon
    else:
      (x0, x1), (y0, y1) = x_range, y_range
      for x, y, mon in self.iter_monsters():
        if x < x0 or y < y0 or x >= x1 or y >= y1: continue
        yield x, y, mon

  def get_monster_at(self, x, y):
    return self.monsters_map[y][x]

  @property
  def num_monsters(self):
    return len(self.monsters)

def connect(lev1, pos1, lev2, pos2):
  """E.g. connect_levels(lev1, cells.StairsUp, lev2, cells.StairsDown)"""

  def find_unique_cell_by_class(lev, cell_cls):
    pos = None
    for x, y, cell in lev.iter_cells():
      if isinstance(cell, cell_cls):
        if pos is not None: return None # two found -> not unique
        pos = (x,y)
    return pos

  if issubclass(pos1, cells.AbstractPortal): pos1 = find_unique_cell_by_class(lev1, pos1)
  if issubclass(pos2, cells.AbstractPortal): pos2 = find_unique_cell_by_class(lev2, pos2)
  assert pos1 and pos2

  portal1, portal2 = lev1.get_cell(*pos1), lev2.get_cell(*pos2)
  portal1.connection = lev2, pos2[0], pos2[1]
  portal2.connection = lev1, pos1[0], pos1[1]


import urllib.request
import yaml
import http.client
import tarfile
import re
import shutil
import tempfile
import logging
import hashlib
import os
import os.path
import subprocess

def create_folder(folder):
    try:
        if not os.path.exists(folder):
            os.makedirs(folder)
            logging.info("created folder %s", folder)
        return True
    except:
        logging.error("could not create %s", folder)
        return False

# return hash of string in defined length
def get_hash(string, length):
    h = hashlib.sha256()
    h.update(bytes(string, 'utf-8'))
    response_hash = h.hexdigest()[:length]
    return response_hash

def get_statuscode(url):
    url_split = url.split("/")
    host = url_split[2]
    path = "/" +"/".join(url_split[3:])
    conn = http.client.HTTPConnection(host)
    conn.request("HEAD", path)
    return conn.getresponse().status

def get_latest_release(distro):
    with open(os.path.join("distributions", distro, "releases"), "r") as releases:
        return releases.readlines()[-1].strip()
    return None

def get_release_config(distro, release):
    config_path = os.path.join("distributions", distro, (release + ".yml"))
    if os.path.exists(config_path):
        with open(config_path, "r") as release_config:
            return yaml.load(release_config.read())

    return None

def get_supported_targets(distro, release):
    response = {}
    targets = get_release_config(distro, release)
    if targets:
        for target in targets["supported"]:
            subtarget = None
            if "/" in target:
                target, subtarget = target.split("/")
            if not target in response:
                response[target] = []
            if subtarget:
                response[target].append(subtarget)
        return response
    return None


#!/opt/conda/envs/dsenv/bin/python

import sys, os
import logging

SPARK_HOME = "/usr/hdp/current/spark2-client"
PYSPARK_PYTHON = "/opt/conda/envs/dsenv/bin/python"
os.environ["PYSPARK_PYTHON"]= PYSPARK_PYTHON
os.environ["SPARK_HOME"] = SPARK_HOME

PYSPARK_HOME = os.path.join(SPARK_HOME, "python/lib")
sys.path.insert(0, os.path.join(PYSPARK_HOME, "py4j-0.10.7-src.zip"))
sys.path.insert(0, os.path.join(PYSPARK_HOME, "pyspark.zip"))

#
# Init the logger
#
logging.basicConfig(level=logging.DEBUG)
logging.info("CURRENT_DIR {}".format(os.getcwd()))
logging.info("SCRIPT CALLED AS {}".format(sys.argv[0]))
logging.info("ARGS {}".format(sys.argv[1:]))

#
# Read script arguments
#
try:
    model_path = sys.argv[1]
    test_path = sys.argv[2]
    predict_path = sys.argv[3]
except:
    logging.critical("Need to pass model path, test_path, predict path")
    sys.exit(1)

logging.info(f"MODEL_PATH {model_path}")
logging.info(f"TEST_PATH {test_path}")
logging.info(f"PREDICT_PATH {predict_path}")

#
# model loading
#
from pyspark.sql import SparkSession

spark = SparkSession.builder.getOrCreate()
spark.sparkContext.setLogLevel('WARN')

from pyspark.ml import Pipeline, PipelineModel

model = PipelineModel.load(model_path)

#
# prediction
#
dataset = spark.read.json(
    test_path,
    multiLine=True
)

dataset_cleaned = dataset.select("id", "reviewText", "overall")
predictions = pipeline_model.transform(dataset_cleaned)
predictions.select("prediction").write.parquet(predict_path, mode="overwrite")


# -*- coding:utf-8 -*-

from io import BytesIO
from flask import Blueprint, make_response
from utils.captcha import Captcha
from utils import zlcache

bp = Blueprint("common", __name__, url_prefix='/c')

@bp.route("/captcha_graph/")
def graph_captcha():
    text, image = Captcha.gene_graph_captcha()
    zlcache.set(text.lower(), text.lower())
    # 在内存中读取二进制文件
    out = BytesIO()
    image.save(out, 'png')
    out.seek(0)
    resp = make_response(out.read())
    resp.content_type = "image/png"

    return resp


# I'm setting the Kivy directory to be a sub of the application directory
# I ran into some problems with system path variables on my desktop box where
# the Kivy defaults were conflicting with our Creo setup. This likely isn't
# necessary on the 'production' setup, but I don't see any harm in leaving it
# here
import os
working_dir = os.getcwd()
kivy_dir = working_dir + '/.kivy'
os.environ['KIVY_HOME'] = kivy_dir

# Kivy imports
import kivy
from kivy.app import App
from kivy.core.window import Window
from kivy.properties import NumericProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget

# Config imports
from config import COM_PORT, DEV_MODE, SCREEN_RES, TRIGGER_DURATION

# Other imports
import numpy as np
import queue
import serial
import struct
import threading

from math import floor
from time import sleep

################################################################################
###### Configuration variables #################################################
################################################################################
""" These should not be edited. User settable configuration values are in the
    file config.py
"""
# Set serial com config for installation. For a windows computer you should
# not need to change any value other than 'PORT'
COM_CONFIG = { 'SAMPLESIZE': 17,
               'FRAMESIZE': 68,
               'PORT': COM_PORT,
               'BAUDRATE': 115200 }

# This is the time interval in seconds between attempted USB reads. This value
# needs to be set shorter than the corresponding write interval on the
# Arduino, otherwise the elapsed time clock will get out of sync and the UI
# will noticeably lag behind the sensor outputs.
USB_READ_INTERVAL = 0.05

DATAFRAME = np.zeros(COM_CONFIG['SAMPLESIZE'], dtype=np.uint32)
"""The DATAFRAME is a 1x<SAMPLESIZE> array of unsigned 32-bit integers.
   The contents are as follows:

   Index    Contents
   0        Running elapsed time in milliseconds
   1        Event counter - discrete channel 1
   2        Event counter - discrete channel 2
   3        Event counter - discrete channel 3
   4        Event counter - discrete channel 4
   5        Event counter - discrete channel 5
   6        Event counter - discrete channel 6
   7        Event counter - discrete channel 7
   8        Event counter - discrete channel 8
   9        Event counter - analog channel 1
   10       Current value - analog channel 1
   11       Event counter - analog channel 2
   12       Current value - analog channel 2
   13       Event counter - analog channel 3
   14       Current value - analog channel 3
   15       Event counter - analog channel 4
   16       Current value - analog channel 4
"""
################################################################################
##### End configuration variables ##############################################
################################################################################

class AnalogLabels(BoxLayout):
    pass

class AnalogLayout(BoxLayout):
    pass

class AnalogMeter(FloatLayout):
    channelID = NumericProperty()

    # Cosmetic margin used in GUI layout
    margin = NumericProperty(4)

    # Threshold values used by Arduino (0 to 1023)
    lowThreshold = NumericProperty(0)
    highThreshold = NumericProperty(1023)

    # Threshold values for display (0-10V or 0-20mA)
    lowThresholdDisp = StringProperty('Low Thres.\r\n------------')
    highThresholdDisp = StringProperty('High Thres.\r\n----------')

    # Analog level sent back from Arduino (0 to 1023)
    level = NumericProperty(0)

    def on_touch_move(self, touch):
        '''Moves the threshold markers'''

        # Do not move markers if test is running
        if app.controlsLayout.testRunning == True:
            return None

        # Only act on touches within AnalogMeter
        if self.collide_point(*touch.pos):

            # Ignore touch if the channel is deactivated
            if app.analogStrips[self.channelID-1].ids.channelLabel.state == 'normal':
                return None

            # Only act further if the touch position is within one of the
            # two threshold markers
            if self.ids.markerLow.collide_point(*touch.pos) | \
                self.ids.markerHigh.collide_point(*touch.pos):
                xBase = self.ids.bgMeter.x
                xRight = self.ids.bgMeter.right

                # Move only the marker closest to the touch position
                highDist = abs(touch.x - self.ids.markerHigh.center_x)
                lowDist = abs(touch.x - self.ids.markerLow.center_x)
                if highDist <= lowDist:
                    # Don't let marker move off of the end of the indicator
                    if touch.x > (xRight - self.margin):
                        self.ids.markerHigh.center_x = xRight - self.margin
                    else:
                        self.ids.markerHigh.center_x = touch.x
                    # Set threshold (0 to 1023) value according to marker position
                    self.highThreshold = int((self.ids.markerHigh.center_x-(xBase+self.margin))*1023/(xRight-xBase-2*self.margin))
                else:
                    if touch.x < (self.ids.bgMeter.x + self.margin):
                        self.ids.markerLow.center_x = self.ids.bgMeter.x + self.margin
                    else:
                        self.ids.markerLow.center_x = touch.x
                    self.lowThreshold = int((self.ids.markerLow.center_x-(xBase+self.margin))*1023/(xRight-xBase-2*self.margin))

                # Update channel configuration
                app.analogStrips[self.channelID-1].updateConfig()

    def updateLabels(self):
        '''Updates the labels showing the threshold values'''
        if app.analogStrips[self.channelID-1].ids.channelLabel.state == 'normal':
            self.lowThresholdDisp = 'Low Thres.\r\n------------'
            self.highThresholdDisp = 'High Thres.\r\n----------'

        elif app.analogStrips[self.channelID-1].ids.currentButton.state == 'down':
            mA = self.highThreshold * 20/1023.0
            mAdisp = '{:0.2f} mA'.format(mA)
            self.highThresholdDisp = 'High Thres.\r\n' + mAdisp

            mA = self.lowThreshold * 20/1023.0
            mAdisp = '{:0.2f} mA'.format(mA)
            self.lowThresholdDisp = 'High Thres.\r\n' + mAdisp

        else:
            volts = self.highThreshold * 10/1023.0
            voltsDisp = '{:0.3f} V'.format(volts)
            self.highThresholdDisp = 'High Thres.\r\n' + voltsDisp

            volts = self.lowThreshold * 10/1023.0
            voltsDisp = '{:0.3f} V'.format(volts)
            self.lowThresholdDisp = 'High Thres.\r\n' + voltsDisp

class ChannelStripAnalog(BoxLayout):
    channelID = NumericProperty()

    # Running tally of threshold crossing events received from Arduino
    eventCounter = NumericProperty(0)

    # Channel configuration value:
    #   0: Off
    #   1: Analog current (0-20mA)
    #   2: Analog voltage (0-10V)
    # High and low threshold values belong to the AnalogMeter class
    channelConfig = NumericProperty(0)

    def updateConfig(self):
        '''Updates the channel configuration'''

        # If test is not running, update channel configuration according to
        # GUI state
        if app.controlsLayout.testRunning == False:
            self.ids.analogMeter.updateLabels()
            if self.ids.channelLabel.state == 'normal':
                #print('Channel A{} is off'.format(str(self.channelID)))
                self.channelConfig = 0
            elif self.ids.currentButton.state == 'down':
                #print('Channel A{} is analog current, low threshold {}, high threshold {}'.format(
                #str(self.channelID), str(self.ids.analogMeter.lowThreshold),
                #str(self.ids.analogMeter.highThreshold)))
                self.channelConfig = 1
            else:
                #print('Channel A{} is analog voltage, low threshold {}, high threshold {}'.format(
                #str(self.channelID), str(self.ids.analogMeter.lowThreshold),
                #str(self.ids.analogMeter.highThreshold)))
                self.channelConfig = 2

        # If test is running. Immediately return GUI to state in accordance with
        # existing config values. This effectively freezes the GUI elements while
        # a test is running (not sure if there's a more straightforward way
        # of doing this).
        else:
            if self.channelConfig == 0:
                self.ids.channelLabel.state = 'normal'
            if self.channelConfig == 1 or self.channelConfig == 2:
                self.ids.channelLabel.state = 'down'
                if self.channelConfig == 1:
                    self.ids.currentButton.state = 'down'
                    self.ids.voltageButton.state = 'normal'
                else:
                    self.ids.currentButton.state = 'normal'
                    self.ids.voltageButton.state = 'down'

class ChannelConfigButton(CheckBox):
    pass

class ChannelStripDiscrete(BoxLayout):
    channelID = NumericProperty()

    # Running tally of threshold crossing events received from Arduin
    eventCounter = NumericProperty(0)

    # Channel configuration value:
    #   0: Off
    #   1: PNP/NPN rising edge
    #   2: PNP/NPN falling edge
    channelConfig = NumericProperty(0)

    def updateConfig(self):
        """Updates the channel configuration."""

        # If test is not running, update channel configuration according to
        # GUI state
        if app.controlsLayout.testRunning == False:
            if self.ids.channelLabel.state == 'normal':
                #print('Channel D{} is off'.format(str(self.channelID)))
                self.channelConfig = 0
            else:
                if self.ids.risingEdgeButton.state == 'down':
                    #print('Channel D{} is PNP/NPN rising edge'.format(str(self.channelID)))
                    self.channelConfig = 1
                else:
                    #print('Channel D{} is PNP/NPN falling edge'.format(str(self.channelID)))
                    self.channelConfig = 2

        # If test is running. Immediately return GUI to state in accordance with
        # existing config values. This effectively freezes the GUI elements while
        # a test is running.
        else:
            if self.channelConfig == 0:
                self.ids.channelLabel.state = 'normal'
            if self.channelConfig == 1 or self.channelConfig == 2:
                self.ids.channelLabel.state = 'down'
                if self.channelConfig == 1:
                    self.ids.risingEdgeButton.state = 'down'
                    self.ids.fallingEdgeButton.state = 'normal'
                else:
                    self.ids.risingEdgeButton.state = 'normal'
                    self.ids.fallingEdgeButton.state = 'down'

class CheckBoxLabel(Label):
    pass

class ControlsLayout(BoxLayout):
    testRunning = False

    def readData(self, dt):
        """Reads and processes data from serial queue.

           Queue is first in first out. In practice, since we are calling this
           function at a higher frequency than we are sending serial packets
           from the Arduino, the queue will have either 0 or 1 item in it."""

        # The queue is usually empty
        if self.q.empty():
            return None

        # When the queue is not empty, get the first packet of data off of the
        # queue and process.
        dataframe = self.q.get()
        self.ids.elapsedTime.updateClock(dataframe[0])

        for strip in app.discreteStrips:
            strip.eventCounter = int(dataframe[strip.channelID])

        for strip in app.analogStrips:
            strip.ids.analogMeter.level = int(dataframe[(strip.channelID*2)+8])
            strip.eventCounter = int(dataframe[(strip.channelID*2)+7])

    def startTest(self):
        """Starts a test"""
        if self.testRunning:
            return None

        try:
            self.usb = serial.Serial(COM_CONFIG['PORT'], COM_CONFIG['BAUDRATE'],
                                timeout = 10)
            sleep(0.5)

            # Send discrete channel configs to Arduino
            for strip in app.discreteStrips:
                self.usb.write(struct.pack('<B', strip.channelConfig))

            # Send analog channel configs to Arduino
            for strip in app.analogStrips:
                self.usb.write(struct.pack('<B', strip.channelConfig))
                self.usb.write(struct.pack('<H', strip.ids.analogMeter.lowThreshold))
                self.usb.write(struct.pack('<H', strip.ids.analogMeter.highThreshold))

            # Send gate threshold time to Arduino
            self.usb.write(struct.pack('<L', self.ids.gateThreshold.gateThresholdMicros))

            # Send trigger duration to Arduino
            triggerDurationMicros = int(TRIGGER_DURATION*1000)
            self.usb.write(struct.pack('<L', triggerDurationMicros))

            self.ids.startButton.disabled = True
            self.ids.stopButton.disabled = False

        except serial.serialutil.SerialException:
            print('\nCOM error.\nSerial connection could not be established.')
            print('Check COM_PORT value in config.py file.\n')
            raise SystemExit

        sleep(0.25)
        self.usb.reset_input_buffer()
        self.q = queue.Queue()
        self.reader = SerialReader(self.q, self.usb)
        self.reader.start()
        self.testRunning = True

        # The data read should always be scheduled for a shorter interval than
        # the arduino is sending updates. Otherwise the UI display will lag
        # behind what is happening in real time.
        Clock.schedule_interval(self.readData, USB_READ_INTERVAL)

    def stopTest(self):
        """Stops a running test."""
        Clock.unschedule(self.readData)
        if self.testRunning:
            self.reader.stop()
            self.ids.startButton.disabled = False
            self.ids.stopButton.disabled = True
            self.testRunning = False
        else:
            pass

class DiscreteLabels(BoxLayout):
    pass

class DiscreteLayout(BoxLayout):
    pass

class EventCounter(Label):
    pass

class ElapsedTime(BoxLayout):
    timeDisplay = StringProperty('--:--:--')

    def updateClock(self, timeMillis):
        """Update the displayed clock value.

           Takes elapsed time in milliseconds, converts to HH:MM:SS format
           and updates GUI display."""

        seconds = timeMillis/1000.0
        hours = floor(seconds/3600)
        minutes = floor(seconds/60 - hours*60)
        seconds = floor(seconds - hours*3600 - minutes*60)
        self.timeDisplay = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)

class GateThresholdControls(BoxLayout):
    # Gate threshold in milliseconds
    gateThresholdMillis = NumericProperty(1.0)

    # Gate threshold in microseconds
    gateThresholdMicros = NumericProperty(1000)

    # Gate treshold for GUI display
    gateThresholdStr = StringProperty('1.000 ms')

    def decrementThreshold(self):
        """Increments gate threshold.

           The increment/decrement value is currently hard-coded to 0.25ms.

           Future improvement: Change increment/decrement value according to
           or smaller, the increment/decrement should as well). Or possibly
           current threshold value (i.e. as the threshold value gets larger
           variable speed scrolling as the buttons are held down."""

        if app.controlsLayout.testRunning == True:
            return None

        if self.gateThresholdMillis >= 0.25:
            self.gateThresholdMillis -= 0.25
            self.gateThresholdStr = '{:0.3f} ms'.format(self.gateThresholdMillis)
            self.gateThresholdMicros = int(self.gateThresholdMillis*1000)

    def incrementThreshold(self):
        """Decrements gate threshold"""

        if app.controlsLayout.testRunning == True:
            return None

        self.gateThresholdMillis += 0.25
        self.gateThresholdStr = '{:0.3f} ms'.format(self.gateThresholdMillis)
        self.gateThresholdMicros = int(self.gateThresholdMillis*1000)

    def manualEntry(self):
        ''' Allows manual entry of gate threshold value '''

        if app.controlsLayout.testRunning == True:
            return None

        popup = Popup(title = 'Enter detection threshold time in ms',
                      content=TextInput(multiline=False), size=(500, 100), size_hint=(None, None))
        popup.bind(on_dismiss=self.setThreshold)
        popup.open()

    def setThreshold(self, popup):
        try:
            self.gateThresholdMillis = float(popup.content.text)
        except ValueError:
            return None

        self.gateThresholdMicros = int(self.gateThresholdMillis*1000)
        self.gateThresholdStr = '{:0.3f} ms'.format(self.gateThresholdMillis)

class LineSeparator(Widget):
    pass

class PassFailCheckBox(CheckBox):
    pass

class SerialReader(threading.Thread):
    def __init__(self, q, usb):
        threading.Thread.__init__(self)
        self.q = q
        self.usb = usb
        self.stopFlag = False
        self.daemon = True

    def serialRead(self):
        """Reads one packet of incoming serial data"""

        data = self.usb.read(COM_CONFIG['FRAMESIZE'])
        DATAFRAME = np.fromstring(data, dtype=np.uint32)
        return DATAFRAME

    def run(self):
        while not self.stopFlag:
            self.q.put(self.serialRead())
        print('Reader thread stopping')

    def stop(self):
        self.stopFlag = True
        self.usb.close()

class ThresholdMarker(Image):
    pass

Window.size = SCREEN_RES
if DEV_MODE == False:
    Window.fullscreen = True

class ShockVibeBoxApp(App):
    def build(self):
        # Top level layout is 3 panel side-by-side box
        mainLayout = BoxLayout(orientation='horizontal')
        discreteLayout = DiscreteLayout(orientation='vertical')
        analogLayout = AnalogLayout(orientation='vertical')
        self.controlsLayout = ControlsLayout(orientation='vertical')

        # Add labels for discrete channels to left half of top level layout
        discreteLabels = DiscreteLabels()
        discreteLayout.add_widget(discreteLabels)

        # Add labels for analog channels to left half of top level layout
        analogLabels = AnalogLabels()
        analogLayout.add_widget(analogLabels)

        # Add discrete channel strips discrete sublayout
        self.discreteStrips = []
        for i in range(8):
            self.discreteStrips.append(ChannelStripDiscrete())
        for i in range(len(self.discreteStrips)):
            self.discreteStrips[i].channelID = i+1
            discreteLayout.add_widget(self.discreteStrips[i])

        # Add analog channel strips to analog sublayout
        self.analogStrips = []
        for i in range(4):
            self.analogStrips.append(ChannelStripAnalog())
        for i in range(len(self.analogStrips)):
            self.analogStrips[i].channelID = i+1
            analogLayout.add_widget(self.analogStrips[i])

        # Add sublayouts to main layout
        mainLayout.add_widget(discreteLayout)
        mainLayout.add_widget(analogLayout)
        mainLayout.add_widget(self.controlsLayout)

        return mainLayout

if __name__ == '__main__':
    app = ShockVibeBoxApp()
    app.run()


from flask import Flask
from flask_restful import Resource, Api,reqparse
from porc import Client
app = Flask(__name__)
api = Api(app)
app.config.from_pyfile('config.cfg')
client = Client(app.config['ORCHESTRATE'])

parser = reqparse.RequestParser()
parser.add_argument('work')

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

class GetAllOrders(Resource):
	def get(self):
		return client.list("WorkOrders").all()

class Order(Resource):
	def get(self, item_id):
		item = client.get("WorkOrders", item_id)
		return item.json
	def delete(self,item_id):
		item = client.delete('WorkOrders', item_id)
		return client.ref

class sendOrder(Resource):
	def post(self):

		args = parser.parse_args()
		response = client.post('WorkOrders', {
  			"work": args['work']
		})
		return {'ref':str(response.ref)}

api.add_resource(GetAllOrders, '/order')
api.add_resource(Order,'/order/<string:item_id>')
api.add_resource(sendOrder,'/order/add/')

if __name__ == '__main__':
    app.run(debug=True)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  1 16:57:55 2020

@author: sujania
"""

import os
import glob
import pickle
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from os.path import join, basename, splitext
from obspy.core import AttribDict
from collections import OrderedDict
from frospy.postprocessing.uncertainties import uncertainties_calculation
from frospy.postprocessing.AnalyseRuns.plot import misfits_cst, listdir
from frospy.postprocessing import plot
from frospy.postprocessing.plot import inv_summary, cst
from frospy.postprocessing.read import read_inversion_summary
from frospy.postprocessing.misfit import plot_Qf
from frospy.core.splittingfunc import Set
from frospy.core.splittingfunc import loadmodel # python2 branch
#from frospy.splitting.load import loadmodel # python3 master branch
from frospy.core.splittingfunc.read import read_cst, get_cst, read_cst_S20RTS
from frospy.util.read import read_modes_in, get_mode_names
from frospy.util.base import sort_human
from frospy.core.setup.settings import Setup, get_mode_list
from frospy.util.base import (chunking_list, split_digit_nondigit,
                            max_cc_degrees, max_sc_degrees)
from frospy.core.setup.settings import read as read_setup
from frospy.core.splittingfunc.plot import sens_kernel
import shapefile
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch
from matplotlib import gridspec

##f = "/Users/sujania/Documents/PhD/NotesUU/Dst/polygons/0s6.dat"
#f = "/data/talavera/notes/Dst/polygons/0s6.dat"
#input2 = OrderedDict() #AttribDict()
#input2['modes'] =  OrderedDict([("00s06",12)])
#input2['modes_sc_dst'] = OrderedDict([("00s06",12)])
#input2['startmodel'] = 'PREM'
#setup2=Setup('CST', input2)
#sf = loadmodel(setup=setup2, ifile = f, name = '0s6', damp = '0s6')

master_db_path = '/net/home/talavera/eejit/data/mycst.sqlite3'
sf = Set()

modes = [
#         ["0S2"],#LM
#         ["0S5"],#LM
#         ["0S6"],#LM
#         ["0S7"],#LM
#         ["1S4"],#LM
#         ["1S7"],#LM
#         ["1S8"],#LM
#         ["1S9"],#LM
         ["1S10"],#LM
#         ["2S6"],#UM
#         ["2S12"],#UM
#         ["2S13"],#UM
#         ["3S6"],#UM
#         ["3S9"],#UM
#         ["0T6"],#UM
#         ["0T8"],#UM
#         ["0T9"],#UM
#         ["1T2"],#UM
#         ["1T5"],#LM
#         ["2T3"],#LM
#         ["2T7"],#LM
         ]

scut = [
#         [2,2], #0S2
#         [8,6], #0S5
#         [10,4], #0S6
#         [12,6], #0S7
#         [6,4], #1S4
#         [10,6], #1S7
#         [12,6], #1S8
#         [12,6], #1S9
         [12,4], #1S10
#         [10,6], #2S6
#         [12,10], #2S12
#         [12,2], #2S13
#         [6,2], #3S6
#         [12,4], #3S9
#         [6,2], #0T6
#         [8,2], #0T8
#         [8,4], #0T9
#         [2,2], #1T2
#         [8,2], #1T5
#         [6,4], #2T3
#         [6,2], #2T7
         ]

db_model = 'dst'
for m,smax in zip(modes,scut):
#    print(m)
    sf = loadmodel(ifile=master_db_path, modes=m,
                   name=db_model, damp='0', db_model=db_model)

    #mdir = "/Users/sujania/Documents/PhD/NotesUU/Dst/ani_polygons/*.shp"
    mdir = "/data/talavera/notes/Dst/ani_polygons/*.shp"
    ifiles = glob.glob(mdir)
    fig = plt.figure(figsize=(8.5,5))
    ax = fig.add_subplot(122)
    ax1 = fig.add_subplot(121)
    gs = gridspec.GridSpec(1, 2, width_ratios=[0.8, 3.2], wspace=0.05)
    ax0 = plt.subplot(gs[0])
    sens_kernel(sf.stats.modes_in[0], title=False, ax=ax0, ticks=False,
                fontsize=12, linewidth=1.5)
    ax = plt.subplot(gs[1])
    m = Basemap(projection='kav7',lon_0=0,resolution='c',ax=ax)

    patchesA = [] # ani
    patchesB = [] # no ani
    patchesC = [] #?
    patchesD = [] # ani pPv from Creasy AGU

    for i,f in enumerate(ifiles):
        f = f.split(".shp")[0]
        name = f.split("/")[-1]
        if name not in ["p5"]: #overlapping studies 5or6 in CA?
            pt = m.readshapefile(f, name="p", drawbounds=True,
                                 color='k', linewidth=0.85)
            for info, shape in zip(m.p_info, m.p):
                x, y = zip(*shape)
                if name in ["p2","p3","p8", "p13"]: # no ani
                    # m.plot(x, y, marker=None, color='r')
                    patchesB.append(Polygon(np.array(shape), True))
                elif name in ["p4", "p7"]: # ani?
                    patchesC.append(Polygon(np.array(shape), True))
                elif name in ["p9", "p12", "p16", "p22"]: # ani pPv
                    patchesD.append(Polygon(np.array(shape), True))
                else: #ani
                    patchesA.append(Polygon(np.array(shape), True))

    ax.add_collection(PatchCollection(patchesA,
                                      facecolor='r',
                                      edgecolor='r',
                                      linewidths=1.,
                                      alpha=0.65,
                                      zorder=150))

    ax.add_collection(PatchCollection(patchesB,
                                      facecolor='grey',
                                      edgecolor='grey',
                                      linewidths=1.,
                                      alpha=0.85,
                                      zorder=100))

    ax.add_collection(PatchCollection(patchesC,
                                      facecolor='k',
                                      edgecolor='k',
                                      linewidths=1.,
                                      alpha=0.65,
                                      zorder=100))

    ax.add_collection(PatchCollection(patchesD,
                                      facecolor='darkred',
                                      edgecolor='darkred',
                                      linewidths=1.,
                                      alpha=0.75,
                                      zorder=150))

    sf.plot_map(
                ax=ax,
                fig=fig,
                smax=smax[1],
    #            smax=12,
                # R=-0.2,
    #             kind="dst",
    #             show_title=False,
                # legend_show=False,
                )
    #
    #fig.savefig('cst',dpi=400,
    #              bbox_inches='tight',
    #              pad_inches=0.,)
    plt.show()


from pico2d import *
import random
from Missile import Missile1
from Missile import Missile2
from Missile import Missile3

__author__ = 'user'

class Player:
    PIXEL_PER_METER = (10.0 / 0.5)
    RUN_SPEED_KMPH = 20.0
    RUN_SPEED_MPM = (RUN_SPEED_KMPH * 1000.0 / 60.0)
    RUN_SPEED_MPS = (RUN_SPEED_MPM / 60.0)
    RUN_SPEED_PPS = (RUN_SPEED_MPS * PIXEL_PER_METER)

    TIME_PER_ACTION = 0.5
    ACTION_PER_TIME = 2.5 / TIME_PER_ACTION
    FRAMES_PER_ACTION = 1

    image = None
    frameSize = 9

    LEFT, RIGHT, IDLE, UP, DOWN, ATTACK = 0, 1, 2, 3, 4, 5

    leftmove = False
    rightmove = False
    upmove = False
    downmove = False

    Missile1_L = []
    Missile2_L = []
    Missile3_L = []

    def __init__(self):
        self.x = 400
        self.y = 90
        self.xdir = 0
        self.ydir = 0
        self.state = self.IDLE
        self.frame = 0
        self.total_frames = 0.0
        if Player.image == None:
            Player.image = load_image('object/Player/Player.png')

    def update(self, frame_time):
        distance = Player.RUN_SPEED_PPS * frame_time
        self.total_frames += Player.FRAMES_PER_ACTION * Player.ACTION_PER_TIME * frame_time
        self.frame = int(self.total_frames)
        self.x += (self.xdir * distance)
        self.y += (self.ydir * distance)

        ms1 = Missile1(0, 0)
        ms2 = Missile2(0, 0)
        ms3 = Missile3(0, 0)

        global rightmove, leftmove, upmove, downmove
        global Missile1_L, Missile2_L, Missile3_L
        for ms1 in Missile1_L:
            ms1.update(frame_time)
            out =  ms1.update(frame_time)
            if out == True:
                Missile1_L.remove(ms1)
        for ms2 in Missile2_L:
            ms2.update(frame_time)
            out = ms2.update(frame_time)
            if out == True:
                Missile2_L.remove(ms2)
        for ms3 in Missile3_L:
            ms3.update(frame_time)
            out = ms3.update(frame_time)
            if out == True:
                Missile3_L.remove(ms3)

        ms1.update(frame_time)
        ms2.update(frame_time)
        ms3.update(frame_time)

        self.frame = (self.frame + 1) % self.frameSize

        if (rightmove == True):
            self.x += 10
            if(upmove == True):
                self.x += 10
                self.y += 10
            elif(downmove == True):
                self.x += 10
                self.y -= 10
        elif (leftmove == True):
            self.x -= 10
            if (upmove == True):
                self.x -= 10
                self.y += 10
            elif (downmove == True):
                self.x -= 10
                self.y -= 10
        elif (upmove == True):
            self.y += 10
            if (rightmove == True):
                self.y += 10
                self.x += 10
            elif (leftmove == True):
                self.y += 10
                self.x -= 10
        elif (downmove == True):
            self.y -= 10
            if (rightmove == True):
                self.y -= 10
                self.x += 10
            elif (leftmove == True):
                self.y -= 10
                self.x -= 10

        def clamp(minimum, y, maximum):
            return max(minimum, min(y, maximum))
        self.x = clamp(50, self.x, 750)
        self.y = clamp(50, self.y, 650)

    def handle_event(self, event):
        global leftmove, rightmove, upmove, downmove
        if (event.type, event.key) == (SDL_KEYDOWN, SDLK_RIGHT):
            rightmove = True
        elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_LEFT):
            leftmove = True
        elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_UP):
            upmove = True
        elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_DOWN):
            downmove = True
        elif (event.type, event.key) == (SDL_KEYUP, SDLK_RIGHT):
            rightmove = False
        elif (event.type, event.key) == (SDL_KEYUP, SDLK_LEFT):
            leftmove = False
        elif (event.type, event.key) == (SDL_KEYUP, SDLK_UP):
            upmove = False
        elif (event.type, event.key) == (SDL_KEYUP, SDLK_DOWN):
            downmove = False

        elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_z):
            Missile1_L.append(Missile1(self.x, self.y + 50))
        elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_x):
            Missile2_L.append(Missile2(self.x, self.y + 50))
        elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_c):
            Missile3_L.append(Missile3(self.x, self.y + 50))

    def draw(self):
        global ms1, ms2, ms3
        self.image.draw(self.x, self.y)
        for ms1 in Missile1_L:
            ms1.draw()
        for ms2 in Missile2_L:
            ms2.draw()
        for ms3 in Missile3_L:
            ms3.draw()


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import collections
from string import punctuation

def h_index(df, figures=False):
    # Calculate index
    H_index = 0
    H_index_distance = np.array(df['distance'].tolist())/1000
    # Determine the H-index
    H_index_distance = sorted(H_index_distance, reverse=True)
    for i in range(len(H_index_distance)):
        if i > H_index_distance[i]:
            H_index = i-1  # Outputs the H_index
            break
    if figures:
        plt.figure()
        plt.hist(H_index_distance)  # Tweak these parameters
        plt.xlabel('Kilometers')
        plt.ylabel('Activity count')
        plt.title('Histogram of activities')

        # Calculate the amount of activities for each bin
        H_index_cum = []
        for i in range(int(max(H_index_distance))):
            H_index_cum.append(sum(j > (i + 1) for j in H_index_distance))

        plt.figure()
        plt.bar(np.add(list(range(0, int(max(H_index_distance)))),1),H_index_cum)
        plt.plot([0, max(H_index_distance)], [0, max(H_index_distance)],'r')  # Add the H-Index line
        plt.xlabel('Kilometers')
        plt.ylabel('Activity count')
        plt.title('Cumulative histogram')
        # Show plots
        plt.show()

    return H_index

def totals(df):
    distance = df['distance'].sum()/1000
    elapsed_time = pd.to_timedelta(df['elapsed_time']).sum()
    kudos = df['kudos_count'].sum()
    moving_time = pd.to_timedelta(df['moving_time']).sum()
    avg_kudos = df['kudos_count'].mean() # Here devided by the amount of not private activities.
    output = {'distance' : distance,
              'elapsed_time' : elapsed_time,
              'kudos' : kudos,
              'moving_time' : moving_time,
              'avg_kudos' : avg_kudos}

    return output

def hr_vs_speed(df):
    df = df.loc[df['has_heartrate'] == True]
    speed = np.array(df['average_speed'].tolist())*3.6
    hr = df['average_heartrate'].tolist()
    plt.plot(speed,hr,'r*')
    plt.title('Heart rate vs Speed plot')
    plt.xlabel('Speed [km/h]')
    plt.ylabel('Heart rate [bpm]')
    plt.show()

def word_usage(df):
    words = []
    for i in range(len(df)):
        last_act = df['name'][i].split()

        words = words + last_act
    top_words = collections.Counter(map(str.lower, words)).most_common(20)
    print(top_words)
    return top_words

def StevenKruijswijkcoeff(df): # Calculates the Steven Kruijswijk Coefficient. (ratio between move/elapsed time)
    SKC = {}
    hours = {}
    for i in range(len(df)):
        SKC[i] = ((get_sec(df.moving_time[i])) / (get_sec(df.elapsed_time[i])))*100
        hours[i] = get_sec(df.elapsed_time[i])/3600
    df['SKC'] = SKC.values()
    df['hours'] = hours.values()

    df_social = df.loc[(df['athlete_count'] > 1)]
    df_remy = df.loc[(df['athlete_count'] == 1)]
    time_social = df_social['hours'].tolist()
    time_remy = df_remy['hours'].tolist()
    SKC_social = df_social['SKC'].tolist()
    SKC_remy = df_remy['SKC'].tolist()

    plt.scatter(time_remy,SKC_remy,  label= 'Remy')
    plt.scatter(time_social, SKC_social, label= 'social')
    plt.xlabel('Total riding time [hours]')
    plt.ylabel('Steven Kruiswijk Coefficient')
    plt.title('Steven Kruiswijk plot')
    plt.legend()
    plt.show()

def get_sec(time_str):
    h, m, s = time_str.split(':')
    return int(h) * 3600 + int(m) * 60 + int(s)


from processor_deep_srl import ProcessorDeepSrl

import argparse
from itertools import izip
import json
import os
import sys

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--model',
                      type=str,
                      default='',
                      required=True,
                      help='SRL Model path.')

    parser.add_argument('--pidmodel',
                      type=str,
                      default='',
                      help='Predicate identfication model path.')

    args = parser.parse_args()

    sys.stderr.write('Starting parser...\n')
    proc = ProcessorDeepSrl(args.model, args.pidmodel)
    sys.stderr.write('Parser started.\n')

    while(True):
        input_str = ''
        while not input_str.endswith('\n\n'):
            input_str += sys.stdin.readline()
            sys.stderr.flush()

        sys.stderr.write('Starting analysis...\n')
        sys.stderr.flush()

        res = proc(json.loads(input_str.decode('utf8')))

        sys.stderr.write('Done.\n')
        sys.stderr.flush()

        sys.stdout.write((json.dumps(res) + '\n\n').encode('utf8'))
        sys.stdout.flush()


def main(socket, petitions):
    import pyscreenshot as ImageGrab
    import os

    im = ImageGrab.grab(bbox=(10, 10, 510, 510))
    if not os.path.exists('tmp'): os.mkdir('tmp')
    im.save('tmp/screenshot.png')
    petitions['sendFile']('tmp/screenshot.png', socket)

# if __name__ == '__main__':
#     main(None, None)


import os
import sys
import csv
import cv2
import numpy as np
from random import shuffle
import sklearn
from sklearn.model_selection import train_test_split
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Conv2D, Activation, BatchNormalization, Cropping2D, Dropout
from keras.layers.pooling import MaxPooling2D
from pathlib import Path, PureWindowsPath, PurePath

data_log = './data/driving_log.csv'
image_dir = Path('./data//IMG/')

data = list()
with open(data_log) as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        data.append(line)

train_data, test_data = train_test_split(data, test_size=0.3)
valid_data, test_data = train_test_split(test_data, test_size=0.5)

def flip(image, angle):
    new_image = cv2.flip(image, 1)
    new_angle = -angle
    return new_image, new_angle

def random_brightness(image, angle):
    multiplier = np.random.uniform(0.5, 1.)
    hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
    hsv[:,:,2] = multiplier*hsv[:,:,2]
    new_image = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
    return new_image, angle

def random_augment(image, angle):
    if np.random.randint(0,2) == 1:
        image, angle = flip(image, angle)
    image, angle = random_brightness(image, angle)
    return image, angle

def generate_batch(data, batch_size=32):
    num_samples = len(data)
    while True:
        shuffle(data)
        for offset in range(0, num_samples, batch_size):
            batch = data[offset:offset+batch_size]
            steering_offset = 0.13
            images = []
            angles = []
            for center_name, left_name, right_name, steering, throttle, brake, speed in batch:
                center_name = str(image_dir.joinpath(PureWindowsPath(center_name).parts[-1]))
                left_name = str(image_dir.joinpath(PureWindowsPath(left_name).parts[-1]))
                right_name = str(image_dir.joinpath(PureWindowsPath(right_name).parts[-1]))

                steering = float(steering)
                # Center Images
                center_image = cv2.imread(center_name)
                center_image = cv2.cvtColor(center_image, cv2.COLOR_BGR2RGB)
                center_image, center_angle = random_augment(center_image, steering)
                images.append(center_image)
                angles.append(center_angle)
                # Left Images
                left_image = cv2.imread(left_name)
                left_image = cv2.cvtColor(left_image, cv2.COLOR_BGR2RGB)
                left_image, left_angle = random_augment(left_image, steering + steering_offset)
                images.append(left_image)
                angles.append(left_angle)
                # Right Images
                right_image = cv2.imread(right_name)
                right_image = cv2.cvtColor(right_image, cv2.COLOR_BGR2RGB)
                right_image, right_angle = random_augment(right_image, steering - steering_offset)
                images.append(right_image)
                angles.append(right_angle)
            X_train = np.array(images)
            Y_train = np.array(angles)
            yield sklearn.utils.shuffle(X_train, Y_train)

train_generator = generate_batch(train_data, batch_size=64)
valid_generator = generate_batch(valid_data, batch_size=64)

ch, row, col = 3, 160, 320

model = Sequential()

model.add(Lambda(lambda x: x/255, input_shape=(row, col, ch), output_shape=(row, col, ch)))

model.add(Cropping2D(((20, 20), (0, 0))))

model.add(Lambda(lambda img: K.tf.image.resize_images(img, (64,64))))

model.add(Conv2D(32, (3,3), strides=2, kernel_initializer='truncated_normal', bias_initializer='zeros'))
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Conv2D(64, (3,3), strides=2, kernel_initializer='truncated_normal', bias_initializer='zeros'))
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Conv2D(128, (3,3), strides=2, kernel_initializer='truncated_normal', bias_initializer='zeros'))
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Conv2D(256, (3,3), strides=2, kernel_initializer='truncated_normal', bias_initializer='zeros'))
model.add(BatchNormalization())
model.add(Activation('relu'))

model.add(Flatten())

model.add(Dense(512, kernel_initializer='truncated_normal', bias_initializer='zeros'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(512, kernel_initializer='truncated_normal', bias_initializer='zeros'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(1, kernel_initializer='truncated_normal', bias_initializer='zeros'))

model.summary()
opt = keras.optimizers.Adam(1e-4)
model.compile(loss='mse', optimizer=opt)
model.fit_generator(train_generator, steps_per_epoch=len(train_data),
                    validation_data=valid_generator, validation_steps=len(valid_data),
                    epochs=1, verbose=1)

model.save('model.h5')


#! /usr/bin/env python3

# scr_common.py
"""Defines for common methods shared across scripts"""

import os, sys

if 'scrjob' not in sys.path:
  sys.path.insert(0, '/'.join(os.path.realpath(__file__).split('/')[:-2]))
  import scrjob

import argparse, inspect
from subprocess import Popen, PIPE
import shlex
from scrjob import scr_const

def tracefunction(frame, event, arg):
  """This method provides a hook for tracing python calls

  Usage:  sys.settrace(scr_common.tracefunction)
  Prints: filename:function:linenum -> event

  The method below will be called on events as scripts execute.
  The print message in the try block attempts to print the filename.
  If the filename could not be obtained, an exception is thrown.

  This output is very verbose. Filters could be added for event type,
  and print messages could be suppressed when the filename is not known.
  """
  try:
    print(
        inspect.getfile(frame).split('/')[-1] + ':' +
        str(frame.f_code.co_name) + '():' + str(frame.f_lineno) + ' -> ' +
        str(event))
  except:
    print(
        str(frame.f_code.co_name) + '():' + str(frame.f_lineno) + ' -> ' +
        str(event))

def interpolate_variables(varstr):
  """This method will expand a string, including paths

  This method allows interpretation of path strings such as:
  ~ . ../ ../../

  Environment variables in the string will also be expanded.
  The input need not be a path.
  """
  if varstr is None or len(varstr) == 0:
    return ''
  # replace ~ and . symbols from front of path
  if varstr[0] == '~':
    varstr = '$HOME' + varstr[1:]
  elif varstr.startswith('..'):
    topdir = '/'.join(os.getcwd().split('/')[:-1])
    varstr = varstr[3:]
    while varstr.startswith('..'):
      topdir = '/'.join(topdir.split('/')[:-1])
      varstr = varstr[3:]
    varstr = topdir + '/' + varstr
  elif varstr[0] == '.':
    varstr = os.getcwd() + varstr[1:]
  if varstr[-1] == '/' and len(varstr) > 1:
    varstr = varstr[:-1]
  return os.path.expandvars(varstr)

def scr_prefix():
  """This method will return the prefix for SCR

  If the environment variable is not set,
  it will return the current working directory.
  Allows the environment variable to contain relative paths by returning
  the string after passing through interpolate_variables
  """
  prefix = os.environ.get('SCR_PREFIX')
  if prefix is None:
    return os.getcwd()
  # tack on current working dir if needed
  # don't resolve symlinks
  # don't worry about missing parts, the calling script calling might create it
  return interpolate_variables(prefix)

def runproc(argv, wait=True, getstdout=False, getstderr=False, verbose=False, shell=False):
  """This method will execute a command using subprocess.Popen

  Required argument
  -----------------
  argv        a string or a list, the command to be ran

  Returns
  -------
  tuple       2 values are returned as a tuple, the values depend on optional parameters
              When getting output (waiting, default):
                The first return value is the output, the second is the returncode
                If only getting stdout OR stderr, the first element will be a string
                If getting stdout AND stderr, the first element will be a list,
                where the first element of the list is stdout and the second is stderr
                The return code will be an integer
              When not getting output, when wait = False:
                When wait is False, this method immediately returns after calling Popen.
                The Popen object will be both the first and second return values
              On ERROR:
                On error this method returns None, None
                This has an exception if getstderr is True, then the first return value
                will contain stderr in the expected position, and the second value will be None

  Optional parameters
  -------------------
    All optional parameters are True/False boolean values
    The parameters getstdout and getstderr require waiting for the program to complete,
    and should not be used in combination with wait=True

  wait        If wait is False this method returns the Popen object as both elements of the tuple
  getstdout   If getstdout is True then this method will include stdout in the first return value
  getstderr   If getstderr is True then this method will include stderr in the first return value
  verbose     If verbose is True then this method will print the command to be executed
  shell       If shell is True, then argv is transformed into -> 'bash -c ' + shlex.quote(argv)

  The shell option is passed into the Popen call, and is supposed to prepend commands (?) with
    '/bin/sh -c', although when I tried simply using shell=True for 'which pdsh' I had errors.
  """
  if shell:
    if type(argv) is list:
      argv = ' '.join(argv)
    # following the security recommendation from subprocess.Popen:
    # turn the command into a shlex quoted string
    argv = 'bash -c ' + shlex.quote(argv)
  # allow caller to pass command as a string, as in:
  #   "ls -lt" rather than ["ls", "-lt"]
  elif type(argv) is str:
    argv = shlex.split(argv)

  if len(argv) < 1:
    return None, None
  try:
    # if verbose, print the command we will run to stdout
    if verbose:
      if type(argv) is list:
        print(" ".join(argv))
      else:
        print(argv)

    runproc = Popen(argv,
                    bufsize=1,
                    stdin=None,
                    stdout=PIPE,
                    stderr=PIPE,
                    shell=shell,
                    universal_newlines=True)
    if wait == False:
      return runproc, runproc
    if getstdout == True and getstderr == True:
      output = runproc.communicate()
      return output, runproc.returncode
    if getstdout == True:
      output = runproc.communicate()[0]
      return output, runproc.returncode
    if getstderr == True:
      output = runproc.communicate()[1]
      return output, runproc.returncode
    runproc.communicate()
    return None, runproc.returncode
  except Exception as e:
    print('runproc: ERROR: ' + str(e))
    if getstdout == True and getstderr == True:
      return ['', str(e)], None
    if getstdout == True:
      return '', None
    if getstderr == True:
      return str(e), None
    return None, None

# pipeproc works as runproc above, except argvs is a list of argv lists
# the first subprocess is opened and from there stdout is chained to stdin
# values returned (returncode/pid/stdout/stderr) will be from the final process
def pipeproc(argvs, wait=True, getstdout=False, getstderr=False):
  """This method is an extension of the above runproc method

  This is essentially duplicated code from the runproc.
   ### TODO : This functionality could be put into runproc, increasing
    the complexity slightly, or it could be kept separate.

  This method will loop through search argv in a list of argvs.
  The output of each previous 'runproc' will be piped to the next as stdin.
  Any return values will come from the result of the final Popen command
  """
  if len(argvs) < 1:
    return None, None
  if len(argvs) == 1:
    return runproc(argvs[0], wait, getstdout, getstderr)
  try:
    # split command into list if given as string
    cmd = argvs[0]
    if type(cmd) is str:
      cmd = shlex.split(cmd)

    nextprog = Popen(cmd,
                     bufsize=1,
                     stdin=None,
                     stdout=PIPE,
                     stderr=PIPE,
                     universal_newlines=True)

    for i in range(1, len(argvs)):
      # split command into list if given as string
      cmd = argvs[i]
      if type(cmd) is str:
        cmd = shlex.split(cmd)

      pipeprog = Popen(cmd,
                       stdin=nextprog.stdout,
                       stdout=PIPE,
                       stderr=PIPE,
                       bufsize=1,
                       universal_newlines=True)
      nextprog.stdout.close()
      nextprog = pipeprog
    if wait == False:
      return nextprog, nextprog.pid
    if getstdout == True and getstderr == True:
      output = nextprog.communicate()
      return output, nextprog.returncode
    if getstdout == True:
      output = nextprog.communicate()[0]
      return output, nextprog.returncode
    if getstderr == True:
      output = nextprog.communicate()[1]
      return output, nextprog.returncode
    return None, nextprog.returncode
  except Exception as e:
    print('pipeproc: ERROR: ' + str(e))
    if getstdout == True and getstderr == True:
      return ['', str(e)], 1
    if getstdout == True:
      return '', 1
    if getstderr == True:
      return str(e), 1
    return None, 1

def choose_bindir():
  """Determine appropriate location of binaries.

  Testing using "make test" or "make check" must operate based on the contents
  of the build directory.

  If the script is being run from the install directory, then return X_BINDIR
  as defined by cmake so installed scripts use installed binaries.  Otherwise,
  determine the appropriate build directory.
  """
  # Needed to find binaries in build dir when testing
  parent_of_this_module = '/'.join(os.path.realpath(__file__).split('/')[:-1])
  if scr_const.X_BINDIR in parent_of_this_module:
      bindir = scr_const.X_BINDIR  # path to install bin directory
  else:
      bindir = os.path.join(scr_const.CMAKE_BINARY_DIR + "/src/")

  return bindir

def log(bindir=None,
        prefix=None,
        username=None,
        jobname=None,
        jobid=None,
        start=None,
        event_type=None,
        event_note=None,
        event_dset=None,
        event_name=None,
        event_start=None,
        event_secs=None):
  """This method is a wrapper for commong logging operations

  Logging operations call the scr_log_event program with formatted arguments.

  This method formats an argv, then calls the scr_log_event method.

  Parameters
  ----------
  All input parameters are optional, with each just extending the log entry
  Input parameters should (with one exception) be string values

  event_note   may be a string or a dictionary, if a dictionary is passed then
               the remaining arguments are duplicated
               This allows several runproc calls without rebuilding argv each time
  """
  if prefix is None:
    prefix = scr_prefix()
    #print('log: prefix is required')
  if bindir is None:
    bindir = scr_const.X_BINDIR
  argv = [bindir + '/scr_log_event', '-p', prefix]
  if username is not None:
    argv.extend(['-u', username])
  if jobname is not None:
    argv.extend(['-j', jobname])
  if jobid is not None:
    argv.extend(['-i', jobid])
  if start is not None:
    argv.extend(['-s', start])
  if event_type is not None:
    argv.extend(['-T', event_type])
  if event_dset is not None:
    argv.extend(['-D', event_dset])
  if event_name is not None:
    argv.extend(['-n', event_name])
  if event_start is not None:
    argv.extend(['-S', event_start])
  if event_secs is not None:
    argv.extend(['-L', event_secs])
  if type(event_note) is dict:
    argv.extend(['-N', ''])
    lastarg = len(argv) - 1
    for key in event_note:
      argv[lastarg] = key + ':' + event_note[key]
      runproc(argv=argv)
  else:
    if event_note is not None:
      argv.extend(['-N', event_note])
    runproc(argv=argv)

if __name__ == '__main__':
  """This script allows being called as a standalone script

  This is meant for testing purposes, to directly call scr_common methods.
  """
  parser = argparse.ArgumentParser(add_help=False,
                                   argument_default=argparse.SUPPRESS,
                                   prog='scr_common')
  parser.add_argument('-h',
                      '--help',
                      action='store_true',
                      help='Show this help message and exit.')
  parser.add_argument('--interpolate',
                      metavar='<variable>',
                      type=str,
                      help='Interpolate a variable string.')
  parser.add_argument('--prefix',
                      action='store_true',
                      help='Print the SCR prefix.')
  parser.add_argument('--runproc',
                      nargs=argparse.REMAINDER,
                      help='Launch process with arguments')
  parser.add_argument(
      '--pipeproc',
      nargs=argparse.REMAINDER,
      help=
      'Launch processes and pipe output to other processes. (separate processes with a colon)'
  )
  parser.add_argument(
      '--log',
      nargs='+',
      metavar='<option=value>',
      help=
      'Create a log entry, available options: bindir, prefix, username, jobname, jobid, start, event_type, event_note, event_dset, event_name, event_start, event_secs'
  )
  args = vars(parser.parse_args())
  if 'help' in args:
    parser.print_help()
    sys.exit(1)
  if 'interpolate' in args:
    print('interpolate_variables(' + args['interpolate'] + ')')
    print('  -> ' + str(interpolate_variables(args['interpolate'])))
  if 'prefix' in args:
    print('scr_prefix()')
    print('  -> ' + str(scr_prefix()))
  if 'runproc' in args:
    print('runproc(' + ' '.join(args['runproc']) + ')')
    out, returncode = runproc(argv=args['runproc'],
                              getstdout=True,
                              getstderr=True)
    print('  process returned with code ' + str(returncode))
    print('  stdout:')
    print(out[0])
    print('  stderr:')
    print(out[1])
  if 'pipeproc' in args:
    printstr = 'pipeproc( '
    argvs = []
    argvs.append([])
    i = 0
    for arg in args['pipeproc']:
      if arg == ':':
        i += 1
        argvs.append([])
        printstr += '| '
      else:
        argvs[i].append(arg)
        printstr += arg + ' '
    print(printstr + ')')
    out, returncode = pipeproc(argvs=argvs, getstdout=True, getstderr=True)
    print('  final process returned with code ' + str(returncode))
    if out is not None:
      print('  stdout:')
      print(out[0])
      print('  stderr:')
      print(out[1])
  if 'log' in args:
    bindir, prefix, username, jobname = None, None, None, None
    jobid, start, event_type, event_note = None, None, None, None
    event_dset, event_name, event_start, event_secs = None, None, None, None
    printstr = 'log('
    for keyvalpair in args['log']:
      if '=' not in keyvalpair:
        continue
      vals = keyvalpair.split('=')
      if vals[0] == 'bindir':
        bindir = vals[1]
      elif vals[0] == 'prefix':
        prefix = vals[1]
      elif vals[0] == 'username':
        username = vals[1]
      elif vals[0] == 'jobname':
        jobname = vals[1]
      elif vals[0] == 'jobid':
        jobid = vals[1]
      elif vals[0] == 'start':
        start = vals[1]
      elif vals[0] == 'event_type':
        event_type = vals[1]
      elif vals[0] == 'event_note':
        event_note = vals[1]
      elif vals[0] == 'event_dset':
        event_dset = vals[1]
      elif vals[0] == 'event_name':
        event_name = vals[1]
      elif vals[0] == 'event_start':
        event_start = vals[1]
      elif vals[0] == 'event_secs':
        event_secs = vals[1]
      else:
        continue
      printstr += keyvalpair + ', '
    if printstr[-1] == '(':
      print('Nothing to log, see \'--help\' for available value pairs.')
      sys.exit(1)
    print(printstr[:-2] + ')')
    log(bindir=bindir,
        prefix=prefix,
        username=username,
        jobname=jobname,
        jobid=jobid,
        start=start,
        event_type=event_type,
        event_note=event_note,
        event_dset=event_dset,
        event_name=event_name,
        event_start=event_start,
        event_secs=event_secs)


import numpy as np
import cv2
import matplotlib.pyplot as plt

#  Loading the image to be testedls
# gets image from camera
cam = cv2.VideoCapture(0)

def detectobject(test_image):
     #test_image = cv2.imread('lays2.jpg')

     test_image_gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)
     #classifiers
     lays_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/Lays_classifier.xml')
     clorox_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/Clorox_classifier.xml')
     kellogg_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/kellogs_classifier.xml')
     cheetos_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/cheetos_classifier.xml')
     kitkat_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/kitkat_classifier.xml')
     vaseline_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/vaseline_classifier.xml')
     pepsi_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/pepsi_classifier.xml')
     clif_clf = cv2.CascadeClassifier('/home/pi/finalProj/Cascades/clif_classifier.xml')

     #rects
     lays_rects = lays_clf.detectMultiScale(test_image_gray)
     clorox_rects = clorox_clf.detectMultiScale(test_image_gray)
     kellogg_rects = kellogg_clf.detectMultiScale(test_image_gray)
     cheetos_rects = cheetos_clf.detectMultiScale(test_image_gray)
     kitkat_rects = kitkat_clf.detectMultiScale(test_image_gray)
     vaseline_rects = vaseline_clf.detectMultiScale(test_image_gray)
     pepsi_rects = pepsi_clf.detectMultiScale(test_image_gray)
     clif_rects = clif_clf.detectMultiScale(test_image_gray)

     print('Objects found: ', len(lays_rects)+len(clorox_rects)++len(kellogg_rects)+
             len(cheetos_rects)+len(kitkat_rects)+len(vaseline_rects)+len(pepsi_rects)+
             len(clif_rects))

     for x,y,w,h in lays_rects:
         cv2.rectangle(test_image, (x, y), (x+w, y+h), (0, 0, 255), 2)
         cv2.putText(test_image,'Lays',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in clorox_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (255, 0, 255), 2)
         cv2.putText(test_image,'Clorox',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in kellogg_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (0, 255, 0), 2)
         cv2.putText(test_image,'kellogg',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in kitkat_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (0, 255, 0), 2)
         cv2.putText(test_image,'Kitkat',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in cheetos_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (0, 255, 0), 2)
         cv2.putText(test_image,'Cheetos',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in vaseline_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (0, 255, 0), 2)
         cv2.putText(test_image,'Vaseline',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in pepsi_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (0, 255, 0), 2)
         cv2.putText(test_image,'Pepsi',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     for x,y,w,h in clif_rects:
         cv2.rectangle(test_image, (x,y), (x+w, y+h), (0, 255, 0), 2)
         cv2.putText(test_image,'Clif',(x-5,y-5),cv2.FONT_HERSHEY_COMPLEX,0.5,2)

     #plt.imshow(cv2.cvtColor(test_image, cv2.COLOR_BGR2RGB))
     #plt.show()

def sift(img1,img2,thresh):
     #reading image
     #img1 = cv2.imread('/home/pi/finalProj/image/lays1.jpg')
     #img2 = cv2.imread('lays2.jpg')

     img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
     img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

     #sift
     sift = cv2.xfeatures2d.SIFT_create()

     keypoints_1, descriptors_1 = sift.detectAndCompute(img1,None)
     keypoints_2, descriptors_2 = sift.detectAndCompute(img2,None)

     FLANN_INDEX_KDTREE = 0

     index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
     search_params = dict(checks=50)   # or pass empty dictionary

     flann = cv2.FlannBasedMatcher(index_params,search_params)

     matches = flann.knnMatch(descriptors_1,descriptors_2,k=2)

     # Need to draw only good matches, so create a mask
     matchesMask = [[0,0] for i in range(len(matches))]
     count = 0
     # ratio test as per Lowe's paper
     for i,(m,n) in enumerate(matches):
          if m.distance < 0.7*n.distance:
               count +=1
               matchesMask[i]=[1,0]

     draw_params = dict(matchColor = (0,255,0),
                    singlePointColor = (255,0,0),
                    matchesMask = matchesMask,
                    flags = 0)

     #img3 = cv2.drawMatchesKnn(img1,keypoints_1,img2,keypoints_2,matches,None,**draw_params)
     print(len(matches))
     print(count)
     if count >= thresh:  # 10 for lays
          print("Detected clorox")
     #plt.imshow(img3,),plt.show()

while(True):
    val, img = cam.read()
    img_spec = cv2.imread('/home/pi/finalProj/image/clorox.jpg')
    detectobject(img)
    cv2.imshow('shopping',img)
    cv2.waitKey(25)
    sift(img_spec,img,12)


import typing
from pathlib import Path

from .PackageRef import PackageRef

class FilesPackageMapping():
	__slots__ = ("ref", "files")

	def __init__(self, ref=None, files=None):
		self.ref = ref
		self.files = files

class IFile2Package():
	def __getattr__(self, file: typing.Union[str, Path]) -> PackageRef:
		raise NotImplementedError()

class IFile2PackagePopulator():
	ID = None
	def __call__(self) -> typing.Iterable[FilesPackageMapping]:
		raise NotImplementedError


# read file, write files/create files

f = open('input.txt', 'r')
maleFile = open('file/MaleFile.txt', 'w')
femaleFile = open('file/FemaleFile.txt', 'w')
for line in f:
    line_split = line.split()
    if line_split[2] == 'Male':
        maleFile.write(line)
    else:
        femaleFile.write(line)
f.close()
maleFile.close()
femaleFile.close()


import sys
import warnings
warnings.filterwarnings('ignore',category=FutureWarning)
from collections import Counter
from src.get_masks.evaluate_quality_masks import Quality_masks
from src.classifiers.classifier_all import All_classifier, evaluate_all
from src.ToT.table_of_truth import ToT
from src.data_classifier.Generator_proba_classifier import Genrator_data_prob_classifier
from src.get_masks.get_masks import Get_masks
from src.nn.nn_model_ref import NN_Model_Ref
from src.data_cipher.create_data import Create_data_binary
from src.utils.initialisation_run import init_all_for_run, init_cipher
from src.utils.config import Config
import argparse
from src.utils.utils import str2bool, two_args_str_int, two_args_str_float, str2list, transform_input_type, str2hexa, \
    transform_input_type2
import numpy as np
import pandas as pd
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# initiate the parser

config = Config()
parser = argparse.ArgumentParser()

parser.add_argument("--seed", default=config.general.seed, type=two_args_str_int, choices=[i for i in range(100)])
parser.add_argument("--device", default=config.general.device, type=two_args_str_int, choices=[i for i in range(100)])
parser.add_argument("--logs_tensorboard", default=config.general.logs_tensorboard)
parser.add_argument("--models_path", default=config.general.models_path)
parser.add_argument("--models_path_load", default=config.general.models_path_load)
parser.add_argument("--cipher", default=config.general.cipher, choices=["speck", "simon", "aes228", "aes224", "simeck", "gimli"])
parser.add_argument("--nombre_round_eval", default=config.general.nombre_round_eval, type=two_args_str_int)
parser.add_argument("--inputs_type", default=config.general.inputs_type, type=transform_input_type)
parser.add_argument("--word_size", default=config.general.word_size, type=two_args_str_int)
parser.add_argument("--alpha", default=config.general.alpha, type=two_args_str_int)
parser.add_argument("--beta", default=config.general.beta, type=two_args_str_int)
parser.add_argument("--type_create_data", default=config.general.type_create_data, choices=["normal", "real_difference"])

parser.add_argument("--diff", default=config.train_nn.diff, type=str2hexa)
parser.add_argument("--retain_model_gohr_ref", default=config.train_nn.retain_model_gohr_ref, type=str2bool)
parser.add_argument("--load_special", default=config.train_nn.load_special, type=str2bool)
parser.add_argument("--finetunning", default=config.train_nn.finetunning, type=str2bool)
parser.add_argument("--model_finetunne", default=config.train_nn.model_finetunne, choices=["baseline", "cnn_attention", "multihead", "deepset", "baseline_bin"])
parser.add_argument("--load_nn_path", default=config.train_nn.load_nn_path)
parser.add_argument("--countinuous_learning", default=config.train_nn.countinuous_learning, type=str2bool)
parser.add_argument("--curriculum_learning", default=config.train_nn.curriculum_learning, type=str2bool)
parser.add_argument("--nbre_epoch_per_stage", default=config.train_nn.nbre_epoch_per_stage, type=two_args_str_int)
parser.add_argument("--a_bit", default=config.train_nn.a_bit, type=two_args_str_int)
parser.add_argument("--type_model", default=config.train_nn.type_model, choices=["baseline_real", "baseline", "cnn_attention", "multihead", "deepset", "baseline_bin", "baseline_bin_v2"])
parser.add_argument("--nbre_sample_train", default=config.train_nn.nbre_sample_train, type=two_args_str_int)
parser.add_argument("--nbre_sample_eval", default=config.train_nn.nbre_sample_eval, type=two_args_str_int)
parser.add_argument("--num_epochs", default=config.train_nn.num_epochs, type=two_args_str_int)
parser.add_argument("--batch_size", default=config.train_nn.batch_size, type=two_args_str_int)
parser.add_argument("--loss_type", default=config.train_nn.loss_type, choices=["BCE", "MSE", "SmoothL1Loss", "CrossEntropyLoss", "F1"])
parser.add_argument("--lr_nn", default=config.train_nn.lr_nn, type=two_args_str_float)
parser.add_argument("--weight_decay_nn", default=config.train_nn.weight_decay_nn, type=two_args_str_float)
parser.add_argument("--momentum_nn", default=config.train_nn.momentum_nn, type=two_args_str_float)
parser.add_argument("--base_lr", default=config.train_nn.base_lr, type=two_args_str_float)
parser.add_argument("--max_lr", default=config.train_nn.max_lr, type=two_args_str_float)
parser.add_argument("--demicycle_1", default=config.train_nn.demicycle_1, type=two_args_str_int)
parser.add_argument("--optimizer_type", default=config.train_nn.optimizer_type, choices=["Adam", "AdamW", "SGD"])
parser.add_argument("--scheduler_type", default=config.train_nn.scheduler_type, choices=["CyclicLR", "None"])
parser.add_argument("--numLayers", default=config.train_nn.numLayers, type=two_args_str_int)
parser.add_argument("--limit", default=config.train_nn.limit, type=two_args_str_int)
parser.add_argument("--kstime", default=config.train_nn.kstime, type=two_args_str_int)
parser.add_argument("--out_channel0", default=config.train_nn.out_channel0, type=two_args_str_int)
parser.add_argument("--out_channel1", default=config.train_nn.out_channel1, type=two_args_str_int)
parser.add_argument("--hidden1", default=config.train_nn.hidden1, type=two_args_str_int)
parser.add_argument("--hidden2", default=config.train_nn.hidden2, type=two_args_str_int)
parser.add_argument("--kernel_size0", default=config.train_nn.kernel_size0, type=two_args_str_int)
parser.add_argument("--kernel_size1", default=config.train_nn.kernel_size1, type=two_args_str_int)
parser.add_argument("--num_workers", default=config.train_nn.num_workers, type=two_args_str_int)
parser.add_argument("--clip_grad_norm", default=config.train_nn.clip_grad_norm, type=two_args_str_float)
parser.add_argument("--end_after_training", default=config.train_nn.end_after_training, type=str2bool)

parser.add_argument("--load_masks", default=config.getting_masks.load_masks, type=str2bool)
parser.add_argument("--file_mask", default=config.getting_masks.file_mask)
parser.add_argument("--nbre_max_masks_load", default=config.getting_masks.nbre_max_masks_load, type=two_args_str_int)
parser.add_argument("--nbre_generate_data_train_val", default=config.getting_masks.nbre_generate_data_train_val, type=two_args_str_int)
parser.add_argument("--nbre_necessaire_val_SV", default=config.getting_masks.nbre_necessaire_val_SV, type=two_args_str_int)
parser.add_argument("--nbre_max_batch", default=config.getting_masks.nbre_max_batch, type=two_args_str_int)
parser.add_argument("--liste_segmentation_prediction", default=config.getting_masks.liste_segmentation_prediction)
parser.add_argument("--liste_methode_extraction", default=config.getting_masks.liste_methode_extraction, type=transform_input_type)
parser.add_argument("--liste_methode_selection", default=config.getting_masks.liste_methode_selection, type=transform_input_type)
parser.add_argument("--hamming_weigth", default=config.getting_masks.hamming_weigth, type=transform_input_type2)
parser.add_argument("--thr_value", default=config.getting_masks.thr_value, type=str2list)
parser.add_argument("--research_new_masks", default=config.getting_masks.research_new_masks, type=str2bool)
parser.add_argument("--save_fig_plot_feature_before_mask", default=config.getting_masks.save_fig_plot_feature_before_mask, type=str2bool)
parser.add_argument("--end_after_step2", default=config.getting_masks.end_after_step2, type=str2bool)

parser.add_argument("--create_new_data_for_ToT", default=config.make_ToT.create_new_data_for_ToT, type=str2bool)
parser.add_argument("--create_ToT_with_only_sample_from_cipher", default=config.make_ToT.create_ToT_with_only_sample_from_cipher, type=str2bool)
parser.add_argument("--nbre_sample_create_ToT", default=config.make_ToT.nbre_sample_create_ToT, type=two_args_str_int)

parser.add_argument("--create_new_data_for_classifier", default=config.make_data_classifier.create_new_data_for_classifier, type=str2bool)
parser.add_argument("--nbre_sample_train_classifier", default=config.make_data_classifier.nbre_sample_train_classifier, type=two_args_str_int)
parser.add_argument("--nbre_sample_val_classifier", default=config.make_data_classifier.nbre_sample_val_classifier, type=two_args_str_int)

parser.add_argument("--retrain_nn_ref", default=config.compare_classifer.retrain_nn_ref, type=str2bool)
parser.add_argument("--num_epch_2", default=config.compare_classifer.num_epch_2, type=two_args_str_int)
parser.add_argument("--batch_size_2", default=config.compare_classifer.batch_size_2, type=two_args_str_int)
parser.add_argument("--classifiers_ours", default=config.compare_classifer.classifiers_ours, type=str2list)
parser.add_argument("--retrain_with_import_features", default=config.compare_classifer.retrain_with_import_features, type=str2bool)
parser.add_argument("--keep_number_most_impactfull", default=config.compare_classifer.keep_number_most_impactfull, type=two_args_str_int)
parser.add_argument("--num_epch_our", default=config.compare_classifer.num_epch_our, type=two_args_str_int)
parser.add_argument("--batch_size_our", default=config.compare_classifer.batch_size_our, type=two_args_str_int)
parser.add_argument("--alpha_test", default=config.compare_classifer.alpha_test, type=two_args_str_float)
parser.add_argument("--quality_of_masks", default=config.compare_classifer.quality_of_masks, type=str2bool)
parser.add_argument("--end_after_step4", default=config.compare_classifer.end_after_step4, type=str2bool)
parser.add_argument("--eval_nn_ref", default=config.compare_classifer.eval_nn_ref, type=str2bool)
parser.add_argument("--compute_independance_feature", default=config.compare_classifer.compute_independance_feature, type=str2bool)
parser.add_argument("--save_data_proba", default=config.compare_classifer.save_data_proba, type=str2bool)

args = parser.parse_args()

#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#
print("---" * 100)
writer, device, rng, path_save_model, path_save_model_train, name_input = init_all_for_run(args)

print("LOAD CIPHER")
print()
cipher = init_cipher(args)
creator_data_binary = Create_data_binary(args, cipher, rng)

#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
print("---" * 100)
print("STEP 1 : LOAD/ TRAIN NN REF")
print()
print("COUNTINUOUS LEARNING: "+ str(args.countinuous_learning) +  " | CURRICULUM LEARNING: " +  str(args.curriculum_learning) + " | MODEL: " + str(args.type_model))
print()

"""nombre_round_eval = args.nombre_round_eval
args.nombre_round_eval = nombre_round_eval - 2
nn_model_ref2 = NN_Model_Ref(args, writer, device, rng, path_save_model, cipher, creator_data_binary, path_save_model_train)
nn_model_ref2.epochs = 10
nn_model_ref2.train_general(name_input)
args.nombre_round_eval = nombre_round_eval - 1
nn_model_ref3 = NN_Model_Ref(args, writer, device, rng, path_save_model, cipher, creator_data_binary, path_save_model_train)
nn_model_ref3.epochs = 10
nn_model_ref3.net = nn_model_ref2.net
nn_model_ref3.train_general(name_input)
args.nombre_round_eval = nombre_round_eval
nn_model_ref = NN_Model_Ref(args, writer, device, rng, path_save_model, cipher, creator_data_binary, path_save_model_train)
nn_model_ref.net = nn_model_ref3.net"""

nn_model_ref = NN_Model_Ref(args, writer, device, rng, path_save_model, cipher, creator_data_binary, path_save_model_train)

if args.retain_model_gohr_ref:
    nn_model_ref.train_general(name_input)
else:
    #nn_model_ref.load_nn()
    try:
        if args.finetunning:
            nn_model_ref.load_nn()
            nn_model_ref.train_from_scractch(name_input + "fine-tune")
        #nn_model_ref.eval(["val"])
        else:
            nn_model_ref.load_nn()
    except:
        print("ERROR")
        print("NO MODEL AVALAIBLE FOR THIS CONFIG")
        print("CHANGE ARGUMENT retain_model_gohr_ref")
        print()
        sys.exit(1)

if args.create_new_data_for_ToT and args.create_new_data_for_classifier:
    del nn_model_ref.X_train_nn_binaire, nn_model_ref.X_val_nn_binaire, nn_model_ref.Y_train_nn_binaire, nn_model_ref.Y_val_nn_binaire
    del nn_model_ref.c0l_train_nn, nn_model_ref.c0l_val_nn, nn_model_ref.c0r_train_nn, nn_model_ref.c0r_val_nn
    del nn_model_ref.c1l_train_nn, nn_model_ref.c1l_val_nn, nn_model_ref.c1r_train_nn, nn_model_ref.c1r_val_nn

print("STEP 1 : DONE")
print("---" * 100)
if args.end_after_training:
    sys.exit(1)

#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
print("STEP 2 : GET MASKS")
print()
print("LOAD MASKS: "+ str(args.load_masks) +  " | RESEARCH NEW: " +  str(args.research_new_masks) + " | MODEL: " + str(args.liste_segmentation_prediction) +" " + str(args.liste_methode_extraction)+" " + str(args.liste_methode_selection)+" " + str(args.hamming_weigth)+" " + str(args.thr_value))
print()
get_masks_gen = Get_masks(args, nn_model_ref.net, path_save_model, rng, creator_data_binary, device)
if args.research_new_masks:
    get_masks_gen.start_step()
    get_masks_gen.save_masks(path_save_model)
    del get_masks_gen.X_deltaout_train, get_masks_gen.X_eval, get_masks_gen.Y_tf, get_masks_gen.Y_eval

print("STEP 2 : DONE")
print("---" * 100)
if args.end_after_step2:
    sys.exit(1)

#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
print("STEP 3 : MAKE M-ODT")
print()
print("NEW DATA: "+ str(args.create_new_data_for_ToT) +  " | PURE ToT: " +  str(args.create_ToT_with_only_sample_from_cipher) )
print()

table_of_truth = ToT(args, nn_model_ref.net, path_save_model, rng, creator_data_binary, device, get_masks_gen.masks, nn_model_ref)
table_of_truth.create_DDT()

del table_of_truth.c0l_create_ToT, table_of_truth.c0r_create_ToT
del table_of_truth.c1l_create_ToT, table_of_truth.c1r_create_ToT

print("STEP 3 : DONE")
print("---" * 100)
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
print("STEP 4 : CREATE DATA PROBA AND CLASSIFY")
print()
print("NEW DATA: "+ str(args.create_new_data_for_classifier))
print()

generator_data = Genrator_data_prob_classifier(args, nn_model_ref.net, path_save_model, rng, creator_data_binary, device, get_masks_gen.masks, nn_model_ref)
generator_data.create_data_g(table_of_truth)
#EVALUATE GOHR NN ON NEW DATASET
nn_model_ref.X_train_nn_binaire = generator_data.X_bin_train
nn_model_ref.X_val_nn_binaire = generator_data.X_bin_val
nn_model_ref.Y_train_nn_binaire = generator_data.Y_create_proba_train
nn_model_ref.Y_val_nn_binaire = generator_data.Y_create_proba_val

if args.eval_nn_ref:
    nn_model_ref.eval_all(["train", "val"])
all_clfs = All_classifier(args, path_save_model, generator_data, get_masks_gen, nn_model_ref, table_of_truth)
#all_clfs.X_train_proba = np.concatenate((all_clfs.X_train_proba, X_feat_temp), axis = 1)
#all_clfs.X_eval_proba =  np.concatenate((all_clfs.X_eval_proba, X_feat_temp_val), axis = 1)
all_clfs.classify_all()

if args.quality_of_masks:
    qm = Quality_masks(args, path_save_model, generator_data, get_masks_gen, nn_model_ref, table_of_truth, all_clfs)
    qm.start_all()
else:
    qm = None

print("STEP 4 : DONE")
print("---" * 100)
if args.end_after_step4:
    sys.exit(1)
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
print("STEP 5: START EVALUATE ClASSIFIERS")
print()

evaluate_all(all_clfs, generator_data, nn_model_ref, table_of_truth, qm, path_save_model)


import cv2
from pyueye import ueye
from image_tools.pyueye_example_camera import Camera
from image_tools.pyueye_example_utils import ImageData, ImageBuffer
import time
import numpy as np

# Initialize camera
cam = Camera()
cam.init()

nRet = ueye.is_ResetToDefault(cam.handle())

# Change the format to 1280x960
formatID = ueye.UINT(8)
nRet = ueye.is_ImageFormat(cam.handle(), ueye.IMGFRMT_CMD_SET_FORMAT, formatID, ueye.sizeof(formatID))

cam.alloc()  # Allocate image_tools memory

# Disable auto exposure
dblEnable = ueye.DOUBLE(0)
dblDummy = ueye.DOUBLE(0)
ueye.is_SetAutoParameter(cam.handle(), ueye.IS_SET_ENABLE_AUTO_SENSOR_GAIN_SHUTTER, dblEnable, dblDummy)

newExposure = ueye.DOUBLE(30)
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, newExposure, ueye.sizeof(newExposure))

ueye.is_Focus(cam.handle(), ueye.FOC_CMD_SET_DISABLE_AUTOFOCUS, None, 0)  # Disable autofocus

focus_overview = ueye.INT(205)  # Focus value for overview image_tools (taken from 570mm above table)
focus_closeup = ueye.INT(144)  # Focus value for closeup image_tools (taken from 190mm above table)

"""
exp_min = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE_RANGE_MIN, exp_min, ueye.sizeof(exp_min))
exp_max = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE_RANGE_MAX, exp_max, ueye.sizeof(exp_max))
print(exp_min, exp_max)
exp_def = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE_DEFAULT, exp_def, ueye.sizeof(exp_def))
print(exp_def)

disable = ueye.DOUBLE(0)
dummy = ueye.DOUBLE(0)
ret = ueye.is_SetAutoParameter(cam.handle(), ueye.IS_SET_ENABLE_AUTO_SENSOR_GAIN_SHUTTER, disable, dummy)
exp_val = ueye.DOUBLE(10.0)
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, exp_val, ueye.sizeof(exp_val))

exp_def = ueye.DOUBLE()
ret = ueye.is_Exposure(cam.handle(), ueye.IS_EXPOSURE_CMD_GET_EXPOSURE, exp_def, ueye.sizeof(exp_def))
print("set exp to", exp_def)"""