text:
"""Predictor.."""
import os
import shutil
import json

import pandas as pd
import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt

from aircraft_detector.utils.utils import (
    retrieve_files,
    get_feature_directory_name,
    refresh_directory,
    print_verbose,
    load_spectrum_settings,
    load_state_settings,
)
import aircraft_detector.utils.feature_helper as fh
import aircraft_detector.utils.pytorch_earlystopping as es
from aircraft_detector.utils.dynamic_net import Net
import aircraft_detector.utils.plot_helper as ph

from aircraft_detector.utils.dynamic_net import (
    set_net_configuration,
    train_network,
    test_network,
    _create_network,
)

class EgoNoisePredictor:
    def __init__(
        self, root_directory, spectrum_settings=None, states_settings=None,
    ):
        # set root directory
        self._dir_root = root_directory
        # set the missing feature settings to their defaults
        if spectrum_settings is None:
            spectrum_settings = {}
        self._spectrum = load_spectrum_settings(spectrum_settings)
        # set the missing states settings to their defaults
        if states_settings is None:
            states_settings = {}
        self._states = load_state_settings(states_settings)

        # derive root input directory (feature dataset) from parameters
        self._dir_root_set = os.path.join(
            self._dir_root,
            "Ego-Noise Prediction",
            "Parameter Sets",
            get_feature_directory_name(self._spectrum),
        )
        # verbosity
        self.verbose = True
        self.super_verbose = False

        # parts of the dataset (initialized in load_datasets)
        self._train_set = None
        self._val_set = None
        self._test_set = None

        # network configuration (initialized in set_net_configuration)
        self._net_config = None

        # set loss to MSE loss
        self._loss_fn = nn.MSELoss()

        # train settings (supplied in train_model)
        self._train_settings = None

    def load_datasets(self):

        # load training, validation, test data
        self._train_set = self._load_data(
            os.path.join(self._dir_root_set, "Dataset", "Train")
        )
        self._val_set = self._load_data(
            os.path.join(self._dir_root_set, "Dataset", "Val")
        )
        self._test_set = self._load_data(
            os.path.join(self._dir_root_set, "Dataset", "Test")
        )

    def _load_data(self, dir_split):

        # load N files
        files_X = retrieve_files(os.path.join(dir_split, "States"))  # input
        files_Y = retrieve_files(os.path.join(dir_split, "Spectra"))  # output

        # load states: NxTxS
        data_X = [pd.read_csv(f, header=None).to_numpy().transpose() for f in files_X]
        # extract only relevant states
        data_X = [
            fh.extract_relevant_states(data, self._states["states"]) for data in data_X
        ]
        # load spectra: NxTxF
        data_Y = [pd.read_csv(f, header=None).to_numpy().transpose() for f in files_Y]

        if self._states["context_frames"] > 0:
            # add context to the dataset: (NxTxS, NxTxF) -> (NxT-CxCxS, NxT-CxCxF)
            data_X, data_Y = list(
                zip(*[self._add_context(dX, dY) for dX, dY in zip(data_X, data_Y)])
            )
        else:
            # add placeholder dim. for X: NxTxS -> NxTx1xS
            data_X = [np.expand_dims(X, 1) for X in data_X]

        # concatenate N and T axes to get 3D set
        data_X = np.concatenate(data_X, axis=0)
        data_Y = np.concatenate(data_Y, axis=0)
        # convert to torch dataset
        X = torch.from_numpy(data_X).float()
        Y = torch.from_numpy(data_Y).float()
        dataset = torch.utils.data.TensorDataset(X, Y)
        return dataset

    def _add_context(self, states, spectra=None):
        # 3D copy of 'original' state data: TxS -> Tx1xS
        states_extended = np.expand_dims(states, 1)

        # shift states in time dim., then add to extended array
        n = 0
        while n < self._states["context_frames"]:
            # get states at previous time index
            states_prev = np.roll(states, n + 1, axis=0)
            # add prev. states to 2nd axis of extended states
            states_extended = np.concatenate(
                (np.expand_dims(states_prev, 1), states_extended), axis=1
            )
            n += 1

        # remove first C time indices for causality: T-CxCxS
        states_extended = states_extended[self._states["context_frames"] :]

        if spectra is None:
            return states_extended
        else:
            # remove first C time indices to match length of states_extended
            spectra_modified = spectra[self._states["context_frames"] :]
            return states_extended, spectra_modified

    def set_net_configuration(self, layers):

        assert (
            self._test_set is not None
        ), "Please load the data via load_datasets before setting a network configuration."

        self._net_config = set_net_configuration(layers, self._test_set)

    def tune_hyperparameters(self, parameterization_dict, training_settings=None):
        """Use for Bayesian Optimization.

        parameterization_dict: contains ax ranges..
        """
        # convert the parameterization to the class config representation
        new_config = _convert_parameterization_to_config(parameterization_dict)
        self.set_net_configuration(new_config)
        # train, evaluate model
        _, losses, _ = self.train_network(training_settings)
        val_loss = losses[1]
        return val_loss

    def train_network(self, train_settings):

        # verify that network config has been set
        assert (
            self._net_config is not None
        ), "Please set a network configuration via set_net_configuration before training the network."

        # store train settings
        self._train_settings = train_settings
        # train network
        network, loss, loss_history = train_network(
            train_settings,
            self._train_set,
            self._val_set,
            self._net_config,
            self._loss_fn,
            self.verbose,
            self.super_verbose,
        )

        return network, loss, loss_history

    def test_network(self, network):

        loss = test_network(
            network, self._test_set, self._net_config["device"], self._loss_fn
        )

        return loss

    def save_network(self, network, loss, overwrite=False):

        # generate output filename and directory for model and config
        network_id = "%.6f_c%d" % (loss, self._states["context_frames"])
        dir_model = os.path.join(self._dir_root_set, "Models", network_id)
        fn_model = "enp_model.pt"
        fn_config = "enp_config.json"

        # create or overwrite directory
        if os.path.exists(dir_model) and not overwrite:
            print_verbose(self.verbose, "Network already exists.")
            return dir_model
        refresh_directory(dir_model)

        # save network
        torch.save(network.state_dict(), os.path.join(dir_model, fn_model))

        # save network config and settings
        config_file = open(os.path.join(dir_model, fn_config), "w")
        json.dump(
            [self._net_config, self._spectrum, self._states, self._train_settings],
            config_file,
        )
        config_file.close()

        return dir_model

    def save_network_output(self, model, dir_model, subset, plot=True):

        # refresh the output directories
        output_subdirs = ["Original", "Predicted", "Residual"]
        for subdir in output_subdirs:
            refresh_directory(os.path.join(dir_model, "Output", subset, subdir))

        # load the original files (states, spectra) in the subset
        dir_states = os.path.join(self._dir_root_set, "Dataset", subset, "States")
        files_states = retrieve_files(dir_states)
        dir_spectra = os.path.join(self._dir_root_set, "Dataset", subset, "Spectra")
        files_spectra = retrieve_files(dir_spectra)

        for i in range(len(files_states)):
            # load original spectra and cut-off context
            original = pd.read_csv(files_spectra[i], header=None).to_numpy()
            if self._states["context_frames"] > 0:
                original = original[:, self._states["context_frames"] :]
            # predict spectra from states file
            predicted = self._predict(model, files_states[i], original.shape)
            # compute residual
            residual = original - predicted

            # plot if desired
            if plot:
                self._plot_model_output(original, predicted, residual)

            # save output
            fn = os.path.split(files_states[i])[-1]  # target filename
            output_spectra = [original, predicted, residual]
            for spectrum, subdir in zip(output_spectra, output_subdirs):
                # save spectrum
                dir_out = os.path.join(dir_model, "Output", subset, subdir)
                pd.DataFrame(spectrum).to_csv(
                    os.path.join(dir_out, fn), index=False, header=False
                )

    def _predict(self, network, file_states, out_shape):
        # load states
        S = pd.read_csv(file_states, header=None).to_numpy().transpose()
        S = fh.extract_relevant_states(S, self._states["states"])
        # add context or placeholder dim
        if self._states["context_frames"] > 0:
            S = self._add_context(S)
        else:
            S = np.expand_dims(S, 1)

        # predict from states
        X = torch.from_numpy(S).float().to(self._net_config["device"])
        predictions = np.zeros(out_shape)
        with torch.no_grad():
            # set in eval mode
            network.eval()
            for i, x in enumerate(X):
                # forward pass (needs fake batch dim.)
                x = x.view(1, *x.size())
                yhat = network(x)
                # store tensor (remove extra dim)
                predictions[:, i] = yhat.squeeze().cpu()

        return predictions

    def _plot_model_output(self, original, predicted, residual):

        fig = plt.figure(figsize=(6, 6))
        # plot original
        plt.subplot(3, 1, 1)
        plt.title("Original spectrum")
        ph.plot_spectrum(original, self._spectrum, colorbar=False)
        # plot predicted
        plt.subplot(3, 1, 2)
        plt.title("Predicted spectrum")
        ph.plot_spectrum(predicted, self._spectrum, colorbar=False)
        # plot residual
        plt.subplot(3, 1, 3)
        plt.title("Residual spectrum")
        ph.plot_spectrum(
            residual, self._spectrum, colormap="coolwarm", colorbar=False,
        )
        fig.tight_layout()
        plt.show()
        fig.savefig("plottyplot.eps", format="eps")

    def load_network(self, network_id=None):
        if network_id is None:
            # load first network (i.e. lowest loss) of those saved
            dir_model = os.path.join(
                self._dir_root_set,
                "Models",
                sorted(os.listdir(os.path.join(self._dir_root_set, "Models")))[0],
            )
        else:
            dir_model = os.path.join(self._dir_root_set, "Models", network_id)
        # load model configuration
        fp_config = os.path.join(dir_model, "enp_config.json")
        # load class attritubtes from config file
        (
            self._net_config,
            self._spectrum,
            self._states,
            self._train_settings,
        ) = json.load(open(fp_config))

        # load model state
        fp_model = os.path.join(dir_model, "enp_model.pt")
        model = _create_network(self._net_config)
        model.load_state_dict(torch.load(fp_model))

        return model, dir_model

def _convert_parameterization_to_config(parameterization):
    """
    Example input:
    parameterization = {'LSTM_layers': 2,
        'LSTM_1_hidden_size_10': 10,
        'LSTM_1_dropout': 0.1,
        'LSTM_2_hidden_size_10': 12,
        'LSTM_3_hidden_size_10': 14,
        'Linear_layers': 2,
        'Linear_1_out_features_10': 10,
        'Dropout_1_p': 0.5,
        'Linear_2_out_features_10': 11,
        'Dropout_2_p': 0.5,
        'Linear_3_out_features_10': 12,
        'Dropout_3_p': 0.5,}

    Target output:
        layers = [
            {'layer_type': 'LSTM', 'hidden_size': 100, 'dropout': 0.1},
            {'layer_type': 'LSTM', 'hidden_size': 120},
            {'layer_type': 'Linear', 'out_features': 100},
            {'layer_type': 'Dropout', 'p': 0.5},
            {'layer_type': 'Linear', 'out_features': 110},
            {'layer_type': 'Dropout', 'p': 0.5},
            ]
    """
    # empty dictionary to store modified parameters
    parameters = {}
    # hard-coded to ensure number of dropout layers does not exceed linear layers
    parameters["Dropout_layers"] = parameterization.get("Linear_layers", 0)

    # get rid of the string indicating values should be multiplied by 10
    for key, value in parameterization.items():
        if "_10" in key:
            fixed_value = 10 * value
            fixed_key = key.replace("_10", "")
            parameters[fixed_key] = fixed_value
        else:
            parameters[key] = value

    # keep track of max. no of layers
    num_layers = {}
    layers = []
    for key, value in parameters.items():
        attributes = key.split("_")
        layer_type = attributes[0]  # i.e. 'Linear'

        if attributes[1] == "layers":
            # number of layers of this type
            num_layers[layer_type] = value
        elif int(attributes[1]) <= num_layers[layer_type]:
            # add to layers if layer number is in range
            layer_name = "_".join([attributes[0], attributes[1]])
            layer_rest = key.replace(layer_name, "")[1:]

            # merge layer dictionaries with the same keys
            layer_exists = False
            for layer in layers:
                if layer_name == layer.get("layer_type"):
                    layer_exists = True
                    break
            if layer_exists:
                layer[layer_rest] = value
            else:
                layers.append({"layer_type": layer_name, layer_rest: value})

    for layer in layers:
        # remove the trailing numbers
        layer["layer_type"] = layer["layer_type"].split("_")[0]

    return layers


