from django.views.generic import View
from apps.BaseView import BaseView
from django.shortcuts import render
from django.core.paginator import Paginator
from apps.SchoolRecord.models import SchoolRecord
from django.http import JsonResponse
from django.http import FileResponse
from apps.BaseView import ImageFormatException
from django.conf import settings
import pandas as pd
import os

class FrontAddView(BaseView):  # 前台学历信息添加
    def get(self,request):

        # 使用模板
        return render(request, 'SchoolRecord/schoolRecord_frontAdd.html')

    def post(self, request):
        schoolRecord = SchoolRecord() # 新建一个学历信息对象然后获取参数
        schoolRecord.schoolRecordName = request.POST.get('schoolRecord.schoolRecordName')
        schoolRecord.save() # 保存学历信息信息到数据库
        return JsonResponse({'success': True, 'message': '保存成功'})

class FrontModifyView(BaseView):  # 前台修改学历信息
    def get(self, request, schoolRecordId):
        context = {'schoolRecordId': schoolRecordId}
        return render(request, 'SchoolRecord/schoolRecord_frontModify.html', context)

class FrontListView(BaseView):  # 前台学历信息查询列表
    def get(self, request):
        return self.handle(request)

    def post(self, request):
        return self.handle(request)

    def handle(self, request):
        self.getCurrentPage(request)  # 获取当前要显示第几页
        # 下面获取查询参数
        # 然后条件组合查询过滤
        schoolRecords = SchoolRecord.objects.all()
        # 对查询结果利用Paginator进行分页
        self.paginator = Paginator(schoolRecords, self.pageSize)
        # 计算总的页码数，要显示的页码列表，总记录等
        self.calculatePages()
        # 获取第page页的Page实例对象
        schoolRecords_page = self.paginator.page(self.currentPage)

        # 构造模板需要的参数
        context = {
            'schoolRecords_page': schoolRecords_page,
            'currentPage': self.currentPage,
            'totalPage': self.totalPage,
            'recordNumber': self.recordNumber,
            'startIndex': self.startIndex,
            'pageList': self.pageList,
        }
        # 渲染模板界面
        return render(request, 'SchoolRecord/schoolRecord_frontquery_result.html', context)

class FrontShowView(View):  # 前台显示学历信息详情页
    def get(self, request, schoolRecordId):
        # 查询需要显示的学历信息对象
        schoolRecord = SchoolRecord.objects.get(schoolRecordId=schoolRecordId)
        context = {
            'schoolRecord': schoolRecord
        }
        # 渲染模板显示
        return render(request, 'SchoolRecord/schoolRecord_frontshow.html', context)

class ListAllView(View): # 前台查询所有学历信息
    def get(self,request):
        schoolRecords = SchoolRecord.objects.all()
        schoolRecordList = []
        for schoolRecord in schoolRecords:
            schoolRecordObj = {
                'schoolRecordId': schoolRecord.schoolRecordId,
                'schoolRecordName': schoolRecord.schoolRecordName,
            }
            schoolRecordList.append(schoolRecordObj)
        return JsonResponse(schoolRecordList, safe=False)

class UpdateView(BaseView):  # Ajax方式学历信息更新
    def get(self, request, schoolRecordId):
        # GET方式请求查询学历信息对象并返回学历信息json格式
        schoolRecord = SchoolRecord.objects.get(schoolRecordId=schoolRecordId)
        return JsonResponse(schoolRecord.getJsonObj())

    def post(self, request, schoolRecordId):
        # POST方式提交学历信息修改信息更新到数据库
        schoolRecord = SchoolRecord.objects.get(schoolRecordId=schoolRecordId)
        schoolRecord.schoolRecordName = request.POST.get('schoolRecord.schoolRecordName')
        schoolRecord.save()
        return JsonResponse({'success': True, 'message': '保存成功'})

class AddView(BaseView):  # 后台学历信息添加
    def get(self,request):

        # 渲染显示模板界面
        return render(request, 'SchoolRecord/schoolRecord_add.html')

    def post(self, request):
        # POST方式处理图书添加业务
        schoolRecord = SchoolRecord() # 新建一个学历信息对象然后获取参数
        schoolRecord.schoolRecordName = request.POST.get('schoolRecord.schoolRecordName')
        schoolRecord.save() # 保存学历信息信息到数据库
        return JsonResponse({'success': True, 'message': '保存成功'})

class BackModifyView(BaseView):  # 后台更新学历信息
    def get(self, request, schoolRecordId):
        context = {'schoolRecordId': schoolRecordId}
        return render(request, 'SchoolRecord/schoolRecord_modify.html', context)

class ListView(BaseView):  # 后台学历信息列表
    def get(self, request):
        # 使用模板
        return render(request, 'SchoolRecord/schoolRecord_query_result.html')

    def post(self, request):
        # 获取当前要显示第几页和每页几条数据
        self.getPageAndSize(request)
        # 收集查询参数
        # 然后条件组合查询过滤
        schoolRecords = SchoolRecord.objects.all()
        # 利用Paginator对查询结果集分页
        self.paginator = Paginator(schoolRecords, self.pageSize)
        # 计算总的页码数，要显示的页码列表，总记录等
        self.calculatePages()
        # 获取第page页的Page实例对象
        schoolRecords_page = self.paginator.page(self.currentPage)
        # 查询的结果集转换为列表
        schoolRecordList = []
        for schoolRecord in schoolRecords_page:
            schoolRecord = schoolRecord.getJsonObj()
            schoolRecordList.append(schoolRecord)
        # 构造模板页面需要的参数
        schoolRecord_res = {
            'rows': schoolRecordList,
            'total': self.recordNumber,
        }
        # 渲染模板页面显示
        return JsonResponse(schoolRecord_res, json_dumps_params={'ensure_ascii':False})

class DeletesView(BaseView):  # 删除学历信息信息
    def get(self, request):
        return self.handle(request)

    def post(self, request):
        return self.handle(request)

    def handle(self, request):
        schoolRecordIds = self.getStrParam(request, 'schoolRecordIds')
        schoolRecordIds = schoolRecordIds.split(',')
        count = 0
        try:
            for schoolRecordId in schoolRecordIds:
                SchoolRecord.objects.get(schoolRecordId=schoolRecordId).delete()
                count = count + 1
            message = '%s条记录删除成功！' % count
            success = True
        except Exception as e:
            message = '数据库外键约束删除失败！'
            success = False
        return JsonResponse({'success': success, 'message': message})

class OutToExcelView(BaseView):  # 导出学历信息信息到excel并下载
    def get(self, request):
        # 收集查询参数
        # 然后条件组合查询过滤
        schoolRecords = SchoolRecord.objects.all()
        #将查询结果集转换成列表
        schoolRecordList = []
        for schoolRecord in schoolRecords:
            schoolRecord = schoolRecord.getJsonObj()
            schoolRecordList.append(schoolRecord)
        # 利用pandas实现数据的导出功能
        pf = pd.DataFrame(schoolRecordList)
        # 设置要导入到excel的列
        columns_map = {
            'schoolRecordId': '记录编号',
            'schoolRecordName': '学历名称',
        }
        pf = pf[columns_map.keys()]
        pf.rename(columns=columns_map, inplace=True)
        # 将空的单元格替换为空字符
        pf.fillna('', inplace=True)
        #设定文件名和导出路径
        filename = 'schoolRecords.xlsx'
        # 这个路径可以在settings中设置也可以直接手动输入
        root_path = settings.MEDIA_ROOT + '/output/'
        file_path = os.path.join(root_path, filename)
        pf.to_excel(file_path, encoding='utf-8', index=False)
        # 将生成的excel文件输出到网页下载
        file = open(file_path, 'rb')
        response = FileResponse(file)
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="schoolRecords.xlsx"'
        return response


import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# 设置摄像头分辨率
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

fourcc = cv2.VideoWriter_fourcc(*'mp4v')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter('/home/eaibot71/test1/photo_video/output.mp4',fourcc, 5.0, (width,height))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,1)
#        out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))   将视频转换为灰色的源
        out.write(frame)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'): # 若按下Q键，则退出循环
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()


"""Create users"""

import pymongo
import fire
import json
import glob
import os
from pymongo.errors import DuplicateKeyError

def create_table(db, drop):
    if db.users.count() > 0:
        if drop:
            db.users.drop()
        else:
            print("Colección users ya existe -- saliendo")
            return
    db.users.create_index([('id', pymongo.ASCENDING)], unique=True)
    db.users.create_index([('provincia', pymongo.ASCENDING)], unique=False)

    print("Colección de usuarios creada")
    print("Índices:")

    for k, v, in db.users.index_information().items():
        print("{} ---> configuración {}".format(k, v))

def create_users_collection(path_to_tweets, database_name="contrastes", drop=False):
    client = pymongo.MongoClient('localhost')
    db = client[database_name]

    create_table(db, drop)

    """
    Vamos a sacar la info del campo user de los tweets que ya tenemos
    """
    directories = glob.glob(os.path.join(path_to_tweets, "**/"))
    print(directories)
    user_count = 0
    for path_to_province in directories:

        provincia = path_to_province.split("/")[-2]
        print("="*80)
        print(provincia)
        # Choose a sample of them
        files = glob.glob(os.path.join(path_to_province, "*.json"))

        current_user_id = None
        for tweets_file in files:
            tweets = json.load(open(tweets_file))

            for tweet in tweets:
                if current_user_id == tweet["user"]["id"]:
                    continue
                else:
                    current_user_id = tweet["user"]["id"]
                doc = tweet["user"]
                doc["provincia"] = provincia

                try:
                    db.users.insert_one(doc)
                    user_count += 1
                except DuplicateKeyError as e:
                    continue

                if user_count % 500 == 0:
                    print("{:.1f}K usuarios procesados".format(user_count / 1000))

    print("{} usuarios han sido creados ".format(db.users.count()))
if __name__ == '__main__':
    fire.Fire(create_users_collection)


import time

def largest_prime_factor(n):
	i = 2
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
	return n

def main():
	print("The result is %d." % largest_prime_factor(600851475143))

if __name__ == '__main__':
	start = time.time()
	main()
	done = time.time()
	print("The solution took %.4f seconds to compute." % (done - start))


pokemon_count = int(input())
highest_level = 0
highest_level_pokemons = []
lowest_level = 9999999
lowest_level_pokemons = []
for _ in range(pokemon_count):
    name, level = input().split()
    level = int(level)
    if level == highest_level:
        highest_level_pokemons.append(name)
    elif level > highest_level:
        highest_level = level
        highest_level_pokemons = [name]

    if level == lowest_level:
        lowest_level_pokemons.append(name)
    elif level < lowest_level:
        lowest_level = level
        lowest_level_pokemons = [name]

"O maior level é LMax, do(s) pokemon(s) P1, P2 ... Pn. E o menor é Lmin, do(s) pokemon(s) P1, P2, ... Pn"
print(f"O maior level é {highest_level}, do(s) pokemon(s) ", end="")
for pokemon in highest_level_pokemons[:-1]:
    print(pokemon, end=", ")
print(highest_level_pokemons[-1], end=". ")
print(f"E o menor é {lowest_level}, do(s) pokemon(s) ", end="")
for pokemon in lowest_level_pokemons[:-1]:
    print(pokemon, end=", ")
print(lowest_level_pokemons[-1])


import sys
MAXNUM = 1001

if __name__ == "__main__":
    N = int(input())
    M = int(input())
    cnt = 0

    d = [0] * MAXNUM
    pre = [0] * MAXNUM
    depth = [0] * MAXNUM
    cost = [0] * MAXNUM
    adj = [[] for _ in range(MAXNUM)]

    for i in range(M):
        p, q, r = map(int, sys.stdin.readline().split())
        if p == 1:
            d[q] = r
            pre[q] = 1
        else:
            adj[p].append((r,q))
            depth[q] += 1

    i = 1
    while i <= N:
        if depth[i] > 0:
            i += 1
        else:
            for j in range(len(adj[i])):
                nv, nx = adj[i][j]
                if d[nx] < d[i] + nv:
                    d[nx] = d[i] + nv
                    pre[nx] = i
                depth[nx] -= 1
            depth[i] = 1
            i = 0

    cost[cnt] = 1
    cnt += 1

    while pre[1] != 1:
        cost[cnt] = pre[1]
        cnt += 1
        pre[1] = pre[pre[1]]
    cost[cnt] = 1
    cnt += 1

    print(d[1])
    for i in range(cnt - 1, -1, -1):
        print(cost[i], end=' ')


#!/usr/bin/env python3
"""
@author: Timothy Baker
@date: 01-30-2019

1D.py

Input:
    kmer number \n
    motif sequence 1 \n
    motif sequence n \n
Output:
    Best Motifs: motif1...motifn \n
    Consensus Sequence: consensus motif \n
"""

import sys
import time
import functools
import operator

