# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------

from termios import BS0
import numpy as np
import torch
import torch.nn as nn
from mmcv.cnn import xavier_init
from mmcv.cnn.bricks.transformer import build_transformer_layer_sequence
from mmcv.runner.base_module import BaseModule

from mmdet.models.utils.builder import TRANSFORMER
from torch.nn.init import normal_
from projects.mmdet3d_plugin.models.utils.visual import save_tensor
from mmcv.runner.base_module import BaseModule
from torchvision.transforms.functional import rotate

from ...bevformer.modules.temporal_self_attention import TemporalSelfAttention
from ...bevformer.modules.spatial_cross_attention import MSDeformableAttention3D
from ...bevformer.modules.decoder import CustomMSDeformableAttention
from projects.mmdet3d_plugin.models.utils.bricks import run_time

from mmcv.runner import force_fp32, auto_fp16
import pdb

@TRANSFORMER.register_module()
class OccupancyTransformer_normal(BaseModule):
    """Implements the Detr3D transformer.
    Args:
        as_two_stage (bool): Generate query from encoder features.
            Default: False.
        num_feature_levels (int): Number of feature maps from FPN:
            Default: 4.
        two_stage_num_proposals (int): Number of proposals when set
            `as_two_stage` as True. Default: 300.
    """

    def __init__(self,
                 num_feature_levels=4,
                 num_cams=6,
                 two_stage_num_proposals=300,
                 encoder=None,
                 decoder=None,
                 embed_dims=256,
                 use_cams_embeds=True,
                 rotate_center=[100, 100],
                 use_decoder=True,
                 **kwargs):
        super(OccupancyTransformer_normal, self).__init__(**kwargs)

        self.encoder = build_transformer_layer_sequence(encoder)
        if use_decoder:
            self.decoder = build_transformer_layer_sequence(decoder)
        self.embed_dims = embed_dims
        self.num_feature_levels = num_feature_levels
        self.num_cams = num_cams
        self.fp16_enabled = False

        self.use_cams_embeds = use_cams_embeds

        self.two_stage_num_proposals = two_stage_num_proposals
        self.init_layers()
        self.rotate_center = rotate_center

    def init_layers(self):
        """Initialize layers of the Detr3DTransformer."""
        self.level_embeds = nn.Parameter(torch.Tensor(
            self.num_feature_levels, self.embed_dims))
        self.cams_embeds = nn.Parameter(
            torch.Tensor(self.num_cams, self.embed_dims))

    def init_weights(self):
        """Initialize the transformer weights."""
        for p in self.parameters():
            if p.dim() > 1:
                nn.init.xavier_uniform_(p)
        for m in self.modules():
            if isinstance(m, MSDeformableAttention3D) or isinstance(m, TemporalSelfAttention) \
                    or isinstance(m, CustomMSDeformableAttention):
                try:
                    m.init_weight()
                except AttributeError:
                    m.init_weights()
        normal_(self.level_embeds)
        normal_(self.cams_embeds)

    @auto_fp16(apply_to=('mlvl_feats', 'bev_queries', 'bev_pos'))
    def get_bev_features(
            self,
            mlvl_feats,
            bev_queries,
            bev_h,
            bev_w,
            grid_length=[0.512, 0.512],
            bev_pos=None,
            use_3d_conv=False,
            bev_z=None,
            direct_proj=False,
            fpn_index=None,
            **kwargs):
        """
        obtain bev features.
        """

        bs = mlvl_feats[0].size(0)
        bev_queries = bev_queries.unsqueeze(1).repeat(1, bs, 1)
        bev_pos = bev_pos.flatten(2).permute(2, 0, 1)

        feat_flatten = []
        spatial_shapes = []
        for lvl, feat in enumerate(mlvl_feats):
            bs, num_cam, c, h, w = feat.shape
            spatial_shape = (h, w)
            feat = feat.flatten(3).permute(1, 0, 3, 2) #num_cam, bs, hw, c
            if not direct_proj:
                if self.use_cams_embeds:
                    feat = feat + self.cams_embeds[:, None, None, :].to(feat.dtype)
                feat = feat + self.level_embeds[None, None, lvl:lvl + 1, :].to(feat.dtype)
            spatial_shapes.append(spatial_shape)
            feat_flatten.append(feat)

        feat_flatten = torch.cat(feat_flatten, 2)
        spatial_shapes = torch.as_tensor(spatial_shapes, dtype=torch.long, device=bev_pos.device)
        level_start_index = torch.cat((spatial_shapes.new_zeros((1,)), spatial_shapes.prod(1).cumsum(0)[:-1]))
        feat_flatten = feat_flatten.permute(0, 2, 1, 3)  # (num_cam, H*W, bs, embed_dims)

        bev_embed = self.encoder(
                bev_queries,
                feat_flatten,
                feat_flatten,
                bev_h=bev_h,
                bev_w=bev_w,
                bev_pos=bev_pos,
                spatial_shapes=spatial_shapes,
                level_start_index=level_start_index,
                use_3d_conv=use_3d_conv,
                bev_z=bev_z,
                direct_proj=direct_proj,
                **kwargs
            )

        return bev_embed


"""upgrade table for s3

Revision ID: 7d02396887fb
Revises: 397763fb176d
Create Date: 2023-04-26 08:37:28.381065

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '7d02396887fb'
down_revision = '397763fb176d'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('images', schema=None) as batch_op:
        batch_op.add_column(sa.Column('original_filename', sa.String(length=100), nullable=True))
        batch_op.add_column(sa.Column('filename', sa.String(length=100), nullable=True))
        batch_op.add_column(sa.Column('bucket', sa.String(length=100), nullable=True))
        batch_op.add_column(sa.Column('region', sa.String(length=100), nullable=True))
        batch_op.drop_constraint('images_img_key', type_='unique')
        batch_op.drop_column('name')
        batch_op.drop_column('mimetype')
        batch_op.drop_column('img')

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('images', schema=None) as batch_op:
        batch_op.add_column(sa.Column('img', sa.VARCHAR(), autoincrement=False, nullable=False))
        batch_op.add_column(sa.Column('mimetype', sa.VARCHAR(), autoincrement=False, nullable=False))
        batch_op.add_column(sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False))
        batch_op.create_unique_constraint('images_img_key', ['img'])
        batch_op.drop_column('region')
        batch_op.drop_column('bucket')
        batch_op.drop_column('filename')
        batch_op.drop_column('original_filename')

    # ### end Alembic commands ###


import calendar
import datetime

import httpx
from loguru import logger

from lnbits.core.services import fee_reserve, get_wallet

async def check_balance(data) -> bool:
    # check if we can pay the invoice before we create the actual swap on boltz
    amount_msat = data.amount * 1000
    fee_reserve_msat = fee_reserve(amount_msat)
    wallet = await get_wallet(data.wallet)
    assert wallet
    if wallet.balance_msat - fee_reserve_msat < amount_msat:
        return False
    return True

def get_timestamp():
    date = datetime.datetime.utcnow()
    return calendar.timegm(date.utctimetuple())

def req_wrap(funcname, *args, **kwargs):
    try:
        try:
            func = getattr(httpx, funcname)
        except AttributeError:
            logger.error('httpx function not found "%s"' % funcname)
        else:
            res = func(*args, timeout=30, **kwargs)
        res.raise_for_status()
        return res
    except httpx.RequestError as exc:
        msg = f"Unreachable: {exc.request.url!r}."
        logger.error(msg)
        raise
    except httpx.HTTPStatusError as exc:
        msg = f"HTTP Status Error: {exc.response.status_code} while requesting {exc.request.url!r}."
        logger.error(msg)
        logger.error(exc.response.json()["error"])
        raise


__author__ = 'Avinash'

import pymysql

# Open database connection
db = pymysql.connect("localhost", "root", "", "python_tutorials")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Prepare SQL query to INSERT a record into the database.
sql = "SELECT * FROM PERSON"
try:
    # Execute the SQL command
    cursor.execute(sql)
    # Fetch all the rows in a list of lists.
    results = cursor.fetchall()
    for row in results:
        fname = row[1]
        lname = row[2]
        age = row[3]
        sex = row[4]
        # Now print fetched result
        print("fname = %s, lname = %s, age = %d, sex = %s" %
              (fname, lname, age, sex, ))
except pymysql.Error:
    print("Error: unable to fetch data")

# disconnect from server
db.close()


"""taskapplication URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from task.views import IndexView,LoginView,RegisterView,AddTaskView, TaskDetailView,TaskListView,TaskDeleteView,RegistrationView,LoginnView
from task import views

urlpatterns = [
    # path('admin/', admin.site.urls),
    # path("home/",IndexView.as_view()),
    # path("login/",LoginView.as_view()),
    # path("register/",RegisterView.as_view()),
    path("task/add",views.AddTaskView.as_view(),name="todo-add"),
    path("todos/all",views.TaskListView.as_view(),name="todo-all"),
    path("todos/<int:id>",views.TaskDetailView.as_view(),name="todo-detail"),
    path("todos/<int:id>/remove",views.TaskDeleteView.as_view(),name="todo-delete"),
    path("accounts/register",views.RegistrationView.as_view(),name="register"),
    path("",views.LoginnView.as_view(),name="signin"),
    path("accounts/logout",views.signout_view,name="signout"),
    path("todos/changes/<int:id>",views.TaskUpdateView.as_view(),name="todo-update")
]


from pyPS4Controller.controller import Controller
from paho.mqtt import client as mqtt_client
import random
import logging
import json

broker = 'test.mosquitto.org'
port = 1883
topic = "python/mqtt/ds4msg"
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = ''
password = ''

logging.basicConfig(
    format='[ds4MQTT][%(asctime)s]\t[%(levelname)s]\t%(message)s',
    level=logging.DEBUG
)

class MyController(Controller):

    def __init__(self, **kwargs):
        Controller.__init__(self, **kwargs)

    def on_x_press(self):
        msg = "Custom x press message"
        logging.debug(msg)
        publish(client, msg)

def on_message(client, userdata, message):
    logging.info("Message received: "  + str(message.payload))

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            logging.info("Connected to MQTT Broker!")
        else:
            logging.warn("Failed to connect, return code %d\n", rc)
    # Set Connecting Client ID
    client = mqtt_client.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(broker, port)
    return client

def publish(client, msg="<empty message>"):
    result = client.publish(topic, msg)
    status = result[0]
    if status == 0:
        logging.info(f"Sent `{msg}` to topic `{topic}`")
    else:
        logging.warn(f"Failed to send message to topic {topic}")

controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)

client = connect_mqtt()
def run():
    client.loop_start()
    client.subscribe(topic)
    publish(client)
    controller.listen()

if __name__ == '__main__':
    run()


from django import forms
from django.forms import ModelForm, DateInput, DateField
import datetime
from .models import Report
from leaflet.forms.widgets import LeafletWidget

class newReportForm(forms.ModelForm):
        class Meta:
                model = Report
                exclude = ['user']
                fields = [
                'user',
                'asunto',
                'imagen',
                'descripcion',
                'geom',
                ]

                labels = {
                'asunto':'Asunto',
                'imagen':'Imagen',
                'descripcion':'Descripción',
                'geom':'Seleccione la ubicación',
        }
                widgets = {
                'asunto': forms.TextInput(attrs={'class':'form-input','placeholder': 'Escriba el asunto'}),
                'imagen': forms.FileInput(attrs={'id':'foto','class':'form-input'}),
                'descripcion': forms.Textarea(attrs={'class':'form-textarea','placeholder': 'Descripción del problema'}),
                'geom': LeafletWidget(attrs={'geom_type': 'POINT'})
}


