import re
import math
import scipy.stats as stats
from statsmodels.stats.multitest import multipletests
import numpy as np
import pandas as pd

from tqdm import tqdm
# import functools
import pprint

from mutagene.dna import (
    nucleotides, complementary_nucleotide,
    bases_dict,
    # comp_dict,
    extended_nucleotides, complementary_extended_nucleotide)

from mutagene.io.motifs import get_known_motifs

import logging
logger = logging.getLogger(__name__)

def identify_motifs(samples_mutations, custom_motif=None, strand=None, threshold=None, dump_matches=None, stat_type=None):
    """
    :param samples_mutations: list of mutations from input file
    :param custom_motif: specified motif to search for
    :param strand: strand(s) to search on (T: transcribed, N: non-transcribed, A: any, or a combination theirof: 'TNA')
    :param dump_matches: pass through to process_mutations, stores all motif matches
    :param stat_type: pass through to process_mutations, choose statistical test
    :return: command-line output
    """
    motif_matches = []
    sig_motif_matches = []
    pvals = []

    if strand is None:
        strand = 'A'
    else:
        strand = set(strand)  # in case TNA codes repeat

    if threshold is None:
        threshold = 0.05

    if custom_motif:
        search_motifs = scanf_motif(custom_motif)
    else:
        motifs = get_known_motifs()
        search_motifs = motifs.copy()
    # search_motifs.extend(scanf_motif(custom_motif))

    _strand_map = {
        'T': 'transcribed',
        'N': 'non-transcribed',
        'A': 'any strand'
    }

    disable_progress_bar = logger.getEffectiveLevel() == logging.DEBUG

    for sample, mutations in tqdm(samples_mutations.items(), leave=False, disable=disable_progress_bar):
        if mutations is not None and len(mutations) > 0:
            first_mut_seq_with_coords = mutations[0][-1]
            window_size = (len(first_mut_seq_with_coords) - 1) // 2

            for m in tqdm(search_motifs, leave=False, disable=disable_progress_bar):
                for s in strand:
                    result, saved_data = process_mutations(
                        mutations,
                        m['motif'],
                        m['position'],
                        m['ref'],
                        m['alt'],
                        window_size,
                        s,
                        stat_type=stat_type)

                    if dump_matches:
                        for chrom, pos in saved_data['mutation_motif']:
                            dump_matches.write(
                                "chr{}\t{}\t{}\t{}\t{}\t{}\n".format(
                                    chrom, pos, int(pos) + 1, sample, m['logo'], _strand_map[s]))

                    debug_data = {
                        'sample': sample,
                        'motif': m['logo'],
                        'strand': s}
                    debug_data.update(result)
                    debug_string = pprint.pformat(debug_data, indent=4)
                    logger.debug(debug_string)

                    motif_matches.append({
                        'sample': sample,
                        'mutagen': m['name'],
                        'motif': m['logo'],
                        'strand': _strand_map[s],
                        'enrichment': result['enrichment'],
                        'mut_min': result['mutation_load'],
                        'mut_max': result['bases_mutated_in_motif'],
                        'odds_ratio': result['odds_ratio'],
                        'pvalue': result['pvalue']
                    })
                    pvals.append(result['pvalue'])

    qvalues = get_corrected_pvalues(pvals)
    for i, motif_dict in enumerate(motif_matches):
        motif_matches[i]['qvalue'] = qvalues[i]
        if motif_dict['mut_min'] == 0:
            continue
        if motif_dict['qvalue'] >= threshold:
            continue
        sig_motif_matches.append(motif_dict)

    return sig_motif_matches

def scanf_motif(custom_motif):
    """ recognize motif syntax like A[C>T]G and create a motif entry """
    m = re.search(
        r'([' + extended_nucleotides + ']*)\\[([' + nucleotides + '])>([' + extended_nucleotides + '])\\]([' + extended_nucleotides + ']*)',
        custom_motif.upper())
    if m:
        g = m.groups('')
        # print("GROUPS", m.group(1), m.group(2), m.group(3), m.group(4))
        entry = {}
        entry['logo'] = m.group(0)
        entry['motif'] = g[0] + g[1] + g[3]
        entry['position'] = len(g[0])
        entry['ref'] = g[1]
        entry['alt'] = g[2]
        if entry['ref'] == entry['alt']:
            return []
        entry['name'] = 'Custom motif'
        entry['references'] = ''
        return [entry, ]
    return []

def calculate_RR(ct):
    """
    Mutation is treatment
    No mutation is placebo

    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: enrichment or risk ratio
    """
    try:
        RR = ((ct.loc['mutation', 'motif'] / (ct.loc['mutation', 'motif'] + ct.loc['mutation', 'no motif'])) /
              (ct.loc['no mutation', 'motif'] / (ct.loc['no mutation', 'motif'] + ct.loc['no mutation', 'no motif'])))
    except ZeroDivisionError:
        RR = 0.0
    return RR

def calculate_RR_for_motif(ct):
    """
    Motif is treatment
    No motif is placebo

    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: enrichment or risk ratio
    """
    try:
        RR = ((ct.loc['mutation', 'motif'] / (ct.loc['mutation', 'motif'] + ct.loc['no mutation', 'motif'])) /
              (ct.loc['mutation', 'no motif'] / (ct.loc['mutation', 'no motif'] + ct.loc['no mutation', 'no motif'])))
    except ZeroDivisionError:
        RR = 0.0
    return RR

def calculate_OR(ct):
    """
    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: odds ratio
    """
    try:
        OR = (
            (ct.loc['mutation', 'motif'] / ct.loc['mutation', 'no motif']) /
            (ct.loc['no mutation', 'motif'] / ct.loc['no mutation', 'no motif']))
    except ZeroDivisionError:
        OR = 0.0
    return OR

def Haldane_correction(ct, pseudocount=0.5):
    """
    :param ct: mutually exclusive counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :return: contigency table after Haldane correction is applied
    """
    """    apply Haldane correction (+ 0.5) if any of the values in the contingency table is zero """

    return ct + pseudocount if np.any(np.isclose(ct.to_numpy(), 0.0)) else ct

def calculate_mutation_load(N_mutations, enrichment):
    """
    Mutation load (minimum estimate) calculation following Gordenin et al protocol
    However, at this point motif matches are not filtered for p-value significance
    That's done in the end after multiple testing correction
    """
    mutation_load = 0.0
    if enrichment > 1.0:
        mutation_load = N_mutations * (enrichment - 1) / enrichment
    # elif p_value < p_value_threshold:   tests for enrichment depletion
    return mutation_load

def get_stats(ct, stat_type='fisher'):
    """
    Calculate Fisher and Chi2 test pvalues,
    :param ct: counts of mutated matching motifs, matching mutations, matching motifs, and matching bases
    :param stat_type: Type of pvalue (Fisher's ('fisher') or Chi-Square ('chi2'))
    :return: pvalue of the corresponding statistical test
    """
    p_val = 1.0

    if stat_type is None:
        stat_type = 'fisher'

    stat_type = stat_type.lower()

    acceptable_tests = ('fisher', 'chi2')
    if stat_type not in acceptable_tests:
        logger.warning('get_stats() can only calculate p-values for ' + str(acceptable_tests))

    if stat_type == 'fisher':
        try:
            p_val = stats.fisher_exact(ct, alternative="greater")[1]
            # if p_val > 0.05:
            #     p_val = stats.fisher_exact(ct, alternative="less")[1] #calculates if motif is underrepresented
        except ValueError:
            p_val = 1.0
    elif stat_type == 'chi2':
        try:
            p_val = stats.chi2_contingency(ct)[1]
        except ValueError:
            p_val = 1.0
    return p_val

def get_corrected_pvalues(p_values):
    qvalues = []
    if len(p_values):
        qvalues = multipletests(pvals=p_values, method='fdr_bh')[1]
    return qvalues

# @functools.lru_cache(maxsize=None)
def get_rev_comp_seq(sequence):
    """
    :param sequence: forward DNA sequence
    :return: reverse complimentary DNA sequence
    """
    # rev_comp_seq = "".join([complementary_nucleotide[i] for i in reversed(sequence)])
    cn = complementary_nucleotide
    return [(i[0], i[1], cn[i[2]], '-') for i in reversed(sequence)]

def mutated_base(mutation, ref, alt):
    """
    :param mutation: [(record.CHROM, record.POS, record.REF, record.ALT)]
    :param ref: list the nucleotide base pre-mutation
    :param alt: list the nucleotide base post-mutation
    :return: True if mutation matches the specified ref and alt
    """
    # makes sure single base substitution
    _, _, mut_ref, mut_alt = mutation
    if mut_alt and mut_ref and len(mut_ref) == 1 and len(mut_alt) == 1 and mut_ref != mut_alt:
        # mutation matches the substitution
        if mutation[2] in bases_dict[ref] and mutation[3] in bases_dict[alt]:
            return True

def find_matching_motifs(seq, motif, motif_position):
    """
    :param seq: DNA sequence
    :param motif: specified motif
    :param motif_position: position of mutated base in motif, 0-base numbering
    :return: generator of matching positions

    TODO: SLOW algorithm O(n * m). Need to create a suffix tree with regexp
    """
    # print("Looking for motif {} in {}, {}".format(motif, sequence, len(sequence) - len(motif)))
    for i in range(len(seq) - len(motif) + 1):
        # s = seq[i: i + len(motif)]
        # print(s)
        for j, c in enumerate(motif):
            if seq[i + j][2] not in bases_dict[c]:
                break
        else:
            yield seq[i + motif_position]

def find_matching_bases(seq, ref, motif, motif_position):
    """
    :param seq:
    :param ref:
    :param motif:
    :param motif_position:
    :return: bases that match mutations
    """
    for i in range(motif_position, len(seq) - (len(motif) - motif_position) + 1):
        # range excludes border of sequence that may be motifs that don't fit window size
        if seq[i][2] in bases_dict[ref]:
            yield seq[i]

def make_contingency_table(
        array=None,
        motif_mutation=None,
        no_motif_mutation=None,
        motif_no_mutation=None,
        no_motif_no_mutation=None):
    """ Make a 2x2 contingency table out of a numpy array or four integers"""

    if array is not None:
        assert isinstance(array, np.ndarray)
        assert array.shape == (2, 2)
    else:
        array = np.array([
            [motif_mutation, no_motif_mutation],
            [motif_no_mutation, no_motif_no_mutation]
        ])
    contingency_table = pd.DataFrame(array)
    contingency_table.columns = ["motif", "no motif"]
    contingency_table.index = ["mutation", "no mutation"]
    return contingency_table

