import serial
import numpy as np
import time

class ADSyncError(Exception):
    pass

class ADSync:
    ANALOG_RANGE = 20
    ANALOG_MAX = 65536
    FREQ_MAX = 700000
    MAX_ADDR = 16384

    def __init__(self, port, baud=921600, timeout=0.5, debug=False):
        """
        Initialize a AD sync device.

        Parameters
        ----------
        port : string
            The serial port address of the device.

        Keywords
        --------
        baud : int (default: 921600)
        timeout : float (default: 0.5)
        debug : bool (default: False)
            If true, prints out all serial communcation with the device.
        """
        self.debug = debug
        self.ser = serial.Serial(port=None, baudrate=baud, timeout=timeout)
        self.ser.port = port
        self.ser.rts = False
        self.ser.dtr = False
        self.ser.open()
        self.byte_rate = baud / 10

    def reset(self):
        """
        Reset the device.

        Note: this works by activing the RTS bit on the serial port, which
        will *not* work over bluetooth!
        """
        self.ser.rts = True
        time.sleep(0.5)
        self.ser.rts = False
        time.sleep(1.0)
        self.ser.flush()

    def idn(self):
        "Return identification string."
        self._cmd("*IDN")
        return(self._reply())

    def stat(self):
        "Return statistics on sync output."
        self._cmd("SYNC STAT")
        return(self._reply())

    def _cmd(self, *args):
        if self.ser.in_waiting:
            self.ser.reset_input_buffer()

        cmd = []
        for arg in args:
            if isinstance(arg, str):
                cmd.append(arg.strip().encode('utf-8'))
            elif isinstance(arg, bytes):
                cmd.append(arg.strip())
            elif isinstance(arg, int):
                cmd.append(b'%d' % arg)
            elif isinstance(arg, np.ndarray):
                arg = bytes(arg)
                cmd.append(b'>%d>' % len(arg) + arg)
            elif arg is None:
                pass
            else:
                raise ADSyncError(
                    "command got data type (%s) it can't handle" % type(arg)
                )

        cmd = b' '.join(cmd) + b'\n'
        self.ser.write(cmd)

        self._last_cmd = cmd

        if self.debug:
            print("Wrote to device: ", cmd)

        return cmd

    def _reply(self):
        reply = self.ser.readline().strip()
        if reply.startswith(b'ERROR:'):
            lc = self._last_cmd
            if len(lc) > 31:
                lc = lc[:28] + b'...'
            raise ADSyncError(reply[6:].decode('utf-8').strip()
                + "\n(serial command: %s)" % repr(lc))

        if self.debug:
            print("Received from device ", reply)

        return reply

    def _bin_reply(self, err=True):
        c = self.ser.read()
        if c == b'>':
            header = self.ser.read_until(b'>')
            try:
                nbytes = int(header[:-1])
            except ValueError:
                raise ADSyncError(
                    'expected binary reply, device returned invalid size' %
                    (c + header)
                )
            data = self.ser.read(nbytes)
            # Should be a newline at the end -> lets flush it
            self.ser.readline()
            return data

        else:
            # This is not a binary reply!  Just treat it normally (prob. error)
            reply = c + self.ser.readline().strip()
            if reply.startswith(b'ERROR:'):
                raise ADSyncError(reply[6:].decode('utf-8').strip())
            elif err:
                raise ADSyncError(
                    'expected binary reply, device returned "%s"' % reply
                )
            return reply

    def start(self):
        "Start the sync output."
        self._cmd(b"SYNC START")
        return self._reply()

    def _analog(self, V, ref=None, clip=None):
        if ref is None:
            ref = -0.5 * self.ANALOG_RANGE
        if clip is None:
            clip = self.ANALOG_MAX-1

        iV = int((V - ref) / self.ANALOG_RANGE * self.ANALOG_MAX + 0.5)
        if clip:
            iV = min(max(iV, 0), clip)

        return iV

    def analog_scale(self, channel, amplitude, offset):
        '''
        Set the output range of an analog output

        Parameters
        ----------
        channel : int
            The analog output channel (0 or 1)
        amplitude : float
            The amplitude of the output wave (1/2 the peak to peak volts)
        offset : float
            The offset of the output wave (volts)

        Note that the full range must lie between -10 and 10 V
        (offset + amplitude <= 10 V, offset - amplitude >= -10 V)
        '''

        if channel not in (0, 1):
            raise ADSyncError("channel must be 0 or 1")
        if amplitude < 0:
            raise ADSyncError("Amplitude must be >= 0")
        if offset + amplitude > 10:
            raise ADSyncError("Analog scale out of range (too high)")
        if offset - amplitude < -10:
            raise ADSyncError("Analog scale out of range (too low)")

        amp = self._analog(2*amplitude, ref=0, clip=self.ANALOG_MAX)
        off = self._analog(offset - amplitude)
        self._cmd(b"ANA%d SCALE" % channel, amp, off)
        return self._reply()

    def analog_set(self, channel, V):
        '''
        Set the default output of an analog channel when it is not running
        in sync mode.  (See `mode` for more details.)

        Parameters
        ----------
        channel : int
            Analog channel, should be 0 or 1
        V : float
            Output volts, should be in the range (-10, 10)

        '''
        if channel not in (0, 1):
            raise ADSyncError("channel must be 0 or 1")
        if V > 10:
            raise ADSyncError("Analog output out of range (too high)")
        if V < -10:
            raise ADSyncError("Analog output out of range (too low)")

        self._cmd(b"ANA%d SET" % channel, self._analog(V))
        return self._reply()

    def mode(self, analog_mode=1, digital_mode=0):
        """
        Set the sync output mode.

        Keywords
        --------
        analog_mode : int (default: 1)
            0: No sync analog output; each channel goes to the default
            1: Analog 0 streams from sync data, analog 1 fixed value
            2: Analog 1 streams from sync data, analog 0 fixed value
            3: Both channels stream, alternating updates. Even sync data
                addresses go to analog 0, odd address to analog 1. Note that
                this halves the update rate of each analog channel, relative to
                the digital signals.
        digital_mode : int (default: 0)
            0: All 16 outputs derived from sync data
            1: "Or" mode. Channels 0-7 are logical "or"ed with channels 8-15.
                8-15 have the normal output. (Can be used to superimpose
                triggered and non-triggered signals.)
            2: "Swap" mode. Swaps channels 0-7 with 8-15. (Can be used to
                switch the digital signals without re-uploading.)
            3: "Swap-Or" mode.  Apply the swap and then the or operation.
        """
        self._cmd("SYNC MODE", analog_mode, digital_mode)
        return self._reply()

    def stop(self):
        "Stop the sync output."
        self._cmd(b"SYNC STOP")
        return self._reply()

    def write(self, addr, data, wait=True):
        """
        Write data to the sync memory.

        Parameters
        ----------
        addr : int
            The address to write to (0-16383)
        data : numpy array
            The data to write; should be a uint32 array.

        Keywords
        --------
        wait : bool (default: true)
            If True, wait for the write to finish before returning.
        """
        self._cmd("SYNC WRITE", addr, data)
        if wait:
            time.sleep((len(data) * 4 / self.byte_rate))
        return self._reply()

    def write_ad(self, addr, dig, ana, scale=1, wait=True):
        """
        Combine analog and digital data into single data stream and write those
        to the sync memory.

        Parameters
        ----------
        addr : int
            The address to write to (0-16383)
        dig : numpy array (integer)
            The digital data to write
        ana : numpy array (float)
            The analog data to write.

        Keywords
        --------
        scale : float (default: 1)
            The scale of the analog data -- plus or minus this value gets
            mapped to the full range.  (I.e. for the default, -1 -> 0 and
            +1 -> 65535.)
        wait : bool (default: true)
            If True, wait for the write to finish before returning.
        """
        if len(dig) != len(ana):
            raise ValueError("digital and analog data should have same length")

        data = np.asarray(dig, 'uint32') << 16
        data += (np.clip(ana * (0.5/scale) + 0.5, 0, 1)
                 * (self.ANALOG_MAX-1)).astype('uint32')

        return self.write(addr, data, wait=wait)

    def rate(self, rate):
        """
        Set the rate for the sync outputs

        Parameters
        ----------
        rate : float
            The output rate in Hz.  Will be rounded to the nearest mHz.
        """
        ipart = int(rate)
        fpart = int((rate - ipart) * 1000 + 0.5)
        self._cmd("SYNC RATE", ipart, fpart)
        return self._reply()

    def addr(self, start, count):
        """
        Set the address range for the sync outputs.

        Parameters
        ----------
        start : int
            The first address of the output.
        count : int
            The total number of ouptut data points
        """
        self._cmd("SYNC ADDR", start, count)
        return self._reply()

    def trigger(self, count=1):
        """
        Trigger channels indicated by trigger mask.

        Keywords
        --------
        counts : int (default: 1)
            The number of periods to trigger for.
        """
        if not isinstance(count, int):
            raise ValueError("Count must be an integer!")

        self._cmd("TRIGGER", count)
        return self._reply()

    def trigger_mask(self, mask):
        """
        Set the trigger mask for the sync outputs.

        Parameters
        ----------
        mask : int
            The bitmask for the trigger.  If a bit is high, then this channel
            is triggered.  (For example: if mask = 38 = 0b00100110 = (1<<1) +
            (1<<2) + (1<<5) then channels 1, 2, and 5 would be triggered.)
        """
        if not isinstance(mask, int):
            raise ValueError("Mask must be an integer!")

        self._cmd("TRIGGER MASK", mask)
        self._reply()

    def led(self, r, g, b):
        """
        Set the indicator LED output.

        Parameters
        ----------
        r, g, b : ints
            The brightness of each channel, 0-255.  Output is gamma corrected.
        """
        self._cmd("LED", r, g, b)
        self._reply()

    def _send_bin(self, data):
        if isinstance(data, np.ndarray):
            data = bytes(data)
        self.ser.write(b'>%d>' % len(data))
        self.ser.write(data)

    def ser_write(self, channel, data):
        """
        Write data to a tunneled serial channel

        Parameters
        ----------
        channel : int (1 or 2)
            The serial channel to write to.
        data : string, bytes, or numpy array
            The string to write.
        """
        if channel not in (1, 2):
            raise ADSyncError("channel must be 1 or 2")

        if isinstance(data, (str, bytes)):
            # Convert to an aray, so that _cmd sends it as binary
            data = np.fromstring(data, dtype='u1')
        elif not isinstance(data, np.ndarray):
            raise ValueError(
                "data should be a string, bytes object or numpy array"
            )

        self._cmd("SER%d WRITE" % channel, data)
        return self._reply()

    def ser_baud(self, channel, baud):
        """
        Set the baud rate of a tunneled serial port

        *Note:* currently the serial channels only work in 8N1 mode with no
        RTS, DTR, or other channels.  This should work with 99% of modern
        serial devices, but may fail in certain cases

        Parameters
        ----------
        channel : int (1 or 2)
            The serial channel to write to.
        baud : int
            The baud rate of the channel.  Non-standard values may not work,
            and will fail silently
        """
        if channel not in (1, 2):
            raise ADSyncError("channel must be 1 or 2")

        self._cmd("SER%d RATE" % channel, baud)
        return self._reply()

    def ser_read(self, channel, max_bytes=None):
        """
        Read data from a tunneled serial channel

        Parameters
        ----------
        channel : int (1 or 2)
            The serial channel to read from

        Keywords
        --------
        max_bytes : int
            If specified, the maximum number of bytes to return.  If not
            specified, will return everything in the buffer
        """
        if channel not in (1, 2):
            raise ADSyncError("channel must be 1 or 2")

        self._cmd("SER%d READ" % channel, max_bytes)
        return self._bin_reply()

    def ser_flush(self, channel):
        """
        Flush data from a tunneled serial channel

        Parameters
        ----------
        channel : int (1 or 2)
            The serial channel to read from
        """
        if channel not in (1, 2):
            raise ADSyncError("channel must be 1 or 2")

        self._cmd("SER%d FLUSH" % channel)
        return self._reply()

    def ser_available(self, channel):
        """
        Return the number of bytes available in a tunneled serial channel

        Parameters
        ----------
        channel : int (1 or 2)
            The serial channel to check
        """
        if channel not in (1, 2):
            raise ADSyncError("channel must be 1 or 2")
        self._cmd("SER%d AVAIL" % channel)
        reply = self._reply()
        try:
            return(int(reply))
        except ValueError:
            raise ADSyncError(
                'SER AVAIL returned "%s" (should have been an int)' % reply
            )

    def bluetooth(self, name=None):
        """
        Enable or disable bluetooth connection.

        Note: once set, the device will remember the bluetooth settings upon
        reboot!

        Keywords
        --------
        name : str or None (default: None)
            If a string is passed, enabled the bluetooth connection with the
            given name.  If None is passed, disable the bluetooth connection
        """
        if name:
            name = np.fromstring(name, dtype='u1')
        else:
            name = None
        self._cmd("BLUETOOTH", name)
        return self._reply()

    def close(self):
        "Close the serial port associated with the device."
        self.ser.close()

    def __del__(self):
        self.close()