import random
from colorama import Fore
n=1
class batsman:
    def __init__(self,name,nick,hand,score,balls_faced,status):
        self.name=name
        self.nick=nick
        self.hand=hand
        self.score=score
        self.balls_faced=balls_faced
        self.status=status

    def bat(self):
        list1=[1,1,1,1,2,1,1,1,1,1,2,2,2,2,3,4,6,0]
        n = random.choice(list1)
        self.score=self.score+n
        self.balls_faced=self.balls_faced+1
        #print(f"You got {n} runs")
        return n

class bowler:
    def __int__(self,name,hand,wickets,balls_bowled):
        self.name=name
        self.hand=hand
        self.wickets=wickets
        self.balls_bowled=balls_bowled

sanath=batsman ("Sanath Jayasooriya","sanath","left",0,0,"Not Out")
kalu=batsman ("Romesh Kaluwitharana","kalu","right",0,0,"Not Out")
gura=batsman ("Asanka Gurusinghe","gura","left",0,0,"Not Out")
ara=batsman ("Aravinda de Silva","ara","right",0,0,"Not Out")
hashan=batsman ("Hashan Thilakarathne","hashan","left",0,0,"Not Out")
vass=batsman ("Chaminda Vass","vass","left",0,0,"Not Out")

pool_of_players=[sanath,kalu,gura,ara,hashan,vass]

def add_player():
    pool_of_players2=[]
    name= input("Please Enter Name of the Player :")
    nick_name=input("Please Enter Nick Name of the Player: ")
    nick_name2=nick_name
    hand=input("Left hand or Right hand: ")

    for yy in pool_of_players:
        pool_of_players2.append(yy.nick)
    if nick_name not in pool_of_players2:
        globals()[nick_name] = batsman(name,nick_name2,hand,0,0,"Not Out")
        pool_of_players.append(globals()[nick_name])
    else:
        print("The Player Already in the Pool")

i=0
no_of_batters=6
list_nick=[]

def bat_order():
    bat_order = []
    list_nick = []
    for za in pool_of_players:
        list_nick.append(za.nick)
    print(Fore.WHITE,f"the available players are\n  {list_nick}")
    for batting_position in range (0,no_of_batters):

        batter_name=input(f"Enter the name of Batting Position {batting_position+1} :")

        while batter_name not in list_nick:
            print("Your player is not in the pool or already allocated")
            print(f"the available players are\n  {list_nick}")
            batter_name = input(f"Enter the name of Batting Position {batting_position + 1} :")
        for z in pool_of_players:
            if z.nick==batter_name:
                bat_order.append(z)
                pool_of_players.remove(z)
        list_nick=[]
        for za in pool_of_players:
            list_nick.append(za.nick)
        print(f"The available players are {list_nick}")

    return bat_order

def playmatch (no_of_balls,bat_order):
    i=0
    for x in range(0, no_of_balls):
        score=bat_order[i].bat()
        if score==0:
            bat_order[i].status="Out"
            i=i+1
        if i==len(bat_order):
            print ("-----------------------ALL OUT-------------------")
            print(f"No of balls faced {x+1}")
            break

tot=0
def print_scorecard():
    print("--------------------------------------------------------------------------------------------------")
    print("--------------------------------------------------------------------------------------------------")
    print(Fore.GREEN, f"{batt_order[0].name}              {batt_order[0].score}     ({batt_order[0].balls_faced})   {batt_order[0].status}")
    print(Fore.GREEN, f"{batt_order[1].name}              {batt_order[1].score}     ({batt_order[1].balls_faced})   {batt_order[1].status}")
    print(Fore.GREEN, f"{batt_order[2].name}              {batt_order[2].score}     ({batt_order[2].balls_faced})  {batt_order[2].status}")
    print(Fore.GREEN, f"{batt_order[3].name}              {batt_order[3].score}     ({batt_order[3].balls_faced})  {batt_order[3].status}")
    print(Fore.GREEN, f"{batt_order[4].name}              {batt_order[4].score}     ({batt_order[4].balls_faced})   {batt_order[4].status}")
    print(Fore.GREEN, f"{batt_order[5].name}              {batt_order[5].score}     ({batt_order[5].balls_faced})   {batt_order[5].status}")
    #print("--------------------------------------------------------------------------------------------------")
    print("--------------------------------------------------------------------------------------------------")
    print(Fore.BLUE,f"-----Team Total is {batt_order[0].score+ batt_order[1].score+batt_order[2].score+batt_order[3].score+batt_order[4].score+batt_order[5].score}--------")
    print("--------------------------------------------------------------------------------------------------")
    #print("--------------------------------------------------------------------------------------------------")
def print_pool(pool_of_players):
    for ii in pool_of_players:
        print(Fore.BLUE,ii.name)
def print_bat_order(pool_of_players):
    jj=1
    for ii in pool_of_players:
        print(f"{jj}.{ii.name}")
        jj=jj+1

#------------------------------------------Game Starts--------------------------------------------

print("We are going to Play a lovely game of cricket.......")
print(f"These are the current players in the squad....")
print_pool(pool_of_players)

user_in=input("Do you want to add any player:")
while user_in=="y":
    add_player()
    print(f"These are the current players in the squad....")
    print_pool(pool_of_players)
    user_in = input("Do you want to add any player:")

batt_order=bat_order()
print("This is the batting Order")
print_bat_order(batt_order)
no_of_balls1 = input("Please enter the no of Balls per match: ")
while no_of_balls1.isnumeric()==False:
    print("Entry not Valid, Enter a Valid number")
    no_of_balls1 = input("Please enter the no of Balls per match: ")

no_of_balls=int(no_of_balls1)
playmatch(no_of_balls,batt_order)
print_scorecard()


import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

# Load the SentimentIntensityAnalyzer from NLTK
sia = SentimentIntensityAnalyzer()

# Here we define some keywords that might be used in swatting calls
swatting_keywords = ['hostage', 'bomb', 'shoot', 'kill', 'gun']

def analyze_call(call_text):
    # Apply sentiment analysis to the call text
    sentiment = sia.polarity_scores(call_text)

    # Check if any swatting keywords are in the call text
    swatting_terms_present = any(term in call_text for term in swatting_keywords)

    # Here's a very simple heuristic to classify a call as potential swatting:
    # If the call's sentiment is overwhelmingly negative, and any swatting keyword is present,
    # we classify it as potential swatting.
    if sentiment['compound'] < -0.5 and swatting_terms_present:
        return True

    return False

# Test the function with a call
call_text = "There's a man with a gun, he's threatening to kill everyone."
is_swatting = analyze_call(call_text)

print(f"Potential swatting call: {is_swatting}")


import pygame
pygame.init()
pygame.mixer.init()

def getImage(file, alpha):
    if alpha:
        return pygame.image.load(file).convert_alpha()
    else:
        return pygame.image.load(file).convert()

def getSound(file):
    return pygame.mixer.Sound(file)

# set mouse cursor
pygame.mouse.set_cursor(*pygame.cursors.tri_left)

FPS = 100                # frames per second, the general speed of the program
WINDOW_WIDTH = 1920      # size of window's width in pixels
WINDOW_HEIGHT = 1080     # size of windows' height in pixels
BOX_SIZE_X = 124         # size of box width in pixels
BOX_SIZE_Y = 124         # size of box height in pixels
GAP_SIZE_X = 41          # size of gap between boxes in pixels
GAP_SIZE_Y = 14
BOARD_COLS = 7           # number of columns of icons // 7
BOARD_ROWS = 6           # number of rows of icons // 6
X_MARGIN = (WINDOW_WIDTH - (BOARD_COLS * (BOX_SIZE_X + GAP_SIZE_X))) // 2
Y_MARGIN = (WINDOW_HEIGHT - (BOARD_ROWS * (BOX_SIZE_Y + GAP_SIZE_Y))) // 2
distance = 50
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

#        R    G    B
GRAY = (100, 100, 100)
NAVYBLUE = (60, 60, 100)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
CYAN = (0, 255, 255)
BLACK = (0, 0, 0)

# images
Board_first = getImage('Assets/Board_First.png', False)
Board_first_small = getImage('Assets/Board_First_small.png', False)  # better sometimes
Board_last = getImage('Assets/Board_Last_smaller.png', True)
Asimon_Red = getImage('Assets/Good_Red.png', True)
Asimon_Yel = getImage('Assets/Good_Yellow.png', True)
Inv_Red = getImage('Assets/Good_Red_Inv.png', True)
Inv_Yel = getImage('Assets/Good_Yellow_Inv.png', True)
Open_Pressed = getImage('Assets/Open.png', False)
Open = getImage('Assets/Open_Pressed.png', False)
EasterEgg = getImage('Assets/hamar.png', False)
Pause_image = getImage('Assets/Connect-Four-Pause.png', False)
Fix_Box = getImage('Assets/Fix_box.png', False)
Glow_Strong = getImage('Assets/Glow.png', True)
Glow_weak = getImage('Assets/lessGlow.png', True)
win_yel = getImage('Assets/yellow finish.png', True)
win_red = getImage('Assets/red finish.png', True)
tie_finish = getImage('Assets/tie finish.png', True)
exit_door = getImage('Assets/exitdoor.png', True)
Asimon_Red = pygame.transform.scale(Asimon_Red, (140, 140))
Asimon_Yel = pygame.transform.scale(Asimon_Yel, (140, 140))
Inv_Red = pygame.transform.scale(Inv_Red, (140, 140))
Inv_Yel = pygame.transform.scale(Inv_Yel, (140, 140))
Glow_Strong = pygame.transform.scale(Glow_Strong, (140, 135))
Glow_weak = pygame.transform.scale(Glow_weak, (140, 135))
exit_door = pygame.transform.scale(exit_door, (140, 140))

# sounds
Mouse_Hover = getSound('Assets/MenuMouse.wav')
falling_sound = getSound('Assets/falling sound.wav')
falling_sound_finish = getSound('Assets/falling sound finish2.wav')
Click_sound = getSound('Assets/Menu Click.wav')
win_sound = getSound('Assets/Winning_Sound.wav')
win_sound_end = getSound('Assets/Winning_sound_end.wav')
falling_sound_finish2 = getSound('Assets/falling sound finish.wav')
tie_sound = getSound('Assets/tie_sound.wav')


"""
    @Difficulty : M 2
    @Status     : AC
    @Time       : 2020/11/15 10:41
    @Author     : Chen Runwen
"""
from typing import List

class Solution:
    def minOperations(self, nums: List[int], x: int) -> int:
        n = len(nums)

        # cum[i]: 前i个数的和
        left = [0]
        for i in range(n):
            if left[-1] + nums[i] > x:
                break
            left.append(left[-1] + nums[i])
        if len(left) == n + 1:
            if left[-1] < x:
                return -1
            if left[-1] == x:
                return n

        j = n
        right = 0
        res = -1
        while right < x:
            while left[i] + right < x:
                j -= 1
                right += nums[j]
            while i > 0 and left[i] + right > x:
                i -= 1

            if left[i] + right == x:
                res = i + n - j if res == -1 else min(res, i + n - j)
                i -= 1
        return res

if __name__ == '__main__':
    nums = [3, 2, 20, 1, 1, 3]
    x = 10
    print(Solution().minOperations(nums, x))

contests/20201115/5602. 将 x 减到 0 的最小操作数.py


#! /usr/bin/env python3

import numpy as np
import pandas as pd
import os
import pickle
from oncotree import OncoTree, OncoNode
from anytree import RenderTree