def process_mutations(mutations, motif, motif_position, ref, alt, range_size, strand, stat_type=None):
    """
    :param mutations: mutations to be analyzed
    :param motif: specified motif to search for
    :param motif_position: location of mutation in motif, 0-base numbering from left of motif
    :param ref: base pre-mutation
    :param alt: base post-mutation
    :param range_size: how far in the motif to search for
    :param strand: strand motif should be searched on
    :param stat_type: type of pvalue: Fisher's (default) or Chi-Square
    :param dump_matches: an optional file handle to save all mutations matching the motif regardless of their significance
    :return: (results summary disctionary, data_dump with stored_data or None if dump_matches is None)
    """
    assert range_size >= 0
    assert len(ref) == 1
    assert len(alt) == 1
    assert 0 <= motif_position < len(motif)
    assert len(set(strand) - set("ATN")) == 0, "[process_mutations] only A, T, N allowed in strand parameter"

    matching_bases = set()
    matching_motifs = set()
    matching_mutated_motifs = set()
    matching_mutated_bases = set()

    # extra loop for sample in sample list
    for chrom, pos, transcript_strand, x, y, seq in mutations:
        # extract the longest sequence we would ever need (motif + range_size); range size = # bases outside mutation
        mutation = chrom, pos, x, y
        rev_seq = get_rev_comp_seq(seq)

        # assuming that all mutations are reported in '+' reference strand
        if strand == 'A' or (strand == 'T' and transcript_strand == '+') or (strand == 'N' and transcript_strand == '-'):
            # not mutated:
            for ref_match in find_matching_bases(seq, ref, motif, motif_position):
                matching_bases.add(ref_match[0:2])
            for motif_match in find_matching_motifs(seq, motif, motif_position):
                matching_motifs.add(motif_match[0:2])

            # mutated:
            if mutated_base(mutation, ref, alt):
                # m = (mutation[0], mutation[1], mutation[2], "+")
                matching_mutated_bases.add(mutation[0:2])

                context_of_mutation = seq[range_size - motif_position: range_size - motif_position + len(motif)]
                for motif_match in find_matching_motifs(context_of_mutation, motif, motif_position):
                    matching_mutated_motifs.add(motif_match[0:2])

        if strand == 'A' or (strand == 'T' and transcript_strand == '-') or (strand == 'N' and transcript_strand == '+'):
            # rev compl: not mutated:
            for ref_match in find_matching_bases(rev_seq, ref, motif, motif_position):
                matching_bases.add(ref_match[0:2])

            for motif_match in find_matching_motifs(rev_seq, motif, motif_position):
                matching_motifs.add(motif_match[0:2])

            # rev compl: mutated:
            if mutated_base(mutation, complementary_extended_nucleotide[ref], complementary_extended_nucleotide[alt]):
                # m = (mutation[0], mutation[1], mutation[2], "-")
                matching_mutated_bases.add(mutation[0:2])

                # rev comp:
                context_of_mutation = rev_seq[range_size - motif_position: range_size - motif_position + len(motif)]
                for motif_match in find_matching_motifs(context_of_mutation, motif, motif_position):
                    matching_mutated_motifs.add(motif_match[0:2])

    motif_mutation_count = len(matching_mutated_motifs)  # bases mutated in motif
    stat_mutation_count = len(matching_mutated_bases - matching_mutated_motifs)  # bases mutated not in motif
    stat_motif_count = len(matching_motifs - matching_mutated_motifs)  # bases not mutated in motif
    stat_ref_count = len(matching_bases - (matching_motifs | matching_mutated_bases))  # bases not mutated not in motif

    # number of A[T>G]T occurrences               motif_mutation_count
    # / number of [T>G] occurrences               stat_mutation_count + motif_mutation_count
    # ----------
    # number of ATT occurrences in DNA context    stat_motif_count
    # / number of T occurrences in DNA context    stat_ref_count + stat_motif_count

    contingency_table = make_contingency_table(
        motif_mutation=motif_mutation_count,
        no_motif_mutation=stat_mutation_count,
        motif_no_mutation=stat_motif_count,
        no_motif_no_mutation=stat_ref_count)

    # data={
    # "'{}>{}' mutation".format(ref, alt): [stat_mutation_count, motif_mutation_count],
    # "no '{}>{}' mutation".format(ref, alt): [stat_ref_count, stat_motif_count]},
    # index=("no '{}' motif".format(motif), "'{}' motif".format(motif)))
    logger.debug("\n" + contingency_table.to_string() + "\n")

    logger.debug("({} / ({} + {})  ) /  ({} / ({} + {}))".format(
        contingency_table.loc['mutation', 'motif'],
        contingency_table.loc['mutation', 'motif'],
        contingency_table.loc['mutation', 'no motif'],
        contingency_table.loc['no mutation', 'motif'],
        contingency_table.loc['no mutation', 'motif'],
        contingency_table.loc['no mutation', 'no motif']))

    contingency_table = Haldane_correction(contingency_table)

    enrichment = risk_ratio = calculate_RR(contingency_table)  # enrichment = risk ratio
    odds_ratio = calculate_OR(contingency_table)

    p_val = get_stats(contingency_table, stat_type)

    mut_load = calculate_mutation_load(motif_mutation_count, enrichment)

    result = {
        'enrichment': enrichment,  # AKA risk ratio
        'odds_ratio': odds_ratio,
        'mutation_load': math.ceil(mut_load),
        'pvalue': p_val,
        'bases_mutated_in_motif': motif_mutation_count,
        'bases_mutated_not_in_motif': stat_mutation_count,
        'bases_not_mutated_in_motif': stat_motif_count,
        'bases_not_mutated_not_in_motif': stat_ref_count,
        'total_mutations': len(mutations)
    }

    saved_matches = {
        'mutation_motif': matching_mutated_motifs
    }
    return result, saved_matches


def encode(s):
  k = '0123456789abcdefghijklmnopqrstuv'

  v = 0
  vbits = 0
  output = []

  for c in s:
    v |= ord(c) << vbits
    vbits += 8

    while vbits >= 5:
      output.append(k[v & 31])
      v >>= 5
      vbits -= 5

  if vbits:
    output.append(k[v])

  return ''.join(output)

def decode(s):
  v = 0
  vbits = 0
  output = []

  for c in s.lower():
    if c >= '0' and c <= '9':
      u = ord(c) - ord('0')
    elif c >= 'a' and c <= 'v':
      u = ord(c) - ord('a') + 10
    else:
      raise ValueError('Invalid base-32 input')

    v |= u << vbits
    vbits += 5

    if vbits >= 8:
      output.append(chr(v & 255))
      v >>= 8
      vbits -= 8

  if vbits >= 5 or v:
    raise ValueError('Invalid base-32 input')

  return ''.join(output)


import sys
sys.stdin = open('../input.txt', 'r')

def solve(d, next):
    global real
    if d == 7:
        if sum(real) == 100:
            print('\n'.join(map(str, sorted(real))))
            sys.exit(0)
    else:
        for i in range(next, 9):
            if sum(real) <= 100:
                real.append(heights[i])
                solve(d+1, next+1)
                real.pop()

heights = [int(input()) for _ in range(9)]
real = []

solve(0, 0)


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

from flask import Flask, render_template
import platform
import netifaces

myApp = Flask(__name__)

@myApp.route('/')
def home():
    data = {'user': 'ramy', 'machine':platform.node(), 'os':platform.system(), 'dist':platform.linux_distribution(), 'interfaces':netifaces.interfaces()}

    return render_template('index.system.html', title='Home', data=data)

if __name__ == '__main__':
    myApp.run(host='0.0.0.0', port=999)


from time import strftime
from configuration_validation import extract_data
from configuration_validation import configuration_validation_tool
import os

def main():
    user_response = input('Do you want to search for all master.pmc files? [Y/N]\n')
    master_file_list = list()
    if user_response.lower() == 'y':
        master_file_list = configuration_validation_tool.find_all_master()
    elif user_response.lower() == 'n':
        master_file_list.append(configuration_validation_tool.find_one_master())
    else:
        print('That is not a valid answer, please try again.')
        main()
    file_data, mcs = build_file(master_file_list)
    export_label_data(mcs, file_data)
    print('Cables labels produced.')
    answer = input('Would you like to produce more cable labels? [Y/N]\n or type "Other" to select a different tool.\n')
    configuration_validation_tool.retry(answer, '3')

def build_file(master_files):
    file_data = list()
    mcs = 'MCS Not found'
    for file in master_files:
        controller_frame = import_label_data(file)
        cable_label_data = cable_label(controller_frame)
        for i in cable_label_data:
            axis_formatted_data = format_cable_label(i)
            file_data.append(axis_formatted_data)
        mcs = str(cable_label_data[0]['mcs'])
    return file_data, mcs

def import_label_data(master_path):
    # Imports the raw data required for the labels from a '.e.pmc' file.
    data_fields = ['e_BrickN_P',
                   'e_DevName_P',
                   'e_AxisID_P',
                   'b_AxisN_P',
                   'a_Commutation_D',
                   'a_IsBrakeUsed_X',
                   'e_StageEncPortN_X',
                   'b_StageEncPortN_P',
                   'e_MotEncPortN_X',
                   'b_MotEncPortN_P',
                   'a_IsEncoderUsed_X',
                   'e_BrickIP_P',
                   ]
    controller_frame = extract_data.main(master_path, data_fields)
    return controller_frame

def cable_label(controller_data):
    # Applies consistent wording for variables, and imports encoder details if required.
    data = list()
    aux = dict()
    for i, v in controller_data.iterrows():
        label = dict()
        i = int(i)
        if controller_data.iloc[i]['a_Commutation_D'].lower() == "'directmicrostepping'":
            label['motor_type'] = 'Stepper'
        elif controller_data.iloc[i]['a_Commutation_D'].lower() == "'notdirectmicrostepping'":
            label['motor_type'] = 'Stepper'
        elif controller_data.iloc[i]['a_Commutation_D'].lower() == "'brushlessdc'":
            label['motor_type'] = 'BrushlessDC'
        elif controller_data.iloc[i]['a_Commutation_D'].lower() == "'dcbrush'":
            label['motor_type'] = 'DCBrushed'
        else:
            print(controller_data.iloc[i]['a_Commutation_D'])
            label['motor_type'] = 'Error'
        if controller_data.iloc[i]['a_IsEncoderUsed_X'] == '1':
            label['enc_type'] = 'ENC'
            label['stage_enc_axis'] = controller_data.iloc[i]['b_StageEncPortN_P']
            label['motor_enc_axis'] = controller_data.iloc[i]['b_MotEncPortN_P']
        else:
            label['enc_type'] = False
            label['stage_enc_axis'] = 'none'
            label['motor_enc_axis'] = 'none'
        if controller_data.iloc[i]['a_IsBrakeUsed_X'] == '1':
            aux['maux'] = 'aux'
            aux['mcs'] = strip_grab(controller_data, 'e_BrickN_P', i)
        label['mcs'] = strip_grab(controller_data, 'e_BrickN_P', i)
        label['axis'] = strip_grab(controller_data, 'b_AxisN_P', i)
        label['axisname'] = strip_grab(controller_data, 'e_AxisID_P', i)
        label['devname'] = strip_grab(controller_data, 'e_DevName_P', i)
        data.append(label.copy())
    data.append(aux.copy())
    return data

def format_cable_label(label_data):
    # Formats the data for use with BMP-71 labeler.
    formatted_data = list()
    valid_mot_axis = ['Stepper', 'BrushlessDC', 'DCBrushed', ]
    valid_enc_axis = ['1', '2', '3', '4', '5', '6', '7', '8', ]
    if 'maux' in label_data and label_data['maux'] == 'aux':
        formatted_data.append('MCS{0},MAUX,\n'.format(label_data['mcs']))
        return formatted_data
    if 'motor_type' in label_data and label_data['motor_type'] in valid_mot_axis:
        formatted_data.append('MCS{0},{1},#{2} - {3},:{4},\n'.format(label_data['mcs'],
                                                                       label_data['motor_type'],
                                                                       label_data['axis'],
                                                                       label_data['devname'],
                                                                       label_data['axisname'], ))
    elif 'motor_type' in label_data and label_data['motor_type'] not in valid_mot_axis:
        formatted_data.append('Motor Type not found!,ERROR,ERROR,ERROR,\n')
    if 'enc_type' in label_data and label_data['enc_type'] is not False and label_data['stage_enc_axis'] in \
            valid_enc_axis:
        formatted_data.append('MCS{0},{1},#{2} - {3},:{4},\n'.format(label_data['mcs'],
                                                                       label_data['enc_type'],
                                                                       label_data['stage_enc_axis'],
                                                                       label_data['devname'],
                                                                       label_data['axisname']))
    if 'enc_type' in label_data and label_data['enc_type'] is not False and label_data['motor_enc_axis'] in \
            valid_enc_axis:
        formatted_data.append('MCS{0},{1},#{2} - {3},:{4},\n'.format(label_data['mcs'],
                                                                       label_data['enc_type'],
                                                                       label_data['motor_enc_axis'],
                                                                       label_data['devname'],
                                                                       label_data['axisname']))

    return formatted_data

def export_label_data(mcs, data):
    # Writs the data to a csv file.
    mcs = str(mcs)
    output_path = os.getcwd().rsplit('\\', 1)[0]
    filename = output_path + '\\output\\Labels_MCS{0}_{1}'.format(mcs, strftime("%Y%m%d%H%M%S"))
    with open(filename + '.csv', 'w') as f:
        for label in data:
            for line in label:
                f.write(line)
        f.close()

def strip_grab(dataframe, var, i):
    # Strips values of unnecessary quotation marks.
    try:
        value = dataframe.iloc[i][var]
    except KeyError:
        value = ' No valid data.'
    if isinstance(value, str):
        value = value.strip("'")
        return value
    else:
        return value


