import time
from contextlib import contextmanager

import pytest

from helpers.cluster import ClickHouseCluster
from helpers.network import PartitionManager
from helpers.test_tools import TSV
from helpers.client import CommandRequest

cluster = ClickHouseCluster(__file__)

node1 = cluster.add_instance('node1', config_dir='configs', with_zookeeper=True)
node2 = cluster.add_instance('node2', config_dir='configs', with_zookeeper=True)
nodes = [node1, node2]

@pytest.fixture(scope="module")
def started_cluster():
    try:
        cluster.start()

        for node in nodes:
            node.query('''
CREATE TABLE simple (date Date, id UInt32)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/0/simple', '{replica}', date, id, 8192);
'''.format(replica=node.name))
            node.query("INSERT INTO simple VALUES (0, 0)")

            node.query('''
CREATE TABLE simple2 (date Date, id UInt32)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/0/simple2', '{replica}', date, id, 8192);
'''.format(replica=node.name))

        yield cluster

    finally:
        pass
        cluster.shutdown()

def test_deduplication_window_in_seconds(started_cluster):
    node = node1

    node.query("INSERT INTO simple2 VALUES (0, 0)")
    time.sleep(1)
    node.query("INSERT INTO simple2 VALUES (0, 0)") # deduplication works here
    node.query("INSERT INTO simple2 VALUES (0, 1)")
    assert TSV(node.query("SELECT count() FROM simple2")) == TSV("2\n")

    # wait clean thread
    time.sleep(2)

    assert TSV.toMat(node.query("SELECT count() FROM system.zookeeper WHERE path='/clickhouse/tables/0/simple2/blocks'"))[0][0] == "1"
    node.query("INSERT INTO simple2 VALUES (0, 0)") # deduplication doesn't works here, the first hash node was deleted
    assert TSV.toMat(node.query("SELECT count() FROM simple2"))[0][0] == "3"

def check_timeout_exception(e):
    s = str(e)
    #print s
    assert s.find('timed out!') >= 0 or s.find('Return code: -9') >= 0

# Currently this test just reproduce incorrect behavior that sould be fixed
def test_deduplication_works_in_case_of_intensive_inserts(started_cluster):
    inserters = []
    fetchers = []

    for node in nodes:
        host = node.ip_address

        inserters.append(CommandRequest(['/bin/bash'], timeout=10, stdin="""
set -e
for i in `seq 1000`; do
    clickhouse-client --host {} -q "INSERT INTO simple VALUES (0, 0)"
done
""".format(host)))

        fetchers.append(CommandRequest(['/bin/bash'], timeout=10, stdin="""
set -e
for i in `seq 1000`; do
    res=`clickhouse-client --host {} -q "SELECT count() FROM simple"`
    if [[ $res -ne 1 ]]; then
        echo "Selected $res elements! Host: {}" 1>&2
        exit -1
    fi;
done
""".format(host, node.name)))

    # There were not errors during INSERTs
    for inserter in inserters:
        try:
            inserter.get_answer()
        except Exception as e:
            check_timeout_exception(e)

    # There were not errors during SELECTs
    for fetcher in fetchers:
        try:
            fetcher.get_answer()
        except Exception as e:
            pass
            # Uncomment when problem will be fixed
            # check_timeout_exception(e)


__author__ = 'achmed'

import zmq
from zmq.devices import Device

from ...config.configwrapper import ConfigWrapper

import logging
logger = logging.getLogger(__name__)

class ZMQSampleBroker(object):
    def __init__(self, cfg):
        self.cfg = ConfigWrapper(other=cfg.get('zmq'))
        self.pulladdr = cfg.get('pulladdr', '0.0.0.0')
        self.pubaddr = cfg.get('pubaddr', '0.0.0.0')
        self.pullport = cfg.get('pullport', 59000)
        self.pubport = cfg.get('pubport', 59001)

    def initialize(self):
        self.forwarddevice = Device(zmq.QUEUE, zmq.PULL, zmq.PUB)
        self.forwarddevice.bind_in('tcp://%s:%d' % (self.pulladdr, self.pullport))
        self.forwarddevice.bind_out('tcp://%s:%d' % (self.pubaddr, self.pubport))
        logger.info('created broker in: PULL@%s:%d out: PUB@%s:%d' % (self.pulladdr, self.pullport, self.pubaddr, self.pubport))

    def start(self):
        logger.info('starting broker')
        self.forwarddevice.start()


num = int(input())
answer = 1
count = 0

for i in range(1, num+1) :
  answer *= i

answer = str(answer)
answer = answer[::-1]

for i in answer :
  if i != '0' :
    break
  else :
    count += 1

print(count)

백준/Silver/1676. 팩토리얼 0의 개수/팩토리얼 0의 개수.py

팩토리얼 0의 개수.py


def print_message():
    nomor1 = 15
    nomor2 = 30
    hasil = nomor1 + nomor2
    print("The result is " + str(hasil))

# call the function
print_message()

def masak():
    bahan = "air"
    alat = "panci"
    print("hari ini gabriel masak", bahan, "dengan alat", alat)

masak()


"""Scans IP network
usage: NetScan.py [-h] [-b <IP>] [-p <ports>] [-m] [-t ##] [-s <IP>]

TCP net scan will scan the default IP interfaces entire network when no additional options are specified

options:
  -h, --help            show this help message and exit
  -b <IP>, --bind <IP>  Specify a local IP interface to bind.
  -p <ports>, --ports <ports>
                        Specify ports to scan default is -p "22,80,81,443,3389,8080"
  -m, --module          Run as a module, supresses extra output, returns a list object.
  -t ##, --threads ##   Specify number of threads. Default is 30
  -s <IP>, --scantarget <IP>
                        Specify an IP, mutiple IPs, range or network to scan examples below

The scantarget can be specified as a single ip, multiple ips, a range, or a CIDR network.

Use Examples:
>Netscan.py -s 192.168.1.1   #Scan a single IP.
>Netscan.py -s 192.168.1.2,192.168.1.9,10.1.1.3 -b 192.168.1.207    #Scan multiple IPs, Binds to specific local IP
>Netscan.py -s 192.168.1.1-192.168.1.10 -p 443,1433      #Scan a range, for ports 443 and 1433
>Netscan.py -m -s 192.168.1.1/24 -t 50    #Scan an entire network with 50 threads, suppresses extra output"""

import ipaddress
import socket
import argparse
import threading
import time
from queue import Queue

# 3rd party modules
import ifaddr
import getmac
import OuiLookup

def get_local_ip():
    """attempts a connection to find default interface ip"""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    return s.getsockname()[0]

def get_hosts(network):
    """get_hosts() will return all the hosts within a provided network, range"""
    network = ipaddress.IPv4Network(network, strict=False)
    hosts_obj = network.hosts()
    hosts = []
    for i in hosts_obj:
        hosts.append(str(i))
    return hosts

def get_netmask(localip):
    """Loop through local network interfaces to find default ip match and return its netmask"""
    adapters = ifaddr.get_adapters()
    for adapter in adapters:
        if str(localip) in str(adapter):
            for ip in adapter.ips:
                network = str(ip.ip) + "/" + str(ip.network_prefix)
                return network

def threader(ports, modulecall):
    """pulls an worker from the queue and processes it"""
    while True:
        # gets an worker from the queue
        worker = q.get()
        # Run the example job with the avail worker in queue (thread)
        portscan(worker, ports, modulecall)
        # completed with the job
        q.task_done()

def portscan(target, ports, modulecall):
    # worker function to scan a specific hosts ports
    # print('DBG Scanning target', target, ports )
    openports = []
    mac = getmac.get_mac_address(ip=target)
    for port in ports:
        # Attempt to connect to each port
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(1)
            s.connect((target, int(port)))
            s.shutdown(2)
            openports.append(port)
        except:
            pass
    if mac:
        # Attempt to lookup oui vendor
        ouilookup = OuiLookup.OuiLookup().query(mac)
        oui = str(ouilookup).replace("'", "-").split("-")
        # Test if ouilookup worked by len, if not say nothing
        if len(oui) >= 4 and len(oui[3]) >= 3:
            if modulecall:
                mfg = str(oui[3])
            else:
                mfg = "Vendor: " + str(oui[3])
        else:
            mfg = " "
        # Test if we found open ports
        if len(openports) >= 1:
            reports = "Listening ports: " + str(openports)
        else:
            reports = " "
        if modulecall:
            # print("Mod dbg: ",str(target) , mac , mfg , str(openports))
            modulereturn.append([str(target), mac, mfg, str(openports)])
        else:
            # output findings to console
            print(str(target).ljust(15), mfg, reports)
    return ()

def clisetup():
    # Get command line options if any, and set defaults.
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="TCP network scanner will scan the default IP interfaces entire network when no additional options are specified",
        epilog="""The scantarget can be specified as a single ip, multiple ips, a range, or a CIDR network.
\nUse Examples:
>NetScan.py -s 192.168.1.1   #Scan a single IP.
>NetScan.py -s 192.168.1.2,192.168.1.9,10.1.1.3 -b 192.168.1.207    #Scan multiple IPs, Binds to specific local IP
>NetScan.py -s 192.168.1.1-192.168.1.10 -p 443,1433      #Scan a range, for ports 443 and 1433
>NetScan.py -m -s 192.168.1.1/24 -t 50    #Scan an entire network with 50 threads, suppresses extra output """,
    )
    parser.add_argument(
        "-b",
        "--bind",
        type=str,
        metavar="<IP>",
        help="Specify a local IP interface to bind.",
    )
    parser.add_argument(
        "-p",
        "--ports",
        type=str,
        metavar="<ports>",
        help='Specify ports to scan default is -p "22,80,81,443,3389,8080"',
    )
    parser.add_argument(
        "-m",
        "--module",
        action="store_true",
        help="Run as a module, supresses extra output, returns a list object.",
    )
    parser.add_argument(
        "-t",
        "--threads",
        type=int,
        metavar="##",
        help="Specify number of threads. Default is 30",
    )
    parser.add_argument(
        "-s",
        "--scantarget",
        type=str,
        metavar="<IP>",
        help="Specify an IP, mutiple IPs, range or network to scan examples below",
    )
    args = parser.parse_args()
    return args

def netscan(**kwargs):
    if __name__ == "__main__":
        # If run directly, get command line args convert to a dict
        clivars = vars(clisetup())
        # Remove any values set to 'None' so kwargs.get works properly
        kwargs = {k: v for k, v in clivars.items() if v is not None}
    # kwargs.get(key[, default])
    threads = int(kwargs.get("threads", "30"))
    modulecall = kwargs.get("module", "")
    ip = kwargs.get("bind", get_local_ip())
    # check for ports parameter
    if kwargs.get("ports") is None:
        getports = "22,80,443,3389,8080"
    else:
        getports = kwargs.get("ports")
    splitports = getports.split(",")
    ports = []
    for port in splitports:
        ports.append(int(port))
    # make targethosts for for each case 1.1.1.1 , range 1.1.1.1-1.1.1.2 or network 1.1.2.1/12
    if "/" in str(kwargs.get("scantarget")):
        # if / assume a proper network
        targethosts = get_hosts(kwargs.get("scantarget"))
    elif "-" in str(kwargs.get("scantarget")):
        # if - assume a range
        targethosts = []
        tgtrange = kwargs.get("scantarget").split("-")
        start_ip = int(ipaddress.IPv4Address(tgtrange[0]))
        end_ip = int(ipaddress.IPv4Address(tgtrange[1]))
        end_ip = end_ip + 1
        # print("Range: ", start_ip , end_ip)
        for ip_int in range((start_ip), (end_ip)):
            targethosts.append(str(ipaddress.IPv4Address(ip_int)))
    elif "," in str(kwargs.get("scantarget")):
        # comma seperated list of hosts
        targethosts = []
        targets = str(kwargs.get("scantarget")).split(",")
        for target in targets:
            targethosts.append(target)
    elif kwargs.get("scantarget"):
        # single target
        targethosts = [kwargs.get("scantarget")]
    else:
        # else deault to scan whole local network
        getmask = get_netmask(ip)
        targetnetwork = ipaddress.ip_network(getmask, False)
        targethosts = get_hosts(targetnetwork)

    if len(targethosts) >= 100:
        # slow warning if large number of hosts
        if not modulecall:
            print(
                "========== Network has "
                + str(len(targethosts))
                + " hosts, this scan may take several minutes to complete. =========="
            )
            print(" ")
    modulereturn = runscan(targethosts, ports, ip, threads, modulecall)
    return modulereturn