#-------------------------------------------#
#----- GENERATIVE MODEL SPECIFICATIONS -----#
#-------------------------------------------#
nodes_dict = {}
### Linear Structure ###
root = OncoNode(genes=[], f=1)
c0 = OncoNode(parent=root, genes=[0,1,2,3], f=0.9)
c1 = OncoNode(parent=c0, genes=[4,5,6,7], f=0.7)
c2 = OncoNode(parent=c1, genes=[8,9,10,11], f=0.5)
c3 = OncoNode(parent=c2, genes=[12,13,14,15], f=0.3)
ps = OncoNode(genes=[16,17,18,19], f=0)
nodes_dict['linear'] = [root, c0, c1, c2, c3, ps]
### Tree Structure ###
root = OncoNode(genes=[], f=1)
c0 = OncoNode(parent=root, genes=[0,1,2], f=0.9)
c1 = OncoNode(parent=c0, genes=[3,4,5], f=0.7)
c2 = OncoNode(parent=c0, genes=[6], f=0.6)
c3 = OncoNode(parent=c0, genes=[7,8], f=0.3)
c1_1 = OncoNode(parent=c1, genes=[9,10,11], f=0.8)
c1_2 = OncoNode(parent=c1, genes=[12,13], f=0.5)
c2_1 = OncoNode(parent=c2, genes=[14,15], f=0.3)
ps = OncoNode(genes=[16,17,18,19], f=0)
nodes_dict['tree'] = [root, c0, c1, c2, c3, c1_1, c1_2, c2_1, ps]
### Mixture Structure ###
root = OncoNode(genes=[], f=1)
c0 = OncoNode(parent=root, genes=[0,1,2], f=0.8)
c1 = OncoNode(parent=root, genes=[3,4,5], f=0.9)
c2 = OncoNode(parent=root, genes=[6], f=0.5)
c1_1 = OncoNode(parent=c1, genes=[7,8], f=0.3)
c1_2 = OncoNode(parent=c1, genes=[9,10,11], f=0.7)
c2_1 = OncoNode(parent=c2, genes=[12,13], f=0.8)
c2_1_1 = OncoNode(parent=c2_1, genes=[14,15], f=0.4)
ps = OncoNode(genes=[16,17,18,19], f=0)
nodes_dict['mixture'] = [root, c0, c1, c2, c1_1, c1_2, c2_1, c2_1_1, ps]
####----------------####------------------####

#-------------------------------------------#
#-------- Constructing the datasets --------#
#-------------------------------------------#

n_list = [50, 100, 200, 500]
e_list = [0.001, 0.01, 0.05, 0.1]
n_dataset_per_setting = 10

if 'synthetic' not in os.listdir('data'):
    os.mkdir('data/synthetic')

for mode in ['linear', 'tree', 'mixture']:
    np.random.seed(1)
    if mode not in os.listdir('data/synthetic'):
        os.mkdir('data/synthetic/{}'.format(mode))

    for e in e_list:
        pkl_file = 'data/synthetic/{}/{}.pkl'.format(mode, e)
        gen_progmo = OncoTree(nodes_dict[mode], pfp=e, pfn=e)
        gene_names = ['g{}'.format(i) for i in gen_progmo.genes]
        with open(pkl_file, 'wb') as f:
            pickle.dump(gen_progmo, f)
        for n in n_list:
            for idx in range(n_dataset_per_setting):
                df_file = 'data/synthetic/{}/{}-{}-{}.csv'.format(mode, n, e, idx)
                dataset, _ =gen_progmo.draw_sample(n)
                tumor_ids = ['tumor{}'.format(i) for i in range(n)]
                df = pd.DataFrame(data=np.array(dataset, dtype=int), columns=gene_names, index=tumor_ids)
                ## Comment lines for csv file:
                comment = '# Synthetic dataset number %i  \n' % idx
                comment += '# Number of tumors: %i \n' % n
                comment += '# Number of mutations: %i \n' % gen_progmo.n_genes
                comment += '# driver tree: \n'
                for pre, fill, node in RenderTree(gen_progmo.root):
                    comment += '# %s%s\n' % (pre, node.genes)
                comment += '# set of passengers: \n'
                for pre, fill, node in RenderTree(gen_progmo.ps):
                    comment += '# %s%s\n' % (pre, node.genes)
                comment += '# p(FP): %.4f \n' % e
                comment += '# p(FN): %.4f \n' % e
                comment += '# ------------------------------------ #\n'
                with open(df_file, 'w') as f:
                    f.write(comment)
                ## Done with comments
                with open(df_file, 'a') as f:
                    df.to_csv(f, sep=',')


import os
import osmium
import numpy as np
import alphashape
from shapely.geometry import MultiPolygon
import yaml
import json
import re
from polycache import PolyCache
from web_server import start_web_server
import concurrent.futures
from gen_postal_codes import gen_postal_codes

polycache = PolyCache("data/polycache.json",'coordinates')
pointcache = PolyCache("data/pointcache.json", 'points')

def key(postal_code):
    return f"{postal_code['start']}-{postal_code['end']}"

class PostalCodeHandler(osmium.SimpleHandler):
    def __init__(self, postal_code_ranges):
        super(PostalCodeHandler, self).__init__()
        self.postal_code_ranges = postal_code_ranges

    def node(self, n):
        if postal_code_tag in n.tags and self._is_valid_postal_code(n.tags[postal_code_tag]):
            postal_code = int(n.tags[postal_code_tag][:4])  # Extract first four digits of postal code
            for pcr in self.postal_code_ranges:
                if int(pcr['start']) <= postal_code <= int(pcr['end']):
                    if 'points' not in pcr:
                        pcr['points'] = []
                    pcr['points'].append((n.location.lon, n.location.lat))
                    break

    def _is_valid_postal_code(self, postal_code):
        pattern = r"[0-9]{4} ?[A-Za-z]{2}"
        return re.match(pattern, postal_code) is not None

def extract_points(xml_file, postal_code_ranges):
    cached, uncached = pointcache.get_cached(postal_code_ranges)
    print(f"{len(cached)} cached pointclouds reused, {len(uncached)} uncached")

    if len(uncached) > 0:
        handler = PostalCodeHandler(uncached)
        handler.apply_file(xml_file)

        pointcache.update_all(handler.postal_code_ranges)

        return handler.postal_code_ranges + cached

    return cached

def calculate_boundary(cloud):
        points_array = np.array(cloud.pop('points',[]))
        alpha = 55
        hull = alphashape.alphashape(points_array, alpha)
        while type(hull) is MultiPolygon and alpha > 10:
            alpha = alpha - 2
            hull = alphashape.alphashape(points_array, alpha)
        print(f"Ended with alpha at {alpha}")
        cloud['coordinates'] = [np.array(list(hull.exterior.coords)).tolist()]

        return cloud

def calculate_outer_boundaries(point_clouds):

    if not point_clouds:
        raise ValueError("No point clouds found for the given postal code ranges.")

    point_clouds = [c for c in point_clouds if 'points' in c]
    point_clouds = [c for c in point_clouds if len(c['points']) > 0]

    with concurrent.futures.ProcessPoolExecutor() as executor:
        futures = [executor.submit(calculate_boundary, i) for i in point_clouds]

        results = []

        i = 0
        for future in futures:
            i = i + 1
            result = future.result()
            results.append(result)
            polycache.update(result)
            print(f"Progress: {i} / {len(futures)}")

    return results

def generate_geojson(polygons, geojson_file):
    features = []
    for item in polygons:
        feature = {
            "type": "Feature",
            "properties": {
                "start": item['start'],
                "end": item['end'],
                "tooltip": f"{item['start']} - {item['end']}
{item['name']}
{item['other_therapist']}",
                "colour": item['colour']
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": item['coordinates']
            }
        }
        features.append(feature)

    data = {
        "type": "FeatureCollection",
        "features": features
    }

    with open(geojson_file, "w") as file:
        json.dump(data, file)

if __name__ == '__main__':
    with open('config.yaml') as file:
        config = yaml.load(file, Loader=yaml.FullLoader)

    gen_postal_codes()
    with open('data/postal_codes.yaml') as file:
        postal_code_ranges = yaml.load(file, Loader=yaml.FullLoader)['postal_codes']

    xml_file = config['xml_file']
    postal_code_tag = config['postal_code_tag']
    point_clouds_file = "data/point_clouds.json"
    geojson_file = "polygons.geojson"
    web_server_port = 8800

    try:
        if not os.path.isfile(geojson_file):

            cached, uncached = polycache.get_cached(postal_code_ranges)
            print(f"{len(cached)} cached polygons reused, {len(uncached)} uncached")
            for pc in uncached:
                print(polycache.key(pc))

            print(" ## Extracting points")
            point_clouds = extract_points(xml_file, uncached)

            print(" ## Calculating boundaries")
            polygons = calculate_outer_boundaries(point_clouds)

            print(" ## Converting data to geojson format")
            generated = cached + polygons
            generate_geojson(generated, geojson_file)

            print(f" ## GeoJSON file saved as {geojson_file}")

        start_web_server(web_server_port)
    except ValueError as e:
        print(e)


import xml.etree.cElementTree as ET

def creator():
	classes = "acacia", "trompa", "balela"
	root = ET.Element("roi_coordinates")

	cls_list = []
	#create a field for each class's roi_coordinates
	for cls in classes:
		curr_class = ET.SubElement(root, cls)

		# xmlET.SubElement(root, cls)
		cls_list.append(curr_class)
	return (root, cls_list)

def manolo(root, classes):
	inteiro = 69
	coordXY = ET.SubElement(classes[0], ("_coordinates_"+ str(inteiro)))

	ET.SubElement(coordXY, "X").text = "800"
	ET.SubElement(coordXY, "Y").text = "600"

	#ET.SubElement(classes[0], point1) = coordXY

	#ET.SubElement(classes[0], "field1", name="blah").text = "some value1"
	ET.SubElement(classes[1], "field2", name="asdfasd").text = "some vlaue2"

	tree = ET.ElementTree(root)
	tree.write("filename.xml")

rt, classes = creator()

manolo(rt, classes)


from dash.orgs.models import Org

from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

class Command(BaseCommand):
    help = "Pulls all contacts, groups and fields from the backend for the specified org"

    def add_arguments(self, parser):
        parser.add_argument("org_id", type=int, metavar="ORG", help="The org to pull contacts for")

    def handle(self, *args, **options):
        org_id = int(options["org_id"])
        try:
            org = Org.objects.get(pk=org_id)
        except Org.DoesNotExist:
            raise CommandError("No such org with id %d" % org_id)

        prompt = """You have requested to pull all contacts, groups and fields for org '%s' (#%d). Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: """ % (
            org.name,
            org.pk,
        )

        if input(prompt).lower() != "yes":
            self.stdout.write("Operation cancelled")
            return

        def progress_callback(num_synced):
            self.stdout.write(" > Synced %d contacts..." % num_synced)

        backend = org.get_backend()

        created, updated, deleted, ignored = backend.pull_fields(org)

        self.stdout.write(
            "Finished field pull (%d created, %d updated, %d deleted, %d ignored)"
            % (created, updated, deleted, ignored)
        )

        created, updated, deleted, ignored = backend.pull_groups(org)

        self.stdout.write(
            "Finished group pull (%d created, %d updated, %d deleted, %d ignored)"
            % (created, updated, deleted, ignored)
        )

        created, updated, deleted, ignored = backend.pull_contacts(org, None, timezone.now(), progress_callback)

        self.stdout.write(
            "Finished contact pull (%d created, %d updated, %d deleted, %d ignored)"
            % (created, updated, deleted, ignored)
        )


