#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sqlite3
from flask import Flask, current_app, g
from collections import defaultdict as dd
from os import path

def qs(ll):
    """return len(l) ?s sepeated by ','  to use in queries"""
    return ','.join('?' for l  in ll)

app = Flask(__name__)
with app.app_context():

    ROOT  = path.dirname(path.realpath(__file__))
    ADMINDB = 'db/admin.db'
    CALLIGDB = 'db/callig.db'
    LCCDB = 'db/lcc.db'

    ############################################################################
    # SET UP CONNECTIONS
    ############################################################################
    def connect_admin():
        return sqlite3.connect(path.join(ROOT, ADMINDB))

    def connect_callig():
        return sqlite3.connect(path.join(ROOT, CALLIGDB))

    def connect_lcc():
        return sqlite3.connect(path.join(ROOT, LCCDB))

    def query_admin(query, args=(), one=False):
        cur = g.admin.execute(query, args)
        rv = [dict((cur.description[idx][0], value)
                   for idx, value in enumerate(row)) for row in cur.fetchall()]
        return (rv[0] if rv else None) if one else rv

    def query_callig(query, args=(), one=False):
        cur = g.callig.execute(query, args)
        rv = [dict((cur.description[idx][0], value)
                   for idx, value in enumerate(row)) for row in cur.fetchall()]
        return (rv[0] if rv else None) if one else rv

    def query_lcc(query, args=(), one=False):
        cur = g.lcc.execute(query, args)
        rv = [dict((cur.description[idx][0], value)
                   for idx, value in enumerate(row)) for row in cur.fetchall()]
        return (rv[0] if rv else None) if one else rv

    def write_admin(query, args=(), one=False):
        cur = g.admin.cursor()
        cur.execute(query, args)
        lastid = cur.lastrowid
        g.admin.commit()
        return lastid

    def write_callig(query, args=(), one=False):
        cur = g.callig.cursor()
        cur.execute(query, args)
        lastid = cur.lastrowid
        g.callig.commit()
        return lastid

    def write_lcc(query, args=(), one=False):
        cur = g.lcc.cursor()
        cur.execute(query, args)
        lastid = cur.lastrowid
        g.lcc.commit()
        return lastid

    ############################################################################
    # ADMIN SQL
    ############################################################################

    def fetch_userid(userID):
        user = None
        for r in query_admin("""SELECT userID, password,
                                       access_level, access_group, full_name
                                FROM users
                                WHERE userID = ?""", [userID]):
            if r['userID']:
                user = (r['userID'], r['password'],
                        r['access_level'], r['access_group'], r['full_name'])
        return user

    def fetch_id_from_userid(userID):
        for r in query_admin("""SELECT id
                                FROM users
                                WHERE userID = ?""", [userID]):
            return r['id']

    def fetch_allusers():
        users = dd()
        for r in query_admin("""SELECT * FROM users"""):
            users[r['id']] = r

        return users

    ############################################################################
    # CALLIG SQL
    ############################################################################

    ############################################################################
    # SEX WITH ME
    ############################################################################
    def write_sexwithme(prompt, answer, seconds, language, username, timestamp):
        """
        Returns the ID of the recently added entry.
        """
        return write_callig("""INSERT INTO sex_with_me (prompt, answer,
                                                      seconds, language,
                                                      username, timestamp)
                               VALUES (?,?,?,?,?,?)""",
                            [prompt, answer, seconds,
                             language, username, timestamp])

    def write_sexwithme_feedback(answer, sex_with_me_id, feedback,
                                 seconds, language, username, timestamp):
        """
        Returns the ID of the recently added entry.
        """
        return write_callig("""INSERT INTO sex_with_me_feedback
                                           (answer, sex_with_me_id, feedback,
                                            seconds, language, username, timestamp)
                               VALUES (?,?,?,?,?,?,?)""",
                            [answer, sex_with_me_id, feedback,
                             seconds, language, username, timestamp])

    def fetch_sexwithme_30():
        result = dd()
        for r in query_callig("""SELECT * FROM sex_with_me WHERE answer IS NOT NULL
                                 ORDER BY timestamp DESC LIMIT 30"""):
            result[r['id']] = [r['prompt'], r['answer'], r['seconds'], r['language'],
                               r['username'], r['timestamp']]
        return result

    ############################################################################
    # WICKED PROVERBS
    ############################################################################
    def write_wickedproverbs(frame, w1, w2, proverb, explanation, seconds,
                             language, username, timestamp):
        """
        Returns the ID of the recently added entry.
        """
        return write_callig("""INSERT INTO wicked_proverbs
                               (frame, w1, w2, proverb, explanation, seconds,
                                language, username, timestamp)
                               VALUES (?,?,?,?,?,?,?,?,?)""",
                            [frame, w1, w2, proverb, explanation, seconds,
                             language, username, timestamp])

    def fetch_wickedproverbs_30():
        result = dd()
        for r in query_callig("""SELECT * FROM wicked_proverbs
                                 WHERE proverb IS NOT NULL
                                 AND explanation IS NOT NULL
                                 ORDER BY timestamp DESC LIMIT 30"""):
            result[r['id']] = [r['frame'], r['proverb'],r['explanation'],
                               r['seconds'], r['language'],
                               r['username'], r['timestamp']]
        return result

    ############################################################################
    # HAIKU ON DEMAND
    ############################################################################
    def write_haikuondemand(title, l1, l2, l3,
                            seconds, language, username, timestamp):
        """
        Returns the ID of the recently added entry.
        """
        return write_callig("""
                            INSERT INTO haiku_on_demand
                            (title, l1, l2, l3,
                             seconds, language, username, timestamp)
                            VALUES (?,?,?,?,?,?,?,?)
                            """, [title, l1, l2, l3,
                                  seconds, language, username, timestamp])

    def write_haikuondemand_feedback(title, feedback, l1, l2, l3, s1, s2, s3,
                                     seconds, language, username, timestamp):
        """
        Returns the ID of the recently added entry.
        """
        return write_callig("""
                            INSERT INTO haiku_on_demand_feedback
                            (title, feedback, l1, l2, l3, s1, s2, s3,
                             seconds, language, username, timestamp)
                            VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
                            """, [title, feedback, l1, l2, l3, s1, s2, s3,
                                  seconds, language, username, timestamp])

    def fetch_haikuondemand_30():
        result = dd()
        for r in query_callig("""
                              SELECT * FROM haiku_on_demand
                              WHERE l1 IS NOT NULL
                              AND l2 IS NOT NULL
                              AND l3 IS NOT NULL
                              ORDER BY timestamp DESC LIMIT 30
                              """):
            result[r['id']] = [r['title'], r['l1'], r['l2'], r['l3'],
                               r['username'], r['timestamp'], r['seconds']]
        return result

    ############################################################################
    # LCC SQL
    ############################################################################

    def fetch_max_doc_id():
        for r in query_lcc("""SELECT MAX(docid) from doc"""):
            if r['MAX(docid)']:
                return r['MAX(docid)']
            else:
                return 0

    def fetch_max_sid():
        for r in query_lcc("""SELECT MAX(sid) from sent"""):
            if r['MAX(sid)']:
                return r['MAX(sid)']
            else:
                return 0

    def fetch_sents_by_docid(docid):
        sents = dd()
        for r in query_lcc("""SELECT sid, sent from sent
                              WHERE docid = ?
                           """, [docid]):
            sents[r['sid']] = r['sent']
        return sents

    def fetch_words_by_sid(sid_min, sid_max):
        words = dd(lambda: dd())
        for r in query_lcc("""SELECT sid, wid, word, pos, lemma from word
                              WHERE sid >= ? AND sid <= ?
                           """, [sid_min, sid_max]):
                words[r['sid']][r['wid']]=[r['word'], r['pos'],r['lemma']]
        return words

    def fetch_max_wid(sid):
        for r in query_lcc("""SELECT MAX(wid) from word WHERE sid = ?""", [sid]):

            if r['MAX(wid)']:
                return r['MAX(wid)']
            else:
                return 0

    def insert_into_doc(docid, docname):
        return write_lcc("""INSERT INTO doc (docid, title)
                               VALUES (?,?)
                            """, [docid, docname])

    def update_html_into_doc(docid, html):
        return write_lcc("""UPDATE doc SET doc = ?
                               WHERE docid = ?
                            """, [html, docid])

    def insert_into_sent(sid, docid, pid, sent):
        return write_lcc("""INSERT INTO sent (sid, docID, pid, sent)
                               VALUES (?,?,?,?)
                            """, [sid, docid, pid, sent])

    def insert_into_word(sid, wid, word, pos, lemma):
        return write_lcc("""INSERT INTO word (sid, wid, word, pos, lemma)
                               VALUES (?,?,?,?,?)
                            """, [sid, wid, word, pos, lemma])

    def insert_into_error(sid, eid, label, comment):
        return write_lcc("""INSERT INTO error (sid, eid, label, comment)
                               VALUES (?,?,?,?)
                            """, [sid, eid, label, comment])


