# -*- coding: utf-8 -*-
"""
Created on Thu Apr  7 10:20:50 2016

@author: zp4wo
"""

#making some report

#step 1 : Done

#step 2 :
import pandas as pd
import numpy as np
dir_ = 'C://Users//zp4wo//Documents//HKJC_edit//memberprofile2015//output//0407//report//'
df_finalS1415_n13_all = pd.read_csv(dir_ + 'df_finalS1415_n13_all.csv', index_col=0)
#df_finalS1415_n13_all = pd.read_csv(dir_ + 'df_finalS1415_n13_all no child.csv', index_col=0)
#df_finalS1415_n10_all = pd.read_csv(dir_ + 'df_finalS1415_n10_all.csv', index_col=0)
df_finalS1415_n10_all = pd.read_csv(dir_ + 'df_finalS1415_n10_all no child.csv', index_col=0)

#select 10 1 6 7 0 5
df_select = df_finalS1415_n13_all[df_finalS1415_n13_all['cluster_no'].isin([10,1,6,7,0,5])]
df_select.loc[:, 'KeyID'] = df_select['KeyID_card_holder'].str.split('_').str[0].astype(int)
df_select.loc[:, 'card_holder_no'] = df_select['KeyID_card_holder'].str.split('_').str[1].astype(int)

dir_1 = 'C://Users//zp4wo//Documents//HKJC_edit//memberprofile2015//data_csv//'
life_stage = pd.read_excel(dir_1 + 'Card Holder Segment Label(no code).xlsx')
#life_stage_pri =  life_stage[life_stage['card_holder_no'] == 1]
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
life_stage.loc[:, 'Segment_no'] = le.fit_transform(life_stage.Segment)
life_stage['KeyID_card_holder'] = life_stage['KeyID'].dropna().astype(int).astype(str) + '_' + life_stage['card_holder_no'].dropna().astype(int).astype(str)

# labels
df_dict_segment = pd.DataFrame(le.classes_, columns=['Segment'])
df_dict_segment.loc[:, 'label'] = np.arange(0,17).astype(str)
df_dict_segment.loc[-1, :] = ['no tag', '17']
df_dict_segment.to_excel(dir_ + 'dict segment.xlsx', index=0)

df_select_labels = pd.merge(df_select, life_stage[['KeyID_card_holder','Segment_no']], on='KeyID_card_holder', how='left')

# 14 stand for no tage in life stage given by HKJC
df_select_labels['Segment_no'].fillna(17, inplace=1)
df_select_labels_drop = df_select_labels.drop(['cluster_no', 'KeyID'], axis=1)

#%%

def profile(X_pre1):
    import copy
    X_pre1_des = X_pre1.drop(['Segment_no', 'card_holder_no'], axis=1).describe().T
    #add clustering
    X_pre = copy.deepcopy(X_pre1.drop('card_holder_no', axis=1))
    X_pre_mean = X_pre.drop('KeyID_card_holder', axis=1).groupby('Segment_no').mean()
    X_pre_cluster_count = X_pre[['KeyID_card_holder','Segment_no']].groupby('Segment_no').count()
    X_pre1_pri_cluster_count = X_pre1[X_pre1['card_holder_no'] == 1][['KeyID_card_holder','Segment_no']].groupby('Segment_no').count()
    X_pre1_sup_cluster_count = X_pre1[X_pre1['card_holder_no'] != 1][['KeyID_card_holder','Segment_no']].groupby('Segment_no').count()
    X_pre_mean_table = pd.concat([X_pre_cluster_count, X_pre1_pri_cluster_count, X_pre1_sup_cluster_count, X_pre_mean],axis=1)
    X_pre_mean_table.columns = np.insert(X_pre_mean_table.columns[3:].values,0,['Segment_count', 'pri_count', 'sup_count'])
    nan_count = X_pre.replace(0,np.nan).drop(['Segment_no','KeyID_card_holder'], axis=1).isnull()
    nan_count['Segment_no'] = X_pre['Segment_no']
    Nan_count_table_cl = nan_count.groupby('Segment_no').sum().T

    X_pre_mean_table_T = X_pre_mean_table.T
    X_pre_mean_table_T.columns =  ['mean_' + x for x in [str(x) for x in [int(x) for x in list(X_pre_mean_table_T.columns)]]]
    Nan_count_table_cl.columns =  ['sum_nan_zero_' + x for x in [str(x) for x in [int(x) for x in list(Nan_count_table_cl.columns)]]]

    meantable_nan_table = pd.concat([X_pre1_des[['count', 'mean']], X_pre_mean_table_T, Nan_count_table_cl], axis=1)
    meantable_nan_table = meantable_nan_table.reindex(index=['Segment_count','pri_count', 'sup_count', 'FB_only_dates', 'Free_Rec_only_dates', 'Charge_Rec_only_dates', 'FB_and_Free_Rec_only_dates', 'FB_and_Charge_Rec_only_dates', 'Free_Rec_and_Charge_Rec_only_dates', 'FB_and_Free_Rec_and_Charge_Rec_dates', 'FB_dates', 'Total_trx_dates', 'Ratio_FB'])
    meantable_nan_table.ix[0,'count'] = meantable_nan_table.ix[0,:].sum()
    meantable_nan_table.ix[1,'count'] = meantable_nan_table.ix[1,:].sum()
    meantable_nan_table.ix[2,'count'] = meantable_nan_table.ix[2,:].sum()
    return meantable_nan_table

profile(df_select_labels_drop)
df_meantable = profile(df_select_labels)
df_meantable.to_excel(dir_ + 'df_meantable life stage 10-5 .xlsx')

#%%

def profile1(X_pre1):
    import copy
    X_pre1_des = X_pre1.drop(['cluster_no'], axis=1).describe().T
    #add clustering
    X_pre = copy.deepcopy(X_pre1.drop('card_holder_no', axis=1))
    X_pre_mean = X_pre.drop('KeyID_card_holder', axis=1).groupby('cluster_no').mean()
    X_pre_cluster_count = X_pre[['KeyID_card_holder','cluster_no']].groupby('cluster_no').count()
    X_pre1_pri_cluster_count = X_pre1[X_pre1['card_holder_no'] == 1][['KeyID_card_holder','cluster_no']].groupby('cluster_no').count()
    X_pre1_sup_cluster_count = X_pre1[X_pre1['card_holder_no'] != 1][['KeyID_card_holder','cluster_no']].groupby('cluster_no').count()
    X_pre_mean_table = pd.concat([X_pre_cluster_count, X_pre1_pri_cluster_count, X_pre1_sup_cluster_count, X_pre_mean],axis=1)
    X_pre_mean_table.columns = np.insert(X_pre_mean_table.columns[3:].values,0,['cluster_count', 'pri_count', 'sup_count'])
    nan_count = X_pre.replace(0,np.nan).drop(['cluster_no','KeyID_card_holder'], axis=1).isnull()
    nan_count['cluster_no'] = X_pre['cluster_no']
    Nan_count_table_cl = nan_count.groupby('cluster_no').sum().T

    X_pre_mean_table_T = X_pre_mean_table.T
    X_pre_mean_table_T.columns =  ['mean_' + x for x in [str(x) for x in [int(x) for x in list(X_pre_mean_table_T.columns)]]]
    Nan_count_table_cl.columns =  ['sum_nan_zero_' + x for x in [str(x) for x in [int(x) for x in list(Nan_count_table_cl.columns)]]]

    meantable_nan_table = pd.concat([X_pre1_des[['count', 'mean']], X_pre_mean_table_T, Nan_count_table_cl], axis=1)
    meantable_nan_table = meantable_nan_table.reindex(index=['cluster_count','pri_count', 'sup_count', 'FB_only_dates', 'Free_Rec_only_dates', 'Charge_Rec_only_dates', 'FB_and_Free_Rec_only_dates', 'FB_and_Charge_Rec_only_dates', 'Free_Rec_and_Charge_Rec_only_dates', 'FB_and_Free_Rec_and_Charge_Rec_dates', 'FB_dates', 'Total_trx_dates', 'Ratio_FB'], columns=['count', 'mean', 'mean_11', 'mean_3', 'mean_4', 'mean_12', 'mean_2', 'mean_8', 'mean_9', 'sum_nan_zero_11', 'sum_nan_zero_3', 'sum_nan_zero_4', 'sum_nan_zero_12', 'sum_nan_zero_2', 'sum_nan_zero_8', 'sum_nan_zero_9'])
    meantable_nan_table.ix[0,'count'] = X_pre1.shape[0]
    meantable_nan_table.ix[1,'count'] = X_pre1[X_pre1['card_holder_no'] == 1].shape[0]
    meantable_nan_table.ix[2,'count'] = X_pre1[X_pre1['card_holder_no'] != 1].shape[0]
    return meantable_nan_table

df_finalS1415_n13_all.loc[:, 'card_holder_no'] = df_finalS1415_n13_all['KeyID_card_holder'].str.split('_').str[1].astype(int)
meantable_n13 = profile1(df_finalS1415_n13_all)
meantable_n13.to_excel(dir_ + 'meantable df_finalS1415_n13_all.xlsx')

df_core = df_finalS1415_n13_all[~df_finalS1415_n13_all['cluster_no'].isin([10,1,6,7,0,5])]
meantable_n13_new = profile1(df_core)
meantable_n13_new.to_excel(dir_ + 'meantable df_finalS1415_n13_core.xlsx')

#%% step 3：
n10_base = df_finalS1415_n13_all[df_finalS1415_n13_all['cluster_no'].isin([10,1,6,7,0,5])][['KeyID_card_holder']]
n10_label = df_finalS1415_n10_all[['KeyID_card_holder', 'cluster_no']]

n10_base_with_label = pd.merge(n10_base, n10_label, on='KeyID_card_holder', how='left')

life_stage_label_n10_label = pd.merge(n10_base_with_label, life_stage[['KeyID_card_holder','Segment_no']], on='KeyID_card_holder', how='left')
crosstable = pd.pivot_table(life_stage_label_n10_label, values='KeyID_card_holder', index='cluster_no', columns='Segment_no',aggfunc=np.count_nonzero)
crosstable.to_excel(dir_ + 'crosstable n10 life stage for 10-5.xlsx')


#NAME:  cameraData.py
#AUTH:  Ryan McCartney, EEE Undergraduate, Queen's University Belfast
#DESC:  A python class for aquiring image data from network streams
#COPY:  Copyright 2018, All Rights Reserved, Ryan McCartney

import threading
import numpy as np
import cv2 as cv
import time
import imutils

#define threading wrapper
def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
        thread.start()
        return thread
    return wrapper

class cameraData:

    frameWidth = 500

    def __init__(self,stream_url,stream_name):

        #Get Stream URL
        self.stream_url = stream_url
        self.stream_name = stream_name
        #Allow opencv to capture the stream
        self.image = cv.VideoCapture(self.stream_url)

    def getFrame(self):

        #Read a single frame
        self.ret, frame = self.image.read()
        #Resize the frame
        frame = imutils.resize(frame, width=self.frameWidth)

        return frame

    def getFramerate(self):

        frames = 200
        start = time.time()

        for i in range (0,frames):

            #Get frame from stream
            frame = self.getFrame()

        #Calculate Frames Per Second
        end = time.time()
        fps = frames/(end-start)
        fps = round(fps, 3)

        return fps

    @threaded
    def streamVideo(self):

        while(1):

            #Get frame from stream
            frame = self.getFrame()

            #Show the frame
            cv.imshow('Stream of {}'.format(self.stream_name),frame)

            # quit program when 'esc' key is pressed
            if cv.waitKey(1) & 0xFF == ord('q'):
                break

        cv.destroyAllWindows()

    @threaded
    def recordVideo(self):

        #Get Date and Time
        currentDateTime = time.strftime("%d.%m.%Y-%H.%M.%S")

        # Define the codec and create VideoWriter object
        fourcc = cv.VideoWriter_fourcc(*'XVID')
        out = cv.VideoWriter('data/video/{}_{}.avi'.format(self.stream_name,currentDateTime),fourcc, 20.0, (640,480))

        while(self.image.isOpened()):
            ret, frame = self.image.read()

            if ret==True:
                #frame = cv.flip(frame,0)

                #write the flipped frame
                out.write(frame)

                cv.imshow('Recording of {}'.format(self.stream_name),frame)
                if cv.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                break

        # Release everything if job is finished
        self.image.release()
        out.release()
        cv.destroyAllWindows()


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

from os import path, getenv