#Reto # 7 - Julio Cesar Palacio Ojeda - Grupo 23 - 01/06/2022
#Aplicación para registrar los datos de los estudiantes que se postulan al departamento de
#prácticas académicas de una de las universidades extranjeras.
#Datos de Entrada: archivo de texto: 'codigos_estudiantes.txt' enviada por el departamento de prácticas académica de la universidad
#Profesor la prueba unitaria por favor verla en el archivo adjunto pueba_unitaria tambien adjunto los pantallazos de la prueba en pdf.
#Se importa la libreria networkx como nx
import networkx as nx
import funciones as fun
#Se importa la libreria pyplot de matplotlib como plt
import matplotlib.pyplot as plt
import datetime

#Inicializamos lista de postulados (leyendo el archivo de texto: 'codigos_estudiantes.txt' enviada por el departamento de prácticas académica de la universidad)
lista_postu = []

for i in fun.leer_archivos():
  lista_postu.append(i)

#Inicializar variables
cola = []
inscritos = {}

#función asignación de turno:
#1.Valida que el documento del estudiante este en la lista enviada por el departamento de prácticas

def asig_turno(lista_postu, codigo):
    if codigo in lista_postu:
        cola.append(codigo)

    return cola

#función llama al estudiante respectivo de acuerdo con su posición en la cola y
#registra los siguientes datos en un diccionario:
##Identificación
##Nombre
##Edad
##Inscrito a prácticas (Ingeniería civil, Ingeniería de sistemas, Ingeniería electrónica)
##Fecha y hora del registro

def inscrito_dpa(cola):
    identif = cola.pop(0)
    print('Estudiante con numero de identificación #: ', identif)
    nombre = str(input('Ingrese su nombre: '))
    edad = int(input('Ingrese su edad: '))
    print('Ingrese la facultad donde va a relizar la practica:\n1. Ingenieria Civil\n2. Ingenieria de Sistemas\n3. Ingenieria Electronica')
    facultad = int(input())
    hora_registro = datetime.datetime.now()

    inscritos[identif] = (nombre,edad, facultad, hora_registro)

    return inscritos

#funcion que cuenta y da el % de los inscritos por tipo de programa y genera los siguientes graficos:
#1.Total inscritos a practicas
#2.Total inscritos por tipo de programa
#3.% de inscritos por facultad

def inscritos_porprograma(inscritos):
    contar1=0
    contar2=0
    contar3=0

    for i in inscritos:
        if inscritos[i][2] == 1:
            contar1 += 1
        if inscritos[i][2] == 2:
            contar2 += 1
        if inscritos[i][2] == 3:
            contar3 += 1

    print('El total de inscritos en la facultad de Ingenieria Civil son: ', contar1)
    print('El total de inscritos en la facultad de Ingenieria de Sistemas son: ', contar2)
    print('El total de inscritos en la facultad de Ingenieria Electronica son: ', contar3)

    total = contar1+contar2+contar3

    print('El % de inscritos en la facultad de Ingenieria Civil es: ', round(contar1/total, 2))
    print('El % de inscritos en la facultad de Ingenieria de Sistemas es: ', round(contar2/total, 2))
    print('El % de inscritos en la facultad de Ingenieria Electronica es: ', round(contar3/total, 2))

    return ''

#Ciclo para asignar un turno al estudiante de manera consecutiva de acuerdo con el orden
#de llegada
#Valida que el estudiante no se haya inscrito ya
n = int(input('Ingrese la cantidad de estudiantes que llegaron: '))
i=0
while i<n:
    cod = int(input('Ingrese el número de identificación del estudiante que se postula: '))
    if cod not in lista_postu:
        print('El estudiante no se encuentra postulado\n')
    elif cod not in inscritos.keys():
        asig_turno(lista_postu, cod)
        inscrito_dpa(cola)
    else:
        print('Estudiante ya se encuentra inscrito\n')

    i += 1

print('El total de inscritos a practica son: ', len(inscritos))
print(inscritos_porprograma(inscritos))

#creaciación de la grafica
#Se crea un grafo vacio
G=nx.Graph()

#Se crean los nodos:
#Nodo DPA
G.add_node("DPA")
#Se crean los nodos de los facultades
G.add_nodes_from(["Ingenieria Sistemas","Ingenieria Civil","Ingenieria Electronica"])
#Se crean los enlaces de las facultades al DPA
G.add_edge("DPA","Ingenieria Sistemas")
G.add_edge("DPA","Ingenieria Civil")
G.add_edge("DPA","Ingenieria Electronica")

#Se dibuja el grafo
nx.draw(G, with_labels=True)
#Se muestra en pantalla
plt.show()


import errno
import socket
import io
from typing import Optional

NONBLOCKING_EXCEPTION_ERROR_NUMBERS = {BlockingIOError: errno.EWOULDBLOCK}
SERVER_CLOSED_CONNECTION_ERROR = "Connection closed by server."
NONBLOCKING_EXCEPTIONS = tuple(NONBLOCKING_EXCEPTION_ERROR_NUMBERS.keys())

SYM_CRLF = b"\n"
SENTINEL = object()

class SocketBuffer:
    def __init__(
            self, socket: socket.socket, socket_read_size: int, socket_timeout: float | None = None
    ):
        self._sock = socket
        self.socket_read_size = socket_read_size
        self.socket_timeout = socket_timeout
        self._buffer = io.BytesIO()

    def unread_bytes(self) -> int:
        """
        Remaining unread length of buffer
        """
        pos = self._buffer.tell()
        end = self._buffer.seek(0, io.SEEK_END)
        self._buffer.seek(pos)
        return end - pos

    def _read_from_socket(
            self,
            length: Optional[int] = None,
            timeout: float | object = SENTINEL,
            raise_on_timeout: Optional[bool] = True,
    ) -> bool:
        sock = self._sock
        socket_read_size = self.socket_read_size
        marker = 0
        custom_timeout = timeout is not SENTINEL

        buf = self._buffer
        current_pos = buf.tell()
        buf.seek(0, io.SEEK_END)
        if custom_timeout:
            sock.settimeout(timeout)
        try:
            while True:
                data = self._sock.recv(socket_read_size)
                # an empty string indicates the server shutdown the socket
                if isinstance(data, bytes) and len(data) == 0:
                    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
                buf.write(data)
                data_length = len(data)
                marker += data_length

                if length is not None and length > marker:
                    continue
                return True
        except socket.timeout:
            if raise_on_timeout:
                raise TimeoutError("Timeout reading from socket")
            return False
        except NONBLOCKING_EXCEPTIONS as ex:
            # if we're in nonblocking mode and the recv raises a
            # blocking error, simply return False indicating that
            # there's no data to be read. otherwise raise the
            # original exception.
            allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS.get(ex.__class__, -1)
            if not raise_on_timeout and ex.errno == allowed:
                return False
            raise ConnectionError(f"Error while reading from socket: {ex.args}")
        finally:
            buf.seek(current_pos)
            if custom_timeout:
                sock.settimeout(self.socket_timeout)

    def can_read(self, timeout: float) -> bool:
        return bool(self.unread_bytes()) or self._read_from_socket(
            timeout=timeout, raise_on_timeout=False
        )

    def read(self, length: int) -> bytes:
        length = length + 2  # make sure to read the \r\n terminator
        # BufferIO will return less than requested if buffer is short
        data = self._buffer.read(length)
        missing = length - len(data)
        if missing:
            # fill up the buffer and read the remainder
            self._read_from_socket(missing)
            data += self._buffer.read(missing)
        return data[:-2]

    def readline(self) -> bytes:
        buf = self._buffer
        data = buf.readline()
        while not data.endswith(SYM_CRLF):
            # there's more data in the socket that we need
            self._read_from_socket()
            data += buf.readline()

        return data[:-1]

    def get_pos(self) -> int:
        """
        Get current read position
        """
        return self._buffer.tell()

    def rewind(self, pos: int) -> None:
        """
        Rewind the buffer to a specific position, to re-start reading
        """
        self._buffer.seek(pos)

    def purge(self) -> None:
        """
        After a successful read, purge the read part of buffer
        """
        unread = self.unread_bytes()

        # Only if we have read all of the buffer do we truncate, to
        # reduce the amount of memory thrashing.  This heuristic
        # can be changed or removed later.
        if unread > 0:
            return

        if unread > 0:
            # move unread data to the front
            view = self._buffer.getbuffer()
            view[:unread] = view[-unread:]
        self._buffer.truncate(unread)
        self._buffer.seek(0)

    def close(self) -> None:
        try:
            self._buffer.close()
        except Exception:
            # issue #633 suggests the purge/close somehow raised a
            # BadFileDescriptor error. Perhaps the client ran out of
            # memory or something else? It's probably OK to ignore
            # any error being raised from purge/close since we're
            # removing the reference to the instance below.
            pass
        self._buffer = None
        self._sock = None

class ResponseParser:
    """Base class for sync-based resp parsing"""

    def __init__(self, socket_read_size):
        self.socket_read_size = socket_read_size
        self.encoder = None
        self._sock = None
        self._buffer = None

    def __del__(self):
        try:
            self.on_disconnect()
        except Exception:
            pass

    def on_connect(self, connection):
        "Called when the socket connects"
        self._sock = connection._sock
        self._buffer = SocketBuffer(
            self._sock, self.socket_read_size, connection._socket_timeout
        )
        self.encoder = connection._encoder

    def on_disconnect(self):
        "Called when the socket disconnects"
        self._sock = None
        if self._buffer is not None:
            self._buffer.close()
            self._buffer = None
        self.encoder = None

    def can_read(self, timeout):
        return self._buffer and self._buffer.can_read(timeout)

    def read_response(self, disable_decoding=False):
        pos = self._buffer.get_pos() if self._buffer else None
        try:
            result = self._read_response(disable_decoding=disable_decoding)
        except BaseException:
            if self._buffer:
                self._buffer.rewind(pos)
            raise
        else:
            self._buffer.purge()
            return result

    def _read_response(self, disable_decoding=False):
        raw = self._buffer.readline()
        if not raw:
            raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)

        if not disable_decoding:
            raw = self.encoder.decode(raw)
        return raw


# -*- coding: utf-8  -*-
"""
Module name: ZhihuSpider;
Author: Duguce;
Description: 抓取知乎某一问题下的所有回答（回答数量不超过800左右）
"""

import datetime
import time, json, re
from time import sleep
import pandas as pd
import config
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

DEBUG = False

def get_html(url):
    driver = get_driver(url)
    # 隐式等待
    driver.implicitly_wait(10)
    # 浏览器最大化
    driver.maximize_window()
    driver.get(url)
    if not DEBUG: time.sleep(random.uniform(3, 4))
    # 定位登录界面关闭按钮
    close_btn = driver.find_element(By.XPATH, "//button[@class='Button Modal-closeButton Button--plain']")
    # 点击登录界面关闭按钮
    close_btn.click()
    scroll_to_bottom(driver)
    answerElementList = driver.find_elements(By.CSS_SELECTOR, "#QuestionAnswers-answers .List-item .ContentItem")
    return answerElementList, driver

def get_driver(url):
    # 输入需要爬取知乎回答的问题链接
    # url = input('请输入需要爬取知乎回答的问题链接：\n')
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    # 谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--disable-gpu')
    # # 禁止图片和CSS加载，减小抓取时间
    # prefs = {'profile.default_content_setting_values': { 'images': 2, 'javascript': 2,
    #                         'plugins': 2,}}
    prefs = {'profile.default_content_setting_values': { 'images': 2, }}
    chrome_options.add_experimental_option("prefs", prefs)

    # 禁止图片和CSS加载，减小抓取时间
    # firefox_profile = webdriver.ChromeOptions()
    # firefox_profile.set_preference('permissions.default.image', 2)
    # firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
    # firefox_profile.set_preference('permissions.default.stylesheet', 2)
    # 打开浏览器 mac 如果是win，在chromedriver后添加.exe
    s = Service("Driver/chromedriver")

    driver = webdriver.Chrome(service=s, options=chrome_options)
    return driver

