from dataclasses import dataclass
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ib_tws_server.ib_imports import *
import inspect
import logging
from typing import Callable, Dict, FrozenSet, List, MutableSet, Set, Type

logger = logging.getLogger(__name__)

class ApiDefinition:
    request_method: Callable = None
    cancel_method: Callable = None
    callback_methods: List[Callable] = None
    done_method: Callable = None
    uses_req_id: bool = False
    has_done_flag: bool = False
    is_subscription: bool = False
    subscription_flag_name: str = None
    subscription_flag_value: bool = True

    def __init__(self, request_method: Callable,
        cancel_method: Callable = None,
        callback_methods: List[Callable] = None,
        done_method: Callable = None,
        uses_req_id: bool = False,
        has_done_flag: bool = False,
        is_subscription: bool = False,
        subscription_flag_name: str = None,
        subscription_flag_value: bool = True):
        self.request_method = request_method
        self.cancel_method = cancel_method
        self.callback_methods = callback_methods
        self.done_method = done_method
        self.uses_req_id = uses_req_id
        self.has_done_flag = has_done_flag
        self.is_subscription = is_subscription
        self.subscription_flag_name = subscription_flag_name
        self.subscription_flag_value = subscription_flag_value

        if self.is_subscription or self.subscription_flag_name is not None:
            if not self.callback_methods or len(self.callback_methods) == 0:
                raise RuntimeError(f"Subscriptions should always have one or more callbacks {request_method.__name__}")

        if self.subscription_flag_name is not None:
            self.is_subscription = False

    @staticmethod
    def verify():
        ignore_methods = IGNORE_METHODS.copy()

        def log_unprocessed_methods():
            predicate = lambda x: inspect.isfunction(x) and not inspect.isbuiltin(x) and not inspect.ismethoddescriptor(x) and not x.__name__.startswith("__")
            for name,val in inspect.getmembers(EClient, predicate):
                if not val in ignore_methods:
                    logger.error(f"Unhandled request: {name}")
            for name,type in inspect.getmembers(EWrapper, predicate):
                if not val in ignore_methods:
                    logger.error(f"Unhandled response: {name}")

        def flag_request_definition(definition: ApiDefinition):
            if (definition.request_method is not None):
                ignore_methods.add(definition.request_method)
            if (definition.cancel_method is not None):
                ignore_methods.add(definition.cancel_method)
            if (definition.callback_methods is not None):
                for m in definition.callback_methods:
                    ignore_methods.add(m)
            if (definition.done_method is not None):
                ignore_methods.add(definition.done_method)

        for d in REQUEST_DEFINITIONS: flag_request_definition(d)
        log_unprocessed_methods()

class TypeDefinition:
    container_type: type
    item_type: str

    def __init__(self, container_type: type, item_type: str):
        self.container_type = container_type
        self.item_type = item_type

IGNORE_METHODS: FrozenSet[Callable] = set({
    EClient.connect,
    EWrapper.connectAck,
    EWrapper.connectionClosed,
    EClient.disconnect,
    EWrapper.error,
    EClient.isConnected,
    EClient.keyboardInterrupt,
    EClient.keyboardInterruptHard,
    EWrapper.logAnswer,
    EClient.logRequest,
    # Enable when using 9.85.x
    #EClient.msgLoopRec,
    #EClient.msgLoopTmo,
    EClient.reset,
    EClient.run,
    EClient.sendMsg,
    EClient.serverVersion,
    EClient.setConnState,
    # Enable when using 9.85.x
    #EClient.setConnectionOptions,
    EClient.startApi,
    EClient.twsConnectionTime,
    EWrapper.verifyAndAuthCompleted,
    EClient.verifyAndAuthMessage,
    EWrapper.verifyAndAuthMessageAPI,
    EClient.verifyAndAuthRequest,
    EWrapper.verifyCompleted,
    EClient.verifyMessage,
    EWrapper.verifyMessageAPI,
    EClient.verifyRequest,
    EWrapper.winError
})

# Mappings of requests with their callbacks.
REQUEST_DEFINITIONS: List[ApiDefinition] = [
    ApiDefinition(request_method=EClient.queryDisplayGroups,
        callback_methods=[EWrapper.displayGroupList],
        uses_req_id=True),
    ApiDefinition(request_method=EClient.exerciseOptions, uses_req_id=True),
    ApiDefinition(request_method=EClient.calculateImpliedVolatility,
        callback_methods=[EWrapper.tickOptionComputation],
        cancel_method=EClient.cancelCalculateImpliedVolatility,
        is_subscription = True,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.calculateOptionPrice,
        callback_methods=[EWrapper.tickOptionComputation],
        cancel_method=EClient.cancelCalculateOptionPrice,
        is_subscription = True,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqAccountSummary,
        callback_methods=[EWrapper.accountSummary],
        cancel_method=EClient.cancelAccountSummary,
        done_method=EWrapper.accountSummaryEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqAccountUpdatesMulti,
        callback_methods=[EWrapper.accountUpdateMulti],
        cancel_method=EClient.cancelAccountUpdatesMulti,
        done_method=EWrapper.accountUpdateMultiEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqFundamentalData,
        callback_methods=[EWrapper.fundamentalData],
        cancel_method=EClient.cancelFundamentalData,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqHeadTimeStamp,
        callback_methods=[EWrapper.headTimestamp],
        cancel_method=EClient.cancelHeadTimeStamp,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqHistogramData,
        callback_methods=[EWrapper.histogramData],
        cancel_method=EClient.cancelHistogramData,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqHistoricalData,
        callback_methods=[EWrapper.historicalData, EWrapper.historicalDataUpdate],
        cancel_method=EClient.cancelHistoricalData,
        done_method=EWrapper.historicalDataEnd,
        subscription_flag_name = 'keepUpToDate',
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqMktData,
        callback_methods=[EWrapper.tickPrice, EWrapper.tickSize,
            EWrapper.tickEFP,
            EWrapper.tickNews, EWrapper.rerouteMktDataReq,
            EWrapper.rerouteMktDepthReq, EWrapper.tickReqParams,
            EWrapper.deltaNeutralValidation],
        cancel_method=EClient.cancelMktData,
        done_method=EWrapper.tickSnapshotEnd,
        subscription_flag_name = 'snapshot',
        subscription_flag_value = False,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqMktDepth,
        callback_methods=[EWrapper.updateMktDepth, EWrapper.updateMktDepthL2],
        cancel_method=EClient.cancelMktDepth,
        is_subscription = True,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqNewsBulletins,
        callback_methods=[EWrapper.updateNewsBulletin],
        cancel_method=EClient.cancelNewsBulletins,
        is_subscription = True),
    ApiDefinition(request_method=EClient.placeOrder,
        callback_methods=[EWrapper.orderStatus],
        is_subscription = True,
        cancel_method=EClient.cancelOrder),
    ApiDefinition(request_method=EClient.reqPnL,
        callback_methods=[EWrapper.pnl],
        cancel_method=EClient.cancelPnL,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqPnLSingle,
        callback_methods=[EWrapper.pnlSingle],
        cancel_method=EClient.cancelPnLSingle,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqPositions,
        callback_methods=[EWrapper.position],
        done_method=EWrapper.positionEnd,
        cancel_method=EClient.cancelPositions),
    ApiDefinition(request_method=EClient.reqPositionsMulti,
        callback_methods=[EWrapper.positionMulti],
        done_method=EWrapper.positionMultiEnd,
        cancel_method=EClient.cancelPositionsMulti,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqRealTimeBars,
        callback_methods=[EWrapper.realtimeBar],
        cancel_method=EClient.cancelRealTimeBars,
        is_subscription = True,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqScannerSubscription,
        callback_methods=[EWrapper.scannerData],
        done_method=EWrapper.scannerDataEnd,
        cancel_method=EClient.cancelScannerSubscription,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqTickByTickData,
        callback_methods=[EWrapper.tickByTickAllLast, EWrapper.tickByTickBidAsk, EWrapper.tickByTickMidPoint],
        cancel_method=EClient.cancelTickByTickData,
        is_subscription = True,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.replaceFA,
        # Enable when using 9.85.x
        #done_method=EWrapper.replaceFAEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqAccountUpdates,
        callback_methods=[EWrapper.updateAccountValue, EWrapper.updateAccountTime, EWrapper.updatePortfolio],
        done_method=EWrapper.accountDownloadEnd),
    ApiDefinition(request_method=EClient.reqOpenOrders),
    ApiDefinition(request_method=EClient.reqAllOpenOrders),
    ApiDefinition(request_method=EClient.reqAutoOpenOrders),
    ApiDefinition(request_method=EClient.reqContractDetails,
        callback_methods=[EWrapper.contractDetails,EWrapper.bondContractDetails],
        done_method=EWrapper.contractDetailsEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqCurrentTime,
        callback_methods=[EWrapper.currentTime]),
    ApiDefinition(request_method=EClient.reqCompletedOrders,
        callback_methods=[EWrapper.completedOrder],
        done_method=EWrapper.completedOrdersEnd),
    ApiDefinition(request_method=EClient.reqExecutions,
        callback_methods=[EWrapper.execDetails],
        done_method=EWrapper.execDetailsEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqFamilyCodes,
        callback_methods=[EWrapper.familyCodes]),
    ApiDefinition(request_method=EClient.reqGlobalCancel),
    ApiDefinition(request_method=EClient.reqHistoricalNews,
        callback_methods=[EWrapper.historicalNews],
        done_method=EWrapper.historicalNewsEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqHistoricalTicks,
        callback_methods=[EWrapper.historicalTicks, EWrapper.historicalTicksBidAsk, EWrapper.historicalTicksLast],
        uses_req_id=True,
        has_done_flag=True),
    ApiDefinition(request_method=EClient.reqIds,
        callback_methods=[EWrapper.nextValidId]),
    ApiDefinition(request_method=EClient.reqManagedAccts,
        callback_methods=[EWrapper.managedAccounts]),
    ApiDefinition(request_method=EClient.reqMarketDataType),
    ApiDefinition(request_method=EClient.reqMarketRule,
        callback_methods=[EWrapper.marketRule]),
    ApiDefinition(request_method=EClient.reqMatchingSymbols,
        callback_methods=[EWrapper.symbolSamples]),
    ApiDefinition(request_method=EClient.reqMktDepthExchanges,
        callback_methods=[EWrapper.mktDepthExchanges]),
    ApiDefinition(request_method=EClient.reqNewsArticle,
        callback_methods=[EWrapper.newsArticle]),
    ApiDefinition(request_method=EClient.reqNewsProviders,
        callback_methods=[EWrapper.newsProviders]),
    ApiDefinition(request_method=EClient.reqScannerParameters,
        callback_methods=[EWrapper.scannerParameters]),
    ApiDefinition(request_method=EClient.reqSecDefOptParams,
        callback_methods=[EWrapper.securityDefinitionOptionParameter],
        done_method=EWrapper.securityDefinitionOptionParameterEnd,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqSmartComponents,
        callback_methods=[EWrapper.smartComponents],
        uses_req_id=True),
    ApiDefinition(request_method=EClient.reqSoftDollarTiers,
        callback_methods=[EWrapper.softDollarTiers],
        uses_req_id=True),
    ApiDefinition(request_method=EClient.requestFA,
        callback_methods=[EWrapper.receiveFA]),
    ApiDefinition(request_method=EClient.setServerLogLevel),
    ApiDefinition(request_method=EClient.subscribeToGroupEvents,
        callback_methods=[EWrapper.displayGroupUpdated],
        cancel_method=EClient.unsubscribeFromGroupEvents,
        is_subscription = True,
        uses_req_id=True),
    ApiDefinition(request_method=EClient.updateDisplayGroup),
    ApiDefinition(request_method=None, callback_methods=[EWrapper.commissionReport]),
    ApiDefinition(request_method=None, callback_methods=[EWrapper.marketDataType]),
    ApiDefinition(request_method=None, callback_methods=[EWrapper.orderBound, EWrapper.orderStatus,EWrapper.openOrder,EWrapper.openOrderEnd])
]