def generate_kmer(dna_sequence, kmer_length):
    """ Takes a DNA sequence and generates all kmer substrings of the sequence
        Args:
            dna_sequence (str) : must be ACGT; no RNA transcripts
            kmer_length (int) : default is set to 3 for codon, but can be changed
        Returns:
            a list of all possible kmers
    """

    kmer_list = [] # initializes the kmer list; order is important

    # iterates through the sequence creating a kmer length sliding window
    if isinstance(dna_sequence, list):
        for seq in dna_sequence:
            for i in range(len(seq) - kmer_length + 1):
                # appends each kmer to the kmer list
                kmer_list.append(seq[i:i+kmer_length])

    if isinstance(dna_sequence, str):
        for i in range(len(dna_sequence) - kmer_length + 1):
            # appends each kmer to the kmer list
            kmer_list.append(dna_sequence[i:i+kmer_length])

    return kmer_list

def hamming_distance(sequence_one, sequence_two):
    """ Calculates hamming distance between 2 strings
        Args:
            sequence_one (str) : sequence of ACGT
            sequence_two (str) : sequence of ACGT
        Returns:
            Hamming distance (int) : number of mismatches between 2 strings
        Raises:
            AssertionError : if sequence lengths are un equal
    """

    assert len(sequence_one) == len(sequence_two), "Sequence lengths are unequal"

    count = 0 #initialize the count to 0
    for n_1, n_2 in zip(sequence_one, sequence_two): #iterates through 2 sequences
        if n_1 != n_2: #checks equality against each nucleotide
            count += 1 # if not equal adds 1 to the count
    return count

def construct_best_motif_list(dna_sequence_list, kmer_length):
    """ constructs a motif matrix of each first kmer from
        the dna_sequence_list
        Args:
            dna_sequence_list (lst) : list of nucleotide strings
            kmer_length (int) : kmer length
            best (bool) : selects the first kmer from each kmer array
        Returns:
            kmer_motif_matrix (txk matrix) : first kmer from each string
                forming a t x k matrix
    """

    kmer_matrix = []
    best_kmer_motif = []

    for seq in dna_sequence_list:
        kmer_matrix.append(generate_kmer(seq, kmer_length))

    # this selects the first kmer from each array
    for kmer_array in enumerate(kmer_matrix):
        best_kmer_motif.append(kmer_matrix[kmer_array[0]][0])

    return best_kmer_motif

def build_profile(motif_list):
    """ generates a dictionary with frequency values at each position
        Args:
            motif_list (lst) : list of sequences of equal length
        Returns:
            profile (dict) : returns a dict with probability values
                keys are nucleotides, should only ever be 4 keys

    """

    k = len(motif_list[0])

    profile = {'A':[0]*k, 'C':[0]*k, 'G':[0]*k, 'T':[0]*k}

    div = float(len(motif_list))

    for i in range(k):
        for motif in motif_list:
            profile[motif[i]][i] += 1
        for key in profile:
            profile[key][i] /= div

    return profile

def prod(factors):
    """ reduces the multipled probability of the probability matrix to 1 or 0"""
    return functools.reduce(operator.mul, factors, 1)

def return_kmer_most_prob(kmer_list, prob_matrix):
    """ takes a list of kmers, and a probability matrix, and returns
        a dict of each kmer, and uses the prod function to label the
        kmer with the highest probability (1)
        Args:
            kmer_list (lst) : list of kmers of equal length
            prob_matrix (dict) : dict from build_profile function with the
            4 nucleotides as they keys and a list of probabilities to be
            multiplied
        Returns:
            dict with kmers as keys, and the value 1 or 0 depending on the
            highest multipled probability.
    """
    return {
        kmer: prod(
            prob_matrix[base][i]
            for i, base in enumerate(kmer)
        )
        for kmer in kmer_list
    }

def build_profile_probable(dna_sequence, kmer_length, prob_matrix):
    """ takes in a sequence, generates the kmers of that sequence, and
        a probabilitiy matrix.
        Utilizes return_kmer_most_prob that multiples the probabilities
        and reduces to a single value either 0 or 1.
        Args:
            dna_sequence (str) : sequence
            kmer_length (int) : kmer_length
            prob_matrix (dict of lsts) : typically from build_profile function
            4 nucleotides with a list of probabilities
        Returns:
            most_prob_motif (str) : the key (kmer) with 1 as the value

    """

    kmer_list = generate_kmer(dna_sequence, kmer_length)

    most_prob_kmer = return_kmer_most_prob(kmer_list, prob_matrix)

    most_prob_motif = max(most_prob_kmer.items(), key=operator.itemgetter(1))[0]

    return most_prob_motif

def find_consensus_sequence(probability_matrix):
    """generates a consensus sequence given a frequency dictionary of lists
        Args:
            frequency_matrix (dict of lists) : probability matrix

        Returns:
            consensus (str) : baesd on the probability matrix, constructs a
            consensus string
    """

    consensus = ''
    dna_length = len(probability_matrix['A'])

    for i in range(dna_length):  # loop over positions in string
        max_freq = -1            # holds the max freq. for this i
        max_freq_base = None     # holds the corresponding base

        for base in 'ACGT':
            if probability_matrix[base][i] >= max_freq:
                max_freq = probability_matrix[base][i]
                max_freq_base = base
            elif probability_matrix[base][i] == max_freq:
                max_freq_base = '-' # more than one base as max

        consensus += max_freq_base  # add new base with max freq
    return consensus

def score_motifs(prob_matrix, motif_list):
    """ takes in probability matrix, and a motif list, and returns a score
        calculates the hamming distance between each motif and the consensus
        sequence.
        Args:
            prob_matrix (dict of lists) : prob matrix with 4 nucleotide keys
            and a list of probabilities
            motif_list (lst) : list of motifs of equal length
        Returns:
            score (int) : the summation of the hamming_distance between the consensus
            sequence and each motif

    """
    score = 0

    consensus_sequence = find_consensus_sequence(prob_matrix)

    for motif in motif_list:
        score += hamming_distance(consensus_sequence, motif)

    return score

def greedy_motif_search(dna_seq_list, kmer_length):
    """ takes in a list of dna_sequences, and a desired kmer_length, returns
        an array of kmers with the lowest hamming distance score between
        a constructed consensus sequence and motifs
        Args:
            dna_seq_list (lst) : list of sequences
            kmer_length (int) : desired kmer length, cannot be longer
            than the sequences entered
        Returns:
            best_motif_array (lst) : an array of the lowest scoring motifs

    """

    t_length = len(dna_seq_list)

    # initializing best motifs by taking the first kmer from each sequence
    # and forming a probability matrix, and score
    best_motif_array = construct_best_motif_list(dna_seq_list, kmer_length)
    best_motif_prob_matrix = build_profile(best_motif_array)
    best_motif_score = score_motifs(best_motif_prob_matrix, best_motif_array)

    # iterating through each kmer of the first sequence in the list
    for kmer_motif in generate_kmer(dna_seq_list[0], kmer_length):

        # initializing each kmer from the first sequence to an array
        kmer_motif_array = [kmer_motif]

        # iterating through the second to the last sequence
        for i in range(1, t_length):

            # first pass generates a probability matrix using a single kmer from
            # the first element in list from the generate_kmer function
            # at each iterating pass, the kmer most probable is added to the
            # kmer_motif_array, updating the probability matrix to be more accurate
            prob_matrix = build_profile(kmer_motif_array)

            most_prob_kmer_motif = build_profile_probable(dna_seq_list[i], kmer_length, prob_matrix)

            kmer_motif_array.append(most_prob_kmer_motif)

        # constructs a score for each kmer_motif_array after reaching the t_length
        # and compares the score against best_motif_score, if less than, the best
        # scoring motif array is updated with the lowest score.
        new_score = score_motifs(prob_matrix, kmer_motif_array)
        if new_score < best_motif_score:
            best_motif_array = kmer_motif_array
            best_motif_score = new_score

    return best_motif_array

def main():
    """ runs mains script """

    lines = sys.stdin.read().splitlines()

    kmer_length = int(lines[0])

    motif_sequence_list = lines[1:]

    motif_array = greedy_motif_search(motif_sequence_list, kmer_length)

    consensus_seq = find_consensus_sequence(build_profile(motif_array))

    with open('1D-output.txt', 'w') as output:
        output.write("Best motifs: " + \
        " ".join(motif_array) + \
        '\n' + \
        "Consensus Sequence: " + \
        str(consensus_seq) + '\n')

if __name__ == '__main__':
    START_TIME = time.time()
    main()
    print(("--- %s seconds ---" % (time.time() - START_TIME)))


import matplotlib.pyplot as plt
import csv
import pandas as pd
import os

def summary(folder,mix):

    if folder == '3x4':
        city = 'jinan'
    elif folder == '4x4':
        city ='Hangzhou'
    elif folder == '6x6_1':
        city ='syn_1'
    elif folder == '6x6_2':
        city ='syn_2'
    elif folder == '6x6_3':
        city ='syn_3'
    elif folder == '6x6_4':
        city ='syn_4'
    elif folder == '6x6_5':
        city ='syn_5'
    elif folder == '6x6_6':
        city ='syn_6'
    elif folder == '6x6_7':
        city ='syn_7'
    elif folder == '6x6_8':
        city ='syn_8'
    elif folder == '16x3':
        city ='newyork'
    else:
        raise Exception ('Wrong City')

    if mix:
        mix = 'mix'
    else:
        mix = 'same'

    single =    mix + '_' + 'basic'     + '_' + city + '_200.csv'
    colight =   mix + '_' + 'colight'   +'_'  + city + '_159.csv'
    frap =      mix + '_' + 'frap'      + '_' + city + '_200.csv'
    mixlight =  mix + '_' + 'mixlight'  + '_' + city + '_200.csv'
    mplight =   mix + '_' + 'mplight'   + '_' + city + '_200.csv'

    colight_df = pd.read_csv(os.path.join(folder,colight), header= None, names=['episode', 'time','NAN'])
    colight_y = colight_df['time'].values.tolist()
    colight_x = [i for i in range(1,len(colight_y)+1)]

    frap_df = pd.read_csv(os.path.join(folder,frap), header= None, names=['episode', 'time','NAN'])
    frap_y = frap_df['time'].values.tolist()
    frap_x = [i for i in range(1,len(frap_y)+1)]

    mplight_df = pd.read_csv(os.path.join(folder,mplight), header= None, names=['episode', 'time','NAN'])
    mplight_y = mplight_df['time'].values.tolist()
    mplight_x = [i for i in range(1,len(mplight_y)+1)]

    mixlight_df = pd.read_csv(os.path.join(folder,mixlight))#header= None, names=['episode','time','total','complete','ratio','stops'])
    mixlight_y = mixlight_df['avg_travel_time'].values.tolist()
    mixlight_x = [i for i in range(1,len(mixlight_y)+1)]

    single_df = pd.read_csv(os.path.join(folder,single))#header= None, names=['episode','time','total','complete','ratio','stops'])
    single_y = single_df['avg_travel_time'].values.tolist()
    single_x = [i for i in range(1,len(single_y)+1)]

    plt.plot(colight_x, colight_y,    color='C1', linewidth=1.5, linestyle='--',   label='CoLight')
    plt.plot(frap_x, frap_y,          color='b',  linewidth=1.5,  linestyle='-',   label='FRAP')
    plt.plot(mplight_x, mplight_y,    color='c',  linewidth=1.5,  linestyle='--',    label='MPLight')
    plt.plot(mixlight_x, mixlight_y,  color='r',  linewidth=1.5,  linestyle='-',    label='MixLight')
    plt.plot(single_x, single_y,      color='g',  linewidth=1.5,  linestyle='--',label='Baisc')

    plt.grid(True)
    plt.axis([0, 100, 1000, 1500])

    plt.xlabel('episode')
    plt.ylabel('avg_travel_time')
    plt.legend()

    if mix == 'mix':
        fig_title = 'fig_mix_env_'+ city + '.png'
        plt_title = 'Mixed type agent environment -' + city
    else:
        fig_title = 'fig_same_env_' + city + '.png'
        plt_title = 'Single type agent environment -' + city

    plt.title(plt_title)

    log_dir = os.path.join(fig_title)

    plt.savefig(log_dir)

    print('Finished!')

if __name__ == '__main__':

    folder = '16x3'
    mix = True

    summary(folder,mix)


x = int(input("please input integer: "))

if x < 0:
  x = 0
  print('negative number is assumed to be zero')
elif  x == 0:
  print('zero')
elif x == 1:
      print('one')
else:
     print('more')


from django.urls import path
from eventos.views import *

urlpatterns= [
    # Paths de Eventos
    # path('evento/listar', eventosList.as_view(), name='eventos_listar'),
    path('evento/listar', eventos_lista, name='eventos_listar'),
    path('evento/nuevo', eventoCreate.as_view(), name='evento_crear'),
    path('evento/editar/<pk>', eventoUpdate.as_view(), name='evento_editar'),
    path('evento/eliminar/<pk>', eventoDelete.as_view(), name='evento_eliminar'),
    ]


import os
from mingus.midi import fluidsynth
from mingus.containers import Note

