#! /usr/bin/env python3

# (re)construit les fichiers README.md de description des challenges

import json
import glob
import os
import io
from collections import namedtuple
import yaml

# tuple
Slug = namedtuple('Slug', ['order',         # numéro pour maintenir l'ordre
                           'link',          # lien markdown vers le fichier local
                           'track',         # lien markdown vers la sous-section du site HackerRank
                           'domain',        # lien markdown vers le domaine sur www.hackerrank.com
                           'main_track',    # identifiant du domaine (pour retrouver la description)
                           'url'])          # url vers le site web hackerrank

# globals
models = {}             # liste des challenges indexés par (contest, slug)
descriptions = {}
playlists = {}

def get_models():
    """ charge les définitions des challenges """

    # les playlists
    for i in glob.iglob(os.path.join("offline", "playlists", "*.json")):
        with open(i, "r") as f:
            data = json.load(f)
            playlists[data['slug']] = data

    # les contests (y compris master_<domain>)
    order = 0
    for i in glob.iglob(os.path.join("offline", "contests", "*.json")):
        with open(i, "r") as f:
            data = json.load(f)

            # la description d'un contest
            if 'name' in data:
                desc = (data['description'] or '').partition('<br')[0]
                descriptions[data['slug']] = {'name': data['name'],
                                              'description': desc}

        # pour tous les challenges dans un contest
        for m in data['models']:
            if 'contest_slug' not in m:
                continue
            order += 1
            m['order'] = order        # ajoute un numéro pour maintenir l'ordre des chapters
            if m['contest_slug'] == 'projecteuler':
                m['order'] -= 10000         # met le ProjectEuler+ en tête des contests
            models[(m['contest_slug'], m['slug'])] = m

def do_domain(domain):

    slugs = {}

    #
    # STEP 1 : analyse un répertoire à la recherche des challenges (récursivement)
    #

    for i in glob.iglob(os.path.join(domain, "**/*"), recursive=True):

        # pas encore trouvé de solution élégante pour exclure les répertoires solution
        if "/js10-create-a-button/" in i or "/js10-buttons-container/" in i or '/js10-binary-calculator/' in i:  # noqa
            continue

        if os.path.isdir(i):
            # crée aussi les README.md dans chaque sous-domaine
            do_domain(i)

        if not os.path.isfile(i):
            continue

        name = os.path.basename(i)
        if name == 'CMakeLists.txt':
            continue
        if name == 'README.md':
            continue

        contest_challenge, lang = os.path.splitext(name)

        langs = {'.hs': 'Haskell',
                 '.erl': 'Erlang',
                 '.py': 'Python',
                 '.c': 'C',
                 '.cpp': 'C++',
                 '.sh': 'bash',
                 '.sql': 'SQL',
                 '.txt': 'text',
                 '.java': 'Java',
                 '.js': 'Javascript',
                 '.html': 'HTML',
                 '.pl': 'Perl'}

        lang = langs.get(lang)
        if not lang:
            # nominal: fichier à ignorer
            # print("LANG NOT FOUND:", name, os.path.splitext(name))
            continue

        contest = 'master'      # par défaut
        zz = os.path.split(os.path.dirname(i))
        if zz[0] == "contests":
            contest = zz[1]

        if (contest, contest_challenge) not in models:
            print("SLUG NOT FOUND:", name, contest_challenge, lang, i, domain)
            continue

        source = os.path.relpath(os.path.realpath(i), start=domain)

        if os.path.islink(source):
            print(source)
            exit()

        r = slugs.get((contest, contest_challenge))
        if r is None:
            m = models[(contest, contest_challenge)]
            m['onboarding'] = None

            if contest != "master":
                url = 'https://www.hackerrank.com/contests/{}/challenges/{}'.format(contest, contest_challenge)   # noqa
            else:
                url = 'https://www.hackerrank.com/challenges/{}'.format(contest_challenge)

            if zz[0] == "interview-preparation-kit":
                # print("--->", zz)
                title = "title"
                track = "track"
                main_track = "main_track"

                if zz[0] in playlists:

                    playlist = playlists[zz[0]]

                    chapter = None
                    for i, c in enumerate(playlist['playlists']):
                        if c['slug'] == zz[1]:
                            chapter = c
                            m['order'] = i + 100000000
                            break

                    title = "[{}]({})".format(
                                playlist['name'],
                                "https://www.hackerrank.com/interview/{}".format(zz[0]))

                    track = "[{}]({})".format(
                                chapter['name'],
                                "https://www.hackerrank.com/interview/{}/{}/challenges".format(zz[0], zz[1]))  # noqa

                    url = "https://www.hackerrank.com/challenges/{}/problem?h_l=playlist&slugs%5B%5D=interview&slugs%5B%5D={}&slugs%5B%5D={}".format(  # noqa
                            contest_challenge,
                            zz[0],
                            zz[1])

            elif m['track'] is not None:
                title = "[{}]({})".format(
                            m['track']['track_name'],
                            "https://www.hackerrank.com/domains/" + m['track']['track_slug'])

                track = "[{}]({}) > [{}]({})".format(
                            m['track']['track_name'],
                            "https://www.hackerrank.com/domains/" + m['track']['track_slug'],
                            m['track']['name'],
                            "https://www.hackerrank.com/domains/" +
                            m['track']['track_slug'] + "/" + m['track']['slug'])

                track = "[{}]({})".format(
                            m['track']['name'],
                            "https://www.hackerrank.com/domains/" +
                            m['track']['track_slug'] + "/" + m['track']['slug'])
                main_track = m['track']['track_slug']
            else:
                x = descriptions.get(m['contest_slug'])['name']
                title = "[{}]({})".format(x, "https://www.hackerrank.com/contests/" +
                                             m['contest_slug'])
                track = ""
                main_track = m['contest_slug']

            r = Slug(order=m['order'],
                     link=['[{}]({})'.format(lang, source)],
                     domain=title,
                     main_track=main_track,
                     track=track,
                     url=url)

            slugs[(contest, contest_challenge)] = r
        else:
            r.link.append('[{}]({})'.format(lang, source))

    order = [(v.order, contest_challenge) for contest_challenge, v in slugs.items()]
    order.sort()

    #
    # STEP 2 : crée l'index des challenges en respectant l'ordre
    #
    with io.StringIO() as out:

        if os.path.exists(os.path.join(domain, "README.md.in")):
            with open(os.path.join(domain, "README.md.in")) as f:
                out.write(f.read())

        prev_contest = None
        prev_domain = None
        prev_track = None

        for _, contest_challenge in order:

            m = models[contest_challenge]
            s = slugs[contest_challenge]

            if prev_domain != s.domain:
                prev_domain = s.domain
                print("", file=out)
                print("### " + prev_domain, file=out)
                if s.main_track in descriptions:
                    print(descriptions[s.main_track]['description'], file=out)
                print("", file=out)

            if prev_track != s.track or prev_contest != contest_challenge[0]:
                prev_contest = contest_challenge[0]
                prev_track = s.track
                if prev_track != "":
                    print("", file=out)
                    print("#### " + prev_track, file=out)
                print("", file=out)
                print("Name | Preview | Code | Difficulty", file=out)
                print("---- | ------- | ---- | ----------", file=out)

            links = ' '.join(sorted(s.link))

            preview = m['preview']
            if not preview:
                preview = m['name']
            preview = preview.replace("\n", " ").strip()

            print('[%s](%s)|%s|%s|%s' % (m['name'], s.url, preview, links,
                                         m['difficulty_name']), file=out)

        print("", file=out)

        md = out.getvalue()

    #
    # STEP 3 : met à jour le fichier README.md
    #
    fn = os.path.join(domain, "README.md")

    if len(md.strip()) == 0:
        if os.path.exists(fn):
            print("delete", fn)
            os.unlink(fn)
    elif not os.path.exists(fn) or md != open(fn, "rt").read():
        print("rewrite", fn)
        open(fn, "wt").write(md)

def main():
    domains = yaml.load(open(os.path.join(os.path.dirname(__file__), ".hr_conf.yaml")))["domains"]

    os.chdir(os.path.dirname(__file__))
    get_models()

    for domain in domains:
        do_domain(domain)

    do_domain("coding-dojo")

if __name__ == '__main__':
    main()


import streamlit as st
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

st.title("""
         EXPLORE DIFFERENT CLASSIFIERS
         """)
dataset_name=st.sidebar.selectbox("select Data Set",("Iris","Breast Cancer","Wine","Digits","DIABETES","BOSTON","LINNERUD"))
classifier_name=st.sidebar.selectbox("Select Classifier",("KNN","SVM","RANDOM FOREST","DECISION TREE","GAUSSION NB","MLP","ADABoost","QUADRATIC DISCRIMINANT"))