"""Missing or incomplete type aliases"""
OVERRIDDEN_TYPE_ALIASES = {
    'SetOfString': 'Set[str]',
    'SetOfFloat': 'Set[float]',
    'ListOfOrder': 'List[ibapi.order.Order]',
    'ListOfFamilyCode': 'List[ibapi.common.FamilyCode]',
    'ListOfContractDescription': 'List[ibapi.contract.ContractDescription]',
    'ListOfDepthExchanges': 'List[ibapi.common.DepthMktDataDescription]',
    'ListOfNewsProviders': 'List[ibapi.common.NewsProvider]',
    'HistogramDataList': 'List[ibapi.common.HistogramData]',
    'ListOfPriceIncrements': 'List[ibapi.common.PriceIncrement]',
    'ListOfHistoricalTick': 'List[ibapi.common.HistoricalTick]',
    'ListOfHistoricalTickBidAsk': 'List[ibapi.common.HistoricalTickBidAsk]',
    'ListOfHistoricalTickLast': 'List[ibapi.common.HistoricalTickLast]',
    'TagValueList': 'List[ibapi.tag_value.TagValue]',
    'SoftDollarTierList': 'List[ibapi.order.SoftDollarTier]',
    'SmartComponentMap': 'Dict[str, ibapi.common.SmartComponent]',
    'OrderId': 'int'
}

"""The types that are aliased by enums. This is used to generate conversion methods
to/from the enum type. These are strings to get the original class from globals"""
ENUM_ALIASES = {
    'TickType': 'TickTypeEnum',
    'FaDataType': 'FaDataTypeEnum',
    'MarketDataType': 'MarketDataTypeEnum',
}

""""""
REQUIRED_FIELDS = {
    'ibapi.common.BarData': {
        'close',
        'date',
        'high',
        'low',
        'open',
    }
}

"""Missing or incorrect hints for members"""
OVERRIDDEN_MEMBER_TYPE_HINTS = {
    'Order': {
        'algoParams': OVERRIDDEN_TYPE_ALIASES['TagValueList'],
        'conditions': 'List[ibapi.order_condition.OrderCondition]',
        'orderComboLegs': 'List[ibapi.order.OrderComboLeg]',
        'orderMiscOptions': 'List[ibapi.tag_value.TagValue]',
        'smartComboRoutingParams': OVERRIDDEN_TYPE_ALIASES['TagValueList'],
        'usePriceMgmtAlgo': 'bool'
    },
    'Contract': {
        'comboLegs': 'List[ibapi.contract.ComboLeg]',
        'deltaNeutralContract': 'ibapi.contract.DeltaNeutralContract'
    },
    'ContractDetails': {
        'secIdList': 'List[str]'
    },
    'ContractDescription': {
        'derivativeSecTypes': 'List[str]'
    },
    'SoftDollarTiers': {
        'tiers': 'List[ibapi.softdollartier.SoftDollarTier]'
    }
}

"""Missing or incorrect method signatures"""
OVERRIDDEN_METHOD_SIGNATURES = {
    'softDollarTiers': 'def softDollarTiers(self, reqId: int, tiers: SoftDollarTierList)'
}


def solution(table, languages, preference):
    answer = ''
    result = []

    languages_dict = dict()
    for i in range(len(languages)):
        languages_dict[languages[i]] = preference[i]

    result_dict = dict()
    for i in range(len(table)):
        x = table[i].split()
        x.reverse()
        total = 0
        for j in range(len(x)-1):
            if x[j] in languages_dict:
                total += languages_dict[x[j]] * (x.index(x[j]) + 1)
        result_dict[x[-1]] = total

    k = max(result_dict.values())
    for i,j in result_dict.items():
        if j == k:
            result.append(i)
    answer = sorted(result)[0]
    return answer

PRG_직업군추천하기_현선.py


from libs import preprocessing
from libs import bancroft_interpolation_solver
from libs import SensorSynchronization
from libs import metrics

import numpy as np
from tqdm import tqdm

def calc_aircraft(aircraft, test_df, sensor_coords, silent=True, sensors_filter_line = None, sensor_synch=None):
    """
    Calculate a track for one aircraft
    :param aircraft: aircraft id
    :param sensor_synch: sensor synchronization object
    :return: all know coordinates for aircraft
    """
    df = test_df.copy()
    mlat_coord_latlong_interp, mlat_time_interp, scores, keypoint_mask = \
        bancroft_interpolation_solver.solve_for_aircraft(df, aircraft, sensor_coords, filter_byTdoA=True,
                                                         silent=silent, sensors_filter_line=sensors_filter_line, sensor_synch=sensor_synch)

    # print('result for aircraft', aircraft, '-', mlat_coord_latlong_interp.shape)

    aircraft_df = preprocessing.get_data_for_aircrafts(df, [aircraft])

    # show prediction charts
    if False:
        key_points = np.abs(scores) < 1e-6

        if mlat_time_interp is not None:
            d_lat = aircraft_df['latitude'] - mlat_coord_latlong_interp[:, 0]
            d_long = aircraft_df['longitude'] - mlat_coord_latlong_interp[:, 1]
            kp_coords = mlat_coord_latlong_interp[key_points]
            kp_time = mlat_time_interp[key_points]

        if np.sum(key_points) >= 2:
            track_vector = kp_coords[-1] - kp_coords[0]
            print('track vector:', track_vector[0:2], 'mean lat/long shift:', [np.mean(d_lat[key_points]) * 111e3, np.mean(d_long[key_points]) * 111e3])

        f, ax = plt.subplots(nrows=3, sharex=True, ncols=2)

        # ax[0].plot(aircraft_df['timeAtServer'], aircraft_df['latitude'], "g", label="lat_true")
        if mlat_time_interp is not None:
            ax[0, 0].plot(mlat_time_interp, mlat_coord_latlong_interp[:, 0], "r", label="lat_pred")
            ax[0, 0].plot(mlat_time_interp, aircraft_df['latitude'], "g", label="lat_pred")
            ax[0, 0].scatter(mlat_time_interp[key_points], mlat_coord_latlong_interp[:, 0][key_points])
            ax[0, 1].plot(mlat_time_interp, d_lat, "r", label="lat_pred")
            ax[0, 1].scatter(mlat_time_interp[key_points], d_lat[key_points])
        ax[0, 0].set_title(f"Lattitude")
        # plt.show()

        # ax[1].plot(aircraft_df['timeAtServer'], aircraft_df['longitude'], "g", label="lon_true")
        if mlat_time_interp is not None:
            ax[1, 0].plot(mlat_time_interp, mlat_coord_latlong_interp[:, 1], "r", label="lon_pred")
            ax[1, 0].plot(mlat_time_interp, aircraft_df['longitude'], "g", label="lon_pred")
            ax[1, 0].scatter(mlat_time_interp[key_points], mlat_coord_latlong_interp[:, 1][key_points])
            ax[1, 1].plot(mlat_time_interp, d_long, "r", label="long_pred")
            ax[1, 1].scatter(mlat_time_interp[key_points], d_long[key_points])
        ax[1, 0].set_title(f"Longitude")
        # plt.show()
        score, dist = metrics.rmse(aircraft_df['latitude'], aircraft_df['longitude'], mlat_coord_latlong_interp[:, 0], mlat_coord_latlong_interp[:, 1], half_threshold = False, return_dist=True)

        # ax[2].plot(aircraft_df['timeAtServer'], aircraft_df['geoAltitude'], "g", label="h_true")
        if mlat_time_interp is not None:
            ax[2, 0].plot(mlat_time_interp, mlat_coord_latlong_interp[:, 2], "r", label="h_pred")
            ax[2, 0].plot(mlat_time_interp, aircraft_df['baroAltitude'], "g", label="h_baro")
            ax[2, 1].plot(mlat_time_interp, dist, "r", label="h_pred")
        ax[2, 0].set_title(f"Height")
        ax[2, 1].set_title(f"RMSE {score}")

        plt.suptitle(f"Aircraft {aircraft}.")

        plt.show()

    return aircraft_df['id'].values, mlat_coord_latlong_interp, scores, keypoint_mask