def runscan(targethosts, ports, ip, threads, modulecall):
    # accept parameters and setup the scan run
    global q, modulereturn
    if modulecall:
        # Create a list to collect return results
        modulereturn = []
    q = Queue()
    for worker in targethosts:
        # populate the queue
        q.put(worker)

    # how many threads
    for x in range(threads):
        t = threading.Thread(target=threader, args=(ports, modulecall))
        # classifying as a daemon, so they will die when the main dies
        t.daemon = True
        # begins, must come after daemon definition
        t.start()
    start = time.time()
    # wait until the thread terminates.
    q.join()
    if __name__ == "__main__":
        # if called directly
        if modulecall:
            # Directly called and modulecall set
            # print("DBG Module Call Dump:")
            print(modulereturn)
        else:
            print("Scan completed.")
            return ()
    else:
        # Called from python
        if not modulecall:
            print("Scan completed.")
            return ()
        else:
            return modulereturn

def main():
    netscan()

# call main
if __name__ == "__main__":
    main()


import time
import cheminfo
import ase
import numpy as np
import rmsd
from ase import units
from ase.io.trajectory import Trajectory
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
from ase.md.verlet import VelocityVerlet
from ase.md.nvtberendsen import NVTBerendsen
from ase.optimize import BFGS
from narupa.ase import ASEImdServer
from calculators import QMLCalculator, get_calculator, get_calculator_rdkit

from ase.calculators.lj import LennardJones
from ase.calculators.emt import EMT

FILENAME_ALPHAS = "data/training_alphas.npy"
FILENAME_REPRESENTATIONS = "data/training_X.npy"
FILENAME_CHARGES = "data/training_Q.npy"

tmpdir = "_tmp_/"

def dump_xyz(atoms, filename):
    coordinates = atoms.get_positions()
    nuclear_charges = atoms.get_atomic_numbers()
    nuclear_charges = [cheminfo.convert(x) for x in nuclear_charges]

    x = rmsd.set_coordinates(nuclear_charges, coordinates)

    f = open(filename, 'w')
    f.write(x)
    f.close()

    return

def serve_md(nuclear_charges, coordinates, calculator=None, temp=None):
    """
    """

    if calculator is None:
        parameters = {}
        parameters["offset"] = -97084.83100465109
        parameters["sigma"] = 10.0
        alphas = np.load(FILENAME_ALPHAS)
        X = np.load(FILENAME_REPRESENTATIONS)
        Q = np.load(FILENAME_CHARGES, allow_pickle=True)
        alphas = np.array(alphas, order="F")
        X = np.array(X, order="F")
        calculator = QMLCalculator(parameters, X, Q, alphas)

    # SET MOLECULE
    molecule = ase.Atoms(nuclear_charges, coordinates)
    molecule.set_calculator(calculator)

    # SET ASE MD
    # Set the momenta corresponding to T=300K
    MaxwellBoltzmannDistribution(molecule, 200 * units.kB)

    time = 0.5

    if temp is None:
        # We want to run MD with constant energy using the VelocityVerlet algorithm.
        dyn = VelocityVerlet(molecule, time * units.fs)  # 1 fs time step.

    else:
        dyn = NVTBerendsen(molecule, time*units.fs, temp, time*units.fs, fixcm=False)

    # SET AND SERVE NARUPA MD
    imd = ASEImdServer(dyn)

    while True:
        imd.run(10)
        print("")
        dump_xyz(molecule, tmpdir + "snapshot.xyz")

    return

def main():

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--mol', action='store', default=None, help='Load molecule for live simulation', metavar="FILE")
    parser.add_argument('--model', action='store', default="ethanol", help='')
    parser.add_argument('--temp', action='store', default=None, help='')
    args = parser.parse_args()

    # input

    if args.mol is None:
        nuclear_charges = np.array([6, 6, 8, 1, 1, 1, 1, 1, 1])
        coordinates = np.array(
            [[0.07230959, 0.61441211, -0.03115568],
            [-1.26644639, -0.27012846, -0.00720771],
            [1.11516977, -0.30732869, 0.06414394],
            [0.10673943, 1.44346835, -0.79573006],
            [-0.02687486, 1.19350887, 0.98075343],
            [-2.06614011, 0.38757505, 0.39276693],
            [-1.68213881, -0.60620688, -0.97804526],
            [-1.18668224, -1.07395366, 0.67075071],
            [1.37492532, -0.56618891, -0.83172035]])
    else:

        nuclear_charges, coordinates = rmsd.get_coordinates_xyz(args.mol)

    # model and simulation

    if args.temp is not None:
        args.temp = float(args.temp)

    calculator = get_calculator(args.model)

    serve_md(nuclear_charges, coordinates, calculator=calculator, temp=args.temp)

    return

if __name__ == "__main__":
    main()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
import torch
from torch import nn

class Reshape(nn.Module):
    def forward(self, x):
        return x.view(-1, 1, 28, 28)

net = nn.Sequential(
    Reshape(),
    # 输入通道为1，输出通道为6的卷积层   (28+4-5+1)*(28+4-5+1) = 28*28
    nn.Conv2d(1, 6, kernel_size=5, padding=2), nn.Sigmoid(),
    # 平均池化层2x2，移动不重叠   28/2 = 14
    nn.AvgPool2d(kernel_size=2, stride=2),
    # 输入通道为6，输出通道为16的卷积层  (14-5+1)*(14-5+1) = 10*10
    nn.Conv2d(6, 16, kernel_size=5), nn.Sigmoid(),
    # 平均池化层2x2，移动不重叠   10/2 = 5
    nn.AvgPool2d(kernel_size=2, stride=2),
    # 将四维卷积层变为一维向量
    nn.Flatten(),
    # 16个输出再乘以5*5的池化层转为向量为16*5*5 = 400
    nn.Linear(16 * 5 * 5, 120), nn.Sigmoid(),
    nn.Linear(120, 84), nn.Sigmoid(),
    # 输出0到9
    nn.Linear(84, 10)
)

X = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32)
for layer in net:
    X = layer(X)
    print(f'{layer.__class__.__name__}output shape\t->\t{X.shape}')


from random import randint

class Missions(object):
    masters = (("Master Yoda", 90),
               ("Master Windu", 80),
               ("Master Plo Koon", 70),
               ("Master Shaak Ti", 65),
               ("Master Obi-Wan Kenobi", 75),
               ("Master Qui-Gon Jinn", 60))

    lords = (("Darth Sidious", 90),
             ("Darth Vader", 80),
             ("Darth Maul", 70),
             ("Darth Plagueis", 65),
             ("Darth Bane", 75),
             ("Darth Tyranus", 60))

    briefs = ("You must be the new apprentice, let's see what you've got!",
              "You came higly recommended by our masters, show me why!",
              "The force is inside all of us, but can you manage it?",
              "Anger leads to both suffering and power, remember that!",
              "Let's see if you can use both the force and the lightsaber!",
              "The force is strong with you, but you must prove yourself!",
              "With great power comes great responsibility, remember it!",
              "You are only as strong as your greatest weakness young one!",
              "The dark side of the force is a pathway to many abilities!",
              "After death, we all become one with the force!",
              "Our masters have given you an assignment, try and do well!",
              "Your mind and power are about to get twisted, get ready!",
              "Remember to always use the force with respect and humility!",
              "We've got a task for you, do your best to solve it!",
              "The more you train, the more powerful you will become!",
              "The force is the one thing all living things share in common!",
              "The lightsaber is a weapon tha combines our old and new ways!",
              "Think through the situation and don't rush into a battle!",
              "Opportunities must be created by you, young apprentice!",
              "Never lose your lightsaber, that weapon is your life!",
              "Both the dark side and the light side have their advantages!",
              "The war between light and darkness is one that is eternal!")

    @classmethod
    def get_random_master(cls):
        index = randint(0, 5)
        return cls.masters[index]

    @classmethod
    def get_random_lord(cls):
        index = randint(0, 5)
        return cls.lords[index]

    @classmethod
    def get_random_brief(cls):
        index = randint(0, 21)
        return cls.briefs[index]


import random
import time
import cv2
import numpy as np
from ultralytics import YOLO
def checkimage(image):
    my_file = open("coco.txt", "r")
    # reading the file
    data = my_file.read()
    # replacing end splitting the text | when newline ('\n') is seen.
    class_list = data.split("\n")
    my_file.close()

    # print(class_list)

    # Generate random colors for class list
    detection_colors = []
    for i in range(len(class_list)):
        r = random.randint(0, 255)
        g = random.randint(0, 255)
        b = random.randint(0, 255)
        detection_colors.append((b, g, r))

    model = YOLO("best.pt", "v8")

    frame_wid = 640
    frame_hyt = 480

    cap = image
    #cap = cv2.VideoCapture("test-video.m4v")
        # Capture frame-by-frame
    # ret, frame = cap.read()
    # if not ret:
    #     print("Can't receive frame (stream end?). Exiting ...")
    detect_params = model.predict(source=[image], conf=0.55, save=False)

    # Convert tensor array to numpy
    DP = detect_params[0].numpy()

    return True

        # Terminate run when "Q" pressed

    # When everything done, release the capture


from PySide2 import QtWidgets, QtGui, QtCore

class Header(QtWidgets.QWidget):
    """Header class for collapsible group"""

    def __init__(self, name, content_widget,pref_name, prefs,  bg_color, margins):
        """
        Constructor
        :param name: Name for the header
        :param content_widget: Widget containing child elements
        :param pref_name
        :param prefs
        :param bg_color: Color of the background
        :param margins
        """
        super(Header, self).__init__()
        self.pref_name = pref_name
        self.prefs = prefs
        self.content = content_widget
        self.expand_ico = QtGui.QPixmap(":teDownArrow.png")
        self.collapse_ico = QtGui.QPixmap(":teRightArrow.png")

        stacked = QtWidgets.QStackedLayout(self)
        stacked.setStackingMode(QtWidgets.QStackedLayout.StackAll)
        background = QtWidgets.QLabel()
        background.setStyleSheet(".QLabel{background-color: "+bg_color+"; border-radius:0px}")

        widget = QtWidgets.QWidget()
        widget.setStyleSheet("margin-left: "+str(margins[0])+"px;"+
                             "margin-top: "+str(margins[1])+"px;"+
                             "margin-right: "+str(margins[2])+"px;"+
                             "margin-bottom: "+str(margins[3])+"px;")
        layout = QtWidgets.QHBoxLayout(widget)

        self.icon = QtWidgets.QLabel()
        self.icon.setPixmap(self.expand_ico)
        layout.addWidget(self.icon)
        layout.setContentsMargins(10, 0, 10, 0)

        font = QtGui.QFont()
        font.setBold(True)
        label = QtWidgets.QLabel(name)
        label.setSizePolicy(QtWidgets.QSizePolicy.Expanding,QtWidgets.QSizePolicy.Minimum)
        label.setFont(font)

        layout.addWidget(label)

        stacked.addWidget(widget)
        stacked.addWidget(background)
        background.setMinimumHeight(layout.sizeHint().height() * 1.4)

        if self.pref_name in self.prefs:
            pref = self.prefs[self.pref_name]
            if "collapsed" in pref and pref["collapsed"]:
                self.collapse()

    def mousePressEvent(self, *args):
        """
        Handle mouse events, call the function to toggle groups
        :param args
        :return:
        """
        self.expand() if not self.content.isVisible() else self.collapse()

    def expand(self):
        """
        Expand the collapsible widget in the UI
        :return:
        """
        self.content.setVisible(True)
        self.icon.setPixmap(self.expand_ico)
        pref = self.prefs[self.pref_name] if self.pref_name in self.prefs else {}
        pref["collapsed"] = False
        self.prefs[self.pref_name] = pref

    def collapse(self):
        """
        Collapse the collapsible widget in the UI
        :return:
        """
        self.content.setVisible(False)
        self.icon.setPixmap(self.collapse_ico)
        pref = self.prefs[self.pref_name] if self.pref_name in self.prefs else {}
        pref["collapsed"] = True
        self.prefs[self.pref_name] = pref