def get_dataset(dataset_name):
    if dataset_name=="Iris":
        data=datasets.load_iris()
    elif dataset_name=="Breast Cancer":
        data=datasets.load_breast_cancer()
    elif dataset_name=="Wine":
        data=datasets.load_wine()
    elif dataset_name:
        data=datasets.load_digits()
    x=data.data
    y=data.target
    return x,y
x,y=get_dataset(dataset_name)
st.write("SHAPE OF DATASET",x.shape)

def add_parameter(clf_name):
    p=dict()
    if clf_name=="KNN":
        K=st.sidebar.slider("K",1,15)
        p["K"]=K
    elif clf_name == "SVM":
        C = st.sidebar.slider("C", 0.01,10.00)
        p["C"] =C
    elif clf_name == "RANDOM FOREST":
        M_D= st.sidebar.slider("M_D",2,15)
        N_E=st.sidebar.slider("N_E",1,100)
        p["M_D"] =M_D
        p["N_E"]=N_E
    elif clf_name == "DECISION TREE":
        M_DD = st.sidebar.slider("M_DD", 2, 15)
        p["M_DD"] = M_DD
    elif clf_name == "MLP":
        p["A"] = 1
    return p

p=add_parameter(classifier_name)

def get_classifier(clf_name,p):
    if clf_name=="KNN":
        clf=KNeighborsClassifier(n_neighbors=p["K"])
    elif clf_name=="SVM":
        clf=SVC(C=p["C"])
    elif clf_name=="RANDOM FOREST":
        clf=RandomForestClassifier(max_depth=p["M_D"],n_estimators=p["N_E"],random_state=900)
    elif clf_name=="DECISION TREE":
        clf=DecisionTreeClassifier(max_depth=p["M_DD"])
    elif clf_name=="GAUSSION NB":
        clf=GaussianNB()
    elif clf_name=="MLP":
        clf=MLPClassifier(alpha=p["A"], max_iter=1000)
    elif clf_name=="ADABoost":
        clf=AdaBoostClassifier()
    elif clf_name == "QUADRATIC DISCRIMINANT":
        clf = QuadraticDiscriminantAnalysis()
    return clf

clf=get_classifier(classifier_name,p)

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.33,random_state=900)
clf.fit(x_train,y_train)
y_p = clf.predict(x_test)
a=accuracy_score(y_test,y_p)
st.write(f"CLASSIFIER={classifier_name}")
st.write(f"ACCURACY={a}")

pca=PCA(2)
x_project=pca.fit_transform(x)

x1=x_project[:,0]
x2=x_project[:,1]
fig=plt.figure()
plt.scatter(x1,x2,c=y,alpha=0.8,cmap="viridis")
plt.xlabel("principle component 1")
plt.ylabel("principle component 2")
plt.colorbar()

st.pyplot(bbox_inches='tight')
st.set_option('deprecation.showPyplotGlobalUse', False)


import main
import alg_cluster
import random
import matplotlib.pyplot as plt
import time

def get_random_clusters(num_clusters):
    result_list = []
    for num in range(num_clusters):
        result_list.append(alg_cluster.Cluster(set([num]), random.random()*2 - 1, random.random()*2 - 1,0,0))
    return result_list

x_data = []
y_data = []
x1_data = []
y1_data = []
for n in range(2,201):
    cluster_list = get_random_clusters(n)

    start_time = time.time()
    main.slow_closest_pair(cluster_list)
    stop_time = time.time()
    elapsed_time = stop_time - start_time
    x_data.append(n)
    y_data.append(elapsed_time)

    start_time = time.time()
    main.fast_closest_pair(cluster_list)
    stop_time = time.time()
    elapsed_time = stop_time - start_time
    x1_data.append(n)
    y1_data.append(elapsed_time)

plt.plot(x_data,y_data, "g-", label = "Slow Closest Pair")
plt.plot(x1_data,y1_data, "r-", label = "Fast Closest Pair")
plt.legend()
plt.title("Running Time Comparison of Closest Pair Algorithms")
plt.suptitle("Desktop Python (PyCharm)")
plt.xlabel("Number of Initial Clusters")
plt.ylabel("Running time (seconds)")
plt.savefig("performanceTime.png")
plt.show()


from django.urls import path
from .views import solicitar_turno, turnos_cliente, turnos_veterinario, VerTurnoVeterinario, ver_turno_cliente

urlpatterns = [
    path('solicitar_turno', solicitar_turno, name='solicitar_turno'),
    path('turnos_cliente', turnos_cliente, name='turnos_cliente'),  # No me gusta el nombre, después lo discutimos
    path('turnos_veterinario', turnos_veterinario, name='turnos_veterinario'),
    path('ver_turno_veterinario/<int:turno_id>', VerTurnoVeterinario.as_view(), name="ver_turno_veterinario"),
    path('ver_turno_cliente/<int:turno_id>', ver_turno_cliente, name='ver_turno_cliente')
]


import bpy
import os
import logging
from pathlib import Path

log = logging.getLogger(__name__)

# in future remove_prefix should be renamed to rename prefix and a target prefix should be specifiable via ui
def fixBones(remove_prefix=False, name_prefix="mixamorig:"):
    bpy.ops.object.mode_set(mode = 'OBJECT')

    if not bpy.ops.object:
        log.warning('[Mixamo Root] Could not find amature object, please select the armature')

    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    bpy.context.object.show_in_front = True

    if remove_prefix:
        for rig in bpy.context.selected_objects:
            if rig.type == 'ARMATURE':
                for mesh in rig.children:
                    for vg in mesh.vertex_groups:
                        new_name = vg.name
                        new_name = new_name.replace(name_prefix,"")
                        rig.pose.bones[vg.name].name = new_name
                        vg.name = new_name
                for bone in rig.pose.bones:
                    bone.name = bone.name.replace(name_prefix,"")
        for action in bpy.data.actions:
            fc = action.fcurves
            for f in fc:
                f.data_path = f.data_path.replace(name_prefix,"")

def scaleAll():
    bpy.ops.object.mode_set(mode='OBJECT')

    prev_context=bpy.context.area.type

    bpy.ops.object.mode_set(mode='POSE')
    bpy.ops.pose.select_all(action='SELECT')
    bpy.context.area.type = 'GRAPH_EDITOR'
    bpy.context.space_data.dopesheet.filter_text = "Location"
    bpy.context.space_data.pivot_point = 'CURSOR'
    bpy.context.space_data.dopesheet.use_filter_invert = False

    bpy.ops.anim.channels_select_all(action='SELECT')

    bpy.ops.transform.resize(value=(1, 0.01, 1), orient_type='GLOBAL',
    orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
    orient_matrix_type='GLOBAL',
    constraint_axis=(False, True, False),
    mirror=True, use_proportional_edit=False,
    proportional_edit_falloff='SMOOTH',
    proportional_size=1,
    use_proportional_connected=False,
    use_proportional_projected=False)

def copyHips(root_bone_name="Root", hip_bone_name="mixamorig:Hips", name_prefix="mixamorig:"):
    bpy.context.area.ui_type = 'FCURVES'
    #SELECT OUR ROOT MOTION BONE
    bpy.ops.pose.select_all(action='DESELECT')
    bpy.context.object.pose.bones[name_prefix + root_bone_name].bone.select = True
    # SET FRAME TO ZERO
    bpy.ops.graph.cursor_set(frame=0.0, value=0.0)
    #ADD NEW KEYFRAME
    bpy.ops.anim.keyframe_insert_menu(type='Location')
    #SELECT ONLY HIPS AND LOCTAIUON GRAPH DATA
    bpy.ops.pose.select_all(action='DESELECT')
    bpy.context.object.pose.bones[hip_bone_name].bone.select = True
    bpy.context.area.ui_type = 'DOPESHEET'
    bpy.context.space_data.dopesheet.filter_text = "Location"
    bpy.context.area.ui_type = 'FCURVES'
    #COPY THE LOCATION VALUES OF THE HIPS AND DELETE THEM
    bpy.ops.graph.copy()
    bpy.ops.graph.select_all(action='DESELECT')

    myFcurves = bpy.context.object.animation_data.action.fcurves

    for i in myFcurves:
        hip_bone_fcvurve = 'pose.bones["'+hip_bone_name+'"].location'
        if str(i.data_path)==hip_bone_fcvurve:
            myFcurves.remove(i)

    bpy.ops.pose.select_all(action='DESELECT')
    bpy.context.object.pose.bones[name_prefix + root_bone_name].bone.select = True
    bpy.ops.graph.paste()

    bpy.context.area.ui_type = 'VIEW_3D'
    bpy.ops.object.mode_set(mode='OBJECT')

