text:
import unittest

class IntcodeComputer():

    OP_ADD = 1
    OP_MULTIPLY = 2
    OP_INPUT = 3
    OP_OUTPUT = 4
    OP_JUMP_TRUE = 5
    OP_JUMP_FALSE = 6
    OP_LESS_THAN = 7
    OP_EQUALS = 8
    OP_MOD_REL = 9
    OP_HALT = 99

    PARAM_MODE_POS = '0'
    PARAM_MODE_IMD = '1'
    PARAM_MODE_REL = '2'

    NOUN_ADDR = 1
    VERB_ADDR = 2
    RESULT_ADDR = 0
    START_ADDR = 0

    INIT_VAL = 0

    def __init__(self, data = []):
        self.inputs = []
        self.memory = []
        self.initial_memory = []
        if data:
            self.load_memory(data)

    def load_memory(self, data):
        self.initial_memory = self.normalize_memory(data)
        self.reset()

    def expand_memory(self, addr):
        needed_mem = addr - (len(self.memory) - 1)
        if needed_mem > 0:
            self.memory += ([self.INIT_VAL] * needed_mem)
        else:
            raise Exception(f'Cannot expand memory for addr {addr}')

    def check_addr(self, addr):
        if addr < 0:
            raise Exception(f'Addr {addr}, cannot be negative')

        if addr >= len(self.memory):
            self.expand_memory(addr)

        return addr

    def reset(self):
        if self.memory:
            del self.memory[:]
        self.memory = self.initial_memory.copy()

        if self.inputs:
            del self.inputs[:]
        self.inputs = []

        self.output = None
        self.last_input = None

        self.instruction_ptr = self.START_ADDR
        self.relative_base = self.START_ADDR

    def add_input(self, data):
        self.inputs.append(data)

    def print_program(self):
        print("Program: {:02d}{:02d}".format(self.memory[self.NOUN_ADDR],self.memory[self.VERB_ADDR]))

    def normalize_memory(self, intcode):
        if type(intcode) is str:
            return list(map(int, intcode.split(',')))

        elif type(intcode) is list:
            if type(intcode[0]) is str:
                return list(map(int, intcode))
            else:
                return intcode

        else:
            raise Exception('Corrupt intcode')

    def get_paramater(self, mode):
        param = self.memory[self.instruction_ptr]
        self.instruction_ptr += 1

        if mode == self.PARAM_MODE_POS:
            addr = self.check_addr(param)
            val = self.memory[addr]
        elif mode == self.PARAM_MODE_REL:
            addr = self.relative_base + param
            addr = self.check_addr(addr)
            val = self.memory[addr]
        elif mode == self.PARAM_MODE_IMD:
            val = param
        else:
            raise Exception(f"Unkown paramater mode: {param}")

        return val

    def set_paramater(self, mode, data):
        param = self.memory[self.instruction_ptr]
        self.instruction_ptr += 1

        if mode == self.PARAM_MODE_POS:
            addr = self.check_addr(param)
            self.memory[addr] = data
        elif mode == self.PARAM_MODE_REL:
            addr = self.relative_base + param
            addr = self.check_addr(addr)
            self.memory[addr] = data
        elif mode == self.PARAM_MODE_IMD:
            raise Exception("Set paramater can't be in immediate mode")
        else:
            raise Exception(f"Unkown paramater mode: {param}")

    def parse_opcode(self):
        mode_opcode_str = '{:>05}'.format(str(self.memory[self.instruction_ptr]))

        # Reverse of the first three chars
        modes = mode_opcode_str[:3][::-1]

        # integer of the last two chars
        opcode = int(mode_opcode_str[3:])

        self.instruction_ptr += 1

        return modes, opcode

    def run(self):
        self.output = None

        while self.instruction_ptr < len(self.memory):
            param_mode, opcode = self.parse_opcode()

            if opcode == self.OP_HALT:
                return 0

            elif opcode == self.OP_ADD:
                in1 = self.get_paramater(param_mode[0])
                in2 = self.get_paramater(param_mode[1])
                self.set_paramater(param_mode[2], in1 + in2)

            elif opcode == self.OP_MULTIPLY:
                in1 = self.get_paramater(param_mode[0])
                in2 = self.get_paramater(param_mode[1])
                self.set_paramater(param_mode[2], in1 * in2)

            elif opcode == self.OP_INPUT:
                if self.inputs:
                    self.last_input = self.inputs.pop()

                if self.last_input != None:
                    self.set_paramater(param_mode[0], self.last_input)
                else:
                    raise Exception(f"{self.last_input} is not a valid input")

            elif opcode == self.OP_OUTPUT:
                self.output = self.get_paramater(param_mode[0])
                return 1

            elif opcode == self.OP_JUMP_TRUE:
                do_jump = self.get_paramater(param_mode[0])
                new_addr = self.get_paramater(param_mode[1])
                if do_jump != 0:
                    self.instruction_ptr = new_addr

            elif opcode == self.OP_JUMP_FALSE:
                do_jump = self.get_paramater(param_mode[0])
                new_addr = self.get_paramater(param_mode[1])
                if do_jump == 0:
                    self.instruction_ptr = new_addr

            elif opcode == self.OP_LESS_THAN:
                in1 = self.get_paramater(param_mode[0])
                in2 = self.get_paramater(param_mode[1])
                if in1 < in2:
                    self.set_paramater(param_mode[2], 1)
                else:
                    self.set_paramater(param_mode[2], 0)

            elif opcode == self.OP_EQUALS:
                in1 = self.get_paramater(param_mode[0])
                in2 = self.get_paramater(param_mode[1])
                if in1 == in2:
                    self.set_paramater(param_mode[2], 1)
                else:
                    self.set_paramater(param_mode[2], 0)

            elif opcode == self.OP_MOD_REL:
                val = self.get_paramater(param_mode[0])
                self.relative_base += val

            else:
                raise Exception(f'Unknown opcode {opcode} at addr {self.instruction_ptr}.')
                self.reset()

        return -1


text:
from PySide import QtGui, QtCore
from AttributeWidgetImpl import AttributeWidget

class ScalarWidget(AttributeWidget):

    def __init__(self, attribute, parentWidget=None, addNotificationListener = True):
        super(ScalarWidget, self).__init__(attribute, parentWidget=parentWidget, addNotificationListener = addNotificationListener)

        hbox = QtGui.QHBoxLayout()

        self._widget = QtGui.QLineEdit(self)
        validator = QtGui.QDoubleValidator(self)
        validator.setDecimals(3)
        self._widget.setValidator(validator)
        hbox.addWidget(self._widget, 1)

        hbox.addStretch(0)
        hbox.setContentsMargins(0, 0, 0, 0)
        self.setLayout(hbox)
        self.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)

        self.updateWidgetValue()
        if self.isEditable():
            self._widget.editingFinished.connect(self._invokeSetter)
        else:
            self._widget.setReadOnly(True)

    def getWidgetValue(self):
        return float(self._widget.text())

    def setWidgetValue(self, value):
        self._widget.setText(str(round(value, 4)))

    @classmethod
    def canDisplay(cls, attribute):
        return(
                attribute.getDataType() == 'Scalar' or
                attribute.getDataType() == 'Float32' or
                attribute.getDataType() == 'Float64'
            )

ScalarWidget.registerPortWidget()

domain: python


text:
import tensorflow as tf
from tensorflow.keras.models import Model
import pandas as pd
import matplotlib.pyplot as plt

import os
import logging

from .common import create_directories

def get_prepared_model(stage: str, no_classes: int, input_shape: list, loss: str, optimizer: str, metrics: list) -> \
        Model:
    """Function creates ANN model and compile.
    Args:
        stage ([str]): stage of experiment
        no_classes ([INT]): No of classes for classification
        input_shape ([int, int]): Input shape for model's input layer
        loss ([str]): Loss function for model
        optimizer ([str]): Optimizer for model
        metrics ([str]): Metrics to watch while training
    Returns:
        model: ANN demo model
    """
    # Define layers
    LAYERS = []
    BASE_LAYERS = [
        tf.keras.layers.Flatten(input_shape=input_shape, name='input_layer'),
        tf.keras.layers.Dense(units=392, activation='relu', name='hidden1'),
        tf.keras.layers.Dense(units=196, activation='relu', name='hidden2'),
        tf.keras.layers.Dense(units=no_classes, activation='softmax', name='output_layer')
    ]

    KERNEL_INIT_LAYERS = [
        tf.keras.layers.Flatten(input_shape=input_shape, name='input_layer'),
        tf.keras.layers.Dense(units=392, activation='relu', name='hidden1', kernel_initializer='glorot_uniform',
                              bias_initializer='zeros'),
        tf.keras.layers.Dense(units=196, activation='relu', name='hidden2', kernel_initializer='glorot_uniform',
                              bias_initializer='zeros'),
        tf.keras.layers.Dense(units=no_classes, activation='softmax', name='output_layer')
    ]

    BN_BEFORE_LAYERS = [
        tf.keras.layers.Flatten(input_shape=input_shape, name='input_layer'),
        tf.keras.layers.Dense(units=392, name='hidden1', kernel_initializer='glorot_uniform'),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Activation('relu'),
        tf.keras.layers.Dense(units=196, name='hidden2', kernel_initializer='glorot_uniform'),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Activation('relu'),
        tf.keras.layers.Dense(units=no_classes, activation='softmax', name='output_layer')
    ]

    BN_AFTER_LAYERS = [
        tf.keras.layers.Flatten(input_shape=input_shape, name='input_layer'),
        tf.keras.layers.Dense(units=392, activation='relu', name='hidden1', kernel_initializer='glorot_uniform',
                              bias_initializer='zeros'),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Dense(units=196, activation='relu', name='hidden2', kernel_initializer='glorot_uniform',
                              bias_initializer='zeros'),
        tf.keras.layers.BatchNormalization(),
        tf.keras.layers.Dense(units=no_classes, activation='softmax', name='output_layer')
    ]

    logging.info("Creating Model..")
    if stage == 'BASE_MODEL':
        LAYERS = BASE_LAYERS
    elif stage == 'KERNEL_INIT_MODEL':
        LAYERS = KERNEL_INIT_LAYERS
    elif stage == 'BN_BEFORE_MODEL':
        LAYERS = BN_BEFORE_LAYERS
    elif stage == 'BN_AFTER_MODEL':
        LAYERS = BN_AFTER_LAYERS

    model_ann = tf.keras.models.Sequential(LAYERS)

    logging.info("Compiling Model..")
    model_ann.compile(loss=loss, optimizer=optimizer, metrics=metrics)

    return model_ann

