import os, sys
import metadata
import pandas as pd
import numpy as np
import random
import math
from collections import defaultdict
from statsmodels.tsa.stattools import adfuller

from plot import plt, sns

def draw(path, srv):
     filename = os.path.join(path, srv["preprocessed_filename"])
     df = pd.read_csv(filename, sep="\t", index_col='time', parse_dates=True)
     bins = defaultdict(list)
     for i, col in enumerate(df.columns):
         serie = df[col].dropna()
         if pd.algos.is_monotonic_float64(serie.values, False)[0]:
             serie = serie.diff()[1:]
         p_value = adfuller(serie, autolag='AIC')[1]
         if math.isnan(p_value): continue
         nearest = 0.05 * round(p_value/0.05)
         bins[nearest].append(serie)
     for bin, members in bins.items():
         series = [serie.name for serie in members]
         if len(members) <= 10:
             columns = series
         else:
             columns = random.sample(series, 10)

         subset = df[columns]
         name = "%s_adf_confidence_%.2f.png" % (srv["name"], bin)
         print(name)
         axes = subset.plot(subplots=True)
         plt.savefig(os.path.join(path, name))
         plt.close("all")

if __name__ == '__main__':
    if len(sys.argv) < 1:
        sys.stderr.write("USAGE: %s measurment\n" % sys.argv[0])
        sys.exit(1)
    for path in sys.argv[1:]:
        services = metadata.load(path)["services"]
        for srv in services:
            draw(path, srv)


from pytest import mark

TOPOLOGY = """
#
# +-------+
# |  sw1  |
# +-------+
#

# Nodes
[type=openswitch name="OpenSwitch"] ops1
"""

@mark.platform_incompatible(['docker'])
def test_opennsl_ct_sflow(topology, step):
    ops1 = topology.get('ops1')

    assert ops1 is not None

    # sflow configuration values
    sampling_rate = 100
    collector_ip = '10.10.10.1'

    # Configuring sflow globally
    step("### Configuring sFlow ###")
    with ops1.libs.vtysh.Configure() as ctx:
        ctx.sflow_enable()
        ctx.sflow_sampling(sampling_rate)
        ctx.sflow_collector(collector_ip)

    collector = {}
    collector['ip'] = '10.10.10.1'
    collector['port'] = '6343'
    collector['vrf'] = 'vrf_default'

    # Comparing values stored in DB with expected values
    sflow_config = ops1.libs.vtysh.show_sflow()
    assert sflow_config['sflow'] == 'enabled'
    assert int(sflow_config['sampling_rate']) == sampling_rate
    assert sflow_config['collector'][0] == collector

    # Verifying whether sampling rate is correctly set in ASIC for the
    # displayed interfaces
    step("### Verifying sampling rate for displayed interfaces ###")
    step("### Issuing ovs-appctl command to get the sflow sampling rate ###")
    ret = ops1("ovs-appctl -t ops-switchd sflow/show-rate", shell="bash")

    appctl_output = ret.splitlines()
    # Skipping headings from the output of ovs-appctl command
    appctl_output = appctl_output[4:]

    for intf in appctl_output:
        # Each line contains sampling rate information per interface:
        # [Interface name, Ingress sampling rate, Egress sampling rate]
        # We expect same samping rate on ingress and egress
        step(str(intf))
        intf_sampling_rates = intf.split()
        assert int(intf_sampling_rates[1]) == sampling_rate
        assert int(intf_sampling_rates[2]) == sampling_rate

    step("### Verifying sFlow knet filters ###")
    knet_output = ops1("ovs-appctl plugin/debug knet filter", shell="bash")
    assert 'sflow_knet_filter_' in knet_output


from django.test import TestCase

from library.models.record import Record
from library.models.documents import Book
from library.models.author import Author

from django.contrib.auth.models import User, Group

from login.models import CustomUser

class TakeFromUserMethodTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        Author.objects.create(first_name='test', last_name='test')
        user = CustomUser.objects.create(email="user@user.com", first_name='test', last_name="testtest",
                                         phone_number="test", address="test")
        groups = Group.objects.create(name='Students')

        user.groups.add(groups)

    def setUp(self):
        book = Book.objects.create(title='test')
        book.authors.set(Author.objects.all())
        self.book = book

        self.record = Record.objects.create(document=self.book)

    def test_take_from_user_takes_from_user(self):
        user = CustomUser.objects.first()
        self.book.give_to_user(user)
        self.book.take_from_user(user)
        record = Record.objects.first()

        self.assertNotEqual(record.user, user)

    def test_take_from_user_reset_status(self):
        user = CustomUser.objects.first()
        self.book.give_to_user(user)
        self.book.take_from_user(user)
        record = Record.objects.first()

        self.assertEqual(record.status, 'a')

    def test_take_from_user_reset_due_to(self):
        user = CustomUser.objects.first()
        self.book.give_to_user(user)
        self.book.take_from_user(user)
        record = Record.objects.first()

        self.assertEqual(record.due_to, None)


'''
Rotoscopy helping function
'''
from sklearn.metrics import confusion_matrix
import numpy as np

import numpy as np
import glob
import argparse
import cv2
import os
import sys
from matplotlib import pyplot as plt

def compute_iou(Y_pred, Y_true, labels):
    '''
    Computes mean IOU given the true mask and the predicted mask.

    Arguments ::
        Y_pred --
        Y_true --
        labels --
    Return ::
        meanIOU --
    '''
    # ytrue, ypred is a flatten vector
    Y_pred = Y_pred.flatten()
    Y_true = Y_true.flatten()

    current = confusion_matrix(Y_true, Y_pred, labels)

    intersection = np.diag(current)

    ground_truth_set = current.sum(axis=1)
    predicted_set = current.sum(axis=0)
    union = ground_truth_set + predicted_set - intersection

    IoU = intersection / union.astype(np.float32)
    return np.mean(IoU)

def get_individual_mask(pre_mask_path, dir_name='', k=100):
    '''
    Generates individual masks.

    Arguments ::
        pre_mask_path -- str | path where the mask is stored
        dir_name -- optional | str | directory name of the masks
        k -- int | optional | mask descriptor
    Return ::
        k -- int | descriptor of the last mask
    '''
    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    pre_mask = cv2.imread(pre_mask_path, 0)
    individual_masks = []
    row_count = 0
    col_count = 0
    flag = True
    pre_row = 0
    pre_col = 0
    init_k = k
    to_del = []

    if len(pre_mask.shape) > 2:
        pre_mask = np.squeeze( cv2.cvtColor(pre_mask, cv2.COLOR_BGR2GRAY))

    for row in range(pre_mask.shape[0]):
        if np.sum(pre_mask[row]) > 0 and flag:
            row_count += 1
            flag = False
        elif np.sum(pre_mask[row]) == 0 and not flag:
            flag = True
            if row_count > 0 and row > 0:
                new_mask = np.zeros_like(pre_mask)
                new_mask[pre_row : row] = pre_mask[pre_row : row]
                if np.sum(new_mask == pre_mask) != np.sum(pre_mask == pre_mask):
                    to_del.append(dir_name+'mask'+str(k-1)+'.png')
                cv2.imwrite(dir_name+'mask'+str(k-1)+'.png', new_mask)
                individual_masks.append(dir_name+'mask'+str(k-1)+'.png')
                k += 1
                pre_row = row

    flag = True
    if k - init_k == 0:
        individual_masks.append(pre_mask_path)
    print(k, individual_masks)

    for row_sep_mask in individual_masks:
        pre_mask = cv2.imread(row_sep_mask, 0)
        temp_masks = []
        flag = True
        if len(pre_mask.shape) > 2:
            pre_mask = np.squeeze( cv2.cvtColor(pre_mask, cv2.COLOR_BGR2GRAY))

        for col in range(pre_mask.shape[1]):
            if np.sum(pre_mask[:, col]) > 0 and flag:
                col_count += 1
                flag = False
            elif np.sum(pre_mask[:, col]) == 0 and not flag:
                flag = True
                if col_count > 0 and col > 0:
                    new_mask = np.zeros_like(pre_mask)
                    new_mask[:, pre_col : col] = pre_mask[:, pre_col : col]
                    if np.sum(new_mask == pre_mask) != np.sum(pre_mask == pre_mask):
                        to_del.append(dir_name+'mask'+str(k)+'.png')
                    cv2.imwrite(dir_name+'mask'+str(k)+'.png', new_mask)
                    temp_masks.append(dir_name+'mask'+str(k)+'.png')
                    k += 1
                    pre_col = col
        if k > init_k:
            init_k = k
        if len(temp_masks) > 0:
            individual_masks.remove(row_sep_mask)
            individual_masks.extend(temp_masks)

    flag = True
    if k > init_k:
        k -= 1
        init_k = k
    print(k, individual_masks)

    for row_sep_mask in individual_masks:
        pre_mask = cv2.imread(row_sep_mask, 0)
        temp_masks = []
        flag = True

        if len(pre_mask.shape) > 2:
            pre_mask = np.squeeze( cv2.cvtColor(pre_mask, cv2.COLOR_BGR2GRAY))

        for row in range(pre_mask.shape[0]):
            if np.sum(pre_mask[row]) > 0 and flag:
                row_count += 1
                flag = False
            elif np.sum(pre_mask[row]) == 0 and not flag:
                flag = True
                if row_count > 0 and row > 0:
                    new_mask = np.zeros_like(pre_mask)
                    new_mask[pre_row : row] = pre_mask[pre_row : row]
                    if np.sum(new_mask == pre_mask) != np.sum(pre_mask == pre_mask):
                        to_del.append(dir_name+'mask'+str(k)+'.png')
                    cv2.imwrite(dir_name+'mask'+str(k)+'.png', new_mask)
                    k += 1
                    pre_row = row

    return k

def remove_same_from_list(dir_name, rm_list):
    '''
    Removes similar images from the rm_list and the list

    Arguments ::
        dir_name -- str | directory name, where the masks are saved.
        rm_list -- list | contains image names to be removed and similar images to be removed
    '''

    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    for image_name in os.listdir(dir_name):
        if image_name.split('.')[-1] == 'png':
            if image_name in rm_list:
                continue
            else:
                image = cv2.imread(dir_name + image_name, 0)
                check = np.sum(image == image)
                if np.max(image) == 255:
                    image = image / 255
                for rm_image_name in rm_list:
                    rm_image = cv2.imread(rm_image_name, 0)
                    if np.max(rm_image) == 255:
                        rm_image = rm_image / 255
                    if np.sum(image == rm_image) == check:
                        os.remove(dir_name + image_name)
                        break

def remove_from_list(dir_name, rm_list):
    '''
    Removes images from rm_list inside dir

    Arguments ::
        dir_name -- str | directory name, where the masks are saved.
        rm_list -- list | contains image names to be removed and similar images to be removed
    '''

    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    for image_name in os.listdir(dir_name):
        if image_name.split('.')[-1] == 'png':
            if image_name in rm_list:
                continue
                os.remove(dir_name + image_name)