class SmoothRamp:
    def __init__(self, t0=0, ts=1, tr=0.5, tj=None, rate=None):
        '''Create a smooth ramp function.

        Keywords
        --------
        t0 : float (default: 0)
            The time between the start of the linear ramp and the active region
        ts : float (default: 1)
            The time of the linear scan region, not including t0
        tr : float (default: 0.5)
            The time to return to the start of the scan
        tj : float (default: tr / 8)
            The jerk timescale, which sets the timescale over which the
            acceleration ramps up.  Should be at most tr / 4
        rate : float (default: 2.0 / ts)
            The scan ramp rate.  Default scans between -1 and 1 over ts

        After creating a ramp function, you can call it like a function, where
        the input is the times at which to compute the ramp.  Optionally, you
        can specify a keyword `d=[0-3]` in the function call, which outputs a
        derivative of the ramp function.
        '''
        if tj is None:
            self.tj = tr / 8
        else:
            self.tj = tj
        if rate is None:
            rate = 2.0 / ts

        self.t0 = t0 # Time from end of acceleration to beginning of active scan
        self.ts = ts # Active scan time
        self.tl = t0 + ts # Linear ramp time
        self.ta = 0.5 * (tr - 4 * self.tj) # Full acceleration time
        self.tr = tr # Return time

        self.x0 = -rate * (t0 + 0.5*ts)
        self.v0 = rate
        self.amax = 4 * (self.tl + self.tr) * self.v0 / (self.tr * (self.tr - 2 * self.tj))
        self.jerk = self.amax / self.tj

        # Motion profiles for each stage: (dt, x0, v0, a0, j)
        self.profile = []

        x = self.x0
        v = self.v0
        a = 0

        # The profiles are determined only by the time and the (constant) jerk
        for i, (dt, j) in enumerate([
                    (self.tl, 0),
                    (self.tj, -self.jerk),
                    (self.ta, 0),
                    (2*self.tj, self.jerk),
                    (self.ta, 0),
                    (self.tj, -self.jerk)
                ]):
            self.profile.append((dt, x, v, a, j))
            x += v*dt + (a/2)*dt**2 + (j/6)*dt**3
            v += a*dt + (j/2)*dt**2
            a += j*dt

        self.T = self.tl + self.tr

    def __call__(self, t, d=0):
        dt = (np.asarray(t) % self.T)
        sort = np.argsort(dt)
        unsort = np.argsort(sort)
        t = dt[sort]
        x = np.zeros_like(dt)
        i0 = 0

        for (dt, x0, v, a, j) in self.profile:
            try:
                i1 = np.where(t > dt)[0][0]
            except:
                i1 = len(t)
            tt = t[i0:i1]
            if d == 0:
                x[i0:i1] = x0 + v*tt + (a/2)*tt**2 + (j/6) * tt**3
            elif d == 1:
                x[i0:i1] = v + a*tt + (j/2)*tt**2
            elif d == 2:
                x[i0:i1] = a + j*tt
            elif d == 3:
                x[i0:i1] = j
            else:
                raise ValueError('derivative (d) should be 0--3')

            t -= dt
            i0 = i1

        return np.array(x[unsort])


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def getTargetCopy(self, original, cloned, target):
        val = target.val
        stack = [cloned]
        while stack:
            temp = []
            for node in stack:
                if node.val == val:
                    return node
                else:
                    if node.left: stack.append(node.left)
                    if node.right: stack.append(node.right)
            stack = temp

Solution/[1379][Easy] Find a Corresponding Node of a Binary Tree in a Clone of That Tree.py


import cv2
import os

def get_label_paths(path):
    path = os.path.join(path, 'labels')
    labels = []
    for filename in os.listdir(path):
        f = os.path.join(path, filename)
        # checking if it is a file
        if os.path.isfile(f):
            labels.append(f)

    return labels

def get_image_paths(path):

    path = os.path.join(path, 'images')
    images = []
    for filename in os.listdir(path):
        f = os.path.join(path, filename)
        # checking if it is a file
        if os.path.isfile(f):
            images.append(f)

    return images

def get_label(image, labels):
    for label in labels:
        label_name = str(label).replace('.txt', '').replace('labels', '')
        image_name = str(image).replace('.jpg', '').replace('images', '')

        if image_name == label_name:
            return label

    return None

