# python 3.6 and more
# FastAPI service for docker
#
# example:
# (build --build_arg WORKERS=5 --build-arg FASTAPI_PORT=8000 -t ASR2Format .)
# docker run -p 8000:8000 --rm ASR2Format
# or local start with uvicorn: uvicorn main:app

fastapi_description = '''
Сервис преобразования распознанных ответов на заданные вопросы в необходимый формат.

Состоит из следующих частей:
 * общая часть - отображает параметры и текущее состояние микросервиса,
   имеет подключаемый логгер со своими настройками
 * операции с числами и числительными - необходимые операции, которые необходимо выполнить после
   распознования речи человека содержащие числительные
 * операции с временем - необходимые операции, которые необходимо выполнить после
   распознования речи человека содержащие время, часть суток и пр.
 * операции с датами - необходимые операции, которые необходимо выполнить после
   распознования речи человека содержащие даты
 * операции с адресами - необходимые операции, которые необходимо выполнить после
   распознования речи человека содержащие описательные признаки адреса в зависимости от особенностей региона
 * операции с услугами - необходимые операции, которые необходимо выполнить после
   распознования речи человека содержащие описательные признаки услуги.

'''

from typing import Any
from fastapi import Depends, FastAPI, Body, Path, Query, Request, HTTPException, status
from functools import lru_cache
from api_settings import Settings
from sheme import *
import uvicorn
import os
import sys
import socket as sock

try:
    from app_logger import get_logger, LogLevel
    from app_logger import Settings as LogSettings
except Exception as e:
    print('Критическая ошибка!\nПроверте значения в конфигурационный файл log_config или переменные среды')
    print(e)
    sys.exit()

# Кэшируемые функции выполняющиеся один раз для заданных параметров
@lru_cache()
def get_settings():
    return Settings()

def get_log_conf():
    return LogSettings()

@lru_cache()
def get_status():
    try:
        host_name = os.uname().nodename
    except:
        host_name = sock.gethostname()
    host_ip = sock.gethostbyname(host_name)
    py_ver = f"{sys.version_info.major}.{sys.version_info.minor}"
    return InfoDynamic(docker_ip=host_ip, docker_name=host_name, py_version=py_ver)

# Вычисление строки сообщения для логгера

def get_log_str(request: Request, func_name: str, answer: Any):
    client_ip = request.client.host
    method = request.method
    path = request.url.path
    if method.upper() == 'POST':
        body = request.body()
        result = "POST POST POST {body}"
    else:
        result = f"From {client_ip} {method} {path} SUCCESS worked {func_name} and return json({answer})"
    return result

############################ FastAPI инициализация
tags_metadata = [
    {
        "name": "common",
        "description": "Состояние и параметры сервиса fastAPI",
    },
    {
        "name": "number_manipulation",
        "description": "Утилиты для работы с числителями",
        # "externalDocs": {
        #     "description": "Thrift IDL MiddleEWare схема",
        #     "url": "https://fastapi.tiangolo.com/",
        # }
    },
    {
        "name": "time_manipulation",
        "description": "Утилиты для работы с временем",
    },
    {
        "name": "date_manipulation",
        "description": "Утилиты для работы с датами",
    },
    {
        "name": "address_search",
        "description": """Утилиты для поиска необходимого офиса или места
        по произвольному сообщению человека, исходя из особенностей региона""",
    },
    {
        "name": "service_search",
        "description": """Утилиты для поиска необходимого сервиса или услуги
        по произвольному сообщению человека, исходя из особенностей региона""",
    },
]
try:
    settings = get_settings()
    if settings.log_driver:
        logger_drv = get_logger(__name__, LogLevel.debug.value)
    else:
        logger_drv = None
except Exception as e:
    print('Критическая ошибка!\nПроверте значения в конфигурационных файлах или переменные среды')
    print(e)
    sys.exit()

app = FastAPI(
    title = settings.name_app,
    description = fastapi_description,
    version = settings.version,
    contact = {
        "name": "Mineev Alexander",
        "email": "mineev.buryat@gmail.com"
    }
)

@app.get('/', tags=['common'], response_model=Settings)
def api_settings(
    request: Request,
    settings: Settings = Depends(get_settings)):
    '''Показать текущие параметры fastapi сервиса
    Параметры меняются через переменные среды. Имя параметра соответсвует переменной среды.'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'api_settings', settings))
    return settings

@app.get('/info', tags=['common'], response_model=InfoDynamic)
def api_info(
    request: Request,
    info: InfoDynamic = Depends(get_status)):
    '''Показать динамические параметры fastapi сервиса
    Отображается текущий ip адрес, dns имя хоста и пр.'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'api_info', info))
    return info