def scroll_to_bottom(driver):
    # 获取当前窗口的总高度
    js = 'return action=document.body.scrollHeight'
    # 初始化滚动条所在的高度
    height = 0
    # 当前窗口总高度
    currHeight = driver.execute_script(js)
    while height < currHeight:
        # 将滚动条调整至页面底端
        for i in range(height, currHeight, 100):
            driver.execute_script("window.scrollTo(0, {})".format(i))
            if not DEBUG: time.sleep(0.02)
        height = currHeight
        currHeight = driver.execute_script(js)
        if not DEBUG: time.sleep(3)

def get_answers(answerElementList, url):
    # 定义一个存储回答中的信息的数据表格
    answerData = pd.DataFrame(
        columns=(
            'question_title', 'answer_url', 'question_url', 'author_name', 'fans_count', 'created_time', 'updated_time',
            'comment_count',
            'voteup_count', 'content'))
    numAnswer = 0
    # 遍历每一个回答并获取回答中的信息
    for answer in answerElementList:
        dictText = json.loads(answer.get_attribute('data-zop'))
        question_title = dictText['title']  # 问题名称
        answer_url = answer.find_element(By.XPATH,
                                         "meta[@itemprop='url' and contains(@content, 'answer')]").get_attribute(
            'content')  # 获取回答的链接
        author_name = dictText['authorName']  # 回答作者名称
        fans_count = answer.find_element(By.XPATH, "*//meta[contains(@itemprop, 'followerCount')]").get_attribute(
            'content')  # 获取粉丝数量
        created_time = answer.find_element(By.XPATH, "meta[@itemprop='dateCreated']").get_attribute(
            'content')  # 获取回答的创建时间
        updated_time = answer.find_element(By.XPATH, "meta[@itemprop='dateModified']").get_attribute(
            'content')  # 获取回答最近的编辑时间
        comment_count = answer.find_element(By.XPATH, "meta[@itemprop='commentCount']").get_attribute(
            'content')  # 获取该回答的评论数量
        voteup_count = answer.find_element(By.XPATH, "meta[@itemprop='upvoteCount']").get_attribute(
            'content')  # 获取回答的赞同数量
        contents = answer.find_elements(By.TAG_NAME, "p")# .text.replace("\n", "")  # 回答内容
        # print(contents.text)
        content = ''.join([content.text for content in contents])
        # print(answer)
        # content = ''
        if not DEBUG: time.sleep(0.001)
        row = {'question_title': [question_title],
               'author_name': [author_name],
               'question_url': [url],
               'answer_url': [answer_url],
               'fans_count': [fans_count],
               'created_time': [created_time],
               'updated_time': [updated_time],
               'comment_count': [comment_count],
               'voteup_count': [voteup_count],
               'content': [content]
               }
        answerData = answerData.append(pd.DataFrame(row), ignore_index=True)
        numAnswer += 1
        print(f"[NORMAL] 问题：【{question_title}】 的第 {numAnswer} 个回答抓取完成...")
        if not DEBUG: time.sleep(0.2)

    return answerData, question_title

if __name__ == '__main__':

    # 获取当前已经抓取的所有问题
    try:
        df_tmp = pd.read_csv('zhihu_result.csv')
        question_url_contained = set(df_tmp['question_url'].to_list())
        del df_tmp

    except Exception as e:
        print('no breakpoint:', e)
        question_url_contained = set()
        # 创建表头
        answerData_init = pd.DataFrame(
            columns=(
                'question_title', 'answer_url', 'question_url', 'author_name', 'fans_count', 'created_time', 'updated_time',
                'comment_count',
                'voteup_count', 'content'))
        answerData_init.to_csv(f'zhihu_result.csv', mode='a', encoding='utf-8', index=False, header = True)

    print('需要抓取的问题数量：', len(config.urls))
    for url in config.urls:
        if url in question_url_contained:
            continue
        print('----------------------------------------')
        print('url:', url)

        # https://www.zhihu.com/question/20000010
        url_num = int(url.split('/')[-1])
        try:
            if not DEBUG: time.sleep(random.uniform(60, 120))
            answerElementList, driver = get_html(url)
            print("[NORMAL] 开始抓取该问题的回答...")
            answerData, question_title = get_answers(answerElementList, url)
            print(f"[NORMAL] 问题：【{question_title}】 的回答全部抓取完成...")
            if not DEBUG: time.sleep(random.uniform(1, 3))
            question_title = re.sub(r'[\W]', '', question_title)
            filename = str(f"result-{datetime.datetime.now().strftime('%Y-%m-%d')}-{question_title}")
            # answerData.to_csv(f'{config.results_path}/{filename}.csv', encoding='utf-8', index=False)
            # 并入总表
            # answerData_all = answerData_all.append(pd.DataFrame(answerData), ignore_index=True)
            #if url_num % 100 == 0:
            # 并入总表
            answerData.to_csv(f'zhihu_result.csv', mode='a', encoding='utf-8', index=False, header = False)
            print(f"[NORMAL] 问题：【{question_title}】 的回答已经保存至 {filename}.xlsx...")
            driver.close()
            # print(e)
            # print(f"[ERROR] 抓取失败...")
        except Exception as e:
            if not DEBUG: time.sleep(random.uniform(300, 400))
            print(e)
            print(f"[ERROR] 抓取失败...")

            continue


import sys
import os
sys.path.append(os.getcwd()+"\\EMG")
import time
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import butter, lfilter, freqz, filtfilt, detrend, sosfilt
from PyQt6.QtCore import Qt, QTimer, QThread, QMutex, pyqtSignal
from PyQt6.QtWidgets import QApplication, QSizePolicy, QFrame, QPushButton
import pyqtgraph as pg
# ui
from ui.canvas_ui.canvasTab_ui.canvasTab import Ui_canvasTab
from widget.canvas_frame.myCanvas.MyCanvas import MyCanvas

class drawTabFrame(QFrame, Ui_canvasTab):
    """标记数据段绘图

    Attributes:
    ----------------
        tabCanvasList: 用于存储tabCanvas的列表(三维)
        findNum: 记录标记数据段的个数
        fromNum: 记录标记数据段的绘图起始位置
        toNum: 记录标记数据段的绘图结束位置
        fresh_state: 记录是否刷新tabCanvas的状态
        arrayLen: 用于存储tab数据的长度(实时)
        tabDataList: 用于存储tab数据的列表(二维 arrayLen * sample_rate)
        data_tab_point: # 存储标记数据段的坐标点(结束记录后存储)
    """

    def __init__(self, parent=None):
        super().__init__()
        self.mainWin = parent
        self.initUI()
        self.initValues()
        self.initCanvasTab()

    def initUI(self):
        self.setupUi(self)
        # 横向滚动条关闭
        self.btn_head.clicked.connect(self.btn_head_clicked)    # 头部节点
        self.btn_tail.clicked.connect(self.btn_tail_clicked)    # 尾部节点
        self.btn_pre.clicked.connect(self.btn_pre_clicked)  # 上一页
        self.btn_next.clicked.connect(self.btn_next_clicked)    # 下一页
        # self.et_page.returnPressed.connect(self.et_page_returnPressed)  # 跳转到指定页_键入
        self.btn_to.clicked.connect(self.btn_to_clicked)    # 跳转到指定页_点击
        self.sb_page.editingFinished.connect(self.sb_page_editingFinished)  # 显示当前页码范围

    def initValues(self):
        self.tabCanvasList = [] # 用于存储tabCanvas的列表(三维)
        self.findNum = 0    # 记录标记数据段的个数
        self.fromNum = 1    # 记录标记数据段的绘图起始位置
        self.toNum = 3    # 记录标记数据段的绘图结束位置
        self.fresh_state = True # 记录是否刷新tabCanvas的状态
        self.arrayLen = 50  # 用于存储tab数据的长度(实时)
        self.tabDataList = np.zeros((self.arrayLen, 4000))   # 用于存储tab数据的列表(二维 arrayLen * sample_rate)
        self.data_tab_point = np.array([])  # 存储标记数据段的坐标点(结束记录后存储)

    def initCanvasTab(self):   # 初始化画布Tab
        pg.setConfigOption('foreground', 'k')   # 设置前景色(绘图折线笔刷颜色) - 黑色

        # 设置tabCanvas(三维)
        for i in range(3):
            tabCanvas = MyCanvas()
            # tabCanvas.setWindowTitle('第'+str(i+1)+'个标记数据段')
            self.plotLayout2.addWidget(tabCanvas)
            self.tabCanvasList.append(tabCanvas)

    def canvasTabDraw(self, fromNum:int, toNum:int):    # 画布Tab绘图
        """画布Tab绘图

        Attributes:
        ----------------
            fromNum: 绘图起始位置
            toNum: 绘图结束位置
        """
        size = toNum - fromNum + 1
        for i in range(size):
            self.tabCanvasList[i].getPlotItem().clearPlots()  # 清空画布Tab
            self.tabCanvasList[i].getPlotItem().setTitle('第'+str(fromNum+i)+'个标记数据段')
            self.tabCanvasList[i].addItem(pg.PlotCurveItem(np.arange(self.data_tab_point[fromNum+i-1], 4000, 1), self.tabDataList[(fromNum+i-1)%20], pen=pg.mkPen('k', width=1.5)))  # 添加画布Tab数据

        self.lb_page.setText(str(fromNum) + '-' + str(toNum))

    def btn_head_clicked(self): # 跳转到头部节点
        self.fresh_state = False
        if self.findNum > 3:
            self.fromNum = 1
            self.toNum = 3
        else:
            self.fromNum = 1
            self.toNum = self.findNum
        self.canvasTabDraw(1, self.findNum)
        ...

    def btn_tail_clicked(self): # 跳转到尾部节点
        self.fresh_state = True
        if self.findNum > 3:
            self.fromNum = self.findNum - 2
            self.toNum = self.findNum
        else:
            self.fromNum = 1
            self.toNum = self.findNum
        self.canvasTabDraw(self.fromNum, self.findNum)
        ...

    def btn_pre_clicked(self):  # 上一页
        self.fresh_state = False
        if self.fromNum > 3:
            self.fromNum -= 1
            self.toNum -= 1
        else:
            self.fromNum = 1
            self.toNum = 3
        self.canvasTabDraw(self.fromNum, self.toNum)
        ...

    def btn_next_clicked(self): # 下一页
        self.fresh_state = False
        if self.fromNum < self.findNum - 2:
            self.fromNum += 1
            self.toNum += 1
        else:
            self.fromNum = self.findNum - 2
            self.toNum = self.findNum
        self.canvasTabDraw(self.fromNum, self.toNum)
        ...

    def sb_page_editingFinished(self):  # 跳转页码_键入
        self.fresh_state = False
        self.fromNum = int(self.sb_page.text()) - 1
        self.toNum = self.fromNum + 2
        if self.fromNum < 1:
            self.fromNum = 1
            self.toNum = 3
        if self.toNum > self.findNum:
            self.toNum = self.findNum
        self.canvasTabDraw(self.fromNum, self.toNum)
        ...

    def btn_to_clicked(self):   # 跳转页码_点击 # WAIT
        ...

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = drawTabFrame()
    win.show()
    sys.exit(app.exec())


import numpy as np
import glob
import read_sph_data as rsd
import warnings
import matplotlib.pyplot as plt

warnings.filterwarnings('ignore')