class BobCollapsibleWidget(QtWidgets.QWidget):
    """Class for creating a collapsible group similar to how it is implement in Maya
        Examples:
            Simple example of how to add a CollapsibleLayout to a QVBoxLayout and attach a QGridLayout
            >>> layout = QtWidgets.QVBoxLayout()
            >>> container = BobCollapsibleWidget("Group")
            >>> layout.addWidget(container)
            >>> content_layout = QtWidgets.QGridLayout(container.contentWidget)
            >>> content_layout.addWidget(QtWidgets.QPushButton("Button"))
    """

    def __init__(self, name,pref_name,  prefs, bg_color="rgb(60, 60, 60)", widget_color="rgb(93, 93, 93)", margins=None):
        """
        Constructor
        :param name: Name for the header
        :param pref_name
        :param prefs
        :param bg_color
        :param widget_color
        :param margins
        """
        super(BobCollapsibleWidget, self).__init__()
        if margins is None:
            margins = [8, 0, 0, 0]

        layout = QtWidgets.QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        self._content_widget = QtWidgets.QWidget()
        self._content_widget.setStyleSheet(".QWidget{background-color: "+bg_color+";border-radius:0px;}")
        self.setStyleSheet(".QWidget{background-color: "+widget_color+";border-radius:5px}")
        header = Header(name, self._content_widget,pref_name, prefs, bg_color,margins)
        layout.addWidget(header)
        layout.addWidget(self._content_widget)

        # assign header methods to instance attributes so they can be called outside of this class
        self.collapse = header.collapse
        self.expand = header.expand
        self.toggle = header.mousePressEvent

    @property
    def contentWidget(self):
        """
        Getter for the content widget
        Returns: Content widget
        """
        return self._content_widget


class MinStack:

    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stac = []

    def push(self, x):
        if self.stac:
            s = min(x,self.stac[-1][1])
            self.stac.append((x,s))
        else:
            self.stac.append((x,x))
        print(self.stac)
    def pop(self):
        self.stac.pop()

    def top(self):
        return self.stac[-1][0]

    def getMin(self):
        if self.stac:
            return self.stac[-1][1]
        else:
            return None
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
s = MinStack()
s.push(1)
s.push(2)
s.push(0)
print(s.top(),s.getMin())
s.pop()
print(s.top(),s.getMin())


import csv

items = []
HMMpair = []
HMMvalue = []
previous = tags[0]
words = []
tags = []
wordHMM = dict()
wordTagPairs = dict()
HMMindex = 0

with open('C:/Users/ashwi/Desktop/Computational Linguistics/HW 3/TDA.txt', 'r') as file:
    reader = csv.reader(file,delimiter=' ')
    for x in reader:
        items = x

for item in items:
    if(item == "@_IN" or item == "@_SYM"):
        item = "@_@"
    try:
        word, tag = item.split("_")
    except:
        continue
    words.append(word)
    tags.append(tag)

print(len(words))

for i in range(1,len(tags)):
    current = tags[i]
    tagPair = str(previous + "," + current)
    if(tagPair in HMMpair):
        HMMvalue[HMMpair.index(tagPair)] = HMMvalue[HMMpair.index(tagPair)] + 1;
    else:
        HMMpair.append(tagPair)
        HMMvalue.append(1)
        HMMindex = HMMindex + 1
    previous = current

print(len(HMMpair))
print(len(HMMvalue))

HMM = dict(zip(HMMpair, HMMvalue))
uniqueTagNames = set(tags)
uniqueTags = dict.fromkeys(uniqueTagNames, 0)

for tag in tags:
    uniqueTags[tag] = uniqueTags[tag] + 1

for key, value in uniqueTags.items():
    print(key + "=" + str(value))

HMM = dict(zip(HMMpair, HMMvalue))
uniqueTagNames = set(tags)
uniqueTags = dict.fromkeys(uniqueTagNames, 0)

for tag in tags:
    uniqueTags[tag] = uniqueTags[tag] + 1
for key, value in uniqueTags.items():
    print(key + "=" + str(value))

for key, value in HMM.items():
    first, second = key.split(",")
    HMM[key] = value / uniqueTags[first]

for i in range(len(words)):
    pair = str(words[i] + "," + tags[i])
    if(pair in wordTagPairs):
        wordTagPairs[pair] = wordTagPairs[pair] + 1
    else:
        wordTagPairs[pair] = 1

for key, value in wordTagPairs.items():
    first, second = key.split(",")
    wordHMM[key] = value / uniqueTags[second]

File = open("C:/Users/ashwi/Desktop/Computational Linguistics/HW 3/sigmas.txt", "w")
for key, value in HMM.items():
    first, second = key.split(",")
    first = str("\'" + first + "\'")
    second = str("\'" + second + "\'")
    key = str(first + "," + second)
    File.write("sigma(" + key + "," + str(value) + ")\n")
File.close()

File = open("C:/Users/ashwi/Desktop/Computational Linguistics/HW 3/taus.txt", "w")
for key, value in wordHMM.items():
    first, second = key.split(",")
    first = str("\'" + first + "\'")
    second = str("\'" + second + "\'")
    key = str(first + "," + second)
    File.write("tau(" + key + "," + str(value) + ")\n")
File.close()


### ETL script for generating input tables to model
### main point: ETL JHU covid-19 case and mortality data

# todo: refactor

## HERE! -> not handled here in python -> Serial Interval Table -> would be worthwhile to reproduce the R for that here
## "serial interval table" <--> that discretized gamma distribution
## so that this script does indeed produce all the required input tables for the model
## alternatively, could just generate that discretized gamma distribution in the R code itself, pre-simulation
## I don't like that idea -> will try to reproduce results in python - but not now -> other more pressing tasks now

import os
import numpy as np
import warnings

warnings.simplefilter(
    action="ignore", category=FutureWarning
)  # suppress pandas "future warning"
import pandas as pd

def makeCaseMortalityTable(dirPath):

    print("\n~ COVID-19 CASE-MORTALITY TABLE ~")

    # E
    print("--- extracting JHU covid-19 case and mortality data ---")

    # what's the issue here? sometimes stalls for some reason -> fetching data from git

    # fetch the JHU time-series data
    # see: https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/README.md
    jhu = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/"
    cases_csv = "time_series_covid19_confirmed_US.csv"
    deaths_csv = "time_series_covid19_deaths_US.csv"
    casesOrig = pd.read_csv(jhu + cases_csv)
    deathsOrig = pd.read_csv(jhu + deaths_csv)

    # T
    print("--- transforming case and mortality data ---")

    # 1. process cases df to match form of EU table
    cases = casesOrig

    # filter for IL
    cases = cases.loc[cases["Province_State"] == "Illinois"]

    ## get daily counts -> make this a fn

    # take this out, put it back after compute
    dc = cases.copy().iloc[:, 11:].astype(np.int64)

    # fix monotone errors
    # rows
    for i in range(len(dc.iloc[:, 0])):
        # cols
        for j in range(len(dc.columns) - 1):
            # per row:
            # if the next value is bigger than current value -> set next val to current val
            # ensures monotonically increasing sequences
            # allows coherent compute of daily counts (increments -> prevents negative increments)
            if dc.iloc[i, j] > dc.iloc[i, j + 1]:
                dc.iloc[i, j + 1] = dc.iloc[i, j]

    # look, they're all monotone now!
    # print(dc.apply(lambda x: x.is_monotonic, axis=1).unique())

    dailyCounts = cases.copy()

    # replace cumulative counts with daily counts (i.e., increments)
    dailyCounts.iloc[:, 12:] = dc.diff(axis=1, periods=1).iloc[:, 1:]

    # treat the daily counts as our working table from here forward
    # note: need to do the same procedure for the death counts
    cases = dailyCounts

    # notice from data repo readme:
    # US counties: UID = 840 (country code3) + XXXXX (5-digit FIPS code)

    # drop redundant columns
    cases = cases.drop(cases.columns[[1, 2, 3, 4, 7, 10]], axis=1)

    # "melt" df into desired form
    # i.e., rows correspond to dates
    idVars = ["UID", "Admin2", "Province_State", "Lat", "Long_"]
    cases = cases.melt(id_vars=idVars, var_name="Date", value_name="Cases")

    # define mapping from our df to theirs
    # first just map the data
    # then can fix column names and python etl code accordingly

    # now, process deaths data, see if we can take the same path to the same outcome

    # 2. process deaths data
    deaths = deathsOrig

    # filter for Illinois
    deaths = deaths.loc[deaths["Province_State"] == "Illinois"]

    # compute increments from cumulative counts - same steps as case table

    # take this out, put it back after compute
    # note: this table has "population" column - 1 additional column, so dates start at 12, not 11
    dc = deaths.copy().iloc[:, 12:].astype(np.int64)

    ## fix monotone errors -> make this a fn
    # rows
    for i in range(len(dc.iloc[:, 0])):
        # cols
        for j in range(len(dc.columns) - 1):
            if dc.iloc[i, j] > dc.iloc[i, j + 1]:
                dc.iloc[i, j + 1] = dc.iloc[i, j]

    # look, they're all monotone now!
    # print(dc.apply(lambda x: x.is_monotonic, axis=1).unique())

    dailyCounts = deaths.copy()
    # replace cumulative counts with daily counts (i.e., increments)
    dailyCounts.iloc[:, 13:] = dc.diff(axis=1, periods=1).iloc[:, 1:]
    # take a look at cook (county)
    dailyCounts[dailyCounts["Admin2"] == "Cook"]

    # now working with daily deaths, not cumulative deaths
    deaths = dailyCounts

    # drop redundant columns -> keep population column
    deaths = deaths.drop(deaths.columns[[1, 2, 3, 4, 7, 10]], axis=1)

    # "melt" df into desired form
    # i.e., rows correspond to dates
    idVars = ["UID", "Admin2", "Province_State", "Lat", "Long_", "Population"]
    deaths = deaths.melt(id_vars=idVars, var_name="Date", value_name="Deaths")

    # note: processing of the two tables is exactly the same except
    # for the deaths table having the extra ID column "Population"
    # so can refactor/streamline code to make it concise and much prettier

    # next task: "merge" the deaths and cases tables

    # danke: https://stackoverflow.com/questions/19125091/pandas-merge-how-to-avoid-duplicating-columns
    # merge df's
    # i.e., inject population and deaths data from deaths df into cases df
    cols_to_use = deaths.columns.difference(cases.columns)
    ILCaseAndMortality = pd.merge(
        cases, deaths[cols_to_use], left_index=True, right_index=True, how="outer"
    )

    # cut out rows where Admin2 is "Out of IL" or "Unassigned" (both have population 0)
    ILCaseAndMortality = ILCaseAndMortality.loc[ILCaseAndMortality["Population"] > 0]

    # rename some columns; improve readability
    renameColsMap = {
        "UID": "CountyID",  # fairly certain this is appropriate, though will double check
        "Admin2": "Town",  # ? -> probably a better name for this
        "Province_State": "State",
        "Lat": "Latitude",
        "Long_": "Longitude",
    }
    ILCaseAndMortality = ILCaseAndMortality.rename(renameColsMap, axis=1)

    # now order the columns nicely
    columnOrder = [
        "Date",
        "Cases",
        "Deaths",
        "CountyID",
        "Town",
        "State",
        "Population",
        "Latitude",
        "Longitude",
    ]

    ILCaseAndMortality = ILCaseAndMortality[columnOrder]

    # looks good -> save it
    # suppressing this for now, so as not to create "unused" tables -> simplify output of this script
    # ILCaseAndMortality.to_csv(dirPath + "/ILCaseAndMortality.csv")

    # next:
    # 1. preserving this table; modify this table to exactly match the scheme of the Euro table
    # 2. save that as a separate file
    # 3. run the model with that table as input
    # 6. refactor all this; sketch plan for actual script(s) (!)

    df = ILCaseAndMortality.copy()

    df["month"], df["day"], df["year"] = df["Date"].str.split("/").str

    # drop extra columns
    df = df.drop(["State", "Latitude", "Longitude"], axis=1)

    # rename remaining columns to match Euro table
    ToEuroColumnsMap = {
        "Date": "dateRep",
        "Cases": "cases",
        "Deaths": "deaths",
        "CountyID": "countryterritoryCode",  # ?
        "Town": "countriesAndTerritories",  # ?
        "Population": "popData2018",
    }

    df = df.rename(ToEuroColumnsMap, axis=1)

    # populate geoID with Town also, just to populate it
    df["geoId"] = df["countriesAndTerritories"]

    # reorder the columns to match Euro table # probably don't hardcode this -> make proper config file (?)
    CaseMortalityColumnOrder = [
        "dateRep",
        "day",
        "month",
        "year",
        "cases",
        "deaths",
        "countriesAndTerritories",
        "geoId",
        "countryterritoryCode",
        "popData2018",
    ]
    df = df[CaseMortalityColumnOrder]

    print("--- saving transformed case and mortality data  ---")

    # okay, done, now save it
    p = dirPath + "/ILCaseAndMortalityV1.csv"
    df.to_csv(p)

    countyIDList = ILCaseAndMortality["CountyID"].unique()

    population_df = (
        ILCaseAndMortality[["CountyID", "Population"]].copy().drop_duplicates()
    )

    return (p, countyIDList, population_df)

