#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 13 16:47:59 2020

@author: nathanyu
"""

import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

class TreeNode():
    """树结点"""
    def __init__(self, feature_idx=None, feature_val=None, feature_name=None, node_val=None, child=None):
        """
        feature_idx:
            该结点对应的划分特征索引
        feature_val:
            划分特征对应的值
        feature_name:
            划分特征名
        node_val:
            该结点存储的值，**只有叶结点才存储类别**
        child:
            子树
        """
        self._feature_idx = feature_idx
        self._feature_val = feature_val
        self._feature_name = feature_name
        # 叶结点存储类别
        self._node_val = node_val
        # 非叶结点存储划分信息
        self._child = child

"""决策树算法Scratch实现"""
class DecisionTreeScratch():
    def __init__(self, feature_name, etype="gain", epsilon=0.01):
        """
        feature_name:
            每列特征名
        etype:
            可取值有
            gain: 使用信息增益
            ratio: 使用信息增益比
        epsilon:
            当信息增益或信息增益比小于该阈值时直接把对应结点作为叶结点
        """
        self._root = None
        self._fea_name = feature_name
        self._etype = etype
        self._epsilon = epsilon

    def fit(self, X, y):
        """
        模型训练
        X:
            训练集，每一行表示一个样本，每一列表示一个特征或属性
        y:
            训练集标签
        """
        self._root = self._build_tree(X, y)

    def predict(self, x):
        """给定输入样本，预测其类别"""
        return self._predict(x, self._root)

    def _build_tree(self, X, y):
        """
        构建树
        X:
            用于构建子树的数据集
        y:
            X对应的标签
        """
        # 最后只剩下一个特征时将该子树置为叶结点
        if X.shape[1] == 1:
            node_val = self._vote_label(y)
            return TreeNode(node_val=node_val)

        # 子树只剩下一个类别时直接置为叶结点
        if np.unique(y).shape[0] == 1:
            return TreeNode(node_val=y[0])

        n_feature = X.shape[1]
        # 最大信息增益或信息增益比
        max_gain = -np.inf
        # 信息增益最大或信息增益比最大的特征索引
        max_fea_idx = 0
        # 遍历每一个特征
        for i in range(n_feature):
            if self._etype == "gain":
                gain = self._calc_gain(X[:, i], y)
            else:
                gain = self._calc_gain_ration(X[:, i], y)
            if gain > max_gain:
                max_gain = gain
                max_fea_idx = i

        # 如果以该特征进行划分信息增益或增益比太小则不进行划分
        if max_gain < self._epsilon:
            node_val = self._vote_label(y)
            return TreeNode(node_val=node_val)

        feature_name = self._fea_name[max_fea_idx]
        child_tree = dict()
        # 遍历所选特征每一个可能的值，对每一个值构建子树
        feature_val = np.unique(X[:, max_fea_idx])
        for fea_val in feature_val:
            # 如果要对连续型数据进行划分，可将此处的==换成<=，类似CART
            child_X = X[X[:, max_fea_idx] == fea_val]
            child_y = y[X[:, max_fea_idx] == fea_val]
            child_X = np.delete(child_X, max_fea_idx, 1)
            # 构建子树
            child_tree[fea_val] = self._build_tree(child_X, child_y)
        return TreeNode(max_fea_idx, feature_name=feature_name, child=child_tree)

    def _predict(self, x, tree=None):
        """
        给定输入样本，将其划分到所属叶结点
        """
        # 根结点
        if tree is None:
            tree = self._root

        # 叶结点直接返回类别
        if tree._node_val is not None:
            return tree._node_val

        fea_idx = tree._feature_idx
        for fea_val, child_node in tree._child.items():
            if x[fea_idx] == fea_val:
                # 如果是叶结点直接返回类别
                if child_node._node_val is not None:
                    return child_node._node_val
                else:
                    # 否则继续去子树中找
                    return self._predict(x, child_node)

    def _vote_label(self, y):
        """统计y中次数出现最多的类别"""
        # 计算每个标签及其出现次数
        label, num_label = np.unique(y, return_counts=True)
        # 返回出现次数最多的类别作为叶结点类别
        return label[np.argmax(num_label)]

    def _calc_entropy(self, y):
        """
        计算熵
        y:
            数据集标签
        """
        entropy = 0
        # 计算每个类别的数量
        _, num_ck = np.unique(y, return_counts=True)
        for n in num_ck:
            p = n / y.shape[0]
            entropy -= p * np.log2(p)
        return entropy

    def _calc_condition_entropy(self, x, y):
        """
        计算条件熵
        x:
            数据集的某个特征对应的列向量，训练集中一行表示一个样本，列表示特征
        y:
            数据集标签
        """
        cond_entropy = 0
        # 计算特征x可能的取值及对应出现的次数
        xval, num_x = np.unique(x, return_counts=True)
        # 遍历该特征的每个值
        for v, n in zip(xval, num_x):
            # 该值所对应的划分
            y_sub = y[x == v]
            sub_entropy = self._calc_entropy(y_sub)
            p = n / y.shape[0]
            cond_entropy += p * sub_entropy
        return cond_entropy

    def _calc_gain(self, x, y):
        """
        计算信息增益
        x:
            数据集的某个特征对应的列向量，训练集中一行表示一个样本，列表示特征
        y:
            数据集标签
        """
        return self._calc_entropy(y) - self._calc_condition_entropy(x, y)

    def _calc_gain_ration(self, x, y):
        """
        计算信息增益比
        x:
            数据集的某个特征对应的列向量，训练集中一行表示一个样本，列表示特征
        y:
            数据集标签
        """
        return self._calc_gain(x, y) / self._calc_entropy(x)

def main():

    dataset = load_iris()
    xtrain, _, ytrain, _ = train_test_split(dataset.data, dataset.target, train_size=0.8, shuffle=True)
    feature_name = dataset.feature_names

    model = DecisionTreeScratch(feature_name, "gain", 0.1)
    model.fit(xtrain, ytrain)

    n_train = xtrain.shape[0]
    n_right = 0
    for i in range(n_train):
        y_pred = model.predict(xtrain[i])
        if y_pred == ytrain[i]:
            n_right += 1
        else:
            print("该样本真实标签为：{}，但是Scratch模型预测标签为：{}".format(ytrain[i], y_pred))
    print("DecisionTree模型在训练集上的准确率为：{}%".format(n_right * 100 / n_train))

if __name__ == "__main__":
    main()


#!/usr/bin/env python
import os

################################################################################
# This script was used to update my regex line that parses an applications     #
# MacUpdate page for the version number.                                       #
################################################################################

def update_files():
    location = "/Users/bryan/Documents/bitbucket/python/installs/"
    old = """m = re.compile(r'(.*?)').search(html_read)"""
    new = """m = re.search(r'<meta name="twitter:data2" content="(.*)">', html_read)"""

    file_list = get_file_list(location)
    files_to_update = check_files(file_list, location, old)

    for _ in files_to_update:
        read_file = open(location+_, 'r')
        data = read_file.read()
        read_file.close()
        replacement_data = data.replace(old, new)
        write_file = open(location+_, 'w')
        write_file.write(replacement_data)
        write_file.close()
        print(_)

def get_file_list(location):
    file_list = []
    list = os.listdir(location)
    for _ in list:
        if ".py" in _:
            file_list.append(_)
    return(file_list)

def check_files(file_list, location, old):
    files_to_update = []
    for _ in file_list:
        read_file = open(location+_, 'r')
        data = read_file.readlines()
        read_file.close()
        for L in data:
            if old in L:
                files_to_update.append(_)
    return(files_to_update)

update_files()

python/modules/macupdate version parse fix.py


import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

inString = 'PsrAntiIn150nm.npy'
outString = 'PsrAntiOut150nm.npy'

frequencies = np.arange(0, 50, 0.05)
Psr1 = np.load(inString)
Psr2 = np.load(outString)
Psr = Psr1 + Psr2

omegas = np.load('Omegas.npy')

for omega in omegas:
    f = np.real(omega)
    idx = (np.abs(frequencies - f)).argmin()
    plt.plot(frequencies[idx], Psr[idx], "r.")

plt.plot(frequencies, Psr)
plt.xlabel("Frequency (GHz)")
plt.ylabel(r"$P_{\mathrm{sr}}$ (A.U.)")
plt.semilogy()
plt.savefig("CombinedHighQuality.png", dpi=1000)


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def sortedArrayToBST(self, nums) -> TreeNode:
        if len(nums) == 0:
            return None
        mid = (len(nums)-1)//2
        root = TreeNode(nums[mid])
        if mid >0:
            root.left = self.sortedArrayToBST(nums[:mid])
        else:
            root.left = None
        if mid+1<len(nums):
            root.right = self.sortedArrayToBST(nums[mid+1:])
        else:
            root.right = None
        return root

    def levelOrder(self, root: TreeNode):
        from collections import deque
        res = deque()
        if root is None:
            return
        deq = deque()
        deq.append(root)

        while deq:
            t = len(deq)
            tmp = []
            for i in range(t):
                first = deq.popleft()
                tmp.append(first.val)
                if first.left is not None:
                    deq.append(first.left)
                if first.right is not None:
                    deq.append(first.right)
            res.append(tmp)
        return list(res)

s = Solution()
root = s.sortedArrayToBST([-10,-3,0,5,9])
print(s.levelOrder(root))


import logging
log = logging.getLogger(__name__)
import numpy as np
from copy import deepcopy
from collections import OrderedDict

from qcodes.instrument.parameter import (
    ManualParameter, InstrumentRefParameter)
from qcodes.utils import validators as vals
from pycqed.instrument_drivers.instrument import Instrument

from pycqed.measurement import sweep_functions as swf

class MeasurementObject(Instrument):
    _acq_weights_type_aliases = {}  # see self.get_acq_weights_type()
    _ro_pulse_type_vals = ['GaussFilteredCosIQPulse',
                           'GaussFilteredCosIQPulseMultiChromatic']
    _allowed_drive_modes = [None]

    def __init__(self, name, **kw):
        super().__init__(name, **kw)

        self.msmt_suffix = '_' + name  # used to append to measurement labels

        self._operations = {}
        self.add_parameter('operations',
                           docstring='a list of all operations available on '
                                     'the measurement object',
                           get_cmd=self._get_operations)

        self.add_parameter('instr_mc',
            parameter_class=InstrumentRefParameter)
        self.add_parameter('instr_pulsar',
            parameter_class=InstrumentRefParameter)
        self.add_parameter('instr_acq',
            parameter_class=InstrumentRefParameter)
        self.add_parameter('instr_ro_lo',
            parameter_class=InstrumentRefParameter,
            vals=vals.MultiType(vals.Enum(None), vals.Strings()))
        self.add_parameter('instr_trigger',
            parameter_class=InstrumentRefParameter)
        self.add_parameter('instr_switch',
            parameter_class=InstrumentRefParameter,
            vals=vals.MultiType(vals.Enum(None), vals.Strings()))

        # acquisition parameters
        self.add_parameter('acq_unit', initial_value=0,
                           vals=vals.Enum(0, 1, 2, 3),
                           docstring='Acquisition device unit (only one for '
                                     'UHFQA and up to 4 for SHFQA).',
                           parameter_class=ManualParameter)
        self.add_parameter('acq_I_channel', initial_value=0,
                           vals=vals.Ints(min_value=0),
                           parameter_class=ManualParameter)
        self.add_parameter('acq_Q_channel', initial_value=1,
                           vals=vals.Ints(min_value=0),
                           parameter_class=ManualParameter)
        self.add_parameter('acq_averages', initial_value=1024,
                           vals=vals.Ints(0, 1000000),
                           parameter_class=ManualParameter)
        self.add_parameter('acq_shots', initial_value=4094,
                           docstring='Number of single shot measurements to do'
                                     'in single shot experiments.',
                           vals=vals.Ints(0, 1048576**2),
                           parameter_class=ManualParameter)
        self.add_parameter('acq_length', initial_value=2.2e-6,
                           vals=vals.Numbers(min_value=1e-8,
                                             max_value=100e-6),
                           parameter_class=ManualParameter)
        awt_docstring = 'Determines what type of integration weights to ' +\
                        'use:\n\tSSB: Single sideband demodulation\n\tDSB: ' +\
                        'Double sideband demodulation\n\tcustom: waveforms ' +\
                        'specified in "acq_weights_I" and "acq_weights_Q"' +\
                        '\n\tcustom_2D: waveforms specified in ' +\
                        '"acq_weights_I/I2" and "acq_weights_Q/Q2"\n\t' +\
                        'square_rot: uses a single integration channel with ' +\
                        'boxcar weights\n\tmanual: keeps the weights ' +\
                        'already set in the acquisition device' +\
                        ''.join([f'\n\t{k}: alias for {v}' for k, v
                                 in self._acq_weights_type_aliases.items()])
        self.add_parameter('acq_weights_type', parameter_class=ManualParameter,
                           vals=vals.Enum(
                               'DSB', 'SSB', 'DSB2', 'square_rot', 'manual',
                               'custom', 'custom_2D',
                               *list(self._acq_weights_type_aliases)),
                           initial_value='SSB',
                           docstring=awt_docstring)
        self.add_parameter('acq_weights_I', vals=vals.Arrays(),
                           label='Optimized weights for I channel',
                           parameter_class=ManualParameter)
        self.add_parameter('acq_weights_Q', vals=vals.Arrays(),
                           label='Optimized weights for Q channel',
                           parameter_class=ManualParameter)
        self.add_parameter('acq_weights_I2', vals=vals.Arrays(),
                           label='Optimized weights for second integration '
                                 'channel I',
                           docstring=("Used for double weighted integration "
                                      "during qutrit readout"),
                           parameter_class=ManualParameter)
        self.add_parameter('acq_weights_Q2', vals=vals.Arrays(),
                           label='Optimized weights for second integration '
                                 'channel Q',
                           docstring=("Used for double weighted integration "
                                      "during qutrit readout"),
                           parameter_class=ManualParameter)
        self.add_parameter('acq_IQ_angle', initial_value=0,
                           docstring='The phase of the integration weights '
                                     'when using SSB, DSB or square_rot '
                                     'integration weights',
                                     label='Acquisition IQ angle', unit='rad',
                           parameter_class=ManualParameter)

        # readout pulse parameters
        self.add_parameter(
            'ro_fixed_lo_freq', unit='Hz',
            set_cmd=lambda f, s=self: s.configure_mod_freqs(
                'ro', ro_fixed_lo_freq=f),
            docstring='Fix the ro LO to a single frequency or to a set of '
                      'allowed frequencies. For allowed options, see the '
                      'argument fixed_lo in the docstring of '
                      'get_closest_lo_freq.')
        self.add_parameter(
            'ro_freq', unit='Hz',
            set_cmd=lambda f, s=self: s.configure_mod_freqs('ro', ro_freq=f),
            label='Readout frequency')
        self.add_parameter('ro_I_offset', unit='V', initial_value=0,
                           parameter_class=ManualParameter,
                           label='DC offset for the readout I channel')
        self.add_parameter('ro_Q_offset', unit='V', initial_value=0,
                           parameter_class=ManualParameter,
                           label='DC offset for the readout Q channel')
        self.add_parameter('ro_lo_power', unit='dBm',
                           parameter_class=ManualParameter,
                           label='Readout pulse upconversion mixer LO power')
        self.add_operation('RO')
        self.add_pulse_parameter('RO', 'ro_pulse_type', 'pulse_type',
                                 vals=vals.Enum(*self._ro_pulse_type_vals),
                                 initial_value='GaussFilteredCosIQPulse')
        self.add_pulse_parameter('RO', 'ro_I_channel', 'I_channel',
                                 initial_value=None, vals=vals.Strings())
        self.add_pulse_parameter('RO', 'ro_Q_channel', 'Q_channel',
                                 initial_value=None, vals=vals.MultiType(
                                     vals.Enum(None), vals.Strings()))
        self.add_pulse_parameter('RO', 'ro_amp', 'amplitude',
                                 initial_value=0.001,
                                 vals=vals.MultiType(vals.Numbers(), vals.Lists()))
        self.add_pulse_parameter('RO', 'ro_length', 'pulse_length',
                                 initial_value=2e-6, vals=vals.Numbers())
        self.add_pulse_parameter('RO', 'ro_delay', 'pulse_delay',
                                 initial_value=0, vals=vals.Numbers())
        self.add_pulse_parameter(
            'RO', 'ro_mod_freq', 'mod_frequency', initial_value=100e6,
            set_parser=lambda f, s=self: s.configure_mod_freqs('ro',
                                                               ro_mod_freq=f),
            vals=vals.MultiType(vals.Numbers(), vals.Lists()))
        self.add_pulse_parameter('RO', 'ro_phase', 'phase',
                                 initial_value=0,
                                 vals=vals.MultiType(vals.Numbers(), vals.Lists()))
        self.add_pulse_parameter('RO', 'ro_phi_skew', 'phi_skew',
                                 initial_value=0,
                                 vals=vals.MultiType(vals.Numbers(), vals.Lists()))
        self.add_pulse_parameter('RO', 'ro_alpha', 'alpha',
                                 initial_value=1,
                                 vals=vals.MultiType(vals.Numbers(), vals.Lists()))
        self.add_pulse_parameter('RO', 'ro_sigma',
                                 'gaussian_filter_sigma',
                                 initial_value=10e-9, vals=vals.Numbers())
        self.add_pulse_parameter('RO', 'ro_buffer_length_start', 'buffer_length_start',
                                 initial_value=10e-9, vals=vals.Numbers())
        self.add_pulse_parameter('RO', 'ro_buffer_length_end', 'buffer_length_end',
                                 initial_value=10e-9, vals=vals.Numbers())
        self.add_pulse_parameter('RO', 'ro_phase_lock', 'phase_lock',
                                 initial_value=False, vals=vals.Bool())
        self.add_pulse_parameter(
            'RO', 'ro_trigger_channels', 'trigger_channels',
            vals=vals.MultiType(vals.Enum(None), vals.Strings(),
                                vals.Lists(vals.Strings())))
        self.add_pulse_parameter(
            'RO', 'ro_trigger_pars', 'trigger_pars',
            vals=vals.MultiType(vals.Enum(None), vals.Dict()))

        # switch parameters
        DEFAULT_SWITCH_MODES = OrderedDict({'default': {}})
        self.add_parameter(
            'switch_modes', parameter_class=ManualParameter,
            initial_value=DEFAULT_SWITCH_MODES, vals=vals.Dict(),
            docstring=
            "A dictionary whose keys are identifiers of switch modes and "
            "whose values are dicts understood by the set_switch method of "
            "the SwitchControls instrument specified in the parameter "
            "instr_switch.")

    def _get_operations(self):
        return self._operations

    def add_operation(self, operation_name):
        self._operations[operation_name] = {}

    def add_pulse_parameter(self,
                            operation_name,
                            parameter_name,
                            argument_name,
                            initial_value=None,
                            vals=vals.Numbers(),
                            **kwargs):
        """
        Add a pulse parameter to the measurement object.

        Args:
            operation_name (str): The operation of which this parameter is an
                argument. e.g. mw_control or CZ
            parameter_name (str): Name of the parameter
            argument_name  (str): Name of the arugment as used in the sequencer
            **kwargs get passed to the add_parameter function
        Raises:
            KeyError: if this instrument already has a parameter with this
                name.
        """
        if parameter_name in self.parameters:
            raise KeyError(
                'Duplicate parameter name {}'.format(parameter_name))

        if operation_name in self.operations().keys():
            self._operations[operation_name][argument_name] = parameter_name
        else:
            raise KeyError(
                'Unknown operation {}, add '.format(operation_name) +
                'first using add operation')

        self.add_parameter(parameter_name,
                           initial_value=initial_value,
                           vals=vals,
                           parameter_class=ManualParameter, **kwargs)

        # for use in RemoteInstruments to add parameters to the server
        # we return the info they need to construct their proxy
        return

    def get_acq_weights_type(self):
        """Returns the value of acq_weights_type after resolving alias names

        Alias names are specified in self._acq_weights_type_aliases.
        """
        wt = self.acq_weights_type()
        return self._acq_weights_type_aliases.get(wt, wt)

    def get_acq_int_channels(self, n_channels=None):
        """Get a list of tuples with the integration channels.

        Args:
            n_channels (int): number of integration channels; if this is None,
                it will be chosen as follows (after resolving aliases according
                to self._acq_weights_type_aliases):
                - 2 for acq_weights_type in ['SSB', 'DSB', 'DSB2',
                    'custom_2D', 'manual']
                - 1 otherwise (in particular for acq_weights_type in
                    ['custom', 'square_rot'])

        Returns
            list with n_channels tuples, where the first entry in each tuple is
            the acq_unit and the second is an integration channel index
        """
        if n_channels is None:
            n_channels = 2 if (self.get_acq_weights_type() in [
                'SSB', 'DSB', 'DSB2', 'custom_2D', 'manual']
                               and self.acq_Q_channel() is not None) else 1
        return [(self.acq_unit(), self.acq_I_channel()),
                (self.acq_unit(), self.acq_Q_channel())][:n_channels]

    def get_acq_inp_channels(self):
        """Get a list of tuples with the acquisition input channels.

        For now, this method assumes that all quadratures available on the
        acquisition unit should be recorded, i.e., two for devices that
        provide I&Q signals, and one otherwise.

        TODO: In the future, a parameter could be added to the measurement
        object to allow recording only one out of two available quadratures.

        Returns
            list of tuples, where the first entry in each tuple is
            the acq_unit and the second is an input channel index
        """
        n_channels = self.instr_acq.get_instr().n_acq_inp_channels
        return [(self.acq_unit(), i) for i in range(n_channels)]

    def get_ro_lo_freq(self):
        """Returns the required local oscillator frequency for readout pulses

        The RO LO freq is calculated from self.ro_mod_freq (intermediate
        frequency) and self.ro_freq.
        """
        # in case of multichromatic readout, take first ro freq, else just
        # wrap the frequency in a list and take the first
        if np.ndim(self.ro_freq()) == 0:
            ro_freq = [self.ro_freq()]
        else:
            ro_freq = self.ro_freq()
        if np.ndim(self.ro_mod_freq()) == 0:
            ro_mod_freq = [self.ro_mod_freq()]
        else:
            ro_mod_freq = self.ro_mod_freq()
        return ro_freq[0] - ro_mod_freq[0]

    def prepare(self, drive=None, switch='modulated'):
        """Prepare instruments for a measurement involving this measurement
        object.

        The preparation includes:
        - configure readout local oscillators
        - call set_readout_weights
        - set switches to the mode required for the measurement

        Args:
            drive (str, None): the kind of drive to be applied (not
                implemented here, to be extended by child classes)
            switch (str): the required switch mode, see the docstring of
            switch_modes
        """
        if drive not in self._allowed_drive_modes:
            raise NotImplementedError(f"Drive mode {drive} not implemented!")

        self.configure_mod_freqs()
        ro_lo = self.instr_ro_lo

        # configure readout local oscillators
        ro_lo_freq = self.get_ro_lo_freq()

        if ro_lo() is not None:  # configure external LO
            if self.ro_Q_channel() is not None:
                # We are on a setup that generates RO pulses by upconverting
                # IQ signals with a continuously running LO, so we switch off
                # gating of the MWG.
                ro_lo.get_instr().pulsemod_state('Off')
            ro_lo.get_instr().power(self.ro_lo_power())
            ro_lo.get_instr().frequency(ro_lo_freq)
            ro_lo.get_instr().on()
        # Provide the ro_lo_freq to the acquisition device to allow
        # configuring an internal LO if needed.
        self.instr_acq.get_instr().set_lo_freq(self.acq_unit(), ro_lo_freq)

        # other preparations
        self.set_readout_weights()
        # set switches to the mode required for the measurement
        self.set_switch(switch)

    def _get_custom_readout_weights(self):
        return dict(
            weights_I=[self.acq_weights_I(), self.acq_weights_I2()],
            weights_Q=[self.acq_weights_Q(), self.acq_weights_Q2()],
        )

    def set_readout_weights(self, weights_type=None, f_mod=None):
        """Set acquisition weights for this measurement object in the
        acquisition device.

        Depending on the weights type, some of the following qcodes
        parameters can have an influence on the programmed weigths (see the
        docstrings of these parameters and of
        AcquisitionDevice._acquisition_generate_weights):
        - instr_acq, acq_unit, acq_I_channel, acq_Q_channel
        - acq_weights_type (if not overridden with the arg weights_type)
        - ro_mod_freq (if not overridden with the arg f_mod)
        - acq_IQ_angle
        - acq_weights_I, acq_weights_I2, acq_weights_Q, acq_weights_Q2

        Args:
            weights_type (str, None): a weights_type understood by
                AcquisitionDevice._acquisition_generate_weights, or the
                default None, in which case the qcodes parameter
                acq_weights_type is used.
            f_mod (float, None): The intermediate frequency of the signal to
                be acquired, or the default None, in which case the qcodes
                parameter ro_mod_freq is used.
        """
        if weights_type is None:
            weights_type = self.get_acq_weights_type()
        if f_mod is None:
            f_mod = self.ro_mod_freq()
        self.instr_acq.get_instr().acquisition_set_weights(
            channels=self.get_acq_int_channels(n_channels=2),
            weights_type=weights_type, mod_freq=f_mod,
            acq_IQ_angle=self.acq_IQ_angle(),
            acq_length=self.acq_length(),
            **self._get_custom_readout_weights()
        )

    def set_switch(self, switch_mode=None):
        """
        Sets the switch control (given in the qcodes parameter instr_switch)
        to the given mode.

        :param switch_mode: (str) the name of a switch mode that is defined in
            the qcodes parameter self.switch_modes. If None, uses the first
            entry of self.switch_modes.
            See the docstring of switch_modes for more details.
        """
        if self.instr_switch() is None:
            return
        if switch_mode is None:
            switch_mode = list(self.switch_modes())[0]
        switch = self.instr_switch.get_instr()
        mode = self.switch_modes().get(switch_mode, None)
        if mode is None:
            log.warning(f'Switch mode {switch_mode} not configured for '
                        f'{self.name}.')
        else:
            switch.set_switch(mode)

    def get_operation_dict(self, operation_dict=None):
        self.configure_mod_freqs()
        if operation_dict is None:
            operation_dict = {}

        for op_name, op in self.operations().items():
            operation_dict[op_name + ' ' + self.name] = {}
            for argument_name, parameter_name in op.items():
                operation_dict[op_name + ' ' + self.name][argument_name] = \
                    self.get(parameter_name)

        operation_dict['RO ' + self.name]['operation_type'] = 'RO'
        operation_dict['Acq ' + self.name] = deepcopy(
            operation_dict['RO ' + self.name])
        operation_dict['Acq ' + self.name]['amplitude'] = 0

        if np.ndim(self.ro_freq()) != 0:
            delta_freqs = np.diff(self.ro_freq(), prepend=self.ro_freq()[0])
            mods = [self.ro_mod_freq() + d for d in delta_freqs]
            operation_dict['RO ' + self.name]['mod_frequency'] = mods

        for code, op in operation_dict.items():
            op['op_code'] = code
        return operation_dict

    def swf_ro_freq_lo(self, bare=False):
        """Create a sweep function for sweeping the readout frequency.

        The sweep is implemented as an LO sweep in case of an acquisition
        device with an external LO. The implementation depends on the
        get_lo_sweep_function method of the acquisition device in case of an
        internal LO (note that it might be an IF sweep or a combined LO and
        IF sweep in that case.)

        Args:
            bare (bool): return the bare LO freq swf without any automatic
                offsets applied. Defaults to False.

        Returns: the Sweep_function object
        """
        if self.instr_ro_lo() is not None:  # external LO
            if bare:
                return swf.mc_parameter_wrapper.wrap_par_to_swf(
                    self.instr_ro_lo.get_instr().frequency)
            else:
                return swf.Offset_Sweep(
                    self.instr_ro_lo.get_instr().frequency,
                    -self.ro_mod_freq(),
                    name='Readout frequency',
                    parameter_name='Readout frequency')
        else:  # no external LO
            return self.instr_acq.get_instr().get_lo_sweep_function(
                self.acq_unit(),
                0 if (self.ro_fixed_lo_freq() or bare) else self.ro_mod_freq(),
                get_closest_lo_freq=(lambda f, s=self:
                                     s.get_closest_lo_freq(f, operation='ro')))

    def swf_ro_mod_freq(self):
        return swf.Offset_Sweep(
            self.ro_mod_freq,
            self.instr_ro_lo.get_instr().frequency(),
            name='Readout frequency',
            parameter_name='Readout frequency')

    def get_closest_lo_freq(self, target_lo_freq, fixed_lo='default',
                            operation=None):
        """Get the closest allowed LO freq for given target LO freq.

        Args:
            target_lo_freq (float): the target Lo freq
            fixed_lo: specification of the allowed LO freq(s), can be:
                - None: no restrictions on the LO freq
                - float: LO fixed to a single freq
                - str: (operation must be provided in this case)
                    - 'default' (default value): use the setting in the
                        measurement object
                    - a qb name to indicated that the LO must be fixed to be
                      the same as for that qb.
                - dict with (a subset of) the following keys:
                    'min' and/or 'max': minimal/maximal allowed LO freq
                    'step': LO fixed to a grid with this step width (grid
                            starting at 'min' if provided and at 0 otherwise)
                - list, np.array: LO fixed to be one of the listed values
            operation (str): the operation for which the LO freq is to be
                determined (e.g., 'ge', 'ro'). Only needed if fixed_lo is a str.

        Returns:
            The allowed LO freq that most closely matches the target
            combination of RF and IF.

        Examples:
            >>> freq, mod_freq = 5898765432, 150e6
            >>> target_lo_freq = freq - mod_freq
            >>> qb.get_closest_lo_freq(target_lo_freq, 'qb1', 'ge')
            >>> qb.get_closest_lo_freq(target_lo_freq, 5.8e9)
            >>> qb.get_closest_lo_freq(
            >>>     target_lo_freq, np.arange(4e9, 6e9 + 1e6, 1e6))
            >>> qb.get_closest_lo_freq(target_lo_freq, {'step': 100e6})
            >>> qb.get_closest_lo_freq(
            >>>     target_lo_freq, {'min': 5.4e9, 'max': 5.6e9})
            >>> qb.get_closest_lo_freq(
            >>>     target_lo_freq, {'min': 6.3e9, 'max': 6.9e9})
            >>> qb.get_closest_lo_freq(
            >>>     target_lo_freq, {'min': 5.4e9, 'max': 6.9e9, 'step': 10e6})
        """
        if fixed_lo == 'default':
            fixed_lo = self.get(f'{operation}_fixed_lo_freq')
        if fixed_lo is None:
            return target_lo_freq
        elif isinstance(fixed_lo, float):
            return fixed_lo
        elif isinstance(fixed_lo, str):
            instr = self.find_instrument(fixed_lo)
            return getattr(instr, f'get_{operation}_lo_freq')()
        elif isinstance(fixed_lo, dict):
            f_min = fixed_lo.get('min', 0)
            f_max = fixed_lo.get('max', np.inf)
            step = fixed_lo.get('step', None)
            lo_freq = max(min(target_lo_freq, f_max) - f_min, 0)
            if step is not None:
                lo_freq = round(lo_freq / step) * step
                if lo_freq > f_max:
                    lo_freq -= step
            lo_freq += f_min
            return lo_freq
        else:
            ind = np.argmin(np.abs(np.array(fixed_lo) - (target_lo_freq)))
            return fixed_lo[ind]

    def configure_mod_freqs(self, operation=None, **kw):
        """Configure modulation freqs (IF) to be compatible with fixed LO freqs

        If {op}_fixed_lo_freq is not None for the operation {op},
        {op}_mod_freq will be updated to {op}_freq' - {op}_fixed_lo_freq.
        The method can be called with kw (see below) as a set_cmd when a
        relevant paramter changes, or without kw as a sanity check, in which
        case it shows a warning when updating an IF.

        Args:
            operation (str, None): configure the IF only for the operation
                indicated by the string or for all operations for which a
                fixed LO freq is configured.
            **kw: If a kew equals the name of a qcodes parameter of the qb,
                the corresponding value supersedes the parameter value.

        Returns:
            - The new IF if called with arguments operation and
              {operation}_mod_freq (can be used as set_parser).
            - None otherwise.
        """
        def get_param(param):
            if param in kw:
                return kw[param]
            else:
                return self.get(param)

        fixed_lo_suffix = '_fixed_lo_freq'
        if operation is None:
            ops = [k[:-len(fixed_lo_suffix)] for k in self.parameters
                   if k.endswith(fixed_lo_suffix)]
        else:
            ops = [operation]

        for op in ops:
            fixed_lo = get_param(f'{op}{fixed_lo_suffix}')
            if fixed_lo is None:
                if operation is not None and f'{op}_mod_freq' in kw:
                    # called for IF change of single op: behave as set_parser
                    return kw[f'{op}_mod_freq']
            else:
                freq = get_param(f'{op}_freq')
                old_mod_freq = get_param(f'{op}_mod_freq')
                if np.ndim(old_mod_freq):
                    raise NotImplementedError(
                        f'{op}: Fixed LO freq in combination with '
                        f'multichromatic mod freq is not implemented.')
                if freq is None:  # freq not yet set
                    mod_freq = old_mod_freq  # no need to update the mod freq
                else:
                    lo_freq = self.get_closest_lo_freq(
                        freq - old_mod_freq, fixed_lo, operation=op)
                    mod_freq = get_param(f'{op}_freq') - lo_freq
                if operation is not None and f'{op}_mod_freq' in kw:
                    # called for IF change of single op: behave as set_parser
                    return mod_freq
                elif old_mod_freq != mod_freq:
                    if not any([k.startswith(f'{op}_') and k != f'{op}_mod_freq'
                            for k in kw]):
                        log.warning(
                            f'{self.name}: {op}_mod_freq {old_mod_freq} is not '
                            f'consistent '
                            f'with the fixed LO freq {fixed_lo} and will be '
                            f'adjusted to {mod_freq}.')
                    self.parameters[f'{op}_mod_freq'].cache._set_from_raw_value(
                        mod_freq)

    def configure_pulsar(self):
        """
        Configure MeasurementObject-specific settings in pulsar:
        - Set AWG channel DC offsets and switch sigouts on,
        see configure_offsets
        """
        # set offsets and turn on AWG outputs
        self.configure_offsets()

    def configure_offsets(self, set_ro_offsets=True, offset_list=None):
        """
        Set AWG channel DC offsets and switch sigouts on.

        :param set_ro_offsets: whether to set offsets for RO channels
        :param offset_list: additional offsets to set
        """
        pulsar = self.instr_pulsar.get_instr()
        if offset_list is None:
            offset_list = []

        if set_ro_offsets:
            offset_list += [('ro_I_channel', 'ro_I_offset'),
                            ('ro_Q_channel', 'ro_Q_offset')]

        for channel_par, offset_par in offset_list:
            ch = self.get(channel_par)
            if ch is not None and ch + '_offset' in pulsar.parameters:
                pulsar.set(ch + '_offset', self.get(offset_par))
                pulsar.sigout_on(ch)

    def link_param_to_operation(self, operation_name, parameter_name,
                                argument_name):
        """
        Links an existing param to an operation for use in the operation dict.

        An example of where to use this would be the flux_channel.
        Only one parameter is specified but it is relevant for multiple flux
        pulses. You don't want a different parameter that specifies the channel
        for the iSWAP and the CZ gate. This can be solved by linking them to
        your operation.

        Args:
            operation_name (str): The operation of which this parameter is an
                argument. e.g. mw_control or CZ
            parameter_name (str): Name of the parameter
            argument_name  (str): Name of the arugment as used in the sequencer
            **kwargs get passed to the add_parameter function
        """
        if parameter_name not in self.parameters:
            raise KeyError('Parameter {} needs to be added first'.format(
                parameter_name))

        if operation_name in self.operations().keys():
            self._operations[operation_name][argument_name] = parameter_name
        else:
            raise KeyError('Unknown operation {}, add '.format(operation_name) +
                           'first using add operation')


from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
from sklearn.pipeline import Pipeline

def split_data(labelled_tweets):
    X = labelled_tweets.lemmatized_content
    y = labelled_tweets.sentiment

    # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=42)

    return train_test_split(X, y, test_size = 0.1, random_state=42)

def train_model(labelled_tweets):
    X_train, X_test, y_train, y_test = split_data(labelled_tweets)
    count_vect = CountVectorizer()
    X_train_bow = count_vect.fit(X_train)
    X_train_bow = count_vect.transform(X_train)
    X_test_bow = count_vect.transform(X_test)

    tfidf_transformer = TfidfTransformer(use_idf=False).fit(X_train_bow)
    X_train_tfidf = tfidf_transformer.transform(X_train_bow)

    classifier = MultinomialNB().fit(X_train_tfidf, y_train)
    y_predict_class = classifier.predict(X_test_bow)

    print("Train data summary")

    print("Classifier Accuracy is {0}".format(metrics.accuracy_score(y_test, y_predict_class)))
    print(y_test.value_counts())

    return count_vect, classifier


import datetime

class MatchException(Exception):
    pass

class Match:
    MATCH_NOT_STARTED,\
    MATCH_IN_PROGRESS,\
    MATCH_OVER,\
    MATCH_CANCELLED\
    = range(4)

    def __init__(self, is_ranked=True):
        self.state = Match.MATCH_NOT_STARTED
        self.is_ranked = is_ranked

    def __str__(self):
        output = " MATCH SCORE ".center(41, "#") + "\n"
        output += "   {0}".format(self.player_1.name).ljust(17)
        output += "{0} - {1}".format(self.player_1.score, self.player_2.score).center(7)
        output += "{0}   ".format(self.player_2.name).rjust(17)
        output += "\n"
        output += "".center(41, "#")
        return output

    def start(self, card_id_1, card_id_2):
        self.state = Match.MATCH_IN_PROGRESS
        self.date = datetime.datetime.now()
        self.player_1 = MatchPlayer(card_id_1)
        self.player_2 = MatchPlayer(card_id_2)

    def update_score(self, player_number, increment = True):

        # Elect player and opponent given player_number
        player = self.player_1 if player_number == 1 else self.player_2
        opponent = self.player_2 if player_number == 1 else self.player_1

        # Evaluate new score
        new_score = player.score + (1 if increment else -1)
        if new_score < 0:
            raise MatchException("Player %s cannot have the score: %s" % (player_number, new_score))

        # Update score
        player.score = new_score

        # Evaluate match
        if player.score >= 11 and player.score > opponent.score + 1:
            # Game over
            self.state = Match.MATCH_OVER

    def cancel_match(self):
        self.state = Match.MATCH_CANCELLED

class MatchPlayer:
    def __init__(self, card_id):
        self.card_id = card_id
        self.score = 0


import re

class VariableResolutionService(object):
    VARIABLES_PREFIX = '@var.'
    VARIABLES_REGEX = '{}(\w+)'.format(VARIABLES_PREFIX)

    def __init__(self, variables):
        self.variables = variables

    def hidrate(self, text):
        variables_to_be_hidrated = re.findall(self.VARIABLES_REGEX, text)

        if not variables_to_be_hidrated:
            return text

        return self.replace(text, variables_to_be_hidrated)

    def replace(self, text, variables):
        response_text = text

        for variable in variables:
            try:
                response_text = response_text.replace(
                    '{}{}'.format(self.VARIABLES_PREFIX, variable),
                    self.variables[variable]
                )
            except KeyError:
                # Empty
                pass

        return response_text


"""Loan module."""

from econopy.constants import ANUAL

class BaseLoan:

    def __init__(self, capital, periodos, interes, tipo_periodo=ANUAL):
        self.capital = capital
        self.periodos = periodos
        self.tipo_periodo = tipo_periodo
        self._interes = interes
        self.cuotas_interes = []
        self.cuotas_amortizacion = []
        self.capital_vivo = []
        self.total_amortizado = []

    @property
    def interest_rate(self):
        return self._interes * self.tipo_periodo

class FrenchLoan(BaseLoan):

    @property
    def quota(self):
        return self.capital * (
            self.interest_rate*(
                1 + self.interest_rate)**self.periodos) / (-1 + (
                    1 + self.interest_rate)**self.periodos)


import time

for i in range(100000000):
	f = open('nums', 'r+')
	lines = f.readlines()
	n = lines[len(lines)-1]
	n = int(n)
	n = n+1
	f.write('\n' + str(n))
	f.close()
	#time.sleep(0.2)


from amazon.paapi import AmazonAPI

KEY    = "<INPUT YOUR KEY>"
SECRET = "<INPUT YOUR SECRET KEY>"
TAG    = "<INPUT YOUR PARTNER TAG>"
COUNTRY = "JP"
keyword = "任天堂Switch"

amazon = AmazonAPI(KEY, SECRET, TAG, COUNTRY)

products = amazon.search_items(keywords=keyword)
asin     = products["data"][0].asin
price    = products["data"][0].offers.listings[0].price.amount
url      = products["data"][0].detail_page_url
title    = products["data"][0].item_info.title.display_value

print(asin, price, url, title)


class Plateau:
    rovers = []

    def __init__(self, rows, columns):
        self.rows = rows
        self.columns = columns

    def can_move(self, coord_x, coord_y):
        return 0 <= coord_x <= self.rows and 0 <= coord_y <= self.columns

    def add_rover(self, rover, instructions):
        for instruction in instructions:
            if instruction == 'M':
                possible_step = rover.possible_step()
                coord_x = possible_step[0]
                coord_y = possible_step[1]

                if self.can_move(coord_x, coord_y):
                    rover.move(coord_x, coord_y)
            else:
                rover.set_orientation(instruction)

        self.rovers.append(rover)

    def get_rover(self, name):
        rover = None

        for item in self.rovers:
            if item.name == name:
                rover = item
                break

        return rover


#!/usr/bin/python3

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

#Reference:**********************************************

# @Time    : 2019/10/2 12:46 下午

# @Author  : baozhiqiang

# @File    : k-core.py

# @User    : bao

# @Software: PyCharm

#Reference:**********************************************

import  commons
import  networkx as nx
class findk_core:
    def __init__(self):
        pass

    def main(self, dir):
            '''
            走来不要那么难，先搞定树吧。才能继续搞定图。
            :return:
            '''
            pre = '../../data/'
            last = '.txt'
            self.initG = commons.get_networkByFile(fileName=pre + dir + last)  # 获取图，
            max_sub_graph = commons.judge_data(self.initG)
            source_list = commons.product_sourceList(max_sub_graph, self.fix_number_source)
            self.true_Source_list = source_list
            self.infectG = commons.propagation1(max_sub_graph, self.true_Source_list)  # 开始传染

            subinfectG = commons.get_subGraph(self.infectG)
            subinfectG.to_undirected()
            subinfectG.remove_edges_from(subinfectG.selfloop_edges())
            data = nx.core_number(subinfectG)

            # Graph.to_undirected()
            print('data', data)
            data_sort = sorted(data.items(), key= lambda x :x[1], reverse= True)
            print('data_sort', data_sort)
            print('max-core',data_sort[0][1])
            core_number1 = 0
            #看下源点在那一层。
            for  node, core_number in data_sort:
                if node in source_list:
                    print('node,core_number', [node,core_number])
                    core_number1 = core_number
            return core_number1

    def cal_distanceError(self, dir):
            self.fix_number_source = 1
            distance = 0
            for i in range(20):
                self.main(dir)
                distance += self.main(dir)
            result = distance / 20
            # 导入time模块
            import time
            # 打印时间戳
            # print(time.time())
            pre = './result/'
            last = '.txt'
            with open(pre + dir + 'first' + last, 'a') as f:
                f.write(str(time.asctime(time.localtime(time.time()))) + '\n')
                f.write(str(20) + '     ' + str(dir) + '    ' + str(result))
            print(distance / 20)

test = findk_core()
filename = 'CA-GrQc'

test.cal_distanceError(filename)


import os

from dirs import PROJECT_ROOT

logs_folder = PROJECT_ROOT / "logs"
os.makedirs(logs_folder, exist_ok=True)

DICT_CONFIG = {
    "version": 1,
    "formatters": {"standard": {"format": "%(asctime)s %(name)-8s %(levelname)-8s %(message)s"}},
    "handlers": {
        "stderr": {"class": "logging.StreamHandler", "level": "DEBUG", "formatter": "standard"},
        "rotating_file_handler": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": "DEBUG",
            "formatter": "standard",
            "filename": logs_folder / "logs.log",
            # Set max log file size to 1MB.
            "maxBytes": 1024 * 1024,
            # At most `backupCount` of backup log files will be kept. If more
            # would be created when rollover occurs, the oldest one is deleted.
            "backupCount": 2,
        },
    },
    "loggers": {
        "main": {
            "handlers": ["stderr", "rotating_file_handler"],
            "level": "DEBUG",
            "propagate": True,
        }
    },
}


from behave import given, when, then
from requests import get
from assertpy import assert_that

@given('an api url with valid response')
def step_imp(context):
    context.get_url = 'https://api.tmsandbox.co.nz/v1/Categories/6327/Details.json?catalogue=false'

@when('user sends request to get response')
def step_imp(context):
    context.response = get(context.get_url)

@then('it should display Name with value Carbon credits')
def step_imp(context):
    assert_that(context.response.status_code).is_equal_to(200)
    response = context.response.json()
    assert_that(response['Name']).described_as('Name Value').is_equal_to('Carbon credits')

@then('it should display CanRelist with boolean value True')
def step_imp(context):
    assert_that(context.response.status_code).is_equal_to(200)
    response = context.response.json()
    assert_that(response['CanRelist']).described_as('CanRelist value').is_true()

@then('Galery Promotion should display description with value 2x larger image')
def step_imp(context):
    assert_that(context.response.status_code).is_equal_to(200)
    response = context.response.json()

    promotion_name = 'Gallery'
    promotions = response['Promotions']
    promotion_counter = len(response['Promotions'])
    counter = 0
    while counter < promotion_counter:
        if promotions[counter]['Name'] == promotion_name:
            promotion_description = promotions[counter]['Description']
        counter = counter + 1

    assert_that(promotion_description).described_as('Galery Promotion Description').contains('2x larger image')


import operator
from megaboxDataProvider import megabox_data_provider

class megabox_booking_api:

    # 개봉작, 상영예정작 목록
    @staticmethod
    def getAllMovieList():
        list1 = megabox_booking_api.getMovieList()
        list2 = megabox_data_provider.getMovieScheduleList()

        return list1 + list2

    @staticmethod
    def getMovieList():
        ret = []
        movieList = megabox_data_provider.getMovieList()
        if movieList != False :
            for movie in movieList:
                i = {}
                i['movieCode'] = movie['movieCode']
                i['movieName'] = movie['koreanTitle']
                ret.append(i)

        return ret

    @staticmethod
    def getCinemasInAllRegion():

        allCinemas = []
        regions = megabox_data_provider.getRegions()
        for r in regions:
            cinemas = megabox_data_provider.getCinemasInRegion(r['groupCode'])
            allCinemas.extend(cinemas)

        return allCinemas

    # movieCode를 상영하는 전국의 영화관 목록을 가져옵니다
    @staticmethod
    def getShowingCinemasInAllRegion(movieCode):
        ret = []
        regions = megabox_data_provider.getShowingRegions(movieCode)
        for r in regions:
            if r['playCnt'] > 0:
                cinemas = megabox_data_provider.getShowingCinemas(movieCode, r['groupCode'])

                for c in cinemas :
                    if c['showSeqCnt'] > 0:
                        ret.append(c)

        ret = sorted(ret, key=lambda k: k['cinemaName'])
        return ret


# coding=utf-8
'''
先把图片变换，后缀+R

json文件坐标变换，注意变换前后seg的个数，后缀名改变，Imgname那项要改掉

'''
import os
import argparse
import imageio
import imgaug as ia
from imgaug.augmentables.polys import Polygon, PolygonsOnImage
import numpy as np
import json
import imgaug.augmenters as iaa
import random

def parse_args():
    parser = argparse.ArgumentParser(description='Test with Mask RCNN networks.')
    # wjh
    parser.add_argument('--imgdir', type=str, default='',
                        help='Test images, use comma to split multiple.')
    parser.add_argument('--tardir', type=str, default='',
                        help='变换后的图像路径')
    parser.add_argument('--savedir', type=str, default='',
                        help='保存变换json的路径')
    parser.add_argument('--jsondir', type=str, default='',
                        help='总json文件的路径')
    parser.add_argument('--pretrained', type=str, default='True',
                        help='Load weights from previously saved parameters. You can specify parameter file name.')
    args = parser.parse_args()
    return args

def draw():
    image = imageio.imread('./flip/IMG_8038R.JPG')
    json_file = './flip/json/IMG_8038R.json'
    with open(json_file, 'rb') as fp:
        data = json.load(fp)
        all_point = []
        for shapes in data['shapes']:
            aaa = Polygon(shapes['points'])
            all_point.append(aaa)
        psoi = ia.PolygonsOnImage(all_point, shape=image.shape)
        aug = iaa.Sequential([
            iaa.Affine(scale=(1, 1), rotate=(0, 0), translate_percent=(0,0),
                       mode=["constant"], cval=0),  # 仿射变换

            # iaa.Fliplr(0.5), # 左右翻转
            # iaa.PerspectiveTransform((0.01, 0.1)), # 透视变换
            # iaa.AddToHueAndSaturation((-20, 20)),  #
            # iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
            # iaa.Sometimes(0.75, iaa.Snowflakes())   # 高斯模糊
        ])
        image_aug, psoi_aug = aug(image=image, polygons=psoi)
        ia.imshow(psoi_aug.draw_on_image(image_aug, alpha_face=0.2, size_points=7))

# 旋转图片
def rotate_img(soudir, tardir, rate, json_dir, save_dir):
    pathdir = os.listdir(soudir)  # sourdir里不要有目录名
    filenum = len(pathdir)
    picknum = int(filenum*rate)
    sample = random.sample(pathdir, picknum)
    for name in sample:
        json_file = json_dir+name[:-4]+'.json'
        with open(json_file, 'rb') as fp:
            image = imageio.imread(soudir+name)
            data = json.load(fp)
            data['imagePath'] = data['imagePath'][:-4] + 'R.JPG'
            all_point = []
            for shapes in data['shapes']:
                aaa = Polygon(shapes['points'])
                all_point.append(aaa)
            psoi = ia.PolygonsOnImage(all_point, shape=image.shape)
            aug = iaa.Sequential([
                iaa.Affine(scale=(0.8, 1), rotate=(-10,10), translate_percent=(-0.1,0.1),
                           mode=["constant"], cval=0),  # 仿射变换

                # iaa.Fliplr(0.5), # 左右翻转
                # iaa.PerspectiveTransform((0.01, 0.1)), # 透视变换
                # iaa.AddToHueAndSaturation((-20, 20)),  #
                # iaa.LinearContrast((0.8, 1.2), per_channel=0.5),
                # iaa.Sometimes(0.75, iaa.Snowflakes())   # 高斯模糊
            ])
            image_aug, psoi_aug = aug(image=image, polygons=psoi)
            psoi_aug_removed = psoi_aug.remove_out_of_image(fully=True, partly=False)
            psoi_aug_removed_clipped = psoi_aug_removed.clip_out_of_image()
            assert len(psoi_aug.polygons) == len(psoi_aug_removed_clipped.polygons)
            for i,shapes in enumerate(data['shapes']):
                shapes['points'] = psoi_aug_removed_clipped.polygons[i].exterior.astype('int32').tolist()

            imageio.imwrite(tardir+name[:-4]+'R.JPG', image_aug)
            json.dump(data, open(save_dir + json_file[-13:-5] + 'R.json', 'w'), indent=4)

if __name__ == '__main__':
    args = parse_args()
    # rotate_img(args.imgdir, args.tardir, 1, args.jsondir, args.savedir)
    draw()


# import pandas as pd
import pickle
'''
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
'''

def floatCast(num):
    return float(num)

def load_and_predict(model_filename, prediction_variables):
    loaded_model = pickle.load(open(model_filename, 'rb'))
    X_predict = [list(map(floatCast, prediction_variables))]
    guess = loaded_model.predict(X_predict)
    print(guess[0])
    return guess

def testlp():
    # contingent on creating the model created in testing build and pickle
    model = 'ldasimple_top50.sav'
    variables = [142, 21, 38, -12, 15, 70, 1235]
    load_and_predict(model, variables)

if __name__ == '__main__':
    testlp()


import numpy as np

class linesearch:
    ''' Algorihms for exact line search '''
    def backtrack(x, Dx, fx, f, convergenced):  # backtrack linesearch
        l = 1
        while True:
            y = x + Dx*l
            fy = f(y)
            if convergenced(fy, fx, l):
                break
            l /= 2
        return l, y, fy

    def quadratic(f, startpoint, delta, eps):  # see book: Introduction to Optimum Design

        # determine interval using golden section search
        r = 1.618
        fold = f(startpoint)
        i = 0
        while True:
            alpha = delta*r**i  # step increment
            fnew = f(startpoint + alpha)
            if fnew >= fold:
                break
            else:
                fold = fnew
                i = i+1
        # define upper (up) and lower (low) limit of golden section
        up = alpha + startpoint
        if i > 1:
            low = startpoint + alpha/(r**2) * delta
        else:
            low = startpoint

        # find minimum using quadratic interpolation in interval
        ai = low + ((up - low)/2)  # mid-point in interval
        candidate = ai  # initial candiadate minimum point
        prev_candidate = startpoint  # previous candidate minimum point
        dif = np.linalg.norm(candidate-prev_candidate)

        while dif > eps:
            # parameters for quad. poly. a0 + a1*x + a2*x²
            a2 = (1/(up-ai)) * ((f(up)-f(low))/(up-low) - (f(ai)-f(low))/(ai-low))
            a1 = ((f(ai)-f(low))/(ai-low)) - a2 * (low+ai)
            a0 = f(low) - a1*low - a2*low**2

            abar = -a1/(2*a2)  # candidate minimum from interpolation
            fbar = f(abar)  # function evaluated at interpoltation candidate
            fai = a0 + a1*ai + a2*ai**2  # poly at ai

            # determine new section based on candidate minimum
            if ai < abar:
                if fai < fbar:
                    up = abar
                else:
                    low = ai
                    ai = abar
            else:
                if fai < fbar:
                    low = abar
                else:
                    up = ai
                    ai = abar

            # update candidate, dif, previous candidate
            candidate = abar
            dif = np.linalg.norm(candidate-prev_candidate)
            prev_candidate = candidate
        return candidate


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
# Embedded file name: T:\InGame\Gameplay\Scripts\Server\objects\components\waiting_line_component.py
# Compiled at: 2020-09-25 20:57:29
# Size of source mod 2**32: 8598 bytes
from collections import defaultdict
from objects.components import Component, componentmethod_with_fallback
from objects.components.types import WAITING_LINE_COMPONENT
from sims4.tuning.dynamic_enum import DynamicEnum
from sims4.tuning.geometric import TunableWeightedUtilityCurveAndWeight
import sims4.log
logger = sims4.log.Logger('Waiting Line', default_owner='ayarger')

class WaitingLine:

    def __init__(self):
        self._line = []
        self._line_head_position = None
        self._line_head_angle = None
        self._line_cone = None
        self._line_head_los_constraint = None

    def _init_line(self, waiting_interaction):
        line_head_data = waiting_interaction.line_head_data
        self._line_head_position = line_head_data[0]
        self._line_head_angle = line_head_data[1]
        self._line_cone = line_head_data[2]
        self._line_head_los_constraint = line_head_data[3]
        self._line_constraint_target = line_head_data[4]
        self._line_update_timing = line_head_data[5]

    def _join_line(self, waiting_interaction):
        if not self._line:
            self._init_line(waiting_interaction)
        self._line.append(waiting_interaction)

    def remove_from_line(self, waiting_interaction):
        if self.is_in_line(waiting_interaction):
            self._line.remove(waiting_interaction)

    def is_in_line(self, interaction):
        return interaction in self._line

    def is_sim_in_line(self, sim):
        return any((sim is interaction.sim for interaction in self._line))

    def is_first_in_line(self, waiting_interaction):
        if not self._line:
            return False
        return self._line[0] is waiting_interaction

    @property
    def first_interaction_in_line(self):
        if self._line:
            return self._line[0]

    @property
    def last_interaction_in_line(self):
        if self._line:
            return self._line[(-1)]

    def get_neighboring_interaction(self, waiting_interaction, *, offset):
        if waiting_interaction in self._line:
            neighbor_index = self._line.index(waiting_interaction) + offset
            if -1 < neighbor_index < len(self._line):
                return self._line[neighbor_index]
            return

    def __len__(self):
        return len(self._line)

class WaitingLineComponent(Component, component_name=WAITING_LINE_COMPONENT, allow_dynamic=True):
    DEFAULT_AUTONOMOUS_WAITING_LINE_PREFERENCE_CURVE = TunableWeightedUtilityCurveAndWeight(description='\n                        A curve that maps the number of sims in a waiting line\n                        for this interaction to an autonomy score multiplier\n                        for this interaction.\n                        \n                        This curve is the default curve for any interaction\n                        that has not yet been tuned to have its own\n                        autonomous_waiting_line_prefence_curve.\n                        ')

    def __init__(self, owner):
        super().__init__(owner)
        self._lines = defaultdict(WaitingLine)
        self.chosen_destinations = []

    def join_line(self, waiting_interaction):
        key = waiting_interaction.waiting_line_key
        self._lines[key]._join_line(waiting_interaction)
        return self._lines.get(key)

    def get_waiting_line(self, key):
        return self._lines.get(key)

    def notify_heads_of_lines(self, *args, sim=None):
        for line in self._lines.values():
            if len(line) > 0:
                line.first_interaction_in_line._push_adjustment_interaction()

    def remove_from_lines(self, waiting_interaction):
        key = waiting_interaction.waiting_line_key
        waiting_line = self._lines.get(key)
        if waiting_line is None:
            return
        waiting_line.remove_from_line(waiting_interaction)
        if len(waiting_line) < 1:
            self._lines.pop(key)

    def is_sim_in_line(self, sim):
        return any((line.is_sim_in_line(sim) for line in self._lines.values()))

    @componentmethod_with_fallback(lambda _: 1)
    def get_waiting_line_autonomy_multiplier(self, interaction):
        key = interaction.waiting_line.waiting_line_key
        line_quantity_to_multiplier_curve = None
        if interaction.waiting_line.autonomous_waiting_line_prefence_curve is not None:
            line_quantity_to_multiplier_curve = interaction.waiting_line.autonomous_waiting_line_prefence_curve
        else:
            if WaitingLineComponent.DEFAULT_AUTONOMOUS_WAITING_LINE_PREFERENCE_CURVE is not None:
                line_quantity_to_multiplier_curve = WaitingLineComponent.DEFAULT_AUTONOMOUS_WAITING_LINE_PREFERENCE_CURVE
            else:
                raise RuntimeError("WaitingLineComponent.DEFAULT_AUTONOMOUS_WAITING_LINE_PREFERENCE_CURVE has not been tuned! Waiting-Line autonomy integration won't work!")
        waiting_line = self._lines.get(key)
        if waiting_line is not None:
            length_of_line = len(waiting_line)
            return line_quantity_to_multiplier_curve.get(length_of_line)
        return 1


def countMaxActivities(arr):
    i , j = 1 , 0
    activities = 1
    n = len(arr)
    while i<n:
        while i<n and arr[i][0]<arr[j][1]:
            i+=1
        if i<n:
              activities+=1

        j = i
        i+=1

    return activities

arr = [[5, 9], [1, 2], [3, 4], [0, 6],[5, 7], [8, 9]]

arr.sort(key = lambda x: x[1])
print(arr)
# print(countMaxActivities(arr))


# -*- coding: utf-8 -*-
import nltk

file_object  = open('testfile.txt', 'r')
output_object  = open('output.txt', 'w')

def word_to_output(pair): #format word/tag-pair and write it to output
	if pair[1] in ["NN", "NNS", "NNP", "NNPS", "PRP", "PRP$", "WP", "WP$"]:
		tag="NOUN"
	elif pair[1] in ["JJ", "JJR", "JJS"]:
		tag="ADJ"
	elif pair[1] in ["VB", "VBD", "VBG", "VBN", "VBP", "VBZ"]:
		tag="VERB"
	elif pair[1] in ["RB", "RBR", "RBS"]:
		tag="ADVERB"
	else: tag = pair[1]
	output_object.write(pair[0] + "(" + tag + ") ")
	if pair[0] == ".":
		output_object.write("\n")

for line in file_object:
	line = nltk.word_tokenize(line)
	line = nltk.pos_tag(line)
	ner = nltk.ne_chunk(line, binary=False)
	for subtree in ner:
		if type(subtree) is nltk.Tree: #named entity
			output_object.write("[NER] ")
			for pair in subtree:
				word_to_output(pair)
			output_object.write("[" + subtree.label() + "] ")
		else: #not a named entity
			word_to_output(subtree)


import os
import sys
DIRNAME = os.path.dirname(__file__)
sys.path.append(os.path.join(DIRNAME, '..', '..'))

import mujoco_py as mujoco
import numpy as np

from src.constrainedChasingEscapingEnv.envMujoco import ResetUniform, IsTerminal, TransitionFunction
from src.constrainedChasingEscapingEnv.state import GetAgentPosFromState
from src.constrainedChasingEscapingEnv.reward import HeuristicDistanceToTarget, RewardFunctionCompete
from src.algorithms.mcts import RollOut, Expand, InitializeChildren, ScoreChild, SelectChild, MCTS, backup, \
    establishPlainActionDist
from src.neuralNetwork.policyValueNet import GenerateModel, restoreVariables
from src.episode import SampleTrajectory, chooseGreedyAction
from exec.trajectoriesSaveLoad import GetSavePath, GenerateAllSampleIndexSavePaths, SaveAllTrajectories, saveToPickle
from src.constrainedChasingEscapingEnv.policies import HeatSeekingDiscreteDeterministicPolicy, \
    HeatSeekingContinuesDeterministicPolicy, stationaryAgentPolicy
from src.constrainedChasingEscapingEnv.analyticGeometryFunctions import computeAngleBetweenVectors

def main():
    numSamples = 1
    maxRunningSteps = 25
    numSimulations = 10

    # Mujoco environment
    dirName = os.path.dirname(__file__)
    physicsDynamicsPath = os.path.join(dirName, '..', '..', 'env', 'xmls', 'twoAgents.xml')
    physicsModel = mujoco.load_model_from_path(physicsDynamicsPath)
    physicsSimulation = mujoco.MjSim(physicsModel)

    qPosInit = (0, 0, 0, 0)
    qVelInit = (0, 0, 0, 0)
    numAgents = 2
    qVelInitNoise = 8
    qPosInitNoise = 9.7
    reset = ResetUniform(physicsSimulation, qPosInit, qVelInit, numAgents, qPosInitNoise, qVelInitNoise)

    sheepId = 0
    wolfId = 1
    xPosIndex = [2, 3]
    getSheepXPos = GetAgentPosFromState(sheepId, xPosIndex)
    getWolfXPos = GetAgentPosFromState(wolfId, xPosIndex)
    killzoneRadius = 2
    isTerminal = IsTerminal(killzoneRadius, getSheepXPos, getWolfXPos)

    numSimulationFrames = 20
    transit = TransitionFunction(physicsSimulation, isTerminal, numSimulationFrames)

    sheepActionSpace = [(10, 0), (7, 7), (0, 10), (-7, 7), (-10, 0), (-7, -7), (0, -10), (7, -7)]
    numActionSpace = len(sheepActionSpace)

    # wolf heatSeeking Policy
    wolfActionSpace = [(8, 0), (6, 6), (0, 8), (-6, 6), (-8, 0), (-6, -6), (0, -8), (6, -6)]
    heatSeekingPolicy = HeatSeekingDiscreteDeterministicPolicy(wolfActionSpace, getSheepXPos, getWolfXPos,
                                                               computeAngleBetweenVectors)

    # transit in sheep simulation
    transitInSheepMCTS = lambda state, action: transit(state, [action, chooseGreedyAction(heatSeekingPolicy(state))])

    # MCTS sheep
    cInit = 1
    cBase = 100
    calculateScore = ScoreChild(cInit, cBase)
    selectChild = SelectChild(calculateScore)

    getUniformActionPrior = lambda state: {action: 1 / numActionSpace for action in sheepActionSpace}
    initializeChildrenUniformPrior = InitializeChildren(sheepActionSpace, transitInSheepMCTS,
                                                        getUniformActionPrior)
    expand = Expand(isTerminal, initializeChildrenUniformPrior)

    aliveBonus = 0.05
    deathPenalty = -1
    rewardFunction = RewardFunctionCompete(aliveBonus, deathPenalty, isTerminal)

    rolloutPolicy = lambda state: sheepActionSpace[np.random.choice(range(numActionSpace))]
    rolloutHeuristicWeight = 0.1
    maxRolloutSteps = 10
    rolloutHeuristic = HeuristicDistanceToTarget(rolloutHeuristicWeight, getWolfXPos, getSheepXPos)
    rollout = RollOut(rolloutPolicy, maxRolloutSteps, transitInSheepMCTS, rewardFunction, isTerminal,
                      rolloutHeuristic)

    mctsSheep = MCTS(numSimulations, selectChild, expand, rollout, backup, establishPlainActionDist)

    # sample trajectory
    sampleTrajectory = SampleTrajectory(maxRunningSteps, transit, isTerminal, reset, chooseGreedyAction)

    # generate trajectories
    policy = lambda state: [mctsSheep(state), heatSeekingPolicy(state)]
    trajectories = [sampleTrajectory(policy) for _ in range(numSamples)]

    # saving trajectories
    trajectorySaveDirectory = os.path.join(dirName, '..', '..', 'data',
                                           'generateTrajectoryForSheepAndWolfBaseline')
    if not os.path.exists(trajectorySaveDirectory):
        os.makedirs(trajectorySaveDirectory)

    trajectorySaveParameters = {'numSimulations': numSimulations, 'killzoneRadius': killzoneRadius,
                                'qPosInitNoise': qPosInitNoise, 'qVelInitNoise': qVelInitNoise,
                                'rolloutHeuristicWeight': rolloutHeuristicWeight, 'maxRunningSteps': maxRunningSteps}
    trajectorySaveExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectorySaveDirectory, trajectorySaveExtension, trajectorySaveParameters)

    path = getTrajectorySavePath(trajectorySaveParameters)
    saveToPickle(trajectories, path)

if __name__ == '__main__':
    main()


################################################################################################################
"""