@app.get('/conf_log', tags=['common'], response_model=LogSettings)
def log_config(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Показать конфигурацию логгера'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

############################# УТИЛИТЫ ################################################

# числительные
# {
#   "utterance": "сто тридцат шеcть двадцать пять и восемьдесят девить",
#   "only_numbers": 0
# }
@app.get('/util/number', tags=['number_manipulation'], response_model=LogSettings)
def word_to_number(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Преобразовать строку содержащие  числительные в список обнаруженых чисел'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

# время
@app.get('/util/time/partday', tags=['time_manipulation'], response_model=LogSettings)
def word_to_partday(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Преобразовать строку содержащее названое человеком часть суток в временной промежуток'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

@app.get('/util/time/time', tags=['time_manipulation'], response_model=LogSettings)
def word_to_time(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Преобразовать строку содержащее названое человеком время в часы и минуты'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

# дата
@app.get('/util/date/date', tags=['date_manipulation'], response_model=LogSettings)
def word_to_date(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Преобразовать строку содержащее названое человеком дату в день, месяц, год с учетом перехода между месяцами и годами'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf
# адрес
@app.get('/util/address/namespace', tags=['address_search'], response_model=LogSettings)
def has_address_namespace(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Показать имеющиеся в базе пространства имен для различных регионов.'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

@app.post('/util/address/namespace', tags=['address_search'], response_model=LogSettings)
def add_address_namespace(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Добавить новый регион с особенностями описания адресов'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

@app.post('/util/address/namespace/{space}', tags=['address_search'], response_model=LogSettings)
def add_address_sign(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Добавить в регион новый описательный признак адреса'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

@app.get('/util/address/search', tags=['address_search'], response_model=LogSettings)
def search_address(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Поиск конкретного адреса по названному человеком описательным признакам'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

# услуга
@app.get('/util/service/namespace', tags=['service_search'], response_model=LogSettings)
def has_service_namespace(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Показать имеющиеся в базе пространства имен услуг для различных регионов.'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

@app.get('/util/service/search', tags=['service_search'], response_model=LogSettings)
def search_service(
    request: Request,
    log_conf: LogSettings = Depends(get_log_conf)):
    '''Поиск конкретной услуги по названному человеком описательным признакам'''
    if settings.log_driver and logger_drv:
        logger_drv.debug(get_log_str(request, 'log_config', log_conf))
    return log_conf

if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host=settings.fastapi_host,
        port=settings.fastapi_port,
        access_log=False)


import logging
import time
import uuid

import pylsl as lsl

from pupil_labs.invisible_lsl_relay.channels import (
    pi_event_channels,
    pi_extract_from_sample,
    pi_gaze_channels,
)

VERSION = "1.0"

logger = logging.getLogger(__name__)

class PupilInvisibleOutlet:
    def __init__(
        self, channel_func, outlet_name, outlet_format, timestamp_query, outlet_uuid
    ):
        self._outlet_uuid = outlet_uuid
        self._channels = channel_func()
        self._outlet = pi_create_outlet(
            self._outlet_uuid, self._channels, outlet_name, outlet_format
        )
        self._timestamp_query = timestamp_query

    def push_sample_to_outlet(self, sample):
        try:
            sample_to_push = [chan.sample_query(sample) for chan in self._channels]
            timestamp_to_push = self._timestamp_query(sample) - get_lsl_time_offset()
        except Exception as exc:
            logger.error(f"Error extracting from sample: {exc}")
            logger.debug(str(sample))
            return
        self._outlet.push_sample(sample_to_push, timestamp_to_push)

class PupilInvisibleGazeOutlet(PupilInvisibleOutlet):
    def __init__(self, device_id=None):
        PupilInvisibleOutlet.__init__(
            self,
            channel_func=pi_gaze_channels,
            outlet_name='Gaze',
            outlet_format=lsl.cf_double64,
            timestamp_query=pi_extract_from_sample('timestamp_unix_seconds'),
            outlet_uuid=f'{device_id or str(uuid.uuid4())}_Gaze',
        )

class PupilInvisibleEventOutlet(PupilInvisibleOutlet):
    def __init__(self, device_id=None):
        PupilInvisibleOutlet.__init__(
            self,
            channel_func=pi_event_channels,
            outlet_name='Event',
            outlet_format=lsl.cf_string,
            timestamp_query=pi_extract_from_sample('timestamp_unix_seconds'),
            outlet_uuid=f'{device_id or str(uuid.uuid4())}_Event',
        )

def pi_create_outlet(outlet_uuid, channels, outlet_name, outlet_format):
    stream_info = pi_streaminfo(outlet_uuid, channels, outlet_name, outlet_format)
    return lsl.StreamOutlet(stream_info)

def pi_streaminfo(outlet_uuid, channels, type_name: str, channel_format):
    stream_info = lsl.StreamInfo(
        name=f"pupil_invisible_{type_name}",
        type=type_name,
        channel_count=len(channels),
        channel_format=channel_format,
        source_id=outlet_uuid,
    )
    stream_info.desc().append_child_value("pupil_invisible_lsl_relay_version", VERSION)
    xml_channels = stream_info.desc().append_child("channels")
    [chan.append_to(xml_channels) for chan in channels]
    return stream_info

def get_lsl_time_offset():
    return time.time() - lsl.local_clock()


import wawmembers.variables as v

'''
Converts numbers to text descriptions.
'''

def training_display(cur, maxt):
    try:
        ratio = float(cur)/float(maxt)
    except:
        ratio = 0
    if 0 <= ratio < 0.2:
        return 'Ragtag'
    elif 0.2 <= ratio < 0.4:
        return 'Disorganised'
    elif 0.4 <= ratio < 0.6:
        return 'Coordinated'
    elif 0.6 <= ratio < 0.8:
        return 'Well-trained'
    elif ratio >= 0.8:
        return 'Finely-honed'

def personalshipname(shiptype):
    if shiptype == 1:
        name = 'personal fighter'
    elif shiptype == 2:
        name = 'militarised yacht'
    elif shiptype == 3:
        name = 'command ship'
    else:
        name = None
    return name

def weariness_display(weariness):
    if weariness == 200:
        return 'Raring to go'
    elif 160 <= weariness < 200:
        return 'Fresh'
    elif 120 <= weariness < 160:
        return 'Ready'
    elif 80 <= weariness < 120:
        return 'Tired'
    elif 40 <= weariness < 80:
        return 'Weary'
    elif weariness < 40:
        return 'Exhausted'

def fleet_display(milinfo, sectorlist, main=True):
    output = []
    #determine highest tier ship to display
    highest = 0
    for sector in sectorlist:
        for ship in list(milinfo[sector]['totalships']._meta.fields)[v.fleetindex:]:
            if milinfo[sector]['totalships'].__dict__[ship.name] > 0:
                shipindex = list(milinfo[sector]['totalships']._meta.fields)[v.fleetindex:].index(ship)
                if shipindex > highest:
                    highest = shipindex
    #order is sector, training, weariness, base power, fuel cost and ships
    output.append(sectorlist)
    traininglist = ['Training']
    wearinesslist = ['Weariness']
    fleetslist = ['Fleets in Sector']
    powerlist = ['Base Power']
    fuellist = ['Base Fuel Cost']
    for sector in sectorlist:
        if milinfo[sector]['fleets'] is 0: #no ships present in sector
            traininglist.append('-')
            wearinesslist.append('-')
            fleetslist.append('-')
            powerlist.append('-')
            fuellist.append('-')
        else:
            traininglist.append(training_display(milinfo[sector]['current'], milinfo[sector]['max']))
            wearinesslist.append(weariness_display(milinfo[sector]['weariness'] / milinfo[sector]['fleets']))
            fleetslist.append(milinfo[sector]['fleets'])
            powerlist.append(milinfo[sector]['power'])
            fuellist.append(milinfo[sector]['fuelcost'])
    output.append(traininglist)
    output.append(wearinesslist)
    if main:
        output.append(fleetslist)
    output.append(powerlist)
    output.append(fuellist)
    shiplist = list(milinfo[sectorlist[0]]['totalships']._meta.fields)[v.fleetindex:]
    for sector in sectorlist: #sets sectors with 0 fleets to display a - instead of 0
        if milinfo[sector]['fleets'] is 0:
            for ship in shiplist:
                milinfo[sector]['totalships'].__dict__[ship.name] = '-'
    if main:
        for i in range (highest+1): #iterates through the tiers and appends the data as a list
            output.append([shiplist[i].name.replace('_', ' '),
                milinfo[sectorlist[0]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[1]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[2]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[3]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[4]]['totalships'].__dict__[shiplist[i].name]])
    else:
        for i in range (highest+1): #could probably be done in a more elegant way, but #notime
            output.append([shiplist[i].name.replace('_', ' '),
                milinfo[sectorlist[0]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[1]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[2]]['totalships'].__dict__[shiplist[i].name],
                milinfo[sectorlist[3]]['totalships'].__dict__[shiplist[i].name],])
    return output

#formats build information and amount for easy html rendering
def milpolicydisplay(world):
    costs = v.shipcosts(world.sector)
    iterlist = ['freighters']
    for element in v.tiers:
        iterlist.append(element.replace(' ', '_').lower() + 's')
        if element == world.techlevel:
            break #max tech level we can produce

    #now assemble a list of dictionaries, not a list of lists
    #because heidi didn't just
    #he absolutely had to add spans and shit
    displaylist = []
    for element in iterlist:
        #human friendly name
        appendage = {
            'name': element.replace('_', ' ').capitalize()[:len(element) - 1],
            'fname': element}
        appendage.update(costs[element])
        displaylist.append(appendage)
    return displaylist

#takes the queryset and prepares for easy display
def fleetmanagementdisplay(fleetlist, owner):
    renderlist = []
    for fleet in fleetlist:
        tmprender = []
        highest = highestship(fleet)
        if fleet.world == fleet.controller:
            tmprender.append(['Status', 'Owned'])
        elif fleet.world == owner:
            tmprender.append(['Status', fleet.controller])
        else:
            tmprender.append(['Status', fleet.world])
        tmprender.append(['Sector', fleet.sector.capitalize()])
        tmprender.append(['Training', training_display(fleet.training, fleet.maxtraining())])
        tmprender.append(['Weariness', weariness_display(fleet.weariness)])
        tmprender.append(['Base power', fleet.power()])
        tmprender.append(['Fuelcost', fleet.fuelcost()])
        for ship in v.shipindices:
            r1 = ship.replace('_', ' ').capitalize()
            r2 = fleet.__dict__[ship]
            tmprender.append([r1, r2])
            if ship == highest:
                break
        renderdict = {'name': fleet.name, 'fleetdata': tmprender, 'pk': fleet.pk}
        renderlist.append(renderdict)
    return renderlist

def fleetexchangedisplay(f, highest): #f stand sfor fleet
    renderlist = [] #THIS BE WHAT WE LOOP OVER LOL XD
    for ship in v.shipindices[:v.shipindices.index(highest)+1]:
        renderlist.append({'ship': ship.replace('_', ' ').capitalize(), 'form': "%s %s" % (f.pk, ship)})
    return renderlist

def highestship(f):
    highest = "freighters"
    for ship in v.shipindices:
        if f.__dict__[ship] > 0:
            highest = ship
    return highest

def longtimeformat(delta):
    days = delta.days
    timedeltaseconds = delta.seconds
    hours, remainder = divmod(timedeltaseconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    if days == 1:
        return '
Last seen: %(days)s day, %(hours)s hr ago
' % {'days':days,'hours':hours}
    elif days > 1:
        return '
Last seen: %s days ago
' % days
    elif hours == 0 and minutes < 10:
        return '
Online
'
    elif hours == 0:
        return '
Last seen: %s minutes ago
' % minutes
    else:
        return '
Last seen: %(hours)s hr %(minutes)s min ago
' % {'hours':hours,'minutes':minutes}

def lastonline(world):
    lastonline = world.lastloggedintime
    delta = v.now() - lastonline
    return longtimeformat(delta)

policyname = {
    'continental': True,
    'Africa': ['response'],
    'Latin America': ['response'],
    'Asia': ['response',
                'response2',
                'response3'],
    'Middle East': ['response']
}


# Iterative way of Depth first graph traversal
# limited only by system  memory

import unittest
from graph import Graph

class DFS:
    def __init__(self, g):
        self.g = g

    def dfs(self, v):
        data = []
        stack = [v]
        done = [False for i in range(self.g.V)]
        while stack:
            node = stack.pop()
            data.append(node)
            done[node] = True
            for each in self.g.adj(node):
                if not done[each]:
                    stack.append(each)
        return data

class TestDFS(unittest.TestCase):
    def test_dfs(self):
        g = Graph(10)
        g.add_edge(1, 2)
        g.add_edge(2, 4)
        g.add_edge(2, 6)
        g.add_edge(7, 8)
        g.add_edge(1, 8)
        self.assertListEqual(DFS(g).dfs(1), [1, 8, 7, 2, 6, 4])

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


# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 16:56:26 2019

@author: marie
"""

dicti={0:'me',1:'Rick',2:'Jamie',3:'Heather',4:'Bryan',5:'Ray',6:'Bas',7:'Patrick',8:'Derek',9:'Joshua'}
Relations=[[0,2,4],[0,1,2],[0,4,0],[0,5,0],[0,7,2],[0,8,0],[0,6,0],[0,9,0],[0,3,8],[1,4,3],[1,6,3],
           [4,6,0],[4,5,0],[5,6,3],[5,7,5],[7,6,7],[6,9,0],[6,3,0],[6,2,0],[8,3,2],[9,3,6],[3,2,9]]
jj=[]
kk=[]
print("The three highest people involved in some kind of direct communication with others in the network.")
for i in range(len(dicti)):
   strength=0# reset the strength to zero for each iterartion
   for value in range(len(Relations)):
        mm=Relations[value]#mm to acess list
        if i==mm[0] or i==mm[1] : #now acess the value in the list of list
            strength=strength+mm[2] #accumulate strength fo each person wheter on any side[0,2,2] or [2,0,2]
   jj.append(strength)
final_list2 = [] # create a list to append values to it

for k in range(0, 3):  # range 3 because i want the highest 3
        max1 = 0# set maximum value to zero for each iteration

        for j in range(len(jj)):
            if jj[j] > max1: #compare to get highest value
                max1 = jj[j];
                new=j #once found ,save the index
                max2 =dicti[j] # now get the name related to this index
        jj[new]=0# replace the maximumvalue with zero so that it can't be found again
        final_list2.append(max2) #append names to the list

print(final_list2)


# coding: utf-8

import math
import copy
import torch
from torch import nn
from torch.nn import functional

from .pretrained import resnet18, resnet18_pre, resnet34, resnet34_pre, resnet50, resnet50_pre
from .BasicModule import BasicModule

class ResNet18(BasicModule):
    def __init__(self, num_classes):
        super(ResNet18, self).__init__()

        # train from scratch
        self.conv1 = resnet18.conv1
        self.bn1 = resnet18.bn1
        self.relu = resnet18.relu
        self.maxpool = resnet18.maxpool
        self.layer1 = resnet18.layer1
        self.layer2 = resnet18.layer2
        self.layer3 = resnet18.layer3
        self.layer4 = resnet18.layer4
        self.avgpool = resnet18.avgpool
        # self.avgpool = nn.AvgPool2d(14, stride=1)
        self.fc = nn.Linear(512, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

        # pre-trained
        # self.conv1 = resnet18_pre.conv1
        # self.bn1 = resnet18_pre.bn1
        # self.relu = resnet18_pre.relu
        # self.maxpool = resnet18_pre.maxpool
        # self.layer1 = resnet18_pre.layer1
        # self.layer2 = resnet18_pre.layer2
        # self.layer3 = resnet18_pre.layer3
        # self.layer4 = resnet18_pre.layer4
        # self.avgpool = resnet18_pre.avgpool
        # # self.avgpool = nn.AvgPool2d(14, stride=1)
        # self.fc = nn.Linear(512, num_classes)

    def forward(self, x):
        feature = self.conv1(x)
        feature = self.bn1(feature)
        feature = self.relu(feature)
        feature = self.maxpool(feature)

        feature = self.layer1(feature)
        feature = self.layer2(feature)
        feature = self.layer3(feature)
        feature = self.layer4(feature)

        feature = self.avgpool(feature)
        feature = feature.view(feature.size(0), -1)
        out = self.fc(feature)
        return out

    def addfeatures(self):
        self.features = nn.Sequential(self.conv1, self.bn1, self.relu, self.maxpool,
                                      self.layer1, self.layer2, self.layer3, self.layer4,
                                      self.avgpool)
        self.classifier = nn.Sequential(self.fc)

class SkipResNet18(BasicModule):
    def __init__(self, num_classes):
        super(SkipResNet18, self).__init__()

        # train from scratch
        self.conv1 = resnet18.conv1
        self.bn1 = resnet18.bn1
        self.relu = resnet18.relu
        self.maxpool = resnet18.maxpool
        self.layer1 = resnet18.layer1
        self.layer2 = resnet18.layer2
        self.layer3 = resnet18.layer3
        self.layer4 = resnet18.layer4

        self.adapool = nn.AdaptiveAvgPool2d(output_size=(7, 7))
        self.adaconv = nn.Conv2d(in_channels=1024, out_channels=512, kernel_size=1, stride=1)

        self.avgpool = resnet18.avgpool
        self.fc = nn.Linear(512, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        feature = self.conv1(x)
        feature = self.bn1(feature)
        feature = self.relu(feature)
        feature = self.maxpool(feature)
        skip_1 = self.adapool(feature)

        feature = self.layer1(feature)
        skip_2 = self.adapool(feature)

        feature = self.layer2(feature)
        skip_3 = self.adapool(feature)

        feature = self.layer3(feature)
        skip_4 = self.adapool(feature)

        feature = self.layer4(feature)

        feature = torch.cat((skip_1, skip_2, skip_3, skip_4, feature), 1)
        feature = self.adaconv(feature)

        feature = self.avgpool(feature)
        feature = feature.view(feature.size(0), -1)
        out = self.fc(feature)
        return out

class DensResNet18(BasicModule):
    def __init__(self, num_classes):
        super(DensResNet18, self).__init__()

        # train from scratch
        self.conv1 = resnet18.conv1
        self.bn1 = resnet18.bn1
        self.relu = resnet18.relu
        self.maxpool = resnet18.maxpool
        self.layer1 = resnet18.layer1
        self.layer2 = resnet18.layer2
        self.layer3 = resnet18.layer3
        self.layer4 = resnet18.layer4

        self.adapool56 = nn.AdaptiveAvgPool2d(output_size=(56, 56))
        self.adapool28 = nn.AdaptiveAvgPool2d(output_size=(28, 28))
        self.adapool14 = nn.AdaptiveAvgPool2d(output_size=(14, 14))
        self.adapool7 = nn.AdaptiveAvgPool2d(output_size=(7, 7))
        self.adaconv1 = nn.Conv2d(in_channels=64+64, out_channels=64, kernel_size=1, stride=1)
        self.adaconv2 = nn.Conv2d(in_channels=64+64+128, out_channels=128, kernel_size=1, stride=1)
        self.adaconv3 = nn.Conv2d(in_channels=64+64+128+256, out_channels=256, kernel_size=1, stride=1)
        self.adaconv4 = nn.Conv2d(in_channels=64+64+128+256+512, out_channels=512, kernel_size=1, stride=1)

        self.avgpool = resnet18.avgpool
        self.fc = nn.Linear(512, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        feature = self.conv1(x)
        feature = self.bn1(feature)
        feature = self.relu(feature)
        f0 = self.maxpool(feature)

        f1 = self.layer1(f0)
        feature = torch.cat((self.adapool56(f0), f1), 1)
        feature = self.adaconv1(feature)

        f2 = self.layer2(feature)
        feature = torch.cat((self.adapool28(f0), self.adapool28(f1), f2), 1)
        feature = self.adaconv2(feature)

        f3 = self.layer3(feature)
        feature = torch.cat((self.adapool14(f0), self.adapool14(f1), self.adapool14(f2), f3), 1)
        feature = self.adaconv3(feature)

        f4 = self.layer4(feature)
        feature = torch.cat((self.adapool7(f0), self.adapool7(f1), self.adapool7(f2), self.adapool7(f3), f4), 1)
        feature = self.adaconv4(feature)

        feature = self.avgpool(feature)
        feature = feature.view(feature.size(0), -1)
        out = self.fc(feature)
        return out

class GuideResNet18(BasicModule):
    def __init__(self, num_classes):
        super(GuideResNet18, self).__init__()

        # train from scratch
        self.conv1 = resnet18.conv1
        self.bn1 = resnet18.bn1
        self.relu = resnet18.relu
        self.maxpool = resnet18.maxpool
        self.layer1 = resnet18.layer1
        self.layer2 = resnet18.layer2
        self.layer3 = resnet18.layer3
        self.layer4 = resnet18.layer4

        # self.upconv1 = nn.ConvTranspose2d(in_channels=512, out_channels=128, kernel_size=3, stride=2, padding=1)
        # self.upconv2 = nn.ConvTranspose2d(in_channels=512, out_channels=128, kernel_size=3, stride=2, padding=1)
        self.upconv1 = nn.Conv2d(in_channels=512, out_channels=128, kernel_size=1, stride=1)
        self.upconv2 = nn.Conv2d(in_channels=512, out_channels=128, kernel_size=1, stride=1)
        self.downconv1 = nn.Conv2d(in_channels=256, out_channels=128, kernel_size=1, stride=1)
        self.downconv2 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=2, padding=1)

        self.resblock1 = copy.deepcopy(resnet18.layer4)
        self.resblock2 = copy.deepcopy(resnet18.layer4)

        self.avgpool = resnet18.avgpool
        self.fc = nn.Linear(1024, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        feature = self.conv1(x)
        feature = self.bn1(feature)
        feature = self.relu(feature)
        feature = self.maxpool(feature)

        feature = self.layer1(feature)
        feature = self.layer2(feature)
        f2 = feature
        feature = self.layer3(feature)
        f3 = feature
        feature = self.layer4(feature)

        up1 = self.upconv1(feature)
        up1 = functional.interpolate(up1, size=(14, 14), mode='bilinear', align_corners=False)
        down1 = self.downconv1(f3)
        up2 = self.upconv2(feature)
        up2 = functional.interpolate(up2, size=(14, 14), mode='bilinear', align_corners=False)
        down2 = self.downconv2(f2)

        guide_feature1 = self.resblock1(torch.cat((up1, down1), 1))
        guide_feature2 = self.resblock2(torch.cat((up2, down2), 1))

        feature = self.avgpool(torch.cat((guide_feature1, guide_feature2), 1))
        feature = feature.view(feature.size(0), -1)
        out = self.fc(feature)
        return out

class ResNet34(BasicModule):
    def __init__(self, num_classes):
        super(ResNet34, self).__init__()

        # train from scratch
        self.conv1 = resnet34.conv1
        self.bn1 = resnet34.bn1
        self.relu = resnet34.relu
        self.maxpool = resnet34.maxpool
        self.layer1 = resnet34.layer1
        self.layer2 = resnet34.layer2
        self.layer3 = resnet34.layer3
        self.layer4 = resnet34.layer4
        self.avgpool = resnet34.avgpool
        self.fc = nn.Linear(512, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        feature = self.conv1(x)
        feature = self.bn1(feature)
        feature = self.relu(feature)
        feature = self.maxpool(feature)

        feature = self.layer1(feature)
        feature = self.layer2(feature)
        feature = self.layer3(feature)
        feature = self.layer4(feature)

        feature = self.avgpool(feature)
        feature = feature.view(feature.size(0), -1)
        out = self.fc(feature)
        return out

class ResNet50(BasicModule):
    def __init__(self, num_classes):
        super(ResNet50, self).__init__()

        # train from scratch
        self.conv1 = resnet50.conv1
        self.bn1 = resnet50.bn1
        self.relu = resnet50.relu
        self.maxpool = resnet50.maxpool
        self.layer1 = resnet50.layer1
        self.layer2 = resnet50.layer2
        self.layer3 = resnet50.layer3
        self.layer4 = resnet50.layer4
        self.avgpool = resnet50.avgpool
        self.fc = nn.Linear(2048, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        feature = self.conv1(x)
        feature = self.bn1(feature)
        feature = self.relu(feature)
        feature = self.maxpool(feature)

        feature = self.layer1(feature)
        feature = self.layer2(feature)
        feature = self.layer3(feature)
        feature = self.layer4(feature)

        feature = self.avgpool(feature)
        feature = feature.view(feature.size(0), -1)
        out = self.fc(feature)
        return out


import pandas as pd
import numpy as np
import scipy as sc
from scipy import interpolate

import ReadDustMap as dust

def colorFunction(g,Teff,wavelength):
    X = np.array(g['teff50'])
    Y = np.array(g['ratio_'+str(wavelength)+'A'])
    function = sc.interpolate.interp1d(X,Y, fill_value = 'extrapolate')
    return function(Teff)

def extinctionRatio(wavelength,extinction_Function):
    ext = extinction_Function.loc[extinction_Function['wavelength']==wavelength]['Alambda_over_A_V']
    return float(ext)

def Vegaflux(HIPvsSH):
    if HIPvsSH == 'SH':
        #flux in the gaia band pass, svo
        return 2.50386e-9
    if HIPvsSH == 'HIP':
        #flux in the  hipparcos band pass, svo
        return 3.76938e-9

def exponent(HIPvsSH,filename,co,interpolated_dustMap,Alambda_overAv):
    pdtable = pd.read_pickle(filename)
    Av , norms = dust.getAv(filename,co,interpolated_dustMap) #dust Extinction
    Av = 1/2*Av
    if HIPvsSH == 'SH':
        absMag = pdtable['mg0'] #in the g band
        expo = np.array( (-0.4)*(absMag - 5 + 5*np.log10(norms) + Alambda_overAv*Av  ) )

    if HIPvsSH == 'HIP':
        absMag = pdtable['absMag_HIP'] #in the hipparcos band
        expo = np.array( (-0.4)*(absMag - 5 + 5*np.log10(norms) + Alambda_overAv*Av  ) )

    return expo #this is a numpy array

def importFiles(HIPvsSH,pdtable,cwd):
    extinction_Function = pd.read_pickle(cwd+'/inputs/extinctionFunction')
    Teff = pdtable['teff50']

    if HIPvsSH == 'SH':
        g = pd.read_pickle(cwd+'/inputs/ColourFunctionStarHorse.pkl')

    if HIPvsSH == 'HIP':
        g = pd.read_pickle(cwd+'/inputs/ColourFunctionHipparcos.pkl')
    return extinction_Function, g, Teff

def getFlux(HIPvsSH,filename, wavelength,co,interpolated_dustMap,cwd):
    pdtable = pd.read_pickle(filename)
    extinction_Function, g, Teff = importFiles(HIPvsSH,pdtable,cwd)

    Alambda_overAv = extinctionRatio(wavelength,extinction_Function)
    expon = exponent(HIPvsSH, filename,co,interpolated_dustMap,Alambda_overAv)

    g_lambda = colorFunction(g,Teff,wavelength)
    F_Vega_detect = Vegaflux(HIPvsSH)
    F_lambda = F_Vega_detect * g_lambda * np.exp( np.log(10) * expon )
    return F_lambda #this is a numpy array


import os
from logging import Logger
from typing import Union, Tuple, Sequence, Optional

import lightgbm as lgb
import matplotlib.pyplot as plt
import numpy as np
import optuna
import pandas as pd
from lightgbm import early_stopping, log_evaluation
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold

from utils.func_utils import create_empty_folder
from .predictor import Predictor

class LGBMPredictor(Predictor):
    def __init__(self, logger: Logger, log_folder: str, name: str = None, cat_feature_names: Optional['list[str]'] = None,
                 drop_feature_names: Optional['list[str]'] = None, override_logs: bool = True, use_random_search: bool = False,
                 task_type: str = 'CPU', perform_feature_analysis: bool = True):
        # avoids mutable arguments, set to an empty list by default if not provided
        if drop_feature_names is None:
            drop_feature_names = []
        if cat_feature_names is None:
            cat_feature_names = []

        # generate a relevant name if not set
        if name is None:
            name = f'LGBM_{task_type}_{"rs" if use_random_search else "default"}'

        super().__init__(logger, log_folder, name, override_logs)

        self.use_random_search = use_random_search
        self.perform_feature_analysis = perform_feature_analysis
        self.task_type = task_type

        self.feature_names = None  # type: list
        self.drop_feature_names = drop_feature_names
        self.label_name = None
        self.drop_column_indexes = None
        self.cat_feature_names = cat_feature_names
        self.cat_feature_indexes = []

    def __get_max_b(self, df: pd.DataFrame):
        return df['blocks'].max()

    def __setup_features_data(self, df: pd.DataFrame):
        # y to predict is always the first column in POPNAS case
        self.label_name = df.columns.values.tolist()[0]

        # drop label and specified columns
        df = df.drop(columns=self.drop_feature_names)
        self.feature_names = df.columns.values.tolist()

        self.cat_feature_indexes = [i for i, f_name in enumerate(self.feature_names) if f_name in self.cat_feature_names]

    def __objective(self, trial: optuna.Trial, X, y):
        param_grid = {
            #         "device_type": trial.suggest_categorical("device_type", ['gpu']),
            "objective": "regression",
            "metric": "l2",
            "n_estimators": trial.suggest_categorical("n_estimators", [2000, 4000]),
            "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3),
            "num_leaves": trial.suggest_int("num_leaves", 20, 300, step=10),
            "max_depth": trial.suggest_int("max_depth", 3, 9),
            "min_data_in_leaf": trial.suggest_int("min_data_in_leaf", 10, 20),
            "max_bin": trial.suggest_int("max_bin", 200, 300),
            "lambda_l1": trial.suggest_float("lambda_l1", 0, 5, step=0.2),
            "min_gain_to_split": trial.suggest_float("min_gain_to_split", 0, 15),
            "bagging_fraction": trial.suggest_float(
                "bagging_fraction", 0.2, 0.9, step=0.1
            ),
            "bagging_freq": trial.suggest_categorical("bagging_freq", [1]),
            "feature_fraction": trial.suggest_float(
                "feature_fraction", 0.2, 0.9, step=0.1
            ),
        }

        cv = KFold(n_splits=5, shuffle=True, random_state=1121218)

        cv_scores = np.empty(5)
        for idx, (train_idx, test_idx) in enumerate(cv.split(X, y)):
            X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
            y_train, y_test = y[train_idx], y[test_idx]

            model = lgb.LGBMRegressor(**param_grid)
            model.fit(
                X_train,
                y_train,
                eval_set=[(X_test, y_test)],
                eval_metric="l2",
                callbacks=[
                    early_stopping(100),
                    log_evaluation(100)
                ],
            )
            preds = model.predict(X_test)
            cv_scores[idx] = mean_squared_error(y_test.values, preds)

        return np.mean(cv_scores)

    def train_ensemble(self, dataset: Union[str, 'list[tuple]'], splits: int = 5, use_data_augmentation=True):
        raise NotImplementedError("LGBM predictor doesn't support ensemble")

    def train(self, dataset: Union[str, 'list[Tuple]'], use_data_augmentation=True):
        # TODO
        if not isinstance(dataset, str):
            raise TypeError('LGBM supports only files, conversion to file is a TODO...')

        dataset_df = pd.read_csv(dataset)
        actual_b = self.__get_max_b(dataset_df)
        if self.feature_names is None:
            self.__setup_features_data(dataset_df)

        dataset_df = dataset_df.drop(columns=self.drop_feature_names)

        x = dataset_df.drop(columns=[self.label_name])
        y = dataset_df[self.label_name]

        train_ds = lgb.Dataset(x, y)
        train_log_folder = os.path.join(self.log_folder, f'B{actual_b}')
        create_empty_folder(train_log_folder)

        # train the model with random search
        if self.use_random_search:
            study = optuna.create_study(direction="minimize", study_name="LGBM Regressor")
            study.optimize(lambda trial: self.__objective(trial, x, y), n_trials=20)

            # LightGBMTunerCV(params, train_ds, folds=KFold(n_splits=5), callbacks=[early_stopping(100), log_evaluation(100)], return_cvbooster=True)

            self._logger.info("Best value (mse): %0.3f", study.best_value)
            self._logger.info("\tBest params:")

            for key, value in study.best_params.items():
                self._logger.info(f"\t\t{key}: {value}")

            self._logger.info("Training model with all samples, with best parameters found")
            self.model = lgb.train(study.best_params, train_ds)
        # else simply train the model with default parameters
        else:
            self.model = lgb.train({'objective': 'regression'}, train_ds)
            self._logger.info('LGBM training complete')

        if self.perform_feature_analysis:
            fig, ax = plt.subplots()
            lgb.plot_importance(self.model, ax)
            fig.savefig(os.path.join(train_log_folder, 'feature_importance.png'), bbox_inches='tight')

    def predict(self, x: list) -> float:
        # make sure categorical features are integers (and not float, pandas converts them to floats like 1.0)
        if self.cat_feature_indexes is not None:
            for cat_index in self.cat_feature_indexes:
                x[cat_index] = int(x[cat_index])

        # returned as a numpy array of single element
        return self.model.predict([x])[0]

    def predict_batch(self, x: 'Sequence[list]') -> np.ndarray:
        # make sure categorical features are integers (and not float, pandas converts them to floats like 1.0)
        if self.cat_feature_indexes is not None:
            for sample in x:
                for cat_index in self.cat_feature_indexes:
                    sample[cat_index] = int(sample[cat_index])

        # returned as a numpy array of single element
        preds = self.model.predict(x)  # type: np.ndarray
        return preds.reshape(-1)

    def prepare_prediction_test_data(self, file_path: str) -> 'tuple[list[Union[str, list[tuple]]], list[list], list[list[float]]]':
        dataset_df = pd.read_csv(file_path)
        max_b = self.__get_max_b(dataset_df)
        self.__setup_features_data(dataset_df)

        dataset_paths = []
        prediction_samples = []
        real_values = []

        for b in range(1, max_b):
            train_df = dataset_df[dataset_df['blocks'] <= b]
            save_path = os.path.join(self.log_folder, f'inputs_B{b}.csv')
            train_df.to_csv(save_path, index=False)
            dataset_paths.append(save_path)

            predictions_df = dataset_df[dataset_df['blocks'] == (b + 1)]
            # in datasets that use data augmentation, filter them in prediction phase
            if 'data_augmented' in predictions_df.columns:
                # use == False, otherwise it will not work properly
                predictions_df = predictions_df[dataset_df['data_augmented'] == False]

            real_values.append(predictions_df[self.label_name].values.tolist())
            predictions_df = predictions_df.drop(columns=[self.label_name] + self.drop_feature_names)
            prediction_samples.append(predictions_df.values.tolist())

        return dataset_paths, prediction_samples, real_values


from django.db import migrations

def add_group(apps, _schema_editor):
    Group = apps.get_model("auth", "Group")
    Group.objects.create(name="Grade publisher", pk=3)

class Migration(migrations.Migration):

    dependencies = [
        ('grades', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(add_group),
    ]


# 要求：顺序并且居中对齐输出以下内容
poem = ["登鹳雀楼",
        "王之涣",
        "白日依山尽",
        "黄河入海流",
        "欲穷千里目",
        "更上一层楼"]
for poem_str in poem:
    print("|%s|"%poem_str.center(10,"　"))

# ljust向左对齐
# rjust向右对齐

# 去除空白字符
# 假设：以下内容是从网络上抓取的
# 要求：顺序并且居中对齐输出以下内容
poem = ["\t\n登鹳雀楼",
        "王之涣",
        "白日依山尽\t\n",
        "黄河入海流",
        "欲穷千里目",
        "更上一层楼"]
for poem_str in poem:
    # 先使用strip方法去除字符串中的空白字符
    # 再使用center方法居中显示文本
    print("|%s|"%poem_str.strip().center(10,"　"))


from arm.logicnode.arm_nodes import *

class TranslateOnLocalAxisNode(ArmLogicTreeNode):
    """Translates (moves) the given object using the given vector in the local coordinates."""
    bl_idname = 'LNTranslateOnLocalAxisNode'
    bl_label = 'Translate On Local Axis'
    arm_section = 'location'
    arm_version = 1

    def arm_init(self, context):
        self.add_input('ArmNodeSocketAction', 'In')
        self.add_input('ArmNodeSocketObject', 'Object')
        self.add_input('ArmFloatSocket', 'Speed')
        self.add_input('ArmIntSocket', 'Forward/Up/Right')
        self.add_input('ArmBoolSocket', 'Inverse')

        self.add_output('ArmNodeSocketAction', 'Out')


""" Batch class for radial regime regression. """

import random
import numpy as np
import scipy as sc

from sklearn.ensemble import IsolationForest

from . import radial_batch_tools as bt
from .decorators import init_components
from ..batchflow import Batch, action, inbatch_parallel, any_action_failed, FilesIndex, R

class RadialBatch(Batch):
    """
    Batch class that stores multiple time series along with other parameters
    for radial flow regime regression.

    Parameters
    ----------
    index : DatasetIndex
        Unique identifiers of rig data in the batch.
    preloaded : tuple, optional
        Data to put in the batch if given. Defaults to ``None``.

    Attributes
    ----------
    index : DatasetIndex
        Unique identifiers of rig data in the batch.
    """
    def __init__(self, index, *args, preloaded=None, **kwargs):
        super().__init__(index, preloaded, *args, **kwargs)
        self.time = self.array_of_nones
        self.derivative = self.array_of_nones
        self.target = self.array_of_nones
        self.predictions = self.array_of_nones

    components = "time", "derivative", "target"

    @property
    def array_of_nones(self):
        """ 1-D ndarray: ``NumPy`` array with ``None`` values."""
        return np.array([None] * len(self.index))

    def _reraise_exceptions(self, results):
        """ Reraise all exceptions in the ``results`` list.

        Parameters
        ----------
        results : list
            Post function computation results.

        Raises
        ------
        RuntimeError
            If any paralleled action raised an ``Exception``.
        """
        if any_action_failed(results):
            all_errors = self.get_errors(results)
            raise RuntimeError("Cannot assemble the batch", all_errors)

    @action
    def load(self, src=None, fmt=None, components=None, *args, **kwargs):
        """ Load given batch components.

        This method supports loading of data from npz format.

        Parameters
        ----------
        src: str, optional
             an array with data
        fmt : str, optional
            Source format.
        components : str or array-like, optional
            Components to load.

        Returns
        -------
        batch : RadialBatch
            Batch with loaded components. Changes batch data inplace.
        """
        _ = args, kwargs
        if isinstance(components, str):
            components = list(components)
        if components is None:
            components = self.components

        if fmt is None:
            self.put_into_data(src, components)
            self._sort(components)
        else:
            self._load(fmt, components)

        return self

    @inbatch_parallel(init='indices')
    def _sort(self, index, components):
        i = self.get_pos(None, 'time', index)
        time_mask = np.argsort(getattr(self, 'time')[i])
        for component in components:
            data = getattr(self, component)[i]
            getattr(self, component)[i] = data[time_mask]

    @inbatch_parallel(init="indices", post="_assemble_load", target="threads")
    def _load(self, indice, fmt=None, components=None, *args, **kwargs):
        """ Load given components from file.

        Parameters
        ----------
        fmt : str, optional
            Source format.
        components : iterable, optional

        Returns
        -------
        batch : RadialBatch
            Batch with loaded components. Changes batch data inplace.

        Raises
        ------
        ValueError
            If source path is not specified and batch's ``index`` is not a
            ``FilesIndex``.
        """
        loaders = {'npz': bt.load_npz}

        if isinstance(self.index, FilesIndex):
            path = self.index.get_fullpath(indice)  # pylint: disable=no-member
        else:
            raise ValueError("Source path is not specified")

        return loaders[fmt](path, components, *args, **kwargs)

    def _assemble_load(self, results, *args, **kwargs):
        """ Concatenate results of different workers and update ``self``.

        Parameters
        ----------
        results : list
            Workers' results.

        Returns
        -------
        batch : RadialBatch
            Assembled batch. Changes components inplace.
        """
        _ = args, kwargs
        self._reraise_exceptions(results)
        components = kwargs.get("components", None)
        if components is None:
            components = self.components
        for comp, data in zip(components, zip(*results)):
            data = np.array(data + (None,))[:-1]
            if comp == 'target':
                data = np.array([d for d in data])
            setattr(self, comp, data)
        return self

    @action
    @init_components
    @inbatch_parallel(init="indices", target="threads")
    def drop_negative(self, index, src=None, dst=None, **kwargs):
        """ Leaves only positive values of derivative. src should be a list with
        two components name. First component must contain time values, second - derivative.

        Parameters
        ----------
        src : list of str len 2, optional
            list with components name where the first component name is a component with
            time, second with derivative.
        dst : list of str len 2, optional
            list with components name to put the result in the same order as src.

        Raises
        ------
        ValueError
            If all values in derivative component are negative.
        """
        _ = kwargs
        i = self.get_pos(None, src[0], index)
        time = getattr(self, src[0])[i]
        derivative = getattr(self, src[1])[i]
        mask = derivative > 0

        if sum(mask) == 0:
            raise ValueError("All values in derivative {} are negative!".format(index))

        getattr(self, dst[0])[i] = time[mask]
        getattr(self, dst[1])[i] = derivative[mask]
        return self

    @action
    @init_components
    @inbatch_parallel(init="indices", target="threads")
    def drop_outliers(self, index, contam=0.1, src=None, dst=None, **kwargs):
        """Drop outliers using Isolation Forest algorithm. src should be a list with
        two components name. First component must contain time values, second - derivative.

        Parameters
        ----------
        contam : float (from 0 to 0.5)
            The amount of contamination of the data set.
        src : list of str len 2, optional
            list with components name where the first component name is a component with
            time, second with derivative.
        dst : list of str len 2, optional
            list with components name to put the result in the same order as src.
        """
        _ = kwargs
        i = self.get_pos(None, src[0], index)

        derivative = getattr(self, src[1])[i]
        time = getattr(self, src[0])[i]

        x_data = np.array([derivative]).T
        isol = IsolationForest(contamination=contam).fit(x_data)
        pred = isol.predict(x_data)
        getattr(self, dst[0])[i] = time[pred == 1]
        getattr(self, dst[1])[i] = derivative[pred == 1]
        return self

    @action
    @init_components
    @inbatch_parallel(init="indices", target="threads")
    def get_samples(self, index, n_points, sampler=None, # pylint: disable=too-many-arguments
                    src=None, dst=None, interpolate='linear', seed=None, **kwargs):
        """ Draws samples from the interpolation of time and derivative components.

        Performs interpolation of the `derivative` on time using
        `scipy.interpolate.interp1d`, samples `n_points` within `time` component
        range, and calculates values of `derivative` in sampled points.
        Sampler should return values from [0, 1] interval, which later is
        stretched to the range on `time` component. Size of the sample equals
        `(1, n_points)`.

        Parameters
        ----------
        n_points : int
            Size of a sample.
        sampler : function or named expression
            Method to sample points within [0, 1] range.
            If callable, it should only take `size` as an argument.
        src : list of str len 2, optional
            list with components name where the first component name is a component with
            time, second with derivative.
        dst : list of str len 2, optional
            list with components name to put the result in the same order as src.
        interpolate : str, optional
            Specifies the kind of interpolation as a string
            ('linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic',
            'previous', 'next', where 'zero', 'slinear', 'quadratic' and 'cubic'
            refer to a spline interpolation of zeroth, first, second or third
            order; 'previous' and 'next' simply return the previous or next value
            of the point) or as an integer specifying the order of the spline
            interpolator to use.
        seed : int, optional
            Numpy seed to be fixed. Defaults to None.
        kwargs : misc
            Named arguments to the sampling
            or interpolation functions.

        Returns
        -------
        batch : RadialBatch
            Batch with changed `time` and `derivative`
            components. Changes components inplace.

        Note
        ----
        If sampler is name expression and it has a following parameters:
        `tmin`, `tmax`, 'size' and others. Than this params should be passed via kwargs.
        See ``beta_sampler`` method for exampleн.
        """
        _ = kwargs
        if seed:
            np.random.seed(seed)

        i = self.get_pos(None, src[0], index)
        time = getattr(self, src[0])[i]
        derivative = getattr(self, src[1])[i]

        interpolater = sc.interpolate.interp1d(time, derivative, kind=interpolate,
                                               bounds_error=True, assume_sorted=True)

        if  isinstance(sampler, R):
            sampler = R(sampler.name, **sampler.kwargs, size=(1, n_points))
            samples = sampler.get()
        elif callable(sampler):
            samples = sampler(size=(1, n_points))
        else:
            raise ValueError("You should specify sampler function!")

        tmin, tmax = np.min(time), np.max(time)
        sample_times = samples * (tmax - tmin) + tmin
        sample_times.sort(axis=-1)
        sample_derivatives = interpolater(sample_times)

        getattr(self, dst[0])[i] = np.array(sample_times)
        getattr(self, dst[1])[i] = np.array(sample_derivatives)
        return self

    @action
    @init_components
    def make_points(self, src=None, dst=None, **kwargs):
        """ Generated new component with name `dst` with value from `src`.

        Parameters
        ----------
        src : list of str len 2, optional
            list with component' name where the first component name is a component with
            time, second with derivative.
        dst : str, optional
            component' name to put data.
        """
        _ = kwargs
        points = []
        for comp in src:
            points.append(np.vstack(getattr(self, comp)))
        if not isinstance(dst, str):
            dst = dst[0]
        setattr(self, dst, np.array(list(zip(*points))).transpose(0, 2, 1))
        return self

    @action
    def make_target(self, src='target', **kwargs):
        """ Reshaped component co vector with shape = (-1, 1)."""
        _ = kwargs
        setattr(self, src, getattr(self, src).reshape(-1, 1))
        return self

    @action
    @init_components
    @inbatch_parallel(init='indices')
    def normalize(self, ix, src=None, dst=None, src_range=None, dst_range=None, **kwargs):
        """ Normalizes data element-wise to range [0, 1] by exctracting
        min(data) and dividing by (max(data)-min(data)).
        Parameters
        ----------
        src : list of either str or None
            Contains names of batch component(s) to be normalized. First component name is a
            component with time, second with derivative.
        dst : list of either str or None
            Contains names of batch component(s) where normalized components will be stored
            in the same order as src..
        dst_range : list of either str or None
            Contains names of batch component(s) where range of normalized componentes will
            be stored.
            Range is saved as a list of 2 elements:
            `[min(component_data), max(component_data) - min(component_data)].`
            if None then no range will be stored.
        src_range : list of either str or None
            Contains names of batch component(s) where range of normalized componentes have
            already been stored. If not None then component will be normalized with
            the given range.
        Returns
        -------
        self

        """
        _ = kwargs
        for i, component in enumerate(src):
            pos = self.get_pos(None, component, ix)
            comp_data = getattr(self, component)[pos]
            if src_range[i]:
                min_value, scale_value = getattr(self, src_range[i])[pos]
            else:
                min_value, scale_value = np.min(comp_data), (np.max(comp_data) - np.min(comp_data))
            new_data = (comp_data - min_value) / scale_value
            getattr(self, dst[i])[pos] = new_data

            if dst_range[i]:
                getattr(self, dst_range[i])[pos] = [min_value, scale_value]
        return self

    @action
    @init_components
    @inbatch_parallel(init='indices')
    def make_grid_data(self, ix, src=None, dst=None, grid_size=500, **kwargs):
        """ Makes grid
        Parameters
        ----------
        src : str or list of str
            Contains names of batch component(s) to make grid data from in format (x, y)
        dst : str or list of str
            Contains names of batch component(s) where (x_grid, y_grid) will be stored.
        grid_size : int
            Number of points in a grid

        Returns
        -------
        self
        """
        _ = kwargs
        if not (len(src) == 2 and len(dst) == 2):
            raise ValueError('Exactly two components must be passed to make grid data')

        pos = self.get_pos(None, src[0], ix)

        x_data = getattr(self, src[0])[pos]
        y_data = getattr(self, src[1])[pos]

        sorted_data = list(zip(*sorted(zip(x_data, y_data), key=lambda x: x[0])))

        x_grid = np.linspace(0, 1, num=grid_size)
        y_grid = np.interp(x_grid, sorted_data[0], sorted_data[1])

        getattr(self, dst[0])[pos] = x_grid
        getattr(self, dst[1])[pos] = y_grid.reshape((-1, 1))
        return self

    @action
    @init_components
    @inbatch_parallel(init='indices')
    def denormalize(self, ix, src=None, dst=None, src_range=None, **kwargs):
        """ Denormalizes component to initial range.

        Parameters
        ----------
        src : list of str
            Contains names of batch component(s) to be denormalized
        dst : list of either str or None
            Contains names of batch component(s) where denormalized components will be stored.
        src_range : list of str
            Contains names of batch component(s) where range data has been stored in format
            `[min(component_data), max(component_data) - min(component_data)].`

        Returns
        -------
        self
        """
        _ = kwargs

        for i, component in enumerate(src):
            pos = self.get_pos(None, component, ix)
            comp_data = getattr(self, component)[pos]
            if src_range[i]:
                min_value, scale_value = getattr(self, src_range[i])[pos]
                new_data = comp_data * scale_value + min_value
                getattr(self, dst[i])[pos] = new_data
            else:
                raise ValueError('Src_range must be provided to denormalize component')
        return self

    @action
    @init_components
    @inbatch_parallel(init='indices')
    def to_log10(self, ix, src=None, dst=None, **kwargs):
        """Takes a decimal logarithm from `src` and saves the resulting value to `dst`

        Parameters
        ----------
        src : src or list
            Name of the component with data
        dst : src or list
            Name of the component to save the result
        """
        _ = kwargs
        if isinstance(src, str):
            src = [src]
            dst = [dst]
        for i, component in enumerate(src):
            pos = self.get_pos(None, component, ix)
            comp_data = getattr(self, component)[pos]
            getattr(self, dst[i])[pos] = np.log10(comp_data)
        return self

    @action
    @init_components
    @inbatch_parallel(init='indices')
    def clip_values(self, ix, src, dst, **kwargs):
        """Clip values from `src` to 0, 1 and save it to `dst`
        """
        _ = kwargs
        if isinstance(src, str):
            src = [src]
            dst = [dst]

        for i, component in enumerate(src):
            pos = self.get_pos(None, component, ix)
            pred = getattr(self, component)[pos]
            getattr(self, dst[i])[pos] = np.clip(pred, 0, 1)
        return self

    @action
    @init_components
    def make_array(self, src=None, dst=None, **kwargs):
        """ Stacks a sequence of arrays in a given component.
        """
        _ = kwargs
        for i, component in enumerate(src):
            setattr(self, dst[i], np.stack(getattr(self, component)))
        return self

    @action
    @init_components
    def expand_dims(self, src=None, dst=None, **kwargs):
        """ Expands the shape of an array stored in a given component
        Inserts a new axis that will appear as the last axis.
        Acts like np.expand_dims with parameter axis=-1.
        Parameters
        ----------
        src : list of str
            Contains names of batch component(s) to be modiified
        dst : list of either str or None
            Contains names of batch component(s) where modiified components will be stored
        Returns
        -------
        self
        """
        _ = kwargs
        for i, component in enumerate(src):
            setattr(self, dst[i], getattr(self, component).reshape((-1, 1)))
        return self

    @action
    def hard_negative_sampling(self, statistics_name=None, fraction=0.5):
        """ Recreate batch with new indices corresponding to the largest loss
        value on the previous iterations.
        Parameters
        ----------
        statistics_name : str
            name of the pipeline variable where the dict with the index and
            corresponding loss values is stored.
        fraction : float in [0, 1]
            A fraction of the hard negative examples in the new batch.
        Returns
        -------
        self
        """
        btch_size = len(self.indices)
        if statistics_name and isinstance(self.pipeline.get_variable(statistics_name), dict):
            loss_history_dict = self.pipeline.get_variable(statistics_name)
            sorted_by_value = sorted(loss_history_dict.items(), key=lambda kv: kv[1])
            hard_count = int(btch_size * fraction)
            hard_indices = np.setdiff1d(np.array([x[0] for x in sorted_by_value]), self.indices)[:hard_count]
            new_index = np.hstack([np.random.choice(self.indices, size=btch_size - hard_count, replace=False), \
                                   hard_indices])
            random.shuffle(new_index)
            batch = RadialBatch(index=self.pipeline.dataset.index.create_subset(new_index))
            return batch
        return self

    @action
    def update_loss_history_dict(self, src='loss_history', dst='loss_history_dict'):
        """ Update pipeline variable dst that is dict with key - element's index and
        corresponding value, i.e. squarred error on that element from src
        Parameters
        ----------
        src : str
            name of the pipeline variable where element-wise statistics
            (e.g. unaggreagated loss) for the batch is stored.
            It can be a list or np.array of length batch_size.
        dst : str
            name of the pipeline variable where the dict with the index and
            corresponding values is stored.
        Returns
        -------
        self
        """
        new_loss_history_dict = dict(zip(self.index.indices, self.pipeline.get_variable(src)))
        if isinstance(self.pipeline.get_variable(dst), dict):
            new_loss_history_dict = {**new_loss_history_dict, **self.pipeline.get_variable(dst)}
        self.pipeline.update_variable(dst, new_loss_history_dict, mode='w')
        return self


def _fileset_impl(ctx):
    srcs = depset(order = "postorder", transitive = [src.files for src in ctx.attr.srcs])

    remap = {}
    for a, b in ctx.attr.maps.items():
        remap[ctx.label.relative(a)] = b

    cmd = ""
    for f in ctx.files.srcs:
        label = f.owner if f.is_source else f.owner.relative(f.basename)
        if label in remap:
            dest = remap[label]
            fd = ctx.actions.declare_file(dest)
            cmd += "mkdir -p " + fd.dirname + "\n"
            cmd += "cp -f '" + f.path + "' '" + fd.path + "'\n"

    script = ctx.actions.declare_file(ctx.label.name + ".cmd.sh")
    ctx.actions.write(output = script, content = cmd)

    # Execute the command
    ctx.actions.run_shell(
        inputs = (
            ctx.files.srcs +
            [script]
        ),
        outputs = ctx.outputs.outs,
        mnemonic = "fileset",
        command = "set -e; sh " + script.path,
        use_default_shell_env = True,
    )

_fileset = rule(
    attrs = {
        "maps": attr.string_dict(
            mandatory = True,
            allow_empty = False,
        ),
        "outs": attr.output_list(
            mandatory = True,
            allow_empty = False,
        ),
        "srcs": attr.label_list(allow_files = True),
    },
    executable = False,
    implementation = _fileset_impl,
)

def fileset(name, srcs = [], mappings = {}, tags = [], **kwargs):
    outs = []
    maps = {}
    rem = []
    for src in srcs:
        done = False
        for prefix, destination in mappings.items():
            if src.startswith(prefix):
                f = destination + src[len(prefix):]
                maps[src] = f
                outs.append(f)
                done = True
        if not done:
            rem.append(src)

    if outs:
        _fileset(
            name = name + ".map",
            srcs = srcs,
            maps = maps,
            outs = outs,
            tags = tags,
        )

    native.filegroup(
        name = name,
        srcs = outs + rem,
        tags = tags,
        **kwargs
    )


class Employee():
    empCount = 0 #initialization of the data member
    empSal = [];
    def __init__(self,name,family,salary,department): #default constructor
        self.empname = name
        self.empfamily = family
        self.empsalary = salary
        self.empdepartment = department
        Employee.empCount +=1  #counts the number of employees
        Employee.empSal.append(salary)  #appends salaray attribute

    def avg_salary(self):
        print('the average salary is')
        sumSal = 0;
        for sal in Employee.empSal:
            sumSal = sumSal+ int(sal);
        return sumSal/len(Employee.empSal)

class FulltimeEmployee(Employee):
    def _init_(self,name,family,salary,department):
        Employee._init_(self,name,family,salary,department)

emp1 = FulltimeEmployee('prasad','kada','75000','Sales');
emp2 = FulltimeEmployee('elijah','micheal','80000','FInance');
emp3 = FulltimeEmployee('taylor','summers','65000','Marketing');
emp4 = FulltimeEmployee('alex','jones','78000','sales');
print(FulltimeEmployee.empCount)
print(FulltimeEmployee.empSal)
avgSal = FulltimeEmployee.avg_salary(Employee);
print(avgSal)


import os
import json

filepath = os .getcwd()

with open(os.path.join(filepath, 'data', 'concept_text.json'), 'r', encoding='utf-8') as f:
    data = json.load(f)

def get_keyword_profile(o_name):
    s = ''
    name = o_name.replace(' ', '_')
    try:
        for i in data[name]:
            dir = {}
            st = "
" + str(i)+"
"+" \

" + str(data[name][i])+"
"
            s += st
            dir[str(i)] = str(data[name][i])
            text = str(data[name][i])
    except:
        text = 'nodefine'

    return {'data': [o_name], 'define': [text]}


# Given the head of a singly linked list, reverse the list,
# and return the reversed list.

# Input: head = [1,2,3,4,5]
# Output: [5,4,3,2,1]

# Input: head = [1,2]
# Output: [2,1]

# Input: head = []
# Output: []

from ListsAndTrees import LinkedList

class Solution:
    def fetchElement(self, head, depth = float('inf')):
        i = 0
        dummy = head
        while dummy.next and i < depth:
            i += 1
            dummy = dummy.next
        return dummy, i

    def helper(self, head, depth=float('inf')):
        if depth==0:
            head.next = None # Cut the last element
            return head
        dummy = head
        head, depth = self.fetchElement(head, depth)
        head.next = self.helper(dummy, depth-1)
        return head

    def reverseList(self, head):
        if not head:
            return head
        if head.next is None:
            return head
        return self.helper(head)

S = Solution()

#Input 1:
l = [1,2,3,4,5]
L = LinkedList()
L.insert(l)

print('\nInput:')
print(L)
L.head = S.reverseList(L.head)
print('\nAfter:')
print(L)

#Input 2:
l = [1,2]
L = LinkedList()
print(L)

print('\nInput:')
print(L)
L.head = S.reverseList(L.head)
print('\nOutput:')
print(L)

#Input 3:
L = LinkedList()
L.insert(l)

print('\nInput:')
print(L)
L.head = S.reverseList(L.head)
print('\nOutput:')
print(L)


class GridGame():

    def __init__(self):
        self.moves = 0
        self.gridarray = [[False for j in range(3)]for i in range(3)]

    def createTable(self):
        document.writeln('
')
        for i in range(3):
            document.writeln('
')
            for j in range(3):
                document.write('      ')
                document.writeln(' 	')
            document.writeln('
')
        document.writeln('
')

    def getPosition(self, pos):
        self.moves = self.moves + 1
        self.updateMoves()
        x = int(pos.id[0])
        y = int(pos.id[1])
        self.gridarray[x][y] = not self.gridarray[x][y]
        # toggle the neighboring cells
        if x>0: self.gridarray[x-1][y] = not self.gridarray[x-1][y]
        if x<2: self.gridarray[x+1][y] = not self.gridarray[x+1][y]
        if y>0: self.gridarray[x][y-1] = not self.gridarray[x][y-1]
        if y<2: self.gridarray[x][y+1] = not self.gridarray[x][y+1]
        self.do_lighting()

    def do_lighting(self):
        countwhite = 0
        for x in range(3):
            for y in range(3):
                cell = document.getElementById(str(x)+str(y))
                if self.gridarray[x][y]:
                    cell.bgColor="white"
                    countwhite += 1
                else:
                    cell.bgColor="grey"
        if countwhite == 9:
            alert("You have managed to turn all lights on! Congratulations")

    def restart(self):
        self.moves = 0
        self.gridarray = [[0 for j in range(3)]for i in range(3)]
        self.updateMoves()
        for x in range(3):
            for y in range(3):
                cell = document.getElementById(str(x)+str(y))
                cell.bgColor="grey"

    def updateMoves(self):
        document.getElementById("moves").innerText = "Moves: "+str(self.moves)

gridGame = GridGame()


# https://leetcode.com/problems/robot-room-cleaner/
# tags: #design, #google, #ordered_set, #segment_tree
#
# Solution: Binary search
# https://leetcode.com/problems/range-module/discuss/244194/Python-solution-using-bisect_left-bisect_right-with-explanation
# Python slicing: https://stackoverflow.com/questions/509211/understanding-slice-notation
# Time complexity: addRange O(n) - removeRange O(n) - queryRange O(log(n)), space complexity: (n)
# n: number of disjoint intervals currently stored
import bisect

class RangeModule:

    def __init__(self):
        self.track = []

    def addRange(self, left: int, right: int) -> None:
        start = bisect.bisect_left(self.track, left)
        end = bisect.bisect_right(self.track, right)

        sub_track = []
        if start % 2 == 0:
            sub_track.append(left)
        if end % 2 == 0:
            sub_track.append(right)

        self.track[start: end] = sub_track

    def queryRange(self, left: int, right: int) -> bool:
        left = bisect.bisect_right(self.track, left)
        right = bisect.bisect_left(self.track, right)

        return left == right and left % 2 == 1

    def removeRange(self, left: int, right: int) -> None:
        start = bisect.bisect_left(self.track, left)
        end = bisect.bisect_right(self.track, right)

        sub_track = []
        if start % 2 == 1:
            sub_track.append(left)
        if end % 2 == 1:
            sub_track.append(right)

        self.track[start: end] = sub_track

if __name__ == '__main__':
    range_module = RangeModule()
    range_module.addRange(10, 20)
    range_module.removeRange(14, 16)
    print(range_module.queryRange(10, 14))  # True
    print(range_module.queryRange(13, 15))  # False
    print(range_module.queryRange(16, 17))  # True


import numpy as np
import pytest
from sklearn.utils.estimator_checks import parametrize_with_checks

import random_tree_models.decisiontree as dtree
import random_tree_models.gradientboostedtrees as gbt
import random_tree_models.xgboost as xgboost

class TestXGBoostTemplate:
    model = xgboost.XGBoostTemplate()

    def test_tree_(self):
        assert not hasattr(self.model, "trees_")

    def test_fit(self):
        try:
            self.model.fit(None, None)
        except NotImplementedError as ex:
            pytest.xfail("XGBoostTemplate.fit expectedly refused call")

    def test_predict(self):
        try:
            self.model.predict(None)
        except NotImplementedError as ex:
            pytest.xfail("XGBoostTemplate.predict expectedly refused call")

class TestXGBoostRegressor:
    X = np.array(
        [
            [-1, -1],
            [1, -1],
            [1, 1],
            [-1, 1],
        ]
    )
    y = np.array([0.0, 0.0, 1.0, 1.0])

    def test_fit(self):
        model = xgboost.XGBoostRegressor()
        model.fit(self.X, self.y)
        assert all(
            [
                isinstance(model, dtree.DecisionTreeRegressor)
                for model in model.trees_
            ]
        )

    def test_predict(self):
        model = xgboost.XGBoostRegressor()
        model.fit(self.X, self.y)
        predictions = model.predict(self.X)
        assert np.allclose(predictions, self.y)

class TestXGBoostClassifier:
    model = xgboost.XGBoostClassifier()

    X = np.array(
        [
            [-1, -1],
            [1, -1],
            [1, 1],
            [-1, 1],
        ]
    )
    y = np.array([False, False, True, True])

    def test_classes_(self):
        assert not hasattr(self.model, "classes_")

    def test_fit(self):
        model = xgboost.XGBoostClassifier()
        model.fit(self.X, self.y)
        assert not hasattr(self.model, "classes_")
        assert all(
            [
                isinstance(model, dtree.DecisionTreeRegressor)
                for model in model.trees_
            ]
        )

    def test_predict(self):
        model = xgboost.XGBoostClassifier()
        model.fit(self.X, self.y)
        predictions = model.predict(self.X)
        assert (predictions == self.y).all()

@pytest.mark.slow
@parametrize_with_checks(
    [xgboost.XGBoostRegressor(), xgboost.XGBoostClassifier()]
)
def test_xgboost_estimators_with_sklearn_checks(estimator, check):
    """Test of estimators using scikit-learn test suite

    Reference: https://scikit-learn.org/stable/modules/generated/sklearn.utils.estimator_checks.parametrize_with_checks.html#sklearn.utils.estimator_checks.parametrize_with_checks
    """
    check(estimator)

@pytest.mark.parametrize(
    "y_float, start_estimate_exp",
    [
        (np.array([-1.0, 1.0]), 0),
        (np.array([-1.0, 1.0, 1.0, 1.0]), 0.5493061443340549),
        (np.array([-1.0, -1.0, -1.0, 1.0]), -0.5493061443340549),
        (np.array([True, True, False, False]), None),
        (np.array([-2.0, -2.0, 2.0, 2.0]), None),
    ],
)
def test_compute_start_estimate_binomial_loglikelihood(
    y_float: np.ndarray, start_estimate_exp: float
):
    try:
        # line to test
        start_estimate = xgboost.compute_start_estimate_binomial_loglikelihood(
            y_float
        )
    except ValueError as ex:
        if start_estimate_exp is None:
            pytest.xfail(f"expectedly failed for non -1 and 1 values")
        else:
            raise ex
    else:
        if start_estimate_exp is None:
            pytest.fail(f"unexpectedly passed for non -1 and 1 values")
        assert np.isclose(start_estimate, start_estimate_exp)

@pytest.mark.parametrize(
    "y,start_estimate,g_exp",
    [
        (np.array([1.0]), 0.5, np.array([0.5])),
        (np.array([1.0, 1.0]), 0.5, np.array([0.5, 0.5])),
    ],
)
def test_compute_derivatives_negative_least_squares(
    y: np.ndarray, start_estimate: float, g_exp: np.ndarray
):
    # line to test
    g, h = xgboost.compute_derivatives_negative_least_squares(y, start_estimate)

    assert g.shape == h.shape
    assert np.allclose(g, g_exp)
    assert np.allclose(h, -1)

@pytest.mark.parametrize(
    "y_float,start_estimate,g_exp,h_exp",
    [
        (
            np.array([-1.0, 1.0]),
            0.0,
            np.array([-1.0, 1.0]),
            np.array([-1.0, -1.0]),
        ),
        (
            np.array([-1.0, -1.0, 1.0, 1.0]),
            0.0,
            np.array([-1.0, -1.0, 1.0, 1.0]),
            np.array([-1.0, -1.0, -1.0, -1.0]),
        ),
        # failure cases
        (np.array([False, True]), 0.0, None, None),
        (np.array([-2.0, 2.0]), 0.0, None, None),
    ],
)
def test_compute_derivatives_binomial_loglikelihood(
    y_float: np.ndarray,
    start_estimate: float,
    g_exp: np.ndarray,
    h_exp: np.ndarray,
):
    yhat = np.ones_like(y_float) * start_estimate
    is_bad = g_exp is None and h_exp is None
    try:
        # line to test
        g, h = xgboost.compute_derivatives_binomial_loglikelihood(y_float, yhat)
    except ValueError as ex:
        if is_bad:
            pytest.xfail("Expectedly failed for incorrect y_float values")
        else:
            raise ex
    else:
        if is_bad:
            pytest.fail("Unexpectedly passed for incorrect y_float values")
        assert g.shape == h.shape
        assert np.allclose(g, g_exp)
        assert np.allclose(h, h_exp)


import numpy as np
import cv2
import boto3
import copy
import numpy as np

from os import listdir
from os.path import isfile, join

def getFaces(img):
    face_cascade = cv2.CascadeClassifier('classifiers/haarcascade_frontalface_alt2.xml')
    profile_cascade = cv2.CascadeClassifier('classifiers/haarcascade_profile.xml')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces_profile = profile_cascade.detectMultiScale(gray, 1.3, 5)
    faces_front   = face_cascade.detectMultiScale(gray,1.3, 5)
    faces = [x for x in faces_profile]
    faces += [x for x in faces_front]
    return faces

def showFaces(img, faces):
    for (x,y,w,h) in faces:
        #print (x,y,w,h)
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        crop_img = img[y:y+h, x:x+w]
        cv2.imshow('crop',crop_img)

        #roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
    cv2.imshow('img',img)

def wanted(suspect_path,crop_image,info_path):
    wanted_path = "wantedTemplate/wanted.jpeg"
    wanted_image = cv2.imread(wanted_path)
    lenghtWanted = len(wanted_image[0])
    heightWanted = len(wanted_image)

    suspect_image = cv2.imread(suspect_path)
    suspect_image = cv2.resize(suspect_image,( ((lenghtWanted*80)//100) , ((heightWanted*78)//100) ))
    lenghtSuspect = len(suspect_image[0])
    heightSuspect = len(suspect_image)

    wanted_image[int(heightWanted*.2):int(heightWanted*.2) + heightSuspect,int(lenghtWanted*.1):int(lenghtWanted*.1) + lenghtSuspect] = suspect_image

    font = cv2.FONT_HERSHEY_SIMPLEX
    crop_image = cv2.resize(crop_image,(lenghtWanted,lenghtWanted))

    cv2.putText(crop_image,'Found',(10,len(crop_image)), font, 3,(255,255,255),2,cv2.LINE_AA)
    faces = np.concatenate((wanted_image, crop_image), axis=0)

    text = np.zeros((len(faces), (len(faces)//2), 3), np.uint8)
    text[:] = (255,255,255)

    with open(info_path) as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    pos = 0
    for line in content:
        pos+=50
        cv2.putText(text,line,(10,pos), font, 1,(0,0,0),2,cv2.LINE_AA)

    full_image = np.concatenate((faces, text), axis=1)

    cv2.imshow("found",full_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


from django.urls import path
from . import views

urlpatterns = [
    path('', views.JobListView.as_view(), name='services-home'),
    path('technician/<str:username>', views.UserJobListView.as_view(), name='technician-jobs'),
    path('job/by_title/', views.TitleJobListView.as_view(), name='title-jobs'),
    path('job/by_period/', views.PeriodJobListView.as_view(), name='title-jobs'),
    path('job/<int:pk>/', views.JobDetailView.as_view(), name='job-detail'),
    path('job/new/', views.JobCreateView.as_view(), name='job-create'),
    path('job/<int:pk>/update/', views.JobUpdateView.as_view(), name='job-update'),
    path('job/<int:pk>/delete/', views.JobDeleteView.as_view(), name='job-delete'),
    path('about/', views.about, name='services-about'),
]


import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',9999))
print(s.recv(1024).decode('utf-8'))
data = [b'Michael', b'Tracy', b'Sarah']
for d in data:
	s.send(d)
	print(s.recv(1024).decode('utf-8'))
s.send(b'exit')
s.close()


"""Defines the enemy class"""

class Enemy():
    """An enemy, goes from state 0 -> 1 -> 2 -> 1 -> repeats"""
    def __init__(self, positions) -> None:
        # By copying state 1 (the mid point), we don't have to do any advanced logic
        self.positions = positions + [positions[1]]
        self.current_position = 1

    def step(self) -> tuple:
        """Take the next step and return the position"""
        self.current_position += 1
        if self.current_position >= len(self.positions):
            self.current_position = 0
        return self.positions[self.current_position]

    def __repr__(self) -> str:
        # This is so the class prints out nicely
        return f'Enemy({self.positions[:-1]})'

    def check_if_near_point(self, x: int, y: int, close=3) -> bool:
        """Check if the enemy is near a point"""
        e_x, e_y = self.positions[self.current_position]
        if abs(e_x - x) <= close and abs(e_y - y) <= close:
            return True
        return False

if __name__ == '__main__':
    # manual testing
    a = Enemy([1, 2, 3])
    print(a.positions)
    print(a.step())
    print(a)


from individual import INDIVIDUAL
import copy
import random

class POPULATION:
	def __init__(self, popSize):
		self.p = {}    # creates an empty population
		self.popSize = popSize  # stores the number of individuals in the population to be initialized
	def Initialize(self):
		for i in range(0, self.popSize):
			self.p[i] = INDIVIDUAL(i)
	def Print(self):
		for i in self.p:
			if(i in self.p): # evaluates to True if there is an entry in the dictionary p with a key equal to i
				self.p[i].Print()
	def Evaluate(self, pb):
		for i in self.p: # this loop starts the evaluation of the individual
			self.p[i].Start_Evaluate(pb) # True activates 'play_blind' runs the robots without drawing
		for i in self.p: # this loop gives the fitness of each individual
			self.p[i].Compute_Fitness()
	def Mutate(self):
		for i in self.p:
			self.p[i].Mutate()
	def Replacewith(self, other):
		for i in self.p:
			if (self.p[i].fitness < other.p[i].fitness):
				self.p[i] = other.p[i]

	def Copy_Best_From(self, other): # copy the best individual in parents and place it into the first element of children
			self.fitnessvar = {}
			for i in other.p:
				self.fitnessvar[i] = other.p[i].fitness
			ID = max(xrange(len(self.fitnessvar)), key = lambda x: self.fitnessvar[x])
			max_value = max(self.fitnessvar)
			self.p[0] = copy.deepcopy(other.p[ID])
			del self.fitnessvar

	def Winner_Of_Tournament_Selection(other): # this method will select the best individual among the parents
		p1 = random.randint(0, len(other.p)-1) # randomly going to select the index of parents to compete with each other
		p2 = random.randint(0, len(other.p)-1)
		# make sure that p1 is different than p2
		while p1 == p2:
			p2 = random.randint(0, len(other.p)-1)
			p1 = random.randint(0, len(other.p)-1)
		if(other.p[p1] > other.p[p2]): # if the value on p1 is higher than the value on p2, then other.p[p1] won the tournament
			return other.p[p1]
		else:
			return other.p[p2]

	def Collect_Children_From(self, other): # simply fill the elements in children with other values of parents
		for i in range(1, len(other.p)):
			winner = other.Winner_Of_Tournament_Selection() #fill the elements of children with the best individuals of parents
			self.p[i] = copy.deepcopy(winner) # fill the elements just by copying the elements from parents

	def Fill_From(self, other): # calls the methods in order to fill the elements of children
		self.Copy_Best_From(other)
		#self.Print()
		self.Collect_Children_From(other)
		#self.Print()


import PySimpleGUI as sg
import os
import subprocess
import platform

pf = platform.system()
if 'Darwin' != pf:
    print(f"あなたが使用しているOSは {pf} ですが,\nこれはMac専用のプログラムのため何もせず終了します.")
    input("\n続行するにはなにかキーを押してください.")
    exit(0)

# PySimpleGUIの確認
freeze_output = subprocess.run("pip3 freeze", capture_output=True, text=True, shell=True).stdout
if not ("PySimpleGUI" in freeze_output):
    subprocess.run("pip3 install PySimpleGUI", shell=True)  # 無ければインストール

# ホームディレクトリへのパス
path_to_HOME = os.environ['HOME']

# アーキテクチャの確認
arch = subprocess.run("arch", capture_output=True, text=True, shell=True).stdout.replace("\n", "")

def Homebrew():
    """Homebrewがインストールされてるかチェックする関数"""
    if arch == "arm64":
        path_to_brew = "/opt/homebrew/bin"  # ファイルチェック
    else:
        path_to_brew = "/usr/local/bin/brew"

    if os.path.exists(path_to_brew):
        comment = "Homebrewは(おそらく)インストール済みです."
    else:
        cmd = '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"'
        subprocess.run(cmd, shell=True)
        comment = "Homebrewはインストールされていなかったので,インストールしておきました."

    return comment

def Homebrewecho():
    """Homebrewのzshrcの設定状況をチェックする関数"""
    if arch != "arm64":
        return "あなたの使っているMacは古いので, Homebrewの設定は必要ありません.\n(M1では設定が必要です.)"

    path_to_zshrc = path_to_HOME + "/.zshrc"   # ファイルチェック
    if not os.path.exists(path_to_zshrc):   # 無ければ作って
        with open(path_to_zshrc, mode='w+') as f:
            f.write('export PATH="/opt/homebrew/bin:$PATH"\n\n')
            return "そもそもなんも作業してないでしょう？こちらで設定しておきました."  # return

    # .zshrcが存在する状況で
    with open(path_to_zshrc, mode='r') as f:  # 既存ファイル読み取り
        path_check = False
        for line in f:
            if 'export PATH="/opt/homebrew/bin:$PATH"' in line:  # 設定チェック
                path_check = True
    if path_check:
        return 'Homebrewは(おそらく)ちゃんと設定されています.\n他の不具合がある場合は,ターミナルを開いて\n      $ open -t ~/.zshrc \nを実行して\n【export PATH="/opt/homebrew/bin:$PATH"】\nという行が書き込まれているか自分で確認してください'
    else:
        with open(path_to_zshrc, mode='a') as f:  # 設定がなければ書き込んでしまう
            f.write('\nexport PATH="/opt/homebrew/bin:$PATH"\n\n')
        return 'export PATH=????? が設定されていなかったので設定しておきました.\nもし他の不具合がある場合は,ターミナルを開いて\n      $ open -t ~/.zshrc \nを実行して\n【export PATH="/opt/homebrew/bin:$PATH"】\nという行が書き込まれているか自分で確認してください'

def Brave():
    """Braveがインストールされているかチェックする関数"""
    # ファイルチェック
    cmd = "ls /Applications"
    output = subprocess.run(cmd, capture_output=True, text=True, shell=True).stdout
    if "Brave" in output:
        return "Braveはインストール済みです."
    else:
        cmd = "brew install --cask brave-browser"
        subprocess.run(cmd, shell=True)
        return "Braveがインストールされていなかったので,こちらでやっておきました."

def Java():
    """JDKがインストールされてるかチェックする関数"""
    if arch == "arm64":
        path_to_jdk = "/opt/homebrew/opt/openjdk/libexec/openjdk.jdk"  # PATHチェック
    else:
        path_to_jdk = "/usr/local/opt/openjdk/libexec/openjdk.jdk"

    if not os.path.exists(path_to_jdk):
        cmd = "brew install openjdk"
        subprocess.run(cmd, shell=True)
        return "Javaがインストールされていなかったので,インストールしておきました."
    else:
        # javacで確認
        javac = f"{path_to_jdk}/Contents/Home/bin/javac --version"
        proc = subprocess.run(javac, shell=True)
        if proc.returncode != 0:
            cmd = "brew install openjdk"
            subprocess.run(cmd, shell=True)
            return "Javaがインストールされていなかったので,インストールしておきました."
        else:
            return "Javaはちゃんとインストールされています."

def JavaSetting():
    # Javaの設定確認1
    # リンクのチェック
    path_to_link = "/Library/Java/JavaVirtualMachines/openjdk.jdk"
    if arch == "arm64":
        path_to_jdk = "/opt/homebrew/opt/openjdk/libexec/openjdk.jdk"  # PATHチェック
    else:
        path_to_jdk = "/usr/local/opt/openjdk/libexec/openjdk.jdk"

    if not os.path.exists(path_to_link):
        print("Macにログインするときのパスワードを入力してください.")
        cmd = f"sudo ln -sfn {path_to_jdk} {path_to_link}"
        subprocess.run(cmd, shell=True)
        comment = "「Java を書きやすくする設定 1」は正しく実行できていませんでしたので,\nこちらでやっておきました.\n\n"
    else:
        comment = "「Java を書きやすくする設定 1」は正しく実行されています.\n\n"

    # Javaの設定確認2
    # .zshrcのチェック
    # JAVA_HOMEの設定
    path_to_zshrc = path_to_HOME + "/.zshrc"
    if not os.path.exists(path_to_zshrc):
        with open(path_to_zshrc, mode='w+') as f:
            f.write(
                '\nexport JAVA_HOME="/Library/Java/JavaVirtualMachines/openjdk.jdk"\nexport PATH="${JAVA_HOME}/Contents/Home/bin:${PATH}"\n\n')
            comment = comment + \
                'そもそもなんもzshrcの設定がなされていないです.\n【export JAVA_HOME="/Library/Java/JavaVirtualMachines/openjdk.jdk"】\n【export PATH="${JAVA_HOME}/Contents/Home/bin:${PATH}"】の2行 はこちらで書き込みました.\n\n'
    else:  # JAVA_HOMEの設定 (.zshrc自体はある)
        with open(path_to_zshrc, mode='r') as f:  # 読み込み
            javahome_check = False  # 内容のチェック
            for line in f:
                if 'export JAVA_HOME="/Library/Java/JavaVirtualMachines/openjdk.jdk"' in line:
                    javahome_check = True
        if javahome_check:
            comment = comment + \
                '「Java を書きやすくする設定 2」は(おそらく)正しく行われています.\n\nもし他の不具合がある場合は,ターミナルを開いて\n      open -t ~/.zshrc \nを実行して中身をチェックしてみてください.\n\n'
        else:
            with open(path_to_zshrc, mode='a') as f:   # 追記
                f.write('\nexport JAVA_HOME="/Library/Java/JavaVirtualMachines/openjdk.jdk"\n')
                comment = comment + '「Java を書きやすくする設定 2」は正しく実行されていませんでした.\n【export JAVA_HOME="/Library/Java/JavaVirtualMachines/openjdk.jdk"】 という行をこちらで書き込みました.\nもし他の不具合がある場合は,ターミナルを開いて\n      open -t ~/.zshrc \nを実行して中身をチェックしてみてください.\n\n'

    # Javaの設定確認3
    # PATHを通す export PATH="${{JAVA_HOME}}/Contents/Home/bin:$PATH"
    with open(path_to_zshrc, mode='r') as f:  # .zshrcのチェック
        path_check = False
        for line in f:
            if 'export PATH="${JAVA_HOME}/Contents/Home/bin:${PATH}"' in line:
                path_check = True
    if not path_check:
        with open(path_to_zshrc, mode='a') as f:  # 追記
            f.write('\nexport PATH="${JAVA_HOME}/Contents/Home/bin:${PATH}"\n')
            comment = comment + \
                'Java を書きやすくする設定 3. は実行されていませんでした.\n【export PATH="${JAVA_HOME}/Contents/Home/bin:${PATH}"】 という行をこちらで書き込みました.\nもし他の不具合がある場合は,ターミナルを開いて\n      open -t ~/.zshrc \nを実行して中身をチェックしてみてください.'
    else:
        comment = comment + \
            '「Java を書きやすくする設定 3」は(おそらく)正しく行われています.\nもし他の不具合がある場合は,ターミナルを開いて\n      open -t ~/.zshrc \nを実行して中身をチェックしてみてください.'

    return comment

questions = {
    'Homebrew': 'Homebrewはちゃんとインストールされてる？',
    'Homebrewecho': 'Homebrewインストール後の\n【 echo \'export PATH="/opt/homebrew/bin:$PATH"\' >> ${HOME}/.zshrc 】\nはちゃんと入力できてる？',
    'Brave': 'Brave(Webブラウザ)はインストールされてる？',
    'Java': 'Javaはインストールされてる？',
    'JavaSetting': 'Javaを書きやすくする設定はちゃんと出来てる？'}

msg = """
これは「低学年が高専沼にどっぷりハマるための Mac OS 初期設定 」の答え合わせプログラムです.

特に初心者が「うまく行っているか自分でわかりにくい」ターミナルでの設定の答え合わせを行います.

↓ から答え合わせをしたい項目を選んでください
"""
sg.theme('Dark Brown')

# 位置
left = 0
right = 0
top = 50

bottom = 50
layout = [
    [sg.Text(msg, size=(80, 10), font=('Arial', 15))],
    [sg.Checkbox('Homebrewはちゃんとインストールされてる？', default=True, font=('Arial', 15), key='Homebrew')],
    [sg.Checkbox('Homebrewインストール後の【 echo \'export PATH???】はちゃんと入力できてる？', font=('Arial', 15), key='Homebrewecho')],
    [sg.Checkbox('Brave(Webブラウザ)はインストールされてる？', font=('Arial', 15), key='Brave')],
    [sg.Checkbox('Javaはインストールされてる？', font=('Arial', 15), key='Java')],
    [sg.Checkbox('Javaを書きやすくする設定はちゃんと出来てる？', font=('Arial', 15), key='JavaSetting')],
    [sg.Push(), sg.Button('答え合わせする', font=('Arial', 15), size=(30, 2), pad=((left, right), (top, bottom)), key='-Btn-'), sg.Push()]
]

window = sg.Window('Mac初期設定答え合わせプログラム', layout)

while True:

    event, value = window.read()  # イベントの入力を待つ

    if event == '-Btn-':
        for q in value:
            if value[q]:
                func = eval(q)
                comment = func()
                sg.popup(comment, font=('Noto Serif CJK JP', 18))
        break

    elif event is None:
        break

window.close()


#
# @lc app=leetcode.cn id=77 lang=python3
#
# [77] 组合
#

# @lc code=start
class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        res = []
        def back(i, k , tmp):
            if k == 0:
                res.append(tmp)
                return
            for j in range(i, n + 1):
                back(j+1, k-1, tmp+[j])
        back(1, k, [])
        return res

# @lc code=end


from datetime import timedelta
from airflow.models import Variable

def get_default_dag_args():
    owner_name = Variable.get('owner')
    return {
    'owner': owner_name,
    'depends_on_past': False,
    'start_date': "2015-09-30T16:41:54+00:00",
    'retries': 5,
    'retry_delay': timedelta(minutes=5),
}


import socket
import struct
import json
sock=socket.socket()
sock.connect(('127.0.0.1',8080))
while True:
    msg=input('>>:').strip()
    if not msg: continue  # 如果输入是空这进入下次循环
    str_len=len(msg)
    data = {'size': str_len}
    json_str=json.dumps(data)
    # 发送报头
    json_len = struct.pack('i', len(json_str))
    sock.send(json_len)
    #发送json
    sock.send(json_str.encode('utf8'))
    #发送内容
    sock.send(msg.encode('utf8'))
sock.close()

网络编程和并发/练习/通客户端.py