def save_model(model_dir: str, model: Model, model_suffix: str) -> None:
    """
    args:
        model_dir: directory to save the model
        model: model object to save
        model_suffix: Suffix to save the model
    """
    create_directories([model_dir])
    model_file = os.path.join(model_dir, f"{model_suffix}.h5")
    model.save(model_file)
    logging.info(f"Saved model: {model_file}")

def save_history_plot(history, plot_dir: str, stage: str) -> None:
    """
    Args:
        history: History object for plotting loss/accuracy curves
        plot_dir: Directory to save plot files
        stage: Stage name for training
    """
    pd.DataFrame(history.history).plot(figsize=(10, 8))
    plt.grid(True)
    create_directories([plot_dir])
    plot_file = os.path.join(plot_dir, stage + "_loss_accuracy.png")
    plt.savefig(plot_file)
    logging.info(f"Loss accuracy plot saved: {plot_file}")

def get_callbacks(checkpoint_dir: str, tensorboard_logs: str, stage: str) -> list:
    """
    Args:
        checkpoint_dir: Directory to save the model at checkpoint
        tensorboard_logs: Directory to save tensorboard logs
        stage: Stage name for training
    Returns:
        callback_list: List of created callbacks
    """
    create_directories([checkpoint_dir, tensorboard_logs])
    tensorboard_cb = tf.keras.callbacks.TensorBoard(tensorboard_logs)
    early_stopping_cb = tf.keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)
    ckpt_file_path = os.path.join(checkpoint_dir, f"{stage}_ckpt_model.h5")
    checkpoint_cb = tf.keras.callbacks.ModelCheckpoint(filepath=ckpt_file_path, save_best_only=True)

    callback_list = [tensorboard_cb, early_stopping_cb, checkpoint_cb]
    logging.info(f"Callbacks created: {callback_list}")
    return callback_list


text:
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

base = pd.read_csv('orchard.csv')

figura = plt.figure()
eixo = figura.add_subplot(1, 1, 1, projection = '3d')
eixo.scatter(base.decrease, base.rowpos, base.colpos)
eixo.set_xlabel('decrease')
eixo.set_ylabel('rowpos')
eixo.set_zlabel('colpos')

# cores
# https://pythonspot.com/3d-scatterplot/


text:
# -*- coding=UTF-8 -*-
# pyright: strict

from __future__ import annotations
import os

import sys

import subprocess

def main():
    subprocess.call(
        ["npx", "pyright"],
        env={
            **os.environ,
            "PATH": os.path.pathsep.join(
                (
                    os.path.dirname(sys.executable),
                    os.getenv("PATH") or "",
                )
            ),
        },
        shell=True,
    )

if __name__ == "__main__":
    main()


text:
# coding=utf-8
# =============================================================================
# Copyright (c) 2001-2019 FLIR Systems, Inc. All Rights Reserved.
#
# This software is the confidential and proprietary information of FLIR
# Integrated Imaging Solutions, Inc. ("Confidential Information"). You
# shall not disclose such Confidential Information and shall use it only in
# accordance with the terms of the license agreement you entered into
# with FLIR Integrated Imaging Solutions, Inc. (FLIR).
#
# FLIR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
# SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE, OR NON-INFRINGEMENT. FLIR SHALL NOT BE LIABLE FOR ANY DAMAGES
# SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
# THIS SOFTWARE OR ITS DERIVATIVES.
# =============================================================================
#
#  Trigger.py shows how to trigger the camera. It relies on information
#  provided in the Enumeration, Acquisition, and NodeMapInfo examples.
#
#  It can also be helpful to familiarize yourself with the ImageFormatControl
#  and Exposure examples. As they are somewhat shorter and simpler, either
#  provides a strong introduction to camera customization.
#
#  This example shows the process of configuring, using, and cleaning up a
#  camera for use with both a software and a hardware trigger.

import os
import PySpin
import sys

NUM_IMAGES = 10  # number of images to grab

class TriggerType:
    SOFTWARE = 1
    HARDWARE = 2

CHOSEN_TRIGGER = TriggerType.SOFTWARE

def configure_trigger(cam):
    """
    This function configures the camera to use a trigger. First, trigger mode is
    set to off in order to select the trigger source. Once the trigger source
    has been selected, trigger mode is then enabled, which has the camera
    capture only a single image upon the execution of the chosen trigger.

     :param cam: Camera to configure trigger for.
     :type cam: CameraPtr
     :return: True if successful, False otherwise.
     :rtype: bool
    """
    result = True

    print('*** CONFIGURING TRIGGER ***\n')

    print('Note that if the application / user software triggers faster than frame time, the trigger may be dropped / skipped by the camera.\n')
    print('If several frames are needed per trigger, a more reliable alternative for such case, is to use the multi-frame mode.\n\n')

    if CHOSEN_TRIGGER == TriggerType.SOFTWARE:
        print('Software trigger chosen ...')
    elif CHOSEN_TRIGGER == TriggerType.HARDWARE:
        print('Hardware trigger chose ...')

    try:
        # Ensure trigger mode off
        # The trigger must be disabled in order to configure whether the source
        # is software or hardware.
        nodemap = cam.GetNodeMap()
        node_trigger_mode = PySpin.CEnumerationPtr(nodemap.GetNode('TriggerMode'))
        if not PySpin.IsAvailable(node_trigger_mode) or not PySpin.IsReadable(node_trigger_mode):
            print('Unable to disable trigger mode (node retrieval). Aborting...')
            return False

        node_trigger_mode_off = node_trigger_mode.GetEntryByName('Off')
        if not PySpin.IsAvailable(node_trigger_mode_off) or not PySpin.IsReadable(node_trigger_mode_off):
            print('Unable to disable trigger mode (enum entry retrieval). Aborting...')
            return False

        node_trigger_mode.SetIntValue(node_trigger_mode_off.GetValue())

        print('Trigger mode disabled...')

        # Set TriggerSelector to FrameStart
        # For this example, the trigger selector should be set to frame start.
        # This is the default for most cameras.
        node_trigger_selector= PySpin.CEnumerationPtr(nodemap.GetNode('TriggerSelector'))
        if not PySpin.IsAvailable(node_trigger_selector) or not PySpin.IsWritable(node_trigger_selector):
            print('Unable to get trigger selector (node retrieval). Aborting...')
            return False

        node_trigger_selector_framestart = node_trigger_selector.GetEntryByName('FrameStart')
        if not PySpin.IsAvailable(node_trigger_selector_framestart) or not PySpin.IsReadable(
                node_trigger_selector_framestart):
            print('Unable to set trigger selector (enum entry retrieval). Aborting...')
            return False
        node_trigger_selector.SetIntValue(node_trigger_selector_framestart.GetValue())

        print('Trigger selector set to frame start...')

        # Select trigger source
        # The trigger source must be set to hardware or software while trigger
        # mode is off.
        node_trigger_source = PySpin.CEnumerationPtr(nodemap.GetNode('TriggerSource'))
        if not PySpin.IsAvailable(node_trigger_source) or not PySpin.IsWritable(node_trigger_source):
            print('Unable to get trigger source (node retrieval). Aborting...')
            return False

        if CHOSEN_TRIGGER == TriggerType.SOFTWARE:
            node_trigger_source_software = node_trigger_source.GetEntryByName('Software')
            if not PySpin.IsAvailable(node_trigger_source_software) or not PySpin.IsReadable(
                    node_trigger_source_software):
                print('Unable to set trigger source (enum entry retrieval). Aborting...')
                return False
            node_trigger_source.SetIntValue(node_trigger_source_software.GetValue())
            print('Trigger source set to software...')

        elif CHOSEN_TRIGGER == TriggerType.HARDWARE:
            node_trigger_source_hardware = node_trigger_source.GetEntryByName('Line0')
            if not PySpin.IsAvailable(node_trigger_source_hardware) or not PySpin.IsReadable(
                    node_trigger_source_hardware):
                print('Unable to set trigger source (enum entry retrieval). Aborting...')
                return False
            node_trigger_source.SetIntValue(node_trigger_source_hardware.GetValue())
            print('Trigger source set to hardware...')

        # Turn trigger mode on
        # Once the appropriate trigger source has been set, turn trigger mode
        # on in order to retrieve images using the trigger.
        node_trigger_mode_on = node_trigger_mode.GetEntryByName('On')
        if not PySpin.IsAvailable(node_trigger_mode_on) or not PySpin.IsReadable(node_trigger_mode_on):
            print('Unable to enable trigger mode (enum entry retrieval). Aborting...')
            return False

        node_trigger_mode.SetIntValue(node_trigger_mode_on.GetValue())
        print('Trigger mode turned back on...')

    except PySpin.SpinnakerException as ex:
        print('Error: %s' % ex)
        return False

    return result

def grab_next_image_by_trigger(nodemap, cam):
    """
    This function acquires an image by executing the trigger node.

    :param cam: Camera to acquire images from.
    :param nodemap: Device nodemap.
    :type cam: CameraPtr
    :type nodemap: INodeMap
    :return: True if successful, False otherwise.
    :rtype: bool
    """
    try:
        result = True
        # Use trigger to capture image
        # The software trigger only feigns being executed by the Enter key;
        # what might not be immediately apparent is that there is not a
        # continuous stream of images being captured; in other examples that
        # acquire images, the camera captures a continuous stream of images.
        # When an image is retrieved, it is plucked from the stream.

        if CHOSEN_TRIGGER == TriggerType.SOFTWARE:
            # Get user input
            input('Press the Enter key to initiate software trigger.')

            # Execute software trigger
            node_softwaretrigger_cmd = PySpin.CCommandPtr(nodemap.GetNode('TriggerSoftware'))
            if not PySpin.IsAvailable(node_softwaretrigger_cmd) or not PySpin.IsWritable(node_softwaretrigger_cmd):
                print('Unable to execute trigger. Aborting...')
                return False

            node_softwaretrigger_cmd.Execute()

            # TODO: Blackfly and Flea3 GEV cameras need 2 second delay after software trigger

        elif CHOSEN_TRIGGER == TriggerType.HARDWARE:
            print('Use the hardware to trigger image acquisition.')

    except PySpin.SpinnakerException as ex:
        print('Error: %s' % ex)
        return False

    return result