def fix_bones_nla(remove_prefix=False, name_prefix="mixamorig:"):
    bpy.ops.object.mode_set(mode = 'OBJECT')

    if not bpy.ops.object:
        log.warning('[Mixamo Root] Could not find amature object, please select the armature')

    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    bpy.context.object.show_in_front = True

def scale_all_nla(armature):
    bpy.ops.object.mode_set(mode='OBJECT')

    # prev_context=bpy.context.area.type

    for track in [x for x in armature.animation_data.nla_tracks]:
        bpy.context.active_nla_track = track
        for strip in track.strips:
            bpy.context.active_nla_strip = strip
            print(bpy.context.active_nla_strip)

    bpy.ops.object.mode_set(mode='POSE')
    bpy.ops.pose.select_all(action='SELECT')
    bpy.context.area.type = 'GRAPH_EDITOR'
    bpy.context.space_data.dopesheet.filter_text = "Location"
    bpy.context.space_data.pivot_point = 'CURSOR'
    bpy.context.space_data.dopesheet.use_filter_invert = False

    bpy.ops.anim.channels_select_all(action='SELECT')

    bpy.ops.transform.resize(value=(1, 0.01, 1), orient_type='GLOBAL',
    orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
    orient_matrix_type='GLOBAL',
    constraint_axis=(False, True, False),
    mirror=True, use_proportional_edit=False,
    proportional_edit_falloff='SMOOTH',
    proportional_size=1,
    use_proportional_connected=False,
    use_proportional_projected=False)

def copy_hips_nla(root_bone_name="Root", hip_bone_name="mixamorig:Hips", name_prefix="mixamorig:"):
    hip_bone_name="Ctrl_Hips"
    bpy.ops.object.mode_set(mode='POSE')
    previous_context = bpy.context.area.ui_type
    bpy.ops.pose.select_all(action='DESELECT')
    while False:
        #SELECT OUR ROOT MOTION BONE
        # bpy.context.object.pose.bones[name_prefix + root_bone_name].bone.select = True

        # bpy.ops.nla.tweakmode_enter()
        # bpy.context.area.ui_type = 'FCURVES'

        # # SET FRAME TO ZERO
        # bpy.ops.graph.cursor_set(frame=0.0, value=0.0)
        # #ADD NEW KEYFRAME
        # bpy.ops.anim.keyframe_insert_menu(type='Location')
        # #SELECT ONLY HIPS AND LOCTAIUON GRAPH DATA
        # bpy.ops.pose.select_all(action='DESELECT')
        # bpy.context.object.pose.bones[hip_bone_name].bone.select = True
        # bpy.context.area.ui_type = 'DOPESHEET'
        # bpy.context.space_data.dopesheet.filter_text = "Location"
        # bpy.context.area.ui_type = 'FCURVES'
        # #COPY THE LOCATION VALUES OF THE HIPS AND DELETE THEM
        # bpy.ops.graph.copy()
        # bpy.ops.graph.select_all(action='DESELECT')

        # myFcurves = bpy.context.object.animation_data.action.fcurves

        # for i in myFcurves:
        #     hip_bone_fcvurve = 'pose.bones["'+hip_bone_name+'"].location'
        #     if str(i.data_path)==hip_bone_fcvurve:
        #         myFcurves.remove(i)

        # bpy.ops.pose.select_all(action='DESELECT')
        # bpy.context.object.pose.bones[name_prefix + root_bone_name].bone.select = True
        # bpy.ops.graph.paste()

        # for animation data in object
        # for
        pass

    for track in bpy.context.object.animation_data.nla_tracks:
        bpy.context.object.animation_data.nla_tracks.active = track
        for strip in track.strips:
            bpy.context.object.pose.bones[name_prefix + root_bone_name].bone.select = True
            bpy.context.area.ui_type = 'NLA_EDITOR'
            bpy.ops.nla.tweakmode_enter()
            bpy.context.area.ui_type = 'FCURVES'
            hip_curves = [fc for fc in strip.fcurves if hip_bone_name in fc.data_path and fc.data_path.startswith('location')]

            # Copy Hips to root
            ## Insert keyframe for root bone
            start_frame = strip.action.frame_range[0]
            # frame sets the x axis cursor (determines the frame, and value the y axis cursor, which is the amplitude of the curve)
            bpy.ops.graph.cursor_set(frame=start_frame, value=0.0)
            bpy.ops.anim.keyframe_insert_menu(type='Location')
            bpy.ops.pose.select_all(action='DESELECT')

            ## Copy Location fcruves
            bpy.context.object.pose.bones[hip_bone_name].bone.select = True
            bpy.context.area.ui_type = 'DOPESHEET'
            bpy.context.space_data.dopesheet.filter_text = "Location"
            bpy.context.area.ui_type = 'FCURVES'
            bpy.ops.graph.copy()
            bpy.ops.graph.select_all(action='DESELECT')

            ## We want to delete the hips locations
            allFcurves = strip.fcurves
            for fc in hip_curves:
                allFcurves.remove(fc)

            ## Paste location fcurves to the root bone
            bpy.ops.pose.select_all(action='DESELECT')
            bpy.context.object.pose.bones[name_prefix + root_bone_name].bone.select = True
            bpy.ops.graph.paste()

            loc_fcurves = [fc for fc in strip.fcurves if root_bone_name in fc.data_path and fc.data_path.startswith('location')]

            # Update Root Bone
            # set z of root to min 0 (not negative).
            for fc in loc_fcurves:
                # Z axis location curve
                if fc.array_index == 2:
                    for kp in fc.keyframe_points:
                        kp.co.z = min(0, abs(kp.co.z))

            # Delete rotation curves for x(0) and y(1) axis. Should we delet Z rotation too?
            # rot_fcurves = [fc for fc in strip.fcurves if root_bone_name in fc.data_path and fc.data_path.startswith('rotation') and (fc.array_index == 0 or fc.array_index == 1)]
            # for fc in rot_fcurves:
            #     strip.fcurves.remove(fc)
            # while(rot_fcurves):
            #     fc = rot_fcurves.pop()
            #     strip.fcurves.remove(fc)
            bpy.context.area.ui_type = 'NLA_EDITOR'
            bpy.ops.nla.tweakmode_exit()
            bpy.context.area.ui_type = previous_context
    bpy.ops.object.mode_set(mode='OBJECT')

def deleteArmature(imported_objects=set()):
    armature = None
    if bpy.context.selected_objects:
        armature = bpy.context.selected_objects[0]
    if imported_objects == set():
        log.warning("[Mixamo Root] No armature imported, nothing to delete")
    else:
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.object.select_all(action='DESELECT')
        for obj in imported_objects:
            bpy.data.objects[obj.name].select_set(True)

    bpy.ops.object.delete(use_global=False, confirm=False)
    if bpy.context.selected_objects:
        bpy.context.view_layer.objects.active = armature

def import_armature(filepath, root_bone_name="Root", hip_bone_name="mixamorig:Hips", remove_prefix=False, name_prefix="mixamorig:",  insert_root=False, delete_armatures=False):
    old_objs = set(bpy.context.scene.objects)
    if insert_root:
        bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
        bpy.ops.import_scene.fbx(filepath = filepath)#,  automatic_bone_orientation=True)
    else:
        bpy.ops.import_scene.fbx(filepath = filepath)#,  automatic_bone_orientation=True)

    imported_objects = set(bpy.context.scene.objects) - old_objs
    imported_actions = [x.animation_data.action for x in imported_objects if x.animation_data]
    print("[Mixamo Root] Now importing: " + str(filepath))
    imported_actions[0].name = Path(filepath).resolve().stem # Only reads the first animation associated with an imported armature

    if insert_root:
        add_root_bone(root_bone_name, hip_bone_name, remove_prefix, name_prefix)

def add_root_bone(root_bone_name="Root", hip_bone_name="mixamorig:Hips", remove_prefix=False, name_prefix="mixamorig:"):
    armature = bpy.context.selected_objects[0]
    bpy.ops.object.mode_set(mode='EDIT')

    root_bone = armature.data.edit_bones.new(name_prefix + root_bone_name)
    root_bone.tail.y = 30

    armature.data.edit_bones[hip_bone_name].parent = armature.data.edit_bones[name_prefix + root_bone_name]
    bpy.ops.object.mode_set(mode='OBJECT')

    fixBones(remove_prefix=remove_prefix, name_prefix=name_prefix)
    scaleAll()
    copyHips(root_bone_name=root_bone_name, hip_bone_name=hip_bone_name, name_prefix=name_prefix)