def predict_all_aircrafts(test_df, aircrafts, sensor_coords, preprocessing_filename=None):
    """
    Locate all aircraft positions
    :param test_df: test data
    :param aircrafts: aircraft ids
    :param sensor_coords: 3D sensor coords
    :param preprocessing_filename: preprocessed file with sensor time shift data for all known sensor pairs
    :return:
    """

    # load preprocessed sensor pairs data
    ss = SensorSynchronization.SensorSynchronization(preprocessing_filename=preprocessing_filename)

    result = []
    for aircraft in tqdm(aircrafts):
        result.append(
            calc_aircraft(aircraft, test_df, sensor_coords, sensors_filter_line=None, sensor_synch=ss))

    predicted_ids = []
    predicted_coords = []
    predicted_scores = []
    predicted_keypoint_mask = []
    for r in result:
        predicted_ids.append(r[0])
        predicted_coords.append(r[1])
        predicted_scores.append(np.array(r[2]))
        predicted_keypoint_mask.append(np.array(r[3]))

    predicted_ids = np.concatenate(predicted_ids)
    predicted_coords = np.concatenate(predicted_coords)
    predicted_scores = np.concatenate(predicted_scores)
    predicted_keypoint_mask = np.concatenate(predicted_keypoint_mask)

    sort_pos = np.argsort(predicted_ids)
    predicted_ids = predicted_ids[sort_pos]
    predicted_coords = predicted_coords[sort_pos]
    predicted_scores = predicted_scores[sort_pos]
    predicted_keypoint_mask = predicted_keypoint_mask[sort_pos]

    return predicted_ids, predicted_coords, predicted_scores, predicted_keypoint_mask


from random import randint
from numpy import base_repr

def gen(n,k):
    strings = []
    for i in range(n):
        random_string = base_repr(randint(1,10**(k-1)),36)
        strings.append(random_string)

    test = '{}\n{}'.format(len(strings),'\n'.join(strings))
    strings.sort()
    clue = '\n'.join(strings)

    return test,clue

def gen1(n,k):
    strings = []
    for i in range(n):
        strings.append('a'*k)

    test = '{}\n{}'.format(len(strings),'\n'.join(strings))
    #strings.sort()
    clue = '\n'.join(strings)

    return test,clue


#benoni.boy
#Google Code Jam
#13 April 2013
import sys
from math import *
from gmpy import *

sys.stdin = open('C:\GCJ\in.txt')
sys.stdout = open('C:\GCJ\out.txt', 'w')

