from django import forms
from django.forms import ModelForm
from .models import LabTest, LabReport
import datetime

class RequestLabForm(forms.Form):
	def __init__(self, *args, **kwargs):
		super(RequestLabForm, self).__init__(*args, **kwargs)
		instance = getattr(self, 'instance', None)
		self.fields['patient'].widget.attrs['readonly'] = True
		self.fields['request_date'].widget.attrs['readonly'] = True
		self.fields['doctor'].widget.attrs['readonly'] = True

	patient = forms.CharField()
	request_date = forms.DateField()
	doctor = forms.CharField()
	lab_test = forms.ModelChoiceField(queryset=LabTest.objects.all())
	doctor_notes  = forms.CharField(widget=forms.Textarea)

class EditLabRequestForm(ModelForm):
	readonly_doctor = forms.CharField(label="Doctor")
	class Meta:
		model = LabReport
		fields = 'patient', 'request_date', 'readonly_doctor', 'lab_test', 'status', 'doctor_notes', 'doctor'
		widgets={'doctor': forms.HiddenInput()}
	def __init__(self, *args, **kwargs):
		super(EditLabRequestForm, self).__init__(*args, **kwargs)
		instance = getattr(self, 'instance', None)
		self.fields['readonly_doctor'].initial = instance.doctor
		self.fields['patient'].widget.attrs['readonly'] = True
		self.fields['request_date'].widget.attrs['readonly'] = True
		self.fields['readonly_doctor'].widget.attrs['readonly'] = True

# For Pharmacists: Add/Update the Results field, update_date field, lab_technician field, and status.
class UpdateLabReportForm(ModelForm):
	readonly_doctor = forms.CharField(label="Doctor")
	readonly_lab_test = forms.CharField(label="Lab test")
	class Meta:
		model = LabReport
		fields = 'patient', 'request_date', 'readonly_doctor', 'readonly_lab_test', 'lab_test', 'status', 'doctor_notes', 'doctor', 'results', 'update_date'
		widgets={'doctor': forms.HiddenInput(),
				'lab_test': forms.HiddenInput(),
				'lab_technician': forms.HiddenInput(),
				'update_date': forms.HiddenInput()}
	def __init__(self, *args, **kwargs):
		super(UpdateLabReportForm, self).__init__(*args, **kwargs)
		instance = getattr(self, 'instance', None)

		self.fields['readonly_doctor'].initial = instance.doctor
		self.fields['readonly_lab_test'].initial = instance.lab_test
		self.fields['patient'].widget.attrs['readonly'] = True
		self.fields['request_date'].widget.attrs['readonly'] = True
		self.fields['readonly_doctor'].widget.attrs['readonly'] = True
		self.fields['readonly_lab_test'].widget.attrs['readonly'] = True
		self.fields['doctor_notes'].widget.attrs['readonly'] = True


"""Open browser to initialize security database for Mkcert."""

from playwright import sync_api

with sync_api.sync_playwright() as playwright:
    browser = playwright.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto("https://www.wikipedia.org")


from django.db import models
from django.db.models import Count

class VenueManager(models.Manager):
    def by_visits(self, event_kind=None):
        """
        Gets Venues in order of how many Events have been held there.
        Adds a `num_visits` field to each one.

        event_kind filters by kind of Event, e.g. 'theatre', 'cinema', etc.
        """
        qs = self.get_queryset()

        if event_kind is not None:
            qs = qs.filter(event__kind=event_kind)

        qs = qs.annotate(num_visits=Count("event")).order_by("-num_visits", "name_sort")

        return qs

class WorkManager(models.Manager):
    def by_views(self, kind=None):
        """
        Gets Works in order of how many times they've been attached to
        Events.

        kind is the kind of Work, e.g. 'play', 'movie', etc.
        """
        qs = self.get_queryset()

        if kind is not None:
            qs = qs.filter(kind=kind)

        qs = qs.annotate(num_views=Count("event")).order_by("-num_views", "title_sort")

        return qs


import magic
import re

from ._base_module import BaseModule

class FileDiscoverer(BaseModule):
    def check_file_mime(self, file_path):
        mime = ''
        try:
            mime = magic.from_file(file_path, mime=True)
        except Exception: pass
        return self.is_mime_allowed(mime)

    def is_mime_allowed(self, mime):
        result = False
        if mime:
            for key, fmt in self.connector.app_config.ALLOWED_FORMATS.iteritems():
                if mime == fmt['mime']:
                    result = True
                    break
        return result

    def get_allowed_exetnsions(self):
        exts = []
        for key, fmt in self.connector.app_config.ALLOWED_FORMATS.iteritems():
            for ext in fmt['extensions']:
                exts.append(ext)
        return exts


import json
from pathlib import Path
import sys

ROOT = Path(__file__).parent.parent
DATA = ROOT / "data"
ASSETS = ROOT / "assets"

OUTPUT = ROOT.parent.parent.parent / "assets" / "datafiles"
POKEMON_OUTPUT = OUTPUT / "pokemon"
MOVES_OUTPUT = OUTPUT / "moves"

ATTRIBUTES = ["STR", "CON", "DEX", "INT", "WIS", "CHA"]

def __load(path):
    with path.open(encoding="utf-8") as fp:
        json_data = json.load(fp)
    return json_data

def load_extra(name):
    p = Path(ASSETS / "extra" / name).with_suffix(".json")
    return __load(p)

MERGE_POKEMON_DATA = load_extra("pokemon")
MERGE_EVOLVE_DATA = load_extra("evolve")
MERGE_FILTER_DATA = load_extra("filter_data")
MERGE_MOVE_DATA = load_extra("moves")
MERGE_ABILITY_DATA = load_extra("abilities")

def merge(a, b, path=None):
    """merges b into a"""
    if path is None: path = []
    for key in b:
        if key in a:
            if isinstance(a[key], dict) and isinstance(b[key], dict):
                merge(a[key], b[key], path + [str(key)])
            elif a[key] == b[key]:
                pass  # same value
            else:  # Overwrite value
                a[key] = b[key]
                # raise Exception('Conflict at %s' % '.'.join(path + [str(key)]))
        else:
            a[key] = b[key]
    return a

def update_progress(progress):
    bar_length = 50  # Modify this to change the length of the progress bar
    status = ""
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
        status = "error: progress var must be float\r\n"
    if progress < 0:
        progress = 0
        status = "Halt...\r\n"
    if progress >= 1:
        progress = 1
        status = "Done...\r\n"
    block = int(round(bar_length*progress))
    text = "\rPercent: [{}] {:.1f}% {}".format("#"*block + "-"*(bar_length-block), progress*100, status)
    sys.stdout.write(text)
    sys.stdout.flush()

def ensure_int(value):
    if value:
        return int(value)
    return None

def ensure_float(value):
    if value:
        return float(value)
    return None

def ensure_string(value):
    if value and value != "None":
        return value.strip('"').strip()
    return None

def ensure_list(value, sep=','):
    if value:
        return [ensure_string(x) for x in value.split(sep)]
    return None

def clean_object(obj):
    if not obj:
        return
    for index in range(len(obj))[::-1]:
        if not obj[index] or obj[index] == "None":
            del obj[index]

def clean_dict(d):
    if type(d) is dict:
        return dict((k, clean_dict(v)) for k, v in d.items() if v is not None)
        # return {k: v for k, clean_output(v) in d.items() if v is not None}
    else:
        return d


from typing import List
import time

# 执行用时：52 ms, 在所有 Python3 提交中击败了88.05% 的用户
# 内存消耗：16.4 MB, 在所有 Python3 提交中击败了46.19% 的用户

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort(reverse=True)
        s.sort(reverse=True)
        max_number = 0
        i = 0
        j = 0
        while i < len(g) and j < len(s):
            if g[i] > s[j]:
                i += 1
                continue
            else:
                max_number += 1
                i += 1
                j += 1
        return max_number

s =Solution()
start = time.perf_counter()
print(s.findContentChildren([1,2], [1,2,3]))
print(f"time cost is: {time.perf_counter() - start}s")


from unittest import TestCase
import requests
from get_conf import get_config

class TestGet(TestCase):
    def setUp(self) -> None:
        self.config, self.main_uri = get_config()
        self.count_files = len(self.config['MASS_FILES'])
        id_cnt = 0
        for i in self.config['MASS_FILES']:
            id_cnt += 1
            with open(i, 'rb') as file:
                payload = file.read()
            params = {'id': str(self.config['ID'] + id_cnt),
                      'name': i,
                      'tag': self.config['TAG'] + str(self.config['ID'])}

            requests.post(self.main_uri + '/api/upload',
                          data=payload, params=params)

    def tearDown(self) -> None:
        id_cnt = 0
        for i in self.config['MASS_FILES']:
            id_cnt += 1
            params = {'id': str(self.config['ID'] + id_cnt)}
            requests.delete(self.main_uri + '/api/delete',
                              params=params)

    def test_get_two(self):  # tc get_3 _5
        params = [('id', str(self.config['ID'] + 1)),
                  ('id', str(self.config['ID'] + 2)),
                  ('tag', 'tag_123123')]
        req_list_id = [str(self.config['ID'] + 1), str(self.config['ID'] + 2)]
        r = requests.get(self.main_uri + '/api/get', params=params)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        res_list_id = []
        for i in r.json():
            res_list_id.append(str(i.get('id')))
            self.assertIn(str(i.get('id')), req_list_id)
        self.assertEqual(req_list_id, res_list_id)
        self.assertEqual(len(r.json()), 2)

    def test_get_id(self):  # tc get_2_1
        params = 'id=' + str(self.config['ID'] + 1)
        r = requests.get(self.main_uri + '/api/get', params=params)
        data = r.json()
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        self.assertEqual(len(r.json()), 1)
        self.assertEqual(self.config['ID'] + 1, data[0].get('id'))
        self.assertEqual('programmers and other.jpg', data[0].get('name'))
        self.assertEqual('tag_123123', data[0].get('tag'))

    def test_get_name(self):  # tc get_2_2
        params = 'name=programmers and other.jpg'
        r = requests.get(self.main_uri + '/api/get', params=params)
        data = r.json()
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        self.assertEqual(len(r.json()), 1)
        self.assertEqual(self.config['ID'] + 1, data[0].get('id'))
        self.assertEqual('programmers and other.jpg', data[0].get('name'))
        self.assertEqual('tag_123123', data[0].get('tag'))

    def test_get_tag(self):  # tc get_2_3 all file have tag=tag_123123
        params = 'tag=tag_123123'
        r = requests.get(self.main_uri + '/api/get', params=params)
        data = r.json()
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        self.assertEqual(len(r.json()), self.count_files)
        id_cnt = 0
        for i in data:
            self.assertEqual(self.config['ID'] + id_cnt + 1, i.get('id'))
            self.assertEqual(self.config['MASS_FILES'][id_cnt], i.get('name'))
            self.assertEqual('tag_123123', i.get('tag'))
            id_cnt += 1

    def test_get_all(self):  # assert counts sended and returned tc get_1
        r = requests.get(self.main_uri + '/api/get')
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        self.assertEqual(len(r.json()), self.count_files)

    def test_get_null(self):  # tc get_4
        params = 'id=' + str(self.config['ID'])  # id start as str(self.config['ID'] + 1)
        r = requests.get(self.main_uri + '/api/get', params=params)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        self.assertLess(len(r.json()), 1)

    def test_get_one_incorrect(self):  # tc get_6
        params = 'id=' + str(self.config['ID']) + '&id=' +\
                 str(self.config['ID'] + 2) +\
                 '&tag=tag_123123'
        req_id = str(self.config['ID'] + 2)
        r = requests.get(self.main_uri + '/api/get', params=params)
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.reason, 'OK')
        self.assertEqual(len(r.json()), 1)
        self.assertEqual(str(r.json()[0].get('id')), req_id)


import numpy
import cv2

face_cascade = cv2.CascadeClassifier('haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascades\haarcascade_eye.xml')

imagem = cv2.imread('fotos/1.jpg')