def makeInterventionsTable(dirPath, countyIDList):
    print("\n~ INTERVENTIONS TABLE ~")

    # -> should remove all their tables, comparisons to their tables etc.
    # self-contained ETL -> we can have our own config -> not the old EU tables

    # task: make a table for IL by county that looks like their covariates table
    # only column is lockdown
    # dates for all counties the same
    # admittedly a dumb table for now, but will get extended later

    print("--- constructing interventions table ---")

    # counties correspond to countries
    ourCovariates = pd.DataFrame(countyIDList, columns=["Country"])

    print("--- loading intervention: lockdown ---")

    # date of IL lockdown: Saturday, March 21st, 2020
    # source: https://www.chicagotribune.com/coronavirus/ct-coronavirus-illinois-shelter-in-place-lockdown-order-20200320-teedakbfw5gvdgmnaxlel54hau-story.html
    ourCovariates["lockdown"] = "2020-03-21"

    print("--- saving covariates table ---")

    # save this new table
    p = dirPath + "/ILInterventionsV1.csv"
    ourCovariates.to_csv(p)

    return p

def makeIFRTable(dirPath, population_df):

    print("\n~ IFR TABLE ~")
    print("--- constructing IFR table ---")

    ourIFR = population_df

    # now need:
    # 1. fatality ratio per stratum (?)
    # 2. relative frequency pre stratum
    # for now, all counties will be treated the same - same age strata rel freq, same fatality ratio

    # IL age distribution
    # actually here's IL age distribution by county according to US census:
    # https://censusreporter.org/data/table/?table=B01001&geo_ids=04000US17,01000US,050|04000US17&primary_geo_id=04000US17

    # for starters applying same distribution to each county, for sake of just running the model as soon as possible
    # later (tomorrow) can get the by-county resolution for age distribution -> HERE! fixme.
    # ILAgeDistr = pd.read_csv("./notebooks/IL-Age-Distr/ageDistr.csv")

    # for now, manually entering this
    # source:
    # https://censusreporter.org/profiles/04000US17-illinois/
    ILAgeDistr = {
        "0-9": [0.119],
        "10-19": [0.131],
        "20-29": [0.136],
        "30-39": [0.135],
        "40-49": [0.127],
        "50-59": [0.132],
        "60-69": [0.114],
        "70-79": [0.067],
        "80+": [0.039],
    }

    ageDist = pd.DataFrame(ILAgeDistr)

    ourIFR[list(ageDist)] = pd.DataFrame(
        np.repeat(ageDist.values, len(ourIFR.index), axis=0)
    )
    strata = list(ILAgeDistr.keys())
    ourIFR[strata] = ourIFR[strata].mul(ourIFR["Population"], axis=0)

    # okay great, so now we have IL population by-county by-age-bracket
    # note: again recall that this is just the IL state-wide distribution applied to each county's population -> HERE! fixme.
    # the by-county age distributions are available and can be easily worked in
    # will just take a bit of data transforming/mapping etc.
    # it's on the todo list

    # next: add the "weighted_fatality" column
    # right now the value for this column doesn't really matter
    # so will do the simplest thing for now and extend later

    # this is the paper ICL consulted for picking their ifr numbers:
    # https://www.thelancet.com/journals/laninf/article/PIIS1473-3099(20)30243-7/fulltext
    ourIFR["weighted_fatality"] = 0.00657

    # reorder columns
    ourIFRColumnOrder = ["CountyID", "weighted_fatality", "Population"] + strata
    ourIFR = ourIFR[ourIFRColumnOrder]

    # save this
    # suppressing this for now -> simplify/minimize number of files generated by this script
    # ourIFR.to_csv(dirPath + "/ILWeightedFatality.csv")

    # now map to euro table for input to model
    mapILToEuroIFR = {
        "CountyID": "country",
        "10-19": "Oct-19",  # extraordinarily painful to look at, but will be fixed soon enough
        "Population": "population",
    }

    ILInputIFR = ourIFR.copy()
    ILInputIFR = ILInputIFR.rename(mapILToEuroIFR, axis=1)
    # fill placeholder values for redundant columns, to match their df exactly ..
    ILInputIFR["Region, subregion, country or area *"] = ILInputIFR["country"]
    ILInputIFR["Unnamed: 0"] = ILInputIFR.index

    # reorder to match their order
    EUColumnOrder = [
        "Unnamed: 0",
        "Region, subregion, country or area *",
        "0-9",
        "Oct-19",
        "20-29",
        "30-39",
        "40-49",
        "50-59",
        "60-69",
        "70-79",
        "80+",
        "weighted_fatality",
        "population",
        "country",
    ]
    ILInputIFR = ILInputIFR[EUColumnOrder]

    print("--- saving IFR table ---")

    # save this
    p = dirPath + "/ILWeightedFatalityV1.csv"
    ILInputIFR.to_csv(p)

    return p

# paper: https://arxiv.org/abs/2004.00756
# data: https://github.com/JieYingWu/COVID-19_US_County-level_Summaries
def fetchSocEc(dirPath):
    print("\n~ SOC-EC TABLE ~")

    print("--- fetching soc-ec table ---")
    path = "https://raw.githubusercontent.com/JieYingWu/COVID-19_US_County-level_Summaries/master/data/counties.csv"
    df = pd.read_csv(path)

    print("--- saving soc-ec table ---")
    p = dirPath + "/SocEc.csv"
    df.to_csv(p)

    return p

# wow I want to really, thoroughly refactor all this so bad
# make a class - the whole thing -> not the most time pressing task though

if __name__ == "__main__":

    # put tables here
    dirPath = "../modelInput"
    os.makedirs(dirPath, exist_ok=True)

    p1, countyIDList, population_df = makeCaseMortalityTable(dirPath)
    p2 = makeInterventionsTable(dirPath, countyIDList)
    p3 = makeIFRTable(dirPath, population_df)
    p4 = fetchSocEc(dirPath)

    print("\n")
    print("tables successfully written to these paths:")
    print("\t", p1)
    print("\t", p2)
    print("\t", p3)
    print("\t", p4)
    print("\n")


import json
from pathlib import Path
import time
import glob
from argparse import ArgumentParser
import math

import tensorflow as tf
from tensorflow import keras

from airsim_gd.dataset.tf_dataset import TFCVDataset
from airsim_gd.vision.nn.utils import train_utils
from airsim_gd.vision.nn import fast_scnn
from airsim_gd.dataset.utils.data import FEATURE_DESCRIPTION

GEN_MODELS = {'fast_scnn': fast_scnn.generate_model}

class CVTrainer(object):
    # TODO: Load params from config yaml file
    def __init__(self, *, train_dir, val_dir, save_dir,
                 arch, sess_name, input_names, output_names,
                 epochs, seed=None, end_learning_rate, batch_size=16, n_aux=0,
                 max_depth, sim_seg_id_list_path, seg_id_map_path):
        self.train_dir = train_dir
        self.val_dir = val_dir
        self.save_dir = Path(save_dir, sess_name)
        self.save_dir.mkdir(exist_ok=True, parents=True)

        self.arch = arch
        self.input_layer_names = input_names
        self.output_layer_names = output_names

        self.epochs = epochs
        self.seed = seed
        self.end_learning_rate = end_learning_rate
        self.batch_size = batch_size

        with open(Path(sim_seg_id_list_path)) as f:
            self.sim_seg_id_list = json.load(f)

        with open(Path(seg_id_map_path)) as f:
            self.id2rgb_dict = json.load(f)

        self.sess_name = time.ctime(time.time()).replace(':', '.') if sess_name is None else sess_name

        print('\nPrepping training dataset...\n')
        self.train_ds = TFCVDataset(tfr_dir=self.train_dir, seed=self.seed, batch_size=self.batch_size,
                                    max_depth=max_depth, output_names=output_names, seg_id_list=self.sim_seg_id_list).generate_dataset()
        print('\nPrepping validation dataset...\n')
        self.val_ds = TFCVDataset(tfr_dir=self.val_dir, seed=self.seed, batch_size=self.batch_size//2,
                                  max_depth=max_depth, output_names=output_names, seg_id_list=self.sim_seg_id_list).generate_dataset()

        file_list = glob.glob(str(Path(self.train_dir, f'*.tfrecord')))
        tf_example = tf.data.TFRecordDataset(file_list[0])
        tf_example = tf_example.map(lambda x: tf.io.parse_single_example(x, FEATURE_DESCRIPTION))
        tf_example = [data for data in tf_example][0]  # only need one
        self.img_shape = (tf_example['image/height'].numpy(), tf_example['image/width'].numpy(), tf_example['image/channels'].numpy())
        self.n_train_data = len(file_list)

    def get_model(self):
        n_classes = len(self.sim_seg_id_list)

        return GEN_MODELS[self.arch](self.img_shape, n_classes)

    def get_callbacks(self):
        ckpt_path = Path(self.save_dir, 'checkpoints')
        cp_cb = keras.callbacks.ModelCheckpoint(filepath=str(ckpt_path), monitor='val_loss', verbose=1, save_best_only=False)

        es_cb = keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=50,
                                              restore_best_weights=True)
        tb_cb = keras.callbacks.TensorBoard(log_dir=str(self.save_dir), histogram_freq=1, write_graph=True, write_images=False,
                                            update_freq='epoch')
        # img_cb = train_utils.TFImageCallback(model_save_dir=self.save_dir, seg_id_list=self.sim_seg_id_list,
        #                                      input_layer_names=self.input_layer_names, output_layer_names=self.output_layer_names,
        #                                      id22rgb_dict=self.id2rgb_dict)
        return [cp_cb, es_cb, tb_cb]

    def load_model(self):
        return keras.models.load_model(self.save_dir)

    def __call__(self, mode):
        callback_list = self.get_callbacks()
        # steps_per_epoch =

        if mode == 'train':
            print('\nCreating model...\n')
            model, loss_dict, loss_weights = self.get_model()
            decay_steps = self.epochs * self.n_train_data
            learning_rate = keras.optimizers.schedules.PolynomialDecay(0.045, decay_steps, self.end_learning_rate,
                                                                       power=0.5)
            optimizer = keras.optimizers.SGD(momentum=0.9, learning_rate=learning_rate)
            model.compile(loss=loss_dict, loss_weights=loss_weights, optimizer=optimizer, metrics=['accuracy'])

            print('\nModel created. Training now...\n')
            history = model.fit(self.train_ds, epochs=self.epochs, validation_data=self.val_ds, callbacks=callback_list)

        elif mode == 'resume':
            print('\nLoading model...\n')
            model = self.load_model()

            print('\nResuming training...\n')
            history = model.fit(self.train_ds, epochs=self.epochs, validation_data=self.val_ds, callbacks=callback_list)

        else:
            raise ValueError(f"Mode {mode} not recognized.")

        print('\nTraining completed.\n')
        model.save(str(self.save_dir), overwrite=False)
        print('\nModel saved.\n')
        return history