T = eval(input())
for i in range(T):
    r, t = list(map(int, input().split()))
    print('Case #' + str(i + 1) + ':', (1 - 2*r + sqrt(4*r * (r - 1) + 1 + 8*t))//4)

sys.stdin.close()
sys.stdout.close()


import pandas as pd

def aggregate_df_onto_DatetimeIndex(
    df, new_index, method, label="right", new_index_tz="utc"
):
    """
    Aggregate a DataFrame or Series using a given DatetimeIndex

    Parameters
    ----------
    df : pandas.DataFrame
        The dataframe that should be reindexed
    new_index : pandas.DatetimeIndex
        The time stamp index on which `df` should be aggregated
    method : numpy function
        The function to be used for aggregation via
        `DataFrame.groupby('new_time_ix').agg(method)`
    label : str {'right', 'left'}, optional
        Which side of the aggregated period to take the label for the new
        index from
    new_index_tz : str, optional
        Defaults to 'utc'. Note that if `new_index` already has time zone
        information, this kwarg is ignored

    Returns
    -------

    df_reindexed : pandas.DataFrame

    """

    if label == "right":
        fill_method = "bfill"
    elif label == "left":
        fill_method = "ffill"
    else:
        raise NotImplementedError('`label` must be "left" or "right"')

    # Make sure we work with a DataFrame and make a copy of it
    df_temp = pd.DataFrame(df).copy()

    # Generate DataFrame with desired DatetimeIndex as data,
    # which will later be reindexed by DatetimeIndex of original DataFrame
    df_new_t = pd.DataFrame(index=new_index, data={"time": new_index})

    # Update time zone info if there is none
    if not df_new_t.index.tzinfo:
        df_new_t.index = df_new_t.index.tz_localize(new_index_tz)

    # Crop both time series to make them cover the same period.
    # This is to avoid the ffill or bfill to run outside of the
    # range of the new index, which produces wrong result for the
    # end point of the time series in the aggregated result
    t_start = max(df_temp.index.min(), df_new_t.index.min())
    t_stop = min(df_temp.index.max(), df_new_t.index.max())
    df_new_t = df_new_t.loc[t_start:t_stop]
    df_temp = df_temp.loc[t_start:t_stop]

    # Reindex to get the forward filled or backwar filled time stamp of the
    # new index which can be used for aggregation in the next step
    df_new_t = df_new_t.reindex(df_temp.index, method=fill_method)

    # Aggregate data onto new DatetimeIndex
    df_temp["new_time_ix"] = df_new_t.time
    df_reindexed = df_temp.groupby("new_time_ix").agg(method)
    # Update name and timezone of new index
    df_reindexed.index.name = df_temp.index.name
    if not df_reindexed.index.tzinfo:
        df_reindexed.index = df_reindexed.index.tz_localize("UTC").tz_convert(
            df_temp.index.tzinfo
        )

    return df_reindexed


class Node:

    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:

    def __init__(self):
        self.head = None

    def add_value(self, value):
        if self.head is None:
            node = Node(value)
            self.head = node
        else:
            temp = self.head
            while temp.next:
                temp = temp.next
            node = Node(value)
            temp.next = node

    def get_head(self):
        return self.head

    def check_list(self, e_id):
        temp = self.head
        while temp:
            if temp.data.e_id == e_id:
                return 0
            temp = temp.next
        return 1

    def delete(self, value):
        head_temp = self.head
        f = 0
        # for stating values
        while head_temp and head_temp.data.emp_id == value:
            f = 1
            temp = head_temp.next
            head_temp = temp

        # any value in the middle
        self.head = head_temp
        temp = self.head
        prev = None

        while temp and temp.next:

            if temp.data.emp_id == value:
                f = 1
                t1 = temp.next
                prev.next = t1
                temp = t1
            else:
                prev = temp
                temp = temp.next

        if temp and temp.data.emp_id == value:
            f = 1
            prev.next = None
        if f != 0:
            return "Employee is deleted"
        else:
            return "Employee not found"


'''
Phương thức ẩn danh
Cú pháp: lambda [ parameter1, parameter2, ...]:
         expression
'''
import math

# Tính S = (x*x +1)^n

s = lambda x,n: math.pow(math.pow(x,2) + 1, n)
print('S = ', s(2,3))
s(3,3)


# -*- coding:utf-8 -*-
# @Time : 2021/7/23 20:13
# @Author : zengln
# @File : 加一.py
# 给定一个由 整数 组成的 非空 数组所表示的非负整数，在该数的基础上加一。
#
#  最高位数字存放在数组的首位， 数组中每个元素只存储单个数字。
#
#  你可以假设除了整数 0 之外，这个整数不会以零开头。
#
#
#
#  示例 1：
#
#
# 输入：digits = [1,2,3]
# 输出：[1,2,4]
# 解释：输入数组表示数字 123。
#
#
#  示例 2：
#
#
# 输入：digits = [4,3,2,1]
# 输出：[4,3,2,2]
# 解释：输入数组表示数字 4321。
#
#
#  示例 3：
#
#
# 输入：digits = [0]
# 输出：[1]
#
#
#
#
#  提示：
#
#
#  1 <= digits.length <= 100
#  0 <= digits[i] <= 9
#
#  Related Topics 数组 数学

class Solution:
    def plusOne(self, digits: List[int]) -> List[int]:
        for index in range(len(digits)-1, -1, -1):
            if digits[index] + 1 < 10:
                digits[index] += 1
                return digits

            digits[index] = 0
        digits.insert(0, 1)
        return digits


from pyspark.sql import SparkSession
from pyspark.sql.functions import explode, col, lit, lead, concat
from pyspark.sql import Window
import pymongo
from neo4j import GraphDatabase
import ast

spark = SparkSession.builder.master('spark://spark-master:7077').config("spark.cores.max","8").getOrCreate()

# Problems with the mongodb connector.
client = pymongo.MongoClient("mongodb://root:password@mongodb:27017")
db = client["mongo"]
coll = db["paths"]

l = []
for x in coll.find():
    #print(x)
    try:
        l.append((int(x['node1']), int(x['node2']), [int(i) for i in ast.literal_eval(x['path'])]))
    except:
        l.append((int(x['node1']), int(x['node2']), [int(i) for i in x['path']]))

#print(l)
df = spark.createDataFrame(l,schema=["node1", "node2", "path"])
#df.show()
window = Window.partitionBy("node1","node2").orderBy("node1","node2")

df_edges = df.select("node1","node2", explode("path").alias("node"))
df_edges = df_edges.withColumn("next_node", lead("node").over(window)).na.drop()
df_edges = df_edges.withColumn("edge", concat(lit("("), col("node"), lit(","), col("next_node"), lit(")")))
#df_edges.show()

df_edge_counts = df_edges.groupBy("edge").count()
#df_edge_counts.show()

total_paths = df.count()
df_edge_betweenness = df_edge_counts.withColumn(
    "edge_betweenness_centrality",
    col("count") / lit(total_paths)
)

df_edge_betweenness.show()

def updateNeo4j(df):
    from neo4j import GraphDatabase
    driver = GraphDatabase.driver("bolt://neo4j:7687", auth=("neo4j", "password"))

    edge = df.edge.strip('(').strip(')').split(',')
    centrality = df.edge_betweenness_centrality
    a = edge[0]
    b = edge[1]

    query = f"""
    MATCH (a:Node)-[r:Road]->(b:Node)
    WHERE a.id = {a}
    AND b.id = {b}
    SET r.centrality = {centrality}
    """

    with driver.session(database='neo4j') as session:
        result = session.run(query)

df_edge_betweenness.foreach(updateNeo4j)
spark.stop()
# Install neo4j on worker nodes


import os
import re
import subprocess
from pathlib import Path

import pyshark
import uvicorn
from fastapi import FastAPI, responses
from pydantic import BaseModel

app = FastAPI()

class pysharkParam(BaseModel):
    interface: str
    bpf_filter: str
    time: int

pcaps = "/var/log/krycy/pcaps/"
logs = "/var/log/krycy/logs/"

if __name__ == "__main__":
    uvicorn.run("agent:app", port=8000, reload=True, access_log=False)

@app.get("/getIfconfig")
def get_ifconfig():
    return subprocess.check_output(["ifconfig"])

@app.put("/sniffing")
def sniffing(param: pysharkParam):
    Path(pcaps).mkdir(parents=True, exist_ok=True)
    logs = os.listdir(pcaps)
    if len(logs) == 0:
        logname = "pyshark.1.pcap"
    else:
        logs = sorted(logs, key=lambda x: (len(x), x))
        lastindex = re.search('\d+', logs[-1]).group(0)
        logname = "pyshark." + str(int(lastindex) + 1) + ".pcap"
    print(pcaps + logname)
    cap = pyshark.LiveCapture(interface=param.interface, bpf_filter=param.bpf_filter, output_file=pcaps + logname)
    cap.sniff(timeout=param.time)

    return responses.FileResponse(pcaps + logname)

@app.get("/getPcapList")
def get_pcap_list():
    Path(pcaps).mkdir(parents=True, exist_ok=True)
    return os.listdir(pcaps)

@app.get("/getPcap")
def get_pcap(name):
    Path(pcaps).mkdir(parents=True, exist_ok=True)
    if os.path.isfile(pcaps + name):
        return responses.FileResponse(pcaps + name)
    return "File doesn't exits"

@app.get("/getLogList")
def get_log_list():
    Path(logs).mkdir(parents=True, exist_ok=True)
    return os.listdir(logs)

@app.get("/getLog")
def get_log(name):
    Path(logs).mkdir(parents=True, exist_ok=True)
    if os.path.isfile(logs + name):
        return responses.FileResponse(logs + name)
    return "File doesn't exits"

@app.get("/command")
def exec_command(command):
    result = subprocess.getoutput(command)
    return result


import inspect
import asyncio

from typing import List, Optional
from octii.api.api import APIClient
from .converters import convert_args
from octii.api.Message import Message

class Command:
    def __init__(self, func, name: str, description: str, aliases: List[str], params_data: List[inspect.Parameter]):
        self.func = func
        self.name = name
        self.description = description
        self.aliases = aliases
        self.params_data = params_data

def create_command(func, *, name: Optional[str] = None, aliases: Optional[List[str]] = None):
    return Command(func, name or func.__name__, func.__doc__ or "No description.", aliases or [], list(inspect.signature(func).parameters.values()))

class Bot:
    def __init__(self, token: str, prefix: str = "!"):
        self._commands = {
            'help': create_command(self.default_help_command, name="help", aliases=['cmds', 'cmd', 'manual'])
        }
        self._api_client = APIClient()
        self._api_client.set_auth_token(token)
        self.prefix = prefix
        self.selfbot = False

    async def default_help_command(self, ctx):
        """The default octii.py help command. Prints out a list of every command and their descriptions."""
        text = "```\n"

        for command in self._commands.values():
            text += command.name + ": " + command.description + "\n"

        ctx.send(text + "```")

    def run(self):
        def handle_message(msg_data):
            if not self.selfbot or msg_data['author']['id'] == self._api_client.user_id:
                if msg_data['content'].startswith(self.prefix):
                    args = msg_data['content'].split(' ')
                    cmd_name = args[0]
                    args = args[1:]

                    try:
                        cmd = self._commands[cmd_name[len(self.prefix):]]
                        asyncio.create_task(cmd.func(create_context_for_message(self, msg_data), *convert_args(cmd.func, self, cmd.params_data, *args)))
                    except KeyError:
                        raise
                        print("Command does not exist!", cmd_name)
                    except Exception as e:
                        raise

        self._api_client.init_sse()
        self._api_client.sse.on_message += handle_message
        self._api_client.run()

    def add_command(self, command):
        self._commands[command.name] = command

    def command(self, *args, **kwargs):
        def decorator(func):
            result = create_command(func, *args, **kwargs)
            self.add_command(result)
            return result

        return decorator

class Context:
    def __init__(self, bot: Bot, msg: Message):
        self._bot = bot
        self.msg = msg
        self.channel_id = ""

    def send(self, msg: str):
        return self._bot._api_client.send_message(self.channel_id, msg)

def create_context_for_message(bot, msg_data):
    ctx = Context(bot, Message.load_json(msg_data))
    ctx._bot = bot
    ctx.channel_id = msg_data['channel_id']
    ctx.author = bot._api_client.fetch_user(msg_data['author']['id'])
    return ctx


from categories.views import CategoryViewset
from payment.views import PaymentViewset, RoomFeeViewset, SummaryViewset
from rest_framework_extensions.routers import ExtendedSimpleRouter
from task.views.assignment_views import AssignmentViewset
from task.views.exam_views import ExamViewset

# from livetutor_py.urls import router
from .views import RoomUserViewset, RoomViewset

router = ExtendedSimpleRouter()
room_routes = router.register(r'rooms', RoomViewset, basename='room')
room_user_routes = router.register(
    'room-users', RoomUserViewset, basename='room-users')
# route for room users and related
room_routes.register(r'users', RoomUserViewset, parents_query_lookups=[
                     'room'], basename='room-users')
room_routes.register(r'fees', RoomFeeViewset,
                     basename='room-fees',  parents_query_lookups=['room'])
#  .register('invite', RoomUserViewSet())

room_routes.register(r'payments', PaymentViewset,
                     basename='room-fee-payments', parents_query_lookups=['room_fee__room'])

room_routes.register(r'payment-summary', SummaryViewset,
                     basename='payment-summary', parents_query_lookups=['room'])
router.register('categories', CategoryViewset, basename='categories')

room_routes.register('assignments', AssignmentViewset,
                     basename='assignments', parents_query_lookups=['room'])
room_routes.register('exams', ExamViewset,
                     basename='exams', parents_query_lookups=['room'])
room_routes.register('notes', ExamViewset,
                     basename='notes', parents_query_lookups=['room'])
urlpatterns = router.urls


from torch import nn
from torch.optim import Adam

class Optimizer(nn.Module):
    def __init__(self, enc_params, dec_params, image_rec_loss, adam_kwargs):
        super(Optimizer, self).__init__()

        self.adam_kwargs = adam_kwargs
        self.image_rec_loss = image_rec_loss
        self.opt = None

        self.set_new_params(enc_params, dec_params)

    def set_new_params(self, enc_params, dec_params, image_rec_loss=None):

        def preprocess(params):
            params = [p for p in params if p.requires_grad]
            return params

        params = preprocess(enc_params) + preprocess(dec_params)
        self.opt = Adam(params, **self.adam_kwargs)

        if image_rec_loss is not None:
            self.image_rec_loss = image_rec_loss

    def compute_loss(self, real_x, rec_x, update_parameters=True):
        loss = self.image_rec_loss(real_x, rec_x)

        if update_parameters:
            if loss.item() > 1e9:
                raise ValueError("Too large value of loss function (>10^9)!")

            self.opt.zero_grad()
            loss.backward()
            self.opt.step()

        loss_info = {"image_rec_loss": loss.item()}
        return loss_info


import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn

import torchvision
import torchvision.transforms as transforms
from advertorch.attacks import L2PGDAttack

import os
import matplotlib.pyplot as plt
import numpy as np
import pkbar

from models import *

file_name = 'basic_training_cat_to_dog_24iters'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
net = CNN()
net = net.to(device)

basic_net = CNN()
basic_net = basic_net.to(device)
checkpoint = torch.load('./checkpoint/basic_training')
basic_net.load_state_dict(checkpoint['net'])
basic_net.eval()
cudnn.benchmark = True

transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
])

train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform_train)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=4)

def basic_training_class_perturbation_main(num_epochs, learning_rate, eps, iterations, new_class, target_class):
    optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9, weight_decay=5e-4)
    adversary  = L2PGDAttack(basic_net, loss_fn=nn.CrossEntropyLoss(), eps=eps, nb_iter=iterations, eps_iter=0.2, rand_init=True, clip_min=0.0, clip_max=1.0, targeted=True)
    criterion = nn.CrossEntropyLoss()
    for epoch in range(0, num_epochs):
        kbar = pkbar.Kbar(target=len(train_loader), epoch=epoch, num_epochs=num_epochs, width=30, always_stateful=True)
        adjust_learning_rate(optimizer, epoch, learning_rate)
        net.train()
        train_loss = 0
        correct = 0
        total = 0
        for batch_idx, (inputs, targets) in enumerate(train_loader):
            inputs, targets = inputs.to(device), targets.to(device)
            optimizer.zero_grad()

            target_ids = torch.where(targets == target_class)[0]
            for id in target_ids:
                inputs[id] = adversary.perturb(torch.unsqueeze(inputs[id], 0), torch.LongTensor([new_class]).to(device))

            benign_outputs = net(inputs)
            loss = criterion(benign_outputs, targets)
            loss.backward()

            optimizer.step()
            train_loss += loss.item()
            _, predicted = benign_outputs.max(1)

            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

            kbar.update(batch_idx, values=[("loss", loss.item()), ("acc", 100. * correct / total)])
        print()
        save_model()

def adjust_learning_rate(optimizer, epoch, learning_rate):
    lr = learning_rate
    if epoch >= 50:
        lr /= 10
    if epoch >= 75:
        lr /= 10
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

def save_model():
    state = {
        'net': net.state_dict()
    }
    if not os.path.isdir('checkpoint'):
        os.mkdir('checkpoint')
    torch.save(state, './checkpoint/' + file_name)