cinza = cv2.cvtColor(imagem, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(cinza, scaleFactor=1.05, minNeighbors=2, minSize=(30,30))

print(faces)

for(x, y, l, a) in faces:
  cv2.rectangle(imagem, (x, y), (x + l, y + a), (0,255,0), 2)
  roi_gray = cinza[y:y+a, x:x+l]
  roi_color = imagem[y:y+a, x:x+l]
  eyes = eye_cascade.detectMultiScale(roi_gray)
  for (ex,ey,el,ea) in eyes:
    cv2.rectangle(roi_color, (ex, ey), (ex + el, ey + ea), (255,0,0), 2)
cv2.imshow("Faces", imagem)
cv2.waitKey()


import asyncio
import os
import pydantic
import typer
import uvicorn
from ._private.pydantic import Config as _PydanticConfig
from .commands import random_populate, runserver

__all__: list[str] = []

class StandardsCLI(pydantic.BaseModel):
    app: typer.Typer = typer.Typer()

    Config = _PydanticConfig

    def runserver(
        self,
        host: str = typer.Option(
            "0.0.0.0", "--host", "-h", help="Host to bind the server to"
        ),
        port: int = typer.Option(
            8000, "--port", "-p", help="Port for the server to listen on"
        ),
        db_url: str = typer.Option(
            os.getenv("DB_URL", ""), help="URL to access database"
        ),
    ) -> None:
        uvicorn.run(
            asyncio.run(runserver(db_url=db_url)),
            host=host,
            port=port,
        )

    def random_populate(
        self,
        db_url: str = typer.Option(
            os.getenv("DB_URL", ""), "--db-url", help="Database URL"
        ),
        amount: int = typer.Option(
            10, "--amount", help="Number of records to populate"
        ),
    ) -> None:
        asyncio.run(random_populate(db_url=db_url, amount=amount))

    def callback(self) -> None:
        pass

    def run(self):
        self.app.command()(self.runserver)
        self.app.command()(self.random_populate)
        self.app.callback()(self.callback)
        self.app()

if __name__ == "__main__":
    cli = StandardsCLI()
    cli.run()


from signal import signal, SIGTERM, SIGHUP, pause
from time import sleep, time
from threading import Thread
from gpiozero import DistanceSensor
import RPi.GPIO as GPIO

import time
import sys
import os
from opcua import Server

class OPCUAController:
    def __init__(self, url):
        self.url = url
        self.server = Server()
        self.node = None
        self.namespaces = {}
        self.object_map = {}
        self.param_node_map = {}

    def set_up(self):
        self.server.set_endpoint(self.url)
        self.node = self.server.get_objects_node()

    def add_namespace(self, name):
        self.namespaces[name] = self.server.register_namespace(name)

    def add_object(self, namespace, object_name):
        if namespace in self.namespaces:
            self.object_map[object_name] = self.node.add_object(self.namespaces[namespace], object_name)

    def add_param_node(self, namespace, object_name, param_node_name, param_node_value):
        if (object_name in self.object_map) and (namespace in self.namespaces):
            self.param_node_map[param_node_name] = self.object_map[object_name].add_variable(self.namespaces[namespace], param_node_name, param_node_value)

    def get_param_node(self, param_node_name):
    	if param_node_name in self.param_node_map:
            return self.param_node_map[param_node_name]

    def set_param_node_writeable(self, param_node_name):
        if param_node_name in self.param_node_map:
            self.param_node_map[param_node_name].set_writable()

class GPIOController:
    def __init__(self):
        self.Trigger = 21
        self.Echo = 26
        self.reading = True
        self.sensor = DistanceSensor(echo=self.Echo, trigger=self.Trigger)
        self.Valve =16
        self.Valve2 = 12
        self.Pump = 20

#manual calculations of distance
############################################
    def distance(self):
        GPIO.output(self.Trigger,True)
        sleep(0.00001)
        GPIO.output(self.Trigger,False)
        while GPIO.input(self.Echo) == 0:
            StartTime = time()
        while GPIO.input(self.Echo) == 1:
            StopTime = time()
        TimeElapsed = StopTime - StartTime
        dist = (TimeElapsed*34300)/2
        return dist
    def reading_dist(self):
        while self.reading:
            dist = self.distance()
        print(f"Distance: {dist}cm")
        sleep(1)
#############################################
    def set_up(self):
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(self.Valve,GPIO.OUT)
        GPIO.setup(self.Valve2,GPIO.OUT)
        GPIO.setup(self.Pump,GPIO.OUT)
    def pump_on(self):
        GPIO.output(self.Pump,GPIO.LOW)
        print("Pump ON")

    def pump_off(self):
        GPIO.output(self.Pump,GPIO.HIGH)
        print("Pump OFF")

    def valve_on(self):
        GPIO.output(self.Valve,GPIO.HIGH)
        print("Valve ON")

    def valve_off(self):
        GPIO.output(self.Valve,GPIO.LOW)
        print("Valve OFF")

    def valve2_on(self):
        GPIO.output(self.Valve2, GPIO.LOW)
        print("Valve2 ON")

    def valve2_off(self):
        GPIO.output(self.Valve2, GPIO.HIGH)
        print("Valve2 OFF")

"""
state: 0|1, whether or not the tank is full
num:  The ultrasonic sensor HC SR04 have a frequency of 40hz, so max it can only take 40 readings in 1 second.
height_of_tank: Height of tank in cm
stopping_dist: When the water level is 4cm from the sensor, we will consider it as full
...
"""
class WaterPlantController:
    def __init__(self, gpioClient):
        self.state = 0
        self.num = 40
        self.height_of_tank = 22
        self.stopping_dist = 4
        self.max_height_of_water = self.height_of_tank - self.stopping_dist
        self.height_of_empty_space = gpioClient.sensor.value*100 - self.stopping_dist
        self.height_of_water = 1- self.height_of_empty_space/self.max_height_of_water
        self.waterlvl = int(self.height_of_water * 100)
        self.level = int(self.waterlvl - (self.waterlvl % 5))

URL = "opc.tcp://10.12.1.56:4840"
NAMESPACE = "OPCUA_RPI_SERVER"
OBJECT_NAME = "Parameters"

PUMP_NODE = "pump"
VALVE_NODE_1 = "valve 1"
VALVE_NODE_2 = "valve 2"
WATER_LEVEL_NODE = "waterlevel"
AUTOPILOT_NODE = "autoPilot"
GATE_NODE = "gate"

opcua = OPCUAController(URL)
opcua.set_up()
opcua.add_namespace(NAMESPACE)
opcua.add_object(NAMESPACE, OBJECT_NAME)
opcua.add_param_node(NAMESPACE, OBJECT_NAME, PUMP_NODE, float(0))
opcua.add_param_node(NAMESPACE, OBJECT_NAME, VALVE_NODE_1, float(0))
opcua.add_param_node(NAMESPACE, OBJECT_NAME, GATE_NODE, float(0))
opcua.add_param_node(NAMESPACE, OBJECT_NAME, WATER_LEVEL_NODE, float(0))
opcua.add_param_node(NAMESPACE, OBJECT_NAME, AUTOPILOT_NODE, float(0))
opcua.add_param_node(NAMESPACE, OBJECT_NAME, VALVE_NODE_2, float(0))
opcua.set_param_node_writeable(PUMP_NODE)
opcua.set_param_node_writeable(VALVE_NODE_1)
opcua.set_param_node_writeable(GATE_NODE)
opcua.set_param_node_writeable(WATER_LEVEL_NODE)
opcua.set_param_node_writeable(AUTOPILOT_NODE)
opcua.set_param_node_writeable(VALVE_NODE_2)
opcua.server.start()
print("server started at {}".format(URL))

gpio = GPIOController()
gpio.set_up()

waterplant = WaterPlantController(gpio)

"""
Function used to return the correct Level state (State machine) used to fix jitter of waterlevel reading from HCS04
By right the 'level' variable should be in multiple of 5 only.
"""
def set_water_level_in_opcua(level, waterlvl, waterNode):
    '''
    If level is <=0, if waterlvl<7, set level and server waterlvl as 0 else set level and server waterlvl as 5
    '''
    if level <= 0:
        if waterlvl < 0+7:
            waterNode.set_value(float(0))
            return 0
        else:
            waterNode.set_value(float(5))
            return 5

    #If level == 5, if waterlvl < 0, set level and server waterlvl as 0, elif waterlvl <12, set level and server waterlvl as 5, else set level and waterlvl as 10

    elif level == 5:
        if waterlvl < level-5:
            waterNode.set_value(float(level-5))
            return level-5
        elif waterlvl < level+ 7:
            waterNode.set_value(float(level))
            return level
        else:
            waterNode.set_value(float(level+5))
            return level+5

    #If level >=100, if waterlvl <93, set level and server waterlvl as 95, else set level and server waterlvl as 100

    elif level >= 100:
        if waterlvl < 100-7:
            waterNode.set_value(float(95))
            return 95
        else:
            waterNode.set_value(float(100))
            return 100

    #if waterlvl < level-7, set level and server waterlvl as (level-5), elif waterlvl < level+7 set level and server waterlvl as level, else set level and server waterlvl as (level+5)

    else:
        if waterlvl < level-7:
            waterNode.set_value(float(level-5))
            return level - 5

        elif waterlvl < level+7:
            waterNode.set_value(float(level))
            return level

        else:
            waterNode.set_value(float(level+5))
            return level +5

def set_state_and_valves_values_and_pump_values(opcuaClient, valve_2, valve_1, pump, final_state):
    opcuaClient.get_param_node(VALVE_NODE_2).set_value(float(valve_2))
    opcuaClient.get_param_node(VALVE_NODE_1).set_value(float(valve_1))
    opcuaClient.get_param_node(PUMP_NODE).set_value(float(pump))
    return final_state

def get_average_reading(number_of_times_to_poll_value, sensor_value):
    total = 0
    for i in range(number_of_times_to_poll_value):
        total += sensor_value
        sleep(0.025)
    avg = total/number_of_times_to_poll_value*100

    return "{:.2f}".format(avg) ,avg

def set_opcua_client_and_gpio_client(opcuaClient, gpioClient):
    if (opcuaClient.get_param_node(PUMP_NODE).get_value()==1) and (opcuaClient.get_param_node(VALVE_NODE_1).get_value()==1):
        opcuaClient.get_param_node(GATE_NODE).set_value(float(1))
    else:
        opcuaClient.get_param_node(GATE_NODE).set_value(float(0))

    if opcuaClient.get_param_node(PUMP_NODE).get_value() == 1:
        gpioClient.pump_on()
    else:
        gpioClient.pump_off()

    if opcuaClient.get_param_node(VALVE_NODE_1).get_value() == 1:
        gpioClient.valve_on()
    else:
        gpioClient.valve_off()

    if opcuaClient.get_param_node(VALVE_NODE_2).get_value()==1:
        gpioClient.valve2_on()
    else:
        gpioClient.valve2_off()

def read_dist(opcuaClient, gpioClient, waterplantClient):
    opcuaClient.get_param_node(AUTOPILOT_NODE).set_value(float(1))

    while gpioClient.reading:
        dist,avg = get_average_reading(waterplantClient.num, gpioClient.sensor.value)
        waterlvl = int(100- ((avg-4)/18 * 100))
        p = opcuaClient.get_param_node(PUMP_NODE).get_value()
        v = opcuaClient.get_param_node(VALVE_NODE_1).get_value()
        v2 = opcuaClient.get_param_node(VALVE_NODE_2).get_value()
        g = opcuaClient.get_param_node(GATE_NODE).get_value()
        w = opcuaClient.get_param_node(WATER_LEVEL_NODE).get_value()
        a = opcuaClient.get_param_node(AUTOPILOT_NODE).get_value()
        print("Distance: " + dist + " cm")
        print(f"waterlvl: {waterlvl}")
        print(f"p:{p}, v:{v}, v2:{v2}, g:{g}, w:{w}, a:{a}")

        waterplant.level = set_water_level_in_opcua(waterplant.level, waterlvl, opcuaClient.get_param_node(WATER_LEVEL_NODE)) ### write to server and set correct machine state

        if opcuaClient.get_param_node(AUTOPILOT_NODE).get_value() == 1:                   ### when autopilot, automatically set server values, else don't do anything
            if waterplantClient.state == 0:  ### when notFull, check server waterlvl, if waterlvl <100, on pump and valve and set state to be notFull, else off pump and valve and set state to be Full
                if opcuaClient.get_param_node(WATER_LEVEL_NODE).get_value() < 100:
                    waterplantClient.state = set_state_and_valves_values_and_pump_values(opcuaClient,0,1,1,0)
                else:
                    waterplantClient.state = set_state_and_valves_values_and_pump_values(opcuaClient,1,0,0,1)
            elif waterplantClient.state == 1:
                if opcuaClient.get_param_node(WATER_LEVEL_NODE).get_value() <= 20:  ### when full, check server waterlvl, if waterlvl <=20, on pump and valve and set state to notFull, else off pump and valve and set state to be notFull
                    waterplantClient.state = set_state_and_valves_values_and_pump_values(opcuaClient,0,1,1,0)
                else:
                    waterplantClient.state = set_state_and_valves_values_and_pump_values(opcuaClient,1,0,0,1)

        set_opcua_client_and_gpio_client(opcuaClient, gpioClient)

def safe_exit(signum,frame):
    exit(1)

signal(SIGTERM, safe_exit)
signal(SIGHUP, safe_exit)

try:
    reader = Thread(target=read_dist, args=(opcua, gpio, waterplant), daemon= True) ### run the program as thread, set daemon as true so that when the parent process is killed, the child is killed as well
    reader.start()
    pause()
except KeyboardInterrupt:
    print('Interrupted')
    try:
        sys.exit(0)
    except SystemExit:
        os._exit(0)
finally:
    reading = False
    sensor.close()


import random
import asyncio

from app.entities.game import BotGame
from app.util.alconna import Commander
from app.util.graia import At, Group, Member, MessageChain, GroupMessage, FunctionWaiter, message

from .data import data

RUNNING = {}
command = Commander("背成语", "背成语")

@command.no_match(events=[GroupMessage])
async def group_learn(group: Group):
    async def waiter(
            waiter_group: Group, waiter_member: Member, waiter_message: MessageChain
    ):
        if waiter_group.id == group.id:
            waiter_saying = waiter_message.display.strip()
            if waiter_saying == "取消":
                return False
            elif waiter_saying == RUNNING[group.id]:
                return waiter_member.id

    if group.id in RUNNING:
        return

    RUNNING[group.id] = None

    while True:
        word_data = random.choice(data)
        RUNNING[group.id] = word_data["word"]

        message(
            f"本回合题目：\n该成语释义：{word_data['explanation']}\n",
            f"出处：{word_data['derivation'].replace(word_data['word'], '*' * len(word_data['word']))}",
        ).target(group).send()
        try:
            answer_qq = await FunctionWaiter(waiter, events=[GroupMessage]).wait(60)
            if answer_qq:
                await BotGame(str(answer_qq)).update_english_answer(1)
                message([
                    "恭喜 ",
                    At(answer_qq),
                    f" 回答正确 {word_data['word']}",
                ]).target(group).send()

                await asyncio.sleep(2)
            else:
                del RUNNING[group.id]
                return message("已结束本次答题").target(group).send()

        except asyncio.TimeoutError:
            del RUNNING[group.id]
            return message(
                    f"本次成语为：{word_data['word']}\n",
                    f"这个成语出自：{word_data['derivation']}\n",
                    f"释义：{word_data['explanation']}\n",
                    f"读音：{word_data['pinyin']}\n",
                    "答题已结束，请重新开启",
            ).target(group).send()


import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np
import nibabel as nib
data_train = np.load('./datalist/train_an.npy')
data_valid = np.load('./datalist/valid_an.npy')
data_test  = np.load('./datalist/test_an.npy')
file_train = data_train[:, 0]
label_train = data_train[:, 1]
file_valid = data_valid[:,0]
label_valid = data_valid[:,1]
file_test = data_test[:,0]
label_test = data_test[:,1]

def default_loader(path):
    img_pil = nib.load(path)
    img_pil = img_pil.get_data()
    img_pil = torch.from_numpy(img_pil)
    return img_pil

class TrainSet(Dataset):

    def __init__(self,loader = default_loader):
        self.image = file_train
        self.label = label_train
        self.loader = loader

    def __getitem__(self, index):
        fn = self.image[index]
        img = self.loader(fn)
        label = self.label[index]
        return img,label

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

class ValidSet(Dataset):
    def __init__(self,loader = default_loader):
        self.image = file_valid
        self.label = label_valid
        self.loader = loader

    def __getitem__(self, index):
        fn = self.image[index]
        img = self.loader(fn)
        label = self.label[index]
        return img,label

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

class TestSet(Dataset):

    def __init__(self,loader = default_loader):
        self.image = file_test
        self.label = label_test
        self.loader = loader

    def __getitem__(self, index):
        fn = self.image[index]
        img = self.loader(fn)
        label = self.label[index]
        return img,label

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


import functools
from typing import Dict, Union

from absl import app
from absl import flags
from absl import logging
from flax import jax_utils
from flax import optim
from flax.metrics import tensorboard
from flax.training import checkpoints
import gin
import jax
from jax import numpy as jnp
from jax import random
import numpy as np
import tensorflow as tf

# ZCW test
import flax

from editablenerf import configs
from editablenerf import datasets
from editablenerf import gpath
from editablenerf import model_utils
from editablenerf import models
from editablenerf import schedules
from editablenerf import training
from editablenerf import utils

flags.DEFINE_enum('mode', None, ['jax_cpu', 'jax_gpu', 'jax_tpu'],
                  'Distributed strategy approach.')

flags.DEFINE_string('base_folder', None, 'where to store ckpts and logs')
flags.mark_flag_as_required('base_folder')
flags.DEFINE_multi_string('gin_bindings', None, 'Gin parameter bindings.')
flags.DEFINE_multi_string('gin_configs', (), 'Gin config files.')
FLAGS = flags.FLAGS

def _log_to_tensorboard(writer: tensorboard.SummaryWriter,
                        state: model_utils.TrainState,
                        scalar_params: training.ScalarParams,
                        stats: Dict[str, Union[Dict[str, jnp.ndarray],
                                               jnp.ndarray]],
                        time_dict: Dict[str, jnp.ndarray]):
  """Log statistics to Tensorboard."""
  step = int(state.optimizer.state.step)

  def _log_scalar(tag, value):
    if value is not None:
      writer.scalar(tag, value, step)

  _log_scalar('params/learning_rate', scalar_params.learning_rate)
  _log_scalar('params/nerf_alpha', state.nerf_alpha)
  _log_scalar('params/warp_alpha', state.warp_alpha)
  _log_scalar('params/hyper_sheet_alpha', state.hyper_sheet_alpha)
  _log_scalar('params/elastic_loss/weight', scalar_params.elastic_loss_weight)

  # pmean is applied in train_step so just take the item.
  for branch in {'coarse', 'fine'}:
    if branch not in stats:
      continue
    for stat_key, stat_value in stats[branch].items():
      writer.scalar(f'{stat_key}/{branch}', stat_value, step)

  _log_scalar('loss/background', stats.get('background_loss'))

  # ZCW add
  _log_scalar('loss/K_points', stats.get('K_points_loss'))
  _log_scalar('loss/flow_loss', stats.get('flow_loss'))
  _log_scalar('loss/depth_loss', stats.get('depth_loss'))
  _log_scalar('loss/weights_reg_loss', stats.get('weights_reg_loss'))
  _log_scalar('params/weights_reg', scalar_params.weights_reg_loss_weight)

  for k, v in time_dict.items():
    writer.scalar(f'time/{k}', v, step)

def _log_histograms(writer: tensorboard.SummaryWriter,
                    state: model_utils.TrainState,
                    model_out):
  """Log histograms to Tensorboard."""
  step = int(state.optimizer.state.step)
  params = state.optimizer.target['model']
  if 'nerf_embed' in params:
    embeddings = params['nerf_embed']['embed']['embedding']
    writer.histogram('nerf_embedding', embeddings, step)
  if 'hyper_embed' in params:
    embeddings = params['hyper_embed']['embed']['embedding']
    writer.histogram('hyper_embedding', embeddings, step)
  if 'warp_embed' in params:
    embeddings = params['warp_embed']['embed']['embedding']
    writer.histogram('warp_embedding', embeddings, step)

  for branch in {'coarse', 'fine'}:
    if 'warped_points' in model_out[branch]:
      points = model_out[branch]['points']
      warped_points = model_out[branch]['warped_points']
      writer.histogram(f'{branch}/spatial_points',
                       warped_points[..., :3], step)
      writer.histogram(f'{branch}/spatial_points_delta',
                       warped_points[..., :3] - points, step)
      if warped_points.shape[-1] > 3:
        writer.histogram(f'{branch}/hyper_points',
                         warped_points[..., 3:], step)

def _log_grads(writer: tensorboard.SummaryWriter, model: models.NerfModel,
               state: model_utils.TrainState):
  """Log histograms to Tensorboard."""
  step = int(state.optimizer.state.step)
  params = state.optimizer.target['model']
  if 'nerf_metadata_encoder' in params:
    embeddings = params['nerf_metadata_encoder']['embed']['embedding']
    writer.histogram('nerf_embedding', embeddings, step)
  if 'hyper_metadata_encoder' in params:
    embeddings = params['hyper_metadata_encoder']['embed']['embedding']
    writer.histogram('hyper_embedding', embeddings, step)
  if 'warp_field' in params and model.warp_metadata_config['type'] == 'glo':
    embeddings = params['warp_metadata_encoder']['embed']['embedding']
    writer.histogram('warp_embedding', embeddings, step)

def main(argv):
  # tf.config.set_soft_device_placement(True)
  # physical_devices = tf.config.list_physical_devices('GPU')
  # gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
  # for gpu in gpus:
  #   tf.config.experimental.set_memory_growth(gpu, True)

  jax.config.parse_flags_with_absl()
  tf.config.experimental.set_visible_devices([], 'GPU')
  del argv
  logging.info('*** Starting experiment')
  # Assume G3 path for config files when running locally.
  gin_configs = FLAGS.gin_configs

  logging.info('*** Loading Gin configs from: %s', str(gin_configs))
  gin.parse_config_files_and_bindings(
      config_files=gin_configs,
      bindings=FLAGS.gin_bindings,
      skip_unknown=True)

  # Load configurations.
  exp_config = configs.ExperimentConfig()
  train_config = configs.TrainConfig()
  dummy_model = models.NerfModel({}, 0, 0)

  # Get directory information.
  exp_dir = gpath.GPath(FLAGS.base_folder)
  if exp_config.subname:
    exp_dir = exp_dir / exp_config.subname
  summary_dir = exp_dir / 'summaries' / 'train'
  checkpoint_dir = exp_dir / 'checkpoints'

  # Log and create directories if this is the main process.
  if jax.process_index() == 0:
    logging.info('exp_dir = %s', exp_dir)
    if not exp_dir.exists():
      exp_dir.mkdir(parents=True, exist_ok=True)

    logging.info('summary_dir = %s', summary_dir)
    if not summary_dir.exists():
      summary_dir.mkdir(parents=True, exist_ok=True)

    logging.info('checkpoint_dir = %s', checkpoint_dir)
    if not checkpoint_dir.exists():
      checkpoint_dir.mkdir(parents=True, exist_ok=True)

  logging.info('Starting process %d. There are %d processes.',
               jax.process_index(), jax.process_count())
  logging.info('Found %d accelerator devices: %s.', jax.local_device_count(),
               str(jax.local_devices()))
  logging.info('Found %d total devices: %s.', jax.device_count(),
               str(jax.devices()))

  rng = random.PRNGKey(exp_config.random_seed)
  # Shift the numpy random seed by process_index() to shuffle data loaded by
  # different processes.
  np.random.seed(exp_config.random_seed + jax.process_index())

  # if train_config.batch_size % jax.device_count() != 0:
  #   raise ValueError('Batch size must be divisible by the number of devices.')

  # ZCW change, use device
  devices = jax.local_devices()[0:4]
  # devices = [jax.local_devices()[0], jax.local_devices()[1], jax.local_devices()[6], jax.local_devices()[7]]
  print(devices)
  if train_config.batch_size % len(devices) != 0:
    raise ValueError('Batch size must be divisible by the number of devices.')

  logging.info('Creating datasource')
  datasource = exp_config.datasource_cls(
      image_scale=exp_config.image_scale,
      random_seed=exp_config.random_seed,
      # Enable metadata based on model needs.
      use_warp_id=dummy_model.use_warp,
      use_appearance_id=(
          dummy_model.nerf_embed_key == 'appearance'
          or dummy_model.hyper_embed_key == 'appearance'),
      use_camera_id=dummy_model.nerf_embed_key == 'camera',
      use_time=dummy_model.warp_embed_key == 'time',
      load_kp_init_file=dummy_model.load_kp_init_file,  # ZCW add
      k_point_num=dummy_model.k_point_num,  # ZCW add
      kp_dimension=dummy_model.kp_dimension)  # ZCW add

  # Create Model.
  logging.info('Initializing models.')
  rng, key = random.split(rng)
  params = {}
  model, params['model'] = models.construct_nerf(
      key,
      batch_size=train_config.batch_size,
      embeddings_dict=datasource.embeddings_dict,
      near=datasource.near,
      far=datasource.far)
  # kp_dimension=datasource.kp_dimension)  # ZCW add

  # Create Jax iterator.
  logging.info('Creating dataset iterator.')
  train_iter = datasource.create_iterator(
      datasource.train_ids,
      flatten=True,
      shuffle=True,
      batch_size=train_config.batch_size,
      prefetch_size=3,
      shuffle_buffer_size=train_config.shuffle_buffer_size,
      devices=devices,
  )

  points_iter = None
  if train_config.use_background_loss:
    points = datasource.load_points(shuffle=True)
    points_batch_size = min(
        len(points),
        len(devices) * train_config.background_points_batch_size)
    points_batch_size -= points_batch_size % len(devices)
    points_dataset = tf.data.Dataset.from_tensor_slices(points)
    points_iter = datasets.iterator_from_dataset(
        points_dataset,
        batch_size=points_batch_size,
        prefetch_size=3,
        devices=devices)

  # ZCW add
  flow_iter = None
  cmr_const = None
  if train_config.use_flow_loss:
    flow, cmr_const = datasource.load_flow()
    cmr_const['coor'] = datasource.load_local_coor()
    flow_dataset = tf.data.Dataset.from_tensor_slices(flow)
    flow_iter = datasets.iterator_from_dataset(
        flow_dataset,
        batch_size=len(devices)*2,
        prefetch_size=3,
        devices=devices)

  learning_rate_sched = schedules.from_config(train_config.lr_schedule)
  nerf_alpha_sched = schedules.from_config(train_config.nerf_alpha_schedule)
  warp_alpha_sched = schedules.from_config(train_config.warp_alpha_schedule)
  hyper_alpha_sched = schedules.from_config(train_config.hyper_alpha_schedule)
  hyper_sheet_alpha_sched = schedules.from_config(
      train_config.hyper_sheet_alpha_schedule)
  elastic_loss_weight_sched = schedules.from_config(
      train_config.elastic_loss_weight_schedule)

  # ZCW add
  # weights_reg_sched = schedules.from_config(train_config.weights_reg_schedule)

  optimizer_def = optim.Adam(learning_rate_sched(0))
  if train_config.use_weight_norm:
    optimizer_def = optim.WeightNorm(optimizer_def)
  optimizer = optimizer_def.create(params)
  state = model_utils.TrainState(
      optimizer=optimizer,
      nerf_alpha=nerf_alpha_sched(0),
      warp_alpha=warp_alpha_sched(0),
      hyper_alpha=hyper_alpha_sched(0),
      hyper_sheet_alpha=hyper_sheet_alpha_sched(0))
  scalar_params = training.ScalarParams(
      learning_rate=learning_rate_sched(0),
      elastic_loss_weight=elastic_loss_weight_sched(0),
      warp_reg_loss_weight=train_config.warp_reg_loss_weight,
      warp_reg_loss_alpha=train_config.warp_reg_loss_alpha,
      warp_reg_loss_scale=train_config.warp_reg_loss_scale,
      background_loss_weight=train_config.background_loss_weight,
      hyper_reg_loss_weight=train_config.hyper_reg_loss_weight,
      K_points_loss_weight=train_config.K_points_loss_weight,  # ZCW add
      flow_loss_weight=train_config.flow_loss_weight,  # ZCW add
      depth_loss_weight=train_config.depth_loss_weight,  # ZCW add
      weights_reg_loss_weight=float(0.0))  # weights_reg_sched(0))  # ZCW add
  state = checkpoints.restore_checkpoint(checkpoint_dir, state)
  init_step = state.optimizer.state.step + 1
  state = jax_utils.replicate(state, devices=devices)
  del params

  # ZCW init
  if train_config.use_flow_loss:
    print('init hyper_embed by datasource')
    state_dict = flax.serialization.to_state_dict(state.optimizer)
    # hyper_embed = jax.device_put_replicated(datasource.kp_init, devices=devices)

    ori_hyper_embed = state_dict['target']['model']['hyper_embed']['embed']['embedding']
    ori_hyper_embed = np.asarray(ori_hyper_embed[0])
    # new_hyper_embed -= 0.025
    kp_size = datasource.kp_init.shape[-1]
    hyper_init = np.concatenate((datasource.kp_init, ori_hyper_embed[..., kp_size:]), axis=1)
    hyper_embed = jax.device_put_replicated(hyper_init, devices=devices)

    state_dict['target']['model']['hyper_embed']['embed']['embedding'] = hyper_embed
    state = state.replace(
        optimizer=flax.serialization.from_state_dict(state.optimizer, state_dict))

  summary_writer = None
  if jax.process_index() == 0:
    config_str = gin.operative_config_str()
    logging.info('Configuration: \n%s', config_str)
    with (exp_dir / 'config.gin').open('w') as f:
      f.write(config_str)
    summary_writer = tensorboard.SummaryWriter(str(summary_dir))
    # ZCW change, debug gin.markdown
    # summary_writer.text('gin/train', textdata=gin.config.markdownify_operative_config_str(config_str), step=0)

  train_step = functools.partial(
      training.train_step,
      model,
      elastic_reduce_method=train_config.elastic_reduce_method,
      elastic_loss_type=train_config.elastic_loss_type,
      use_elastic_loss=train_config.use_elastic_loss,
      use_background_loss=train_config.use_background_loss,
      use_warp_reg_loss=train_config.use_warp_reg_loss,
      use_hyper_reg_loss=train_config.use_hyper_reg_loss,
      use_K_points_loss=train_config.use_K_points_loss,  # ZCW add
      use_flow_loss=train_config.use_flow_loss,  # ZCW add
      use_weights_reg_loss=train_config.use_weights_reg_loss,  # ZCW add
      k_point_num=datasource.k_point_num,  # ZCW add
      cmr_const=cmr_const,  # ZCW add
  )
  ptrain_step = jax.pmap(
      train_step,
      axis_name='batch',
      devices=devices,
      # rng_key, state, batch, scalar_params.
      in_axes=(0, 0, 0, None),
      # Treat use_elastic_loss as compile-time static.
      donate_argnums=(2,),  # Donate the 'batch' argument.
  )

  if devices:
    n_local_devices = len(devices)
  else:
    n_local_devices = jax.local_device_count()

  logging.info('Starting training')
  # Make random seed separate across processes.
  rng = rng + jax.process_index()
  keys = random.split(rng, n_local_devices)
  time_tracker = utils.TimeTracker()
  time_tracker.tic('data', 'total')
  for step, batch in zip(range(init_step, train_config.max_steps + 1),
                         train_iter):
    if points_iter is not None:
      batch['background_points'] = next(points_iter)
    if flow_iter is not None:
      batch['flow_data'] = next(flow_iter)

    time_tracker.toc('data')
    # See: b/162398046.
    # pytype: disable=attribute-error
    scalar_params = scalar_params.replace(
        learning_rate=learning_rate_sched(step),
        elastic_loss_weight=elastic_loss_weight_sched(step))#,
        #weights_reg_loss_weight=weights_reg_sched(step))  # ZCW add

    # ZCW add
    # if step == 50000:
    #   scalar_params = scalar_params.replace(weights_reg_loss_weight=float(train_config.weights_reg_loss_weight / 2))
    #   print('weights_reg_loss_weight')
    # if step == 100000:
    #   scalar_params = scalar_params.replace(weights_reg_loss_weight=float(train_config.weights_reg_loss_weight))
    #   print('weights_reg_loss_weight')
    # if step == 150000:
    #   scalar_params = scalar_params.replace(weights_reg_loss_weight=float(0))
    #   print('weights_reg_loss_weight')

    # pytype: enable=attribute-error
    nerf_alpha = jax_utils.replicate(nerf_alpha_sched(step), devices)
    warp_alpha = jax_utils.replicate(warp_alpha_sched(step), devices)
    hyper_alpha = jax_utils.replicate(hyper_alpha_sched(step), devices)
    hyper_sheet_alpha = jax_utils.replicate(
        hyper_sheet_alpha_sched(step), devices)
    state = state.replace(nerf_alpha=nerf_alpha,
                          warp_alpha=warp_alpha,
                          hyper_alpha=hyper_alpha,
                          hyper_sheet_alpha=hyper_sheet_alpha)

    with time_tracker.record_time('train_step'):
      state, stats, keys, model_out = ptrain_step(
          keys, state, batch, scalar_params)
      time_tracker.toc('total')

    if step % train_config.print_every == 0 and jax.process_index() == 0:
      logging.info('step=%d, nerf_alpha=%.04f, warp_alpha=%.04f, %s', step,  # weights_reg==%f,
                   nerf_alpha_sched(step),
                   warp_alpha_sched(step),
                   #weights_reg_sched(step),
                   time_tracker.summary_str('last'))
      coarse_metrics_str = ', '.join(
          [f'{k}={v.mean():.04f}' for k, v in stats['coarse'].items()])
      fine_metrics_str = ', '.join(
          [f'{k}={v.mean():.04f}' for k, v in stats['fine'].items()])
      logging.info('\tcoarse metrics: %s', coarse_metrics_str)
      if 'fine' in stats:
        logging.info('\tfine metrics: %s', fine_metrics_str)

    if step % train_config.save_every == 0 and jax.process_index() == 0:
      training.save_checkpoint(checkpoint_dir, state, keep=50) # ZCW change 2

    if step % train_config.log_every == 0 and jax.process_index() == 0:
      # Only log via process 0.
      _log_to_tensorboard(
          summary_writer,
          jax_utils.unreplicate(state),
          scalar_params,
          jax_utils.unreplicate(stats),
          time_dict=time_tracker.summary('mean'))
      time_tracker.reset()

    if step % train_config.histogram_every == 0 and jax.process_index() == 0:
      _log_histograms(summary_writer, jax_utils.unreplicate(state), model_out)

    time_tracker.tic('data', 'total')

  if train_config.max_steps % train_config.save_every != 0:
    training.save_checkpoint(checkpoint_dir, state, keep=50) # ZCW change 2

if __name__ == '__main__':
  app.run(main)


import collections
import os
import sys

import torch
from torch.nn import functional as F

sys.path.append("../../")
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from sys_config import EXP_DIR

def print_stats(labels_list, name):
    """

    :param stats_list: format: [(class_i, %), (class_i+1, %), ...]
    :return:
    """
    c = collections.Counter(labels_list)
    stats_list = [(i, c[i] / len(labels_list) * 100.0) for i in c]

    str = "{} set stats: ".format(name)
    for i in range(0, len(stats_list)):
        str += "class {}: {}% ".format(stats_list[i][0], round(stats_list[i][1]),3)

    print(str)
    return

def number_h(num):
    for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
        if abs(num) < 1000.0:
            return "%3.1f%s" % (num, unit)
        num /= 1000.0
    return "%.1f%s" % (num, 'Yi')

def create_dir(dir_path):
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
        print('Created {}'.format(dir_path))
        return

def create_exp_dirs(args):
    model_type = args.model_type if args.model_type != 'allenai/scibert' else 'bert'

    exp_name = 'al_{}_{}_{}'.format(args.dataset_name,
                                          model_type,
                                          args.acquisition)

    if args.indicator is not None: exp_name += '_{}'.format(args.indicator)

    al_iterations_dir = EXP_DIR
    create_dir(al_iterations_dir)
    # /experiments/{dataset}_{model}_{acquisition}
    al_config_dir = os.path.join(al_iterations_dir, exp_name)
    create_dir(al_config_dir)
    # /experiments/al_iterations/{dataset}_{model}_{acquisition}/{variant}_{seed}
    var_seed = '{}'.format(args.seed)
    if args.indicator is not None: var_seed += '_{}'.format(args.indicator)
    if args.mc_samples is None and args.acquisition in ['entropy', 'least_conf']:
        var_seed += '_vanilla'
    # if args.debug: var_seed += '_debug'
    if args.reverse: var_seed += '_reverse'
    if args.mean_embs: var_seed += '_inputs'
    if args.mean_out: var_seed += '_outputs'
    if args.cls: var_seed += '_cls'
    if args.ce: var_seed += '_ce'
    if args.operator != "mean" and args.acquisition=="adv_train": var_seed += '_{}'.format(args.operator)
    if args.knn_lab: var_seed += '_knn_lab'
    if args.bert_score: var_seed += '_bs'
    if args.bert_rep: var_seed += '_br'
    if args.tfidf: var_seed += '_tfidf'

    results_per_iteration_dir = os.path.join(al_config_dir, var_seed)
    create_dir(results_per_iteration_dir)

    # d_pool_dir = os.path.join(FIG_DIR, 'd_pools')
    # create_dir(d_pool_dir)
    # d_pool_dir = os.path.join(d_pool_dir, '{}_{}_{}'.format(args.dataset_name,
    #                                                                   args.model_type,
    #                                                                   args.acquisition))
    # create_dir(d_pool_dir)
    # d_pool_dir = os.path.join(d_pool_dir, var_seed)
    # create_dir(d_pool_dir)

    exp_dir = os.path.join(EXP_DIR, exp_name)
    # create_dir(exp_dir)

    # fig_dir = os.path.join(FIG_DIR, '{}_{}_{}'.format(args.dataset_name, args.model_type, args.acquisition), var_seed)

    # return fig_dir, results_per_iteration_dir
    return results_per_iteration_dir


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = r'''
---
module: veeam-job
short_description: Управлние задачами файлового бекапа veeam на linux
version_added: "0.1.0"

description: Позволяет создавать, удалять и просматривать задания бекапа. Поддерживает только файловый бекап (fileLevel).

options:
    type:
        description:
        - C(job) создание или удаление задачи файлового (fileLevel) бекапа.
        - C(list) список задач бекапа.
        required: true
        type: str
        choices: [ job, list ]
    state:
        description:
        - C(present) создание задачи бекапа.
        - C(absent) удаление задачи бекапа.
        type: str
        choices: [ absent, present ]
        required: false
        default: present
    prefreeze:
        description: Путь к скрипту для уровня prefreeze.
        type: path
        required: false
    includedirs:
        description: Путь к директории для бекапа. Обязательное для state C(present).
        type: path
        required: false
    name:
        description: Имя задачи бекапа. Обязательное для state C(present) и C(absent).
        type: str
        required: false
    reponame:
        description: Имя репозитория. Обязательное для state C(present).
        type: str
        required: false
    maxpoints:
        description: Глубина хранения.
        type: int
        required: false
    rundays:
        description: Дни запуска (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, All). Обязательное для C(runat).
        type: int
        required: false
    runat:
        description: Время запуска HH:MM. Обязательное для C(rundays).
        type: int
        required: false

author:
    - Sergey Sedov (@sergeeximius)
'''

EXAMPLES = r'''
'''

RETURN = r'''
'''

import os
import subprocess
import select
import json

from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import b

def cmd(command):
    # This is code from https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/service.py
    # Copyright: (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
    # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
    lang_env = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C')
    p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=lang_env)
    stdout = b("")
    stderr = b("")
    fds = [p.stdout, p.stderr]
    # Wait for all output, or until the main process is dead and its output is done.
    while fds:
        rfd, wfd, efd = select.select(fds, [], fds, 1)
        if not (rfd + wfd + efd) and p.poll() is not None:
            break
        if p.stdout in rfd:
            dat = os.read(p.stdout.fileno(), 4096)
            if not dat:
                fds.remove(p.stdout)
            stdout += dat
        if p.stderr in rfd:
            dat = os.read(p.stderr.fileno(), 4096)
            if not dat:
                fds.remove(p.stderr)
            stderr += dat
    p.wait()
    return (p.returncode, stdout, stderr)