def main():
    path_to_datasets_directory = ".."
    path = os.path.join(os.path.dirname(os.path.abspath(__file__)), path_to_datasets_directory, 'datasets', 'supermarket_datasetV2')

    images = get_image_paths(path)
    labels = get_label_paths(path)

    with open(os.path.join(path, "classes.txt")) as f:
        classes = f.readlines()

    for image in images:
        label = get_label(image, labels)

        if label:
            visualized_image = cv2.imread(image)
            with open(label) as f:
                yolo_labels = f.readlines()
            for yolo_label in yolo_labels:
                yolo_label = [float(x) for x in yolo_label.split(' ')]
                product_class = int(yolo_label[0])

                yolo_label = yolo_label[1:]
                x1 = int(yolo_label[0]*visualized_image.shape[1] - 0.5*yolo_label[2]*visualized_image.shape[1])
                y1 = int(yolo_label[1]*visualized_image.shape[0] - 0.5*yolo_label[3]*visualized_image.shape[0])
                x2 = int(yolo_label[0]*visualized_image.shape[1] + 0.5*yolo_label[2]*visualized_image.shape[1])
                y2 = int(yolo_label[1]*visualized_image.shape[0] + 0.5*yolo_label[3]*visualized_image.shape[0])

                cv2.rectangle(visualized_image, (x1, y1), (x2, y2), (0,0,255), 2)

                cv2.putText(visualized_image, classes[product_class].replace('\n', ''), (int(x1), int(y1)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 2, cv2.LINE_AA)
            cv2.imshow('image', visualized_image)
            cv2.waitKey(0)

if __name__ == "__main__":
    main()


# main.py will be my endpoint, always

# how will the CNN_Classifier/__init__.py work?
# check this code here

# import the logger created here CNN_Classifier/__init__.py
from CNN_Classifier import logger
# from src.CNN_Classifier import logger
# could also omit the src. part since we installed it from the setup.py file (I installed CNN_Classifier as a local package!)

# logger.info("Welcome to our custom log.")
# git bash -> python main.py
# [2023-11-16 18:14:55,724: INFO: main: Welcome to our custom log.]

#############################################################################################################################
#############################################################################################################################
#############################################################################################################################

# Data Ingestion
from CNN_Classifier.pipeline.stage_01_data_ingestion import DataIngestionTrainingPipeline

STAGE_NAME = "Data Ingestion stage"
try:
    logger.info(f">>>>> stage {STAGE_NAME} started <<<<<")
    data_ingestion = DataIngestionTrainingPipeline()
    data_ingestion.main()
    logger.info(f">>>>> stage {STAGE_NAME} completed <<<<<\n\nx=========x")
except Exception as e:
    logger.exception(e)
    raise e

# Prepare Base Model
from CNN_Classifier.pipeline.stage_02_prepare_base_model import PrepareBaseModelTrainingPipeline

STAGE_NAME = "Prepare Base Model"
try:
    logger.info(f"**************************************")
    logger.info(f">>>>> stage {STAGE_NAME} started <<<<<")
    prepare_base_model = PrepareBaseModelTrainingPipeline()
    prepare_base_model.main()
    logger.info(f">>>>> stage {STAGE_NAME} completed <<<<<\n\nx=========x")
except Exception as e:
    logger.exception(e)
    raise e

# Model training
from CNN_Classifier.pipeline.stage_03_model_training import ModelTrainingPipeline
STAGE_NAME = "Model Training"
try:
    logger.info(f"**************************************")
    logger.info(f">>>>> stage {STAGE_NAME} started <<<<<")
    model_trainer = ModelTrainingPipeline()
    model_trainer.main()
    logger.info(f">>>>> stage {STAGE_NAME} completed <<<<<\n\nx=========x")
except Exception as e:
    logger.exception(e)
    raise e

# Model evaluation
from CNN_Classifier.pipeline.stage_04_model_evaluation import EvaluationPipeline
STAGE_NAME = "Model Evaluation"
try:
    logger.info(f"**************************************")
    logger.info(f">>>>> stage {STAGE_NAME} started <<<<<")
    model_evalution = EvaluationPipeline()
    model_evalution.main()
    logger.info(f">>>>> stage {STAGE_NAME} completed <<<<<\n\nx=========x")
except Exception as e:
    logger.exception(e)
    raise e


#!/usr/bin/python3
"""
This script lists the first State object from database passed into program
"""

import sys
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

if __name__ == "__main__":
    engine = create_engine('mysql+mysqldb://{}:{}@localhost:3306/{}'
                           .format(sys.argv[1], sys.argv[2], sys.argv[3]))
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()

    state = session.query(State).first()

    if not state:
        print("Nothing")
    else:
        print("{}: {}".format(state.id, state.name))


from mysql import connector as sql
import csv

# Open the connection to the MySQL server and initialize cursor
connection = sql.MySQLConnection()
connection.connect(host='localhost', user='''removed for security''',
		passwd= '''removed for security''', db='WIRC_POL')
cursor = connection.cursor()

# Create the WIRC_POL database
cursor.execute('CREATE DATABASE IF NOT EXISTS WIRC_POL ')
#cursor.execute('USE WIRC_POL')

### Upload targets to database ###
# Gather the columns to be used, formatting them appropriately for MySQL
# Make all types TEXT(1000), can refine later?
f = open('master_BD.csv', 'r')
columns_str = f.readline()
f.close()
columns_str = columns_str[:-1]
columns_list = columns_str.split(',')
for i in range(0, len(columns_list)):
	columns_list[i] = '`' + columns_list[i] + '`'
columns_str = ' TEXT, '.join(columns_list) + ' TEXT'
columns = '(' + columns_str + ')'
 # for i in range(0, len(columns_list)):
 # 	columns_list[i] += ' TEXT'
columns_type = '(id INT(11) NOT NULL AUTO_INCREMENT, ' + columns_str + ', PRIMARY KEY (id))'
# Create a table to hold the data we will load in, using the gathered columns
cursor.execute("CREATE TABLE IF NOT EXISTS targets "+ columns_type)
# Format the VALUES part of the INSERT INTO query: 'VALUES(%s, %s, ... %s)'
values_str = 'VALUES('
for i in range(0, len(columns_list) - 1):
	values_str += '%s, '
values_str += '%s)'
# Load in the data from the csv file
csv_data = csv.reader(file('master_BD.csv'))
i = 0
# for col in columns_list:
# 	cursor.execute('SELECt * FROM targets LIMIT 1')
# 	cursor.fetchall()
# 	if col not in cursor.column_names:
# 		cursor.execute('ALTER TABLE targets ADD ' + col + ' TEXT')
columns = '(' + ', '.join(columns_list) + ')'
for row in csv_data:
	if i == 0:
		i = 1
		continue
	#cursor.execute('DELETE FROM targets WHERE `Name` = \"{}\"'.format(row[0]))
	cursor.execute('INSERT INTO targets ' + columns + ' ' + values_str, row)

### Upload standards to database ###
# Gather the columns to be used, formatting them appropriately for MySQL
# Make all types TEXT(1000), can refine later
f = open('standards.csv', 'r')
columns_str = f.readline()
f.close()
columns_str = columns_str[:-2]
columns_list = columns_str.split(',')
for i in range(0, len(columns_list)):
	columns_list[i] = '`' + columns_list[i] + '`'
columns_str = ', '.join(columns_list)
columns = '(' + columns_str + ')'
for i in range(0, len(columns_list)):
	columns_list[i] += ' TEXT'
columns_type = '(stan_id INT(11) NOT NULL AUTO_INCREMENT, ' + ', '.join(columns_list) + ', PRIMARY KEY (stan_id))'
# Create a table to hold the data we will load in, using the gathered columns
cursor.execute("CREATE TABLE IF NOT EXISTS standards "+ columns_type)
# Format the VALUES part of the INSERT INTO query: 'VALUES(%s, %s, ... %s)'
values_str = 'VALUES('
for i in range(0, len(columns_list) - 1):
	values_str += '%s, '
values_str += '%s)'
# Load in the data from the csv file
csv_data = csv.reader(file('standards.csv'))
i = 0
for row in csv_data:
	if i == 0:
		i = 1
		continue
	cursor.execute('INSERT INTO standards' + columns + ' ' + values_str, row)

### Upload target references to database ###
# # Gather the columns to be used, formatting them appropriately for MySQL
# # Make all types TEXT(1000), can refine later
f = open('target_refs.csv', 'r')
columns_str = f.readline()
f.close()
columns_str = columns_str[:-2]
columns_list = columns_str.split(',')
for i in range(0, len(columns_list)):
	columns_list[i] = '`' + columns_list[i] + '`'
columns_str = ', '.join(columns_list)
columns = '(' + columns_str + ')'
for i in range(0, len(columns_list)):
	columns_list[i] += ' TEXT'
columns_type = '(tar_id INT(11) NOT NULL, ' + \
				', '.join(columns_list) + ')'
# Create a table to hold the data we will load in, using the gathered columns
cursor.execute("CREATE TABLE IF NOT EXISTS target_refs "+ columns_type)
# Format the VALUES part of the INSERT INTO query: 'VALUES(%s, %s, ... %s)'
values_str = 'VALUES('
for i in range(0, len(columns_list) - 1):
	values_str += '%s, '
values_str += '%s)'
# Load in the data from the csv file
csv_data = csv.reader(file('target_refs.csv'))
i = 0
for row in csv_data:
	if i == 0:
		i = 1
		continue
	cursor.execute('INSERT INTO target_refs' + columns + ' ' + values_str, row)

# Close the connection
connection.commit()
cursor.close()
connection.close()

# The below is used to finish up target_refs id matching
'''
After this is set up, you need to do id matching on target_refs.
On mysql, execute:

UPDATE target_refs
SET tar_id = -1;

INSERT INTO targets (id, Name)
VALUES (-1, "None/Unknown");

UPDATE target_refs, targets
SET target_refs.tar_id = targets.id
WHERE targets.Name LIKE CONCAT("%", target_refs.name) OR
target_refs.Name LIKE CONCAT("2 MASS ", targets.`2MASS Name`);

ALTER TABLE target_refs
ADD INDEX ref_id (tar_id);

ALTER TABLE target_refs
ADD FOREIGN KEY (tar_id)
REFERENCES targets(id);
'''

SURF 2018/File Database/DB Setup/config_db.py


#coding:utf-8
'''
Created on 2017年1月17日

@author: coldJune
'''
from bs4 import BeautifulSoup
import urllib.request
import re

url='https://github.com/coldJune'

def catchLink(url):
    '''
                提 取网页中的超链接
    '''
    links=[]
    html=urllib.request.urlopen(url)
    html=html.read()
    soup=BeautifulSoup(html,'lxml')
    a=soup.findAll('a')
    for link in a:
        links=a['href']
    return
if __name__=='__main__':
    catchLink(url)


import asyncio
import logging

from aiogram.types import Message, ParseMode
from aiogram import Dispatcher

from main import dp
from service.main_menu import create_default_profile
from keyboards import replykeyboards, inlinekeyboards
from messages import default, templates

@dp.message_handler(commands=['start'])
async def send_welcome_messages(message: Message):
    """
        Sending welcome messages and set default user profile
    """
    user_id: int = message.from_user.id
    username: str = message.from_user.username
    user_full_name: str = message.from_user.full_name

    logging.info(f"User {user_id} enter in bot")

    task_creating_profile = asyncio.create_task(create_default_profile(user_id, username))
    await task_creating_profile
    await message.answer(templates.WELCOME_MESSAGE.format(user_full_name),
                         parse_mode=ParseMode.HTML, reply_markup=inlinekeyboards.TUTORIAL_MARKUP)
    await asyncio.sleep(1)
    await message.answer(default.MAIN_MENU_INFO_MESSAGE,
                         parse_mode=ParseMode.HTML,
                         reply_markup=replykeyboards.MAIN_MENU_MARKUP)

@dp.message_handler(lambda message: message.text == replykeyboards.BUTTON_RETURN_TO_MAIN_MENU)
async def return_to_main_menu(message: Message):
    """
        Load main menu if user press return button in another menu
    """
    await message.answer(default.MAIN_MENU_INFO_MESSAGE,
                         parse_mode=ParseMode.HTML,
                         reply_markup=replykeyboards.MAIN_MENU_MARKUP)

@dp.message_handler(commands=['menu'])
async def open_menu(message: Message):
    await return_to_main_menu(message)

@dp.message_handler(commands=['tutorial'])
async def send_tutorials(message: Message):
    await message.answer("https://t.me/seekleads/1")

@dp.message_handler(commands=['support'])
async def send_admin_contact(message: Message):
    await message.answer("При возникновении проблем при работе с ботом, напишите мне @Mr_GriM01",
                         reply_markup=inlinekeyboards.HELP_MARKUP)

@dp.message_handler(commands=['manager'])
async def send_manager_contact(message: Message):
    await message.answer("Контакт менеджера @I_III_I_II", reply_markup=inlinekeyboards.MANAGER_MARKUP)

@dp.message_handler(lambda message: message.text == replykeyboards.BUTTON_CHAT_LIST)
async def get_list_chat_settings(message: Message):
    """
        Load chats settings menu
    """
    await message.answer(default.CHAT_LIST_INFO,
                         parse_mode=ParseMode.HTML,
                         reply_markup=replykeyboards.CHAT_LIST_MARKUP)
    await asyncio.sleep(0.2)
    await message.answer(default.HELP_WITH_CHATS_MESSAGE,
                         reply_markup=inlinekeyboards.MANAGER_MARKUP)

@dp.message_handler(lambda message: message.text == replykeyboards.BUTTON_KEY_EXCEPT_WORDS)
async def get_key_except_words_settings(message: Message):
    """
        Load key/except words settings menu
    """
    await message.answer(default.KEY_EXCEPT_WORDS_INFO,
                         parse_mode=ParseMode.HTML,
                         reply_markup=replykeyboards.KEY_EXCEPT_MARKUP)

@dp.message_handler(lambda message: message.text == replykeyboards.BUTTON_SEARCHER_INSTANT)
async def get_instant_searcher_settings(message: Message):
    """
        Load searcher setting menu for instant result
    """
    await message.answer(default.SEARCHER_SETTINGS_INFO,
                         parse_mode=ParseMode.HTML,
                         reply_markup=replykeyboards.SEARCHER_INSTANT_MODE)

@dp.message_handler(lambda message: message.text == replykeyboards.BUTTON_INFO_SUBSCRIPTION)
async def get_subscription_info(message: Message):
    """
        Load subscription info menu
    """
    await message.answer(default.SUBSCRIPTION_SETTINGS_INFO,
                         reply_markup=replykeyboards.SUBSCRIPTION_INFO_MARKUP)

@dp.message_handler(lambda message: message.text == replykeyboards.BUTTON_SEARCHER_ONLINE)
async def get_online_searcher_settings(message: Message):
    """
        Load searcher setting menu for online search
    """
    await message.answer(default.ONLINE_SETTINGS_INFO,
                         reply_markup=replykeyboards.BOT_SETTINGS_MARKUP)

def register_main_menu(dispatcher: Dispatcher):
    dispatcher.register_message_handler(send_welcome_messages,
                                        commands=['start'])
    dispatcher.register_message_handler(open_menu,
                                        commands=['menu'])
    dispatcher.register_message_handler(send_tutorials,
                                        commands=['tutorial'])
    dispatcher.register_message_handler(send_admin_contact,
                                        commands=['support'])
    dispatcher.register_message_handler(send_manager_contact,
                                        commands=['manager'])
    dispatcher.register_message_handler(return_to_main_menu,
                                        lambda message: message.text == replykeyboards.BUTTON_RETURN_TO_MAIN_MENU)
    dispatcher.register_message_handler(get_list_chat_settings,
                                        lambda message: message.text == replykeyboards.BUTTON_CHAT_LIST)
    dispatcher.register_message_handler(get_key_except_words_settings,
                                        lambda message: message.text == replykeyboards.BUTTON_KEY_EXCEPT_WORDS)
    dispatcher.register_message_handler(get_instant_searcher_settings,
                                        lambda message: message.text == replykeyboards.BUTTON_SEARCHER_INSTANT)
    dispatcher.register_message_handler(get_subscription_info,
                                        lambda message: message.text == replykeyboards.BUTTON_INFO_SUBSCRIPTION)
    dispatcher.register_message_handler(get_online_searcher_settings,
                                        lambda message: message.text == replykeyboards.BUTTON_SEARCHER_ONLINE)


import torch, logging
import torch.nn as nn
import numpy as np

from dsl import *
from pcfg import PCFG
from pcfg_logprob import LogProbPCFG

class Q_Predictor(nn.Module):
    def __init__(self,
        feature_extractor,
        template_dsl,
        template_cfg,
        list_variables):
        """
        feature_extractor: a neural network module
        taking a list of tasks (each task is a list of input-outputs)
        and returning a tensor of shape
        [len(list_of_tasks), feature_extractor.output_dimensionality]

        template_dsl: a dsl giving the list of primitives

        template_cfg: a cfg giving the structure that will be output
        """
        super(Q_Predictor, self).__init__()

        self.feature_extractor = feature_extractor
        H = self.feature_extractor.output_dimensionality # hidden

        self.template_cfg = template_cfg
        self.template_dsl = template_dsl

        self.number_of_primitives = len(template_dsl.list_primitives)
        self.list_variables = list_variables
        self.number_of_outputs = self.number_of_primitives + len(self.list_variables)
        self.number_of_parents = self.number_of_primitives + 1 # parent can be None
        self.maximum_arguments = max(len(primitive.type.arguments())
                                     for primitive in template_dsl.list_primitives)
        self.q_predictor = nn.Linear(H,
             self.number_of_outputs*self.number_of_parents*self.maximum_arguments)
        # print(self.number_of_outputs*self.number_of_parents*self.maximum_arguments)
    def q_vector_to_dictionary(self,q):
        """
        q: size self.number_of_outputs*self.number_of_parents*self.maximum_arguments
        """
        q = q.view(self.number_of_parents, self.maximum_arguments, self.number_of_outputs)
        q = nn.LogSoftmax(-1)(q)

        q_dictionary = {}
        for parent_index, parent in enumerate([None] + self.template_dsl.list_primitives):
            for argument_number in range(self.maximum_arguments):
                for primitive_index, primitive in enumerate(self.template_dsl.list_primitives):
                    q_dictionary[parent, argument_number, primitive] = \
                    q[parent_index, argument_number, primitive_index]
                for variable_index, variable in enumerate(self.list_variables):
                    q_dictionary[parent, argument_number, variable] = \
                    q[parent_index, argument_number, self.number_of_primitives + variable_index]
        return q_dictionary

    def forward(self, tasks):
        """
        tasks: list of tasks

        returns: list of PCFGs
        """
        features = self.feature_extractor(tasks)
        q = self.q_predictor(features)
        q_dictionary = [self.q_vector_to_dictionary(q[b])
              for b in range(len(tasks))]
        grammars = [self.template_cfg.Q_to_PCFG(q_dictionary[b])
              for b in range(len(tasks))]
        return grammars

    def train(self, programs, tasks, epochs=200):
        optimizer = torch.optim.Adam(self.parameters())

        for step in range(epochs):
            optimizer.zero_grad()
            grammars = self(tasks)
            likelihood = sum(grammar.log_probability_program(self.template_cfg.start, program)
                             for grammar,program in zip(grammars, programs))
            (-likelihood).backward()
            optimizer.step()

            if step % 100 == 0:
                logging.debug("optimization step {}\tlog likelihood {}".format(step, likelihood))
                logging.debug("grammars {}".format(grammars))

    def test(self, programs, tasks):
        grammars = self(tasks)
        for grammar, program in zip(grammars, programs):
            grammar = grammar.normalise()
            logging.debug("predicted grammar {}".format(grammar))
            logging.info("intended program {}\nprobability {}".format(program, grammar.probability_program(self.template_cfg.start, program)))


from tensorflow import keras
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import os

def save_fig(fig_id, Images_path, tight_layout=False, fig_extension="pdf", resolution=300):
    path = os.path.join(Images_path, fig_id + "." + fig_extension)
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format=fig_extension, dpi=resolution)