def acquire_images(cam, nodemap, nodemap_tldevice):
    """
    This function acquires and saves 10 images from a device.
    Please see Acquisition example for more in-depth comments on acquiring images.

    :param cam: Camera to acquire images from.
    :param nodemap: Device nodemap.
    :param nodemap_tldevice: Transport layer device nodemap.
    :type cam: CameraPtr
    :type nodemap: INodeMap
    :type nodemap_tldevice: INodeMap
    :return: True if successful, False otherwise.
    :rtype: bool
    """

    print('*** IMAGE ACQUISITION ***\n')
    try:
        result = True

        # Set acquisition mode to continuous
        # In order to access the node entries, they have to be casted to a pointer type (CEnumerationPtr here)
        node_acquisition_mode = PySpin.CEnumerationPtr(nodemap.GetNode('AcquisitionMode'))
        if not PySpin.IsAvailable(node_acquisition_mode) or not PySpin.IsWritable(node_acquisition_mode):
            print('Unable to set acquisition mode to continuous (enum retrieval). Aborting...')
            return False

        # Retrieve entry node from enumeration node
        node_acquisition_mode_continuous = node_acquisition_mode.GetEntryByName('Continuous')
        if not PySpin.IsAvailable(node_acquisition_mode_continuous) or not PySpin.IsReadable(
                node_acquisition_mode_continuous):
            print('Unable to set acquisition mode to continuous (entry retrieval). Aborting...')
            return False

        # Retrieve integer value from entry node
        acquisition_mode_continuous = node_acquisition_mode_continuous.GetValue()

        # Set integer value from entry node as new value of enumeration node
        node_acquisition_mode.SetIntValue(acquisition_mode_continuous)

        print('Acquisition mode set to continuous...')

        #  Begin acquiring images
        cam.BeginAcquisition()

        print('Acquiring images...')

        #  Retrieve device serial number for filename
        #
        #  *** NOTES ***
        #  The device serial number is retrieved in order to keep cameras from
        #  overwriting one another. Grabbing image IDs could also accomplish
        #  this.
        device_serial_number = ''
        node_device_serial_number = PySpin.CStringPtr(nodemap_tldevice.GetNode('DeviceSerialNumber'))
        if PySpin.IsAvailable(node_device_serial_number) and PySpin.IsReadable(node_device_serial_number):
            device_serial_number = node_device_serial_number.GetValue()
            print('Device serial number retrieved as %s...' % device_serial_number)

        # Retrieve, convert, and save images
        for i in range(NUM_IMAGES):
            try:

                #  Retrieve the next image from the trigger
                result &= grab_next_image_by_trigger(nodemap, cam)

                #  Retrieve next received image
                image_result = cam.GetNextImage(1000)

                #  Ensure image completion
                if image_result.IsIncomplete():
                    print('Image incomplete with image status %d ...' % image_result.GetImageStatus())

                else:

                    #  Print image information; height and width recorded in pixels
                    #
                    #  *** NOTES ***
                    #  Images have quite a bit of available metadata including
                    #  things such as CRC, image status, and offset values, to
                    #  name a few.
                    width = image_result.GetWidth()
                    height = image_result.GetHeight()
                    print('Grabbed Image %d, width = %d, height = %d' % (i, width, height))

                    #  Convert image to mono 8
                    #
                    #  *** NOTES ***
                    #  Images can be converted between pixel formats by using
                    #  the appropriate enumeration value. Unlike the original
                    #  image, the converted one does not need to be released as
                    #  it does not affect the camera buffer.
                    #
                    #  When converting images, color processing algorithm is an
                    #  optional parameter.
                    image_converted = image_result.Convert(PySpin.PixelFormat_Mono8, PySpin.HQ_LINEAR)

                    # Create a unique filename
                    if device_serial_number:
                        filename = 'Trigger-%s-%d.jpg' % (device_serial_number, i)
                    else:  # if serial number is empty
                        filename = 'Trigger-%d.jpg' % i

                    # Save image
                    #
                    #  *** NOTES ***
                    #  The standard practice of the examples is to use device
                    #  serial numbers to keep images of one device from
                    #  overwriting those of another.
                    image_converted.Save(filename)
                    print('Image saved at %s\n' % filename)

                    #  Release image
                    #
                    #  *** NOTES ***
                    #  Images retrieved directly from the camera (i.e. non-converted
                    #  images) need to be released in order to keep from filling the
                    #  buffer.
                    image_result.Release()

            except PySpin.SpinnakerException as ex:
                print('Error: %s' % ex)
                return False

        # End acquisition
        #
        #  *** NOTES ***
        #  Ending acquisition appropriately helps ensure that devices clean up
        #  properly and do not need to be power-cycled to maintain integrity.
        cam.EndAcquisition()

    except PySpin.SpinnakerException as ex:
        print('Error: %s' % ex)
        return False

    return result

def reset_trigger(nodemap):
    """
    This function returns the camera to a normal state by turning off trigger mode.

    :param nodemap: Transport layer device nodemap.
    :type nodemap: INodeMap
    :returns: True if successful, False otherwise.
    :rtype: bool
    """
    try:
        result = True
        node_trigger_mode = PySpin.CEnumerationPtr(nodemap.GetNode('TriggerMode'))
        if not PySpin.IsAvailable(node_trigger_mode) or not PySpin.IsReadable(node_trigger_mode):
            print('Unable to disable trigger mode (node retrieval). Aborting...')
            return False

        node_trigger_mode_off = node_trigger_mode.GetEntryByName('Off')
        if not PySpin.IsAvailable(node_trigger_mode_off) or not PySpin.IsReadable(node_trigger_mode_off):
            print('Unable to disable trigger mode (enum entry retrieval). Aborting...')
            return False

        node_trigger_mode.SetIntValue(node_trigger_mode_off.GetValue())

        print('Trigger mode disabled...')

    except PySpin.SpinnakerException as ex:
        print('Error: %s' % ex)
        result = False

    return result

def print_device_info(nodemap):
    """
    This function prints the device information of the camera from the transport
    layer; please see NodeMapInfo example for more in-depth comments on printing
    device information from the nodemap.

    :param nodemap: Transport layer device nodemap.
    :type nodemap: INodeMap
    :returns: True if successful, False otherwise.
    :rtype: bool
    """

    print('*** DEVICE INFORMATION ***\n')

    try:
        result = True
        node_device_information = PySpin.CCategoryPtr(nodemap.GetNode('DeviceInformation'))

        if PySpin.IsAvailable(node_device_information) and PySpin.IsReadable(node_device_information):
            features = node_device_information.GetFeatures()
            for feature in features:
                node_feature = PySpin.CValuePtr(feature)
                print('%s: %s' % (node_feature.GetName(),
                                  node_feature.ToString() if PySpin.IsReadable(node_feature) else 'Node not readable'))

        else:
            print('Device control information not available.')

    except PySpin.SpinnakerException as ex:
        print('Error: %s' % ex)
        return False

    return result

def run_single_camera(cam):
    """
    This function acts as the body of the example; please see NodeMapInfo example
    for more in-depth comments on setting up cameras.

    :param cam: Camera to run on.
    :type cam: CameraPtr
    :return: True if successful, False otherwise.
    :rtype: bool
    """
    try:
        result = True
        err = False

        # Retrieve TL device nodemap and print device information
        nodemap_tldevice = cam.GetTLDeviceNodeMap()

        result &= print_device_info(nodemap_tldevice)

        # Initialize camera
        cam.Init()

        # Retrieve GenICam nodemap
        nodemap = cam.GetNodeMap()

        # Configure trigger
        if configure_trigger(cam) is False:
            return False

        # Acquire images
        result &= acquire_images(cam, nodemap, nodemap_tldevice)

        # Reset trigger
        result &= reset_trigger(nodemap)

        # Deinitialize camera
        cam.DeInit()

    except PySpin.SpinnakerException as ex:
        print('Error: %s' % ex)
        result = False

    return result

def main():
    """
    Example entry point; please see Enumeration example for more in-depth
    comments on preparing and cleaning up the system.

    :return: True if successful, False otherwise.
    :rtype: bool
    """

    # Since this application saves images in the current folder
    # we must ensure that we have permission to write to this folder.
    # If we do not have permission, fail right away.
    try:
        test_file = open('test.txt', 'w+')
    except IOError:
        print('Unable to write to current directory. Please check permissions.')
        input('Press Enter to exit...')
        return False

    test_file.close()
    os.remove(test_file.name)

    result = True

    # Retrieve singleton reference to system object
    system = PySpin.System.GetInstance()

    # Get current library version
    version = system.GetLibraryVersion()
    print('Library version: %d.%d.%d.%d' % (version.major, version.minor, version.type, version.build))

    # Retrieve list of cameras from the system
    cam_list = system.GetCameras()

    num_cameras = cam_list.GetSize()

    print('Number of cameras detected: %d' % num_cameras)

    # Finish if there are no cameras
    if num_cameras == 0:
        # Clear camera list before releasing system
        cam_list.Clear()

        # Release system instance
        system.ReleaseInstance()

        print('Not enough cameras!')
        input('Done! Press Enter to exit...')
        return False

    # Run example on each camera
    for i, cam in enumerate(cam_list):

        print('Running example for camera %d...' % i)

        result &= run_single_camera(cam)
        print('Camera %d example complete... \n' % i)

    # Release reference to camera
    # NOTE: Unlike the C++ examples, we cannot rely on pointer objects being automatically
    # cleaned up when going out of scope.
    # The usage of del is preferred to assigning the variable to None.
    del cam

    # Clear camera list before releasing system
    cam_list.Clear()

    # Release system instance
    system.ReleaseInstance()

    input('Done! Press Enter to exit...')
    return result

if __name__ == '__main__':
    if main():
        sys.exit(0)
    else:
        sys.exit(1)


text:
#  Copyright (C) 2021 Xilinx, Inc
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

# This file is used to configure your project.
# Read more about the various options under:
# http://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files

# -*- coding: utf-8 -*-
"""
    Setup file for logicnets.
    Use setup.cfg to configure your project.

    This file was generated with PyScaffold 3.2.3.
    PyScaffold helps you to put up the scaffold of your new Python project.
    Learn more under: https://pyscaffold.org/
"""
import sys

from pkg_resources import VersionConflict, require
from setuptools import setup

try:
    require('setuptools>=38.3')
except VersionConflict:
    print("Error: version of setuptools is too old (<38.3)!")
    sys.exit(1)

if __name__ == "__main__":
    setup(use_pyscaffold=True)