def main(args):
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        try:
            # Currently, memory growth needs to be the same across GPUs
            for gpu in gpus:
                tf.config.experimental.set_memory_growth(gpu, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
        except RuntimeError as e:
            # Memory growth must be set before GPUs have been initialized
            print(e)

    trainer = CVTrainer(train_dir=args.train_dir, val_dir=args.val_dir, save_dir=args.save_dir,
                        arch=args.arch, sess_name=args.sess_name,
                        epochs=args.epochs, seed=args.seed, end_learning_rate=args.end_learning_rate, batch_size=args.batch_size, n_aux=args.n_aux,
                        max_depth=args.max_depth, sim_seg_id_list_path=args.sim_seg_id_list_path, seg_id_map_path=args.seg_id_map_path,
                        input_names=args.input_names, output_names=args.output_names)

    trainer(args.mode)

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument('--train_dir', type=str, default=Path('../data/nominal/train'))
    parser.add_argument('--val_dir', type=str, default=Path('../data/nominal/val'))
    parser.add_argument('--save_dir', type=str, default=Path.cwd().parent.joinpath("model_training"))
    parser.add_argument('--mode', type=str, choices=['train', 'resume'])
    parser.add_argument('--arch', type=str, default='fast_scnn')
    parser.add_argument('--arch_args', type=str, default=None)
    parser.add_argument('--sess_name', type=str, default=time.ctime(time.time()).replace(':', '.'))
    parser.add_argument('--seed', type=int, default=None)
    parser.add_argument('--sim_seg_id_list_path', type=str, default=Path('sim_seg_id_list.json'))
    parser.add_argument('--seg_id_map_path', type=str, default=Path("airsim_gd/dataset/segmentation_id_maps.json"))
    parser.add_argument('--epochs', type=int, default=1000)
    parser.add_argument('--batch_size', type=int, default=16)
    parser.add_argument('--end_learning_rate', type=float, default=0.00001)
    parser.add_argument('--n_aux', type=int, default=0)
    parser.add_argument('--max_depth', type=float, default=100.0)
    parser.add_argument('--input_names', type=str, nargs='+', default=['input_layer'])
    parser.add_argument('--output_names', type=str, nargs='+', default=None)
    args = parser.parse_args()
    main(args)


import argparse
from typing import NoReturn

from Component import Component, NodeType
from LearnerInstance import LearnerInstance
from Message import Message, MessageLearnerCatchUp
from MessageController import MessageController

class Learner(Component):

    def __init__(self, id: int = None, config_path: str = 'config', ttl=1):
        super().__init__(NodeType.Leaner, id, config_path, ttl)
        self.state: LearnerInstance = LearnerInstance()

        self.handler: MessageController = MessageController(self)
        self.send_catch_up_message()

    def handle_message(self, serialized_msg: Message) -> NoReturn:
        message = Message.deserialize(serialized_msg)
        self.handler.handle(message)

    def send_catch_up_message(self) -> NoReturn:
        if self.state.last_ordered_instance == -1:
            self.send(NodeType.Proposer, MessageLearnerCatchUp(self.state.last_ordered_instance, -1))

    def run(self) -> NoReturn:
        self.log.debug('Leaner {} is ready to receive'.format(self.id))
        while True:
            value = self.receive()
            self.thread_pool.submit(self.handle_message, value)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Paxos Learner component')

    parser.add_argument('id', action="store", help='Acceptor ID', type=int)
    parser.add_argument('config_path', action="store", help='Path to config file')
    args = parser.parse_args()

    learner = Learner(args.id, args.config_path)

    try:
        learner.run()
    except (KeyboardInterrupt, SystemExit):
        learner.thread_pool.shutdown()


# -*- coding: utf-8 -*-
"""
Created on Wed Apr 26 01:00:17 2023

@author: shada
"""

from leo_dqn_agents import DQN_agent
# from env_cartpole import CartPoleEnv
from leo_env import LeoEnv
import matplotlib.pyplot as plt

class DRL:
    def __init__(self,num_episodes):

        # Define the environment

        # self.env = CartPoleEnv()

        self.env = LeoEnv()

        state_size = self.env.observation_space
        action_size = self.env.action_space

        # Define the DQN agent

        self.agent = DQN_agent(state_size, action_size, n_episodes=num_episodes)

        self.steps = 0

        self.episodic_mean_loss = []
        self.episodic_mean_reward = []

    def interact(self, state):
        # Agent chooses an action based on the epsilon greedy policy given the current state
        action = self.agent.choose_action(state)

        # The environment returns reward and next state based on the action taken by the agent
        next_state, reward, done = self.env.step(action)

        # Updates the replay buffer, no memory constraints for simplicity

        self.agent.update_replay_buffer(state, action, reward, next_state, done)

        # Updates the current state
        state = next_state

        # Agent learns how to behave in the environment

        loss = self.agent.learn()

        # Updates the no of steps
        self.steps += 1

        # Updates epsilon value
        self.agent.update_epsilon()

        return state, reward, done, loss

    def episodic_learn(self):
        # Learning the model

        for episode in range(self.agent.n_episodes):

            # Initialize training parameters

            state = self.env.reset()

            done = False
            self.steps = 0

            iteration_loss = []
            iteration_reward = []
            iteration = 0

            while not done:
                state, reward, done, loss = self.interact(state)
                iteration_loss.append(loss)

                # print(f"Episode: {episode + 1}/{self.agent.n_episodes}, Iteration: {iteration + 1}, Reward: {reward},Loss: {loss}")
                iteration_reward.append(reward)
                iteration += 1

            self.episodic_mean_loss.append(sum(iteration_loss) / len(iteration_loss))
            self.episodic_mean_reward.append (sum(iteration_reward) / len(iteration_reward))

            # Updates episodic durations
            self.agent.update_episode_durations(self.steps)
            print(f"Episode: {episode + 1}/{self.agent.n_episodes},  Episodic Mean Reward: {sum(iteration_reward) / len(iteration_reward)}")

            # Synchronize target Q network with the policy Q network

            self.agent.sync_target_q(episode)

    def plot_curves(self):
        # Create a new figure for the CDF of average elevation angles
        plt.figure()
        plt.plot(self.episodic_mean_reward)
        plt.xlabel('Episode index')
        plt.ylabel('Mean reward')
        plt.title('Mean reward vs episode')
        plt.show()


class Solution:
    def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
        hq = []
        for i, point in enumerate(points):
            d = (abs(point[0]) ** 2 + abs(point[1]) ** 2) ** 0.5
            if i < k:
                heapq.heappush(hq, (-d, i))
            elif (d < -hq[0][0]):
                heapq.heappushpop(hq, (-d, i))
        results = []
        for pointInfo in hq:
            results.append(points[pointInfo[1]])
        return results


import sys
import json
import pickle
import argparse
from lop.nets.ffnn import FFNN
from lop.nets.linear import MyLinear
from lop.algos.bp import Backprop
from lop.algos.cbp import ContinualBackprop
from lop.utils.miscellaneous import *

def expr(params: {}):
    agent_type = params['agent']
    env_file = params['env_file']
    num_data_points = int(params['num_data_points'])
    to_log = False
    to_log_grad = False
    to_log_activation = False
    beta_1 = 0.9
    beta_2 = 0.999
    weight_decay = 0.0
    accumulate = False
    perturb_scale = 0
    if 'to_log' in params.keys():
        to_log = params['to_log']
    if 'to_log_grad' in params.keys():
        to_log_grad = params['to_log_grad']
    if 'to_log_activation' in params.keys():
        to_log_activation = params['to_log_activation']
    if 'beta_1' in params.keys():
        beta_1 = params['beta_1']
    if 'beta_2' in params.keys():
        beta_2 = params['beta_2']
    if 'weight_decay' in params.keys():
        weight_decay = params['weight_decay']
    if 'accumulate' in params.keys():
        accumulate = params['accumulate']
    if 'perturb_scale' in params.keys():
        perturb_scale = params['perturb_scale']

    num_inputs = params['num_inputs']
    num_features = params['num_features']
    hidden_activation = params['hidden_activation']
    step_size = params['step_size']
    opt = params['opt']
    replacement_rate = params["replacement_rate"]
    decay_rate = params["decay_rate"]
    mt = 10
    util_type='adaptable_contribution'
    init = 'kaiming'
    if "mt" in params.keys():
        mt = params["mt"]
    if "util_type" in params.keys():
        util_type = params["util_type"]
    if "init" in params.keys():
        init = params["init"]

    if agent_type == 'linear':
        net = MyLinear(
            input_size=num_inputs,
        )
    else:
        net = FFNN(
            input_size=num_inputs,
            num_features=num_features,
            hidden_activation=hidden_activation,
        )

    if agent_type == 'bp' or agent_type == 'linear' or agent_type == 'l2':
        learner = Backprop(
            net=net,
            step_size=step_size,
            opt=opt,
            beta_1=beta_1,
            beta_2=beta_2,
            weight_decay=weight_decay,
            to_perturb=(perturb_scale > 0),
            perturb_scale=perturb_scale,
        )
    elif agent_type == 'cbp':
        learner = ContinualBackprop(
            net=net,
            step_size=step_size,
            opt=opt,
            replacement_rate=replacement_rate,
            decay_rate=decay_rate,
            device='cpu',
            maturity_threshold=mt,
            util_type=util_type,
            init=init,
            accumulate=accumulate,
        )

    with open(env_file, 'rb+') as f:
        inputs, outputs, _ = pickle.load(f)

    errs = torch.zeros((num_data_points), dtype=torch.float)
    if to_log: weight_mag = torch.zeros((num_data_points, 2), dtype=torch.float)
    if to_log_grad: grad_mag = torch.zeros((num_data_points, 2), dtype=torch.float)
    if to_log_activation: activation = torch.zeros((num_data_points, ), dtype=torch.float)
    for i in tqdm(range(num_data_points)):
        x, y = inputs[i: i+1], outputs[i: i+1]
        err = learner.learn(x=x, target=y)
        if to_log:
            weight_mag[i][0] = learner.net.layers[0].weight.data.abs().mean()
            weight_mag[i][1] = learner.net.layers[-1].weight.data.abs().mean()
        if to_log_grad:
            grad_mag[i][0] = learner.net.layers[0].weight.grad.data.abs().mean()
            grad_mag[i][1] = learner.net.layers[-1].weight.grad.data.abs().mean()
        if to_log_activation:
            if hidden_activation == 'relu':
                activation[i] = (learner.previous_features[0] == 0).float().mean()
            if hidden_activation == 'tanh':
                activation[i] = (learner.previous_features[0].abs() > 0.9).float().mean()
        errs[i] = err

    data_to_save = {
        'errs': errs.numpy()
    }
    if to_log:
        data_to_save['weight_mag'] = weight_mag.numpy()
    if to_log_grad:
        data_to_save['grad_mag'] = grad_mag.numpy()
    if to_log_activation:
        data_to_save['activation'] = activation.numpy()
    return data_to_save

def main(arguments):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-c', help="Path to the file containing the parameters for the experiment",
                        type=str, default='temp_cfg/0.json')
    args = parser.parse_args(arguments)
    cfg_file = args.c

    with open(cfg_file, 'r') as f:
        params = json.load(f)

    data = expr(params)

    with open(params['data_file'], 'wb+') as f:
        pickle.dump(data, f)

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))


#!/usr/bin/env python
import time
import math
import rospy
from std_msgs.msg import Float64
from geometry_msgs.msg import Twist
from robot import RobotCalculator