def add_root_bone_nla(root_bone_name="Root", hip_bone_name="mixamorig:Hips", name_prefix="mixamorig:"):#remove_prefix=False, name_prefix="mixamorig:"):
    armature = bpy.context.selected_objects[0]
    bpy.ops.object.mode_set(mode='EDIT')

    # Add root bone to edit bones
    root_bone = armature.data.edit_bones.new(name_prefix + root_bone_name)
    root_bone.tail.z = .25

    armature.data.edit_bones[hip_bone_name].parent = armature.data.edit_bones[name_prefix + root_bone_name]
    bpy.ops.object.mode_set(mode='OBJECT')

    # fix_bones_nla(remove_prefix=remove_prefix, name_prefix=name_prefix)
    # scale_all_nla()
    copy_hips_nla(root_bone_name=root_bone_name, hip_bone_name=hip_bone_name, name_prefix=name_prefix)

def push(obj, action, track_name=None, start_frame=0):
    # Simulate push :
    # * add a track
    # * add an action on track
    # * lock & mute the track
    # * remove active action from object
    tracks = obj.animation_data.nla_tracks
    new_track = tracks.new(prev=None)
    if track_name:
        new_track.name = track_name
    strip = new_track.strips.new(action.name, start_frame, action)
    obj.animation_data.action = None

def get_all_anims(source_dir, root_bone_name="Root", hip_bone_name="mixamorig:Hips", remove_prefix=False, name_prefix="mixamorig:",  insert_root=False, delete_armatures=False):
    files = os.listdir(source_dir)
    num_files = len(files)
    current_context = bpy.context.area.ui_type
    old_objs = set(bpy.context.scene.objects)

    for file in files:
        print("file: " + str(file))
        try:
            filepath = source_dir+"/"+file
            import_armature(filepath, root_bone_name, hip_bone_name, remove_prefix, name_prefix, insert_root, delete_armatures)
            imported_objects = set(bpy.context.scene.objects) - old_objs
            if delete_armatures and num_files > 1:
                deleteArmature(imported_objects)
                num_files -= 1

        except Exception as e:
            log.error("[Mixamo Root] ERROR get_all_anims raised %s when processing %s" % (str(e), file))
            return -1
    bpy.context.area.ui_type = current_context
    bpy.context.scene.frame_start = 0
    bpy.ops.object.mode_set(mode='OBJECT')

def apply_all_anims(delete_applied_armatures=False, control_rig=None, push_nla=False):
    if control_rig and control_rig.type == 'ARMATURE':
        bpy.ops.object.mode_set(mode='OBJECT')

        imported_objects = set(bpy.context.scene.objects)
        imported_armatures = [x for x in imported_objects if x.type == 'ARMATURE' and x.name != control_rig.name]

        for obj in imported_armatures:
            action_name = obj.animation_data.action.name
            bpy.context.scene.mix_source_armature = obj
            bpy.context.view_layer.objects.active = control_rig

            bpy.ops.mr.import_anim_to_rig()

            bpy.context.view_layer.objects.active = control_rig
            selected_action = control_rig.animation_data.action
            selected_action.name = 'ctrl_' + action_name
            # created_actions.append(selected_action)

            if push_nla:
                push(control_rig, selected_action, None, int(selected_action.frame_start))

            if delete_applied_armatures:
                bpy.context.view_layer.objects.active = control_rig
                deleteArmature(set([obj]))

if __name__ == "__main__":
    dir_path = "" # If using script in place please set this before running.
    get_all_anims(dir_path)
    print("[Mixamo Root] Run as plugin, or copy script in text editor while setting parameter defaults.")


from __future__ import print_function
import argparse
import codecs
import numpy as np
import json
import requests

"""
This file is part of the computer assignments for the course DD1418/DD2418 Language engineering at KTH.
Created 2017 by Johan Boye and Patrik Jonell.
"""

"""
This module computes the minimum-cost alignment of two strings.
"""

"""
When printing the results, only print BREAKOFF characters per line.
"""
BREAKOFF = 60

def compute_backpointers(s0, s1):
    """

Computes and returns the backpointer array (see Jurafsky and Martin, Fig 3.27)
    arising from the calculation of the minimal edit distance of two strings
    s0 and s1.

The backpointer array has three dimensions. The first two are the row and
    column indices of the table in Fig 3.27. The third dimension either has
    the value 0 (in which case the value is the row index of the cell the backpointer
    is pointing to), or the value 1 (the value is the column index). For example, if
    the backpointer from cell (5,5) is to cell (5,4), then
    backptr[5][5][0]=5 and backptr[5][5][1]=4.

    :param s0: The first string.
    :param s1: The second string.
    :return: The backpointer array.
    """
    if s0 == None or s1 == None:
        raise Exception('Both s0 and s1 have to be set')

    backptr = [[[0, 0] for y in range(len(s1)+1)] for x in range(len(s0)+1)]

    # YOUR CODE HERE
    D = [[[0] for y in range(len(s1)+1)] for x in range(len(s0)+1)] # Distance matrix

    # Basic setup
    # First row, all columns
    for k in range(len(s1) + 1):
        D[0][k][0] = k
        if k == 0:
            continue
        backptr[0][k][0], backptr[0][k][1] = 0, k-1
    # First column, all rows
    for k in range(len(s0) + 1):
        D[k][0][0] = k
        if k == 0:
            continue
        backptr[k][0][0], backptr[k][0][1] = k-1, 0

    # Loop through both strings (inner part of matrix, i.e. excluding first column and first row)
    for i in range(1, len(s0)+1):
        for j in range(1, len(s1)+1):
            left_cost = D[i][j-1][0] + 1 # Cost to come from the left
            below_cost = D[i-1][j][0] + 1 # Cost to come from below
            # Cost to come from diagonally behind
            diag_cost = D[i-1][j-1][0]
            if (s0[i-1] != s1[j-1]):
                diag_cost += 2

            # Check which cost is cheapest (preference for diag_cost)
            if ((left_cost < below_cost) and (left_cost < diag_cost)):
                # We should come from the left in our backtrace matrix
                D[i][j][0] = left_cost
                backptr[i][j][0], backptr[i][j][1] = i, j-1
            elif ((below_cost <= left_cost) and (below_cost < diag_cost)):
                # We should come from below
                D[i][j][0] = below_cost
                backptr[i][j][0], backptr[i][j][1] = i-1, j
            elif ((diag_cost <= left_cost) and (diag_cost <= below_cost)):
                # We should come from diagonally behind
                D[i][j][0] = diag_cost
                backptr[i][j][0], backptr[i][j][1] = i-1, j-1

    return backptr

def subst_cost(c0, c1):
    """
    The cost of a substitution is 2 if the characters are different
    or 0 otherwise (when, in fact, there is no substitution).
    """
    return 0 if c0 == c1 else 2

def align(s0, s1, backptr):
    """

Finds the best alignment of two different strings s0
    and s1, given an array of backpointers.

The alignment is made by padding the input strings with spaces. If, for
    instance, the strings are around and rounded,
    then the padded strings should be around   and
     rounded.

    :param s0: The first string.
    :param s1: The second string.
    :param backptr: A three-dimensional matrix of backpointers, as returned by
    the diff method above.
    :return: An array containing exactly two strings. The first string (index 0
    in the array) contains the string s0 padded with spaces
    as described above, the second string (index 1 in the array) contains
    the string s1 padded with spaces.
    """
    result = ['', '']

    # YOUR CODE HERE
    i0 = len(s0)
    j0 = len(s1)
    # (i0, j0) gives us top right corner of backptr matrix

    while True:
        i, j = backptr[i0][j0][0], backptr[i0][j0][1]

        if ((i0 - 1 == i) and (j0 - 1 == j)):
            # Then, this is a diagonal move.
            result[0] += s0[i0 - 1]
            result[1] += s1[j0 - 1]
        elif (j0 - 1 == j):
            # This is a move from the left
            result[0] += ' '
            result[1] += s1[j0 - 1]
        elif (i0 - 1 == i):
            # This is a move from below
            result[0] += s0[i0 - 1]
            result[1] += ' '

        i0, j0 = i, j

        if ((i0 == 0) and (j0 == 0)):
            # We have reached index (0, 0) in the backptr matrix.
            break

    #print(backptr)

    return result

