from django.shortcuts import render
from django.http import Http404
from eventapp.models import Event
import datetime
from eventapp.forms import ContactForm
from django.core.mail import send_mail
from eventapp.forms import ContactForm
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
from django.shortcuts import redirect
# Create your views here.
def index(request):
	events = Event.objects.all()
	y = 0
	for x in events:
		y += 1

	return render(request, 'eventapp/home.html',{
			'events' : events,
			'count': y,
		})

def current(request):
	events = Event.objects.all()
	return render(request, 'eventapp/index.html',{
			'events' : events,
			'date': datetime.datetime.now(),
		})
def event_detail(request, id):
	event =  Event.objects.get(id = id)
	try:
		event =  Event.objects.get(id = id)
	except Event.DoesNotExist:
		raise Http404('Tis event doesnt exist')
	return render(request, 'eventapp/event_detail.html',{
			'event' : event,
		})
def sendmail(request):
	events = Event.objects.all()
	return render(request, 'eventapp/sent.html',{
			'events' : events,
			'date': datetime.datetime.now(),
		})

def email(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['mona.kwamboka@gmail.com'], fail_silently=False)
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('thanks')
    return render(request, "eventapp/email.html", {'form': form})

def thanks(request):
    return HttpResponse('Thank you for your message.')
def contact(request):
	form_class = ContactForm
	if request.method == 'POST':
		form = form_class(data=request.POST)
		if form.is_valid():
			contact_name = request.POST.get(
                'contact_name'
            , '')
			contact_email = request.POST.get(
                'contact_email'
            , '')
			form_content = request.POST.get('content', '')

            # Email the profile with the
            # contact information
			template = get_template('eventapp/contact_template.txt')
			context = Context({
                'contact_name': contact_name,
                'contact_email': contact_email,
                'form_content': form_content,
            })
			content = template.render(context)
			email = EmailMessage(
                "New contact form submission",
                content,
                "Your website" +'',
                ['mona.kwamboka@gmail.com'],
                headers = {'Reply-To': contact_email }
            )
			email.send()
			return redirect('eventapp/contacts.html')

	return render(request, 'eventapp/contacts.html', {
        'form': form_class,
    })


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

img = cv2.imread(r'D:\Btech practicals\DIP\lena.png',0)

def binn(num):
    bin_num = [int(i) for i in list('{0:0b}'.format(num))]
    for j in range(8 - len(bin_num)):
        bin_num.insert(0,0)
    return bin_num

def decimal(listt):
    x = 0
    for i in range(8):
        x = x + int(listt[i])*(2**(7-i))
    return x

def bit_change(bit,img):
    z = np.zeros([512,512])
    for i in range(512):
        for j in range(512):
            x = binn(img[i][j])
            for k in range(8):
                if k == bit:
                    x[k] = x[k]
                else:
                    x[k] = 0
            x1 = decimal(x)
            z[i][j] = x1
    return z

if __name__ == "__main__":
    for i in range(1,9):
        plt.subplot(2,4,i)
        plt.imshow(bit_change(i-1,img),cmap = 'gray')

2016BEC035 Shubham Innani/12. Implementation of Zooming and Shrinking/dip/prac8/bitplane.py


__author__ = 'penglong_feng'
CSRF_ENABLED = True
SECRET_KEY = 'xt-erpcloud.kingdee.com'

#mongo setting
MONGO_HOST='192.168.22.198'
MONGO_PORT='27017'
MONGO_DBNAME='paylm'
#  PREFIX_AUTO_START_REQUEST, PREFIX_REPLICA_SET, PREFIX_READ_PREFERENCE, PREFIX_USERNAM


# ERROR -1: program entries are wrong
# ERROR -2: invalid files
# RETURN 1: finished by number of interactions

import sys
import os

import kNN
import genetic_algorithm

def read_file():
    if(len(sys.argv) != 4):
        print('\n\n---------------> ERROR: -1 <---------------')
        print('Run programm with: ')
        print('# python .\\main.py .\\training_file.txt .\\test_line.txt value_of_k\n\n')
        return -1

    if(os.path.isfile(str(sys.argv[1])) == False or os.path.isfile(str(sys.argv[2])) == False):
        print('\n\n---------------> ERROR: -2 <---------------')
        print('Invalid Files!\n\n')
        return -2

    print('\n\nProgram developed by Vitor Bueno (RA: 1921959) for Artificial Intelligence Subject at the Federal Technological University of Paraná - Câmpus Campo Mourão')
    print('§§§§§§§§§§§ k-NN and Genetic Algorithms §§§§§§§§§§§\n\n')

    k = int(sys.argv[3])
    training_file = open(str(sys.argv[1]), 'r')
    test_file = open(str(sys.argv[2]), 'r')

    training_schema = []
    line_file_training = training_file.readline()
    while line_file_training:
        array_float = [float(i) for i in line_file_training.rstrip('\n\r').split(' ')]
        training_schema.append(array_float)
        line_file_training = training_file.readline()

    test_schema = []
    line_file_test = test_file.readline()
    while line_file_test:
        array_float = [float(i) for i in line_file_test.rstrip('\n\r').split(' ')]
        test_schema.append(array_float)
        line_file_test = test_file.readline()

    return training_schema, test_schema, k