def plot_series(series, n_steps, y=None, y_pred=None, x_label="Trading Days", y_label="Closing Price", legend=True):
    plt.plot(series,".-",color='steelblue')
    if y is not None:
        plt.plot(n_steps, y, ".-",color='lightskyblue', label="Target")
    if y_pred is not None:
        plt.plot(n_steps, y_pred, "x-",color='orange', markersize=5, label="Prediction")
    plt.grid(True)
    if x_label:
        plt.xlabel(x_label, fontsize=10)
    if y_label:
        plt.ylabel(y_label, fontsize=10)
    plt.hlines(0, 0, 100, linewidth=1)
    plt.axis([0, n_steps + 1, 0, 1])
    if legend and (y or y_pred):
        plt.legend(fontsize=10, loc="upper left")

def plot_learning_curves(loss, val_loss):
    plt.plot(np.arange(len(loss)) + 0.5, loss, "b.-", label="Training loss")
    plt.plot(np.arange(len(val_loss)) + 1, val_loss, "r.-", label="Validation loss")
    plt.gca().xaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))
    plt.axis([1, 20, 0, 0.05])
    plt.legend(fontsize=14)
    plt.xlabel("Epochs")
    plt.ylabel("Loss")
    plt.grid(True)

def plot_multiple_forecasts_3D(X, Y, Y_pred):
    n_steps = X.shape[1]
    ahead = Y.shape[1]
    plot_series(X[0, :, 0], n_steps)
    plt.plot(np.arange(n_steps, n_steps + ahead), Y[0, :, 0], ".-",color='lightskyblue', label="Actual")
    plt.plot(np.arange(n_steps, n_steps + ahead), Y_pred[0, :, 0], "x-",color='orange', label="Forecast", markersize=5)
    plt.axis([n_steps - 5, n_steps + ahead, 0, 200])
    plt.legend(fontsize=10)

def plot_multiple_forecasts_3D_test(X, Y, Y_pred):
    n_steps = X.shape[1]
    ahead = Y.shape[2]
    iterations = [0,60,120,180]
    plot_series(X[0, :, 0], n_steps)
    for i in iterations:
        plt.plot(np.arange(n_steps + i, n_steps + ahead + i), Y[i, -1, :], ".-",color='lightskyblue', label="Actual")
        plt.plot(np.arange(n_steps + i, n_steps + ahead + i), Y_pred[i, :, :], "x-",color='orange', label="Forecast", markersize=5)
    plt.axis([n_steps - 5, 500, 0, 2])
    plt.legend(fontsize=10)

def plot_multiple_forecasts_2D(X, Y, Y_pred):
    n_steps = X.shape[1]
    ahead = Y.shape[1]
    plot_series(X[0, :], n_steps)
    plt.plot(np.arange(n_steps, n_steps + ahead), Y[0, :], ".-",color='lightskyblue', label="Actual")
    plt.plot(np.arange(n_steps, n_steps + ahead), Y_pred[0, :], "x-",color='orange', label="Forecast", markersize=5)
    plt.axis([n_steps - 5, n_steps + ahead, 0, 200])
    plt.legend(fontsize=10)

def last_time_step_mse(Y_true, Y_pred):
    return keras.metrics.mean_squared_error(Y_true[:, -1], Y_pred[:, -1])

def create_sets_vector(db_scaled, n_steps, t_steps):
    new_series = []
    for row in range(len(db_scaled)-n_steps-t_steps+1):
        ser = db_scaled[row:row+n_steps+t_steps]
        new_series.append(ser)

    new_series = np.array(new_series)
    new_series = new_series.reshape(new_series.shape[0], new_series.shape[1], 1)

    len_set = len(new_series)
    len_train = int(2/3 * len_set)
    len_val = int(5/6 * len_set)
    len_test = len_set - 1

    X_train, Y_train = new_series[:len_train, :n_steps], new_series[:len_train,-t_steps:-1,0]
    X_val, Y_val = new_series[len_train:len_val, :n_steps], new_series[len_train:len_val, -t_steps:,0]
    X_test, Y_test = new_series[len_val:len_test, :n_steps], new_series[len_val:len_test, -t_steps:,0]
    X_new, Y_new = new_series[-1:,:n_steps], new_series[len_test:,-t_steps:,0]

    return X_train, Y_train, X_val, Y_val, X_test, Y_test, X_new, Y_new

def split_set(db):
    len_set = len(db)
    len_train = int(2/3 * len_set)
    len_val = int(5/6 * len_set)

    train, val, test = db[:len_train], db[len_train:len_val], db[len_val:]
    train = np.array(train).reshape(-1,1)
    val = np.array(val).reshape(-1,1)
    test = np.array(test).reshape(-1,1)
    return train, val, test

def preprocess_data(db_scaled,n_steps,t_steps):
    new_series = []
    for row in range(len(db_scaled)-n_steps-t_steps+1):
        ser = db_scaled[row:row+n_steps+t_steps]
        new_series.append(ser)

    new_series = np.array(new_series)
    new_series = new_series.reshape(new_series.shape[0], new_series.shape[1], 1)

    len_set = len(new_series)

    Y = np.empty((len_set, n_steps, t_steps))
    for step_ahead in range(1, t_steps + 1):
        Y[ : , : ,step_ahead - 1] = new_series[ : , step_ahead:step_ahead + n_steps, 0]

    X = new_series[:,:n_steps]

    return  X, Y

def create_sets_sequence_sc(db, n_steps, t_steps):

    len_set = len(db)
    len_train = int(2/3 * len_set)
    len_val = int(5/6 * len_set)
    len_test = len_set

    print(len_set)
    print(len_train)
    print(len_val)
    print(len_test)

    train = db[:len_train]
    val = db[len_train:len_val]
    test = db[len_val:len_test]

    scaler = MinMaxScaler()
    train = scaler.fit_transform(train)
    val = scaler.transform(val)
    test = scaler.transform(test)

    #train, mean_train, std_train = transf_data(train)
    #val, mean_val, std_val = transf_data(val)
    #test, mean_test, std_test = transf_data(test)

    def preprocess_data(db_scaled):
        new_series = []
        for row in range(len(db_scaled)-n_steps-t_steps+1):
            ser = db_scaled[row:row+n_steps+t_steps]
            new_series.append(ser)

        new_series = np.array(new_series)
        new_series = new_series.reshape(new_series.shape[0], new_series.shape[1], 1)

        len_set = len(new_series)

        Y = np.empty((len_set, n_steps, t_steps))
        for step_ahead in range(1, t_steps + 1):
            Y[ : , : ,step_ahead - 1] = new_series[ : , step_ahead:step_ahead + n_steps, 0]

        X = new_series[:,:n_steps]

        return  X, Y

    X_train, Y_train = preprocess_data(train)
    X_val, Y_val = preprocess_data(val)
    X_test, Y_test = preprocess_data(test)

    return X_train, Y_train, X_val, Y_val, X_test, Y_test, #mean_test, #std_test

def create_sets_sequence(db_scaled, n_steps, t_steps):
    new_series = []
    for row in range(len(db_scaled)-n_steps-t_steps+1):
        ser = db_scaled[row:row+n_steps+t_steps]
        new_series.append(ser)

    new_series = np.array(new_series)
    new_series = new_series.reshape(new_series.shape[0], new_series.shape[1], 1)

    len_set = len(new_series)
    len_train = int(2/3 * len_set)
    len_val = int(5/6 * len_set)
    len_test = len_set - 1

    Y = np.empty((len_set, n_steps, t_steps))
    for step_ahead in range(1, t_steps + 1):
        Y[ : , : ,step_ahead - 1] = new_series[ : , step_ahead:step_ahead + n_steps, 0]

    X_train, Y_train = new_series[:len_train, :n_steps], Y[:len_train]
    X_val, Y_val = new_series[len_train:len_val, :n_steps], Y[len_train:len_val]
    X_test, Y_test = new_series[len_val:len_test, :n_steps], Y[len_val:len_test]
    X_new, Y_new = new_series[-1:,:n_steps], new_series[len_test:,n_steps:]

    return X_train, Y_train, X_val, Y_val, X_test, Y_test, X_new, Y_new

def read_data(file_input):
    dataset = pd.read_csv(file_input, skiprows=1)
    dataset.columns = ['Date', 'Close']

    dataset = dataset.dropna(how='any')

    db = dataset['Close']
    return db

def transf_data(db):
    #db = np.log(db) / np.array(10)
    #return db
    db = np.log(db)
    mean_db = np.mean(db)
    std_db = np.std(db)
    new_db = (db - np.array(mean_db)) / np.array(std_db)

    return new_db, mean_db, std_db

def inv_tranf_data(output, mean_db, std_db):
    #output = np.array(10) * output
    #output = np.exp(output)
    #return output

    output = output * np.array(std_db)
    output = output + mean_db
    output = np.exp(output)

    return output

def inv_tranf_data_sc(output, mean_db, std_db):

    pass

class MCDropout(keras.layers.Dropout):
    def call(self, inputs):
        return super().call(inputs, training=True)

    def get_config(self):
        return {
        'rate': self.rate,
        'noise_shape': self.noise_shape,
        'seed': self.seed
         }

    @classmethod
    def from_config(cls, config):
        return cls(**config)

class GatedActivationUnit(keras.layers.Layer):
    def __init__(self, activation="tanh", **kwargs):
        super().__init__(**kwargs)
        self.activation = keras.activations.get(activation)
    def call(self, inputs):
        n_filters = inputs.shape[-1] // 2
        linear_output = self.activation(inputs[..., :n_filters])
        gate = keras.activations.sigmoid(inputs[..., n_filters:])
        return self.activation(linear_output) * gate

def wavenet_residual_block(inputs, n_filters, dilation_rate):
    z = keras.layers.Conv1D(2 * n_filters, kernel_size=2, padding="causal",
                            dilation_rate=dilation_rate)(inputs)
    z = GatedActivationUnit()(z)
    z = keras.layers.Conv1D(n_filters, kernel_size=1)(z)
    return keras.layers.Add()([z, inputs]), z


import json
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
from tmtoolkit.topicmod.evaluate import metric_coherence_gensim
import pandas as pd
import numpy as np
import pickle
from multiprocessing import Pool
from tqdm import tqdm
import multiprocessing as mp
import sys
import os

project_path = os.path.join(os.path.dirname(__file__), "..")
print(project_path)
num_cpus = mp.cpu_count()

def tokenize(doc):
    tokens = doc.split(" ")
    tokens = [word for word in tokens if len(word.strip()) > 0]
    return tokens

