import tensorflow as tf
from board_class import Board
from memory_class import Memory
from critic_class import Critic
from actor_class import Actor
import numpy as np

gamma = 0.5#discount factor
batch_size = 200

def fix_policy(state, policy):
    for i in range(9):
        if state[i] != 0:
            policy[i] = 0
    probsum = np.sum(policy)
    return policy/probsum

def choose_action(state, actor, sess):
    policy = actor.get_policy(state.reshape((1, 9)), sess)[0]
    policy = fix_policy(state, policy)
    return np.random.choice(9, p=policy)

#we don't want the network to be different for X and O, so we make each player see the board as X would
def state_from_board(board, counter):
    state = np.array(board.board)
    if counter == 1:
        state = -state
    state = state
    return state

memory = Memory(3000)

actor = Actor(0.001)
critic = Critic(0.001)

with tf.Session() as sess:
    sess.run(actor.var_init)
    sess.run(critic.var_init)
    for game in range(4500):
        if game%100 == 0:
            print(game)
        board = Board()
        winner = ''
        counter = 0
        symbols = ['X', 'O']
        #we need to store samples temporarily because we don't get their values till the end of each game
        samples = []#each sample contains state, action, reward, and next state
        while winner == '':
            state = state_from_board(board, counter)

            action = choose_action(state, actor, sess)

            current_sample = []
            current_sample.append(state)
            current_sample.append(action)

            winner = board.setSquare(action, symbols[counter])
            current_sample.append(0.5)#placeholder reward. we change this when we know the winner

            samples.append(current_sample)
            #switch to next player
            counter = (counter + 1)%2

        #lol this is so ugly
        xreward = 0
        if winner == 'X':
            xreward = 0.5
        elif winner == 'O':
            xreward = -0.5

        #add the next state to each sample and set rewards based on winner
        num_samples = len(samples)
        for i in range(num_samples):
            #next state
            if i < num_samples - 2:
                samples[i].append(samples[i + 2][0])
            else:
                samples[i].append(None)

            if i%2 == 0:
                samples[i][2] = samples[i][2] + xreward*(i+1)/num_samples
            else:
                samples[i][2] = samples[i][2] - xreward*(i+1)/num_samples
            memory.add_sample(samples[i])

        sample_batch = memory.sample_samples(batch_size)
        actual_batch_size = len(sample_batch)
        state_batch = np.zeros((actual_batch_size, 9))
        next_state_batch = np.zeros((actual_batch_size, 9))
        action_batch = np.array([sample[1] for sample in sample_batch])

        for i, sample in enumerate(sample_batch):
            state_batch[i] = sample[0]
            if sample[3] is not None:
                next_state_batch[i] = sample[3]

        qsa_batch = critic.predict_batch(state_batch, sess)
        policy_batch = actor.get_policy_batch(state_batch, sess)
        advantage_batch = np.array([0 for i in range(actual_batch_size)])

        next_qsa_batch = critic.predict_batch(next_state_batch, sess)
        next_policy_batch = actor.get_policy_batch(next_state_batch, sess)
        #next_value_batch = critic.predict_value_batch(next_state_batch, sess)

        #fix up the policy so that invalid actions have a probability of zero
        for i in range(actual_batch_size):
            policy_batch[i] = fix_policy(state_batch[i, :], policy_batch[i, :])
            next_policy_batch[i] = fix_policy(next_state_batch[i, :], next_policy_batch[i, :])

        for i in range(actual_batch_size):
            #dot product is used here to compute expected value
            next_value = np.dot(next_qsa_batch[i], next_policy_batch[i])
            #next_value = next_value_batch[i]

            if sample_batch[i][3] is not None:
                qsa_batch[i, action_batch[i]] = sample_batch[i][2] + gamma*next_value
            else:
                qsa_batch[i, action_batch[i]] = sample_batch[i][2]

            advantage_batch[i] = qsa_batch[i, action_batch[i]]

        critic.train_batch(state_batch, qsa_batch, sess)
        actor.train_batch(state_batch, action_batch, advantage_batch, sess)
    critic.save(sess, 'tic_tac_toe_critic_nobad_a2c')
    actor.save(sess, 'tic_tac_toe_actor_nobad_a2c')
    critic.plot_losses('a2c_critic_losses')
    actor.plot_scores('a2c_scores')


from numpy import prod

import cupy
from cupy.fft import config
from cupy.fft._fft import (_convert_fft_type, _default_fft_func, _fft,
                           _get_cufft_plan_nd, _get_fftn_out_size,
                           _output_dtype)
from cupy.fft._cache import get_plan_cache

def get_fft_plan(a, shape=None, axes=None, value_type='C2C'):
    """ Generate a CUDA FFT plan for transforming up to three axes.

    Args:
        a (cupy.ndarray): Array to be transform, assumed to be either C- or
            F- contiguous.
        shape (None or tuple of ints): Shape of the transformed axes of the
            output. If ``shape`` is not given, the lengths of the input along
            the axes specified by ``axes`` are used.
        axes (None or int or tuple of int):  The axes of the array to
            transform. If `None`, it is assumed that all axes are transformed.

            Currently, for performing N-D transform these must be a set of up
            to three adjacent axes, and must include either the first or the
            last axis of the array.
        value_type (str): The FFT type to perform. Acceptable values are:

            * 'C2C': complex-to-complex transform (default)
            * 'R2C': real-to-complex transform
            * 'C2R': complex-to-real transform

    Returns:
        a cuFFT plan for either 1D transform (``cupy.cuda.cufft.Plan1d``) or
        N-D transform (``cupy.cuda.cufft.PlanNd``).

    .. note::
        The returned plan can not only be passed as one of the arguments of
        the functions in ``cupyx.scipy.fftpack``, but also be used as a
        context manager for both ``cupy.fft`` and ``cupyx.scipy.fftpack``
        functions:

        .. code-block:: python

            x = cupy.random.random(16).reshape(4, 4).astype(complex)
            plan = cupyx.scipy.fftpack.get_fft_plan(x)
            with plan:
                y = cupy.fft.fftn(x)
                # alternatively:
                y = cupyx.scipy.fftpack.fftn(x)  # no explicit plan is given!
            # alternatively:
            y = cupyx.scipy.fftpack.fftn(x, plan=plan)  # pass plan explicitly

        In the first case, no cuFFT plan will be generated automatically,
        even if ``cupy.fft.config.enable_nd_planning = True`` is set.

    .. note::
        If this function is called under the context of
        :func:`~cupy.fft.config.set_cufft_callbacks`, the generated plan will
        have callbacks enabled.

    .. warning::
        This API is a deviation from SciPy's, is currently experimental, and
        may be changed in the future version.
    """
    from cupy.cuda import cufft

    # check input array
    if a.flags.c_contiguous:
        order = 'C'
    elif a.flags.f_contiguous:
        order = 'F'
    else:
        raise ValueError('Input array a must be contiguous')

    if isinstance(shape, int):
        shape = (shape,)
    if isinstance(axes, int):
        axes = (axes,)
    if (shape is not None) and (axes is not None) and len(shape) != len(axes):
        raise ValueError('Shape and axes have different lengths.')

    # check axes
    # n=1: 1d (need axis1D); n>1: Nd
    if axes is None:
        n = a.ndim if shape is None else len(shape)
        axes = tuple(i for i in range(-n, 0))
        if n == 1:
            axis1D = 0
    else:  # axes is a tuple
        n = len(axes)
        if n == 1:
            axis1D = axes[0]
            if axis1D >= a.ndim or axis1D < -a.ndim:
                err = 'The chosen axis ({0}) exceeds the number of '\
                      'dimensions of a ({1})'.format(axis1D, a.ndim)
                raise ValueError(err)
        elif n > 3:
            raise ValueError('Only up to three axes is supported')

    # Note that "shape" here refers to the shape along trasformed axes, not
    # the shape of the output array, and we need to convert it to the latter.
    # The result is as if "a=_cook_shape(a); return a.shape" is called.
    # Because of this, we need to use (possibly unsorted) axes.
    transformed_shape = shape
    shape = list(a.shape)
    if transformed_shape is not None:
        for s, axis in zip(transformed_shape, axes):
            if s is not None:
                if axis == axes[-1] and value_type == 'C2R':
                    s = s // 2 + 1
                shape[axis] = s
    shape = tuple(shape)

    # check value_type
    out_dtype = _output_dtype(a.dtype, value_type)
    fft_type = _convert_fft_type(out_dtype, value_type)
    # TODO(leofang): figure out if we really have to skip F-order?
    if n > 1 and value_type != 'C2C' and a.flags.f_contiguous:
        raise ValueError('C2R/R2C PlanNd for F-order arrays is not supported')

    # generate plan
    # (load from cache if it exists, otherwise create one but don't cache it)
    if n > 1:  # ND transform
        if cupy.cuda.runtime.is_hip and value_type == 'C2R':
            raise RuntimeError("hipFFT's C2R PlanNd is buggy and unsupported")
        out_size = _get_fftn_out_size(
            shape, transformed_shape, axes[-1], value_type)
        # _get_cufft_plan_nd interacts with plan cache and callback
        plan = _get_cufft_plan_nd(
            shape, fft_type, axes=axes, order=order, out_size=out_size,
            to_cache=False)
    else:  # 1D transform
        # prepare plan arguments
        if value_type != 'C2R':
            out_size = shape[axis1D]
        else:
            out_size = _get_fftn_out_size(
                shape, transformed_shape, axis1D, value_type)
        batch = prod(shape) // shape[axis1D]
        devices = None if not config.use_multi_gpus else config._devices

        keys = (out_size, fft_type, batch, devices)
        mgr = config.get_current_callback_manager()
        if mgr is not None:
            # to avoid a weird segfault, we generate and cache distinct plans
            # for every possible (load_aux, store_aux) pairs; the plans are
            # still generated from the same external Python module
            load_aux = mgr.cb_load_aux_arr
            store_aux = mgr.cb_store_aux_arr
            keys += (mgr.cb_load, mgr.cb_store,
                     0 if load_aux is None else load_aux.data.ptr,
                     0 if store_aux is None else store_aux.data.ptr)
        cache = get_plan_cache()
        cached_plan = cache.get(keys)
        if cached_plan is not None:
            plan = cached_plan
        elif mgr is None:
            plan = cufft.Plan1d(out_size, fft_type, batch, devices=devices)
        else:  # has callback
            # TODO(leofang): support multi-GPU callback (devices is ignored)
            if devices:
                raise NotImplementedError('multi-GPU cuFFT callbacks are not '
                                          'yet supported')
            plan = mgr.create_plan(('Plan1d', keys[:-3]))
            mgr.set_callbacks(plan)

    return plan