from models.random import RandomAgent
import os
import gym
import random, json
import numpy as np
import matplotlib.pyplot as plt
from copy import deepcopy

from toybox import Toybox, Input
from toybox.interventions.breakout import BreakoutIntervention

from envs.wrappers.paths import (
    get_intervention_dir,
    breakout_env_id,
    get_root_intervention_dir,
)
from envs.wrappers.breakout.interventions.reset_wrapper import (
    BreakoutResetWrapper,
)

from envs.wrappers.start_states import (
    get_start_env,
    sample_start_states,
)

def write_intervention_json(state, state_num, count):
    environment = "Breakout"
    with open(
        f"{get_intervention_dir(state_num, environment)}/{count}.json",
        "w",
    ) as outfile:
        json.dump(state, outfile)

# BRICK INTERVENTIONS

def get_drop_brick_col_interventions(env, state_num, count):
    """Drop column of blocks."""
    state = env.toybox.state_to_json()
    num_cols = 18

    for col in range(num_cols):
        state = Toybox("breakout").state_to_json()

        for i in range(len(state["bricks"])):
            if col == state["bricks"][i]["col"]:
                # print(row)
                # print(state["bricks"][i]["row"])
                state["bricks"][i]["alive"] = False

        write_intervention_json(state, state_num, count)
        count = count + 1
    return count

def get_drop_brick_row_interventions(env, state_num, count):
    """Drop row or column of blocks."""
    state = env.toybox.state_to_json()
    num_rows = 6

    for row in range(num_rows):
        state = Toybox("breakout").state_to_json()

        for i in range(len(state["bricks"])):
            if row == state["bricks"][i]["row"]:
                # print(row)
                # print(state["bricks"][i]["row"])
                state["bricks"][i]["alive"] = False

        write_intervention_json(state, state_num, count)

        count = count + 1
    return count

# PADDLE INTERVENTIONS #

def get_shift_paddle_interventions(env, state_num, count):
    """Start the agent in different positions."""
    state = env.toybox.state_to_json()
    xs = [30.0, 90.0, 160.0, 210.0]
    for x in xs:
        state = Toybox("breakout").state_to_json()
        state["paddle"]["position"]["x"] = x
        write_intervention_json(state, state_num, count)
        count = count + 1
    return count

def get_paddle_speed_interventions(env, state_num, count):
    """Set the agent's speed."""
    state = env.toybox.state_to_json()
    speeds = [1.0, 2.0, 3.0, 5.0, 6.0, 7.0]
    for speed in speeds:
        state = Toybox("breakout").state_to_json()
        state["paddle_speed"] = speed
        write_intervention_json(state, state_num, count)
        count = count + 1
    return count

def get_paddle_width_interventions(env, state_num, count):
    """Change the width of the paddle"""
    state = env.toybox.state_to_json()
    widths = [6.0, 12.0, 36.0, 48.0]
    for width in widths:
        state = Toybox("breakout").state_to_json()
        state["paddle_width"] = width
        write_intervention_json(state, state_num, count)
        count = count + 1
    return count

# BALL INTERVENTTIONS #

def get_ball_radius_interventions(env, state_num, count):
    """Start the ball in different positions."""
    state = env.toybox.state_to_json()
    print(state)
    radii = [0.5, 1.0, 3.0, 4.0]
    for radius in radii:
        state = Toybox("breakout").state_to_json()
        state["ball_radius"] = radius
        write_intervention_json(state, state_num, count)
        count = count + 1

        env.toybox.write_state_json(state)
        dir_path = "storage/states/interventions/Breakout/images/"
        env.toybox.save_frame_image(dir_path + "{}.png".format(count))
    return count

def create_intervention_states(num_states: int):
    """Create JSON states for all interventions."""
    dir = get_root_intervention_dir("Breakout")

    for state_num in range(
        num_states
    ):  # 0th state is the default start state of the game
        env = get_start_env(
            state_num,
            lives=3,
            environment="Breakout",
        )

        count = 0
        prevcount = count
        count = get_paddle_width_interventions(env, state_num, count)
        print(f"{prevcount} to {count-1} get_paddle_width_interventions")
        prevcount = count
        count = get_paddle_speed_interventions(env, state_num, count)
        print(f"{prevcount} to {count-1} get_paddle_speed_interventions")
        prevcount = count
        count = get_shift_paddle_interventions(env, state_num, count)
        print(f"{prevcount} to {count-1} get_shift_paddle_interventions")
        prevcount = count

        count = get_drop_brick_row_interventions(env, state_num, count)
        print(f"{prevcount} to {count-1} get_drop_brick_row_interventions")
        prevcount = count
        count = get_drop_brick_col_interventions(env, state_num, count)
        print(f"{prevcount} to {count-1} get_drop_brick_col_interventions")

        print(f"Created {count} intervention states for state {state_num} in `{dir}`.")
    return count

def get_single_intervened_environment(
    state_num, intervention_number, want_feature_vec, lives
):
    env = gym.make(breakout_env_id)
    env = BreakoutResetWrapper(
        env, state_num=state_num, intv=intervention_number, lives=lives
    )
    return env

def get_intervened_environments(state_num, want_feature_vec, lives):
    # make them if they aren't created
    count = create_intervention_states(state_num + 1)
    envlist = []
    for intv in range(count):
        env = get_single_intervened_environment(
            state_num, intv, want_feature_vec, lives
        )
        envlist.append(env)
    return envlist

def get_all_intervened_environments(num_states, want_feature_vec, lives):
    count = create_intervention_states(num_states)
    envlist = []
    for state_num in range(num_states):
        for intv in range(count):
            env = get_single_intervened_environment(
                state_num, intv, want_feature_vec, lives
            )
            envlist.append(env)
    return envlist

if __name__ == "__main__":
    num_states = 1
    agent = RandomAgent(gym.make(breakout_env_id).action_space)
    sample_start_states(agent, num_states, "Breakout", device="cpu")
    create_intervention_states(num_states)


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
import sys
sys.path.append("C:/Users10710/OneDrive/桌面/HW8/")
from kmeans import k_means, Cal_Distance, Cal_Accuracy
import math
import matplotlib.pyplot as plt
import random
import numpy as np
import copy
import pandas as pd
from pandas import Series,DataFrame

def Cal_Accuracy_testdata(result,testcount):
    #For iris dataset
    classify_data = [[] for i in range(3)]

    classify_data[0] = result[0:test_count[0]]
    classify_data[1] = result[test_count[0]:test_count[0]+test_count[1]]
    classify_data[2] = result[test_count[0]+test_count[1]:test_count[0]+test_count[1]+test_count[2]]
    numberlabel = [[],[],[]]
    count = [[],[],[]]
    acc = [[],[],[]]
    for i in range(0,3):
        numberlabel[i] = max(classify_data[i],key=classify_data[i].count)
        count[i] = classify_data[i].count(numberlabel[i])
    for i in range(0,3):
        acc[i] = count[i]/len(classify_data[i])

    acc = np.mean(acc)

    return acc

def Cal_Distance_digitnumber(feature, center):
    dimension = len(feature)
    dist = 0.0
    for i in range(dimension):
        dist += (feature[i] - center[i]) ** 2
    return math.sqrt(dist)

def testdata_classify(testdata,center):
    data = testdata[:,1:]
    labels = [-1 for i in range(len(data))]

    for labelIndex, item in enumerate(data):
        classIndex = -1
        minDist = math.inf
        for i, point in enumerate(center):
            dist = Cal_Distance_digitnumber(item, point)
            #Minimize the distance ,select the cluster
            if(dist < minDist):
                classIndex = i
                minDist = dist
        labels[labelIndex] = classIndex
    return labels

train_dataset =  pd.read_csv('C:/Users/10710/OneDrive/桌面/xuezhengyuansb/ziptrain.csv', header=None)
train_dataset = train_dataset.drop([0],axis =0)
train_dataset = train_dataset.drop([0],axis =1)
train_dataset = train_dataset[train_dataset[1].isin([1,4,8,'1','4','8'])]

test_dataset = pd.read_csv('C:/Users/10710/OneDrive/桌面/xuezhengyuansb/ziptest.csv', header=None)
test_dataset = test_dataset.drop([0],axis = 0)
test_dataset = test_dataset.drop([0],axis = 1)
test_dataset = test_dataset[test_dataset[1].isin([1,4,8,'1','4','8'])]

train_dataset_np = train_dataset.values
test_dataset_np = test_dataset.values

for i in range(0,len(train_dataset_np)):
    for j in range(0,len(train_dataset_np[0])):
        train_dataset_np[i][j] =float(train_dataset_np[i][j])

for i in range(0,len(test_dataset_np)):
    for j in range(0,len(test_dataset_np[0])):
        test_dataset_np[i][j] =float(test_dataset_np[i][j])

#The first number is class [1,4,8]
#print(train_dataset_np)
#print(test_dataset_np)

#Count 1,4,8:
train_count = [0,0,0]

for i in range(0,len(train_dataset_np)):
    if train_dataset_np[i][0] == 1:
        train_count[0] += 1
    if train_dataset_np[i][0] == 4:
        train_count[1] += 1
    if train_dataset_np[i][0] == 8:
        train_count[2] += 1
#print(train_count)

test_count = [0,0,0]

for i in range(0,len(test_dataset_np)):
    if test_dataset_np[i][0] == 1:
        test_count[0] += 1
    if test_dataset_np[i][0] == 4:
        test_count[1] += 1
    if test_dataset_np[i][0] == 8:
        test_count[2] += 1

#print(train_count)
train_dataset_np_order = train_dataset_np[train_dataset_np[:,0].argsort()]#The number:1:1005, 4:652, 8:542
test_dataset_np_order = test_dataset_np[test_dataset_np[:,0].argsort()]

#print(train_dataset_np_order)

result = k_means(train_dataset_np_order, 3, 1000)
print(result[1])

#A method to assign class labels to each of your clusters：
acc = Cal_Accuracy_testdata(result[1], train_count)
print("The acc of training is :",acc)

#Predict labels on the zip.test data:
labels = testdata_classify(test_dataset_np_order, result[2])
print("Print labels", labels)