class DeliveryRobot(object):
    def __init__(self):
        rospy.loginfo("CuriosityRoverAckerMan Initialising...")

        self.calculator = RobotCalculator(
            (19.6, 25.1, 25.1, 19.6), (math.pi / 180, 1.0472))  # (1 degree, 60 degree)
        self.publishers_curiosity_d = {}
        self.controller_ns = "beetlebot"
        self.controller_command = "command"
        self.controllers_list = ["back_wheel_L_joint_velocity_controller",
                                "back_wheel_R_joint_velocity_controller",
                                "front_wheel_L_joint_velocity_controller",
                                "front_wheel_R_joint_velocity_controller",
                                "middle_wheel_L_joint_velocity_controller",
                                "middle_wheel_R_joint_velocity_controller",
                                "sterring_B_L_joint_position_controller",
                                "sterring_B_R_joint_position_controller",
                                "sterring_F_L_joint_position_controller",
                                "sterring_F_R_joint_position_controller"]

        for controller_name in self.controllers_list:
            topic_name = "/"+self.controller_ns+"/" + \
                controller_name+"/"+self.controller_command
            self.publishers_curiosity_d[controller_name] = rospy.Publisher(
                topic_name,
                Float64,
                queue_size=1)

        self.wait_publishers_to_be_ready()
        self.init_publisher_variables()
        self.init_state()

        self.cmd_vel_msg = Twist()
        cmd_vel_topic = "/cmd_vel"
        rospy.Subscriber(cmd_vel_topic, Twist, self.cmd_vel_callback)

        rospy.logwarn("beetlebot_AckerMan...READY")

    def cmd_vel_callback(self, msg):
        self.cmd_vel_msg = msg

    def wait_publishers_to_be_ready(self):

        rate_wait = rospy.Rate(10)
        for controller_name, publisher_obj in self.publishers_curiosity_d.iteritems():
            publisher_ready = False
            while not publisher_ready:
                rospy.logwarn("Checking Publisher for ==>" + str(controller_name))
                pub_num = publisher_obj.get_num_connections()
                publisher_ready = (pub_num > 0)
                rate_wait.sleep()
            rospy.loginfo("Publisher ==>" + str(controller_name) + "...READY")

    def init_publisher_variables(self):
        """
        We create variables for more pythonic access access to publishers
        and not need to access any more
        :return:
        """
        # Get the publishers for wheel speed
        self.back_wheel_L = self.publishers_curiosity_d[self.controllers_list[0]]
        self.back_wheel_R = self.publishers_curiosity_d[self.controllers_list[1]]
        self.front_wheel_L = self.publishers_curiosity_d[self.controllers_list[2]]
        self.front_wheel_R = self.publishers_curiosity_d[self.controllers_list[3]]
        self.middle_wheel_L = self.publishers_curiosity_d[self.controllers_list[4]]
        self.middle_wheel_R = self.publishers_curiosity_d[self.controllers_list[5]]
        # Get the publishers for steering
        self.suspension_steer_B_L = self.publishers_curiosity_d[self.controllers_list[6]]
        self.suspension_steer_B_R = self.publishers_curiosity_d[self.controllers_list[7]]
        self.suspension_steer_F_L = self.publishers_curiosity_d[self.controllers_list[8]]
        self.suspension_steer_F_R = self.publishers_curiosity_d[self.controllers_list[9]]

        # Init Messages
        self.back_wheel_L_velocity_msg = Float64()
        self.back_wheel_R_velocity_msg = Float64()
        self.front_wheel_L_velocity_msg = Float64()
        self.front_wheel_R_velocity_msg = Float64()
        self.middle_wheel_L_velocity_msg = Float64()
        self.middle_wheel_R_velocity_msg = Float64()
        self.suspension_steer_B_L_pos_msg = Float64()
        self.suspension_steer_B_R_pos_msg = Float64()
        self.suspension_steer_F_L_pos_msg = Float64()
        self.suspension_steer_F_R_pos_msg = Float64()

    def init_state(self):
        self.set_turning_radius(None, False)
        self.set_wheels_speed(0.0, None, False)

    def set_turning_radius(self, turn_radius, isRotate):

        r_angle = self.calculator.calculateDegree(turn_radius, isRotate)

        self.suspension_steer_B_L_pos_msg.data = r_angle['3']
        self.suspension_steer_B_R_pos_msg.data = r_angle['6']
        self.suspension_steer_F_L_pos_msg.data = r_angle['1']
        self.suspension_steer_F_R_pos_msg.data = r_angle['4']

        self.suspension_steer_B_L.publish(self.suspension_steer_B_L_pos_msg)
        self.suspension_steer_B_R.publish(self.suspension_steer_B_R_pos_msg)
        self.suspension_steer_F_L.publish(self.suspension_steer_F_L_pos_msg)
        self.suspension_steer_F_R.publish(self.suspension_steer_F_R_pos_msg)

    def set_wheels_speed(self, turning_speed, turning_radius, isRotate):
        velocity = self.calculator.calculateVelocity(
            turning_speed, turning_radius, isRotate)

        self.back_wheel_L_velocity_msg.data = velocity['3']
        self.back_wheel_R_velocity_msg.data = velocity['6']
        self.front_wheel_L_velocity_msg.data = velocity['1']
        self.front_wheel_R_velocity_msg.data = velocity['4']
        self.middle_wheel_L_velocity_msg.data = velocity['2']
        self.middle_wheel_R_velocity_msg.data = velocity['5']

        self.back_wheel_L.publish(self.back_wheel_L_velocity_msg)
        self.back_wheel_R.publish(self.back_wheel_R_velocity_msg)
        self.front_wheel_L.publish(self.front_wheel_L_velocity_msg)
        self.front_wheel_R.publish(self.front_wheel_R_velocity_msg)
        self.middle_wheel_L.publish(self.middle_wheel_L_velocity_msg)
        self.middle_wheel_R.publish(self.middle_wheel_R_velocity_msg)

    def move_with_cmd_vel(self):
        wheel_speed = self.cmd_vel_msg.linear.x
        turning_radius = self.cmd_vel_msg.angular.z
        # Check if delivery-robot want to rotate
        isRotate = True if self.cmd_vel_msg.linear.z != 0 else False

        if turning_radius == 0.0:
            turning_radius = None

        rospy.logdebug("turning_radius="+str(turning_radius) + ",wheel_speed="+str(wheel_speed))
        self.set_turning_radius(turning_radius, isRotate)
        self.set_wheels_speed(wheel_speed, turning_radius, isRotate)

if __name__ == "__main__":
    rospy.init_node("beetlebot_AckerMan_node", log_level=rospy.INFO)
    delivery_robot = DeliveryRobot()
    rate = rospy.Rate(10.0)
    while not rospy.is_shutdown():
        delivery_robot.move_with_cmd_vel()
        rate.sleep()


from tkinter import *
from decimal import *

root = Tk()
root.title('Calculator')

activeStr = '' # для текущего числа
stack = [] # для предыдущего числа

def calculate():
    global stack
    global Num
    result = 0
    operand2 = Decimal(stack.pop())
    operation = stack.pop()
    operand1 = Decimal(stack.pop())
    if operation == '+':
        result = operand1 + operand2
    elif operation == '-':
        result = operand1 - operand2
    elif operation == '*':
        result = operand1 * operand2
    elif operation == '/':
        if not (operand2 == 0) :
            result = operand1 / operand2
    if operation == '/' and operand2 == 0 :
        Num.configure(text="error")
    else :
        Num.configure(text=str(result))

def click(text):
    global activeStr
    global stack
    global prNum
    global operation
    if text == 'C':
        stack.clear()
        activeStr = ''
        Num.configure(text='')
        prNum.configure(text='')
        operation.configure(text='')
    elif '0' <= text <= '9':
        activeStr += text
        Num.configure(text=activeStr)
    elif text == '.':
        if activeStr.find('.') == -1:
            activeStr += text
            Num.configure(text=activeStr)
    else: # знаки операций
        #if(operation['text']
        if len(stack) >= 2:
            stack.append(Num['text'])
            calculate()
            stack.clear()
            stack.append(Num['text'])
            activeStr = ''
            prNum.configure(text=Num['text'])
            Num.configure(text='')
            operation.configure(text=text)
            if text != '=':
                stack.append(text)
            else:
                operation.configure(text='')
        else:
            if text != '=':
                stack.append(Num['text'])
                stack.append(text)
                prNum.configure(text=Num['text'])
                activeStr = ''
                Num.configure(text='')
                operation.configure(text = text)
            #else :
             #   activeStr = str(Decimal(stack.pop()))
              #  label.configure(text = activeStr)

buttons = (('7', '8', '9', '/', '4'),
           ('4', '5', '6', '*', '4'),
           ('1', '2', '3', '-', '4'),
           ('0', '.', '=', '+', '4')
           ) # "массив" с текстами кнопок

Num = Label(root, text='', width=80, height = 5, font = 'consolas 14') # строка текущего состояния
Num.grid(row=1, column=0, columnspan=4, sticky="nsew")
prNum = Label(root, text='', width=60, height = 5, font = 'consolas 14')
prNum.grid(row=0, column=0, columnspan=3)
operation = Label(root, text='', width=20, height = 5, font = 'consolas 14')
operation.grid(row = 0, column = 3)

button = Button(root, text='C', command=lambda text='C': click(text))
button.grid(row=1, column=3, sticky="nsew")

for row in range(4):
    for col in range(4):
        button = Button(root, text=buttons[row][col], height = 5, font = 'consolas 14',
                command=lambda row=row, col=col: click(buttons[row][col]))
        button.grid(row=row + 3, column=col, sticky="nsew")

root.grid_rowconfigure(6, weight=1)
root.grid_columnconfigure(4, weight=1)

root.mainloop()


import random
from english_words import english_words_set
from PyDictionary import PyDictionary
words = list(english_words_set)
chosen = "r"
dict = PyDictionary()
meaning = dict.meaning(chosen)
print(f"This is The Meaning Of The Word You Are Going To Guess{meaning}")
chose_edit = chosen
def anti_vowel(c = input):
    newstr = c
    vowels = ('b','R', 'c', 'd', 'f', 'g', 'h', 'j','k','l','m','n','p','q','r','s','t','v','w','x','y','z')
    for x in c.lower():
        if x in vowels:
            newstr = newstr.replace(x,"_")
    newstr = list(newstr)
    print(newstr)

chose_edit = list(chosen)
anti_vowel(chosen)
guess = input("What letter Do U want To enter First").lower

for position in range(len(chose_edit)):
    letter = chose_edit[position]
    if letter == guess:
        print("k")


import numpy as np
from pathlib import Path
import shutil
import subprocess
import os
import h5py

from openquake.hazardlib.source.point import PointSource
from openquake.hazardlib.geo.point import Point
from openquake.hazardlib.geo.nodalplane import NodalPlane
from openquake.hazardlib.pmf import PMF
from openquake.hazardlib.scalerel.point import PointMSR
from openquake.hazardlib.mfd.evenly_discretized import EvenlyDiscretizedMFD
from openquake.hazardlib import sourcewriter
from openquake.hazardlib.tom import PoissonTOM, NegativeBinomialTOM

def run_model(folder_name):
    """
    Runs a folder where the openquake files are found: Job.ini, source model, source logic tree, grid and gmpe logic tree
    :param folder_name: Location of Openquake source files
    :return:
    """
    job_path = os.path.join(folder_name, 'job.ini')
    print('Running Openquake')

    oq_proc = subprocess.call(['oq', 'engine', '--run', job_path], stdout=subprocess.PIPE)
    if oq_proc == 0:
        # If success, gets calculation id
        process_2 = subprocess.run(['oq', 'engine', '--lhc'], stdout=subprocess.PIPE)
        out = str(process_2).split(r'\n')[-2]
        line = out.split()
        print(line)
        calc_id = int(line[0])
        src = os.path.join(Path.home(), 'oqdata', f'calc_{calc_id}.hdf5')
        # Copy calculation hdf5 database into model folder
        shutil.copy(src, folder_name)
        print(f'Done. hdf5 database copied to {folder_name}')
    else:
        #if oq throws error
        raise Exception('Run failed')