simdir = "/home/hatfull/data/ulfhednar/relax/r022u_1.52M_2350Myr_100k_cubic_spline_hacked/"
datafiles = [min(glob.glob(simdir+"out*.sph")),max(glob.glob(simdir+"out*.sph"))]
moddir = "/home/hatfull/data/ulfhednar/starsev/s001u_V1309/models/"
modelfile = moddir+"m_1.52_t_2350.mod.muse"

#datafile = simdir+"out0398.sph"

a = np.float64(7.5657e-15) #erg/cm^3/K^4
k = np.float64(1.38064852e-16) #erg/K
m_H = np.float64(1.6737236e-24) #g
g = np.float64(1./1000.) #kg
cm = np.float64(1./100.) #m
s = np.float64(1.)
J = np.float64(g * cm**2. / s**2.) #Joules
G = np.float64(1./(6.67408e-11 / cm**3. * g * s**2.)) #Gravitational cosntant in cgs
Ms = np.float64(1./(1.9891e30 / g)) #Mass of the sun in cgs
Rs = np.float64(1./(6.9599e8 / cm)) #Radius of the sun in cgs
gamma = 5./3.

def read_data(datafile):
    data_code, header = rsd.read(datafile,return_header=True)
    r_code = np.sqrt(data_code[:,0]**2. + data_code[:,1]**2. + data_code[:,2]**2.)
    data_code = data_code[np.argsort(r_code)] #Sort the data by radius so we can get mass coords.

    mencl = np.cumsum(data_code[:,3])
    r_code = np.sqrt(data_code[:,0]**2. + data_code[:,1]**2. + data_code[:,2]**2.)

    m_code = data_code[:,3]
    u_code = data_code[:,12] / G / Ms * Rs #ergs/g
    #w_code = data_code[:,14] / G / Ms * Rs #ergs/g
    w_code = -mencl / r_code / G / Ms * Rs

    t = header[9]
    return r_code[1:], mencl[1:], m_code[1:], u_code[1:], w_code[1:], t #Exclude the core

data_model = np.loadtxt(modelfile)
data_model = data_model[np.where(data_model[:,0] > 0.181972)] #Exclude the core
#We need to remove entries that have duplicate values of M so we can integrate
mockdata = data_model[:,0]
unqinds = np.array([],dtype=np.int32)
for i in range(0, len(mockdata)-1):
    if mockdata[i] != mockdata[i+1]:
        unqinds = np.append(unqinds,i)

if mockdata[-2] == mockdata[-1]:
    unqinds = np.append(unqinds,-1)

data_model = data_model[unqinds] #Remove duplicate entries except for the very last duplicate entry
m_model = data_model[:,0]
r_model = data_model[:,1]
u_model = data_model[:,20]
w_model = -m_model / r_model / G / Ms * Rs

r_code0, mencl0, m_code0, u_code0, w_code0, t0 = read_data(datafiles[0])
r_code1, mencl1, m_code1, u_code1, w_code1, t1 = read_data(datafiles[1])

plt.plot(r_model,np.log10(-w_model),color='k',label='EV 1D (766)')
plt.plot(r_code0,np.log10(-w_code0),color='r',label='SPH ($t='+str(t0)[:6]+'$)')
plt.plot(r_code1,np.log10(-w_code1),color='b',label='SPH ($t='+str(t1)[:6]+'$)')

plt.legend()

plt.xlabel("$r/R_{\\odot}$")
plt.ylabel("log$_{10}\ (-w)$ [ergs g$^{-1}$]")

plt.title(simdir.split("/")[-2]).set_position([0.5,1.05])

plt.savefig(simdir.split("/")[-2].split("_")[0]+"_w.png")

#plt.show()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Event',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('external_id', models.IntegerField()),
                ('title', models.CharField(max_length=100)),
                ('text', models.TextField(null=True, blank=True)),
                ('description', models.TextField(null=True, blank=True)),
                ('stage_theatre', models.CharField(max_length=100, null=True, blank=True)),
                ('price', models.NullBooleanField()),
                ('kids', models.NullBooleanField()),
                ('age_restricted', models.SmallIntegerField(null=True, blank=True)),
                ('run_time', models.SmallIntegerField(null=True, blank=True)),
            ],
        ),
        migrations.CreateModel(
            name='EventPerson',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('event', models.ForeignKey(related_name='person_set', to='kudago_app.Event')),
            ],
        ),
        migrations.CreateModel(
            name='EventType',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('slug', models.SlugField()),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.CreateModel(
            name='Image',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('url', models.URLField()),
            ],
        ),
        migrations.CreateModel(
            name='Person',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='Places',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ],
        ),
        migrations.CreateModel(
            name='Role',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=100)),
            ],
        ),
        migrations.CreateModel(
            name='Schedule',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ],
        ),
        migrations.CreateModel(
            name='Tag',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=140)),
            ],
        ),
        migrations.AddField(
            model_name='eventperson',
            name='person',
            field=models.ForeignKey(related_name='event_set', to='kudago_app.Person'),
        ),
        migrations.AddField(
            model_name='eventperson',
            name='role',
            field=models.ForeignKey(related_name='event_persons_set', to='kudago_app.Role'),
        ),
        migrations.AddField(
            model_name='event',
            name='gallery',
            field=models.ManyToManyField(related_name='events', to='kudago_app.Image'),
        ),
        migrations.AddField(
            model_name='event',
            name='persons',
            field=models.ManyToManyField(related_name='events', through='kudago_app.EventPerson', to='kudago_app.Person'),
        ),
        migrations.AddField(
            model_name='event',
            name='tags',
            field=models.ManyToManyField(related_name='events', to='kudago_app.Tag'),
        ),
        migrations.AddField(
            model_name='event',
            name='type',
            field=models.ForeignKey(related_name='events', to='kudago_app.EventType'),
        ),
    ]


import json
import typing
from collections import OrderedDict

from flask import Flask, render_template
from flask_compress import Compress

from db import db
from models import FediInstance

app = Flask('fedisus')
app.config.from_envvar('FEDISUS_CONFIG', True)
app.config.from_object('settings')
db.init_app(app)
compress = Compress(app)

from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(
    app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)

def count_reject_popularity(instances: typing.List[FediInstance], only_more_than_one=True):
    import operator
    r = {}
    for i in instances:
        rej = i.MRF_Reject.split(', ')
        for reject in rej:
            if reject in r:
                r[reject] += 1
            else:
                r[reject] = 1

    if only_more_than_one:
        for k in list(r.keys()):
            if r[k] <= 1:
                del r[k]
    return OrderedDict(sorted(r.items(), key=operator.itemgetter(1), reverse=True))

@app.route('/')
@compress.compressed()
def index():
    with_reject = FediInstance.query.filter(FediInstance.Valid == True, FediInstance.MRF_Reject != None)

    ctx = {
        'stats': {
            'numinstances': FediInstance.query.count(),
            'numvalid': FediInstance.query.filter(FediInstance.Valid == True).count(),
            'nummrf': FediInstance.query.filter(FediInstance.MRF_Policies != None).count(),
            'numreject': with_reject.count(),
        },
        'rejects': with_reject.all(),
        'reject_popularity': count_reject_popularity(with_reject.all()),
        'last_update': FediInstance.query.order_by(FediInstance.modified_at.desc()).first().modified_at
    }

    return render_template('simple_index.html', **ctx)

@app.route('/rejects.json')
@compress.compressed()
def rejects_json():
    with_reject = FediInstance.query.filter(FediInstance.Valid == True, FediInstance.MRF_Reject != None)
    num = with_reject.count()

    reject_stats = count_reject_popularity(with_reject.all(), only_more_than_one=False)
    res = dict(reject_stats)

    return json.dumps(res)

@app.route('/detail/<instance>')
def details(instance: str):
    instance = instance.strip()
    fi = FediInstance.query.filter(FediInstance.Address == instance).first()
    if fi is None:
        return "Instance not found or not yet scraped, try again later", 404

    difi = dict(fi.__dict__)
    difi.pop('_sa_instance_state')
    difi['modified_at'] = str(difi['modified_at'])
    difi['created_at'] = str(difi['created_at'])

    banning = []
    if fi.MRF_Reject is not None and len(fi.MRF_Reject) > 0:
        banning = fi.MRF_Reject.split(', ')

    ctx = {
        'i': fi,
        'banning': banning,
        'banned_by': FediInstance.query.filter(FediInstance.MRF_Reject.contains(fi.Address)).all(),
        'rawinfo': json.dumps(difi, sort_keys=True, indent=4)
    }

    return render_template('detail.html', **ctx)

if __name__ == '__main__':
    from models import FediInstance

    db.init_app(app)
    with app.app_context():
        db.create_all()
        db.session.commit()
    app.run(debug=True)


import sys
import os
from pathlib import Path
import random
import json
import pandas as pd
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from seaborn import heatmap
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QRadioButton, QLabel, QVBoxLayout, QHBoxLayout, \
    QButtonGroup, QTableView, QHeaderView, QSizePolicy, QTableWidget, QTableWidgetItem
from PyQt5.QtGui import QPixmap, QFont
from PyQt5.QtCore import Qt
from PyQt5 import QtCore
from typing import List
from results_reader import ResultReader, MonteCarloPlotter

from pdb import set_trace

if os.path.exists("/media/barrachina/data"):
    root_drive = Path("/media/barrachina/data")
elif os.path.exists("D:/"):
    root_drive = Path("D:/")
else:
    raise FileNotFoundError("Results path not found")

BASE_PATHS = {
    "BRET": str(root_drive / "datasets/PolSar/Bretigny-ONERA/bret-2003.png"),
    "OBER": str(
        root_drive / "datasets/PolSar/Oberpfaffenhofen/ESAR_Oberpfaffenhofen_T6/Master_Track_Slave_Track/T6/PauliRGB_T1.bmp"),
    "SF-AIRSAR": str(root_drive / "datasets/PolSar/San Francisco/PolSF/SF-AIRSAR/SF-AIRSAR-Pauli.bmp"),
    "SF-RS2": str(root_drive / "datasets/PolSar/San Francisco/PolSF/SF-RS2/SF-RS2-Pauli.bmp"),
    "FLEVOLAND": str(root_drive / "datasets/PolSar/Flevoland/AIRSAR_Flevoland/T3/PauliRGB.bmp"),
    "GARON": str(root_drive / "/datasets/PolSar/garon/20141125-1_djit_rad.png")
}
GROUND_TRUTH_PATHS = {
    "BRET": str(root_drive / "datasets/PolSar/Bretigny-ONERA/labels_4roi.png"),
    "OBER": str(root_drive / "datasets/PolSar/Oberpfaffenhofen/ground_truth.png"),
    "SF-AIRSAR": str(root_drive / "datasets/PolSar/San Francisco/PolSF/SF-AIRSAR/SF-AIRSAR-label3d.png"),
    "SF-RS2": str(root_drive / "datasets/PolSar/San Francisco/PolSF/SF-RS2/SF-RS2-label3d.png"),
    "FLEVOLAND": str(root_drive / "datasets/PolSar/Flevoland/AIRSAR_Flevoland/ground_truth.png"),
    "GARON": str(root_drive / "/datasets/PolSar/garon/20141125-1_djit_rad-labels.png")
}
START_VALUES = {
    "dataset": 'SF-AIRSAR',
    "model": "cao",
    "dtype": 'complex',
    "library": "complex",
    "dataset_mode": 'k',
    "dataset_method": 'random',
    "balance": 'None',
}

DEFAULT_MATPLOTLIB_COLORS = plt.rcParams['axes.prop_cycle'].by_key()['color']