text:
import csv
import six
import sys
import time

from datetime import (
    datetime,
    date,
    timedelta,
)
from xml.etree import cElementTree as ElementTree

from django.core.management.base import BaseCommand

from corehq.apps.users.util import SYSTEM_USER_ID
from corehq.form_processor.backends.sql.dbaccessors import CaseAccessorSQL, CaseReindexAccessor
from corehq.form_processor.exceptions import CaseNotFound
from corehq.form_processor.interfaces.dbaccessors import CaseAccessors
from corehq.apps.locations.models import SQLLocation
from corehq.apps.hqcase.utils import submit_case_blocks
from corehq.form_processor.backends.sql.dbaccessors import iter_all_rows

from casexml.apps.case.mock import CaseBlock

DOMAIN = "icds-cas"
CASE_TYPE = "person"
CUT_OFF_AGE_IN_YEARS = 6
date_today = date.today()
CUT_OFF_DOB = str(date_today.replace(year=date_today.year - CUT_OFF_AGE_IN_YEARS))

DOB_PROPERTY = "dob"
MOTHER_NAME_PROPERTY = "mother_name"
MOTHER_INDEX_IDENTIFIER = "mother"

CASE_ITERATION_COUNT = 10000
MAX_RESCUE_EXCEPTIONS_ON_UPDATE = 5

CSV_HEADERS = ['Case ID', 'Mother Case ID', 'Mother Name']

class Command(BaseCommand):
    help = """
    Iterate person cases updated in last 100 days (3 months with buffer) in a single partition,
    Find the ones which are
        - not deleted
        - not belonging to test locations,
        - with age less than 6 years using dob case property,
            - if there is related mother case, populate mother_name case property with it's name
    Returns two lists of case ids, the ones updated and the ones that could not be updated
    """

    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.db_alias = None
        self.log_progress = False
        self.test_locations = None
        self.case_accessor = CaseAccessors(DOMAIN)

    def add_arguments(self, parser):
        parser.add_argument('db_alias')
        parser.add_argument(
            '--log',
            action='store_true',
            dest='log_progress',
            default=False,
            help="log progress"
        )

    def handle(self, db_alias, log_progress, **options):
        self.db_alias = db_alias
        self.log_progress = log_progress
        self.test_locations = find_test_awc_locations()

        filename = self._find_case_ids_without_mother_name()
        if self.log_progress:
            print('starting update now for cases')
        self._update_cases(filename)

    def _find_case_ids_without_mother_name(self):
        start_date = date.today() - timedelta(days=100)
        reindex_accessor = CaseReindexAccessor(
            domain=DOMAIN,
            case_type=CASE_TYPE, limit_db_aliases=[self.db_alias],
            start_date=start_date
        )

        filename = 'cases_without_mother_name_part_%s_%s.csv' % (self.db_alias, datetime.utcnow())
        cases_with_no_mother_name_filename = 'cases_with_no_mother_name_part_%s_%s.csv' % (
            self.db_alias, datetime.utcnow())
        with open(filename, 'w') as output:
            with open(cases_with_no_mother_name_filename, 'w') as no_mother_name_file:
                cases_iterated = 0
                writer = csv.writer(output)
                writer.writerow(CSV_HEADERS)
                no_mother_name_writer = csv.writer(no_mother_name_file)
                no_mother_name_writer.writerow(['Case ID'])
                if self.log_progress:
                    print('iterating now')
                for case in iter_all_rows(reindex_accessor):
                    if self.log_progress and cases_iterated % CASE_ITERATION_COUNT == 0:
                        print("cases iterated: %s" % cases_iterated)
                    cases_iterated += 1
                    if self._case_needs_to_be_updated(case):
                        mother_case_id, mother_name = self._find_mother_case_id_and_name(case)
                        if mother_case_id and mother_name:
                            writer.writerow([case.case_id, mother_case_id, mother_name])
                        else:
                            no_mother_name_writer.writerow([case.case_id])
        return filename

    def _find_mother_case_id_and_name(self, case):
        mother_case_ids = [i.referenced_id for i in CaseAccessorSQL.get_indices(DOMAIN, case.case_id)
                           if i.identifier == MOTHER_INDEX_IDENTIFIER]
        if len(mother_case_ids) == 1:
            try:
                mother_case = self.case_accessor.get_case(mother_case_ids[0])
            except CaseNotFound:
                pass
            else:
                return mother_case.case_id, mother_case.name
        return None, None

    def _case_needs_to_be_updated(self, case):
        if case.deleted:
            return False
        assert case.type == CASE_TYPE
        if bool(case.owner_id) and case.owner_id in self.test_locations:
            return False
        dob = case.get_case_property(DOB_PROPERTY)
        if dob and dob > CUT_OFF_DOB and not case.get_case_property(MOTHER_NAME_PROPERTY):
            return True
        return False

    def _update_cases(self, filename):
        exceptions_raised = 0
        updates = {}  # case id: mother name
        counter = 0
        with open(filename, 'r') as _input:
            reader = csv.DictReader(_input)
            with open('cases_without_mother_name_part_%s_updated.csv' % self.db_alias, 'w') as output:
                writer = csv.writer(output)
                writer.writerow(['Case ID', 'Mother Name'])
                for row in reader:
                    updates[row['Case ID']] = row['Mother Name']
                    counter += 1
                    if counter > 0 and counter % 100 == 0:
                        case_ids = self._reassured_case_ids_to_update(list(updates.keys()))
                        skip_ids = updates.keys() - case_ids
                        for case_id in skip_ids:
                            updates.pop(case_id)
                        for case_id, mother_name in updates.items():
                            writer.writerow([case_id, mother_name])
                        exceptions_raised = self._submit_update_form(updates, exceptions_raised)
                        if self.log_progress:
                            print("cases updated: %s" % counter)
                        updates = {}
                        counter = 0
                # update the pending batch
                for case_id, mother_name in updates.items():
                    writer.writerow([case_id, mother_name])
                exceptions_raised = self._submit_update_form(updates, exceptions_raised)

    def _submit_update_form(self, updates, exceptions_raised):
        update_case_blocks = self.create_case_blocks(updates)
        if not update_case_blocks:
            return exceptions_raised
        for attempt in range(MAX_RESCUE_EXCEPTIONS_ON_UPDATE):
            try:
                submit_case_blocks(update_case_blocks, DOMAIN, user_id=SYSTEM_USER_ID)
            except Exception as e:
                exc = sys.exc_info()
                exceptions_raised += 1
                if self.log_progress:
                    print("rescuing exception %s %s" % (exceptions_raised, str(e)))
                if exceptions_raised > MAX_RESCUE_EXCEPTIONS_ON_UPDATE:
                    six.reraise(*exc)
                else:
                    time.sleep(60)  # wait for 1 min before trying again
            else:
                break
        return exceptions_raised

    def create_case_blocks(self, updates):
        case_blocks = []
        for case_id, mother_name in updates.items():
            case_block = CaseBlock.deprecated_init(case_id,
                                   update={MOTHER_NAME_PROPERTY: mother_name},
                                   user_id=SYSTEM_USER_ID)
            case_block = ElementTree.tostring(case_block.as_xml()).decode('utf-8')
            case_blocks.append(case_block)
        return case_blocks

    def _reassured_case_ids_to_update(self, case_ids):
        # reconfirm the cases before updating to avoid removing updates in between
        # fetching case ids and updating
        invalid_cases = self.case_accessor.get_cases(case_ids)
        case_ids_list = set()
        for invalid_case in invalid_cases:
            if self._case_needs_to_be_updated(invalid_case):
                case_ids_list.add(invalid_case.case_id)
        return case_ids_list

def find_test_awc_locations():
    test_locations = set()
    for location in SQLLocation.active_objects.filter(location_type__code='state', domain=DOMAIN):
        if location.metadata.get('is_test_location') == 'test':
            test_locations.update(
                location.get_descendants(include_self=True).
                filter(location_type__code='awc').values_list('location_id', flat=True)
            )
    return test_locations


text:
# MIT License
#
# Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from unittest import mock

import numpy as np
import pytest
from helpers.scenario import temp_scenario

from smarts.core.agent_interface import AgentInterface
from smarts.core.coordinates import Heading, Pose
from smarts.core.plan import Plan
from smarts.core.scenario import Scenario
from smarts.core.sensors import DrivenPathSensor, WaypointsSensor
from smarts.sstudio import gen_scenario
from smarts.sstudio import types as t

AGENT_ID = "Agent-007"

def test_driven_path_sensor():
    vehicle = mock.Mock()
    sim = mock.Mock()

    max_path_length = 5
    sensor = DrivenPathSensor(vehicle, max_path_length=max_path_length)

    positions = [(x, 0, 0) for x in range(0, 100, 10)]
    sim_times = list(range(0, 50, 5))
    for idx, (position, sim_time) in enumerate(zip(positions, sim_times)):
        sim.elapsed_sim_time = sim_time
        vehicle.position = position
        sensor.track_latest_driven_path(sim)

        if idx >= 3:
            assert sensor.distance_travelled(sim, last_n_steps=3) == 30
            assert sensor.distance_travelled(sim, last_n_seconds=10) == 20

        assert len(sensor()) <= max_path_length

    sensor.teardown()

@pytest.fixture
def scenarios():
    with temp_scenario(name="straight", map="maps/6lane.net.xml") as scenario_root:
        ego_missions = [
            t.Mission(
                t.Route(
                    begin=("edge-west-WE", 0, 10),
                    end=("edge-east-WE", 0, "max"),
                )
            ),
        ]
        gen_scenario(
            t.Scenario(ego_missions=ego_missions),
            output_dir=scenario_root,
        )

        yield Scenario.variations_for_all_scenario_roots(
            [str(scenario_root)], [AGENT_ID]
        )

def test_waypoints_sensor(scenarios):
    scenario = next(scenarios)
    sim = mock.Mock()
    vehicle = mock.Mock()
    vehicle.pose = Pose(
        position=np.array([33, -65, 0]),
        orientation=[0, 0, 0, 0],
        heading_=Heading(0),
    )

    mission = scenario.missions[AGENT_ID]
    plan = Plan(scenario.road_map, mission)

    sensor = WaypointsSensor(vehicle, plan)
    waypoints = sensor()

    assert len(waypoints) == 3