"""

# Built-in/Generic Imports

# Libs

# Own modules

__version__ = '1.1.1'
__author__ = 'Victor Guillet'
__date__ = '31/01/2020'

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

class gen_item:
    def __init__(self, label):
        """
        Item class used to generate various item types
        Available items labels:  - Health
                                 - Weapon
                                 - Armor
                                 - Tool

        Available items size:    - S
                                 - M
                                 - L

        :param label: Object label
        """
        # ----- Setup reference properties
        self.type = "Item"
        self.label = label

        item_size = label.split("_")[0]

        if item_size == "S":
            self.rating = 5

        elif item_size == "M":
            self.rating = 20

        elif item_size == "L":
            self.rating = 50


import random
import time
import numpy as np
import reference.hashtable

class HashMap:
    def __init__(self,size=20,itemCount=0):
        self.size = size
        self.itemCount=itemCount
        self.map = np.array()
        for n in range(self.size):
            self.map.append((-1, -1))

    def hasher(self,key):
        output = 0
        for c in key:
            random.seed(ord(c))
            output += ord(c) * random.randint(1, 10000000)
        return output%self.size

    def upsize(self):
        self.size*=2
        newMap=[]
        for n in range(self.size):
            newMap.append((-1, -1))

        for item in self.map:
            k,v=item
            if item!=(-1,-1):
                if newMap[self.hasher(k)]==(-1,-1):
                    newMap[self.hasher(k)]=item
                else:
                    n=1
                    while newMap[(self.hasher(k)+n)%self.size]!=(-1,-1):
                        if n>self.size:
                            break
                        n+=1
                    newMap[(self.hasher(k)+n)%self.size]=item

        self.map=newMap

        return self

    def add(self,key,value):
        self.itemCount+=1
        if self.itemCount>self.size*.8:
            self.upsize()

        hashed=self.hasher(key)

        if self.map[hashed]==(-1,-1):
            self.map[hashed]=(key,value)
        else:
            n=1
            while self.map[(hashed+n)%self.size]!=(-1,-1):
                if n>self.size:
                    break
                n+=1
            self.map[(hashed+n)%self.size]=(key,value)

        return self.map

    def delete(self,key):
        hashed=self.hasher(key,self.size)
        k,_=self.map[hashed]
        if k==key:
            self.map[hashed]=(-1,-1)
        else:
            n=1
            while k!=key:
                k,_=self.map[(hashed+n)%self.size]
                if n>self.size:
                    return False
                n+=1
            self.map[(hashed+n)%self.size]=(-1,-1)

        return self.map

    def get(self,key):
        k,v=self.map[self.hasher(key)]
        if k==key:
            return v
        else:
            n=0
            while k!=key:
                k,_=self.map[(self.hasher(key)+n)%self.size]
                if n>self.size:
                    return False
                n+=1
            _,v2=self.map[self.hasher(key)+n-1]
            return v2

    def getHashMap(self):
        return self.map

    def getItemCount(self):
        return self.itemCount

    def getSize(self):
        return self.size

class IRHashMap:
    def __init__(self,size=20,itemCount=0):
        self.size = size
        self.transferSize=size*2
        self.itemCount=itemCount
        self.map = []
        self.transferMap=[]
        self.thingsToTransfer=[]
        self.currentlyResizing=False
        for n in range(self.size):
            self.map.append((-1, -1))

        self.currentlyResizing = False

    def hasher(self,key):
        if self.currentlyResizing:
            size=self.size*2
        else:
            size=self.size

        output = 0
        for c in key:
            random.seed(ord(c))
            output += ord(c) * random.randint(1, 10000000)

        return output%size

    def add(self,key,value):
        if self.currentlyResizing:
            self.transfer()

            self.itemCount+=1

            hashed=self.hasher(key)

            if self.transferMap[hashed]==(-1,-1):
                self.transferMap[hashed]=(key,value)
            else:
                n=1
                while self.transferMap[(hashed+n)%self.transferSize]!=(-1,-1):
                    if n>self.transferSize:
                        break
                    n+=1
                self.transferMap[(hashed+n)%self.transferSize]=(key,value)

        else:
            self.itemCount+=1
            if self.itemCount>self.size*.8:
                self.upsize()

            hashed=self.hasher(key)

            if self.map[hashed%self.size]==(-1,-1):
                self.map[hashed%self.size]=(key,value)
            else:
                n=1
                while self.map[(hashed+n)%self.size]!=(-1,-1):
                    if n>self.size:
                        break
                    n+=1
                self.map[(hashed+n)%self.size]=(key,value)

        return self

    def upsize(self):
        self.transferSize=self.size*2
        self.transferMap=[]
        for n in range(self.transferSize):
            self.transferMap.append((-1, -1))

        self.currentlyResizing=True

        self.thingsToTransfer=self.map

        return self

    def transfer(self):
        n=0
        while self.thingsToTransfer and n<4:
            key,value=self.thingsToTransfer.pop()
            if (key,value)!=(-1,-1):
                hashed=self.hasher(key)

                if self.transferMap[hashed]==(-1,-1):
                    self.transferMap[hashed]=(key,value)
                else:
                    n=1
                    while self.transferMap[(hashed+n)%self.transferSize]!=(-1,-1):
                        if n>self.size:
                            break
                        n+=1
                    self.transferMap[(hashed+n)%self.transferSize]=(key,value)

                n+=1

        if not self.thingsToTransfer:
            self.currentlyResizing=False
            self.map=self.transferMap
            self.size=self.transferSize

    def getHashMap(self):
        if self.currentlyResizing:
            while self.thingsToTransfer:
                self.transfer()

        return self.map

    def getItemCount(self):
        return self.itemCount

    def getSize(self):
        return self.size

# store=HashMap()

random.seed("Seward")
target=2**random.randint(10,100000000)

HashTable = reference.hashtable.new()

f=open("benchmarkReferenceByTens.csv","w")
n=0
startTime=time.time()
while n<target and time.time()<startTime+300:
    key,value=str(int(random.random()*100000)),str(int(random.random()*100000))
    # store.insert(key,value)
    reference.hashtable.set(HashTable, key, value)

    if n%10==0:
        timeNow=str(time.time()-startTime)
        f.write(timeNow + ", " + str(n) + "\n")

    n+=1

reference.hashtable.list(HashTable)

# print(store.getHashMap())
# print(store.getItemCount())
# print(store.getSize())

# store.add("hello","howdy")
# print(store.get("hello"))
# print(store.getItemCount())
# print(store.getSize())


#
# [279] Perfect Squares
#
# https://leetcode.com/problems/perfect-squares/description/
#
# algorithms
# Medium (38.01%)
# Total Accepted:    116.2K
# Total Submissions: 305.5K
# Testcase Example:  '12'
#
# Given a positive integer n, find the least number of perfect square numbers
# (for example, 1, 4, 9, 16, ...) which sum to n.
#
# Example 1:
#
#
# Input: n = 12
# Output: 3
# Explanation: 12 = 4 + 4 + 4.
#
# Example 2:
#
#
# Input: n = 13
# Output: 2
# Explanation: 13 = 4 + 9.
#
import math

class Solution:
    def numSquares(self, n):
        """
        :type n: int
        :rtype: int
        """
        # Based on Lagrange's Four Square theorem
        # Setp 1: If n is a perfect square number, result is 1
        tmp = math.floor(math.sqrt(n))
        if tmp*tmp == n:
            return 1
        # Step 2: For all numbers can be described as 4^k(8m+7), result is 4
        m = n
        while m % 4 == 0:
            m //= 4
        if m % 8 == 7:
            return 4
        # Step 3: For all numbers can be described as x^2 + y^2, x, y != 0,
        # result is 2
        i = math.floor(math.sqrt(n))
        while i > 0:
            tmp = math.floor(math.sqrt(n-i*i))
            if tmp*tmp + i*i == n:
                return 2
            i -= 1
        # Step 4: For all other numbers, result is 3
        return 3


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

def Check():
    ans=0
    for i in range(5):
        if ''.join(map(str,K[i]))=='OOOOO':ans+=1
    for j in range(5):
        if K[0][j]+K[1][j]+K[2][j]+K[3][j]+K[4][j]=='OOOOO':ans+=1
    if K[0][0]+K[1][1]+K[2][2]+K[3][3]+K[4][4]=='OOOOO':ans+=1
    if K[0][4]+K[1][3]+K[2][2]+K[3][1]+K[4][0]=='OOOOO':ans+=1
    if ans>=3:print(t+1);exit()
D=[[*map(int,input().split())] for _ in range(5)]
K=[['.']*5 for _ in range(5)]
S=[[*map(int,input().split())] for _ in range(5)]
for t in range(25):
    a=t//5
    b=t%5
    for i in range(5):
        for j in range(5):
            if D[i][j]==S[a][b]:
                K[i][j]='O';Check()

Algorithm and Data Structure/Baekjoon/2000번대/02500번대/02578번 빙고/good answer.py


import pandas as pd
from optparse import OptionParser
import os

def run_apriori(transaction, min_support, min_confidence):
    support_list = []
    candidate_item_set_list = get_1_item_set(transaction)
    support = {tuple([int(item)]): 0 for item in candidate_item_set_list}
    for t in transaction:
        items = set(t.split(','))
        for item in items:
            support[tuple([int(item)])] += 1
    length = 1
    while True:
        print('Length:{}\n# of candidate item set: {}'.format(length, len(candidate_item_set_list)))
        length += 1
        support = {k: v for k, v in support.items() if v >= min_support}
        support_list.append([(k, v) for k, v in support.items()])
        candidate_item_set_list = generate_candidate_item_set([x for x in support.keys()], length)
        if len(candidate_item_set_list) == 0:
            break
        transaction = [x for x in transaction if len(x.split(',')) >= length]
        # transaction = transaction[transaction.apply(lambda x: len(x.split(',')) >= length, axis=1)]
        support = {tuple(sorted(item)): 0 for item in candidate_item_set_list}
        for t in transaction:
            items = set(int(x) for x in t.split(','))
            for k, v in support.items():
                if set(k).issubset(items):
                    support[k] += 1
    return support_list, get_confidence(support_list, min_confidence)

def get_confidence(support_list, confidence):
    """
    Return the confidence rules. confidence(pre->post)=support(pre+post)/support(pre)
    :param support_list: [((item_list), support)]
    :param confidence: minimum confidence
    :return: rules: [((pre, post), confidence)]
    """
    result = []
    for item_list in support_list[1:][::-1]:
        for support_item in item_list:
            rules = generate_rules(support_item)
            for pre, post in rules:
                pre_length = len(pre) - 1
                pre_support = [x for x in filter(lambda x: x[0] == pre, support_list[pre_length])][0][1]
                support_item_list_support = [x for x in filter(
                    lambda x: x[0] == support_item[0], support_list[len(support_item[0]) - 1])][0][1]
                this_confidence = float(support_item_list_support) / pre_support
                if this_confidence >= confidence:
                    result.append(tuple([tuple([pre, post]), this_confidence]))
    return result

def generate_rules(items):
    """
    Guarantee that len(pre)+len(post)=len(items)
    :param items: list of frequent items
    :return: [(pre, post)]
    """
    result = []
    support_item = items[0]
    import itertools
    for length in range(1, len(support_item)):
        pre_list = itertools.combinations(support_item, length)
        for pre_item in pre_list:
            post_item = tuple(set(support_item) - set(pre_item))
            result.append((tuple(sorted(pre_item)), tuple(sorted(post_item))))
    return result

def generate_candidate_item_set(item_list, length):
    """
    Generate candidate item list whose length is specified
    :param item_list: a list of frequent item set
    :param length: each frequent-item-set's length
    :return: candidate item set
    """
    result = []
    for index1 in range(len(item_list) - 1):
        for index2 in range(index1 + 1, len(item_list)):
            union = set(item_list[index1]).union(set(item_list[index2]))
            if len(union) == length:
                result.append(tuple(union))
    return result

def get_1_item_set(transaction):
    result = set()
    for t in transaction:
        items = t.split(',')
        for item in items:
            result.add(item)
    return list(result)

def filter_purchase_record(user_behavior_record):
    purchase = user_behavior_record[user_behavior_record['action_type'].isin([2])]
    selected = ['user_id', 'item_id', 'time_stamp']
    purchase = purchase.filter(items=selected)
    print('Finish filtering purchase records, size: {} lines of record.'.format(len(purchase)))
    return purchase.sort_values(by=['time_stamp'])

def construct_transaction(user_behavior_file, truncate_log_size, transaction_file='data_set/transaction.csv'):
    if transaction_file and os.path.exists(transaction_file):
        transaction = pd.read_csv(transaction_file)
    else:
        user_behavior_record = construct_user_behavior_list(user_behavior_file, truncate_log_size)
        purchase_record = filter_purchase_record(user_behavior_record)
        groups = purchase_record.groupby(['time_stamp', 'user_id'])  # transaction = same user buys items in same day
        columns = ['items']
        transaction = pd.DataFrame(columns=columns)
        for t in groups:
            items = t[1]
            transaction = transaction.append(
                {
                    'items': ','.join(items['item_id'].astype('str').values)
                }, ignore_index=True)
        transaction.to_csv('data_set/transaction.csv', index=False)
        print('Finish constructing transaction record, size: {} lines of record'.format(len(transaction)))
    transaction = transaction[:1024 * 16].values
    transaction = [x[0] for x in transaction]
    return transaction

def construct_user_behavior_list(user_behavior_file, truncate_log_size):
    result = pd.read_csv(user_behavior_file)
    print('Finish reading user behavior file, size: {} lines of record.'.format(len(result)))
    result.sample(frac=1)
    result = result[:truncate_log_size]
    print('Finish shuffling and truncating user behavior record, size: {} lines of record.'.format(len(result)))
    return result

def generate_result_string(support_list, rules):
    result = ['------------------ FREQUENT ITEM SET ------------------']
    for i in range(0, len(support_list)):
        length = i + 1
        result.append('Item set length: {}'.format(length))
        for item, support in support_list[i]:
            result.append('\titem: {}, sup: {}'.format(item, support))
    result.append("\n------------------------ RULES ------------------------")
    for rule_item in rules:
        rule, confidence = rule_item
        pre, post = rule
        result.append("rule: {} ==> {}, confidence: {}".format(pre, post, confidence))
    return '\n'.join(result)

def write_to_file(result, destination='result/apriori_result.txt'):
    if not os.path.exists('result'):
        os.makedirs('result')
    with open(destination, 'w') as f:
        f.write(result)

def dispose():
    df = pd.read_csv('data_set/transaction.csv')
    columns = ['items']
    t = pd.DataFrame(columns=columns)
    for index, row in df.iterrows():
        t = t.append(
            {
                'items': row['items']
            }, ignore_index=True)
    t.to_csv('data_set/transaction_disposed.csv', index=False)

def main():
    opt_parser = OptionParser()
    opt_parser.add_option('-f', '--input_file',
                          dest='input',
                          help='user behavior record csv',
                          default='data_set/user_log_format1.csv')
    opt_parser.add_option('-s', '--min_support',
                          dest='min_support',
                          help='minimum support value',
                          default=3,
                          type='int')
    opt_parser.add_option('-c', '--min_confidence',
                          dest='min_confidence',
                          help='minimum confidence value',
                          default=0.6,
                          type='float')
    opt_parser.add_option('-t', '--truncate_log_size',
                          dest='truncate_log_size',
                          help='truncate log size',
                          default=1024 * 1024 * 16,
                          type='int')
    (options, args) = opt_parser.parse_args()

    # user_behavior = construct_user_behavior_list(options.input, options.truncate_log_size)
    transaction_record = construct_transaction(options.input, options.truncate_log_size)
    support_item_list, confidence_rules = run_apriori(transaction_record, options.min_support, options.min_confidence)
    result_string = generate_result_string(support_item_list, confidence_rules)
    print(result_string)
    write_to_file(result_string)

if __name__ == '__main__':
    main()


class Solution(object):
    def getUglyNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        i = j = k = 0
        res = [0] * n # 开辟空间,不能res = []
        res[0] = 1
        for s in range(1,n):
            res[s] = min(res[i] * 2, min(res[j] * 3, res[k] * 5))
            if res[s] == res[i] * 2:
                i += 1
            if res[s] == res[j] * 3: # 不用else是为了去重: 6 = 2 * 3
                j += 1
            if res[s] == res[k] * 5:
                k += 1
        return res.pop()


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

import sys
import os
import warnings
import torch
import torch.nn as nn
from torchvision import datasets, transforms
import numpy as np
import argparse
import json
import cv2
import time
from models.net_ss_d_0_net0 import net
from utils.utils_ss_d import save_checkpoint
from data.data_loader_ss_d_0 import ImageDataset

parser = argparse.ArgumentParser(description='PyTorch rain24')
parser.add_argument('--pre', '-p', metavar='PRETRAINED', default=None, type=str, help='path to the pretrained model')
args = parser.parse_args()
args.best_precision = 0
args.best_mae = sys.maxsize
args.best_mse = sys.maxsize
args.best_epoch = -1
checkpoint_save_dir = '/home/rainkeeper/Projects/PycharmProjects/rain24/checkpoints_rain24_ss_d_0_0_try0'
terminal_log_file = os.path.join(checkpoint_save_dir, 'terminal_log.txt')
terminal_file = open(terminal_log_file, 'a')

def main():
    args.lr = 1e-4
    args.batch_size = 1
    args.momentum = 0.95
    args.decay = 5 * 1e-4
    args.alpha = 1
    args.start_epoch = 0
    args.epochs = 200
    args.workers = 2
    args.seed = time.time()
    args.print_freq = 400
    args.gpu_id = "cuda:1"
    args.dataset_id = 1

    train_image_dir = '/home/rainkeeper/Projects/Datasets/few_shot/self_supervised_data' + str(args.dataset_id) + '/Train_Image/crop_distance'
    val_image_dir = '/home/rainkeeper/Projects/Datasets/few_shot/self_supervised_data' + str(args.dataset_id) + '/Test_Image/crop_distance'

    args.device = torch.device(args.gpu_id if torch.cuda.is_available() else "cpu")
    torch.cuda.manual_seed(args.seed)

    model = net()
    model.to(args.device)

    criterion = nn.MarginRankingLoss(margin=1.0, reduction='sum').to(args.device)
    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr)

    for epoch in range(args.start_epoch, args.start_epoch + args.epochs):
        print('begin train...')
        print('begin train...', file=terminal_file)
        train_accuracy = train(train_image_dir, model, criterion, optimizer, epoch)
        print('begin test...')
        print('begin test...', file=terminal_file)
        val_accuracy = val(val_image_dir, model, epoch)
        print('\n')
        print('\n', file=terminal_file)

        if epoch >= 0:
            save_checkpoint({
                'epoch': epoch + 1,
                'arch': args.pre,
                'state_dict': model.state_dict(),
                'optimizer': optimizer.state_dict()
            }, checkpoint_save_dir, epoch, train_accuracy, val_accuracy)

def train(train_image_dir, model, criterion, optimizer, epoch):
    losses = AverageMeter()

    trainset = ImageDataset(img_folder=train_image_dir,
                            train=True,
                            transform=transforms.Compose([
                                transforms.ToTensor()
                            ])
                            )
    train_loader = torch.utils.data.DataLoader(trainset, shuffle=True, batch_size=args.batch_size,
                                               num_workers=args.workers)
    print('epoch %d, processed %d samples, lr %.10f' % (
        epoch, epoch * len(train_loader.dataset), args.lr))
    print('epoch %d, processed %d samples, lr %.10f' % (
        epoch, epoch * len(train_loader.dataset), args.lr), file=terminal_file)

    model.train()

    count = 0
    for i, (imgs, img_name) in enumerate(train_loader):
        images = [img[1].to(args.device) for img in imgs]
        labels = [img[0].to(args.device) for img in imgs]

        distances = model(images)      # 定义: 人越小，distance越大

        x1 = torch.empty(9).to(args.device)
        x2 = torch.empty(9).to(args.device)
        y = torch.empty(9).to(args.device)
        l1 = torch.empty(9).to(args.device)
        l2 = torch.empty(9).to(args.device)

        index = 0

        for m in range(6):
            for n in range(m + 1, 6, 1):
                if labels[m] != labels[n]:
                    x1[index] = distances[m]
                    x2[index] = distances[n]
                    gt_label = torch.FloatTensor([-1]) if labels[m] < labels[n] else torch.FloatTensor([1])
                    gt_label = gt_label.to(args.device)
                    y[index] = gt_label
                    l1[index] = labels[m]
                    l2[index] = labels[n]
                    index += 1

        assert index == 9

        loss = criterion(x1, x2, y)

        losses.update(loss.item(), 1)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if i % args.print_freq == 0:
            print('Epoch: [{0}][{1}/{2}]\t'
                  'Loss {loss.val:.8f} ({loss.avg:.8f})\t'
                .format(
                epoch, i, len(train_loader), loss=losses), file=terminal_file)
            print('Epoch: [{0}][{1}/{2}]\t'
                  'Loss {loss.val:.8f} ({loss.avg:.8f})\t'
                .format(
                epoch, i, len(train_loader), loss=losses))

        x1 = [x_temp.data.cpu().numpy() for x_temp in x1]
        x2 = [x_temp.data.cpu().numpy() for x_temp in x2]
        y = [y_temp.data.cpu().numpy() for y_temp in y]
        l1 = [l_temp.data.cpu().numpy() for l_temp in l1]
        l2 = [l_temp.data.cpu().numpy() for l_temp in l2]

        for p in range(len(x1)):
            if x1[p] < x2[p] and l1[p] < l2[p]:
                count += 1
            if x1[p] > x2[p] and l1[p] > l2[p]:
                count += 1

    print('count=', count)
    print('train_image_num=', len(train_loader.dataset))

    accuracy = count / (9 * len(train_loader.dataset))

    print('Epoch: [{0}][{1}]\t'
          'Train Accuracy {accuracy:.6f}\t'
        .format(
        epoch, len(train_loader.dataset), accuracy=accuracy))
    print('Epoch: [{0}][{1}]\t'
          'Train Accuracy {accuracy:.6f}\t'
        .format(
        epoch, len(train_loader.dataset), accuracy=accuracy), file=terminal_file)

    return accuracy

def val(val_image_dir, model, epoch):
    valset = ImageDataset(img_folder=val_image_dir,
                          train=False,
                          transform=transforms.Compose([
                              transforms.ToTensor(),
                          ]),
                          )
    val_loader = torch.utils.data.DataLoader(valset, shuffle=False, batch_size=args.batch_size,
                                               num_workers=args.workers)
    print('epoch %d, processed %d samples, lr %.10f' % (
        epoch, epoch * len(val_loader.dataset), args.lr))
    print('epoch %d, processed %d samples, lr %.10f' % (
        epoch, epoch * len(val_loader.dataset), args.lr), file=terminal_file)

    model.eval()
    end = time.time()

    count = 0
    with torch.no_grad():
        for i, (imgs, img_name) in enumerate(val_loader):
            images = [img[1].to(args.device) for img in imgs]
            labels = [img[0].to(args.device) for img in imgs]

            distances = model(images)

            distances = [distance.data.cpu().numpy() for distance in distances]
            labels = [label.data.cpu().numpy() for label in labels]

            for m in range(6):
                for n in range(m + 1, 6, 1):
                    if (labels[m] - labels[n]) * (distances[m] - distances[n]) > 0:
                        count += 1

            if i % 100 == 0:
                for m in range(len(distances)):
                    print('distances: %.6f, labels: %d' % (distances[m], labels[m]))
                print('\n')
                for m in range(len(distances)):
                    print('distances: %.6f, labels: %d' % (distances[m], labels[m]), file=terminal_file)
                print('\n', file=terminal_file)

    accuracy = count / (9 * len(val_loader.dataset))

    print('count=', count)
    print('test_image_num=', len(val_loader.dataset))

    print('Epoch: [{0}][{1}]\t'
          'Test Accuracy {accuracy:.6f}\n'
        .format(
        epoch, len(val_loader.dataset), accuracy=accuracy), file=terminal_file)
    print('Epoch: [{0}][{1}]\t'
          'Test Accuracy {accuracy:.6f}\n'
        .format(
        epoch, len(val_loader.dataset), accuracy=accuracy))

    return accuracy

class AverageMeter(object):
    def __init__(self):
        self.reset()
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

if __name__ == '__main__':
    main()