def return_logging_json(ERROR_FILE_NAME):
    return {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s',
            },
            'reporter_log': {
                'format': '%(message)s',
            },
        },
        'handlers': {
            'file': {
                'level': 'ERROR',
                'formatter': 'standard',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': ERROR_FILE_NAME,
                'maxBytes': 1024 * 1024,
                'backupCount': 5,
            },

            'console': {
                'level': 'DEBUG',
                'formatter': 'standard',
                'class': 'logging.StreamHandler',
            },
        },
        'loggers': {
            'repotserver': {
                'handlers': ['file'],
                'level': 'ERROR',
                'propagate': True,
            },

            'reporter': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': True,
            },
        },
    }

def return_main_file_name(main_logfilename='common.log'):
    if getenv('SPACIAL_CONFIG'):
        pass
    return main_logfilename

class BaseConfig(object):
    # 项目跟目录
    def __init__(self):
        pass

    # logging
    LOG_DIR = 'logs'

    # logging  错误日志输出文件名
    ERROR_FILE_NAME = path.join(LOG_DIR, return_main_file_name())

    LOGING_CONFIG = return_logging_json(ERROR_FILE_NAME)

class LocalhostDevMysql(BaseConfig):
    # logging  错误日志输出文件名
    main_logfilename = 'dev_normal.log'
    ERROR_FILE_NAME = path.join(BaseConfig.LOG_DIR, main_logfilename)

    # mysql  uri
    MODE = "mysql"
    MYSQL_URL = 'mysql+mysqlconnector://root:123456@0.0.0.0:3306/goodluck_dev?charset=utf8'

class LocalhostDevRedis(BaseConfig):
    # logging  错误日志输出文件名
    main_logfilename = 'dev_common.log'
    ERROR_FILE_NAME = path.join(BaseConfig.LOG_DIR, main_logfilename)

    # redis config
    MODE = "redis"
    REDIS_HOST = '192.168.0.100'
    REDIS_PORT = 6379


from functools import cached_property
from typing import Optional

from models_library.basic_types import BootModeEnum, LogLevel
from pydantic import Field, SecretStr
from pydantic.class_validators import validator
from pydantic.networks import HttpUrl
from settings_library.base import BaseCustomSettings
from settings_library.logging_utils import MixinLoggingSettings
from settings_library.postgres import PostgresSettings

# SERVICES CLIENTS --------------------------------------------

class _UrlMixin:
    def _build_url(self, prefix: str) -> str:
        prefix = prefix.upper()
        return HttpUrl.build(
            scheme="http",
            host=getattr(self, f"{prefix}_HOST"),
            port=f"{getattr(self, f'{prefix}_PORT')}",
            path=f"/{getattr(self, f'{prefix}_VTAG')}",
        )

class WebServerSettings(BaseCustomSettings, _UrlMixin):
    WEBSERVER_HOST: str = "webserver"
    WEBSERVER_PORT: int = 8080
    WEBSERVER_VTAG: str = "v0"

    WEBSERVER_SESSION_SECRET_KEY: SecretStr
    WEBSERVER_SESSION_NAME: str = "osparc.WEBAPI_SESSION"

    @cached_property
    def base_url(self) -> str:
        return self._build_url("WEBSERVER")

# TODO: dynamically create types with minimal options?
class CatalogSettings(BaseCustomSettings, _UrlMixin):
    CATALOG_HOST: str = "catalog"
    CATALOG_PORT: int = 8000
    CATALOG_VTAG: str = "v0"

    @cached_property
    def base_url(self) -> str:
        return self._build_url("CATALOG")

class StorageSettings(BaseCustomSettings, _UrlMixin):
    STORAGE_HOST: str = "storage"
    STORAGE_PORT: int = 8080
    STORAGE_VTAG: str = "v0"

    @cached_property
    def base_url(self) -> str:
        return self._build_url("STORAGE")

class DirectorV2Settings(BaseCustomSettings, _UrlMixin):
    DIRECTOR_V2_HOST: str = "director-v2"
    DIRECTOR_V2_PORT: int = 8000
    DIRECTOR_V2_VTAG: str = "v2"

    @cached_property
    def base_url(self) -> str:
        return self._build_url("DIRECTOR")

# MAIN SETTINGS --------------------------------------------

class AppSettings(BaseCustomSettings, MixinLoggingSettings):
    # pylint: disable=no-self-use
    # pylint: disable=no-self-argument

    # DOCKER
    SC_BOOT_MODE: Optional[BootModeEnum]

    # LOGGING
    LOG_LEVEL: LogLevel = Field(
        LogLevel.INFO.value,
        env=["API_SERVER_LOGLEVEL", "LOG_LEVEL", "LOGLEVEL"],
    )

    # POSTGRES
    API_SERVER_POSTGRES: Optional[PostgresSettings]

    # SERVICES with http API
    API_SERVER_WEBSERVER: Optional[WebServerSettings]
    API_SERVER_CATALOG: Optional[CatalogSettings]
    API_SERVER_STORAGE: Optional[StorageSettings]
    API_SERVER_DIRECTOR_V2: Optional[DirectorV2Settings]

    API_SERVER_DEV_FEATURES_ENABLED: bool = Field(
        False, env=["API_SERVER_DEV_FEATURES_ENABLED", "FAKE_API_SERVER_ENABLED"]
    )

    API_SERVER_REMOTE_DEBUG_PORT: int = 3000

    @cached_property
    def debug(self) -> bool:
        """If True, debug tracebacks should be returned on errors."""
        return self.SC_BOOT_MODE in [
            BootModeEnum.DEBUG,
            BootModeEnum.DEVELOPMENT,
            BootModeEnum.LOCAL,
        ]

    @validator("LOG_LEVEL", pre=True)
    @classmethod
    def _validate_loglevel(cls, value) -> str:
        return cls.validate_log_level(value)


import os
from rest_framework import status
from .common import TestCommon
import json