if __name__ == "__main__":

    print("Loading tech corpus")
    with open(os.path.join(project_path, "data/tech_review_word_corpus_nn_adj_adv.pkl"), "rb") as f:
        tech_review_corpus = pickle.load(f)
    reviews = pd.DataFrame(tech_review_corpus).review.tolist()

    print("Total workers:", num_cpus)

    print("Tokenize the corpus")

    with open(os.path.join(project_path, "data/stop_words.json"), "r") as f:
        stop_words = json.load(f)

    vectorizer = CountVectorizer(
        min_df=3, max_df=.90, tokenizer=tokenize, stop_words=stop_words, ngram_range=(1, 2))
    X = vectorizer.fit_transform(reviews)
    print("Total Vocab Size", len(vectorizer.vocabulary_))

    sum_words = X.sum(axis=0)
    words_freq = [(word, sum_words[0, idx])
                  for word, idx in vectorizer.vocabulary_.items()]
    print(sorted(words_freq, key=lambda x: x[1], reverse=True)[:50])

    topics_range = range(15, 6, -1)
    alpha = list(np.arange(0.01, 1, 0.3))
    alpha.append(None)
    beta = list(np.arange(0.01, 1, 0.3))
    beta.append(None)

    parameters = []
    for k in topics_range:
        for a in alpha:
            for b in beta:
                parameters.append({
                    "k": k, "alpha": a, "beta": b
                })

    print("Total parameter values to train", len(parameters))

    result = []

    for param in tqdm(parameters):
        aspect_file_name = "results/LDA/lda-aspect-k-{0}-a-{1}-b-{2}-reduced_words.json".format(
            str(param["k"])[:4],
            str(param["alpha"])[:4],
            str(param["beta"])[:4]
        )

        lda = LatentDirichletAllocation(
            learning_method="batch",
            random_state=100,
            n_components=param["k"],
            doc_topic_prior=param["alpha"],
            topic_word_prior=param["beta"],
            n_jobs=num_cpus - 2,
            max_iter=50
        )

        lda.fit(X)

        score = metric_coherence_gensim(measure='u_mass',
            top_n=25,
            topic_word_distrib=lda.components_,
            dtm=X,
            vocab=np.array([x for x in vectorizer.vocabulary_.keys()]),
            return_mean=True
        )

        result.append({
            "k":param["k"],
            "alpha":param["alpha"],
            "beta":param["beta"],
            "score":score
        })

        pd.DataFrame(result).to_csv(os.path.join(project_path,"data/lda_noun_umass.csv"))

    print("Done!")


from django import forms
from .models import Day, Comment

class DayCreateForm(forms.ModelForm):

    class Meta:
        model = Day
        fields = '__all__'

class CommentCreateForm(forms.ModelForm):

    def __int__(self, *args, **kwargs):
        super().__int__(*args, **kwargs)
        for field in self.fields.values():
            field.widget.attrs['class'] = 'form-control'

    class Meta:
        model = Comment
        fields = ('name', 'text', 'date')


from queue import deque
from typing import List

def allPathsSourceTarget(start: int, end: int, graph: List[List[int]]) -> List[List[int]]:
        paths = []

        def backtrack(node, path):
            if node == end:
                paths.append(list(path))
                return
            for nextNode in graph[node]:
                path.append(nextNode)
                backtrack(nextNode, path)
                path.pop()

        path = deque([start])
        backtrack(start, path)
        return paths

g = [[4,3,1],[3,2,4],[3],[4],[]]

print(allPathsSourceTarget(1, 4, g))


import sys

lst = []
for i in range(int(sys.stdin.readline())):
    inp = sys.stdin.readline().split()
    if len(inp) == 2:
        inp[1] = int(inp[1])
        m, n = inp
        if m == "add":
            if n not in lst:
                lst.append(n)
        if m == "remove":
            if n in lst:
                lst.remove(n)
        if m == "check":
            if n in lst:
                print(1)
            else:
                print(0)
        if m == "toggle":
            if n in lst:
                lst.remove(n)
            else:
                lst.append(n)
    else:
        if inp[0] == "all":
            lst = [i for i in range(1, 21)]
        else:
            lst = []


import pandas as pd
import numpy as np
import argparse
import sys

pd.set_option('display.max_columns', 20)
pd.set_option('display.max_rows', 300)

parser = argparse.ArgumentParser()
parser.add_argument('--input', nargs = '+', help = 'input data path')
args = parser.parse_args()

team_counts = {}
number_of_pitch_types = []
games_played = []
ppg_df = pd.DataFrame([], columns = ['count', 'StarterVsCloser'])
left_count = 0
right_count = 0
pitch_types_count_dict = {}

for pitcher in args.input:
    df = pd.read_csv(pitcher)
    last_index = len(df) - 1

    # Assign pitcher to last team they played for
    if df.loc[last_index, 'inning_half'] == 'top':
        team = df.loc[last_index, 'home_team']
    else:
        team = df.loc[last_index, 'away_team']

    if team in team_counts:
        team_counts[team] += 1
    else:
        team_counts[team] = 1

    # Number of pitch types
    unique_pitch_types = df['group_pitch_type'].unique()
    pitches = len(unique_pitch_types)
    p_type = df.loc[0, 'StartVsRelief']
    number_of_pitch_types.append([pitches, p_type])

    # Total games
    g_played = len(df['game_id'].unique())
    games_played.append([g_played, p_type])

    # Pitches per game
    p_per_game = df.groupby('game_id')['home_team'].count().reset_index()
    p_per_game.rename(columns = {'home_team':'count'}, inplace = True)
    p_per_game['StarterVsCloser'] = p_type
    p_per_game.drop(['game_id'], axis = 1, inplace = True)
    ppg_df = pd.concat([ppg_df, p_per_game], axis = 0)

    # Left verse Right
    hand = df.loc[0, 'p_handedness']
    if hand == 'R':
        right_count += 1
    else:
        left_count += 1

    # Pitch types
    for pitch_type in unique_pitch_types:
        print(pitch_type)
        if pitch_type in pitch_types_count_dict:
            pitch_types_count_dict[pitch_type] += 1
        else:
            pitch_types_count_dict[pitch_type] = 1

teams_list = []
for key, value in team_counts.items():
    temp = [key,value]
    teams_list.append(temp)

df = pd.DataFrame(teams_list, columns = ['team', 'count'])
#print('teams')
#print(df, '\n')
df.to_csv('visualisations/exploratory_analysis/teams.csv', index = False)

pitch_types_count = pd.DataFrame(number_of_pitch_types, columns = ['count', 'StarterVsCloser'])
#print('number of pitch types')
#print(pitch_types_count, '\n')
pitch_types_count.to_csv('visualisations/exploratory_analysis/pitch_type_counts.csv', index = False)

ppg_df.reset_index(drop = True, inplace = True)
#print('pitches per game')
#print(ppg_df)
ppg_df.to_csv('visualisations/exploratory_analysis/pitches_per_game.csv', index = False)

hand_df = pd.DataFrame([['Right', right_count],['Left', left_count]], columns = ['hand', 'count'])
#print(hand_df)
hand_df.to_csv('visualisations/exploratory_analysis/handedness.csv', index = False)

gp_df = pd.DataFrame(games_played, columns = ['count', 'StarterVsCloser'])
#print(gp_df)
gp_df.to_csv('visualisations/exploratory_analysis/games_played.csv', index = False)

pitch_type_list = []
for key, value in pitch_types_count_dict.items():
    temp = [key, value]
    pitch_type_list.append(temp)

type_df = pd.DataFrame(pitch_type_list, columns = ['Pitch_Type', 'Count'])
type_df = type_df.loc[~type_df['Pitch_Type'].isnull(), :]
type_df.to_csv('visualisations/exploratory_analysis/pitch_types.csv', index = False)


import re

def email_check(email, domain):
    response = re.findall("^[a-z]+[1-9]*[a-z]*@{}$".format(domain), email)
    if response:
        return "Valid"
    else:
        return "Invalid"

print(email_check("harrydbst123@gmail.com", "gmail.com"))


import mysql.connector
from mysql.connector import errorcode
import json
import logging

def setup_logger(logger_name, log_file, level=logging.INFO):
    log_setup = logging.getLogger(logger_name)
    formatter = logging.Formatter('[%(levelname)s] %(message)s')
    file_handler = logging.FileHandler(log_file, mode='w')
    file_handler.setFormatter(formatter)
    log_setup.setLevel(level)
    log_setup.addHandler(file_handler)
    return log_setup

def file_manager(filename, logger, file_type='JSON', mode='r'):
    try:
        if file_type == 'JSON':
            if mode == 'r':
                with open(filename, mode) as f:
                    return json.loads(f.read())

        elif file_type == 'text':
            if mode == 'r':
                with open(filename, mode) as f:
                    return [x.strip() for x in f.readlines()]

    except Exception as err:
        logger.exception(err)
        raise err

def connect_db(logger):
    cursor = None
    try:
        conn_details = file_manager('config/connection.json', logger)
        connection = mysql.connector.connect(**conn_details)
        if connection.is_connected():
            logger.info("DB Connection Established with host %s of database %s \n" %
                         (conn_details['host'], conn_details['database']))
            cursor = connection.cursor()

    except (mysql.connector.Error, Exception) as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            logger.error("Bad Credentials for %s \n" % conn_details['database'])
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            logger.exception("Database %s does not exist \n" % conn_details['database'])
        else:
            logger.exception(err)
        raise err

    else:
        return connection, cursor


# LeetCode:
# 16.02.2022
# Runtime: 410 ms, faster than 68.45% of Python3 online submissions for Find All Numbers Disappeared in an Array.
# Memory Usage: 26.2 MB, less than 10.15% of Python3 online submissions for Find All Numbers Disappeared in an Array.

# Test:
# if nums = [1,1]
# return [2]

from typing import List

def findDisappearedNumbers(nums: List[int]) -> List[int]:
    n_nums = [i for i in range(1, len(nums) + 1)]
    return [i for i in set(n_nums).difference(set(nums))]

nums = [4,3,2,7,8,2,3,1]
print(findDisappearedNumbers(nums))


from django import forms
from django.utils.translation import gettext_lazy as _
from apps.core.models import Event, Event_Status
from apps.tag.models import Tags
from django.utils.log import getLogger

log = getLogger('django')

class BaseEventForm(forms.Form):
    YES_OR_NO = (
        (0, _('no')),
        (1, _('yes')),
    )

    title = forms.CharField(
        label=_('title'),
        widget=forms.TextInput(attrs={'class':'form-control'}),

    )

    tag = forms.CharField(
        label=_('tag'),
        widget=forms.TextInput(attrs={'class':'form-control'}),

    )

    # toptag = forms.CharField(
    #     label=_('Top_Tag'),
    #     widget=forms.TextInput(attrs={'class':'form-control'}),
    #     help_text=_('')
    # )

    slug = forms.CharField(
        label=_('slug'),
        widget=forms.TextInput(attrs={'class':'form-control'}),

    )
    # change widget to textInput by An , explain later .
    status = forms.ChoiceField(
        label=_('is_on_top'),
        choices=YES_OR_NO,
        widget=forms.TextInput( attrs={'class':'form-control'}, ),
        required=False,

        initial=0,
    )

    is_published = forms.ChoiceField(
        label=_('is_published'),
        choices=YES_OR_NO,
        widget=forms.Select( attrs={'class':'form-control'}),
        required=False,
        help_text=_('is the event published ?'),
    )

    is_top = forms.ChoiceField(
        label=_('is_event_on_top'),
        choices=YES_OR_NO,
        widget=forms.Select(attrs={'class':'form-control'}),
        required=False,
        help_text=_('is the event on top ? ')
    )

    def clean_tag(self):
        _tag = self.cleaned_data.get('tag')

        try:
            Tags.objects.get(name=_tag)
        except Tags.DoesNotExist:
            raise forms.ValidationError(
                _('tag is not exist'),
            )
        return _tag

    def clean_toptag(self):
        _tag = self.cleaned_data.get('toptag')

        try:
            Tags.objects.get(name=_tag)
        except Tags.DoesNotExist:
            raise forms.ValidationError(
                _('tag is not exist'),
            )
        return _tag

    def clean_status(self):
        _status = self.cleaned_data.get('status')
        return int(_status)

    def clean_is_published(self):
        _is_published = self.cleaned_data.get('is_published')
        return int(_is_published)

    def clean_is_top(self):
        _is_top = self.cleaned_data.get('is_top')
        return int(_is_top)