#Acc:
acc_test = Cal_Accuracy_testdata(labels, test_count)
print("The acc of test is :",acc_test)

#PCA:
train_data_pca = train_dataset_np_order[:,1:]
train_pca = PCA(n_components= 2)
pca_data = train_pca.fit_transform(train_data_pca)
print(pca_data)

pca_data_label = train_dataset_np_order[:,0:1]
pca_train_data = np.hstack((pca_data_label, pca_data))
print(pca_train_data)

#pca kmeans:
result_pca = k_means(pca_train_data, 3, 1000)
acc_pca = Cal_Accuracy_testdata(result_pca[1], train_count)
print("The acc of training is :",acc_pca)

#acc_loss:
print("The acc loss is :", acc-acc_pca)

if __name__ == '__main__':
    pass


class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))

# Example 1:
ransomNote = "a"
magazine = "b"
# Output: false
s = Solution()
result = s.canConstruct(ransomNote, magazine)
print(result)
print(result == False)

# Example 2:
ransomNote = "aa"
magazine = "ab"
# Output: false
s = Solution()
result = s.canConstruct(ransomNote, magazine)
print(result)
print(result == False)

# Example 3:
ransomNote = "aa"
magazine = "aab"
# Output: true
s = Solution()
result = s.canConstruct(ransomNote, magazine)
print(result)
print(result == True)


import sys
import re
from operator import itemgetter

import youtube_dl
import pydub
import eyed3

class Downloader:
    def __init__(self, url, path):
        self.url = url
        self.path = path

    def run(self):
        print('[1/3] Downloading the audio')
        self.download()
        print('[2/3] Dowloading metadata')
        track_list = self.get_tracklist()
        print('[3/3] Cutting and adding metadata')
        self.cut(track_list)
        print('DONE!')

    def get_tracklist(self):
        # download metadata
        ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.%(ext)s'})
        with ydl:
            result = ydl.extract_info(
                self.url,
                download=False
            )

        # extracting tracklist from description:
        track_list = re.findall('(?P<datetime>\d+\:\d+)[ \|\:-]*(?P<artist>[^\-\:]*)[ -\:\|](?P<track>.*)', result['description'])
        for i, t in enumerate(track_list):
            arr = [0, 0, "", ""]
            datetime = t[0].split(":")
            arr[0] = int(datetime[0])*60 + int(datetime[1])
            arr[2] = t[1].strip()
            arr[3] = t[2].strip()
            track_list[i] = arr

        # adding end of track duration
        track_list.sort(key = itemgetter(0))
        for i in range(len(track_list)-1):
            track_list[i][1] = track_list[i+1][0]
        track_list[-1][1] = result['duration']

        return track_list

    def download(self):
        ydl = youtube_dl.YoutubeDL({
                "format": "bestaudio/best",
                "outtmpl": f"/tmp/%(id)s.%(ext)s",
                "ignoreerrors": True,
                "postprocessors": [
                    {
                        "key": "FFmpegExtractAudio",
                        "preferredcodec": "mp3",
                        "preferredquality": "320",
                    }
                ],
            }
        )

        with ydl:
            ydl.download([self.url])

    def cut(self, track_list):
        # load downloaded data
        filepath = f'/tmp/{self.url.split("v=")[-1]}.mp3'
        tracks = pydub.AudioSegment.from_mp3(filepath)

        for i, arr in enumerate(track_list):
            # cut the file
            file_name = arr[2] + ' - ' + arr[3] + '.mp3'
            tracks[arr[0]*1000:arr[1]*1000].export(self.path + '/' + file_name, format="mp3")

            # add metadata
            audiofile = eyed3.load(self.path + '/' + file_name)
            audiofile.tag.title = track_list[i][3]
            audiofile.tag.artist = track_list[i][2]
            audiofile.tag.save()

if __name__ == "__main__":
    if len(sys.argv) == 3:
        Downloader(sys.argv[1], sys.argv[2]).run()
    else:
        print("========================================")
        print("                 Playlisteo             ")
        print("========================================")
        print("")
        print("USAGE : python playlisteo.py <URL> <Destination_folder>")
        print('Please type the full destination path')


# -*- coding: utf-8 -*-
"""
Migrate permissions
"""
import sys

from django.apps import apps
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.db.utils import IntegrityError
from django.utils.translation import gettext as _

from guardian.models import UserObjectPermissionBase

from onadata.libs.utils.model_tools import queryset_iterator

User = get_user_model()

class Command(BaseCommand):
    """Migrate permissions"""

    help = _("Migrate permissions")

    def add_arguments(self, parser):
        parser.add_argument(
            "--model",
            "-m",
            action="store_true",
            dest="app_model",
            default=False,
            help="The model the permission belong too." " (app.model format)",
        )
        parser.add_argument(
            "--perm-table",
            "-p",
            action="store_true",
            dest="perms_tbl",
            default=False,
            help="The new model permission are stored in" " (app.model format)",
        )

    def handle(self, *args, **options):
        self.stdout.write("Migrate permissions started", ending="\n")

        if len(args) < 2:
            self.stdout.write(
                "This command takes two argument -m and -p "
                "Example: "
                "-m logger.Team "
                "-p logger.TeamUserObjectPermission"
            )
            sys.exit()

        if options["app_model"]:
            app_model = args[0]
        else:
            self.stdout.write("-m , should be set as the first argument")
            sys.exit()

        if options["perms_tbl"]:
            perms_tbl = args[1]
        else:
            self.stdout.write("-p , should be set as the second argument")
            sys.exit()

        model = apps.get_model(app_model)
        perms_model = apps.get_model(perms_tbl)

        if not issubclass(perms_model, UserObjectPermissionBase):
            self.stdout.write(
                "-p , should be a model of a class that is "
                "a subclass of UserObjectPermissionBase"
            )
            sys.exit()

        content_type = ContentType.objects.get(
            model=model.__name__.lower(), app_label=model._meta.app_label
        )

        # Get all the users
        users = User.objects.exclude(
            username__iexact=settings.ANONYMOUS_DEFAULT_USERNAME
        ).order_by("username")

        for user in queryset_iterator(users):
            self.stdout.write(f"Processing: {user.pk} - {user.username}")
            for uop in (
                user.userobjectpermission_set.filter(content_type=content_type)
                .select_related("permission", "content_type")
                .prefetch_related("permission", "content_type")
            ):
                try:
                    perms_model(
                        content_object=uop.content_object,
                        user=user,
                        permission=uop.permission,
                    ).save()
                except IntegrityError:
                    continue
                except ValueError:
                    pass


def read_source_file(file_name):
    # Initialize an empty dictionary to store the key-value pairs
    data_dict = {}

    # Open and read the colon-separated file
    with open(file_name, 'r') as file:
        for line in file:
            # Split each line into key and value using the colon (':') as a separator
            key, value = line.strip().split(':', 1)

            # Store the key-value pair in the dictionary
            data_dict[key.strip()] = value.strip()
    return data_dict


import streamlit as st
import pandas as pd
import seaborn as sns

def main():
    st.title('Streamlit Data Visualisation')

    fileUpload = st.file_uploader('Upload your file', type=['csv', 'xlsx'])
    df = None
    if fileUpload is not None:
        if fileUpload.type == 'text/csv':
            df = pd.read_csv(fileUpload)
        elif fileUpload.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
            df = pd.read_excel(fileUpload)

        st.dataframe(df)

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
import pandas as pd

def handler(event, context):
    df = pd.DataFrame(
        {
            "A": 1.0,
            "B": pd.Timestamp("20130102"),
            "C": pd.Series(1, index=list(range(4)), dtype="float32"),
            "D": pd.Categorical(["test", "train", "test", "train"]),
            "E": "foo",
        }
    )
    print(df.head())
    return "pandas version:"+pd.__version__


import time

from eth_typing import BlockNumber

from trinity.components.builtin.metrics.abc import MetricsServiceAPI

class SyncMetricsRegistry:
    """
    Registry to track weighted moving average of pivot events, and report to InfluxDB.
    """
    def __init__(self, metrics_service: MetricsServiceAPI) -> None:
        self.metrics_service = metrics_service
        self.pivot_meter = metrics_service.registry.meter('trinity.p2p/sync/pivot_rate.meter')

    def record_lag(self, lag: int) -> None:
        self.metrics_service.registry.gauge('trinity.sync/chain_head_lag').set_value(lag)

    async def record_pivot(self, block_number: BlockNumber) -> None:
        # record pivot and send event annotation to influxdb
        self.pivot_meter.mark()
        pivot_time = int(time.time())
        post_data = (
            f'events title="beam pivot @ block {block_number}",'
            f'text="pivot event",tags="{self.metrics_service.registry.host}" {pivot_time}'
        )
        await self.metrics_service.send_annotation(post_data)


from robot.api.parsing import Token

from robotidy.disablers import skip_section_if_disabled
from robotidy.transformers import Transformer