text:
#!/usr/bin/env python
'''
Pull random words from http://world.std.com/~reinhold/diceware.wordlist.asc

Written 2013 Hal Canary.
Dedicated to the public domain.
'''
import random,math,sys,os
useDevRandom = True
dicewareWordlist = '~/Downloads/diceware.wordlist.asc'
with open(os.path.expanduser(dicewareWordlist)) as f:
	WordList = [line.split()[1]
				for nu,line in enumerate(f) if 2 <= nu < 7778]
def GetRandom():
	if useDevRandom:
		with open('/dev/random', 'rb') as f:
			random.seed(f.read(16))
		return random
	else:
		return random.SystemRandom()
required_entropy = 128
numwords = int(math.ceil(required_entropy / math.log(len(WordList),2)))
s = ' '.join(GetRandom().choice(WordList) for i in xrange(numwords))
sys.stdout.write(s)
sys.stdout.flush()
sys.stderr.write('\n')


text:
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

class LineageBackend(object):
    def send_lineage(self,
                     operator=None, inlets=None, outlets=None, context=None):
        """
        Sends lineage metadata to a backend
        :param operator: the operator executing a transformation on the inlets and outlets
        :param inlets: the inlets to this operator
        :param outlets: the outlets from this operator
        :param context: the current context of the task instance
        """
        raise NotImplementedError()


text:
import os
import csv
import shutil
from datetime import datetime
from numpy import logspace

import torch
import torch.nn as nn
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data import DataLoader
from torch.optim import Adam

from dataset.e_piano import create_epiano_datasets, create_pop909_datasets

from model.music_transformer import MusicTransformer

from model.discriminator import MusicDiscriminator
from model.classifier import CNNDiscriminator

from model.loss import SmoothCrossEntropyLoss

from utilities.constants import *
from utilities.WGAN_GP import WassersteinLoss
from utilities.device import get_device, use_cuda
from utilities.lr_scheduling import LrStepTracker, get_lr
from utilities.argument_funcs import parse_train_args, print_train_args, write_model_params
from utilities.run_model import train_epoch, eval_model

CSV_HEADER = ["Epoch", "Learn rate", "Avg Train loss", "Train Accuracy", "Avg Eval loss", "Eval accuracy"]

dis_filter_sizes = [2, 3, 4, 5]
dis_num_filters = [300, 300, 300, 300]

# Baseline is an untrained epoch that we evaluate as a baseline loss and accuracy
BASELINE_EPOCH = -1

# main
def main():
    """
    ----------
    Author: Damon Gwinn
    ----------
    Entry point. Trains a model specified by command line arguments
    ----------
    """

    args = parse_train_args()
    print_train_args(args)

    if(args.force_cpu):
        use_cuda(False)
        print("WARNING: Forced CPU usage, expect model to perform slower")
        print("")

    eventid = f"{datetime.now().strftime('MusicTransformer-%Y.%m.%d')}_gan_{args.gan}_creative_{args.creative}_ce_{args.ce_smoothing}"

    args.output_dir = args.output_dir  + "/" +  eventid

    os.makedirs(args.output_dir, exist_ok=True)

    ##### Output prep #####
    params_file = os.path.join(args.output_dir, "model_params.txt")
    write_model_params(args, params_file)

    weights_folder = os.path.join(args.output_dir, "weights")
    os.makedirs(weights_folder, exist_ok=True)

    results_folder = os.path.join(args.output_dir, "results")
    os.makedirs(results_folder, exist_ok=True)

    results_file = os.path.join(results_folder, "results.csv")
    best_loss_file = os.path.join(results_folder, "best_loss_weights.pickle")
    best_acc_file = os.path.join(results_folder, "best_acc_weights.pickle")
    best_loss_critic_file = os.path.join(results_folder, "best_loss_critic_weights.pickle")
    best_acc_critic_file = os.path.join(results_folder, "best_acc_critic_weights.pickle")

    best_loss_classifier_file = os.path.join(
        results_folder, "best_loss_classifier_weights.pickle")
    best_acc_classifier_file = os.path.join(
        results_folder, "best_acc_classifier_weights.pickle")

    best_text = os.path.join(results_folder, "best_epochs.txt")

    ##### Tensorboard #####
    if(args.no_tensorboard):
        tensorboard_summary = None
    else:
        from torch.utils.tensorboard import SummaryWriter

        tensorboad_dir = os.path.join(args.output_dir, "tensorboard/" + eventid)
        tensorboard_summary = SummaryWriter(log_dir=tensorboad_dir)

    ##### Datasets #####
    # 데이터셋이 바뀌기 때문에 아래와같이 해주어야함
    if args.interval and args.octave:
        print("octave interval dataset!!")
        classic_train, classic_val, classic_test = create_epiano_datasets('./dataset/octave_interval_e_piano', args.max_sequence,
                                                                          condition_token=args.condition_token, interval = args.interval, octave = args.octave)
        pop909_dataset = create_pop909_datasets('./dataset/logscale_pop909', args.max_sequence, condition_token=args.condition_token, interval = args.interval, octave = args.octave)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                       [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1),
                                                                        len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                       generator=torch.Generator().manual_seed(42))
    elif args.octave and args.fusion_encoding and args.absolute:
        print("absolute dataset!!")
        classic_train, classic_val, classic_test = create_epiano_datasets('./dataset/octave_fusion_absolute_e_piano', args.max_sequence,
                                                                          condition_token=args.condition_token, interval = args.interval, octave = args.octave, fusion = args.fusion_encoding, absolute = args.absolute)
        pop909_dataset = create_pop909_datasets('./dataset/pop909_absolute', args.max_sequence, condition_token=args.condition_token, interval = args.interval, octave = args.octave, fusion = args.fusion_encoding, absolute = args.absolute)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                       [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1),
                                                                        len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                       generator=torch.Generator().manual_seed(42))
    elif args.interval and not args.octave:
        print("interval dataset!!")
        classic_train, classic_val, classic_test = create_epiano_datasets('./dataset/logscale_e_piano', args.max_sequence,
                                                                          condition_token=args.condition_token, interval = args.interval, octave = args.octave)
        pop909_dataset = create_pop909_datasets('./dataset/logscale_pop909', args.max_sequence, condition_token=args.condition_token, interval = args.interval, octave = args.octave)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                       [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1),
                                                                        len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                       generator=torch.Generator().manual_seed(42))
    elif args.octave and args.fusion_encoding:
        print("Octave_fusion dataset!!")
        classic_train, classic_val, classic_test = create_epiano_datasets('./dataset/octave_fusion_e_piano', args.max_sequence,
                                                                          condition_token=args.condition_token, interval = args.interval, octave = args.octave, fusion = args.fusion_encoding)
        pop909_dataset = create_pop909_datasets('./dataset/logscale_pop909', args.max_sequence, condition_token=args.condition_token, interval = args.interval, octave = args.octave, fusion = args.fusion_encoding)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                       [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1),
                                                                        len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                       generator=torch.Generator().manual_seed(42))
    elif not args.interval and args.octave and not args.fusion_encoding:
        print("Octave dataset!!")
        classic_train, classic_val, classic_test = create_epiano_datasets('./dataset/octave_e_piano', args.max_sequence,
                                                                          condition_token=args.condition_token, interval = args.interval, octave = args.octave)
        pop909_dataset = create_pop909_datasets('./dataset/pop909_octave', args.max_sequence, condition_token=args.condition_token, interval = args.interval, octave = args.octave)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                       [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1),
                                                                        len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                       generator=torch.Generator().manual_seed(42))
    elif args.logscale:
        print("logscvale dataset")
        classic_train, classic_val, classic_test = create_epiano_datasets('./dataset/logscale_epiano0420', args.max_sequence, random_seq=True,
                                                                            condition_token=args.condition_token, interval = args.interval, octave = args.octave, logscale=args.logscale, absolute = args.absolute)
        pop909_dataset = create_pop909_datasets('./dataset/logscale_pop0420', args.max_sequence, random_seq=True, condition_token=args.condition_token, interval = args.interval, octave = args.octave, logscale=args.logscale, absolute = args.absolute)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                        [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1),
                                                                        len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                        generator=torch.Generator().manual_seed(42))
    else:
        classic_train, classic_val, classic_test = create_epiano_datasets(args.classic_input_dir, args.max_sequence,
                                                                          condition_token = args.condition_token, octave = args.octave)
        pop909_dataset = create_pop909_datasets('dataset/pop_pickle/', args.max_sequence, condition_token = args.condition_token, octave = args.octave)
        pop_train, pop_valid, pop_test = torch.utils.data.random_split(pop909_dataset,
                                                                       [int(len(pop909_dataset) * 0.8), int(len(pop909_dataset) * 0.1), len(pop909_dataset) - int(len(pop909_dataset) * 0.8) - int(len(pop909_dataset) * 0.1)],
                                                                       generator=torch.Generator().manual_seed(42))

    if args.data == 'both':
        print("Dataset: both")
        train_dataset = torch.utils.data.ConcatDataset([ classic_train, pop_train])
        val_dataset = torch.utils.data.ConcatDataset([ classic_val, pop_valid])
    elif args.data == 'classic':
        print("Dataset: classic")
        train_dataset = torch.utils.data.ConcatDataset([classic_train])
        val_dataset = torch.utils.data.ConcatDataset([classic_val])
    else:
        print("Dataset: pop")
        train_dataset = torch.utils.data.ConcatDataset([pop_train])
        val_dataset = torch.utils.data.ConcatDataset([pop_valid])

    test_dataset = torch.utils.data.ConcatDataset([classic_test, pop_test])

    train_loader = DataLoader(train_dataset, batch_size=args.batch_size, num_workers=args.n_workers, shuffle=True)
    val_loader = DataLoader(val_dataset, batch_size=args.batch_size, num_workers=args.n_workers)
    test_loader = DataLoader(test_dataset, batch_size=args.batch_size, num_workers=args.n_workers)

    model = MusicTransformer(n_layers=args.n_layers, num_heads=args.num_heads,
                d_model=args.d_model, dim_feedforward=args.dim_feedforward, dropout=args.dropout,
                                    max_sequence=args.max_sequence, rpr=args.rpr,
                                    condition_token = args.condition_token, interval = args.interval, octave = args.octave,
                                    fusion = args.fusion_encoding, absolute = args.absolute, logscale=args.logscale).to(get_device())

    # EY critic
    # num_prime = args.num_prime
    critic = MusicDiscriminator(n_layers=args.n_layers // 2, num_heads=args.num_heads // 2,
                d_model=args.d_model // 2, dim_feedforward=args.dim_feedforward // 2, dropout=args.dropout,
                max_sequence=args.max_sequence, rpr=args.rpr).to(get_device())

    classifier = MusicDiscriminator(n_layers=args.n_layers // 2, num_heads=args.num_heads // 2,
                                 d_model=args.d_model // 2, dim_feedforward=args.dim_feedforward // 2, dropout=args.dropout,
                                 max_sequence=args.max_sequence, rpr=args.rpr).to(get_device())

    if args.creative:
        classifier.load_state_dict(torch.load('best_classifier_acc_0.9883.pickle'))

    ##### Continuing from previous training session #####
    start_epoch = BASELINE_EPOCH
    if(args.continue_weights is not None):
        if(args.continue_epoch is None):
            print("ERROR: Need epoch number to continue from (-continue_epoch) when using continue_weights")
            return
        else:
            model.load_state_dict(torch.load(args.continue_weights))
            start_epoch = args.continue_epoch
    elif(args.continue_epoch is not None):
        print("ERROR: Need continue weights (-continue_weights) when using continue_epoch")
        return

    ##### Lr Scheduler vs static lr #####
    if(args.lr is None):
        if(args.continue_epoch is None):
            init_step = 0
        else:
            init_step = args.continue_epoch * len(train_loader)

        lr = LR_DEFAULT_START
        lr_stepper = LrStepTracker(args.d_model, SCHEDULER_WARMUP_STEPS, init_step)
    else:
        lr = args.lr

    ##### Not smoothing evaluation loss #####
    if args.interval and args.octave:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD_OCTAVE_INTERVAL)
    elif args.interval and not args.octave:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD_INTERVAL)
    elif args.octave and args.fusion_encoding and args.absolute:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD_OCTAVE_FUSION_ABSOLUTE)
    elif args.octave and args.fusion_encoding:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD_OCTAVE_FUSION)
    elif not args.interval and args.octave and not args.fusion_encoding:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD_OCTAVE)
    elif args.logscale:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD_RELATIVE)
    else:
        eval_loss_func = nn.CrossEntropyLoss(ignore_index=TOKEN_PAD)

    ##### SmoothCrossEntropyLoss or CrossEntropyLoss for training #####
    if(args.ce_smoothing is None):
        train_loss_func = eval_loss_func
    else:
        if args.interval and args.octave:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE_OCTAVE_INTERVAL, ignore_index=TOKEN_PAD_INTERVAL)
        elif args.interval and not args.octave:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE_INTERVAL, ignore_index=TOKEN_PAD_INTERVAL)
        elif not args.interval and args.octave and args.fusion_encoding and args.absolute:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE_OCTAVE_FUSION_ABSOLUTE, ignore_index=TOKEN_PAD_OCTAVE_FUSION_ABSOLUTE)
        elif not args.interval and args.octave and args.fusion_encoding:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE_OCTAVE_FUSION, ignore_index=TOKEN_PAD_OCTAVE_FUSION)
        elif not args.interval and args.octave and not args.fusion_encoding:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE_OCTAVE, ignore_index=TOKEN_PAD_OCTAVE)
        elif args.logscale:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE_RELATIVE, ignore_index=TOKEN_PAD_RELATIVE)
        else:
            train_loss_func = SmoothCrossEntropyLoss(args.ce_smoothing, VOCAB_SIZE, ignore_index=TOKEN_PAD)

    ##### EY - WGAN Loss #####
    classifier_loss_func = nn.MSELoss()

    ##### Optimizer #####
    opt = Adam(model.parameters(), lr=lr, betas=(ADAM_BETA_1, ADAM_BETA_2), eps=ADAM_EPSILON)
    critic_opt = Adam(critic.parameters(), lr=lr, betas=(ADAM_BETA_1, ADAM_BETA_2), eps=ADAM_EPSILON)
    classifier_opt = Adam(classifier.parameters(), lr=lr, betas=(ADAM_BETA_1, ADAM_BETA_2), eps=ADAM_EPSILON)

    if(args.lr is None):
        lr_scheduler = LambdaLR(opt, lr_stepper.step)
        critic_lr_scheduler = LambdaLR(critic_opt, lr_stepper.step)
        classifier_lr_scheduler = LambdaLR(classifier_opt, lr_stepper.step)
    else:
        lr_scheduler = None

    ##### Tracking best evaluation accuracy #####
    best_eval_acc        = 0.0
    best_eval_acc_epoch  = -1
    best_eval_loss       = float("inf")
    best_eval_loss_epoch = -1

    ##### Results reporting #####
    if(not os.path.isfile(results_file)):
        with open(results_file, "w", newline="") as o_stream:
            writer = csv.writer(o_stream)
            writer.writerow(CSV_HEADER)

    ##### TRAIN LOOP #####
    for epoch in range(start_epoch, args.epochs):
        # Baseline has no training and acts as a base loss and accuracy (epoch 0 in a sense)
        if(epoch >= BASELINE_EPOCH):
            print(SEPERATOR)
            print("NEW EPOCH:", epoch+1)
            print(SEPERATOR)
            print("")

            # Train
            # EY 고쳐야 할 부분의 시작
            train_loss, train_acc, dis_loss, gen_loss, cre_loss, gan_accuracy, class_accuracy, creativity = train_epoch(epoch+1, model, critic, classifier, train_loader, train_loss_func, classifier_loss_func, opt, critic_opt, classifier_opt, lr_scheduler, critic_lr_scheduler, classifier_lr_scheduler, args)

            print(SEPERATOR)
            print("Evaluating:")
        else:
            print(SEPERATOR)
            print("Baseline model evaluation (Epoch 0):")

        # Eval
        # train_loss, train_acc = eval_model(model, train_loader, train_loss_func)
        eval_loss, eval_acc = eval_model(model, val_loader, eval_loss_func, args)

        # Learn rate
        lr = get_lr(opt)

        print("Epoch:", epoch+1)
        print("Avg train loss:", train_loss)
        print("Avg train acc:", train_acc)
        print("Avg eval loss:", eval_loss)
        print("Avg eval acc:", eval_acc)
        print(SEPERATOR)
        print("")

        new_best = False

        if(eval_acc > best_eval_acc):
            best_eval_acc = eval_acc
            best_eval_acc_epoch  = epoch+1
            torch.save(model.state_dict(), best_acc_file)
            torch.save(critic.state_dict(), best_acc_critic_file)
            torch.save(classifier.state_dict(), best_acc_classifier_file)
            new_best = True

        if(eval_loss < best_eval_loss):
            best_eval_loss       = eval_loss
            best_eval_loss_epoch = epoch+1
            torch.save(model.state_dict(), best_loss_file)
            torch.save(critic.state_dict(), best_loss_critic_file)
            torch.save(classifier.state_dict(), best_loss_classifier_file)
            new_best = True

        # Writing out new bests
        if(new_best):
            with open(best_text, "w") as o_stream:
                print("Best eval acc epoch:", best_eval_acc_epoch, file=o_stream)
                print("Best eval acc:", best_eval_acc, file=o_stream)
                print("")
                print("Best eval loss epoch:", best_eval_loss_epoch, file=o_stream)
                print("Best eval loss:", best_eval_loss, file=o_stream)

        if(not args.no_tensorboard):
            tensorboard_summary.add_scalar("Avg_CE_loss/train", train_loss, global_step=epoch+1)
            tensorboard_summary.add_scalar("Avg_CE_loss/eval", eval_loss, global_step=epoch+1)