text:
from . import Executable
from .join_feature import JoinFeature

class FourTi2Executable(Executable):
    r"""
    Feature for the 4ti2 executables.
    """
    def __init__(self, name):
        r"""
        TESTS::

            sage: from sage.features.four_ti_2 import FourTi2Executable
            sage: isinstance(FourTi2Executable('hilbert'), FourTi2Executable)
            True
        """
        from sage.env import SAGE_ENV
        Executable.__init__(self,
                            name="4ti2-" + name,
                            executable=SAGE_ENV.get("FOURTITWO_" + name.upper(), None) or name,
                            spkg="4ti2")

class FourTi2(JoinFeature):
    r"""
    A :class:`sage.features.Feature` describing the presence of the ``4ti2`` executables.

    EXAMPLES::

        sage: from sage.features.four_ti_2 import FourTi2
        sage: FourTi2().is_present()  # optional - 4ti2
        FeatureTestResult('4ti2', True)
    """
    def __init__(self):
        r"""
        TESTS::

            sage: from sage.features.four_ti_2 import FourTi2
            sage: isinstance(FourTi2(), FourTi2)
            True
        """
        JoinFeature.__init__(self, '4ti2',
                             [FourTi2Executable(x)
                              # same list is tested in build/pkgs/4ti2/spkg-configure.m4
                              for x in ('hilbert', 'markov', 'graver', 'zsolve', 'qsolve',
                                        'rays', 'ppi', 'circuits', 'groebner')])

ext: py

reason: from sage


text:
#File: gini_plot.py
#Created: Mon 12 Mar 2012 03:00:01 PM CDT
#Last Change: Mon 12 Mar 2012 03:49:05 PM CDT
#Author: Steven Boada

import numpy as np
import pylab as plt

gini = np.loadtxt('gdss_e4_f160_m25.morph')
icd = np.loadtxt('icd_1.5_3.5_gsd_full_ston.txt')

f1 = plt.figure(1,figsize=(8,8))
f1s1 = f1.add_subplot(111)
f2 = plt.figure(2,figsize=(8,8))
f2s1 = f2.add_subplot(111)
f3 = plt.figure(3,figsize=(8,8))
f3s1 = f3.add_subplot(111)

for i in range(len(icd)):
	for j in range(len(gini)):
		if icd[i][0] == gini[j][0]:
			if icd[i][11] > 30.0: # STON_I
				if icd[i][9]> 0.1:
					f1s1.plot(gini[i][20],gini[i][19],
					c='#FE4C00',marker='s',markersize=8)
				elif icd[i][9]>0.05:
					f1s1.plot(gini[i][20],gini[i][19],
					c='#FFB236',marker='o',markersize=7)
                		elif icd[i][9]>0.01:
					f1s1.plot(gini[i][20],gini[i][19],
					c='#BD83F7',marker='o',markersize=7)
                		else:
					f1s1.plot(gini[i][20],gini[i][19],
					c='#03469E',marker='o',markersize=7)
        		elif 20.0 < icd[i][11] and icd[i][11] < 30.0: # STON_I
				f1s1.plot(gini[i][20],gini[i][19],
				c='0.8',marker='s',alpha=0.4)
        		else:
				f1s1.plot(gini[i][20],gini[i][19],
				c='0.8',marker='.',alpha=0.4)

			if icd[i][15] > 30.0: # STON_Z
				if icd[i][9]> 0.1:
                        		f2s1.plot(gini[i][20],gini[i][19],
					c='#FE4C00',marker='s',markersize=8)
                		elif icd[i][9]>0.05:
					f2s1.plot(gini[i][20],gini[i][19],
					c='#FFB236',marker='o',markersize=7)
                		elif icd[i][9]>0.01:
					f2s1.plot(gini[i][20],gini[i][19],
					c='#BD83F7',marker='o',markersize=7)
                		else:
					f2s1.plot(gini[i][20],gini[i][19],
					c='#03469E',marker='o',markersize=7)
        		elif 20.0 < icd[i][15] and icd[i][15] < 30.0: # STON_Z
				f2s1.plot(gini[i][20],gini[i][19],
					c='0.8',marker='s',alpha=0.4)
        		else:
				f2s1.plot(gini[i][20],gini[i][19],
					c='0.8',marker='.',alpha=0.4)

			if icd[i][19] > 30.0: # STON_J
				if icd[i][9]> 0.1:
                        		f3s1.plot(gini[i][20],gini[i][19],
						c='#FE4C00',marker='s',markersize=8)
                		elif icd[i][9]>0.05:
					f3s1.plot(gini[i][20],gini[i][19],
						c='#FFB236',marker='o',markersize=7)
                		elif icd[i][9]>0.01:
					f3s1.plot(gini[i][20],gini[i][19],
						c='#BD83F7',marker='o',markersize=7)
                		else:
					f3s1.plot(gini[i][20],gini[i][19],
						c='#03469E',marker='o',markersize=7)
        		elif 20.0 < icd[i][19] and icd[i][19] < 30.0: # STON_J
				f3s1.plot(gini[i][20],gini[i][19],
					c='0.8',marker='s',alpha=0.4)
        		else:
				f3s1.plot(gini[i][20],gini[i][19],
					c='0.8',marker='.',alpha=0.4)

m1 = np.arange(-3.0,0.0,0.1)
m2 = np.arange(-3.0,-1.68,0.1)

plt.figure(1)
f1s1.plot(m1,-0.14*m1+0.33,'--',color='g')
f1s1.plot(m2,0.14*m2+0.80,'.',color='r')

f1s1.set_xlim(-3,0)
f1s1.set_ylim(0.3,0.8)
ax = plt.gca()
ax.set_xlim(ax.get_xlim()[::-1])
f1s1.tick_params(axis='both',pad=7)
f1s1.set_xlabel(r"$M_{20}$",fontsize=20)
f1s1.set_ylabel("G",fontsize=20)
f1s1.legend()

plt.figure(2)
f2s1.plot(m1,-0.14*m1+0.33,'--',color='g')
f2s1.plot(m2,0.14*m2+0.80,'.',color='r')

f2s1.set_xlim(-3,0)
f2s1.set_ylim(0.3,0.8)
ax = plt.gca()
ax.set_xlim(ax.get_xlim()[::-1])
f2s1.tick_params(axis='both',pad=7)
f2s1.set_xlabel(r"$M_{20}$",fontsize=20)
f2s1.set_ylabel("G",fontsize=20)

plt.figure(3)
f3s1.plot(m1,-0.14*m1+0.33,'--',color='g')
f3s1.plot(m2,0.14*m2+0.80,'.',color='r')

f3s1.set_xlim(-3,0)
f3s1.set_ylim(0.3,0.8)
ax = plt.gca()
ax.set_xlim(ax.get_xlim()[::-1])
f3s1.tick_params(axis='both',pad=7)
f3s1.set_xlabel(r"$M_{20}$",fontsize=20)
f3s1.set_ylabel("G",fontsize=20)

plt.show()

max_stars_repo_name: boada/ICD

max_issues_repo_name: boada/ICD

max_forks_repo_name: boada/ICD

reason: import numpy


text:
#!/usr/bin/env python3
# coding: utf-8
from magicgui import magic_factory, widgets
import numpy as np
from skimage import feature
from napari.types import LabelsData, PointsData
import napari
from typing_extensions import Annotated

@magic_factory
def make_point_cloud_sampling(
    viewer: "napari.viewer.Viewer",
    input: LabelsData,
    sampling_frequency: Annotated[int, {"min": 1, "max": 100, "step": 1}]=5,
    sigma: Annotated[float, {"min": 0, "max": 10, "step": 0.1}]=1.0,
    face_color: Annotated[str, {"choices": ["red", "green", "blue", "yellow"]}]='red',
    point_size: Annotated[int, {"min": 1, "max": 20, "step": 1}]=5
):
    from napari.qt import thread_worker
    pbar = widgets.ProgressBar()
    pbar.range = (0, 0)  # unknown duration
    make_point_cloud_sampling.insert(0, pbar)  # add progress bar to the top of widget

    # this function will be called after we return
    def _add_data(return_value, self=make_point_cloud_sampling):
        data, kwargs = return_value
        viewer.add_points(data, **kwargs)
        self.pop(0).hide()  # remove the progress bar

    @thread_worker(connect={"returned": _add_data})
    def _point_cloud_sampling(input: LabelsData,
                              sampling_frequency: float=0.01,
                              sigma: float=1.0,
                              edge_color: str='black',
                              face_color: str='red',
                              point_size: int=5):
        point_lst = []
        for z in range(input.shape[0]):
            img = (input[z] > 0).astype('uint8') * 255
            img = feature.canny(img, sigma=sigma)
            points = np.where(img == 1)
            # Just keep values at every n-th position
            for i in range(len(points[0])):
                if np.random.rand() < sampling_frequency:
                    point_lst.append([z, points[0][i], points[1][i]])

        kwargs = dict(
            edge_color=edge_color,
            face_color=face_color,
            size=point_size
        )

        return (np.asarray(point_lst), kwargs)

    _point_cloud_sampling(input=input,
                          sampling_frequency=sampling_frequency / 100,
                          sigma=sigma,
                          face_color=face_color,
                          point_size=point_size)

max_stars_repo_name: krentzd/napari-test

max_issues_repo_name: krentzd/napari-test

max_forks_repo_name: krentzd/napari-test