def main():
    training_schema, test_schema, k = read_file()

    genetic_algorithm.main(training_schema, test_schema, k)
    # kNN.main(training_schema, test_schema, k)

main()


# -*- coding: utf-8 -*-
import os
import sys

SPARK_HOME = '/home/hadoop'  + '/spark'

os.environ['SPARK_HOME'] = SPARK_HOME
sys.path.append(SPARK_HOME + "/python")
sys.path.append(SPARK_HOME + "/python/lib/py4j-0.10.7-src.zip")
print(sys.path)

from pyspark import SparkContext, SparkConf, Row

appName = "spark2django"  # 应用程序名称
master = "spark://localhost:8080"  # 222.28.78.90:7077为主节点ip和端口，请换成自己的主节点主机名称
conf = SparkConf().setAppName(appName).setMaster(master)
sc = SparkContext(conf=conf)
def getTest():
    data = [1, 2, 3, 4, 5]
    distData = sc.parallelize(data)
    res = distData.reduce(lambda a, b: a + b)
    print (res)
    return res
def textfile():
    text_file = sc.textFile("hdfs:///sparkdir/dfile1")
    print(text_file.collect())
    df = text_file.map(lambda r: Row(r)).toDF(["line"])
    print(df)
if __name__ == '__main__':
    getTest()
    #textfile()


import spotipy
import spotipy.util as util
import os
import json
import pandas as pd
import sys
import spotify_utils
import time
import youtube_search

reload(sys)
sys.setdefaultencoding("utf8")

# define constants
spotify = spotipy.Spotify()
pause_duration = 0.25
base_q = "track:\"{}\" artist:\"{}\" album:\"{}\""

# variables to store cache every x times
requests_count = 0
cache_save_frequency = 10

def get_duration(track, artist, album, is_clean):
    global requests_count
    requests_count += 1

    if is_clean:
        q = base_q.format(track, artist, album)
    else:
        q = get_cleaned_q(track, artist, album)

    if q in song_cache:
        print("{} out of {} - {}: {} milliseconds".format(requests_count + 1, df.shape[0], track, song_cache[q]))
        return song_cache[q]

    # time.sleep(pause_duration)

    results = spotify.search(q=q, type="track")

    if len(results["tracks"]["items"]) == 0:
        return

    duration = results["tracks"]["items"][0]["duration_ms"]
    song_cache[q] = duration
    print("{} out of {} - {}: {} milliseconds".format(requests_count + 1, df.shape[0], track, song_cache[q]))

    if requests_count % cache_save_frequency == 0:
        spotify_utils.save_cache_to_disk(song_cache)
    return duration

def get_duration_from_lastfm(mbid):
    global requests_count
    requests_count += 1
    if mbid in song_cache:
        print("{} out of {} - {} milliseconds".format(requests_count + 1, df.shape[0], song_cache[mbid]))
        return song_cache[mbid]

    time.sleep(pause_duration)

    response = spotify_utils.get_response(mbid)
    duration = response["track"]["duration"]

    song_cache[mbid] = duration
    print("{} out of {} - {} milliseconds".format(requests_count + 1, df.shape[0], song_cache[mbid]))
    if requests_count % cache_save_frequency == 0:
        spotify_utils.save_cache_to_disk(song_cache)
    return duration

def get_duration_from_youtube(q):
    global requests_count
    requests_count += 1

    if q in song_cache:
        print("{} out of {} - {} milliseconds".format(requests_count + 1, df.shape[0], song_cache[q]))
        return song_cache[q]

    duration = youtube_search.youtube_search(q)
    song_cache[q] = duration
    print("{} out of {} - {} milliseconds".format(requests_count + 1, df.shape[0], song_cache[q]))
    if requests_count % cache_save_frequency == 0:
        spotify_utils.save_cache_to_disk(song_cache)
    return duration

def title_cleaner(string):
    first_dash = string.find("[")
    first_parenthesis = string.find("(")
    if first_dash != -1 and first_parenthesis != -1:
        min_index = min(first_dash, first_parenthesis)
        return string[:min_index]
    if first_dash == -1 and first_parenthesis == -1:
        return string
    if first_dash != -1:
        return string[:first_dash]
    return string[:first_parenthesis]

def get_cleaned_q(track, artist, album):
    track = title_cleaner(track)
    artist = title_cleaner(artist)
    album = title_cleaner(album)
    q = base_q.format(track, artist, album)
    return q

foo = lambda x: get_duration(x["Track"], x["Artist"], x["Album"], x["Duration"] > 0)

song_cache = spotify_utils.get_cache()
# df = pd.read_csv("../data/2016-05.csv", encoding="utf-8")
# df["Duration"] = df.apply(lambda x: get_duration(x["Track"], x["Artist"], x["Album"], True), axis=1)
# spotify_utils.save_cache_to_disk(song_cache)
# df.to_csv("data/songs.csv", index=None, encoding="utf-8")