def print_alignment(s):
    """

Prints two aligned strings (= strings padded with spaces).
    Note that this printing method assumes that the padded strings
    are in the reverse order, compared to the original strings
    (because we are following backpointers from the end of the
    original strings).

    :param s: An array of two equally long strings, representing
    the alignment of the two original strings.
    """
    if s[0] == None or s[1] == None:
        return None
    start_index = len(s[0]) - 1
    while start_index > 0:
        end_index = max(0, start_index - BREAKOFF + 1)
        print_list = ['', '', '']
        for i in range(start_index, end_index-1 , -1):
            print_list[0] += s[0][i]
            print_list[1] += '|' if s[0][i] == s[1][i] else ' '
            print_list[2] += s[1][i]

        for x in print_list: print(x)
        start_index -= BREAKOFF

def main():
    """
    Parse command line arguments
    """
    parser = argparse.ArgumentParser(description='Aligner')
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('--file', '-f', type=str, nargs=2, help='align two strings')
    group.add_argument('--string', '-s', type=str, nargs=2, help='align the contents of two files')

    parser.add_argument('--check', action='store_true', help='check if your alignment is correct')

    arguments = parser.parse_args()

    if arguments.file:
        f1, f2 = arguments.file
        with codecs.open(f1, 'r', 'utf-8') as f:
            s1 = f.read().replace('\n', '')
        with codecs.open(f2, 'r', 'utf-8') as f:
            s2 = f.read().replace('\n', '')

    elif arguments.string:
        s1, s2 = arguments.string

    if arguments.check:
        payload = json.dumps({
            's1': s1,
            's2': s2,
            'result': align(s1, s2, compute_backpointers(s1, s2))
        })
        response = requests.post(
            'https://language-engineering.herokuapp.com/correct',
            data=payload,
            headers={'content-type': 'application/json'}
        )
        response_data = response.json()
        if response_data['correct']:
            print_alignment( align(s1, s2, compute_backpointers(s1, s2)))
            print('Success! Your results are correct')
        else:
            print('Your results:\n')
            print_alignment( align(s1, s2, compute_backpointers(s1, s2)))
            print("The server's results\n")
            print_alignment(response_data['result'])
            print("Your results differ from the server's results")
    else:
        print_alignment( align(s1, s2, compute_backpointers(s1, s2)))

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -----------------------
# Supplementary Material for Deith and Brodie 2020; “Predicting defaunation – accurately mapping bushmeat hunting pressure over large areas”
#    doi: 10.1098/rspb.2019-2677
#------------------------
# Code to iterate through GFLOW results files, modify the outputs based on
#     human population density, and then sum this into a final cumulative
#     accessibility map.
#
# This script uses maps created with 'MSYBorneo_ResistanceMapLayers_Preparation.py'
#   and GFLOW.
# Created by Mairin Deith on Nov12 2017
# Last edited on Nov15 2019
#------------------------

# Import libraries
import os, sys
import subprocess
import gdal
import re
import glob
import rasterio as rio
import numpy as np
from pathlib import Path
from datetime import datetime
from collections import defaultdict

### HELPER FUNCTIONS
# From https://stackoverflow.com/questions/5419204/index-of-duplicates-items-in-a-python-list
# This function passes through a list and keeps a list of locations seen for each item,
#   and returns items seen more than once

def list_duplicates(seq):
    tally = defaultdict(list)
    for i,item in enumerate(seq):
        tally[item].append(i)
    return ((key,locs) for key,locs in tally.items()
                            if len(locs)>1)

def list_singles(seq):
    tally = defaultdict(list)
    for i,item in enumerate(seq):
        tally[item].append(i)
    return ((key,locs) for key,locs in tally.items()
                            if len(locs)==1)

### GLOBAL INFO
# File paths/gdalcalc paths

basedir = os.path.abspath('/home/mairin/Documents/GradSchool/Research/CircuitTheory_Borneo/Revised_CTMapCreation/')
basefname = 'ClusterOutput_Summation'

walk_types = ['AllNodes','VillageSabah','VillageSabahOther'] # types of nodes to identify from gazetteers

for w in walk_types:
    if w == 'AllNodes':
        rivers = ['impassableRivers', 'passableRivers'] #, 'passableRivers'] # uncomment after untarred
    else:
        rivers = ['impassableRivers']
    for r in rivers:
        print(w+", " + r)
        src_dir = os.path.join(basedir, 'SourceSinks_WalkingOnly_'+w, r,'NodesTSV')
        out_dir = os.path.join(basedir, 'GFlowOutputs','ClusterFlexOutputs', 'SepTmpOutputs_Walking'+w, r)
        asc_dir = out_dir
        output_fname = basefname + '_Walking' + w + '_' + r + '.asc'
        cum_outmap = os.path.join(asc_dir, output_fname)
        if os.path.exists(cum_outmap): # Don't bother if the file doesn't exist
            exists = True
            delete = str(input("\n\nShould the existing map (%s) be deleted?\n'Y' for yes, 'N' for no > " %(os.path.basename(cum_outmap))))
            delete = delete.upper()
            while delete!='N' and delete!='Y':
                delete = str(input("Sorry, I was expecting either 'Y' or 'N'. \nShould the existing map %s be deleted?\n'Y' for yes, 'N' for no >" %(os.path.basename(cum_outmap))))
                delete = delete.upper()
            if delete=='Y':
                os.remove(cum_outmap)
                exists = False
            if delete=='N':
                exists = True
        else:
            exists = False
        ### WORK ON FILES TO GENERATE CALCULATION INFORMATION
        asc_list = glob.glob(asc_dir+"/output_temp_*")
        # Untar the files if they have not already been extracted
        if len(asc_list)==0:
            tar_file = glob.glob(asc_dir+"/*.tar.gz")
            print(len(tar_file))
            if len(tar_file) == 0:
                print("!!! \n No .tar or .asc files in %s, moving on...\n!!!" %(asc_dir))
                continue
            if len(tar_file) == 1:
                print("...Untarring " + tar_file[0])
                with open(os.devnull, 'wb') as devnull:
                    subprocess.check_call(['tar','-C',asc_dir,'-zxvf', tar_file[0]], stdout=devnull, stderr=subprocess.STDOUT)