text:
from typing import Optional

from pyspark.sql import Column, DataFrame
from pyspark.sql.functions import from_unixtime, to_timestamp

from spark_auto_mapper.data_types.data_type_base import AutoMapperDataTypeBase
from spark_auto_mapper.helpers.value_parser import AutoMapperValueParser
from spark_auto_mapper.type_definitions.defined_types import AutoMapperNumberInputType

class AutoMapperUnixTimestampType(AutoMapperDataTypeBase):
    def __init__(self, value: AutoMapperNumberInputType) -> None:
        """
        Converts the value to a timestamp type in Spark

        :param value: value
        :param formats: (Optional) formats to use for trying to parse the value otherwise uses Spark defaults
        """
        super().__init__()

        self.value: AutoMapperDataTypeBase = (
            value
            if isinstance(value, AutoMapperDataTypeBase)
            else AutoMapperValueParser.parse_value(value)
        )

    def get_column_spec(
        self, source_df: Optional[DataFrame], current_column: Optional[Column]
    ) -> Column:
        # Convert from unix timestamp
        column_spec: Column = to_timestamp(
            from_unixtime(
                self.value.get_column_spec(
                    source_df=source_df, current_column=current_column
                ),
                format="yyyy-MM-dd HH:mm:ss",
            ),
            format="yyyy-MM-dd HH:mm:ss",
        )

        if source_df is not None:
            return column_spec
        else:
            column_spec = self.value.get_column_spec(
                source_df=source_df, current_column=current_column
            )
            return column_spec


text:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from targets.firefox.fx_testcase import *