class OrderTags(Transformer):
    """
    Order tags.

    Tags are ordered in lexicographic order like this:

    ```robotframework
    *** Test Cases ***
    Tags Upper Lower
        [Tags]    ba    Ab    Bb    Ca    Cb    aa
        My Keyword

    *** Keywords ***
    My Keyword
        [Tags]    ba    Ab    Bb    Ca    Cb    aa
        No Operation
    ```

    To:

    ```robotframework
    *** Test Cases ***
    Tags Upper Lower
        [Tags]    aa    Ab    ba    Bb    Ca    Cb
        My Keyword

    *** Keywords ***
    My Keyword
        [Tags]    aa    Ab    ba    Bb    Ca    Cb
        No Operation
    ```

    Default order can be changed using following parameters:
      - ``case_sensitive = False``
      - ``reverse = False``
    """

    ENABLED = False

    def __init__(
        self,
        case_sensitive: bool = False,
        reverse: bool = False,
        default_tags: bool = True,
        force_tags: bool = True,
    ):
        super().__init__()
        self.key = self.get_key(case_sensitive)
        self.reverse = reverse
        self.default_tags = default_tags
        self.force_tags = force_tags

    @skip_section_if_disabled
    def visit_Section(self, node):  # noqa
        return self.generic_visit(node)

    def visit_Tags(self, node):  # noqa
        return self.order_tags(node, indent=True)

    def visit_DefaultTags(self, node):  # noqa
        return self.order_tags(node) if self.default_tags else node

    def visit_ForceTags(self, node):  # noqa
        return self.order_tags(node) if self.force_tags else node

    def order_tags(self, node, indent=False):
        if self.disablers.is_node_disabled(node):
            return node
        ordered_tags = sorted(
            (tag.value for tag in node.data_tokens[1:]),
            key=self.key,
            reverse=self.reverse,
        )
        if len(ordered_tags) <= 1:
            return node
        comments = node.get_tokens(Token.COMMENT)
        tokens = []
        if indent:
            tokens.append(Token(Token.SEPARATOR, self.formatting_config.indent))
        tokens.append(node.data_tokens[0])
        tag_tokens = (Token(Token.ARGUMENT, tag) for tag in ordered_tags)
        tokens.extend(self.join_tokens(tag_tokens))
        tokens.extend(self.join_tokens(comments))
        tokens.append(Token(Token.EOL))
        node.tokens = tokens
        return node

    def join_tokens(self, tokens):
        joined_tokens = []
        separator = Token(Token.SEPARATOR, self.formatting_config.separator)
        for token in tokens:
            joined_tokens.append(separator)
            joined_tokens.append(token)
        return joined_tokens

    @staticmethod
    def get_key(case_sensitive):
        return str if case_sensitive else str.casefold


import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

# region euclidean_distance
# todo: функция евклидова расстояния между двумя точками где x1(x,y) x2(x,y)
def euclidean_distance(x1, x2):
    return np.sqrt(np.sum((x1 - x2) ** 2))

# endregion

class KMeans:
    # K - кол-во кластеров max_iters - максимальное кол-во иттераций для оптимизации
    def __init__(self, K=2, max_iters=100, plot_steps=False, GlobalItter=1):
        # K - кол-во кластеров
        self.K = K
        self.max_iters = max_iters
        self.plot_steps = plot_steps
        self.GlobalItter = GlobalItter
        self.Distance = 0

        # Список списков индексов для каждого кластера для 3х: [[], [], []]
        self.clusters = [[] for _ in range(self.K)]
        # print("__init__ self.clusters", self.clusters)
        # средний вектор признаков для каждого кластера
        self.centroids = []

    def funcItter(self):
        Gi = self.GlobalItter
        print("funcItter: ", Gi)
        return Gi

    def predict(self, X):
        # todo: точки
        self.X = X
        # print("predict self.X", self.X)
        # число выборок и ко-во функций X.shape = кол-во точек
        self.n_samples, self.n_features = X.shape
        # print("self.n_samples", self.n_samples)

        # todo: Инициализация ценроидов
        # todo: рандомно инициализируем центроиды
        random_samples_idxs = np.random.choice(self.n_samples, self.K, replace=False)
        self.centroids = [self.X[idx] for idx in random_samples_idxs]
        # print("self.centroids", self.centroids)

        # Оптимизация (в диапазоне max itter)
        for _ in range(self.max_iters):
            self.GlobalItter = self.GlobalItter + _
            # print("self.GlobalItter: ", self.GlobalItter)
            if self.plot_steps:
                self.plot()
            # todo: Обновляем кластеры (отправляем 3 точки центроида)
            self.clusters = self._create_clusters(self.centroids)
            # todo: итог: получили индексы всех точек распределенных по ближайшим к ним кластерам
            # todo: при этом центроиды рандомны на первом шаге
            #  todo: строим снова график после обновления кластеров
            if self.plot_steps:
                self.plot()

            # todo: сохраняем старые точки(центроидов) в centroids_old
            centroids_old = self.centroids
            # todo: обновляем центроиды
            # todo: среднее значение кластеров к центроиду (для каждого кластера вычисляем среднее значения)
            # todo: отдаем полученные кластеры с индексами точек
            self.centroids = self._get_centroids(self.clusters)
            # todo: получаем новые координаты центроидов для наших кластеров
            # todo: строим снова график после обновления центроидов
            if self.plot_steps:
                self.plot()

            # todo: проверка сходимости расчитывает расстояние между каждым старым и новым центроидами для всех центроидов
            # todo: и проверяет равно ли это 0 если да выходим из цикла for
            if self._is_converged(centroids_old, self.centroids):
                # print("self.GlobalItter: ", self.GlobalItter)
                break
        # todo: вернуть метки кластера
        # print("predict -- self.clusters: ", self.clusters)
        # self.clusters это список индексов точек в self.K для каждого из кластеров
        # print("predict -- self._get_cluster_labels(self.clusters): \n", self._get_cluster_labels(self.clusters))
        return self._get_cluster_labels(self.clusters)

    def _get_cluster_labels(self, clusters):
        # todo: принимаем список точек для каждого из кластеров
        # todo: self.n_samples = кол-во точек
        # print("_get_cluster_labels -- self.n_samples ", self.n_samples)
        labels = np.empty(self.n_samples)
        # print("_get_cluster_labels -- labels np.empty \n", labels)
        # todo: для каждой выборки возвращаем кластер которому он был назначен
        for clusters_idx, cluster in enumerate(clusters):
            # print("_get_cluster_labels -- clusters_idx: ", clusters_idx)
            # print("_get_cluster_labels -- cluster: \n", cluster)
            # todo: проходимся по всем точкам в кластере
            for sample_idx in cluster:
                # print("_get_cluster_labels -- sample_idx: \n", sample_idx)
                # todo: присваиваем каждому индексу (пустого) массива labels  под индексом точки из кластера
                # todo: и присваиваем значение индекса(номер) кластера
                labels[sample_idx] = clusters_idx
            # print("_get_cluster_labels -- for labels: \n", labels)
        # todo: получаем массив из 200 элементов где каждое значение это индекс кластера
        # print("_get_cluster_labels -- return labels: \n", labels)
        return labels

    # todo: Создание кластеров
    def _create_clusters(self, centroids):
        clusters = [[] for _ in range(self.K)]
        # print("_create_clusters clusters ", clusters)

        # для индекса точки и точки перечислить точку из X
        for idx, sample in enumerate(self.X):
            # todo: индекс точки:
            # print("idx: ", idx)
            # todo: точка:
            # print("sample: ", sample)

            # todo: получить индекс ближайшего центроида к выбранной точке
            centroid_idx = self._closest_centroid(sample, centroids)
            # print("centroid_idx ", centroid_idx)
            # todo: добавляем к кластеру с центроидом centroid_idx индекс этой точки,
            # todo: где индекс кластера равен номеру(индексу) центроида
            clusters[centroid_idx].append(idx)
            # print("_create_clusters -- clusters 2 ", clusters)
        return clusters

    # todo: получить ближайший центроид к выбранной точке sample - точка, centroids - точка центроида
    def _closest_centroid(self, sample, centroids):
        # todo: расстояние от данной точки до каждого из (трех) центроида(ов)
        # _closest_centroid -- distances [5.066825296401784, 4.56377602701544, 1.454288868000231]
        # todo: получаем массив из расстояний
        distances = [euclidean_distance(sample, point) for point in centroids]
        # print("_closest_centroid -- distances", distances)
        # todo: получаем индекс с минимальным расстоянием (ближайший индекс равен numpy.argmin этих расстояний)
        closest_idx = np.argmin(distances)
        # т.к. index то с 0 начинается 1 2 => 1.454288868000231 min
        # _closest_centroid -- closest_idx 2
        # print("_closest_centroid -- closest_idx: ", closest_idx)
        return closest_idx

    # среднее значение кластеров к центроиду (для каждого кластера вычисляем среднее значения)
    def _get_centroids(self, clusters):
        # print("_get_centroids -- clusters: ", clusters)
        centroids = np.zeros((self.K, self.n_features))
        # print("_get_centroids -- self.K: ", self.K)
        # print("_get_centroids -- self.n_features: ", self.n_features)
        # print("_get_centroids -- centroids: ", centroids)
        for cluster_idx, cluster in enumerate(clusters):
            # todo: для каждого индекса выбираем кластер из массива
            # print("_get_centroids -- cluster_idx: ", cluster_idx)
            # print("_get_centroids -- cluster: ", cluster)

            # todo: назначаем текущему кластеру среднее арифметическое значение
            # print("_get_centroids -- self.X[cluster]: ", self.X[cluster])
            # todo: достаем каждую точку по индексу из кластера
            # todo: и считаем среднее арифметическое значение всех X и Y
            cluster_mean = np.mean(self.X[cluster], axis=0)
            # print("_get_centroids -- cluster_mean: ", cluster_mean)
            centroids[cluster_idx] = cluster_mean
            # print("_get_centroids -- centroids 3: ", centroids)
        # print("_get_centroids -- centroids  итог для return: ", centroids)
        # todo: получаем координаты новых центроидов в каждом кластере
        return centroids

    # todo: проверка сходимости
    def _is_converged(self, centroids_old, centroids):
        # todo: принимает координаты старых центроидов и новых
        # print("_is_converged -- centroids_old: \n", centroids_old)
        # print("_is_converged -- centroids: \n", centroids)
        # todo: координаты старого центроида первого кластера = x1
        # todo: координаты нового центроида первого кластера = x2
        distances = [euclidean_distance(centroids_old[i], centroids[i]) for i in range(self.K)]
        # todo: получаем расстояние между старым и новым центроидом в каждом кластере
        # print("_is_converged -- distances: ", distances)
        # print("_is_converged -- sum(distances): ", sum(distances))
        # todo: суммируем и проверяем равно ли нулю
        return sum(distances) == 0

    def plot(self):
        fig, ax = plt.subplots(figsize=(8, 4))
        # todo: перебираем кластеры
        for i, index in enumerate(self.clusters):
            # print("plot -- i: ", i)
            # print("plot -- index: ", index)
            # todo: получаем текущие точки равные собственной точке х
            point = self.X[index].T
            # print("plot -- self.X[index]: \n", self.X[index])
            # print("plot -- point: \n", self.X[index])
            # todo: рисуем все точки для каждого кластера свой цвет
            ax.scatter(*point)
            # todo: наносим все центроиды
        for point in self.centroids:
            ax.scatter(*point, marker='x', color="black", linewidth=2)
        plt.show()

    def Dist(self):
        # todo: получаем список координаты центроидов в каждом кластере
        # print("Dist -- self.centroids: ", self.centroids)
        # todo: получаем индексы точек i кластера
        for i, index in enumerate(self.clusters):
            # print("Dist -- i: ", i)
            # print("Dist -- list index: ", index)
            # print("Dist -- centroids: ", self.centroids[i])
            # todo: 2) сумма расстояний от точек до центроида в i кластере
            tempDistance = 0
            for j in index:
                # todo: для каждого индекса в i кластере
                # print("Dist -- j: ", j)
                # todo: получаем нужную точку
                # print("Dist -- self.X[j]: ", self.X[j])
                # todo: 1) получаем расстояние от точки до центроида в i кластере и суммируем с остальными
                tempDistance = tempDistance + euclidean_distance(self.X[j], self.centroids[i])
            # print("Dist -- tempDistance: ", tempDistance)
            self.Distance = self.Distance + tempDistance
        # print("Dist -- return self.Distance: ", self.Distance)
        return self.Distance