def jobInfo(jobname):
    rc, stdout, stderr = cmd(["veeamconfig", "job", "info", "--name", jobname])
    ji = {}
    for line in to_text(stdout).split('\n'):
        if ("Repository name" in line):
            ji["reponame"] = line.split(':')[1].strip()
        if ("Pre-freeze command" in line):
            ji["prefreeze"] = line.split(':')[1].strip()
        if ("Include Directory" in line):
            ji["includedirs"] = line.split(':')[1].strip()
        if ("Max points" in line):
            ji["maxpoints"] = line.split(':')[1].strip()
        si = scheduleInfo(jobname)
        ji["rundays"] = si["rundays"]
        ji["runat"] = si["runat"]
    return ji

def scheduleInfo(jobname):
    rc, stdout, stderr = cmd(["veeamconfig", "schedule", "show", "--jobName", jobname])
    si = {}
    for line in to_text(stdout).split('\n'):
        if ("Every day" in line):
            si["rundays"] = "All"
        if ("Days" in line):
            si["rundays"] = line.split(':')[1].strip()
        if ("At" in line):
            si["runat"] = line.split(':', 1)[1].strip()
    return si

def main():
    module_args = dict(
        type=dict(type='str', required=True, choices=['job', 'list']),
        state=dict(type='str', required=False, choices=['absent', 'present']),
        prefreeze=dict(type='path', required=False),
        includedirs=dict(type='path', required=False),
        name=dict(type='str', required=False),
        reponame=dict(type='str', required=False),
        maxpoints=dict(type='str', required=False),
        rundays=dict(type='str', required=False),
        runat=dict(type='str', required=False)
    )
    required_if_args = [
        ['type', 'job', ['name', 'state']],
        ['state', 'present', ['includedirs', 'reponame']]
    ]
    required_together_args=[
        [ 'rundays', 'runat' ]
    ]
    result = dict(
        changed=False,
    )
    module = AnsibleModule(
        argument_spec=module_args,
        required_if=required_if_args,
        required_together=required_together_args,
        supports_check_mode=True
    )
    if module.check_mode:
        module.exit_json(**result)

    if module.params['type'] == 'list':
        rc, stdout, stderr = cmd(["veeamconfig", "job", "list"])
        if rc == 0:
            result['jobs'] = []
            for str in to_text(stdout).split('\n')[1:-1]:
                result['jobs'].append(str.split()[0])
        else:
            module.fail_json(msg=json.dumps(to_text(stderr)), **result)

    if module.params['type'] == 'job':
        if module.params['state'] == 'present':
            cmd_txt = ["veeamconfig", "job", "create", "fileLevel", "--name", module.params['name'], "--includeDirs", module.params['includedirs'], "--repoName", module.params['reponame']]
            if module.params['prefreeze'] != None:
                cmd_txt.append("--prefreeze")
                cmd_txt.append(module.params['prefreeze'])
            if module.params['maxpoints'] != None:
                cmd_txt.append("--maxPoints")
                cmd_txt.append(module.params['maxpoints'])
            if module.params['rundays'] != None:
                if module.params['rundays'].lower() == "all":
                    cmd_txt.append("--daily")
                else:
                    cmd_txt.append("--weekdays")
                    cmd_txt.append(module.params['rundays'])
            if module.params['runat'] != None:
                cmd_txt.append("--at")
                cmd_txt.append(module.params['runat'])
        else:
            cmd_txt = ["veeamconfig", "job", "delete", "--name", module.params['name']]
        rc, stdout, stderr = cmd(cmd_txt)
        if rc == 0:
            result['message'] = json.dumps(to_text(stdout).split('\n')[0])
            result['changed'] = True
        else:
            if ("already exists" in to_text(stderr).split('\n')[0]):
                ji = jobInfo(module.params['name'])
                for key in ji:
                    if ji[key].lower().replace(' ', '') != module.params[key].lower().replace(' ', ''):
                        if key in ["rundays", "runat"]:
                            cmd_txt = ["veeamconfig", "schedule", "set", "--jobName", module.params['name']]
                            if module.params['rundays'] != None:
                                if module.params['rundays'].lower() == "all":
                                    cmd_txt.append("--daily")
                                else:
                                    cmd_txt.append("--weekdays")
                                    cmd_txt.append(module.params['rundays'])
                            if module.params['runat'] != None:
                                cmd_txt.append("--at")
                                cmd_txt.append(module.params['runat'])
                        else:
                            cmd_txt = ["veeamconfig", "job", "edit", "fileLevel", "--includeDirs", module.params['includedirs'], "--repoName", module.params['reponame']]
                            if module.params['prefreeze'] != None:
                                cmd_txt.append("--prefreeze")
                                cmd_txt.append(module.params['prefreeze'])
                            if module.params['maxpoints'] != None:
                                cmd_txt.append("--maxPoints")
                                cmd_txt.append(module.params['maxpoints'])
                            cmd_txt.append("for")
                            cmd_txt.append("--name")
                            cmd_txt.append(module.params['name'])
                        rc, stdout, stderr = cmd(cmd_txt)
                        if rc == 0:
                            result['changed'] = True
                            result['message'] = json.dumps(to_text(stdout).split('\n')[0])
                        else:
                            result['message'] = json.dumps(to_text(stderr).split('\n')[0])
                        break
                    else:
                        result['message'] = json.dumps(to_text(stderr).split('\n')[0])
            else:
                module.fail_json(msg=json.dumps(to_text(stderr).split('\n')[0]), **result)

    module.exit_json(**result)