# Este script se emplea para crear los archivos de audio con los que se va a trabajar y se va
# a entrenar y a validar la SNN. Con este script y el archivo que genera los acordes (llamado
# audio_chords_input) se crearán una serie de archivos que constituirán una base de datos única
# y propia para realizar los experimentos a los que va destinado este proyecto de fin de grado.
# Los arhivos de audio generados, de tipo .wav, se almacenarán en las carpetas denominadas
# audios_train y audios_validation.

# Para entrenar la red se empleará el audio de una octava completa, con los 12 sonidos que la componen.
# Para realizar la validación se presentarán diferentes audios de características variadas.

music_seq = {}
# Escala 12 semitonos en la octava 4 para el entrenamiento
music_seq['scale12oct4'] = ['C-4','C#-4','D-4','D#-4','E-4','F-4','F#-4','G-4','G#-4','A-4','A#-4','B-4']
# Escala 7 tonos
music_seq['scale7oct5'] = ['B-5','A-5','G-5','F-5','E-5','D-5','C-5']
# Melodía básica 1: Arpegios
music_seq['melody1oct2'] = ['G-2','E-2','C-2','A-2','F-2','D-2']
# Melodía básica 2: Principio versionado transpuesto del Fur Ellise, de Beethoven
music_seq['melody2oct6'] = ['G-6','F#-6','G-6','F#-6','G-6','D-6','F-6','D#-6','C-6']
# Melodía básica 3: Invención propia
music_seq['melody3oct4'] = ['C-4','A#-4','G#-4','E-4','F-4','G#-4','G-4','D-4','D#-4','G-4','F-4','C-4','D-4','B-4','B-4']
# Melodía básica 4: Principio versiones de la obra Swan Lake
music_seq['melody4oct3'] = ['B-3','B-3','E-3','F#-3','G-3','A-3','B-3','G-3','B-3','E-3','G-3','E-3','C-3','G-3','E-3','E-3']
# Melodía básica 5: Primeros compases introduccion Titanic
music_seq['melody5oct4'] = ['C-4','D-4','D-4','E-4','E-4','D-4','C-4','D-4','G-4','G-4','E-4','G-4','A-4','A-4','A-4','G-4','D-4','D-4','D-4']
# Melodía 6: Minuet G Major, Bach
music_seq['Minuet'] = ['D-5','D-5','G-4','A-4','B-4','C-5','D-5','D-5','G-4','G-4','E-5','E-5','C-5','D-5','E-5','F#-5','G-5','G-5',
                      'G-4','G-4','C-5','C-5','D-5','C-5','B-4','A-4','B-4','B-4','C-5','B-4','A-4','G-4','A-4','A-4','B-4','A-4','G-4','F#-4','G-4','G-4']
# Melodía 7: Prelude G Minor, Bach
music_seq['BachGminor'] = ['A#-4','D-4','C-4','D-4','A#-3','D-4','C-4','D-4','A#-4','D-4','C-4','D-4','A#-3','D-4','C-4','D-4','A#-4','D#-4','D-4','D#-4','A#-3','D#-4','D-4','D#-4',
                         'A#-4','D#-4','D-4','D#-4','A#-3','D#-4','D-4','D#-4','A-4','C-4','A#-3','C-4','A-3','C-4','A#-3','C-4','A-4','C-4','A#-3','C-4','A-3','C-4','A#-3','C-4',
                         'A-4','D-4','C-4','D-4','A#-3','D-4','C-4','D-4','A-4','D-4','C-4','D-4','A#-3','D-4','C-4','D-4',]
# Melodía 8: Waltz, Frederic Chopin
music_seq['Waltz1'] = ['E-4','A-4','B-4','C-5','C-5','D-5','E-5','F-5','F-5','B-4','C-5','D-5','A-5','G-5','F-5','E-5', 'D#-5','E-5','E-5','C-5','D-5','E-5','E-5','F-5','G-5',
                       'A-5','A-5','G-5','G-5','F#-5','G-5', 'D-6', 'F-5', 'E-5','E-5']
# Melodía 9: Sonatine, Clementi
music_seq['Sonatine'] = ['C-4','C-4','E-4','C-4','G-3','G-3','G-3','G-3','C-4','C-4','E-4','C-4','G-3','G-3','G-3','G-4','F-4','E-4','D-4','C-4','B-3','C-4','B-3','C-4','D-4','C-4','B-3',
                         'A-3','G-3','G-3','G-3']
# Melodía 10: River Flows in You, Yiruma
music_seq['Yiruma'] = ['A-5','B-5','A-5','G#-5','A-5','A-4','E-5','A-4','A-5','B-5','A-5','G#-5','A-5','A-4','E-5','A-4','A-5','B-5','A-5','G#-5','A-5','B-5','C#-6','D-6','E-6','C#-6','B-5',
                       'A-5','G#-5', 'E-5','E-5','B-4','B-4','G#-4','G#-4']

# Separación entre las notas para la realización de los experimentos con distintos tiempos.
# Estos tiempos son, también, el tiempo que se mantiene la nota sostenida, su duración (ver función play_notes_seq).

s_separation = [0.5, 1, 2]

# Se inicializa el instrumento con el que se quiere grabar el audio mediante los archivos .sf2
# En este caso es "Piano", si se quiere otro, comentar el piano y descomentar la línea del instrumento deseado.

fluidsynth.init(sf2='soundfonts/FluidR3 GM2-2.SF2')
#fluidsynth.init(sf2='soundfonts/Guitar Acoustic.sf2')
#fluidsynth.init(sf2='soundfonts/Clarinete.sf2')
#fluidsynth.init(sf2='soundfonts/trumpet_collection.sf2')
#fluidsynth.init(sf2='soundfonts/Violin.SF2')

# Se define la función para iniciar la consecución y generación de notas definidas previamente.

def play_notes_seq(seq, time_seconds):
    for note_i in seq:
        note = Note(note_i)
        fluidsynth.play_Note(note)
        fluidsynth.midi.sleep(seconds=time_seconds) #tiempo de la nota sostenida
        fluidsynth.stop_Note(note) #detiene la reproducción de la nota para que no se solape con la siguiente

# Se generan los audios y se guardan. El número contenido en el "for" final es el número de veces que se quiere
# repetir la secuencia de notas en el mismo audio.

n = 1
for seq in music_seq:
    for time_seconds in s_separation:
        aux_name_file = '/tmp/temp%d.wav' % n
        fluidsynth.midi.start_recording(aux_name_file)
        for _ in range(1):
            play_notes_seq(music_seq[seq], time_seconds)

        final_filename = 'audios_validation/%s_%.1f_s.wav' % (seq, time_seconds)

        # colocacion en el canal derecho y mezcla a 44100 Hz
        os.system("sox %s %s remix 1 rate 44100" % (aux_name_file, final_filename))
        n += 1


from falcon import testing

from ddtrace import config
from ddtrace.contrib.falcon.patch import FALCON_VERSION
from tests.utils import DummyTracer
from tests.utils import TracerTestCase

from .app import get_app
from .test_suite import FalconTestMixin

class DistributedTracingTestCase(testing.TestCase, FalconTestMixin, TracerTestCase):
    """Executes tests using the manual instrumentation so a middleware
    is explicitly added.
    """

    def setUp(self):
        super(DistributedTracingTestCase, self).setUp()
        self._service = "falcon"
        self.tracer = DummyTracer()
        self.api = get_app(tracer=self.tracer)
        if FALCON_VERSION >= (2, 0, 0):
            self.client = testing.TestClient(self.api)
        else:
            self.client = self

    def test_distributed_tracing_enabled(self):
        config.falcon["distributed_tracing"] = True
        headers = {
            "x-datadog-trace-id": "100",
            "x-datadog-parent-id": "42",
        }
        out = self.make_test_call("/200", headers=headers, expected_status_code=200)
        assert out.content.decode("utf-8") == "Success"

        traces = self.tracer.pop_traces()

        assert len(traces) == 1
        assert len(traces[0]) == 1

        assert traces[0][0].parent_id == 42
        assert traces[0][0].trace_id == 100

    def test_distributed_tracing_disabled_via_int_config(self):
        config.falcon["distributed_tracing"] = False
        self.tracer = DummyTracer()
        self.api = get_app(tracer=self.tracer)
        if FALCON_VERSION >= (2, 0, 0):
            self.client = testing.TestClient(self.api)
        headers = {
            "x-datadog-trace-id": "100",
            "x-datadog-parent-id": "42",
        }
        out = self.make_test_call("/200", headers=headers, expected_status_code=200)
        assert out.content.decode("utf-8") == "Success"

        traces = self.tracer.pop_traces()

        assert len(traces) == 1
        assert len(traces[0]) == 1

        assert traces[0][0].parent_id != 42
        assert traces[0][0].trace_id != 100

    @TracerTestCase.run_in_subprocess(env_overrides=dict(DD_FALCON_DISTRIBUTED_TRACING="False"))
    def test_distributed_tracing_disabled_via_env_var(self):
        self.tracer = DummyTracer()
        self.api = get_app(tracer=self.tracer)

        if FALCON_VERSION >= (2, 0, 0):
            self.client = testing.TestClient(self.api)
        headers = {
            "x-datadog-trace-id": "100",
            "x-datadog-parent-id": "42",
        }
        out = self.make_test_call("/200", headers=headers, expected_status_code=200)
        assert out.content.decode("utf-8") == "Success"

        traces = self.tracer.pop_traces()
        assert len(traces) == 1
        assert len(traces[0]) == 1

        assert traces[0][0].parent_id != 42
        assert traces[0][0].trace_id != 100


import time
import os
from random import randrange, randint
item = 0