def majorityElement(nums):
    # Moore’s Voting Algorithm
    n = len(nums)
    count = 0
    element = None

    for i in range(n):
        if count == 0:
            count = 1
            element = nums[i]

        elif element == nums[i]:
            count += 1

        else:
            count -= 1

    # Checking if the stored element is the majority element

    count2 = 0

    for i in range(n):
        if nums[i] == element:
            count2 += 1

    if count2 > (n / 2):
        return element

    return -1

nums = [2, 2, 1, 1, 1, 2, 2]
print(majorityElement(nums))


from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras import regularizers
import numpy as np
import pandas as pd
import math as math
import sys
import os
import keras
from keras.models import load_model
from keras.layers import Dropout , Flatten
from keras.layers import BatchNormalization
from keras.preprocessing.text import text_to_word_sequence
from keras.preprocessing.text import one_hot
import string
from keras.layers import MaxPooling1D
from keras.layers import Flatten
from keras.layers import ConvLSTM2D
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import LSTM,GRU,TimeDistributed
from keras.layers import Dense
from keras.layers.embeddings import Embedding
from gensim.models.word2vec import Word2Vec

def normal_string(string):

	if not string:
		return ""
	if len(string) <= 2:
		return string
	if len(string) > 2 and string[0] == string[1] and string[1] == string[2]:
		return normal_string(string[1:])
	return string[0] + normal_string(string[1:])

def remove_space(text):

	index_list = [i for i, letter in enumerate(text) if letter == '\'']
	remove_list = []
	for i in range(0,len(index_list)):
		if index_list[i]-1 >= 0 and text[index_list[i]-1] == ' ':
			remove_list.append(index_list[i]-1)
		if index_list[i]+1 < len(text) and text[index_list[i]+1] == ' ':
			remove_list.append(index_list[i]+1)
		#remove_list.append(index_list[i])
	text = "".join([char for idx, char in enumerate(text) if idx not in remove_list])
	return text

mode = sys.argv[3]

test_data_filename = sys.argv[1]
t_lines = [line.rstrip('\n') for line in open(test_data_filename,'r' , errors='replace' , encoding='utf-8')]
t_lines = t_lines[1:]
for i in range(0,len(t_lines)):
	num = len(str(i))
	t_lines[i] = t_lines[i][num+1:]
w2v_t_lines = []
for i in range(0, len(t_lines)):
	t_lines[i] = remove_space(t_lines[i])
	tk = text_to_word_sequence(t_lines[i], filters='', lower=True, split=' ')
	tmp_line = []
	tmp = ""
	for j in range(0,len(tk)):
		tk[j] = tk[j].encode("ascii", errors="ignore").decode()
		tk[j] = normal_string(tk[j])
		tmp_line.append(tk[j])
		tmp = tmp + tk[j] + " "
	t_lines[i] = tmp
	w2v_t_lines.append(tmp_line)

model = Word2Vec.load("gensim_w2v_0.82693_0602_model")
word_vectors = model.wv
vocab = []
for k, v in word_vectors.vocab.items():
	vocab.append( (k,v.index) )

vocab = sorted(vocab , key=lambda x:x[1])
word_index_dict = {}
for i in range(0,len(vocab)):
	word = vocab[i][0]
	word_index_dict[word] = i+1
word_index_dict["unknown_word"] = len(vocab)+1

test_ind = []
for i in range(len(w2v_t_lines)):
	tmp = []
	for w in w2v_t_lines[i]:
		if w not in word_index_dict:
			tmp.append(word_index_dict["unknown_word"])
		else:
			tmp.append(word_index_dict[w])
	test_ind.append(tmp)

rnn_model = load_model("0602_gensim_0.82693.h5")
test = sequence.pad_sequences(test_ind, maxlen=33)
p = 0.0
p += rnn_model.predict(test)
ans_filename = sys.argv[2]
ans_file = open(ans_filename , 'w')
ans_file.write("id,label\n")

for i in range(0,len(p)):
	ans_file.write(str(i))
	ans_file.write(',')
	if p[i][0] >= 0.5:
		ans_file.write('1')
	else:
		ans_file.write('0')
	ans_file.write('\n')


"""
This module contains the main transmittance/reflectance calculation
bits.

Users can run the calculations through `model.Model()` and avoid
accessing `core` directly.
"""

import numpy as np
import scipy as sp

def rt_amp(index, delta, theta, pol):
    """Calculate the reflected and transmitted amplitudes through the
    system.

    Parameters
    ----------
    index : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    delta : numpy array
        An array of wavenumber offsets.
    theta : numpy array
        An array of angles in radians.
    pol : string
        The polarization of the source wave: 's' or 'p',
        or 'u'.

    Returns
    -------
    r, t : tuple
        A tuple where 'r' is the reflected amplitude, and 't' is the
        transmitted amplitude.
    """
    t_amp, r_amp = make_rt_amp_matrix(index, theta, pol)
    m_mat = make_m_matrix(index, t_amp, r_amp, delta)

    m_prime = make_2x2(1., 0., 0., 1., dtype=complex)
    for i in range(1, len(index)-1):
        m_prime = np.dot(m_prime, m_mat[i])

    C_m = make_2x2(1., r_amp[0, 1], r_amp[0, 1], 1., dtype=complex)
    m_prime = np.dot(C_m / t_amp[0, 1], m_prime)
    trans_amp = 1 / m_prime[0, 0]
    ref_amp = m_prime[1, 0] / m_prime[0, 0]
    return ref_amp, trans_amp

def make_rt_amp_matrix(index, theta, pol):
    """Construct reflection and transmission amplitude matrices.

    Parameters
    ----------
    index : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    theta : numpy array
        An array of angles in radians.
    pol : string
        The polarization of the source wave: 's' or 'p'.

    Returns
    -------
    t_mat, r_mat : tuple
        The t- and r-amplitude matrices.
    """
    t_mat = np.zeros((len(index), len(index)), dtype=complex)
    r_mat = np.zeros((len(index), len(index)), dtype=complex)
    for i in range(len(index) - 1):
        t_mat[i, i+1] = t_interface(index[i], index[i+1], theta[i], theta[i+1], pol)
        r_mat[i, i+1] = r_interface(index[i], index[i+1], theta[i], theta[i+1], pol)
    return t_mat, r_mat

def make_m_matrix(index, t_matrix, r_matrix, delta):
    """Construct the characteristic matrix of the model.

    Parameters
    ----------
    index : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    t_matrix : numpy array
        The t-amplitude matrix
    r_matrix : numpy array
        The r-amplitude matrix
    delta : numpy array
        An array of wavenumber offsets.

    Returns
    -------
    m_mat : numpy array
        The characteristic matrix of the model
    """
    m_mat = np.zeros((len(index), 2, 2), dtype=complex)
    for i in range(1, len(index)-1):
        C_m = make_2x2(np.exp(-1j * delta[i]), 0., 0., np.exp(1j * delta[i]),
                       dtype=complex)
        r_m = make_2x2(1., r_matrix[i, i+1], r_matrix[i, i+1], 1., dtype=complex)
        m_mat[i] = (1 / t_matrix[i, i+1]) * np.dot(C_m, r_m)
    return m_mat

def r_power(r_amp):
    """Return the fraction of reflected power.

    Parameters
    ----------
    r_amp : float
        The net reflection amplitude after calculating the transfer
        matrix.

    Returns
    -------
    R : numpy array
        The model reflectance
    """
    return np.abs(r_amp)**2

def t_power(t_amp, index_i, index_f, theta_i, theta_f):
    """Return the fraction of transmitted power.

    Parameters
    ----------
    t_amp : float
        The net transmission amplitude after calculating the transfer
        matrix.
    index_i : float
        The index of refraction of the source material.
    index_f : float
        The index of refraction of the terminating material.
    theta_i : float
        The angle of incidence (radians) at the initial interface.
    theta_f : float
        The angle of incidence (radians) at the final interface.

    Returns
    -------
    T : numpy array
        The model transmittance
    """
    return np.abs(t_amp**2) * \
           ( (index_f * np.cos(theta_f)) / (index_i * np.cos(theta_i) ) )

def r_interface(index1, index2, theta1, theta2, pol):
    """Calculate the reflected amplitude at an interface.

    Parameters
    ----------
    index1 : float
        The index of refraction of the first material.
    index2 : float
        The index of refraction of the second material.
    theta1 : float
        The angle of incidence at interface 1, in radians
    theta2 : float
        The angle of incidence at interface 2, in radians
    pol : string
        The polarization of the source wave (either 's' or 'p').

    Returns
    -------
    reflected amplitude : float
        The amplitude of the reflected field at the interface
    """
    if pol == 's':
        numerator = (index1 * np.cos(theta1) - index2 * np.cos(theta2))
        denominator = (index1 * np.cos(theta1) + index2 * np.cos(theta2))
    elif pol == 'p':
        numerator = (index2 * np.cos(theta1) - index1 * np.cos(theta2))
        denominator = (index1 * np.cos(theta2) + index2 * np.cos(theta1))
    else:
        raise ValueError("Polarization must be 's' or 'p'")
    return numerator / denominator

def t_interface(index1, index2, theta1, theta2, pol):
    """Calculate the transmission amplitude at an interface.

    Parameters
    ----------
    index1 : float
        The index of refraction of the first material.
    index2 : float
        The index of refraction of the second material.
    theta1 : float
        The angle of incidence at interface 1, in radians
    theta2 : float
        The angle of incidence at interface 2, in radians
    pol : string
        The polarization of the source wave (either 's' or 'p').

    Returns
    -------
    transmitted_amplitude : float
        The amplitude of the transmitted field at the interface
    """
    if pol == 's':
        numerator = 2 * index1 * np.cos(theta1)
        denominator = (index1 * np.cos(theta1) + index2 * np.cos(theta2))
    elif pol == 'p':
        numerator = 2 * index1 * np.cos(theta1)
        denominator = (index1 * np.cos(theta2) + index2 * np.cos(theta1))
    else:
        raise ValueError("Polarization must be 's' or 'p'")
    return numerator / denominator

def wavenumber(freq, index, tand):
    """Calculate the wavenumber in a material.

    Parameters
    ----------
    freq : float
        The frequency at which to calculate the wavevector, k
    tand : numpy array
        An array of loss tangents, ordered from source to terminating
    index : numpy array
        An array of refractive indices, ordered from source to
        terminating layer

    Returns
    -------
    k : array
        The complex wavenumber, k
    """
    k = 2 * np.pi * (freq / 3e8) * index * np.sqrt(1 + 1j * tand)
    return k

def alpha2imagn(freq, a, b, n):
    """Convert Halpern's 'a' and 'b' from an absorption coefficient
    of the form `a*freq**b` to a (frequency-dependent) .

    Parameters
    ----------
    freq : numpy array or float
        The frequency (Hz) (or frequencies) at which to calculate the loss
        tangent.
    a : float
        Halpern's 'a' coefficient
    b : float
        Halpern's 'b' coefficient
    n : float
        The real part of the material's refractive index

    Returns
    -------
    imagn : numpy array or float
        The imaginary component of the refractive index
    """
    nu = freq / 30e9
    # First we need the frequency-dependent absorption coefficient,
    # alpha, which we get from the Halpern fit. From that we will
    # calculate k(appa), the extinction coefficient, for each
    # frequency of interest
    alpha = 2 * a * nu**b

    # This is the absorption-extinction coefficient relation as ~written
    # in Born & Wolf Principles of Optics 1st Ed., 1959, Ch. 13.1,
    # Pg. 614, Eq. 21
    # The factor of 3e10 (c in units of cms^-1) ensures that our k is
    # unitless, as it ought to be.
    imagn = (100 * 3e8 * alpha) / (4 * np.pi * n * freq)
    return imagn