if __name__ == '__main__':
    main()


import time
import dowel
from dowel import logger, tabular
from garage.misc.prog_bar_counter import ProgBarCounter
import numpy as np
import torch
from gym.wrappers import RecordVideo
from tqdm import tqdm

def record_gym_video(env, path, n_episodes=10, policy=None):
    recorder = RecordVideo(env, path)
    obses = recorder.reset()

    pbar = tqdm(range(n_episodes))
    for i_eps in pbar:
        pbar.set_postfix(episode=i_eps + 1)
        for _ in range(env.max_steps):

            # increment steps and episodes
            recorder.step_id += 1
            if recorder.recording:
                recorder.video_recorder.capture_frame()
                recorder.recorded_frames += 1

            if policy:
                adjs = env.get_proximity_adj_mat() if policy.proximity_adj else None
                # print(env.adj_raw)
                # env.render()
                # input()
                avail_actions = env.get_avail_actions()
                actions = policy.get_actions(obses, avail_actions,
                                             adjs=adjs, greedy=True)[0]
            else:
                actions = [env.action_space.sample() for _ in range(env.n_agents)]

            obses, _, done, infos = env.step(actions)

            if done:
                for _ in range(5): # insert a pause
                    recorder.video_recorder.capture_frame()
                recorder.reset()
                break

    recorder.close_video_recorder()