df = pd.read_csv("data/songs.csv", encoding="utf-8")

# df["Duration"] = df.apply(foo, axis=1)
# spotify_utils.save_cache_to_disk(song_cache)
# df.to_csv("data/songs.csv", encoding="utf-8", index=None)
#
# df[df["Duration"].isnull() & df["Track mbid"].notnull()] = df[
#     df["Duration"].isnull() & df["Track mbid"].notnull()].apply(
#     lambda x: get_duration_from_lastfm(x["Track mbid"]), axis=1)

# for index, row in df[df["Duration"].isnull() & df["Track mbid"].notnull()].iterrows():
#     duration = get_duration_from_lastfm(row["Track mbid"])
#     df.loc[index, "Duration"] = duration

# for index, row in df[df["Duration"].isnull()].iterrows():
#     q = "{} {}".format(row["Artist"], row["Track"])
#     if row["Track"] == "Music":
#         q = "{} {} {}".format(row["Artist"], row["Track"], row["Album"])
#
#     print(q)
#     duration = get_duration_from_youtube(q)
#     df.loc[index, "Duration"] = duration
#
# spotify_utils.save_cache_to_disk(song_cache)
# df.to_csv("data/songs.csv", index=None, encoding="utf-8")


#  Hint:  You may not need all of these.  Remove the unused functions.
class Ticket:
    def __init__(self, source, destination):
        self.source = source
        self.destination = destination

def reconstruct_trip(tickets, length):
    # hash each ticket with source and destination being key and value
    ticket_cache = {}
    route = [None] * length

    for ticket in tickets:
        ticket_cache[ticket.source] = ticket.destination
    next = ticket_cache["NONE"]

    for i in range(0, length):
        route[i] = next
        next = ticket_cache[next]
    return route

# for testing


import json

import mock
import pytest
import responses
from apksearch.entities import PackageBase, PackageVariant, PackageVersion

from mapadroid.mad_apk import utils, wizard
from mapadroid.mad_apk.apk_enums import APKArch
from mapadroid.utils.global_variables import BACKEND_SUPPORTED_VERSIONS

mock_versions = {
    "32": [
        "0.123.0",
        "0.123.1",
    ],
    "64": [
        "0.123.1",
    ]
}
mock_versions_limited = {
    "32": [
        "0.123.0",
    ],
    "64": []
}
mock_versions_json = json.dumps(mock_versions)

@pytest.mark.parametrize(
    "response_body,response_code,token,expected,err", [
        (None, 500, None, None, ValueError),
        (None, 403, "CoolToken", None, ConnectionError),
        (None, 500, "CoolToken", None, ConnectionError),
        (mock_versions_json[0:-2], 200, "CoolToken", None, ValueError),
        (mock_versions_json, 200, "CoolToken", mock_versions, None),
    ])
@responses.activate
def test_get_backend_versions(response_body, response_code, token, expected, err):
    responses.add(
        responses.GET,
        url=BACKEND_SUPPORTED_VERSIONS,
        body=response_body,
        status=response_code
    )
    if not err:
        assert utils.get_backend_versions(token) == expected
    else:
        with pytest.raises(err):
            utils.get_backend_versions(token)

mock_versions = {
    "32": [
        "0.123.0",
        "0.123.1",
    ],
    "64": [
        "0.123.1",
    ]
}
file_resp_limited = {"0.123.0_32": 1}
file_resp_full = {"0.123.0_32": 1, "0.123.1_32": 2, "0.123.1_64": 3}

@pytest.mark.parametrize(
    "file_resp,expected", [
        (json.dumps(file_resp_limited), mock_versions_limited),
        (json.dumps(file_resp_full), mock_versions),
    ]
)
def test_get_local_versions(file_resp, expected):
    with mock.patch("mapadroid.utils.functions.open", new_callable=mock.mock_open, read_data=file_resp):
        assert utils.get_local_versions() == expected

@pytest.mark.parametrize(
    "supported_versions,arch,version,is_supported", [
        (mock_versions, APKArch.armeabi_v7a, "0.123.0", True),
        (mock_versions, APKArch.arm64_v8a, "0.123.0", False),
        (mock_versions, APKArch.armeabi_v7a, "0.123.1", True),
        (mock_versions, APKArch.arm64_v8a, "0.123.1", True),
    ])
@responses.activate
def test_supported_pogo_version(supported_versions, arch, version, is_supported, mocker):
    mocker.patch("mapadroid.mad_apk.utils.get_backend_versions", return_value=supported_versions)
    assert utils.supported_pogo_version(arch, version, "SomeToken") == is_supported

var_a = PackageVariant("APK", "nodpi", 1)
var_a_a = PackageVariant("BUNDLE", "nodpi", 11)
var_b = PackageVariant("APK", "nodpi", 3)
var_c = PackageVariant("APK", "nodpi", 4)
pkg_version = PackageBase("Pokemon GO", "ffff", versions={
    "0.123.0": PackageVersion(
        "nolinky",
        arch_data={
            "armeabi-v7a": [var_a, var_a_a]
        }
    ),
    "0.123.1": PackageVersion(
        "nolinky",
        arch_data={
            "armeabi-v7a": [var_b],
            "arm64-v8a": [var_c]
        }
    )
})