class CreateEventForm(BaseEventForm):

    def save(self):
        _title = self.cleaned_data.get('title')
        _tag = self.cleaned_data.get('tag')
        _toptag = self.cleaned_data.get('toptag', '')
        _slug = self.cleaned_data.get('slug')
        _status = self.cleaned_data.get('status')
        _is_top = self.cleaned_data.get('is_top')
        _is_published = self.cleaned_data.get('is_published')
        # replace _status with _is_top
        _status = _is_top

        log.info(_status)

        if _status:
            Event.objects.all().update(status = False)
            Event_Status.objects.all().update(is_top=False)
        # only one Event_status can have is_top=True ATTR, SO DOES Event

        event = Event.objects.create(
            title = _title,
            tag = _tag,
            toptag=_toptag,
            slug = _slug,
            status = _status,
        )
        event_status = Event_Status(event=event, is_published=_is_published, is_top=_is_top)
        event_status.save()

        return event

class EditEventForm(BaseEventForm):

    def __init__(self, event, *args, **kwargs):
        self.event = event
        super(EditEventForm, self).__init__(*args, **kwargs)

    def save(self):
        _title = self.cleaned_data.get('title')
        _tag = self.cleaned_data.get('tag')
        # _toptag = self.cleaned_data.get('toptag')
        _slug = self.cleaned_data.get('slug')
        _status = self.cleaned_data.get('status', False)
        # _status = int(_status)

        _is_published = self.cleaned_data.get('is_published',False)
        _is_published = int(_is_published)

        _is_top = self.cleaned_data.get('is_top',False)
        _is_top = int(_is_top)

        _status = _is_top

        if _status:
            Event.objects.all().update(status = False)
            Event_Status.objects.all().update(is_top=False)

        self.event.title = _title
        self.event.tag = _tag
        # self.event.toptag = _toptag
        self.event.slug = _slug
        self.event.status = _status
        self.event.event_status.is_published = _is_published
        self.event.event_status.is_top = _is_top
        self.event.event_status.save()
        self.event.save()

__author__ = 'edison'


#beforeunload dont work, change to keep alive instead
from flask import Flask, render_template, send_from_directory
from PIL import Image
import glob, os, os.path
from threading import Thread
from time import sleep

app = Flask(__name__)
URL = 'http://127.0.0.1:5000/'
#BROWSER can be set to 'edge', 'chrome', 'firefox'
BROWSER = 'edge'
USERNAME = os.getlogin()
SAVEPATH = "C:/Users/"+USERNAME+"/OneDrive/background/"
WAITETIME = 1000
if not os.path.exists(SAVEPATH):
    os.makedirs(SAVEPATH)

global USERCOUNT
USERCOUNT = 0

def threaded_reset(arg):
    sleep(1)
    global USERCOUNT
    USERCOUNT = 0

@app.route("/")
def main():
    global USERCOUNT
    if(USERCOUNT == 0):
        USERCOUNT+=1
        if not os.path.exists('static'):
            os.mkdir('static')
        image_list = []
        for filename in glob.glob('C:/Users/'+USERNAME+'/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets/*'):
            im=Image.open(filename)
            if(im.width == 1920):
                name = im.filename.split("Assets\\")[1]+".jpg"
                im=im.save("./static/"+name)
                #im.show()
                image_list.append("/static/"+name)
        t1 = Thread(target=threaded_reset, args=(10,))
        t1.start()
        return render_template('resave.html', image_list = image_list, DoubleOpen = False)

    return render_template('resave.html', image_list = [], DoubleOpen = True)

@app.route("/save/<name>")
def save(name):
    im=Image.open("./static/"+name)
    path = SAVEPATH+name
    im=im.save(path)
    return render_template('saved.html', name = name, savepath = SAVEPATH, waittime = WAITETIME)

@app.route("/static/<name>")
def static_file(name):
    return send_from_directory('static', name)

@app.route('/shutdown', methods=['GET'])
def shutdown():
    with os.scandir('static/') as dir:
        for file in dir:
            os.remove(file.path)
    os._exit(0)

#webbrowser.open has a bug that opens the link twice
#webbrowser.open('http://127.0.0.1:5000/')
if(BROWSER != 'edge'):
    BROWSER+=" "
else:
    BROWSER="microsoft-edge:"
os.system('powershell.exe start '+BROWSER+URL)

if __name__ == "__main__":
    app.run(debug=True)


"""
Utilities for working with Rebound archives.

Michael S. Emanuel
Fri Aug 23 16:13:28 2019
"""

# Core
import numpy as np

# Astronomy
import rebound

# Utilities
from datetime import datetime, timedelta
from collections import namedtuple
from itertools import chain
from pathlib import Path
import os
from tqdm.auto import tqdm as tqdm_auto

# MSE imports
from utils import hash_id_crc32
from astro_utils import mjd_to_datetime, OrbitalElement

# Typing
from typing import List, Tuple, Dict, Set, Optional

# *********************************************************************************************************************
# Directory for archives; make if missing
dir_archive: str = '../data/rebound/archive'
Path(dir_archive).mkdir(parents=True, exist_ok=True)

# *********************************************************************************************************************
def calc_fname_archive(sim: rebound.simulation, mjd0: int, mjd1: int) -> str:
    """Generate filename for the simulation archive from a simulation"""
    # Tokens for frequency and hash of bodies
    freq_str = f'sf{sim.steps_per_day}'
    body_hash_str: str = f'bh{hash_id_crc32(tuple(sim.body_names))}'
    fname: str =  f'{sim.body_collection}_{mjd0}_{mjd1}_{freq_str}_{body_hash_str}.bin'
    return fname

# *********************************************************************************************************************
def make_archive_impl(sim_epoch: rebound.Simulation,
                      mjd0: int,
                      mjd1: int,
                      time_step: float,
                      save_step: int,
                      save_elements: bool,
                      progbar: bool) -> None:
    """
    Create a rebound simulation archive and save it to disk.
    INPUTS:
        fname_archive:  the file name to save the archive to
        sim_epoch:      rebound simulation object as of the epoch time; to be integrated in both directions
        mjd0:           the earliest MJD to simulate back to
        mjd1:           the latest MJD to simulate forward to
        time_step:      the time step in days for the simulation
        save_step:      the interval for saving snapshots to the simulation archive
        save_elements:  flag indiciting whether to save orbital elements
        progbar:        flag - whether to display a progress bar
    OUTPUTS:
        None.  saves the assembled simulation archive to disk.
    """
    # Get archive filename from simulation
    fname_archive: str = calc_fname_archive(sim_epoch, mjd0, mjd1)
    # Path of archive file
    path_archive: Path = Path(dir_archive, fname_archive)
    pathstr_archive: str = path_archive.as_posix()

    # Look up the epoch from the base simulation
    epoch: int = sim_epoch.epoch

    # Look up body IDs and names
    body_ids: np.ndarray = sim_epoch.body_ids
    body_names: np.ndarray = sim_epoch.body_names

    # Convert epoch, start and end times relative to a base date of the simulation start
    # This way, time is indexed from t0=0 to t1 = (dt1-dt0)
    t0: float = 0.0
    t1: float = mjd1 - mjd0
    t_epoch: float = float(epoch) - mjd0

    # Create copies of the simulation to integrate forward and backward
    sim_fwd: rebound.Simulation = sim_epoch.copy()
    sim_back: rebound.Simulation = sim_epoch.copy()

    # Set the time counter on both simulation copies to the epoch time
    sim_fwd.t = t_epoch
    sim_back.t = t_epoch
    # Flip sign of dt on sim_back
    sim_back.dt *= -1.0

    # Set the times for snapshots in both directions;
    ts: np.array = np.arange(t0, t1+time_step, time_step)
    idx: int = np.searchsorted(ts, t_epoch, side='left')
    ts_fwd: np.array = ts[idx:]
    ts_back: np.array = ts[:idx][::-1]

    # The epochs corresponding to the times in ts; as MJD and as datetime
    epochs: np.array = ts + (epoch - t_epoch)
    dt0: datetime = mjd_to_datetime(mjd0)
    epochs_dt: np.array = np.array([dt0 + timedelta(t) for t in ts])

    # File names for forward and backward integrations
    path_fwd: Path = Path(dir_archive, fname_archive.replace('.bin', '_fwd.bin'))
    path_back: Path = Path(dir_archive, fname_archive.replace('.bin', '_back.bin'))
    pathstr_fwd: str = path_fwd.as_posix()
    pathstr_back: str = path_back.as_posix()

    # Number of snapshots
    M_back: int = len(ts_back)
    M_fwd: int = len(ts_fwd)
    M: int = M_back + M_fwd
    # Number of particles
    N: int = sim_epoch.N

    # Initialize arrays for the position and velocity
    shape_qv: Tuple[int] = (M, N, 3)
    q: np.array = np.zeros(shape_qv, dtype=np.float64)
    v: np.array = np.zeros(shape_qv, dtype=np.float64)

    # Initialize arrays for orbital elements if applicable
    if save_elements:
        # Arrays for a, e, inc, Omega, omega, f
        shape_elt: Tuple[int] = (M, N)
        orb_a: np.array = np.zeros(shape_elt, dtype=np.float64)
        orb_e: np.array = np.zeros(shape_elt, dtype=np.float64)
        orb_inc: np.array = np.zeros(shape_elt, dtype=np.float64)
        orb_Omega: np.array = np.zeros(shape_elt, dtype=np.float64)
        orb_omega: np.array = np.zeros(shape_elt, dtype=np.float64)
        orb_f: np.array = np.zeros(shape_elt, dtype=np.float64)

        # Wrap these into a named tuple
        elts: OrbitalElement = \
            OrbitalElement(a=orb_a, e=orb_e, inc=orb_inc,
                           Omega=orb_Omega, omega=orb_omega, f=orb_f)
    else:
        elts = OrbitalElement(a=0.0, e=0.0, inc=0.0, Omega=0.0, omega=0.0, f=0.0)

    # Subfunction: process one row of the loop
    def process_row(sim: rebound.Simulation, fname: str, t: float, row: int):
        # Integrate to the current time step with an exact finish time
        sim.integrate(t, exact_finish_time=1)

        # Serialize the position and velocity
        sim.serialize_particle_data(xyz=q[row])
        sim.serialize_particle_data(vxvyvz=v[row])

        # Save a snapshot on multiples of save_step or the first / last row
        if (row % save_step == 0) or (row in (0, M-1)):
            sim.simulationarchive_snapshot(filename=fname)

        # Save the orbital elements if applicable
        if save_elements:
            # Compute the orbital elements vs. the sun as primary
            primary = sim.particles['Sun']
            orbits = sim.calculate_orbits(primary=primary, jacobi_masses=False)
            # Save the elements on this date as an Nx6 array
            # This approach only traverses the (slow) Python list orbits one time
            elt_array = np.array([[orb.a, orb.e, orb.inc, orb.Omega, orb.omega, orb.f] \
                                  for orb in orbits])
            # Save the elements into the current row of the named orbital elements arrays
            # The LHS row mask starts at 1 b/c orbital elements are not computed for the first object (Sun)
            orb_a[row, 1:] = elt_array[:, 0]
            orb_e[row, 1:] = elt_array[:, 1]
            orb_inc[row, 1:] = elt_array[:, 2]
            orb_Omega[row, 1:] = elt_array[:, 3]
            orb_omega[row, 1:] = elt_array[:, 4]
            orb_f[row, 1:] = elt_array[:, 5]

    # Integrate the simulation forward in time
    idx_fwd: List[Tuple[int, float]] = list(enumerate(ts_fwd))
    if progbar:
        idx_fwd = tqdm_auto(idx_fwd)
    for i, t in idx_fwd:
        # Row index for position data
        row: int = M_back + i
        # Process this row
        process_row(sim=sim_fwd, fname=pathstr_fwd, t=t, row=row)

    # Integrate the simulation backward in time
    idx_back: List[Tuple[int, float]] = list(enumerate(ts_back))
    if progbar:
        idx_back = tqdm_auto(idx_back)
    for i, t in idx_back:
        # Row index for position data
        row: int = M_back - (i+1)
        # Process this row
        process_row(sim=sim_back, fname=pathstr_back, t=t, row=row)

    # Load the archives with the forward and backward snapshots
    sa_fwd: rebound.SimulationArchive = rebound.SimulationArchive(pathstr_fwd) if idx_fwd else []
    sa_back: rebound.SimulationArchive = rebound.SimulationArchive(pathstr_back) if idx_back else []

    # Filename for numpy arrays of position and velocity
    path_np = Path(dir_archive, fname_archive.replace('.bin', '.npz'))
    pathstr_np = path_np.as_posix()

    # Save the object name hashes
    hashes: np.array = np.zeros(N, dtype=np.uint32)
    sim_epoch.serialize_particle_data(hash=hashes)

    # Combine the forward and backward archives in forward order from t0 to t1
    sims = chain(reversed(sa_back), sa_fwd)
    # Process each simulation snapshot in turn
    for i, sim in enumerate(sims):
        # Save a snapshot on multiples of save_step
        sim.simulationarchive_snapshot(pathstr_archive)

    # Save the numpy arrays with the object hashes, position and velocity
    np.savez(pathstr_np,
             q=q, v=v, elts=elts,
             ts=ts, epochs=epochs, epochs_dt=epochs_dt,
             hashes=hashes, body_ids=body_ids, body_names=body_names)

    # Delete the forward and backward archives
    path_fwd.unlink(missing_ok=True)
    path_back.unlink(missing_ok=True)