def remove_empty(dir_name):
    '''
    Given a directory name, removes all the empty images.

    Arguments ::
        dir_name -- str | directory name, where the masks are saved.
    '''
    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    for image_name in os.listdir(dir_name):
        if image_name.split('.')[-1] == 'png':
            if np.sum(cv2.imread(dir_name + image_name, 0)) == 0:
                os.remove(dir_name + image_name)

def remove_same(dir_name):
    '''
    Given a directory name, removes all the images which are same.

    Arguments ::
        dir_name -- str | directory name, where the masks are saved.
    '''
    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    for image_name in os.listdir(dir_name):
        for image2_name in os.listdir(dir_name):
            if image_name != image2_name:
                if image_name.split('.')[-1] == 'png':
                    if np.sum(cv2.imread(dir_name + image_name, 0) != cv2.imread(dir_name + image2_name, 0)) == 0:
                        os.remove(dir_name + image_name)

def remove_low_area_mask(dir_name):
    '''
    Given a directory name, removes all the masks for which the area is very low.

    Arguments ::
        dir_name -- str | directory name, where the masks are saved.
    '''
    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    for image_name in os.listdir(dir_name):
        if image_name.split('.')[-1] == 'png':
            image = cv2.imread(dir_name + image_name, 0)
            max_pix = np.max(image)
            if np.sum(image) < 18 * max_pix:
                    os.remove(dir_name + image_name)

def remove_superset(dir_name):
    '''
    Given a directory name, removes all the images which are superset of any image.

    Arguments ::
        dir_name -- str | directory name, where the masks are saved.
    '''
    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    for image_name in os.listdir(dir_name):
        if not os.path.isfile(dir_name + image_name):
            continue
        for image2_name in os.listdir(dir_name):
            if not os.path.isfile(dir_name + image_name):
                break
            if not os.path.isfile(dir_name + image2_name):
                continue
            if image_name != image2_name:
                if image_name.split('.')[-1] == 'png':
                    image = cv2.imread(dir_name + image_name, 0)
                    image2 = cv2.imread(dir_name + image2_name, 0)

                    max_pix = np.max(image)
                    image_bool = image == max_pix
                    image2_bool = image == max_pix
                    imagex_bool = np.logical_and(image_bool, image2_bool)
                    # print(image_name, image2_name, np.sum(imagex_bool))

                    if np.sum(image_bool != imagex_bool) == 0:
                        os.remove(dir_name + image_name)
                    elif np.sum(image2_bool != imagex_bool) == 0:
                        os.remove(dir_name + image2_name)
                    if not os.path.isfile(dir_name + image_name):
                        break

def get_labels(mask):
    """
    Gets all the lables in a RGB segmented image

    Arguments ::
        mask -- ndarray | ndarray representing mask
    Return ::
        n_labels -- ndarray | representing unique labels without background
    """
    labels = np.unique(cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY))
    return labels[np.where(labels != 0)]

def isolate_class(mask, class_id):
    """
    Extracts greyscale masks of all the classes given a RGB mask

    Arguments ::

    Returns ::

    """
    gmask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    temp_mask = np.zeros_like(gmask)
    temp_mask[gmask == class_id] = 255

    # temp_mask = cv2.cvtColor(temp_mask, cv2.COLOR_BGR2GRAY)
    # temp_mask[temp_mask > 0] = 255

    return temp_mask

if __name__ == '__main__':
    mask = cv2.imread(sys.argv[1])
    cv2.imwrite('a.png', cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY))
    labels = get_labels(mask)
    print(labels)

    i = 1
    for label in labels:
        label_mask = isolate_class(mask, label)
        cv2.imwrite('mask'+str(i)+'.png', label_mask)
        i += 1

    '''
    dir_name = sys.argv[1]
    if dir_name[-1] != '/':
        dir_name = dir_name+'/'

    k = 200
    for image_name in os.listdir(dir_name):
        if image_name.split('.')[-1] == 'png':
            k = get_individual_mask(dir_name + image_name, dir_name, k=k)

    remove_empty(dir_name)
    remove_same(dir_name)
    '''


from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Fieldset, Field
from crispy_forms.bootstrap import *
from burials.models import *

class GenericFilterFormHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(GenericFilterFormHelper, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.form_class = 'genericFilterForm'
        self.form_method = 'GET'
        self.add_input(Submit('Filter', 'search'))

class MainListFilterFormHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super(MainListFilterFormHelper, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.form_class = 'genericFilterForm'
        self.form_method = 'GET'
        #self.helper.form_tag = False
        self.add_input(Submit('Filter', 'Search'))
        self.layout = Layout(
            Accordion(
                AccordionGroup(
                    'Burial search options',
                    'burial_id',
                    'burial_group',
                    'burial_type',
                    'C14_dendro',
                    'absolute_age',
                    'secondary_burial',
                    'displaced',
                    'extraordinary_burial',
                    'construction',
                    'arrangement',
                    'cover',
                    'cover_type',
                    'grave_pit_form',
                    'grave_pit_orientation',
                    'length',
                    'width',
                    'diameter',
                    'height',
                    'filling_objects',
                    'intentionally_deposited',
                    'filling',
                    'post_holes',
                    'surface_identification_mark',
                    'erdgraebchen',
                    'other_features',
                    css_id="burial_search_fields",
                    active=False,
                ),
                AccordionGroup(
                    'Burial site search options',
                    'burial_site__name',
                    'burial_site__alternative_name',
                    'burial_site__location',
                    'burial_site__topography',
                    'burial_site__excavation',
                    'burial_site__distance_to_next_settlement',
                    'burial_site__type_of_burial_site',
                    'burial_site__disturbance',
                    'burial_site__total_graves',
                    'burial_site__dating',
                    'burial_site__absolute_dating',
                    css_id="burial_site_search_options"),
                AccordionGroup(
                    'Urn and Urn Cover related search options',
                    'urn__basic_shape',
                    'urn__urn_type',
                    'urn__variation',
                    'urn__urn_id',
                    'urn__urncover_exists',
                    'urn__urncover__basic_shape',
                    'urn__urncover__upside_down',
                    'urn__urncover__fragment',
                    'urn__urncover__cover_id',
                    css_id="urn_and_urn_cover_search_options"
                ),
                AccordionGroup(
                    'Grave Good search options',
                    'gravegood__name',
                    'gravegood__material',
                    'gravegood__condition',
                    'gravegood__position',
                    'gravegood__amount_countable',
                    css_id="grave_good_search_options"),
                AccordionGroup(
                    'Organic Grave Good search options',
                    'gravegoodother__food',
                    'gravegoodother__other_organic_grave_good',
                    'gravegoodother__position',
                    'gravegoodother__amount_countable',
                    css_id="organic_grave_good_search_options"),
                AccordionGroup(
                    'Anthropology search options',
                    'deadbodyremains__age',
                    'deadbodyremains__gender',
                    'deadbodyremains__temperature',
                    'deadbodyremains__position',
                    'deadbodyremains__weight',
                    'deadbodyremains__pathology',
                    'deadbodyremains__total_weight',
                    'deadbodyremains__amount_countable',
                    css_id="anthropology_search_options"),
                AccordionGroup(
                    'Animal Remains search options',
                    'animalremains__species',
                    'animalremains__age',
                    'animalremains__sex',
                    'animalremains__weight',
                    'animalremains__position',
                    'animalremains__amount_countable',
                    css_id="animal_remains_search_options"),
                css_id="accordion",
                )
            )


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#  DESCRIPTION: zero array and M operations
#       AUTHOR: artemirk@gmail.com ()
# ===============================================================================
n, m = (map(int ,input().split(' ')))
erg = [0 for en in range(n)]
ende, added = 0, 0

for q in range(m):
    k1, k2, z = map(int, input().split(' '))
    erg[k1-1] += z
    if k2 < n :
        erg[k2] -= z

print(erg)
for r in erg:
    added += r
    if added > ende:
        ende = added

print(ende)
#(n,m) = map(int,input().strip().split(' '))
#ray = [0] * n
#mx = 0
#
#for i in range(m):
#    (a,b,k) = map(int,input().strip().split(' '))
#    ray[a-1] += k
#    if (mx < ray[l]): mx = ray[l]
#
#print(ray)
#print(mx)


import pandas as pd
import numpy as np

# df = pd.read_csv(
#     '2.csv',
#     header=None,
#     names=['id','nama','usia','massa'],
#     # na_values=['-','n.a']
# )
# # print(df.set_index('id'))
# df = df.set_index('id')

# 1,Andi,21,65
# 2,Budi,22,45
# 3,Caca,23,76
# 4,Deni,24,65
# 5,Euis,25,76
# 6,Fafa,26,54
# 7,Gaga,27,55
# 8,Hani,28,78
# 9,Inne,29,82
# 10,Janu,30,43
# print(df[['nama','massa','usia']])
# print(df.iloc[1::2].sort_values(by='nama',ascending=False)[['nama','usia']])

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

# 1,Andi,21,65
# 2,Budi,22,45
# 3,n.a,-,76
# 4,Deni,24,65
# 5,Euis,25,n.a
# 6,Fafa,26,n.a
# 7,Gaga,27,55
# 8,Hani,-,n.a
# 9,n.a,29,82
# 10,Janu,30,43

# print(df['massa'].mean())
# df = df.fillna(0)
# df = df.fillna({
#     'massa':0,
#     'usia':0,
#     'nama': 'Tanpa nama'
# })
# df = df.fillna(method='ffill',axis='columns') #bfill = backwards
# df = df.fillna(method='ffill')
# df = df.interpolate().fillna({
#     'nama': 'Tanpa nama'
# })
# df = df.ffill().fillna({
#     'nama': 'Tanpa nama'
# })

# df=df.dropna() #yang ada NaN ga boleh ikut
# df=df.dropna(thresh=2)
# df=df.dropna(axis='columns')
# df = df.dropna(subset=['nama'])
# df = df.replace(['-','n.a'], 'Data Tidak Tersedia')
# df = df.replace(['-','n.a'], np.NaN)
# df = df.fillna('x')
# df = df.replace({
#     '-' : 0,
#     'n.a': np.NaN
# })
# df = df.replace({
#     '-' : 0,
#     'n.a': np.NaN
# })
# df = df.replace(
#     to_replace=['-','n.a'],
#     method = 'ffill'
# )
# print(df)

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

# 1,Andi,21,65
# 2,Budi,22,45
# 3,-,-,76
# 4,Deni,24,65
# 5,Euis,25,-
# 6,Fafa,26,-
# 7,Gaga,27,55
# 8,Hani,-,n.a
# 9,-,29,82
# 10,Janu,30,43

# df=df.replace({
#     'nama': '-',
#     'usia': '-',
#     # 'massa':['-','n.a']
# },{'nama':'Anonim',
#     'usia':0,
#     # 'massa': 0
#     })

# df['massa'] = df['massa'].replace(
#     to_replace=['-','n.a'],
#     method='ffill'
# )

# print(df)

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

# 1,Andi,21,65,Jakarta
# 2,Budi,22,45,Bandung
# 3,Caca,23,76,Jakarta
# 4,Deni,24,46,Bandung
# 5,Euis,25,76,Denpasar
# 6,Fafa,26,55,Jakarta
# 7,Gaga,27,87,Denpasar
# 8,Hani,28,76,Jakarta
# 9,Halo,29,82,Surabaya
# 10,Janu,30,43,Bandung

df = pd.read_csv(
    '2.csv',
    header=None,
    names=['id','nama','usia','massa','kota'],
    # na_values=['-','n.a']
)
# print(df.set_index('id'))
df = df.set_index('id')
# df = df.drop('usia',axis=1)
df = df.drop('nama',axis=1)
grup = df.groupby('kota')
dfJkt=grup.get_group('Jakarta')
dfBdg=grup.get_group('Bandung')

print(dfJkt['massa'].mean())
# print(dfJkt[['nama']][dfJkt['massa']>68])
# print(dfJkt[dfJkt['massa']>=dfJkt['massa'].mean()])
print(grup.max())
print(grup.mean())


import pytest
import tensorflow as tf
import numpy as np

from tensorflow_addons.image import color_ops
from PIL import Image, ImageOps, ImageEnhance
from skimage.exposure import equalize_hist

_DTYPES = {
    np.uint8,
    np.int32,
    np.int64,
    np.float16,
    np.float32,
    np.float64,
}

_SHAPES = {(5, 5), (5, 5, 1), (5, 5, 3), (4, 5, 5), (4, 5, 5, 1), (4, 5, 5, 3)}

@pytest.mark.parametrize("dtype", _DTYPES)
@pytest.mark.parametrize("shape", _SHAPES)
def test_equalize_dtype_shape(dtype, shape):
    image = np.ones(shape=shape, dtype=dtype)
    equalized = color_ops.equalize(tf.constant(image)).numpy()
    np.testing.assert_equal(equalized, image)
    assert equalized.dtype == image.dtype

@pytest.mark.usefixtures("maybe_run_functions_eagerly")
def test_equalize_with_PIL():
    np.random.seed(0)
    image = np.random.randint(low=0, high=255, size=(4, 3, 3, 3), dtype=np.uint8)
    equalized = np.stack([ImageOps.equalize(Image.fromarray(i)) for i in image])
    np.testing.assert_equal(color_ops.equalize(tf.constant(image)).numpy(), equalized)

@pytest.mark.usefixtures("maybe_run_functions_eagerly")
@pytest.mark.parametrize("bins", [256, 65536])
def test_equalize_with_skimage(bins):
    np.random.seed(0)
    image = np.random.randint(low=0, high=256, size=(5, 5, 3, 3), dtype=np.uint8)
    equalized = tf.cast(color_ops.equalize(tf.constant(image), bins), np.float16)
    equalized = tf.math.divide(equalized, 255)
    skiequalized = equalize_hist(image, bins)
    assert (
        tf.reduce_mean(
            tf.math.abs(tf.reshape(tf.math.subtract(equalized, skiequalized), [-1]))
        )
        <= 0.015
    ), "Greater than 0.015 mean absolute difference in arrays"
    # Test asserts a tolerance of at least 0.015 mean difference between the two
    # arrays due to the difference in implementation between PIL and skimage.
    # PIL's and skimage's equalized arrays can have a mean difference ranging
    # from 0.006-0.036 according showcase done in
    # https://github.com/tensorflow/addons/pull/2381

@pytest.mark.parametrize("dtype", _DTYPES)
@pytest.mark.parametrize("shape", _SHAPES)
def test_sharpness_dtype_shape(dtype, shape):
    image = np.ones(shape=shape, dtype=dtype)
    sharp = color_ops.sharpness(tf.constant(image), 0).numpy()
    np.testing.assert_equal(sharp, image)
    assert sharp.dtype == image.dtype

@pytest.mark.parametrize("factor", [0, 0.25, 0.5, 0.75, 1])
def test_sharpness_with_PIL(factor):
    np.random.seed(0)
    image = np.random.randint(low=0, high=255, size=(10, 5, 5, 3), dtype=np.uint8)
    sharpened = np.stack(
        [ImageEnhance.Sharpness(Image.fromarray(i)).enhance(factor) for i in image]
    )
    np.testing.assert_allclose(
        color_ops.sharpness(tf.constant(image), factor).numpy(), sharpened, atol=1
    )


# You will be given a string s of size n you have to perform a perticulat type query multiples times on string,
# the query string T, find the lengh of a lexicographically smallest suffix string of S starts with T if there is no suffix return 0....

# Input Format:
# String S
# Q Query of Array....

def sol(S,T):
    if S.find(T) == -1:
        return 0
    return len(S[S.find(T):])

# Test Case 1
# S = "mazdpoghyykht"
# T = ["yk"]

# Test Case 2
# S = "ktmrgjswhwxu"
# T = ["z","w"]

# Test Case 3
S = "tvtrpudosnlydgi"
T = ["do"]

sum = 0
for i in T:
    sum = sum + sol(S,i)

print(sum)


import re
import os

def read_inlier_ratio_from_file(file_path):
    with open(file_path, 'r') as txt_file:
        lines = txt_file.readlines()
        if len(lines) >= 2:
            # Extract numerical value using regular expression
            inlier_ratio_line = lines[1].strip()
            match = re.search(r'\d+\.\d+', inlier_ratio_line)
            if match:
                inlier_ratio = float(match.group())
                return inlier_ratio
    return None

def calculate_average_inlier_ratio(file_paths):
    inlier_ratios = []
    for file_path in file_paths:
        inlier_ratio = read_inlier_ratio_from_file(file_path)
        if inlier_ratio is not None:
            inlier_ratios.append(inlier_ratio)

    if inlier_ratios:
        average_inlier_ratio = sum(inlier_ratios) / len(inlier_ratios)
        return average_inlier_ratio
    return None

directory_path = "past_results/results_current_best_pbr_new/results_poses_RANSAC/results"  # Replace this with the actual directory path
file_paths = []

for filename in os.listdir(directory_path):
    if filename.endswith(".txt"):
        file_paths.append(os.path.join(directory_path, filename))

#file_paths = ['past_results/results_125_pt_files/results_poses_RANSAC/results/obj_1_result_3.txt', 'past_results/results_125_pt_files/results_poses_RANSAC/results/obj_1_result_14.txt', 'past_results/results_125_pt_files/results_poses_RANSAC/results/obj_1_result_24.txt']

average_inlier_ratio = calculate_average_inlier_ratio(file_paths)
if average_inlier_ratio is not None:
    print("Average Inlier Ratio:", average_inlier_ratio)
else:
    print("No inlier ratios found in the files.")


import os
import imutils
import cv2

# Create an Image object from an Image
inputdir='X:/Projekte/iDAI.shapes/Mining_Shapes/INCOMING/testfpdf/ZenonID_000013934'
for image in os.listdir(inputdir):
    if image.endswith('.jpg'):
        print(image)
        colorImage  = cv2.imread(inputdir + '/' + image)
        rotated = imutils.rotate(colorImage, 180)
        cv2.imwrite(inputdir + '/' + image, rotated)


import logging
from pathlib import Path

import pandas as pd

from foodscope.mappings import expand_greeks

_log = logging.getLogger(__name__)
_csv_dir = Path(Path(__file__).parent.parent, "v2020_04_07")

def lazy_property(fn):
    attr = f"_lazy_{fn.__name__}"

    @property
    def _lazy(self):
        if not hasattr(self, attr):
            setattr(self, attr, fn(self))
        return getattr(self, attr)

    return _lazy

class FooDb:
    def __init__(self) -> None:
        self.food = Food()
        self.compound = Compound()
        self.nutrient = Nutrient()
        self.health_effect = HealthEffect()
        self.compounds_health_effect = CompoundsHealthEffect()
        self._content = None

    @lazy_property
    def content(self):
        return Content()

    def foods_by_compound(self, compound: str):
        _log.warn("Use Food.by_compound instead")
        return self.food.by_compound(compound)

    def health_effects(self, food: str):
        df = filter_content(source_type="Compound")
        result = pd.merge(self.food.select(food), df, on="food_id")
        result = pd.merge(
            result, self.compound, left_on="source_id", right_on="compound_id"
        )
        result = pd.merge(result, self.compounds_health_effect, on="compound_id")
        return pd.merge(result, self.health_effect, on="health_effect_id")

    def composition(self, food: str, source=None):
        if source is None:
            source = ("Compound", "Nutrient")

        df = Content(cols=["source_id", "source_type", "food_id"])
        df = pd.merge(df, self.food.select(food), on="food_id")
        result = pd.DataFrame()
        if "Compound" in source:
            result = result.append(
                pd.merge(
                    filter_content(df, "Compound"),
                    self.compound,
                    left_on="source_id",
                    right_on="compound_id",
                )
            )
        if "Nutrient" in source:
            result = result.append(
                pd.merge(
                    filter_content(df, "Nutrient"),
                    self.nutrient,
                    left_on="source_id",
                    right_on="nutrient_id",
                )
            )
        return result

class Table(pd.DataFrame):
    _filename = None
    _cols = None
    _rename = None

    def __init__(self, cols=None):
        if cols is None:
            cols = self._cols
        elif cols == "all":
            cols = None
        df = load(self._filename, cols)
        if self._rename is not None:
            df.rename(columns=self._rename, inplace=True)
        super().__init__(df)

    def select(self, name):
        matches = expand_greeks(name)
        return self.equiv(matches)

    def equiv(self, group):
        if isinstance(group, str):
            _log.warn("Use .select() instead")
            group = [group]
        if not isinstance(group, (list, tuple, set)):
            raise TypeError("group should be iterable")
        return self.loc[self.name.str.contains("|".join(group), case=False)]

class EntityTable(Table):
    def by_id(self, row_id):
        return self.loc[self.get(self._id) == row_id, :]

class Nutrient(EntityTable):
    _filename = "Nutrient.csv"
    _cols = ["id", "name"]
    _rename = {"id": "nutrient_id"}
    _id = "nutrient_id"

class Compound(EntityTable):
    _filename = "Compound.csv"
    _cols = ["id", "name"]
    _rename = {"id": "compound_id"}
    _id = "compound_id"

    def health_effects(self, group):
        result = self.equiv(group)
        result = pd.merge(result, CompoundsHealthEffect(), on="compound_id")
        return pd.merge(result, HealthEffect(), on="health_effect_id")

class Food(EntityTable):
    _filename = "Food.csv"
    _cols = ["id", "name"]
    _rename = {"id": "food_id"}
    _id = "food_id"

    def by_compound(self, name):
        compounds = DB.compound.select(name)
        ids = compounds.compound_id
        df = filter_content(DB.content, source_type="Compound")
        mask = df["source_id"].isin(ids)
        df = df.loc[mask, :]
        df.rename(columns={"source_id": "compound_id"}, inplace=True)

        merged = pd.merge(df, self, on="food_id")
        return pd.merge(merged, compounds, on="compound_id")

    def subtract(self, name):
        compounds = DB.compound.select(name)
        ids = compounds.compound_id
        df = filter_content(DB.content, source_type="Compound")
        mask = df["source_id"].isin(ids)
        filtered_ids = df.loc[~mask, :].food_id
        return self.loc[self.food_id.isin(filtered_ids)]

class HealthEffect(EntityTable):
    _filename = "HealthEffect.csv"
    _cols = ["id", "name", "description"]
    _rename = {"id": "health_effect_id"}
    _id = "health_effect_id"

class CompoundsHealthEffect(Table):
    _filename = "CompoundsHealthEffect.csv"
    _cols = ["id", "compound_id", "health_effect_id"]

class Content(Table):
    _filename = "Content.csv"
    _cols = [
        "id",
        "food_id",
        "source_id",
        "source_type",
        "orig_content",
        "orig_min",
        "orig_max",
    ]

def load(filename, cols=None):
    return pd.read_csv(Path(_csv_dir, filename), usecols=cols)

def filter_content(df=None, source_type=None, cols=None):
    df = Content(cols) if df is None else df
    if source_type not in ("Nutrient", "Compound"):
        raise ValueError(f"Invalid source_type={source_type}")
    return df.loc[df.source_type == source_type, :]

def foods_by_compound(compound: str):
    return DB.foods_by_compound(compound)

def health_effects(food: str):
    return DB.health_effects(food)

# probably need outer join if source is both
def composition(food: str, source=None):
    return DB.composition(food, source)

DB = FooDb()


from django.shortcuts import render

# Create your views here.
# -*- coding: utf-8 -*-
"""
Views of Product app
"""

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
import json
from django.urls import reverse_lazy
from django.views.generic import FormView
from obp.api import API, APIError
from .forms import CreateProductForm
from django.views.decorators.csrf import csrf_exempt
from base.utils import exception_handle

class IndexProductView(LoginRequiredMixin, FormView):
    """Index view for Product"""
    template_name = "products/index.html"
    form_class = CreateProductForm
    success_url = reverse_lazy('products-create')

    def dispatch(self, request, *args, **kwargs):
        self.api = API(request.session.get('obp'))
        return super(IndexProductView, self).dispatch(request, *args, **kwargs)

    def get_form(self, *args, **kwargs):
        form = super(IndexProductView, self).get_form(*args, **kwargs)
        # Cannot add api in constructor: super complains about unknown kwarg
        form.api = self.api
        fields = form.fields
        print(fields, "These are fields")
        try:
            fields['bank_id'].choices = self.api.get_bank_id_choices()
        except APIError as err:
            messages.error(self.request, err)
        except Exception as err:
            messages.error(self.request, err)
        return form

    def form_valid(self, form):
        try:
           data = form.cleaned_data
           print(data, "This is a data")
           urlpath = '/banks/{}/products'.format(bank_id)
           payload={
                "parent_product_code": data["parent_product_code"],
                "name": data["name"],
                "more_info_url": data["more_info_url"],
                "terms_and_conditions_url": data["terms_and_conditions_url"],
                "description": data["description"],
                "meta": {
                        "license": {
                        "id": "ODbL-1.0",
                        "name": data["meta_license_name"] if data["meta_license_name"]!="" else "license name"
                        }
                    },
           }
           result = self.api.put(urlpath, payload=payload)
        except APIError as err:
            messages.error(self.request, err)
            return super(IndexProductView, self).form_invalid(form)
        except Exception as err:
            messages.error(self.request, "Unknown Error")
            return super(IndexProductView, self).form_invalid(form)
        if 'code' in result and result['code']>=400:
            messages.error(self.request, result['message'])
            return super(IndexProductView, self).form_valid(form)
        msg = 'Product {} for Bank {} has been created successfully!'.format(result['product_code'], result['bank_id'])
        messages.success(self.request, msg)
        return super(IndexProductView, self).form_valid(form)

class UpdateProductView(LoginRequiredMixin, FormView):
    template_name = "products/update.html"
    success_url = '/products/list'
    form_class = CreateProductForm

    def dispatch(self, request, *args, **kwargs):
        self.api = API(request.session.get('obp'))
        return super(UpdateProductView, self).dispatch(request, *args, **kwargs)

    def get_form(self, *args, **kwargs):
        form = super(UpdateProductView, self).get_form(*args, **kwargs)
        # Cannot add api in constructor: super complains about unknown kwarg
        form.api = self.api
        fields = form.fields
        urlpath = "/banks/{}/products/{}".format(self.kwargs['bank_id'], self.kwargs['product_code'])
        try:
            fields['bank_id'].choices = self.api.get_bank_id_choices()
        except APIError as err:
            messages.error(self.request, err)
        except Exception as err:
            messages.error(self.request, err)
        try:
            result = self.api.get(urlpath)
            fields['parent_product_code'].initial = self.kwargs['parent_product_code']
            fields['name'].initial = result['name']
            fields['more_info_url'].initial = result['more_info_url']
            fields['terms_and_conditions_url'].initial = result['terms_and_conditions_url']
            fields['description'].initial = result['description']
            fields['meta_license_id'].initial = result['meta']['license']['id']
            fields['meta_license_name'].initial = result['meta']['license']['name']
        except APIError as err:
            messages.error(self.request, err)
        except Exception as err:
            messages.error(self.request, "Unknown Error {}".format(err))
        return form
    def form_valid(self, form):
        data = form.cleaned_data
        urlpath = '/banks/{}/products/{}'.format(data["bank_id"], data["product_code"])
        payload = {
            #"product_code": data["product_code"],
            "parent_product_code": data["parent_product_code"],
            #"bank_id": data["bank_id"],
            "name": data["name"],
            "more_info_url": data["more_info_url"],
            "terms_and_conditions_url": data["terms_and_conditions_url"],
            "description": data["description"],
            "meta": {
                "license": {
                    "id": "PDDL",
                    "name": data["meta_license_name"] if data["meta_license_name"]!="" else "license name"
                }
            },
        }
        try:
            result = self.api.put(urlpath, payload=payload)
            print(result, "This is result")
            if 'code' in result and result['code']>=400:
                error_once_only(self.request, result['message'])
                return super(UpdateProductView, self).form_invalid(form)
        except APIError as err:
            messages.error(self.request, err)
            return super(UpdateProductView, self).form_invalid(form)
        except Exception as err:
            messages.error(self.request, err)
            return super(UpdateProductView, self).form_invalid(form)
        msg = 'Product {} for Bank {} has been Update successfully!'.format(  # noqa
            data["product_code"], data["bank_id"])
        messages.success(self.request, msg)
        return super(UpdateProductView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(UpdateProductView, self).get_context_data(**kwargs)
        self.bank_id = self.kwargs['bank_id']
        self.branch_id = self.kwargs['product_code']
        context.update({
            'product_code': self.product_code,
            'bank_id': self.bank_id
        })
        return context

@exception_handle
@csrf_exempt
def create_list(request):
    print(request.POST, "createProductList listt")
    return HttpResponse("
View 1
")


import os
import sys
import argparse
import glob
import pprint
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

import screed
import sourmash
from sourmash.sourmash_args import load_file_as_signatures
import sourmash.fig

def try_reading_csv(groups_file):
    # autodetect format
    if '.tsv' in groups_file or '.csv' in groups_file:
        separator = '\t'
        if '.csv' in groups_file:
            separator = ','
        try:
            samples = pd.read_csv(groups_file, dtype=str, sep=separator)
        except Exception as e:
            sys.stderr.write(f"\n\tError: {groups_file} file is not properly formatted. Please fix.\n\n")
            print(e)
    elif '.xls' in groups_file:
        try:
            samples = pd.read_excel(groups_file, dtype=str, sep=separator)
        except Exception as e:
            sys.stderr.write(f"\n\tError: {groups_file} file is not properly formatted. Please fix.\n\n")
            print(e)
    return samples

def get_anchor_containment(acclist, siglist):
    # return accession, anchor containment as list of tuples
    contain_tuples = []
    anchor_sig = siglist[0]
    for acc, sig in zip (acclist, siglist):
        containment = anchor_sig.contained_by(sig)
        contain_tuples.append((acc, containment))
    return contain_tuples

def get_anchor_jaccard(acclist, siglist):
    # return accession, anchor containment as list of tuples
    jaccard_tuples = []
    anchor_sig = siglist[0]
    for acc, sig in zip (acclist, siglist):
        jaccard_dist = anchor_sig.jaccard(sig)
        jaccard_tuples.append((acc, jaccard_dist))
    return jaccard_tuples

def assess_jaccard_and_containment(groupD, acc2sig, ksize, alphabet, sigdir="", abund=False): # provide options for choosing to use abund or not? (compute either one OR both, as is done now)
    anchorJaccard,anchorContainment = {},{}
    for group, accInfo in groupD.items():
        steps_to_common_ancestor = {"species": 0, "genus": 1, "family": 2, "order": 3, "class": 4, "phylum": 5, "superkingdom": 6}
        siglist = [""]*7
        acclist = [""]*7
        for rank, acc in accInfo.items():
            idx = steps_to_common_ancestor[rank]
            # load signatures
            sigfile = os.path.join(sigdir, f"{acc}.sig")
            sig =  list(load_file_as_signatures(sigfile, ksize=int(ksize), select_moltype=alphabet))
            siglist[idx] = sig[0]
            acclist[idx] = acc
        # assess jaccard and containment from anchor
        anchorContainment[group] = get_anchor_containment(acclist,siglist)
        anchorJaccard[group] = get_anchor_jaccard(acclist,siglist)
    return anchorContainment, anchorJaccard

def plot_all_distances(anchorDist, dist_csv, dist_long_csv, dist_plot=None, containment=False):
    # given all the distances generated from each path, boxplot the distances!
    # groupDF['steps_to_common_ancestor'] = groupDF["rank"].map(steps_to_common_ancestor)
    # for now, just turn into pandas DF and box plot
    # first, build distance dataframe as before (to keep plotting the same for now)
    distance_only = {}
    for key, val in anchorDist.items():
        #key = path number; val = list of tuples [(acc, anchordist), (acc,anchordist)]
        distance_only[key] = [v[1] for v in val]
    rank_order= ["anchor_species", "genus", "family", "order", "class", "phylum", "superkingdom"]
    # this imports with path as the rownames.
    distDF = pd.DataFrame.from_dict(distance_only, orient="index", columns= rank_order)

    distDF.to_csv(dist_csv, index_label="evolpath")

    if dist_plot:
        sns.set_style("white")
        plt.figure(figsize=(11,7))
        plt.xticks(fontsize=14)
        plt.yticks(fontsize=12)
        distDF.drop(columns=["anchor_species"], inplace=True)
        g=sns.boxenplot(data=distDF, palette="GnBu_d")
        g.set(yscale="log")
        #sns.stripplot(data=distDF,color='black',alpha=0.7) #, size=8)
        plt.xlabel("Common Ancestor", size=18, labelpad=20)
        if containment:
            plt.ylabel("Containment", size=20, labelpad=15)
        else:
            plt.ylabel("Jaccard", size=20, labelpad=15)
        plt.savefig(dist_plot)
        sys.stderr.write(f"wrote plot to {dist_plot}\n")

    ## now, build long form data
    distanceTest = pd.DataFrame.from_dict(anchorDist, orient="index", columns= rank_order)
    distanceTest = distanceTest.reset_index() # make index (path name) a column
    distanceTest = distanceTest.rename(columns={"index": "path"})
    distanceMelt = pd.melt(distanceTest, id_vars=["path"], value_vars=["anchor_species", "genus", "family", "order", "class", "phylum", "superkingdom"], var_name='rank')
    if containment:
        distanceMelt[['accession', 'containment']] = pd.DataFrame(distanceMelt['value'].tolist(), index=distanceMelt.index)
    else:
        distanceMelt[['accession', 'jaccard']] = pd.DataFrame(distanceMelt['value'].tolist(), index=distanceMelt.index)
    distanceMelt.drop("value", axis=1, inplace=True)
    distanceMelt['rank'] = pd.Categorical(distanceMelt['rank'], rank_order)
    distanceMelt.sort_values(by=["path","rank"], inplace=True)
    distanceMelt.to_csv(dist_long_csv, index=False)
    sys.stderr.write(f"wrote long distance csv to {dist_long_csv}\n")

def main(args):
    # from query csv, build dictionary of group:: filenames
    lineages = pd.read_csv(args.lineages_csv, dtype=str, sep=",", header=0)
    pathinfo = pd.read_csv(args.paths_csv, dtype=str, sep="\t", header=0)
    pathinfo = pathinfo.merge(lineages, on="accession")
    group2acc = (pathinfo.groupby('path').apply(lambda x: dict(zip(x['rank'],x["accession"]))).to_dict())

    #load signames from file at once
    #all_sigfiles = sourmash.sourmash_args.load_file_list_of_signatures(args.from_file)
    #acc2sig = {}
    #for fn in all_sigfiles:
    #    acc = os.path.basename(fn).rsplit(".sig")[0]
    #    sig = load_file_as_signatures(fn, ksize=args.ksize, select_moltype=args.alphabet)
    #    acc2sig[acc] = sig

    # assess similarity
    contain, jaccard = assess_jaccard_and_containment(group2acc, pathinfo, args.ksize, args.alphabet, sigdir=args.sigdir, abund=False)
    # build dataframe and plot
    plot_all_distances(jaccard, args.anchor_jaccard_csv, args.anchor_jaccard_long_csv, dist_plot=args.anchor_jaccard_plot)
    plot_all_distances(contain, args.anchor_containment_csv, args.anchor_containment_long_csv, dist_plot=args.anchor_containment_plot, containment=True)

def cmdline(sys_args):
    "Command line entry point w/argparse action."
    p = argparse.ArgumentParser()
    p.add_argument("paths_csv")
    p.add_argument("--from-file", required=True)
    p.add_argument("--sigdir", default="")
    p.add_argument("--alphabet", default="protein")
    p.add_argument("--ksize", default=11)
    p.add_argument("--lineages-csv", required=True)
    p.add_argument("--signature-name-column", default="filename", help="column with signature names in the sbt. By default, this should be the fasta file basename")
    p.add_argument("--anchor-jaccard-csv")
    p.add_argument("--anchor-jaccard-long-csv")
    p.add_argument("--anchor-containment-csv")
    p.add_argument("--anchor-containment-long-csv")
    p.add_argument("--anchor-jaccard-plot")
    p.add_argument("--anchor-containment-plot")
    args = p.parse_args()
    return main(args)

if __name__ == '__main__':
    returncode = cmdline(sys.argv[1:])
    sys.exit(returncode)


import sublime
import os
import json
import platform
import sys

if sys.version_info < (3,):
    from printer import p
else:
    from .printer import p

def get_project_settings(filename):
    """
    Returns a settings map that contains project settings
    either from a .deprc file or the plugin's sublime-settings file
    """
    project_settings_path = os.path.join(find_base_path(filename), '.deprc')

    settings = {}

    if os.path.exists(project_settings_path):
        p('Using found .deprc within: ', project_settings_path)
        json_data = open(project_settings_path)
        data = json.load(json_data)
        json_data.close()

        settings = get_settings_from_source(data)
    else:
        p('Using default settings instead')

        settings = get_settings_from_source(sublime.load_settings('Dependents.sublime-settings'))

    return settings

def get_settings_from_source(source):
    settings = {}

    settings['node_path'] = source.get('node_path', '')

    # Users shouldn't need to worry about the leading colon (if necessary #124)
    if settings['node_path'] and not settings['node_path'].startswith(':') and platform.system() != 'Windows':
        settings['node_path'] = ':' + settings['node_path']

    return settings

def find_base_path(filename):
    """
    Finds the most relevant folder to the currently active filename
    to serve as the base path of the current
    """
    starting_dir = os.path.dirname(filename)
    p('starting search for .deprc in: ', starting_dir)

    folder_with_deprc = get_folder_with_deprc(starting_dir)
    p('Closest folder with a deprc:', folder_with_deprc)

    return folder_with_deprc

def get_folder_with_deprc(baseFolder):
    if not baseFolder or baseFolder == '/':
        return ''

    deprc_file = os.path.join(baseFolder, '.deprc')

    if os.path.exists(deprc_file):
        return baseFolder
    else:
        return get_folder_with_deprc(os.path.dirname(baseFolder))


import json
import requests

searches = []

with open (r"C:\Users\Anup\OneDrive\Chatshoppie_dev\ebay_search_test.txt", "r") as searchfile:

    searches = searchfile.readlines()

for item in searches:
    search = item.split(',')[0]

    url=('http://svcs.ebay.com/services/search/FindingService/v1\
?SECURITY-APPNAME=Automati-Askmanyq-PRD-f43ce4284-a0bada92\
&OPERATION-NAME=findItemsByKeywords\
&SERVICE-VERSION=1.0.0\
&RESPONSE-DATA-FORMAT=JSON\
&REST-PAYLOAD\
&GLOBAL-ID=EBAY-AU\
&affiliate.trackingId=5338413822\
&affiliate.networkId=9\
&affiliate.customId=CS0714\
&itemFilter(0).name=Condition\
&itemFilter(0).value=New\
&itemFilter(1).paramName=Currency\
&itemFilter(1).paramValue=AUD\
&itemFilter(2).name=MinPrice\
&itemFilter(2).value=0\
&itemFilter(3).name=MaxPrice\
&itemFilter(3).value=50\
&apectFilter.aspectName=Buying Format\
&aspectFilter.aspectValueName=Buy It Now\
&aspectFilter.aspectName=Style\
&aspectFilter.aspectValueName=Trench Coat\
&aspectFilter.aspectName=Colour\
&aspectFilter.aspectValueName=Red\
&keywords='+ item)

    url=url.replace(" ","%20")

    apiResult = requests.get(url)
    parsedoc = apiResult.json()

    #print (parsedoc)

    for item in (parsedoc["findItemsByKeywordsResponse"][0]["searchResult"][0]["item"]):
        Productname=item["title"][0]
        Producturl=item["viewItemURL"][0]
        Imageurl=item["galleryURL"][0]

        condition=item['condition'][0]['conditionDisplayName'][0]
        price=item['sellingStatus'][0]["convertedCurrentPrice"][0]['__value__']

        print(Productname +" "+Producturl +" "+Imageurl+price+" "+condition)

  #print(parsedoc)


from array import array
from npipes.consts import *
from npipes.pipes import Pipes
from npipes.message import Message

class IPC(Pipes):
    def __init__(self, pipe_name=IPC_FIFO_NAME):
        super().__init__()
        self.__msg_parser = Message()
        self.__msg = ''
        self.pipe = pipe_name

    @property
    def message(self):
        return self.__msg

    @staticmethod
    def __calc_crc(msg, polynomial=0xEDB88320):
        # Ref: https://stackoverflow.com/questions/41564890/crc32-calculation-in-python-without-using-libraries
        table = array('L')
        for byte in range(256):
            crc = 0
            for bit in range(8):
                if (byte ^ crc) & 1:
                    crc = (crc >> 1) ^ polynomial
                else:
                    crc >>= 1
                byte >>= 1
            table.append(crc)
        value = 0xffffffff
        for ch in msg:
            value = table[(ord(ch) ^ value) & 0xff] ^ (value >> 8)
        return (-1 - value) & 0xffffffff

    def send_message(self, data: str, crc: bool = False) -> bool:
        try:
            crc32 = 0
            if crc:
                crc32 = IPC.__calc_crc(data)
            self.__msg_parser.data = data
            self.__msg_parser.crc32 = crc32
            self.__msg_parser.encapsulate_msg()
            self.__msg = self.__msg_parser.message
            self.write(self.__msg)
            return True
        except Exception as err:
            print(err)
            return False

    def receive_message(self, crc: bool = False) -> str:
        try:
            msg = self.read()
            self.__msg_parser.read_msg(msg)
            data = self.__msg_parser.data
            recv_crc32 = self.__msg_parser.crc32
            crc32 = IPC.__calc_crc(data)
            if crc:
                if recv_crc32 == crc32:
                    self.__msg = data
                    return data
                else:
                    print('Failed to validate the CRC')
                    print(f'Received CRC: {recv_crc32}')
                    print(f'Calculated CRC: {crc32}')
                    self.__msg = ''
                    return ''
            else:
                self.__msg = data
                return data
        except Exception as err:
            print(err)
            return ''


# main.py
#/usr/bin/python3

import sys
import argparse
import datetime
import jsonpickle
import os
#from pathlib import Path

class StatusReport():
    def __init__(self):
        self.monday_date : datetime
        self.messages = [ "", "", "", "", ""]
        self.next_week_goal = ""
        self.need_help = ""

    def overwrite(self, day) -> bool:
        n : str = ""
        if self.messages[day] != "":
            n = input("Already filled: " + self.messages[day] + "\nErase? (y/n)")
        else: return True

        n = n.lower().strip()
        if n == "y" or n == "yes":
            return True
        return False

    def setMondayDate(self, date) -> None:
        self.monday_date = date

    def recordMessage(self, day):
        assert(day < 8 and day > -1)

        if self.overwrite(day):
            self.messages[day] = input("enter reflection: ")
        else:
            return

    def recordMessage(self, day, message = "") -> None:
        assert(day < 8 and day > -1)
        print (message)
        if self.overwrite(day):
            if message != "":
                self.messages[day] = message
            else:
                self.messages[day] = input("enter reflection: ")
            print("message written" + message)
        else:
            print("message not written")
            return

    def isFinished(self) -> bool:
        return self.next_week_goal != "" and self.need_help != ""

    def setNextWeekGoal(self) -> None:
        n : str = ""
        if self.next_week_goal != "":
            n = input("Already filled! Erase? (y/n)")
        if n == "y" or n == "Y" or n == "":
            self.next_week_goal = input("enter next step: ")
        else:
            return

    def setNeedHelp(self) -> None:
        n : str = ""
        if self.need_help != "":
            n = input("Already filled! Erase need help? (y/n)")
        if n == "y" or n == "Y" or n == "":
            self.need_help = input("enter need help: ")
        else:
            return

    def generate(self) -> str:
            return """Launchie Weekly Report
Name: Eric Sims
Date of Monday: """ + self.monday_date.strftime("%d %B %Y") + """

What I've learned from specific activities and accomplishments throughout the week
Monday: """ + self.messages[0] + """
Tuesday: """ + self.messages[1] + """
Wednesday: """ + self.messages[2] + """
Thursday: """ + self.messages[3] + """
Friday: """ + self.messages[4] + """

Planned accomplishments for next week: """ + self.next_week_goal + """

Requested assistance: """ + self.need_help

# Command line arguments
arg_parser = argparse.ArgumentParser(description="Filling out the Weekly Status Report")
arg_parser.add_argument("-m", "--message", type=str, help="Reflect on the day")
arg_parser.add_argument("-e", "--email", action= 'store_true', help="Email to supervisor")
arg_parser.add_argument("-g", "--goal", help="Goal for next week")
arg_parser.add_argument("-n", "--need", help="Requesting help")
arg_parser.add_argument("-d", "--display", action= 'store_true', help="Display report")

group = arg_parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")

args = arg_parser.parse_args()

def prompt():
    print( """************** Launchie Report Filler ****************
    w - Reflections for today's work
    p - Reflections for another weekday
    f - Finish the document
    e - Email report to supervisor
    h - help
    q - quit (auto saves)
******************************************************""")

verbose : bool = args.verbose
quiet : bool  = args.quiet
cmd : str = ""

currentDate = datetime.datetime.now()
weekday = currentDate.weekday()
mondayDate = datetime.datetime.now() - datetime.timedelta(days=currentDate.weekday())
filePathJSON = "data/report_" + mondayDate.strftime("%m_%d_%Y") + ".json"
filePathTXT = "reports/report_" + mondayDate.strftime("%m_%d_%Y") + ".txt"

print("filePath: " + filePathJSON) if verbose else None

# Check if report is already created
if (os.path.isfile(filePathJSON)):
    with open(filePathJSON, "r") as f:
        report = jsonpickle.decode(f.read())
else:
    # no previous report made, make a new one
    report = StatusReport()
    report.setMondayDate(mondayDate)

if args.message != None:
    report.recordMessage(weekday, str(args.message))
    print("Recorded message for " + currentDate.strftime("%A"))
elif args.display:
    print(report.generate())
elif args.goal != None:
    report.nextweek_goal = args.goal
    print("Set next week goal: " + report.next_week_goal) if verbose else None
elif args.need != None:
    report.need_help = args.need
    print("Set need help: " + report.need_help) if verbose else None
else:
    # INPUT EVENT LOOP
    prompt() if not quiet else None
    while (1):
        cmd = input(">>> ")
        if cmd == 'q': break

        elif cmd == 'w':
            print ("response for today") if verbose else None
            report.recordMessage(weekday)
            print(report.messages) if verbose else None

        elif cmd == 'p':
            print("response for previous days") if verbose else None
            custom_day = int(input("0 - Monday\t1 - Tuesday\t2 - Wednesday\t3 - Thursday\t4 - Friday\nday: "))
            report.recordMessage(custom_day)
            print(report.messages) if verbose else None

        elif cmd == 'e':
            print("emailing supervisor") if verbose else None
            txt = report.generate()
            with open(filePathTXT, "w") as f:
                f.write(txt)

        elif cmd == 'f':
            print("finishing documentation") if verbose else None
            report.setNextWeekGoal()
            report.setNeedHelp()

        elif cmd == 'h' or cmd == 'help':
            prompt()

        else:
            print("Invalid command")

# Save report
frozen = jsonpickle.encode(report)

with open(filePathJSON, "w") as f:
    f.write(frozen)


#!/usr/bin/python

in_file_path = "../CHR_combined.imputed.painted.raw.txt"
out_file_path = "../CHR_combined.imputed.painted.raw.txt.mapfile"

in_file = open(in_file_path,"r")
out_file = open(out_file_path,"w")

num = 0
prev_chrom = ""

first = True
for line in in_file:
    if line[0] == "*":
        frags = line.split("\t")
        name = frags[0]
        chrom = name.strip("*").split(".")[0]
        if prev_chrom != chrom:
            num+=1
            prev_chrom = chrom
        if first:
            first = False
        else:
            out_file.write("\n")
        out_file.write("{}\t{}".format(num,name))

in_file.close()
out_file.close()


import numpy as np
from scipy.special import gamma as spgamma
from scipy.special import digamma as spdigamma
from scipy.special import polygamma as sppolygamma
from scipy.special import bernoulli as spbernoulli
from scipy.optimize import root as sproot
from wormbrain.match import pairwise_distance
import mistofrutta as mf
import multiprocessing as mp
import time
import matplotlib.pyplot as plt

def _neighborhood(pwise_dist,cutoff=2.):
    D = pwise_dist
    mediana = np.average(np.sort(D,axis=0)[1,:])
    neighborWeights = np.where(D<cutoff*mediana,1.,0.)
    neighborN = np.sum(neighborWeights,axis=0)

    return neighborN, neighborWeights

def _studt(sigma2,Gamma,D,pwise_dist):
    A = spgamma(0.5*(Gamma+D))
    B = np.sqrt(2.*sigma2) * (Gamma+spgamma(0.5))**(0.5*D) * spgamma(0.5*Gamma)
    C = (1.0 + pwise_dist*(1./(sigma2*Gamma[:,None])))**(0.5*(D+Gamma[:,None]))

    return A[:,None]/(B[:,None]*C)

def _eqforgamma(Gamma,CDE_term):
    Gammahalves = 0.5*Gamma
    AB = -spdigamma(Gammahalves) + np.log(Gammahalves)

    out = np.empty_like(Gammahalves)
    mf.approx.trigamma(Gammahalves,len(Gammahalves),out,9)
    jac = -0.5*out + 1./Gamma
    Jac = np.diag(jac)

    return (1.+AB+CDE_term), Jac

def _eqforalpha(alpha,p,sumPoverN):
    #Eq. (20)', switching indices for x and y to match their first paper.
    #Added an alpha to the first term inside the \biggl ( in the final line!!
    #Is it reasonable to use an approximate form for the exponential?
    #print(alpha*np.max(sumPoverN)) #around 0.2 max
    #print(alpha*np.min(sumPoverN)) # 0 (always positive, right?)
    A_term = np.empty_like(sumPoverN)
    mf.approx.exp(alpha*sumPoverN,np.prod(sumPoverN.shape),A_term,0)
    #A_term = np.exp(alpha*sumPoverN) # TODO approximate this exponential
    B_term = np.sum(sumPoverN * A_term,axis=1) #There's an alpha here or not???
    C_term = np.sum(A_term,axis=1)
    D_term = -B_term/C_term

    return np.sum(p*(alpha*sumPoverN+D_term[:,None]))

var_dict = {}

def init_worker(A_rawarr, B_rawarr, kwargs):
    for key in kwargs:
        var_dict[key] = kwargs[key]
    var_dict['A_rawarr'] = A_rawarr
    var_dict['B_rawarr'] = B_rawarr

def dsmm(A,B,**kwargs):
    # If multiple A pointsets are passed, run the registrations in parallel
    if len(A.shape)==3:
        print("parallel")
        if __name__ == '__main__':
            cores = kwargs.pop('cores')
            A_rawarr = mp.RawArray('d', int(np.prod(A.shape)))
            A_rawarr_np = np.frombuffer(A_rawarr, dtype=np.float64).reshape(A.shape)
            np.copyto(A_rawarr_np, A)
            B_rawarr = mp.RawArray('d', int(np.prod(B.shape)))
            B_rawarr_np = np.frombuffer(B_rawarr, dtype=np.float64).reshape(B.shape)
            np.copyto(B_rawarr_np, B)

            kwargs['Ashape'] = A.shape
            kwargs['Bshape'] = B.shape

            pool = mp.Pool(processes=cores, initializer=init_worker,
                                initargs=(A_rawarr, B_rawarr, kwargs))

            P = pool.map(_dsmm_parallel_wrapper, range(A.shape[0]))
            pool.close()
            pool.join()
            return P

    elif len(A.shape)==2:
        return _dsmm(A,B,**kwargs)

def _dsmm_parallel_wrapper(i):
    #a = np.prod(var_dict['Ashape'][1:])
    #A = np.frombuffer(var_dict['A_rawarr'])[a*i:a*(i+1)].reshape(var_dict['Ashape'])
    #B = np.frombuffer(var_dict['B_rawarr']).reshape(var_dict['Bshape'])

    A = i[0]
    BB = i[1]
    for B in BB:
        _dsmm(A, B,returnOnlyP=True)
    #print(i)

    print("Hi")
    return _dsmm(A, B,returnOnlyP=True)

def _dsmm(Y,X,returnOnlyP=False,beta=2.0,llambda=1.5,neighbor_cutoff=10.0,
            conv_epsilon=1e-3,eq_tol=1e-2,Ashape=(1,1),Bshape=(1,1)):
    # This is the version running with a single pair of pointsets.
    # This registers Y onto X (Y is what is changed)
    # In wormb.match, I always pass A,B, where B is the refBrain.
    tm1 = time.time()
    #Y = np.copy(A)
    #X = np.copy(B)

    # Preprocess ("normalize" in Vemuri's language)

    X -= np.average(X,axis=0)
    X /= np.max(np.absolute(X),axis=0)
    X += np.min(X,axis=0)
    Y -= np.average(Y,axis=0)
    Y /= np.max(np.absolute(Y),axis=0)
    Y += np.min(Y,axis=0)

    N = X.shape[0]
    M = Y.shape[0]
    D = X.shape[1]

    # Init parameters
    pwise_dist = pairwise_distance(Y,X,squared=True)
    mf.approx.pwise_dist2(Y,X,M,N,D,pwise_dist)
    beta2 = beta**2
    w = np.ones((M,N))*(1./M/N)
    Gamma = np.ones(M)*3.
    Gamma_old = np.copy(Gamma)
    sigma2 = np.sum(pwise_dist)/(D*M*N)
    pwise_distYY = pairwise_distance(Y,Y,squared=True)
    G = np.exp(-pwise_distYY/(2.*beta2))
    Identity = np.diag(np.ones(M))

    # Allocate arrays
    F_t = np.empty((M,N))
    wF_t = np.empty((M,N))
    wF_t_sum = np.empty(N)
    p = np.empty((M,N))
    u = np.empty((M,N))
    #D_term = np.empty(M)
    CDE_term = np.empty(M)
    hatP = np.empty((M,N))
    hatPI = np.empty((M,M))
    hatPI_diag = np.empty(M)
    hatPIG = np.empty((M,M))
    hatPX = np.empty((M,D))
    hatPIY = np.empty((M,D))
    W = np.empty((M,D))
    GW = np.empty((M,D))

    #Additional stuff from new paper
    sumPoverN = np.zeros((M,N))
    expAlphaSumPoverN = np.zeros((M,N))
    alpha=1.
    #neighborN, neighborWeights = _neighborhood(pwise_distYY,neighbor_cutoff)

    # Convergence
    relerr = 1000.
    regerror = np.sum(pairwise_distance(X,Y,squared=True))

    tm2 = time.time()
    i = 0
    ##### array[y,x]
    while relerr > conv_epsilon:
        t0 = time.time()
        #Step3 (Eq. (5))
        #F_t[:] = _studt(sigma2,Gamma,D,pwise_dist)

        mf.approx.studt(pwise_dist,M,N,sigma2,Gamma,D,F_t)

        #Step3:E-Step
        #Eq. (17)'
        '''
        wF_t = w*F_t
        p[:] = wF_t / np.sum(wF_t,axis=0)[None,:]
        '''
        mf.approx.step1(w,F_t,wF_t,wF_t_sum,p,M,N)
        #p = np.absolute(p)

        #Eq. (16) and (21)'
        '''
        u[:] = (Gamma[:,None] + D) / (Gamma[:,None] + pwise_dist/sigma2)
        '''
        mf.approx.step2(u,Gamma,pwise_dist,sigma2,M,N,D)
        #u = np.absolute(u)

        t1 = time.time()
        #Eq. (20)'
        #neighborN, neighborWeights = _neighborhood(pwise_distYY,neighbor_cutoff)
        #sumPoverN = np.sum(neighborWeights[...,None]*p[None,...],axis=1)/neighborN[:,None] ##############TODO this takes a long time! 2.5 ms! It's because I augment the dimensionality so much.
        t2b = time.time()
        mf.approx.getsumPoverN(pwise_distYY, N, N, neighbor_cutoff, p, sumPoverN) #yay!
        t2c = time.time()

        #Result = sproot(_eqforalpha,x0=alpha,args=(p,sumPoverN),method="hybr",tol=eq_tol)
        #alpha = Result['x'][0]
        Alpha = np.array([alpha])
        mf.approx.solve_foralpha(p,M,N,sumPoverN,Alpha)
        alpha = Alpha[0]

        t2 = time.time()
        #Step4:M-Step
        # Eq. (18)'
        '''
        expAlphaSumPoverN = np.exp(alpha*sumPoverN)
        w[:] = expAlphaSumPoverN*(1./np.sum(expAlphaSumPoverN,axis=0)[None,:])
        '''
        mf.approx.step3(sumPoverN,expAlphaSumPoverN,w,alpha,M,N)
        t3 = time.time()

        #Eq. (23)
        #Gamma_old = np.copy(Gamma)
        # C,D,E_terms were terms in the function _eqforgamma, but since they
        # never change, I calculate them just once out here.
        '''
        Gammaoldpdhalves = np.absolute(0.5*(Gamma_old+D))
        C_term = np.sum(p*(np.log(u)-u),axis=1)/np.sum(p,axis=1)
        D_term = spdigamma(Gammaoldpdhalves)
        E_term = -np.log(Gammaoldpdhalves)
        CDE_term = C_term + D_term + E_term'''
        mf.approx.step4(Gamma,p,u,CDE_term,M,N,D)
        #print("CDE_term")
        #print(CDE_term[0:4])
        #Result = sproot(_eqforgamma,x0=Gamma_old,args=(CDE_term),method="hybr",tol=eq_tol,jac=True,options={'col_deriv':1})
        #Gamma = Result['x']
        mf.approx.solve_forgamma(CDE_term,len(CDE_term),Gamma)

        t4 = time.time()
        #Eq. (26)
        # TODO hatP[:] = p*u
        #hatPI[:] = np.diag(np.dot(hatP,np.ones(N)))
        #TODO hatPI_diag[:] = np.sum(hatP,axis=1)
        #hatPI[:] = np.diag(hatPI_diag)
        #TODO G[:] = np.exp(-0.5/beta2*pwise_distYY) #TODO ????.T
        t4d = time.time()
        #hatPIG[:] = np.dot(hatPI,G)
        #mf.approx.mfdot(hatPI,G,hatPI.shape[0],hatPI.shape[1],G.shape[1],hatPIG,True) #TODO recheck times. TODO the first matrix is diagonal: the dot product is simpler!
        #mf.approx.mfdot_diag(hatPI_diag,G,G.shape[0],G.shape[1],hatPIG,True)
        #TODO hatPIG[:] = hatPI_diag[:,None]*G

        #mf.approx.mfdot(hatP,X,hatP.shape[0],hatP.shape[1],X.shape[1],hatPX,True)
        #hatPIY[:] = np.dot(hatPI,Y)
        #mf.approx.mfdot(hatPI,Y,hatPI.shape[0],hatPI.shape[1],Y.shape[1],hatPIY,True)
        #TODO hatPIY = hatPI_diag[:,None]*Y
        mf.approx.step5(p,u,hatP,hatPI_diag,hatPIG,hatPIY,hatPX,G,W,GW,X,Y,pwise_dist,pwise_distYY,beta2,llambda,sigma2,M,N,D)
        #print("W ")
        #print(W[0])
        #print("Y ")
        #print(Y[0])

        #hatPX[:] = np.dot(hatP,X)
        t4e = time.time()
        #TODO A = np.linalg.inv(hatPIG)#TODO FIXME +llambda*sigma2*Identity)
        t4f = time.time()
        #TODO B = hatPX - hatPIY
        #TODO W = np.dot(A,B)
        #mf.approx.mfdot(A,B,A.shape[0],A.shape[1],B.shape[1],W,True)
        #Step5 moved here to optimize
        #mf.approx.mfdot(G,W,G.shape[0],G.shape[1],W.shape[1],GW,True)
        #Y += GW
        #TODO Y += np.dot(G,W)
        t4b = time.time()
        #pwise_dist = pairwise_distance(Y,X,squared=True)
        #pwise_distYY = pairwise_distance(Y,Y,squared=True)
        #TODO mf.approx.pwise_dist2(Y,X,M,N,D,pwise_dist)
        #TODO mf.approx.pwise_dist2_same(Y,M,D,pwise_distYY)
        t4c = time.time()
        t5 = time.time()
        #Back to Step4
        #Eq. (27)
        AA = np.sum(hatP*pwise_dist)#np.sum((X[None,:]-Y[:,None])**2,axis=-1))
        BB = D*np.sum(hatP) # or just p as in the old paper?
        sigma2 = AA*(1./BB)
        #print(sigma2)
        t5b = time.time()
        #Step6
        regerror_old = regerror
        regerror = np.sum(pwise_dist)
        relerr = np.absolute((regerror-regerror_old)/regerror_old)

        '''if i==1:
            print("py")
            print("F_t")
            print(F_t[0,0:4])
            print("p")
            print(p[0,0:4])
            print("u")
            print(u[0,0:4])
            print("sumPoverN")
            print(sumPoverN[0,0:4])
            print("alpha")
            print(alpha)
            print("CDE")
            print(CDE_term[0:4])
            print("gamma")
            print(Gamma[0:4])
            print("Y")
            print(Y[0])
            print("sigma2")
            print(sigma2)
            #quit()'''

        i += 1
        if i>1: t6bis = t6
        t6 = time.time()

    print("t-2-t-1",tm2-tm1)
    print("t1-t0",t1-t0)
    print("t2b-t1",t2b-t1)
    print("t2c-t2b",t2c-t2b)
    print("t2-t2c",t2-t2c)
    print("t2-t1",t2-t1)
    print("t3-t2",t3-t2)
    print("t4-t3",t4-t3)
    print("t4c-t4b",t4c-t4b)
    print("t4d-t4",t4d-t4)
    print("t4e-t4d",t4e-t4d)
    print("t4f-t4e",t4f-t4e)
    print("t5-t4",t5-t4)
    print("t5b-t5",t5b-t5)
    print("t6-t5",t6-t5)
    print("t6-t0",t6-t0)
    try:
        print("t0-t6b",t0-t6bis)
    except:
        pass
    print("i",i)
    print("ratio",(t2-t1+t4-t3)/(t6-t0))

    if returnOnlyP:
        return p
    else:
        return Y, X, p


import numpy as np
import os
import numpy as np
import random
import glob
import json
import random
from speechblender import *
import glob
import shutil

text_list  = '/alt/asr/yelkheir/kaldi/egs/arab_align_gop/s5/data/text'
text = {}
with open(text_list) as r:
    lines = r.readlines()
    for line in lines:
        id, char = line.split('\t')[0], line.split('\t')[1][:-1]
        text[id] = char

for elem in ['speechblender/augmented_50', 'speechblender/augmented_50_0']:
    print(elem)
    dir = elem.split('/')[1]
    if not os.path.isdir(dir):
        os.mkdir(dir)
    with open(dir+'/wav.scp','w') as w1, open(dir+'/text','w') as w2, open(dir+'/utt2spk','w') as w3:
        for subfolder in glob.glob(elem+'/*'):
            print(subfolder)
            name, loc = subfolder.split('/')[-1].split('.')[0], subfolder
            tst = text[name]
            w1.write(name)
            w1.write('\t')
            w1.write(loc)
            w1.write('\n')

            w2.write(name)
            w2.write('\t')
            w2.write(tst)
            w2.write('\n')

            w3.write(name)
            w3.write('\t')
            w3.write(name)
            w3.write('\n')


from src.color_print import *
import subprocess

def waf_check(url, output=None):
    print_info('使用wafw00f进行waf检测')
    command_str = 'wafw00f -v -a ' + url
    if output == None:
        print_info(command_str)
        command = command_str.split(' ')
        rsp = subprocess.Popen(command)
        rsp.communicate()
    else:
        command = command_str.split(' ')
        p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        p.wait()
        out = p.stdout.read().decode()
        try:
            with open(output, 'w') as fp:
                fp.write(out)
            print_info('wafw00f成功获取WAF信息')
            print_info('保存路径为 ' + color.yellow(output))
        except:
            print_error('输入的文件名错误')


from flask import Flask, render_template, Response, make_response, send_file
import audio_recognizer
import json
import video_recognizer
import os
import numpy as np

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'

app = Flask(__name__, static_folder='static')

global audio_emotion
global multi_emotion

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

@app.route('/video_feed')
def video_feed():
    return Response(video_recognizer.gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/multimodal1')
def multimodal1():
    return render_template('question.html')

@app.route('/multimodal')
def multimodal():
    return render_template('multimodal.html')

@app.route('/question2html')
def question2html():
    return render_template('question2.html')

@app.route('/questio2')
def questio2():
    return render_template('multimodal2.html')

@app.route('/live-data_multi')
def live_data_multi():
    """ This function is called from graph_multi.js that updates the multimodal diagram.
    It fetches predictions, updates global variables and returns a multimodal prediction. """
    global multi_emotion
    f = open('video_prediction.json')
    video_data = json.load(f)
    audio_data = audio_recognizer.analyze_audio()
    newdict = [{"name": "Video",
                "data": [{"name": "Angry", "value": round(video_data[0] * 100, 2)},
                         {"name": "Fearful", "value": round(video_data[1] * 100, 2)},
                         {"name": "Happy", "value": round(video_data[2] * 100, 2)},
                         {"name": "Sad", "value": round(video_data[3] * 100, 2)}]},
               {"name": "Audio",
                "data": [{"name": "Angry", "value": round(audio_data[0] * 100, 2)},
                         {"name": "Fearful", "value": round(audio_data[1] * 100, 2)},
                         {"name": "Happy", "value": round(audio_data[2] * 100, 2)},
                         {"name": "Sad", "value": round(audio_data[3] * 100, 2)}]}]
    multimodal_data = []
    video_data = np.array(video_data)
    for i in range(len(audio_data)):
        multimodal_data.append(0.7 * video_data[i] + 0.3 * audio_data[i])
    multi_emotion = multimodal_data

    response = make_response(json.dumps(newdict))
    response.content_type = 'application/json'
    return response

@app.route('/multi_emotion')
def multi_emotion():
    try:
        data = multi_emotion
    except NameError:
        data = [0.0, 0.0, 0.0, 0.0]
    print('multi array: ', data)
    if np.argmax(data) == 0:
        return send_file('static\\images\\angry.jpg', mimetype='image/jpg')
    elif np.argmax(data) == 1:
        return send_file('static\\images\\fear.jpg', mimetype='image/jpg')
    elif np.argmax(data) == 2:
        return send_file('static\\images\\happy.jpg', mimetype='image/jpg')
    elif np.argmax(data) == 3:
        return send_file('static\\images\\sad.jpg', mimetype='image/jpg')
    else:
        return send_file('static\\images\\neutral.jpg', mimetype='image/jpg')

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


import requests
import process_prov_map

import plotly.express as px
import pandas as pd
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from datetime import datetime, date
from conf import app
from conf import server

# Section 4. dropdown provinsi
section4 = html.Div([
    html.Br(),
    html.H3('Informasi Per-Provinsi'),
    dcc.Dropdown(
        id='list-provinsi',
        options=process_prov_map.list_provinsi,
        placeholder='Masukkan provinsi',
        value='Jambi',
        multi=False
    ),

    html.Div(id='output')
])

@app.callback(
    Output('output', 'children'),
    Input('list-provinsi', 'value')
)
def update_info(value):

    # fungsi card
    def card(angka, status, penambahan):
        '''
        Untuk card status kasus aktif, sembuh, meninggal dan dirawat
        '''
        card_content = [
            dbc.CardBody(
                [
                    html.H2(angka, className="card-title"),
                    html.H5(status),
                    html.P(
                        penambahan,
                        className='card-text',
                    ),
                ]
            ),
        ]
        return card_content

    # fungsi bar chart
    def get_bar(dataframe, title):
        '''
        Generate barchart data covid indonesia
        '''
        chart = px.bar(
            dataframe,
            x='key',
            y='doc_count',
            title=title,
            labels={'doc_count': '(Persen (%))'},
            color='key',
            )
        chart.update_layout(
                xaxis=dict(
                    showgrid=False
                ),
                yaxis=dict(
                    showgrid=True,
                    gridcolor='lightgrey',
                ),
                plot_bgcolor='white',
                title_x=0.5,
                showlegend=False,

                hoverlabel= dict(
                    bgcolor='white'
                )
            )
        chart.update_yaxes(fixedrange=True)
        chart.update_xaxes(fixedrange=True, title_text='')
        return chart

    def get_pie(dataframe, title):
        '''
        Generate pie chart untuk kelompok usia dan jenis kelamin
        '''
        chart = px.pie(
            dataframe,
            title=title,
            values='doc_count',
            names='key',
            hover_name='key',
            labels={
                'key': 'Key',
                'doc_count': 'Persen'
            }
        )
        chart.update_layout(
            title_x=0.5,
            hoverlabel = dict(
                bgcolor='white'
            )
        )
        return chart

    def generate_chart(kondisi, mode, title):
        '''
        Fungsi untuk generate chart
        kondisi: nama kolom pada dataframe
        mode: bar/pie
        title: chart title
        '''
        jumlah_data = data['data']['kasus'][kondisi]['current_data']
        dataframe = pd.DataFrame(data['data']['kasus'][kondisi]['list_data'])

        if mode == 'bar':
            out = get_bar(
                dataframe,
                title='%s Provinsi %s (%s data)' % (title, provinsi.title(), jumlah_data),
            )
        elif mode == 'pie':
            out = get_pie(
                dataframe,
                title='%s Provinsi %s (%s data)' % (title, provinsi.title(), jumlah_data),
            )
        else:
            print('Periksa kembali parameter mode (pie/bar)')

        return out

    def get_chart(df, x, y, title, legend_title):
        chart = px.line(
            df,
            x=x,
            y=y,
            title=title,
            labels={
                'KASUS':'Kasus Baru',
                'tanggal':'Tanggal',
                'MENINGGAL': 'Meninggal',
                'SEMBUH': 'Sembuh'
                },
            )
        chart.update_yaxes(title_text='', fixedrange=True)
        chart.update_xaxes(title_text='')
        chart.update_layout(
            xaxis=dict(
                showgrid=True,
                gridcolor='lightgrey',
                rangeslider=dict(
                    visible=True
                ),
                type='date'
            ),
            yaxis=dict(
                showgrid=True,
                gridcolor='lightgrey'
            ),
            plot_bgcolor='white',
            title_x=0.5,
            height=600,
            legend_title_text=legend_title,
            hovermode='x unified'
        )

        return chart

    if value != None:
        prov = value.upper().replace(' ', '_')
        link = 'https://data.covid19.go.id/public/api/prov_detail_' + prov + '.json'
        a = requests.get(link)
        data = a.json()

        # ubah format tanggal
        tgl = datetime.strptime(data['last_date'], '%Y-%m-%d')
        tanggal_update = tgl.strftime('%d %B %Y')

        # membuat dataframe untuk plot
        df_perprov = pd.DataFrame(data['list_perkembangan'])

        # merubah timestamp ke datetime object
        df_perprov['tanggal'] = df_perprov['tanggal'].apply(lambda x: date.fromtimestamp(x/1000))

        # jumlah data
        akhir = df_perprov.shape[0]

        # Nama provinsi
        provinsi = data['provinsi']

        # total kasus
        total_kasus = df_perprov['AKUMULASI_KASUS'][akhir-1]
        total_sembuh = df_perprov['AKUMULASI_SEMBUH'][akhir-1]
        total_meninggal = df_perprov['AKUMULASI_MENINGGAL'][akhir-1]
        total_dirawat = df_perprov['AKUMULASI_DIRAWAT_OR_ISOLASI'][akhir-1]

        # statistik provinsi
        gabungan = get_chart(
            df_perprov,
            x='tanggal',
            y=['KASUS', 'MENINGGAL', 'SEMBUH'],
            title='Perkembangan Kasus Per-Hari (Grafik Gabungan) Provinsi %s' % (provinsi),
            legend_title=''
        )

        # Gejala positif covid
        gejala_positif = generate_chart(
            kondisi='gejala',
            mode='bar',
            title='Gejala Positif Covid-19'
        )

        # Kondisi penyerta positif covid-19
        kondisi_penyerta = generate_chart(
            kondisi='kondisi_penyerta',
            mode='bar',
            title='Kondisi Penyerta Positif Covid-19'
        )

        # Kelompok umur
        kelompok_umur = generate_chart(
            kondisi='kelompok_umur',
            mode='pie',
            title='Kelompok Umur Positif Covid-19'
        )

        # Kelompok jenis kelamin
        kelompok_jk = generate_chart(
            kondisi='jenis_kelamin',
            mode='pie',
            title='Jenis Kelamin Positif Covid-19'
        )

        # Variable yang akan di return
        out = html.Div([
            html.Br(),

            # Row nama provinsi
            dbc.Row([
                html.H2(provinsi),
            ], justify='center'),

            # Row statistik provinsi
            dbc.Row([
                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_kasus, ','),
                            'TERKONFIRMASI',
                            '+%s Kasus' % (format(df_perprov['KASUS'][akhir-1], ','))
                        ), color='primary')
                ]),

                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_dirawat, ','),
                            'KASUS AKTIF',
                            '+%s Kasus Aktif' % (format(df_perprov['DIRAWAT_OR_ISOLASI'][akhir-1], ','))
                        ), color='warning')
                ]),

                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_sembuh, ','),
                            'SEMBUH',
                            '+%s Kasus Sembuh' % (format(df_perprov['SEMBUH'][akhir-1], ','))
                        ), color='success')
                ]),

                dbc.Col([
                    dbc.Card(
                        card(
                            format(total_meninggal, ','),
                            'MENINGGAL',
                            '+%s Kasus Meninggal' % (format(df_perprov['MENINGGAL'][akhir-1], ','))
                        ), color='danger')
                ]),

            ], style={'color': 'white'}),

            # Row chart provinsi
            dbc.Row([
                dbc.Col([
                    dcc.Graph(figure=gabungan, config={'displayModeBar': False})
                ])
            ]),

            # Row statistik gejala
            dbc.Row([
                dbc.Col([
                    dcc.Graph(
                        figure=gejala_positif,
                        config={'displayModeBar': False})
                ]),

                dbc.Col([
                    dcc.Graph(
                        figure=kondisi_penyerta,
                        config={'displayModeBar': False})
                ]),

            ]),

            # Row kelompok usia dan jenis kelamin
            dbc.Row([
                dbc.Col([
                    dcc.Graph(
                        figure=kelompok_umur,
                        config={'displayModeBar': False}
                    )
                ]),

                dbc.Col([
                    dcc.Graph(
                        figure=kelompok_jk,
                        config={'displayModeBar': False}
                    )
                ])

            ])

        ])

        return out


#!/usr/bin/env python
# coding=gbk
# -*- coding : utf-8 -*-
from urllib import request, parse

ua_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"}
url = "https://www.baidu.com/s"
keyword = input("ÇëÊäÈë¹Ø¼ü×Ö£º ")
wd = {"wd": keyword}
wd = parse.urlencode(wd)
full_url = url + "?" + wd
print(full_url)
req = request.Request(full_url, headers=ua_headers)
response = request.urlopen(req)
html = response.read()
print(html.decode(encoding="utf-8"))