@pytest.mark.parametrize("avail_versions,supported_ver,arch,exp_ver,package", [
    (pkg_version, mock_versions_limited, APKArch.armeabi_v7a, "0.123.0", var_a),
    (pkg_version, mock_versions_limited, APKArch.arm64_v8a, None, None),
    (pkg_version, mock_versions, APKArch.armeabi_v7a, "0.123.1", var_b),
    (pkg_version, mock_versions, APKArch.arm64_v8a, "0.123.1", var_c)
])
def test_get_latest_supported(avail_versions, supported_ver, arch, exp_ver, package, mocker):
    mocker.patch("mapadroid.mad_apk.utils.get_backend_versions", return_value=supported_ver)
    assert wizard.APKWizard.get_latest_supported(arch, avail_versions, "Token") == (exp_ver, package)

@pytest.mark.parametrize("gls_v,sto_v,lvc_v,arch,msg", [
    (("0.123.0", var_a), "0.122.0", 0, APKArch.armeabi_v7a, "Newer version found"),
    (("0.123.0", var_a), "0.123.0", 1, APKArch.armeabi_v7a, "Already have the latest version"),
    (("0.123.0", var_a), "0.123.0", 10, APKArch.armeabi_v7a, "Unable to find a supported version")
])
@mock.patch("mapadroid.mad_apk.wizard.get_available_versions")
@mock.patch("mapadroid.mad_apk.wizard.APKWizard.get_latest_supported")
@mock.patch("mapadroid.mad_apk.wizard.APKWizard.lookup_version_code")
def tests_find_latest_pogo(lvc, gls, gav, gls_v, sto_v, lvc_v, arch, msg, caplog, wiz_instance):
    gav.return_value = None
    gls.return_value = gls_v
    lvc.return_value = lvc_v
    wiz_instance.storage.get_current_version.return_value = sto_v
    results = wiz_instance.find_latest_pogo(arch)
    assert results == ("0.123.0", var_a)
    assert msg in caplog.text

@mock.patch("mapadroid.mad_apk.wizard.package_search")
def test_ensure_cache_for_download(psearch):
    wizard.get_available_versions.cache_clear()
    wizard.get_available_versions()
    wizard.get_available_versions()
    psearch.assert_called_once()

@mock.patch("mapadroid.mad_apk.wizard.package_search")
def test_parsing_error(psearch, caplog):
    wizard.get_available_versions.cache_clear()
    psearch.side_effect = mock.Mock(side_effect=IndexError)
    with pytest.raises(wizard.SearchError):
        wizard.get_available_versions()
    assert "Unable to query APKMirror" in caplog.text


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

from functools import partial
import os
import os.path as osp

from vai.dpuv1.rt.xdnn_io import loadImageBlobFromFileScriptBase

IMAGEROOT = osp.join(os.environ['HOME'], 'CK-TOOLS/dataset-imagenet-ilsvrc2012-val-min')
IMAGELIST = osp.join(os.environ['HOME'], 'CK-TOOLS/dataset-imagenet-ilsvrc2012-val-min/val.txt')
INPUT_NODES = 'input'

### Preprocessing formulas
### Available transformations: ['resize', 'resize2mindim', 'resize2maxdim', 'crop_letterbox',
###                             'crop_center', 'plot', 'pxlscale', 'meansub', 'chtranspose', 'chswap']

CMD_SEQ        = {
    'resnet50_v1_tf':[
        ('meansub', [103.94, 116.78, 123.68]),
        ('chswap',(2,1,0)),
        ('resize', [256, 256]),
        ('crop_center', [224, 224]),
        ],
    'inception_v1_tf':[
        ('pxlscale', 1/255.),
        ('meansub', 0.5),
        ('pxlscale', 2),
        ('resize', [256, 256]),
        ('crop_center', [224, 224]),
        ],
    'inception_v3_tf':[
        ('pxlscale', 1/255.),
        ('meansub', 0.5),
        ('pxlscale', 2),
        ('resize', [342, 342]),
        ('crop_center', [299, 299]),
        ],
    }

def load_image(iter, image_list = None, pre_process_function = None, input_nodes = 'data', get_shapes = False, batch_size = 1):
    images = []
    shapes = []
    labels = []
    for i in range(batch_size):
        image_name, label  = image_list[iter * batch_size + i].split(' ')
        print((image_name))
        image, shape = loadImageBlobFromFileScriptBase(image_name, pre_process_function)
        images.append(image)
        shapes.append(shape)
    if get_shapes:
        return {input_nodes: images}, shapes
    else:
        return {input_nodes: images}