def alpha2tand(freq, a, b, n):
    """Convert Halpern's 'a' and 'b' from an absorption coefficient
    of the form `a*freq**b` to a (frequency-dependent) loss tangent.

    Parameters
    ----------
    freq : numpy array or float
        The frequency (Hz) (or frequencies) at which to calculate the loss
        tangent.
    a : float
        Halpern's 'a' coefficient
    b : float
        Halpern's 'b' coefficient
    n : float
        The real part of the material's refractive index

    Returns
    -------
    tand : numpy array
        The loss tangent of the material at the given frequency and
        Halpern coefficients.
    """
    imagn = alpha2imagn(freq, a, b, n)

    # The complex index of refraction of a material is related to the
    # complex (relative) permittivity by the relation:
    #   e_r = e' + i*e'' = n^2 = (n + i*k)^2 = n^2 - k^2 + i*2nk
    # By equating the real and imaginary parts we are left with:
    #   e' = (n^2 - k^2); e'' = 2nk
    # With this information we can find the loss tangent, which is simply
    # the ratio of the real and imaginary parts of the relative
    # permittivity:
    #   tand = (e''/e')
    ep = n**2 - imagn**2
    epp = 2 * n * imagn
    tand = epp / ep
    return tand

def make_2x2(a11, a12, a21, a22, dtype=float):
    """Return a 2x2 array quickly.

    Parameters
    ----------
    a11 : float
        Array element [0, 0].
    a12 : float
        Array element [0, 1].
    a21 : float
        Array element [1, 0].
    a22 : float
        Array element [1, 1].
    dtype : dtype, optional
        The datatype of the array. Defaults to float.

    Returns
    -------
    array : numpy array
        A 2x2 array [[a11, a12], [a21, a22]]
    """
    array = np.empty((2, 2), dtype=dtype)
    array[0, 0] = a11
    array[0, 1] = a12
    array[1, 0] = a21
    array[1, 1] = a22
    return array

def prop_wavenumber(k, d, theta):
    """Propagate the wave through a material and calculate its offset,
    delta.

    Parameters
    ----------
    k : array
        The wavenumber
    d : array
        An array of distances (thicknesses), ordered from source to
        terminating layer
    theta : float
        The angle the wave passes through the medium

    Returns
    -------
    delta : array
        The phase difference
    """
    # Turn off 'invalid multiplication' error; it's just the 'inf' boundaries
    olderr = sp.seterr(invalid='ignore')
    delta = k * d * np.cos(theta)
    # Now turn the error back on
    sp.seterr(**olderr)
    return delta

def refract(n, theta0):
    """Calculate the angle by which an incident ray is refracted

    Parameters
    ----------
    n : numpy array
        An array of refractive indices, ordered from source layer to
        terminator layer.
    theta0 : float
        The initial angle of incidence (radians)

    Returns
    -------
    thetas : numpy array
        The Snell angles at each interface
    """
    # Make a nice pairwise generator so we can avoid playing games with
    # index counting
    thetas = [theta0]
    ngen = zip(n, n[1:])
    for i, rind in enumerate(ngen):
        theta = np.arcsin(np.real_if_close( rind[0] * np.sin(thetas[i]) / rind[1] ))
        thetas.append(theta)
    return np.asarray(thetas)

def replace_tand(freq, tand_array, halpern_dict):
    """Calculate a frequency-dependent loss tangent from a material's
    Halpern coefficiencts if they exist.

    Parameters
    ----------
    freq : float
        The frequency at which to calculate the loss tangent
    tand_array : numpy array
        The loss tangents of the materials, ordered from Source to
        Terminator
    halpern_dict : dict
        A dictionary keyed by layer index, containing Halpern coefficients

    Returns
    -------
    tand_array : numpy array
        The loss tangents of the materials, ordered from Source to
        Terminator. Where possible, the Halpern coefficients have been
        applied to make the terms frequency-dependent.
    """
    for k, v in halpern_dict.items():
        tand_array[k] = alpha2tand(freq, v['a'], v['b'], v['n'])
    return tand_array

def main(params):
    """Run a transmittance/reflectance calculation for the given parameters.

    This function is the primary entry-point to the calculation, and should
    not be called directly. Instead, call `Model.run()`.

    If you must call `core.main()` directly, only do so after first calling
    `Model.set_up()`.

    Parameters
    ----------
    params : dict
        The dictionary contructed by `Model.set_up`. See that function
        documentation for details.

    Returns
    -------
    result : dict
        A dictionary with three keys:
         * `frequency`: the frequency (in Hz) at which T and R were calculated
         * `transmittance`: the output transmittance (T) of the model
         * `reflectance`: the output reflectance (R) of the model
    """
    rind = params['rind']
    thick = params['thick']
    tand = params['tand']
    pol = params['pol']
    theta0 = params['theta0']
    theta = refract(rind, theta0)
    freq = params['freq']
    halps = params['halpern_layers']

    # Create containers for the reflection/transmission values we calculate
    # at each frequency
    ts = []
    rs = []

    for f in freq:
        if len(halps.keys()) > 0:
            tand = replace_tand(f, tand, halps)
        ks = wavenumber(f, rind, tand)
        delta = prop_wavenumber(ks, thick, theta)
        r_amp, t_amp = rt_amp(rind, delta, theta, pol)
        t_pow = t_power(t_amp, rind[0], rind[-1], theta[0], theta[-1])
        r_pow = r_power(r_amp)
        ts.append(t_pow)
        rs.append(r_pow)

    ts = np.asarray(ts)
    rs = np.asarray(rs)

    results = {'frequency':freq, 'transmittance':ts, 'reflectance':rs}
    return results


from llm_rs import SessionConfig, GenerationConfig, Gpt2

class Chainer:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(Chainer, cls).__new__(cls)
            cls._instance.init_chainer()
        return cls._instance

    def init_chainer(self):
        self.stop_words = ['<EOL>', '<eol>', '<Eol>', 'pertanyaan :', 'Human', 'human', 'Pertanyaan', '\n']
        self.previous_qa = []

        session_config = SessionConfig(
            threads=4,
            context_length=1300,
            prefer_mmap=False
        )

        self.generation_config = GenerationConfig(
            top_p=0.44,
            top_k=1,
            temperature=0.22,
            max_new_tokens=120,
            repetition_penalty=1.13,
            stop_words=self.stop_words
        )

        self.model = Gpt2("2midguifSfFt5SbHJsxP.bin", session_config=session_config)

    def chain(self, user_input):
        if self.previous_qa:
            previous_question, previous_answer = self.previous_qa[-1]
        else:
            previous_question, previous_answer = "", ""

        template = f"saya bisa menjawab pertanyaan dengan masalah kesehatan.\nHai! Saya adalah chatbot yang akan menjawab pertanyaan seputar kesehatan. Saya adalah chatbot, bukan manusia.\nanda dapat menanyakan saya pertanyaan seputar kesehatan melalui kolom teks dibawah.\n\nPertanyaan saya:\n{previous_question}\n\nJawaban anda:\n{previous_answer}\n\nPertanyaan saya: {user_input}.\nJawaban anda :"

        result = self.model.generate(template, generation_config=self.generation_config)
        response = result.text.strip()

        self.previous_qa.append((user_input, response))

        if len(self.previous_qa) > 1:
            self.previous_qa.pop(0)

        return response

generator = Chainer()

def generate(text_input):
    result = generator.chain(text_input)
    return result


# -*- coding: utf-8 -*-
#!/usr/bin/env python
from theory.conf import settings
from theory.utils.safestring import markSafe
from theory.utils import six

def format(number, decimalSep, decimalPos=None, grouping=0, thousandSep='',
      forceGrouping=False):
  """
  Gets a number (as a number or string), and returns it as a string,
  using formats defined as arguments:

  * decimalSep: Decimal separator symbol (for example ".")
  * decimalPos: Number of decimal positions
  * grouping: Number of digits in every group limited by thousand separator
  * thousandSep: Thousand separator symbol (for example ",")
  """
  useGrouping = settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR
  useGrouping = useGrouping or forceGrouping
  useGrouping = useGrouping and grouping > 0
  # Make the common case fast
  if isinstance(number, int) and not useGrouping and not decimalPos:
    return markSafe(six.textType(number))
  # sign
  sign = ''
  strNumber = six.textType(number)
  if strNumber[0] == '-':
    sign = '-'
    strNumber = strNumber[1:]
  # decimal part
  if '.' in strNumber:
    intPart, decPart = strNumber.split('.')
    if decimalPos is not None:
      decPart = decPart[:decimalPos]
  else:
    intPart, decPart = strNumber, ''
  if decimalPos is not None:
    decPart = decPart + ('0' * (decimalPos - len(decPart)))
  if decPart:
    decPart = decimalSep + decPart
  # grouping
  if useGrouping:
    intPartGd = ''
    for cnt, digit in enumerate(intPart[::-1]):
      if cnt and not cnt % grouping:
        intPartGd += thousandSep
      intPartGd += digit
    intPart = intPartGd[::-1]
  return sign + intPart + decPart


#!/usr/bin/python3

# enable debugging
import cgi, cgitb
import json
import requests
import responses
cgitb.enable()

class Expense:
     def __init__(self, exp_name,exp_date,exp_amount,exp_type):
         self.name = exp_name
         self.date = exp_date
         self.amount = exp_amount
         self.type = exp_type

form = cgi.FieldStorage()
exp_name = form.getvalue('exp_name')
exp_date  = form.getvalue('exp_date')
exp_amount = form.getvalue('exp_amount')
exp_type  = form.getvalue('exp_type')

expense = Expense(exp_name,exp_date,exp_amount,exp_type)

jsonString = json.dumps(expense.__dict__)
jsonFile = open("/var/www/html/data.json", "a+")
jsonFile.write(jsonString)
jsonFile.close()

print('Content-Type: text/plain')
print('')
print('sucessful')
print('<br>')
print(jsonString)


from django import template
from ..models import Page

register = template.Library()

@register.simple_tag
def main_menu():
    "Query top-level pages"
    return Page.objects.with_tree_fields().filter(
        parent=None, is_active=True)


import pytest

from base.client_base import TestcaseBase
from common import common_func as cf
from common import common_type as ct
from common.common_type import CaseLabel
from utils.util_log import test_log as log

# customer rg
rg_name_0 = "RG_0"
rg_name_1 = "RG_1"

# coll name
coll_name_1 = "ResourceGroup_111"
coll_name_2 = "ResourceGroup_222"

# resource group info of 4 qns
resource_group_info = [
    {"name": rg_name_0, "available_node": 1, "capacity": 1, "loaded_replica": {coll_name_1: 1}},
    {"name": rg_name_1, "available_node": 1, "capacity": 1, "loaded_replica": {coll_name_1: 1}},
    {"name": ct.default_resource_group_name, "available_node": 2,
     "capacity": ct.default_resource_group_capacity, "loaded_replica": {coll_name_2: 2}}
]