"""
Fit a sin curve to radial velocity data
"""

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import leastsq
import glob as g

flist = g.glob('*')
for i, entry in enumerate(flist):
    print(str(i) + '\t' + entry)
index = int(input('Give index of desired file: '))
fname = flist[index]

d_phase = []
d_rv = []
err = []

with open(fname, 'r') as fin:
    fin.readline()
    for line in fin:
        newline = line.split()

        d_phase.append(float(newline[0]))
        d_rv.append(float(newline[1]))
        err.append(float(newline[2]))

phase = np.asarray(d_phase)
rv = np.asarray(d_rv)

N = len(phase)
purephase = np.arange(-0.5, 0.5, 0.01)

guess_mean = 0
guess_semiamp = 20
guess_offset = np.pi

guess_plot = guess_semiamp*np.sin(purephase*np.pi + guess_offset)

optimize_func = lambda x: x[0]*np.sin(phase*np.pi + x[1]) + x[2] - rv

cal_std, cal_offset, cal_mean = leastsq(optimize_func,
                                       [guess_semiamp, guess_offset, guess_mean])[0]

cal_plot = cal_std*np.sin(phase*np.pi + cal_offset) + cal_mean

print('Least squares fit parameters')
print('Mean =', cal_mean)
print('Semi-amplitude =', cal_std)
print('Phase offset =', cal_offset)

plt.plot(phase, rv, '.', color = 'grey')
plt.plot(purephase, guess_plot, 'o', label='parameters from exonailer')
plt.plot(phase, cal_plot, 'o', label='optimised parameters')
plt.legend()
plt.show()


import json
import requests
from models import db, Recipe

def add_structure_recipe(res):
    """takes response from API request and structures the return recipe info for db, returns recipe object"""

    response_string = res.text # get text from response
    response_dict = json.loads(response_string) #convert to json dict

    id = response_dict["id"]
    title = response_dict["title"]
    try:
        img = response_dict["image"]
    except KeyError:
        print("No image available")
        img = ""
    protein = response_dict["nutrition"]["nutrients"][9]["amount"]
    source_url = response_dict["sourceUrl"]

    ingredient_info = response_dict["extendedIngredients"] # list of ingredients with all the info
    ingredients = []
    for ing in ingredient_info: # make a list with just the amount, unit, name
        ingredients.append([ing["amount"], ing["unit"], ing["name"]])

    if len(response_dict["analyzedInstructions"]) != 0:
        instructions = response_dict["analyzedInstructions"][0]["steps"]
    else:
        instructions = {}
    time = response_dict["readyInMinutes"]
    servings= response_dict["servings"]

    # adds recipe to database
    recipe = Recipe.addRecipe(
        id=id,
        title=title,
        img=img,
        protein=protein,
        source_url=source_url,
        ingredients=json.dumps(ingredients), #converts to json
        instructions=json.dumps(instructions), #converts to json
        time=time,
        servings=servings
    )

    db.session.commit()

    return recipe

def gets_recipes(API_KEY, min_protein, intolerances, query):
    """Makes a request to spoonacular api for recipes based on query"""

    res = requests.get(f"https://api.spoonacular.com/recipes/complexSearch?apiKey={API_KEY}&minProtein={min_protein}&intolerances={intolerances}&number=3&diet=vegan&query={query}&addRecipeInformation=True")
    response_string = res.text
    response_dict = json.loads(response_string) #convert to json dict

    return response_dict


#!/usr/bin/python3
""" Task 11 """
from sys import argv
from model_state import Base, State
from sqlalchemy import create_engine, asc
from sqlalchemy.orm import sessionmaker

def Task11():
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'.format(argv[1],
                           argv[2], argv[3]), pool_pre_ping=True)
    Base.metadata.create_all(engine)

    Session = sessionmaker(bind=engine)
    session = Session()
    item = State(name='Louisiana')
    instance = session.add(item)
    instance = session.query(State).filter(State.name == 'Louisiana').first()
    print(str(instance.id))
    session.commit()
    session.close()

if __name__ == "__main__":
    Task11()


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

"""
Module that contains widgets classes related with icons
"""

from __future__ import print_function, division, absolute_import

from functools import partial

from Qt.QtCore import Signal, QSize
from Qt.QtWidgets import QSizePolicy, QToolButton, QMenu

from tpDcc.managers import resources
from tpDcc.libs.qt.core import base
from tpDcc.libs.qt.widgets import layouts, buttons

class IconPickerButton(QToolButton):
    def __init__(self, *args, **kwargs):
        super(IconPickerButton, self).__init__(*args, **kwargs)

        self._icon_path = None
        self.setCheckable(True)

    # ============================================================================================================
    # BASE
    # ============================================================================================================

    def icon_path(self):
        """
        Returns current button icon path
        :return: str
        """

        return self._icon_path

    def set_icon_path(self, icon_path):
        """
        Sets path for the icon button
        :param icon_path: str
        """

        self._icon_path = icon_path
        self.setToolTip(icon_path)
        self.setStatusTip(icon_path)
        icon = resources.icon(icon_path)
        self.setIcon(icon)

class IconPicker(base.BaseFrame):

    iconChanged = Signal(object)

    def __init__(self, *args, **kwargs):
        super(IconPicker, self).__init__(*args, **kwargs)

        self._buttons = list()
        self._current_icon = None
        self._menu_button = None

    # ============================================================================================================
    # OVERRIDES
    # ============================================================================================================

    def get_main_layout(self):
        return layouts.GridLayout(spacing=0, margins=(0, 0, 0, 0))

    def enterEvent(self, event):
        """
        Overrides base BaseFrame enterEvent function to fix a bug with custom actions
        :param event:
        :return:
        """
        if self.parent():
            menu = self.parent().parent()
            if isinstance(menu, QMenu):
                menu.setActiveAction(None)

    # ============================================================================================================
    # BASE
    # ============================================================================================================

    def menu_button(self):
        """
        Returns the menu button used for browsing custom icons
        :return: QWidget
        """

        return self._menu_button

    def current_icon(self):
        """
        Returns current icon
        :return: QIcon
        """

        return self._current_icon

    def set_current_icon(self, icon):
        """
        Sets the current icon
        :param icon: Icon
        """

        self._current_icon = icon
        self.refresh()

    def set_icons(self, icons):
        """
        Sets the icons
        :param icons: list(str) or list(Icon)
        :return:
        """

        self.delete_buttons()

        first = True
        last = False

        positions = [(i, j) for i in range(5) for j in range(5)]
        # positions = [(i, j) for i in range(1) for j in range(5)]
        for i, (position, icon_path) in enumerate(zip(positions, icons)):
            if i == len(icons) - 1:
                last = True
            icon_callback = partial(self._on_icon_changed, icon_path)
            icon_button = IconPickerButton(self)
            icon_button.set_icon_path(icon_path)
            icon_button.setIconSize(QSize(16, 16))
            icon_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
            icon_button.setProperty('first', first)
            icon_button.setProperty('last', last)
            icon_button.clicked.connect(icon_callback)
            self.main_layout.addWidget(icon_button, *position)
            self._buttons.append(icon_button)
            first = False

        self._menu_button = buttons.BaseButton(parent=self)
        self._menu_button.setIcon(resources.icon('open'))
        self._menu_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self._menu_button.clicked.connect(self.browse_icon)
        self.main_layout.addWidget(self._menu_button)
        # self.main_layout.addWidget(self._menu_button, 0, len(icons))
        self.refresh()

    def browse_icon(self):
        print('NOT IMPLEMENTED YET!')
        pass

    def refresh(self):
        """
        Updates the current state of the selected icon
        """

        for button in self._buttons:
            button.setChecked(button.icon_path() == self.current_icon())

    def delete_buttons(self):
        """
        Deletes all the icon buttons
        """

        main_layout = self.main_layout
        while main_layout.count():
            item = main_layout.takeAt(0)
            item_widget = item.widget()
            if item_widget:
                item_widget.deleteLater()

    # ============================================================================================================
    # CALLBACKS
    # ============================================================================================================

    def _on_icon_changed(self, icon_path):
        """
        Internal callback function that is triggered when user selects a new icon
        :param icon_path: str
        """

        self.set_current_icon(icon_path)
        self.iconChanged.emit(icon_path)


import matplotlib.pyplot as plt

############################
def getX(line) :
	start = line.find('x="')
	if start == -1 :
		return -999
	start += 3

	end = line.find('y="')
	if end == -1 :
		return -999
	end -= 2

	strX = line[start:end]
	return int(strX)
############################

############################
def getY(line) :
	start = line.find('y="')
	if start == -1 :
		return
	start += 3

	end = line.find('/>')
	if end == -1 :
		return
	end -= 2

	strY = line[start:end]
	return int(strY)