def standard_eval(env, policy, n_episodes=20, greedy=True, load_from_file=False,
                  render=False, recorder=None, max_steps=10000):
    if recorder is not None:
        render = False # force off
    if load_from_file:
        logger.add_output(dowel.StdOutput())
    logger.log('Evaluating policy, {} episodes, greedy = {} ...'.format(
        n_episodes, greedy))
    episode_rewards = []
    pbar = ProgBarCounter(n_episodes)
    for e in range(n_episodes):
        obs = env.reset()
        policy.reset([True])
        terminated = False
        t = 0
        episode_rewards.append(0)
        while not terminated:
            if render:
                env.render()
                # time.sleep(0.05)
            if recorder is not None:
                recorder.capture_frame()
            if not env.centralized:
                # obs.shape = (n_agents, n_envs, obs_dim)
                obs = torch.Tensor(obs).unsqueeze(1) # add n_envs dim
                avail_actions = torch.Tensor(env.get_avail_actions()).unsqueeze(1)
                actions, agent_infos = policy.get_actions(obs,
                    avail_actions, greedy=greedy)
                if len(actions.shape) == 3: # n-d action
                    actions = actions[:, 0, :]
                elif len(actions.shape) == 2: # 1-d action
                    actions = actions[:, 0]
                obs, reward, terminated, info = env.step(actions) # n_env = 1
                terminated = all(terminated)
            else:
                # obs.shape = (n_envs, n_agents * obs_dim)
                obs = np.array([obs])
                avail_actions = np.array([env.get_avail_actions()])
                actions, agent_infos = policy.get_actions(obs,
                    avail_actions, greedy=greedy)
                obs, reward, terminated, info = env.step(actions[0]) # n_env = 1
            t += 1
            if t > max_steps:
                terminated = True
            episode_rewards[-1] += np.mean(reward)
        pbar.inc(1)
    pbar.stop()
    policy.reset([True])
    avg_return = np.mean(episode_rewards)
    logger.log('EvalAvgReturn: {}'.format(avg_return))
    if not load_from_file:
        tabular.record('EvalAvgReturn', avg_return)


