import warnings
from enum import Flag, auto, unique

class DutTypeInt(object):
    """Class for DutType flags. Adds the nodes attribute to an integer value. This allows direct assignment of nodes to a DutType.

    Parameters
    ----------
    value : int
    node  : [str]
        List of nodes.
    """

    def __init__(self, value, *, string, nodes=None):
        try:
            self.value = value.value
        except AttributeError:
            self.value = int(value)

        if nodes is not None:
            self.nodes = nodes
        else:
            try:
                self.nodes = value.nodes
            except AttributeError:
                self.nodes = []

        self.string = string

    def get_nodes(self):
        """Return the nodes that are typically found in this Dut_type. For convenience.

        Repeated here just to get rid of the pylint error. The real method is below in the DutType-flag

        Returns
        -------
        nodes  :  list of strings
            List of strings
        """
        return self.nodes

    def get_string(self):
        """Return the string that describes this Dut_type.

        Returns
        -------
        string  :  string
            the string that describes this object
        """
        return self.string

    def __and__(self, other):
        try:
            return DutTypeInt(self.value & other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value & other, string=self.get_string(), nodes=self.nodes)

    def __rand__(self, other):
        return self.__and__(other)

    def __xor__(self, other):
        try:
            return DutTypeInt(self.value ^ other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value ^ other, string=self.get_string(), nodes=self.nodes)

    def __rxor__(self, other):
        return self.__xor__(other)

    def __or__(self, other):
        try:
            return DutTypeInt(self.value | other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value | other, string=self.get_string(), nodes=self.nodes)

    def __ror__(self, other):
        return self.__or__(other)

    def __eq__(self, other):
        try:
            return self.value == other.value
        except AttributeError:
            return self.value == other

    def __invert__(self):
        return DutTypeInt(~self.value, string=self.get_string(), nodes=self.nodes)

    def __bool__(self):
        return bool(self.value)

    def __str__(self):
        return self.string

    def __int__(self):
        return self.value

    def is_subtype(self, other):
        """Test if a device is a subtype of an other device/devicetype

        Ignores the flag_subtype!

        Parameters
        ----------
        other : int, DutTypeInt
        """
        # remove subtype flag..
        # val_subtype_flags = (
        #     DutTypeFlag._flag_subtype_1
        #     | DutTypeFlag._flag_subtype_2
        #     | DutTypeFlag._flag_subtype_3
        #     | DutTypeFlag._flag_subtype_4
        # )
        if DutTypeFlag._flag_subtype_1 & self:
            self_wo_subtype = self - DutTypeFlag._flag_subtype_1
        elif DutTypeFlag._flag_subtype_2 & self:
            self_wo_subtype = self - DutTypeFlag._flag_subtype_2
        elif DutTypeFlag._flag_subtype_3 & self:
            self_wo_subtype = self - DutTypeFlag._flag_subtype_3
        elif DutTypeFlag._flag_subtype_4 & self:
            self_wo_subtype = self - DutTypeFlag._flag_subtype_4
        else:
            self_wo_subtype = self

        if DutTypeFlag._flag_subtype_1 & other:
            other_wo_subtype = other - DutTypeFlag._flag_subtype_1
        elif DutTypeFlag._flag_subtype_2 & other:
            other_wo_subtype = other - DutTypeFlag._flag_subtype_2
        elif DutTypeFlag._flag_subtype_3 & other:
            other_wo_subtype = other - DutTypeFlag._flag_subtype_3
        elif DutTypeFlag._flag_subtype_4 & other:
            other_wo_subtype = other - DutTypeFlag._flag_subtype_4
        else:
            other_wo_subtype = other

        res = self_wo_subtype & other_wo_subtype
        return res == other

    def __lt__(self, other):
        try:
            return DutTypeInt(self.value < other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value < other, string=self.get_string(), nodes=self.nodes)

    def __le__(self, other):
        try:
            return DutTypeInt(self.value <= other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value <= other, string=self.get_string(), nodes=self.nodes)

    def __gt__(self, other):
        try:
            return DutTypeInt(self.value > other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value > other, string=self.get_string(), nodes=self.nodes)

    def __ge__(self, other):
        try:
            return DutTypeInt(self.value >= other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value >= other, string=self.get_string(), nodes=self.nodes)

    def __sub__(self, other):
        try:
            return DutTypeInt(self.value - other.value, string=self.get_string(), nodes=self.nodes)
        except AttributeError:
            return DutTypeInt(self.value - other, string=self.get_string(), nodes=self.nodes)

    def __hash__(self):
        return hash((self.value, tuple(self.nodes), self.string))

    def bit_length(self):
        return self.value.bit_length()

    def serialize(self):
        "Converts the DutTypeInt into a dictionary with only strings (ready to be serealized to json)"
        dut_type = repr(self)
        i_start = 1  # cut away starting "<"
        i_end = dut_type.find(" object")
        return {
            "DutType": dut_type[i_start:i_end],
            "string": self.string,
            "value": self.value,
            "nodes": self.nodes,
            "__DutType__": "1.0.0",
        }

@unique  # do not allow same values for different types
class DutTypeFlag(Flag):
    """
    Flags which represents most common devices that might need to be handled by DMT

    Methods
    -------
    get_nodes(filename)
        Returns the names of the nodes typicall found in the specified DutType
    """

    _flag_subtype_1 = auto()
    _flag_subtype_2 = auto()
    _flag_subtype_3 = auto()
    _flag_subtype_4 = auto()
    flag_device = auto()
    flag_bulk = auto()
    flag_meas_struct = auto()
    flag_deemb_struct = auto()
    flag_transistor = auto()
    flag_bjt = auto()
    flag_bjt_deemb = auto()
    flag_mos = auto()
    flag_mos_deemb = auto()
    flag_diode = auto()
    flag_cap = auto()
    flag_res = auto()
    flag_tetrode = auto()
    flag_tlm = auto()
    flag_deem = auto()
    flag_vdp = auto()

    # flag_npn = auto()
    # flag_pnp = auto()
    # flag_n_mos = auto()
    # flag_p_mos = auto()

    flag_open = auto()
    flag_short = auto()

    def get_nodes(self):
        """
        Return the nodes that are typically found in this DutType. For convenience.

        Returns
        -------
        nodes  :  list of strings
            List of strings
        """
        return []

    def get_string(self):
        """
        Return the string that describes this DutType.

        Returns
        -------
        nodes  :  string
            String that describes this DutType.
        """
        return ""

    def __str__(self):
        return str(self.value.__class__)

    def serialize(self):
        "Converts the DutTypeFlag into a dictionary with only strings (ready to be serealized to json)"
        dut_type = repr(self)
        i_start = 1  # cut away starting "<"
        i_end = dut_type.find(":")
        return {
            "DutType": dut_type[i_start:i_end],
            "string": self.get_string(),
            "value": self.value,
            "nodes": self.get_nodes(),
            "__DutType__": "1.0.0",
        }

class DutType(object):
    """concrete DutTypes to be used for DutViews"""

    dummy = DutTypeInt(0, string="dummy")  # dummy is nothing! Use DutTypeInt to allow get_nodes
    device = DutTypeInt(DutTypeFlag.flag_device, string="device")
    bulk = DutTypeInt(DutTypeFlag.flag_bulk, string="bulk")
    meas_struct = DutTypeInt(DutTypeFlag.flag_meas_struct, string="meas_struct")
    deemb_struct = DutTypeInt(DutTypeFlag.flag_deemb_struct, string="deemb_struct")

    # now the mixed flags, are DutTypeInts as the numbers are already given:
    transistor = DutTypeInt(device | DutTypeFlag.flag_transistor, string="transistor")
    bjt = DutTypeInt(transistor | DutTypeFlag.flag_bjt, nodes=["B", "C", "E", "S"], string="bjt")
    mos = DutTypeInt(transistor | DutTypeFlag.flag_mos, nodes=["G", "D", "S", "B"], string="mos")
    deem_bjt = DutTypeInt(
        deemb_struct | DutTypeFlag.flag_bjt_deemb,
        nodes=["B", "C", "E", "S"],
        string="bjt_deemb",
    )  # because of node names :(
    deem_mos = DutTypeInt(
        deemb_struct | DutTypeFlag.flag_mos_deemb,
        nodes=["G", "D", "S", "B"],
        string="mos_deemb",
    )  # because of node names :(

    # npn = DutTypeInt(bjt | DutTypeFlag.flag_npn, string="npn")  # nodes are inherited from bjt
    npn = DutTypeInt(
        bjt | DutTypeFlag._flag_subtype_1, string="npn"
    )  # nodes are inherited from bjt
    # pnp = DutTypeInt(bjt | DutTypeFlag.flag_pnp, string="pnp")
    pnp = DutTypeInt(bjt | DutTypeFlag._flag_subtype_2, string="pnp")
    # n_mos = DutTypeInt(mos | DutTypeFlag.flag_n_mos, string="nmos")
    n_mos = DutTypeInt(mos | DutTypeFlag._flag_subtype_1, string="nmos")
    # p_mos = DutTypeInt(mos | DutTypeFlag.flag_p_mos, string="pmos")
    p_mos = DutTypeInt(mos | DutTypeFlag._flag_subtype_2, string="pmos")

    diode = DutTypeInt(device | DutTypeFlag.flag_diode, nodes=["C", "A"], string="diode")
    pn_diode = DutTypeInt(diode | DutTypeFlag._flag_subtype_1, string="pn-diode")
    pin_diode = DutTypeInt(diode | DutTypeFlag._flag_subtype_2, string="pin-diode")
    cap = DutTypeInt(device | DutTypeFlag.flag_cap, nodes=["C", "A"], string="capacitance")
    res = DutTypeInt(device | DutTypeFlag.flag_res, nodes=["C", "A"], string="resistor")

    tlm = DutTypeInt(
        meas_struct | DutTypeFlag.flag_tlm, nodes=["L", "M", "R"], string="tlm"
    )  # left, middle, right
    tlmb = DutTypeInt(tlm | DutTypeFlag._flag_subtype_1, string="tlm-base")
    tlmc = DutTypeInt(tlm | DutTypeFlag._flag_subtype_2, string="tlm-collector")
    tlmbc = DutTypeInt(tlm | DutTypeFlag._flag_subtype_3, string="tlm-base-collector")

    vdp = DutTypeInt(
        meas_struct | DutTypeFlag.flag_vdp, nodes=["A", "B", "C", "D"], string="vdp"
    )  # four arbitrary contacts

    deem_open_bjt = DutTypeInt(deem_bjt | DutTypeFlag.flag_open, string="open-bjt")
    deem_short_bjt = DutTypeInt(deem_bjt | DutTypeFlag.flag_short, string="short-bjt")

    deem_open_mos = DutTypeInt(deem_mos | DutTypeFlag.flag_open, string="open-mos")
    deem_short_mos = DutTypeInt(deem_mos | DutTypeFlag.flag_short, string="short-mos")

    tetrode = DutTypeInt(
        meas_struct | DutTypeFlag.flag_tetrode,
        nodes=["B1", "B2", "E", "C", "S"],
        string="tetrode",
    )

    cap_ac = DutTypeInt(
        cap | meas_struct, nodes=["L", "R", "G", "S"], string="capacitance-ac"
    )  # capacitance in GSG pads, each pad is one Capacitance, so S(1,1) and S(2,2) are wanted...

    @classmethod
    def deserialize(cls, dict_loaded):
        """Static class method to create a DutType from a loaded dictionary.

        Returns
        -------
        DutTypeInt or DutTypeFlag
            DutType ready to be used.
        """
        if "DutTypeFlag" in dict_loaded["DutType"]:
            dut_type = getattr(DutTypeFlag, dict_loaded["DutType"].split(".")[1])
            # just be sure...
            dut_type.nodes = dict_loaded["nodes"]
            dut_type.string = dict_loaded["string"]
        elif "DutTypeInt" in dict_loaded["DutType"]:
            types_int = [
                member
                for member in cls.__dict__.keys()
                if not member.startswith("__") and not callable(getattr(cls, member))
            ]
            dut_type = None
            for key_dut_typ_int in types_int:
                dut_type_int = getattr(cls, key_dut_typ_int)
                if dut_type_int.get_string() != dict_loaded["string"]:
                    continue

                dut_type = dut_type_int
            # just be sure...
            if dut_type is None:
                warnings.warn(
                    "DMT.DutType: Did not find the loaded duttype with the string '"
                    + dict_loaded["string"]
                    + "' in the current DMT version."
                )
                dut_type = DutTypeInt(
                    dict_loaded["value"],
                    string=dict_loaded["string"],
                    nodes=dict_loaded["nodes"],
                )
            else:
                dut_type.nodes = dict_loaded["nodes"]
        else:
            raise IOError(
                "DMT.DutType: I dont know how to deserealize the DutType: " + dict_loaded["DutType"]
            )

        return dut_type