############################

def draw_glif(name) :
	try :
		glif = open(name)
		lines = [line.rstrip('\n') for line in glif]

		listX = []
		listY = []
		fig = plt.figure()
		fig.suptitle(name)
		color = ['#f44242', '#f4a142', '#ebf442', '#74f442', '#424bf4', '#ad42f4']
		ind = 0
		for line in lines :
			if '</contour>' in line :
				for i in range(len(listX)) :
					if i == len(listX) - 1 :
						j = 0
					else :
						j = i + 1
					print(len(listX), len(listY))
					print(i, j)
					plt.plot([listX[i], listX[j]], [listY[i], listY[j]], color=color[ind])
					print('[' , listX[i] , ',' , listY[i] , '], [' , listX[j] , ',' , listY[j] , ']')

				listX.clear()
				listY.clear()
				print('-----------------------------')
				ind = (ind+1) % 6
				continue

			if 'type' not in line :
				continue

			x = getX(line)
			y = getY(line)
			if x != -999 and y != -999 :
				listX.append(getX(line))
				listY.append(getY(line))

		plt.show()
		return

	except IOError :
		print('Could not read file :', name)
		return

glifName = input('glif number: ')
draw_glif('cid' + glifName + '.glif')


# -*- coding: utf-8 -*-
"""
Created on Sun Aug 29 16:59:44 2021

@author: utilisateur
"""

import re
import string
import os
import time
import nltk
from nltk import word_tokenize
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from collections import Counter
from wordcloud import WordCloud
import streamlit as st
# import altair as alt

st.title('RAKUTEN Python Data Visualization')
st.markdown("""
* Application test for MASTER FINAL PRESENTATION
""")

df = pd.read_csv('X_train_update.csv',index_col=0)

designation = df['designation']
description = df['description']

### TEST LE 31 08 (boutons barre de gauche)
with st.sidebar:
            st.title("RAKUTEN PROJECT")
            st.subheader("Select a page below:")
            mode = st.radio(
                "Menu",
                [
                    "DataViz",
                    "Model Game",
                    "A little theory"
                ],
            )

        #st.sidebar.header('User Data Selection')
            st.sidebar.subheader('Variables Désignation & Description')
            st.sidebar.subheader('Target')

        #### TESTS KO
        # selector=st.sidebar.multiselect[description, designation]

        #options = st.multiselect(
        #    [description, designation]
        #st.write('You selected:', options)
if mode == "DataViz" :
            # ETUDE DES NAN'S
            st.header('Observation des données texte manquantes')
            figW=plt.figure()
            sns.heatmap(df.isna(),cmap="coolwarm", center = 10.0, cbar=False) #(bins = 100, color='gray')
            st.pyplot (figW)

            #sorted_text_data = st.sidebar.multiselect(designation, description)
            # df_selected_sector = df[df.isin(sorted_text_data)]
            st.write(designation)
            st.write(description)

            st.header('Longueur des mots des variables TEXTE RAKUTEN ')
            st.write()

            # ETUDE DE LA COLONNE DESIGNATION

            df['designation'] = df['designation'].apply(lambda _: str(_))
            words_per_review = df.designation.apply(lambda x: len(x.split(" ")))

            #if st.button('Show Plot Designation'):
                #st.header('Longeur des mots de la variable description')
                #st.area_chart(df.designation.apply(lambda x: len(x.split(" "))))

            if st.button('Show Plot Designation'):
                st.header('Longueur des mots de la variable designation - autre graph')
                figx=plt.figure()
                sns.histplot (words_per_review, kde=True,binwidth = 10, color='black') #(bins = 100, color='gray')
                plt.xlim(0,200)
                st.pyplot (figx)

            df['description'] = df['description'].apply(lambda _: str(_))
            words_per_review2 = df.description.apply(lambda x: len(x.split(" ")))

            #if st.button('Show Plot Description'):
            #    st.header('Longeur des mots de la variable description')
            #    st.area_chart(df.description.apply(lambda x: len(x.split(" "))))
            if st.button('Show Plot Description'):
                figy=plt.figure()
                sns.histplot (words_per_review2, kde=True, binwidth = 10, color='black') #(bins = 100, color='gray')
                plt.xlim(0,200)
                st.pyplot (figy)

            #if st.button('Show Plots'):
                #st.header('Longeur des mots de la variable description')
                #st.pyplot (figx)

            target = pd.read_csv('Y_train_CVw08PX.csv',index_col=0)

            st.header('Etude du target RAKUTEN ')
            st.write()

            df['target'] = target

            if st.button('Show Plot Target'):
                figa=plt.figure(figsize=(18,12))
                df['target'].value_counts().plot.pie()
                plt.xticks(rotation=45)
                st.pyplot (figa)

            #df['target'] = target
            ##percent_target = 100 * df['target'].value_counts()/len(df)
            #percent_target = df['target'].value_counts(sort=True, ascending=True)
            #if st.button('Show Plot Target'):
            #    figz=plt.figure()
            #    sns.barplot (x=df['target'], y=percent_target, color='gray') #(bins = 100, color='gray')
            #    plt.xticks(rotation=90)
            #    st.pyplot (figz)

elif mode == "Model Game":
        st.title("Model Game")
        st.write("Choose the selected label of image")

        # CODE EDGAR#####

        st.sidebar.write('selected class:')
        # Initialization
        if 'key' not in st.session_state:
            st.session_state['key'] = 'value'

        #st.json({'name':"Jesse",'gender':"male"})
        classes ={'Livre occasion' : 10,'Journaux et revues' :2280}
        #,2403,2522,2705,40,50,60,2462,2905,1140,1160,1180,1280,1281,
        #          1300,1302,1560,2582,1320,2220,2583,2585,1920,2060,1301,1940}

        for classe in classes:
            for label in classe :
                if st.session_state[label]:
                    class_man = classe [label]
                    st.sidebar.write(class_man)

        # init
        if 'true_class' not in st.session_state:
            st.session_state['true_class']='value'

        if class_man==st.session_state['true_class']:
            st.sidebar.write('Bonne réponse')

        else :
            st.sidebar.write('Mauvaise réponse')

        st.sidebar.write('Vraie classe :')
        st.session_state['true_class']=all_classes[y[i].item()]

        @st.cache
        def load_tf_model():
            path=r'C:\Users\utilisateur\CatModel_84916_model.hdf5'
            saved_model = tf.keras.models.load_model(path)
            # saved_model.load_weights(model_path + filename + '.hdf5')
            return saved_model()

        model = load_tf_model()

        test_datagen=tf.keras.preprocessing.image.ImageDataGenerator

elif mode == "A little theory":

        st.title("Theory of CNN")

        st.write(" Sources : Exemple of efficient CNN pretrained model"
                 "https://keras.io/api/applications/efficientnet/. "
                 "Exemple of API CONCATENATE OF MODELS"
                 "https://keras.io/api/layers/merging_layers/concatenate/.")

        if st.button('CNN'):

            uploaded_file = st.file_uploader("CONV_1", type="png")

        if uploaded_file is not None:
        # Convert the file to an opencv image.
            file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
            opencv_image = cv2.imdecode(file_bytes, 1)

            # Now do something with the image! For example, let's display it:
            st.image(opencv_image, channels="BGR")


from django.contrib import admin
from django.contrib.admin import SimpleListFilter
from django.utils.translation import ugettext_lazy as _
from app.models import *

class CarInline(admin.TabularInline):
    model = Order.car.through

class CarAdmin(admin.ModelAdmin):
    list_display = ('brand', 'dimensions', 'engine', 'image', 'color', 'num_seat')
    inlines = [
        CarInline,
    ]
    #actions_on_bottom = False
    #date_hierarchy = 'date'
    #list_editable = ['color',]
    #search_fields = ('num_seat',)
    #fields = (('brand', 'engine'), 'color', 'num_seat')
"""    fieldsets = (
        (None, {'fields': (('brand','color', 'image'),'num_seat',)}),
                 ('Advanced options',
                  {
                      'classes':('collapse',),
                      'fields':('price',),
                      'description':'<b>This is descriptions!</b>'
                  })
    )
"""

class FuelAdmin(admin.ModelAdmin):
    list_display = ('brand', 'type')

class BrandAdmin(admin.ModelAdmin):
    list_display = ('name',)

class EngineAdmin(admin.ModelAdmin):
    list_display = ('power','displacement','fuel')

class DimensionsAdmin(admin.ModelAdmin):
    list_display = ('length','height','weight')

class TechCertificateAdmin(admin.ModelAdmin):
    list_display = ('VIN',)

class OwnerAdmin(admin.ModelAdmin):
    list_display = ('name', 'tel', 'address')