def get_input_fn(pre_processing_function_name, imageroot = IMAGEROOT, imagelist = IMAGELIST, input_nodes = INPUT_NODES):
    cmd_seq = CMD_SEQ[pre_processing_function_name]
    with open(imagelist) as fin:
        lines = fin.readlines()
        image_list = list(map(lambda x:os.path.join(imageroot, x.strip()), lines))
    return partial(load_image, image_list = image_list, pre_process_function = cmd_seq, input_nodes = input_nodes)

for name in CMD_SEQ:
    globals()['input_fn_{}'.format(name)] = get_input_fn(name)


import pandas as pd
import os
from tqdm import tqdm
import pickle
import argparse

#print(os.listdir(os.path.join(ROCO_PATH,"train")))

''' build dataframes for roco'''

def build_dataframe(split):

    licences = pd.read_csv(os.path.join(ROCO_PATH,split,"licences.txt"))
    #print(licences)

    captions = pd.read_csv(os.path.join(ROCO_PATH,split,"captions.txt"), sep='\t', names=['ROCO_ID','caption'],header=None)

    #print(captions)

    result = pd.merge(licences, captions, on="ROCO_ID")
    #print(result.iloc[0])
    columns = ['name','caption']
    df = result[columns]
    filename = 'traindata.csv' if split == 'train' else 'valdata.csv'
    print(filename)
    df.to_csv(os.path.join(ROCO_PATH,split,filename), index=False, header=columns)
    return df

# import IPython; IPython.embed(); exit(1)

''' retrieve vocabulary from the captions'''

def count_keywords(split, keywords):
    num_lines = sum(1 for line in open(os.path.join(ROCO_PATH,split,"keywords.txt"),'r'))
    print('num_lines',num_lines)
    with open(os.path.join(ROCO_PATH,split,"keywords.txt"), "r") as f:
    #     for i,line in enumerate(tqdm(f, total=num_lines)):
    #         keys = line.split('\t')[1:]
    #         for k in keys:
    #             if k != '':
    #                 k = k.strip()
    #                 if k not in keywords:
    #                     keywords.append(k)
    # print('len',len(keywords))
    # return keywords

        for i,line in enumerate(tqdm(f, total=num_lines)):
            roco_id = line.split('\t')[0]
            keys = line.split('\t')[1:]

            words = []
            for k in keys:
                if k != '':
                    words.append(k.strip())

            keywords[roco_id] = words

        return keywords

''' sort keywords '''

def sort_keywords():
    KEYWORDS_PATH = os.path.join(ROCO_PATH,'train')
    columns = ('id','keys')
    keywords_df = pd.read_csv( os.path.join(KEYWORDS_PATH,"keywords.txt"), sep='\t\t', names=columns, engine='python') #read and parse
    rows = [{'id':x, 'keys':y} for x, y in zip(keywords_df['id'], keywords_df['keys'])]

    traindata_df = pd.read_csv(os.path.join(KEYWORDS_PATH,"traindata.csv"))

    del keywords_df
    rows_list = []
    for row in rows:

            dict1 = {}
            if row['keys'] is None:
                continue
            keys = row['keys'].split('\t')

            # get input row in dictionary format
            # key = col_name
            dict1['id'] = row['id']
            dict1['keys'] = " ".join(keys)
            keys.sort()
            dict1['sorted_keys'] = "".join(keys)

            rows_list.append(dict1)

    df = pd.DataFrame(rows_list)
    res = pd.merge(df, traindata_df, on="id")
    res.drop('caption', axis=1, inplace=True)
    df = res.sort_values('sorted_keys')
    df.to_csv(os.path.join(KEYWORDS_PATH,'keywords_sorted_name.csv'), index=False, header=('id','keys','sorted_keys','name'))
    #import IPython; IPython.embed(); exit(0)
    return df

parser = argparse.ArgumentParser(description="preprocess roco tasks")
parser.add_argument('--task', type=str, choices=['dataframe', 'vocab', 'sort_keywords'], help="name for wandb run", required=True)
parser.add_argument('--roco_dir', type=str, default = 'D:\ist\Tese\ROCO\\', help='path to dataset', required = False)

args = parser.parse_args()
print(args)

ROCO_PATH = args.roco_dir

if args.task == 'dataframe':
    train_df = build_dataframe('train')
    val_df = build_dataframe('validation')

if args.task == 'vocab':
    keywords = {}
    keywords = count_keywords('train', keywords)
    keywords = count_keywords('validation', keywords)
    print('len',len(keywords))
    with open(os.path.join(ROCO_PATH,'vocab','med_vocab.pkl'), 'wb') as fp:
        pickle.dump(keywords, fp, protocol=pickle.DEFAULT_PROTOCOL)

if args.task == 'sort_keywords':
    df = sort_keywords()


from os import system, name
import time

    ## A Hopper is a type of traincar. It's steep sides allow for
    ## the transportation of large quantities of raw material,
    ## like coal. This class does the same for the boxcar game.
    ## It containes functions for the other objects to use.

#==============================================================================