#!/usr/bin/env python

# ==========================================================================
# Author:         scps950707
# Email:          scps950707@gmail.com
# Created:        2018-06-19 15:20
# Last Modified:  2018-06-21 19:40
# Filename:       parse.py
# Description:    Plot the malloc/free sequence from paired file
# ==========================================================================
from __future__ import print_function
from sys import stdin
import matplotlib.pyplot as plt
from collections import Counter

allSeq = []

# format example:
# malloc 5 0xfcfdc0
# free 5 0xfcfdc0

for line in stdin:
    data = line.rstrip('\n').split(' ')
    if data[0] == "malloc" or data[0] == "free":
        allSeq.append({'op': data[0],
                       'size': int(data[1]),
                       'addr': data[2],
                       'isMatched': False})

allocSizeSeq = [i['size'] for i in allSeq if i['op'] == "malloc"]
dic = dict()
dic = Counter(allocSizeSeq)
# for key in sorted(dic):
for key in dic:
    # print("%s: %s" % (key, dic[key]))
    plt.plot([key], [dic[key]], marker='o', markersize=6, color="red")
plt.title('Allocation Size Distribution', size=26)
plt.xlabel('Allocation Size (bytes)', size=26)
plt.ylabel('Number of allocations', size=26)
plt.xticks(fontsize=26)
plt.yticks(fontsize=26)
plt.xscale('log')
plt.yscale('log')
plt.show()


import argparse

import torch
import torch.backends.cudnn as cudnn
import numpy as np
import PIL.Image as pil_image