def fft(x, n=None, axis=-1, overwrite_x=False, plan=None):
    """Compute the one-dimensional FFT.

    Args:
        x (cupy.ndarray): Array to be transformed.
        n (None or int): Length of the transformed axis of the output. If ``n``
            is not given, the length of the input along the axis specified by
            ``axis`` is used.
        axis (int): Axis over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axis``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(x, axis)

            Note that `plan` is defaulted to None, meaning CuPy will use an
            auto-generated plan behind the scene.

    Returns:
        cupy.ndarray:
            The transformed array which shape is specified by ``n`` and type
            will convert to complex if that of the input is another.

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.

    .. seealso:: :func:`scipy.fftpack.fft`
    """
    from cupy.cuda import cufft
    return _fft(x, (n,), (axis,), None, cufft.CUFFT_FORWARD,
                overwrite_x=overwrite_x, plan=plan)

def ifft(x, n=None, axis=-1, overwrite_x=False, plan=None):
    """Compute the one-dimensional inverse FFT.

    Args:
        x (cupy.ndarray): Array to be transformed.
        n (None or int): Length of the transformed axis of the output. If ``n``
            is not given, the length of the input along the axis specified by
            ``axis`` is used.
        axis (int): Axis over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axis``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(x, axis)

            Note that `plan` is defaulted to None, meaning CuPy will use an
            auto-generated plan behind the scene.

    Returns:
        cupy.ndarray:
            The transformed array which shape is specified by ``n`` and type
            will convert to complex if that of the input is another.

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.

    .. seealso:: :func:`scipy.fftpack.ifft`
    """
    from cupy.cuda import cufft
    return _fft(x, (n,), (axis,), None, cufft.CUFFT_INVERSE,
                overwrite_x=overwrite_x, plan=plan)

def fft2(x, shape=None, axes=(-2, -1), overwrite_x=False, plan=None):
    """Compute the two-dimensional FFT.

    Args:
        x (cupy.ndarray): Array to be transformed.
        shape (None or tuple of ints): Shape of the transformed axes of the
            output. If ``shape`` is not given, the lengths of the input along
            the axes specified by ``axes`` are used.
        axes (tuple of ints): Axes over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axes``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(x, axes)

            Note that `plan` is defaulted to None, meaning CuPy will either
            use an auto-generated plan behind the scene if cupy.fft.config.
            enable_nd_planning = True, or use no cuFFT plan if it is set to
            False.

    Returns:
        cupy.ndarray:
            The transformed array which shape is specified by ``shape`` and
            type will convert to complex if that of the input is another.

    .. seealso:: :func:`scipy.fftpack.fft2`

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.
    """
    from cupy.cuda import cufft

    func = _default_fft_func(x, shape, axes, plan)
    return func(x, shape, axes, None, cufft.CUFFT_FORWARD,
                overwrite_x=overwrite_x, plan=plan)

def ifft2(x, shape=None, axes=(-2, -1), overwrite_x=False, plan=None):
    """Compute the two-dimensional inverse FFT.

    Args:
        x (cupy.ndarray): Array to be transformed.
        shape (None or tuple of ints): Shape of the transformed axes of the
            output. If ``shape`` is not given, the lengths of the input along
            the axes specified by ``axes`` are used.
        axes (tuple of ints): Axes over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axes``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(x, axes)

            Note that `plan` is defaulted to None, meaning CuPy will either
            use an auto-generated plan behind the scene if cupy.fft.config.
            enable_nd_planning = True, or use no cuFFT plan if it is set to
            False.

    Returns:
        cupy.ndarray:
            The transformed array which shape is specified by ``shape`` and
            type will convert to complex if that of the input is another.

    .. seealso:: :func:`scipy.fftpack.ifft2`

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.
    """
    from cupy.cuda import cufft

    func = _default_fft_func(x, shape, axes, plan)
    return func(x, shape, axes, None, cufft.CUFFT_INVERSE,
                overwrite_x=overwrite_x, plan=plan)

def fftn(x, shape=None, axes=None, overwrite_x=False, plan=None):
    """Compute the N-dimensional FFT.

    Args:
        x (cupy.ndarray): Array to be transformed.
        shape (None or tuple of ints): Shape of the transformed axes of the
            output. If ``shape`` is not given, the lengths of the input along
            the axes specified by ``axes`` are used.
        axes (tuple of ints): Axes over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axes``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(x, axes)

            Note that `plan` is defaulted to None, meaning CuPy will either
            use an auto-generated plan behind the scene if cupy.fft.config.
            enable_nd_planning = True, or use no cuFFT plan if it is set to
            False.

    Returns:
        cupy.ndarray:
            The transformed array which shape is specified by ``shape`` and
            type will convert to complex if that of the input is another.

    .. seealso:: :func:`scipy.fftpack.fftn`

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.
    """
    from cupy.cuda import cufft

    func = _default_fft_func(x, shape, axes, plan)
    return func(x, shape, axes, None, cufft.CUFFT_FORWARD,
                overwrite_x=overwrite_x, plan=plan)

def ifftn(x, shape=None, axes=None, overwrite_x=False, plan=None):
    """Compute the N-dimensional inverse FFT.

    Args:
        x (cupy.ndarray): Array to be transformed.
        shape (None or tuple of ints): Shape of the transformed axes of the
            output. If ``shape`` is not given, the lengths of the input along
            the axes specified by ``axes`` are used.
        axes (tuple of ints): Axes over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.PlanNd` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axes``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(x, axes)

            Note that `plan` is defaulted to None, meaning CuPy will either
            use an auto-generated plan behind the scene if cupy.fft.config.
            enable_nd_planning = True, or use no cuFFT plan if it is set to
            False.

    Returns:
        cupy.ndarray:
            The transformed array which shape is specified by ``shape`` and
            type will convert to complex if that of the input is another.

    .. seealso:: :func:`scipy.fftpack.ifftn`

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.
    """
    from cupy.cuda import cufft

    func = _default_fft_func(x, shape, axes, plan)
    return func(x, shape, axes, None, cufft.CUFFT_INVERSE,
                overwrite_x=overwrite_x, plan=plan)

def rfft(x, n=None, axis=-1, overwrite_x=False, plan=None):
    """Compute the one-dimensional FFT for real input.

    The returned real array contains

    .. code-block:: python

        [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2))]  # if n is even
        [y(0),Re(y(1)),Im(y(1)),...,Re(y(n/2)),Im(y(n/2))]  # if n is odd

    Args:
        x (cupy.ndarray): Array to be transformed.
        n (None or int): Length of the transformed axis of the output. If ``n``
            is not given, the length of the input along the axis specified by
            ``axis`` is used.
        axis (int): Axis over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.
        plan (:class:`cupy.cuda.cufft.Plan1d` or ``None``): a cuFFT plan for
            transforming ``x`` over ``axis``, which can be obtained using::

                plan = cupyx.scipy.fftpack.get_fft_plan(
                    x, axes, value_type='R2C')

            Note that `plan` is defaulted to None, meaning CuPy will either
            use an auto-generated plan behind the scene if cupy.fft.config.
            enable_nd_planning = True, or use no cuFFT plan if it is set to
            False.

    Returns:
        cupy.ndarray:
            The transformed array.

    .. seealso:: :func:`scipy.fftpack.rfft`

    .. note::
       The argument `plan` is currently experimental and the interface may be
       changed in the future version.
    """
    from cupy.cuda import cufft

    if n is None:
        n = x.shape[axis]

    shape = list(x.shape)
    shape[axis] = n
    f = _fft(x, (n,), (axis,), None, cufft.CUFFT_FORWARD, 'R2C',
             overwrite_x=overwrite_x, plan=plan)
    z = cupy.empty(shape, f.real.dtype)

    slice_z = [slice(None)] * x.ndim
    slice_f = [slice(None)] * x.ndim

    slice_z[axis] = slice(1)
    slice_f[axis] = slice(1)
    z[tuple(slice_z)] = f[tuple(slice_f)].real

    slice_z[axis] = slice(1, None, 2)
    slice_f[axis] = slice(1, None)
    z[tuple(slice_z)] = f[tuple(slice_f)].real

    slice_z[axis] = slice(2, None, 2)
    slice_f[axis] = slice(1, n - f.shape[axis] + 1)
    z[tuple(slice_z)] = f[tuple(slice_f)].imag

    return z