class TestChaosRG(TestcaseBase):
    """ Test case of end to end"""

    def teardown_method(self, method):
        log.info(("*" * 35) + " teardown " + ("*" * 35))
        log.info("[teardown_method] Start teardown test case %s..." %
                 method.__name__)
        log.info("skip drop collection")

    @pytest.mark.tags(CaseLabel.L3)
    def test_milvus_resource_group(self):
        nb = 10000
        # collection rg map
        collection_rg_map = {
            coll_name_1: {"resource_groups": [rg_name_0, rg_name_1], "replica_number": 2},
            coll_name_2: {"resource_groups": [ct.default_resource_group_name], "replica_number": 2}
        }

        self._connect()
        # create RG_0, RG_1, transfer 1 node to RG_0, 1 node to RG_1

        for rg_info in resource_group_info:
            rg_name = rg_info["name"]
            if rg_name != ct.default_resource_group_name:
                _, create_rg_res = self.utility_wrap.create_resource_group(rg_name)
                assert create_rg_res
                log.info(f"[ResourceGroup] Create rg {rg_name} done")
                self.utility_wrap.transfer_node(source=ct.default_resource_group_name, target=rg_name,
                                                num_node=rg_info["available_node"])
                log.info(
                    f'[ResourceGroup] Transfer {rg_info["available_node"]} nodes from {ct.default_resource_group_name} to {rg_name} done')

        # verify RGs
        resource_groups, _ = self.utility_wrap.list_resource_groups()
        assert len(resource_groups) == len(resource_group_info)
        assert all([rg_info["name"] in resource_groups for rg_info in resource_group_info])
        for rg_info in resource_group_info:
            rg_info = {"name": rg_info["name"],
                       "capacity": rg_info["capacity"],
                       "num_available_node": rg_info["available_node"],
                       "num_loaded_replica": {},
                       "num_outgoing_node": {},
                       "num_incoming_node": {}
                       }
            desc_rg_info, _ = self.utility_wrap.describe_resource_group(name=rg_info["name"],
                                                                        check_task=ct.CheckTasks.check_rg_property,
                                                                        check_items=rg_info)
            log.info(f'[ResourceGroup] Rg of {rg_info["name"]} info is: {desc_rg_info}')

        # prepare collection C1, C2
        # create
        data = cf.gen_default_dataframe_data(nb=nb)
        index_params = {"index_type": "HNSW", "metric_type": "L2", "params": {"M": 48, "efConstruction": 500}}

        for coll_name in coll_name_1, coll_name_2:
            # create
            collection_w = self.init_collection_wrap(name=coll_name, active_trace=True)
            log.info(f"create collection {collection_w.name} done")
            entities = collection_w.num_entities

            # insert
            _, res = collection_w.insert(data)
            assert res
            log.info(f"insert {nb} entities done")

            # flush
            _, check_result = collection_w.flush(timeout=180)
            assert check_result
            assert collection_w.num_entities == nb + entities
            entities = collection_w.num_entities
            log.info(f"flush done with entities: {entities}")

            # index
            index, _ = collection_w.create_index(field_name=ct.default_float_vec_field_name,
                                                 index_params=index_params,
                                                 index_name=cf.gen_unique_str())
            index, _ = collection_w.create_index(field_name=ct.default_string_field_name,
                                                 index_params={},
                                                 index_name=cf.gen_unique_str())
            index_infos = [index.to_dict() for index in collection_w.indexes]
            log.info(f"index info: {index_infos}")

            # load coll_rg_a, 2 replicas -> RG_0, RG_1
            # load coll_rg_b, 2 replicas -> default_RG
            collection_w.load(replica_number=collection_rg_map[coll_name]["replica_number"],
                              _resource_groups=collection_rg_map[coll_name]["resource_groups"])

            # show query segment info
            segment_info, _ = self.utility_wrap.get_query_segment_info(collection_w.name)
            log.info(f"{collection_w.name} segment info: {segment_info}")

            # show replicas info
            replicas, _ = collection_w.get_replicas()
            log.info(f"{collection_w.name} replica info: {replicas}")

            # search
            search_vectors = cf.gen_vectors(ct.default_nq, ct.default_dim)
            search_params = {"metric_type": "L2", "params": {"ef": 64}}
            search_res, _ = collection_w.search(data=search_vectors,
                                                anns_field=ct.default_float_vec_field_name,
                                                param=search_params, limit=ct.default_limit, expr="int64 >= 0")
            assert len(search_res) == ct.default_nq
            assert len(search_res[0]) == ct.default_limit

            # query and delete
            term_expr = f'{ct.default_int64_field_name} < 100'
            query_res, _ = collection_w.query(term_expr)
            assert len(query_res) == 100

            delete_expr = f'{ct.default_int64_field_name} in {[i for i in range(100)]}'
            collection_w.delete(delete_expr)
            collection_w.query(term_expr, check_task=ct.CheckTasks.check_query_empty)

        # verify rg replica info
        for rg_info in resource_group_info:
            rg_info = {"name": rg_info["name"],
                       "capacity": rg_info["capacity"],
                       "num_available_node": rg_info["available_node"],
                       "num_loaded_replica": rg_info["loaded_replica"],
                       "num_outgoing_node": {},
                       "num_incoming_node": {}
                       }
            desc_rg_info_2, _ = self.utility_wrap.describe_resource_group(name=rg_info["name"],
                                                                          check_task=ct.CheckTasks.check_rg_property,
                                                                          check_items=rg_info)
            log.info(f'[ResourceGroup] Rg of {rg_info["name"]} info is: {desc_rg_info_2}')

    @pytest.mark.tags(CaseLabel.L3)
    def test_verify_milvus_resource_group(self):
        self._connect()

        # verify collection exist
        all_collections, _ = self.utility_wrap.list_collections()
        assert all(coll_name in all_collections for coll_name in [coll_name_1, coll_name_2])

        # verify resource groups
        for rg_info in resource_group_info:
            rg_info = {"name": rg_info["name"],
                       "capacity": rg_info["capacity"],
                       "num_available_node": rg_info["available_node"],
                       "num_loaded_replica": rg_info["loaded_replica"],
                       "num_outgoing_node": {},
                       "num_incoming_node": {}
                       }
            desc_rg_info, _ = self.utility_wrap.describe_resource_group(name=rg_info["name"],
                                                                        check_task=ct.CheckTasks.check_rg_property,
                                                                        check_items=rg_info)
            log.info(f'[ResourceGroup] Rg of {rg_info["name"]} info is: {desc_rg_info}')

        # search
        for coll_name in coll_name_2, coll_name_1:
            # get query segment info
            segment, _ = self.utility_wrap.get_query_segment_info(coll_name)
            log.info(f"{coll_name} query segment info: {segment}")

            # get replicas
            collection_w = self.init_collection_wrap(name=coll_name, active_trace=True)
            replicas, _ = collection_w.get_replicas(check_task=ct.CheckTasks.check_nothing)
            log.info(f"{coll_name} replicas: {replicas}")

            # search
            for i in range(100):
                search_vectors = cf.gen_vectors(ct.default_nq, ct.default_dim)
                search_params = {"metric_type": "L2", "params": {"ef": 64}}
                search_res, _ = collection_w.search(data=search_vectors,
                                                    anns_field=ct.default_float_vec_field_name,
                                                    param=search_params, limit=ct.default_limit, expr="int64 >= 0")
                assert len(search_res) == ct.default_nq
                assert len(search_res[0]) == ct.default_limit

            # show query segment info finally
            segment_2, _ = self.utility_wrap.get_query_segment_info(coll_name)
            log.info(f"{coll_name} query segment info: {segment_2}")

            # show replicas finally
            replicas_2, _ = collection_w.get_replicas()
            log.info(f"{coll_name} replicas: {replicas_2}")


from IPython import get_ipython

def type_of_script():
    """
    Detects and returns the type of python kernel
    :return: string 'jupyter' or 'ipython' or 'terminal'
    """
    try:
        ipy_str = str(type(get_ipython()))
        if 'zmqshell' in ipy_str:
            return 'jupyter'
        if 'terminal' in ipy_str:
            return 'ipython'
    except:
        return 'terminal'

if type_of_script() == 'jupyter':
    from tqdm.notebook import tqdm
else:
    from tqdm import tqdm

import matplotlib.pyplot as plt # type: module
import matplotlib.ticker as ticker
from matplotlib import colormaps
from matplotlib.colors import Normalize
import matplotlib.gridspec as gridspec

import numpy as np
import os, glob
import time
import warnings

from rur.fortranfile import FortranFile
from rur import uri, uhmi, painter, drawer
from rur.sci.photometry import measure_luminosity
from rur.sci.geometry import get_angles, euler_angle
from rur.utool import rotate_data
from scipy.ndimage import gaussian_filter
uri.timer.verbose=1
# from rur.sci.kinematics import f_getpot

from icl_IO import mode2repo, pklsave, pklload
from icl_tool import *
from icl_numba import large_isin, large_isind, isin
from icl_draw import drawsnap, add_scalebar, addtext, MakeSub_nolabel, label_to_in, fancy_axis, circle
import argparse, subprocess
from importlib import reload
import cmasher as cmr
from copy import deepcopy
from multiprocessing import Pool, shared_memory

mode = 'nh'
iout = 1026
repo, rurmode, dp = mode2repo(mode)
snap = uri.RamsesSnapshot(repo, iout, mode=rurmode)
snaps = uri.TimeSeries(snap)
snaps.read_iout_avail()
nout = snaps.iout_avail['iout']
gals = uhmi.HaloMaker.load(snap, galaxy=True, double_precision=dp)
hals = uhmi.HaloMaker.load(snap, galaxy=False, double_precision=dp)
database = f"/home/jeon/MissingSat/database"

from common_func import *

tree = pklload(f"{database}/02_main_progenitors.pickle")
if(os.path.exists(f"{database}/halo_dict.pickle")):
    halos = pklload(f"{database}/halo_dict.pickle")
else:
    halos = {'catalog':{}, 'index':{}}
    uri.timer.verbose=0
    for iout in tqdm(np.unique(tree['timestep'])):
        isnap = snaps.get_snap(iout)
        ihals = uhmi.HaloMaker.load(isnap, galaxy=False, double_precision=dp)
        indicies = np.zeros(len(ihals), dtype=int)
        iids = tree[tree['timestep'] == iout]['id']
        ihals = ihals[iids-1]
        indicies[iids-1] = np.arange(len(iids))
        halos['catalog'][iout] = ihals
        halos['index'][iout] = indicies
    pklsave(halos, f"{database}/halo_dict.pickle")

def _ibox(h, factor=1):
    return np.array([
                    [h['x']-factor*h['r'], h['x']+factor*h['r']],
                    [h['y']-factor*h['r'], h['y']+factor*h['r']],
                    [h['z']-factor*h['r'], h['z']+factor*h['r']]
                    ])

uri.timer.verbose=0
for iout in np.unique(tree['timestep'])[::-1]:
    if(os.path.exists(f"{database}/main_prog/cpulist/cpulist_{iout:05d}.pickle")): continue
    cpudict = {}
    targets = halos['catalog'][iout]
    isnap = snaps.get_snap(iout)

    cpulists = []
    with Pool(32) as pool:
        async_result = [
                    pool.apply_async(
                            uri.get_cpulist,
                            (_ibox(h,factor=1.1), None, isnap.levelmax, isnap.bound_key, isnap.ndim, 5, isnap.ncpu)
                            ) for h in targets
                    ]
        iterobj = tqdm(async_result, total=len(targets), desc=f"iout={iout:05d}")
        for r in iterobj:
            cpulists.append(r.get())
    cpulists = np.unique( np.concatenate(cpulists) )
    cpudict['all'] = cpulists
    pklsave(cpudict, f"{database}/main_prog/cpulist/cpulist_{iout:05d}.pickle")
    print(f"`{database}/main_prog/cpulist/cpulist_{iout:05d}.pickle` save done")
    isnap.clear()


from sklearn.model_selection import train_test_split
from sklearn import svm

def svm_classification(X, y, C_in, gamma_in, kernel_in):
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=y, random_state=42)

    classifierSVM = svm.SVC(C=C_in, degree=2, gamma=gamma_in, kernel=kernel_in)
    # training
    classifierSVM.fit(X_train, y_train)
    # prediksi data test

    y_pred_SVM = classifierSVM.predict(X_test)
    # return X_train, X_test, y_train, y_test, classifierSVM, y_pred_SVM
    return classifierSVM, y_pred_SVM, y_test


# Rotating or flipping an image
from PIL import Image

def main():
    image = Image.open('../lenna.png')
    image.show('Original')

    # Rotate 60 degrees counter clockwise
    rotated_image = image.rotate(60)
    rotated_image.show('Rotate 60')

    # Rotate using Image.transpose
    # Transpose supports these values:
    #  - Image.FLIP_LEFT_RIGHT
    #  - Image.FLIP_TOP_BOTTOM
    #  - Image.ROTATE_90
    #  - Image.ROTATE_180
    #  - Image.ROTATE_270
    rotated_image = image.transpose(Image.ROTATE_90)
    rotated_image.show('Rotate 90')

    # Flip horizontal
    flipped_image = image.transpose(Image.FLIP_LEFT_RIGHT)
    flipped_image.show('Flip horizontal')

    # Flip vertical
    flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
    flipped_image.show('Flip vertical')

if __name__ == '__main__':
    main()


from django.urls import path
from . import views as blog_views
# import users.views as user_views
from .views import (
    PostListView,
    PostDetailView,
    PostCreateView,
    PostUpdateView,
    PostDeleteView,
    UserPostListView
    )

urlpatterns = [
    path('', PostListView.as_view(), name='blog-home'),
    path('user/<str:username>', UserPostListView.as_view(), name='user-posts'),
    # using a variable in the route (for individual posts, which will be numbered)
    # the detail view is expecting the "pk" variable (we could change this in the class)
    path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
    path('post/<int:pk>/update', PostUpdateView.as_view(), name='post-update'),
    path('post/<int:pk>/delete', PostDeleteView.as_view(), name='post-delete'),
    path('post/new/', PostCreateView.as_view(), name='post-create'),
    path('about/', blog_views.about, name='blog-about'),
]