class TestContacts(TestCommon):

    def setUp(self):
        super(TestContacts, self).setUp()
        self.client.credentials(HTTP_AUTHORIZATION=self.token_tenant_0)
        file = os.path.abspath('base/tests/jsons/expected_doctors.json')
        with open(file) as f:
            self.json_expected = json.load(f)
        self.json_expected = self.json_expected.get('contactInfo')

    def test_get_contact(self):
        """Validate struct of doctors json"""
        response = self.client.get('/contacts/1/', content_type='application/json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            json.loads(response.content.decode('utf-8')), self.json_expected,
            "The response is incorrect")

    def test_post_contacts(self):
        """Create Contacts"""
        del self.json_expected['url']
        del self.json_expected['id']
        response = self.client.post('/contacts/', self.json_expected, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_post_contacts_with_required_fields(self):
        """Create Doctors with only required fields"""
        json_expected = {'firstName': 'Doctor', 'lastName': 'Demo'}
        response = self.client.post('/contacts/', json_expected, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_patch_contacts(self):
        """Update doctor"""
        json_aux = {'firstName': 'Alejandro Miguel', 'country': 'AR',
                    'postalCode': '1112'}
        response = self.client.patch('/contacts/1/', json_aux, format='json')
        self._update_json(self.json_expected, json_aux)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(json.loads(response.content.decode('utf-8')), self.json_expected)

    def test_delete_contacts(self):
        """Test delete contacts and validate that this not exist"""
        response = self.client.delete('/contacts/1/', format='json')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        response = self.client.delete('/contacts/1/', format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_get_contacts_tenant(self):
        """Test get contacts list from company 0"""
        response = self.client.get('/contacts/', format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['meta']['collectionSize'], 4)

    def test_search_contacts(self):
        """Test search doctor by name"""
        # Search taxId
        response = self.client.get('/contacts/?search=14987654', format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['meta']['collectionSize'], 1)

        # Search Name
        response = self.client.get('/contacts/?search=Toled', format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['meta']['collectionSize'], 1)

        # Search Name of tenant 0
        response = self.client.get('/contacts/?search=Luis', format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['meta']['collectionSize'], 0)

    def test_delete_contacts_tenat_incorrect(self):
        self.client.credentials(HTTP_AUTHORIZATION=self.token_tenant_1)
        response = self.client.delete('/contacts/1/', format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_update_contacts_tenat_incorrect(self):
        self.client.credentials(HTTP_AUTHORIZATION=self.token_tenant_1)
        json_aux = {'firstName': 'Alejandro Miguel', 'country': 'AR',
                    'postalCode': '1112'}
        response = self.client.patch('/contacts/1/', json_aux, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_contact_json_invalid(self):
        self.client.credentials(HTTP_AUTHORIZATION='{}')
        response = self.client.get('/contacts/1/', format='json')
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)


import matplotlib.pyplot as plt
import numpy as np

def abline(slope, intercept):
    axes = plt.gca()
    x_vals = np.array(axes.get_xlim())
    y_vals = intercept + slope * x_vals
    plt.plot(x_vals, y_vals, "--")

abline(3, 5)
plt.show()


# 임의 에러 발생
class BigNumberError(Exception):
    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        return self.msg

# 예외 처리
try:
    print("한자리 숫자 나누기")
    num1 = int(input("숫자1 >"))
    num2 = int(input("숫자2 >"))
    if num1 >= 10 or num2 >= 10:
        raise BigNumberError("입력값: {0}, {1}" .format(num1, num2))
    print("{0} / {1} = {2}" .format(num1, num2, int(num1/num2)))
except ValueError:
    print("잘못된 값 입력! 한 자리만 입력")
except BigNumberError as err:
    print("예외 발생! 한자리만 입력할 것!")
    print(err)
finally:
    print("계산기 이용 종료")

# Quiz) 동네에 항상 대기 손님이 있는 맛있는 치킨집이 있습니다
# 대기 손님의 치킨 요리 시간을 줄이고자 자동 주문 시스템을 제작하였습니다
# 시스템 코드를 확인하고 적절한 예외처리를 구문을 넣으시오

# 조건1: 1보다 작거나 숫자가 아닌 입력값이 들어올 때는 ValueError 로 처리
#         출력 메세지: "잘못된 값을 입력하였습니다"
# 조건2: 대기 손님이 주문할 수 있는 총 치킨량은 10마리로 한정
#         치킨 소진 시 사용자 정의 에러[SoldOutError]를 발생시키고 프로그램 종료
#         출력 메세지: "재고가 소진되어 더 이상 주문을 받지 않습니다"

# [코드]
class SoldOutError(Exception):
    pass

chicken = 10
waiting = 1 # 홀 안에는 현재 만석, 대기번호 1부터 시작
while(True):
    try:
        print("[남은 치킨 : {0}]" .format(chicken))
        order = int(input("치킨 몇 마리 주문하시겠습니까?"))
        if order > chicken: # 남은 치킨보다 주문량이 많을 때
            print("재료가 부족합니다")
        elif order <= 0:
            raise ValueError
        else:
            print("[대기번호 {0}] {1}마리 주문이 완료되었습니다" .format(waiting, order))
            waiting += 1
            chicken -= order

        if chicken == 0:
            raise SoldOutError

    except ValueError:
        print("잘못된 값을 입력하였습니다")

    except SoldOutError:
        print("재고가 소진되어 더 이상 주문을 받지 않습니다")
        break


from flask import request
from flask.ext.restful import abort, Resource
from sqlalchemy.exc import IntegrityError

from uamhnach import api, db, models, permission_required

class Permissions(Resource):

    @permission_required('permission_read')
    def get(self):
        return [p.to_json() for p in models.Permission.query.all()]

    @permission_required('permission_create')
    def post(self):
        payload = request.get_json()

        try:
            name = payload['name']
        except KeyError:
            abort(400)

        permission = models.Permission(name)
        db.session.add(permission)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            abort(403)

        return permission.to_json(), 201

class Permission(Resource):

    @permission_required('permission_read')
    def get(self, permission_id):
        permission = \
            models.Permission.query.filter_by(id=permission_id).first()
        if permission is None:
            abort(404)
        return permission.to_json()

    @permission_required('permission_update')
    def put(self, permission_id):
        permission = \
            models.Permission.query.filter_by(id=permission_id).first()
        if permission is None:
            abort(404)

        payload = request.get_json()

        for group_id, action in payload.iteritems():
            group = models.Group.query.filter_by(id=group_id).first()
            if group is None:
                abort(403)
            if action == 'add':
                permission.groups.append(group)
            elif action == 'delete':
                if group not in permission.groups:
                    abort(403)
                permission.groups.remove(group)
            else:
                abort(403)

        db.session.add(permission)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            abort(403)
        return permission.to_json(), 200

    @permission_required('permission_delete')
    def delete(self, permission_id):
        permission = \
            models.Permission.query.filter_by(id=permission_id).first()
        if permission is None:
            abort(404)

        db.session.delete(permission)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            abort(403)

api.add_resource(Permissions, '/permissions')
api.add_resource(Permission, '/permission/<int:permission_id>')


import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math

f = open("Gamma.txt", "r")
row = f.readlines()
list = []
for i in range(len(row)):
    column_list = row[i].strip().split(",")  # 每一行以，为分隔符split后是一个列表
    column_list.pop()#去掉最后一行属性g
    list.append(column_list)  # 加入list_source
a=np.array(list)#转化为np数组
a=a.astype(float)#转换为浮点类型
MeanVector=np.mean(a,axis=0)#均值向量
center=a-MeanVector#中心化
innerProduct=np.dot(center.T,center)
print(innerProduct/len(center))#求内积
Kroneckerproduct=0
for i in range(len(center)):
    Kroneckerproduct = Kroneckerproduct+center[i].reshape(len(center[0]),1)*center[i]
print(Kroneckerproduct/len(center))#求外积
t=center.T#通过中心化后的向量计算属性1和2的夹角
corr=np.corrcoef(t[0],t[1])#计算第一列属性和第二列属性相关性
print(corr[0][1])
picture = plt.figure()
ax1 = picture.add_subplot(111)  #设置标题
ax1.set_title("Correlation scatter plot")
plt.scatter(t[0],t[1])
plt.xlabel('Attributes 1')  #设置X轴标签
plt.ylabel('Attributes 2') #设置Y轴标签
plt.show()
# 正态分布的概率密度函数。可以理解成 x 是 mu（均值）和 sigma（标准差）的函数
def normfun(x, mu, sigma):
    pdf = np.exp(-((x - mu) ** 2) / (2 * sigma ** 2)) / (sigma * np.sqrt(2 * np.pi))
    return pdf
#计算mu和sigma
mu=np.mean(a,axis=0)[0]#计算第一列均值即mu
sigma=np.var(a.T[0])#计算第一列方差及sigma
fig = plt.figure()
ax1 = picture.add_subplot(111)
ax1.set_title("Probability density function")
# Python实现正态分布
# 绘制正态分布概率密度函数
x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 50)
y_sig = np.exp(-(x - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma)
plt.plot(x, y_sig, "r-", linewidth=2)
plt.vlines(mu, 0, np.exp(-(mu - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma), colors="c",
           linestyles="dashed")
plt.vlines(mu + sigma, 0, np.exp(-(mu + sigma - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma),
           colors="k", linestyles="dotted")
plt.vlines(mu - sigma, 0, np.exp(-(mu - sigma - mu) ** 2 / (2 * sigma ** 2)) / (math.sqrt(2 * math.pi) * sigma),
           colors="k", linestyles="dotted")
plt.xticks([mu - sigma, mu, mu + sigma], ['μ-σ', 'μ', 'μ+σ'])
plt.xlabel('Attributes 1')
plt.ylabel('Attributes 2')
plt.title('Normal Distribution: $\mu = %.2f, $sigma=%.2f' % (mu, sigma))
plt.grid(True)
plt.show()
#求每一列的方差
list=[]
for i in range(len(b[0])):
    list.append(np.var(b.T[i]))
print(list)
maxIndex=list.index(max(list))
minIndex=list.index(min(list))
print(maxIndex+1)
print(minIndex+1)
#求矩阵两列协方差
Cov={}
for i in range(9):
    for j in range(i+1,10):
        st=str(i+1)+'-'+str(j+1)
        Cov[st]= np.cov(a.T[i],a.T[j])[0][1]#遍历求协方差
print(Cov)
print(min(Cov, key=Cov.get))#取最小值打印
print(max(Cov, key=Cov.get))#取最大值打印


"""
--------------------------------------------------------------------------
Boil Buddy: Text message alerts for a boiling pot of water
--------------------------------------------------------------------------
License:
Copyright 2020 Nicolas Escobar
nick[dot]bob[dot]escobar[at]gmail[dot]com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------
Boil Buddy uses a piezoelectric sensor to measure amplitude of noise in order
to determine if a pot of water is boiling. It compares noise to the baseline
established before the water starts to boil to observe change. When it detects
a sufficient change which cannot be attributed to environmental factors like
bumping or clicking of the heating element, then it will send a text to alert
the user that the pot is boiing.

I had a lot of trouble with the ADC and also the statistics library. In order
to work around this, I used my one good run of boiling data (boiltest2.csv) and
fed it in as if it were data from the sensor, using fake_daqloop. Standard
deviation data from this trial was computed in MATLAB and is in fake_devs.csv.
If the statistics library works properly, it will use it instead of the MATLAB
data. If the ADC is functioning properly, change 'fake_daqloop' back to
'daqloop' within the main script.

"""

# ------------------------------------------------------------------------
# Imports
# ------------------------------------------------------------------------
import statistics as stat
import time
from threading import Thread, Event
import bisect

from piezo import Piezo
from sms import SMS

""" Only used for "fake" tasks! Can be removed if data acquisition and
    pstdev from statistics are working properly.
"""
import csv

# ------------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------------

# ------------------------------------------------------------------------
# Global variables
# ------------------------------------------------------------------------

# ------------------------------------------------------------------------
# Functions / Classes
# ------------------------------------------------------------------------
def daqloop(t,vals):

    start_time = time.time()

    while True:
        try:
            # Record sensor data and the time observed.
            time_elapsed = time.time() - start_time
            newval = piezo.read()

            # Add values to the lists.
            t.append(time_elapsed)
            vals.append(newval)

            # Rest for a bit.
            time.sleep(float(1/piezo.sampling_freq))

        except boiling.is_set():
            break

def fake_daqloop(t,vals):

    with open('boiltest2.csv', newline='') as csvfile:
        data = list(csv.reader(csvfile))
        rec_t = [float(i) for i in data[0]]
        rec_vals = [float(i) for i in data[1]]

    while not boiling.is_set():
        # Record sensor data and the time observed.
        time_elapsed = rec_t[len(t)]
        newval = rec_vals[len(vals)]

        # Add values to the lists.
        t.append(time_elapsed)
        vals.append(newval)

        # Rest for a bit.
        time.sleep(float(1/piezo.sampling_freq))

# ------------------------------------------------------------------------
# Main script
# ------------------------------------------------------------------------
if __name__ == '__main__':

    print("Program Start")

    piezo = Piezo()
    sms = SMS()
    boiling = Event()

    # Initialize lists for time and sensor data.
    t = []
    vals = []

    # Using a fake data acquisition because of an ADC problem.
    daq = Thread(target=fake_daqloop, args=(t,vals))
    daq.start()
    print("Data acquisition started")

    #initial baseline measurement, first 10s of data
    time.sleep(10)
    print("Gathering base data")
    base_vals = vals

    # Standard deviation hasn't been working, value for boiltest2 is hardcoded.
    try:
        base_dev = stat.pstdev(base_vals)
    except AssertionError:
        base_dev = 2.037

        #Also go ahead and load up fake deviations here, needed later in loop.
        with open('fake_devs.csv', newline='') as csvfile:
            data = list(csv.reader(csvfile))
            fake_devs = [float(i) for i in data[0]]

    # Amount standard deviation has to increase to be considered boiling.
    threshold_mult = 3.5
    boiling_dev = threshold_mult*base_dev

    # Size of increase to ignore, probably from non-boiling factors (bumps).
    over_mult = 15
    over_dev = over_mult*base_dev

    # Loop to check if water is boiling every 15s.
    test_index = [None, None, None, None, None]
    test_devs = [None, None, None, None, None]

    while not boiling.is_set():
        time.sleep(10)
        print("Checking if boiling...")
        # Take a snapshot because t and vals are updating constantly.
        test_t = t
        test_vals = vals
        curr_time = test_t[-1]

        # Mean regression filter to shrink spikes.
        avg_value = sum(test_vals)/len(test_vals)
        normalized_vals = [abs(x - avg_value) for x in test_vals]
        spike_index = [x for x, v in enumerate(normalized_vals) if v > 100]
        for i in spike_index:
            test_vals[i] = avg_value

        # Obtain indices for 15, 12, 9, 6, 3s before the current time.
        test_index[0] = bisect.bisect_left(test_t, test_t[-1] - 15)
        test_index[1] = bisect.bisect_left(test_t, test_t[-1] - 12)
        test_index[2] = bisect.bisect_left(test_t, test_t[-1] - 9)
        test_index[3] = bisect.bisect_left(test_t, test_t[-1] - 6)
        test_index[4] = bisect.bisect_left(test_t, test_t[-1] - 3)

        # Calculate standard deviation from overall mean, or use stored vals.
        try:
            test_devs[0] = stat.pstdev(test_vals[test_index[0]:test_index[1]], avg_value)
            test_devs[1] = stat.pstdev(test_vals[test_index[1]:test_index[2]], avg_value)
            test_devs[2] = stat.pstdev(test_vals[test_index[2]:test_index[3]], avg_value)
            test_devs[3] = stat.pstdev(test_vals[test_index[3]:test_index[4]], avg_value)
            test_devs[4] = stat.pstdev(test_vals[test_index[4]:-1], avg_value)
        except AssertionError:
            test_devs[0] = fake_devs[test_index[1]]
            test_devs[1] = fake_devs[test_index[2]]
            test_devs[2] = fake_devs[test_index[3]]
            test_devs[3] = fake_devs[test_index[4]]
            test_devs[4] = fake_devs[len(t)-1]

        # Check recent standard deviations to see if they meet criteria.
        if (all(i >= boiling_dev for i in test_devs) and
            all(i <= over_dev for i in test_devs)
            ):
            print("The water is boiling!")
            boiling.set()
        else:
            print("Not boiling yet.")

    # Send text message to alert.
    sms.send("Your water is boiling!")


#       module_bgp-md5.py
#
#       Copyright 2010 Daniel Mende <dmende@ernw.de>
#

#       Redistribution and use in source and binary forms, with or without
#       modification, are permitted provided that the following conditions are
#       met:
#
#       * Redistributions of source code must retain the above copyright
#         notice, this list of conditions and the following disclaimer.
#       * Redistributions in binary form must reproduce the above
#         copyright notice, this list of conditions and the following disclaimer
#         in the documentation and/or other materials provided with the
#         distribution.
#       * Neither the name of the  nor the names of its
#         contributors may be used to endorse or promote products derived from
#         this software without specific prior written permission.
#
#       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#       A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#       OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import tempfile
import threading
import time

import dpkt
import dumbnet

gobject = None
gtk = None
urwid = None

class bgp_md5bf(threading.Thread):
    def __init__(self, parent, iter, digest, data):
        self.parent = parent
        self.iter = iter
        self.digest = digest
        self.data = data
        self.obj = None
        threading.Thread.__init__(self)

    def run(self):
        if self.parent.platform == "Windows":
            import bf
        else:
            from loki_bindings import bf
        l = self.parent.parent
        self.obj = bf.tcpmd5_bf()
        self.obj.num_threads = l.bruteforce_threads
        if not l.bruteforce:
            self.obj.mode = bf.MODE_WORDLIST
            self.obj.wordlist = l.wordlist
        else:
            if not l.bruteforce_full:
                self.obj.mode = bf.MODE_ALPHANUM
            else:
                self.obj.mode = bf.MODE_FULL
        self.obj.pre_data = self.data
        self.obj.hash_data = self.digest

        self.obj.start()
        while self.obj.running:
            time.sleep(0.01)

        if self.parent.ui == 'gtk':
            with gtk.gdk.lock:
                src = self.parent.liststore.get_value(self.iter, self.parent.SOURCE_ROW)
                dst = self.parent.liststore.get_value(self.iter, self.parent.DESTINATION_ROW)
                if not self.obj.pw is None:
                    self.parent.liststore.set_value(self.iter, self.parent.SECRET_ROW, self.obj.pw)
                    self.parent.log("TCP-MD5: Found password '%s' for connection %s->%s" % (self.obj.pw, src, dst))
                else:
                    self.paren.liststore.set_value(self.iter, self.parent.SECRET_ROW, "NOT FOUND")
                    self.parent.log("TCP-MD5: No password found for connection %s->%s" % (src, dst))
        self.obj = None

    def quit(self):
        if not self.obj is None:
            self.obj.stop()
            self.obj = None

class mod_class(object):
    SOURCE_ROW = 0
    DESTINATION_ROW = 1
    SECRET_ROW = 2

    def __init__(self, parent, platform, ui):
        self.parent = parent
        self.platform = platform
        self.name = "tcp-md5"
        self.gladefile = "/modules/module_tcp-md5.glade"
        self.ui = ui
        if self.ui == 'gtk':
            import gobject as gobject_
            import gtk as gtk_
            import gtk.glade as glade_
            global gobject
            global gtk
            gobject = gobject_
            gtk = gtk_
            gtk.glade = glade_
            self.liststore = gtk.ListStore(str, str, str)
        elif self.ui == 'urw':
            import urwid as urwid_
            global urwid
            urwid = urwid_
        self.opts = None

    def start_mod(self):
        self.opts = {}

    def shut_mod(self):
        if self.opts:
            for i in self.opts:
                (iter, data, digest, thread) = self.opts[i]
                if thread:
                    if thread.is_alive():
                        thread.quit()
        if self.ui == 'gtk':
            self.liststore.clear()

    def get_root(self):
        self.glade_xml = gtk.glade.XML(self.parent.data_dir + self.gladefile)
        dic = { "on_crack_button_clicked" : self.on_crack_button_clicked
                }
        self.glade_xml.signal_autoconnect(dic)

        self.treeview = self.glade_xml.get_widget("treeview")
        self.treeview.set_model(self.liststore)
        self.treeview.set_headers_visible(True)

        column = gtk.TreeViewColumn()
        column.set_title("Source")
        render_text = gtk.CellRendererText()
        column.pack_start(render_text, expand=True)
        column.add_attribute(render_text, 'text', self.SOURCE_ROW)
        self.treeview.append_column(column)
        column = gtk.TreeViewColumn()
        column.set_title("Destination")
        render_text = gtk.CellRendererText()
        column.pack_start(render_text, expand=True)
        column.add_attribute(render_text, 'text', self.DESTINATION_ROW)
        self.treeview.append_column(column)
        column = gtk.TreeViewColumn()
        column.set_title("Secret")
        render_text = gtk.CellRendererText()
        column.pack_start(render_text, expand=True)
        column.add_attribute(render_text, 'text', self.SECRET_ROW)
        self.treeview.append_column(column)
        return self.glade_xml.get_widget("root")

    def log(self, msg):
        self.__log(msg, self.name)

    def set_log(self, log):
        self.__log = log

    def get_tcp_checks(self):
        return (self.check_tcp, self.input_tcp)

    def check_tcp(self, tcp):
        if tcp.opts != '':
            return (True, False)
        else:
            return (False, False)

    def input_tcp(self, eth, ip, tcp, timestamp):
        opts = dpkt.tcp.parse_opts(tcp.opts)
        for (opt, data) in opts:
            if opt == dpkt.tcp.TCP_OPT_MD5:
                src = dumbnet.ip_ntoa(ip.src)
                dst = dumbnet.ip_ntoa(ip.dst)
                ident = "%s:%i->%s:%i" % (src, tcp.sport, dst, tcp.dport)
                if ident not in self.opts:
                    if self.ui == 'gtk':
                        iter = self.liststore.append(["%s:%i" % (src, tcp.sport), "%s:%i" % (dst, tcp.dport), "CAPTURED"])
                    self.opts[ident] = (iter, str(eth.data), data, None)
                    self.log("TCP-MD5: Got MD5 data for connection %s" % (ident))

    # SIGNALS #

    def on_crack_button_clicked(self, btn):
        select = self.treeview.get_selection()
        (model, paths) = select.get_selected_rows()
        for i in paths:
            iter = model.get_iter(i)
            src = model.get_value(iter, self.SOURCE_ROW)
            dst = model.get_value(iter, self.DESTINATION_ROW)
            ident = "%s->%s" % (src, dst)
            (iter, data, digest, thread) = self.opts[ident]
            if thread:
                return
            thread = bgp_md5bf(self, iter, digest, data)
            model.set_value(iter, self.SECRET_ROW, "RUNNING")
            thread.start()
            self.opts[ident] = (iter, data, digest, thread)


import functools

def validate_list(func):
	@functools.wraps(func)
	def wrapper(*args, **kwargs):
		if 'items' in kwargs:
			items = kwargs['items']
			if not isinstance(items, (list, tuple, set)):
				raise TypeError(
					'Expected list, tuple or set, got %s.' % type(items))
			kwargs['items'] = list(items)
		return func(*args, **kwargs)
	return wrapper


import pymel.core as pm

def set_pivots_to_bottom():
    """
    Usage:
        set_pivots_to_bottom()
    """
    for obj in pm.ls(sl=True):
        pos = pm.pointPosition(obj.rotatePivot, w=True)
        center = pm.pointPosition(obj.boundingBox, w=True)

def connect_end_to_start_scaleY():
    for obj in pm.ls(sl=True):
        start = pm.PyNode(obj.name().replace('end','start'))
        start.scaleY.connect(obj.scaleY)

def setup_vis_tolerance():
    vis_tolerance_attr = pm.PyNode('global_CTRL.visibility_tolerance')

    for obj in pm.ls(sl=True,type='transform'):
        end_ctrl = pm.PyNode(obj.name().replace('bevel_','').replace('GEO','end_CTRL'))
        start_ctrl = pm.PyNode(obj.name().replace('bevel_','').replace('GEO','start_CTRL'))
        dist = pm.createNode('distanceBetween')
        end_ctrl.worldMatrix[0].connect(dist.inMatrix1)
        start_ctrl.worldMatrix[0].connect(dist.inMatrix2)
        cond = pm.createNode('condition')
        vis_tolerance_attr.connect(cond.secondTerm)
        cond.operation.set(4)
        dist.distance.connect(cond.firstTerm)
        cond.outColorR.connect(obj.visibility)

def scale_based_on_distance(source, targets, control):
    """
    Usage:
        scale_based_on_distance(pm.ls(sl=True)[0], pm.ls(sl=True)[1:], pm.ls(sl=True)[0])
    """
    control.addAttr('distance_tolerance', at='double', dv=10)
    control.attr('distance_tolerance').set(e=True, keyable=True)
    dist_tolerance = control.attr('distance_tolerance')

    control.addAttr('scale_max', at='double', dv=1)
    control.attr('scale_max').set(e=True, keyable=True)
    scale_max = control.attr('scale_max')

    for target in targets:
        dist = dist_between(target, source)

        anim_curve = pm.createNode('animCurveTL')
        anim_curve.addKey(0, 0, 'slow', 'slow')
        anim_curve.addKey(1, 1, 'slow', 'slow')

        # If the distance between is higher than the tolerance
        # Use the tolerance, if it's lower, use distance
        cond = pm.createNode('condition')
        cond.operation.set(4)
        dist.distance.connect(cond.firstTerm)
        dist_tolerance.connect(cond.secondTerm)
        dist.distance.connect(cond.colorIfTrueR)
        dist_tolerance.connect(cond.colorIfFalseR)

        remap = pm.createNode('remapValue')
        cond.outColorR.connect(remap.inputValue)
        dist_tolerance.connect(remap.inputMax)

        remap.outValue.connect(anim_curve.input)

        remap_inv = pm.createNode('remapValue')
        anim_curve.output.connect(remap_inv.inputValue)
        scale_max.connect(remap_inv.outputMin)
        remap_inv.outputMax.set(0)

        remap_inv.outValue.connect(target.scaleY)

def dist_between(t1, t2):
    dist = pm.createNode('distanceBetween')
    t1.worldMatrix[0].connect(dist.inMatrix1)
    t2.worldMatrix[0].connect(dist.inMatrix2)
    return dist


# import cv2
# # Readingour Image1
# # my_firstpic = cv2.imread(
# # 'C:/Users/TP/Pictures/west bengal/bishnupur/mqdefaultILPT6GSR.jpg', 1)

# my_first = cv2.imread(
#     '/home/neo/Téléchargements/Photo_camille.jpg', 1)
# cv2.imshow('image', my_first)
# # Readingour Image2
# cap = cv2.VideoCapture(0)
# ret, my_sec = cap.read()

# img = cv2.add(my_first, my_sec)
# cv2.waitKey(0)
# cv2.distroyAllWindows()
import cv2
import numpy as np

img = cv2.imread("pyimg.jpg")
contrast_img = cv2.addWeighted(img, 2.5, np.zeros(img.shape, img.dtype), 0, 0)

cv2.imshow("Original Image", img)

cv2.imshow("Contrast Image", contrast_img)

cv2.waitKey(0)


users = {
    "Skarwa4491": {
        "First_name": "Swapnil",
        "Last_name": "Karwa",
        "Location": "India"
    },

    "Pooja0507": {

        "First_name": "Pooja",
        "Last_name": "Mantri",
        "Location": "Ahmednagar"

    }

}

for user_name, user_info in users.items():
    print(user_name + "-", end=" ")
    print(user_info["First_name"] + " " + user_info["Last_name"])


import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")

##
def genGridWorld():
    O = -1e5  # Dangerous places to avoid
    D = 35    # Dirt
    W = -100  # Water
    C = -3000 # Cat
    T = 1000  # Toy
    grid_list = {0:'', O:'O', D:'D', W:'W', C:'C', T:'T'}
    grid_world = np.array([[0, O, O, 0, 0, O, O, 0, 0, 0],
        [0, 0, 0, 0, D, O, 0, 0, D, 0],
        [0, D, 0, 0, 0, O, 0, 0, O, 0],
        [O, O, O, O, 0, O, 0, O, O, O],
        [D, 0, 0, D, 0, O, T, D, 0, 0],
        [0, O, D, D, 0, O, W, 0, 0, 0],
        [W, O, 0, O, 0, O, D, O, O, 0],
        [W, 0, 0, O, D, 0, 0, O, D, 0],
        [0, 0, 0, D, C, O, 0, 0, D, 0]])
    return grid_world, grid_list

##
def showWorld(grid_world, tlt):
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    ax.set_title(tlt)
    ax.set_xticks(np.arange(0.5,10.5,1))
    ax.set_yticks(np.arange(0.5,9.5,1))
    ax.grid(color='b', linestyle='-', linewidth=1)
    ax.imshow(grid_world, interpolation='nearest', cmap='copper')
    return ax

##
def showTextState(grid_world, grid_list, ax):
    for x in range(grid_world.shape[0]):
        for y in range(grid_world.shape[1]):
            if grid_world[x,y] >= -3000:
                ax.annotate(grid_list.get(grid_world[x,y]), xy=(y,x), horizontalalignment='center')

##
def showPolicy(policy, ax):
    for x in range(policy.shape[0]):
        for y in range(policy.shape[1]):
            if policy[x,y] == 0:
                ax.annotate('$\downarrow$', xy=(y,x), horizontalalignment='center')
            elif policy[x,y] == 1:
                ax.annotate(r'$\rightarrow$', xy=(y,x), horizontalalignment='center')
            elif policy[x,y] == 2:
                ax.annotate('$\u2191$', xy=(y,x), horizontalalignment='center')
            elif policy[x,y] == 3:
                ax.annotate('$\leftarrow$', xy=(y,x), horizontalalignment='center')
            elif policy[x,y] == 4:
                ax.annotate('$\perp$', xy=(y,x), horizontalalignment='center')

##
def prob_model(row, col, action, use_prob_model=False):
    if not use_prob_model:
        if action == 0:
            # down
            row_next = row + 1
            col_next = col
            if row_next > 8:
                row_next = row
        elif action == 1:
            # right
            row_next = row
            col_next = col + 1
            if col_next > 9:
                col_next = col
        elif action == 2:
            # up
            row_next = row - 1
            col_next = col
            if row_next < 0:
                row_next = row
        elif action == 3:
            # left
            row_next = row
            col_next = col - 1
            if col_next < 0:
                col_next = col
        elif action == 4:
            # prep -> stay.
            row_next = row
            col_next = col
        return row_next, col_next
    elif use_prob_model:
        row_next = np.zeros(4)
        col_next = np.zeros(4)
        prob = np.zeros(4)
        if action == 0:
            # down
            row_next[0], col_next[0] = prob_model(row, col, 0)
            prob[0] = np.array([0.7])
            row_next[1], col_next[1] = prob_model(row, col, 1) # right
            prob[1] = 0.1
            row_next[2], col_next[2] = prob_model(row, col, 3) # left
            prob[2] = 0.1
            row_next[3], col_next[3] = prob_model(row, col, 4) # stay
            prob[3] = 0.1
        elif action == 1:
            # right
            row_next[0], col_next[0] = prob_model(row, col, 1)
            prob[0] = np.array([0.7])
            row_next[1], col_next[1] = prob_model(row, col, 0)  # down
            prob[1] = 0.1
            row_next[2], col_next[2] = prob_model(row, col, 2)  # up
            prob[2] = 0.1
            row_next[3], col_next[3] = prob_model(row, col, 4)  # stay
            prob[3] = 0.1
        elif action == 2:
            # up
            row_next[0], col_next[0] = prob_model(row, col, 2)
            prob[0] = np.array([0.7])
            row_next[1], col_next[1] = prob_model(row, col, 1)  # right
            prob[1] = 0.1
            row_next[2], col_next[2] = prob_model(row, col, 3)  # left
            prob[2] = 0.1
            row_next[3], col_next[3] = prob_model(row, col, 4)  # stay
            prob[3] = 0.1
        elif action == 3:
            # left
            row_next[0], col_next[0] = prob_model(row, col, 3)
            prob[0] = np.array([0.7])
            row_next[1], col_next[1] = prob_model(row, col, 0)  # down
            prob[1] = 0.1
            row_next[2], col_next[2] = prob_model(row, col, 2)  # up
            prob[2] = 0.1
            row_next[3], col_next[3] = prob_model(row, col, 4)  # stay
            prob[3] = 0.1
        elif action == 4:
            # prep
            row_next = np.array([row])
            col_next = np.array([col])
            prob = np.array([1])
        return row_next, col_next, prob

def ValIter(R, discount=None, maxSteps=None, infHor=False, probModel=False):
    #R = grid_world matrix! state metric for reward
    if infHor==False:
        T = maxSteps
        # down, right, up, left, prep
        actions = np.array([0, 1, 2, 3, 4])
        Q = np.zeros((R.shape[0], R.shape[1], actions.shape[0], T))
        V = np.zeros((R.shape[0], R.shape[1], T))
        # r = np.zeros((R.shape[0], R.shape[1], actions.shape[0], T))
        # r[:,:,T-1] = R
        V[:, :, T - 1] = R
        # iterate over all ts
        # iterate backwards in time! for schleife rückwärts
        for t in range(T - 2, -1, -1):
            # iterate over all state rows
            for row in range(R.shape[0]):
                # iterate over all columns
                for col in range(R.shape[1]):
                    # iterate over all actions
                    for action in range(actions.shape[0]):
                        if not probModel:
                            row_next, col_next = prob_model(row, col, action)
                            Q[row, col, action, t] = R[row, col] + V[row_next, col_next, t + 1]
                        elif probModel:
                            #Task 2.d) : each next state or action has additionally a probability value
                            row_next, col_next, prob = prob_model(row, col, action, use_prob_model=True)
                            Q[row, col, action, t] = R[row, col]
                            for i in range(row_next.shape[0]):
                                Q[row, col, action, t] = Q[row, col, action, t]+prob[i] * V[int(row_next[i]), int(col_next[i]), t + 1]
                    V[row, col, t] = max(Q[row, col, :, t])
    else:
        # infinite horizon:
        # down, right, up, left, prep
        actions = np.array([0, 1, 2, 3, 4])
        max_k = maxSteps
        Q = np.zeros((R.shape[0], R.shape[1], actions.shape[0], max_k))
        V = np.zeros((R.shape[0], R.shape[1], max_k))
        k = 0
        # for schleife vorwärts:
        for k in range(max_k-1):
            # iterate over all state rows
            for row in range(R.shape[0]):
                # iterate over all columns
                for col in range(R.shape[1]):
                    # iterate over all actions
                    for action in range(actions.shape[0]):
                        row_next, col_next = prob_model(row, col, action)
                        Q[row, col, action, k+1] = R[row, col] + discount*V[row_next, col_next, k]
                    V[row, col, k+1] = max(Q[row, col, :, k+1])

            #set to zero:
            #brich ab wenn konvergiert!
            if (np.abs(V[:,:,k]-V[:,:,k+1])<0.01).all():
                V = V[:,:,0:k+1]
                Q = Q[:,:,:,0:k+1]
                break
        V = np.flip(V, axis=2)
        Q = np.flip(Q, axis=3)

    return V, Q

##
def maxAction(V, R, discount, probModel=None):
    return 0

##
def findPolicy(V, Q, probModel=None):
    pi = np.zeros((Q.shape[0], Q.shape[1], Q.shape[3]))
    for t in range(Q.shape[3]):
        for row in range(Q.shape[0]):
            for col in range(Q.shape[1]):
                pi[row, col, t] = np.argmax(Q[row, col, :,t])
    return pi

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

saveFigures = False

data = genGridWorld()
grid_world = data[0]
grid_list = data[1]

#probModel = ...

ax = showWorld(grid_world, 'Environment')
showTextState(grid_world, grid_list, ax)
if saveFigures:
    plt.savefig('gridworld.pdf')

# Finite Horizon
V, Q = ValIter(grid_world, maxSteps=10)
V = V[:,:,0];
showWorld(np.maximum(V, 0), 'Value Function - Finite Horizon')
if saveFigures:
    plt.savefig('value_Fin_15.pdf')

policy = findPolicy(V, Q)
ax = showWorld(grid_world, 'Policy - Finite Horizon')
showPolicy(policy[:,:,0], ax)
if saveFigures:
    plt.savefig('policy_Fin_15.pdf')

#Infinite Horizon
V, Q = ValIter(grid_world, maxSteps=100, discount=0.1, infHor=True)
V = V[:,:,0];
showWorld(np.maximum(V, 0), 'Value Function - Infinite Horizon')
if saveFigures:
    plt.savefig('value_Inf_08.pdf')

policy = findPolicy(V, Q);
ax = showWorld(grid_world, 'Policy - Infinite Horizon')
showPolicy(policy[:,:,0], ax)
if saveFigures:
    plt.savefig('policy_Inf_08.pdf')

# Finite Horizon with Probabilistic Transition
V, Q = ValIter(grid_world, maxSteps=15, probModel=True)
V = V[:,:,0];
showWorld(np.maximum(V, 0), 'Value Function - Finite Horizon with Probabilistic Transition')
if saveFigures:
    plt.savefig('value_Fin_15_prob.pdf')

policy = findPolicy(V, Q)
ax = showWorld(grid_world, 'Policy - Finite Horizon with Probabilistic Transition')
showPolicy(policy[:,:,0], ax)
if saveFigures:
    plt.savefig('policy_Fin_15_prob.pdf')

plt.show()


#!/usr/local/bin/python
"""
***********************************************
- PROGRAM: parse_mpileup.py
- CONTACT: Gaurav Jain(gaurav.jain@tum.de)
***********************************************
"""

def main():

    # Get the snvbed dict
    snvdict = get_snvbed_dict(input_snvbed_file)

    print("- Reading and converting mergedvcf file to bed file...")
    with open(input_pileup_file, 'r') as f:
        # http://samtools.sourceforge.net/pileup.shtml
        # head mergedvcf/txt/DS08_5320_LivMet-1-Mutect2.pileup | cut - f1-7
        # Final column is the ReadName
        # ┌───────┬──────────┬───────────────┬───────────────┬──────────────────────┬──────────────────────┬──────────────────────┐
        # │ Chrom │ Position │ ReferenceBase │ totalCoverage │      ReadBases       │     BaseQuality      │    MappingQuality    │
        # ├───────┼──────────┼───────────────┼───────────────┼──────────────────────┼──────────────────────┼──────────────────────┤
        # │     1 │  7411767 │ A             │            20 │ ,...,.......,....... │ ???>=>>>>>>;?>>>>=>< │ ]]]]]]]]]]]]]UUUUUUL │
        # │     1 │  7649718 │ c             │            15 │ ,.,.$A,,,,,,,a,,     │ BBBU@B@CCBBB?!B      │ ]SR@+J?E];]L4@V      │
        # └───────┴──────────┴───────────────┴───────────────┴──────────────────────┴──────────────────────┴──────────────────────┘
        # ...

        # Write header to the output line
        pileupHeader         = "Chrom\tGenomicPos\tRef\tTotalCov\tReadBases\tBaseQ\tMapQ\tReadName"
        BaseQualHeader       = "\t".join(["BaseQ" + l for l in ['Mean', 'Std', 'Min', '25pc', 'Median', '75pc', 'Max']])
        mappingQualityHeader = "\t".join(["MapQ" + l for l in ['Mean', 'Std', 'Min', '25pc', 'Median', '75pc', 'Max']])
        ofile.write("{0}\t{1}\t{2}\n".format(pileupHeader, BaseQualHeader, mappingQualityHeader))

        for line in useful_lines(f):
            # Split the line
            row  = line.split("\t")
            # Get relevant keys from pileup file
            pileupKey      = "{0}:{1}".format(row[0], row[1])  # 1:7649718
            _referenceBase = row[2].upper()                    # C
            _totalCoverage = row[3]                            # 15
            readBases      = row[4]                            # ,.,.$A,,,,,,,A,,
            baseQuality    = row[5]                            # BBBU@B@CCBBB?!B
            mappingQuality = row[6]                            # ]SR@+J?E];]L4@V
            itcln,_itln,ill,callString = get_clean_readbase_indel_counts(readBases)
            callString     = callString.upper()
            subsList       = get_unique_list(re.findall('[ACGT]', callString)) # ['g', 't']

            # Get the snvbed dictionary value for pileupKey
            snvbed = snvdict[pileupKey]                   # new: 1_3160218_T_C_4 ...old: DS08_5320_LivMet-1|1_7649718_CC_AT|2|DS08_5320_LivMet-3-Mutect2

            # Parse snvbed value to respective variables (m2  = mutect2)
            try:
                m2row            = snvbed.split('_')
                _m2ReferenceBase = m2row[2].upper()     # CC: refrence base ... store only first base (C) as second is not considered
                m2AltBase        = m2row[3].upper()     # AT: Alt      base ... store only first base (A) as second is not considered
                _m2AltReadsCount = m2row[4].upper()     # 4 : Alt reads (skip if zero)

                ## Get the index of the snv from the callString and then use that index to get the quality of those reads
                # Get the index of snv
                # NOTE: Before calculating the indexes, I need to remove $ and ^ sign which are the part of the CALF format
                #       Also at the read base column, a symbol `^' marks the start of a read segment which is a contiguous
                #       subsequence on the read separated by `N/S/H' CIGAR operations. The ASCII of the character following
                #       `^' minus 33 gives the mapping quality. A symbol `$' marks the end of a read segment. Start and end
                #       markers of a read are largely inspired by Phil Green's CALF (compact alignment format) format.
                #       These markers make it possible to reconstruct the read sequences from pileup.
                # http://www.phrap.org/phredphrap/calf.pdf

                # https://stackoverflow.com/questions/14215338/python-remove-multiple-character-in-list-of-string/14215379
                # Before: ,.,.$A,,,,,,,A,,
                # After : ,.,.A,,,,,,,A,,
                # Positions of A in ,.,.A,,,,,,,A,, are [4, 12]
                snvIdx = [pos for pos, c in enumerate(callString) if c == m2AltBase[0]]

                # debugPos = '7:119640568'
                # if pileupKey == debugPos:
                #     print(row)
                #     print("subsList      : {0}".format(subsList))
                # #     print("m2row         : {0}". format(m2row))
                #     print("snvIdx        : {0}". format(snvIdx))
                # #     print("m2AltBase     : {0}". format(m2AltBase))
                # #     print("readBases     : {0}". format(readBases))
                # #     print("callString    : {0}". format(callString))
                # #     print("mappingQuality: {0}". format(mappingQuality))
                # #     print("indelListCount: {0}". format(itcln))
                # #     print("indelList     : {0}". format(_itln))
                # #     print("indelLocation : {0}". format(ill))
                # #     print("callString    : {0}". format(callString))

                baseQscores          = "\t\t\t\t\t\t"
                mappingQualityscores = "\t\t\t\t\t\t"
                if len(subsList) > 0 and m2AltBase[0] in subsList:
                    # if pileupKey == debugPos:   print("1")
                    # if pileupKey == debugPos: print("here: {}".format(subsList))
                    # Get the base and mapping quality for the snv index
                    # https://www.geeksforgeeks.org/python-accessing-all-elements-at-given-list-of-indexes/
                    # https://stackoverflow.com/questions/25082410/apply-function-to-each-element-of-a-list?rq=1
                    # Decided to go for descripting of all the reads for that variant
                    # In [87]: pd.DataFrame([ord(i)-33 for i in list(itemgetter(*snvIdx)(mappingQuality))]).describe().T
                    #        count  mean      std       min    25%    50%   75%    max
                    #   0    2.0    14.5      6.363961  10.0   12.25  14.5  16.75  19.0
                    # Final string = '30.50\t0.71\t30\t30.25\t30.50\t30.75\t31'
                    baseQDF = pd.DataFrame([ord(i)-33 for i in list(itemgetter(*snvIdx)(baseQuality))]).describe().T.values.tolist()[0]
                    baseQscores = "{0:1.2f}\t{1:1.2f}\t{2:1.0f}\t{3:1.2f}\t{4:1.2f}\t{5:1.2f}\t{6:1.0f}".format(baseQDF[1], baseQDF[2], baseQDF[3], baseQDF[4], baseQDF[5], baseQDF[6], baseQDF[7])
                    mappingQualityDF  = pd.DataFrame([ord(i)-33 for i in list(itemgetter(*snvIdx)(mappingQuality))]).describe().T.values.tolist()[0]
                    # if pileupKey == debugPos:   print(mappingQuality)
                    mappingQualityscores  = "{0:1.2f}\t{1:1.2f}\t{2:1.0f}\t{3:1.2f}\t{4:1.2f}\t{5:1.2f}\t{6:1.0f}".format(mappingQualityDF[1], mappingQualityDF[2], mappingQualityDF[3], mappingQualityDF[4], mappingQualityDF[5], mappingQualityDF[6], mappingQualityDF[7])
                elif itcln > 0:
                    # if pileupKey == debugPos:   print("INDELs")
                    mappingQualityDF      = pd.DataFrame([ord(i)-33 for i in list(itemgetter(*ill)(mappingQuality))]).describe().T.values.tolist()[0]
                    mappingQualityscores  = "{0:1.2f}\t{1:1.2f}\t{2:1.0f}\t{3:1.2f}\t{4:1.2f}\t{5:1.2f}\t{6:1.0f}".format(mappingQualityDF[1], mappingQualityDF[2], mappingQualityDF[3], mappingQualityDF[4], mappingQualityDF[5], mappingQualityDF[6], mappingQualityDF[7])
                else:
                    # if pileupKey == debugPos:  print("{2}: Alteration {0} is not found in list of all alterations {1}".format(m2AltBase[0], subsList, pileupKey))
                    # print("{}".format(pileupKey))
                    print("{2}: Alteration {0} is not found in list of all alterations {1}".format(m2AltBase[0], subsList, pileupKey))
                    pass

                # Write to the output line
                ofile.write("{0}\t{1}\t{2}\n".format(line, baseQscores, mappingQualityscores))
            except:
                # print("MY INDEL ERROR:", line)
                pass

    ofile.close()
    print("\n- Your text  output file is: {0}".format(output_file))

################ USER DEFINED FUNCTIONS ###################
def get_snvbed_dict(input_snvbed_file):
    ''' Get the annotation in a dictionary '''

    # Get the information in a dictionary
    d = defaultdict(int)
    with open(input_snvbed_file, 'r') as fg:
        ######### OLD ##############
        # sort - V - k1, 1 - k3, 3 mergedvcf/txt/DS08_5320_LivMet-1-Mutect2.bed | head
        # ┌───┬──────────┬──────────┬─────────────────────────────────────────────────────────────────┐
        # │ 1 │  7411766 │  7411767 │ DS08_5320_LivMet-1|1_7411767_A_G|0|DS08_5320_LungMet-1-Mutect2  │
        # │ 1 │  7649717 │  7649718 │ DS08_5320_LivMet-1|1_7649718_CC_AT|2|DS08_5320_LivMet-3-Mutect2 │
        # │ 1 │ 10554584 │ 10554585 │ DS08_5320_LivMet-1|1_10554585_C_G|0|DS08_5320_LungMet-1-Mutect2 │
        # │ 1 │ 19228312 │ 19228313 │ DS08_5320_LivMet-1|1_19228313_AG_A|0|DS08_5320_LivMet-3-Mutect2 │
        # └───┴──────────┴──────────┴─────────────────────────────────────────────────────────────────┘
        # ....

        # NEW
        # ┌───┬─────────┬─────────┬──────────────────┐
        # │ 1 │ 3160217 │ 3160218 │ 1_3160218_T_C_4  │
        # │ 1 │ 3409856 │ 3409857 │ 1_3409857_A_G_22 │
        # │ 1 │ 5059015 │ 5059016 │ 1_5059016_T_A_11 │
        # └───┴─────────┴─────────┴──────────────────┘
        # ...

        # Loop through rest of the file
        for line in useful_lines(fg):
            row = re.split('\t', line)
            try:
                d["{0}:{1}".format(row[0], row[2])] = row[3]  # key = 1:7649718
            except:
                pass

    return d

def get_clean_readbase_indel_counts(bases):
    """
    Removes from the call string in,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg mpileup (5th column) the ^ character and
    the char next to it.,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg
    bases:,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg
        String of read bases (5th col,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tggumn of mpileup)
    Return:,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg
        Same string as bases but with,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg ^ and the following char removed as well as
        indels,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg
    Example:,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg
        bases= '^A....,,.,.,...,,,.,....^k.'
        get_clean_readbase(bases) >>> '....,,.,.,...,,,.,.....'
    Source: Dario Beraldi (https://github.com/dariober)
            https://raw.githubusercontent.com/dariober/bioinformatics-cafe/master/bam2methylation/bam2methylation.py

    Another good read: https://www.biostars.org/p/76829/

    Also:
    Count occurance of differnt types of indels in the base string
    For example:
    1) ,$,,,,,,,,,,,,,,,,,,,,,,,,,,-2tg,,,-2tg,-2tg,,,g,,,-2tgg                        has 1 types [-2tg]
    2) ,$,,,,,,,,,,,,,.,,.,,,,,,,,,,aa,,,,-4gaga,-2ga,,,,,,,-2ga,,,-4gaga,,,,-2gaa*,,, has 2 types ['4gaga', '2ga']

    """
    # NOTE:
    callString     = ''
    skip           = False
    getIndelNum    = False # Switch to start accumulating ints following +/- if indelFound flag is set
    indelNumList   = []    # List of ints following +/-. Converted to int() will give indel length
    nskip          = 0
    indelList      = []    # List of all indels (unique)

    # # Retun NAN if input is nan
    # if(pd.isna(bases)):
    #     print("\n- Skipping NANs ...")
    #     return (np.nan, np.nan)

    indelType    = ''  # + or -
    indelLoc     = 0
    indelLocList = []
    for i, x in enumerate(bases):
        # if bases == ',,,,,,+2tg,+2tg,,+2tg': print("\n{}) origLoc {}: {}".format(i, indelLoc, x))
        # print("\n{}) origLoc {}: {}".format(i, indelLoc, x))
        if nskip > 0:
            nskip -= 1
            indelList.append(x.lower())
            indelLoc -= 1
        elif x  == '^':
            skip= True
            indelLoc -= 1
        elif skip:
            skip= False
            indelLoc -= 1
        elif x == '$':
            indelLoc -= 1
            continue # Skip end-of-read marker
        elif x in ('+', '-'):
            indelLoc -= 1
            indelType = x
            indelLocList.append(indelLoc)
            getIndelNum = True
        elif getIndelNum:
            indelLoc -= 1
            if x.isdigit():
                indelNumList.append(x)
                indelList.append('|')
                indelList.append(indelType)
                indelList.append(x.lower())
                indelType=''
            else:
                indelList.append(x.lower())
                try:
                    nskip = int(''.join(indelNumList))-1
                except:
                    pass
                indelNumList= []
                getIndelNum= False
        else:
            # if bases == ',,,,,,+2tg,+2tg,,+2tg': print("{}) Location{}: {}".format(i, indelLoc, x))
            # print("{}) Location{}: {}".format(i, indelLoc, x))
            callString += x

        indelLoc += 1

    # Split the list into sublists separated by '|'
    # Input  = [',', '|', '2', 't', 'g', '|', '2', 't', 'g', '|', '2', 't', 'g', '|', '2', 't', 'g']
    # Output = [[','], ['2tg'], ['2tg'], ['2tg'], ['2tg']]
    # Or
    # Input  = [',', '|', '4', 'g', 'a', 'g', 'a', '|', '2', 'g', 'a', '|', '2', 'g', 'a', '|', '4', 'g', 'a', 'g', 'a', '|', '2', 'g', 'a']
    # Output = [[','], ['4gaga'], ['2ga'], ['2ga'], ['4gaga'], ['2ga']]
    indelList = [[''.join(list(g))] for k,g in groupby(breakby(indelList, '|', None), lambda x: x is not None) if k]

    # Flatten the list and remove the first element (',')
    # Input  = [[','], ['2tg'], ['2tg'], ['2tg'], ['2tg']]
    # Output = ['2tg']
    # Or
    # Input  = [[','], ['4gaga'], ['2ga'], ['2ga'], ['4gaga'], ['2ga']]
    # Output = ['4gaga', '2ga']
    indelList = get_unique_list(list(chain.from_iterable(indelList))[1:])

    # Get the length of the list to obtain number of unique indels
    # return(len(indelList), callString)
    return(int(len(indelList)), "|".join(indelList), indelLocList, callString)

def breakby(biglist, sep, delim=None):
    '''
    Split list into lists based on a character occurring inside of an element
    Source: https://stackoverflow.com/questions/45281189/split-list-into-lists-based-on-a-character-occurring-inside-of-an-element/45281271
    '''
    from itertools import groupby
    for item in biglist:
        p = item.split(sep)
        yield p[0]
        if len(p) > 1:
            yield delim
            yield p[1]

def check_options():
    ''' Checks the options to the program '''

    # Create parser object
    parser = argparse.ArgumentParser(add_help=True,
        formatter_class=argparse.RawTextHelpFormatter,
        epilog=textwrap.dedent('''\
        ----------------- SAMPLE USAGE ------------------
        - python scripts/parse_mpileup.py -ip=output/mPDAC_cherrypicked1/mergedvcf/txt/DS08_5320_LivMet-1-Mutect2.pileup -ib=output/mPDAC_cherrypicked1/mergedvcf/txt/DS08_5320_LivMet-1-Mutect2.bed
        -------------------------------------------------
        CONTACT:
            Gaurav Jain
            gaurav.jain@tum.de
        -------------------------------------------------
        '''))

    # Add arguments
    parser.add_argument("-ip", metavar='--ipfile', help="input  pileup file", dest="input_pileup_file", type=str, required=True)
    parser.add_argument("-ib", metavar='--ibfile', help="input  snvbed file", dest="input_snvbed_file", type=str, required=True)

    # Print the help message only if no arguments are supplied
    if len(sys.argv)==1:
        parser.print_help()
        sys.exit(1)

    # Save the STDOUT output in a log file
    if parser.parse_args().input_pileup_file:
        logdir="{0}/logs".format(get_file_info(get_file_info(parser.parse_args().input_pileup_file)[0])[0])
        create_dir(logdir)
        logfile = "{0}/{1}_parse_tumor_exact_pileup.log".format(logdir, get_file_info(parser.parse_args().input_pileup_file)[1])
    else:
        logdir  = "{0}/logs".format(os.getcwd())
        create_dir(logdir)
        logfile = "{0}/{1}.log".format(logdir,get_file_info(sys.argv[0])[1])

    logf = open(logfile, 'w')
    sys.stdout = Log(logf, sys.stdout)

    # Parse command line with parse_args and store it in an object
    args = parser.parse_args()
    print_initial_arguments(parser)
    return args

if __name__=="__main__":
    print(__doc__)

    # Built in modules
    import argparse
    import os.path
    import sys

    # 3rd party modules
    import textwrap
    import re
    import numpy as np
    import scipy as sp
    import pandas as pd
    import matplotlib as mp
    #mp.use('Agg') # to use matplotlib without X11
    import matplotlib.pyplot as plt
    import subprocess
    import binascii as bi
    import scipy.stats as stats
    from collections import *
    from numpy import nanmean
    from operator import itemgetter
    from itertools import groupby, chain

    # for looping files in a dir
    import glob

    # user defined modules
    from gjainPyLib import *      # import all the functions from the Gaurav`s python library

    ### for color scale
    from  matplotlib import colors
    from itertools import cycle, islice # barplot colors

    ################ USER CONFIGURATION ###################
    np.set_printoptions(precision=6)
    #######################################################

    # Get input options
    args = check_options()

    # Store the variables
    input_pileup_file = args.input_pileup_file
    input_snvbed_file = args.input_snvbed_file

    # Get output file name
    output_file = "{0}_parsed_tumor_exact_pileup.txt".format(get_file_info(input_pileup_file)[3])
    ofile = open(output_file, 'w')

    main()


from __future__ import division

import argparse, os, codecs, re
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm

from chorus.helpers import create_chroma
####

no_word = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '[', ']', ',', '.', '<', '>', ':']
root = 'data'
candidate_limit = 10
songs_root = os.path.join(root, 'songs')
accs_root = os.path.join(root, 'accompany')
lyric_root = os.path.join(root, 'lyrics')
json_root = os.path.join(root, 'json')

####

def get_sentence_ts(line: str) -> float:
    minute, second = re.search(r'\[(.*)\]', line).groups()[0].split(':')
    return int(minute) * 60 + float(second)

def get_word_ts(line: str) -> list:
    second = re.findall(r'\<(.*?)\,.*?\>', line)
    second = [int(sec) for sec in second]
    return second

def get_words(line: str) -> str:
    for chr in no_word:
        line = line.replace(chr, '')
    return line.strip()

def cal_lyric_similarity(sentence1: str, sentence2: str) -> float:
    ''' content and position '''
    '''两个句子相同字符数 / 长句子长度 * （短句子 / 长句子）'''

    sentence1, sentence2 = list(sentence1), list(sentence2)
    if len(sentence1) > len(sentence2):
        return round(sum([1 for word in sentence2 if word in sentence1]) / len(sentence1) * len(sentence2) / len(sentence1), 3)
    else:
        return round(sum([1 for word in sentence1 if word in sentence2]) / len(sentence2) * len(sentence1) / len(sentence2), 3)

def cal_chroma_similarity(chroma1: np.array, chroma2: np.array) -> float:
    '''计算以单句歌词为界的音频相似度'''
    def cal_iou_smilarity(chroma1: np.array, chroma2: np.array) -> float:
        assert chroma1.shape >= chroma2.shape
        return np.array([((chroma2 - chroma1[:, idx:idx+chroma2.shape[1]]) ** 2).mean() for idx in range(chroma1.shape[1] - chroma2.shape[1] + 1)]).min()

    if chroma1.shape[-1] >= chroma2.shape[-1]:
        return cal_iou_smilarity(chroma1, chroma2)
    else:
        return cal_iou_smilarity(chroma2, chroma1)

def get_lyric_seg(filepath: str) -> np.array:
    '''
    input:
    filepath: string

    output:
    info   : tuple (starttime, endtime, word_num_per_sentence, re-intro, sentence)

    '''

    with codecs.open(filepath, mode='r', encoding='utf8') as f:
        sentence = []
        sentence_ts = []
        sentence_duration = []
        for line in f:
            if line.strip() == '':
                continue
            else:
                sentence.append(get_words(line))
                sentence_ts.append(get_sentence_ts(line))
                sentence_duration.append(sum(get_word_ts(line)))

    sentence =  [sec for sec in sentence if sec != '']
    sentence_duration = [ts/1000 for ts in sentence_duration if ts != 0]
    sentence_start = sorted(list(set(sentence_ts)))
    word_num = [len(sec) for sec in sentence]
    sentence_end = [sentence_start[idx] + sentence_duration[idx] for idx in range(len(sentence))]
    re_intro = [sentence_start[0]] + [sentence_start[idx] - sentence_end[idx-1] for idx in range(1, len(sentence))]
    assert len(sentence) == len(sentence_end) == len(sentence_start) == len(word_num) == len(re_intro)

    info = [[sentence_start[idx], sentence_end[idx], word_num[idx], re_intro[idx], sentence[idx]] for idx in range(len(sentence))]

    return np.array(info)

def get_chroma_similarity(filepath: str, lyric_info: np.array) -> np.array:
    chroma, _, _, song_length_sec = create_chroma(filepath)
    num_samples = chroma.shape[1]
    chroma_sr = num_samples / song_length_sec
    chroma_lyric = [chroma[:, int(float(lyric_info[idx, 0]) * chroma_sr): int(float(lyric_info[idx, 1]) * chroma_sr)] for idx in range(lyric_info.shape[0])]
    similarity_chroma = np.zeros((len(chroma_lyric), len(chroma_lyric)))
    for idx1 in range(len(chroma_lyric)):
        for idx2 in range(len(chroma_lyric)):
            if abs(int(lyric_info[idx1][2]) - int(lyric_info[idx2][2])) == 0:   # word num error
                similarity_chroma[idx1, idx2] = 1 - cal_chroma_similarity(chroma_lyric[idx1], chroma_lyric[idx2])
            else:
                similarity_chroma[idx1, idx2] = 0
    return similarity_chroma

def get_similar_seg_matrix(similarity_chroma: np.array) -> np.array:
    similar_seg_matrix = np.zeros((similarity_chroma.shape[0], 10)) - 1
    for idx in range(similarity_chroma.shape[0]):
        flag = 0
        for idx_lyric in range(similarity_chroma.shape[1]):
            current_idx = np.argsort(- similarity_chroma[idx])[idx_lyric]
            if similarity_chroma[idx][current_idx] not in [0, 1] and similarity_chroma[idx][current_idx] >= 0.9:
                #f.write(str(lyric_info[np.argsort(similarity_chroma[idx])[idx_lyric]][-1])+',')
                similar_seg_matrix[idx][flag] = current_idx
                flag += 1
            else:
                pass
            if flag == candidate_limit or similarity_chroma[idx][current_idx] < 0.9 :  #   candidate_limit 相似
                break
    return similar_seg_matrix

def find_seg(idx: list, similar_seg_matrix: np.array, flag_matrix: np.array) -> tuple:
    start_value = int(similar_seg_matrix[idx[0], idx[1]])
    if start_value == -1:
        return (False, [[], []])
    flag_matrix[idx[0], idx[1]] = 1
    seg1 = [start_value,]
    seg2 = [idx[0],]
    flag = False
    for idx1 in range(idx[0]+1, similar_seg_matrix.shape[0]-1):
        if (start_value+1) in similar_seg_matrix[idx1]:
            start_value += 1
            seg1.append(start_value)
            seg2.append(idx1)
            flag = True
            flag_matrix[idx1][np.argwhere(similar_seg_matrix[idx1] == start_value)] = 1
        elif idx1 <= similar_seg_matrix.shape[0] - 2 and (start_value+2) in similar_seg_matrix[idx1+1]:
            start_value += 1
            seg1.append(start_value)
            seg2.append(idx1)
            flag = True
        else:
            break
    if len(seg1) <= 3 or len(seg1) >= 15:
        flag = False

    return (flag, [seg1, seg2])

def get_candidate_paragraph(similar_seg_matrix: np.array) -> np.array:
    flag_matrix = np.zeros_like(similar_seg_matrix)
    seg_candidate = []
    for idx1 in range(similar_seg_matrix.shape[0]):
        for idx2 in range(similar_seg_matrix.shape[1]):
            if flag_matrix[idx1][idx2] == 0:
                save_flag, seg = find_seg([idx1, idx2], similar_seg_matrix, flag_matrix)
                if save_flag == True:
                    seg_candidate.append(seg)
                else:
                    pass
            else:
                pass
    return seg_candidate

def get_root_lyric_line(candidate_paragraph: list, sentence_num: int) -> list:
    def add_node(node_list: np.array, row: int,node: int) -> np.array:
        for idx in range(node_list[row].shape[-1]):
            if node in node_list[row] or node == row: # node == row
                break
            elif node_list[row][idx] == -1:
                node_list[row][idx] = node
            else:
                pass
        return node_list[row]
    '''
    def get_min_index(similar_list: np.array, idx: int) -> int:
        # not all is -1
        # not the min index
        while max(similar_list[idx]) >= 0 and min([int(num) for num in similar_list[idx] if num >= 0]) < idx:
            idx = min([int(num) for num in similar_list[idx] if num >= 0])

        return int(idx)
    '''
    similar_list = np.zeros((sentence_num, candidate_limit)) - 1
    for seg_group in candidate_paragraph:
        seg1, seg2 = seg_group
        for idx in range(len(seg1)):
            similar_list[seg2[idx]] = add_node(similar_list, seg2[idx], seg1[idx])

    return similar_list

def get_seg_point(root_lyric_line: np.array, lyric_info: np.array) -> np.array: #   将问题规约为 “每一句属于上一段还是下一段，或者不切分”
    seg_point = np.zeros(root_lyric_line.shape[0])
    seg_point[0] = 1
    def get_break_and_cont(first_line: np.array, second_line: np.array) -> int:   #   获取上一句间断点
        break_num = 0   #   上下句的间断
        cont_num = 0    #   上下句连续
        for point in first_line:
            if point == -1:
                break
            elif point + 1 in second_line:
                cont_num += 1
            else:
                break_num += 1
        for point in second_line:
            if point == -1:
                break
            elif point - 1 in first_line and point != 0:
                pass
            else:
                break_num += 1
        return break_num, cont_num

    for idx in range(1, root_lyric_line.shape[0]-1):    #   判断当前句属于上段还是下段,又或者当前不切分-----当前句是否切分
        prev_break_num, prev_cont_num = get_break_and_cont(root_lyric_line[idx-1], root_lyric_line[idx])
        #next_break_num, next_cont_num = get_break_and_cont(root_lyric_line[idx], root_lyric_line[idx+1])
        limit_time_conti = 1#   歌曲段落间隔大于5s 切分， 另开新的一段
        if float(lyric_info[idx, -2]) >= limit_time_conti: #   歌曲段落间隔大于5s 切分， 另开新的一段
            seg_point[idx] = 1
            if seg_point[idx-1] == 1 and float(lyric_info[idx, -2]) < float(lyric_info[idx-1, -2]):
                seg_point[idx] = 0
            elif seg_point[idx-1] == 1 and idx != 1:   #   连续分割track
                seg_point[idx-1] = 0
        elif prev_break_num - prev_cont_num > 0:#= 0 and prev_break_num != 0:
            seg_point[idx] = 1
            if seg_point[idx-1] == 1 and idx != 1 and float(lyric_info[idx-1, -2]) < limit_time_conti:   #   连续分割track, 开头避开
                seg_point[idx-1] = 0
            elif seg_point[idx-1] == 1 and idx != 1 and float(lyric_info[idx-1, -2]) >= limit_time_conti:
                seg_point[idx] = 0
            elif idx == 1 and float(lyric_info[idx-1, -2]) < limit_time_conti:
                seg_point[idx] = 0
            else:
                pass
        elif sum(root_lyric_line[idx]) == -10:  #   如果[-1, ..., -1]，则自动与上一句连接
            continue
        else:
            pass

    return seg_point

def get_final_paragraph(songPath: str, lyricPath: str) -> list: #   Origin song , not accompany
    lyric_info = get_lyric_seg(lyricPath)
    similarity_chroma = get_chroma_similarity(songPath, lyric_info)
    similar_seg_matrix = get_similar_seg_matrix(similarity_chroma)
    root_line = get_root_lyric_line(get_candidate_paragraph(similar_seg_matrix), similar_seg_matrix.shape[0])
    seg_point = get_seg_point(root_line, lyric_info)
    final_paragraph = []
    temp_paragraph = []
    for idx, point in enumerate(seg_point):
        if point == 1 and idx == 0: #   开头为1
            temp_paragraph = [float(lyric_info[idx, 0]), float(lyric_info[idx, 1])]
        elif point == 1 and idx != len(seg_point) - 1:  #   歌中的1，不是末尾的1，不是开头1
            temp_paragraph[1] = float(lyric_info[idx-1, 1])
            final_paragraph.append(temp_paragraph)
            temp_paragraph = [float(lyric_info[idx, 0]), float(lyric_info[idx, 1])]
        elif point == 1 and idx == len(seg_point) - 1:  #   末尾的1
            temp_paragraph[1] = float(lyric_info[idx-1, 1])
            final_paragraph.append(temp_paragraph)
            temp_paragraph = [float(lyric_info[idx, 0]), float(lyric_info[idx, 1])]
            final_paragraph.append(temp_paragraph)
        elif idx == len(seg_point) - 1: #   末尾0
            temp_paragraph[1] = float(lyric_info[idx, 1])
            final_paragraph.append(temp_paragraph)
        else:   #   中间0，非开头0，非末尾0
            temp_paragraph[1] = float(lyric_info[idx, 1])
    return final_paragraph

#   歌词中含有歌曲名字 副歌区间开始置信度增加
#

def main():
    songname = '我的好兄弟'   # 因为爱情
    lyric_info = get_lyric_seg(os.path.join(lyric_root, songname + '.txt'))

    #similarity_chroma = get_chroma_similarity(os.path.join(accs_root, songname + '.mp3'), lyric_info)
    similarity_chroma = get_chroma_similarity(os.path.join(songs_root, songname + '.mp2'), lyric_info)

    similar_seg_matrix = get_similar_seg_matrix(similarity_chroma)
    #for idx in range(similar_seg_matrix.shape[0]):
    #    print('{}\t{}'.format(lyric_info[idx][-1], [lyric_info[int(index)][-1] for index in similar_seg_matrix[idx] if index >= 0]))
    #    print('{}\t{}'.format(similarity_chroma[idx][idx], [round(similarity_chroma[idx, int(index)], 3) for index in similar_seg_matrix[idx] if index >= 0]))
    root_line = get_root_lyric_line(get_candidate_paragraph(similar_seg_matrix), similar_seg_matrix.shape[0])
    seg_point = get_seg_point(root_line, lyric_info)
    for idx, point in enumerate(seg_point):
        print('{}\t{}\t{}\t{}\t->{}\t{}'.format(root_line[idx], point, round(float(lyric_info[idx][-2]), 3), round(float(lyric_info[idx][0]), 3), round(float(lyric_info[idx][1]), 3), lyric_info[idx][-1]))
    #print(get_final_paragraph(os.path.join(songs_root, songname + '.mp2'), os.path.join(lyric_root, songname + '.txt')))
    # 相似系数（与后面相似的程度） & 包含系数（副歌句子出现的位置必定是副歌）
    # 两个系数 相互trade-off 达到nash-balance
    # 部分地方整句拆开  脑袋都是你心里都是你 脑袋都是你/心里都是你

    # 相似矩阵的最大索引越界

if __name__ == "__main__":
    main()


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 14:52:26 2018

@author: arje
"""

import utility
import numpy as np
from sklearn import metrics
import cloudpickle as pickle
import dim2_keras as dim2

"""
hyper-parameters
"""
learning_rate = [0.001, 0.0001,0.00001, 0.000001] #parameter used in gradient descent optimization
epochs_schedule = [50, 50, 50, 50] #how many times to go over the training obs during optimizing
regularizer = 0.0001 #?
target_shape = [64, 64] # the size of the patches given to the network
batch_size = 128 #the number of training obs to use at once

"""
data
"""
train_images = np.load('./data/train_new.npy') #which training set to load
train_annotation = '../data/train_2class_new.txt' #img id, coordinates of bb, class
val_images = np.load('../data/val_new.npy') #which validation set to load
val_annotation = '../data/val_2class_new.txt'
test_images = np.load('../data/test_new.npy')
test_annotation = '../data/test_2class_new.txt'

#Creates generators for object patches
train_gen, train_steps = utility.get_patch_generator_1class_new(train_images, train_annotation, target_shape, batch_size, True) #creates train patches from the original images
val_gen, val_steps = utility.get_patch_generator_1class_new(val_images, val_annotation, target_shape, batch_size, False)

"""
Model setup
"""
#Read in the model structure
model = dim2.dim2_model((target_shape[0],target_shape[1],3), 2)
#model = md.get_flower_model((target_shape[0],target_shape[1],3),2)

#Initialize the model
current_weights = model.get_weights()
optimal_weights = model.get_weights() #initialization
optimal_val_acc = 0
cb = utility.CustomCallback()
history = {}

""" network training """

for lr, epoch in zip(learning_rate, epochs_schedule): #zip goes over two lists at the same time

    model.optimizer.lr = lr #sets learning rate for model
    model.set_weights(current_weights) #sets weights for model

    h = model.fit_generator(train_gen, train_steps, epoch, callbacks=[cb,],
                            validation_data=val_gen, validation_steps=val_steps) #a generator is used so we don't keep unnecessary things in memory

    history = utility.merge_history(history, h.history) #history returns training and validation loss and other metrics, so basically the fit of the model

    if optimal_val_acc < cb.monitor_quantity:
        optimal_weights = cb.model_weights #saves weights of the best model

    current_weights = model.get_weights() #updates the model weights and goes to next lr & epoch

"""
evaluate the best model with validation set
"""
model.set_weights(optimal_weights) #use the best possible weights
val_gen, val_steps = utility.get_patch_generator_1class_scale(val_images, val_annotation, target_shape, batch_size, False) #same as before

y_pred = np.zeros((1,2)) #initialize zero arrays of length 5
y_true = np.zeros((1,2))

for iteration in range(val_steps):
    #python 3 version:
    x, y = next(val_gen)
    #x, y = val_gen.next() #gives values for these both
    y_true = np.concatenate((y_true, y), axis=0) #concatenate adds the new value to the array
    y_pred = np.concatenate((y_pred, model.predict(x)), axis=0)

y_true = np.argmax(y_true[1:], axis=-1) #leaves out the first row and searches maximums for all other rows, y_true's are arrays with 1 for the true class and 0 for the others
y_pred = np.argmax(y_pred[1:], axis=-1) #y_pred's are arrays with the probabilities of all classes. This takes just the most probable label.

confusion = metrics.confusion_matrix(y_true, y_pred)
performance = {'val_confusion':confusion} #creating dictionary
performance['val_accuracy'] = metrics.accuracy_score(y_true, y_pred) #adding to the dictionary
performance['val_precision'] = metrics.precision_score(y_true, y_pred, average='macro')
performance['val_recall'] = metrics.recall_score(y_true, y_pred, average='macro')
performance['val_f1'] = metrics.f1_score(y_true, y_pred, average='macro')
performance['history'] = history
performance['weights'] = optimal_weights

"""
evaluate the best model with test set
"""
model.set_weights(optimal_weights) #use the best possible weights
test_gen, test_steps = utility.get_patch_generator_1class_scale(test_images, test_annotation, target_shape, batch_size, False) #same as before

y_pred = np.zeros((1,2)) #initialize zero arrays of length 5
y_true = np.zeros((1,2))

for iteration in range(test_steps):
    #python 3 version:
    x, y = next(test_gen)
    #x, y = test_gen.next() #gives values for these both
    y_true = np.concatenate((y_true, y), axis=0) #concatenate adds the new value to the array
    y_pred = np.concatenate((y_pred, model.predict(x)), axis=0)

y_true = np.argmax(y_true[1:], axis=-1) #leaves out the first row and searches maximums for all other rows, y_true's are arrays with 1 for the true class and 0 for the others
y_pred = np.argmax(y_pred[1:], axis=-1) #y_pred's are arrays with the probabilities of all classes. This takes just the most probable label.

confusion = metrics.confusion_matrix(y_true, y_pred)
performance['test_confusion'] = confusion #creating dictionary
performance['test_accuracy'] = metrics.accuracy_score(y_true, y_pred) #adding to the dictionary
performance['test_precision'] = metrics.precision_score(y_true, y_pred, average='macro')
performance['test_recall'] = metrics.recall_score(y_true, y_pred, average='macro')
performance['test_f1'] = metrics.f1_score(y_true, y_pred, average='macro')

print(performance['val_accuracy'])
print(performance['val_confusion'])
print(performance['test_accuracy'])
print(performance['test_confusion'])

output_file = './result/model_2class_dim_keras_new.pickle'
fid = open(output_file,'wb')
pickle.dump(performance, fid) #just a way of saving the performance results
fid.close()


def hcf(m, n):
	if m < n:
		return hcf(n, m)
	elif m % n == 0:
		return n
	else:
		return hcf(n, m%n)

def locate(pair, lst):
	if not lst:
		return 0
	mid = len(lst) // 2
	mid_pair = lst[mid]
	diff = mid_pair[0]*pair[1]-mid_pair[1]*pair[0]
	if not diff:
		return mid
	elif diff > 0:
		return locate(pair, lst[:mid])
	else:
		return mid+1 + locate(pair, lst[mid+1:])

def fractions(d):
	lst = list()
	def helper(d_):
		nonlocal lst
		for i in range(1, d_):
			if hcf(i, d_) != 1:
				pass
			else:
				pos = locate((i, d_), lst)
				lst = lst[:pos] + [(i, d_)] + lst[pos:]
		print(lst)
		if d_ > 2:
			helper(d_-1)
		return lst
	return helper(d)

print(fractions(10**6))


import datetime
from . import errors

# User-configurable functions and variables available in all contexts.
builtins = {
    'range': range,
}

# A wrapper around a stack of dictionaries.
class DataStack:
    def __init__(self):
        self.stack = []

    def __getitem__(self, key):
        for d in reversed(self.stack):
            if key in d:
                return d[key]
        raise KeyError(key)

# A Context object is a wrapper around the user's input data. Its `.resolve()` method contains
# the lookup-logic for resolving dotted variable names.
class Context:
    def __init__(self, data_dict, template):

        # Data store of resolvable variable names for the .resolve() method.
        self.data = DataStack()

        # Standard builtins.
        self.data.stack.append(
            {'context': self, 'is_defined': self.is_defined,}
        )

        # User-configurable builtins.
        self.data.stack.append(builtins)

        # Instance-specific data.
        self.data.stack.append(data_dict)

        # Nodes can store state information here to avoid threading issues.
        self.stash = {}

        # This reference gives nodes access to their parent template object.
        self.template = template

    def __setitem__(self, key, value):
        self.data.stack[-1][key] = value

    def __getitem__(self, key):
        return self.data[key]

    def push(self, data=None):
        self.data.stack.append(data or {})

    def pop(self):
        self.data.stack.pop()

    def get(self, key, default=None):
        for d in reversed(self.data.stack):
            if key in d:
                return d[key]
        return default

    def update(self, data_dict):
        self.data.stack[-1].update(data_dict)

    def resolve(self, varstring, token):
        words = []
        result = self.data
        for word in varstring.split('.'):
            words.append(word)
            if hasattr(result, word):
                result = getattr(result, word)
            else:
                try:
                    result = result[word]
                except:
                    try:
                        result = result[int(word)]
                    except:
                        msg = f"Cannot resolve the variable '{'.'.join(words)}'"
                        raise errors.UndefinedVariable(msg, token) from None
        return result

    def is_defined(self, varstring):
        current = self.data
        for word in varstring.split('.'):
            if hasattr(current, word):
                current = getattr(current, word)
            else:
                try:
                    current = current[word]
                except:
                    try:
                        current = current[int(word)]
                    except:
                        return False
        return True


from enum import Enum
import json

class PROTOCOL(Enum):
    """
    游戏协议定义
    根据实际游戏协议扩展
    """

    C2SHeartbeat = 100  # 心跳包
    C2SSignup = 101  # 报名参加游戏
    C2SStart = 102  # 游戏开始
    C2SGameover = 103  # 主播主动结束游戏
    C2SPlayerScore = 104  # 玩家上报得分

    S2CHeartbeat = 200  # 心跳响应包
    S2CRoomClosed = 201  # 游戏房间已经结束关闭
    S2CGameStart = 202  # 游戏开始
    S2CGameOver = 203  # 游戏结束
    S2CPlayerJoin = 204  # 玩家进入
    S2CPlayerSignup = 205  # 玩家报名
    S2CRealtimeRank = 206  # 实时分数排行榜
    S2CTeams = 207  # 队伍信息


class Solution:
    def similarRGB(self, color):
        """
        :type color: str
        :rtype: str
        """
        code = ['00', '11', '22', '33', '44', '55', '66', '77', '88', '99',
                'aa', 'bb', 'cc', 'dd', 'ee', 'ff']

        def closest(color):
            pos = bisect.bisect(code, color)
            if pos == 0:
                return code[0]
            if pos == len(code):
                return code[-1]
            res = min([pos - 1, pos], key=lambda x: abs(int(color, base=16) - int(code[x], base=16)))
            return code[res]

        return '#' + closest(color[1:3]) + closest(color[3:5]) + closest(color[5:7])


# Write A Program To Obtain The Average and Standard Deviation of 5 Numbers

# Import Numpy for Numerical Calculations
import numpy as np

# Initiating an empty list
nums = []

# Creating a loop to ask 5 user inputs
for i in range(5):
    # Appending the empty list with the user inputs.
    nums.append(float(input(str(i+1) + ". Enter the numner: ")))

# Printing the average of the numbers input by the user.
print("The average of the 5 numbers is:", np.mean(nums))

# Printing the standard deviation of the numbers input by the user.
print("The standard deviation of the 5 numbers is:", np.std(nums))

Python/Python Programs/Average & Standard Deviation of 5 Numbers.py