class Test(FirefoxTest):
    @pytest.mark.details(
        description='Browser controls work as expected.',
        locale=['en-US'],
        test_case_id='119481',
        test_suite_id='1998'
    )
    def run(self, firefox):
        window_controls_minimize_pattern = Pattern('window_controls_minimize.png')
        hover_minimize_control_pattern = Pattern('hover_minimize_control.png')
        window_controls_restore_pattern = Pattern('window_controls_restore.png')
        hover_restore_control_pattern = Pattern('hover_restore_control.png')
        window_controls_maximize_pattern = Pattern('window_controls_maximize.png')
        hover_maximize_control_pattern = Pattern('hover_maximize_control.png')
        window_controls_close_pattern = Pattern('window_controls_close.png')
        hover_close_control_pattern = Pattern('hover_close_control.png')

        navigate(LocalWeb.FIREFOX_TEST_SITE)
        assert exists(LocalWeb.FIREFOX_LOGO, 10), 'Page successfully loaded, firefox logo found.'

        if OSHelper.is_linux():
            Mouse().move(Location(0, 0))

        hover(window_controls_minimize_pattern)
        assert exists(hover_minimize_control_pattern, 10), 'Hover over the \'minimize\' button works correctly.'

        if OSHelper.is_windows() or OSHelper.is_linux():
            hover(window_controls_restore_pattern)
            assert exists(hover_restore_control_pattern, 10), 'Hover over the \'restore\' button works correctly.'

        if OSHelper.is_mac():
            middle = find(hover_maximize_control_pattern)
            Mouse().move(Location(middle.x + 7, middle.y + 5))
            assert exists(hover_maximize_control_pattern, 10), 'Hover over the \'maximize\' button works correctly.'

            Mouse().move(Location(middle.x - 35, middle.y + 5))
            assert exists(hover_close_control_pattern.similar(0.7), 10), \
                'Hover over the \'close\' button works correctly.'
        else:
            hover(window_controls_close_pattern)
            assert exists(hover_close_control_pattern, 10), 'Hover over the \'close\' button works correctly.'

        if OSHelper.is_windows() or OSHelper.is_linux():
            click_window_control('restore', 'main')
            time.sleep(Settings.DEFAULT_UI_DELAY)
            hover(window_controls_maximize_pattern)
            assert exists(hover_maximize_control_pattern, 10), \
                'Hover over the \'maximize\' button works correctly; Window successfully restored.'
        if OSHelper:
            hover(Pattern('home_button.png'))
        click_window_control('minimize', 'main')
        time.sleep(Settings.DEFAULT_UI_DELAY)

        try:
            assert wait_vanish(LocalWeb.FIREFOX_LOGO, 10), 'Window successfully minimized.'
        except FindError:
            raise FindError('Window not minimized.')

        restore_window_from_taskbar()

        if OSHelper.is_windows():
            click_window_control('maximize', 'main')

        assert exists(LocalWeb.FIREFOX_LOGO, 10), 'Window successfully opened again.'

        click_window_control('close', 'main')

        try:
            assert wait_vanish(LocalWeb.FIREFOX_LOGO, 10), 'Window successfully closed.'
        except FindError:
            assert False, 'Window successfully closed.'


text:
# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2017-02-17 20:39

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [("scripts", "0007_auto_20150403_2339"), ("comms", "0010_auto_20161206_1912")]

    operations = [
        migrations.AddField(
            model_name="msg",
            name="db_receivers_scripts",
            field=models.ManyToManyField(
                blank=True,
                help_text="script_receivers",
                null=True,
                related_name="receiver_script_set",
                to="scripts.ScriptDB",
            ),
        ),
        migrations.AddField(
            model_name="msg",
            name="db_sender_scripts",
            field=models.ManyToManyField(
                blank=True,
                db_index=True,
                null=True,
                related_name="sender_script_set",
                to="scripts.ScriptDB",
                verbose_name="sender(script)",
            ),
        ),
    ]


text:
####################################################################################
# Jiten Dhandha, 2020                                                              #
# CFit is a curve fitting tool in python, based on the method of least squares.    #
# It comes equipped with some standard functions and a graphical user interface.   #
#                                                                                #
# Inspired by: LSFR.py, Abie Marshall, The University of Manchester, 2016          #
####################################################################################

####################################################################################
#                                    LIBRARIES                                     #
####################################################################################

import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')    #This requires PyQt5 to be installed.
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import scipy.optimize as opt
import scipy.special as sp
import scipy.stats as stats
import scipy.linalg as linalg
import warnings

####################################################################################
#                                 LIST OF FUNCTIONS                                #
####################################################################################

#Class to hold all relevant function information
class Function():
    def __init__(self,name,func,numberOfParameters,rawFuncStr,unicodeFuncStr,rawParametersStr,unicodeParametersStr):
        self.name = name
        self.func = func
        self.numberOfParameters = numberOfParameters
        self.rawFuncStr = rawFuncStr
        self.unicodeFuncStr = unicodeFuncStr
        self.rawParametersStr = rawParametersStr
        self.unicodeParametersStr = unicodeParametersStr

'''
Current supported functions are as follows:
Polynomial: constant, linear, quadratic, cubic, quartic, quintic
Periodic functions: sine wave, square wave
Peak shape functions: gaussian, poisson, laplace, lorentz
Polynomial-based functions: power law
Exponentials and logarithms: exponential, logarithm
'''

#Dictonary to hold the functions
functions = {
            'Constant':
             Function(name='Constant',
                      func=lambda x,a: np.polyval([a],x),
                      numberOfParameters=1,
                      rawFuncStr=r"$y = a$",
                      unicodeFuncStr="y = a",
                      rawParametersStr=[r'$a$'],
                      unicodeParametersStr=['a']),

            'Linear':
            Function(name='Linear',
                     func=lambda x,a,b: np.polyval([a,b],x),
                     numberOfParameters=2,
                     rawFuncStr=r"$y = ax+b$",
                     unicodeFuncStr="y = ax+b",
                     rawParametersStr=[r'$a$',r'$b$'],
                     unicodeParametersStr=['a','b']),

            'Quadratic':
            Function(name='Quadratic',
                     func=lambda x,a,b,c: np.polyval([a,b,c],x),
                     numberOfParameters=3,
                     rawFuncStr=r"$y = ax^2+bx+c$",
                     unicodeFuncStr="y = ax\u00B2+bx+c",
                     rawParametersStr=[r'$a$',r'$b$',r'$c$'],
                     unicodeParametersStr=['a','b','c']),

            'Cubic':
            Function(name='Cubic',
                     func=lambda x,a,b,c,d: np.polyval([a,b,c,d],x),
                     numberOfParameters=4,
                     rawFuncStr=r"$y = ax^3+bx^2+cx+d$",
                     unicodeFuncStr="y = ax\u00B3+bx\u00B2+cx+d",
                     rawParametersStr=[r'$a$',r'$b$',r'$c$',r'$d$'],
                     unicodeParametersStr=['a','b','c','d']),

            'Quartic':
            Function(name='Quadratic',
                     func=lambda x,a,b,c,d,e: np.polyval([a,b,c,d,e],x),
                     numberOfParameters=5,
                     rawFuncStr=r"$y = ax^4+bx^3+cx^2+dx+e$",
                     unicodeFuncStr="y = ax\u2074+bx\u00B3+cx\u00B2+dx+e",
                     rawParametersStr=[r'$a$',r'$b$',r'$c$',r'$d$',r'$e$'],
                     unicodeParametersStr=['a','b','c','d','e']),

            'Quintic':
            Function(name='Quintic',
                     func=lambda x,a,b,c,d,e,f: np.polyval([a,b,c,d,e,f],x),
                     numberOfParameters=6,
                     rawFuncStr=r"$y = ax^5+bx^4+cx^3+dx^2+ex+f$",
                     unicodeFuncStr="y = ax\u2075+bx\u2074+cx\u00B3+dx\u00B2+ex+f",
                     rawParametersStr=[r'$a$',r'$b$',r'$c$',r'$d$',r'$e$',r'$f$'],
                     unicodeParametersStr=['a','b','c','d','e','f']),

            'Sine wave':
            Function(name='Sine wave',
                     func=lambda x,y0,A,omg,phi: y0 + A*np.sin(omg*x+phi),
                     numberOfParameters=4,
                     rawFuncStr=r"$y = y_0 + A[\sin(\omega x+\phi)]$",
                     unicodeFuncStr="y = y\u2080 + A sin(\u03C9x+\u03D5)",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$\omega$',r'$\phi$'],
                     unicodeParametersStr=['y\u2080','A','\u03C9','\u03D5']),

            'Square wave':
            Function(name='Square wave',
                     func=lambda x,y0,A,omg,phi: y0 + A*np.sign(np.sin(omg*x+phi)),
                     numberOfParameters=4,
                     rawFuncStr=r"$y = y_0 + A\/signum[\sin(\omega x+\phi)]$",
                     unicodeFuncStr="y = y\u2080 + A signum[sin(\u03C9x+\u03D5)]",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$\omega$',r'$\phi$'],
                     unicodeParametersStr=['y\u2080','A','\u03C9','\u03D5']),

            'Gaussian':
            Function(name='Gaussian',
                     func=lambda x,y0,A,mu,sig: y0 + (A/(sig*np.sqrt(2*np.pi)))*np.exp((-1/2)*((x-mu)/sig)**2),
                     numberOfParameters=4,
                     rawFuncStr=r"$y = y_0 + \frac{A}{\sigma \sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$",
                     unicodeFuncStr="y = y\u2080 + A/[\u03C3 \u221A(2\u03C0)] \u00D7 e^[-(x-\u03BC)\u00B2/(2\u03C3\u00B2)]",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$\mu$',r'$\sigma$'],
                     unicodeParametersStr=['y\u2080','A','\u03BC','\u03C3']),

            'Poisson':
            Function(name='Poisson',
                     func=lambda x,y0,A,lmd: y0 + A*(np.exp(-lmd))*(lmd**x)/sp.gamma(x),
                     numberOfParameters=3,
                     rawFuncStr=r"$y = y_0 + A\/\frac{e^{-\lambda}\lambda^x}{x!}$",
                     unicodeFuncStr="y = y\u2080 + A [(e^\u03BB)(\u03BB^x)]/x!",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$\lambda$'],
                     unicodeParametersStr=['y\u2080','A','\u03BB']),

            'Laplacian':
            Function(name='Laplacian',
                     func=lambda x,y0,A,mu,b: y0 + (A/(2*b))*np.exp(-np.abs(x-mu)/b),
                     numberOfParameters=4,
                     rawFuncStr=r"$y = y_0 + \frac{A}{2b}e^{-\frac{|x-\mu|}{b}}$",
                     unicodeFuncStr="y = y\u2080 + A/(2b) \u00D7 e^(-|(x-\u03BC)|/b)",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$\mu$',r'$b$'],
                     unicodeParametersStr=['y\u2080','A','\u03BC','b']),

            'Lorentzian':
            Function(name='Lorentzian',
                     func=lambda x,y0,A,x0,omg: y0 + (2*A/np.pi)*(omg/(4*(x-x0)**2+omg**2)),
                     numberOfParameters=4,
                     rawFuncStr=r"$y = y_0 + \frac{2A}{\pi}\frac{\omega}{4(x-x_0)^2+\omega^2}$",
                     unicodeFuncStr="y = y\u2080 + (2A/\u03C0) \u00D7 (\u03C9/[4(x-x\u2080)\u00B2+\u03C9\u00B2])",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$x_0$',r'$\omega$'],
                     unicodeParametersStr=['y\u2080','A','x\u2080','\u03C9']),

            'Power':
            Function(name='Power',
                     func=lambda x,A,b: A*(x)**b,
                     numberOfParameters=2,
                     rawFuncStr=r"$y = Ax^b$",
                     unicodeFuncStr="y = A x\u1D47",
                     rawParametersStr=[r'$A$',r'$b$'],
                     unicodeParametersStr=['A','b']),

            'Exponential':
            Function(name='Exponential',
                     func=lambda x,y0,A,b: y0 + A*np.exp(b*x),
                     numberOfParameters=3,
                     rawFuncStr=r"$y = y_0 + A\/e^{bx}$",
                     unicodeFuncStr="y = y\u2080 + A e^(bx)",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$b$'],
                     unicodeParametersStr=['y\u2080','A','b']),

            'Logarithm':
            Function(name='Logarithm',
                     func=lambda x,y0,A,x0: y0 + A*np.log(x-x0),
                     numberOfParameters=3,
                     rawFuncStr=r"$y = y_0 + A\/log(x-x_0)$",
                     unicodeFuncStr="y = y\u2080 + A log(x-x\u2080)",
                     rawParametersStr=[r'$y_0$',r'$A$',r'$x_0$'],
                     unicodeParametersStr=['y\u2080','A','x\u2080'])
            }