# *********************************************************************************************************************
def make_archive(sim_epoch: rebound.Simulation,
                 mjd0: int, mjd1: int,
                 time_step: int, save_step: int,
                 save_elements: bool,
                 progbar: bool) -> rebound.SimulationArchive:
    """
    Load a rebound archive if available; otherwise generate it and save it to disk.
    INPUTS:
        fname_archive:  the file name to save the archive to
        sim_epoch:      rebound simulation object as of the epoch time; to be integrated in both directions
        mjd0:           the earliest MJD to simulate back to
        mjd1:           the latest MJD to simulate forward to
        time_step:      the time step in days for the simulation
        save_step:      the interval for saving snapshots to the simulation archive
        save_elements:  flag indiciting whether to save orbital elements
        progbar:        flag - whether to display a progress bar
    OUTPUTS:
        SimulationArchive: Rebound SimulationArchive object loaded from disk
    """
    # Get archive filename from simulation
    fname_archive: str = calc_fname_archive(sim_epoch, mjd0, mjd1)
    # Path of archive
    path_archive = os.path.join(dir_archive, fname_archive)
    try:
        # First try to load the named archive
        sa = rebound.SimulationArchive(filename=path_archive)
        print(f'Loaded archive {fname_archive}.')
    except:
        # If the archive is not on disk, save it to disk
        print(f'Generating archive {fname_archive}\n'
              f'from mjd {mjd0} to {mjd1}, time_step={time_step}, save_step={save_step}...')
        make_archive_impl(sim_epoch=sim_epoch, mjd0=mjd0, mjd1=mjd1, time_step=time_step,
                          save_step=save_step, save_elements=save_elements, progbar=progbar)
        # Load the new archive into memory
        sa = rebound.SimulationArchive(filename=path_archive)

    # Bind the archive filename to the archive
    sa.fname_archive: str = fname_archive
    sa.fname_np: str = fname_archive.replace('.bin', '.npz')
    sa.body_collection = sim_epoch.body_collection
    # Bind various dates and time steps
    sa.epoch = sim_epoch.epoch
    sa.mjd0: int = mjd0
    sa.mjd1: int = mjd1
    sa.time_step: int = time_step
    # Bind the simulation at the epoch
    sa.sim_epoch = sim_epoch

    return sa


from agent import *

def random_number_minimum(mean, var, min):
    return max(min, np.random.normal(mean, var))

def random_idm_p(desired_v):
    acc_max = np.random.uniform(1.8, 2.2)  # recommended: 1.4
    acc_min = np.random.uniform(-2., -4.)
    min_dis = random_number_minimum(4, 1, 2)
    acc_com = np.random.normal(-2.0, 0.1)
    thw = random_number_minimum(1.5, 0.1, 1.0)  # recommended: 1.5
    vd = desired_v + np.random.normal(0., 0.5)
    return IdmParameter(acc_max, acc_min, min_dis, acc_com, thw, vd)

def random_idm_p_truck(desired_v):
    acc_max = np.random.uniform(0.8, 1.1)  # recommended: 1.4
    acc_min = np.random.uniform(-1.0, -2.0)
    min_dis = random_number_minimum(4, 1, 2)
    acc_com = np.random.normal(-2.0, 0.1)
    thw = random_number_minimum(1.2, 0.1, 1.0)  # recommended: 1.5
    vd = desired_v + np.random.normal(0., 1.)
    return IdmParameter(acc_max, acc_min, min_dis, acc_com, thw, vd)

def random_idm_p_merging(desired_v):
    acc_max = np.random.uniform(2.0, 2.5)  # merging vehicle should move more dynamic as surroundings
    acc_min = np.random.uniform(-3., -4.)
    min_dis = random_number_minimum(4, 1, 2)
    acc_com = np.random.normal(-2.0, 0.1)
    thw = random_number_minimum(1.5, 0.1, 1.0)
    vd = desired_v + np.random.normal(0., 0.5)
    return IdmParameter(acc_max, acc_min, min_dis, acc_com, thw, vd)

def random_yielding_p():
    original_parameter = [1.70968573, 0.00582201, 0.40742229, 1.02390871,
                          -0.32091885, 1.48127062, -0.69499426, -0.34547462,
                          -0.56578542, -1.911662, -0.03998742, 0.02613949, -0.02392805]
    randomized_parameter = []
    for p in original_parameter:
        randomized_parameter.append(np.random.normal(p, abs(p) * 0.2))
    return randomized_parameter

def random_mobil_p():
    politeness_f = np.random.uniform(0, 1)
    delta_a = np.random.normal(0.5, 0.2)
    bias_a = delta_a + np.random.normal(0.1, 0.05)
    return MOBILParameter(politeness_f, delta_a, bias_a)

class RandomDensityAndVelocityParameter:
    def __init__(self, thw_mean, thw_var, mean_v_diff_to_limit, v_var, s_range=None, num_vehicles=None):
        self.thw_mean = thw_mean  # mean time headway between generated agents in one lane
        self.thw_var = thw_var  # variance of time headway between generated agents in one lane
        # mean velocity difference of initial velocity to speed limit of generated agents
        self.mean_v_diff_to_limit = mean_v_diff_to_limit
        # variance of initial velocity
        self.v_var = v_var
        # optional: longitudinal range of spawned agents of one lane.
        # If a lane has 1000m length, [0, 500] range will only generate agents in the first 500m
        self.s_range = s_range
        # optional: maximum number of agents in one lane
        self.num_vehicles = num_vehicles

class RandomTrafficGenerator:

    def __init__(self):
        self.vehicle_id = 0
        self.shapes = [[4.5, 1.9], [4.8, 2], [4.9, 2.1], [5.2, 2.2], [5.9, 2.4]]  # typical shapes from data
        self.truck_shape = [[11.5, 2.5], [12, 2.5], [10, 2.5]]

    def random_agents_on_map(self, m, parameter, unsafe_lc_ratio=0., truck_ratio=0.):
        agents_merging, agents_main = [], []
        v_limit_candidates = []
        truck_lane_id = m.truck_lane_id()
        for _, c in m.corridors.items():
            if c.type == "main":
                v_limit_candidates.append(c.v_limit)
        for i, c in m.corridors.items():
            if c.type == "main":
                if i == truck_lane_id:
                    agents_main.extend(self.random_agents_on_lane(c, parameter[i], v_limit_candidates, "idm",
                                                                  unsafe_lc_ratio=unsafe_lc_ratio,
                                                                  truck_ratio=truck_ratio))
                else:
                    agents_main.extend(self.random_agents_on_lane(c, parameter[i], v_limit_candidates, "idm",
                                                                  unsafe_lc_ratio=unsafe_lc_ratio))
            if c.type == "merging":
                agents_merging.extend(self.random_agents_on_lane(c, parameter[i], v_limit_candidates,
                                                                 "merging_closest_gap_policy"))
        return agents_merging, agents_main

    def random_agents_on_lane(self, corridor, params, v_limit_candidates, behavior_model,
                              unsafe_lc_ratio=0., truck_ratio=0.):
        vehicles = []
        s_range = [0, corridor.centerSimplified.path_length]
        num_vehicles = 1e10
        if params.s_range:
            s_range = [max(s_range[0], params.s_range[0]), min(s_range[1], params.s_range[1])]
        if params.num_vehicles:
            num_vehicles = params.num_vehicles
        x_frenet = np.random.uniform(s_range[0], 0.3 * corridor.v_limit * params.thw_mean)
        while x_frenet < s_range[1] and len(vehicles) < num_vehicles:
            y_frenet = np.random.normal(0, 0.2)
            v = np.random.normal(corridor.v_limit + params.mean_v_diff_to_limit, params.v_var)
            x, y, yaw = corridor.centerSimplified.to_global_coordinate(x_frenet, y_frenet)
            random_num = np.random.uniform(0, 1)
            if random_num < truck_ratio:
                l, w = random.choice(self.truck_shape)
                idm_p = random_idm_p_truck(min(v_limit_candidates))
            else:
                l, w = random.choice(self.shapes)
                idm_p = random_idm_p(random.choice(v_limit_candidates))
            change_intention_threshold = np.random.uniform(0.4, 0.7)  # Willingness of changing yielding intention
            b_model = behavior_model
            # 10% idm
            if behavior_model == "idm":
                random_num = np.random.uniform(0, 1)
                if 1 - unsafe_lc_ratio >= random_num > 0.1:
                    b_model = "idm_mobil_lane_change_safe"
                elif random_num > 1 - unsafe_lc_ratio:
                    b_model = "idm_mobil_lane_change"
            vehicle = Agent(self.vehicle_id, x, y, v, yaw, 0, l, w, idm_p, random_yielding_p(), random_mobil_p(),
                            b_model, change_intention_threshold)
            if "merging" in behavior_model:
                vehicle.idm_param = random_idm_p_merging(random.choice(v_limit_candidates))
            vehicles.append(vehicle)
            x_frenet += np.random.uniform(corridor.v_limit * (params.thw_mean - params.thw_var),
                                          corridor.v_limit * (params.thw_mean + params.thw_var)) + l
            self.vehicle_id += 1
        return vehicles


# https://swexpertacademy.com/main/learn/course/lectureProblemViewer.do
import sys
sys.stdin = open('input.txt')

T = int(input())
# 통과하려고 만든 같은 로직...
for tc in range(T):
    n = int(input())
    num_li = sorted(list(map(int, input().split())))
    mode = 0  # 큰 수 자리인지 작은 수 자리인지 판별하는 용도
    count = 0  # 몇 개 출력했는지 체크하는 용도
    l, r = 0, -1
    ans = []
    print('#{}'.format(tc + 1), end='')
    while count < 10:  # 10번 출력하도록
        now = 0
        if mode == 0:  # 큰 수 출력차례
            now = num_li[r]
            r -= 1
            mode = 1
        elif mode == 1:
            now = num_li[l]
            l += 1
            mode = 0
        print(' {}'.format(now), end='')
        count += 1  # 원소 출력 후 카운트 +1
    if tc < T - 1:  # 마지막 케이스 아닌 경우 줄바꿈
        print()


from sys import stdin
def input(): return stdin.readline().rstrip()

n = int(input())
while 1:
    s = str(n)
    t = 0
    for c in s:
        t += ord(c) - ord("0")
    if n%t == 0:
        print(n)
        break
    n += 1


from csp import *