class DataFrameModel(QtCore.QAbstractTableModel):
    DtypeRole = QtCore.Qt.UserRole + 1000
    ValueRole = QtCore.Qt.UserRole + 1001

    def __init__(self, df=pd.DataFrame(), parent=None):
        super(DataFrameModel, self).__init__(parent)
        self._dataframe = df

    def setDataFrame(self, dataframe):
        self.beginResetModel()
        self._dataframe = dataframe.copy()
        self.endResetModel()

    def dataFrame(self):
        return self._dataframe

    dataFrame = QtCore.pyqtProperty(pd.DataFrame, fget=dataFrame, fset=setDataFrame)

    @QtCore.pyqtSlot(int, QtCore.Qt.Orientation, result=str)
    def headerData(self, section: int, orientation: QtCore.Qt.Orientation, role: int = QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return self._dataframe.columns[section]
            else:
                return str(self._dataframe.index[section])
        return QtCore.QVariant()

    def rowCount(self, parent=QtCore.QModelIndex()):
        if parent.isValid():
            return 0
        return len(self._dataframe.index)

    def columnCount(self, parent=QtCore.QModelIndex()):
        if parent.isValid():
            return 0
        return self._dataframe.columns.size

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if not index.isValid() or not (0 <= index.row() < self.rowCount() and 0 <= index.column() < self.columnCount()):
            return QtCore.QVariant()
        row = self._dataframe.index[index.row()]
        col = self._dataframe.columns[index.column()]
        dt = self._dataframe[col].dtype

        val = self._dataframe.iloc[row][col]
        if role == QtCore.Qt.DisplayRole:
            return str(val)
        elif role == DataFrameModel.ValueRole:
            return val
        if role == DataFrameModel.DtypeRole:
            return dt
        return QtCore.QVariant()

    def roleNames(self):
        roles = {
            QtCore.Qt.DisplayRole: b'display',
            DataFrameModel.DtypeRole: b'dtype',
            DataFrameModel.ValueRole: b'value'
        }
        return roles

    def flags(self, index) -> Qt.ItemFlags:
        # QtCore.Qt.ItemIsEditable |
        # | QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled
        # | QtCore.Qt.ItemIsTristate
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # Variables
        self.simulation_results = ResultReader()
        self.params = START_VALUES  # start config to show
        self.plotter = MonteCarloPlotter()

        # Qt objects
        self.setWindowTitle("Results")
        self.label_image = QLabel()
        self.ground_truth_image = QLabel()
        self.params_label = QLabel(str(self.params))
        self.params_label.setAlignment(Qt.AlignCenter)
        self.btngroup = []

        widget = QWidget()

        outer_layout = QVBoxLayout()
        lower_layout = self._get_lower_layout()
        outer_layout.addLayout(self._get_upper_layout())
        outer_layout.addLayout(lower_layout)
        widget.setLayout(outer_layout)
        # widget.setLayout(hlayout)
        self.setCentralWidget(widget)
        self.show()
        self.update_information()

    # Layouts
    def _get_accuracy_layout(self):
        self.acc_values_title = QLabel("Accuracy")
        myFont = QFont()
        myFont.setBold(True)
        self.acc_values_title.setFont(myFont)
        key = []
        key.append("Train OA")
        key.append("Train AA")
        key.append("Validation OA")
        key.append("Validation AA")
        key.append("Test OA")
        key.append("Test AA")
        self.tableAccWidget = QTableWidget()
        self.tableAccWidget.setRowCount(4)
        self.tableAccWidget.setColumnCount(6)
        self.tableAccWidget.setHorizontalHeaderLabels(key)
        self.tableAccWidget.setVerticalHeaderLabels(['mean', 'median', 'IQR', 'range'])
        self.tableAccWidget.setMinimumHeight(150)
        vbox = QVBoxLayout()
        vbox.addWidget(self.acc_values_title)
        vbox.addWidget(self.tableAccWidget)
        return vbox

    def _get_upper_layout(self):
        hlayout = QHBoxLayout()  # Main layout. Horizontal 2 things, radio buttons + image
        vlayout = QVBoxLayout()
        vlayout.addLayout(self.radiobuttons())
        vlayout.addWidget(self._get_conf_figure_widget())
        hlayout.addLayout(vlayout)
        img_layout = QVBoxLayout()
        img_layout.addWidget(self.params_label)  # Current config
        img_layout.addWidget(self.label_image)  # Show image
        hlayout.addLayout(img_layout)
        return hlayout

    def _get_lower_layout(self):
        hlayout = QHBoxLayout()

        hlayout.addWidget(self._get_dataframe_table_layout())
        hlayout.addLayout(self._get_figure_layout())

        return hlayout

    def _get_dataframe_table_layout(self):
        # layout = QHBoxLayout()
        self.tableView = QTableView()
        # self.verticalLayout.addWidget(self.tableView)
        # set_trace()
        self.tableView.setModel(
            DataFrameModel(self.simulation_results.df
                           .drop_duplicates()
                           .sort_values(by=['dataset', 'model', 'dtype', 'library',
                                            'dataset_mode', 'dataset_method', 'balance'])
                           .reset_index(drop=True)
                           )
        )
        self.tableView.setAlternatingRowColors(True)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        for i in range(len(self.simulation_results.df.keys())):
            self.tableView.horizontalHeader().setSectionResizeMode(i, QHeaderView.ResizeToContents)
        self.tableView.setMinimumWidth(600)
        sp = self.tableView.sizePolicy()
        sp.setHorizontalPolicy(QSizePolicy.Minimum)
        self.tableView.setSizePolicy(sp)
        # layout.addWidget(self.tableView)
        return self.tableView

    def _get_figure_layout(self):
        self.figure = plt.figure()
        self.figure.tight_layout()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)
        vlay = QVBoxLayout()
        vlay.addWidget(self.toolbar)
        vlay.addWidget(self.canvas)
        return vlay

    def _get_conf_figure_widget(self):
        self.conf_figure = plt.figure()
        self.conf_figure.tight_layout()
        self.conf_canvas = FigureCanvas(self.conf_figure)
        return self.conf_canvas

    def radiobuttons(self):
        outer_vlay = QVBoxLayout()
        hl = QHBoxLayout()
        vlayout = QVBoxLayout()

        vlayout.addLayout(self.add_title(self.dataset_radiobutton(), name="Dataset"))
        vlayout.addLayout(self.add_title(self.model_radiobutton(), "Model"))
        vlayout.addLayout(self.add_title(self.dtype_radiobuttons(), "Dtype"))
        vlayout.addLayout(self.add_title(self.library_radiobutton(), "Library"))
        vlayout.addLayout(self.add_title(self.dataset_mode_radiobuttons(), "Dataset Mode"))
        vlayout.addLayout(self.add_title(self.model_method_radiobutton(), "Dataset Method"))
        vlayout.addLayout(self.add_title(self.balance_radiobuttons(), "Balance"))
        vlayout.addLayout(self.add_title(self.equiv_technique_radiobutton(), "Equivalent Technique"))

        hl.addLayout(vlayout)
        hl.addWidget(self.ground_truth_image)

        outer_vlay.addLayout(hl)
        outer_vlay.addStretch()
        outer_vlay.addLayout(self._get_accuracy_layout())
        return outer_vlay

    def dataset_mode_radiobuttons(self):
        vlayout = QHBoxLayout()
        self.coh_rb = QRadioButton("coh")
        self.coh_rb.toggled.connect(lambda: self.update_information("dataset_mode", self.coh_rb.text()))

        self.pauli_rb = QRadioButton("k", self)
        self.pauli_rb.toggled.connect(lambda: self.update_information("dataset_mode", self.pauli_rb.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(self.pauli_rb)
        self.btngroup[-1].addButton(self.coh_rb)

        self.coh_rb.setChecked(True)
        vlayout.addWidget(self.coh_rb)
        vlayout.addWidget(self.pauli_rb)
        vlayout.addStretch()

        return vlayout

    def dtype_radiobuttons(self):
        vlayout = QHBoxLayout()
        self.complex_dtype_rb = QRadioButton("complex")
        self.complex_dtype_rb.toggled.connect(
            lambda state: state and self.update_information("dtype", self.complex_dtype_rb.text()))

        self.real_dtype_rb = QRadioButton("real_imag", self)
        self.real_dtype_rb.toggled.connect(
            lambda state: state and self.update_information("dtype", self.real_dtype_rb.text()))

        rb2 = QRadioButton("amplitude_phase")
        rb2.toggled.connect(lambda state: state and self.update_information("dtype", rb2.text()))
        rb3 = QRadioButton("amplitude_only")
        rb3.toggled.connect(lambda state: state and self.update_information("dtype", rb3.text()))
        rb4 = QRadioButton("real_only")
        rb4.toggled.connect(lambda state: state and self.update_information("dtype", rb4.text()))

        self.btngroup.append(QButtonGroup())
        # self.btngroup[-1].addButton(self.real_dtype_rb)
        self.btngroup[-1].addButton(self.complex_dtype_rb)
        self.btngroup[-1].addButton(self.real_dtype_rb)
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(rb3)
        self.btngroup[-1].addButton(rb4)

        self.complex_dtype_rb.setChecked(True)
        vlayout.addWidget(self.complex_dtype_rb)
        vlayout.addWidget(self.real_dtype_rb)
        vlayout.addWidget(rb2)
        vlayout.addWidget(rb3)
        vlayout.addWidget(rb4)
        vlayout.addStretch()

        return vlayout

    def balance_radiobuttons(self):
        vlayout = QHBoxLayout()
        rb1 = QRadioButton("none")
        rb1.toggled.connect(lambda: self.update_information("balance", rb1.text()))

        rb2 = QRadioButton("loss", self)
        rb2.toggled.connect(lambda: self.update_information("balance", rb2.text()))

        rb3 = QRadioButton("dataset")
        rb3.toggled.connect(lambda: self.update_information("balance", rb3.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb3)
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(rb1)

        rb1.setChecked(True)
        vlayout.addWidget(rb1)
        vlayout.addWidget(rb2)
        vlayout.addWidget(rb3)
        vlayout.addStretch()

        return vlayout

    def equiv_technique_radiobutton(self):
        vlayout = QHBoxLayout()
        rb1 = QRadioButton("ratio_tp")
        rb1.toggled.connect(lambda: self.update_information("equiv_technique", rb1.text()))

        rb2 = QRadioButton("np", self)
        rb2.toggled.connect(lambda: self.update_information("equiv_technique", rb2.text()))

        rb3 = QRadioButton("alternate_tp")
        rb3.toggled.connect(lambda: self.update_information("equiv_technique", rb3.text()))

        rb4 = QRadioButton("none")
        rb4.toggled.connect(lambda: self.update_information("equiv_technique", rb4.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb4)
        self.btngroup[-1].addButton(rb3)
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(rb1)

        rb1.setChecked(True)
        vlayout.addWidget(rb1)
        vlayout.addWidget(rb2)
        vlayout.addWidget(rb3)
        vlayout.addWidget(rb4)
        vlayout.addStretch()

        return vlayout

    def library_radiobutton(self):
        vlayout = QHBoxLayout()
        self.cvnn_library_rb = QRadioButton("cvnn", self)
        self.cvnn_library_rb.toggled.connect(
            lambda state: state and self.update_information("library", self.cvnn_library_rb.text()))

        rb2 = QRadioButton("tensorflow", self)
        rb2.toggled.connect(lambda state: state and self.update_information("library", rb2.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(self.cvnn_library_rb)

        self.cvnn_library_rb.setChecked(True)
        vlayout.addWidget(self.cvnn_library_rb)
        vlayout.addWidget(rb2)
        vlayout.addStretch()

        return vlayout

    def model_method_radiobutton(self):
        vlayout = QHBoxLayout()
        rb1 = QRadioButton("random")
        rb1.toggled.connect(lambda: self.update_information("dataset_method", rb1.text()))

        rb2 = QRadioButton("separate", self)
        rb2.toggled.connect(lambda: self.update_information("dataset_method", rb2.text()))

        rb3 = QRadioButton("single_separated_image")
        rb3.toggled.connect(lambda: self.update_information("dataset_method", rb3.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb3)
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(rb1)

        rb1.setChecked(True)
        vlayout.addWidget(rb1)
        vlayout.addWidget(rb2)
        vlayout.addWidget(rb3)
        vlayout.addStretch()

        return vlayout

    def model_radiobutton(self):
        vlayout = QHBoxLayout()
        rb1 = QRadioButton("cao")
        rb1.toggled.connect(lambda: self.update_information("model", rb1.text()))

        rb2 = QRadioButton("own", self)
        rb2.toggled.connect(lambda: self.update_information("model", rb2.text()))

        # rb3 = QRadioButton("zhang")
        # rb3.toggled.connect(lambda: self.update_information("model", rb3.text()))
        #
        # rb4 = QRadioButton("haensch", self)
        # rb4.toggled.connect(lambda: self.update_information("model", rb4.text()))
        #
        # rb5 = QRadioButton("tan", self)
        # rb5.toggled.connect(lambda: self.update_information("model", rb5.text()))

        rb6 = QRadioButton("cnn", self)
        rb6.toggled.connect(lambda: self.update_information("model", rb6.text()))

        rb7 = QRadioButton("mlp", self)
        rb7.toggled.connect(lambda: self.update_information("model", rb7.text()))

        rb8 = QRadioButton("expanded-mlp", self)
        rb8.toggled.connect(lambda: self.update_information("model", rb8.text()))

        rb9 = QRadioButton("expanded-cnn", self)
        rb9.toggled.connect(lambda: self.update_information("model", rb9.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb9)
        self.btngroup[-1].addButton(rb8)
        self.btngroup[-1].addButton(rb7)
        self.btngroup[-1].addButton(rb6)
        # self.btngroup[-1].addButton(rb5)
        # self.btngroup[-1].addButton(rb4)
        # self.btngroup[-1].addButton(rb3)
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(rb1)

        rb1.setChecked(True)
        vlayout.addWidget(rb1)
        vlayout.addWidget(rb2)
        # vlayout.addWidget(rb3)
        # vlayout.addWidget(rb4)
        # vlayout.addWidget(rb5)
        vlayout.addWidget(rb6)
        vlayout.addWidget(rb7)
        vlayout.addWidget(rb8)
        vlayout.addWidget(rb9)
        vlayout.addStretch()

        return vlayout

    def dataset_radiobutton(self):
        vlayout = QHBoxLayout()
        rb1 = QRadioButton("SF-AIRSAR")
        rb1.toggled.connect(lambda: self.update_information("dataset", rb1.text()))

        rb2 = QRadioButton("SF-RS2", self)
        rb2.toggled.connect(lambda: self.update_information("dataset", rb2.text()))

        rb3 = QRadioButton("OBER")
        rb3.toggled.connect(lambda: self.update_information("dataset", rb3.text()))

        rb4 = QRadioButton("BRET", self)
        rb4.toggled.connect(lambda: self.update_information("dataset", rb4.text()))

        rb5 = QRadioButton("FLEVOLAND", self)
        rb5.toggled.connect(lambda: self.update_information("dataset", rb5.text()))

        self.btngroup.append(QButtonGroup())
        self.btngroup[-1].addButton(rb5)
        self.btngroup[-1].addButton(rb4)
        self.btngroup[-1].addButton(rb3)
        self.btngroup[-1].addButton(rb2)
        self.btngroup[-1].addButton(rb1)

        rb1.setChecked(True)
        vlayout.addWidget(rb1)
        vlayout.addWidget(rb2)
        vlayout.addWidget(rb3)
        vlayout.addWidget(rb4)
        vlayout.addWidget(rb5)
        vlayout.addStretch()

        return vlayout

    def add_title(self, layout, name: str, up: bool = True):
        lay = QVBoxLayout()
        l1 = QLabel(name)
        myFont = QFont()
        myFont.setBold(True)
        l1.setFont(myFont)
        l1.setAlignment(Qt.AlignLeft)
        if up:
            lay.addWidget(l1)
        lay.addLayout(layout)
        # lay.addStretch()
        if not up:
            lay.addWidget(l1)
        return lay

    # Update information
    def get_image(self, image_path: List[str]):
        if not image_path:
            pixmap = QPixmap(BASE_PATHS[self.params["dataset"]])
        else:
            image_path = random.choice(image_path)
            pixmap = QPixmap(image_path)
        scaled_pixmap = pixmap.scaled(900, 700, QtCore.Qt.KeepAspectRatio)
        self.label_image.setPixmap(scaled_pixmap)
        self.label_image.resize(scaled_pixmap.width(), scaled_pixmap.height())

    def get_image_ground_truth(self):
        pixmap = QPixmap(GROUND_TRUTH_PATHS[self.params["dataset"]])
        scaled_pixmap = pixmap.scaled(400, 200, QtCore.Qt.KeepAspectRatio)
        self.ground_truth_image.setPixmap(scaled_pixmap)
        self.ground_truth_image.resize(scaled_pixmap.width(), scaled_pixmap.height())

    def clear_plot(self):
        self.figure.clear()
        self.canvas.draw()

    def plot(self, history_path):
        self.figure.clear()
        ax1 = self.figure.add_subplot(121)
        ax2 = self.figure.add_subplot(122)
        ax1.clear()
        ax2.clear()
        self.plotter.plot(data=history_path, ax=ax1, keys=["accuracy", "val_accuracy"])
        self.plotter.plot(data=history_path, ax=ax2, keys=["loss", "val_loss"])
        ax1.grid(True, axis='both')
        ax2.grid(True, axis='both')
        self.canvas.draw()

    def clear_conf_matrix(self):
        if hasattr(self, "conf_figure"):
            self.conf_figure.clear()
            self.conf_canvas.draw()

    def plot_conf_matrix(self, conf_list):
        if hasattr(self, "conf_figure"):
            self.conf_figure.clear()
            ax1 = self.conf_figure.add_subplot(131)
            ax2 = self.conf_figure.add_subplot(132)
            ax3 = self.conf_figure.add_subplot(133)
            ax1.clear()
            ax2.clear()
            ax3.clear()
            heatmap(conf_list[0].drop('Total', axis=1).drop('Total'), ax=ax1, annot=True, cmap="rocket_r")
            heatmap(conf_list[1].drop('Total', axis=1).drop('Total'), ax=ax2, annot=True, cmap="rocket_r")
            if len(conf_list) > 2:
                heatmap(conf_list[2].drop('Total', axis=1).drop('Total'), ax=ax3, annot=True, cmap="rocket_r")
            self.conf_canvas.draw()

    def print_values(self, json_key):
        if json_key is not None and hasattr(self, "tableAccWidget"):
            self.acc_values_title.setText(f"Accuracy (total count {int(self.simulation_results.get_total_count(json_key))})")
            for i, stat in enumerate(["mean", "median", "iqr", 'range']):
                for j, dat in enumerate(["train", "val", "test"]):
                    for k, variable in enumerate(["accuracy", "average_accuracy"]):
                        self.tableAccWidget.setItem(i, 2*j + k, QTableWidgetItem(
                            self.simulation_results.get_eval_stat_string(json_key=json_key, dataset=dat,
                                                                         stat=stat, variable=variable)))
            header = self.tableAccWidget.horizontalHeader()
            header.setSectionResizeMode(0, QHeaderView.ResizeToContents)
            header.setSectionResizeMode(1, QHeaderView.ResizeToContents)
            header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
            header.setSectionResizeMode(3, QHeaderView.ResizeToContents)
            header.setSectionResizeMode(4, QHeaderView.ResizeToContents)
            header.setSectionResizeMode(5, QHeaderView.ResizeToContents)
            # header.setMinimumSectionSize(200)
        elif hasattr(self, "tableAccWidget"):
            self.acc_values_title.setText("Accuracy")
            for i in range(len(["mean", "median", "iqr", 'range'])):
                self.tableAccWidget.setItem(i, 0, QTableWidgetItem(f"00.00%"))
                self.tableAccWidget.setItem(i, 1, QTableWidgetItem(f"00.00%"))
                self.tableAccWidget.setItem(i, 2, QTableWidgetItem(f"00.00%"))
                self.tableAccWidget.setItem(i, 3, QTableWidgetItem(f"00.00%"))
                self.tableAccWidget.setItem(i, 4, QTableWidgetItem(f"00.00%"))
                self.tableAccWidget.setItem(i, 5, QTableWidgetItem(f"00.00%"))

    def _verify_combinations(self, key, value):
        if value == "complex":
            if hasattr(self, 'cvnn_library_rb'):  # It wont exists if I still didnt create the radiobutton.
                self.cvnn_library_rb.setChecked(True)  # Set library cvnn
        elif value == "tensorflow":
            if hasattr(self, 'real_dtype_rb'):
                if self.complex_dtype_rb.isChecked():
                    self.real_dtype_rb.setChecked(True)  # Set real dtype
        elif key == 'dataset':
            if value == "OBER":
                if hasattr(self, 'coh_rb'):
                    self.coh_rb.setChecked(True)  # Set real dtype
                    self.coh_rb.setEnabled(False)
                if hasattr(self, 'pauli_rb'):
                    self.pauli_rb.setEnabled(False)
            else:
                if hasattr(self, 'coh_rb'):
                    self.coh_rb.setEnabled(True)
                if hasattr(self, 'pauli_rb'):
                    self.pauli_rb.setEnabled(True)
        elif key == "equiv_technique" and value != "ratio_tp":
            if hasattr(self, 'real_dtype_rb'):
                self.real_dtype_rb.setChecked(True)  # Set real dtype

    def update_information(self, key=None, value=None):
        if key and value:
            self.params[key] = value
            self._verify_combinations(key, value)
        self.params_label.setText(str(self.params))
        # Not yet working.
        # Try https://stackoverflow.com/questions/49929668/disable-and-enable-radiobuttons-from-another-radiobutton-in-pyqt4-python
        json_key = json.dumps(self.params, sort_keys=True)
        self.get_image(self.simulation_results.get_image(json_key))
        self.get_image_ground_truth()
        if self.simulation_results.data_exists(json_key):
            stats = self.simulation_results.get_stats(json_key=json_key)
            conf_stats = self.simulation_results.get_conf_stats(json_key=json_key)
            self.plot(stats) if self.simulation_results.data_exists(json_key) else self.clear_plot()
            self.print_values(json_key)
            self.plot_conf_matrix(conf_stats) if conf_stats else self.clear_conf_matrix()
        else:
            self.print_values(None)
            self.clear_plot()
            self.clear_conf_matrix()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec()


# Import thư viện
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.feature_selection import RFE
import matplotlib.pyplot as plt

# Tạo dữ liệu mẫu
X = np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]])
Y = np.array([10, 20, 25, 30, 35])

# Khởi tạo mô hình tuyến tính
model = LinearRegression()

# Áp dụng hồi quy từng bước
selector = RFE(model, n_features_to_select=1)
selector = selector.fit(X.T, Y)

# Dự đoán
Y_pred = selector.predict(X.T)
print("Dự đoán: ", Y_pred)

# Hiển thị biểu đồ
plt.scatter(X[0], Y, color='blue', label='Dữ liệu thực tế')
plt.plot(X[0], Y_pred, color='red', label='Dự đoán', linewidth=2)
plt.title('Hồi Quy Từng Bước: Doanh số bán hàng')
plt.xlabel('Quảng cáo')
plt.ylabel('Doanh số')
plt.legend()
plt.show()