def display(txt, t=0):
    # This function clears the terminal and prints a new line.
    # It also stalls the program for t=time in seconds.

    # Evaluates type of operating system because mac and linux use
    # different commands.
    if name == 'nt':
        _ = system('cls')
    else:
        _ = system('clear')
    print(f'\r{txt}')
    # Time stall is at end. Each function calling clear()
    # can determine how long the program should pause
    # after it is called.
    time.sleep(t)

#==============================================================================

def log(logstring):
    print(f'log: {logstring}')


import sys
# TASK_URL: https://open.kattis.com/problems/iforaneye

#sys.stdin = open('test.in')
lines = sys.stdin.readlines()
rows = int(lines[0])
word3 = ["bea","bee","owe","why","and","one","won","too","two","for","you","sea","see","eye","are"]
code3 = ["b","b","o","y","&","1","1","2","2","4","u","c","c","i","r"]
word2 = ["at","to","oh","be"]
code2 = ["@","2","o","b"]
for i in range(rows):
    string = str(lines[i+1]).lower()
    for j in range(0, len(string)):
        again = True
        if string[j:j+4] == "four":
            string = string[:j] + "4" + string[j+4:]
            again = False
        if again:
            for k in range(0,15):
                if string[j:j+3] == word3[k]:
                    string = string[:j] + code3[k] + string[j+3:]
                    again = False
                    break
        if again:
            for k in range(0,4):
                if string[j:j+2] == word2[k]:
                    string = string[:j] + code2[k] + string[j+2:]
                    break
    string = string[0].upper() + string[1:]
    print(string, end="")


#!/usr/bin/env python

# Patrik Vavra 2019

"""
Node for detecting ArUco markers and publishing transformations of detected markers
after tranformation to ROS coordinate system.

"""

from numpy import array, zeros, dot, load, finfo
import cv2
from yaml import load, YAMLError
import rospy
from math import atan2, sqrt
from threading import Thread, Event

from geometry_msgs.msg import PoseWithCovarianceStamped
from mech_ros_msgs.msg import MarkerList
from mech_ros_msgs.msg import Marker

class VideoCapture(Thread):

    def __init__(self, ready):
        Thread.__init__(self)
        self.daemon = True
        self.ready = ready
        # self.cap = cv2.VideoCapture(0)
        self.cap = cv2.VideoCapture('tcpclientsrc host=mechros2.local port=8080  ! gdpdepay !  rtph264depay ! avdec_h264 ! videoconvert ! appsink sync=false', cv2.CAP_GSTREAMER)

        # ROS adjustable parameters
        image_width = rospy.get_param("~image_width", 1280)
        image_height = rospy.get_param("~image_height", 720)
        fps = rospy.get_param("~fps", 20)

        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, image_height)
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, image_width)
        self.cap.set(cv2.CAP_PROP_FPS, fps)
        self.cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
        self.cap.set(cv2.CAP_PROP_FOCUS, 0)
        self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)

        self.current_frame = None
        self.time = None
        self.initialized = False
        self.start()

        if not self.cap.isOpened():
            rospy.logerr("Cannot open stream")
            exit()
        else:
            rospy.loginfo("Stream OPENED")

    def run(self):
        while True:
            ret, frame = self.cap.read()
            if ret:
                self.time = rospy.Time.now()
                # Flip image and convert to grayscale
                self.current_frame = cv2.cvtColor(cv2.flip(frame,-1),cv2.COLOR_BGR2GRAY)
                if not(self.initialized):
                    self.ready.set()
                    self.initialized = True

    def get_current_frame(self):
        return self.current_frame, self.time