def parse_db(folder):
    """
    Reads a hdf5 database and parses the mean curves.  Intensity measure levels (imtl) are fixed.
    :param folder:
    :return:
    """
    files = list(os.walk(folder))[0][2]
    calcs = [i for i in files if 'calc' in i]
    ids = [int(i.split('_')[1].split('.')[0]) for i in calcs]
    calc_path = os.path.join(folder, calcs[int(np.argmax(ids))])

    db = h5py.File(os.path.join(folder, calc_path), 'r')

    grid = np.stack((db['sitecol']['lon'], db['sitecol']['lat'])).T

    hcurves = db['hcurves-stats'][:, 0, 0, :]

    imtl = np.logspace(-2, 0.2, 30)   # Same as all models' job.ini

    data = {'grid': grid,
            'hcurves': hcurves,
            'imtl': imtl}
    return data

class hazard_model:

    def __init__(self, name=None):
        self.name = name
        self.cells = np.array(0, np.dtype(float))
        self.rates = np.array(0, np.dtype(float))
        self.dispersion = np.array(0, np.dtype(float))
        self.rates_mbin = np.array(0, np.dtype(float))
        self.sources = []

        self.mmin = 5.
        self.mmax = 8.
        self.mbin = 0.1
        self.magnitudes = np.arange( self.mmin,  self.mmax + self.mbin,  self.mbin)

    @property
    def nsources(self):
        return self.cells.shape[0]

    def read_forecast(self, filename, eepas=False):
        """
        Parses forecast files
        :param forecast_name: path to forecast
        :param eepas: Flag to indicate if the forecast is just a spatial PDF (False) or, magnitude bins spatial PDFs (True)

        """
        if eepas is False:
            data = np.genfromtxt(filename, delimiter=',', skip_header=1)
            self.cells = np.array([[i[0] + i[1], i[2] + i[3]] for i in data[:, :4]])/2.
            self.rates = data[:, 8]
            self.dispersion = data[:, 9]

        else:
            data = np.genfromtxt(filename, delimiter=',', skip_header=1)
            self.cells = np.array([[i[0] + i[1], i[2] + i[3]] for i in data[:, :4]])/2.
            self.rates_mbin = data[:, 6:-1]
            self.rates = np.sum(self.rates_mbin, axis=1)

            self.dispersion = data[:, -1]

    def scale(self, scale):
        """
        Scales rates and magnitude rates
        :param scale:
        :return:
        """
        self.rates *= scale
        self.rates_mbin *= scale

    def set_trunc_gr(self, bval, eepas=False):
        """
        Projects spatial rates using a truncated Gutenberg-Richter magnitude-frequency-distribution into magnitude bins.
        If the model contains EEPAS, scales the magnitude bins pdf scaling each magnitude PDF by the weights of a truncated GR distribution
        :param bval: b-value of the distribution
        :return:
        """

        mags = self.magnitudes
        mbin = self.mbin
        mmin = self.mmin
        mmax = self.mmax
        rates = self.rates

        weights = (10 ** (- bval * (mags - mbin / 2.)) - 10 ** (- bval * (mags + mbin / 2.))) / (
                   10 ** (- bval * (mmin - mbin / 2.)) - 10 ** (- bval * (mmax + mbin / 2.)))

        if eepas is False:
            rates_mbin = np.zeros((rates.shape[0], mags.shape[0]))
            for i, mu in enumerate(rates):
                rates_mbin[i, :] = mu * weights
            self.rates_mbin = rates_mbin

        else:
            rates_mbin = self.rates_mbin

            for i, mu_m in enumerate(rates_mbin):
                self.rates_mbin[i, :] = weights * mu_m
                self.rates[i] = np.sum(self.rates_mbin[i, :])

    def get_point_srcs(self,
                       time_span=1,
                       tect_region='Active Shallow Crust',
                       rupt_mesh_spacing=5.0,
                       magnitude_scale_rel=PointMSR(),
                       rupt_aspect_ratio=1.0,
                       upper_seis_depth=0,
                       lower_seis_depth=30,
                       nodal_dist=[(1, NodalPlane(0, 90, 0.))],
                       hypo_dist=((0.5, 10.0), (0.5, 30.0))):
        """
        Creates the Openquake objects necessary to write a source nrml file. Assign a Negative Binomial temporal model
        if the dispersion is greater than 0, and a Poisson temporal model if 0.
        """
        npd = PMF(nodal_dist)
        hd = PMF(hypo_dist)

        for n in range(self.nsources):
            mfd = EvenlyDiscretizedMFD(self.mmin, self.mbin, self.rates_mbin[n, :])
            if self.dispersion[n] != 0:
                mu_tot = self.rates[n]
                alpha = self.dispersion[n]
                tom = NegativeBinomialTOM(time_span, mu_tot, alpha)
            else:
                tom = PoissonTOM(1)

            source = PointSource(source_id='%05i' % n,
                                 name='point%05i' % n,
                                 tectonic_region_type=tect_region,
                                 mfd=mfd,
                                 rupture_mesh_spacing=rupt_mesh_spacing,
                                 magnitude_scaling_relationship=magnitude_scale_rel,
                                 rupture_aspect_ratio=rupt_aspect_ratio,
                                 temporal_occurrence_model=tom,
                                 upper_seismogenic_depth=upper_seis_depth,
                                 lower_seismogenic_depth=lower_seis_depth,
                                 location=Point(self.cells[n, 0],
                                                self.cells[n, 1]),
                                 nodal_plane_distribution=npd,
                                 hypocenter_distribution=hd)
            self.sources.append(source)

    def write_source(self, source_path):

        sourcewriter.write_source_model(source_path,
                                        self.sources,
                                        name=self.name,
                                        investigation_time=1)


'''
    # RPC Cliente #
    # Autores: Henrique Moura Bini e Vinicius Henrique Soares
    # Data de criação: 15/11/2021
    # Data de modificação: 18/11/2021
    # Descrição: Implementação de um serviço de gerenciamento de notas que deve enviar informações via gRPC:
            - LISTAR_ALUNOS (1): Tenta listar os alunos de uma disciplina com base no código da disciplina, ano e semestre.
            - ALTERAR_NOTA (2): Tenta alterar a nota de um aluno em uma disciplina com base no código do aluno, código da disciplina, ano e semestre.
            - ALTERAR_FALTAS (3): Tenta alterar a quantidade de faltas de um aluno com base no código do aluno, código da disciplina, ano e semestre.
            - LISTAR_DISCIPLINAS_CURSO (4): Tenta listar as disciplinas de um curso com base no código do curso.
            - LISTAR_DISCIPLINAS_ALUNO (5): Tenta listar os alunos de uma disciplina com base no ra, ano e semestre.
            - INSERIR_MATRICULA (6): Tenta inserir uma nova matrícula de um aluno em uma disciplina.

'''

import grpc

import gerenciamentoNotas_pb2_grpc
import gerenciamentoNotas_pb2

channel = grpc.insecure_channel('localhost:7778')
stub = gerenciamentoNotas_pb2_grpc.GerenciadorDeNotasStub(channel)

# Constantes para o tipo de requisição
LISTAR_ALUNOS = "1"
ALTERAR_NOTA = "2"
ALTERAR_FALTAS = "3"
LISTAR_DISCIPLINAS_CURSO = "4"
LISTAR_DISCIPLINAS_ALUNO = "5"
INSERIR_MATRICULA = "6"

# Função para receber os dados da requisição do cliente e enviá los para o servidor
def dadosRequisicao(opcao):
    if opcao == LISTAR_ALUNOS:
        # Variável para armazenar os dados da requisição
        request = gerenciamentoNotas_pb2.ListarAlunosRequest()
        # Recebe os dados da requisição
        codigoDisciplina = (input("Digite o código da disciplina: "))
        ano = input("Digite o ano: ")
        semestre = input("Digite o semestre: ")
        # Verifica se os dados da requisição são válidos
        if ano.isdigit() and semestre.isdigit() and codigoDisciplina != "" and ano != "" and semestre != "":
            request.codigoDisciplina = codigoDisciplina
            request.ano = int(ano)
            request.semestre = int(semestre)
            # Envia os dados da requisição para o servidor e recebe a resposta
            response = stub.ListarAlunos(request)
            # Verifica se a requisição foi bem sucedida
            if response.mensagem != "":
                # Imprime os dados da resposta
                print(response.mensagem)
            else:
                # Imprime os dados da resposta
                for aluno in response.alunos:
                    print(aluno, end="")
                    print("===========================")
        else:
            print("Algum dos campos não foi preenchido corretamente")

    elif opcao == ALTERAR_NOTA:
        # Variável para armazenar os dados da requisição
        request = gerenciamentoNotas_pb2.AlterarNotaRequest()
        # Recebe os dados da requisição
        ra = input("Digite o RA do aluno: ")
        codigoDisciplina = input("Digite o código da disciplina: ")
        ano = input("Digite o ano da disciplina: ")
        semestre = input("Digite o semestre da disciplina: ")
        nota = input("Digite a nota do aluno: ")
        # Verifica se os dados da requisição são válidos
        if ra.isdigit() and ano.isdigit() and semestre.isdigit() and nota.isdigit() and codigoDisciplina != "" and ano != "" and semestre != "" and nota != "" and ra != "":
            request.ra = int(ra)
            request.codigoDisciplina = codigoDisciplina
            request.ano = int(ano)
            request.semestre = int(semestre)
            request.nota = float(nota)
            # Envia os dados da requisição para o servidor e recebe a resposta
            response = stub.AlterarNota(request)
            # Verifica se a requisição foi bem sucedida
            if response.mensagem != "":
                # Imprime os dados da resposta
                print(response.mensagem)
            else:
                # Imprime os dados da resposta
                print("Ra: " + str(response.ra) + "\nCodigo da Disciplina:" + str(response.codigoDisciplina) + "\nAno: " + str(response.ano) + "\nSemestre: " + str(response.semestre) + "\nNota: " + str(response.nota))
        else:
            print("Algum dos campos não foi preenchido corretamente")

    elif opcao == ALTERAR_FALTAS:
        # Variável para armazenar os dados da requisição
        request = gerenciamentoNotas_pb2.AlterarFaltasRequest()
        # Recebe os dados da requisição
        ra = input("Digite o RA do aluno: ")
        codigoDisciplina = input("Digite o código da disciplina: ")
        ano = input("Digite o ano da disciplina: ")
        semestre = input("Digite o semestre da disciplina: ")
        faltas = input("Digite a quantidade de faltas do aluno: ")
        # Verifica se os dados da requisição são válidos
        if ra != '' and codigoDisciplina != '' and ano != '' and semestre != '' and faltas != '' and ra.isdigit() and ano.isdigit() and semestre.isdigit() and faltas.isdigit():
            request.ra = int(ra)
            request.codigoDisciplina = codigoDisciplina
            request.ano = int(ano)
            request.semestre = int(semestre)
            request.faltas = int(faltas)
            # Envia os dados da requisição para o servidor e recebe a resposta
            response = stub.AlterarFaltas(request)
            # Verifica se a requisição foi bem sucedida
            if response.mensagem != "":
                print(response.mensagem)
            else:
                print("Ra: " + str(response.ra) + "\nCodigo da Disciplina:" + str(response.codigoDisciplina) + "\nAno: " + str(response.ano) + "\nSemestre: " + str(response.semestre) + "\nFaltas: " + str(response.faltas))
        else:
            print("Algum dos campos não foi preenchido corretamente")

    elif opcao == LISTAR_DISCIPLINAS_CURSO:
        # Variável para armazenar os dados da requisição
        request = gerenciamentoNotas_pb2.ListarDisciplinasCursoRequest()
        # Recebe os dados da requisição
        codigoCurso = input("Digite o código do curso: ")
        # Verifica se os dados da requisição são válidos
        if codigoCurso.isdigit() and codigoCurso != "":
            request.codigoCurso = int(codigoCurso)
            # Envia os dados da requisição para o servidor e recebe a resposta
            response = stub.ListarDisciplinasCurso(request)
            # Verifica se a requisição foi bem sucedida
            if response.mensagem != "":
                print(response.mensagem)
            else:
                # Imprime os dados da resposta
                for i in range(len(response.disciplinas)):
                    print("Código da Disciplina: " + str(response.disciplinas[i].codigoDisciplina) + "\nNome: " + str(response.disciplinas[i].nome) + "\nProfessor: " + str(response.disciplinas[i].professor))
                    print("===========================")
        else:
            print("Algum dos campos não foi preenchido corretamente")

    elif opcao == LISTAR_DISCIPLINAS_ALUNO:
        # Variável para armazenar os dados da requisição
        request = gerenciamentoNotas_pb2.ListarDisciplinasAlunoRequest()
        # Recebe os dados da requisição
        ra = input("Digite o RA do aluno: ")
        ano = input("Digite o ano: ")
        semestre = input("Digite o semestre: ")
        # Verifica se os dados da requisição são válidos
        if ra.isdigit() and ano.isdigit() and semestre.isdigit() and ra != '' and ano != '' and semestre != '':
            request.ra = int(ra)
            request.ano = int(ano)
            request.semestre = int(semestre)
            # Envia os dados da requisição para o servidor e recebe a resposta
            response = stub.ListarDisciplinasAluno(request)
            # Verifica se a requisição foi bem sucedida
            if response.mensagem != "":
                print(response.mensagem)
            else:
                # Imprime os dados da resposta
                for i in range(len(response.disciplinas)):
                        print("Ra: " + str(response.disciplinas[i].ra) + "\nCódigo da Disciplina: " + str(response.disciplinas[i].codigoDisciplina) + "\nNota: " + str(response.disciplinas[i].nota) + "\nFaltas: " + str(response.disciplinas[i].faltas))
                        print("===========================")
        else:
            print("Algum dos campos não foi preenchido corretamente")

    elif opcao == INSERIR_MATRICULA:
        # Variável para armazenar os dados da requisição
        request = gerenciamentoNotas_pb2.InserirMatriculaRequest()
        # Recebe os dados da requisição
        ra = input("Digite o código do aluno: ")
        codigoDisciplina = input("Digite o código da disciplina: ")
        ano = input("Digite o ano da disciplina: ")
        semestre = input("Digite o semestre da disciplina: ")
        # Verifica se os dados da requisição são válidos
        if ra.isdigit() and ano.isdigit() and semestre.isdigit() and codigoDisciplina != "" and ano != "" and semestre != "" and ra != "":
            request.matricula.ra = int(ra)
            request.matricula.codigoDisciplina = codigoDisciplina
            request.matricula.ano = int(ano)
            request.matricula.semestre = int(semestre)
            # Envia os dados da requisição para o servidor e recebe a resposta
            response = stub.InserirMatricula(request)
            # Verifica se a requisição foi bem sucedida
            if response.mensagem != "":
                print(response.mensagem)
            else:
                print("Ra: " + str(response.matricula.ra) + "\nCodigo da Disciplina:" + str(response.matricula.codigoDisciplina) + "\nAno: " + str(response.matricula.ano) + "\nSemestre: " + str(response.matricula.semestre))
        else:
            print("Algum dos campos não foi preenchido corretamente")
    else:
        print("Opção inválida")