from __future__ import division

# Our Backend for the App!
# Built with Flask

# Import Flask
import flask
import requests
import os
from flask import send_file

import re
import sys
# Create the application
app = flask.Flask(__name__)

# serving home.html
@app.route('/', methods=['GET'])
def serve_page():
    return flask.render_template('home.html')

# process query
@app.route('/process_query', methods=['POST'])
def process_query():
    data = flask.request.form  # is a dictionary
    input = data['user_input']
    input_in_list = input.split(' ')
    return flask.render_template('home.html', same=processInput(input_in_list), og=input)

def processInput(input_in_list):
    for s, i in enumerate(input_in_list):
        if "bye" in i.lower():
            input_in_list[s] = "static/bye.jpg"
        if "hello" in i.lower():
            input_in_list[s] = "static/hello.png"
        if "yes" in i.lower():
            input_in_list[s] = "static/yes.png"
        if "no" in i.lower():
            input_in_list[s] = "static/no.png"
        if "please" in i.lower():
            input_in_list[s] = "static/please.png"
        if "thanks" in i.lower():
            input_in_list[s] = "static/thanks.png"
        if "who" in i.lower():
            input_in_list[s] = "static/who.png"
        if "what" in i.lower():
            input_in_list[s] = "static/what.png"
        if "when" in i.lower():
            input_in_list[s] = "static/when.png"
        if "where" in i.lower():
            input_in_list[s] = "static/where.png"
        if "why" in i.lower():
            input_in_list[s] = "static/why.png"
        if "which" in i.lower():
            input_in_list[s] = "static/which.png"
        if "how" in i.lower():
            input_in_list[s] = "static/how.png"
    return input_in_list
def listen_print_loop(responses):
    """Iterates through server responses and prints them.

    The responses passed is a generator that will block until a response
    is provided by the server.

    Each response may contain multiple results, and each result may contain
    multiple alternatives; for details, see https://goo.gl/tjCPAU.  Here we
    print only the transcription for the top alternative of the top result.

    In this case, responses are provided for interim results as well. If the
    response is an interim one, print a line feed at the end of it, to allow
    the next result to overwrite it, until the response is a final one. For the
    final one, print a newline to preserve the finalized transcription.
    """
    num_chars_printed = 0
    for response in responses:
        if not response.results:
            continue

        # The `results` list is consecutive. For streaming, we only care about
        # the first result being considered, since once it's `is_final`, it
        # moves on to considering the next utterance.
        result = response.results[0]
        if not result.alternatives:
            continue

        # Display the transcription of the top alternative.
        transcript = result.alternatives[0].transcript

        # Display interim results, but with a carriage return at the end of the
        # line, so subsequent lines will overwrite them.
        #
        # If the previous result was longer than this one, we need to print
        # some extra spaces to overwrite the previous result
        overwrite_chars = ' ' * (num_chars_printed - len(transcript))

        if not result.is_final:
            sys.stdout.write(transcript + overwrite_chars + '\r')
            sys.stdout.flush()

            num_chars_printed = len(transcript)

        else:
            return flask.render_template('home.html', same=processInput("".join(transcript).split(" ")), og="".join(transcript))

            # Exit recognition if any of the transcribed phrases could be
            # one of our keywords.
            if re.search(r'\b(exit|quit)\b', transcript, re.I):
                print('Exiting..')
                break

            num_chars_printed = 0
@app.route('/speech', methods=['GET'])
def main():
    # See http://g.co/cloud/speech/docs/languages
    # for a list of supported languages.
    language_code = 'en-US'  # a BCP-47 language tag

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


from langdetect import detect

def to_sentences(text):
    text = text.replace("\n", " ")
    sentences = [s + '.' for s in text.split(".") if s != ""]
    return sentences

def divide(text, input_size=5000):
    """
    Divide text into chunks of input_size

    Args:
        text (str): Text to be divided
        input_size (int): Size of each chunk
    """
    # short input_size if asian
    lang = detect(text)
    if lang in ['ko', 'ja', 'zh-cn', 'zh-tw', 'zh-hk']:
        input_size = min(input_size, 1300)

    # divide text by words
    text = to_sentences(text)
    result = []
    temp = ""
    for word in text:
        if len(temp + word) >= input_size:
            result.append(temp)
            temp = ""
        temp += word
    result.append(temp)
    return result


"""
include packages
"""

from settings import *
import sqlite3
import discord
from discord import app_commands
import sys
import signal
import deepl
from typing import Optional

from lib import vote as vt
from lib import deepl as dl

"""
Global variables
"""

connection : sqlite3.Connection       = sqlite3.connect(DATABASE)
intents    : discord.Intents          = discord.Intents.all()
client     : discord.Client           = discord.Client(intents=intents)
tree       : app_commands.CommandTree = app_commands.CommandTree(client=client)

"""
Setup
"""

vt.init(connection)
with open(DEEPL_API_KEY) as f:
    dl_translator = dl.LoggingTranslator(f.read(), connection=connection)

"""
Commands
"""

@tree.command(
    name='test',
    description='This is a test'
)
@app_commands.describe(
    message='Your message',
    hello='Hello message'
)
@app_commands.rename(
    message='text'
)
@app_commands.choices(
    hello=[
        app_commands.Choice(name='Good Morning', value='Good Morning'),
        app_commands.Choice(name='Good Afternoon', value='Good Afternoon'),
        app_commands.Choice(name='Good Evening', value='Good Evening'),
        app_commands.Choice(name='Good Night', value='Good Night')
    ]
)
@app_commands.guild_only
async def test(ctx: discord.Interaction, message: str, hello: str):
    await ctx.response.send_message('This is a test message.\nYour message is ...\n'+message+'\n'+hello)

@tree.command(
    name='vote',
    description='投票を行う'
)
@app_commands.describe(
    title='投票のお題',
    visible='投票結果を表示する際に投票先を表示する',
)
@app_commands.choices(
    visible=[
        app_commands.Choice(name='表示する', value='Yes'),
        app_commands.Choice(name='表示しない', value='No')
    ]
)
@app_commands.guild_only
async def vote_with_any_choices(ctx: discord.Interaction, title: str, visible: str='Yes'):
    try:
        await ctx.response.send_modal(vt.VoteModal(title=title, visible=visible))
    except Exception as e:
        print(e.with_traceback(sys.exc_info()[2]))

@tree.command(
    name='deepl',
    description='DeepL翻訳を使用してテキストを翻訳する（default: Auto→JP）'
)
@app_commands.describe(
    text='翻訳するテキスト',
    source_language='翻訳前の言語（default: 自動検出）',
    target_language='翻訳後の言語（default: 日本語）'
)
@app_commands.choices(
    source_language=dl.DcLanguageList.SOURCE,
    target_language=dl.DcLanguageList.TARGET
)
async def deepl_translate(ctx: discord.Interaction, text: str, source_language: Optional[str] = None, target_language: str = deepl.Language.JAPANESE):
    try:
        if source_language == "":
            source_language = None

        translated_text = dl_translator.translate_text(
            ctx=ctx,
            text=text,
            source_lang=source_language,
            target_lang=target_language
        )

        t = "> " + text.replace("\n", "\n> ") + "\n"

        await ctx.response.send_message(t + translated_text.text)

    except Exception as e:
        print(e.with_traceback(sys.exc_info()[2]))

"""
Events
"""

@client.event
async def on_ready():
    print('Bot is ready')
    await tree.sync()

"""
Cleanups
"""

def cleanup():
    global connection
    connection.close()

def signal_handler(signum, frame):
    cleanup()
    sys.exit(1)

if __name__ == '__main__':
    signal.signal(signal.SIGTERM, signal_handler)
    try:
        with open(TOKEN) as f:
            client.run(f.read())
    finally:
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        cleanup()


# coding: utf8

"""锁Lock 用于避免进程间对shared memory的争夺"""

import multiprocessing
import time

def job(val, num, lo):
    lo.acquire()   # 取得锁
    for _ in range(10):
        time.sleep(0.1)
        val.value += num
        print(val.value)
    lo.release()   # 释放锁

def multicore():
    lo = multiprocessing.Lock()   # 创建锁对象

    share_memory = multiprocessing.Value("i", 0)   # 初始化为0的一个共享int块变量

    res1 = multiprocessing.Process(target=job, args=(share_memory, 1, lo))
    res2 = multiprocessing.Process(target=job, args=(share_memory, 9, lo))
    res1.start()
    res2.start()
    res1.join()
    res2.join()

if __name__ == "__main__":
    multicore()


#!/usr/bin/env python3

#######################
#  ACE3 Setup Script  #
#######################

import os
import sys
import shutil
import platform
import subprocess
import winreg

######## GLOBALS #########
MAINDIR = "z"
PROJECTDIR = "ace"
CBA = "P:\\x\\cba"
##########################

def main():
    FULLDIR = "{}\\{}".format(MAINDIR, PROJECTDIR)
    print("""
  ######################################
  # ACE3 Development Environment Setup #
  ######################################

  This script will create your ACE3 dev environment for you.

  Before you run this, you should already have:
    - The Arma 3 Tools installed properly via Steam
    - A properly set up P-drive

  If you have not done those things yet, please abort this script in the next step and do so first.

  This script will create two hard links on your system, both pointing to your ACE3 project folder:
    [Arma 3 installation directory]\\{} => ACE3 project folder
    P:\\{}                              => ACE3 project folder

  It will also copy the required CBA includes to {}, if you do not have the CBA source code already.""".format(FULLDIR, FULLDIR, CBA))
    print("\n")

    try:
        reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
        key = winreg.OpenKey(reg,
                             r"SOFTWARE\Wow6432Node\bohemia interactive\arma 3")
        armapath = winreg.EnumValue(key, 1)[1]
    except:
        print("Failed to determine Arma 3 Path.")
        return 1

    if not os.path.exists("P:\\"):
        print("No P-drive detected.")
        return 2

    scriptpath = os.path.realpath(__file__)
    projectpath = os.path.dirname(os.path.dirname(scriptpath))

    print("# Detected Paths:")
    print("  Arma Path:    {}".format(armapath))
    print("  Project Path: {}".format(projectpath))

    repl = input("\nAre these correct? (y/n): ")
    if repl.lower() != "y":
        return 3

    hemmt_path = os.path.join(projectpath, ".hemttout", "dev")
    print("\n# Use Hemmt Dev Path for arma filepatching:")
    print(f"  y: {hemmt_path}")
    print(f"  n: {projectpath}")
    repl = input("(y/n): ")
    filepatching_path = projectpath
    if repl.lower() == "y":
        if not os.path.exists(hemmt_path):
            print(f"creating {hemmt_path}")
            os.makedirs(hemmt_path)
        filepatching_path = hemmt_path

    if os.path.exists("P:\\{}\\{}".format(MAINDIR, PROJECTDIR)):
        print("Link on P: already exists. Please finish the setup manually.")
        return 4

    if os.path.exists(os.path.join(armapath, MAINDIR, PROJECTDIR)):
        print("Link in Arma directory already exists. Please finish the setup manually.")
        return 5
    print("\n# Creating links ...")

    try:
        if not os.path.exists("P:\\{}".format(MAINDIR)):
            os.mkdir("P:\\{}".format(MAINDIR))
        if not os.path.exists(os.path.join(armapath, MAINDIR)):
            os.mkdir(os.path.join(armapath, MAINDIR))

        subprocess.call(["cmd", "/c", "mklink", "/J", "P:\\{}\\{}".format(MAINDIR, PROJECTDIR), projectpath])
        subprocess.call(["cmd", "/c", "mklink", "/J", os.path.join(armapath, MAINDIR, PROJECTDIR), filepatching_path])
    except:
        raise
        print("Something went wrong during the link creation. Please finish the setup manually.")
        return 6

    print("# Links created successfully.")

    print("\n# Copying required CBA includes ...")

    if os.path.exists(CBA):
        print("{} already exists, skipping.".format(CBA))
        return -1

    try:
        shutil.copytree(os.path.join(projectpath, "include", "x", "cba"), CBA)
    except:
        raise
        print("Something went wrong while copying CBA includes. Please copy include\\x\\cba to {} manually.".format(CBA))
        return 7

    print("# CBA includes copied successfully to {}.".format(CBA))

    return 0