#terminal print commands
#define function
def startrail():
  #defining colors and clear screen
  ANSI_CLEAR_SCREEN = u"\u001B[2J"
  ANSI_HOME_CURSOR = u"\u001B[0;0H\u001B[2"
  OCEAN_COLOR = u"\u001B[44m\u001B[2D"
  SHIP_COLOR = '\33[32m' #u"\u001B[35m\u001B[2D"
  RESET_COLOR = u"\u001B[0m\u001B[2D"

  CBLACK  = '\33[30m'
  CRED    = '\33[31m'
  CGREEN  = '\33[32m'
  CYELLOW = '\33[33m'
  CBLUE   = '\33[34m'
  CVIOLET = '\33[35m'
  CBEIGE  = '\33[36m'
  CWHITE  = '\33[37m'

  possibleColor = [ CBLACK, CRED, CGREEN, CYELLOW, CBLUE, CVIOLET, CBEIGE, CWHITE]
  #defining function that prints the ocean
  def ocean_print():
    #print ocean
    print(ANSI_CLEAR_SCREEN, ANSI_HOME_CURSOR)
    print("\n\n\n\n")
    print(OCEAN_COLOR + "  " * 50)

  #print ship with colors and leading spaces
  def ship_print(position):
    #os.system('cls' if os.name=='nt' else 'clear')
    print(ANSI_HOME_CURSOR)
    print(RESET_COLOR)

    #defining arrays
    characters = ["★", "☆", "⋆", "★", "☆", "✦", "✧", "✫", "✬", "✯", "⭐"]
    delete = [2, 4, 3, 5, 6, 7]

    #defining different functions to keep randomization constant
    def spaceFunction():
      return position - delete[randrange(5)] #number of spaces behind the trail
    def insert():
      return position - space #number of star characters printed behind the ship
    def sp00():
      return " " * space #prints the spaces
    def randomRange():
      return characters[randrange(11)] #chooses random character from array
    def color():
      return possibleColor[randrange(7)] #chooses random color from array

    #defining the variables from the functions above
    global space
    space = spaceFunction()
    sp00 = sp00()
    insert = insert() #position - space
    randomRange = randomRange()
    color = color()

    #4 for looops for printing each line of code
    print(sp00, end="")
    for x in range(int(insert - 1)):
      randomEvent = randint(0,4)
      if randomEvent < 2:
        print(possibleColor[randrange(7)] + characters[randrange(11)], end=" ")
      else:
        print(possibleColor[randrange(7)] + " ", end=" ")
    print(SHIP_COLOR + "    |\   ")

    print(sp00, end="")
    for x in range(int(insert - 1)):
      randomEvent = randint(0,4)
      if randomEvent < 2:
        print(possibleColor[randrange(7)] + characters[randrange(11)], end=" ")
      else:
        print(possibleColor[randrange(7)] + " ", end=" ")
    print(SHIP_COLOR +"    |/   ")

    print(sp00, end="")
    for x in range(int(insert - 1)):
      randomEvent = randint(0,4)
      if randomEvent < 2:
        print(possibleColor[randrange(7)] + characters[randrange(11)], end=" ")
      else:
        print(possibleColor[randrange(7)] + " ", end=" ")
    print(SHIP_COLOR +"\__ |__/ ")

    print(sp00, end="")
    for x in range(int(insert - 1)):
      randomEvent = randint(0,4)
      if randomEvent < 2:
        print(possibleColor[randrange(7)] + characters[randrange(11)], end=" ")
      else:
        print(possibleColor[randrange(7)] + " ", end=" ")
    print(SHIP_COLOR +" \____/  ")

  #ship function, iterface into this file
  def ship():
    #only need to print ocean once
    ocean_print()

    #loop control variables
    start = 0         #start at zero
    distance = 80  #how many times to repeat
    step = 4       #count by 2

    #loop purpose is to animate ship sailing
    for position in range(start,distance, step):

      ship_print(position)  #call to function with parameter
      time.sleep(.1)
      space

  ship()


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 13 11:01:17 2019
阶乘n! = 1 x 2 x 3 x ... x n，用函数fact(n)表示，可以看出：
fact(n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! x n = fact(n-1) x n
所以，fact(n)可以表示为n * fact(n-1)，只有n=1时需要特殊处理
"""
#阶乘运算
def fact(x):
    if x==1:
        return 1
    return x *fact(x-1)
print(fact(4))
print(fact(5))
#print(fact(1000))       #栈溢出，RecursionError: maximum recursion depth exceeded in comparison

'''
使用递归函数需要注意防止栈溢出。
在计算机中，函数调用是通过栈（stack）这种数据结构实现的，每当进入一个函数调用，栈就会加一层栈帧，每当函数返回，栈就会减一层栈帧。
由于栈的大小不是无限的，所以，递归调用的次数过多，会导致栈溢出。可以试试fact(1000)：
>>> fact(1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded in comparison
解决递归调用栈溢出的方法是通过尾递归优化，事实上尾递归和循环的效果是一样的，所以，把循环看成是一种特殊的尾递归函数也是可以的。

尾递归是指，在函数返回的时候，调用自身本身，并且，return语句不能包含表达式。
这样，编译器或者解释器就可以把尾递归做优化，使递归本身无论调用多少次，都只占用一个栈帧，不会出现栈溢出的情况。

上面的fact(n)函数由于return n * fact(n - 1)引入了乘法表达式，所以就不是尾递归了。
要改成尾递归方式，需要多一点代码，主要是要把每一步的乘积传入到递归函数中：
'''
#return fact_iter(num-1,num*product)仅返回递归函数本身
#num-1和num*product在函数调用前就会被计算，不影响函数调用。
def  fact(x):
    return fact_iter(x,1)

def fact_iter(num,products):
    if num==1:
        return products
    return fact_iter(num-1,num*products)

print(fact(3))
print(fact(4))
print(fact(5))

#   n=int(input("numbers:"))
#   print(fact(n))

'''
尾递归调用时，如果做了优化，栈不会增长，因此，无论多少次调用也不会导致栈溢出。

遗憾的是，大多数编程语言没有针对尾递归做优化，Python解释器也没有做优化，所以，即使把上面的fact(n)函数改成尾递归方式，也会导致栈溢出。

小结
使用递归函数的优点是逻辑简单清晰，缺点是过深的调用会导致栈溢出。
针对尾递归优化的语言可以通过尾递归防止栈溢出。
尾递归事实上和循环是等价的，没有循环语句的编程语言只能通过尾递归实现循环。
Python标准的解释器没有针对尾递归做优化，任何递归函数都存在栈溢出的问题。
'''

'''
练习
汉诺塔的移动可以用递归函数非常简单地实现。
请编写move(n, a, b, c)函数，它接收参数n，表示3个柱子A、B、C中第1个柱子A的盘子数量，
然后打印出把所有盘子从A借助B移动到C的方法，例如：
# -*- coding: utf-8 -*-
def move(n, a, b, c):

    if n == 1:
        print(a, '-->', c)

# 期待输出:
# A --> C
# A --> B
# C --> B
# A --> C
# B --> A
# B --> C
# A --> C
move(3, 'A', 'B', 'C')
汉诺塔问题是源于印度一个古老传说的益智玩具，三根柱子A、B、C，在A柱子上从下往上按照大小顺序摞着N片黄金圆盘，现需将所有圆盘转移到C柱上。
规定：小圆盘上不能放大圆盘，在三根柱子之间一次只能移动一个圆盘。
#无论多少个圆块，可以抽象成为同一套思路：
想办法把(n-1)个a柱上的圆块先移动到b柱，
然后把最底部最大的一个圆块移动到c柱，
最后把b柱上的(n-1)个圆块移动到c柱
'''

def move(n, a, b, c):
    if not isinstance(n,int):
        raise TypeError('n must be integer')
    if n == 1:
        print(a, '-->', c)  #定义从a移动到c的操作

    elif n > 1:
        move(n-1,a,c,b) #把(n-1)个a柱上的圆块先移动到缓冲区b柱
        move(1,a,b,c)   #把a柱底部最大的一个圆块移动到c柱 #move(1,a,b,c)即为 print(a, '-->', c)
        move(n-1,b,a,c) #最后把b柱上的(n-1)个圆块移动到c柱
    else:
        raise TypeError('n must be greater than 0')

move(5, 'A', 'B', 'C')
print('*************')
move(4, 'A', 'B', 'C')
print('*************')
move(3, 'A', 'B', 'C')
print('*************')
move(2, 'A', 'B', 'C')
print('*************')
move(1, 'A', 'B', 'C')

#1,3,7,15,31
#次数为2^n-1,n=(1,2,3,4……)


from os.path import exists, isdir, basename, join, splitext
from os import makedirs
from glob import glob
from random import sample, seed
from scipy import ones, mod, arange, array, where, ndarray, hstack, linspace, histogram, vstack, amax, amin
from scipy.misc import imread, imresize
from scipy.cluster.vq import vq
import numpy
#from vl_phow import vl_phow
#from vlfeat import vl_ikmeans
from scipy.io import loadmat, savemat
from sklearn import svm
from sklearn.metrics import confusion_matrix, accuracy_score
import pylab as pl
from datetime import datetime
from sklearn.kernel_approximation import AdditiveChi2Sampler
from pickle import dump, load
import numpy as np
import matplotlib.pyplot as plt

IDENTIFIER = '18.08.24'
SAVETODISC = False
FEATUREMAP = True
OVERWRITE = False  # DON'T load mat files genereated with a different seed!!!
SAMPLE_SEED = 42
TINYPROBLEM = False
VERBOSE = True  # set to 'SVM' if you want to get the svm output
MULTIPROCESSING = False

class Configuration(object):
    def __init__(self, identifier=''):
        #self.calDir = '.\dataset\\101_ObjectCategories'
        self.calDir = '/mlspace/xyy/dev_kmean_svm/dataset/101_ObjectCategories'
        self.dataDir = 'tempresults'
        if not exists(self.dataDir):
            makedirs(self.dataDir)
            print ("folder " + self.dataDir + " created")
        self.autoDownloadData = True
        self.numTrain = 30
        self.numTest = 50
        self.imagesperclass = self.numTrain + self.numTest
        self.numClasses = 102
        self.numWords = 600
        self.numSpatialX = [2, 4]
        self.numSpatialY = [2, 4]
        self.quantizer = 'vq'
        self.svm = SVMParameters(C=10)
        self.phowOpts = PHOWOptions(Verbose=False, Sizes=[4, 6, 8, 10], Step=3)
        self.clobber = False
        self.tinyProblem = TINYPROBLEM
        self.prefix = 'baseline'
        self.randSeed = 1
        self.verbose = True
        self.extensions = [".jpg", ".bmp", ".png", ".pgm", ".tif", ".tiff"]
        self.images_for_histogram = 30
        self.numbers_of_features_for_histogram = 100000

        self.vocabPath = join(self.dataDir, identifier + '-vocab.py.mat')
        self.histPath = join(self.dataDir, identifier + '-hists.py.mat')
        self.modelPath = join(self.dataDir, self.prefix + identifier + '-model.py.mat')
        self.resultPath = join(self.dataDir, self.prefix + identifier + '-result')

        if self.tinyProblem:
            print ("Using 'tiny' protocol with different parameters than the .m code")
            # self.prefix = 'tiny'
            # self.numClasses = 5
            # self.images_for_histogram = 10
            # self.numbers_of_features_for_histogram = 1000
            # self.numTrain
            # self.numSpatialX = 2
            # self.numWords = 100
            # self.numTrain = 2
            # self.numTest = 2
            # self.phowOpts = PHOWOptions(Verbose=2, Sizes=7, Step=5)

         # tests and conversions
        self.phowOpts.Sizes = ensure_type_array(self.phowOpts.Sizes)
        self.numSpatialX = ensure_type_array(self.numSpatialX)
        self.numSpatialY = ensure_type_array(self.numSpatialY)
        if (self.numSpatialX != self.numSpatialY).any():
            messageformat = [str(self.numSpatialX), str(self.numSpatialY)]
            message = "(self.numSpatialX != self.numSpatialY), because {0} != {1}".format(*messageformat)
            raise ValueError(message)

def ensure_type_array(data):
    if type(data) is not ndarray:
        if type(data) is list:
            data = array(data)
        else:
            data = array([data])
    return data

def standarizeImage(im):
    im = array(im, 'float32')
    if np.shape(im)[0] > 480:
        resize_factor = 480.0 / np.shape(im)[0]
        im = imresize(im, resize_factor)
    if amax(im) > 1.1:
        im = im / 255.0
    assert((amax(im) > 0.01) & (amax(im) <= 1))
    assert((amin(im) >= 0.00))
    return im

def get_classes(datasetpath, numClasses):
    classes_paths = [files for files in glob(datasetpath + '/*')]
    #classes_paths.sort()
    # basename获取对应路径下文件的名字
    classes = [basename(class_path) for class_path in classes_paths]
    print(len(classes))
    if len(classes) == 0:
        raise ValueError('no classes found')
    if len(classes) < numClasses:
        raise ValueError('conf.numClasses is bigger than the number of folders')
    classes = classes[:numClasses]
    return classes

def get_imgfiles(path, extensions):
    all_files = []
    # extend类似append，添加一个序列
    # splitext文件路径（path）和文件的扩展名（ext)
    all_files.extend([join(path, basename(fname)) for fname in glob(path+'/*')
                      if splitext(fname)[-1].lower() in extensions])
    return all_files

def get_all_images(classes, conf):
    all_images = []
    all_images_class_labels = []
    sel_train = []
    sel_test = []

    # 同时列出数据和数据下标
    k = 0
    p = 0
    for i, imageclass in enumerate(classes):
        path = join(conf.calDir, imageclass)
        extensions = conf.extensions
        imgs = get_imgfiles(path, extensions)
        if len(imgs) == 0:
             raise ValueError('no images for class ' + str(imageclass))
        # 从序列a中随机抽取n个元素，并将n个元素生以list形式返回 imagesperclass:30
        if len(imgs)<=conf.imagesperclass:
            imgs = sample(imgs, len(imgs))
            class_labels = list(i * ones(len(imgs)))
        else:
            imgs = sample(imgs, conf.imagesperclass)
            class_labels = list(i * ones(conf.imagesperclass))
        all_images = all_images + imgs
        #class_labels = list(i * ones(conf.imagesperclass))
        all_images_class_labels = all_images_class_labels + class_labels
        for j in range(k,len(all_images)):
            if (j-p) < conf.numTrain:
                sel_train.append(j)
            else:
                sel_test.append(j)
        k = len(all_images)
        p = k
    all_images_class_labels = array(all_images_class_labels, 'int')
    return all_images, all_images_class_labels,sel_train, sel_test

def create_split(all_images, conf):
    temp = mod(arange(len(all_images)), conf.imagesperclass) < conf.numTrain
    selTrain = where(temp == True)[0]
    selTest = where(temp == False)[0]

    return selTrain, selTest

class SVMParameters(object):
    def __init__(self, C):
        self.C = C

class PHOWOptions(object):
    def __init__(self, Verbose, Sizes, Step):
        self.Verbose = Verbose
        self.Sizes = Sizes
        self.Step = Step

def main_fun():
    seed(SAMPLE_SEED)
    conf = Configuration(IDENTIFIER)
    if VERBOSE:
        print(str(datetime.now()) + ' finished conf')
    classes = get_classes(conf.calDir, conf.numClasses) # 所以类别名字
    classes = classes[1:]
    print(classes)
    #print(classes)
    all_images, all_iamges_class_labels, selTrain, selTest = get_all_images(classes, conf)
    #print(all_iamges_class_labels) #(3060,1)# 图片标签
    #selTrain, selTest = create_split(all_images, conf)
    print(len(all_iamges_class_labels))
    print(len(all_images))
    # im = imread(all_images[30])
    # plt.title(classes[all_iamges_class_labels[30]])
    # plt.imshow(im)
    # plt.show()
    return classes, all_images, all_iamges_class_labels, selTrain, selTest

#main_fun()


import os
import pickle
from pulsar_ml.ml.loader import Loader
from pulsar_ml.ml.pre_processor import PreProcessor
from pulsar_ml.ml.model import Model
from pulsar_ml.ml.evaluator import Evaluator

def main():
    loader = Loader()
    preprocessor = PreProcessor()
    ml_model = Model()
    ml_evaluator = Evaluator()

    dataset_csv_path = 'data/HTRU_2.csv'
    attributes = [
        'mean',
        'standard_deviation',
        'excess_kurtosis',
        'skewness',
        'dm_snr_mean',
        'dm_snr_standard_deviation',
        'dm_snr_excess_kurtosis',
        'dm_snr_skewness',
        'class'
    ]
    test_size = 0.2

    pulsar_dataset = loader.load_data(dataset_csv_path, attributes)

    X_train, X_test, Y_train, Y_test = preprocessor.preprocess(pulsar_dataset, test_size)

    model = ml_model.trainRandomForestClassifier(X_train, Y_train, 10)

    predictions = ml_model.predictRandomForestClassifier(model, X_test)

    accuracy = ml_evaluator.evaluate_accuracy(predictions, Y_test)
    precision = ml_evaluator.evaluate_precision(predictions, Y_test, average='binary')
    recall = ml_evaluator.evaluate_recall(predictions, Y_test, average='binary')

    print("Accuracy: {}".format(accuracy))
    print("Precision: {}".format(precision))
    print("Recall: {}".format(recall))

    with open(os.path.join("./", 'random-forest-pulsar-model.pkl'), 'wb') as out:
        pickle.dump(model, out)

main()


import re
import time
import get_page_list
import get_small_page
import wget_download_list

def get_index_page_list():
    base_url = ['http://soundbible.com/free-sound-effects-', '.html']
    ret = [str(i) for i in range(1, 213)]
    return [i.join(base_url) for i in ret]

def find_audio_urls(page_html):
    mp3_url_re = 'http://soundbible.com/mp3/.*?.mp3'
    return re.findall(mp3_url_re, page_html)

def main(interval):
    index_list = get_index_page_list()
    mp3_urls_all = []
    for index_url in index_list:
        print('getting url:', index_url)
        page_html = get_small_page.get(index_url)
        mp3_urls = find_audio_urls(page_html)
        print('\n'.join(mp3_urls))
        mp3_urls_all.extend(mp3_urls)
        time.sleep(interval)

    wget_download_list.download_list(mp3_urls_all, interval)
    return mp3_urls_all

if __name__ == '__main__':
    print(main(1))


from setuptools import setup

def readme_content():
    with open('README.rst') as readme_file:
        data = readme_file.read()
    return data

setup(
    name='stinger',
    version='1.0.0',
    description='Simple Honeypot logging',
    long_description=readme_content(),
    author='Abraham',
    author_email='abrahamcalvillo@ymail.com',
    license='MIT',
    packages=['stinger'],
    zip_safe=False,
    install_requires=[
        'twisted',
        'paramiko',
        'keyboard'
    ]
)


from machine import Pin, reset
import gc

from src.ota.ota_updater import OTAUpdater

update_led = Pin(2, Pin.OUT)

def check_for_updates():
    otaUpdater = OTAUpdater(
        'https://github.com/choco-tech/choco-monitor',
        main_dir='src'
    )
    hasUpdated = otaUpdater.install_update_if_available()

    if hasUpdated:
        reset()
    else:
        del(otaUpdater)
        gc.collect()

def start_update():
    update_led.value(1)

    check_for_updates()

    update_led.value(0)


import os.path
import datetime

import bundleframework as bf
import debug
import tracelog
import err_code_mgr
import cmd_code_def
import worker_taskid_define
import db_sync_mit
import mit

from ne_info_mgr import NEInfoMgr
from db_sync_base import DBSyncStatus
from db_sync_base import DBSyncResult
from db_sync_base import DBSyncObject, DBSyncEvent
from db_sync_base import DBSyncType, DBSyncOperationType
from db_sync_util import DBSyncUtil

from notify_ne_id_pid_handler import NotifyNEIdPidHandler

class DBSyncUpdateHandler(bf.CmdHandler):
    """
    Class: DBSyncUpdateHandler
    Description: 数据同步接收处理类
    Base: CmdHandler
    Others:
    """
    def handle_cmd(self, frame):
        """
        Method: handle_cmd
        Description: 同步消息处理函数
        Parameter:
            frame: 请求消息
        Return: 无
        Others:
        """
        buf = frame.get_data()
        tracelog.info('received data from client at %s' % datetime.datetime.now().isoformat())
        ne_id = NEInfoMgr.get_ne_id_by_pid(frame.get_sender_pid())

        if ne_id is None:
            tracelog.error("received unknow data sync notification from NE, "
                            "pid:%d" % frame.get_sender_pid())
            return

        debug.info('ne id: %d' % (ne_id))

        # 如果网元正处于全同步中，那么直接忽视本次事件通知
        if not NEInfoMgr.is_ne_state_normal(ne_id):
            tracelog.info('database is full-synchonizing')
            return

        sync_object = DBSyncObject.deserialize(buf)
        result = DBSyncResult()
        result.id = sync_object.id
        debug.info('sync object id: %d' % sync_object.id)

        for event in sync_object.sync_events:
            if event.type == DBSyncType.FULL:
                self.__handle_full_sync_ntf(frame, ne_id, sync_object, event)
                self.get_worker().get_app().set_sync_sn(ne_id, event.priority, event.id)
                return

        events = []
        for event in sync_object.sync_events:
            sync_sn = self.get_worker().get_app().get_sync_sn(ne_id, event.priority)
            debug.info('ne_id: %d, priority: %d, event_id: %d sync_sn: %d' % (ne_id, event.priority, event.id, sync_sn))
            if event.id <= sync_sn:
                #该Event已经同步成功，重复提交将被忽略
                tracelog.info('event#%d was already synchonized.current sn: %d' % (event.id, sync_sn))
                result.event_ids.append(event.id)
            else:
                events.append(event)

        manager = self.get_worker().get_app().get_data_manager()
        debug.info('incremental sync starts')
        ret_code = manager.sync_data(events, ne_id)
        if ret_code != DBSyncStatus.ERROR_SUCCESS:
            tracelog.info('sync data failed. exit code: %d' % ret_code)
            if ret_code == DBSyncStatus.ERROR_CONFLICT:
                # 将EAU的状态设置为需要全同步
                NEInfoMgr.set_ne_need_sync_full(ne_id
                       , self.get_worker().get_app().get_mit_manager()
                       , True)
            # 不给EAU回应答；EAU收到收不到应答超时后，下次重试
            return

        for event in events:
            self.get_worker().get_app().set_sync_sn(ne_id, event.priority, event.id)
            debug.info('  event#%04d: return %d' % (event.id, ret_code))
            result.event_ids.append(event.id)

        self.__send_ack(frame, result.serialize())
        tracelog.info(result.event_ids)
        tracelog.info('sync processing completed.')

    def __handle_full_sync_ntf(self, frame, ne_id, sync_object, event):
        """
        Method: __handle_full_sync_ntf
        Description: 处理全同步事件通知
        Parameter:
            frame: 数据帧
            ne_id: 网元的ID
            sync_object: 同步传输对象
            event: 事件通知
        Return:
        Others:
        """

        tracelog.info('receive full sync notification from NE(%d)' % ne_id)

        error_code = NEInfoMgr.set_ne_need_sync_full(ne_id
                    , self.get_worker().get_app().get_mit_manager()
                    , True)

        if error_code != 0:
            tracelog.error("set_ne_need_sync_full() failed: %d" % error_code)
            # 不给EAU回应答；EAU收到收不到应答后，下次重试
            return

        result = DBSyncResult()
        result.id = sync_object.id
        result.return_code = error_code
        result.error_message = ''
        result.event_ids = []
        self.__send_ack(frame, result.serialize())

    def __send_ack(self, req_frame, data):
        """
        Method: __send_ack
        Description: 发送应答消息给网元
        Parameter:
            req_frame: 请求消息
            data: 应答消息的数据
        Return:
        Others:
        """

        frame_ack = bf.AppFrame()
        frame_ack.prepare_for_ack(req_frame)
        frame_ack.add_data(data)

        frame_ack.set_next_pid(self.get_worker().get_pid("EAUGate"))
        self.get_worker().dispatch_frame_to_process_by_pid(frame_ack.get_receiver_pid(), frame_ack)

class DBSyncUpdateWorker(bf.CmdWorker):
    """
    Class: DBSyncWorker
    Description: DBSync操作worker
    Base: CmdWorker
    Others:
    """

    def __init__(self):
        """
        Method: __init__
        Description: 对象初始化函数
        Parameter: 无
        Return:
        Others:
        """

        bf.CmdWorker.__init__(self, name = "DBSyncUpdateWorker"
                              ,min_task_id = worker_taskid_define.DB_SYNC_WORKER_MIN_TASK_ID
                              ,max_task_id = worker_taskid_define.DB_SYNC_WORKER_MAX_TASK_ID)

    def ready_for_work(self):
        """
        Method: ready_for_work
        Description: 注册同步消息处理handler
        Parameter: 无
        Return: 0，成功
        Others:
        """
        tracelog.info('ready for db sync update worker')

        handler = DBSyncUpdateHandler()
        self.register_handler(handler, cmd_code_def.CMD_SYNC_DATA)

        handler = NotifyNEIdPidHandler()
        self.register_handler(handler, cmd_code_def.CMD_NTF_NE_ID_PID)
        return 0


from models.Discriminator import Discriminator
from models.VGG19 import Vgg19
from models.DINet import DINet
from models.Syncnet import SyncNetPerception
from models.color_syncnet import SyncNet_color  as SyncNet
from utils.training_utils import get_scheduler, update_learning_rate,GANLoss
from config.config import DINetTrainingOptions
from sync_batchnorm import convert_model
from torch.utils.data import DataLoader
from dataset.dataset_DINet_clip_colorsync import DINetDataset

import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import os
import torch.nn.functional as F

from torch.utils.tensorboard import SummaryWriter
import cv2

def _load(checkpoint_path):
    use_cuda = torch.cuda.is_available()
    if use_cuda:
        checkpoint = torch.load(checkpoint_path)
    else:
        checkpoint = torch.load(checkpoint_path,
                                map_location=lambda storage, loc: storage)
    return checkpoint

def load_checkpoint(path, model):
    print("Load checkpoint from: {}".format(path))
    checkpoint = _load(path)
    s = checkpoint["state_dict"]
    new_s = {}
    for k, v in s.items():
        new_s[k.replace('module.', '')] = v
    model.load_state_dict(new_s)
    return model

logloss = nn.BCELoss()
def cosine_loss(a, v, y):
    d = nn.functional.cosine_similarity(a, v)
    loss = logloss(d.unsqueeze(1), y)
    return loss

if __name__ == "__main__":
    '''
            clip training code of DINet
            in the resolution you want, using clip training code after frame training

        '''
    # load config
    opt = DINetTrainingOptions().parse_args()

    writer = SummaryWriter('./logs/'  + opt.result_path.split('/')[-1])

    random.seed(opt.seed)
    np.random.seed(opt.seed)
    torch.cuda.manual_seed(opt.seed)
    # load training data
    train_data = DINetDataset(opt.train_data,opt.augment_num,opt.mouth_region_size)
    training_data_loader = DataLoader(dataset=train_data,  batch_size=opt.batch_size, shuffle=True,drop_last=True)
    train_data_length = len(training_data_loader)
    # init network
    opt.audio_channel = 256 #######
    net_g = DINet(opt.source_channel,opt.ref_channel,opt.audio_channel).cuda()
    net_dI = Discriminator(opt.source_channel ,opt.D_block_expansion, opt.D_num_blocks, opt.D_max_features).cuda()
    net_dV = Discriminator(opt.source_channel * 5, opt.D_block_expansion, opt.D_num_blocks, opt.D_max_features).cuda()
    net_vgg = Vgg19().cuda()

    # import pdb
    # pdb.set_trace()
    # for sync net
    # net_lipsync = SyncNetPerception(opt.pretrained_syncnet_path).cuda()
    net_lipsync = SyncNet().cuda()
    for p in net_lipsync.parameters():
        p.requires_grad = False
    # pdb.set_trace()
    net_lipsync = load_checkpoint(opt.pretrained_syncnet_path, net_lipsync)
    net_lipsync = net_lipsync.cuda()

    # parallel
    net_g = nn.DataParallel(net_g)
    net_g = convert_model(net_g)
    net_dI = nn.DataParallel(net_dI)
    net_dV = nn.DataParallel(net_dV)
    net_vgg = nn.DataParallel(net_vgg)
    # setup optimizer
    optimizer_g = optim.Adam(net_g.parameters(), lr=opt.lr_g)
    optimizer_dI = optim.Adam(net_dI.parameters(), lr=opt.lr_dI)
    optimizer_dV = optim.Adam(net_dV.parameters(), lr=opt.lr_dI)
    ## load frame trained DInet weight
    print('loading frame trained DINet weight from: {}'.format(opt.pretrained_frame_DINet_path))
    checkpoint = torch.load(opt.pretrained_frame_DINet_path)
    net_g.load_state_dict(checkpoint['state_dict']['net_g'])
    # set criterion
    criterionGAN = GANLoss().cuda()
    criterionL1 = nn.L1Loss().cuda()
    criterionMSE = nn.MSELoss().cuda()
    # set scheduler
    net_g_scheduler = get_scheduler(optimizer_g, opt.non_decay, opt.decay)
    net_dI_scheduler = get_scheduler(optimizer_dI, opt.non_decay, opt.decay)
    net_dV_scheduler = get_scheduler(optimizer_dV, opt.non_decay, opt.decay)
    # set label of syncnet perception loss
    real_tensor = torch.tensor(1.0).cuda()
    # start train
    for epoch in range(opt.start_epoch, opt.non_decay+opt.decay+1):
        net_g.train()
        for iteration, data in enumerate(training_data_loader):
            # forward
            source_clip,source_clip_mask, reference_clip,deep_speech_clip,deep_speech_full, sync_mel, sync_y = data

            source_clip = torch.cat(torch.split(source_clip, 1, dim=1), 0).squeeze(1).float().cuda()
            source_clip_mask = torch.cat(torch.split(source_clip_mask, 1, dim=1), 0).squeeze(1).float().cuda()
            reference_clip = torch.cat(torch.split(reference_clip, 1, dim=1), 0).squeeze(1).float().cuda()
            deep_speech_clip = torch.cat(torch.split(deep_speech_clip, 1, dim=1), 0).squeeze(1).float().cuda()
            deep_speech_full = deep_speech_full.float().cuda()

            fake_out = net_g(source_clip_mask,reference_clip,deep_speech_clip)
            fake_out_half = F.avg_pool2d(fake_out, 3, 2, 1, count_include_pad=False)
            source_clip_half = F.interpolate(source_clip, scale_factor=0.5, mode='bilinear')

            # crop sync input fake data
            fake_out_tmp = fake_out.clone()
            _, _, h_fake, w_fake = fake_out_tmp.shape
            fake_out_tmp = fake_out_tmp[:, :, int(h_fake * 0.380) : int(h_fake * 0.818), :]
            fake_out_tmp = torch.split(fake_out_tmp, opt.batch_size, dim=0)
            sync_fake_input = [[] for i in range(opt.batch_size)]
            for ref_id in range(5):
                for batch_id in range(opt.batch_size):
                    tmp_img = fake_out_tmp[ref_id][batch_id, :, :, :].permute(1, 2, 0).detach().cpu().numpy()[:, :, ::-1]
                    tmp_img = cv2.resize(tmp_img, (96, 48))
                    sync_fake_input[batch_id].append(tmp_img)
            batch_all_person_data = []
            for batch_single_person_data in sync_fake_input:
                batch_single_person_array = np.array(batch_single_person_data)
                batch_single_person_array = np.concatenate(batch_single_person_array, axis=2)
                batch_single_person_array = batch_single_person_array.transpose(2, 0, 1)
                batch_all_person_data.append(batch_single_person_array)
            sync_fake_img_tensor = torch.FloatTensor(batch_all_person_data)

            # prepare syncnet input data
            sync_mel = sync_mel.cuda()
            sync_fake_img_tensor = sync_fake_img_tensor.cuda()
            sync_y = sync_y.cuda()

            # vis network output img
            if iteration % 100 == 0:
                fake_frame = fake_out[0, :, :, :].squeeze(0).permute(1, 2, 0).detach().cpu().numpy() * 255
                real_frame = source_clip[0, :, :, :].permute(1, 2, 0).detach().cpu().numpy() * 255
                fake_img_name = "./vis_img_clip/epoch" + str(epoch).zfill(4) + '_' + str(iteration).zfill(5) + '_fake.jpg'
                real_img_name = "./vis_img_clip/epoch" + str(epoch).zfill(4) + '_' + str(iteration).zfill(5) + '_real.jpg'
                cv2.imwrite(fake_img_name, fake_frame[:, :, ::-1])
                cv2.imwrite(real_img_name, real_frame[:, :, ::-1])

            # (1) Update DI network
            optimizer_dI.zero_grad()
            _,pred_fake_dI = net_dI(fake_out)
            loss_dI_fake = criterionGAN(pred_fake_dI, False)
            _,pred_real_dI = net_dI(source_clip)
            loss_dI_real = criterionGAN(pred_real_dI, True)
            # Combined DI loss
            # loss_dI = (loss_dI_fake + loss_dI_real) * 0.5               # GAN Loss D(true + Fake)  using  Discriminator-image-model
            loss_dI = (loss_dI_fake + loss_dI_real) * 0.5 * 1.5              # GAN Loss D(true + Fake)  using  Discriminator-image-model
            loss_dI.backward(retain_graph=True)
            optimizer_dI.step()

            # (2) Update DV network
            optimizer_dV.zero_grad()
            condition_fake_dV = torch.cat(torch.split(fake_out, opt.batch_size, dim=0), 1)
            _, pred_fake_dV = net_dV(condition_fake_dV)
            loss_dV_fake = criterionGAN(pred_fake_dV, False)
            condition_real_dV = torch.cat(torch.split(source_clip, opt.batch_size, dim=0), 1)
            _, pred_real_dV = net_dV(condition_real_dV)
            loss_dV_real = criterionGAN(pred_real_dV, True)
            # Combined DV loss
            loss_dV = (loss_dV_fake + loss_dV_real) * 0.5 * 1.5              # GAN Loss D(true + Fake) using  Discriminator-video-model
            # loss_dV = (loss_dV_fake + loss_dV_real) * 0.5              # GAN Loss D(true + Fake) using  Discriminator-video-model
            loss_dV.backward(retain_graph=True)
            optimizer_dV.step()

            # (2) Update DINet
            optimizer_g.zero_grad()
            # compute perception loss
            perception_real = net_vgg(source_clip)
            perception_fake = net_vgg(fake_out)
            perception_real_half = net_vgg(source_clip_half)
            perception_fake_half = net_vgg(fake_out_half)
            loss_g_perception = 0
            for i in range(len(perception_real)):
                loss_g_perception += criterionL1(perception_fake[i], perception_real[i])
                loss_g_perception += criterionL1(perception_fake_half[i], perception_real_half[i])
            loss_g_perception = (loss_g_perception / (len(perception_real) * 2)) * opt.lamb_perception        # perception-Loss  using  DINet(net_g)

            _, pred_fake_dI = net_dI(fake_out)
            _, pred_fake_dV = net_dV(condition_fake_dV)
            # # gan dI loss
            loss_g_dI = criterionGAN(pred_fake_dI, True)                 # GAN Loss G  using Discriminator-image-model
            # # gan dV loss
            loss_g_dV = criterionGAN(pred_fake_dV, True)                 # GAN Loss G  using Discriminator-video-model

            # ## color sync loss
            sync_a, sync_v = net_lipsync(sync_mel, sync_fake_img_tensor)
            loss_sync = cosine_loss(sync_a, sync_v, sync_y)

            # combine all losses
            loss_g =   loss_g_perception +  1.5 * (loss_g_dI +loss_g_dV) + loss_sync * 1.0
            # loss_g =   loss_g_perception +  loss_g_dI +loss_g_dV + loss_sync
            loss_g.backward()
            optimizer_g.step()

            print(
                "===> Epoch[{}]({}/{}):  Loss_Gan_D_image: {:.4f} Loss_Gan_G_image: {:.4f} Loss_Gan_D_video: {:.4f} Loss_Gan_G_video: {:.4f} Loss_perception: {:.4f} Loss_sync: {:.4f} lr_g = {:.7f} ".format(
                    epoch, iteration, len(training_data_loader), float(loss_dI), float(loss_g_dI),float(loss_dV), float(loss_g_dV), float(loss_g_perception),float(loss_sync),
                    optimizer_g.param_groups[0]['lr']))

            writer.add_scalar("Loss_Gan_D_image", float(loss_dI), epoch*len(training_data_loader)+iteration)
            writer.add_scalar("Loss_Gan_G_image", float(loss_g_dI), epoch*len(training_data_loader)+iteration)
            writer.add_scalar("Loss_Gan_D_video", float(loss_dV), epoch*len(training_data_loader)+iteration)
            writer.add_scalar("Loss_Gan_G_video", float(loss_g_dV), epoch*len(training_data_loader)+iteration)
            writer.add_scalar("Loss_perception", float(loss_g_perception), epoch*len(training_data_loader)+iteration)
            writer.add_scalar("Loss_sync", float(loss_sync), epoch*len(training_data_loader)+iteration)
            writer.add_scalar("lr_g", float(optimizer_g.param_groups[0]['lr']), epoch*len(training_data_loader)+iteration)

        update_learning_rate(net_g_scheduler, optimizer_g)
        update_learning_rate(net_dI_scheduler, optimizer_dI)
        update_learning_rate(net_dV_scheduler, optimizer_dV)

        writer.add_scalar("Loss_DI_epoch", float(loss_dI), epoch)
        writer.add_scalar("Loss_GI_epoch", float(loss_g_dI), epoch)
        writer.add_scalar("Loss_DV_epoch", float(loss_dV), epoch)
        writer.add_scalar("Loss_Gan_G_video", float(loss_g_dV), epoch)
        writer.add_scalar("Loss_perception_epoch", float(loss_g_perception), epoch)
        writer.add_scalar("Loss_sync_epoch", float(loss_sync), epoch)
        writer.add_scalar("lr_g_epoch", float(optimizer_g.param_groups[0]['lr']), epoch)

        # checkpoint
        if epoch %  opt.checkpoint == 0:
            if not os.path.exists(opt.result_path):
                os.mkdir(opt.result_path)
            model_out_path = os.path.join(opt.result_path, 'netG_model_epoch_{}.pth'.format(epoch))
            states = {
                'epoch': epoch + 1,
                'state_dict': {'net_g': net_g.state_dict(),'net_dI': net_dI.state_dict(),'net_dV': net_dV.state_dict()},
                'optimizer': {'net_g': optimizer_g.state_dict(), 'net_dI': optimizer_dI.state_dict(), 'net_dV': optimizer_dV.state_dict()}
            }
            torch.save(states, model_out_path)
            print("Checkpoint saved to {}".format(epoch))


from typing import Optional

import plotly.graph_objects as go
from munch import Munch
from munch import munchify
from plotly.graph_objs import Figure
from plotly.subplots import make_subplots

from app.cpt.model import CPT
from app.ground_model.constants import UNIQUE_TNO_SOIL_TYPES
from app.ground_model.model import build_combined_rep_and_exit_point_layout
from viktor import Color
from viktor.geo import SoilLayout

def visualize_exit_point_soil_layouts(
    segment_params: Munch, exit_point_params: Munch, cpt: Optional[CPT], rep_soil_layout: SoilLayout
) -> str:
    """Returns the Plotly plot for the comparison of the two soil layouts of a selected exit point"""
    number_columns = 2 if segment_params.show_cpt else 1
    fig = make_subplots(
        rows=1,
        cols=number_columns,
        shared_yaxes=True,
        horizontal_spacing=0.00,
        column_widths=[3] * number_columns,
    )

    if segment_params.show_cpt:
        add_cpt_trace(fig, cpt)

    add_bar_soil_layouts(segment_params, exit_point_params, fig, number_columns, rep_soil_layout)

    fig.update_layout(barmode="stack", template="plotly_white", legend=dict(x=1.10, y=0.5))

    return fig.to_json()

def visualize_representative_layouts(
    detailed_soil_layout: SoilLayout, simplified_soil_layout: SoilLayout, segment_params: Munch
) -> str:
    fig = make_subplots(
        rows=1,
        cols=1,
        shared_yaxes=True,
        horizontal_spacing=0.00,
        column_widths=[3],
    )
    unique_user_soil_types = [soil.name for soil in segment_params.input_selection.materials.table]

    add_trace_classified_layout(
        fig,
        nb_columns=1,
        soil_layout=munchify(detailed_soil_layout.serialize()),
        unique_user_soil_types=unique_user_soil_types,
        x_name="1D representatieve layout",
    )
    add_trace_repr_layout(
        fig, munchify(simplified_soil_layout.serialize()), nb_columns=1, x_name="1D rep. gesimplificeerde layout"
    )

    fig.update_layout(barmode="stack", template="plotly_white", legend=dict(x=1.10, y=0.5))

    return fig.to_json()

def visualize_leakage_point_layouts(
    tno_soil_layout: SoilLayout,
    classified_soil_layout: SoilLayout,
    segment_params: Munch,
    rep_soil_layout: Optional[SoilLayout] = None,
) -> str:
    unique_user_soil_types = [soil.name for soil in segment_params.input_selection.materials.table]
    fig = make_subplots(
        rows=1,
        cols=2,
        shared_yaxes=True,
        horizontal_spacing=0.00,
        column_widths=[3, 3],
    )
    add_trace_tno_layout(fig, tno_soil_layout, 1)

    add_trace_classified_layout(
        fig, munchify(classified_soil_layout.serialize()), 1, unique_user_soil_types, x_name="Geïnterpreteerd lagen"
    )

    if rep_soil_layout:
        add_trace_repr_layout(fig, munchify(rep_soil_layout.serialize()), nb_columns=2, x_name="Dijkvak")

    fig.update_layout(barmode="stack", template="plotly_white", legend=dict(x=1.10, y=0.5))
    return fig.to_json()

def add_cpt_trace(fig: Figure, cpt: CPT):
    """Add the trace of the cone resistance for the selected CPT"""
    fig.add_trace(
        go.Scatter(
            name="Cone Resistance",
            x=cpt.parsed_cpt.qc,
            y=[el * 1e-3 for el in cpt.parsed_cpt.elevation],
            mode="lines",
            line=dict(color="mediumblue", width=1),
            legendgroup="Cone Resistance",
        ),
        row=1,
        col=1,
    )
    standard_line_options = dict(showline=True, linewidth=2, linecolor="LightGrey")
    fig.update_yaxes(**standard_line_options, dtick=1, showticklabels=True, col=1)

def add_bar_soil_layouts(
    segment_params: Munch, exit_point_params: Munch, fig: Figure, number_columns: int, rep_soil_layout: SoilLayout
):
    """Add the traces for the Soil layouts bar charts:
    - TNO soil layout
    - Saved soil layout of the exit point entity (actual soil layout used for Uplift and heave calculations)
    - Prevision soil layout translated from the user soil layout table.
    - Representative soil layout of the segment at the exit point location.
    """
    unique_user_soil_types = [soil.name for soil in segment_params.input_selection.materials.table]

    # Add bars for each soil type separately in order to be able to set legend labels
    # TNO and user layouts are kept separated so that the legend groups can be distinguished
    add_trace_tno_layout(fig, exit_point_params.tno_soil_layout, number_columns)

    add_trace_classified_layout(
        fig,
        exit_point_params.classified_soil_layout,
        number_columns,
        unique_user_soil_types,
        x_name="Geïnterpreteerd",
    )

    soil_layout_piping = build_combined_rep_and_exit_point_layout(
        exit_point_params.classified_soil_layout, rep_soil_layout
    )

    add_trace_repr_layout(fig, munchify(rep_soil_layout.serialize()), number_columns, "Dijkvak
(Stap 1)")
    add_trace_repr_layout(
        fig,
        munchify(soil_layout_piping.serialize()),
        number_columns,
        "Dijkvak
(Piping berekening)",
        showlegend=False,
    )

    standard_line_options = dict(showline=True, linewidth=2, linecolor="LightGrey")
    fig.update_yaxes(
        **standard_line_options,
        dtick=1,
        showticklabels=True,
        side="right",
        title="Elevation [m NAP]",
        col=number_columns,
    )
    fig.update_layout(legend=dict(groupclick="toggleitem"))

def add_trace_tno_layout(fig: Figure, tno_soil_layout: SoilLayout, nb_columns: int):
    for ui_name in UNIQUE_TNO_SOIL_TYPES:
        original_layers = [layer for layer in tno_soil_layout.layers if layer.soil.name == ui_name]

        soil_type_layers = [*original_layers]
        fig.add_trace(
            go.Bar(
                name=ui_name,
                x=["TNO"] * len(original_layers),
                y=[-(layer.top_of_layer - layer.bottom_of_layer) for layer in soil_type_layers],
                width=0.5,
                marker=dict(
                    color=[f"rgb{Color(*layer.soil.color).rgb}" for layer in soil_type_layers],
                    pattern_shape=["/" for layer in soil_type_layers if layer.properties.get("aquifer")],
                ),
                hovertext=[
                    f"Grondsoort: {layer.soil.name}
"
                    f"Bovenkant laag: {layer.top_of_layer:.2f} m NAP
"
                    f"Onderkant laag: {layer.bottom_of_layer:.2f} m NAP
"
                    f"Horizontale doorlatendheid: {layer.properties.horizontal_permeability:.2f} m/d
"
                    f"Verticale doorlatendheid: {layer.properties.vertical_permeability:.2f} m/d
"
                    f"Kans op veen: {layer.properties.kans_1_veen:.2f} %
"
                    f"Kans op klei: {layer.properties.kans_2_klei:.2f} %
"
                    f"Kans op zandige klei: {layer.properties.kans_3_kleiig_zand:.2f} %
"
                    f"Kans op los gepakt zand: {layer.properties.kans_5_zand_fijn:.2f} %
"
                    f"Kans op matig gepakt zand: {layer.properties.kans_6_zand_matig_grof:.2f} %
"
                    f"Kans op vast gepakt zand: {layer.properties.kans_7_zand_grof:.2f} %"
                    for layer in soil_type_layers
                ],
                hoverinfo="text",
                legendgroup="TNO",
                legendgrouptitle_text="TNO lagen",
                base=[layer.top_of_layer for layer in soil_type_layers],
            ),
            col=nb_columns,
            row=1,
        )

def add_trace_repr_layout(fig: Figure, representative_layout, nb_columns: int, x_name: str, showlegend: bool = True):
    for ui_name in ["cover_layer", "first_aquifer", "intermediate_aquitard", "second_aquifer"]:
        representative_layers = [layer for layer in representative_layout.layers if layer.soil.name == ui_name]
        layer_type_name = representative_layers[0].soil.properties.ui_name if representative_layers else None
        try:
            hovertext = [
                f"Grondsoort: {layer.soil.name}
"
                f"Bovenkant laag: {layer.top_of_layer:.2f}
"
                f"Onderkant laag: {layer.bottom_of_layer:.2f}
"
                f"Verticale doorlatendheid: {layer.properties.get('vertical_permeability'):.2f} m/d
"
                f"Horizontale doorlatendheid: {layer.properties.get('horizontal_permeability'):.2f} m/d
"
                f"Gewicht droog: {layer.properties.get('gamma_dry'):.1f} kN/m3
"
                f"Gewicht nat: {layer.properties.get('gamma_wet'):.1f} kN/m3
"
                f"d70: {layer.properties.get('grain_size_d70')} mm"
                for layer in representative_layers
            ]
        except TypeError:
            hovertext = [
                f"Grondsoort: {layer.soil.name}
"
                f"Bovenkant laag: {layer.top_of_layer:.2f}
"
                f"Onderkant laag: {layer.bottom_of_layer:.2f}
"
                f"Verticale doorlatendheid: {layer.properties.get('vertical_permeability')} m/d
"
                f"Horizontale doorlatendheid: {layer.properties.get('horizontal_permeability')} m/d
"
                f"Gewicht droog: {layer.properties.get('gamma_dry')} kN/m3
"
                f"Gewicht nat: {layer.properties.get('gamma_wet')} kN/m3
"
                f"d70: {layer.properties.get('grain_size_d70')} mm"
                for layer in representative_layers
            ]
        fig.add_trace(
            go.Bar(
                name=layer_type_name,
                x=[x_name] * len(representative_layers),
                y=[-(layer.top_of_layer - layer.bottom_of_layer) for layer in representative_layers],
                width=0.5,
                marker=dict(
                    color=[f"rgb{Color(*layer.soil.color).rgb}" for layer in representative_layers],
                    pattern_shape=["/" if layer.properties.get("aquifer") else "" for layer in representative_layers],
                ),
                hovertext=hovertext,
                hoverinfo="text",
                legendgroup="representative_layout",
                legendgrouptitle_text="Dijvak",
                showlegend=showlegend,
                base=[layer.top_of_layer for layer in representative_layers],
            ),
            col=nb_columns,
            row=1,
        )

def add_trace_classified_layout(
    fig: Figure,
    soil_layout: Munch,
    nb_columns: int,
    unique_user_soil_types,
    x_name: str = "saved_layout",
    showlegend=True,
):
    """Add traces for the classified soil layouts:
    - the saved layout of the exit point
    - the prevision soil layout translated from the user table.
    """

    for ui_name in unique_user_soil_types:
        soil_type_layers = [layer for layer in soil_layout.layers if layer.soil.name == ui_name]
        fig.add_trace(
            go.Bar(
                name=ui_name,
                x=[x_name] * len(soil_type_layers),
                y=[-(layer.top_of_layer - layer.bottom_of_layer) for layer in soil_type_layers],
                width=0.5,
                marker=dict(
                    color=[f"rgb{Color(*layer.soil.color).rgb}" for layer in soil_type_layers],
                    pattern_shape=["/" if layer.properties.get("aquifer") else "" for layer in soil_type_layers],
                ),
                hovertext=[
                    f"Grondsoort: {layer.soil.name}
"
                    f"Bovenkant laag: {layer.top_of_layer:.2f}
"
                    f"Onderkant laag: {layer.bottom_of_layer:.2f}"
                    for layer in soil_type_layers
                ],
                hoverinfo="text",
                legendgroup="user_defined_layout",
                legendgrouptitle_text="Geïnterpreteerd lagen",
                showlegend=showlegend,
                base=[layer.top_of_layer for layer in soil_type_layers],
            ),
            col=nb_columns,
            row=1,
        )


#정수 X가주어졌을 때, 정수 X에사용할수있는연산은다음과같이 4가지입니다.
#• X가 5로나누어떨어지면 5로나눕니다.
#• X가 3으로나누어떨어지면 3으로나눕니다.
#• X가 2로나누어떨어지면 2로나눕니다.
#• X에서 1을뺍니다.

#정수 X가주어졌을 때, 연산 4개를적절히사용해서값을 1로만들고자합니다. 연산을사용하는횟수의최솟값을출력하세요.

#입력조건
#• 첫째줄에정수 X 가주어진다.
#출력조건
#• 첫째줄에연산을하는횟수의최솟값을출력한다.

#입력예시 26
#출력예시 3

#연산횟수를 세는 함수를 만든다
def countC(x):
  d = [0] * 1000001
  #다이나믹프로그래밍 배열 만들기
  for i in range(2,x+1):
    d[i] = d[i-1]+1
  #연산 1에 해당하는 값을 넣어둔다
    if i%2 == 0:
      d[i] = min(d[i],d[i//2]+1)
  #2의 배수에 해당하는 인덱스 연산횟수 갱신
    if i%3 == 0:
      d[i] = min(d[i], d[i//3]+1)
  #3의 배수에 해당하는 인덱스 연산횟수 갱신
    if i%5 == 0:
      d[i] = min(d[i], d[i//5]+1)
  #5의 배수에 해당하는 인덱스 연산횟수 갱신
  return d[x]# 마지막 인덱스의 값을 출력

x = int(input())
print(countC(x))


import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
__all__ = ['SegmentationLosses', 'OhemCrossEntropy2d', 'OHEMSegmentationLosses']

class SegmentationLosses(nn.CrossEntropyLoss):
    """2D Cross Entropy Loss with Auxilary Loss"""
    def __init__(self, se_loss=False, se_weight=0.2, nclass=-1,
                 aux=False, aux_weight=0.4, weight=None,
                 ignore_index=-1):
        super(SegmentationLosses, self).__init__(weight, None, ignore_index)
        self.se_loss = se_loss
        self.aux = aux
        self.nclass = nclass
        self.se_weight = se_weight
        self.aux_weight = aux_weight
        self.bceloss = nn.BCELoss(weight)

    def forward(self, *inputs):
        if not self.se_loss and not self.aux:
            return super(SegmentationLosses, self).forward(*inputs)
        elif not self.se_loss:
            pred1, pred2, target = tuple(inputs)
            loss1 = super(SegmentationLosses, self).forward(pred1, target)
            loss2 = super(SegmentationLosses, self).forward(pred2, target)
            return loss1 + self.aux_weight * loss2
        elif not self.aux:
            pred, se_pred, target = tuple(inputs)
            se_target = self._get_batch_label_vector(target, nclass=self.nclass).type_as(pred)
            loss1 = super(SegmentationLosses, self).forward(pred, target)
            loss2 = self.bceloss(torch.sigmoid(se_pred), se_target)
            return loss1 + self.se_weight * loss2
        else:
            pred1, se_pred, pred2, target = tuple(inputs)
            se_target = self._get_batch_label_vector(target, nclass=self.nclass).type_as(pred1)
            loss1 = super(SegmentationLosses, self).forward(pred1, target)
            loss2 = super(SegmentationLosses, self).forward(pred2, target)
            loss3 = self.bceloss(torch.sigmoid(se_pred), se_target)
            return loss1 + self.aux_weight * loss2 + self.se_weight * loss3

    @staticmethod
    def _get_batch_label_vector(target, nclass):
        # target is a 3D Variable BxHxW, output is 2D BxnClass
        batch = target.size(0)
        tvect = Variable(torch.zeros(batch, nclass))
        for i in range(batch):
            hist = torch.histc(target[i].cpu().data.float(),
                               bins=nclass, min=0,
                               max=nclass-1)
            vect = hist>0
            tvect[i] = vect
        return tvect

# adapted from https://github.com/PkuRainBow/OCNet/blob/master/utils/loss.py
class OhemCrossEntropy2d(nn.Module):
    def __init__(self, ignore_label=-1, thresh=0.7, min_kept=100000, use_weight=True):
        super(OhemCrossEntropy2d, self).__init__()
        self.ignore_label = ignore_label
        self.thresh = float(thresh)
        self.min_kept = int(min_kept)
        if use_weight:
            print("w/ class balance")
            weight = torch.FloatTensor([0.8373, 0.918, 0.866, 1.0345, 1.0166, 0.9969, 0.9754,
                1.0489, 0.8786, 1.0023, 0.9539, 0.9843, 1.1116, 0.9037, 1.0865, 1.0955,
                1.0865, 1.1529, 1.0507])
            self.criterion = torch.nn.CrossEntropyLoss(weight=weight, ignore_index=ignore_label)
        else:
            print("w/o class balance")
            self.criterion = torch.nn.CrossEntropyLoss(ignore_index=ignore_label)

    def forward(self, predict, target, weight=None):
        """
        Args:
            predict:(n, c, h, w)
            target:(n, h, w)
            weight (Tensor, optional): a manual rescaling weight given to each class.
                                       If given, has to be a Tensor of size "nclasses"
        """
        assert not target.requires_grad
        assert predict.dim() == 4
        assert target.dim() == 3
        assert predict.size(0) == target.size(0), "{0} vs {1} ".format(predict.size(0), target.size(0))
        assert predict.size(2) == target.size(1), "{0} vs {1} ".format(predict.size(2), target.size(1))
        assert predict.size(3) == target.size(2), "{0} vs {1} ".format(predict.size(3), target.size(3))

        n, c, h, w = predict.size()
        input_label = target.data.cpu().numpy().ravel().astype(np.int32)
        x = np.rollaxis(predict.data.cpu().numpy(), 1).reshape((c, -1))
        input_prob = np.exp(x - x.max(axis=0).reshape((1, -1)))
        input_prob /= input_prob.sum(axis=0).reshape((1, -1))

        valid_flag = input_label != self.ignore_label
        valid_inds = np.where(valid_flag)[0]
        label = input_label[valid_flag]
        num_valid = valid_flag.sum()
        if self.min_kept >= num_valid:
            print('Labels: {}'.format(num_valid))
        elif num_valid > 0:
            prob = input_prob[:,valid_flag]
            pred = prob[label, np.arange(len(label), dtype=np.int32)]
            threshold = self.thresh
            if self.min_kept > 0:
                index = pred.argsort()
                threshold_index = index[ min(len(index), self.min_kept) - 1 ]
                if pred[threshold_index] > self.thresh:
                    threshold = pred[threshold_index]
            kept_flag = pred <= threshold
            valid_inds = valid_inds[kept_flag]

        label = input_label[valid_inds].copy()
        input_label.fill(self.ignore_label)
        input_label[valid_inds] = label
        valid_flag_new = input_label != self.ignore_label
        # print(np.sum(valid_flag_new))
        target = Variable(torch.from_numpy(input_label.reshape(target.size())).long().cuda())

        return self.criterion(predict, target)

class OHEMSegmentationLosses(OhemCrossEntropy2d):
    """2D Cross Entropy Loss with Auxilary Loss"""
    def __init__(self, se_loss=False, se_weight=0.2, nclass=-1,
                 aux=False, aux_weight=0.4, weight=None,
                 ignore_index=-1):
        super(OHEMSegmentationLosses, self).__init__(ignore_index)
        self.se_loss = se_loss
        self.aux = aux
        self.nclass = nclass
        self.se_weight = se_weight
        self.aux_weight = aux_weight
        self.bceloss = nn.BCELoss(weight)

    def forward(self, *inputs):
        if not self.se_loss and not self.aux:
            return super(OHEMSegmentationLosses, self).forward(*inputs)
        elif not self.se_loss:
            pred1, pred2, target = tuple(inputs)
            loss1 = super(OHEMSegmentationLosses, self).forward(pred1, target)
            loss2 = super(OHEMSegmentationLosses, self).forward(pred2, target)
            return loss1 + self.aux_weight * loss2
        elif not self.aux:
            pred, se_pred, target = tuple(inputs)
            se_target = self._get_batch_label_vector(target, nclass=self.nclass).type_as(pred)
            loss1 = super(OHEMSegmentationLosses, self).forward(pred, target)
            loss2 = self.bceloss(torch.sigmoid(se_pred), se_target)
            return loss1 + self.se_weight * loss2
        else:
            pred1, se_pred, pred2, target = tuple(inputs)
            se_target = self._get_batch_label_vector(target, nclass=self.nclass).type_as(pred1)
            loss1 = super(OHEMSegmentationLosses, self).forward(pred1, target)
            loss2 = super(OHEMSegmentationLosses, self).forward(pred2, target)
            loss3 = self.bceloss(torch.sigmoid(se_pred), se_target)
            return loss1 + self.aux_weight * loss2 + self.se_weight * loss3

    @staticmethod
    def _get_batch_label_vector(target, nclass):
        # target is a 3D Variable BxHxW, output is 2D BxnClass
        batch = target.size(0)
        tvect = Variable(torch.zeros(batch, nclass))
        for i in range(batch):
            hist = torch.histc(target[i].cpu().data.float(),
                               bins=nclass, min=0,
                               max=nclass-1)
            vect = hist>0
            tvect[i] = vect
        return tvect


import numpy as np
from sklearn.metrics import classification_report
from sklearn.model_selection import StratifiedKFold
from sklearn import metrics
import pandas as pd
import argparse

class Markov():

    def fasta2np(self, fasta):
        f = open(fasta)
        doc = f.readlines()
        list = []
        for seq in doc:
            if seq.startswith(">"):
                continue
            else:
                flag = seq.strip()
                a = [flag[i:i + 2] for i in range(len(flag) - 1)]
                list.append(a)
        m = np.array(list)
        return m

    '''m:训练集,p训练集中正例的个数'''

    def fit(self, m, p):
        pos = m[:p, :]

        neg = m[p:, :]

        '''pos的初始概率'''
        atgc1 = {}
        for i in range(len(pos)):
            if pos[i][0][0] in atgc1:

                atgc1[pos[i][0][0]] = atgc1[pos[i][0][0]] + 1
            else:
                atgc1[pos[i][0][0]] = 1
        for i, j in atgc1.items():
            atgc1[i] = atgc1[i] / len(pos)

        '''neg的初始概率'''
        atgc2 = {}
        for i in range(len(neg)):
            if neg[i][0][0] in atgc2:

                atgc2[neg[i][0][0]] = atgc2[neg[i][0][0]] + 1
            else:
                atgc2[neg[i][0][0]] = 1
        for i, j in atgc2.items():
            atgc2[i] = atgc2[i] / len(neg)

        '''pos的转移概率'''
        posdict = []
        for j in range(len(pos[0])):
            flag = {}
            a = 0
            t = 0
            g = 0
            c = 0
            for i in range(len(pos)):
                if pos[i][j][0] == "A":
                    a += 1
                elif pos[i][j][0] == "T":
                    t += 1
                elif pos[i][j][0] == "G":
                    g += 1
                else:
                    c += 1

                if pos[i, j] in flag:
                    flag[pos[i, j]] = flag[pos[i, j]] + 1
                else:
                    flag[pos[i, j]] = 1

            for i in flag:

                if i[0] == "A":
                    flag[i] = flag[i] / a
                elif i[0] == "T":
                    flag[i] = flag[i] / t
                elif i[0] == "G":
                    flag[i] = flag[i] / g
                else:
                    flag[i] = flag[i] / c

            posdict.append(flag)

        '''neg的转移概率'''
        negdict = []
        for j in range(len(neg[0])):
            flag = {}
            a = 0
            t = 0
            g = 0
            c = 0
            for i in range(len(neg)):

                if neg[i][j][0] == "A":
                    a += 1
                elif neg[i][j][0] == "T":
                    t += 1
                elif neg[i][j][0] == "G":
                    g += 1
                else:
                    c += 1

                if neg[i, j] in flag:
                    flag[neg[i, j]] = flag[neg[i, j]] + 1
                else:
                    flag[neg[i, j]] = 1

            for i in flag:

                if i[0] == "A":
                    flag[i] = flag[i] / a
                elif i[0] == "T":
                    flag[i] = flag[i] / t
                elif i[0] == "G":
                    flag[i] = flag[i] / g
                else:
                    flag[i] = flag[i] / c

            negdict.append(flag)

        posdict = np.array(posdict)
        negdict = np.array(negdict)

        return posdict, negdict, atgc1, atgc2

    def predict(self, test_set, posdict, negdict, atgc1, atgc2):
        posfea = []
        for i in range(len(test_set)):
            pos = 1
            if test_set[i][0][0] in atgc1:
                pos = pos * atgc1[test_set[i][0][0]]
            for j in range(len(test_set[i])):
                if test_set[i][j] in posdict[j]:
                    pos = pos * (posdict[j][test_set[i][j]])
                else:
                    pos = pos * 1
            posfea.append(pos)

        negfea = []
        for i in range(len(test_set)):
            neg = 1
            if test_set[i][0][0] in atgc1:
                neg = neg * atgc2[test_set[i][0][0]]
            for j in range(len(test_set[i])):
                if test_set[i][j] in negdict[j]:
                    neg = neg * (negdict[j][test_set[i][j]])
                else:
                    neg = neg * 1
            negfea.append(neg)

        label = np.array(posfea) / np.array(negfea)

        return label

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-trainfasta', required=True, help="trainfasta file name")
    parser.add_argument('-trainpos', required=True, type=int, help="trainpos")
    parser.add_argument('-trainneg', required=True, type=int, help="trainneg")
    parser.add_argument('-testfasta',  help="testfasta file name")
    parser.add_argument('-testpos',  type=int, help="testpos")
    parser.add_argument('-testneg',  type=int, help="testneg")
    parser.add_argument('-cv', default=10, type=int, help="cv")

    args = parser.parse_args()
    print(args)

    m = Markov()
    x = m.fasta2np(args.trainfasta)
    y = np.array([0] * args.trainpos + [1] * args.trainneg)

    if args.testfasta:
        x1 = m.fasta2np(args.testfasta)
        y1 = np.array([0] * args.testpos + [1] * args.testneg)

    if args.testfasta:
        posdict, negdict, atgc1, atgc2 = m.fit(x, args.trainpos)

        label = m.predict(x1, posdict, negdict, atgc1, atgc2)
        l = []
        for i in label:
            if i < 1:
                l.append(1)
            else:
                l.append(0)
        l=np.array(l)

        print("ACC:{}".format(metrics.accuracy_score(y1,l)))
        print("MCC:{}\n".format(metrics.matthews_corrcoef(y1,l)))
        print(classification_report(y1, l))
        print("confusion matrix\n")
        print(pd.crosstab(pd.Series(y1, name='Actual'), pd.Series(l, name='Predicted')))
    else:
        skf = StratifiedKFold(n_splits=args.cv)
        acc = []
        testlabel=[]
        truelabel=[]
        for train_index, test_index in skf.split(x, y):

            X_train, X_test = x[train_index], x[test_index]
            y_train, y_test = y[train_index], y[test_index]

            num = 0
            for i in y_train:
                if i == 0:
                    num += 1

            posdict, negdict, atgc1, atgc2 = m.fit(X_train, num)
            label = m.predict(X_test, posdict, negdict, atgc1, atgc2)

            l = []
            for i in label:
                if i < 1:
                    l.append(1)
                else:
                    l.append(0)

            acc.append(metrics.accuracy_score(y_test, l))
            testlabel+=l
            truelabel+=y_test.tolist()

            # print("ACC:{}".format(metrics.accuracy_score(y_test, l)))
        # print(np.mean(acc))
        print("ACC:{}".format(metrics.accuracy_score(truelabel,testlabel)))
        print("MCC:{}\n".format(metrics.matthews_corrcoef(truelabel, testlabel)))
        print(classification_report(truelabel, testlabel))
        print("confusion matrix\n")
        print(pd.crosstab(pd.Series(truelabel, name='Actual'), pd.Series(testlabel, name='Predicted')))

if __name__ == '__main__':
    main()


from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User

from librete.contrib.categories.models import Category
from librete.utils.models import TimestampModel
from .constants import PRIORITY_CHOICES, STATUS_CHOICES, ACTIVE

class Task(TimestampModel):
    name = models.CharField(_('Name'),
                            max_length=254)
    author = models.ForeignKey(User,
                               related_name='tasks',
                               on_delete=models.CASCADE)
    category = models.ForeignKey(Category,
                                 related_name='tasks',
                                 on_delete=models.CASCADE)
    start_date = models.DateTimeField(_('Start date'),
                                      blank=True,
                                      null=True)
    end_date = models.DateTimeField(_('End date'),
                                    blank=True,
                                    null=True)
    description = models.CharField(_('Description'),
                                   max_length=1000,
                                   blank=True)
    parent = models.ForeignKey('self',
                               blank=True,
                               null=True,
                               on_delete=models.CASCADE)
    priority = models.CharField(_('Priority'),
                                max_length=254,
                                choices=PRIORITY_CHOICES,
                                blank=True)
    status = models.CharField(_('Status'),
                              max_length=254,
                              choices=STATUS_CHOICES,
                              default=ACTIVE)

    class Meta:
        ordering = ['created_at']
        verbose_name = _('Task')
        verbose_name_plural = _('Tasks')

    def __str__(self):
        return self.name