class Detector:

    def __init__(self):

        rospy.init_node("Aruco_detect")

        self.ready = Event()
        self.stream = VideoCapture(self.ready)

        self.c_matrix = None
        self.dist_coeff = None

        # ROS parameters
        self.marker_length = rospy.get_param("~marker_length", 0.118)
        marker_detector_topic = rospy.get_param("~markers_topic","/markers")
        self.frame_id = rospy.get_param("~camera_frame","front_camera_link")
        calibration_file = rospy.get_param("~calibration_file", "/home/patrik/catkin_ws/src/mech_ros/Config_ARuco/camera.yaml")
        calibration_file_type = rospy.get_param("~calibration_file_type","yaml")
        dictionary = rospy.get_param("~dictionary","DICT_5X5_50")

        # Define ArUco detector and dictionary
        try:
            self.dictionary = cv2.aruco.getPredefinedDictionary(getattr(cv2.aruco, dictionary))
        except:
            rospy.logerr("Wrongly defined ArUco dictionary!")

        arucoParams = cv2.aruco.DetectorParameters_create()
        arucoParams.adaptiveThreshConstant = 7
        #arucoParams.adaptiveThreshWinSizeMax = 27 # default 23
        #arucoParams.adaptiveThreshWinSizeMin = 3 # default 3
        #arucoParams.adaptiveThreshWinSizeStep = 8 # default 10

        ##arucoParams.cornerRefinementMethod = 1
        ##arucoParams.cornerRefinementMaxIterations = 30
        ##arucoParams.cornerRefinementMinAccuracy = 0.01
        ##arucoParams.cornerRefinementWinSize = 5

        arucoParams.errorCorrectionRate = 0.6
        arucoParams.minCornerDistanceRate = 0.05 # min distance between marker corners,
        # min. distance[pix] = Perimeter*minCornerDistanceRate
        arucoParams.minMarkerDistanceRate = 0.05 # min distance between corners of different markers,
        # min. distance[pix] = Perimeter(smaller one)*minMarkerDistanceRate
        arucoParams.minMarkerPerimeterRate = 0.1
        arucoParams.maxMarkerPerimeterRate = 4.0
        arucoParams.minOtsuStdDev = 5.0
        arucoParams.perspectiveRemoveIgnoredMarginPerCell = 0.13
        arucoParams.perspectiveRemovePixelPerCell = 8
        arucoParams.polygonalApproxAccuracyRate = 0.01
        arucoParams.markerBorderBits = 1
        arucoParams.maxErroneousBitsInBorderRate = 0.04
        arucoParams.minDistanceToBorder = 3

        self.arucoParams = arucoParams

        # Init publisher
        self.marker_publisher = rospy.Publisher(marker_detector_topic, MarkerList, queue_size=10)

        # Load camera calibration files
        self.load_calibration(calibration_file, calibration_file_type)

    def run_detection(self):

        # Matrix for conversion from ROS frame to OpenCV in camera
        R_ROS_O_camera = array([[  0.0,  0.0,   1.0],
        [  -1.0,   0.0,  0.0],
        [  0.0,   -1.0,   0.0]])

        # Matrix for conversion from OpenCV frame to ROS in marker
        R_O_ROS_marker = array([[  0.0,  1.0,   0.0],
        [  0.0,   0.0,  1.0],
        [  1.0,   0.0,   0.0]])

        # Wait until first frame is initialized
        self.ready.wait()

        while(self.stream.cap.isOpened()) and not(rospy.is_shutdown()):
            image, time = self.stream.get_current_frame()
            markerIds = array([])
            markerCorners = array([])

            markerCorners,markerIds,_ = cv2.aruco.detectMarkers(image, self.dictionary, parameters = self.arucoParams, cameraMatrix = self.c_matrix,
            distCoeff = self.dist_coeff )

            if len(markerCorners) > 0:
                rvec, tvec, _objPoints = cv2.aruco.estimatePoseSingleMarkers(markerCorners, self.marker_length, self.c_matrix, self.dist_coeff) # For a single marker
                aruco_MarkerList = MarkerList()
                aruco_MarkerList.header.stamp = time
                aruco_MarkerList.header.frame_id = self.frame_id

                for i in range(markerIds.size):

                    # Calculate surface area in pixels
                    surface = cv2.contourArea(markerCorners[i], False)

                    # Fill MarkerList with each marker
                    aruco_Marker = Marker()
                    aruco_Marker.id = str(markerIds[i,0])
                    aruco_Marker.surface = surface

                    # Convert Rodrigues vector to rotation matrix
                    Rmat = zeros(shape=(3,3))
                    cv2.Rodrigues(rvec[i,0],Rmat)

                    # Convert from Opencv frame to ROS frame in camera
                    R = dot(R_ROS_O_camera, Rmat)

                    # Convert inverted matrix from Opencv frame to ROS frame in marker
                    R = dot(R, R_O_ROS_marker)

                    # Convert from Rotation matrix to Euler angles
                    Euler = self.rotationMatrixToEulerAngles(R.T) # Rotation from marker to camera

                    # Fill Marker orientation vector
                    aruco_Marker.pose.orientation.r = Euler[0]
                    aruco_Marker.pose.orientation.p = Euler[1]
                    aruco_Marker.pose.orientation.y = Euler[2]

                    # Coordinate vector of camera position from marker in camera coordinate frame
                    aruco_Marker.pose.position.x = -tvec[i,0,2]
                    aruco_Marker.pose.position.y = tvec[i,0,0]
                    aruco_Marker.pose.position.z = -tvec[i,0,1]

                    ## For compatibility with gazebo
                    aruco_Marker.header.stamp = time

                    # All coordinates are in marker frame
                    aruco_MarkerList.markers.append(aruco_Marker)

                self.marker_publisher.publish(aruco_MarkerList)

        self.stream.cap.release()

    ## Load coefficients
    def load_calibration(self, calibration_file, load_method = "yaml"):

        if load_method == "yaml":
            with open(calibration_file) as stream:
                try:
                    data_loaded = load(stream)
                    self.c_matrix = array(data_loaded["camera_matrix"]["data"]).reshape((3,3))
                    self.dist_coeff = array(data_loaded["distortion_coefficients"]["data"])
                    rospy.logdebug("Calibration files for camera loaded")
                except YAMLError as exc:
                    rospy.logerr(exc)

        elif load_method == "numpy":
            with load(calibration_file) as X:
                self.c_matrix, self.dist_coeff, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
                rospy.logdebug("Calibration files for camera loaded")

        else:
            rospy.logerr("Specify valid calibration_file_type. Valid types: 'yaml','numpy'")

    def rotationMatrixToEulerAngles(self, M, cy_thresh=None):
        # cy_thresh : None or scalar, optional
        #    threshold below which to give up on straightforward arctan for
        #    estimating x rotation.  If None (default), estimate from
        #    precision of input. Source : http://www.graphicsgems.org/
        _FLOAT_EPS_4 = finfo(float).eps * 4.0
        if cy_thresh is None:
            try:
                cy_thresh = finfo(M.dtype).eps * 4
            except ValueError:
                cy_thresh = _FLOAT_EPS_4
        r11, r12, r13, r21, r22, r23, r31, r32, r33 = M.flat
        # cy: sqrt((cos(y)*cos(z))**2 + (cos(x)*cos(y))**2)
        cy = sqrt(r33*r33 + r23*r23)
        if cy > cy_thresh: # cos(y) not close to zero, standard form
            z = atan2(-r12,  r11) # atan2(cos(y)*sin(z), cos(y)*cos(z))
            y = atan2(r13,  cy) # atan2(sin(y), cy)
            x = atan2(-r23, r33) # atan2(cos(y)*sin(x), cos(x)*cos(y))
        else: # cos(y) (close to) zero, so x -> 0.0 (see above)
            # so r21 -> sin(z), r22 -> cos(z) and
            z = atan2(r21,  r22)
            y = atan2(r13,  cy) # atan2(sin(y), cy)
            x = 0.0
        return [x, y, z]