if __name__ == "__main__":
    exitcode = main()

    if exitcode > 0:
        print("\nSomething went wrong during the setup. Make sure you run this script as administrator. If these issues persist, please follow the instructions on the ACE3 wiki to perform the setup manually.")
    else:
        print("\nSetup successfully completed.")

    input("\nPress enter to exit ...")
    sys.exit(exitcode)


from RepSys import Error, config
from RepSys.util import execcmd
from RepSys.VCS import *
from os.path import basename, dirname
from os import chdir, getcwd
import sys
import re
import time
from xml.etree import cElementTree as ElementTree
import subprocess

class GITLogEntry(VCSLogEntry):
    def __init__(self, revision, author, date):
        VCSLogEntry.__init__(self, revision, author, data)

class GIT(VCS):
    def __init__(self):
        VCS.__init__(self)
        self.vcs_name = "git"
        self.vcs_command = config.get("global", "git-command", "git")
        self.vcs_supports['clone'] = True
        self.env_defaults = {"GIT_SSH": self.vcs_wrapper}

    def clone(self, url, targetpath, **kwargs):
        if url.split(':')[0].find("svn") < 0:
            return VCS.clone(self, url, targetpath, **kwargs)
        else:
            # To speed things up on huge repositories, we'll just grab all the
            # revision numbers for this specific directory and grab these only
            # in stead of having to go through each and every revision...
            retval, result = execcmd("svn log --stop-on-copy --xml %s" % url)
            if retval:
                return retval
            parser = ElementTree.XMLTreeBuilder()
            result = "".join(result.split("\n"))
            parser.feed(result)
            log = parser.close()
            logentries = log.getiterator("logentry")
            revisions = []
            topurl = dirname(url)
            trunk = basename(url)
            tags = "releases"
            execcmd("git svn init %s --trunk=%s --tags=%s %s" % (topurl, trunk, tags, targetpath), show=True)
            chdir(targetpath)
            for entry in logentries:
                revisions.append(entry.attrib["revision"])
            while revisions:
                execcmd("git svn fetch -r%d" % int(revisions.pop()), show=True)

            cmd = ["svn", "rebase"]
            return self._execVcs_success(*cmd, **kwargs)

class SVNLook(VCSLook):
    def __init__(self, repospath, txn=None, rev=None):
        VCSLook.__init__(self, repospath, txn, rev)

# vim:et:ts=4:sw=4


import gzip
import inspect
import os
from io import StringIO
from typing import Optional, List

import numpy as np
import pandas as pd

def time_map(time_a: float, time_b: float, packet_a: int, packet_b: int, time_c: int, window_tolerance: int = 0) -> \
		Optional[float]:
	"""
	Map an API time into a packet number.
	This function was done in order to have a nice visualisation.
	The window tolereance is used to capture nearby calls
			=========     |
			ta      tb    tc=>pc
			pa      pb

	Args:
		time_a (float): time of the begining of the flow (in seconds since epoch, time.time())
		time_b (float): time of the ending of the flow
		packet_a (int): packet number of the begining of the flow
		packet_b (int): packet number of the ending of the flow
		time_c (float): time of the api call
		window_tolerance (int): time shift in second in which api calls are still considered to belong to the flow

	Returns:
		int: packet number, return None if the mapping fails
	"""

	window_tolerance *= 1000000
	# Check if inside flow
	if time_a <= time_c <= time_b:  # Chain comparaison
		return packet_a + (time_c - time_a) * (packet_b - packet_a) / (time_b - time_a)
	# Check if in the window border (simple)
	if time_a - window_tolerance <= time_c <= time_b:
		return packet_a
	"""if time_a <= time_c <= time_b + window_tolerance:
		return packet_b"""
	# Outside
	return None

def pure_time_map(time_a: float, time_b: float, time_c: float, window_tolerance: int = 0) -> Optional[float]:
	"""
	Map an API time into a packet number.
	This function was done in order to have a nice visualisation.
	The window tolereance is used to capture nearby calls
			=========     |
			ta      tb    tc=>pc

	Args:
		time_a (float): time of the begining of the flow (in seconds since epoch, time.time())
		time_b (float): time of the ending of the flow
		time_c (float): time of the api call
		window_tolerance (int): time shift in second in which api calls are still considered to belong to the flow

	Returns:
		int: time, return None if the mapping fails
	"""

	window_tolerance *= 1000000
	# Check if inside flow
	if time_a <= time_c <= time_b:  # Chain comparaison
		return time_c
	# Check if in the window border (simple)
	if time_a - window_tolerance <= time_c <= time_b:
		return time_a
	"""if time_a <= time_c <= time_b + window_tolerance:
		return time_b"""
	# Outside
	return None

def get_child_pids(current_pid: int, diff: pd.DataFrame) -> List:
	"""
	Get the child process pid of one process given its pid

	Args:
		current_pid (int): parent pid
		diff (pd dataframe): df recorded

	Returns:
		list: list of pids
	"""

	childs = diff[diff["parent_pid"] == current_pid]
	if childs.shape[0] == 0:
		return [current_pid]
	return [current_pid] + [v for index, row in childs.iterrows() for v in get_child_pids(row["process_id"], diff)]

def get_malware_pids(malware_name: str = "2020-09-30-Trickbot-EXE.exe", path: str = "./") -> List:
	"""
	Get the pids of all the malware generated processes

	Args:
		malware_name (str, optional): name of the malware. Defaults to "2020-09-08-Trickbot-EXE-gtag-ono72.exe".

	Returns:
		list: list of pids
		path: path of the malware
	"""

	first = pd.read_csv(path + "process_pre.csv")
	post = pd.read_csv(path + "process_post.csv")

	first.drop(first.columns[0], axis=1, inplace=True)
	post.drop(post.columns[0], axis=1, inplace=True)

	diff = first.merge(post, indicator=True,
	                   how='right').loc[lambda x: x['_merge'] != 'both']
	try:
		malware_pid = int(diff.loc[diff['process_name'] == malware_name]["process_id"].astype(float))
	except TypeError:
		raise RuntimeError('Malware PID not found, check malware name')
	return get_child_pids(malware_pid, diff)

def gzip_to_string(file_path: str) -> str:
	"""
	Open a gzip file and load the content in a string
	This function exists because the gzip may not be proprerly closed.
	In this case, the end is corrupted but the rest can be read.
	Args:
		file_path (string): path of the gzip file

	Returns:
		str: content of the gzip file
	"""

	gzip_file = gzip.open(file_path, "rt")
	string = ""
	while True:
		try:
			line = gzip_file.readline()
			string += line
		except EOFError:
			break
	return string

def get_malware_traces(path: str = "./") -> List:
	"""
	Get a list of dataframe representing the frida trace
	Its current format is [time, api_name, category]

	Returns:
		list: list of dataframe
	"""

	pids = list(set(get_malware_pids(path=path)))
	traces = []
	for pid in pids:
		if not os.path.isfile(f"{path}frida_{pid}.txt.gz"):
			print("Trace for {pid} does not exist")
		header = ["time", "api", "category"]
		frida_str = StringIO(gzip_to_string(f"{path}frida_{pid}.txt.gz"))
		dataframe = pd.read_csv(frida_str, names=header)
		dataframe.drop(
			dataframe.loc[dataframe['api'] == 'error'].index, inplace=True)
		dataframe.drop_duplicates(
			subset=['time', 'api'], keep='first', inplace=True)
		dataframe.reset_index(drop=True, inplace=True)
		traces.append(dataframe)
		dataframe["time_int"] = (dataframe["time"] * 1000000).astype(int)
	return traces

class Singleton(type):
	# Singleton modified to handle arguments (singleton for each argument set)
	_instances = {}
	_init = {}

	def __init__(cls, name, bases, dct):
		cls._init[cls] = dct.get('__init__', None)

	def __call__(cls, *args, **kwargs):
		init = cls._init[cls]
		if init is not None:
			key = (cls, frozenset(inspect.getcallargs(init, None, *args, **kwargs).items()))
		else:
			key = cls

		if key not in cls._instances:
			cls._instances[key] = super(Singleton, cls).__call__(*args, **kwargs)
		return cls._instances[key]

class MalwareTraceExtractor(metaclass=Singleton):
	def __init__(self, malware_name: str, path: str):
		self.malware_name = malware_name
		self.path = path
		self.trace_array = None
		self._get_trace()

	def _get_trace(self) -> None:
		pids = list(set(get_malware_pids(self.malware_name, path=self.path)))
		return_array = np.empty((0, 4))
		for pid in pids:
			if not os.path.isfile(f"{self.path}frida_{pid}.txt.gz"):
				# print(f"Trace for {pid} does not exist")
				continue
			header = ["time", "api", "category"]
			frida_str = StringIO(gzip_to_string(f"{self.path}frida_{pid}.txt.gz"))
			dataframe = pd.read_csv(frida_str, names=header)
			dataframe.drop(
				dataframe.loc[dataframe['api'] == 'error'].index, inplace=True)
			dataframe.drop_duplicates(
				subset=['time', 'api'], keep='first', inplace=True)
			dataframe.reset_index(drop=True, inplace=True)
			dataframe["time_int"] = (dataframe["time"] * 1000000).astype(np.int64)
			# To numpy
			np_array_df = dataframe.to_numpy()
			# Stack to the final array
			return_array = np.vstack((return_array, np_array_df))
		self.trace_array = return_array
		print(len(self.trace_array))

	def get_merge_trace(self) -> np.ndarray:
		return self.trace_array

def get_segmented_flow_syscalls(segmented_flow: np.ndarray, malware_process_name: str, path: str = "./",
                                time_delay_allowed: int = 0) -> np.ndarray:
	"""
	Get a list of API calls corresponding to the segmented flow
	:param segmented_flow:
	:param malware_process_name:
	:param path:
	:param time_delay_allowed:
	:return:
	"""
	min_time, max_time = segmented_flow[0][2]
	for group in segmented_flow[1:]:
		# ['HANDSHAKE', [0, 2], [1612708961378936, 1612708961422139]]
		timea, timeb = group[2]
		min_time = min(timea, min_time)
		max_time = max(timeb, max_time)
	# print(min_time, max_time, max_time - min_time)
	trace_extractor = MalwareTraceExtractor(malware_name=malware_process_name, path=path)
	calls = trace_extractor.get_merge_trace()
	# calls = get_malware_traces_merged(malware_process_name, path=path)
	returned_calls = np.empty((0, 4))
	for call in calls:
		mapping = pure_time_map(min_time, max_time, call[3], time_delay_allowed)
		if mapping is not None:
			returned_calls = np.vstack((returned_calls, call))
	return returned_calls

if __name__ == "__main__":
	PATH = "trickbot1_1/"


# dicesimulation.py

# The following code computes the exact probability distribution
# for the sum of two dice:
# probabilities = stdarray.create1D(13, 0.0)
# for i in range(1, 7):
#     for j in range(1, 7):
#         probabilities[i+j] += 1.0
# for k in range(2, 13):
#     probabilities[k] /= 36.0

# After this code completes, probabilities[k] is the probability
# that the dice sum to k. Run experiments to validate this calculation
# simulating n dice throws, keeping track of the frequencies of
# occurrence of each value when you compute the sum of two random
# integers between 1 and 6. How large does n have to be before your
# empirical results match the exact results to three decimal places?

# ans: n must be around 10_000_000  {python3 dicesimulation.py 10000000}

import sys
import random
import stdio
import stdarray

n = int(sys.argv[1])

theoretical_probabilities = stdarray.create1D(13, 0.0)
for i in range(1, 7):
    for j in range(1, 7):
        theoretical_probabilities[i+j] += 1.0
for k in range(2, 13):
    theoretical_probabilities[k] /= 36.0

# simulate dice throw
dice_sum = stdarray.create1D(13, 0.0)
for i in range(0, n):
    first_die = random.randint(1, 6)
    second_die = random.randint(1, 6)
    dice_sum[first_die + second_die] += 1