class TotalListFilter(SimpleListFilter):

    title = _('Total')

    parameter_name = 'total'

    def lookups(self, request, model_admin):

        return (
            ('100d', _('less than $ 100')),
            ('10000d', _('more than $ 100 of less than $ 10,000')),
            ('100000d', _('more than $ 10,000')),
        )

    def queryset(self, request, queryset):

        if self.value() == '100d':
            return queryset.filter(total__lt=100)
        if self.value() == '10000d':
            return queryset.filter(total__gte=100,
                                   total__lt=10000)
        if self.value() == '100000d':
            return queryset.filter(total__gte=10000)

class OrderAdmin(admin.ModelAdmin):
    list_display = ('owner', 'date', 'total', 'quantity')
    list_filter = (TotalListFilter,)
    inlines = [
        CarInline,
    ]
    exclude = ('car',)

admin.site.register(Car, CarAdmin)
admin.site.register(Fuel, FuelAdmin)
admin.site.register(Brand, BrandAdmin)
admin.site.register(Engine, EngineAdmin)
admin.site.register(Dimensions, DimensionsAdmin)
admin.site.register(TechCertificate, TechCertificateAdmin)
admin.site.register(Owner, OwnerAdmin)
admin.site.register(Order, OrderAdmin)


from django.urls import path
from .views import primera,segunda,login, formulario,admin,agregarcanciones,agregarartistas,linkeimagen,lista
urlpatterns = [
path ('Inicio/', primera , name='homes'),
path ('Secundario/', segunda, name='dosh'),
path ('Login/', login, name='login'),
path ('Formulario/', formulario, name='Form'),
path ('Admin/', admin, name='Admin'),
path ('AgregarCanciones/', agregarcanciones, name='Agregacanciones'),
path ('AgregarArtistas/', agregarartistas, name='Agregaartistas'),
path ('LinkeImagen/', linkeimagen, name='Admin'),
path ('Link/', lista, name='lista'),

]


from collections import defaultdict

import numpy as np
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import Border, Font, PatternFill, Side
from openpyxl.utils import get_column_letter

class WriteInExcel:

    def __init__(self, file_name='UML_Spreadsheet.xlsx'):
        """[summary]

        Keyword Arguments:
            file_name {str} -- [description] (default: {'UML_Spreadsheet.xlsx'})
        """
        self.file_name = file_name
        self.writer = pd.ExcelWriter(self.file_name, engine='xlsxwriter')
        self.count = 0

    def get_number_of_rows_in_df(self, agg_data):
        """compute number of rows in the dataframe using aggregated data.

        Arguments:
            agg_data {list} -- the complete data generated for the source code.

        Returns:
            int -- number of rows that will be plotted in the dataframe.
        """
        number_of_rows = 0
        for class_dict in agg_data:
            number_of_rows += 1 + len(class_dict['Methods'])
        return number_of_rows

    def create_pandas_dataframe(self, agg_data, skip_cols):
        """create pandas dataframe to be plotted in the excel/csv sheet.

        Arguments:
            agg_data {list} -- the complete data generated for the source code.
            skip_cols {int} -- number of columns to be skipped on the left hand side of the
                               class in the dataframe.

        Returns:
            pandas.DataFrame -- the final dataframe created which is to be further styled and plotted in excel.
        """
        number_of_rows = self.get_number_of_rows_in_df(agg_data)
        print('Number of rows in total: {}'.format(number_of_rows))
        columns = ['' for _ in range(skip_cols+2)]
        df = pd.DataFrame(
            index=np.arange(0, number_of_rows),
            columns=columns+['Class']
        )
        row_counter = 0
        column_counter = skip_cols-1
        # mapping to store row, col for dependents.
        dependents_col_counter = {}
        # parent: child mapping created just for plotting tree like line
        parent_to_child_mapping = defaultdict(list)
        dependee_to_dependents_mapping = defaultdict(list)
        # {class: [row number, {methods: row_number}]} mapping
        self.class_row_mapping = defaultdict(list)
        # mapping to store dark edges -> 0: [17, 10, 13] is a COLUMN:ROWS range.
        self.dark_edges_column = defaultdict(list)
        self.inheritance_edges_column = defaultdict(list)
        prev_class_row_counter = 0
        for class_data in agg_data:
            base_class = class_data['Class']
            self.class_row_mapping[base_class] = [row_counter, {}]
            methods = class_data['Methods']
            parents = class_data['Parents']
            dependents = class_data['Dependents']
            columns = ['' for _ in range(skip_cols+2)]
            df.loc[row_counter] = columns + [base_class]
            row_counter += 1
            for method in methods:
                # print('Inserting method: {} of class: {} at row: {} and column: {}'.format(
                    # method, base_class, row_counter, skip_cols+1))
                df.iloc[row_counter, skip_cols+2] = method
                self.class_row_mapping[base_class][1][method] = (
                    row_counter, skip_cols+2)
                row_counter += 1
            if parents:
                for parent in parents:
                    parent_to_child_mapping[parent].append(base_class)
            if dependents:
                for dependent in dependents:
                    dependee_to_dependents_mapping[base_class].append(
                        dependent)
            prev_class_row_counter = row_counter

        for dependent in dependents_col_counter.keys():
            for column in dependents_col_counter[dependent]:
                df.iloc[self.class_row_mapping[dependent][0], column] = "←"
                self.dark_edges_column[column].append(
                    self.class_row_mapping[dependent][0])
        # get a single list of dependees and parents so as to draw a common vertical pipe
        dependees_and_parents_combined = set()
        for dependee in dependee_to_dependents_mapping.keys():
            dependees_and_parents_combined.add(dependee)
        for parent in parent_to_child_mapping.keys():
            dependees_and_parents_combined.add(parent)
        print("All classes that are parents or dependees are: ")
        print(dependees_and_parents_combined)
        for class_ in dependees_and_parents_combined:
            if not self.class_row_mapping.get(class_, None):
                print(
                    'This class is in dependees and parents combined but not in class row mapping: {}'.format(class_))
                continue
            df.iloc[self.class_row_mapping[class_][0]][column_counter] = "⇒"
            self.dark_edges_column[column_counter].append(
                self.class_row_mapping[class_][0]
            )
            if class_ in dependee_to_dependents_mapping:
                df.iloc[self.class_row_mapping[class_][0]][skip_cols] = "⇒"
                for dependent in dependee_to_dependents_mapping[class_]:
                    df.iloc[self.class_row_mapping[dependent]
                            [0], column_counter] = "←"
                    self.dark_edges_column[column_counter].append(
                        self.class_row_mapping[dependent][0])
            if class_ in parent_to_child_mapping:
                df.iloc[self.class_row_mapping[class_][0]][skip_cols+1] = "▷"
                for child in parent_to_child_mapping[class_]:
                    df.iloc[self.class_row_mapping[child]
                            [0], column_counter] = "◁"
                    self.dark_edges_column[column_counter].append(
                        self.class_row_mapping[child][0])
            column_counter -= 1
        # convert all NaN to None.
        df = df.replace(np.nan, '', regex=True)
        # print("Create DF was called.")
        # print(df)
        return df

    def integrate_sequence_diagram_in_df(self, df, function_sequence, use_case):
        """integrates sequence diagram in the dataframe.

        Arguments:
            df {pandas.DataFrame} -- dataframe that has to incorporate the sequence diagram.
            function_sequence {[type]} -- sequence of the functions called while tracing a particular sequence diagram.
            use_case {str} -- name of the use case for which we are drawing the sequence diagram.

        Returns:
            [type] --
        """
        # add new columns in dataframe
        number_of_columns_pre_sequence = len(df.columns)
        for event_number in range(len(function_sequence)):
            df['{}'.format(event_number)] = np.nan
        event_counter = 1
        # counter to check whether its the first or last column in sequence diagram section
        for event in function_sequence:
            print("Event is: {}".format(event))
            if "main_2" in event:
                continue
            caller, callee = event
            print("caller is: {}".format(caller))
            caller_class, caller_function = caller.split('.')
            callee_class, callee_function = callee.split('.')
            caller_row_number, caller_column_number = self.class_row_mapping[
                caller_class][1][caller_function]
            callee_row_number, callee_column_number = self.class_row_mapping[
                callee_class][1][callee_function]
            caller_column_number, callee_column_number = number_of_columns_pre_sequence + \
                event_counter - 1, number_of_columns_pre_sequence + event_counter - 1
            self.dark_edges_column[caller_column_number].extend(
                [caller_row_number, callee_row_number])
            if df.iloc[caller_row_number, caller_column_number] == "←":
                df.iloc[caller_row_number, caller_column_number] = "↔"
            else:
                df.iloc[caller_row_number, caller_column_number] = "→"
            if df.iloc[callee_row_number, callee_column_number] == "→":
                df.iloc[callee_row_number, callee_column_number] = "↔"
            else:
                df.iloc[callee_row_number, callee_column_number] = "←"
            event_counter += 1
        df = df.replace(np.nan, '', regex=True)
        return df

    def write_df_to_excel(self, df, sheet_name, skip_cols, classes={}, use_case=None):
        """write dataframe to excel

        Arguments:
            df {pandas.DataFrame} -- the dataframe that we have to write to excel after making some changes.
            sheet_name {str} -- name of the target excel sheet/
            skip_cols {int} -- number of columns to be skipped for drawing out arrows.

        Keyword Arguments:
            classes {dict} -- name of the classes (default: {{}})
            use_case {str} -- use case for which we are plotting a particular sequence diagram (default: {None})
        """
        # print("Use Case as an argument is: {}".format(use_case))
        self.count += 1
        if self.count == 2:
            self.file_name = 'Use_Case_{}'.format(use_case) + self.file_name
            self.writer = pd.ExcelWriter(self.file_name, engine='xlsxwriter')
        df.to_excel(self.writer, sheet_name=sheet_name,
                    header=True, index=False)
        self.writer.save()
        self.writer.close()
        import sys
        wb = load_workbook(filename=self.file_name)
        ws = wb[sheet_name]
        ws.column_dimensions['{}'.format(get_column_letter(
            skip_cols+1))].width = 8.4  # len(Parent) = (6+1)*1.2
        # len(Methods/Children) + 1(M) + 1(C) + 1(/) = 19*1.2
        ws.column_dimensions['{}'.format(
            get_column_letter(skip_cols+2))].width = 22.8
        bd = Side(style='thick', color='000000')
        # to check whether a col in sheet's columns has arrived for dark edges.
        col_check_counter = 0
        print('Skip columns are: {}'.format(skip_cols))
        for _ in ws.columns:
            if col_check_counter in self.dark_edges_column:
                print('Col {} is in dark edges column'.format(col_check_counter))
                column_letter = get_column_letter(col_check_counter+1)
                row_range = sorted(self.dark_edges_column[col_check_counter])
                '''
                plus one because dataframe's 0 is excel'1 but column headings are on 1 so we need to start from 2.
                the second addition of changing 0-indexed to 1-indexed comes in the next line while plotting.
                Now, second index of list is +2 so that we can cover the full range(0 to 3 is to be covered,
                so for loop would be from range(0,4))
                get the first and last row to draw the dark edges.
                '''
                for row_iterator in range(row_range[0]+1, row_range[-1]+2):
                    print('{}{}'.format(column_letter, row_iterator+1))
                    if col_check_counter >= skip_cols:  # should it be > or >=
                        ws['{}{}'.format(column_letter, row_iterator+1)
                           ].border = Border(right=bd)
                    else:
                        ws['{}{}'.format(column_letter, row_iterator+1)
                           ].border = Border(left=bd)
            col_check_counter += 1

        red_fill = PatternFill(
            start_color='FFFF0000',
            end_color='FFFF0000',
            fill_type='solid'
        )
        for row in range(1, ws.max_row+1):
            column_letter = get_column_letter(skip_cols+1)
            if ws['{}{}'.format(column_letter, row)].value:
                ws['{}{}'.format(column_letter, row)].fill = red_fill
            column_letter = get_column_letter(skip_cols+2)
            if ws['{}{}'.format(column_letter, row)].value:
                ws['{}{}'.format(column_letter, row)].fill = red_fill
        # add a new row in the worksheet
        if use_case:
            print("Working for the Use Case: {}".format(use_case))
            ws.insert_rows(0)
            use_case_column_letter = get_column_letter(skip_cols+4)
            use_case_cell = '{}{}'.format(use_case_column_letter, 1)
            print("Use Case cell is {}".format(use_case_cell))
            ws[use_case_cell] = use_case
            # adjust the width of all columns
        col_counter = 1
        for _ in ws.columns:
            if col_counter == skip_cols+3:
                ws.column_dimensions[get_column_letter(col_counter)].width = 36
                col_counter += 1
                continue
            else:
                ws.column_dimensions[get_column_letter(
                    col_counter)].hidden = True
            ws.column_dimensions[get_column_letter(col_counter)].width = 3
            col_counter += 1
        # give bold font to cells with Classes
        classes_column = get_column_letter(skip_cols+3)
        font = Font(bold=True)
        for row in range(1, ws.max_row+1):
            if ws['{}{}'.format(classes_column, row)].value:
                if ws['{}{}'.format(classes_column, row)].value in classes:
                    ws['{}{}'.format(classes_column, row)].font = font
                else:
                    ws.row_dimensions[row].hidden = True
        wb.save(self.file_name)
        print("{}:{} done!".format(self.file_name, sheet_name))