def irfft(x, n=None, axis=-1, overwrite_x=False):
    """Compute the one-dimensional inverse FFT for real input.

    Args:
        x (cupy.ndarray): Array to be transformed.
        n (None or int): Length of the transformed axis of the output. If ``n``
            is not given, the length of the input along the axis specified by
            ``axis`` is used.
        axis (int): Axis over which to compute the FFT.
        overwrite_x (bool): If True, the contents of ``x`` can be destroyed.

    Returns:
        cupy.ndarray:
            The transformed array.

    .. seealso:: :func:`scipy.fftpack.irfft`

    .. note::
       This function does not support a precomputed `plan`. If you need this
       capability, please consider using :func:`cupy.fft.irfft` or :func:`
       cupyx.scipy.fft.irfft`.
    """
    from cupy.cuda import cufft

    if n is None:
        n = x.shape[axis]
    m = min(n, x.shape[axis])

    shape = list(x.shape)
    shape[axis] = n // 2 + 1
    if x.dtype in (cupy.float16, cupy.float32):
        z = cupy.zeros(shape, dtype=cupy.complex64)
    else:
        z = cupy.zeros(shape, dtype=cupy.complex128)

    slice_x = [slice(None)] * x.ndim
    slice_z = [slice(None)] * x.ndim

    slice_x[axis] = slice(1)
    slice_z[axis] = slice(1)
    z[tuple(slice_z)].real = x[tuple(slice_x)]

    slice_x[axis] = slice(1, m, 2)
    slice_z[axis] = slice(1, m // 2 + 1)
    z[tuple(slice_z)].real = x[tuple(slice_x)]

    slice_x[axis] = slice(2, m, 2)
    slice_z[axis] = slice(1, (m + 1) // 2)
    z[tuple(slice_z)].imag = x[tuple(slice_x)]

    return _fft(z, (n,), (axis,), None, cufft.CUFFT_INVERSE, 'C2R',
                overwrite_x=overwrite_x)


from django.contrib import admin
from django.urls import path
from tareas import views

urlpatterns = [
    path("admin/", admin.site.urls),

    path ("", views.menu, name = "menu"),

    path ("registro/", views.registro, name = "registro"),
    path ("iniciar_sesion/", views.iniciar_sesion, name = "iniciar_sesion"),
    path ("salir/", views.salir, name = "salir"),

    path ("crear_tarea/", views.crear_tarea, name = "crear_tarea"),
    path ("tareas/", views.tareas, name = "tareas"),
    path ("tarea/<int:tarea_id>", views.tarea, name = "tarea"),
    path ("tarea/<int:tarea_id>/completa", views.tarea_completa, name = "tarea_completa"),
    path ("tarea/<int:tarea_id>/borada", views.borar_tarea, name = "borar_tarea")
]

Programacion/002 ejemplos/002 - 14 django/16 django proyrcto inicio de cesion/django_crud/urls.py


a = [1 ,2 ,3 ,4 ,5]
print(a)

print(a[3])

n = 10
a = [0] * n
a[0] = 1
print(a)

array = [i for i in range(10)]
print(array)

# 리스트 컴프리 핸션
# 이 방식을 사용하지 않고 단순히 나타내면 콜바이 레퍼런스로 변해서, 데이터 전체가 변하게 된다.
array_2 = [[0] * 10 for _ in range(10) ]
array_2[0][0] = 1

# 리스트 에서 특정 값 제거하기
a = [1, 2, 3, 4, 5, 5]
remove_set = {3, 5}

answer = [i for i in a if i not in remove_set ]

print(answer)


import regTrees
from numpy import *
import matplotlib.pyplot as plt

myDat = regTrees.loadDataSet('ex00.txt')
myMat = mat(myDat)
print(regTrees.createTree(myMat))
plt.plot(myMat[:,0],myMat[:,1], 'ro')
plt.show()

myDat1 = regTrees.loadDataSet('ex0.txt')
myMat1 = mat(myDat1)
print(regTrees.createTree(myMat1))
plt.plot(myMat1[:,1],myMat1[:,2], 'ro')
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from hs_digitizer import *
import glob
import scipy.signal as ss
from scipy.optimize import curve_fit
import re
import matplotlib

#Ns = 500000
#Fs = 200000.
path = "/data/20181030/bead1/high_speed_digitizer/golden_data/amp_ramp_50k_good"
files = glob.glob(path + "/*.h5")
fi_init = 1e5
init_file = 0
final_file = len(files)
n_file = final_file-init_file

sfun = lambda fname: int(re.findall('\d+.h5', fname)[0][:-3])

files.sort(key = sfun)
bw = 2000.
bw_sb = 0.02
obj0 = hsDat(files[init_file])
t0 = obj0.attribs['time']
Ns = obj0.attribs['nsamp']
Fs = obj0.attribs['fsamp']
freqs = np.fft.rfftfreq(Ns, d = 1./Fs)
tarr0 = np.linspace(0, Ns/Fs, Ns)

def line(x, m, b):
    return m*x + b

def dec2(arr, fac):
    return ss.decimate(ss.decimate(arr, fac), fac)

def sqrt_fun(x, a):
    return a*np.sqrt(x)

fc = fi_init
plot_dat = True

matplotlib.rcParams.update({'font.size':12})
f, ax = plt.subplots(dpi = 200)
files = np.array(files)
inds = [0, 100, 200, 300, 400, 499]
files = files[inds]
labels = ["62.5kV/m", "50.0kV/m", "37.5kV/m", "25.0kV/m", "12.5kV/m", "0.0kV/m"]
files = list(files)
p_bool = np.abs(freqs-fc)<bw
freqs /= 1000
fc/=1000
bw/=1000
for i, f in enumerate(files):
    print(i)
    try:
        obj = hsDat(f)
        fft = np.fft.rfft(obj.dat[:, 0])
        if plot_dat:
            ax.plot(freqs, np.abs(fft), label = labels[i])
    except:
        print("bad file")

ax.set_yscale("log")
ax.set_xlim([fc-bw/2., fc+bw/2.])
plt.xlabel("Frequency[kHz]")
plt.ylabel("Optical Power [arb]")
plt.legend()
plt.tight_layout()
plt.show()


__author__ = "Domenico Solazzo"
__version__ = "0.1"

RESPONSE_CODES = {
        200: "OK: Success",
        202: "Accepted: The request was accepted and the user was queued for processing",
        401: "Not Authorized: either you need to provide authentication credentials, or the credentials provided aren't valid.",
        403: "Bad Request: Your request is invalid and we'll return and error message that tells you why. This is the status code if you have exceeded the rate limit.",
        404: "Not Found: either you are requesting an invalid URI or the resource in question doesn't exist.",
        500: "Internal Server Error: we did something wrong.",
        502: "Bad Gateway: returned if Klout is down or being upgraded.",
        503: "Service Unavailable: the Klout servers are up, but are overloaded with requests. Try again later."
}
class KloutError( Exception ):
    def __init__(self, code=0, msg=''):
        super(KloutError, self).__init__()
        self.code = code
        self.msg = msg
    def __str__(self):
        return repr(self)
    def __repr__(self):
        return "%i: %s" % (self.code, self.msg)

class Klout( object ):
    def __init__(self, key, serviceType="service"):
        self._apiKey = key
        self.__service = self.__getProxyFactory(serviceType)

    def __getProxyFactory(self, serviceType):
        service = None
        if serviceType == "test":
            service = TestKloutService(serviceType)
        else:
            service = KloutService(self._apiKey)
        self.__service = service
        return self.__service

    def score(self, users):
        """
            Retrieve a Klout score
            @param: users - List of usernames
            @return: A list of tuples in the form (username, klout_score)
        """
        if not users:
            raise KloutError(0, "No Users")
        if not isinstance(users, (list, tuple)):
            raise KloutError(0, "Wrong input.")
        users = ",".join(users)
        query = {"users": users}
        result = self.__service.makeCall("score", query)

        return result
    def show(self, users):
        """
            Retrieve a user object
            @param: users - List of usernames
            @return: A dictionary with the returned data
        """
        if not users:
            raise KloutError(0, "No Users.")
        if not isinstance(users, (list, tuple)):
            raise KloutError(0, "Wrong input.")
        users = ",".join(users)
        query = {"users":users}
        result = self.__service.makeCall("user", query)
        return result
    def topics(self, users):
        """
            Returns the top 3 topics objects
            @param: users - A list of usernames
            @return: A list of dicts in the form [{username:['topic1, topic2, topic3]..}
        """
        if not users:
            raise KloutError(0, "No Users")
        if not isinstance(users, (list, tuple)):
            raise KloutError(0, "Wrong Input.")
        users = ",".join(users)
        query = {"users":users}
        result = self.__service.makeCall("topics", query)
        return result
    def influencerOf(self, users):
        """
            Returns up to 5 user score pairs for user that are influencer for the given user
            @param: users - A list of usernames
            @return: A list of dicts in the form [{username:[(username, score),..}
        """
        if not users:
            raise KloutError(0, "No Users")
        if not isinstance(users, (list, tuple)):
            raise KloutError(0, "Wrong Input.")
        users = ",".join(users)
        query = {"users":users}
        result = self.__service.makeCall("influencerOf", query)
        return result
    def influencedBy(self, users):
        """
            Returns up to 5 user score pairs for user that are influenced by the given user
            @param: users - A list of usernames
            @return: A list of dicts in the form [{username:[(username, score),..}
        """
        if not users:
            raise KloutError(0, "No Users")
        if not isinstance(users, (list, tuple)):
            raise KloutError(0, "Wrong Input.")
        users = ",".join(users)
        query = {"users":users}
        result = self.__service.makeCall("influencedBy", query)
        return result

class KloutService(object):
    def __init__(self, apiKey):
        self.apiKey = apiKey
        self.VERSION_API = "/1/"
        self.API_URL = "api.klout.com"

    def getCallUrl(self, callName):
        servicePath = ""
        if callName == "score":
            servicePath = "klout.json"
        elif callName == "user":
            servicePath = "users/show.json"
        elif callName == "topics":
            servicePath = "users/topics.json"
        elif callName == "influencedBy":
            servicePath = "soi/influenced_by.json"
        elif callName == "influencerOf":
            servicePath = "soi/influencer_of.json"
        else:
            raise Exception("Url not available")
        return self.VERSION_API + servicePath

    def _remove_empty_params(self, query):
        if not isinstance(query, type({})):
            raise Exception("Wrong query in input")
        returnedQuery = {}
        for key in query:
            if not query[key] == None:
                returnedQuery[key] = query[key]
        return returnedQuery

    def makeCall(self, callName, query):
        import urllib, httplib, json
        url = self.getCallUrl(callName)

        query = self._remove_empty_params(query)

        if 'key' not in query:
            query["key"] = self.apiKey

        queryStr = urllib.urlencode(query)

        if len(query) > 0:
            if url.find("?") == -1:
                url = url + "?" + queryStr
            else:
                url = url + "&" + queryStr
        try:
            conn = httplib.HTTPConnection(self.API_URL)
            conn.request('GET', url)
            response = conn.getresponse()
            data = response.read()
            data = json.loads(data)
        except httplib.HTTPException as err:
            msg = err.read() or RESPONSE_CODES.get(err.code, err.message)
            raise KloutError(err.code, msg)
        except ValueError:
            msg = "Invalid data: %s" % data
            raise KloutError(0, msg)
        return data
class TestKloutService(KloutService):
    def makeCall(self, callName, query):
        if callName == "score":
            return {"users":[{"twitter_screen_name":"user1","kscore":23.02}]}
        elif callName == "user":
            return {"users":[{
                "twitter_id": "111111",
                "twitter_screen_name":"name",
                "score":{
                    "kscore":10,
                    "slope":1,
                    "description":"description",
                    "kclass_id":1,
                    "kclass":"Socializer",
                    "kclass_description":"kclass description",
                    "network_score":22,
                    "amplification_score":18,
                    "true_reach": 10,
                    "delta_1day": 0.2,
                    "delta_5day": 0.4
                }
            }]}
        elif callName == "topics":
            return {"users":[{"twitter_screen_name":"user1", "topics":["python"]}]}
        elif callName == "influencedBy":
            return {"users":[
                                {
                                "twitter_screen_name":"user1",
                                "influencers":[{"twitter_screen_name":"user2",
                                                "kscore":10.00
                                                }]
                                }
                            ]
                    }
        elif callName == "influencerOf":
            return {"users":[
                                {
                                "twitter_screen_name":"user1",
                                "influencers":[{"twitter_screen_name":"user2",
                                                "kscore":10.00
                                                }]
                                }
                            ]
                    }
        elif callName == "history":
            return {'dates':[], 'klout_score':[], 'amplification':[],
                    'retweets':[], 'mentions':[],'network':[],
                    'followers_following':[], 'followers_count':[], 'mentioners':[],
                    'retweeters':[],'true_reach':[],'in_out':[]
                    }


import numpy as np
import pandas as pd

class Node():
    """
        Class for defining each node of the Decision Tree.
    """
    def __init__(self, attr = None, pred = None, class_label = None) -> None:
        self.attr = attr
        self.children = None
        self.isLeaf = False
        self.pred = pred
        self.class_label = class_label

class DecisionTreeClassifierID3():
    """
        Class for implementing Decision Tree Classifier using ID3 (Iterative Dichotomiser 3) Algorithm.
    """

    def __init__(self):
        self.root = None

    def isBalanced(self, df):
        """
            Used to check if all tuples belong to a single class
            : param y: array, label or true values
            : return: boolean, True if all tuples belong to a single class, False otherwise.
        """
        return len(list(df.value_counts())) == 1

    def getEntropy(self, total, df):
        """
            Used to calculate entropy for a particular class value of a column
            : param total: int, total number of row/tuples/training examples
            : param df: array, column
            : return: int
        """
        labels = sorted(df.value_counts().to_dict().items())
        entropy = 0
        for label in labels:
            f = (label[1] / total)
            entropy -= f * np.log(f)
        return entropy

    def gain(self, column, y):
        """
            Used to calculate gain for a column
            : param column: array, column
            : param y: array, label or true values
            : return: int, gain for the column
        """
        total = len(column)
        labels = sorted(y.value_counts().to_dict().items())
        fp = (labels[0][1] / total)
        fn = (labels[1][1] / total)
        total_entropy = - (fp * np.log(fp)) - (fn * np.log(fn))
        g = total_entropy
        concat_df = pd.concat([column, y], axis=1)
        df_dict = {g: d['label']
                   for g, d in concat_df.groupby(by=[concat_df.columns[0]])}
        for key, value in df_dict.items():
            g -= (len(value) / total) * self.getEntropy(key, total, value)
        return g

    def getMaxGain(self, X, y):
        """
            Used to find the attribute which provides maximum gain.
            : param X: 2D array, matrix of features, with each row being a data entry
            : param y: array, label or true values
            : return: tuple, tuple of attribute name/column name and entropy value
        """
        cols = X.columns
        gain_dict = {}
        for col in cols:
            a = X[col]
            gain_dict[col] = self.gain(a, y)

        return sorted(gain_dict.items(), key=lambda x: x[1], reverse=True)[0]

    def buildTree(self, X, y, attr_classes, class_val=None):
        """
            Used to build the decision tree.
            : param X: 2D array, matrix of features, with each row being a data entry
            : param y: array, label or true values
            : param attr_classes: dict, dictionary of list of distinct classes for each column
            : param class_val: string, distinct class, classification is based on
            : return: Node, a node for the tree
        """
        root = Node()
        if self.isBalanced(y):
            root.isLeaf = True
            root.pred = y.iloc[0]
        elif X is None:
            root.isLeaf = True
            root.pred = y.mode()
        else:
            maxGain = self.getMaxGain(X, y)
            maxGainCol = maxGain[0]
            pred = y.mode()[0]
            attr_list = attr_classes[maxGainCol].copy()
            concat_df = pd.concat([X, y], axis=1)
            df_dict = {g: d for g, d in concat_df.groupby(by=[maxGainCol])}
            root.attr = maxGainCol
            root.children = []
            for key, value in df_dict.items():
                attr_list.remove(key)
                new_X = value.drop(maxGainCol, axis=1).iloc[:, :-1]
                new_y = value.iloc[:, -1]
                root.children.append(self.buildTree(
                    new_X, new_y, attr_classes, key))
            if len(attr_list) > 0:
                root.pred = pred
        root.class_label = class_val
        return root

    def printTree(self, root, num_spaces=0):
        """
            Used to print the decision tree.
            : param root: Node, node of the decision tree
            : param num_spaces: int, number of spaces to be printed
            : return: None
        """
        print("\t" * num_spaces, end="")
        print(root.class_label, "->", end=" ")
        if root.children is None:
            print(root.pred)
        else:
            print(root.attr)
            for child in root.children:
                self.printTree(child, num_spaces + 1)

    def train(self, X, y):
        """
            Used to train the Decision Tree Classifier
            : param X: 2D array, matrix of features, with each row being a data entry
            : param y: array, label or true values
            : return: None
        """
        attr_classes = {}
        cols = X.columns
        for col in cols:
            attr_classes[col] = list(X[col].value_counts().keys())

        self.root = self.buildTree(X, y, attr_classes)

    def predict_one_example(self, X, root):
        """
            Used to predict the value of y for a single example.
            : param X: tuple, one single data entry
            : param root: Node, node of the decision tree
            : return: array, predicted values
        """
        if root.isLeaf:
            return root.pred
        col = root.attr
        val = X[col]
        next_root = [x for x in root.children if x.class_label == val]
        if len(next_root) == 0:
            return root.pred
        return self.predict_one_example(X, next_root[0])

    def predict(self, X):
        """
            Used to predict the value of y
            : param X: 2D array, matrix of features, with each row being a data entry
            : return: array, predicted values
        """
        pred_y = []
        for i in range(len(X)):
            pred_y.append(self.predict_one_example(
                X.iloc[i, :], self.root))

        return pred_y


# -*- coding: utf-8 -*-
import os
import sys
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin

from xbmcvfs import translatePath
from libs.session import Session
from libs.utils import get_url, check_settings

def list_settings(label):
    addon = xbmcaddon.Addon()
    _handle = int(sys.argv[1])
    xbmcplugin.setPluginCategory(_handle, label)

    list_item = xbmcgui.ListItem(label = 'Kanály')
    url = get_url(action='manage_channels', label = 'Kanály')
    xbmcplugin.addDirectoryItem(_handle, url, list_item, True)

    list_item = xbmcgui.ListItem(label = addon.getLocalizedString(300101))
    url = get_url(action='list_devices', label = addon.getLocalizedString(300101))
    xbmcplugin.addDirectoryItem(_handle, url, list_item, True)

    list_item = xbmcgui.ListItem(label = addon.getLocalizedString(300102))
    url = get_url(action='addon_settings', label = addon.getLocalizedString(300102))
    xbmcplugin.addDirectoryItem(_handle, url, list_item, True)

    xbmcplugin.endOfDirectory(_handle)

def list_devices(label):
    addon = xbmcaddon.Addon()
    _handle = int(sys.argv[1])
    xbmcplugin.setPluginCategory(_handle, label)
    session = Session()
    devices = session.get_devices()
    for id in devices:
        list_item = xbmcgui.ListItem(label = devices[id]['title'] + '\n' + '[COLOR=gray]' + addon.getLocalizedString(300208) + ': ' + devices[id]['last_activity'] + '[/COLOR]')
        url = get_url(action='remove_device', id = id, title = devices[id]['title'], last_activity = devices[id]['last_activity'])
        xbmcplugin.addDirectoryItem(_handle, url, list_item, True)
    xbmcplugin.endOfDirectory(_handle)

def remove_device(id, title, last_activity):
    addon = xbmcaddon.Addon()
    response = xbmcgui.Dialog().yesno(addon.getLocalizedString(300300), addon.getLocalizedString(300301) + ' ' + title + ' (' + addon.getLocalizedString(300208) + ': ' + last_activity + ')?')
    if response == True:
        session = Session()
        session.delete_device(id)
        xbmc.executebuiltin('Container.Refresh')

class Settings:
    def __init__(self):
        self.is_settings_ok = check_settings()

    def save_json_data(self, file, data):
        addon = xbmcaddon.Addon()
        addon_userdata_dir = translatePath(addon.getAddonInfo('profile'))
        if self.is_settings_ok:
            filename = os.path.join(addon_userdata_dir, file['filename'])
            try:
                with open(filename, "w") as f:
                    f.write('%s\n' % data)
            except IOError:
                xbmcgui.Dialog().notification('Rebit.tv', addon.getLocalizedString(300201) + file['description'], xbmcgui.NOTIFICATION_ERROR, 5000)

    def load_json_data(self, file):
        data = None
        if self.is_settings_ok:
            addon = xbmcaddon.Addon()
            addon_userdata_dir = translatePath(addon.getAddonInfo('profile'))
            filename = os.path.join(addon_userdata_dir, file['filename'])
            try:
                with open(filename, "r") as f:
                    for row in f:
                        data = row[:-1]
            except IOError as error:
                if error.errno != 2:
                    xbmcgui.Dialog().notification('Rebit.tv', addon.getLocalizedString(300202) + file['description'], xbmcgui.NOTIFICATION_ERROR, 5000)
        return data

    def reset_json_data(self, file):
        if self.is_settings_ok:
            addon = xbmcaddon.Addon()
            addon_userdata_dir = translatePath(addon.getAddonInfo('profile'))
            filename = os.path.join(addon_userdata_dir, file['filename'])
            if os.path.exists(filename):
                try:
                    os.remove(filename)
                except IOError:
                    xbmcgui.Dialog().notification('Rebit.tv', addon.getLocalizedString(300203) + file['description'], xbmcgui.NOTIFICATION_ERROR, 5000)


#! /usr/bin/env -S python3 -u
import os, shutil, sys, glob, traceback
from easyterm import *

help_msg="""This program downloads one specific NCBI assembly, executes certains operations, then cleans up data

### Input/Output:
-a     genome NCBI accession
-o     folder to download to

### Actions:
-c     bash command template
-cf    bash command template read from this file
-p     python command template
-pf    python command template read from this file

In all templates above, these placeholders can be used:
{accession}   genome NCBI accession, e.g. GCA_000209535.1
{genomefile}  path to genome fasta file
{taxid}       taxonomy id
{species}     species name, e.g. "Drosophila melanogaster"
{mspecies}    masked species, e.g. "Drosophila_melanogaster"

### Other options:
-k      keep files instead of cleaning them up at the end
-w      max workers for downloads at once
-sh     open shells for bash commands. Required for complex commands
        (e.g. sequential commands, or using redirections)

-print_opt   print currently active options
-h | --help  print this help and exit"""

command_line_synonyms={'t':'temp'}
def_opt= {'a':'',
          'o':'./',
          'c':'',
          'cf':'',
          'p':'',
          'pf':'',
          'k':False,
          'sh':False,
          'w':1,
          'temp':'/tmp/'}

temp_folder=None

##### start main program function
def main(args={}):
  """We're encapsulating nearly the whole program in a function which is executed when
  the script is directly executed. This provides the alternative of being able
  to run the same thing as module: importing this 'main' function and running it with
  a 'args' dictionary containing options and arguments, equivalent to opt
  """

  ### loading options
  if not args:
    opt=command_line_options(def_opt, help_msg, synonyms=command_line_synonyms)
  else:
    opt=args

  # if not opt['cf'] and not opt['c']:
  #   raise NoTracebackError("ERROR you must define a template command with -c or -cf")
  if opt['c'] or opt['cf']:
    bash_template_command=(opt['c']
                           if opt['c'] else
                           '\n'.join([x.strip() for x in open(opt['cf'])]))
  if opt['p'] or opt['pf']:
    py_template_command=(opt['p']
                         if opt['p'] else
                         '\n'.join([x.strip() for x in open(opt['pf'])]))

  if not opt['o']:
    raise NoTracebackError("ERROR you must provide an output folder with -o")
  outfolder=opt['o'].rstrip('/')
  if not os.path.exists(outfolder):
    os.makedirs(outfolder)

  if not opt['a']:
    raise NoTracebackError("ERROR you must provide an accession with -a")
  accession=opt['a']

  datadir=f'{outfolder}/dataset.{accession}'
  zipfile=datadir+'.zip'

  write('*** Options accepted: ', how='green')
  write(opt)
  write('')

  write('*** Download metadata (dehydrated)', how='green')
  ## download dehydrated
  cmd_download_dehydra = f"""\
datasets download genome accession {accession} \
--reference --dehydrated \
--exclude-genomic-cds --exclude-gff3 --exclude-protein --exclude-rna \
--filename {zipfile} """
  run_cmd(cmd_download_dehydra,
          stdout=None, stderr=None) # messages printed to screen

  write('*** Reformatting metadata', how='green')
  ## get some metadata
  cmd_format_tsv = f"""
dataformat tsv genome \
--package {zipfile} \
--fields tax-id,organism-name"""
  x = run_cmd(cmd_format_tsv).stdout
  taxid, species = x.split('\n')[1].split('\t')
  mspecies=mask_chars(species)
  write(f'accession: {accession}')
  write(f'taxid:     {taxid}')
  write(f'species:   {species}')
  write(f'mspecies:  {mspecies}')

  write('*** Unzipping metadata, removing zipfile', how='green')
  ## prep for download: unzip
  cmd_unzip_dehydra=f"unzip -o -d {datadir} {zipfile}"
  run_cmd(cmd_unzip_dehydra,
          stdout=None, stderr=None) # messages printed to screen
  write(f'removing {zipfile}')
  os.remove(zipfile)

  write('')
  write('*** Downloading genome data', how='green')
  ## download / hydrate
  progressbar=''  if sys.stdout.isatty() else ' --no-progressbar '
  cmd_download_hydra=f"""
datasets rehydrate \
--directory {datadir} \
--match "/{accession}/" \
--max-workers {opt['w']} \
{progressbar} """
  run_cmd(cmd_download_hydra,
          stdout=None, stderr=None) # messages printed to screen

  write('')
  write('*** Compacting chromosomes into a single fasta', how='green')
  fasta_regexp=f'{datadir}/ncbi_dataset/data/{accession}/*fna'
  genomefile=  f'{datadir}/ncbi_dataset/data/{accession}/{accession}.fasta'
  index=0
  with open(genomefile, 'wb') as wfd:
    for index, chromfile in enumerate(glob.iglob(fasta_regexp)):
      service(chromfile)
      with open(chromfile,'rb') as fd:
        shutil.copyfileobj(fd, wfd)
  # cmd_compact_fasta=f'cat {fasta_regexp} > {genomefile}'
  # run_cmd(cmd_compact_fasta)
  write(f'Concatenating {index+1} chromosomes or contigs \n to genomefile: {genomefile}')

  write('*** Removing chromosomes fasta files', how='green')
  for chromfile in glob.iglob(fasta_regexp):
    os.remove(chromfile)

  if not any( [opt[k] for k in ['c', 'p', 'cf', 'pf']] ):
    write('')
    write('*** <No commands to be executed>', how='green')
  try:
    if opt['c'] or opt['cf']:
      write('')
      write('*** Running bash command', how='green')
      #template='{genomefile} {species} {mspecies}'
      bash_cmd=bash_template_command.format(**locals())
      write(bash_cmd)
      run_cmd(bash_cmd,
              shell=opt['sh'],
              stdout=None, stderr=None) # messages printed to screen, if not redicted

    if opt['p'] or opt['pf']:
      write('')
      write('*** Running python command', how='green')
      py_cmd=py_template_command.format(**locals())
      write(py_cmd)
      exec(py_cmd)

  except Exception:
    write('')
    write('*** an ERROR occured !', how='red')
    traceback.print_exc()

  if not opt['k']:
    write('')
    write('*** Cleaning up all data', how='green')
    write(f'removing {datadir}')
    shutil.rmtree(datadir)
  else:
    write('')
    write('*** Leaving data in place', how='green')
    write(f'check {datadir}')

  # creating a temporary folder with random name inside the -temp argument
  # temp_folder=random_folder(opt['temp'])
  # write(f'Using temporary folder={temp_folder}')

  ### insert your code here

##### end main program function

### function executed when program execution is over:
def close_program():
  pass
  # if temp_folder is not None and os.path.isdir(temp_folder):
  #   # deleting temporary folder
  #   shutil.rmtree(temp_folder)

if __name__ == "__main__":
  try:
    main()
    close_program()
  except Exception as e:
    close_program()
    raise e from None


'''
1. input the members in the team and take the score.
2. if x has a higher score than other two then
3. create a set arranged in order of the score.
4. create a function to compare the scores.
5. decide the order is valid or not using if else ladder.
6. if anyone has higher skill score then yes.
7. if such order is not possible then no.

'''

def result(a,b): #creating a function to compare result
    f1=(a[0] >= b[0] and a[1] >= b[1] and a[2] >= b[2]) #if x skilll score is greater than b and x skill score is grreater than z then return the 1st person i.e. x
    f2=(a[0] > b[0] or a[1] > b[1] or a[2] > b[2]) #if any score is greter than other members then retrun that member
    return f1 and f2
for _ in range(int(input())): #looping through the skills of team members
    x=list(map(int,input().split())) #x score
    y=list(map(int,input().split())) #y score
    z=list(map(int,input().split())) #z score
    if result(x,y) and result(y,z): #if anyone is greater than other print yes
        print("yes")
    elif result(x,z) and result(z,y): #comparing z
        print("yes")
    elif result(y,x) and result(x,z):
        print("yes")
    elif result(y,z) and result(z,x):
        print("yes")
    elif result(z,x) and result(x,y):
        print("yes")
    elif result(z,y) and result(y,x): #comparing y
        print("yes")
    else:
        print("no") #if such order doesn't exists


import heapq

roads = [["ULSAN","BUSAN"],["DAEJEON","ULSAN"],["DAEJEON","GWANGJU"],["SEOUL","DAEJEON"],["SEOUL","ULSAN"],["DAEJEON","DAEGU"],["GWANGJU","BUSAN"],["DAEGU","GWANGJU"],["DAEGU","BUSAN"],["ULSAN","DAEGU"],["GWANGJU","YEOSU"],["BUSAN","YEOSU"]]
node = []
for road in roads:
    x, y = road
    node.append(x)
    node.append(y)
node = set(node)
num = len(node)

graph = [[i]*(num+1) for i in range(num+1)]

for road in roads:
    x, y = road
    for i in graph:
        if x == i[0]:
            i.extend([y])

print(graph)
# graph = [[i] for i in node]
# print(graph)
#
# for road in roads:
#     x, y = road
#     for i in graph:
#         if x == i[0]:
#             i.extend([y])
#
#
# print(graph)
# def dij(depart, arrive):
#     q = []
#     dist = [0] * num
#     heapq.heappush(q,(0,depart))
#     dist[depart] = 0
#     while q:
#         dist, now = heapq.heappop(q)
#         for nd, ndist in graph[now]:
#             ndist += dist
#             if dist[nd]
#


from datetime import datetime
from persistent.list import PersistentList
from zope.annotation import IAnnotations
import logging

TWITTER_KEY = "noise.addon.twitter"
FACEBOOK_KEY = "noise.addon.facebook"
EMAIL_KEY = "noise.addon.email"
HARDCOPY_KEY = "noise.addon.hardcopy"

TWITTER_CSV_HEADERS = ["timestamp", "twitter-text", "tweet-text",
                       "firstname", "lastname", "email", "phone", "keepposted"]
FACEBOOK_CSV_HEADERS = ["timestamp"]
EMAIL_CSV_HEADERS = ["timestamp", "email-text", "email_body", "firstname",
                     "lastname", "email", "phone", "keepposted"]
HARDCOPY_CSV_HEADERS = ["timestamp", "hardcopy-text", "hardcopy_body",
                        "firstname", "lastname", "address", "zipcode", "city",
                        "phone", "keepposted"]

logger = logging.getLogger('noise.addon')

class NoiseRecord(object):
    """ A Noise Record containing form data
    """

    def __init__(self, timestamp, record):
        self._timestamp = timestamp
        self._record = str(record)

    @property
    def get_record(self):
        return eval(self._record)

    @property
    def get_timestamp(self):
        return self._timestamp

def setupAnnotations(context, key, reset=False):
    annotations = IAnnotations(context)

    if reset or (not key in annotations):
        annotations[key] = PersistentList()

    return annotations

def add_noise(context, key, record):
    annotations = setupAnnotations(context, key)
    annotations[key].append(
        NoiseRecord(datetime.now().strftime("%d-%m-%Y %H:%M"), record)
    )

def get_noise(context, key):
    annotations = setupAnnotations(context, key)

    data = []
    if key in annotations:
        data = annotations[key]

    data = [d for d in data if isinstance(d, NoiseRecord)]

    return data

def status(context, key):
    annotations = IAnnotations(context)

    return annotations.get(key, [])


# -*- coding: utf-8 -*-
import numpy as np

def get_linear_value(current_index, start_value, total_steps, end_value=0, **kwargs):
    values = np.linspace(start_value, end_value, total_steps, dtype=np.float32)
    values = values / start_value * start_value
    return values[current_index]

def get_cosine_value(current_index, start_value, total_steps, end_value=0, **kwargs):
    values = np.linspace(end_value, total_steps, total_steps, dtype=np.float32)
    values = np.cos(values * np.pi / total_steps)
    values = (values + 1) * start_value / 2
    return values[current_index]

def get_ema_value(current_index, start_value, eta, **kwargs):
    value = start_value * eta ** current_index
    return value

INTERPOLATION_SCHEDULERS = {
    'ema': get_ema_value,
    'cos': get_cosine_value,
    'linear': get_linear_value,
}


from collections import defaultdict
def solution(genres, plays):
    answer = []
    stream = defaultdict(list)
    # 같은 장르내에서는 plays수가 같을 수 있지만
    # 장르 합은 다른 장르의 합과 다르다
    for g,p in zip(genres, plays):
        stream[g].append(p)
    answer = []
    stream = sorted(stream.items(), key = lambda x:-sum(x[1])) # list
    # 인덱스는 앞에것부터 찾음
    for i,j in stream:
        j.sort(reverse=True)
    for i in range(len(stream)): #장르가 2개 이상 가능
        if len(stream[i][1]) == 1: answer.append(plays.index(stream[i][1][0]))
        else: # 길이가 2이상 (2개 다 넣일 수 있음)
            answer.append(plays.index(stream[i][1][0]))
            plays[plays.index(stream[i][1][0])] = -1
            answer.append(plays.index(stream[i][1][1]))
    return answer


import unittest
import warnings
from datetime import date, datetime
from decimal import Decimal

import pytz
from babel import Locale

from fluent.runtime.types import FluentDateType, FluentNumber, fluent_date, fluent_number

class TestFluentNumber(unittest.TestCase):

    locale = Locale.parse('en_US')

    def setUp(self):
        self.cur_pos = fluent_number(123456.78123,
                                     currency='USD',
                                     style='currency')
        self.cur_neg = fluent_number(-123456.78123,
                                     currency='USD',
                                     style='currency')

    def test_int(self):
        i = fluent_number(1)
        self.assertTrue(isinstance(i, int))
        self.assertTrue(isinstance(i, FluentNumber))
        self.assertEqual(i + 1, 2)

    def test_float(self):
        f = fluent_number(1.1)
        self.assertTrue(isinstance(f, float))
        self.assertTrue(isinstance(f, FluentNumber))
        self.assertEqual(f + 1, 2.1)

    def test_decimal(self):
        d = Decimal('1.1')
        self.assertTrue(isinstance(fluent_number(d), Decimal))
        self.assertTrue(isinstance(fluent_number(d), FluentNumber))
        self.assertEqual(d + 1, Decimal('2.1'))

    def test_disallow_nonexistant_options(self):
        self.assertRaises(
            TypeError,
            fluent_number,
            1,
            not_a_real_option=True,
        )

    def test_style_validation(self):
        self.assertRaises(ValueError,
                          fluent_number,
                          1,
                          style='xyz')

    def test_use_grouping(self):
        f1 = fluent_number(123456.78, useGrouping=True)
        f2 = fluent_number(123456.78, useGrouping=False)
        self.assertEqual(f1.format(self.locale), "123,456.78")
        self.assertEqual(f2.format(self.locale), "123456.78")
        # ensure we didn't mutate anything when we created the new
        # NumberPattern:
        self.assertEqual(f1.format(self.locale), "123,456.78")

    def test_use_grouping_decimal(self):
        d = Decimal('123456.78')
        f1 = fluent_number(d, useGrouping=True)
        f2 = fluent_number(d, useGrouping=False)
        self.assertEqual(f1.format(self.locale), "123,456.78")
        self.assertEqual(f2.format(self.locale), "123456.78")

    def test_minimum_integer_digits(self):
        f = fluent_number(1.23, minimumIntegerDigits=3)
        self.assertEqual(f.format(self.locale), "001.23")

    def test_minimum_integer_digits_decimal(self):
        f = fluent_number(Decimal('1.23'), minimumIntegerDigits=3)
        self.assertEqual(f.format(self.locale), "001.23")

    def test_minimum_fraction_digits(self):
        f = fluent_number(1.2, minimumFractionDigits=3)
        self.assertEqual(f.format(self.locale), "1.200")

    def test_maximum_fraction_digits(self):
        f1 = fluent_number(1.23456)
        self.assertEqual(f1.format(self.locale), "1.235")
        f2 = fluent_number(1.23456, maximumFractionDigits=5)
        self.assertEqual(f2.format(self.locale), "1.23456")

    def test_minimum_significant_digits(self):
        f1 = fluent_number(123, minimumSignificantDigits=5)
        self.assertEqual(f1.format(self.locale), "123.00")
        f2 = fluent_number(12.3, minimumSignificantDigits=5)
        self.assertEqual(f2.format(self.locale), "12.300")

    def test_maximum_significant_digits(self):
        f1 = fluent_number(123456, maximumSignificantDigits=3)
        self.assertEqual(f1.format(self.locale), "123,000")
        f2 = fluent_number(12.3456, maximumSignificantDigits=3)
        self.assertEqual(f2.format(self.locale), "12.3")
        f3 = fluent_number(12, maximumSignificantDigits=5)
        self.assertEqual(f3.format(self.locale), "12")

    def test_currency(self):
        # This test the default currencyDisplay value
        self.assertEqual(self.cur_pos.format(self.locale), "$123,456.78")

    def test_currency_display_validation(self):
        self.assertRaises(ValueError,
                          fluent_number,
                          1234,
                          currencyDisplay="junk")

    def test_currency_display_symbol(self):
        cur_pos_sym = fluent_number(self.cur_pos, currencyDisplay="symbol")
        cur_neg_sym = fluent_number(self.cur_neg, currencyDisplay="symbol")
        self.assertEqual(cur_pos_sym.format(self.locale), "$123,456.78")
        self.assertEqual(cur_neg_sym.format(self.locale), "-$123,456.78")

    def test_currency_display_code(self):
        # Outputs here were determined by comparing with Javascrpt
        # Intl.NumberFormat in Firefox.
        cur_pos_code = fluent_number(self.cur_pos, currencyDisplay="code")
        cur_neg_code = fluent_number(self.cur_neg, currencyDisplay="code")
        self.assertEqual(cur_pos_code.format(self.locale), "USD123,456.78")
        self.assertEqual(cur_neg_code.format(self.locale), "-USD123,456.78")

    @unittest.skip("Babel doesn't provide support for this yet")
    def test_currency_display_name(self):
        cur_pos_name = fluent_number(self.cur_pos, currencyDisplay="name")
        cur_neg_name = fluent_number(self.cur_neg, currencyDisplay="name")
        self.assertEqual(cur_pos_name.format(self.locale), "123,456.78 US dollars")
        self.assertEqual(cur_neg_name.format(self.locale), "-123,456.78 US dollars")

        # Some others locales:
        hr_BA = Locale.parse('hr_BA')
        self.assertEqual(cur_pos_name.format(hr_BA),
                         "123.456,78 američkih dolara")
        es_GT = Locale.parse('es_GT')
        self.assertEqual(cur_pos_name.format(es_GT),
                         "dólares estadounidenses 123,456.78")

    def test_copy_attributes(self):
        f1 = fluent_number(123456.78, useGrouping=False)
        self.assertEqual(f1.options.useGrouping, False)

        # Check we didn't mutate anything
        self.assertIs(FluentNumber.default_number_format_options.useGrouping, True)

        f2 = fluent_number(f1, style="percent")
        self.assertEqual(f2.options.style, "percent")

        # Check we copied
        self.assertEqual(f2.options.useGrouping, False)

        # and didn't mutate anything
        self.assertEqual(f1.options.style, "decimal")
        self.assertEqual(FluentNumber.default_number_format_options.style, "decimal")

class TestFluentDate(unittest.TestCase):

    locale = Locale.parse('en_US')

    def setUp(self):
        self.a_date = date(2018, 2, 1)
        self.a_datetime = datetime(2018, 2, 1, 14, 15, 16, 123456,
                                   tzinfo=pytz.UTC)

    def test_date(self):
        fd = fluent_date(self.a_date)
        self.assertTrue(isinstance(fd, date))
        self.assertTrue(isinstance(fd, FluentDateType))
        self.assertEqual(fd.year, self.a_date.year)
        self.assertEqual(fd.month, self.a_date.month)
        self.assertEqual(fd.day, self.a_date.day)

    def test_datetime(self):
        fd = fluent_date(self.a_datetime)
        self.assertTrue(isinstance(fd, datetime))
        self.assertTrue(isinstance(fd, FluentDateType))
        self.assertEqual(fd.year, self.a_datetime.year)
        self.assertEqual(fd.month, self.a_datetime.month)
        self.assertEqual(fd.day, self.a_datetime.day)
        self.assertEqual(fd.hour, self.a_datetime.hour)
        self.assertEqual(fd.minute, self.a_datetime.minute)
        self.assertEqual(fd.second, self.a_datetime.second)
        self.assertEqual(fd.microsecond, self.a_datetime.microsecond)
        self.assertEqual(fd.tzinfo, self.a_datetime.tzinfo)

    def test_format_defaults(self):
        fd = fluent_date(self.a_date)
        en_US = Locale.parse('en_US')
        en_GB = Locale.parse('en_GB')
        self.assertEqual(fd.format(en_GB), '1 Feb 2018')
        self.assertEqual(fd.format(en_US), 'Feb 1, 2018')

    def test_dateStyle_date(self):
        fd = fluent_date(self.a_date, dateStyle='long')
        en_US = Locale.parse('en_US')
        en_GB = Locale.parse('en_GB')
        self.assertEqual(fd.format(en_GB), '1 February 2018')
        self.assertEqual(fd.format(en_US), 'February 1, 2018')

    def test_dateStyle_datetime(self):
        fd = fluent_date(self.a_datetime, dateStyle='long')
        en_US = Locale.parse('en_US')
        en_GB = Locale.parse('en_GB')
        self.assertEqual(fd.format(en_GB), '1 February 2018')
        self.assertEqual(fd.format(en_US), 'February 1, 2018')

    def test_timeStyle_datetime(self):
        fd = fluent_date(self.a_datetime, timeStyle='short')
        en_US = Locale.parse('en_US')
        en_GB = Locale.parse('en_GB')
        self.assertRegex(fd.format(en_US), '^2:15\\sPM$')
        self.assertEqual(fd.format(en_GB), '14:15')

    def test_dateStyle_and_timeStyle_datetime(self):
        fd = fluent_date(self.a_datetime, timeStyle='short', dateStyle='short')
        en_US = Locale.parse('en_US')
        en_GB = Locale.parse('en_GB')
        self.assertRegex(fd.format(en_US), '^2/1/18, 2:15\\sPM$')
        self.assertEqual(fd.format(en_GB), '01/02/2018, 14:15')

    def test_validate_dateStyle(self):
        self.assertRaises(ValueError,
                          fluent_date,
                          self.a_date,
                          dateStyle="nothing")

    def test_validate_timeStyle(self):
        self.assertRaises(ValueError,
                          fluent_date,
                          self.a_datetime,
                          timeStyle="nothing")

    def test_timeZone(self):
        en_GB = Locale.parse('en_GB')
        LondonTZ = pytz.timezone('Europe/London')

        # 1st July is a date in British Summer Time

        # datetime object with tzinfo set to BST
        dt1 = datetime(2018, 7, 1, 23, 30, 0, tzinfo=pytz.UTC).astimezone(LondonTZ)
        fd1 = fluent_date(dt1, dateStyle='short', timeStyle='short')
        self.assertEqual(fd1.format(en_GB), '02/07/2018, 00:30')
        fd1b = fluent_date(dt1, dateStyle='full', timeStyle='full')
        self.assertRegex(fd1b.format(en_GB), '^Monday, 2 July 2018(,| at) 00:30:00 British Summer Time$')
        fd1c = fluent_date(dt1, dateStyle='short')
        self.assertEqual(fd1c.format(en_GB), '02/07/2018')
        fd1d = fluent_date(dt1, timeStyle='short')
        self.assertEqual(fd1d.format(en_GB), '00:30')

        # datetime object with no TZ, TZ passed in to fluent_date
        dt2 = datetime(2018, 7, 1, 23, 30, 0)  # Assumed UTC
        fd2 = fluent_date(dt2, dateStyle='short', timeStyle='short',
                          timeZone='Europe/London')
        self.assertEqual(fd2.format(en_GB), '02/07/2018, 00:30')
        fd2b = fluent_date(dt2, dateStyle='full', timeStyle='full',
                           timeZone='Europe/London')
        self.assertRegex(fd2b.format(en_GB), '^Monday, 2 July 2018(,| at) 00:30:00 British Summer Time$')
        fd2c = fluent_date(dt2, dateStyle='short',
                           timeZone='Europe/London')
        self.assertEqual(fd2c.format(en_GB), '02/07/2018')
        fd2d = fluent_date(dt1, timeStyle='short',
                           timeZone='Europe/London')
        self.assertEqual(fd2d.format(en_GB), '00:30')

    def test_allow_unsupported_options(self):
        # We are just checking that these don't raise exceptions
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            fluent_date(self.a_date,
                        hour12=True,
                        weekday="narrow",
                        era="narrow",
                        year="numeric",
                        month="numeric",
                        day="numeric",
                        hour="numeric",
                        minute="numeric",
                        second="numeric",
                        timeZoneName="short",
                        )

    def test_disallow_nonexistant_options(self):
        self.assertRaises(
            TypeError,
            fluent_date,
            self.a_date,
            not_a_real_option=True,
        )

    def test_dont_wrap_unnecessarily(self):
        f1 = fluent_date(self.a_date)
        f2 = fluent_date(f1)
        self.assertIs(f1, f2)

    def test_copy_attributes(self):
        f1 = fluent_date(self.a_date, dateStyle='long', hour12=False)
        self.assertEqual(f1.options.dateStyle, 'long')

        f2 = fluent_date(f1, hour12=False)

        # Check we copied other attributes:
        self.assertEqual(f2.options.dateStyle, "long")
        self.assertEqual(f2.options.hour12, False)

        # Check we can override
        f3 = fluent_date(f2, dateStyle="full")
        self.assertEqual(f3.options.dateStyle, "full")

        # and didn't mutate anything
        self.assertEqual(f1.options.dateStyle, "long")
        self.assertEqual(f2.options.dateStyle, "long")


import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import geojson
import json
import time

def chip_image1(img, chip_size=(300, 300)):
    """
    Segment an image into NxWxH chips

    Args:
        img : Array of image to be chipped
        chip_size : A list of (width,height) dimensions for chips

    Outputs:
        An ndarray of shape (N,W,H,3) where N is the number of chips,
            W is the width per chip, and H is the height per chip.

    """
    width, height, _ = img.shape
    wn, hn = chip_size
    images = np.zeros((int(width / wn) * int(height / hn), wn, hn, 3))
    k = 0
    for i in tqdm(range(int(width / wn))):
        for j in range(int(height / hn)):
            chip = img[wn * i:wn * (i + 1), hn * j:hn * (j + 1), :3]
            images[k] = chip

            k = k + 1

    return images.astype(np.uint8)

with open(fname) as f:
        data = json.load(f)

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(in_channels, conv_size, kernel_size=kernel_size, padding=2),
            nn.BatchNorm2d(conv_size),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(conv_size, conv_size*2, kernel_size=kernel_size, padding=2),
            nn.BatchNorm2d(conv_size*2),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.fc = nn.Linear(conv_size * in_channels * (conv_size*2), num_classes)

    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out
# -----------------------------------------------------------------------------------
cnn = CNN()
cnn.cuda()

random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
#if cuda:
torch.cuda.manual_seed(0)
torch.cuda.manual_seed_all(0)
torch.backends.cudnn.benchmark = True

# -----------------------------------------------------------------------------------
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)
# -----------------------------------------------------------------------------------
# Train the Model
for epoch in range(num_epochs):
    for images, labels in train_loader:
        np.shape(images)
        np.shape(lables)
        images = torchvision.transforms.functional.to_tensor(images)
        np.shape(images)
        images = Variable(images).cuda()
        labels = Variable(labels).cuda()

        # Forward + Backward + Optimize
        optimizer.zero_grad()
        outputs = cnn(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        if (i + 1) % 100 == 0:
            print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
                  % (epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, loss.item()))

# -----------------------------------------------------------------------------------
# Test the Model
cnn.eval()  # Change model to 'eval' mode (BN uses moving mean/var).
correct = 0
total = 0
for images, labels in test_loader:
    images = Variable(images).cuda()
    outputs = cnn(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted.cpu() == labels).sum()
# -----------------------------------------------------------------------------------
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
# -----------------------------------------------------------------------------------
# Save the Trained Model
torch.save(cnn.state_dict(), 'cnn.pkl')


import json
from math import sqrt

# Returns a distance-based similarity score for person1 and person2
def sim_distance(prefs, person1, person2):
    # Get the list of shared_items
    si = {}
    for item in prefs[person1]:
        if item in prefs[person2]: si[item] = 1

    # if they have no ratings in common, return 0
    if len(si) == 0: return 0

    # Add up the squares of all the differences
    sum_of_squares = sum([pow(prefs[person1][item] - prefs[person2][item], 2)
                          for item in prefs[person1] if item in prefs[person2]])

    return 1 / (1 + sum_of_squares)

# Returns the Pearson correlation coefficient for p1 and p2
def sim_pearson(prefs, p1, p2):
    # Get the list of mutually rated items
    si = {}
    for item in prefs[p1]:
        if item in prefs[p2]:
            si[item] = 1

    # if they are no ratings in common, return 0
    if len(si) == 0:
        return 0

    # Sum calculations
    n = len(si)

    # Sums of all the preferences
    sum1 = sum([prefs[p1][it] for it in si])
    sum2 = sum([prefs[p2][it] for it in si])

    # Sums of the squares
    sum1Sq = sum([pow(prefs[p1][it], 2) for it in si])
    sum2Sq = sum([pow(prefs[p2][it], 2) for it in si])

    # Sum of the products
    pSum = sum([prefs[p1][it] * prefs[p2][it] for it in si])

    # Calculate r (Pearson score)
    num = pSum - (sum1 * sum2 / n)
    den = sqrt((sum1Sq - pow(sum1, 2) / n) * (sum2Sq - pow(sum2, 2) / n))
    if den == 0:
        return 0

    r = num / den
    return r

# Returns the best matches for person from the prefs dictionary.
# Number of results and similarity function are optional params.
def top_matches(prefs, person, n=5, similarity=sim_pearson):
    scores = [(similarity(prefs, person, other), other)
              for other in prefs if other != person]
    scores.sort()
    scores.reverse()
    return scores[0:n]

def calculate_similar_items(prefs, n=10):
    # Create a dictionary of items showing which other items they
    # are most similar to.
    result = {}
    # Invert the preference matrix to be item-centric
    c = 0
    for item in prefs:
        # Status updates for large datasets
        c += 1
        if c % 100 == 0: print("%d / %d" % (c, len(prefs)))
        # Find the most similar items to this one
        scores = top_matches(prefs, item, n=n, similarity=sim_distance)
        result[item] = scores
    return result

def get_recommended_items(prefs, item_match, user):
    userRatings = prefs[user]
    scores = {}
    totalSim = {}
    # Loop over items rated by this user
    for (item, rating) in userRatings.items():
        try:
            # Loop over items similar to this one
            for (similarity, item2) in item_match[item]:
                # Ignore if this user has already rated this item
                if item2 in userRatings: continue
                # Weighted sum of rating times similarity
                scores.setdefault(item2, 0)
                scores[item2] += similarity * rating
                # Sum of all the similarities
                totalSim.setdefault(item2, 0)
                totalSim[item2] += similarity
        except KeyError:
            print("Missing Key %s" % (item))

    # Divide each total score by total weighting to get an average
    # TODO avoid double lookups
    rankings = [(score / totalSim[item], item) for item, score in scores.items() if totalSim[item] != 0]

    # Return the rankings from highest to lowest
    rankings.sort()
    rankings.reverse()
    return rankings

user_dict = {}
business_dict = {}

with open('/home/vicky/Documents/it/notes/AI/UW/Project/data/review.json') as f:
    for line in f:
        line = json.loads(line)
        user = str(line['user_id'])
        business = str(line['business_id'])
        rate = line['stars']

        if business not in business_dict:
            business_dict[business] = {}
        business_dict[business][user] = rate

        if user not in user_dict:
            user_dict[user] = {}
        user_dict[user][business] = rate

        # for key, value in user_dict.items():
        # print("Key : %s, Value: %s"% (key,value))
# for key, values in items_similar.items():
#     for i in range(len(values)):
#         if values[i][0] > 0.5:
#             print("Key : %s, Value : %s"% (values[i][0], values[i][1]))
# for j in range(len(values[i])):
#     print(values[i][j])

# bus_6nnI3DfHn-DTd6tWnZu7Jg
users_similar = calculate_similar_items(user_dict)
print(get_recommended_items(business_dict, users_similar, 'bus_F1tOtPzcsQk8PqNOatVsCg'))

# usr_zsZBYWYEmLLs81_f-HHM8w
# buss_similar = calculate_similar_items(business_dict)
# print(get_recommended_items(user_dict, buss_similar, 'usr_zsZBYWYEmLLs81_f-HHM8w'))


# Opus/UrbanSim urban simulation software.
# Copyright (C) 2005-2009 University of Washington
# See opus_core/LICENSE

from opus_core.variables.variable import Variable
from opus_core.misc import unique
from numpy import zeros, logical_not

class total_spaces(Variable):
    """return proposed spaces (units) according to unit name of the building type of the component
    """

    _return_type="int32"

    def dependencies(self):
        return [
                "urbansim_parcel.development_project_proposal_component.is_residential",
                "psrc_parcel.development_project_proposal_component.job_capacity_computed",
                "urbansim_parcel.development_project_proposal_component.residential_units"
                ]

    def compute(self,  dataset_pool):
        dppc = self.get_dataset()
        results = zeros(dppc.size(), dtype=self._return_type)
        is_residential = dppc["is_residential"].astype('bool')
        is_non_residential = logical_not(dppc["is_residential"])
        results[is_residential] = (dppc["residential_units"][is_residential]).astype(self._return_type)
        results[is_non_residential] = (dppc["job_capacity_computed"][is_non_residential]).astype(self._return_type)
        return results

    def post_check(self,  values, dataset_pool=None):
#        size = dataset_pool.get_dataset("building").size()
        self.do_check("x >= 0")

from opus_core.tests import opus_unittest
from opus_core.datasets.dataset_pool import DatasetPool
from opus_core.storage_factory import StorageFactory
from numpy import array
from opus_core.tests.utils.variable_tester import VariableTester

class Tests(opus_unittest.OpusTestCase):
    def test_my_inputs(self):
        tester = VariableTester(
            __file__,
            package_order=['urbansim_parcel','urbansim'],
            test_data={
            "development_project_proposal_component":{"proposal_component_id":array([1,2,3,4,5,6,7,8,9,10]),
                                                      "building_type_id":     array([1,2,2,2,1,2,1,3,2,2]),
                                                      "job_capacity_computed":        array([1,0,0,0,1,3,3,1,2,2])*1000,
                                                      "residential_units":    array([0,3,1,2,0,1,0,1,2,4]),
#                        "unit_name":           array(["building_sqft","residential_units","residential_units","residential_units",
#                          'building_sqft',"residential_units","building_sqft", "parcel_sqft",
#                          "residential_units","residential_units",]),
                },
           "building_type":{
                        "building_type_id":    array([1, 2, 3]),
                        "is_residential":      array([0, 1, 0]),
                        }
        }
        )

        should_be = array([1000, 3, 1, 2, 1000,  1, 3000, 1000, 2, 4])

        tester.test_is_close_for_variable_defined_by_this_module(self, should_be)

if __name__=='__main__':
    opus_unittest.main()


import unittest
import vics
import os

global test_db
test_db = "testing_db.sqlite"

class TestVicsServer(unittest.TestCase):

    def test_create_new_database(self):
        vics.create_new_database(test_db)
        self.assertTrue(os.path.exists(test_db) == 1)
        os.remove(test_db)