class myCSP(CSP):

    def __init__(self,name):
        self.constrains = {}
        self.deadEndCounter = None #dict of dicts to count how many times a tuple reached a conflict (for dom/wdeg)
        self.visitedNodes = 0
        self.constraintChecks = 0
        self.conflictsCount = 0
        self.conflicts = {} #dict of sets to save information durring backjumping
        data = self.readFile(name)
        super().__init__(data[0],data[1],data[2],self.f)

    def readFile(self,fileName):
    #dom file
        domFile = open("./rlfap/dom" + fileName, "r")
        int(domFile.readline())
        tempDomains = {}
        for line in domFile:
            domain = int(line.split()[0])
            count = int(line.split()[1])
            values = []
            for i in range(0,count):
                values.append(int(line.split()[i+2]))
            tempDomains[domain] = values

        neighbors = {}
        self.deadEndCounter = {}

    #var file
        varFile = open("./rlfap/var" + fileName, "r")
        int(varFile.readline())
        domains = {}
        variables = []
        for line in varFile:
            variable = int(line.split()[0])
            domain = int(line.split()[1])
            variables.append(variable)
            neighbors[variable] = [] #Initiliazing empty list for neighbors of current var
            self.conflicts[variable] = set()
            domains[variable]=tempDomains[domain]
            self.deadEndCounter[variable] = {} #Initiliazing dict to count how many times a tuple reached a conflict (for dom/wdeg)

    #ctr file
        self.constrains = {}
        ctrFile = open("./rlfap/ctr" + fileName, "r")
        int(ctrFile.readline())
        for line in ctrFile:
            var1 = int(line.split()[0])
            var2 = int(line.split()[1])
            constrain = line.split()[2]
            constrainVal = int(line.split()[3])
            neighbors[var1].append(var2)
            neighbors[var2].append(var1)
            self.deadEndCounter[var1][var2] = 1 #Initiliazing 1 as weight for heuristic
            self.deadEndCounter[var2][var1] = 1 #Initiliazing 1 as weight for heuristic
            self.constrains[(var1,var2)]=(constrain,constrainVal)

        return (variables,domains,neighbors)

    def f(self,A, a, B, b): #the constraints requested function which will be given to CSP
        if (A,B) in self.constrains.keys():
            constrain = self.constrains[(A,B)]
        elif (B,A) in self.constrains.keys():
            constrain = self.constrains[(B,A)]

        if constrain[0] == '>':
            return abs(a-b) > constrain[1]
        elif constrain[0] == '<':
            return abs(a-b) < constrain[1]
        elif constrain[0] == '=':
            return abs(a-b) == constrain[1]

def findSum(csp, minVar):
    Sum = 1
    for var in csp.deadEndCounter[minVar]:
        Sum += csp.deadEndCounter[minVar][var]

    return Sum

def heuristic(assignment, csp): #Impliments the dom/wdeg as in the paper
    Min = None
    for var in csp.variables:
        if var not in assignment:
            legal = num_legal_values(csp, var, assignment)
            tSum = findSum(csp, var)
            if Min != None:
                if Min/minSum > legal/tSum:
                    Min = legal
                    minVar = var
                    minSum = tSum
            else:
                Min = legal
                minVar = var
                minSum = tSum

    return minVar

def my_forward_checking(csp, var, value, assignment, removals):
    """Prune neighbor values inconsistent with var=value."""

    csp.support_pruning()
    for B in csp.neighbors[var]:
        if B not in assignment:
            for b in csp.curr_domains[B][:]:
                csp.constraintChecks += 1 #Counting number of constrains
                if not csp.constraints(var, value, B, b):
                    csp.prune(B, b, removals)
            if not csp.curr_domains[B]:
                #Counting for dom/wdeg
                csp.deadEndCounter[var][B] += 1
                csp.deadEndCounter[B][var] += 1
                return False
    return True

#AC3 but simply calling my_revise to count constraints and collect data for dom/wdeg
def my_AC3(csp, queue=None, removals=None, arc_heuristic=dom_j_up):
    """[Figure 6.3]"""

    if queue is None:
        queue = {(Xi, Xk) for Xi in csp.variables for Xk in csp.neighbors[Xi]}
    csp.support_pruning()
    queue = arc_heuristic(csp, queue)
    checks = 0
    while queue:
        (Xi, Xj) = queue.pop()
        revised, checks = my_revise(csp, Xi, Xj, removals, checks)
        if revised:
            if not csp.curr_domains[Xi]:
                return False, checks  # CSP is inconsistent
            for Xk in csp.neighbors[Xi]:
                if Xk != Xj:
                    queue.add((Xk, Xi))
    return True, checks  # CSP is satisfiable

#Using AC3
def my_mac(csp, var, value, assignment, removals, constraint_propagation=my_AC3):
    """Maintain arc consistency."""
    return constraint_propagation(csp, {(X, var) for X in csp.neighbors[var]}, removals)

def my_revise(csp, Xi, Xj, removals, checks=0):
    """Return true if we remove a value."""
    revised = False
    for x in csp.curr_domains[Xi][:]:
        # If Xi=x conflicts with Xj=y for every possible y, eliminate Xi=x
        # if all(not csp.constraints(Xi, x, Xj, y) for y in csp.curr_domains[Xj]):
        conflict = True
        for y in csp.curr_domains[Xj]:
            csp.constraintChecks += 1 #Counting number of constrains
            if csp.constraints(Xi, x, Xj, y):
                conflict = False
            checks += 1
            if not conflict:
                break
        if conflict:
            #Counting for dom/wdeg
            csp.deadEndCounter[Xj][Xi] += 1
            csp.deadEndCounter[Xi][Xj] += 1
            csp.prune(Xi, x, removals)
            revised = True
    return revised, checks

def my_backtracking_search(csp, select_unassigned_variable=first_unassigned_variable,
                        order_domain_values=unordered_domain_values, inference=no_inference):
    """[Figure 6.5]"""

    csp.visitedNodes = 0
    csp.constraintChecks = 0

    def backtrack(assignment):
        if len(assignment) == len(csp.variables):
            return assignment
        var = select_unassigned_variable(assignment, csp)
        for value in order_domain_values(var, assignment, csp):
            if 0 == csp.nconflicts(var, value, assignment):
                csp.assign(var, value, assignment)
                csp.visitedNodes += 1 #Counting number of visited nodes
                removals = csp.suppose(var, value)
                if inference(csp, var, value, assignment, removals):
                    result = backtrack(assignment)
                    if result is not None:
                        return result
                csp.restore(removals)
        csp.unassign(var, assignment)
        return None

    result = backtrack({})
    assert result is None or csp.goal_test(result)
    return result

def my_forward_checkingCBJ(csp, var, value, assignment, removals):
    """Prune neighbor values inconsistent with var=value."""

    csp.support_pruning()
    for B in csp.neighbors[var]:
        conflict = False
        if B not in assignment:
            for b in csp.curr_domains[B][:]:
                csp.constraintChecks += 1
                if not csp.constraints(var, value, B, b):
                    csp.prune(B, b, removals)
                    conflict = True
            if conflict: #Adding conflict in the set for backjumbing
                csp.conflicts[B].add(var)
            if not csp.curr_domains[B]:
                csp.deadEndCounter[var][B] += 1
                csp.deadEndCounter[B][var] += 1
                if conflict: #Adding the rest conflicts in the set for backjumbing
                    for conflic in csp.conflicts[var]:
                        csp.conflicts[B].add(conflic)
                return False
    return True

def backJumping_search(csp, select_unassigned_variable=first_unassigned_variable,
                        order_domain_values=unordered_domain_values, inference=no_inference):
    """[Figure 6.5]"""

    csp.visitedNodes = 0
    csp.constraintChecks = 0

    def backJump(assignment):
        if len(assignment) == len(csp.variables):
            return (assignment,None)
        var = select_unassigned_variable(assignment, csp)
        for value in order_domain_values(var, assignment, csp):
            if 0 == csp.nconflicts(var, value, assignment):
                csp.assign(var, value, assignment)
                csp.visitedNodes += 1
                removals = csp.suppose(var, value)
                if inference(csp, var, value, assignment, removals):
                    result = backJump(assignment)
                    if result[0] is not None:
                        return result
                    #If the backjumping stops at this point of the recursion
                    if result[1] != None and var not in csp.conflicts[result[1]]:
                        #The function returns None to indicate the end of the backjumping...
                        csp.restore(removals)
                        csp.unassign(var, assignment)
                        for key in csp.conflicts:#... after removing the current variable from the conflict sets
                            if var in csp.conflicts[key]:
                                csp.conflicts[key].remove(var)
                        return (None,result[1])

                csp.restore(removals)
        csp.unassign(var, assignment)
        return (None,var)

    result = backJump({})
    assert result[0] is None or csp.goal_test(result[0])
    return result[0]

def my_min_conflicts(csp, max_steps=100000):
    """Solve a CSP by stochastic Hill Climbing on the number of conflicts."""
    # Generate a complete assignment for all variables (probably with conflicts)
    csp.current = current = {}
    for var in csp.variables:
        val = min_conflicts_value(csp, var, current)
        csp.assign(var, val, current)
    # Now repeatedly choose a random conflicted variable and change it
    for i in range(max_steps):
        conflicted = csp.conflicted_vars(current)
        if not conflicted:
            return current
        var = random.choice(conflicted)
        val = min_conflicts_value(csp, var, current)
        csp.assign(var, val, current)

    csp.conflictsCount = len(csp.conflicted_vars(current)) #Simply counting the conflicts the algorithm left

    return None


class Node:
    def __init__(self,data):
        self.data = data
        self.left = None
        self.right = None

def findValue(root, key):

    if root is None or root.data == key:
        return root

    elif root.data<key:
        return findValue(root.right, key)

    return findValue(root.left, key)

if __name__ == "__main__":
    root  = Node(8)

    root.left = Node(4)
    root.right = Node(10)

    root.left.left = Node(3)
    root.left.right = Node(5)

    root.right.left = Node(9)
    root.right.right = Node(11)

    print(findValue(root, 5))

Questions & Problems/Trees/Binary Search Trees/1.FindValues.py


class TargetdException(Exception):
    # Common
    INVALID = -1
    NAME_CONFLICT = -50
    NO_SUPPORT = -153
    UNEXPECTED_EXIT_CODE = -303
    INVALID_ARGUMENT = -32602

    # Specific to block
    EXISTS_INITIATOR = -52
    NOT_FOUND_VOLUME = -103
    NOT_FOUND_VOLUME_EXPORT = -151
    NOT_FOUND_VOLUME_GROUP = -152
    NOT_FOUND_ACCESS_GROUP = -200
    VOLUME_MASKED = -303
    NO_FREE_HOST_LUN_ID = -1000

    # Specific to FS/NFS
    EXISTS_CLONE_NAME = -51
    EXISTS_FS_NAME = -53
    NOT_FOUND_FS = -104
    INVALID_POOL = -110
    NOT_FOUND_SS = -112
    NOT_FOUND_NFS_EXPORT = -400
    NFS_NO_SUPPORT = -401

    def __init__(self, code: int, message: str):
        super().__init__(message)
        self.code = code


import unittest

from Game.Effects.Conditions.Filters.intersection_filter import IntersectionFilter
from Test.dummy_filter import DummyFilter

class evaluate(unittest.TestCase):
    """ Test cases of evaluate """

    def  setUp(self):
        """ Build the Filters for the test """
        self.intersectionValues = [3, 4]
        self.filters = [DummyFilter(results=[1,2]+self.intersectionValues), DummyFilter(results=[5,6]+self.intersectionValues)]

    def intersectionReturned(self):
        """ Test that the intersection is returned """
        filter = IntersectionFilter(self.filters)
        results = filter.evaluate(None)
        self.assertEquals(results, self.intersectionValues, "The intersection values should be returned")

# Collect all test cases in this class
testcasesEvaluate = ["intersectionReturned"]
suiteEvaluate = unittest.TestSuite(map(evaluate, testcasesEvaluate))

##########################################################

# Collect all test cases in this file
suites = [suiteEvaluate]
suite = unittest.TestSuite(suites)

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    runner.run(suite)