# Python program to illustrate HoughLine
# method for line detection
import cv2
import numpy as np

# Reading the required image in
# which operations are to be done.
# Make sure that the image is in the same
# directory in which this python program is
#img = cv2.imread('sudoku-original.jpg')
img = cv2.imread('index.jpeg')

# filter colour range we want
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
low_yellow = np.array([18, 94, 140])
up_yellow = np.array([48, 255, 255])
mask = cv2.inRange(hsv, low_yellow, up_yellow)

cv2.imshow('mask',mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Apply edge detection method on the image
edges = cv2.Canny(mask,75,150)

# This returns an array of r and theta values
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 30, maxLineGap=250)

# The below for loop runs till r and theta values
# are in the range of the 2d array
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 3)

cv2.imshow('bexiga',edges)
cv2.imshow('imagem',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User

class Article(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()
    author = models.ForeignKey(User)
    created_at = models.DateTimeField()
    image = models.ImageField(upload_to='article_images/', blank=True)

    def __unicode__(self):
        return self.title

class Comment(models.Model):
    article = models.ForeignKey(Article, related_name='comments')
    text = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User)
    image = models.ImageField(upload_to='comments_images/', blank=True)

    def __unicode__(self):
        return self.article.title

class Period(models.Model):
    start_date = models.DateField()
    end_date = models.DateField()


from benchbuild.utils.wrapping import wrap
from benchbuild.projects.benchbuild.group import BenchBuildGroup
from benchbuild.utils.compiler import lt_clang
from benchbuild.utils.downloader import Wget
from benchbuild.utils.run import run

from plumbum import local
from benchbuild.utils.cmd import tar, cp, make

from os import path

class XZ(BenchBuildGroup):
    """ XZ """
    NAME = 'xz'
    DOMAIN = 'compression'
    VERSION = '5.2.1'

    testfiles = ["text.html", "chicken.jpg", "control", "input.source",
                 "liberty.jpg"]

    def prepare(self):
        super(XZ, self).prepare()
        testfiles = [path.join(self.testdir, x) for x in self.testfiles]
        cp(testfiles, self.builddir)

    src_dir = "xz-{0}".format(VERSION)
    SRC_FILE = src_dir + ".tar.gz"
    src_uri = "http://tukaani.org/xz/" + SRC_FILE

    def download(self):
        Wget(self.src_uri, self.SRC_FILE)
        tar('xfz', self.SRC_FILE)

    def run_tests(self, experiment, run):
        exp = wrap(path.join(self.src_dir, "src", "xz", "xz"), experiment)

        # Compress
        run(exp["--compress", "-f", "-k", "-e", "-9", "text.html"])
        run(exp["--compress", "-f", "-k", "-e", "-9", "chicken.jpg"])
        run(exp["--compress", "-f", "-k", "-e", "-9", "control"])
        run(exp["--compress", "-f", "-k", "-e", "-9", "input.source"])
        run(exp["--compress", "-f", "-k", "-e", "-9", "liberty.jpg"])

        # Decompress
        run(exp["--decompress", "-f", "-k", "text.html.xz"])
        run(exp["--decompress", "-f", "-k", "chicken.jpg.xz"])
        run(exp["--decompress", "-f", "-k", "control.xz"])
        run(exp["--decompress", "-f", "-k", "input.source.xz"])
        run(exp["--decompress", "-f", "-k", "liberty.jpg.xz"])

    def configure(self):
        clang = lt_clang(self.cflags, self.ldflags)
        with local.cwd(self.src_dir):
            configure = local["./configure"]
            with local.env(CC=str(clang)):
                run(configure["--enable-threads=no", "--with-gnu-ld=yes",
                              "--disable-shared",
                              "--disable-dependency-tracking",
                              "--disable-xzdec", "--disable-lzmadec",
                              "--disable-lzmainfo", "--disable-lzma-links",
                              "--disable-scripts", "--disable-doc"])

    def build(self):
        clang = lt_clang(self.cflags, self.ldflags, self.compiler_extension)
        with local.cwd(self.src_dir):
            run(make["CC=" + str(clang), "clean", "all"])


#!/usr/bin/env python

""" Gets the read bases at each position specified by the first argument, which
is a VCF/BED/GFF/GTF file, from an indexed BAMfile/s or pileup/s:
    1. Whether the loci is heterzygous or homozygous if the genotyping is not
    known
    2. The p-value of the loci exhibiting allelic imbalance if the loci is
    heterzygous.

Usage:
    python getReads.py bedfile/vcf tab-delimited sample_mapping -o OUTPUT

Limtations: Doesn't accurately account paralogous gene expression, which can
manifest itself in allelic imbalance.  One way would be to have an input file
of all paralogous genes and calculated the probability of a read mismatching
and then the liklihood that it is something not valulable.

Written by Jeffrey Hsu
2010
"""

# Standard Libraries
import optparse, pickle
import cProfile
import functools
from collections import defaultdict

import pysam
import numpy as np
import pandas as pd

from pySeq.formats.panVCF import VCF
import pySeq.stats.likelihood_funcs as lf
from pySeq.pysam_callbacks.allele_counter import AlleleCounter

class lociInformation():
    """ Contains information about a given position in a BAM file

    Change into slots also make this into cython.
    """

    BASE_INDEX = {'A':0, 'a':0, 'C':1, 'c':1, 'G':2, 'g':2, 'T':3, 't':3}
    def __init__(self, region, position, phredThreshold=0):
        """ Location is one-based.  Internally this will get changed to zero
        base by pysam.
        """
        self.region = region
        self.position = int(position)
        self.phredThreshold = phredThreshold
        self.counts = np.zeros(4, dtype=np.uint8)

    def __call__(self, pileups, position=None,
        phredThreshold=None, genotype=None):
        """ Genotype file contains the infromation the actualy genotyping
        calls.

        """
        if position == None: position = self.position
        else:  pass
        if phredThreshold == None: qualT = self.phredThreshold
        if pileups.pos != position-1: pass
        else:
            for read in pileups.pileups:
                qpos = read.qpos
                base_quality = read.alignment.qual[qpos]
                if ord(base_quality)-33 > qualT:
                    base = read.alignment.seq[qpos]
                    base = self.BASE_INDEX[base]
                    #strand = read.alignment.is_reverse
                    self.counts[base] += 1

def threshold_counts(counts, threshold=30, number=1):
    """ Returns True

    Makes sure that at least one of the samples meet a read count threshold.
    """
    counter = 0
    for i in counts:
        if sum(i) > threshold: counter += 1
    if counter >= number:return True
    else: return False

def column_names_to_bams(colname):
    """ Conversion of colnames to actual bam files
    """

    return colname + ".bam"

def counts_for_individuals(sample_genotype, c_m=None, chrm=None, pos=None, path=None,
                           bamfile_func = column_names_to_bams):
    """
    """
    # Restrict to heterozygotes
    reduced_chrm = chrm[sample_genotype == 1]
    reduced_pos = pos[sample_genotype == 1]
    reduced_index = sample_genotype.index[sample_genotype == 1]
    bamfile = pysam.Samfile(sample_genotype.name, 'rb')
    for i, j, k in zip(reduced_chrm, reduced_pos, reduced_index):
        """ Vectorize this?
        """
        print(i, j)
        variant = lociInformation(str(i), j,
                                  phredThreshold=0)
        bamfile.pileup(variant.region, variant.position,
                        variant.position+1, callback=variant)
        """
        variant_2 = AlleleCounter('chr' + str(i), j,
                                   phredThreshold=0)
        bamfile.fetch(variant.region, variant.position,
                        variant.position+1, callback=variant_2)
        print(variant.counts, variant_2.counts)
        """
        c_m.ix[k, [(sample_genotype.name, 0), (sample_genotype.name, 1), (sample_genotype.name, 2), (sample_genotype.name, 3),]] = variant.counts.T

def inner_apply(x, chrom, position):
    """ Try inner apply for the
    """
    pass

def main():
    """ Main loop that iterates over the desired loci to calculate Aellic
    Imbalance

    """

    #################################################################
    # Argument and Options Parsing
    #################################################################

    p = optparse.OptionParser(__doc__)
    p.add_option("-o", "--output", dest="filename", help="write \
            report to FILE")
    p.add_option("-G", "--genotype", dest="G", help=\
            "Use imputed/genotypes if available, should be in VCF file format")
    p.add_option("-v",  "--vcf_file", action="store_true", dest="inputisvcfile",
                 help="the input is a VCF file")
    p.add_option("-q", "--quality_threshold", type="int", dest="qual",
                 help="base quality threshold to take allele counts from")
    p.add_option("-p", "--pileup", action="store_true", dest="p",
                 help= "Input files are pileup files")
    p.add_option("-D", "--debug", action="store_true", dest="D", help="debug")
    p.add_option("-c", "--count-threshold", action="store", type="int",
                 dest="c", help="Set the count threshold for making AEI calls")
    p.add_option("-V", "--output_vcf", action="store_true", dest="outputVCF",
                 help="Output the results to a VCF file")
    p.add_option("-A", "--auto_parse", action="store_true", dest="auto",
                 help="Autoparse readgroups, if set to false will assume a\
                 single sample in each file")

    options, args = p.parse_args()
    if options.qual: pass
    else: options.qual = 20

    # For testing purposes
    debug = 1
    output = open(options.filename, 'wb')

    vcf = VCF(args[0])

    # A tab delimited file mapping sample names to bams #############
    bam_inputs = open(args[1], 'rU')
    sample_to_file = {}
    for line in bam_inputs:
        line = line.split("\t")
        sample_to_file[line[0]] = line[1].rstrip("\n")

    # Handling of multiple BAM/SAM inputs

    INDEX_BASE = ['A', 'C', 'G', 'T']
    if options.c:
        count_threshold = options.c
    else:
        count_threshold = 20

    multi_tuples = []
    # Temproary change the column names
    vcf.vcf.rename(columns=sample_to_file, inplace=True)
    for i in vcf.vcf.columns[9:]:
        multi_tuples.append(i)
        multi_tuples.append(i)
        multi_tuples.append(i)
        multi_tuples.append(i)
    multi = zip(multi_tuples, [0,1,2,3]*len(vcf.vcf.columns[9:]))
    multi_index = pd.MultiIndex.from_tuples(multi, names=['sample', 'alleles'])

    counts_matrix = pd.DataFrame(np.zeros((vcf.vcf.shape[0], len(vcf.vcf.columns[9:])*4), dtype=np.int16),
                                 index=vcf.vcf.index, columns=multi_index)

    counts_fixed = functools.partial(counts_for_individuals, c_m=counts_matrix,
                                     chrm=vcf.vcf['CHROM'], pos=vcf.vcf['POS'])
    vcf.vcf.ix[:, 9:].apply(counts_fixed, axis=0)
    counts_matrix.to_csv(options.filename)
    pickle.p

if __name__ == '__main__':
    main()
    #cProfile.run('main()')


import datetime

import pytz

from django.db import models
from django.db.models import F
from django.utils.translation import ugettext_lazy as _

from smart_selects.db_fields import ChainedForeignKey

from tracpro.polls.models import Answer, Question, Poll

class BaselineTerm(models.Model):
    # e.g., 2015 Term 3 Attendance for P3 Girls
    # A term of time to gather statistics for a baseline chart
    # baseline_question: the answer to this will determine our baseline
    #                    information for all dates
    #                    ie. How many students are enrolled?
    # follow_up_question: the answers to this question will determine the
    #                     follow-up information, over the date range
    #                     (start_date -> end_date)
    #                     ie. How many students attended today?

    org = models.ForeignKey("orgs.Org", verbose_name=_(
        "Organization"), related_name="baseline_terms")
    name = models.CharField(max_length=255, help_text=_(
        "For example: 2015 Term 3 Attendance for P3 Girls"))
    start_date = models.DateField()
    end_date = models.DateField()

    baseline_poll = models.ForeignKey(Poll, related_name="baseline_terms")
    baseline_question = ChainedForeignKey(
        Question,
        chained_field='baseline_poll',
        chained_model_field='poll',
        auto_choose=True,
        related_name="baseline_terms",
        help_text=_("All baseline poll results over time will display in chart.")
    )

    follow_up_poll = models.ForeignKey(Poll)
    follow_up_question = ChainedForeignKey(
        Question,
        chained_field='follow_up_poll',
        chained_model_field='poll',
        auto_choose=True,
        help_text=_("Follow up poll responses over time to compare to the baseline values.")
    )
    y_axis_title = models.CharField(max_length=255, null=True, blank=True,
                                    help_text=_("The title for the y axis of the chart."))

    @classmethod
    def get_all(cls, org):
        baseline_terms = cls.objects.filter(org=org)
        return baseline_terms

    def _get_answers(self, question, regions, region_selected):
        """
        Retrieve answers to the question that are relevant for this
        BaselineTerm.
        """
        midnight = datetime.time(0, 0, 0, tzinfo=pytz.utc)
        start = datetime.datetime.combine(self.start_date, midnight)
        end = datetime.datetime.combine(self.end_date + datetime.timedelta(days=1), midnight)

        answers = Answer.objects.filter(
            question=question, submitted_on__gte=start, submitted_on__lt=end)
        answers = answers.annotate(region_name=F('response__contact__region__name'))
        answers = answers.select_related('response', 'response__contact')

        all_regions = answers.values('response__contact__region',
                                     'region_name').distinct('response__contact__region')

        if regions:
            answers = answers.filter(response__contact__region__in=regions)
            all_regions = answers.values('response__contact__region',
                                         'region_name').distinct('response__contact__region')

        if region_selected:
            answers = answers.filter(response__contact__region__in=[region_selected])

        return answers, all_regions

    def get_baseline(self, regions, region_selected):
        """ Get all baseline responses """
        answers, all_regions = self._get_answers(self.baseline_question, regions, region_selected)

        # Separate out baseline values per region
        region_answers = {}
        dates = []
        for region_name in set(a.region_name.encode('ascii') for a in answers):
            answers_by_region = answers.filter(region_name=region_name)
            answer_sums, dates = answers_by_region.numeric_sum_group_by_date()
            region_answers[region_name] = {'values': answer_sums}
        return region_answers, dates

    def get_follow_up(self, regions, region_selected):
        """ Get all follow up responses summed by region """
        answers, all_regions = self._get_answers(self.follow_up_question, regions, region_selected)

        # Loop through all regions in answers and create
        # a dict of values and dates per Region
        # ex.
        # { 'Kumpala': {'values': [35,...], 'dates': [datetime.date(2015, 8, 12),...]} }
        region_answers = {}
        dates = []
        for region_name in set(a.region_name.encode('ascii') for a in answers):
            answers_by_region = answers.filter(region_name=region_name)
            answer_sums, dates = answers_by_region.numeric_sum_group_by_date()
            region_answers[region_name] = {'values': answer_sums}
        return region_answers, dates, all_regions

    def check_for_data(self, regions):
        answers, all_regions = self._get_answers(self.baseline_question, regions, 0)
        if answers:
            return True
        else:
            return False


import math
import numpy as np
from craam import crobust
from craam_utility import convertDeterministicPolicyListToStochastic
from craam_utility import convertDeterministicPolicyToStochastic
from ortools.linear_solver import pywraplp
from Simulator import Simulator
from PolicySelection import PolicySelection

class PolicyGeneration:
    def __init__(self, horizon, discount_factor, initial_state_distribution):
        self.valueMap = None
        self.horizon = horizon
        self.discount_factor = discount_factor
        self.initial_state_distribution = initial_state_distribution

    def generateAllPolicies(self, optimalPolicies, mdpDictionary):
        policiesToEvaluate = []
        policyNames = []

        """
        policiesToEvaluate.append(PolicySelection(self.selectBestPolicyMaxMinStateCombined(optimalPolicies, mdpDictionary)))
        policyNames.append("MaxMinStateCombined")
        policiesToEvaluate.append(PolicySelection(self.selectBestPolicyMinMaxStateCombined(optimalPolicies, mdpDictionary)))
        policyNames.append("MinMaxStateCombined")
        policiesToEvaluate.append(PolicySelection(self.selectBestPolicyBalancedStateCombined(optimalPolicies, mdpDictionary)))
        policyNames.append("BalancedStateCombined")
        """

        policiesToEvaluate.append(self.selectBestPolicyMaxMin(optimalPolicies, mdpDictionary))
        policyNames.append("Robust")
        policiesToEvaluate.append(self.selectBestPolicyMinMax(optimalPolicies, mdpDictionary))
        policyNames.append("Regret Minimization")
        policiesToEvaluate.append(self.selectBestPolicyBalanced(optimalPolicies, mdpDictionary))
        policyNames.append("Selection Regret")
        #policiesToEvaluate.append(self.selectBestPolicyMilpDet(optimalPolicies, mdpDictionary))
        #policyNames.append("MILP-Det")
        #policiesToEvaluate.append(self.selectBestPolicyOsr(optimalPolicies, mdpDictionary))
        #policyNames.append("OSR")

        return (policiesToEvaluate, policyNames)

    def _initializeValueMaps(self, policies, mdps):
        if self.valueMap is None:
            self.valueMap = self._generateValueMaps(convertDeterministicPolicyListToStochastic(policies), mdps)

    def _generateValueMaps(self, policies, mdps):
        # Get the average values for all policies and natures
        numNatures = len(mdps)
        numPolicies = len(policies)
        valueMap = [[0 for x in range(numPolicies)] for y in range(numNatures)]
        for policyIndex in range(numPolicies):
            currentPolicy = policies[policyIndex]
            for natureIndex in range(numNatures):
                currentMdp = mdps[natureIndex]
                results_array = currentMdp.solve_mpi_stochastic(iterations=self.horizon, policy=currentPolicy)[0]
                results = np.average(np.array(results_array))
                valueMap[natureIndex][policyIndex] = results
        return valueMap

    def _getValueMap(self, policies, mdps):
        self._initializeValueMaps(policies, mdps)
        return self.valueMap

    def selectBestPolicyMaxMinStateCombined(self, optimalPolicies, mdps):
        #Solve the linear progam
        valueMap = self._getValueMap(optimalPolicies, mdps)
        policyProbabilities = self._getLinearProgrammingSolution(np.array(valueMap), "MaxMin")
        self._printProbabilites(policyProbabilities, "maxminstatecombined")

        #Use the probabilities to create a new policy
        return self._combinePolicies(convertDeterministicPolicyListToStochastic(optimalPolicies), policyProbabilities)

    def selectBestPolicyMaxMin(self, optimalPolicies, mdps):
        #Solve the linear progam
        valueMap = self._getValueMap(optimalPolicies, mdps)
        policyProbabilities = self._getLinearProgrammingSolution(np.array(valueMap), "MaxMin")
        self._printProbabilites(policyProbabilities, "maxmin")

        #Use the probabilities to create a new policy
        return PolicySelection(convertDeterministicPolicyListToStochastic(optimalPolicies), policyProbabilities)

    def selectBestPolicyMinMaxStateCombined(self, optimalPolicies, mdps):
        #Create the regret matrix
        valueMap = self._getValueMap(optimalPolicies, mdps)
        numNatures = len(mdps)

        regretMap = np.array(valueMap).max(axis=1).reshape(numNatures,1) - valueMap

        policyProbabilities = self._getLinearProgrammingSolution(regretMap, "MinMax")
        self._printProbabilites(policyProbabilities, "minmaxstatecombined")

        #Use the probabilities to create a new policy
        return self._combinePolicies(convertDeterministicPolicyListToStochastic(optimalPolicies), policyProbabilities)

    def selectBestPolicyMinMax(self, optimalPolicies, mdps):
        #Create the regret matrix
        valueMap = self._getValueMap(optimalPolicies, mdps)
        numNatures = len(mdps)

        regretMap = np.array(valueMap).max(axis=1).reshape(numNatures,1) - valueMap

        policyProbabilities = self._getLinearProgrammingSolution(regretMap, "MinMax")
        self._printProbabilites(policyProbabilities, "minmax")

        #Use the probabilities to create a new policy
        return PolicySelection(convertDeterministicPolicyListToStochastic(optimalPolicies), policyProbabilities)

    def selectBestPolicyMilpDet(self, optimalPolicies, mdps):
        solver = pywraplp.Solver('MySolver', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
        optimalPolicies = np.array(optimalPolicies)
        numActions = np.max(optimalPolicies)

        #Get the maximum value that any state/action could produce
        maxStateActionValue = self._getValueMap(optimalPolicies, mdps)[0][0]
        for currentMdp in mdps:
            results_array = currentMdp.solve_mpi_stochastic(iterations=self.horizon)[0]
            thisMaxValue = np.max(results_array)
            if thisMaxValue > maxStateActionValue:
                maxStateActionValue = thisMaxValue

        #Create a policy of intvars
        milpPolicy = [[solver.IntVar(0,1,str("p" + str(i) + "," + str(j))) for j in range(numActions)] for i in range(optimalPolicies.shape[1])]

        #Setup the constraint that exactly one of each action must be 1
        for currentState in milpPolicy:
            solver.Add(np.sum(currentState) == 1 )

        #For each nature setup the constraint that regret is greater than or equal to the highest possible value minus the expected value of a policy
        regret = solver.NumVar(-solver.infinity, solver.infinity, "Regret")
        maxValues = np.array(valueMap).max(axis=1)
        for i in range(len(mdps)):
            currentMdp = mdps[i]
            bestValue = maxValue[i]
            solver.Add(regret >= bestValue - expectedValue)

        pass

    def selectBestPolicyOsr(self, optimalPolicies, mdps):
        keepGoing = True
        goodEnough = 1  #Because I said so
        optimalPolicyValueMap = self._getValueMap(optimalPolicies, mdps)
        maxValuesPerNature = np.array(optimalPolicyValueMap).max(axis=1)

        #Get the maximum value that any state/action could produce
        maxStateActionValue = self._getValueMap(optimalPolicies, mdps)[0][0]
        for currentMdp in mdps:
            results_array = currentMdp.solve_mpi_stochastic(iterations=self.horizon)[0]
            thisMaxValue = np.max(results_array)
            if thisMaxValue > maxStateActionValue:
                maxStateActionValue = thisMaxValue
        osr_new = maxStateActionValue
        osr_old = maxStateActionValue

        #initialize the "random" policy
        policy_half_one = convertDeterministicPolicyToStochastic(optimalPolicies[0])
        policy_half_two = policy_half_one

        #Loop through and...
        while keepGoing:
            keepGoing = False
            tau = self.horizon - 1
            while tau >= 0:
                #Get the state step occupancy data for each nature
                occupancyList = []
                for currentMdp in mdps:
                    previousOccupancy = mdps[0].occfreq_action_horizon_stochcastic(np.array(self.initial_state_distribution), self.discount_factor, \
                                        policy_half_one, self.horizon - 1)
                    thisOccupancy = mdps[0].occfreq_action_horizon_stochcastic(np.array(self.initial_state_distribution), self.discount_factor, \
                                        policy_half_one, self.horizon)
                    stepOccupancy = np.array(thisOccupancy) - np.array(previousOccupancy)
                    occupancyList.append(stepOccupancy)

                #Calcualte the next policy
                policy_half_two, osr_new = self._oneStepRegret(tau, policy_half_one, mdps, occupancyList, maxValuesPerNature, osr_old)
                self._osrUpdateValuesAtTau(policy_half_two)
                if repr(policy_half_two) != repr(policy_half_one):
                    keepGoing = True
                tau = tau - 1
            if math.fabs(osr_old - osr_new) < goodEnough:
                keepGoing = False
            self._osrUpdateOccupancy(policy_half_two)
            policy_half_one = policy_half_two   #This isn't right
            osr_old = osr_new

        return PolicySelection(policy_half_one)

    def _oneStepRegret(self, tau, policy, mdps, occupancyList, maxValuesPerNature, delete_me):
        #We just need to maximize the second component of equation 32 and do equation 33.
        #Then recalculate the value of the new policy and return that as the new osr.
        solver = pywraplp.Solver('MySolver', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
        changeableValue = solver.NumVar(-solver.infinity(), solver.infinity(), "osr")

        """
        solver = pywraplp.Solver('MySolver', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
        osr = solver.NumVar(-solver.infinity(), solver.infinity(), "osr")
        numNature = len(mdps)

        #Get the value of this policy for all natures
        policyValuePerNature = []
        for currentMdp in mdps:
            results_array = currentMdp.solve_mpi_stochastic(iterations=self.horizon, policy=policy)[0]
            policyValuePerNature.append(np.average(np.array(results_array)))

        #OSR greater or equal to the regret of each nature
        for i in range(numNature):
            solver.Add(osr >= maxValuesPerNature[i] - policyValuePerNature[i] )

        solver.Minimize(osr)
        """

        return policy, delete_me

    def _osrUpdateValuesAtTau(self, policy):
        pass

    def _osrUpdateOccupancy(self, policy):
        pass

    def selectBestPolicyBalancedStateCombined(self, optimalPolicies, mdps):
        maxMinPolicy = self.selectBestPolicyMaxMinStateCombined(optimalPolicies, mdps)
        minMaxPolicy = self.selectBestPolicyMinMaxStateCombined(optimalPolicies, mdps)

        bestPolicies = [maxMinPolicy, minMaxPolicy]
        valueMap = np.array(self._generateValueMaps(bestPolicies, mdps))
        regretMap = valueMap.max(axis=1).reshape(valueMap.shape[0],1) - valueMap

        probabilities = self._getLinearProgrammingSolution(regretMap, "MinMax")
        self._printProbabilites(probabilities, "selection regret statecombined")
        return self._combinePolicies(bestPolicies, probabilities)

    def selectBestPolicyBalanced(self, optimalPolicies, mdps):
        maxMinPolicy = self.selectBestPolicyMaxMin(optimalPolicies, mdps)
        minMaxPolicy = self.selectBestPolicyMinMax(optimalPolicies, mdps)

        maxMinValueMap = self._generateValueMaps(maxMinPolicy.getPolicies()[0], mdps)
        minMaxValueMap = self._generateValueMaps(minMaxPolicy.getPolicies()[0], mdps)
        maxMinWeights = maxMinPolicy.getPolicies()[1]
        minMaxWeights = minMaxPolicy.getPolicies()[1]

        maxMinSelectedValue = np.sum(maxMinValueMap * maxMinWeights, axis=1)
        minMaxSelectedValue = np.sum(minMaxValueMap * minMaxWeights, axis=1)

        valueMap = np.append(maxMinSelectedValue, minMaxSelectedValue).reshape((maxMinSelectedValue.shape[0],2), order='F')
        regretMap = valueMap.max(axis=1).reshape(valueMap.shape[0],1) - valueMap

        probabilities = self._getLinearProgrammingSolution(regretMap, "MinMax")
        self._printProbabilites(probabilities, "selection regret")
        return PolicySelection.combine(np.array([maxMinPolicy, minMaxPolicy]), probabilities)

    def _printProbabilites(self, probabilities, algorithmName):
        weightString = ""
        for probability in probabilities:
            weightString = weightString + " & "
            weightString = weightString + "{0:.4f}".format(probability)

        print(algorithmName + weightString)

    def _combinePolicies(self, policyArray, probabilityArray):
        policyMatrices = [np.matrix(x) for x in policyArray]
        policyArray = sum(np.multiply(policyMatrices, \
            np.array(probabilityArray).reshape(len(probabilityArray),1,1)))
        return policyArray

    # The matrix is a [x][y] matrix where x are natures and y are policies
    def _getLinearProgrammingSolution(self, valueMap, typeName):
        numPolicies = valueMap.shape[1]
        numNatures = valueMap.shape[0]

        minValue = np.min(valueMap) - 1
        adjustedValueMap = np.array(valueMap) - minValue

        solver = pywraplp.Solver('MySolver', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
        probabilities = [solver.NumVar(0.0, 1.0, str(i)) for i in range(numPolicies)]
        value = solver.NumVar(-solver.infinity(), solver.infinity(), "value")

        if typeName == "MaxMin":
            solver.Add(np.sum(probabilities) == 1 )
            for i in range(numNatures):
                currentNatureValueRow = adjustedValueMap[i]
                condition = probabilities[0] * currentNatureValueRow[0]
                for j in range(len(probabilities)-1):
                    condition = condition + probabilities[j+1] * currentNatureValueRow[j+1]
                solver.Add( value <= condition )

            solver.Maximize( value )
        elif typeName == "MinMax":
            solver.Add(np.sum(probabilities) == 1 )
            for i in range(numNatures):
                currentNatureValueRow = adjustedValueMap[i]
                condition = probabilities[0] * currentNatureValueRow[0]
                for j in range(len(probabilities)-1):
                    condition = condition + probabilities[j+1] * currentNatureValueRow[j+1]
                solver.Add( value >= condition )

            solver.Minimize( value )
        else:
            raise Exception("Bad")

        solver.Solve()

        policyProbabilities = [probabilities[i].solution_value() for i in range(numPolicies)]
        return policyProbabilities


from datetime import date

from pandas.io import json
from attractions_recc import *
import pandas as pd
import re
import datetime as dt
import json

low = 50
high = 899
province = 'british_columbia'
start = dt.date.today()
end = start + dt.timedelta(6)
att_df = pd.read_json('etl/attractions.json',orient='records')

category_df = att_df.groupby('category').size().reset_index().sort_values([0],ascending=False)[:20]
categories = list(category_df.category.values)
cat_rating = {'luxury_&_special_occasions': 5.0, 'outdoor_activities': 4.0, 'recommended_experiences': 5.0, 'food,_wine_&_nightlife': 4.0, 'cruises,_sailing_&_water_tours': 3.0, 'cultural_&_theme_tours': 5.0}

def feed_input(price_low,price_high,province,days,att_df,categories,cat_rating):
    filename, user, rbm_att = get_recc(att_df, cat_rating)
    with_url = filter_df(filename, user, low, high, province, att_df)

    final = dict()
    final['timeofday'] = []
    final['image'] = []
    final['name'] = []
    final['location'] = []
    final['price'] = []
    final['rating'] = []
    final['category'] = []
    total_days = end - start
    for i in range(1,total_days.days+2):
        for j in range(2):
            final['timeofday'].append('Morning')
        for j in range(2):
            final['timeofday'].append('Evening')

    for i in range(len(final['timeofday'])):
        if i%4 == 0:
            final = top_recc(with_url, final)
        else:
            final = find_closest(with_url, final['location'][-1],final['timeofday'][i], final)

    #print(final)
    t = final_output(days,final)
    return json.dumps(t)