__metaclass__ = type

from zope.interface import implementer

from lp.bugs.interfaces.bug import IBugDelta

@implementer(IBugDelta)
class BugDelta:
    """See `IBugDelta`."""

    def __init__(self, bug, bugurl, user,
                 title=None, description=None, name=None,
                 private=None, security_related=None, information_type=None,
                 duplicateof=None, external_reference=None, bugwatch=None,
                 cve=None, attachment=None, tags=None,
                 added_bugtasks=None, bugtask_deltas=None,
                 bug_before_modification=None):
        self.bug = bug
        self.bug_before_modification = bug_before_modification
        self.bugurl = bugurl
        self.user = user
        self.title = title
        self.description = description
        self.name = name
        self.private = private
        self.security_related = security_related
        self.information_type = information_type
        self.duplicateof = duplicateof
        self.external_reference = external_reference
        self.bugwatch = bugwatch
        self.cve = cve
        self.attachment = attachment
        self.tags = tags
        self.added_bugtasks = added_bugtasks
        self.bugtask_deltas = bugtask_deltas


import scrapy
import pandas as pd
import numpy as np
from scrapy.crawler import CrawlerProcess

class IndianBusesSpider(scrapy.Spider):
    name = 'indian_buses_ext'
    user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"

    start_urls = ['https://buses.cardekho.com/filter/bus.html',]

    def parse(self,response):
        for titles in response.xpath('//html/body/section/div/div[3]/div[2]'):
            yield{
                'model':titles.xpath('.//ul').extract(),
                # 'price':titles.xpath('.//div/text()').extract(),
            }

process = CrawlerProcess()
# process.crawl(TractorSpider)
process.crawl(IndianBusesSpider)
process.start()

# /html/body/div[4]/div[1]/div[1]/div[2]/div/div[3]/div[5]/div[2]/div[2]/div/section/div/div/div/div/div/text()
# /html/body/div[4]/div[1]/div[1]/div[2]/div/div[3]/div[5]/div[2]/div[2]/div/section/div/div/div/div
# /html/body/div[4]/div[1]/div[1]/div[2]/div/div[3]/div[5]/div[2]/div[2]/div//div/div/div/div/div[2]/text()


from django.contrib import admin
from django.urls import path,re_path
from negocio import views as nviews

urlpatterns = [
    path('', nviews.home, name="home"),
    path('ingresos/', nviews.ingresos, name="ingresos"),
    path('ingresos/editar/', nviews.editar, name="editar"),
    path('eliminar/<int:id>', nviews.eliminar, name="eliminar"),
    path('ingresos/editar/<int:id>', nviews.editar, name="editar"),
    path('perfil/',nviews.perfil,name='perfil'),
    path('notificaciones/',nviews.notificaciones,name='notificaciones',),
    path('eliminar_noti/<int:id>',nviews.eliminar_noti,name='eliminar_noti'),
]


# --------------------
# ---- the module ----
# --------------------

import cv2
import mediapipe as mp
import time

class PoseEstimator():
    def __init__(self, static_mode=False, upper_body_only=False, smoothness=True, detection_confidence=0.5, tracking_confidence=0.5):
        self.static_mode = static_mode
        self.upper_body_only = upper_body_only
        self.smoothness = smoothness
        self.detection_confidence = detection_confidence
        self.tracking_confidence = tracking_confidence

        self.cap = cv2.VideoCapture(0)

        self.mp_pose = mp.solutions.pose
        self.pose = self.mp_pose.Pose(self.static_mode, self.upper_body_only, self.smoothness, self.detection_confidence, self.tracking_confidence)

        self.mp_draw = mp.solutions.drawing_utils

    def find_pose(self, img, draw=True):
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.pose.process(img_rgb)
        if self.results.pose_landmarks:
            self.mp_draw.draw_landmarks(img, self.results.pose_landmarks, self.mp_pose.POSE_CONNECTIONS)
        return img

    def find_position(self, img, draw=True):
        lm_list = []
        if self.results.pose_landmarks:
            for id, lm in enumerate(self.results.pose_landmarks.landmark):
                h, w, c = img.shape
                cX, cY = int(lm.x * w), int(lm.y * h)
                lm_list.append([id, cX, cY])
                if draw and (id == 0 or id == 1 or id == 4 or id == 13 or id == 14):
                    cv2.circle(img, (cX,cY), 6, (132, 44, 76), cv2.FILLED)
        return lm_list

def main():
    cap = cv2.VideoCapture(0)

    current_time = 0
    previous_time = 0
    frames = 0

    detector = PoseEstimator()

    while True:
        succes, img = cap.read()
        img = detector.find_pose(img)
        lm_list = detector.find_position(img)
        while current_time == previous_time:
            current_time = time.time()
        fps = 1/(current_time-previous_time)
        previous_time = current_time
        cv2.putText(img, str(int(fps)), (5, 35), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 3)

        cv2.imshow("Video", img)
        cv2.waitKey(1)

if __name__ == "__main__":
    main()


from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
from PIL import Image

# Define AES encryption parameters
KEY_SIZE = 16  # AES-128
BLOCK_SIZE = 16  # 128-bit blocks

# Function to encrypt an image file with AES
def encrypt_image(image_path, key):
    # Load image
    image = Image.open(image_path)

    # Convert image to RGB mode if it's in a different mode (e.g., RGBA)
    if image.mode != "RGB":
        image = image.convert("RGB")

    # Convert image to bytes
    image_bytes = image.tobytes()

    # Pad the image bytes to a multiple of the block size
    padded_image_bytes = pad(image_bytes, BLOCK_SIZE)

    # Generate a random initialization vector (IV)
    iv = get_random_bytes(BLOCK_SIZE)

    # Create AES cipher object in CBC mode
    cipher = AES.new(key, AES.MODE_CBC, iv)

    # Encrypt the padded image bytes
    ciphertext = cipher.encrypt(padded_image_bytes)

    # Return the ciphertext and IV
    return ciphertext, iv

# Function to decrypt an image file with AES
def decrypt_image(ciphertext, iv, key, image_path):
    # Create AES cipher object in CBC mode
    cipher = AES.new(key, AES.MODE_CBC, iv)

    # Decrypt the ciphertext
    decrypted_bytes = cipher.decrypt(ciphertext)

    # Unpad the decrypted bytes
    unpadded_bytes = unpad(decrypted_bytes, BLOCK_SIZE)

    # Create an image from the decrypted bytes
    decrypted_image = Image.frombytes("RGB", (image_path.size[0], image_path.size[1]), unpadded_bytes)

    # Save the decrypted image
    decrypted_image.save("decrypted_image.jpg")

# Load the image
image_path = "image.jpg"

# Generate a random 128-bit AES key
key = get_random_bytes(KEY_SIZE)

# Encrypt the image
ciphertext, iv = encrypt_image(image_path, key)

# Save the ciphertext and IV to files
with open("ciphertext.bin", "wb") as f:
    f.write(ciphertext)
with open("iv.bin", "wb") as f:
    f.write(iv)

# Decrypt the image
with open("ciphertext.bin", "rb") as f:
    ciphertext = f.read()
with open("iv.bin", "rb") as f:
    iv = f.read()
decrypt_image(ciphertext, iv, key, Image.open(image_path))


from collections.abc import Sequence
from contextlib import contextmanager

# Third-party imports

# Local imports

# ============================================================================
# Helpers
# ============================================================================

# The below is a helper in constructing xpath queries.
# For example:
#
#     clsname = xpath_clsmatch('hello')
#     xpath = '//div[{clsname}]'.format(clsname=clsname)
#
# The above will have xpath contain:
#
#     //div[contains(concat(' ', normalize-space(@class), ' '), ' hello ')]
def xpath_clsmatch(clsname, *other):
    if isinstance(clsname, str):
        clsnames = [clsname] + list(other)
    elif isinstance(clsname, Sequence):
        if other:
            raise ValueError()
        clsnames = clsname
    else:
        clsnames = [str(clsname)] + list(other)

    item = "contains(concat(' ', normalize-space(@class), ' '), ' {} ')"
    allcfunc = [item.format(n) for n in clsnames]
    return ' and '.join(allcfunc)

@contextmanager
def noop_context():
    """A do nothing context"""
    yield

# ============================================================================
#
# ============================================================================


import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import threading
import time
from darts import TimeSeries
from darts.models import KalmanFilter
from darts.utils import timeseries_generation as tg
from darts.metrics import mape
from darts.models import NBEATSModel,TransformerModel
from sklearn.metrics import r2_score
from darts.dataprocessing.transformers import Scaler, MissingValuesFiller
from sklearn.metrics import mean_absolute_error
import xgboost as xgb
from xgboost import plot_importance, plot_tree
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import MinMaxScaler

def nbeats_v2_model_full(df,forecasting_col,future_limit):
    print("Nbeats_v1 Stars....")
    target = df
    limit = len(target)

    train = target
    val = target

    train = TimeSeries.from_dataframe(train)
    val = TimeSeries.from_dataframe(val)

    model_nbeats = NBEATSModel(
    input_chunk_length=5,
    output_chunk_length=2,
    generic_architecture=False,
    num_stacks=10,
    num_blocks=3,
    num_layers=4,
    layer_widths=512,
    n_epochs=100,
    nr_epochs_val_period=1,
    batch_size=5,
    model_name="nbeats_interpretable_run",
    )

    model_nbeats.fit(series=train, verbose=True)

    future = pd.DataFrame()
    future[forecasting_col] = df[forecasting_col].to_list()

    start_limit = len(future)
    end_limit = start_limit + future_limit

    remaning_values = [0 for x in range(start_limit,end_limit)]
    remaning_data_frame = pd.DataFrame()
    remaning_data_frame[forecasting_col] = remaning_values

    future = future.append(remaning_data_frame,ignore_index=True)

    future_series = TimeSeries.from_series(future)

    pred_series = model_nbeats.historical_forecasts(
    future_series,
    start=start_limit,
    retrain=False,
    verbose=False,)

    pred_array = np.absolute(pred_series.univariate_values())
    return pred_array