from models import FSRCNN
from utils import convert_ycbcr_to_rgb, preprocess, calc_psnr

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights-file', type=str,default = './output')
    parser.add_argument('--image-file', type=str,default = './data/Set5_x3.h5')
    parser.add_argument('--scale', type=int, default=3)
    args = parser.parse_args()

    cudnn.benchmark = True
    device = torch.device('npu:0' if torch.npu.is_available() else 'cpu')

    model = FSRCNN(scale_factor=args.scale).to(device)

    state_dict = model.state_dict()
    for n, p in torch.load(args.weights_file, map_location=lambda storage, loc: storage).items():
        if n in state_dict.keys():
            state_dict[n].copy_(p)
        else:
            raise KeyError(n)

    model.eval()

    image = pil_image.open(args.image_file).convert('RGB')

    image_width = (image.width // args.scale) * args.scale
    image_height = (image.height // args.scale) * args.scale

    hr = image.resize((image_width, image_height), resample=pil_image.BICUBIC)
    lr = hr.resize((hr.width // args.scale, hr.height // args.scale), resample=pil_image.BICUBIC)
    bicubic = lr.resize((lr.width * args.scale, lr.height * args.scale), resample=pil_image.BICUBIC)
    bicubic.save(args.image_file.replace('.', '_bicubic_x{}.'.format(args.scale)))

    lr, _ = preprocess(lr, device)
    hr, _ = preprocess(hr, device)
    _, ycbcr = preprocess(bicubic, device)

    with torch.no_grad():
        preds = model(lr).clamp(0.0, 1.0)

    psnr = calc_psnr(hr, preds)
    print('PSNR: {:.2f}'.format(psnr))

    preds = preds.mul(255.0).cpu().numpy().squeeze(0).squeeze(0)

    output = np.array([preds, ycbcr[..., 1], ycbcr[..., 2]]).transpose([1, 2, 0])
    output = np.clip(convert_ycbcr_to_rgb(output), 0.0, 255.0).astype(np.uint8)
    output = pil_image.fromarray(output)
    output.save(args.image_file.replace('.', '_fsrcnn_x{}.'.format(args.scale)))


from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, status, mixins
from rest_framework.decorators import action
from django.http import JsonResponse
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.response import Response

from .filters import BoardFilter
from .models import (Board, Favorite, ParticipantInBoard)
from .permissions import (IsAuthor, IsParticipant, IsStaff,
                          IsAuthorOrParticipantOrAdminListParticipantsAndTags,
                          IsAuthorOrModeratorOrAdminDelParticipantsPutTags)
from .serializers import (BoardListOrCreateSerializer, BoardSerializer,
                          ParticipantInBoardSerializer,
                          SwitchModeratorSerializer,
                          SearchBoardSerializer, SearchCardSerializer)
from .tag_serializer import TagSerializer
from cards.models import Card

class BoardViewSet(viewsets.ModelViewSet):
    filter_backends = [DjangoFilterBackend, ]
    filter_class = BoardFilter

    def get_queryset(self):
        user = self.request.user

        if user.is_superuser or user.is_staff:
            return Board.objects.all()

        return Board.objects.filter(participants__id=self.request.user.id)

    def get_serializer_class(self):

        if self.action in ('list', 'create'):
            return BoardListOrCreateSerializer

        if self.action in ('retrieve', 'update', 'partial_update', 'destroy'):
            return BoardSerializer

    def get_permissions(self):

        if self.action in ('list', 'create'):
            return [IsAuthenticated()]

        if self.action in ('retrieve', 'favorite', 'leave'):
            return [(IsAuthor | IsParticipant | IsStaff)()]

        if self.action in ('update', 'partial_update', 'destroy',
                           'switch_moderator'):
            return [(IsAuthor | IsStaff)()]

    @action(detail=True, methods=['post', 'delete'])
    def favorite(self, request, **kwargs):
        user = request.user
        board = get_object_or_404(Board, id=kwargs.get('pk'))
        self.check_object_permissions(self.request, board)

        if request.method == 'POST':
            _, is_created = Favorite.objects.get_or_create(user=user,
                                                           board=board)

            if not is_created:
                return Response({
                    'status': 'error',
                    'message': 'Вы уже добавили данную доску в избранное!'},
                    status=status.HTTP_400_BAD_REQUEST)

            return Response(status=status.HTTP_201_CREATED)

        if request.method == 'DELETE':
            count, _ = Favorite.objects.filter(user=user, board=board).delete()

            if count == 0:
                return Response({
                    'status': 'error',
                    'message': 'Данной доски нет в списке избранных!'},
                    status=status.HTTP_400_BAD_REQUEST)

            return Response(status=status.HTTP_204_NO_CONTENT)

    @action(detail=True, methods=['post'])
    def switch_moderator(self, request, **kwargs):
        board = get_object_or_404(Board, id=kwargs.get('pk'))
        self.check_object_permissions(self.request, board)

        data = request.data
        data['board_id'] = kwargs.get('pk')
        serializer = SwitchModeratorSerializer(data=data)
        serializer.is_valid(raise_exception=True)

        user_id = request.data['id']
        participant_in_board = get_object_or_404(ParticipantInBoard,
                                                 board=board,
                                                 participant__=user_id,
                                                 )

        if participant_in_board.is_moderator:
            participant_in_board.is_moderator = False
            participant_in_board.save()

            return Response(
                {'status': 'success',
                 'message': 'Данный пользователь больше не модератор!'},
                status=status.HTTP_200_OK)

        participant_in_board.is_moderator = True
        participant_in_board.save()

        return Response({'status': 'success',
                         'message': 'Данный пользователь теперь модератор!'},
                        status=status.HTTP_200_OK)

    @action(detail=True, methods=['post'])
    def leave(self, request, **kwargs):
        board = get_object_or_404(Board, id=kwargs.get('pk'))
        self.check_object_permissions(self.request, board)

        if request.user == board.author:
            return Response({
                'status': 'error',
                'message': 'Автор доски не может покинуть доску!'},
                status=status.HTTP_400_BAD_REQUEST)

        board.participants.remove(request.user)
        return Response(status=status.HTTP_204_NO_CONTENT)

class ParticipantInBoardViewSet(viewsets.GenericViewSet,
                                mixins.ListModelMixin,
                                mixins.RetrieveModelMixin,
                                mixins.DestroyModelMixin):
    serializer_class = ParticipantInBoardSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['is_moderator']

    def get_queryset(self):
        board = get_object_or_404(Board, id=self.kwargs.get('board_id'))

        return ParticipantInBoard.objects.filter(board=board)

    def get_permissions(self):

        if self.action == 'list':
            return [IsAuthorOrParticipantOrAdminListParticipantsAndTags()]

        if self.action == 'retrieve':
            return [(IsAuthor | IsParticipant | IsStaff)()]

        if self.action == 'destroy':
            return [IsAuthorOrModeratorOrAdminDelParticipantsPutTags()]

    def destroy(self, request, *args, **kwargs):
        board = get_object_or_404(Board, id=kwargs.get('board_id'))
        user_id = kwargs.get('pk')
        participant_in_board = get_object_or_404(ParticipantInBoard,
                                                 board=board,
                                                 participant__id=user_id,
                                                 )

        if participant_in_board.participant == board.author:
            return Response({
                'status': 'error',
                'message': 'Автора доски нельзя исключить из участников!'},
                status=status.HTTP_400_BAD_REQUEST)

        if participant_in_board.is_moderator and request.user != board.author:
            return Response({
                'status': 'error',
                'message': 'Исключить модератора может только автор доски!'},
                status=status.HTTP_400_BAD_REQUEST)

        board.participants.remove(user_id)

        for list_ in board.lists.all():
            for card in list_.cards.all():
                card.participants.remove(user_id)

        return Response(status=status.HTTP_204_NO_CONTENT)

class TagInBoardViewSet(viewsets.GenericViewSet,
                        mixins.ListModelMixin,
                        mixins.RetrieveModelMixin,
                        mixins.UpdateModelMixin):
    serializer_class = TagSerializer

    def get_queryset(self):
        board = get_object_or_404(Board, id=self.kwargs.get('board_id'))

        return board.tags.all()

    def get_permissions(self):

        if self.action == 'list':
            return [IsAuthorOrParticipantOrAdminListParticipantsAndTags()]

        if self.action == 'retrieve':
            return [(IsAuthor | IsParticipant | IsStaff)()]

        if self.action in ('update', 'partial_update',):
            return [IsAuthorOrModeratorOrAdminDelParticipantsPutTags()]

class SearchAPIView(APIView):

    def get(self, request):
        name = request.GET.get('name', None)
        boards = Board.objects.filter(participants__id=self.request.user.id)
        cards = Card.objects.filter(
            list__board__participants__id=self.request.user.id)

        if name:
            boards = boards.filter(name__icontains=name)
            cards = cards.filter(name__icontains=name)

            return JsonResponse({
                'boards': SearchBoardSerializer(instance=boards,
                                                many=True).data,
                'cards': SearchCardSerializer(instance=cards,
                                              many=True).data
            })

        return JsonResponse({'boards': [], 'cards': []})


import numpy as np
import porepy as pp
import scipy.sparse as sps
import scipy.spatial

import pygeon as pg

class VoronoiGrid(pg.Grid):
    """docstring for VoronoiGrid."""

    def __init__(self, num_bdry_els, num_pts, seed=None, name="VoronoiGrid"):
        # Generate the internal seed points for the Voronoi grid

        if seed is not None:
            np.random.seed(seed)
        pts = np.random.rand(2, num_pts)

        bdry_mesh_size = 1.0 / num_bdry_els
        bdry_margin = 0.5 * bdry_mesh_size
        pts = bdry_margin + pts * (1 - 2 * bdry_margin)

        # Append the boundary seeds
        bdry_pts = np.linspace(bdry_mesh_size / 2, 1 - bdry_mesh_size / 2, num_bdry_els)

        east = np.vstack((np.ones_like(bdry_pts), bdry_pts))
        north = np.vstack((bdry_pts[::-1], np.ones_like(bdry_pts)))
        west = np.vstack((np.zeros_like(bdry_pts), bdry_pts[::-1]))
        south = np.vstack((bdry_pts, np.zeros_like(bdry_pts)))

        pts = np.hstack((east, north, west, south, pts))

        # Use Scipy to generate the Voronoi grid
        vor = scipy.spatial.Voronoi(pts[:2, :].T)

        # Connect infinite faces to a boundary node
        ridge_dict = dict((tuple(sorted(k)), v) for k, v in vor.ridge_dict.items())

        bdry_ind_0 = np.arange(4 * num_bdry_els)
        bdry_ind_1 = np.roll(bdry_ind_0, -1)

        new_verts = np.zeros((2, bdry_ind_0.size))
        num_verts = vor.vertices.shape[0]

        int_to_bdry = {}

        for id0, id1 in zip(bdry_ind_0, bdry_ind_1):
            new_verts[:, id0] = (pts[:, id0] + pts[:, id1]) / 2

            dashed_line = ridge_dict[tuple(sorted((id0, id1)))]
            face_indx = vor.ridge_vertices.index(dashed_line)

            int_to_bdry[vor.ridge_vertices[face_indx][1]] = num_verts + id0
            vor.ridge_vertices[face_indx][0] = num_verts + id0
            vor.ridge_vertices.append([num_verts + id0, num_verts + id1])

        # Sort ridge_vertices for quick lookup
        [rv.sort() for rv in vor.ridge_vertices]

        # Complete regions at the boundary
        for id0 in bdry_ind_0:
            region_nodes = vor.regions[vor.point_region[id0]]

            idx = region_nodes.index(-1)
            region_nodes[idx : idx + 1] = [
                int_to_bdry[region_nodes[idx - 1]],
                int_to_bdry[region_nodes[idx - len(region_nodes) + 1]],
            ]

        corner_idx = np.arange(num_bdry_els - 1, 4 * num_bdry_els, num_bdry_els)
        new_verts[:, corner_idx] = np.round(new_verts[:, corner_idx])

        vor.vertices = np.vstack((vor.vertices, new_verts.T))

        # Get the node coordinates
        nodes = vor.vertices.T
        nodes = np.vstack((nodes, np.zeros(nodes.shape[1])))

        # Derive face-node connectivity
        internal_faces = [f for f in vor.ridge_vertices]
        indices = np.hstack(internal_faces)
        indptr = 2 * np.arange(len(internal_faces) + 1)
        data = np.ones(2 * len(internal_faces), dtype=int)
        vor.ridge_points
        face_nodes = sps.csc_matrix((data, indices, indptr))

        # Compute cell-face connectivity

        # Extract the start and end nodes of the region faces
        internal_regions = [r for r in vor.regions if len(r) > 0]

        for indx, r in enumerate(internal_regions):
            check = pp.geometry_property_checks.is_ccw_polygon(nodes[:2, r])
            internal_regions[indx] = r[:: 2 * check - 1]

        start_node = np.hstack(internal_regions)
        end_node = np.hstack([np.roll(r, -1) for r in internal_regions])

        # Construct a matrix with ones on the nodes for each region face
        face_finder_indices = np.vstack((start_node, end_node)).ravel("F")
        face_finder_indptr = 2 * np.arange(start_node.size + 1)
        face_finder = sps.csc_matrix(
            (np.ones_like(face_finder_indices), face_finder_indices, face_finder_indptr)
        )

        # Multiply with face_nodes to match region faces with their global number
        FaFi = face_finder.T @ face_nodes

        # Extract the indices, indptr and orientation
        cf_data = np.sign(end_node - start_node).astype(int)
        cf_indices = FaFi.indices[FaFi.data == 2]
        cf_indptr = np.hstack((0, np.cumsum([len(r) for r in internal_regions])))

        assert (
            cf_data.size == cf_indices.size
        ), "Try coarsening the boundaries or increasing the number of interior points"

        cell_faces = sps.csc_matrix((cf_data, cf_indices, cf_indptr))

        # Generate a PyGeoN grid
        super().__init__(2, nodes, face_nodes, cell_faces, name)


from selenium import webdriver
import time

print("Enter youtube URL: ",end=" ")
url_user=input()

#The line below is used to import the webdriver. You can replace chrome by your favourite web browser. Check the readme for more info!
driver=webdriver.Chrome()
driver.get("https://ytmp3.cc/en13/")

urlBar=driver.find_element_by_xpath("//*[@id='input']")
urlBar.send_keys(url_user)

driver.find_element_by_xpath("//*[@id='submit']").click()

#I made this 10 second pause, in order for the converter to process longer videos, howerver you can reduce the hold to a second or two. Check the readme for more info!
time.sleep(10)
driver.find_element_by_xpath("//*[@id='buttons']/a[1]").click()
driver.close()


from pathlib import Path
import csv

dldir_name = 'download1/225.csv'
dldir_path = Path(dldir_name)
dldir_new_name = 'download1/226.csv'

# Read CSV file
with open(dldir_path, mode="r", encoding="utf-8") as rf:
    reader = csv.reader(rf)
    # Header Skip
    next(reader)
    # Output File
    with open(dldir_new_name, mode="w", encoding="utf-8") as wf:
        writer = csv.writer(wf)
        writer.writerow(["id"])
        # Output Row
        for line in reader:
            writer.writerow([line[0]*2])


import logging
import torch.nn as nn
import torch.nn.functional as F

logger = logging.getLogger(__name__)

class Model_2(nn.Module):
    '''Define an exsample CNN model, which should slightly better that Model_1.

    Ref: https://shonit2096.medium.com/cnn-on-cifar10-data-set-using-pytorch-34be87e09844
    '''

    def __init__(self):
        super().__init__()
        # Convolutional layer
        self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
        self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
        self.conv3 = nn.Conv2d(32, 64, 3, padding=1)
        # Max pooling layer
        self.pool = nn.MaxPool2d(2, 2)
        # Fully connected layers
        self.fc1 = nn.Linear(64 * 4 * 4, 512)
        self.fc2 = nn.Linear(512, 64)
        self.fc3 = nn.Linear(64, 10)
        # Dropout
        self.dropout = nn.Dropout(p=0.5)

    def forward(self, x):
        # Add sequence of convolutional and max pooling layers
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = self.pool(F.relu(self.conv3(x)))
        # Flattening
        x = x.view(-1, 64 * 4 * 4)
        # Fully connected layers
        x = self.dropout(F.relu(self.fc1(x)))
        x = self.dropout(F.relu(self.fc2(x)))
        x = self.fc3(x)
        return x


import graphene
from graphene_django import DjangoObjectType

from .models import Category, Product, Image

class CategoryType(DjangoObjectType):
    class Meta:
        model = Category
        fields = ("id", "name", "parent", "ordering", "slug", "is_active")

class ProductImageType(DjangoObjectType):
    class Meta:
        model = Image
        field = (
            "id",
            "product",
            "name",
            "image",
            "image_ppoi",
            "alt_text",
            "is_feature",
            "created_at",
            "updated_at",
        )

    def resolve_image(self, info) -> str:
        if self.image:
            self.image = info.context.build_absolute_uri(self.image.url)
        return self.image

class ProductType(DjangoObjectType):
    class Meta:
        model = Product
        fields = (
            "id",
            "title",
            "category",
            "vendor",
            "description",
            "slug",
            "price",
            "is_available",
            "condition",
            "created_at",
            "updated_at",
        )

class Query(graphene.ObjectType):
    all_Categories = graphene.List(CategoryType)
    category_by_name = graphene.Field(CategoryType, name=graphene.String(required=True))
    all_Products = graphene.List(ProductType)
    all_Products_by_name = graphene.Field(
        ProductType, slug=graphene.String(required=True)
    )

    def resolve_category_by_name(self, info, name) -> CategoryType:
        try:
            return Category.objects.get(name=name)
        except Category.DoesNotExist:
            return None

    def resolve_all_Products_by_name(self, info, slug) -> ProductType:
        try:
            return Product.objects.get(slug=slug)
        except Product.DoesNotExist:
            return None

    def resolve_all_Categories(self, info) -> CategoryType:
        return Category.objects.filter(level=1)

    def resolve_all_Products(self, info) -> ProductType:
        return Product.objects.all()


from typing import Any
import cv2
import numpy as np
import openpyxl
import os
import requests

class ImageSample:
    # class to download the sample images from the XLSX file
    def __init__(self, file_path: str):
        self.file_path = file_path
        self.image_dict = {}
        os.makedirs("data/initial_images", exist_ok=True)
        os.makedirs("data/final_images", exist_ok=True)

    def read_xlsx_file(self):
        try:
            workbook = openpyxl.load_workbook(self.file_path)
            sheet = workbook.active

            for row in sheet.iter_rows(min_row=2, values_only=True):
                key = row[0]
                initial_image_url = row[3]
                final_image_url = row[4]

                if not key or not initial_image_url or not final_image_url:
                    continue

                self.image_dict[key] = {
                    'initial_image': self.download_image(
                        initial_image_url, "data/initial_images/"),
                    'final_image': self.download_image(
                        final_image_url, "data/final_images/")
                }

        except Exception as e:
            print(f"Error while reading the XLSX file: {e}")

    def download_image(self, url: str, image_directory: str) -> Any:
        image_path = image_directory + os.path.basename(url)
        if os.path.exists(image_path):
            return self.read_image(image_path)
        try:
            response = requests.get(url)
            if response.status_code == 200:

                with open(image_path, 'wb') as f:
                    f.write(response.content)

                print(f"Downloaded: {url} -> {image_path}")
                return response.content
            else:
                print(
                    f"Failed to download: {url})")
                return None

        except Exception as e:
            print(f"Error while downloading {url}: {e}")
            return None

    def read_image(self, image_path: str) -> np.ndarray:
        try:
            image = cv2.imread(image_path)
            return image
        except Exception as e:
            print(f"Error while reading the image: {e}")
            return None

    def get_image_dict(self):
        return self.image_dict


# -*- coding:utf-8 -*-
# 作者：KKKC

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier as RF # random forest
from sklearn.feature_selection import chi2, VarianceThreshold

def Get_feature():
    train_data = pd.read_csv('G:/ML/balanced_data.csv')
    test_data = pd.read_csv('G:/ML/Test-min-max.csv')
    x_train = train_data.iloc[:,0:-1]
    y_train = train_data.iloc[:,-1]
    name = x_train.columns.values
    print(name)
    x_test = test_data.iloc[:,0:-1]
    y_test = test_data.iloc[:,-1]
    # 划分训练集和测试集
    # x_train , x_test , y_train, y_test = train_test_split(X,Y,test_size=0.3)
    chi = chi2(x_train,y_train)
    sort = np.argsort(chi[0])[::-1]# 对卡方值进行排序
    feature = sort[0:30] # 排名前30的特征
    print(feature)

    # name list
    feature_name = []
    for i in feature:
        feature_name.append(name[i])

    print(feature_name)

    # 转化为pd
    x_train = pd.DataFrame(x_train)
    x_test = pd.DataFrame(x_test)

    for i in x_train.columns:
        if i not in feature_name:
            x_train.drop(i,axis=1,inplace=True)
            x_test.drop(i,axis=1,inplace=True)

    print(x_train.shape)

    # 构建rf模型进行进一步的特征选择

    model = RF(n_estimators=100,oob_score=True,random_state=1234)
    model = model.fit(x_train,y_train)
    feature_list = model.feature_importances_ # 重要性列表
    feature_sort = np.argsort(feature_list)[::-1] # 排序
    feature_rf = feature_sort[0:2] # 2个特征，筛选几个改几个

    # 获取相应特征的名字
    name = []
    for i in feature_rf:
        name.append(x_train.columns[i])

    print(name)

    # 删除其他特征
    for i in x_train.columns:
        if i not in name:
            x_train.drop(i,axis=1,inplace=True)
            x_test.drop(i,axis=1,inplace=True)

    #
    x_train.to_csv('C:/Users/HP/Desktop/x_train.csv')
    x_test.to_csv('C:/Users/HP/Desktop/x_test.csv')
    y_train.to_csv('C:/Users/HP/Desktop/y_train.csv')
    y_test.to_csv('C:/Users/HP/Desktop/y_test.csv')
    return x_train,x_test,y_train,y_test


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
import sublime
import sublime_plugin
import re
import datetime

class StrToPs(sublime_plugin.TextCommand):
    def run(self, edit):
        print("___________NEW RUN___________")
        v = self.view
        resultStr = ""
        startTime = datetime.datetime.now()
        regions = v.sel()
        begin  = True
        level = 0
        need_skip = False
        print("PreFOR")
        for region in regions:
            s = v.substr(region)
            #ps = s[15:]
            list_res = s.split("*")
            j = 0
            for str_p in list_res:
                print(str(j)+"-"+str_p)
                j=j+1
            size_res = len(list_res)
            limit = int(list_res[3])
            cur_index = 7
            st_value  = int(list_res[cur_index - 1])
            print("PreInvoke TravelTree")
            resultStr, cur_index = StrToPs.TravelTree(level, 0, st_value, list_res, size_res, size_res, cur_index, resultStr)

        if resultStr:
            newView = v.window().new_file()
            newView.run_command("append",{"characters": resultStr})
            newView.run_command("append",{"characters": "\nExecute time: " + str((datetime.datetime.now() - startTime).seconds) + " seconds"})
            #newView.show_popup(, sublime.HTML, location=-1, on_navigate=print)
            newView.set_syntax_file('Packages/User/SiebelLog.tmLanguage')
        sublime.status_message("Complete - StrToPS")

    def TravelTree(level, limit, nextLen, list_res, list_size, list_limit_size, start_index, result_str):
        print("_____________________InvokeTravelTree")
        cur_index = start_index
        print("cur_index="+str(cur_index))
        st_value  = nextLen
        print("st_value="+str(st_value))
        resultStr = result_str
        print("resultStr="+str(resultStr))

        begin  = True
        first_run = True
        local_limit = limit
        print("local_limit="+str(local_limit))
        cur_limit = 0
        while True:
            if cur_index > list_limit_size or  cur_index >= list_size or (local_limit > 0 and cur_limit >= local_limit):
                print("c="+str(cur_index)+" l="+str(list_size)+" ll="+str(list_limit_size))
                print("local_limit="+str(local_limit)+" cur_limit="+str(cur_limit))
                return [resultStr, cur_index - 1]
            else:
                print("cur_index="+str(cur_index))
                print("st_value="+str(st_value))
                cur_str = list_res[cur_index]
                print("cur_str="+cur_str)
                if begin:
                    if cur_str.isdigit():
                        print("resultStrPRETRAVEL="+resultStr)
                        print("cur_index="+str(cur_index))
                        print("level="+str(level))
                        print("list_size="+str(list_size))
                        print("list_limit_size="+str(list_limit_size))
                        print("list_res[cur_index]="+list_res[cur_index])
                        print("st_value="+str(st_value))
                        next_level = level + 1
                        next_lenght = int(list_res[cur_index + 1])
                        next_position = cur_index + 2
                        next_size = next_position + st_value * 2 + int(cur_str) + 1
                        print("next_position="+str(next_position))
                        print("next_lenght="+str(next_lenght))
                        #cur_index = cur_index + 2
                        #resultStr, cur_index = StrToPs.TravelTree(next_level, next_lenght, list_res, next_size, next_position, resultStr)
                        if int(cur_str) == 0:
                            resultStr, cur_index = StrToPs.TravelTree(next_level, 0, next_lenght, list_res, list_size, next_size, next_position, resultStr)
                        else:
                            resultStr, cur_index = StrToPs.TravelTree(next_level, int(cur_str), next_lenght, list_res, list_size, list_size, next_position, resultStr)
                        cur_limit = cur_limit + 1
                        #return [resultStr, cur_index]
                    else:
                        begin = False
                        i = 0
                        if first_run:
                            lvl_tmp = level - 1
                            first_run = False
                        else:
                            lvl_tmp = level

                        while i < lvl_tmp:
                            resultStr = resultStr + "-"
                            i = i + 1

                        resultStr = resultStr + "[" + cur_str[:st_value] + "]"
                        #print("bName="+cur_str)
                        #print("bSize="+cur_str[st_value:])
                        st_value = int(cur_str[st_value:])
                else:
                    begin = True
                    #print(cur_str)
                    #print(cur_str[:st_value])
                    #print(cur_str[st_value:])
                    if st_value > 0:
                        resultStr = resultStr + "='" + cur_str[:st_value] +"'\n"
                    else:
                        resultStr = resultStr + "\n"
                    #print("st="+str(st_value) +" len="+str(len(cur_str)))
                    #if st_value < len(cur_str):
                    if cur_index <= list_size:
                        st_value = int(cur_str[st_value:])
            cur_index = cur_index + 1

            #print (str(list_res[7])[:int(st_value)])
            #while cur_index < size_res:
            #    cur_str = list_res[cur_index]
            #    if begin:
            #        if cur_str.isdigit():
            #            cur_index = cur_index + 1
            #            level = level + 1
            #            cur_str = list_res[cur_index]
            #            st_value = int(cur_str)
            #        else:
            #            begin = False
            #            i = 0
            #            while i < level:
            #                resultStr = resultStr + "-"
            #                i = i + 1
            #            resultStr = resultStr + "[" + cur_str[:st_value] + "]"
            #            print("bName="+cur_str)
            #            print("bSize="+cur_str[st_value:])
            #            st_value = int(cur_str[st_value:])
            #    else:
            #        begin = True
                    #print(cur_str)
                    #print(cur_str[:st_value])
                    #print(cur_str[st_value:])
            #        resultStr = resultStr + "='" + cur_str[:st_value] +"'\n"
                    #print("st="+str(st_value) +" len="+str(len(cur_str)))
            #        if st_value < len(cur_str):
            #            st_value = int(cur_str[st_value:])
            #    cur_index = cur_index + 1

            ##k = 1
            ##for res in list_res:
            ##    if len(res) > 1 or (len(res) == 1 and k == size_res):
            ##        if not need_skip:
            ##            if begin:
                            ##Если ==1, значить проперти
            ##                if int(res[-1:]) != 0:
            ##                    i = 0
            ##                    while i < level:
                                    #resultStr = resultStr + "-"
            ##                        i = i + 1
                                #resultStr = resultStr + res[:-1]
            ##                    begin = False
                            ##Иначе дочерний элемент
            ##                else:
            ##                    level = level + 1
            ##                    i = 0
            ##                    while i < level:
            ##                        resultStr = resultStr + "-"
            ##                        i = i + 1
                                #resultStr = resultStr + res[:-1] + "\n"
            ##                    need_skip = True
                        #else:
                            ##Если последний элемент, то не обрезаем последний символ
                            #if k == size_res:
                                #resultStr = resultStr + "=" + res + "\n"
                            ##Обрезаем последний символ элемента
                            #else:
                                #resultStr = resultStr + "=" + res[:-1] + "\n"
                            #    begin = True
                    #else:
                    #    need_skip = False
                #else:
                #    print(res)
                #    need_skip = False
                #    begin = True
                #k = k + 1


from django.urls import re_path

from basketapp.views import (
    view, add, remove
)

app_name = 'basketapp'

urlpatterns = [
    re_path(r'^$', view, name='view'),
    re_path(r'^add/(?P<pk>\d+)/$', add, name='add'),
    re_path(r'^remove/(?P<pk>\d+)/$', remove, name='remove')
]


import os
import random
import torch
import argparse
import numpy as np

from io_handler import PairIoHandler, PlayerIoHandler, SampleIoHandler, set_args
from util.utils import clamp, seed_torch, prepare, log_args_and_backup_code

def gen_pairs(grid_size: int, pair_num: int, stride: int = 1) -> np.ndarray:
    """
    Input:
        grid_size: int, the image is partitioned to grid_size * grid_size patches. Each patch is considered as a player.
        pair_num: int, how many (i,j) pairs to sample for one image
        stride: int, j should be sampled in a neighborhood of i. stride is the radius of the neighborhood.
            e.g. if stride=1, then j should be sampled from the 8 neighbors around i
                if stride=2, then j should be sampled from the 24 neighbors around i
    Return:
        total_pairs: (pair_num,2) array, sampled (i,j) pairs
    """

    neighbors = [(i, j) for i in range(-stride, stride + 1)
                 for j in range(-stride, stride + 1)
                 if
                 i != 0 or j != 0]

    total_pairs = []
    for _ in range(pair_num):
        while True:
            x1 = np.random.randint(0, grid_size)
            y1 = np.random.randint(0, grid_size)
            point1 = x1 * grid_size + y1

            neighbor = random.choice(neighbors)
            x2 = clamp(x1 + neighbor[0], 0, grid_size - 1)
            y2 = clamp(y1 + neighbor[1], 0, grid_size - 1)
            point2 = x2 * grid_size + y2

            if point1 == point2:
                continue

            if [point1, point2] in total_pairs or [point2, point1] in total_pairs:
                continue
            else:
                total_pairs.append(list([point1, point2]))
                break

    return np.array(total_pairs)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--output_dirname', default="result", type=str)
    parser.add_argument('--inter_type', default="pixel", type=str, choices=["pixel"])
    parser.add_argument('--arch', default="our_alexnet_cifar10_normal_lr0.01_log1_da_flip_crop_best", type=str,
                        choices=[
                            # --- cifar 10 ---
                            "our_alexnet_cifar10_normal_lr0.01_log1_da_flip_crop_best",

                            "our_alexnet_cifar10_dp_pos0_entropy_deltav_baseline_0.5_0.0_lam1.0_1.0_grid16_lr0.01_log1_da_flip_crop_best",
                            "our_alexnet_cifar10_dp_pos0_deltav_baseline_0.7_0.3_lam1.0_1.0_grid16_lr0.01_log1_da_flip_crop_best",
                            "our_alexnet_cifar10_dp_pos0_entropy_deltav_baseline_1.0_0.7_lam1.0_1.0_grid16_lr0.01_log1_da_flip_crop_best",

                        ])
    parser.add_argument("--dataset", default="cifar10", type=str, choices=['cifar10'])
    parser.add_argument('--gpu_id', default=1, type=int, help="GPU ID")
    parser.add_argument('--chosen_class', default='random', type=str, choices=['random'])
    parser.add_argument('--seed', default=0, type=int, help="random seed")
    parser.add_argument('--grid_size', default=16, type=int,
                        help="partition the input image to grid_size * grid_size patches"
                             "each patch is considered as a player")

    args = parser.parse_args()

    set_args(args)
    os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_id)
    args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    seed_torch(args.seed)

    log_args_and_backup_code(args, __file__)

    sample_io_handler = SampleIoHandler(args)
    pair_io_handler = PairIoHandler(args)
    player_io_handler = PlayerIoHandler(args)

    image_list_selected = sample_io_handler.load()
    total_pairs = []
    model, dataloader = prepare(args, train=True)

    # sample (i,j) pairs and contexts S
    for index, (name, _, _) in enumerate(dataloader):
        print('\rPairs: \033[1;31m\033[5m%03d\033[0m/%03d' % (index + 1, len(image_list_selected)), end='')

        seed_torch(1000 * index + args.seed) # seed for sampling (i,j) pair
        pairs = gen_pairs(args.grid_size, args.pairs_number, args.stride)
        for ratio in args.ratios:
            m = int((args.grid_size ** 2 - 2) * ratio)  # m-order

            seed_torch(1000 * index + m + 1 + args.seed) # seed for sampling context S
            players_with_ratio = []
            for pair in pairs:
                point1, point2 = pair[0], pair[1]
                context = list(range(args.grid_size ** 2))
                context.remove(point1)
                context.remove(point2)

                curr_players = []
                for _ in range(args.samples_number_of_s):
                    curr_players.append(np.random.choice(context, m, replace=False)) # sample contexts of cardinality m

                players_with_ratio.append(curr_players)
            players_with_ratio = np.array(players_with_ratio)  # (pair_num, sample_num_of_s, m), contexts S of cardinality m for different (i,j) pairs
            print(players_with_ratio.shape)
            player_io_handler.save(round(ratio * 100), name[0], players_with_ratio)
        total_pairs.append(pairs)
    total_pairs = np.array(total_pairs)  # (num_imgs, num_pairs, 2), all (i,j) pairs
    print(total_pairs.shape)
    pair_io_handler.save(total_pairs)
    print('\nDone!')


#!/usr/bin/python3

#encoding=utf-8
import os
import serial
import time
import sys
import sqlite3
from struct import *
ser = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=2.0)
def read_pm_line(_port):
    rv = b''
    while True:
        ch1 = _port.read()
        if ch1 == b'\x42':
            ch2 = _port.read()
            if ch2 == b'\x4d':
                rv += ch1 + ch2
                rv += _port.read(38)
                return rv
def main():
    i = 1;
    while (i < 10):
        print("haha begin %dth round" % i)
        recv = read_pm_line(ser)
        tmp = recv[4:36]
        datas = unpack('>hhhhhhhhhhhhhhhh', tmp)
        if datas[4] >= 0 and datas[4] < 500 \
           and datas[3] >= 0 and datas[3] < 500 \
           and datas[5] >= 0 and datas[5] < 500 \
           and datas[12] >= 0 and datas[12] < 5000 \
           and datas[13] > 0 and datas[13] < 400 \
           and datas[14] > 0 and datas[14] < 800:
            break
        ser.flushInput()
        time.sleep(0.5)
        pm2p5 = datas[4]
        pm1p0 = datas[3]
        pm10 = datas[5]
        HCHO = datas[12]/1000.0
        temp = datas[13]/10.0
        humidity = datas[14]/10.0
        print('heihei %d %d %d %f %f %f' % (pm2p5, pm1p0, pm10, HCHO, temp, humidity));
        i = i + 1

    if i == 10:
        print("no result")
        return

    pm2p5 = datas[4]
    pm1p0 = datas[3]
    pm10 = datas[5]
    HCHO = datas[12]/1000.0
    temp = datas[13]/10.0
    humidity = datas[14]/10.0
    print('haha %d %d %d %f %f %f' % (pm2p5, pm1p0, pm10, HCHO, temp, humidity));

    conn = sqlite3.connect('/home/pi/plantower/plantower.db')
    c = conn.cursor()
    c.execute('''update sensor
            set value = %d
            where key = "pm2.5"''' % pm2p5)
    conn.commit()
    c.execute('''update sensor
            set value = %d
            where key = "pm1.0"''' % pm1p0)
    conn.commit()
    c.execute('''update sensor
            set value = %d
            where key = "pm10"''' % pm10)
    conn.commit()
    c.execute('''update sensor
            set value = %f
            where key = "HCHO"''' % HCHO)
    conn.commit()
    c.execute('''update sensor
            set value = %f
            where key = "temp"''' % temp)
    conn.commit()
    c.execute('''update sensor
            set value = %f
            where key = "humidity"''' % humidity)
    conn.commit()
    conn.close()

    ser.flushInput()
    time.sleep(0.1)
if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()


from django.http import Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
from backend.api import Character

def people(request, role=None):

    character_query = Character.Query(request.redis_conn, request.mission.name)
    character_ordering = list(request.redis_conn.lrange("character-ordering:%s" % request.mission.name, 0, -1))
    sort_characters = lambda l: sorted(
        list(l),
        key=lambda x: character_ordering.index(x.identifier) if x.identifier in character_ordering else 100
    )

    if role:
        people = [
            {
                'name': role,
                'members': sort_characters(character_query.role(role)),
            }
        ]
        more_people = False
    else:
        all_people = sort_characters(character_query)
        astronauts = list(character_query.role('astronaut'))
        ops = sort_characters(character_query.role('mission-ops-title'))
        people = [
            {
                'name': 'Flight Crew',
                'members': astronauts,
                'view': 'full'
            },
            {
                'name': 'Mission Control',
                'members': ops,
                'view': 'simple'
            }
        ]
        more_people = len(list(character_query.role('mission-ops')))

    # 404 if we have no content
    if 1 == len(people) and 0 == len(people[0]['members']):
        raise Http404( "No people were found" )
    return render_to_response(
        'people/people.html',
        {
            'role':   role,
            'people': people,
            'more_people': more_people,
        },
        context_instance = RequestContext(request),
    )


import os
from typing import Optional

import appdirs

APP_NAME = 'golem'
DEFAULT_DATA_DIR = 'default'

def get_local_datadir(
        name: Optional[str] = None,
        root_dir: Optional[str] = None,
        env_suffix: Optional[str] = None
) -> str:
    """ Helper function for datadir transition.

        It returns path to a data directory of given name in 'data' dir.
        Usage should be avoid at all costs. It is always better to ask for
        a dir the upper layer (like Client instance).
    """
    if not name:
        name = DEFAULT_DATA_DIR

    if not env_suffix:
        from golem.config.active import DATA_DIR  # type: ignore # noqa
        env_suffix = DATA_DIR

    if not root_dir:
        root_dir = os.path.join(appdirs.user_data_dir(APP_NAME), name)
    return os.path.join(root_dir, env_suffix)   # type: ignore # noqa


import io
import json
import os

import altair as alt
import numpy as np
import pandas as pd
import tensorflow as tf

# Altair uses a lot of method chaining, such as
# chart.mark_bar().encode(...).properties(...), so allowing backslash
# continuation to break this into separate lines makes the code more readable.
# pylint: disable=g-backslash-continuation

OLD_LIB_BASE_URL = 'https://cdn.jsdelivr.net/npm//'
NEW_LIB_BASE_URL = 'https://storage.googleapis.com/deepvariant/lib/vega/'

VEGA_VERSION = '5'
VEGA_LITE_VERSION = '3.4.0'
VEGA_EMBED_VERSION = '4'

# "pretty" genotype strings:
REF = 'Ref (0/0)'
HET = 'Het (0/x)'
HOM = 'Hom (x/x)'
UNCALLED = 'Uncalled (./.)'
HET_BOTH = 'Het - two variants (x/y)'

# Establish ordering of bases to keep it consistent
BASES = ['A', 'G', 'T', 'C']

BAR_COLOR_DEPTH = '#4a1486'
BAR_COLOR_QUAL = '#0c2c84'
BAR_COLOR_GQ = '#0c2c84'

BIALLELIC_SNP = 'Biallelic_SNP'
BIALLELIC_INSERTION = 'Biallelic_Insertion'
BIALLELIC_DELETION = 'Biallelic_Deletion'
BIALLELIC_MNP = 'Biallelic_MNP'
MULTIALLELIC_SNP = 'Multiallelic_SNP'
MULTIALLELIC_INSERTION = 'Multiallelic_Insertion'
MULTIALLELIC_DELETION = 'Multiallelic_Deletion'
MULTIALLELIC_COMPLEX = 'Multiallelic_Complex'
REFCALL = 'RefCall'

ordered_variant_type_labels = [
    BIALLELIC_INSERTION, BIALLELIC_DELETION, BIALLELIC_SNP, BIALLELIC_MNP,
    MULTIALLELIC_INSERTION, MULTIALLELIC_DELETION, MULTIALLELIC_SNP,
    MULTIALLELIC_COMPLEX, REFCALL
]

def _dict_to_dataframe(dictionary):
  """Turn a dict object into a dataframe of with label and value columns."""
  df = pd.DataFrame({
      'label': list(dictionary.keys()),
      'value': list(dictionary.values())
  })
  return df

def _prettify_genotype(genotype):
  """Get more human-readable display name and grouping for a given genotype."""
  pretty = genotype
  group = 'others'
  alleles = json.loads(genotype)
  if len(alleles) == 2:
    g1, g2 = sorted(alleles)
    if g1 == 0 and g2 == 0:
      pretty = REF
      group = 'main'
    elif g1 == -1 and g2 == -1:
      pretty = UNCALLED
    elif g1 == 0 and g2 > 0:
      pretty = HET
      group = 'main'
    elif g1 == g2:
      pretty = HOM
      group = 'main'
    else:
      pretty = HET_BOTH
  return pretty, group

def _build_type_chart(variant_type_counts):
  """Create a chart of the counts of each variant type."""
  width = 400
  height = 200
  title = 'Variant types'
  variant_type_data = _dict_to_dataframe(variant_type_counts)
  type_chart = _placeholder_for_empty_chart(
      'No entries in VCF', width=width, height=height, title=title)
  if not variant_type_data.empty:
    bars = alt.Chart(variant_type_data).mark_bar().encode(
        x=alt.X(
            'label',
            title=None,
            sort=ordered_variant_type_labels,
            axis=alt.Axis(labelAngle=-45)),
        y=alt.Y('value', axis=alt.Axis(title='Count', format='s')),
        tooltip=alt.Tooltip('value', format='.4s'),
        color=alt.Color(
            'label',
            legend=None,
            scale=alt.Scale(scheme='set1', domain=ordered_variant_type_labels)))
    labels = bars.mark_text(dy=-5).encode(text=alt.Text('value', format='.4s'))
    type_chart = (bars + labels).properties(
        width=width, height=height, title=title)
  return type_chart

def _build_qual_histogram(data):
  """Create the Quality(QUAL) histogram."""
  width = 200
  height = 200
  title = 'Quality score'
  qual_data = pd.DataFrame(data)
  qual_histogram = _placeholder_for_empty_chart(
      'No entries in VCF', width=width, height=height, title=title)
  if not qual_data.empty:
    # s = bin_start, e = bin_end, c = count
    domain = [min(0, data[0]['s']), max(150, data[-1]['e'])]
    qual_histogram = alt.Chart(qual_data).mark_bar(color=BAR_COLOR_QUAL) \
        .encode(
            x=alt.X('s', title='QUAL', scale=alt.Scale(domain=domain)),
            x2='e',
            y=alt.Y('c', title='Count', stack=True, axis=alt.Axis(format='s'))) \
        .properties(
            width=width, height=height,
            title=title) \
        .interactive(bind_y=False)
  return qual_histogram

def _build_gq_histogram(data):
  """Create the Genotype quality (GQ) histogram."""
  # gq = genotype quality, found at :GQ: in FORMAT column of VCF
  width = 200
  height = 200
  title = 'Genotype quality'
  gq_data = _integer_counts_to_histogram(data)
  gq_histogram = _placeholder_for_empty_chart(
      'No entries in VCF with GQ', width=width, height=height, title=title)
  if not gq_data.empty:
    # standardize x-axis limits across reports
    domain = [min(0, data[0][0]), max(150, data[-1][0])]
    # s = bin_start, e = bin_end, c = count
    gq_histogram = alt.Chart(gq_data).mark_bar(color=BAR_COLOR_GQ) \
        .encode(
            x=alt.X('s', title='GQ', scale=alt.Scale(domain=domain)),
            x2='e',
            y=alt.Y('c', title='Count', stack=True, axis=alt.Axis(format='s'))) \
        .properties(width=width, height=height, title=title) \
        .interactive(bind_y=False)
  return gq_histogram

def _build_vaf_histograms(histogram_json):
  """Create VAF histograms split by genotype."""
  guides = {REF: 0, HET: 0.5, HOM: 1}
  hist_data = pd.DataFrame()
  for key in histogram_json:
    g = pd.DataFrame(histogram_json[key])
    pretty, group = _prettify_genotype(key)
    g['GT'] = pretty  # pretty genotype name
    g['g'] = group  # main/other genotypes
    g['l'] = guides.get(pretty, None)  # vertical line as guide
    hist_data = hist_data.append(g)

  main_hist_data = hist_data[hist_data['g'] == 'main']
  other_hist_data = hist_data[hist_data['g'] == 'others']

  # Main genotypes (ref, het, hom-alt)
  # Histogram bars themselves
  # s = bin_start, e = bin_end, c = count
  bars = alt.Chart(main_hist_data).mark_bar().encode(
      x=alt.X('s', title='VAF'),
      x2='e',
      y=alt.Y('c', title='Count', stack=True, axis=alt.Axis(format='s')))
  # Vertical lines
  guides = alt.Chart(main_hist_data).mark_rule().encode(x='l')
  # Facet into 3 plots by genotype
  vaf_histograms = (bars + guides) \
    .properties(width=200, height=200) \
    .facet(column=alt.Column('GT',
                             title='Main genotypes',
                             sort=[REF, HET, HOM])) \
    .resolve_scale(y='independent')

  # Other genotypes (uncalled, het with two alt alleles)
  # s = bin_start, e = bin_end, c = count
  other_vaf_histograms = alt.Chart(other_hist_data) \
    .mark_bar().encode(
        x=alt.X('s', title='VAF'),
        x2='e',
        y=alt.Y('c', title='Count', stack=True, axis=alt.Axis(format='s')),
        column=alt.Column('GT', title='Other genotypes')) \
    .properties(width=150, height=150) \
    .resolve_scale(y='independent')
  return vaf_histograms, other_vaf_histograms

def _placeholder_for_empty_chart(text_to_display,
                                 width=100,
                                 height=100,
                                 title=''):
  chart = alt.Chart({'values': [{'placeholder': text_to_display}]}) \
      .mark_text(size=14).encode(text='placeholder:N') \
      .properties(width=width, height=height, title=title)
  return chart

def _build_base_change_chart(data):
  """Create the base change chart."""
  width = 100
  height = 200
  placeholder_width = (4 * width) + 80  # 4 charts, plus constant spacing
  title = 'Biallelic base changes from reference'
  base_change_data = pd.DataFrame(data, columns=['ref', 'alt', 'count'])

  base_change_chart = _placeholder_for_empty_chart(
      'No biallelic SNPs', width=placeholder_width, height=height, title=title)
  if not base_change_data.empty:
    bars = alt.Chart(base_change_data).mark_bar().encode(
        x=alt.X('alt', title='to alt'),
        y=alt.Y('count', title='Count', axis=alt.Axis(format='s')),
        color=alt.Color(
            'alt',
            legend=None,
            sort=BASES,
            scale=alt.Scale(scheme='category20', domain=BASES)),
        tooltip=alt.Tooltip('count', format='.4s'))
    labels = bars.mark_text(dy=-5, fontWeight='bold').encode(text='alt')

    base_change_chart = (bars + labels) \
        .properties(width=100, height=200) \
        .facet(column=alt.Column('ref',
                                 title=title,
                                 sort=BASES))

  return base_change_chart

def _integer_counts_to_histogram(num_count_pairs):
  """Turn paired numbers and their counts into data for a histogram.

  This centers the bars on the exact integer for clarity. For example, the bar
  for 3 is centered on 3 instead of being between 3 and 4 as in numpy's default
  histogram.

  Args:
    num_count_pairs: list of [num, count] pairs

  Returns:
    a pandas dataframe with num, count (bin count), s (bin start), e (bin end)
  """
  histogram_data = pd.DataFrame(num_count_pairs, columns=['num', 'c'])
  # For a proper histogram, use s and e to force each bar to cover
  # exactly one integer position:
  histogram_data['s'] = histogram_data['num'] - 0.5
  histogram_data['e'] = histogram_data['num'] + 0.5
  histogram_data = histogram_data.drop(columns=['num'])
  return histogram_data

def _build_indel_size_chart(data):
  """Create the indel size chart."""
  width = 400
  height = 100
  placeholder_height = (2 * height) + 20  # 2 charts, plus spacing
  title = 'Biallelic indel size distribution'
  ordered_labels = ['Insertion', 'Deletion']
  indel_size_data = _integer_counts_to_histogram(data)
  indel_size_data['type'] = np.where(indel_size_data['s'] > 0, 'Insertion',
                                     'Deletion')

  indel_size_chart = _placeholder_for_empty_chart(
      'No biallelic indels',
      width=width,
      height=placeholder_height,
      title=title)

  if not indel_size_data.empty:
    indels_linear = alt.Chart(indel_size_data).mark_bar().encode(
        x=alt.X('s', title='size'),
        x2='e',
        y=alt.Y('c', title='Count', axis=alt.Axis(format='s')),
        color=alt.Color('type', sort=ordered_labels,
                        scale=alt.Scale(scheme='set1'))) \
      .properties(width=400, height=100,
                  title=title) \
      .interactive(bind_y=False)

    indel_log = alt.Chart(indel_size_data).mark_bar().encode(
        x=alt.X('s', title='size'),
        x2='e',
        y=alt.Y(
            'c',
            title='Count',
            axis=alt.Axis(format='s'),
            scale=alt.Scale(type='log', base=10)),
        color=alt.Color('type', sort=ordered_labels,
                        scale=alt.Scale(scheme='set1'))) \
      .properties(width=400, height=100) \
      .interactive(bind_y=False)

    indel_size_chart = alt.vconcat(indels_linear, indel_log) \
        .resolve_scale(color='shared')
  return indel_size_chart

def _build_depth_histogram(data):
  """Build histogram with depth (DP)."""
  width = 200
  height = 200
  title = 'Depth'
  depth_data = _integer_counts_to_histogram(data)
  depth_histogram = _placeholder_for_empty_chart(
      'No entries in VCF with DP', width=width, height=height, title=title)
  if not depth_data.empty:
    # s = bin_start, e = bin_end, c = count
    depth_histogram = alt.Chart(depth_data).mark_bar(color=BAR_COLOR_DEPTH) \
        .encode(x=alt.X('s', title='Depth'),
                x2='e',
                y=alt.Y('c', title='Count', stack=True, axis=alt.Axis(format='s'))) \
        .properties(width=width, height=height, title=title) \
        .interactive(bind_y=False)
  return depth_histogram

def _build_tt_chart(titv_counts):
  """Built chart showing counts of transitions and transversions."""
  width = 150
  height = 200

  ti = titv_counts['Transition']
  tv = titv_counts['Transversion']
  # Show TiTv ratio with fallback to avoid division by 0
  titv_ratio = '%.2f' % (float(ti) / tv) if tv > 0 else '%d / 0' % (ti)
  title = 'Biallelic Ti/Tv ratio: %s' % (titv_ratio)

  tt_chart = _placeholder_for_empty_chart(
      'No biallelic SNPs', width=width, height=height, title=title)
  tt_labels = ['Transition', 'Transversion']
  if sum([titv_counts[k] for k in titv_counts]) > 0:
    tt_data = _dict_to_dataframe(titv_counts)
    bars = alt.Chart(tt_data).mark_bar().encode(
        x=alt.X(
            'label', sort=tt_labels, axis=alt.Axis(title=None, labelAngle=0)),
        y=alt.Y('value', axis=alt.Axis(title='Count', format='s')),
        tooltip=alt.Tooltip('value', format='.4s'),
        color=alt.Color(
            'label',
            legend=None,
            sort=tt_labels,
            scale=alt.Scale(scheme='teals', domain=tt_labels)))
    labels = bars.mark_text(dy=-5).encode(text=alt.Text('value', format='.4s'))
    tt_chart = (bars + labels).properties(
        title=title, width=width, height=height)
  return tt_chart

def _build_all_charts(vis_data, sample_name=''):
  """Build all charts and combine into a single interface."""

  # Row 1
  type_chart = _build_type_chart(vis_data['variant_type_counts'])
  depth_chart = _build_depth_histogram(vis_data['depth_histogram'])
  qual_histogram = _build_qual_histogram(vis_data['qual_histogram'])
  gq_histogram = _build_gq_histogram(vis_data['gq_histogram'])
  row1 = alt.hconcat(type_chart, depth_chart, qual_histogram, gq_histogram) \
      .resolve_scale(color='independent')

  # Row 2
  vaf_histograms, other_vaf_histograms = _build_vaf_histograms(
      vis_data['vaf_histograms_by_genotype'])
  row2 = alt.hconcat(vaf_histograms, other_vaf_histograms)

  # Row 3
  base_change_chart = _build_base_change_chart(vis_data['base_changes'])
  indel_size_chart = _build_indel_size_chart(vis_data['indel_sizes'])
  tt_chart = _build_tt_chart(vis_data['titv_counts'])
  row3 = alt.hconcat(base_change_chart, tt_chart, indel_size_chart) \
      .resolve_scale(color='independent')

  # Putting it all together
  all_charts = alt.vconcat(row1, row2, row3)

  all_charts = all_charts.properties(title=sample_name, spacing=70) \
      .configure_header(labelFontSize=16, titleFontSize=20) \
      .configure_title(fontSize=20)
  return all_charts

def _altair_chart_to_html(altair_chart, download_filename):
  """Write to a temporary string stand-in for the file to replace import URLs.

  Args:
    altair_chart: a chart object made by Altair.
    download_filename: string filename base for when users export images.

  Returns:
    HTML in string format.
  """
  temp_writer = io.StringIO()
  altair_chart.save(
      temp_writer,
      format='html',
      embed_options={'downloadFileName': download_filename},
      vegalite_version=VEGA_LITE_VERSION,
      vega_version=VEGA_VERSION,
      vegaembed_version=VEGA_EMBED_VERSION)
  temp_html_string = temp_writer.getvalue()
  html_with_new_cdn = temp_html_string.replace(OLD_LIB_BASE_URL,
                                               NEW_LIB_BASE_URL)
  return html_with_new_cdn

def _save_html(basename, all_charts):
  """Save Altair chart as an HTML file."""
  output_path = basename + '.visual_report.html'
  image_download_filename = os.path.basename(basename) + '.visual_report'
  html_string = _altair_chart_to_html(
      altair_chart=all_charts, download_filename=image_download_filename)

  with tf.io.gfile.GFile(output_path, 'w') as writer:
    writer.write(html_string)

def create_visual_report(basename, vis_data, sample_name=''):
  """Build visual report with several charts."""
  all_charts = _build_all_charts(vis_data, sample_name)
  _save_html(basename, all_charts)


import random
import pandas as pd
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Load csv file
nba = pd.read_csv("DIct.csv")

@app.get("/")
async def root():
    # Generate 5 random numbers between 0 and len(nba)
    random_numbers = random.sample(range(0, len(nba)), 5)
    # Create a list of dictionaries containing the randomly selected rows
    word_list = []
    for i in random_numbers:
        row_dict = nba.loc[[i]].to_dict(orient='records')[0]
        word_list.append(row_dict)
    return {"words": word_list}

@app.post("/word")
async def get_word_meaning(input_word: str):
    # Load dictionary DataFrame
    columns = ['english_word', 'malayalam_definition']
    df = pd.read_csv("DIct.csv", header=None, names=columns, skiprows=1)

    # Convert input word to lowercase
    input_word = input_word.strip().lower()

    # Find all rows that contain the input word or sub-word
    result = df.loc[df['english_word'].str.lower().str.contains(input_word)]

    # If rows are found, return the meanings
    if not result.empty:
        # Filter rows that contain the exact input word
        exact_matches = result[result['english_word'].str.lower() == input_word]
        if not exact_matches.empty:
            meanings = exact_matches['malayalam_definition'].tolist()
            return {"word": input_word.capitalize(), "meaning": meanings}

        # Filter rows that contain the input word as a sub-word
        subword_matches = result[result['english_word'].str.lower() != input_word]
        if not subword_matches.empty:
            word_list = subword_matches['english_word'].tolist()[:5]
            meaning_list = subword_matches['malayalam_definition'].tolist()[:5]
            word_meaning_list = [{"word": word, "meaning": meaning} for word, meaning in zip(word_list, meaning_list)]
            return {"words": word_meaning_list}
    else:
        return {"message": "Word not found in dictionary"}


from datasets import load_dataset
import torch
from tqdm import tqdm
from torch.optim import AdamW
from torch.utils.data import DataLoader
from utils import plot_loss_curve, plot_lr_curve
from datasets import load_dataset
from transformers import GPT2Tokenizer, GPT2LMHeadModel
import torch.cuda.amp as amp
from transformers import DataCollatorForLanguageModeling
import argparse
import os
import numpy as np
import loralib as lora
from peft import LoraConfig, get_peft_model, PeftModel, PeftModelForCausalLM
# If the dataset is gated/private, make sure you have run huggingface-cli login

torch.cuda.empty_cache()

PATH = os.environ['HF_CHECKPOINT']

parser = argparse.ArgumentParser()
parser.add_argument('--resume', default='', help='path to latest checkpoint pytorch style')
parser.add_argument('--export', default='LoRA/checkpoint.pt', help='path to save checkpoint pytorch style')
parser.add_argument('--lora', default='LoRA/lora', help='path to save lora checkpoint from model.save_pretrained method')
parser.add_argument('--save', default='LoRA/upload', help='path to the folder to upload to huggingface')
parser.add_argument('--epoch', default=5, help='number of epochs to train')
parser.add_argument('--batch_size', default=2, help='batch size')
parser.add_argument('--lr', default=5e-5, help='learning rate')
parser.add_argument('--num_workers', default=4, help='number of workers')
parser.add_argument('--test_size', default=0.05, help='test size')
parser.add_argument('--samples', default=int(1e5), help='number of samples')
parser.add_argument('--merge', default=True, help='merge model')
parser.add_argument('--test_every', default=int(5e3), help='do inference every n samples')
parser.add_argument('--infer', default='What is a computer?', help='message to send when inferring')
args = parser.parse_args()

def get_conversation(dataset):
    formatted_conversations = []
    for i in range(len(dataset['conversation']) - 1):
        if dataset['conversation'][i]['content'] != '' and dataset['conversation'][i + 1]['content'] != '':
            formatted_conversations.append(f"<{dataset['conversation'][i]['role'].upper()}> {dataset['conversation'][i]['content']}")
    text = ' '.join(formatted_conversations)
    return {'text': text}

def tokenize_function(datasets):
    return tokenizer(datasets['text'],padding=True,truncation=True, return_tensors="pt",max_length=1024)

def tokenized_dataset():
    dataset = load_dataset('lmsys/lmsys-chat-1m',split='train')
    dataset = dataset.filter(lambda x: x["language"] == 'English')
    if args.samples != 0:
        dataset = dataset.select(range(args.samples))
    dataset = dataset.map(get_conversation,remove_columns=["conversation"])
    dataset = dataset.map(tokenize_function, batched=True,batch_size=10000,num_proc=4)
    dataset.set_format(type='torch', columns=['input_ids','attention_mask'])
    return dataset

def infer(inp):
    inp = "<USER>" + inp + "<ASSISTANT>"
    token = tokenizer(inp, return_tensors="pt")
    X = token["input_ids"].to(device)
    a = token["attention_mask"].to(device)
    output = model.generate(input_ids=X,attention_mask = a,pad_token_id=tokenizer.eos_token_id,max_new_tokens=128,repetition_penalty=2.0)
    output = tokenizer.decode(output[0])
    return output

def save():
    """
    Saves the LoRA model to the specified path and prints the path to the console. If the `merge` flag is set to True,
    also saves the merged model to the specified path and prints the path to the console.
    """
    model.save_pretrained(os.path.join(PATH,args.lora))
    print("Lora model saved to {}".format(os.path.join(PATH,args.lora)))
    ###### save model ######
    if args.merge == True:
        merge_model = model.merge_and_unload()
        merge_model.save_pretrained(os.path.join(PATH,args.save))
        # save the merged model and the lora model
        # model_to_merge = PeftModel.from_pretrained(GPT2LMHeadModel.from_pretrained("gpt2").to(device),os.path.join(PATH,args.lora))
        # merged_model = model_to_merge.merge_and_unload()
        # merged_model.save_pretrained(os.path.join(PATH,args.save))
        print("Model saved to {} waiting to upload".format(os.path.join(PATH,args.save)))

def train():
    """
    Trains the GPT-2 model using the specified training and validation dataloaders, and saves the best model checkpoint based on validation loss.

    Returns:
        None
    """
    history = []
    best_loss = np.inf
    start_epoch = 1
    if args.resume:
        if os.path.isfile(os.path.join(PATH,args.resume)):
            print("===> loading checkpoints '{}'".format(os.path.join(PATH,args.resume)))
            print("===> Peft model loaded from '{}'".format(os.path.join(PATH,args.lora)))
            checkpoint = torch.load(os.path.join(PATH,args.resume))
            model.load_state_dict(checkpoint['model'],strict=False)
            optimizer.load_state_dict(checkpoint['optimizer'])
            start_epoch = checkpoint['epoch']
            history = checkpoint['history']
            best_loss = checkpoint['best_loss']
            # model.from_pretrained(GPT2LMHeadModel.from_pretrained("gpt2").to(device),os.path.join(PATH,args.lora))
            # print("checkpoint loaded: epoch = {}, best loss = {}".format(start_epoch,best_loss))
        else:
            print("===> no models found at '{}'".format(args.resume))

    #train
    scaler = amp.GradScaler()
    for epoch in range(start_epoch,args.epoch + 1):
        print("epoch: ", epoch)
        result = {'train_loss': [], 'valid_loss': [], 'lrs': [], 'best_loss': best_loss}
        model.train()
        train_loss = 0
        for i in enumerate(tqdm(train_dataloader)):
            idx = i[0]
            batch = i[1]
            token = batch['input_ids'].to(device)
            mask = batch['attention_mask'].to(device)
            optimizer.zero_grad()
            with amp.autocast():
                loss = model(token, attention_mask=mask, labels=token).loss
                train_loss += loss.item()
            scaler.scale(loss).backward()
            scaler.step(optimizer)
            scaler.update()
            # args.test_every//args.batch_size
            if idx%(args.test_every) == 0:
                print(infer(args.infer))

        train_loss /= len(train_dataloader)

        model.eval()
        with torch.no_grad():
            valid_loss = 0
            for batch in tqdm(val_dataloader):
                token = batch['input_ids'].to(device)
                mask = batch['attention_mask'].to(device)
                loss = model(token, attention_mask=mask, labels=token).loss
                valid_loss += loss.item()
            valid_loss /= len(val_dataloader)

        if valid_loss < best_loss:
            best_loss = valid_loss
            model_out_path = os.path.join(PATH, args.export)
            state = {"epoch": epoch,
                     "model": model.state_dict(),
                    "optimizer": optimizer.state_dict(),
                    "best_loss": best_loss,
                    "history": history}
            torch.save(state, model_out_path)

            print("===> Checkpoint saved to {}".format(model_out_path))
            model.save_pretrained(os.path.join(PATH,args.lora))
            print("Lora model saved to {}".format(os.path.join(PATH,args.lora)))

        result['train_loss'].append(train_loss)
        result['valid_loss'].append(valid_loss)
        result['lrs'].append(optimizer.param_groups[0]['lr'])
        result['best_loss'] = best_loss
        history.append(result)

        print('Train Loss: {:.4f}'.format(train_loss))
        print('Val Loss: {:.4f}'.format(valid_loss))
        print(infer("Hello, how are you?"))
        plot_loss_curve(history)
        plot_lr_curve(history)

    print("Training done! model saved to {} with best loss {:.4f}".format(os.path.join(PATH,args.lora),best_loss))

if __name__ == '__main__':
    # set device
    device = "cuda:1" if torch.cuda.is_available() else "cpu"
    # set tokenizer
    tokenizer = GPT2Tokenizer.from_pretrained('gpt2',bos_token='<BOS>', eos_token='<EOS>', pad_token='<PAD>')
    tokenizer.add_tokens(["<ASSISTANT>","<USER>"])
    # collator
    data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False)

    dataset = tokenized_dataset()
    dataset = dataset.train_test_split(test_size=args.test_size, shuffle=True)
    train_ds = dataset['train']
    test_ds = dataset['test']
    # DataLoader
    train_dataloader = DataLoader(train_ds, shuffle=True, batch_size=args.batch_size, num_workers=args.num_workers, collate_fn=data_collator)
    val_dataloader = DataLoader(test_ds, batch_size=args.batch_size,shuffle=True, num_workers=args.num_workers, collate_fn=data_collator)

    # model
    model = GPT2LMHeadModel.from_pretrained("gpt2").to(device)
    model.resize_token_embeddings(len(tokenizer))
    model = model.to(device)

    for param in model.parameters():
        param.requires_grad = False  # freeze the model - train adapters later
        if param.ndim == 1:
            # cast the small parameters (e.g. layernorm) to fp32 for stability
            param.data = param.data.to(torch.float32)

    # add adapters
    config = LoraConfig(
        r=16, #attention heads
        lora_alpha=32, #alpha scaling
        fan_in_fan_out = True,
        lora_dropout=0.05,
        bias="none",
        task_type="CAUSAL_LM" # set this for CLM or Seq2Seq
    )

    model = PeftModelForCausalLM(model, config)
    model.print_trainable_parameters()

    # optimizer
    optimizer = AdamW(model.parameters(), lr=args.lr)
    train()
    save()


from django.shortcuts import render, HttpResponse, redirect
from django.contrib import messages
from .models import Show
import datetime

def index(request):
    context = {
        "allshows": Show.objects.all().values()
    }
    #print(context)
    return render(request, "watch/index.html", context)

def showpage(request, showid):
    context = {
        "theshow": Show.objects.get(id=showid)
    }
    return render(request, "watch/read.html", context)

#Show editor
def editpage(request, editid):
    context = {
        "editshow": Show.objects.get(id=editid)
    }
    return render(request, "watch/edit.html", context)

#Show edits
def editedshow(request):
    if request.method == "POST":
        idshow = request.POST["showid"]
        thisshow = Show.objects.get(id=idshow)

        errors = Show.objects.edit_validator(request.POST)
        if len(errors) > 0:
            for key, value in errors.items():
                messages.error(request, value)
            return redirect("/shows/"+idshow+"/edit")
        else:
            ti = request.POST["title"]
            ne = request.POST["network"]
            de = request.POST["description"]
            da = request.POST["date"]
            thisshow.title = ti
            thisshow.network = ne
            thisshow.desc = de
            if not len(da) < 10:
                thisshow.release = da
            thisshow.save()
            return redirect("/shows/"+idshow)

#Show creator
def createpage(request):
    return render(request, "watch/new.html")

#New show
def newshow(request):
    if request.method == "POST":
        errors = Show.objects.add_validator(request.POST)
        if len(errors) > 0:
            for key, value in errors.items():
                messages.error(request, value)
            return redirect("/shows/create")
        else:
            ti = request.POST["title"]
            ne = request.POST["network"]
            da = request.POST["date"]
            de = request.POST["description"]
            Show.objects.create(title=ti, network=ne, release=da, desc=de)
            return redirect("/shows")

def deleteshow(request, deleteid):
    Show.objects.get(id=deleteid).delete()
    return redirect("/shows")


from base.read_Data import read_data
from base.init_driver import init_driver
from pages.page_obj import Page_obj
import pytest, allure
from base.public_var import screenshot_path
import os, time

def get_test_data():
    data_list = []
    datas = read_data("phone_login.yaml").get("phone_login")
    print(datas)
    for k in datas.keys():
        data_list.append((k, datas.get(k).get('phone_no'),
                          datas.get(k).get("vervify_code"),
                          datas.get(k).get("expected")))
    return data_list

class Test_phone_login:
    def setup_class(self):
        self.driver = init_driver()
        self.page_obj = Page_obj(self.driver)

    def teardown_class(self):
        self.driver.quit()

    @allure.step(title="点击手机号登录")
    @pytest.fixture()
    def click_login_method(self):
        self.page_obj.re_login_method_page().click_phone_login()

    @allure.step(title="输入手机号登录信息")
    @pytest.mark.usefixtures("click_login_method")
    @pytest.mark.parametrize("case_no,phone_no,vervify_code,expected", get_test_data())
    def test_input_user_info(self, case_no, phone_no, vervify_code, expected):
        self.page_obj.re_phone_login_page().input_user_info(phone_no, vervify_code)
        time.sleep(2)
        assert expected == self.page_obj.re_phone_login_page().find_search_button()
        file_path = os.path.join(screenshot_path,
                                 self.__class__.__name__ + "_%s.png" % (time.strftime("%y_%d_%d__%H_%M_%S")))
        # print("file_path:",file_path)
        self.page_obj.re_phone_login_page().screen_shot(file_path)


from collections import Counter
from data import users_interests

def get_popular_interests():
    return Counter(
        interest for user_interests in users_interests for interest in user_interests
    ).most_common()

def most_popular_new_interests(user_interests, max_results=5):
    suggestions = [
        (interest, frequency)
        for interest, frequency in get_popular_interests()
        if interest not in user_interests
    ]

    return suggestions[:max_results]

def main():
    print(most_popular_new_interests(users_interests[0]))
    print(most_popular_new_interests(users_interests[3]))

if __name__ == "__main__":
    main()


import subprocess, colorama, requests, base64, os

os.system('@title AnyDesk Ip Grabber | by zReeaper1337 && cls')

from colorama import Fore, Style

colorama.init()

anydesk_pids = []
anydesk_address = {}
ip_addr = []
old_port = 0
old_ip = ''

# banner
print("Made by zReeaper1337")
print("GitHub https://github.com/zReeaper1337")
print("Discord zReeaper#1337")

while 1:
	try:
		if str(subprocess.check_output("tasklist")).count('AnyDesk') <= 3:
			pass
		else:
			for line in str(subprocess.check_output("tasklist")).replace('b"', '"').replace('\\r', '').replace('\\n', '\n').split('\n'):
				if 'AnyDesk' in line:
					try:
						anydesk_pids.append(line.split('.exe')[1].split()[0].replace(' ', ''))

					except Exception as e:
						pass
			nstats_output_lines = str(subprocess.check_output('netstat -p TCP -n -a -o')).replace('b"', '"').replace('\\r', '').replace('\\n', '\n').split('\n')
			for pid in anydesk_pids:
				for line in nstats_output_lines:
					if pid in line and not 'LISTENING' in line:
						try:
							parts = line.split()
							protocol = parts[0]
							local_addr = parts[1]
							remote_addr = parts[2].split(':')[0]
							remote_port = parts[2].split(':')[1]
							anydesk_address[remote_addr] = int(remote_port)
						except Exception as e:
							print(e)
			for ip, port in anydesk_address.items():
				if int(port) > old_port and not '169.254.' in ip:
					old_port = int(port)
					old_ip = ip
			remote_ip = old_ip
			remote_port = old_port
			print(f'{Fore.GREEN} connection established!')
			try:
				json_data = requests.get(f'http://extreme-ip-lookup.com/json/' + remote_ip).json()
				print(Style.BRIGHT + '''
{12}╔════════════════════════════════════════════╗
{12}║ {13}IP{14}:{13} {0}{6} {12}║
{12}║ {13}Country{14}:{13} {1}{7} {12}║
{12}║ {13}City{14}:{13} {2}{8} {12}║
{12}║ {13}ISP{14}:{13} {3}{9} {12}║
{12}║ {13}Lat{14}:{13} {4}{10} {12}║
{12}║ {13}Lon{14}:{13} {5}{11} {12}║
{12}╚════════════════════════════════════════════╝
				'''.format(json_data['query'], json_data['country'], json_data['city'], json_data['isp'], json_data['lat'], json_data['lon'], (' ' * (38 - int(len(json_data['query'])))), (' ' * (33 - int(len(json_data['country'])))), (' ' * (36 - int(len(json_data['city'])))), (' ' * (37 - int(len(json_data['isp'])))), (' ' * (37 - int(len(json_data['lat'])))), (' ' * (37 - int(len(json_data['lon'])))),Fore.RED, Fore.WHITE, Fore.BLACK))
			except:
				print('hum.')
			input('press \'enter\' to continue...')
			exit()
	except KeyboardInterrupt:
		print('ctrl+c'); exit()


from flask import (
    Blueprint, g, render_template, abort, request, flash, redirect, url_for, jsonify
)

from justifii.database import db_session
from justifii.blueprints.auth import login_required
from justifii.models import Rationale, Text

bp = Blueprint('text', __name__, url_prefix='/text')

def get_text(text_id):
    text = Text.query.get(text_id)

    if text is None:
        abort(404, "Text #{} doesn't exist.".format(text_id))

    return text

@bp.route('/')
def index():
    return render_template('text/index.html', texts=Text.query.all())

@bp.route('/_get_texts', methods=('GET',))
def get_texts():
    data = [{
        'id': text.id,
        'fpath': text.fpath,
        'label': text.label.name,
        'show_url': url_for('text.show', text_id=text.id),
        'justify_url': url_for('text.justify', text_id=text.id),
    } for text in Text.query.all()]

    return jsonify(data=data)

@bp.route('/<int:text_id>')
def show(text_id):
    text = get_text(text_id)

    return render_template('text/show.html', text=text)

@bp.route('/<int:text_id>/justify', methods=('GET', 'POST'))
@login_required
def justify(text_id):
    text = get_text(text_id)

    existing_rationale = Rationale.query.filter_by(user_id=g.user.id, text_id=text_id).first()
    new = existing_rationale is None
    rationale = existing_rationale or Rationale()

    if new:
        rationale.user = g.user
        rationale.text = text

    if request.method == 'POST':
        tokens = request.form.getlist('tokens[]')
        error = None

        if not tokens:
            error = "No tokens selected"

        if error is not None:
            flash(error, 'danger')
        else:
            rationale.tokens = [int(token) for token in tokens]
            if new:
                db_session.add(rationale)
            db_session.commit()
            return redirect(url_for('text.show', text_id=text_id))

    return render_template('text/justify.html', text=text, rationale=rationale)