#                os.system('tar -C ' + asc_dir + ' -zxvf ' + asc_dir + '/*.tar.gz')
                asc_list = glob.glob(asc_dir+"/output_temp_*")
            if len(tar_file) > 1:
                print("!!!\nMore than one .tar file in %s, moving on...     \n!!!" %(asc_dir))
                continue
        # Modify file names to isolate just numbers
        iterpop = [a.replace('output_temp_', '').replace('_nodes', '').replace('.asc','') for a in asc_list]
        # Separate the number of iterations from population size (for lookups)
        niter = []
        p = []
        p_mult = []
        p_mult_tmp = []
        for a in iterpop:
            ntmp, ptmp = re.split("_", os.path.basename(a))[0:2]
            niter.append(int(ntmp))
            p.append(float(ptmp))
            multtmp = np.log10(float(ptmp)/78.54)*-0.054357+0.179789
            if multtmp < 0:
                multtmp = 0.0001
            p_mult_tmp.append(multtmp)
            p_mult.append(multtmp*float(ptmp))
        # Calculates the expected number of hunters based on mini lit review
        #   Each population density was calculated within a 5km buffer; so divide
        #   by 78.54 (approximate area of a 5km-radius circle) for density per km2
        # Find singles/duplicates:
        single_src = sorted(list_singles(p))
        dup_src = sorted(list_duplicates(p))
        ### CREATE EMPTY MAP OR READ EXISTING
        # Use same dimensions/transformation as asc_list[0]
        if exists == False:
            with rio.open(os.path.join(asc_dir,asc_list[0]), 'r') as ds:
                tmp_meta = ds.profile
                tmp_shape = ds.shape  # read in height and width
            intmap = np.empty(tmp_shape, dtype=np.float32)
            print("Creating blank output map: %s" %(os.path.basename(cum_outmap)))
            with rio.open(cum_outmap, 'w', **tmp_meta) as ds:
                ds.write_band(1, intmap)
        elif exists == True:
            print("Reading existing cumulative map %s" %(os.path.basename(cum_outmap)))
            with rio.open(cum_outmap, 'r') as ds:
                intmap = ds.read()
            with rio.open(os.path.join(asc_dir,asc_list[0]), 'r') as ds:
                tmp_meta = ds.profile
        ### BEGIN CALCULATIONS
        print("Calculating single-source file maps...")
        save_counter = 0 # save every 5 steps
        singles_counter = 0
        total = len(single_src)
        for s in single_src:
            singles_counter += 1
            print("\n...Processing map %s of %s...\n" %(singles_counter, total))
            ntmp = niter[int(s[1][0])]
            pmtmp = p_mult[int(s[1][0])]
            # Find corresponding source TSV file:
            src_f = glob.glob(src_dir+"/*%s*" %(str(ptmp)+"_"))[0]
            with open(src_f) as file:
                head = [next(file) for x in range(1)]
            # Number of sources is just the first # of the second column minus 1
            nsource = int(re.split("\t", head[0])[1].replace("\n", ""))-1
            asc_tmp = os.path.join(asc_dir, asc_list[int(s[1][0])])
            print("......Opening file: %s" %(os.path.basename(asc_tmp)))
            with rio.open(asc_tmp, 'r') as ds:
                tmp = ds.read()
            tmp[tmp == -9999] = 0
            intmap = intmap + ((tmp * nsource * pmtmp) / ntmp)
            Path(os.path.join(asc_dir, "Processed", os.path.basename(asc_tmp[0]))).touch()
            save_counter += 1
            if save_counter == 10:
                with rio.open(cum_outmap, 'w', **tmp_meta) as ds:
                    ds.write(intmap)
                save_counter = 0
            with open(os.devnull, 'wb') as devnull:
                    subprocess.check_call(['rm',asc_tmp],stdout=devnull, stderr=subprocess.STDOUT)
        if total!=0:
            with rio.open(cum_outmap, 'w', **tmp_meta) as ds:
                ds.write(intmap)
        total = len(dup_src)
        dup_counter = 0
        for d in dup_src:
            dup_counter += 1
            print("\n...Processing map %s of %s...\n" %(dup_counter, total))
            # Population at 0th index, location at 1st
            ptmp = d[0]
            pmtmp = p_mult[int(d[1][0])]
            for f in range(len(d[1])):
                ntmp = niter[int(d[1][f])]
                src_f = glob.glob(src_dir+"/*%s*" %(str(ptmp)+"_"))[f]
                with open(src_f) as file:
                    head = [next(file) for x in range(1)]
                nsource = int(re.split("\t", head[0])[1].replace("\n", ""))-1
                asc_tmp = os.path.join(asc_dir, asc_list[int(d[1][f])])
                print("......Opening file: %s" %(os.path.basename(asc_tmp)))
                with rio.open(asc_tmp, 'r') as ds:
                    tmp = ds.read()
                tmp[tmp == -9999] = 0
                intmap = intmap + ((tmp * nsource * pmtmp) / ntmp)
                Path(os.path.join(asc_dir, "Processed", os.path.basename(asc_tmp[0]))).touch()
                save_counter += 1
            if save_counter >= 10:
                with rio.open(cum_outmap, 'w', **tmp_meta) as ds:
                    ds.write(intmap)
                save_counter = 0
            with open(os.devnull, 'wb') as devnull:
                    subprocess.check_call(['rm',asc_tmp],stdout=devnull, stderr=subprocess.STDOUT)
        with rio.open(cum_outmap, 'w', **tmp_meta) as ds:
            ds.write(intmap)


## The wext merged datafile
import sys
input_file = sys.argv[1]
data_file = sys.argv[2]
output_file = sys.argv[3]
cutoff = float(sys.argv[4])
#cutoff = 5

import pandas as pd
from sklearn.metrics import precision_recall_curve
from random import random
import math
from scipy.stats import chi2
import numpy as np

import logging
#filename =
logging.basicConfig(filename="log/"+ sys.argv[0].split('/')[0] + ".log", level=logging.INFO)

#logging.getLogger().setLevel('DEBUG')

logging.debug("Data file: "+data_file)
logging.debug("Output file: "+output_file)
logging.debug("cuttoff: "+str(cutoff))

data = pd.read_csv(data_file, delimiter='\t')

logging.debug(data.columns)
data = data[(data['Index1'] != "Index1")] # get rid of extra headers from cat
data['Distance'] = data['Distance'].apply(int)
data['P-Value'] = data['P-Value'].apply(float)
data = data[data['Distance'] > 0]
data = data.drop_duplicates()

logging.debug("Imported data. DF size:"+str(data.shape))

data_sorted = data.fillna(1).replace(float('-inf'), 1).sort_values(by = 'P-Value')
data_sorted = data_sorted[data_sorted['P-Value'] > 0]
data_sorted['True'] = data_sorted.apply(lambda x: x['Gene1'][-1] == x['Gene2'][-1], axis=1)
data_sorted['Pair'] = data_sorted.apply(lambda x: x['Gene1'][:-1] + ':' +  x['Gene2'][:-1], axis = 1)
data_sorted['Index1'] = data_sorted['Index1'].apply(int)
data_sorted['Index2'] = data_sorted['Index2'].apply(int)
data_sorted['log p'] = data_sorted['P-Value'].apply(math.log)

logging.debug("Sorted data. DF size:"+str(data_sorted.shape))

def p_value(x):
    k = 4
    v = chi2.logsf(-2*x, k)
    return v

def is_correct(x):
    if x != 0:
        return x < 0
    else:
        return random() < 0.5

diff = data_sorted
diff = diff[['Pair', 'Distance',  'log p', 'True']].groupby(['Pair', 'Distance', 'True']).sum()
diff.reset_index(inplace=True)
diff['log p joint'] = diff['log p'].apply(p_value)
diff['log p sum'] = diff.apply(lambda x: x['log p joint'] if x['True'] else -x['log p joint'], axis=1)

logging.debug("Created diff. Diff size:" + str(diff.shape))

div = diff[['Pair', 'Distance', 'log p sum']].groupby(by = ['Pair','Distance']).sum()
div = div[div['log p sum'] != 0]
div['log p abs'] = div['log p sum'].apply(abs)
div['correct'] = div['log p sum'].apply(lambda x: x < 0)
div['correct2'] = div['log p sum'].apply(is_correct)
div.reset_index(inplace=True)

logging.debug("Created div. Div size:" + str(div.shape))
logging.debug("Score span: " + str(div['log p sum'].max()) +"\t"+ str(div['log p sum'].min()))

import vcf

pos_index_map = {}

data = pd.read_csv(input_file)

pos_index_map = {}
def make_pos_index_map(x):
    pos_index_map[x['loc']] = x.name + 1
data.apply(make_pos_index_map, axis=1)

def write_fragment(cutoff, output_file):
    with open(output_file,'w') as out:
        dclip = div[div['log p abs'] > cutoff]
        #dclip = div
        for i in range(len(dclip)):
            d = dclip.iloc[i]
            v1 = min(map(int, d['Pair'].split(':')))
            v2 = max(map(int, d['Pair'].split(':')))

            i1 = pos_index_map[v1]
            i2 = pos_index_map[v2]
            a1 = "0"
            a2 = "0"

            if d['log p sum'] < 0:
                if random() > 0.5: allele = "00"
                else: allele = "11"
            else:
                if random() > 0.5: allele = "10"
                else: allele = "01"

            if abs(i1 - i2) == 1:
                q = '++'
                line = "1 {0} {1} {2} {3}\n".format(d['Pair'], i1, allele, q)
            else:
                q = '++'
                line = "2 {0} {1} {2} {3} {4} {5}\n".format(d['Pair'], i1, allele[0], i2, allele[1], q)
            out.write(line)

write_fragment(cutoff, output_file)


import numpy as np
from sympy import symbols, pi, sin, cos, atan2, sqrt, simplify
from sympy.matrices import Matrix
import tf

"""
Test file for building the Kuka 6 DoF manipulator's forward and inverse
kinematic code.

FK(thetas) -> pose
IK(pose) -> thetas
"""

def build_mod_dh_matrix(s, theta, alpha, d, a):
    """Build the modified DH transformation matrix based on the provided
    theta, alpha, d and a values.

    :param s: Dictionary of DH parameters for the manipulator
    :param theta: Sympy symbol
    :param alpha: Sympy symbol
    :param d: Sympy symbol
    :param a: Sympy symbol
    :return: Sympy Matrix object of the DH transformation matrix
    """
    # Create the transformation matrix template
    Ta_b = Matrix([[            cos(theta),           -sin(theta),           0,             a],
                   [ sin(theta)*cos(alpha), cos(theta)*cos(alpha), -sin(alpha), -sin(alpha)*d],
                   [ sin(theta)*sin(alpha), cos(theta)*sin(alpha),  cos(alpha),  cos(alpha)*d],
                   [                     0,                     0,           0,             1]])
    # Substitute in the DH parameters into the matrix
    Ta_b = Ta_b.subs(s)
    return Ta_b

def rot_x(q):
    r_x = Matrix([[ 1,      0,       0],
                  [ 0, cos(q), -sin(q)],
                  [ 0, sin(q),  cos(q)]])
    return r_x