def xgboost_model_full(df,forecasting_col,date_col,future_limit,type,end_date):

    print("Xgboost Starts....")
    reg = xgb.XGBRegressor(n_estimators=10000)
    df[forecasting_col] = df[forecasting_col].astype(float)
    df.reset_index(inplace=True)
    df['dayofweek'] = df[date_col].dt.dayofweek
    df['quarter'] = df[date_col].dt.quarter
    df['month'] = df[date_col].dt.month
    df['year'] = df[date_col].dt.year
    df['dayofyear'] = df[date_col].dt.dayofyear
    df['dayofmonth'] = df[date_col].dt.day
    df['weekofyear'] = df[date_col].dt.weekofyear
    df['week']  =  df[date_col].dt.week
    df['weekday'] = df[date_col].dt.weekday
    df['daysInMonth'] = df[date_col].dt.days_in_month
    df['is_quater_start'] = (df[date_col].dt.is_quarter_start).astype('float')
    df['is_quater_end'] =(df[date_col].dt.is_quarter_end).astype('float')
    df['is_leap_year'] = (df[date_col].dt.is_leap_year).astype('float')

    scaler_0 = MinMaxScaler()
    scaler_1 = MinMaxScaler()
    scaler_2 = MinMaxScaler()
    scaler_3 = MinMaxScaler()
    scaler_4 = MinMaxScaler()
    scaler_5 = MinMaxScaler()
    scaler_6 = MinMaxScaler()
    scaler_7 = MinMaxScaler()
    scaler_8 = MinMaxScaler()
    scaler_9 = MinMaxScaler()
    scaler_10 = MinMaxScaler()
    scaler_11 = MinMaxScaler()
    scaler_12 = MinMaxScaler()

    df['dayofweek'] = scaler_0.fit_transform(df[['dayofweek']])
    df['quarter'] = scaler_1.fit_transform(df[['quarter']])
    df['month'] = scaler_2.fit_transform(df[['month']])
    df['year'] = scaler_3.fit_transform(df[['year']])
    df['dayofyear'] = scaler_4.fit_transform(df[['dayofyear']])
    df['dayofmonth'] = scaler_5.fit_transform(df[['dayofmonth']])
    df['weekofyear'] = scaler_6.fit_transform(df[['weekofyear']])
    df['week']  =  scaler_7.fit_transform(df[['week']])
    df['weekday'] = scaler_8.fit_transform(df[['weekday']])
    df['daysInMonth'] = scaler_9.fit_transform(df[['daysInMonth']])
    df['is_quater_start'] = scaler_10.fit_transform(df[['is_quater_start']])
    df['is_quater_end'] = scaler_11.fit_transform(df[['is_quater_end']])
    df['is_leap_year'] = scaler_12.fit_transform(df[['is_leap_year']])

    limit = len(df[forecasting_col])

    train_X_data = df[['dayofweek','dayofyear','dayofmonth','weekofyear','week','weekday','daysInMonth','is_leap_year']].iloc[0:limit,:]
    train_Y_data = df[[forecasting_col]].iloc[0:limit,:]

    reg.fit(train_X_data, train_Y_data,
        eval_set=[(train_X_data, train_Y_data)],
        early_stopping_rounds=1000,
        verbose=False)

    future_dates = None
    if(type=='day'):
        future_dates = pd.date_range(start=str(end_date), periods=future_limit)
    elif(type=='week'):
        future_dates = pd.date_range(start=str(end_date), periods=future_limit,freq="W")
    elif(type=='month'):
        future_dates = pd.date_range(start=str(end_date), periods=future_limit,freq="M")
    elif(type=='year'):
        future_dates = pd.date_range(start=str(end_date), periods=future_limit,freq="Y")

    test_df = pd.DataFrame()
    test_df[date_col] = future_dates

    test_df['dayofweek'] = test_df[date_col].dt.dayofweek
    test_df['quarter'] = test_df[date_col].dt.quarter
    test_df['month'] = test_df[date_col].dt.month
    test_df['year'] = test_df[date_col].dt.year
    test_df['dayofyear'] = test_df[date_col].dt.dayofyear
    test_df['dayofmonth'] = test_df[date_col].dt.day
    test_df['weekofyear'] = test_df[date_col].dt.weekofyear
    test_df['week']  =  test_df[date_col].dt.week
    test_df['weekday'] = test_df[date_col].dt.weekday
    test_df['daysInMonth'] = test_df[date_col].dt.days_in_month
    test_df['is_quater_start'] = (test_df[date_col].dt.is_quarter_start).astype('float')
    test_df['is_quater_end'] =(test_df[date_col].dt.is_quarter_end).astype('float')
    test_df['is_leap_year'] = (test_df[date_col].dt.is_leap_year).astype('float')

    test_df['dayofweek'] = scaler_0.transform(test_df[['dayofweek']])
    test_df['quarter'] = scaler_1.transform(test_df[['quarter']])
    test_df['month'] = scaler_2.transform(test_df[['month']])
    test_df['year'] = scaler_3.transform(test_df[['year']])
    test_df['dayofyear'] = scaler_4.transform(test_df[['dayofyear']])
    test_df['dayofmonth'] = scaler_5.transform(test_df[['dayofmonth']])
    test_df['weekofyear'] = scaler_6.transform(test_df[['weekofyear']])
    test_df['week']  =  scaler_7.transform(test_df[['week']])
    test_df['weekday'] = scaler_8.transform(test_df[['weekday']])
    test_df['daysInMonth'] = scaler_9.transform(test_df[['daysInMonth']])
    test_df['is_quater_start'] = scaler_10.transform(test_df[['is_quater_start']])
    test_df['is_quater_end'] = scaler_11.transform(test_df[['is_quater_end']])
    test_df['is_leap_year'] = scaler_12.transform(test_df[['is_leap_year']])

    test_df = test_df[['dayofweek','dayofyear','dayofmonth','weekofyear','week','weekday','daysInMonth','is_leap_year']]
    preds= reg.predict(test_df)
    return preds

def transformers_model_full(df,forecasting_col,future_limit):
    target = df

    train = target
    val = target

    train = TimeSeries.from_dataframe(train)
    val = TimeSeries.from_dataframe(val)

    model = TransformerModel(
            input_chunk_length=12,
            output_chunk_length=1,
            batch_size=4,
            n_epochs=100,
            model_name="transformer",
            nr_epochs_val_period=10,
            d_model=16,
            nhead=2,
            num_encoder_layers=10,
            num_decoder_layers=10,
            dim_feedforward=128,
            dropout=0.5,
            activation="relu",
            random_state=42,
            save_checkpoints=True,
            force_reset=True,)

    model.fit(series=train, val_series=val, verbose=True)

    future = pd.DataFrame()
    future[forecasting_col] = df[forecasting_col].to_list()

    start_limit = len(future)
    end_limit = start_limit + future_limit

    remaning_values = [0 for x in range(start_limit,end_limit)]
    remaning_data_frame = pd.DataFrame()
    remaning_data_frame[forecasting_col] = remaning_values

    future = future.append(remaning_data_frame,ignore_index=True)

    future_series = TimeSeries.from_series(future)

    pred_series = model.historical_forecasts(
    future_series,
    start=start_limit,
    retrain=False,
    verbose=False,)

    # pred_array = np.absolute(pred_series.univariate_values())
    pred_array = pred_series.univariate_values()
    return pred_array


#! /usr/bin/env python3

from .pkt_dispatch import PacketDispatcher
from collections import deque
from scapy.all import sniff
from threading import Thread
from time import sleep

class PacketIfaceDispatcher(PacketDispatcher):
    '''Definition of PacketExtractor for NIC packet extraction
    Args:
        pkt_src: str <- network interface (wlan0, eth0, lo, ...)
        maxbuf: int <- max. number of packets able to hold without readings
        filtr: str <- tcpdump-like filters (BPF)
    '''
    def __init__(self, pkt_src=None, maxbuf=100, filtr=""):
        '''
        '''
        super(PacketIfaceDispatcher, self).__init__(pkt_src)
        self._filter = filtr
        self._circbuf = deque(maxlen=maxbuf)
        self._pkt_miss = 0
        self._maxbuf = maxbuf

    def __next__(self):
        '''
        '''
        try:
            thread = Thread(target=self._sniff)
            thread.start()
            while True:
                try:
                    pkt = self._circbuf.popleft()
                except IndexError:
                    sleep(0.5)
                else:
                    return pkt
        except KeyboardInterrupt:
            raise StopIteration
        finally:
            thread.join()

    def _sniff(self):
        '''
        '''
        sniff(iface=self._pkt_src, prn=self._pkt_callback, filter=self._filter,
              store=0)

    def _pkt_callback(self, pkt):
        '''
        '''
        if len(self._circbuf) == self._maxbuf:
            self._pkt_miss += 1
        self._circbuf.append(pkt)


"""
Created on Wed Mar 18 12:55:05 2020

@author: User
"""

import numpy as np
import matplotlib.pylab as plt
import math
import dump

Avogadro = 6.02214076e23
Boltzmann = 1.38064852e-23

def wallHitCheck(pos, vels, box):
    """ This function enforces reflective boundary conditions.
    All particles that hit a wall  have their velocity updated
    in the opposite direction.
    @pos: atomic positions (ndarray)
    @vels: atomic velocity (ndarray, updated if collisions detected)
    @box: simulation box size (tuple)
    """
    ndims = len(box)

    for i in range(ndims):
        vels[((pos[:,i] <= box[i][0]) | (pos[:,i] >= box[i][1])),i] *= -0.4

def integrate(pos, vels, forces, mass, dt):
    """ A simple forward Euler integrator that moves the system in time
    @pos: atomic positions (ndarray, updated)
    @vels: atomic velocity (ndarray, updated)
    """
    pos += vels * dt

    vels += forces * dt / mass[np.newaxis].T

def computeForce(pos, mass, vels, sepP, sepF, permFS, radius, temp, relax, dt, step):
    """ Computes the Langevin force for all particles
    @mass: particle mass (ndarray)
    @vels: particle velocities (ndarray)
    @temp: temperature (float)
    @relax: thermostat constant (float)
    @dt: simulation timestep (float)
    returns forces (ndarray)
    """

    natoms, ndims = vels.shape

    time = step * dt

    #Magnetic Force
    switch = 0.15e-11
    B0_og  = 0.8e-3

    if switch <= time < 3*switch:
        B0 = (B0_og/switch)*time - B0_og
        f = -1
    elif 3*switch <= time:
        B0=0
        f =1
    else:
        B0 = -(B0_og/switch)*time + B0_og
        f = 1
    dis = 100*1e-8/500
    c = (((4/3)*math.pi*radius**3 * (sepP - sepF) * B0**2)/permFS)
    a = 1e-3

    magForce = (-1*f) * c[np.newaxis].T * (pos+dis)/((pos+dis)**2 + a**2)**4
    magForce[:, [0,2]] = 0

    #Brownian motion

    sigma = np.sqrt(2.0 * mass * temp * Boltzmann / (relax * dt))

    noise = np.random.randn(natoms, ndims) * sigma[np.newaxis].T

    #Viscous force

    visc = - (vels * mass[np.newaxis].T) / relax

    force = magForce + noise + visc

    return force

def removeCOM(pos, mass):
    """ Removes center of mass motion. This function is not used. """
    pos -= np.dot(mass, pos) / mass.sum()