if __name__ == '__main__':
    try:
        ArUcoDetector = Detector()
        ArUcoDetector.run_detection()
        rospy.spin()
    except rospy.ROSInterruptException:
        pass


import copy
import numpy as np
from sklearn.datasets import load_iris, make_multilabel_classification
from sklearn.preprocessing import OneHotEncoder, normalize
from sklearn.metrics import f1_score
from alipy.query_strategy.query_type import QueryTypeAURO
from alipy.query_strategy.multi_label import LabelRankingModel
from alipy.index.multi_label_tools import get_Xy_in_multilabel
from alipy import ToolBox

X, y = load_iris(return_X_y=True)
# X, mult_y = make_multilabel_classification(n_samples=2500, n_labels=3, n_classes=10, n_features=15)
mlb = OneHotEncoder()
mult_y = mlb.fit_transform(y.reshape((-1, 1)))
mult_y = np.asarray(mult_y.todense())
X = normalize(X, norm='l2')
mult_y_for_metric = mult_y.copy()
mult_y[mult_y == 0] = -1

alibox = ToolBox(X=X, y=mult_y, query_type='PartLabels')
alibox.split_AL(test_ratio=0.2, initial_label_rate=0.05, all_class=False)
model = LabelRankingModel() # base model

# query type strategy
AURO_results = []

for round in range(5):

    train_idx, test_idx, label_ind, unlab_ind = alibox.get_split(round)
    # Get intermediate results saver for one fold experiment
    saver = alibox.get_stateio(round)
    query_y = mult_y.copy() # for labeling `less relevant`
    AURO_strategy = QueryTypeAURO(X=X, y=mult_y)
    # init model
    X_tr, y_tr, _ = get_Xy_in_multilabel(label_ind, X=X, y=mult_y)
    model.fit(X=X_tr, y=y_tr)

    for iter in range(100):

        select_ins, select_y1, select_y2 = AURO_strategy.select(label_ind, unlab_ind, model=model, y_mat=query_y)

        # relevance
        y1 = mult_y[select_ins, select_y1]
        y2 = mult_y[select_ins, select_y2]
        if y1 < 0 and y2 < 0:
            query_y[select_ins, select_y1] = -1
            query_y[select_ins, select_y2] = -1
        elif y1 > y2:
            query_y[select_ins, select_y1] = 1
            query_y[select_ins, select_y2] = 0.5
        else:
            query_y[select_ins, select_y1] = 0.5
            query_y[select_ins, select_y2] = 1

        # record results
        label_ind.update([(select_ins, select_y1), (select_ins, select_y2)])
        unlab_ind.difference_update([(select_ins, select_y1), (select_ins, select_y2)])

        # train/test
        X_tr, y_tr, _ = get_Xy_in_multilabel([(select_ins, select_y1), (select_ins, select_y2)], X=X, y=query_y, unknown_element=0)
        model.fit(X=X_tr, y=y_tr, is_incremental=True)
        pres, pred = model.predict(X[test_idx])

        # using sklearn to calc micro-f1
        pred[pred == -1] = 0
        perf = f1_score(y_true=mult_y_for_metric[test_idx], y_pred=pred, average='micro')

        # save
        st = alibox.State(select_index=[(select_ins, select_y1), (select_ins, select_y2)], performance=perf)
        saver.add_state(st)

    AURO_results.append(copy.copy(saver))

analyser = alibox.get_experiment_analyser()
analyser.add_method(method_name='AURO', method_results=AURO_results)
analyser.plot_learning_curves(plot_interval=5)