####################################################################################
#                                  GLOBAL VARIABLES                                #
####################################################################################

#DATA RELATED VARIABLES
data = []           #holds the data from data file
x = []              #holds the x values from the data file
y = []              #holds the y values from the data file
y_err = []          #holds the y errors, either from user file or generated
ERR = bool          #boolean to check if data file contains errors
numberOfDataPoints = int    #holds the number of points in the data file
#FIT FUNCTION RELATED VARIABLES
function = ''       #string holding the function to fit to
numberOfParameters = int    #holds the number of parameters of the fitting function
#FITTING VARIABLES
fitStructure = []       #holds the fitting information from curve_fit/polyfit
fitParameters = []      #holds the fitting parameters
fitErrors = []          #holds the errors on the fitting parameters
chiSquared = float      #holds the final chi-squared value of the fit
redChiSquared = float   #holds the final reduced chi-squared value of the fit
redChiSquaredLimits = []    #holds the "acceptable range" of reduced chi-squared

####################################################################################
#                                 READING USER FILE                                #
####################################################################################
'''
This function tries to read the file held at fileLocation and sets the global
variables that hold all the information about the data set.
@Arguments:
fileLocation - string containing the location of the file chosen by user.
@Return value:
Returns an integer that specifies success (0) or failure (non 0) of the function.
'''
def readFile(fileLocation):

    #Access to global variables
    global data
    global x
    global y
    global y_err
    global ERR
    global numberOfDataPoints

    #Checking if the file string is empty
    if(fileLocation == ''):
        return 1

    #Checking if the file is a .txt or .csv file
    if(not fileLocation.endswith('.txt') and not fileLocation.endswith('.csv') ):
        return 2

    #Trying to populate the data array from the file (allows both spaces and commas)
    try:
        with open(fileLocation, 'r') as file:
            clean_lines = [' '.join(line.split()) for line in file]
            for delims in [(' ,',','),(', ',','),(' ',',')]:
                clean_lines = [line.replace(*delims) for line in clean_lines]
            data = np.genfromtxt(clean_lines, delimiter=',',dtype='float_')
    except (TypeError, ValueError, AttributeError):
        return 3

    #Checking if the data array has 2 or 3 columns
    try:
        if(not len(data[0])==2 and not len(data[0])==3):
            return 4
    except TypeError:
        return 4

    #Checking if there are any NaN's or Inf's in the data
    if(np.any(np.isnan(data)) or np.any(np.isinf(data))):
        return 5

    #Checking if the errors are all positive number
    if(len(data[0])==3 and np.any(data[:,2]<=0)):
        return 6

    #Setting global variables
    numberOfDataPoints = len(data)
    data = data[data[:,0].argsort()]    #Sorting the array in ascending order along x column
    x = data[:,0]
    y = data[:,1]

    #Checking if error along y axis has been provided
    if(len(data[0])==2):
        y_err = np.array([1 for i in data])   #Constant error to aid in best chi-squared estimate
        ERR = False
    elif(len(data[0])==3):
        y_err = data[:,2]
        ERR = True

    #All ran correctly!
    return 0

####################################################################################
#                             FIT - RELATED FUNCTIONS                              #
####################################################################################
'''
This function calculates the chi-squared against the data set given specific
values of fitting function parameters.
@Arguments:
params - array containing parameters of the fitting function to calculate chi-squared
against
@Return value:
Returns chi-squared as a float.
'''
def calcChiSquared(params):

    #Access to global variables
    global function
    global x
    global y
    global y_err

    #Returning chi-squared value for the given fitting function parameters
    return np.sum( ((y-functions[function].func(x,*params))/y_err)**2 )

'''
This function calculates the final chi-squared as well as reduced chi-squared
of the fit. It also calculates the acceptable range of reduced chi-squared based on
the chi-squared statistic.
@Arguments:
--
@Return value:
--
'''
def calcGoodnessOfFit():

    #Access to global variables
    global numberOfDataPoints
    global numberOfParameters
    global fitParameters
    global chiSquared
    global redChiSquared
    global redChiSquaredLimits

    #Calculating degrees of freedom
    degreesOfFreedom = numberOfDataPoints - numberOfParameters
    #Calculating chi-squared and reduced chi-squared
    chiSquared = calcChiSquared(fitParameters)
    redChiSquared = chiSquared/degreesOfFreedom

    #Calculating the "acceptable" range of reduced chi-squared
    pValues = [0.95,0.05]
    redChiSquaredLimits = stats.chi2.isf(pValues,degreesOfFreedom)/degreesOfFreedom

'''
This function provides an initial guess for the final fitting to take place in
fitFunction(). It comes into play when the user wants to fit the data automatically.
The initial guess is based on a two step procedure. It involves looking at the data:
1) and figuring out a single-valued "guess"
2) or figuring out bounds on the parameters and obtaining a guess from that by
global minimization of chi-squared using the scipy differential evolution algorithm.
@Arguments:
--
@Return value:
Returns an integer denoting success (0) or failure (non 0) of the function.
'''
def guessParameters():

    #Access to global variables
    global function
    global x
    global y
    global numberOfDataPoints

    #Useful quantities for parameter estimation
    xmin = min(x)
    xmax = max(x)
    ymin = min(y)
    ymax = max(y)

    #Empty array to store "initial guess"
    iniParameters = []

    #All the parameter estimation happens here

    if(function in ['Constant','Linear','Quadratic','Cubic','Quartic','Quintic']):

        order = numberOfParameters - 1
        iniParameters = np.polyfit(x,y,deg=order,w=1/y_err)

    elif(function=='Sine wave'):

        x_range = xmax - xmin
        y_range = ymax - ymin

        y0_bound = (ymin+2/5*abs(y_range),ymax-2/5*abs(y_range))
        A_bound = (abs(y_range)/3,2*abs(y_range)/3)
        phi_bound = (0,2*np.pi)

        y_avg = np.average(y)
        y_std = np.std(y)
        yscaled = []
        for i in y:
            if(i>y_avg+y_std):
                yscaled.append(1)
            elif(i<y_avg-y_std):
                yscaled.append(-1)
            else:
                yscaled.append(0)
        flag = yscaled[0]
        crossings = 0
        for i in yscaled:
            if(i==0):
                continue
            if(flag==0):
                flag=i
            elif(i==-flag):
                flag = -flag
                crossings+=1
        crossings = crossings/2
        guess_f = crossings/x_range

        omg_bound = (0.5*(2*np.pi)*guess_f,2*(2*np.pi)*guess_f)

        BOUNDS = [y0_bound, A_bound, omg_bound, phi_bound]
        BOUNDS = [np.sort(bound) for bound in BOUNDS]

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore')
            iniParameters = opt.differential_evolution(calcChiSquared,bounds=BOUNDS,seed=0).x

    elif(function=='Square wave'):

        x_range = xmax - xmin
        y_range = ymax - ymin

        y0_bound = (ymin+2/5*abs(y_range),ymax-2/5*abs(y_range))
        A_bound = (abs(y_range)/3,2*abs(y_range)/3)
        phi_bound = (0,2*np.pi)

        y_avg = np.average(y)
        y_std = np.std(y)
        yscaled = []
        for i in y:
            if(i>y_avg+y_std):
                yscaled.append(1)
            elif(i<y_avg-y_std):
                yscaled.append(-1)
            else:
                yscaled.append(0)
        flag = yscaled[0]
        crossings = 0
        for i in yscaled:
            if(i==0):
                continue
            if(flag==0):
                flag=i
            elif(i==-flag):
                flag = -flag
                crossings+=1
        crossings = crossings/2
        guess_f = crossings/x_range

        omg_bound = (0.5*(2*np.pi)*guess_f,2*(2*np.pi)*guess_f)

        BOUNDS = [y0_bound, A_bound, omg_bound, phi_bound]
        BOUNDS = [np.sort(bound) for bound in BOUNDS]

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore')
            iniParameters = opt.differential_evolution(calcChiSquared,bounds=BOUNDS,seed=0).x

    elif(function=='Gaussian'):

        x_range = xmax - xmin
        y_range = ymax - ymin

        mu_bound = (xmin-x_range,xmax+x_range)
        omg_bound = (0,x_range)

        A_bound1 = (abs(y_range)/3,2*abs(y_range)*2.5*x_range)
        y0_bound1 = (ymin-y_range,ymin+y_range/2)
        BOUNDS1 = [y0_bound1, A_bound1, mu_bound, omg_bound]
        BOUNDS1 = [np.sort(bound) for bound in BOUNDS1]

        A_bound2 = (-abs(y_range)/3,-2*abs(y_range)*2.5*x_range)
        y0_bound2 = (ymax-y_range/2,ymax+y_range)
        BOUNDS2 = [y0_bound2, A_bound2, mu_bound, omg_bound]
        BOUNDS2 = [np.sort(bound) for bound in BOUNDS2]

        BOUNDS_LIST = [BOUNDS1,BOUNDS2]

        with warnings.catch_warnings():
            warnings.filterwarnings('ignore')
            bestChiSquared = np.inf
            for BOUNDS in BOUNDS_LIST:
                tempParameters = opt.differential_evolution(calcChiSquared,bounds=BOUNDS,seed=0).x
                tempChiSquared = calcChiSquared(tempParameters)
                if(tempChiSquared < bestChiSquared):
                    bestChiSquared = tempChiSquared
                    iniParameters = tempParameters

    elif(function=='Poisson'):

        x_range = xmax - xmin
        y_range = ymax - ymin

        lmd_bound = (max(0,xmin-x_range),xmax+x_range)