def run(**args):
    """ This is the main function that solves Langevin's equations for
    a system of natoms usinga forward Euler scheme, and returns an output
    list that stores the time and the temperture.

    @natoms (int): number of particles
    @temp (float): temperature (in Kelvin)
    @mass (float): particle mass (in Kg)
    @relax (float): relaxation constant (in seconds)
    @dt (float): simulation timestep (s)
    @nsteps (int): total number of steps the solver performs
    @box (tuple): simulation box size (in meters) of size dimensions x 2
    e.g. box = ((-1e-9, 1e-9), (-1e-9, 1e-9)) defines a 2D square
    @ofname (string): filename to write output to
    @freq (int): write output every 'freq' steps

    @[radius]: particle radius (for visualization)

    Returns a list (of size nsteps x 2) containing the time and temperature.

    """

    natoms, dt, temp, box = args['natoms'], args['dt'], args['temp'], args['box']
    mass, relax, nsteps, radius = args['mass'], args['relax'], args['steps'], args['radius']
    sepP, sepF, permFS = args['sepP'], args['sepF'], args['permFS']
    ofname, freq = args['ofname'], args['freq']

    dim = len(box)

    #set to appear in a specific location of the box
    pos = np.random.rand(natoms,dim)
    for i in range(dim):
        if i == 0:
            pos[:,i] = box[0][1]/10
        if i == 1:
            pos[:,i] = box[1][1]/2
        if i == 2:
            pos[:,i] = 0

    if 'initial_vels' not in args:
        vels = np.random.rand(natoms,3)
    else:
        vels = np.ones((natoms,3)) * args['initial_vels']

    mass = np.ones(natoms) * mass #/ Avogadro #I define in grams in paras
    radius = np.ones(natoms) * radius
    step = 0

    output = []

    while step <= nsteps:
        step += 1

        # Compute all forces
        forces = computeForce(pos, mass, vels, sepP, sepF, permFS, radius, temp, relax, dt, step)

        # Move the system in time
        integrate(pos, vels, forces, mass, dt)

        # Check if any particle has collided with the wall
        wallHitCheck(pos,vels,box)

        # Compute output (temperature)
        ins_temp = np.sum(np.dot(mass, (vels - vels.mean(axis=0))**2)) / (Boltzmann * dim * natoms)
        output.append([step * dt, ins_temp])

        if not step%freq:
            dump.writeOutput(ofname, natoms, step, box, radius=radius, pos=pos, v=vels)

    return np.array(output)

if __name__ == '__main__':

#    params = {
#        'natoms': 10,
#        'temp': 300,
#        'mass': 0.001,
#        'radius': 120e-12,
#        'relax': 1e-13,
#        'dt': 1e-15,
#        'steps': 10000,
#        'freq': 100,
#        'box': ((0, 1e-8), (0, 1e-8), (0, 240e-12)),
#        'sepP': 7200,
#        'sepF': 0.712e-6,
#        'permFS': math.pi*4e-7,
#        'ofname': 'traj-hydrogen-3D.dump'
#        }

    """These are the new parameters that no longer cause an overload"""
    params = {
        'natoms': 10,
        'temp': 3e13,
        'mass': 27e-12,
        'radius': 50e-6,
        'initial_vels': 1e-6,
        'relax': 1e-6,
        'dt': 1e-8,
        'steps': 100000,
        'freq': 1000,
        'box': ((0, 1e-3), (0, 1e-3), (0, 1e-3)),
        'sepP': 7200,
        'sepF': 0.712e-6,
        'permFS': math.pi*4e-7,
        'ofname': 'traj-hydrogen-3D.dump'
        }
    output = run(**params)

    plt.plot(output[:,0], output[:,1])
    plt.xlabel('Time (ps)')
    plt.ylabel('Temp (K)')
    plt.show()


import pandas as pd
import numpy as np
import os
import torch
from torch.utils.data import DataLoader
from sklearn.preprocessing import StandardScaler,MinMaxScaler

class TECDataset:
    def __init__(self, path, mode = 'maxmin', patch_size = 3, target_hour = 24, input_history = 1, depths_len = 3) -> None:
        self.path = path
        self.patch_size = patch_size * (2**depths_len)
        self.target_hour = target_hour
        self.input_history = input_history
        self.val, self.val2 = 0, 0
        self.mode = "None"
        self.year = ""
        self.tec_data = self.get_data()
        if mode == 'maxmin':
            self.val, self.val2 = self.maxmin()
            self.mode = 'max_min'
        elif mode == 'z_score': #z_score
            self.val, self.val2 = self.z_scores()
            self.mode = 'z_score'
        self._input, self.target = self.create_dataset()

    #Let the data go through normalization
    def maxmin(self) :
        max_tec = 500
        min_tec = 0
        self.tec_data = np.array(self.tec_data) / max_tec
        return max_tec, min_tec

    #Let the data go through standardization
    def z_scores(self) :
        standardizer  = StandardScaler()
        scaler = standardizer.fit(self.tec_data)
        self.tec_data = scaler.transform(self.tec_data)
        mean, std = scaler.mean_, scaler.scale_
        return std[0], mean[0]

    #read data from csv
    def get_data(self):
        dataset = []
        for file_name in os.listdir(self.path):
            self.year += (file_name.split('.')[0] + ', ')
            list_col = np.arange(5123)
            data = pd.read_csv(os.path.join(self.path, file_name), skiprows= 5, usecols = list_col)
            data = data.values
            dataset.append(data[:, 11:])

        tec_data = dataset[0]
        for i in range(1, len(dataset)):
            tec_data = np.vstack((tec_data,dataset[i]))
        return tec_data

    #Create a dataset that fits the model
    def create_dataset(self) :
        _input, target = [], []
        for idx in range(self.input_history-1, len(self.tec_data)):
            if idx + self.target_hour>= len(self.tec_data):
                break
            input_history_TEC, target_TEC = [], []
            if _input:
                input_history_TEC = _input[-1][1:]
                input_history_TEC.append(self.create_input(idx))
            else:
                for i in range(self.input_history):
                    input_history_TEC.append(self.create_input(i))
            # target_world = self.create_target(idx)
            target_TEC.append(self.create_input(i+self.target_hour))
            _input.append(input_history_TEC)
            target.append(target_TEC)
        return np.array(_input), np.array(target)

    def create_input(self, idx):
        world = []
        for longitude in range(71):
            latitude = []
            for lat in range(72):
                latitude.append(self.tec_data[idx][longitude*72 + lat])
            world.append(latitude)
        world.append([0 for _ in range(72)])
        return world

    def create_target(self, idx):
        #target_size = [9, 576]
        patch_count = 72*72//(self.patch_size*self.patch_size)
        target_world = [[]for _ in range(patch_count)]

        for longitude in range(71):
            for lat in range(72):
                patch_idx = (longitude//self.patch_size)*(72//self.patch_size) + lat//self.patch_size
                target_world[patch_idx].append(self.tec_data[idx + self.target_hour][longitude*72 + lat])

        # padding zero to the last latitude
        for patch_idx in range(patch_count - (72//self.patch_size), patch_count): #padding zero to the final latitude
            for _ in range(self.patch_size):
                target_world[patch_idx].append(0)
        # print(np.array(target_world).shape[:])
        return target_world

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

    def __getitem__(self, idx):
        _input = torch.tensor(self._input[idx], dtype=torch.float)
        target = torch.tensor(self.target[idx], dtype=torch.float)
        return _input, target,

if __name__ == '__main__':
    dataset = TECDataset('../data/valid', mode = 'maxmin', target_hour = 4, input_history = 1)
    dataloader = DataLoader(dataset, batch_size=24)
    for data in dataloader:
        print(np.shape(data[0]))
        print(np.shape(data[1]))
        print(data)
        input()
        break


import os
import img2pdf

dirname = "./"
imgs = []

for r, _, f in os.walk(dirname):
  for filename in f:
    if filename.endswith(".jpg") or filename.endswith(".png"):
      imgs.append(os.path.join(r, filename))
imgs.sort()

with open("output.pdf", "wb") as f:
  f.write(img2pdf.convert(imgs))


from django.db import models

PATENTE_CHOICES = (
    ('S2', 'Soldado de 2ª Classe'),
    ('S1', 'Soldado de 1ª Classe'),
    ('CB', 'Cabo'),
    ('3S', '3º Sargento'),
    ('2S', '2º Sargento'),
    ('1S', '1º Sargento'),
    ('SO', 'Sub-Oficial'),
    ('AP', 'Aspirante'),
    ('2T', '2º Tenente'),
    ('1T', '1º Tenente'),
    ('CP', 'Capitão'),
    ('MJ', 'Major'),
    ('TC', 'Tenente-Coronel'),
    ('CL', 'Coronel'),
    ('BR', 'Brigadeiro'),
    ('MB', 'Major-Brigadeiro'),
    ('TB', 'Tenente-Brigadeiro'),
    ('CV', 'Civil'),
    )

PROJETO_CHOICES = (
    ('FABDOC', 'FABDOC'),
    ('SPCOA', 'SPCOA'),
    ('SAGEM', 'SAGEM'),
    ('C2/CENTRAL', 'C2/CENTRAL'),
    ('C2/DEA2', 'C2/DEA2'),
    ('C2/HERCULES 2', 'C2/HERCULES 2'),
    ('IFFM4BR', 'IFFM4BR'),
    ('SIMULADOR T27', 'SIMULADOR T27'),
    ('A29 RV', 'A29 RV'),
)

class Feedback(models.Model):
    nome = models.CharField(max_length=30)
    patente = models.CharField(max_length=2, choices= PATENTE_CHOICES)
    email = models.EmailField(max_length=50)
    contato = models.CharField(max_length=11)
    projeto = models.CharField(max_length=13,choices = PROJETO_CHOICES)
    feedback = models.TextField(max_length=500)

def __str__(self):
    return self.nome
# Create your models here.


"""empty message

Revision ID: 09ce168a116c
Revises: 2dad0ed2abac
Create Date: 2023-03-26 15:14:41.113830

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '09ce168a116c'
down_revision = '2dad0ed2abac'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('likess',
    sa.Column('like_id', sa.Integer(), nullable=False),
    sa.Column('like_state', sa.Boolean(), nullable=True),
    sa.Column('liked_id', sa.Integer(), nullable=False),
    sa.Column('liker_id', sa.String(), nullable=False),
    sa.ForeignKeyConstraint(['liked_id'], ['content.id'], ),
    sa.ForeignKeyConstraint(['liker_id'], ['subscriber.id'], ),
    sa.PrimaryKeyConstraint('like_id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('likess')
    # ### end Alembic commands ###


import boto3
import logging
from urllib.parse import urlparse

log = logging.getLogger(__name__)

def _botocore_default_region(session=None):
    if session is None:
        session = boto3.session.get_available_regions('s3')
    return session.region_name

def _auto_find_region(session=None):
    region_name = _botocore_default_region(session)

    if region_name is None:
        raise ValueError('AWS region name is not supplied and default region can not be found')

    return region_name

def make_s3_client(region_name=None,
                   session=None,
                   profile=None,
                   use_ssl=True):
    if session is None:
        if profile is None:
            session = boto3.session.Session()
        else:
            session = boto3.session.Session(profile_name=profile)

    if region_name is None:
        region_name = _auto_find_region(session)

    protocol = 'https' if use_ssl else 'http'

    s3 = session.client('s3',
                        endpoint_url='{}://s3.{}.amazonaws.com'.format(protocol, region_name))
    return s3

def _s3_url_parse(url):
    uu = urlparse(url)
    return uu.netloc, uu.path.lstrip('/')

def s3_ls_dir(uri, s3=None, aws_profile=None):
    bucket, prefix = _s3_url_parse(uri)

    if len(prefix) > 0 and not prefix.endswith('/'):
        prefix += '/'

    if not s3:
        s3 = make_s3_client(profile=aws_profile)

    paginator = s3.get_paginator('list_objects_v2')

    for page in paginator.paginate(Bucket=bucket,
                                   Prefix=prefix,
                                   Delimiter='/'):
        sub_dirs = page.get('CommonPrefixes', [])
        files = page.get('Contents', [])

        for p in sub_dirs:
            yield 's3://{bucket}/{path}'.format(bucket=bucket, path=p['Prefix'])

        for o in files:
            yield 's3://{bucket}/{path}'.format(bucket=bucket, path=o['Key'])

def s3_fetch(url, s3=None, aws_profile=None, **kwargs):
    if not s3:
        s3 = make_s3_client(profile=aws_profile)

    bucket, key = _s3_url_parse(url)
    oo = s3.get_object(Bucket=bucket, Key=key, **kwargs)
    return oo['Body'].read()