def rot_y(q):
    r_y = Matrix([[  cos(q), 0, sin(q)],
                  [       0, 1,      0],
                  [ -sin(q), 0, cos(q)]])
    return r_y

def rot_z(q):
    r_z = Matrix([[ cos(q), -sin(q), 0],
                  [ sin(q),  cos(q), 0],
                  [      0,       0, 1]])
    return r_z

# Conversion factors between radians and degrees
rtd = 180 / pi
dtr = pi / 180

# Define DH parameter symbols
theta1, theta2, theta3, theta4, theta5, theta6, theta7 = symbols('theta1:8')
alpha0, alpha1, alpha2, alpha3, alpha4, alpha5, alpha6 = symbols('alpha0:7')
d1, d2, d3, d4, d5, d6, d7 = symbols('d1:8')    # link offsets
a0, a1, a2, a3, a4, a5, a6 = symbols('a0:7')    # link lengths

# Modified DH params for KUKA KR210
s = {alpha0:     0, d1:  0.75, a0:      0,
     alpha1: -pi/2, d2:     0, a1:   0.35, theta2: theta2 - pi/2,
     alpha2:     0, d3:     0, a2:   1.25,
     alpha3: -pi/2, d4:  1.50, a3: -0.054,
     alpha4:  pi/2, d5:     0, a4:      0,
     alpha5: -pi/2, d6:     0, a5:      0,
     alpha6:     0, d7: 0.303, a6:      0, theta7: 0,}

# EE location and orientation
#px = req.poses[x].position.x
#py = req.poses[x].position.y
#pz = req.poses[x].position.z
#(roll, pitch, yaw) = tf.transformations.euler_from_quaternion(
#    [req.poses[x].orientation.x, req.poses[x].orientation.y,
#     req.poses[x].orientation.z, req.poses[x].orientation.w])

# Test pose and orientation of the end effector with all angles at 0 degrees
px = 2.1529
py = 0.0
pz = 1.9465
roll = 0.0
pitch = 0.0
yaw = 0.0

##############################################################################
# Step 1: Convert pose and orientation into a transformation matrix to
#   compute the wrist center

# Build EE roation matrix
Rrpy = rot_z(yaw) * rot_y(pitch) * rot_x(roll)
lx = Rrpy[0, 0]
ly = Rrpy[1, 0]
lz = Rrpy[2, 0]

# Calculate the wrist center (test should be (1.85, 0, 1.947)
wx = px - (s[d7] + s[d6]) * lx
wy = py - (s[d7] + s[d6]) * ly
wz = pz - (s[d7] + s[d6]) * lz
#print('WC location: (%s, %s, %s)' % (wx, wy, wz))

##############################################################################
# Step 2: Calculate thetas for joint 1, 2 and 3 (determines EE position)

# Determine the angle for joint 1
theta1 = atan2(wy, wx).evalf()
theta1 = np.clip(theta1, -185*dtr, 185*dtr)

# Find the coordinates of x2, y2 and z2 considering theta 1
x2 = s[a1] * cos(theta1)
y2 = s[a1] * sin(theta1)
z2 = s[d1]

# Find the x, y and z distances between joint 2 and the wrist center
X2_WC = wx - x2
Y2_WC = wy - y2
Z2_WC = wz - z2

# Find the distances between joint 2 and the wrist center
L2_WC = sqrt(X2_WC**2 + Y2_WC**2 + Z2_WC**2)

# Find the distance between joint 2 and the wrist center
L3_4 = 0.96     # Distance from joint 3 to joint 4
L4_5 = 0.54     # Distance from joint 4 to joint 5 (WC)
L3_4_x = sqrt(L3_4**2 + abs(s[a3])**2)      # X distance from joint 3 to joint 4
phi1 = pi - atan2(abs(s[a3]), L3_4_x)
L3_WC = sqrt(L3_4**2 + L4_5**2 - 2 * L3_4 * L4_5 * cos(phi1))

# Determine the angle for joint 3
cos_phi2 = (L2_WC**2 - L3_WC**2 - s[a2]**2) / (-2 * L3_WC * s[a2])
if abs(cos_phi2) > 1:
    cos_phi2 = 1
    print('cos_phi2 is greater than 1')
phi2 = atan2(sqrt(1 - cos_phi2**2), cos_phi2)
theta3 = (pi/2 - phi2).evalf()
theta3 = np.clip(theta3, -210*dtr, (155-90)*dtr)

# Determine the angle for joint 2
L2_WC_xy = sqrt(X2_WC**2 + Y2_WC**2)
phi3 = atan2(Z2_WC, L2_WC_xy)
cos_phi4 = (L3_WC**2 - L2_WC**2 - s[a2]**2) / (-2 * L2_WC * s[a2])
if abs(cos_phi4) > 1:
    cos_phi4 = 1
    print('cos_phi4 is greater than 1')
phi4 = atan2(sqrt(1 - cos_phi4**2), cos_phi4)
theta2 = (pi/2 - (phi3 + phi4)).evalf()
theta2 = np.clip(theta2, -45*dtr, 85*dtr)

##############################################################################
# Step 3: Determine the rotation matrix for the spherical wrist joints

# Build the transformation matrices of the first 3 joints
T0_1 = build_mod_dh_matrix(s=s, theta=theta1, alpha=alpha0, d=d1, a=a0)
T1_2 = build_mod_dh_matrix(s=s, theta=theta2, alpha=alpha1, d=d2, a=a1)
T2_3 = build_mod_dh_matrix(s=s, theta=theta3, alpha=alpha2, d=d3, a=a2)

# Rotation matrix of the first three joints
R0_3 = (T0_1 * T1_2 * T2_3).evalf(subs={theta1: theta1,
                                        theta2: theta2,
                                        theta3: theta3})[0:3, 0:3]

# Correction to account for orientation difference between definition of
#   gripper link in the URDF file and the DH convention.
#   (rotation around Z axis by 180 deg and Y axis by -90 deg)
R_corr = simplify(rot_z(pi) * rot_y(-pi/2))

# Calculate the symbolic rotation matrix of the spherical wrist joints
R3_6 = R0_3.T * Rrpy * R_corr

##############################################################################
# Step 4: Calculate the spherical wrist joint angles by converting the
#   the rotation matrix to Euler angles

# tf requires a numpy matrix instead of a sympy matrix
R3_6_np = np.array(R3_6).astype(np.float64)

# Convert the rotation matrix to Euler angles using tf
alpha, beta, gamma = tf.transformations.euler_from_matrix(
    R3_6_np, axes='rxyz')   # xyx, yzx, xyz
theta4 = alpha
theta5 = beta
theta6 = gamma

theta4 = np.pi/2 + theta4
theta5 = np.pi/2 - theta5
#theta6 = theta6 - 2*np.pi

#r11 = R3_6[0, 0]
#r12 = R3_6[0, 1]
#r13 = R3_6[0, 2]
#r21 = R3_6[1, 0]
#r31 = R3_6[2, 0]
#r32 = R3_6[2, 1]
#r33 = R3_6[2, 2]
#
## Pitch angle; rotation around the y-axis
#theta5 = atan2(-r31, sqrt(r11**2 + r21**2)).evalf()
#theta5 = np.clip(theta5, -125*dtr, 125*dtr)
#
#if r31 == 1:
#    # Gimbal lock at pitch = -90
#    theta4 = 0                          # yaw = 0
#    theta6 = atan2(-r12, -r13).evalf()  # roll
#    print('Gimbal lock at pitch = -90')
#elif r31 == -1:
#    # Gimal lock at pitch = 90
#    theta4 = 0                          # yaw = 0
#    theta6 = atan2(r12, r13).evalf()    # roll
#    print('Gimbal lock at pitch = 90')
#else:
#    # General orientation
#
#    # Yaw angle; rotation around the z-axis
#    theta4 = (atan2(r21, r11)).evalf()
#    theta4 = np.clip(theta4, -350*dtr, 350*dtr)
#
#    # Roll angle; rotation around the x-axis
#    theta6 = (atan2(r32, r33)).evalf()
#    theta6 = np.clip(theta6, -350*dtr, 350*dtr)

print('Theta 1: %s' % theta1)
print('Theta 2: %s' % theta2)
print('Theta 3: %s' % theta3)
print('Theta 4: %s' % theta4)
print('Theta 5: %s' % theta5)
print('Theta 6: %s' % theta6)


from html_parser import MyHTMLParser
import urllib.request
from bs4 import BeautifulSoup
import requests

from language_detecter import LanguageDetector