# todo: k means
if __name__ == '__main__':
    # todo: генерируем точки
    np.random.seed(42)
    X, y = make_blobs(200, random_state=0)
    # print(X.shape)
    print("кол-во точек: ", len(X))

    # default
    clusters = 3
    print("кол-во кластеров: ", clusters)
    GlobalItter = 1
    k = KMeans(K=clusters, max_iters=150, plot_steps=False, GlobalItter=GlobalItter)
    y_pred = k.predict(X)
    k.Dist()
    k.plot()

    # todo: 1) Определить число возможных кластеров
    # todo: от одного до два корня из кол-ва точек
    n = 2 * np.sqrt(len(X))
    Sum_of_distance = []
    # todo: приблизительное кол-во кластеров
    print(round(n))
    for i in range(1, round(n)):
        # todo: прогоняем по алгоритму с i кластероми
        temp_k = KMeans(K=i, max_iters=150, plot_steps=False, GlobalItter=GlobalItter)
        temp_y_pred = temp_k.predict(X)
        # todo: получаем значение суммы расстояний
        result_sum_klasters = temp_k.Dist()
        # print("result_sum_klasters: ", result_sum_klasters)
        # todo: 3.3) добавляем в список сумм
        Sum_of_distance.append(result_sum_klasters)
    print("Sum_of_distance: ", Sum_of_distance)

    # todo: 2) вычислить оптимальное значение кластеров
    Optimum_klasters = float('inf')
    print("k: ", Optimum_klasters)
    min = float('inf')
    print("min: ", min)
    for i in range(2, len(Sum_of_distance) - 1):
        tmp = abs(Sum_of_distance[i] - Sum_of_distance[i + 1]) / abs(Sum_of_distance[i - 1] - Sum_of_distance[i])
        if min > tmp:
            min = tmp
            Optimum_klasters = i
    print("Оптимальное кол-во кластеров: ", Optimum_klasters)

    # todo: считаем с оптимальным значением
    GlobalItter = 1
    t = KMeans(K=Optimum_klasters, max_iters=150, plot_steps=True, GlobalItter=GlobalItter)
    new_pred = t.predict(X)
    t.plot()


"""
Defaults for GlobalVars class

"""

__copyright__ = """
Copyright 2008 Sean Ross-Ross
"""
__license__ =  """
This file is part of SLIMpy .

SLIMpy is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SLIMpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
"""

import os
from os.path import curdir, join, sep, isfile, devnull, abspath
NULLOUT = open( devnull, 'w' )

DEFAULTS = {
            "verbose": 0,
            "debug" : ['cmd','err','stat'] ,
            "check_path" : False,
            "strict_check" : 2,
            "memsize":500 ,
#            "tmpdir": abspath( curdir ),
#            "ispipe":True,
            "runtype":'normal' ,
            "globaltmpdir": abspath( curdir ),
            "localtmpdir": join( sep, 'var', 'tmp' ),
            'datapath': abspath( os.environ.get( 'DATAPATH', abspath( curdir ) ) ),

            "mpi": False,
            "MPIFLAGS": "${MPIRUN} -np ${np} -hostfile ${NODEFILE}",
            "NODEFILE" : None,
            "NODELIST" : [],
            "nodemap_type":'lin' ,

            "MPIRUN" :  "mpirun_ssh",
            "np" :  1 ,
            "rsh":  'ssh',
            "eps":0,
            "nwin": 0,
            "abridge":True,
            "test_devel":False,
            'abridgeMap' : {abspath( curdir ):'.'},
            'walltime':30,
            'no_del':False,
            'logfile':None,

            'keep_tb_info': True,
            'show_std_err': False,
            'use_scalar_obj': True,

            'sync_disk_om_rm': True,
            'SYNC_CMD': 'sync',

            'run_again':False,

            }

tmpPath = join( sep, 'var', 'tmp' )
PBS_JOBID = os.environ.get( 'PBS_JOBID', '' )

PBS_JOBID = join( tmpPath, PBS_JOBID )

if os.access( PBS_JOBID, os.W_OK | os.R_OK ):
    DEFAULTS['tmpdatapath'] = abspath( PBS_JOBID )
else:
    DEFAULTS['tmpdatapath'] = abspath( curdir )

SYS_DEFAULTS = DEFAULTS.copy()

def rc_update_defaults():
    """
    try and get the defaults from the SLIMpy rcfile
    """

    SLIMPY_RC = os.environ.get( 'SLIMPY_RC', None )
    HOME = os.environ.get( 'HOME', None )

    if SLIMPY_RC is None:
        if isfile( '.slimpy_rc' ):
            SLIMPY_RC = '.slimpy_rc'
        elif isfile( join( HOME, '.slimpy_rc' ) ):
            SLIMPY_RC = join( HOME, '.slimpy_rc' )

    if SLIMPY_RC is not None:
        rc_globals = {}
        execfile( SLIMPY_RC, rc_globals )

        try:
            DEFAULTS.update( rc_globals['DEFAULTS'] )
        except KeyError:
            raise KeyError( "slimpy rcfile must contain 'DEFAULTS' attribute" )

rc_update_defaults()


from math import ceil
import sys

def syn_alts(reference_vcf):
	ref_col = None
	alt_col = None

	# create mapping from sequence to allele for each position in reference vcf
	seq_to_idx = dict()
	idx_to_seq = dict()
	with open(reference_vcf, "r") as f:
		for line in f.read().splitlines():
			if len(line) < 2 or line[0:2] == "##":
				continue

			sp = line.split("\t")
			if line[0] == "#":
				for i in range(len(sp)):
					if sp[i] == "REF":
						ref_col = i
						continue
					if sp[i] == "ALT":
						alt_col = i
						continue
				continue

			pos = int(sp[1])
			pos_list = [sp[ref_col]]
			pos_dict = dict()
			pos_dict[sp[ref_col]] = 0
			for i, alt in enumerate(sp[alt_col].split(",")):
				pos_list.append(alt)
				pos_dict[alt] = i+1
			idx_to_seq[pos] = pos_list
			seq_to_idx[pos] = pos_dict

	# rename alleles in input vcf
	ref_col = None
	alt_col = None
	format_col = None
	for line in sys.stdin:
		if len(line) < 2:
			sys.stdout.write(line)
			continue

		if line[:2] == "##":
			sys.stdout.write(line)
			continue

		sp = line.split("\t")
		if line[0] == "#":
			for i in range(len(sp)):
				if sp[i] == "REF":
					ref_col = i
					continue
				if sp[i] == "ALT":
					alt_col = i
					continue
				if sp[i] == "FORMAT":
					format_col = i
					continue
			sys.stdout.write(line)
			continue

		pos = int(sp[1])
		if pos not in seq_to_idx:
			sys.stdout.write(line)
			continue

		# create name map
		alleles = [sp[ref_col]] + sp[alt_col].split(",")
		assert len(alleles) >= 2

		new_alleles = 0
		require_rewrite = False
		name_map = dict() # maps old name to new name
		for i, allele in enumerate(alleles):
			if allele not in seq_to_idx[pos]:
				seq_to_idx[pos][allele] = len(seq_to_idx[pos])
				idx_to_seq[pos].append(allele)
			name_map[i] = seq_to_idx[pos][allele]
			if name_map[i] != i:
				require_rewrite = True

		if not require_rewrite:
			sys.stdout.write(line)
			continue

		# rewrite alleles
		line_out = "\t".join(sp[:ref_col]) + "\t"
		line_out += idx_to_seq[pos][0] + "\t"
		if alt_col - ref_col > 1:
			line_out += "\t".join(sp[ref_col+1:alt_col]) + "\t"
		line_out += ",".join([idx_to_seq[pos][i] for i in range(1, len(idx_to_seq[pos]))]) + "\t"
		line_out += "\t".join(sp[alt_col+1:format_col+1]) + "\t"

		# update genotypes/phasings
		gt_col = -1
		sp2 = sp[format_col].split(":")
		for i in range(len(sp2)):
			if sp2[i] == "GT":
				gt_col = i
				break

		if gt_col < 0:
			sys.stdout.write(line_out + "\t".join(sp[formal_col+1:]))
			continue

		for sampleseq in sp[format_col+1:]:
			sp2 = sampleseq.split(":")
			gt = sp2[gt_col]
			sep = "/" if "/" in gt else "|"
			p = gt.split(sep)
			sp2[gt_col] = sep.join([str(name_map[int(a)]) for a in p])
			line_out += ":".join(sp2) + "\t"

		sys.stdout.write(line_out + "\n")

def main(argv):
	syn_alts(argv[1])

if __name__ == "__main__":
	main(sys.argv)