empirical_probabilities = stdarray.create1D(13, 0.0)
for i in range(0, len(empirical_probabilities)):
    empirical_probabilities[i] = dice_sum[i] / sum(dice_sum)

diff = stdarray.create1D(13, 0.0)
stdio.writeln('sum  theo  empir  diff')
for i in range(2, 13):
    theoretical_probabilities[i] = round(theoretical_probabilities[i], 3)
    empirical_probabilities[i] = round(empirical_probabilities[i], 3)
    diff[i] = round(theoretical_probabilities[i] - empirical_probabilities[i], 3)
    if len(str(i)) < 2:
        stdio.write(' ')
    stdio.writeln(str(i) + '  ' + str(theoretical_probabilities[i]) + '  ' + str(empirical_probabilities[i]) + '  ' + str(diff[i]))


import matplotlib.pyplot as plt
import numpy as np

# ~~~ DEFINE DATA ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
np.random.seed(1337)

n = 1000000
x = np.random.standard_normal(n)
y = x + .5 * np.random.standard_normal(n)
hist, xedges, yedges = np.histogram2d(x, y, bins=100, density=True)
hist[hist == 0] = None

t = np.linspace(0, 3 * np.pi, 1000)
style = 'mpl'

# ~~~ PLOT LINEAR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fig, ax = plt.subplots()
plt.plot(t, np.sin(t), t, np.cos(t), t, 2 * np.cos(t))
plt.tight_layout()
plt.savefig(f'gallery/{style}_plot.png')
plt.close()

# legend
fig, ax = plt.subplots()
plt.plot(t, np.sin(t), label='sin')
plt.plot(t, np.cos(t), label='cos')
plt.plot(t, 2 * np.cos(t), label='2cos')
plt.legend(title='function:')
plt.tight_layout()
plt.savefig(f'gallery/{style}_plot_legend.png')
plt.close()

# mulitple subgallery
fig, axs = plt.subplots(3, 1, sharex=True, gridspec_kw={'hspace': 0.000})
axs[0].plot(t, np.sin(t))
axs[1].plot(t[::20], np.cos(t[::20]), 'o-')
axs[2].plot(t, 2 * np.cos(t), t, np.sin(t))
plt.tight_layout()
plt.savefig(f'gallery/{style}_plot_multiple.png')
plt.close()

# ~~~ PLOT IMSHOW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fig, ax = plt.subplots()
plt.imshow(hist)
plt.tight_layout()
plt.savefig(f'gallery/{style}_imshow.png')
plt.close()

# cbar
fig, ax = plt.subplots()
im = plt.imshow(hist)
plt.colorbar(im)
plt.tight_layout()
plt.savefig(f'gallery/{style}_imshow_cbar.png')
plt.close()


from Film import Film
from Forgalmazo import Forgalmazo
import datetime

def fajl_beolvas():
    filmek = []
    fp = open('nyitohetvege.txt', 'r', encoding='utf-8')
    lines = fp.readlines()
    fp.close()
    for line in lines[1:]:
        n_line = line.rstrip()
        (eredetiCim, magyarCim,bemutato,forgalmazo,
            bevel,latogato) = n_line.split(';')
        film = Film(eredetiCim, magyarCim,bemutato,forgalmazo,
            bevel,latogato)
        filmek.append(film)
    return filmek

def feladat3(filmek):
    print('3. feladat: Filmek száma az állományban: ', end='')
    filmek_szama = len(filmek)
    print(filmek_szama, 'db')

def feladat4(filmek):
    print('4. feladat: UIP Duna Film forgalmazó 1. ', end='')
    print('hetes bevételeinek összege: ', end='')
    osszeg=0
    for film in filmek:
        if film.forgalmazo == 'UIP':
            osszeg+=int(film.bevel)
    print("{:,}".format(osszeg), 'Ft')

def feladat5(filmek):
    print('5. feladat: Legtöbb látogató az első héten:')
    max_film = filmek[0]
    for film in filmek:
        if int(film.latogato) > int(max_film.latogato):
            max_film = film

    print('\tEredeti cím:', max_film.eredetiCim)
    print('\tMagyar cím:', max_film.magyarCim)
    print('\tForgalmazó:', max_film.forgalmazo)
    print('\tBevétel az első héten:', max_film.bevel, 'Ft')
    print('\tLátogatók száma:', max_film.latogato, 'fő')

def tartalmazTeszt(eredetiCim, magyarCim):
    eredetiTartalmaz=False
    if 'W' in eredetiCim:
        eredetiTartalmaz=True
    if 'w' in eredetiCim:
        eredetiTartalmaz=True
    magyarTartalmazza=False
    if 'W' in magyarCim:
        magyarTartalmazza=True
    if 'w' in magyarCim:
        magyarTartalmazza=True

    if eredetiTartalmaz and magyarTartalmazza:
        return True
    else:
        return False

def feladat6(filmek):
    print('6. feladat: ', end='')

    n=len(filmek)
    i=0
    while (i<n and
        not tartalmazTeszt(filmek[i].eredetiCim, filmek[i].magyarCim)):
        i+=1
    if i<n:
        print("Ilyen film volt!")
    else:
        print("Ilyen film nem volt!")

def forgalmazoTeszt(forgalmazok, forgalmazo):
    n=len(forgalmazok)
    i=0
    while i<n and forgalmazok[i].nev != forgalmazo:
        i+=1
    if i<n:
        return True
    else:
        return False

def feladat7(filmek):
    mezonevek = 'forgalmazo;filmekSzama\n'
    forgalmazok = []
    for film in filmek:
        if not forgalmazoTeszt(forgalmazok, film.forgalmazo):
            forgalmazo = Forgalmazo(film.forgalmazo)
            forgalmazok.append(forgalmazo)
        else:
            n=len(forgalmazok)
            for i in range(0, n):
                if forgalmazok[i].nev == film.forgalmazo:
                    forgalmazok[i].filmek += 1
    fp = open('stat.csv', 'w', encoding='utf-8')
    fp.write(mezonevek)
    for forgalmazo in forgalmazok:
        if forgalmazo.filmek>1:
            fp.write(forgalmazo.nev + ';' + str(forgalmazo.filmek) + '\n')
    fp.close()

def feladat8(filmek):
    print('8. feladat: A leghosszabb időszak két ', end='')
    print('InterCom-os bemutató között: ', end='')

    elsoBemutato = None
    max_kul = 0
    for film in filmek:
        if film.forgalmazo == 'InterCom':
            isoDatum = film.bemutato.replace('.', '-')
            if elsoBemutato == None:
                elsoBemutato=datetime.date.fromisoformat(isoDatum)
            else:
                kovBemutato = datetime.date.fromisoformat(isoDatum)
                kul = kovBemutato - elsoBemutato
                if kul.total_seconds() > max_kul:
                    max_kul=kul.total_seconds()
                elsoBemutato = kovBemutato
    nap = max_kul  // (24 * 3600)
    print(int(nap), 'nap')

filmek = fajl_beolvas()
# ~ feladat3(filmek)
# ~ feladat4(filmek)
# ~ feladat5(filmek)
# ~ feladat6(filmek)
# ~ feladat7(filmek)
feladat8(filmek)


import numpy as np

class TrinomialModel(object):  # Here we start defining our 'class' --> Trinomial Model!
    # First, a method to initialize our `TrinomialModel` algorithm!
    def __init__(self, S0, r, sigma, mat):
        self.__s0 = S0
        self.__r = r
        self.__sigma = sigma
        self.__T = mat

    # Second, we build a method (function) to compute the risk-neutral probabilities!
    def __compute_probs(self):
        self.__pu = (
            (
                np.exp(self.__r * self.__h / 2)
                - np.exp(-self.__sigma * np.sqrt(self.__h / 2))
            )
            / (
                np.exp(self.__sigma * np.sqrt(self.__h / 2))
                - np.exp(-self.__sigma * np.sqrt(self.__h / 2))
            )
        ) ** 2
        self.__pd = (
            (
                -np.exp(self.__r * self.__h / 2)
                + np.exp(self.__sigma * np.sqrt(self.__h / 2))
            )
            / (
                np.exp(self.__sigma * np.sqrt(self.__h / 2))
                - np.exp(-self.__sigma * np.sqrt(self.__h / 2))
            )
        ) ** 2
        self.__pm = 1 - self.__pu - self.__pd

        assert 0 <= self.__pu <= 1.0, "p_u should lie in [0, 1] given %s" % self.__pu
        assert 0 <= self.__pd <= 1.0, "p_d should lie in [0, 1] given %s" % self.__pd
        assert 0 <= self.__pm <= 1.0, "p_m should lie in [0, 1] given %s" % self.__pm

    # Third, this method checks whether the given parameters are alright and that we have a 'recombining tree'!
    def __check_up_value(self, up):
        if up is None:
            up = np.exp(self.__sigma * np.sqrt(2 * self.__h))

        assert up > 0.0, "up should be non negative"

        down = 1 / up

        assert down < up, "up <= 1. / up = down"

        self.__up = up
        self.__down = down

    # Four, we use this method to compute underlying stock price path
    def __gen_stock_vec(self, nb):
        vec_u = self.__up * np.ones(nb)
        np.cumprod(vec_u, out=vec_u)

        vec_d = self.__down * np.ones(nb)
        np.cumprod(vec_d, out=vec_d)

        res = np.concatenate((vec_d[::-1], [1.0], vec_u))
        res *= self.__s0

        return res

    # Fifth, we declare a Payoff method to be completed afterwards depending on the instrument we are pricing!
    def payoff(self, stock_vec):
        raise NotImplementedError()

    # Sixth, compute current prices!
    def compute_current_price(self, crt_vec_stock, nxt_vec_prices):
        expectation = np.zeros(crt_vec_stock.size)
        for i in range(expectation.size):
            tmp = nxt_vec_prices[i] * self.__pd
            tmp += nxt_vec_prices[i + 1] * self.__pm
            tmp += nxt_vec_prices[i + 2] * self.__pu

            expectation[i] = tmp

        return self.__discount * expectation

    # Seventh, Option pricing!
    def price(self, nb_steps, up=None):
        assert nb_steps > 0, "nb_steps shoud be > 0"

        nb_steps = int(nb_steps)

        self.__h = self.__T / nb_steps
        self.__check_up_value(up)
        self.__compute_probs()

        self.__discount = np.exp(-self.__r * self.__h)

        final_vec_stock = self.__gen_stock_vec(nb_steps)
        final_payoff = self.payoff(final_vec_stock)
        nxt_vec_prices = final_payoff

        for i in range(1, nb_steps + 1):
            vec_stock = self.__gen_stock_vec(nb_steps - i)
            nxt_vec_prices = self.compute_current_price(vec_stock, nxt_vec_prices)

        return nxt_vec_prices[0]

class TrinomialCall(TrinomialModel):
    def __init__(self, S0, r, sigma, mat, K):
        super(TrinomialCall, self).__init__(S0, r, sigma, mat)
        self.__K = K

    def payoff(self, s):
        return np.maximum(s - self.__K, 0.0)

class TrinomialAmerican_C(TrinomialCall):
    def __init__(self, S0, r, sigma, mat, K):
        super(TrinomialAmerican_C, self).__init__(S0, r, sigma, mat, K)

    def compute_current_price(self, crt_vec_stock, nxt_vec_prices):
        expectation = TrinomialCall.compute_current_price(
            self, crt_vec_stock, nxt_vec_prices
        )
        return np.maximum(expectation, TrinomialCall.payoff(self, crt_vec_stock))

class TrinomialPut(TrinomialModel):
    def __init__(self, S0, r, sigma, mat, K):
        super(TrinomialPut, self).__init__(S0, r, sigma, mat)
        self.__K = K

    def payoff(self, s):
        return np.maximum(self.__K - s, 0.0)

class TrinomialAmerican_P(TrinomialPut):
    def __init__(self, S0, r, sigma, mat, K):
        super(TrinomialAmerican_P, self).__init__(S0, r, sigma, mat, K)

    def compute_current_price(self, crt_vec_stock, nxt_vec_prices):
        expectation = TrinomialPut.compute_current_price(
            self, crt_vec_stock, nxt_vec_prices
        )
        return np.maximum(expectation, TrinomialPut.payoff(self, crt_vec_stock))