parser = MyHTMLParser()
#url = "https://www.vpnverbinding.nl/beste-vpn/netflix/"
url = "https://www.vpnconexion.es/blog/mejor-vpn-para-netflix/?_ga=2.224715098.1306859094.1600959792-1235625754.1600959792"
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(req)
html = response.read()
page = requests.get(url).text
soup = BeautifulSoup(page, "html.parser")
print("Analyzing....")
print(soup.title.string)

#get the webpage language
language = soup.html["lang"].replace("-","_")

print("The language webpage is: "+language)

lang_validate = LanguageDetector(language)

print("-----------")

#find the titles h3,h2,h1 too text in p, div and span inside the divs
contentTable  = soup.find('div')
rows  = contentTable.find_all(['h3', 'h2', 'h1', 'p', 'div', 'span', 'img', 'li', 'ul'])
for row in rows:
	if not (row.string is None):
	   #print(row.string)
	   #print("············")
	   #append to set the blocks read in the webpage. (use Set for no repeated)
	   lang_validate.html_blocks.add(row)

for block in lang_validate.html_blocks:
	lang_validate.is_in_setlanguage(block.string.strip())

print("-----# blocks ------> "+str(len(lang_validate.html_blocks)))

print("=========================")
print("Words Not translated....")
print("=========================")
#lang_validate.clear_not_translated_words()
for i in lang_validate.not_translated:
	print(i)


import cv2
import numpy as np
from calibrate_frame import *
from socket import gethostname

class Camera(object):
    """
    Camera access wrapper.
    """

    def __init__(self, pitch=0, port=0, test = 0):
        self.capture = cv2.VideoCapture(port)
        self.pitch = pitch
        self.test = test

    def get_frame(self, radial_dist=0):
        """
        Retrieve a frame from the camera.

        Returns the frame if available, otherwise returns None.
        """

        if self.test == 0:
            status, frame = self.capture.read()
            frame = step(frame, self.pitch)
        elif self.test == 1:
            frame = cv2.imread('pitch0.png')

        return frame

    def close(self):
        self.capture.release()


from flask import Flask, request, render_template

students = [
    {'studentNo': '10001', 'studentName': 'Student 1'},
    {'studentNo': '10002', 'studentName': 'Student 2'},
]

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html', students=students)

app.run(debug=True)


#!/usr/bin/env python3

import json
import logging
from watchdog.events import FileSystemEventHandler, FileModifiedEvent
from watchdog.observers import Observer
import xml.etree.ElementTree as ET

logger = logging.getLogger(__name__)

class IoMBianAvahiServicesFileHandler(FileSystemEventHandler):

    def __init__(self, file_path="/etc/avahi/services/iombian.service"):
        self.file_path = file_path
        self.tree = None
        self.services_discovered_callback = None
        self.observer = None
        self.load_file()

    def start(self):
        logger.debug("Starting Avahi Service File Handler")
        if self.observer:
            logger.error("Service already started")
            return
        self.observer = Observer()
        self.observer.schedule(self, self.file_path)
        self.observer.start()
        self.load_services()

    def stop(self):
        logger.debug("Stopping Avahi Service File Handler")
        if self.observer:
            self.observer.stop()
            self.observer.join()

    def add_services_discovered_callback(self, callback):
        if self.services_discovered_callback:
            logger.warn("Services discovered callback already set")
            return
        self.services_discovered_callback = callback

    def load_file(self):
        self.tree = ET.parse(self.file_path)

    def on_modified(self, event):
        if isinstance(event, FileModifiedEvent):
            logger.debug(f"Avahi file ('{self.file_path}') has been modified")
            self.load_services()

    def load_services(self):
        services = {}
        txt_records_elements = self.tree.findall(".//txt-record")
        for txt_record_element in txt_records_elements:
            txt_record = txt_record_element.text
            service_name, service_info = txt_record.split("=")
            services[service_name] = json.loads(service_info)
        if self.services_discovered_callback:
            self.services_discovered_callback(services)


import argparse
import json

from pyspark.sql import SparkSession

def main(input_hfs_path,
         outliers_output_hfs_path,
         clean_output_hfs_path,
         config):
    from filters.api import resolve_filter
    spark = SparkSession \
        .builder \
        .appName("TextOutlier") \
        .getOrCreate()
    sc = spark.sparkContext

    raw_data = sc.textFile(input_hfs_path)
    print("Read from {}".format(input_hfs_path))
    filtered_rdds = []

    original_data = raw_data \
        .zipWithIndex() \
        .map(lambda _: (_[1], _[0]))
    data = original_data

    if config["show_counts"]:
        total_count = data.count()
        remaining_count = total_count
    else:
        total_count = remaining_count = -1

    total_filtered_count = 0
    for filter_index, filter_config in enumerate(config["filters"]):
        filter_instance = resolve_filter(filter_config)
        print("Running [{}] {}".format(
            filter_index, filter_instance.short_name
        ))
        data, filtered_data = filter_instance.run_filter(
            data=data,
            filter_index=filter_index,
        )
        filtered_rdds.append(filtered_data)
        if config["show_counts"]:
            filtered_count = filtered_data.count()
            total_filtered_count += filtered_count
            print("  Filtered out {} observation{} -- "
                  "{:.2f}% of total, {:.2f}% of remainder".format(
                        filtered_count,
                        "" if filtered_count == 1 else "s",
                        filtered_count / total_count * 100,
                        filtered_count / remaining_count * 100
                    ))
            remaining_count -= filtered_count

    if config["show_counts"]:
        print("ORIGINAL:  {} ({:.2f}%)".format(
            total_count, 100,
        ))
        print("FILTERED:  {} ({:.2f}%)".format(
            total_filtered_count, total_filtered_count / total_count * 100,
        ))
        print("REMAINING: {} ({:.2f}%)".format(
            remaining_count, remaining_count / total_count * 100,
        ))

    filtered_rdd = sc.union(filtered_rdds)
    outliers = filtered_rdd \
        .leftOuterJoin(original_data) \
        .map(lambda _: (_[0], _[1][1], _[1][0]))

    print("Writing outliers to {}".format(outliers_output_hfs_path))
    outliers.saveAsTextFile(outliers_output_hfs_path)

    if clean_output_hfs_path:
        print("Writing clean output to {}".format(clean_output_hfs_path))
        data.saveAsTextFile(clean_output_hfs_path)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Textual Outlier Detection')
    parser.add_argument("--input_hfs_path", type=str, required=True)
    parser.add_argument("--outliers_output_hfs_path", type=str, required=True)
    parser.add_argument("--clean_output_hfs_path",
                        type=str, required=False, default="")
    parser.add_argument("--config_json_path", type=str, required=True)
    args = parser.parse_args()

    with open(args.config_json_path, "r") as f:
        config_ = json.loads(f.read())
    main(
        input_hfs_path=args.input_hfs_path,
        outliers_output_hfs_path=args.outliers_output_hfs_path,
        clean_output_hfs_path=args.clean_output_hfs_path,
        config=config_,
    )


""" pretrain a word2vec on the corpus"""
import argparse
import os
from os.path import join, exists
from time import time
from datetime import timedelta

import gensim

class Sentences(object):
    """ needed for gensim word2vec training"""
    def __init__(self, data_path):
        with open(data_path, 'r') as fin:
            self.lines = fin.readlines()

    def __iter__(self):
        for line in self.lines:
            report_id, txt, classes = line.strip('\n').split('|,|')
            yield txt.lower().split()

def main(args):
    start = time()
    save_dir = args.path
    if not exists(save_dir):
        os.makedirs(save_dir)
    sentences = Sentences(args.data_path)
    model = gensim.models.Word2Vec(
        size=args.dim, min_count=5, workers=16, sg=1)
    model.build_vocab(sentences)
    print('vocab built in {}'.format(timedelta(seconds=time()-start)))
    model.train(sentences,
                total_examples=model.corpus_count, epochs=model.iter)

    model.save(join(save_dir, 'word2vec.{}d.{}.bin'.format(
        args.dim, len(model.wv.vocab))))
    model.wv.save_word2vec_format(join(
        save_dir,
        'word2vec.{}d.{}.w2v'.format(args.dim, len(model.wv.vocab))
    ))

    print('word2vec trained in {}'.format(timedelta(seconds=time()-start)))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='train word2vec embedding used for model initialization'
    )
    parser.add_argument("--data_path", type=str, default='./tc_data/track1_round1_train_20210222.csv')
    parser.add_argument('--path', type=str, default='./user_data/model_data/', help='root of the model')
    parser.add_argument('--dim', action='store', type=int, default=256)
    args = parser.parse_args()

    main(args)