text:
# -*- coding: utf-8 -*-
'''
#-------------------------------------------------------------------------------
# NATIONAL UNIVERSITY OF SINGAPORE - NUS
# SINGAPORE INSTITUTE FOR NEUROTECHNOLOGY - SINAPSE
# Singapore
# URL: http://www.sinapseinstitute.org
#-------------------------------------------------------------------------------
# Neuromorphic Engineering Group
# Author: Andrei Nakagawa-Silva, MSc
# Contact: nakagawa.andrei@gmail.com
#-------------------------------------------------------------------------------
# Description: GUI for controlling the UR10
# With this GUI it is possible to create a text file containing several positions
# or joint angles that can be used to generate standard UR10 poses
#-------------------------------------------------------------------------------
'''
#-------------------------------------------------------------------------------
import os, sys, glob
sys.path.append('../shape_recognition/libraries/general')
sys.path.append('../shape_recognition/libraries/iLimb')
sys.path.append('../shape_recognition/libraries/UR10')
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
from PyQt5.QtWidgets import QFileDialog #file management
import numpy as np #numpy
from UR10 import * #UR10 libraries
#-------------------------------------------------------------------------------
Ui_MainWindow, QMainWindow = loadUiType('formUR10_gui.ui')
#-------------------------------------------------------------------------------
class FormUR10(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(FormUR10,self).__init__()
        self.setupUi(self)
        self.UR10Cont = None #object that will be used for controlling the UR10
        #create the pose manager object
        #object that will be used for managing poses
        self.UR10PoseManager = URPoseManager()
        #timer to update the GUI
        self.timer = QTimer()
        self.timer.setInterval(200)
        self.timer.timeout.connect(self.update)
        #create a list with the keys -> name of positions or joints
        self.dictKeys = []
        #dictionary containing the stored positions or joints
        self.dictPosJoints = dict()
        self.init() #initialize the GUI

    #initalize the GUI
    def init(self):
        #create the events
        #connect to UR10
        self.btnConnect.clicked.connect(self.doConnect)
        #move UR10
        self.btnMove.clicked.connect(self.doMove)
        #move joints
        self.btnMoveJoints.clicked.connect(self.doMoveJoints)
        #add a new position to the list
        self.btnAddPosition.clicked.connect(self.doAddPos)
        #add a new set of joints to the list
        self.btnAddJoint.clicked.connect(self.doAddJoints)
        #save the pose file
        self.btnSave.clicked.connect(self.doSave)
        #update the positions and joints
        self.btnUpdatePosJoints.clicked.connect(self.doUpdatePosJoints)
        #create an array with all the textbox
        self.tbCont = [self.tbX,self.tbY,self.tbZ,self.tbRx,self.tbRy,self.tbRz]
        self.tbJoints = [self.tbBase,self.tbShoulder,self.tbElbow,self.tbWrist1,self.tbWrist2,self.tbWrist3]
        #sets the initial position values to '0'
        [x.setText('0') for x in self.tbCont]
        #sets the initial joint values to '0'
        [x.setText('0') for x in self.tbJoints]
        #set default time to 10 s
        self.tbTime.setText('10')
        #define the most likely ip anda port
        self.tbIP.setText('10.1.1.6') #ip
        self.tbPort.setText('30002') #port_send

    #connect to the UR10
    def doConnect(self):
        #create the controller object
        #self.UR10Cont = UR10Controller(int(self.tbPort.text()),self.tbIP.text(),1024)
        self.UR10Cont = UR10Controller(self.tbIP.text(),port_send=int(self.tbPort.text()))
        #connect to the UR10
        self.UR10Cont.connect()
        #read positions and joints for the first time to populate the textbox
        self.UR10Cont.read_joints_and_xyzR()
        #populate the positions
        for i in range(len(self.tbCont)):
            self.tbCont[i].setText(str(self.UR10Cont.xyzR[i]))
        #populate the joints
        for i in range(len(self.tbJoints)):
            self.tbJoints[i].setText(str(self.UR10Cont.joints[i]))
        #wait and then start reading continuously
        time.sleep(0.01)
        #if connected, activate the reading function
        self.timer.start()
        #debugging
        #print(self.UR10Cont.ip,self.UR10Cont.port)

    #move the UR10
    def doMove(self):
        #check if the UR10 controller object has been instantiated
        if self.UR10Cont is not None:
            #retrieve the coordinates
            x = float(self.tbX.text())
            y = float(self.tbY.text())
            z = float(self.tbZ.text())
            #retrieve the orientation of the end-effector
            rx = float(self.tbRx.text())
            ry = float(self.tbRy.text())
            rz = float(self.tbRz.text())
            #time duration of the movement
            t = float(self.tbTime.text())
            #create the pose vector
            posevec = [x,y,z,rx,ry,rz]
            #move the UR10
            self.UR10Cont.movej(posevec,t)
            print('moving...')
            #debugging
            print(posevec)
        else:
            return False

    #move UR10 to specified joints
    def doMoveJoints(self):
        if self.UR10Cont is not None:
            t = 10 #always 20 seconds for now
            j = [0]*len(self.tbJoints)
            for k in range(len(self.tbJoints)):
                j[k] = np.deg2rad(float(self.tbJoints[k].text()))
            self.UR10Cont.movejoint(j,t)
            print(j) #debugging

    #add position to the list
    def doAddPos(self):
        if self.UR10PoseManager is not None:
            pose = []
            #create the pose vector with the values contained in the
            #textbox related to position and orientation of the UR10
            [pose.append(float(x.text())) for x in self.tbCont]
            #retrieve the name of the position
            posname = self.tbName.text()
            #add the position to the pose manager
            self.UR10PoseManager.addPosition(posname,pose)
            #saving the position
            print('saving the position:', pose)
            #add the position saved to the list box containing the positions saved
            self.updatePoses()

    #add joint to the list
    def doAddJoints(self):
        if self.UR10PoseManager is not None:
            joints = []
            #create the joints vector with the values contained in the
            #textbox related to the joints of the UR10
            [joints.append(np.deg2rad(float(x.text()))) for x in self.tbJoints]
            #retrieve the name of the position
            posname = self.tbName.text()
            #add the position to the pose manager
            self.UR10PoseManager.addJoint(posname,joints)
            #saving the position
            print('saving the joints:', joints)
            #add the position saved to the list box containing the positions saved
            self.updatePoses()

    #save the positions or joints stored in the dictionary to a
    #file
    def doSave(self):
        if self.UR10PoseManager is not None:
            #open a save file dialog
            options = QFileDialog.Options()
            options |= QFileDialog.DontUseNativeDialog
            #open the dialog and receives the filename
            diag = QFileDialog()
            filename,_ = diag.getSaveFileName(None,'Title','','URPose (*.urpose)')
            #saves the poses
            #if clicked 'Cancel', then string will be False
            if filename:
                self.UR10PoseManager.save(filename) #saving

    def updatePoses(self):
        #clear the list
        self.lbPoses.clear()
        #get all the keys
        keys = self.UR10PoseManager.getPoseNames()
        for k in range(len(keys)):
            #get the positions/joints for a given key (name)
            value = self.UR10PoseManager.getPosJoint(keys[k])
            #add to the list of poses
            self.lbPoses.addItem(str(keys[k]) + ' ' + str(value))

    #read from the UR10 and update the GUI
    def update(self):
        #check if the controller object has been instantiated
        if self.UR10Cont is not None:
            self.UR10Cont.read_joints_and_xyzR()
            #print(self.UR10Cont.xyzR) #debugging
            strposjoints = ''
            strposjoints += 'X: ' + str(self.UR10Cont.xyzR[0]) + '\n'
            strposjoints += 'Y: ' + str(self.UR10Cont.xyzR[1]) + '\n'
            strposjoints += 'Z: ' + str(self.UR10Cont.xyzR[2]) + '\n'
            strposjoints += 'RX: ' + str(self.UR10Cont.xyzR[3]) + '\n'
            strposjoints += 'RY: ' + str(self.UR10Cont.xyzR[4]) + '\n'
            strposjoints += 'RZ: ' + str(self.UR10Cont.xyzR[5]) + '\n'
            strposjoints += 'Base: ' + str(self.UR10Cont.joints[0]) + '\n'
            strposjoints += 'Shoulder: ' + str(self.UR10Cont.joints[1]) + '\n'
            strposjoints += 'Elbow: ' + str(self.UR10Cont.joints[2]) + '\n'
            strposjoints += 'Wrist 1: ' + str(self.UR10Cont.joints[3]) + '\n'
            strposjoints += 'Wrist 2: ' + str(self.UR10Cont.joints[4]) + '\n'
            strposjoints += 'Wrist 3: ' + str(self.UR10Cont.joints[5]) + '\n'
            self.tbStatus.setPlainText(strposjoints)

    def doUpdatePosJoints(self):
        #read positions and joints for the first time to populate the textbox
        self.UR10Cont.read_joints_and_xyzR()
        #populate the positions
        for i in range(len(self.tbCont)):
            self.tbCont[i].setText(str(self.UR10Cont.xyzR[i]))
        #populate the joints
        for i in range(len(self.tbJoints)):
            self.tbJoints[i].setText(str(self.UR10Cont.joints[i]))
#-------------------------------------------------------------------------------
#Run the app
if __name__ == '__main__':
    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    app = QtWidgets.QApplication(sys.argv)
    main = FormUR10()
    main.show()
    sys.exit(app.exec_())


text:
import numpy as np
from AutoDiff.forwardNode import ForwardNode
from AutoDiff.reverseNode import ReverseNode

__all__ = ['sin', 'cos', 'log', 'exp', 'sqrt', 'tan', 'cot', 'sec', 'csc',
           'arctan', 'arcsin', 'arccos', 'tanh', 'sinh', 'cosh', 'log_base']

def exp(node):
    '''
    Compute the exponent of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after taking exponent

    Examples:
    >>> x = ForwardNode(3, trace=1, var=['x'])
    >>> exp(x)
    ForwardNode Variable: ['x'],  Value: 20.085536923187668, Trace: [20.08553692]

    '''
    if isinstance(node, (int, float)):
        return np.exp(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.exp(node.value), node.trace * np.exp(node.value), node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.exp(node.value))
        node.children.append((np.exp(node.value), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def log(node):
    '''
    Compute the log of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after taking log

    Examples:
    >>> x = ForwardNode(3, trace=1, var=['x'])
    >>> log(x)
    ForwardNode Variable: ['x'],  Value: 1.0986122886681098, Trace: [0.33333333]

    '''
    if isinstance(node, (int, float)):
        if node <= 0:
            raise ValueError("Invalid inpput: cannot take log for value <= 0")
        return np.log(node)
    elif isinstance(node, ForwardNode):
        if node.value <= 0:
            raise ValueError("Invalid inpput: cannot take log for value <= 0")
        return ForwardNode(np.log(node.value), node.trace / node.value, node.var)
    elif isinstance(node, ReverseNode):
        if node.value <= 0:
            raise ValueError("Invalid inpput: cannot take log for value <= 0")
        new = ReverseNode(np.log(node.value))
        node.children.append((1/node.value, new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def sqrt(node):
    '''
    Compute the square root of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after taking the square root

    Examples:
    >>> x = ForwardNode(4, trace=1, var=['x'])
    >>> sqrt(x)
    ForwardNode Variable: ['x'],  Value: 2.0, Trace: [0.25]

    '''
    if isinstance(node, (int, float)):
        if node < 0:
            raise ValueError(f"Invalid Value: cannot calculate square root of {node}.")
        else:
            return np.sqrt(node)
    elif isinstance(node, ForwardNode):
        if node.value < 0:
            raise ValueError(f"Invalid Value: cannot calculate square root of {node.value}.")
        else:
            return ForwardNode(node.value ** 0.5, node.trace * 0.5 * node.value ** (-0.5), node.var)
    elif isinstance(node, ReverseNode):
        if node.value < 0:
            raise ValueError(f"Invalid Value: cannot calculate square root of {node.value}.")
        else:
            new = ReverseNode(node.value ** 0.5)
            node.children.append((0.5 * node.value ** (-0.5), new))
            return new
    else:
        raise AttributeError("Invalid Input!")

def sin(node):
    '''
    Compute the sine of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after sin operation

    Examples:
    >>> x = ForwardNode(np.pi, trace=1, var=['x'])
    >>> sin(x)
    ForwardNode Variable: ['x'],  Value: 1.2246467991473532e-16, Trace: [-1.]

    '''
    if isinstance(node, (int, float)):
        return np.sin(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.sin(node.value), node.trace * np.cos(node.value), node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.sin(node.value))
        node.children.append((np.cos(node.value), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def cos(node):
    '''
    Compute the cosine of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after cos operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> cos(x)
    ForwardNode Variable: ['x'],  Value: 1.0, Trace: [-0.]

    '''
    if isinstance(node, (int, float)):
        return np.cos(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.cos(node.value), -1.0 * node.trace * np.sin(node.value), node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.cos(node.value))
        node.children.append((-np.sin(node.value), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def tan(node):
    '''
    Compute the tangent of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after tan operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> tan(x)
    ForwardNode Variable: ['x'],  Value: 0.0, Trace: [1.]

    '''
    if isinstance(node, (int, float)):
        if node % (np.pi / 2) == 0 and node % np.pi != 0:
            raise ValueError(f"Invalid input: derivative for tangent of {node} doesn't exist")
        return np.tan(node)
    elif isinstance(node, ForwardNode):
        if node.value % (np.pi / 2) == 0 and node.value % np.pi != 0:
            raise ValueError(f"Invalid input: derivative for tangent of {node.value} doesn't exist")
        return ForwardNode(np.tan(node.value), node.trace / np.cos(node.value) ** 2, node.var)
    elif isinstance(node, ReverseNode):
        if node.value % (np.pi / 2) == 0 and node.value % np.pi != 0:
            raise ValueError(f"Invalid input: derivative for tangent of {node.value} doesn't exist")
        new = ReverseNode(np.tan(node.value))
        node.children.append((1.0 / np.cos(node.value) ** 2, new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def cot(node):
    '''
    Compute the cotangent of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after cot operation

    Examples:
    >>> x = ForwardNode(np.pi / 2, trace=1, var=['x'])
    >>> cot(x)
    ForwardNode Variable: ['x'],  Value: 6.123233995736766e-17, Trace: [-1.]

    '''
    if isinstance(node, (int, float)):
        if node % np.pi == 0:
            raise ValueError(f"Invalid Value: cotangent of {node} does not exist.")
        return 1 / np.tan(node)
    elif isinstance(node, ForwardNode):
        if node.value % np.pi == 0:
            raise ValueError(f"Invalid Value: cotangent of {node.value} does not exist.")
        return ForwardNode(1 / np.tan(node.value), node.trace * (-1.0) / np.sin(node.value) ** 2, node.var)
    elif isinstance(node, ReverseNode):
        if node.value % np.pi == 0:
            raise ValueError(f"Invalid Value: cotangent of {node.value} does not exist.")
        new = ReverseNode(1 / np.tan(node.value))
        node.children.append((-1.0 / np.sin(node.value) ** 2, new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def sec(node):
    '''
    Compute the secant of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after sec operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> sec(x)
    ForwardNode Variable: ['x'],  Value: 1.0, Trace: [0.]

    '''
    if isinstance(node, (int, float)):
        if node % (np.pi / 2) == 0 and node % np.pi != 0:
            raise ValueError(f"Invalid Value: secant of {node} does not exist.")
        return 1 / np.cos(node)
    elif isinstance(node, ForwardNode):
        if node.value % (np.pi / 2) == 0 and node.value % np.pi != 0:
            raise ValueError(f"Invalid Value: secant of {node.value} does not exist.")
        return ForwardNode(1 / np.cos(node.value), node.trace * np.sin(node.value) / np.cos(node.value) ** 2, node.var)
    elif isinstance(node, ReverseNode):
        if node.value % (np.pi / 2) == 0 and node.value % np.pi != 0:
            raise ValueError(f"Invalid Value: secant of {node.value} does not exist.")
        new = ReverseNode(1 / np.cos(node.value))
        node.children.append((np.sin(node.value) / np.cos(node.value) ** 2, new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def csc(node):
    '''
    Compute the cosecant of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after csc operation

    Examples:
    >>> x = ForwardNode(np.pi / 2, trace=1, var=['x'])
    >>> csc(x)
    ForwardNode Variable: ['x'],  Value: 1.0, Trace: [-6.123234e-17]

    '''
    if isinstance(node, (int, float)):
        if node % np.pi == 0:
            raise ValueError(f"Invalid Value: cosecant of {node} does not exist.")
        return 1 / np.sin(node)
    elif isinstance(node, ForwardNode):
        if node.value % np.pi == 0:
            raise ValueError(f"Invalid Value: cosecant of {node.value} does not exist.")
        return ForwardNode(1 / np.sin(node.value), node.trace * (-1.0) * np.cos(node.value) / np.sin(node.value) ** 2,
                           node.var)
    elif isinstance(node, ReverseNode):
        if node.value % np.pi == 0:
            raise ValueError(f"Invalid Value: cosecant of {node.value} does not exist.")
        new = ReverseNode(1 / np.sin(node.value))
        node.children.append((-1.0 * np.cos(node.value) / np.sin(node.value) ** 2, new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def arcsin(node):
    '''
    Compute the arcsine of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after arcsin operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> arcsin(x)
    ForwardNode Variable: ['x'],  Value: 0.0, Trace: [1.]

    '''
    if isinstance(node, (int, float)):
        if np.abs(node) > 1:
            raise ValueError(f"Invalid Value: arcsin of {node} does not exist.")
        return np.arcsin(node)
    elif isinstance(node, ForwardNode):
        if np.abs(node.value) >= 1:
            raise ValueError(f"Invalid Value: derivative of arcsin of {node.value} does not exist.")
        return ForwardNode(np.arcsin(node.value), node.trace / np.sqrt(1 - node.value ** 2), node.var)
    elif isinstance(node, ReverseNode):
        if np.abs(node.value) >= 1:
            raise ValueError(f"Invalid Value: derivative of arcsin of {node.value} does not exist.")
        new = ReverseNode(np.arcsin(node.value))
        node.children.append((1.0 / np.sqrt(1 - node.value ** 2), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def arccos(node):
    '''
    Compute the arccosine of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after arccos operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> arccos(x)
    ForwardNode Variable: ['x'],  Value: 1.5707963267948966, Trace: [-1.]

    '''
    if isinstance(node, (int, float)):
        if np.abs(node) > 1:
            raise ValueError(f"Invalid Value: arccos of {node} does not exist.")
        return np.arccos(node)
    elif isinstance(node, ForwardNode):
        if np.abs(node.value) > 1:
            raise ValueError(f"Invalid Value: derivative of arccos of {node.value} does not exist.")
        return ForwardNode(np.arccos(node.value), node.trace * (-1.0) / np.sqrt(1 - node.value ** 2), node.var)
    elif isinstance(node, ReverseNode):
        if np.abs(node.value) > 1:
            raise ValueError(f"Invalid Value: derivative of arccos of {node.value} does not exist.")
        new = ReverseNode(np.arccos(node.value))
        node.children.append((-1.0 / np.sqrt(1 - node.value ** 2), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def arctan(node):
    '''
    Compute the arctangent of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after arctan operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> arctan(x)
    ForwardNode Variable: ['x'],  Value: 0.0, Trace: [1.]

    '''
    if isinstance(node, (int, float)):
        return np.arctan(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.arctan(node.value), node.trace / (1 + node.value ** 2), node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.arctan(node.value))
        node.children.append((1.0 / (1 + node.value ** 2), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def sinh(node):
    '''
    Compute the sinh of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after sinh operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> sinh(x)
    ForwardNode Variable: ['x'],  Value: 0.0, Trace: [1.]

    '''
    if isinstance(node, (int, float)):
        return np.sinh(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.sinh(node.value), node.trace * np.cosh(node.value), node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.sinh(node.value))
        node.children.append((np.cosh(node.value), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def cosh(node):
    '''
    Compute the cosh of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after cosh operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> cosh(x)
    ForwardNode Variable: ['x'],  Value: 1.0, Trace: [0.]

    '''
    if isinstance(node, (int, float)):
        return np.cosh(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.cosh(node.value), node.trace * np.sinh(node.value), node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.cosh(node.value))
        node.children.append((np.sinh(node.value), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def tanh(node):
    '''
    Compute the tanh of the ForwardNode object

    Input:
    self - a ForwardNode variable

    Output:
    The value and trace of the ForwardNode object after tanh operation

    Examples:
    >>> x = ForwardNode(0, trace=1, var=['x'])
    >>> tanh(x)
    ForwardNode Variable: ['x'],  Value: 0.0, Trace: [1.]

    '''
    if isinstance(node, (int, float)):
        return np.tanh(node)
    elif isinstance(node, ForwardNode):
        return ForwardNode(np.tanh(node.value), node.trace / np.cosh(node.value) ** 2, node.var)
    elif isinstance(node, ReverseNode):
        new = ReverseNode(np.tanh(node.value))
        node.children.append((1 / np.cosh(node.value) ** 2, new))
        return new
    else:
        raise AttributeError("Invalid Input!")

def log_base(node, base=10):
    '''
    Compute the log of the ForwardNode object with base specified in the base parameter

    Input:
    self - a ForwardNode variable
    base - a number indicating the base

    Output:
    The value and trace of the ForwardNode object after the logarithm operation with a specific base

    Examples:
    >>> x = ForwardNode(np.exp(3), trace=1, var=['x'])
    >>> log_base(x, base = np.e)
    ForwardNode Variable: ['x1'],  Value: 3.0, Trace: [0.04978707]

    '''
    if (not isinstance(base, (int, float))) or base <= 0:
        raise ValueError("Invalid input: base must be a positive number")
    if isinstance(node, (int, float)):
        if node < 0:
            raise ValueError(f"Invalid input: base-{base} log of {node} does not exist.")
        return np.log(node) / np.log(base)
    elif isinstance(node, ForwardNode):
        if node.value < 0:
            raise ValueError(f"Invalid input: base-{base} log of {node.value} does not exist.")
        return ForwardNode(np.log(node.value) / np.log(base), node.trace / (node.value * np.log(base)), node.var)
    elif isinstance(node, ReverseNode):
        if node.value < 0:
            raise ValueError(f"Invalid input: base-{base} log of {node.value} does not exist.")
        new = ReverseNode(np.log(node.value) / np.log(base))
        node.children.append((1.0 / (node.value * np.log(base)), new))
        return new
    else:
        raise AttributeError("Invalid Input!")

max_stars_repo_name: chelsilarious/AutoDiff

max_issues_repo_name: chelsilarious/AutoDiff

max_forks_repo_name: chelsilarious/AutoDiff


text:
import pandas
import numpy as np
from sklearn.linear_model import Perceptron
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler

def learn(X,y):
    clf = Perceptron(random_state=241)
    clf.fit(X, y)

    return clf

def test(X, y, clf):
    y_actual = clf.predict(X)
    result = accuracy_score(y, y_actual)

    return result

def load(file_name):
    data = pandas.read_csv(file_name)
    y = data[data.columns[0]]
    X = data.drop(data.columns[0], axis=1)

    return X, y

def test_perceptron():
    X_test, y_test = load('perceptron-test.csv')
    X_train, y_train = load('perceptron-train.csv')
    clf = learn(X_train, y_train)
    result1 = test(X_test, y_test, clf)
    print "before :" + str(result1)
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)
    clf = learn(X_train_scaled, y_train)
    result2 = test(X_test_scaled, y_test, clf)
    print "after :" + str(result2)
    print "diff :" + str(result2 - result1)

if __name__ == "__main__":
   test_perceptron()

max_stars_repo_path: week 2/part 2/task1.py

max_stars_repo_name: GrimRanger/Coursera

max_issues_repo_path: week 2/part 2/task1.py

max_issues_repo_name: GrimRanger/Coursera

max_forks_repo_path: week 2/part 2/task1.py

max_forks_repo_name: GrimRanger/Coursera


text:
# -*- encoding: utf-8 -*-
# Module iaisequal

import numpy

def iaisequal(f1, f2, MSG=None):

    if f1.shape != f2.shape:
      return False
    return numpy.all(f1 == f2)


text:
import random
import numpy as np

__all__ = ['scale_size_ratio', 'rand_gen', 'random_safe_box_location']

def scale_size_ratio(back_img_size: tuple, top_img_size: tuple,  ratio: float = 0.5, rtype: str = 'w'):
    hbk, wbk = back_img_size
    hbs, wbs = top_img_size
    hbk_ratio, wbk_ratio = ratio * hbk, ratio * wbk
    if rtype == 'w':
        vratio = wbk_ratio
        vratio = vratio / wbs
    elif rtype == 'h':
        vratio = hbk_ratio
        vratio = vratio / wbs

    wnbs, hnbs = vratio * wbs, vratio * hbs
    return int(hnbs), int(wnbs)

def rand_gen(max_val: int, limit_val: int):
    repeat, result = True, max_val
    while repeat:
        result = random.randint(0, max_val)
        if (result - limit_val) > 0: repeat = False
    return result

def random_safe_box_location(back_size: tuple, base_size: tuple):
    hbk, wbk = back_size
    hbs, wbs = base_size

    xmax, ymax = rand_gen(wbk, wbs), rand_gen(hbk, hbs)
    xmin, ymin = xmax - wbs, ymax - hbs

    return xmin, ymin, xmax, ymax


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 22 00:21:20 2020

@author: manal
"""

import numpy as np
# from GPy import *
import GPy
from GPy.kern.src.stationary import Stationary
import matplotlib.pyplot as plt

class Cosin(Stationary):
    """
    Cosine kernel:
    .. math::
       k(r) = \sigma^2 \cos ( r )

       r(x, x') = \\sqrt{ \\sum_{q=1}^Q \\frac{(x_q - x'_q)^2}{\ell_q^2} }
    """
    def __init__(self, input_dim, variance=1., lengthscale=None, ARD=False, active_dims=None, name='Cosin'):
        super(Cosin, self).__init__(input_dim, variance, lengthscale, ARD, active_dims, name)

    def K_of_r(self, r):
        return self.variance * np.cos(r)

    def dK_dr(self, r):
        return -self.variance * np.sin(r)

    def dK_dX(self, X, X2, dimX):
        r = self._scaled_dist(X, X2)
        id = np.where(r<=1e-6)
        dK_dr = self.dK_dr(r)
        dist = X[:,None,dimX]-X2[None,:,dimX]
        lengthscale2inv = (np.ones((X.shape[1]))/(self.lengthscale**2))[dimX]
        k = 2. * lengthscale2inv * dist * r**(-1) * dK_dr
        for i in range(id[0].shape[0]):
            k[i,i]= -dist[i,i]
        return k
        # return r,2. * lengthscale2inv * dist * r1 * dK_dr

    def dK_dX2(self,X,X2,dimX2):
        return -1.*self.dK_dX(X,X2, dimX2)

    def dK2_dXdX2(self, X, X2, dimX, dimX2):
        if X2 is None:
            X2 = X
        r = self._scaled_dist(X, X2)
        dist = X[:,None,:]-X2[None,:,:]
        lengthscale2inv = np.ones((X.shape[1]))/(self.lengthscale**2)
        K = self.K_of_r(r)
        dK_dr = self.dK_dr(r)
        dist = X[:,None,:]-X2[None,:,:]
        lengthscale2inv = np.ones((X.shape[1]))/(self.lengthscale**2)
        l1 = lengthscale2inv[dimX]
        l2 = lengthscale2inv[dimX2]
        d1 = dist[:,:,dimX]
        d2 = dist[:,:,dimX]
        id = np.where(r<=1e-6)
        k = (dimX==dimX2)*(-2.)*l1*d1*r**(-1)*dK_dr + l1*d1*r**(-1) * (2.*l1*d1*r**(-1)*dK_dr + 4*l2*d2*K)
        for i in range(id[0].shape[0]):
            k[i,i]= -dist[i,i,dimX2]*dist[i,i,dimX]
        return k

#%%
k0 = Cosin(1, active_dims=0)
k01 = Cosin(1)
k1 = Cosin(1, active_dims=1)
k2 = Cosin(3)

x0train = np.linspace(-5,5,100).reshape(-1,1)
x1train = np.linspace(-5,5,100).reshape(-1,1)
xtrain = np.hstack((x0train, x1train))

# print('Training points:')
# print(xtrain)
# print()

k0_K = k0.K(x0train,x0train)
dk0 = k0.dK_dX(x0train, x0train, 0)
dk0_2 = k0.dK2_dXdX2(x0train, x0train, 0, 0)

# print('K0 = ')
# print(k0_K,'\n')
# print('dK0.dX: \n', dk0,'\n')
# print('dK2.dXdX2: \n',dk0_2)
# # print()
# # k0.dK_dX(x0train, x0train, 0).plot()
# plt.figure()
# plt.plot(k0_K[0,:])
# plt.show
# plt.figure()
# plt.plot(dk0[0,:])
# plt.show
# plt.figure()
# plt.plot(dk0_2[0,:])
# plt.show
# # plt.plot(r,k0.dK_dX(x0train,x0train,0))
# plt.figure()
# plt.imshow(k)
# plt.figure()
# plt.imshow(k[1:5,1:5])
# plt.figure()
# plt.imshow(r)

# k01_K = k01.K(x0train,x0train)
# dk01 = k01.dK_dX(x0train, x0train, 0)
# dk01_2 = k01.dK2_dXdX2(x0train, x0train, 0, 0)
# print('k: \n', k01_K-k0_K)
# print('dk: \n', dk01-dk0)
# print('dk2: \n', dk01_2-dk0_2)

# k1_K = k1.K(xtrain,xtrain)
# dk1 = k1.dK_dX(xtrain, xtrain, 1)
# dk1_2 = k1.dK2_dXdX2(xtrain, xtrain, 1, 1)
# print('K1 = ')
# print(k1_K,'\n')  # Need 2D vectors, here, as active_dims=1
# print('dk1.dX: \n',dk1,'\n')
# print('dk12.dXdX2: \n',dk1_2)
# plt.figure()
# plt.plot(k1_K[0,:])
# plt.show
# plt.figure()
# plt.plot(dk1[0,:])
# plt.show
# plt.figure()
# plt.plot(dk1_2[0,:])
# plt.show
# print()
# # plt.plot(k1.dK_dX)
# print('k0_der: \n', k0_der.K(xtrain,xtrain),'\n')

# xtrain2 = np.hstack((x0train, x1train, x1train))
# k2_K = k2.K(xtrain2,xtrain2)
# dk2 = k2.dK_dX(xtrain2, xtrain2, 2)
# dk2_2 = k2.dK2_dXdX2(xtrain2, xtrain2, 2, 2)
# print('K2 = ')
# print(k2_K,'\n')  # Need 2D vectors, here, as active_dims=1
# print('dk2.dX: \n',dk2,'\n')
# print('dk22.dXdX2: \n',dk2_2)
# plt.figure()
# plt.plot(k2_K[0,:])
# plt.show
# plt.figure()
# plt.plot(dk2[0,:])
# plt.show
# plt.figure()
# plt.plot(dk2_2[0,:])
# plt.show

#%%

import pytest
# from katharina import kernels
import func

def test_kern_Cosine():
    x0train = np.linspace(-5,5,100).reshape(-1,1)
    # x1train = np.linspace(-5,5,100).reshape(-1,1)
    # xtrain = np.hstack((x0train, x1train))

    k0 = Cosin(1, active_dims=0)
    k0_K = k0.K(x0train,x0train)
    dk0 = k0.dK_dX(x0train, x0train, 0)
    dk0_2 = k0.dK2_dXdX2(x0train, x0train, 0, 0)

    l = np.array([1, 1])

    K = np.zeros((len(x0train),len(x0train)))
    for i in range(K.shape[0]):
        for j in range(K.shape[1]):
            K[i,j] = func.f_kern(x0train[i], x0train[i], x0train[j], x0train[j], l)

    dK = np.zeros((len(x0train),len(x0train)))
    for i in range(dK.shape[0]):
        for j in range(dK.shape[1]):
            dK[i,j] = func.dkdx(x0train[i], x0train[i], x0train[j], x0train[j], l)

    dK2 = np.zeros((len(x0train),len(x0train)))
    for i in range(dK2.shape[0]):
        for j in range(dK2.shape[1]):
            dK2[i,j] = func.d2kdxdx0(x0train[i], x0train[i], x0train[j], x0train[j], l)

    # print(k)
    # print(",,,,")
    # print(k1a)

    plt.figure()
    plt.imshow(K)
    plt.figure()
    plt.imshow(k0_K)
    plt.figure()
    plt.plot(K[0,:])

    plt.figure()
    plt.imshow(dK)
    plt.figure()
    plt.imshow(dk0)
    plt.figure()
    plt.plot(dK[0,:])

    plt.figure()
    plt.imshow(dK2)
    plt.figure()
    plt.imshow(dk0_2)
    plt.figure()
    plt.plot(dK2[0,:])

    print(k0_K)
    print(K)
    print()
    print(dk0)
    print(dK)
    print()
    print(dk0_2)
    print(dK2)
    print()
    # print(np.isclose(k0_K, K, rtol=1e-6))
    # assert np.isclose(dk0, dK, rtol=1e-6)
    # assert np.isclose(dk0_2, dK2, rtol=1e-6)

# %%
test_kern_Cosine()

max_stars_repo_name: krystophny/profit

max_issues_repo_name: krystophny/profit

max_forks_repo_name: krystophny/profit


text:
import unittest
from copy import deepcopy
from numpy import float32

import ray
from ray.tune.registry import register_env
from ray.rllib.env import PettingZooEnv
from ray.rllib.agents.registry import get_trainer_class

from pettingzoo.butterfly import pistonball_v4
from pettingzoo.mpe import simple_spread_v2
from supersuit import normalize_obs_v0, dtype_v0, color_reduction_v0

class TestPettingZooEnv(unittest.TestCase):
    def setUp(self) -> None:
        ray.init()

    def tearDown(self) -> None:
        ray.shutdown()

    def test_pettingzoo_pistonball_v4_policies_are_dict_env(self):
        def env_creator(config):
            env = pistonball_v4.env(local_ratio=config.get("local_ratio", 0.2))
            env = dtype_v0(env, dtype=float32)
            env = color_reduction_v0(env, mode="R")
            env = normalize_obs_v0(env)
            return env

        config = deepcopy(get_trainer_class("PPO").get_default_config())
        config["env_config"] = {"local_ratio": 0.5}
        # Register env
        register_env("pistonball",
                     lambda config: PettingZooEnv(env_creator(config)))
        env = PettingZooEnv(env_creator(config))
        observation_space = env.observation_space
        action_space = env.action_space
        del env

        config["multiagent"] = {
            # Setup a single, shared policy for all agents.
            "policies": {
                "av": (None, observation_space, action_space, {})
            },
            # Map all agents to that policy.
            "policy_mapping_fn": lambda agent_id, episode, **kwargs: "av",
        }

        config["log_level"] = "DEBUG"
        config["num_workers"] = 1
        # Fragment length, collected at once from each worker
        # and for each agent!
        config["rollout_fragment_length"] = 30
        # Training batch size -> Fragments are concatenated up to this point.
        config["train_batch_size"] = 200
        # After n steps, force reset simulation
        config["horizon"] = 200
        # Default: False
        config["no_done_at_end"] = False
        trainer = get_trainer_class("PPO")(env="pistonball", config=config)
        trainer.train()

    def test_pettingzoo_env(self):
        register_env("simple_spread",
                     lambda _: PettingZooEnv(simple_spread_v2.env()))
        env = PettingZooEnv(simple_spread_v2.env())
        observation_space = env.observation_space
        action_space = env.action_space
        del env

        agent_class = get_trainer_class("PPO")

        config = deepcopy(agent_class.get_default_config())

        config["multiagent"] = {
            # Set of policy IDs (by default, will use Trainer's
            # default policy class, the env's obs/act spaces and config={}).
            "policies": {
                "av": (None, observation_space, action_space, {})
            },
            # Mapping function that always returns "av" as policy ID to use
            # (for any agent).
            "policy_mapping_fn": lambda agent_id, episode, **kwargs: "av"
        }

        config["log_level"] = "DEBUG"
        config["num_workers"] = 0
        config["rollout_fragment_length"] = 30
        config["train_batch_size"] = 200
        config["horizon"] = 200  # After n steps, force reset simulation
        config["no_done_at_end"] = False

        agent = agent_class(env="simple_spread", config=config)
        agent.train()

if __name__ == "__main__":
    import pytest
    import sys
    sys.exit(pytest.main(["-v", __file__]))

max_stars_repo_name: daobook/ray

max_issues_repo_name: daobook/ray

max_forks_repo_name: gramhagen/ray

reason: from numpy


text:
#importing numpy , pandas and matplotlib to work on the data

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import datetime

#the function for processing the data to turn it into a single variable which contain all instances
def data_preprocessing(dataset_m):

    dataset = dataset_m[0]
    x = dataset.iloc[:,0:1].values
    y = dataset.iloc[:,1:11].values

    error_indexes =[]
    for i in range(len(x)):
        if (x[i][0]=='timestamp'):
            error_indexes.append(i)

    x = np.delete(x,error_indexes,0)
    y = np.delete(y,error_indexes,0)

    from sklearn.preprocessing import Imputer
    imputer = Imputer(missing_values = "NaN",strategy = "most_frequent",axis = 0)
    y = imputer.fit_transform(y)

    y_binary = np.int64(y>0)

    x_timestamp = np.float64(x)

#print(datetime.datetime.fromtimestamp(x_timestamp[0][0]))
#print(datetime.datetime.fromtimestamp(x_timestamp[1][0]))

    x_weekday = []
    x_hour = []
    x_minute = []
    x_second = []
    x_month = []
    for k in range(len(x_timestamp)):
        x_weekday.append((datetime.datetime.fromtimestamp(x_timestamp[k][0])).weekday())
        x_hour.append((datetime.datetime.fromtimestamp(x_timestamp[k][0])).hour)
        x_minute.append((datetime.datetime.fromtimestamp(x_timestamp[k][0])).minute)
        x_second.append((datetime.datetime.fromtimestamp(x_timestamp[k][0])).second)
        x_month.append((datetime.datetime.fromtimestamp(x_timestamp[k][0])).month)
    x_features = np.c_[x_timestamp,x_month]
    x_features = np.c_[x_features,x_weekday]
    x_features = np.c_[x_features,x_hour]
    x_features = np.c_[x_features,x_minute]
    x_features = np.c_[x_features,x_second]

#x_features=[]
#y_binary = []

    length = len(dataset_m)
    for i in range(1,length):
        print(i," -->")
        temp_dataset = dataset_m[i]
        x_temp = temp_dataset.iloc[:,0:1].values
        y_temp = temp_dataset.iloc[:,1:11].values

        error_indexes_temp =[]
        for i in range(len(x_temp)):
            if (x_temp[i][0]=='timestamp'):
                error_indexes_temp.append(i)

        x_temp = np.delete(x_temp,error_indexes_temp,0)
        y_temp = np.delete(y_temp,error_indexes_temp,0)

        from sklearn.preprocessing import Imputer
        imputer = Imputer(missing_values = "NaN",strategy = "most_frequent",axis = 0)
        y_temp = imputer.fit_transform(y_temp)
        y_binary_temp = np.int64(y_temp>0)
        x_timestamp_temp = np.float64(x_temp)

        temp_x_weekday = []
        temp_x_hour = []
        temp_x_minute = []
        temp_x_second = []
        temp_x_month = []

        for k in range(len(x_temp)):
            temp_x_weekday.append((datetime.datetime.fromtimestamp(x_timestamp_temp[k][0])).weekday())
            temp_x_hour.append((datetime.datetime.fromtimestamp(x_timestamp_temp[k][0])).hour)
            temp_x_minute.append((datetime.datetime.fromtimestamp(x_timestamp_temp[k][0])).minute)
            temp_x_second.append((datetime.datetime.fromtimestamp(x_timestamp_temp[k][0])).second)
            temp_x_month.append((datetime.datetime.fromtimestamp(x_timestamp_temp[k][0])).month)
        temp_x_features = np.c_[x_timestamp_temp,temp_x_month]
        temp_x_features = np.c_[temp_x_features,temp_x_weekday]
        temp_x_features = np.c_[temp_x_features,temp_x_hour]
        temp_x_features = np.c_[temp_x_features,temp_x_minute]
        temp_x_features = np.c_[temp_x_features,temp_x_second]

        x_features = np.r_[x_features,temp_x_features]
        y_binary = np.r_[y_binary,y_binary_temp]
        y = np.r_[y,y_temp]

    return x_features,y_binary,y

# now retreiving all the addresses to import the files
def address_finder():
    list_of_building_names = os.listdir()
    address_of_greend_dataset = os.getcwd()
    address_of_all_building_folders_inside = []
    ## this loop extracts the address of all buildings
    for i in range(len(list_of_building_names)):
        address_of_all_building_folders_inside.append(address_of_greend_dataset+'/'+list_of_building_names[i])
    # this loop extracts the addresses of all the folders inside the building folder
    list_of_address_inside_building = []
    for i in range(len(address_of_all_building_folders_inside)):
        temp_address = os.listdir()
        len_address = len(temp_address)
        temp_list = os.listdir(address_of_all_building_folders_inside[i])
        list_of_address_inside_building.append([])
        for k in range(len(temp_list)):
            list_of_address_inside_building[i].append(address_of_all_building_folders_inside[i]+'/'+temp_list[k])
    csv = '.csv'
    csv_address_list = []
    #this loop extracts all the address of all the csv file
    for i in range(len(list_of_address_inside_building)):
        no_of_month = len(list_of_address_inside_building[i])
        csv_address_list.append([])
        for k in range(no_of_month):
            no_of_days = len(os.listdir(list_of_address_inside_building[i][k]))
            csv_address_list[i].append([])
            temp_list1 = os.listdir(list_of_address_inside_building[i][k])
            for j in range(no_of_days):
                csv_address_list[i][k].append(list_of_address_inside_building[i][k]+'/'+temp_list1[j])
    return csv_address_list

csv_address_list = address_finder()
building_2_year_csv_address = csv_address_list[2]
no_month_data_of_building_2 = len(building_2_year_csv_address)
building_2_year_address = []
for i in range(no_month_data_of_building_2):
    building_2_year_address = building_2_year_csv_address[i]+building_2_year_address

dataset_year= []
for i in range(len(building_2_year_address)):
    dataset_year.append(pd.read_csv(building_2_year_address[i]))

x_year,y_year,y_year_values = data_preprocessing(dataset_year)

from sklearn.externals import joblib
joblib.dump(x_year,"x_year.pkl")
joblib.dump(y_year,"y_year.pkl")
joblib.dump(y_year_values,"y_year_values.pkl")
joblib.dump(dataset_year,"dataset_year.pkl")


text:
'''
Future Update Notes:
1) Non-supervisor mode is not coded.
2) Controller functions should be updated.
3) robot_creator function takes too much arguments and more argument
   necessary for options like robot tags, data transfer between real
   robot and player, etc. New structure might be necessary.
4) Codes modified for admin-permissions in Windows, in Linux they should
   be arranged.
5) Robot tag creation not included.
6) Collaboration of game manager and world creator is still not
   considered.
7) Controller paths should be arranged according to Linux system and
   webots configuration.
'''

import numpy as np
from decimal import Decimal
import math
from pathlib import Path
import ctypes, sys #for Windows only

def is_admin():# Windows Admin Permissions
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def grid_material(no):
	'''
	New texture may be added easily with location of texture file(should
	be in jpg format) only corresponding number will assigned to texture
	in the function.
	'''
	if no == 0:
		return 0
	if no == 1:
		return 'textures/earth_texture.jpg'
	if no == 2:
		return 'textures/water_texture.jpg'
	if no == 3:
		return 'textures/desert_texture.jpg'
	if no == 4:
		return 'textures/cell_texture.jpg'

def floor_text(x,z,i,grid_length,material,world_text):
	'''
	Creates script of every floor element of floor matrix.
	'''
	world_text.append('Floor {\n')
	world_text.append('  translation {0} 0 {1}\n'.format(x,z))
	world_text.append('  name "floor({0})"\n'.format(i))
	world_text.append('  size {0} {0}\n'.format(grid_length))
	world_text.append('  tileSize {0} {0}\n'.format(grid_length))
	world_text.append('  appearance Appearance {\n')
	world_text.append('    texture ImageTexture {\n')
	world_text.append('      url [\n')
	world_text.append('        "{}"\n'.format(material))
	world_text.append('      ]\n')
	world_text.append('      filtering 0\n')
	world_text.append('    }}}\n')

def arena_creator(floor_matrix, grid_length,world_text):
	'''
	floor_matrix is a matrix and it decides shape of the arena, number
	of grids, grid colors. Each matrix element is a grid texture.
	Corresponding element number will be defined. Value of grid_length
	in meters.
	For example:
	A = [1 3]	Element value: 0 = box obstacle, 1 = earth, 2 = water,
	    [3 4]				   3 = sand, 4 = cell
	'''
	i = 0
	for ix,iz in np.ndindex(floor_matrix.shape):
		x = (grid_length / 2) + (iz * grid_length)
		z = (grid_length / 2) + (ix * grid_length)
		material = grid_material(floor_matrix[ix,iz])
		if material != 0:
			floor_text(x,z,i,grid_length,material,world_text)
		if material == 0:
			obstacle(x,z,i,grid_length,world_text)
		i += 1

def distance_sensor(r_no,s_no,x,z,r,cov,res,s,body,world_text,main,loop):
	'''
	x, z and r are x-coordinate, z-coordinate and direction(rotation
	around	y axis), repectively. Values of x, z, r should calculated
	w.r.t. robot body. cov(Coverage) is range of the sensor and
	res(resolution) tells smallest change in distance a sensor can
	detect. x, z, r and coverage in meters. Body is imaginary body of
	sensor device and value of body can be True or False. r_no and s_no
	are id of the robot that carries sensor and id of sensor(there might
	be multiple sensor on a robot, they must identified distinctly),
	repectively. Imaginary body is black as default. r is in degrees.
	s is for supervisor mode of the robot(True or False). main and loop
	are main and loop part of the controller.
	'''
	pi_5f = float("{:.5f}".format(math.pi))
	r = r / 180 * pi_5f
	world_text.append('    DistanceSensor {\n')
	world_text.append('      translation {0} 0 {1}\n'.format(x,z))
	world_text.append('      rotation 0 1 0 {}\n'.format(r))
	if body == True:
		world_text.append('      children [\n')
		world_text.append('        Shape {\n')
		world_text.append('          appearance PBRAppearance {\n')
		world_text.append('            baseColor 0 0 0\n')
		world_text.append('            roughness 1\n')
		world_text.append('            metalness 0\n')
		world_text.append('          }\n')
		world_text.append('          geometry Box {\n')
		world_text.append('            size 0.001 0.001 0.001\n')
		world_text.append('          }}]\n')
	world_text.append('      name "ds_{0}_{1}"\n'.format(r_no,s_no))
	world_text.append('      lookupTable [\n')
	world_text.append('        0 0 0\n')
	world_text.append('        {0} {1} 0\n'.format(cov,res))
	world_text.append('      ]}\n')
	#Controller Part
	if s == True:
		main.append('ds_1 = supervisor.getDistanceSensor("ds_{0}_{1}")\n'.format(r_no,s_no))
		main.append('ds_1.enable(timeStep)\n')
		main.append('ds.append(ds_1)\n')

	#if s == False:

def robot_controller(r_no,supervisor,main,loop):
	'''
	r_no is robot no. main and loop are part of controller scripts which
	necessary for devices and sensors.
	'''
	robot_controller_main = [] # main function of robot controller
	robot_controller_loop = [] # loop function of robot controller
	robot_controller_main.append('import json\n')
	robot_controller_main.append('from pathlib import Path\n')
	robot_controller_main.append('from controller import *\n')
	robot_controller_main.append('\n')
	robot_controller_main.append('timeStep = 32\n')#Default
	robot_controller_main.append('ds = []\n')
	robot_controller_main.append('file_to_open = Path("C:/Users/Korcan/Desktop/ME462/l_r_of_Robot_1.txt")\n') #EDIT THISSS

	if supervisor == True:
		robot_controller_main.append('supervisor = Supervisor()\n')
		robot_controller_main.append('robot_node = supervisor.getFromDef("Robot_{}")\n'.format(r_no))
		robot_controller_main.append('trans_field = robot_node.getField("translation")\n')
		robot_controller_main.append('rot_field = robot_node.getField("rotation")\n')
		robot_controller_main.append('\n')

		robot_controller_loop.append('while supervisor.step(timeStep) != -1:\n')
		robot_controller_loop.append('    val_translation = trans_field.getSFVec3f()\n')
		robot_controller_loop.append('    val_rotation = rot_field.getSFRotation()\n')
		robot_controller_loop.append("    f = open(file_to_open, mode='r')\n")
		robot_controller_loop.append('    data_as_string = f.readlines()\n')
		robot_controller_loop.append('    f.close()\n')
		robot_controller_loop.append('    try:\n')
		robot_controller_loop.append('        if len(data_as_string[{}]) != 0:\n'.format(r_no-1))
		robot_controller_loop.append('            data = json.loads(data_as_string[{}])\n'.format(r_no-1))
		robot_controller_loop.append('            trans_field.setSFVec3f(data[0])\n')
		robot_controller_loop.append('            rot_field.setSFRotation(data[1])\n')
		robot_controller_loop.append('    except IndexError:\n')
		robot_controller_loop.append('        pass\n')
		robot_controller_loop.append('\n')

	loop.append('    for e in ds:\n')
	loop.append('        print(e.getValue())\n')

	#if super == False:

	robot_controller_main = robot_controller_main + main
	robot_controller_loop = robot_controller_loop + loop
	final_controller = robot_controller_main + robot_controller_loop

	location = "C:/Program Files/Webots/projects/default/controllers/Robot_{}".format(r_no)# EDIT THISSS
	if is_admin():#Windows admin permissions inside of this if part of the program
		Path(location).mkdir(parents=False, exist_ok=True)
		[f.unlink() for f in Path(location).glob("*") if f.is_file()]
		f = open(location + "/Robot_{}.py".format(r_no), "w")
		f.writelines(final_controller)
		f.close()
	else:
    # Re-run the program with admin rights
		ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)

def robot_creator(x,z,r_no,supervisor,world_text):
	'''
	Value of supervisor can be True or False. If value is False than
	motors are enabled. r_no is id of the robot. x and z are start
	coordinates of robot. r_no should not be 0, 0 is used for Arena
	Top Camera.
	'''
	main = []
	loop = []

	world_text.append('DEF Robot_{} Robot '.format(r_no))
	world_text.append('{\n')
	world_text.append('  translation {0} 0.03 {1}\n'.format(x,z))
	world_text.append('  children [\n')
	#Below lines for robot body
	world_text.append('    DEF robot_{}_body Shape '.format(r_no))
	world_text.append('{\n')
	world_text.append('      appearance PBRAppearance {\n')
	world_text.append('        baseColor 0.917647 0.145098 0.145098\n')
	world_text.append('        roughness 1\n')
	world_text.append('        metalness 0\n')
	world_text.append('      }\n')
	world_text.append('      geometry Box {\n')
	world_text.append('        size 0.09 0.06 0.07\n')
	world_text.append('      }}\n')
	#Below lines for sensor
	distance_sensor(r_no,1,0.045,0,0,0.1,100,supervisor,False,world_text,main,loop)
	distance_sensor(r_no,2,-0.045,0,180,0.1,100,supervisor,False,world_text,main,loop)
	distance_sensor(r_no,3,0,0.035,-90,0.1,100,supervisor,False,world_text,main,loop)
	distance_sensor(r_no,4,0,-0.035,90,0.1,100,supervisor,False,world_text,main,loop)
	#Below lines for motor when no real robots exist
	if supervisor == False:
		motor()
	world_text.append('  ]\n')
	#end of the children of robot
	world_text.append('  name "robot_{}"\n'.format(r_no))
	world_text.append('  boundingObject USE robot_{}_body\n'.format(r_no))
	world_text.append('  controller "Robot_{}"\n'.format(r_no))
	if supervisor == True:
		world_text.append('  supervisor TRUE\n')
	world_text.append('}\n')

	#controller of robot
	robot_controller(r_no,supervisor,main,loop)

def motor(x,z):
	'''
	x, y are coordinates are w.r.t the robot body and x, y values are
	in meters.
	'''

def obstacle(x,z,i,a,world_text):
	'''
	Cubic obstacle with side size a in meters. x, y are coordinate
	values of obstacle w.r.t general coordinate axis. Base color is
	the color of the obstacle and it made black as default.
	'''
	world_text.append('Solid {\n')
	world_text.append('  translation {0} {1} {2}\n'.format(x,a/2,z))
	world_text.append('  children [\n')
	world_text.append('    DEF obstacle_{0} Shape '.format(i))
	world_text.append('{\n')
	world_text.append('      appearance PBRAppearance {\n')
	world_text.append('        baseColor 0 0 0\n')
	world_text.append('        roughness 1\n')
	world_text.append('        metalness 0\n')
	world_text.append('      }\n')
	world_text.append('      geometry Box {\n')
	world_text.append('        size {0} {0} {0}\n'.format(a))
	world_text.append('      }}]\n')
	world_text.append('  name "obstacle_{}"\n'.format(i))
	world_text.append('  boundingObject USE obstacle_{}\n'.format(i))
	world_text.append('}\n')

def arena_top_cam(a,grid_length,y,width,height,world_text):
	'''
	x, z coordinates of middle point of the arena and can be found by
	arena matrix(a) with grid_length. Value of y should be proper
	perpendicular distance from the floor and y value in meters. Values
	of width and height are resolution of camera and values are in
	pixels.
	'''
	x = a.shape[0]
	x = x / 2 * grid_length
	z = a.shape[1]
	z = z / 2 * grid_length
	y = y * (width / height) #Assumed width > height

	world_text.append('DEF Arena_Cam Robot {\n')
	world_text.append('  translation {0} {1} {2}\n'.format(x,y,z))
	world_text.append('  rotation -1 0 0 1.5708\n')
	world_text.append('  children [\n')
	world_text.append('    Camera {\n')
	world_text.append('      name "Arena_Top_Cam"\n')
	world_text.append('      width {}\n'.format(width))
	world_text.append('      height {}\n'.format(height))
	world_text.append('    }]\n')
	world_text.append('  name "robot_0"\n')
	world_text.append('  controller "arena_top_cam"\n')
	world_text.append('  supervisor TRUE\n')
	world_text.append('}\n')

def world_creator(floor_matrix,grid_length,basic_time_step):
	'''
	floor_matrix is a matrix and it decides shape of the arena, number
	of grids, grid colors. Each matrix element is a grid texture.
	Corresponding element number will be defined. Value of grid_length
	in meters. basic_time_step is the time step increament used by
	Webots and expressed in milliseconds. Default value of basic time
	step in Webots is 32ms.
	'''
	contents = []
	a = floor_matrix.shape
	m = a[0]
	n = a[1]
	x = m / 2 * grid_length
	z = n / 2 * grid_length
	max_length = max(m,n) * grid_length
	y = max_length / 0.748 #Field of view calculations

	#Main contents of world
	contents.append('#VRML_SIM R2020a utf8\n')
	contents.append('WorldInfo {\n')
	contents.append('  basicTimeStep {}\n'.format(basic_time_step))
	contents.append('}\n')
	contents.append('Viewpoint {\n')
	contents.append('  orientation -1 0 0 1.5708\n')
	contents.append('  position {0} {1} {2}\n'.format(x,2*y,z))
	contents.append('}\n')
	contents.append('TexturedBackground {\n')
	contents.append('}\n')
	contents.append('TexturedBackgroundLight {\n')
	contents.append('}\n')

	#Element of world: Arena, Robots, Top Camera
	arena_creator(floor_matrix, grid_length,contents)
	robot_creator(-grid_length,grid_length,1,True,contents)
	robot_creator(-grid_length,3*grid_length,2,True,contents)
	arena_top_cam(floor_matrix,grid_length,y,1280,720,contents)

	f = open("sample_world.wbt", "w")
	f.writelines(contents)
	f.close()

a = np.random.randint(0,5,size=(10,10))
print(a)
print(a.shape)
world_creator(a, 0.15, 32)


text:
import numpy as np
from numba import jit
import matplotlib.pyplot as plt
import quantecon as qe
from quantecon.distributions import BetaBinomial

n, a, b = 50, 200, 100
w_min, w_max = 10, 60
w_vals = np.linspace(w_min, w_max, n+1)
dist = BetaBinomial(n, a, b)
psi_vals = dist.pdf()

def plot_w_distribution(w_vals, psi_vals):
    fig, ax = plt.subplots(figsize=(9, 6.5))
    ax.stem(w_vals, psi_vals, label='$\phi (w\')$')
    ax.set_xlabel('wages')
    ax.set_ylabel('probabilities')
    plt.show()

def optimal_policy_step(v, w_weights, w, beta=0.99, unemployment=25):
    v_next = np.maximum(w / (1 - beta),
                        unemployment + beta*np.dot(v, w_weights))

    return v_next

def compute_reservation_wage(w, w_weights, max_iter=500, epsilon=1e-6, beta=0.99, unemployment=25):
    v = w / (1 - beta)

    i = 0
    error = epsilon + 1

    while i < max_iter and error > epsilon:
        v_next = optimal_policy_step(
            v, w_weights, w, beta=beta, unemployment=unemployment)

        error = np.max(np.abs(v_next - v))
        v = v_next
        i += 1

    reservation_wage = (1 - beta) * (unemployment + beta*np.dot(v, w_weights))
    return reservation_wage

reservation_wage = compute_reservation_wage(w_vals, psi_vals)

print(reservation_wage)

reason: import numpy,from numba


text:
import argparse
from typing import Dict, List, Tuple, Union

import albumentations as albu
import cv2
import numpy as np
import onnx
import onnxruntime as ort
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils import model_zoo
from torchvision.ops import nms

from retinaface.box_utils import decode, decode_landm
from retinaface.network import RetinaFace
from retinaface.prior_box import priorbox
from retinaface.utils import tensor_from_rgb_image, vis_annotations

state_dict = model_zoo.load_url(
    "https://github.com/ternaus/retinaface/releases/download/0.01/retinaface_resnet50_2020-07-20-f168fae3c.zip",
    progress=True,
    map_location="cpu",
)

class M(nn.Module):
    def __init__(self, max_size: int = 1280):
        super().__init__()
        self.model = RetinaFace(
            name="Resnet50",
            pretrained=False,
            return_layers={"layer2": 1, "layer3": 2, "layer4": 3},
            in_channels=256,
            out_channels=256,
        )
        self.model.load_state_dict(state_dict)

        self.max_size = max_size

        self.scale_landmarks = torch.from_numpy(np.tile([self.max_size, self.max_size], 5))
        self.scale_bboxes = torch.from_numpy(np.tile([self.max_size, self.max_size], 2))

        self.prior_box = priorbox(
            min_sizes=[[16, 32], [64, 128], [256, 512]],
            steps=[8, 16, 32],
            clip=False,
            image_size=(self.max_size, self.max_size),
        )
        self.nms_threshold: float = 0.4
        self.variance = [0.1, 0.2]
        self.confidence_threshold: float = 0.7

    def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        loc, conf, land = self.model(x)

        conf = F.softmax(conf, dim=-1)

        boxes = decode(loc.data[0], self.prior_box, self.variance)

        boxes *= self.scale_bboxes
        scores = conf[0][:, 1]

        landmarks = decode_landm(land.data[0], self.prior_box, self.variance)
        landmarks *= self.scale_landmarks

        # ignore low scores
        valid_index = torch.where(scores > self.confidence_threshold)[0]
        boxes = boxes[valid_index]
        landmarks = landmarks[valid_index]
        scores = scores[valid_index]

        # do NMS
        keep = nms(boxes, scores, self.nms_threshold)
        boxes = boxes[keep, :]

        landmarks = landmarks[keep]
        scores = scores[keep]
        return boxes, scores, landmarks

def prepare_image(image: np.ndarray, max_size: int = 1280) -> np.ndarray:
    image = albu.Compose([albu.LongestMaxSize(max_size=max_size), albu.Normalize(p=1)])(image=image)["image"]

    height, width = image.shape[:2]

    return cv2.copyMakeBorder(image, 0, max_size - height, 0, max_size - width, borderType=cv2.BORDER_CONSTANT)

def main() -> None:
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg(
        "-m",
        "--max_size",
        type=int,
        help="Size of the input image. The onnx model will predict on (max_size, max_size)",
        required=True,
    )

    arg("-o", "--output_file", type=str, help="Path to save onnx model.", required=True)
    args = parser.parse_args()

    raw_image = cv2.imread("tests/data/13.jpg")

    image = prepare_image(raw_image, args.max_size)

    x = tensor_from_rgb_image(image).unsqueeze(0).float()

    model = M(max_size=args.max_size)
    model.eval()
    with torch.no_grad():
        out_torch = model(x)

    torch.onnx.export(
        model,
        x,
        args.output_file,
        verbose=True,
        opset_version=12,
        input_names=["input"],
        export_params=True,
        do_constant_folding=True,
    )

    onnx_model = onnx.load(args.output_file)
    onnx.checker.check_model(onnx_model)

    ort_session = ort.InferenceSession(args.output_file)

    outputs = ort_session.run(None, {"input": np.expand_dims(np.transpose(image, (2, 0, 1)), 0)})

    for i in range(3):
        if not np.allclose(out_torch[i].numpy(), outputs[i]):
            raise ValueError("torch and onnx models do not match!")

    annotations: List[Dict[str, List[Union[float, List[float]]]]] = []

    for box_id, box in enumerate(outputs[0]):
        annotations += [
            {
                "bbox": box.tolist(),
                "score": outputs[1][box_id],
                "landmarks": outputs[2][box_id].reshape(-1, 2).tolist(),
            }
        ]

    im = albu.Compose([albu.LongestMaxSize(max_size=1280)])(image=raw_image)["image"]
    cv2.imwrite("example.jpg", vis_annotations(im, annotations))

if __name__ == "__main__":
    main()


text:
import argparse
import cv2
import numpy as np
import face_identification

parser = argparse.ArgumentParser(description='face identification python api test')
# general
parser.add_argument('--model_path',
                    type=str,
                    default='',
                    required=True,
                    help='Seeta face identification model path')

parser.add_argument('--image_path',
                    type=str,
                    default='',
                    required=True,
                    help='Test image path')

args = parser.parse_args()

faceIdentification = face_identification.SeetaFaceIdentification(args.model_path)

img = cv2.imread(args.image_path, 1)
if img is None:
    print("Open image " + args.image_path + " failed!")
    exit(0)

# [x, y, width, height, roll, pitch, yaw, score]
# box = [158.0, 216.0, 224.0, 224.0, 0.0, 0.0, 0.0, 24.380210876464844]
facelandmarks = [[218.02700805664062, 320.05078125, 301.3433532714844, 300.4508972167969, 271.5216979980469, 375.4873046875, 253.0943145751953, 409.58197021484375, 314.4736328125, 393.3882141113281]]

t0 = cv2.getTickCount()
gallery_fea = faceIdentification.extractFeatureWithCrop(img, facelandmarks)
probe_fea = faceIdentification.extractFeatureWithCrop(img, facelandmarks)
t1 = cv2.getTickCount()
secs = (t1 - t0)/cv2.getTickFrequency() / 2
print("Detections takes ", secs, " seconds")

print("Sim is : ", faceIdentification.calcSimilarity(gallery_fea, probe_fea))

max_stars_repo_name: jnulzl/SeetaFaceEngine

max_issues_repo_name: jnulzl/SeetaFaceEngine

max_forks_repo_name: jnulzl/SeetaFaceEngine


text:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

class VoteModel:
    """
    Summary:
        Optimization algorithm using voting methods

    Required Parameters:
        nk_model is an instance of the NKLandscape class (though, as an optimization method,
        this parameter could take any class instance which has binary array solutions of fixed size N and
        a calculate_fitness method)

        solutions is a 2d binary numpy array of shape (num_solutions, N) where N is the size of solutions
        to the provided nk model

    Optional Parameters:
        possible_vote_indices is the region of the solution that is allowed to be modified.
        If this parameter is not provided, the entire solution is allowed to be modified.

        vote_size determines the number of bit positions to vote on in each voting round

        vote_type determines the voting model used to decide which proposal to implement

    To do:
        -Implement Plurality with Runoff

        -Implement Borda Rule

        -Implement Hare Rule

        -Implement Coombs Rule

    """

    def __init__(
        self,
        nk_model=None,
        solutions=None,
        possible_vote_indices=None,
        vote_size=2,
        vote_type="plurality",
    ):
        if nk_model is None:
            raise ValueError("An NKLandscape model must be provided")
        self.nk = nk_model

        if solutions is None:
            raise ValueError("A set of voting solutions must be provided")
        self.solutions = np.copy(solutions)

        if possible_vote_indices is None:
            self.possible_vote_indices = np.arange(self.nk.N)
        else:
            self.possible_vote_indices = possible_vote_indices

        self.num_solutions = solutions.shape[0]

        self.vote_size = vote_size
        self.vote_type = vote_type

        self.mean_list = np.zeros(shape=(50, 2))

    def _calculate_proposal_fitnesses(self, proposal_indices):
        proposal_fitnesses = np.zeros((self.num_solutions, 2 ** self.vote_size))
        for i, solution in enumerate(self.solutions):
            for proposal in range(2 ** self.vote_size):
                binary_proposal = self._decimal_to_binary(proposal, self.vote_size)

                proposed_solution = np.copy(solution)
                np.put(proposed_solution, proposal_indices, binary_proposal)

                proposal_fitnesses[i, proposal] = self.nk.calculate_fitness(
                    proposed_solution
                )

        return proposal_fitnesses

    def _decimal_to_binary(self, decimal, length):
        binary = list(map(int, bin(decimal)[2:]))
        pad = length - len(binary)
        binary = [0] * pad + binary

        return np.asarray(binary, int)

    def _binary_to_decimal(self, binary):
        decimal = 0
        for i in range(len(binary)):
            decimal += binary[len(binary) - i - 1] * 2 ** i

        return decimal

    def _determine_winner(self, proposal_fitnesses):
        if self.vote_type == "plurality":
            tally = np.zeros(2 ** self.vote_size)
            for i in range(self.num_solutions):
                vote = np.argwhere(
                    proposal_fitnesses[i] == np.max(proposal_fitnesses[i])
                )[0]
                tally[vote[0]] += 1
            decimal_winner = np.argwhere(tally == np.max(tally))[0][0]
            binary_winner = self._decimal_to_binary(decimal_winner, self.vote_size)

        elif self.vote_type == "approval":
            tally = np.zeros(2 ** self.vote_size)
            current_fitnesses = self.get_fitnesses()
            for i in range(self.num_solutions):
                votes = np.argwhere(proposal_fitnesses[i] > current_fitnesses[i]).T

                if len(votes[0]) > 0:
                    tally[votes[0]] += 1
                else:
                    self_vote = np.argwhere(
                        proposal_fitnesses[i] == current_fitnesses[i]
                    ).T
                    tally[self_vote[0]] += 1

            decimal_winner = np.argwhere(tally == np.max(tally))[0][0]
            binary_winner = self._decimal_to_binary(decimal_winner, self.vote_size)

        elif self.vote_type == "ranked":  # This is where new code started
            current_fitnesses = self.get_fitnesses()[np.newaxis].T
            marginal_scores = proposal_fitnesses - np.tile(
                current_fitnesses, 2 ** self.vote_size
            )
            scores = np.sort(marginal_scores, axis=1)
            # Implement Borda Rule here

            decimal_winner = np.argwhere(scores == np.max(scores))[0][0]
            binary_winner = self._decimal_to_binary(decimal_winner, self.vote_size)

        # A version of score that is most similar to actual score voting (actually more like cumulative voting).
        # Each solution has the same amount of score (1) to alot to the proposals
        elif self.vote_type == "normalized_score":
            minimum_proposal_fitnesses = np.min(proposal_fitnesses, axis=1)[
                np.newaxis
            ].T
            minimum_proposal_fitnesses = np.tile(
                minimum_proposal_fitnesses, 2 ** self.vote_size
            )
            positive_proposal_fitnesses = (
                proposal_fitnesses - minimum_proposal_fitnesses
            )

            normalizing_factors = np.sum(positive_proposal_fitnesses, axis=1)[
                np.newaxis
            ].T
            normalizing_factors = np.tile(normalizing_factors, 2 ** self.vote_size)
            normalized_scores = np.divide(
                positive_proposal_fitnesses, normalizing_factors
            )

            scores = np.sum(normalized_scores, axis=0)
            decimal_winner = np.argwhere(scores == np.max(scores))[0][0]
            binary_winner = self._decimal_to_binary(decimal_winner, self.vote_size)
        # These assume voters know how their utility differs from another's (in the case of nk
        # landscapes this is obviously true)
        elif self.vote_type == "total_score":
            scores = np.sum(proposal_fitnesses, axis=0)
            decimal_winner = np.argwhere(scores == np.max(scores))[0][0]
            binary_winner = self._decimal_to_binary(decimal_winner, self.vote_size)
        # I think this should be the optimal setting for optimization
        elif self.vote_type == "marginal_score":
            current_fitnesses = self.get_fitnesses()[np.newaxis].T
            marginal_scores = proposal_fitnesses - np.tile(
                current_fitnesses, 2 ** self.vote_size
            )
            scores = np.sum(marginal_scores, axis=0)
            decimal_winner = np.argwhere(scores == np.max(scores))[0][0]
            binary_winner = self._decimal_to_binary(decimal_winner, self.vote_size)

        return binary_winner

    def _update_solutions(self, winner, proposal_indices):
        for i in range(self.num_solutions):
            np.put(self.solutions[i], proposal_indices, winner)

        self.solutions = np.unique(self.solutions, axis=0)
        self.num_solutions = self.solutions.shape[0]

    def _generate_vote_indicies(self):
        np.random.shuffle(self.possible_vote_indices)

        return self.possible_vote_indices[0 : self.vote_size]

    def print_distribution(self, i, verbose):
        if verbose:
            fitnesses = self.get_fitnesses()
            print(
                "\n vote iterations:",
                i + 1,
                "num_solutions:",
                self.get_num_solutions(),
                "\n mean:",
                np.mean(fitnesses),
                "variance:",
                np.var(fitnesses),
                "\n min:",
                np.min(fitnesses),
                "max:",
                np.max(fitnesses),
            )
            sns.distplot(fitnesses)
            plt.show()

    def get_mean(self):
        fitnesses = self.get_fitnesses()
        return np.mean(fitnesses)

    def get_variance(self):
        fitnesses = self.get_fitnesses()
        return np.var(fitnesses)

    def get_max(self):
        fitness = self.get_fitnesses()
        return np.max(fitness)

    def get_min(self):
        fitness = self.get_fitnesses()
        return np.min(fitness)

    def run(self, iterations=100, until_unique=False, verbose=False):
        for i in range(iterations):
            proposal_indices = self._generate_vote_indicies()

            proposal_fitnesses = self._calculate_proposal_fitnesses(proposal_indices)

            winner = self._determine_winner(proposal_fitnesses)

            self._update_solutions(winner, proposal_indices)

            self.print_distribution(i, verbose)

            self.mean_list[i] = [i, self.get_mean()]

            if until_unique and (self.num_solutions == 1):
                break

    def get_solutions(self):
        return self.solutions

    def set_solutions(self, solutions):
        self.solutions = solutions
        self.num_solutions = self.solutions.shape[0]

    def get_num_solutions(self):
        return self.num_solutions

    def get_fitnesses(self):
        fitnesses = np.zeros(self.num_solutions)

        for i in range(self.num_solutions):
            fitnesses[i] = self.nk.calculate_fitness(self.solutions[i])

        return fitnesses

    def get_nk_model(self):
        return self.nk

max_stars_repo_name: joshnunley/voting-optimization

max_issues_repo_name: joshnunley/voting-optimization

max_forks_repo_name: joshnunley/voting-optimization