def main():

    print("1 - Listar Alunos")
    print("2 - Alterar Nota")
    print("3 - Alterar Faltas")
    print("4 - Listar Disciplinas do Curso")
    print("5 - Listar Disciplinas do Aluno")
    print("6 - Inserir Matricula")
    # Recebe a opção do usuário
    while True:
        opcao = input("Digite a opção desejada: ")
        # Verifica se a opção é válida
        if(opcao.isdigit()):
            dadosRequisicao(opcao)
        else:
            print("Opção inválida")

main()


from pymongo import MongoClient, DESCENDING
import logging

class DataReceiver:
    def __init__(self):
        self.client = MongoClient('mongo')
        self.db = self.client["wazuhl"]
        self.rewards = self.db["rewards"]

    def get_latest_rewards(self, last_n=0):
        query = self.rewards.find(sort=[("step", DESCENDING)],
                                  limit=last_n, projection={"reward": 1,
                                                            "step": 1})
        steps_rewards = ((record["step"], record["reward"]) for record in query)
        if not steps_rewards:
            return [], []
        steps, rewards = zip(*steps_rewards)
        logging.info(steps)
        logging.info(rewards)
        return steps, rewards


def LongDiv(divisor):
    dividend = [1]
    divIndex = 0
    answer = []

    i = 0
    div = dividend[divIndex]
    pattern = False
    divs = []
    while div != 0 and not pattern and i < 1000:
        while div < divisor:
            divIndex += 1
            if divIndex == len(dividend):
                dividend.append(0)
            div *= 10
            div += dividend[divIndex]
            if div in divs:
                pattern = True
                break
            if div//divisor == 0:
                answer.append(0)
        divs.append(div)
        if pattern:
            break
        answer.append(div//divisor)
        div = div % divisor
        i +=1

    return (len(answer))

largest = 0
sol = 0
for i in range(2, 1000):
    #continue
    j = LongDiv(i)
    if largest < j:
        sol = i
        largest = j

print("SOLUTION")
print(sol)


"""Crawl game URLs in matchday data."""

import datetime
import urllib3
import certifi
import numpy as np
import pandas as pd
import multiprocessing as mp

from bs4 import BeautifulSoup
from bld.project_paths import project_paths_join as ppj

def get_soup_obj(url):
    """
    Returns the soup object from a given url.
    """

    http = urllib3.PoolManager(
        cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
    page_request = http.request("GET", url)
    soup = BeautifulSoup(page_request.data, 'lxml')

    return soup

def get_mtchdy_data(league_url):
    """
    Returns all game URLs for a given matchday URL. Moreover, it is
    indicated whether the respective game already took place and if not, when
    they will be played.
    """

    # Get games soup.
    matchday_soup = get_soup_obj(league_url)
    games = matchday_soup.findAll(
        "td", {"class": "liga_spielplan_container"})

    # Get game information.
    matchday_df = pd.DataFrame()
    for j, game in enumerate(games):
        game_dict = {}
        game_dict["game_url"] = game.a["href"]
        if game.find("div", {"liga_spieltag_vorschau_datum_content_ergebnis"}) != None:
            game_dict["doable"] = 1
            game_dict["reason"] = 0
        elif game.find("div", {"class": "liga_spieltag_vorschau_datum_content"}) != None:
            game_dict["doable"] = 0
            game_dict["reason"] = game.find(
                "div", {"class": "liga_spieltag_vorschau_datum_content"}).text
        else:
            game_dict["doable"] = 0
            game_dict["reason"] = np.nan

        matchday_df = matchday_df.append(game_dict, ignore_index=True)

    if len(matchday_df) != 0:
        matchday_df["mtchdy_url"] = league_url
        return matchday_df
    else:
        pass

def main():
    # Load league data.
    league_df = pd.read_csv(
        ppj("OUT_DATA_FOOTBALL", "matchday_data.csv"), encoding='cp1252')

    # Scrape matchday data via multiprocessing.
    df_list = []
    with mp.Pool() as pool:
        out = pool.map(get_mtchdy_data, league_df.mtchdy_url.values)
        df_list.extend(out)

    # Create df from df list.
    game_df = pd.concat(df_list)

    # Merge to league data and save as CSV.
    game_df = game_df.merge(league_df, on="mtchdy_url")
    game_df.to_csv(ppj("OUT_DATA_FOOTBALL", "game_urls.csv"), index=False)

if __name__ == '__main__':
    main()


from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    age = models.IntegerField(default= 20)
    longitude = models.DecimalField(max_digits=20, decimal_places=12,default=0)
    latitude = models.DecimalField(max_digits=20, decimal_places=12, default=0)
    location_text = models.TextField(default="")
    aboutMe = models.TextField(default="")

@receiver(post_save, sender= User)
def create_user_profile(sender,instance,created, **kwargs):
     if created:
         UserProfile.objects.create(user= instance)

@receiver(post_save, sender= User)
def save_user_profile(sender,instance,**kwargs):
    instance.userprofile.save()

class Instruments(models.Model):
    user =models.ForeignKey(User, on_delete=models.CASCADE)
    Instrument = models.CharField(max_length=200)
    experience = models.CharField(max_length=50)

    def __str__(self):
        return "%s %s %s" % (self.user, self.Instrument, self.experience)

class urls(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    url = models.URLField(null=True)


import argparse
import numpy as np
import os

parser = argparse.ArgumentParser()
parser.add_argument("--min_sigma_mag", type=float, default=-5)
parser.add_argument("--max_sigma_mag", type=float, default=2)
parser.add_argument("--n_sigma", type=int, default=60)
parser.add_argument("--p", type=int, default=2)
parser.add_argument("--q", type=int, default=2)
parser.add_argument("--kappa", type=float, default=0.9)

args = parser.parse_args()

sigmas = np.logspace(args.min_sigma_mag, args.max_sigma_mag, args.n_sigma)

template = """

#!/bin/bash

#PBS -l walltime=48:00:00
#PBS -l select=1:ncpus=1:mem=5gb
#PBS -J 1-{}

module load lang/cuda
module load lang/python/anaconda/pytorch

cd /home/jr19127/loss-surfaces-of-gans/scripts

# PBS ARRAY INDEX is 1-up, but bash arrays are 0-up, so leading pad with -1

sigmas=({})

# Execute code
python rmt_sigmaz_exp.py --sigma ${{sigmas[$(( ($PBS_ARRAY_INDEX-1)  ))]}} --out {}/results_$(( ($PBS_ARRAY_INDEX-1))) --p {} --q {} --kappa {:.1f}
"""
outdir = f"/work/jr19127/gan-loss-surfaces/rmt_results/vary_sigma/p{args.p}q{args.q}"
os.makedirs(outdir, exist_ok=True)

sigma_str = " ".join(["{:.7f}".format(s) for s in sigmas])
script = template.format(len(sigmas), sigma_str, outdir,  args.p, args.q, args.kappa)

with open(f"run_rmt_vary_sigmaz_p{args.p}q{args.q}.sh", "w") as fout:
    fout.write(script)


import jwt
import datetime

# Clave secreta para firmar y verificar el token (puede ser cualquier valor secreto)
clave_secreta = 'mi_clave_secreta'

# Función para generar un token JWT
def generar_token(datos):
    # Definir fecha de expiración del token (en este caso, 1 hora desde ahora)
    fecha_expiracion = datetime.datetime.utcnow() + datetime.timedelta(hours=1)

    # Crear payload (datos) del token
    payload = {
        'datos': datos,
        'exp': fecha_expiracion
    }

    # Generar el token JWT
    token = jwt.encode(payload, clave_secreta, algorithm='HS256')
    return token

# Función para verificar un token JWT
def verificar_token(token):
    try:
        # Decodificar el token y obtener el payload (datos)
        payload = jwt.decode(token, clave_secreta, algorithms=['HS256'])
        return payload['datos']
    except jwt.ExpiredSignatureError:
        return 'El token ha expirado.'
    except jwt.InvalidTokenError:
        return 'Token inválido.'

# Ejemplo de uso
datos_usuario = {
    'id': 123,
    'nombre': 'Usuario Ejemplo',
    'rol': 'admin'
}

# Generar el token
token_generado = generar_token(datos_usuario)
print('Token generado:', token_generado)

# Verificar el token
datos_verificados = verificar_token(token_generado)
print('Datos verificados:', datos_verificados)


"""
https://leetcode.com/problems/3sum/
"""

def get_pairs(nums, sum_of_2, first_num):
    result = []
    s = set()
    for i in range(len(nums)):
        if nums[i] == nums[i - 1]:
            if nums[i] + nums[i - 1] != sum_of_2:
                continue
        if sum_of_2 - nums[i] in s:
            result.append([first_num, nums[i], sum_of_2 - nums[i]])
        s.add(nums[i])
    return result

def get_triplets(nums):
    result = []
    if len(nums) < 3:
        return result
    for i in range(len(nums)):
        res = get_pairs(nums[i + 1 :], -nums[i], nums[i])
        if len(res) != 0:
            result += res

    dedupl_set = set()
    for item in result:
        item.sort()
        dedupl_set.add(tuple(item))
    result = []
    for item in dedupl_set:
        result.append(list(item))

    return result