text:
"""
@author: ahmed allam <ahmed.allam@yale.edu>

"""

import numpy
from .hosemi_crf_ad import HOSemiCRFADModelRepresentation, HOSemiCRFAD
from .utilities import HO_AStarSearcher, vectorized_logsumexp

class HOCRFADModelRepresentation(HOSemiCRFADModelRepresentation):
    """Model representation that will hold data structures to be used in :class:`HOCRFAD` class

       it includes all attributes in the  :class:`HOSemiCRFADModelRepresentation` parent class
    """

    def __init__(self):
        # call super class
        super().__init__()

    def filter_activated_states(self, activated_states, accum_active_states, boundary):
        """filter/prune states and y features

           Args:
               activated_states: dictionary containing possible active states/y features
                                it has the form {patt_len:{patt_1, patt_2, ...}}
               accum_active_states: dictionary of only possible active states by position
                                    it has the form {pos_1:{state_1, state_2, ...}}
               boundary: tuple (u,v) representing the current boundary in the sequence
        """

        Z_elems = self.Z_elems
        filtered_activestates = {}
        __, pos = boundary

        for z_len in activated_states:
            if z_len == 1:
                continue
            start_pos = pos - z_len + 1
            if (start_pos, start_pos) in accum_active_states:
                filtered_activestates[z_len] = set()
                for z_patt in activated_states[z_len]:
                    check = True
                    zelems = Z_elems[z_patt]
                    for i in range(z_len):
                        pos_bound = (start_pos + i, start_pos + i)
                        if pos_bound not in accum_active_states:
                            check = False
                            break
                        if zelems[i] not in accum_active_states[pos_bound]:
                            check = False
                            break
                    if check:
                        filtered_activestates[z_len].add(z_patt)
        return filtered_activestates

class HOCRFAD(HOSemiCRFAD):
    """higher-order CRF model that uses algorithmic differentiation in gradient computation

      Args:
          model: an instance of :class:`HOCRFADModelRepresentation` class
          seqs_representer: an instance of :class:`SeqsRepresenter` class
          seqs_info: dictionary holding sequences info

      Keyword Arguments:
          load_info_fromdisk: integer from 0 to 5 specifying number of cached data
                              to be kept in memory. 0 means keep everything while
                              5 means load everything from disk

      Attributes:
          model: an instance of :class:`HOCRFADModelRepresentation` class
          weights: a numpy vector representing feature weights
          seqs_representer: an instance of :class:`pyseqlab.feature_extraction.SeqsRepresenter` class
          seqs_info: dictionary holding sequences info
          beam_size: determines the size of the beam for state pruning
          fun_dict: a function map
          def_cached_entities: a list of the names of cached entities sorted (descending)
                               based on estimated space required in memory

    """

    def __init__(self, model, seqs_representer, seqs_info, load_info_fromdisk=5):

        super().__init__(model, seqs_representer, seqs_info, load_info_fromdisk)

    def compute_fpotential(self, w, active_features):
        """compute the potential of active features in a specified boundary

           Args:
               w: weight vector (numpy vector)
               active_features: dictionary of activated features in a specified boundary
        """
        model = self.model
        pky_codebook = model.pky_codebook
        z_pky_map = model.z_pky_map
        f_potential = numpy.zeros(len(pky_codebook))

        # to consider caching the w_indx and fval as in cached_pf
        for z in active_features:
            w_indx, f_val = active_features[z]
            potential = numpy.dot(w[w_indx], f_val)
            # get all pky's in coded format where z maintains a suffix relation with them
            pky_c_list = z_pky_map[z]
            f_potential[pky_c_list] += potential

        return f_potential

    def compute_forward_vec(self, w, seq_id):
        """compute the forward matrix (alpha matrix)

           Args:
               w: weight vector (numpy vector)
               seq_id: integer representing unique id assigned to the sequence

           .. note::

              activefeatures need to be loaded first in :attr:`seqs.info`
        """
        model = self.model
        pi_pky_map = model.pi_pky_map
        P_codebook = model.P_codebook
        P_len = model.P_len
        T = self.seqs_info[seq_id]["T"]
        active_features = self.seqs_info[seq_id]["activefeatures"]

        alpha = numpy.ones((T + 1, len(P_codebook)), dtype="longdouble") * (-numpy.inf)
        alpha[0, P_codebook[""]] = 0

        fpotential_perboundary = {}
        for j in range(1, T + 1):
            boundary = (j, j)
            # compute f_potential
            f_potential = self.compute_fpotential(w, active_features[boundary])
            fpotential_perboundary[boundary] = f_potential
            for pi in pi_pky_map:
                pi_c = P_codebook[pi]
                if j >= P_len[pi]:
                    pky_c_list, pk_c_list = pi_pky_map[pi]
                    vec = f_potential[pky_c_list] + alpha[j - 1, pk_c_list]
                    alpha[j, pi_c] = vectorized_logsumexp(vec)

        self.seqs_info[seq_id]["fpotential"] = fpotential_perboundary

        return alpha

    def compute_backward_vec(self, w, seq_id):
        """compute the backward matrix (beta matrix)

           Args:
               w: weight vector (numpy vector)
               seq_id: integer representing unique id assigned to the sequence

           .. note::

              fpotential per boundary dictionary should be available in :attr:`seqs.info`
        """
        model = self.model
        pi_pky_map = model.pi_pky_map
        P_codebook = model.P_codebook
        len_P = len(P_codebook)
        T = self.seqs_info[seq_id]["T"]
        fpotential_perboundary = self.seqs_info[seq_id]["fpotential"]

        beta = numpy.ones((T + 2, len(P_codebook)), dtype="longdouble") * (-numpy.inf)
        beta[T + 1, :] = 0

        for j in reversed(range(1, T + 1)):
            track_comp = numpy.ones((len_P, len_P), dtype="longdouble") * (-numpy.inf)
            f_potential = fpotential_perboundary[j, j]
            for pi in pi_pky_map:
                pi_c = P_codebook[pi]
                pky_c_list, pk_c_list = pi_pky_map[pi]
                vec = f_potential[pky_c_list] + beta[j + 1, pi_c]
                track_comp[pk_c_list, pi_c] = vec
            for p_c in P_codebook.values():
                beta[j, p_c] = vectorized_logsumexp(track_comp[p_c, :])
        return beta

    def compute_marginals(self, seq_id):
        """ compute the marginal (i.e. probability of each y pattern at each position)

            Args:
                seq_id: integer representing unique id assigned to the sequence

            .. note::

               - fpotential per boundary dictionary should be available in :attr:`seqs.info`
               - alpha matrix should be available in :attr:`seqs.info`
               - beta matrix should be available in :attr:`seqs.info`
               - Z (i.e. P(x)) should be available in :attr:`seqs.info`
        """
        model = self.model
        Z_codebook = model.Z_codebook
        z_pi_piy = model.z_pi_piy_map
        T = self.seqs_info[seq_id]["T"]
        L = self.model.L

        alpha = self.seqs_info[seq_id]["alpha"]
        beta = self.seqs_info[seq_id]["beta"]
        Z = self.seqs_info[seq_id]["Z"]
        fpotential_perboundary = self.seqs_info[seq_id]["fpotential"]

        P_marginals = numpy.zeros((T + 1, len(model.Z_codebook)), dtype="longdouble")

        for j in range(1, T + 1):
            for d in range(L):
                u = j
                v = j + d
                if v > T:
                    break
                boundary = (u, v)
                f_potential = fpotential_perboundary[boundary]
                for z in Z_codebook:
                    pi_c, piy_c, pk_c = z_pi_piy[z]
                    numerator = (
                        alpha[u - 1, pi_c] + f_potential[piy_c] + beta[v + 1, pk_c]
                    )
                    P_marginals[j, Z_codebook[z]] = numpy.exp(
                        vectorized_logsumexp(numerator) - Z
                    )

        return P_marginals

    #     def compute_marginals(self, w, seq_id):
    #         model = self.model
    #         P_codebook = model.P_codebook
    #         len_P = len(P_codebook)
    #         pi_z_pk = model.pi_z_pk
    #         Z_codebook = model.Z_codebook
    #         T = self.seqs_info[seq_id]["T"]
    #         alpha = self.seqs_info[seq_id]['alpha']
    #         beta = self.seqs_info[seq_id]['beta']
    #         P_marginals = numpy.zeros((T+1, len(model.Z_codebook)), dtype='longdouble')
    #         print("alpha ", alpha)
    #         Z = self.seqs_info[seq_id]['Z']
    #         print("Z ", Z)
    #         fpotential_perboundary = self.seqs_info[seq_id]['fpotential']
    #         print(pi_z_pk)
    #         print(P_codebook)
    #         f_transition = model.f_transition
    #         pky_z = model.z_pky
    #         pky_codebook = model.pky_codebook
    #         print("pky_z ", pky_z)
    #         for j in reversed(range(1, T+1)):
    #             marginal_dict = {}
    #             f_potential = fpotential_perboundary[j, j]
    #             for pi in f_transition:
    #                 beta_pi = beta[j+1, P_codebook[pi]]
    #                 for pky in f_transition[pi]:
    #                     pk, y = f_transition[pi][pky]
    #                     accum = alpha[j-1, P_codebook[pk]] + f_potential[pky_codebook[pky]] + beta_pi
    #                     for z_patt in pky_z[pky]:
    #                         if(z_patt in marginal_dict):
    #                             marginal_dict[z_patt] = numpy.logaddexp(marginal_dict[z_patt], accum)
    #                         else:
    #                             marginal_dict[z_patt] = accum
    #             print("j ", j)
    #             print("marginal ", marginal_dict)
    #             for z_patt in marginal_dict:
    #                 P_marginals[j, Z_codebook[z_patt]] = numpy.exp(marginal_dict[z_patt]-Z)
    #         self.seqs_info[seq_id]['P_marginal'] = P_marginals
    #         print(P_marginals)
    #         return(P_marginals)

    def compute_feature_expectation(self, seq_id, P_marginals, grad):
        """compute the features expectations (i.e. expected count of the feature based on learned model)

           Args:
               seq_id: integer representing unique id assigned to the sequence
               P_marginals: probability matrix for y patterns at each position in time
               grad: numpy vector with dimension equal to the weight vector. It represents the gradient
                     that will be computed using the feature expectation and the global features of the sequence

           .. note::

             - activefeatures (per boundary) dictionary should be available in :attr:`seqs.info`
             - P_marginal (marginal probability matrix) should be available in :attr:`seqs.info`
        """
        activefeatures = self.seqs_info[seq_id]["activefeatures"]
        Z_codebook = self.model.Z_codebook
        for boundary, features_dict in activefeatures.items():
            u, __ = boundary
            for z_patt in features_dict:
                w_indx, f_val = features_dict[z_patt]
                grad[w_indx] += f_val * P_marginals[u, Z_codebook[z_patt]]

    def prune_states(self, j, delta, beam_size):
        """prune states that fall off the specified beam size

           Args:
               j: current position (integer) in the sequence
               delta: score matrix
               beam_size: specified size of the beam (integer)
        """
        P_codebook_rev = self.model.P_codebook_rev
        P_elems = self.model.P_elems
        #         pi_lendict = self.model.pi_lendict

        #         # sort the pi in descending order of their score
        #         indx_sorted_pi = numpy.argsort(delta[j,:])[::-1]
        #         # identify states falling out of the beam
        #         indx_falling_pi = indx_sorted_pi[beam_size:]
        #         # identify top-k states/pi
        #         indx_topk_pi = indx_sorted_pi[:beam_size]
        #         # remove the effect of states/pi falling out of the beam
        #         delta[j, indx_falling_pi] = -numpy.inf

        # using argpartition as better alternative to argsort
        indx_partitioned_pi = numpy.argpartition(-delta[j, :], beam_size)
        # identify top-k states/pi
        indx_topk_pi = indx_partitioned_pi[:beam_size]
        #         # identify states falling out of the beam
        #         indx_falling_pi = indx_partitioned_pi[beam_size:]
        #         # remove the effect of states/pi falling out of the beam
        #         delta[j, indx_falling_pi] = -numpy.inf

        # get topk states
        topk_pi = {P_codebook_rev[indx] for indx in indx_topk_pi}
        topk_states = set()
        for pi in topk_pi:
            topk_states.add(P_elems[pi][-1])
        return topk_states

    def viterbi(self, w, seq_id, beam_size, stop_off_beam=False, y_ref=[], K=1):
        """decode sequences using viterbi decoder

           Args:
               w: weight vector (numpy vector)
               seq_id: integer representing unique id assigned to the sequence
               beam_size: integer representing the size of the beam

           Keyword Arguments:
               stop_off_beam: boolean indicating if to stop when the reference state \
                              falls off the beam (used in perceptron/search based learning)
               y_ref: reference sequence list of labels (used while learning)
               K: integer indicating number of decoded sequences required (i.e. top-k list)

        """
        model = self.model
        P_elems = model.P_elems
        pi_pky_map = model.pi_pky_map
        P_codebook = model.P_codebook
        P_codebook_rev = model.P_codebook_rev
        len_P = len(P_codebook)
        P_len = model.P_len
        num_states = model.num_states
        T = self.seqs_info[seq_id]["T"]
        # records max score at every time step
        delta = numpy.ones((T + 1, len_P), dtype="longdouble") * (-numpy.inf)
        # the score for the empty sequence at time 0 is 1
        delta[0, P_codebook[""]] = 0
        back_track = {}
        # records where violation occurs -- it is 1-based indexing
        viol_index = []

        if beam_size == num_states:
            # case of exact search and decoding
            l = {}
            l["activefeatures"] = (seq_id,)
            self.check_cached_info(seq_id, l)
            active_features = self.seqs_info[seq_id]["activefeatures"]
            for j in range(1, T + 1):
                boundary = (j, j)
                # vector of size len(pky)
                f_potential = self.compute_fpotential(w, active_features[boundary])
                # ^print("f_potential ", f_potential)
                for pi in pi_pky_map:
                    pi_c = P_codebook[pi]
                    pky_c_list, pk_c_list = pi_pky_map[pi]
                    vec = f_potential[pky_c_list] + delta[j - 1, pk_c_list]
                    delta[j, pi_c] = numpy.max(vec)
                    # print("max chosen ", delta[j, P_codebook[pi]])
                    argmax_ind = numpy.argmax(vec)
                    # print("argmax chosen ", argmax_ind)
                    pk_c_max = pk_c_list[argmax_ind]
                    pk = P_codebook_rev[pk_c_max]
                    y = P_elems[pk][-1]
                    back_track[j, pi_c] = (pk_c_max, y)

        else:
            # case of inexact search and decoding
            l = dict()
            l["seg_features"] = (seq_id,)
            self.check_cached_info(seq_id, l)
            # tracks active states by boundary
            accum_activestates = {}
            for j in range(1, T + 1):
                boundary = (j, j)
                active_features = self.identify_activefeatures(
                    seq_id, boundary, accum_activestates
                )
                # vector of size len(pky)
                f_potential = self.compute_fpotential(w, active_features)
                # ^print("f_potential ", f_potential)
                for pi in pi_pky_map:
                    pi_c = P_codebook[pi]
                    pky_c_list, pk_c_list = pi_pky_map[pi]
                    vec = f_potential[pky_c_list] + delta[j - 1, pk_c_list]
                    delta[j, pi_c] = numpy.max(vec)
                    # print("max chosen ", delta[j, P_codebook[pi]])
                    argmax_ind = numpy.argmax(vec)
                    # print("argmax chosen ", argmax_ind)
                    pk_c_max = pk_c_list[argmax_ind]
                    pk = P_codebook_rev[pk_c_max]
                    y = P_elems[pk][-1]
                    back_track[j, pi_c] = (pk_c_max, y)

                topk_states = self.prune_states(j, delta, beam_size)
                # update tracked active states -- to consider renaming it
                accum_activestates[boundary] = accum_activestates[
                    boundary
                ].intersection(topk_states)
                # ^print('delta[{},:] = {} '.format(j, delta[j,:]))
                # ^print("topk_states ", topk_states)
                if y_ref:
                    if y_ref[j - 1] not in topk_states:
                        viol_index.append(j)
                        if stop_off_beam:
                            T = j
                            break
        if K == 1:
            # decoding the sequence
            Y_decoded = []
            p_T_c = numpy.argmax(delta[T, :])
            p_T = P_codebook_rev[p_T_c]
            y_T = P_elems[p_T][-1]
            Y_decoded.append((p_T_c, y_T))
            t = T - 1
            while t > 0:
                p_tplus1_c = Y_decoded[-1][0]
                p_t_c, y_t = back_track[(t + 1, p_tplus1_c)]
                Y_decoded.append((p_t_c, y_t))
                t -= 1
            Y_decoded.reverse()

            Y_decoded = [yt for __, yt in Y_decoded]
            #             print("Y_decoded {}".format(Y_decoded))
            #             print('delta ', delta)
            #             print('backtrack ', back_track)
            #             print("P_codebook ", P_codebook)
            return (Y_decoded, viol_index)
        else:
            asearcher = HO_AStarSearcher(P_codebook_rev, P_elems)
            topK = asearcher.search(delta, back_track, T, K)
            #             print('topk ', topK)
            return (topK, viol_index)

if __name__ == "__main__":
    pass

max_stars_repo_name: bratao/-PySeqLab

max_issues_repo_name: bratao/-PySeqLab

max_forks_repo_name: bratao/-PySeqLab


text:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from Particle import Particle
import math
import copy

class SolarSystem:

    Bodies = {
    "Sun": Particle(np.array([-5.268391798044327E+08, 1.121961619492936E+09, 2.303527271642291E+06]), np.array([-1.458972541688275E+01, -2.832859102319356, 4.012008784973518E-01]), np.array([0,0,0]),'Sun', 1988500e24),
    "Mercury": Particle(np.array([-5.669018162803669E+10, 7.008195061744226E+09, 5.635150439174352E+09]), np.array([-1.521806038600316E+04, -4.635893621771400E+04, -2.392854154766454E+03]), np.array([0,0,0]),'Mercury', 3.302e23),
    "Venus": Particle(np.array([8.246543740774950E+10, -6.919156287526262E+10, -5.751914587944355E+09]), np.array([2.240047318813742E+04, 2.658488246369971E+04, -9.282854972459447E+02]), np.array([0,0,0]),'Venus', 48.685e23),
    "Earth": Particle(np.array([5.906912657111683E+10, 1.361364615947178E+11, -4.053347636044025E+06]), np.array([-2.776505428873560E+04, 1.191351144213665E+04, 1.049235001183924E-06]), np.array([0,0,0]),'Earth', 5.97237e24),
    "Mars": Particle(np.array([-2.269765923061599E+11, -8.439854451129478E+10, 3.766406579966728E+09]), np.array([9.450111587574932E+03, -2.059773509516776E+04, -6.633806333549366E+02]), np.array([0,0,0]),'Mars', 6.4171e23),
    "Jupiter": Particle(np.array([4.702337191946314E+10, -7.809871652380029E+11, 2.186985246720254E+09]), np.array([1.288248808230946E+04, 1.408096008552179E+03, -2.940330994804424E+02]), np.array([0,0,0]),'Jupiter', 1898.13e24),
    "Saturn": Particle(np.array([5.470899048658303E+11, -1.396979427977031E+12, 2.511176088232636E+09]), np.array([8.458315571985436E+03, 3.494319061589365E+03, -3.976061988801199E+02]), np.array([0,0,0]),'Saturn', 5.6834e26),
    "Uranus": Particle(np.array([2.436281384336293E+12, 1.690650542307573E+12, -2.528320960737050E+10]), np.array([-3.932436201328645E+03, 5.277484871220007E+03, 7.089560790105365E+01]), np.array([0,0,0]),'Uranus', 86.813e24),
    "Neptune": Particle(np.array([4.371367805850703E+12, -9.643277653361726E+11, -8.088408981320387E+10]), np.array([1.134049327787622E+03, 5.339872901957968E+03, -1.355977575275114E+02]), np.array([0,0,0]),'Neptune', 102.413e24),
    "Pluto": Particle(np.array([1.928267996633479E+12, -4.694008966102598E+12, -5.548298611397791E+10]), np.array([5.144539834778824E+03, 8.922443245187941E+02, -1.603509104441115E+03]), np.array([0,0,0]),'Pluto', 1.307e22)
    }
    #Bodies can be modified, added or removed

    delta=3600      #time step in seconds, can be changed
    time=31500000   #total time the simulation is run for in seconds, can be changed

    axis_xscale=[-5*10**12,5*10**12] #modifies the x axis of the graph plotted by the Plot() function
    axis_yscale=[-5*10**12,5*10**12] #modifies the y axis of the graph plotted by the Plot() function
    axis_zscale=[-5*10**12,5*10**12] #modifies the z axis of the graph plotted by the Plot() function

    xpoints={}
    ypoints={}
    zpoints={}

    Data=[]     #this will become a list of all the positions, velocities and accelerations of all particles for every value of delta

    for body in Bodies:     #creates a list of x, y and z co-ordinates for every body in SolarSystem.Bodies, allowing bodies to be added and removed without having to change the code
        xpoints[body]=[]
        ypoints[body]=[]
        zpoints[body]=[]

    acc=np.array([0,0,0], dtype=float)
    for i in range(time):
        if i % delta == 0:  #ensures the calculations are done for multiples of the time step delta supplied
            for j in Bodies:
                Bodies[j].acceleration=np.array([0,0,0], dtype=float)
                for k in Bodies:
                    if k==j:
                        continue    #so the body does not interact with itself
                    else:
                        r=np.linalg.norm(Bodies[k].position-Bodies[j].position)
                        Vector=(Bodies[k].position-Bodies[j].position)/r
                        acc=(Particle.G*Bodies[k].mass/(r**2))*Vector
                        Bodies[j].acceleration+=np.array(acc, dtype=float)

                Bodies[j].update(delta)
                xpoints[j].append(Bodies[j].position[0])
                ypoints[j].append(Bodies[j].position[1])
                zpoints[j].append(Bodies[j].position[2])

            item = [i, copy.deepcopy(Bodies)]
            Data.append(item)

ext: py

reason: import numpy


text:
import importlib
import os
import pathlib
import random
import sys
import pandas as pd
import numpy as np
from functools import partial
from joblib import func_inspect as jfi

from ..logging import logger

__all__ = [
    'deserialize_partial',
    'normalize_labels',
    'partial_call_signature',
    'read_space_delimited',
    'reservoir_sample',
    'serialize_partial',
]

_MODULE = sys.modules[__name__]
_MODULE_DIR = pathlib.Path(os.path.dirname(os.path.abspath(__file__)))

def read_space_delimited(filename, skiprows=None, class_labels=True, metadata=None):
    """Read an space-delimited file

    Data is space-delimited. Last column is the (string) label for the data

    Note: we can't use automatic comment detection, as `#` characters are also
    used as data labels.

    Parameters
    ----------
    skiprows: list-like, int or callable, optional
        list of rows to skip when reading the file. See `pandas.read_csv`
        entry on `skiprows` for more
    class_labels: boolean
        if true, the last column is treated as the class (target) label
    """
    with open(filename, 'r') as fd:
        df = pd.read_csv(fd, skiprows=skiprows, skip_blank_lines=True,
                           comment=None, header=None, sep=' ', dtype=str)
        # targets are last column. Data is everything else
        if class_labels is True:
            target = df.loc[:, df.columns[-1]].values
            data = df.loc[:, df.columns[:-1]].values
        else:
            data = df.values
            target = np.zeros(data.shape[0])
        return data, target, metadata

def normalize_labels(target):
    """Map an arbitary target vector to an integer vector

    Returns
    -------
    tuple: (mapped_target, label_map)

    where:
        mapped_target: integer vector of same shape as target
        label_map: dict mapping mapped_target integers to original labels

    Examples
    --------
    >>> target = np.array(['a','b','c','a'])
    >>> mapped_target, label_map = normalize_labels(target)
    >>> mapped_target
    array([0, 1, 2, 0])

    The following should always be true

    >>> all(np.vectorize(label_map.get)(mapped_target) == target)
    True
    """
    label_map = {k:v for k, v in enumerate(np.unique(target))}
    label_map_inv = {v:k for k, v in label_map.items()}
    mapped_target = np.vectorize(label_map_inv.get)(target)

    return mapped_target, label_map

def partial_call_signature(func):
    """Return the fully qualified call signature for a (partial) function
    """
    func = partial(func)
    fa = jfi.getfullargspec(func)
    default_kw = {}
    if fa.args:
        default_kw = dict(zip(fa.args, fa.defaults))
    if getattr(fa, 'kwonlydefaults', None):
        fq_keywords = {**default_kw, **fa.kwonlydefaults}
    else:
        fq_keywords = default_kw
    return jfi.format_signature(func.func, *func.args, **fq_keywords)

def process_dataset_default(metadata=None, **kwargs):
    """Placeholder for data processing function"""
    dataset_name = kwargs.get('dataset_name', 'unknown-dataset')
    logger.warning(f"Default {dataset_name}: Add parse function to generate `data` or `target`")
    return None, None, metadata

def deserialize_partial(func_dict, delete_keys=False,
                        output_key_base='load_function'):
    """Convert a serialized function call into a partial

    if there is an error, returns a default function (process_dataset_default)

    Parameters
    ----------
    func_dict: dict containing
        {output_key_base}_name: function name
        {output_key_base}_module: module containing function
        {output_key_base}_args: args to pass to function
        {output_key_base}_kwargs: kwargs to pass to function
    """

    if delete_keys:
        args = func_dict.pop(f"{output_key_base}_args", [])
        kwargs = func_dict.pop(f"{output_key_base}_kwargs", {})
        base_name = func_dict.pop(f"{output_key_base}_name", 'process_dataset_default')
        func_mod_name = func_dict.pop(f'{output_key_base}_module', None)
    else:
        args = func_dict.get(f"{output_key_base}_args", [])
        kwargs = func_dict.get(f"{output_key_base}_kwargs", {})
        base_name = func_dict.get(f"{output_key_base}_name", 'process_dataset_default')
        func_mod_name = func_dict.get(f'{output_key_base}_module', None)

    fail_func = partial(process_dataset_default, dataset_name=base_name)

    try:
        if func_mod_name:
            func_mod = importlib.import_module(func_mod_name)
        else:
            func_mod = _MODULE
        func_name = getattr(func_mod, base_name, fail_func)
    except ModuleNotFoundError as e:
        logger.error(f"Invalid parse_function: {e}")
        func_name = fail_func
    func = partial(func_name, *args, **kwargs)

    return func

def serialize_partial(func_name, output_key_base='load_function'):
    """Serialize a function call to a dictionary.

    Parameters
    ----------
    func_name: partial function.
    output_key_base

    Returns
    -------
    dict containing:
        {output_key_base}_name: function name
        {output_key_base}_module: fully-qualified module name containing function
        {output_key_base}_args: args to pass to function
        {output_key_base}_kwargs: kwargs to pass to function
    """

    entry = {}
    if func_name is None:
        logger.warning(f"serialize_partial: `{output_key_base}` is None. Ignoring.")
        return entry
    func = partial(func_name)
    entry[f'{output_key_base}_module'] = ".".join(jfi.get_func_name(func.func)[0])
    entry[f'{output_key_base}_name'] = jfi.get_func_name(func.func)[1]
    entry[f'{output_key_base}_args'] = func.args
    entry[f'{output_key_base}_kwargs'] = func.keywords
    return entry

def reservoir_sample(filename, n_samples=1, random_seed=None):
    """Return a random subset of lines from a file

    Parameters
    ----------
    filename: path
        File to be loaded
    n_samples: int
        number of lines to return
    random_seed: int or None
        If set, use this as the random seed
    """
    if random_seed is not None:
        random.seed(random_seed)
    sample = []
    with open(filename) as f:
        for n, line in enumerate(f):
            if n < n_samples:
                sample.append(line.rstrip())
            else:
                r = random.randint(0, n_samples)
                if r < n_samples:
                    sample[r] = line.rstrip()
    return sample

max_stars_repo_path: {{ cookiecutter.repo_name }}/{{ cookiecutter.module_name }}/data/utils.py

max_stars_repo_name: paveles/cookiecutter-easydata


text:
#! /Users/rkrsn/miniconda/bin/python
from __future__ import print_function, division

import os
import sys

# Update PYTHONPATH
root = os.path.abspath(os.path.join(os.getcwd().split('src')[0], 'src'))
if not root in sys.path: sys.path.append(root)

import numpy as np
import csv
from random import seed as rseed
# from cliffsDelta import cliffs
from lib.dEvol import tuner
from tools.sk import rdivDemo
from numpy import sum

from Prediction import rforest, Bugs
from methods1 import *

from Planner.CROSSTREES import xtrees
from Planner.HOW import treatments as HOW
from Planner.strawman import strawman

def say(x):
    sys.stdout.write(str(x))

def write2file(data, fname='Untitled', ext='.txt'):
    with open('.temp/' + fname + ext, 'w') as fwrite:
        writer = csv.writer(fwrite, delimiter=',')
        if not isinstance(data[0], list):
            writer.writerow(data)
        else:
            for b in data:
                writer.writerow(b)

def genTable(tbl, rows, name='tmp'):
    header = [h.name for h in tbl.headers[:-1]]
    with open(name + '.csv', 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow(header)
        for el in rows:
            if len(el[:-1]) < len(header):
                writer.writerow(el)
            else:
                writer.writerow(el[:-1])

    return createTbl([name + '.csv'])

class run():
    def __init__(self,
                 pred=rforest, _smoteit=True, _n=-1,
                 _tuneit=False, dataName=None, reps=1):

        self.pred = pred
        self.dataName = dataName
        self.out, self.out_pred = [self.dataName], []
        self._smoteit = _smoteit
        self.train, self.test = self.categorize()
        self.reps = reps
        self._n = _n
        self.tunedParams = None if not _tuneit \
            else tuner(self.pred, self.train[_n])
        self.headers = createTbl(
            self.train[
                self._n],
            isBin=False,
            bugThres=1).headers

    def categorize(self):
        dir = os.path.join(root, 'Data/Jureczko')
        self.projects = [Name for _, Name, __ in walk(dir)][0]
        self.numData = len(self.projects)  # Number of data
        one, two = explore(dir)
        data = [one[i] + two[i] for i in xrange(len(one))]

        def withinClass(data):
            N = len(data)
            return [(data[:n], [data[n]]) for n in range(1, N)]

        def whereis():
            for indx, name in enumerate(self.projects):
                if name == self.dataName:
                    return indx

        try:
            return [
                       dat[0] for dat in withinClass(data[whereis()])], [
                       dat[1] for dat in withinClass(data[whereis()])]  # Train, Test
        except:
            set_trace()

    def logResults(self, *args):
        for a in args:
            print(a)

    def go(self):
        base = lambda X: sorted(X)[-1] - sorted(X)[0]
        newRows = lambda newTab: map(lambda Rows: Rows.cells[:-1], newTab._rows)
        after = lambda newTab: self.pred(
            train_DF,
            newTab,
            tunings=self.tunedParams,
            smoteit=True)
        frac = lambda aft: 1 - (sum([0 if a < 1 else 1 for a in aft]) \
                                / sum([0 if b < 1 else 1 for b in actual]))

        for planner in ['xtrees', 'CD']:
            out = [planner]
            for _ in xrange(self.reps):
                predRows = []
                train_DF = createTbl(self.train[self._n], isBin=True)
                test_df = createTbl(self.test[self._n], isBin=True)
                actual = np.array(Bugs(test_df))
                before = self.pred(train_DF, test_df,
                                   tunings=self.tunedParams,
                                   smoteit=True)

                predRows = [row.cells for row in createTbl(
                    self.test[self._n], isBin=True)._rows if row.cells[-2] > 0]

                predTest = genTable(test_df, rows=predRows, name='Before_temp')

                "Apply Different Planners"
                if planner == 'xtrees':
                    newTab = xtrees(train=self.train[-1],
                                    test_DF=predTest,
                                    bin=False,
                                    majority=True).main()
                    genTable(test_df, rows=newRows(newTab), name='After_xtrees')
                #          set_trace()
                elif planner == 'XTREE':
                    newTab = xtrees(train=self.train[-1],
                                    test_DF=predTest,
                                    bin=False,
                                    majority=False).main()

                elif planner == 'BIC':
                    newTab = HOW(train=self.train[-1],
                                 test=self.test[-1],
                                 test_df=predTest).main()

                elif planner == 'CD':
                    newTab = strawman(train=self.train[-1], test=self.test[-1]).main()

                elif planner == 'CD+FS':
                    newTab = strawman(train=self.train[-1], test=self.test[-1]
                                      , prune=True).main()

                out.append(frac(after(newTab)))
            #      self.logResults(out)
            yield out

        # ---------- Debug ----------
        #    set_trace()

    def delta1(self, cDict, headers, norm):
        for el in cDict:
            D = len(headers[:-2]) * [0]
            for k in el.keys():
                for i, n in enumerate(headers[:-1]):
                    if n.name[1:] == k:
                        D[i] += 100
            yield D

    def delta0(self, headers, norm, Planner='xtrees'):
        before, after = open('.temp/before.txt'), open('.temp/' + Planner + '.txt')
        D = len(headers[:-1]) * [0]
        for line1, line2 in zip(before, after):
            row1 = np.array([float(l) for l in line1.strip().split(',')[:-2]])
            row2 = np.array([float(l) for l in line2.strip().split(',')[:-1]])
            changed = (row2 - row1).tolist()
            for i, c in enumerate(changed):
                if c > 0:
                    D[i] += 100
        return D

    def deltas(self, planner):
        delta = []
        train_DF = createTbl(self.train[self._n], isBin=True, bugThres=1)
        test_df = createTbl(self.test[self._n], isBin=True, bugThres=1)
        actual = np.array(Bugs(test_df))
        before = self.pred(train_DF, test_df, tunings=self.tunedParams,
                           smoteit=True)
        allRows = np.array(
            map(
                lambda Rows: np.array(
                    Rows.cells[
                    :-
                    1]),
                train_DF._rows +
                test_df._rows))

        def min_max():
            N = len(allRows[0])
            base = lambda X: sorted(X)[-1] - sorted(X)[0]
            return [base([r[i] for r in allRows]) for i in xrange(N)]

        predRows = [row.cells for row in createTbl(self.test[self._n], isBin=True)._rows if row.cells[-2] > 0]

        write2file(predRows, fname='before')  # save file

        """
        Apply Learner
        """
        for _ in xrange(1):
            predTest = genTable(test_df, rows=predRows)

            newRows = lambda newTab: map(lambda Rows: Rows.cells[:-1], newTab._rows)

            "Apply Different Planners"
            if planner == 'xtrees':
                xTrees = xtrees(train=self.train[-1],
                                test_DF=predTest,
                                bin=False,
                                majority=True).main(justDeltas=True)
                delta.append(
                    [d for d in self.delta1(xTrees, train_DF.headers, norm=len(predRows))])
                return (np.sum(
                    delta[0], axis=0) / np.array((len(predRows[0]) - 2) * [len(predRows)])).tolist()

            elif planner == 'XTREE' or planner == 'XTREE':
                C4_5 = xtrees(train=self.train[-1],
                              test_DF=predTest,
                              bin=False, majority=False).main(justDeltas=True)

                delta.append(
                    [d for d in self.delta1(C4_5, train_DF.headers, norm=len(predRows))])
                return (np.sum(
                    delta[0], axis=0) / np.array((len(predRows[0]) - 2) * [len(predRows)])).tolist()

            elif planner == 'BIC':
                how = HOW(train=self.train[-1],
                          test=self.test[-1],
                          test_df=predTest).main(justDeltas=True)
                delta.append(
                    [d for d in self.delta1(how, train_DF.headers, norm=len(predRows))])
                return (np.sum(
                    delta[0], axis=0) / np.array((len(predRows[0]) - 2) * [len(predRows)])).tolist()

            elif planner == 'CD':
                baseln = strawman(
                    train=self.train[-1], test=self.test[-1]).main(justDeltas=True)
                delta.append(
                    [d for d in self.delta1(baseln, train_DF.headers, norm=len(predRows))])
                return (np.sum(
                    delta[0], axis=0) / np.array((len(predRows[0]) - 2) * [len(predRows)])).tolist()

            elif planner == 'CD+FS':
                baselnFss = strawman(
                    train=self.train[-1], test=self.test[-1], prune=True).main(justDeltas=True)
                delta.append(
                    [d for d in self.delta1(baselnFss, train_DF.headers, norm=len(predRows))])
                return (np.sum(
                    delta[0], axis=0) / np.array((len(predRows[0]) - 2) * [len(predRows)])).tolist()

                # -------- DEBUG! --------
                # set_trace()

def deltaCSVwriter0():
    Planners = ['XTREE', 'BIC', 'CD', 'CD+FS']
    print(',%s,%s,%s,%s' % tuple(Planners))
    for name in ['ant', 'ivy', 'jedit', 'lucene', 'poi']:
        say(name)
        delta = []
        R = run(dataName=name, reps=1)  # Setup Files.
        for p in Planners:
            delta.append(R.deltas(planner=p))

        D = np.mean(delta, axis=1).tolist()
        for n in D:
            say(',%0.2f' % (n))
        print('')

# set_trace()

def deltaCSVwriter(type='Indv'):
    if type == 'Indv':
        for name in ['lucene']:
            print('##', name)
            delta = []
            Planners = ['XTREE', 'BIC', 'CD', 'CD+FS']
            R = run(dataName=name, reps=1)  # Setup Files.

            for p in Planners:
                delta.append(R.deltas(planner=p))

            def getRow(i):
                for d in delta:
                    yield d[i]

                #       set_trace()

            with open('/Users/rkrsn/git/GNU-Plots/rkrsn/errorbar/%s.csv' %
                              (name), 'w') as csvfile:
                writer = csv.writer(csvfile, delimiter=' ')
                writer.writerow(["Features"] + Planners)
                for i, h in enumerate(run(dataName=name).headers[:-2]):
                    writer.writerow([h.name[1:]] + [el for el in getRow(i)])
                #      set_trace()
    elif type == 'All':
        delta = []
        for name in ['ivy', 'jedit', 'lucene', 'poi', 'ant']:
            print('##', name)
            delta.extend(run(dataName=name, reps=4).deltas())
        y = np.median(delta, axis=0)
        yhi, ylo = np.percentile(delta, q=[75, 25], axis=0)
        dat1 = sorted([(h.name[1:], a, b, c) for h, a, b, c in zip(
            run(dataName=name).headers[:-2], y, ylo, yhi)], key=lambda F: F[1])
        dat = np.asarray([(d[0], n, d[1], d[2], d[3])
                          for d, n in zip(dat1, range(1, 21))])
        with open('/Users/rkrsn/git/GNU-Plots/rkrsn/errorbar/all.csv', 'w') as csvfile:
            writer = csv.writer(csvfile, delimiter=' ')
            for el in dat[()]:
                writer.writerow(el)

def rdiv():
    lst = []

    def striplines(line):
        listedline = line.strip().split(',')  # split around the = sign
        listedline[0] = listedline[0][2:-1]
        lists = [listedline[0]]
        for ll in listedline[1:-1]:
            lists.append(float(ll))
        return lists

    f = open('./jedit.dat')
    for line in f:
        lst.append(striplines(line[:-1]))

    rdivDemo(lst, isLatex=False)
    set_trace()

def deltaTest():
    for file in ['ivy', 'poi', 'jedit', 'ant', 'lucene']:
        print('##', file)
        R = run(dataName=file, reps=12).deltas()

def _test(file='ant'):
    rseed(1)
    for file in ['ivy', 'lucene', 'jedit', 'poi', 'ant']:
        print('## %s\n' % (file))
        R = [r for r in run(dataName=file, reps=10, _tuneit=False).go()]
        rdivDemo(R, isLatex=False)

if __name__ == '__main__':
    _test()
    # deltaTest()
    # rdiv()
    # deltaCSVwriter(type='All')
    #   deltaCSVwriter(type='Indv')
#  deltaCSVwriter0()
#   eval(cmd())

max_stars_repo_name: ai-se/XTREE

max_issues_repo_name: ai-se/XTREE

max_forks_repo_name: ai-se/XTREE

reason: import numpy,from numpy


text:
import random
from typing import Tuple, List

import cv2
import numpy as np

def fix_seed(seed: int = 0) -> None:
    random.seed(seed)
    np.random.seed(seed)


text:
"""
This script allows the user to create SDF and MJCF robot files
that will be used to realize a simulation with Bullet, MuJoCo
or Gazebo. USE WITH PYTHON3 ONLY!!
"""

from copy import copy
from lxml import etree
import math
import numpy as np

__author__ = "Gabriel Urbain"
__copyright__ = "Copyright 2017, Human Brain Projet, SP10"

__license__ = "MIT"
__version__ = "1.0"
__maintainer__ = "Gabriel Urbain"
__email__ = "gabriel.urbain@ugent.be"
__status__ = "Research"
__date__ = "June 1st, 2017"

class Point(object):
    """
    Class representing a 3D point for geometric manipulations
    """

    def __init__(self, x, y, z, model_scale=1):

        self.x = x
        self.y = y
        self.z = z
        self.model_scale = model_scale

    def offset_by(self, x_offset, y_offset, z_offset):

        self.x = self.x + x_offset
        self.y = self.y + y_offset
        self.z = self.z + z_offset

    def distance_to(self, point):

        return np.sqrt((self.x - point.x)**2 + (self.y - point.y)**2 + (self.z - point.z)**2)

    def rotate_in_yz(self, angle):

        self.z, self.y = self.y*math.cos(angle) - self.z*math.sin(angle), self.y*math.sin(angle) + \
                         self.z*math.cos(angle)

    def get_rescaled_text(self):

        return "{0:.5f} {1:.5f} {2:.5f}".format(self.x / self.model_scale, self.y / self.model_scale, self.z /
                                                self.model_scale)

class Leg(object):
    """
    Create the XML for a single leg
    """

    def __init__(self, leg_id, config, tendons, actuators, sensors, model_scale=1):

        self.leg_id = leg_id
        self.config = config
        self.tendons = tendons
        self.actuators = actuators
        self.sensors = sensors
        self.model_scale = model_scale
        self.tibia_x_offset = 1.05*(-1)**(self.leg_id-1)

        self.femur_angle = 0
        self.femur_joint = None
        self.femur_tibia_joint = None
        self.femur_attachment = None
        self.tibia_attachment = None
        self.tibia_foot = None
        self.tibia_foot2 = None
        self.calc_morphology()

    def calc_tibia_spring_attachment(self):

        tibia_y = (self.config['femur_spring_tibia_joint_dst']**2 + self.config['spring_length']**2 -
                   self.config['tibia_spring_to_joint_dst']**2) / (2 * self.config['femur_spring_tibia_joint_dst'])
        tibia_z = -1.0 * math.sqrt(self.config['spring_length']**2 - tibia_y**2)

        tibia_point = Point(self.tibia_x_offset, tibia_y, tibia_z, self.model_scale)
        femur_point = Point(0, self.config['femur_length'] - self.config['femur_spring_tibia_joint_dst'],
                            0, self.model_scale)
        tibia_point.offset_by(0, self.config['femur_length'] - self.config['femur_spring_tibia_joint_dst'], 0)

        # Do rotation
        rotation_angle = self.femur_angle
        tibia_point.rotate_in_yz(rotation_angle)
        femur_point.rotate_in_yz(rotation_angle)

        return femur_point, tibia_point

    def calc_morphology(self):

        self.femur_angle = (self.config['femur_angle'] - 180) / 360 * 2 * math.pi

        self.femur_joint = Point(0, 0, 0, self.model_scale)
        self.femur_tibia_joint = Point(
            0,
            math.sin(self.femur_angle) * self.config['femur_length'],
            math.cos(self.femur_angle) * self.config['femur_length'], self.model_scale)
        self.femur_attachment, self.tibia_attachment = self.calc_tibia_spring_attachment()

        tibia_foot_y = self.femur_tibia_joint.y + \
                       ((self.femur_tibia_joint.y - self.tibia_attachment.y) /
                        self.config['tibia_spring_to_joint_dst'] *
                        (self.config['tibia_length']-self.config['tibia_spring_to_joint_dst']))
        tibia_foot_z = self.femur_tibia_joint.z + \
                       ((self.femur_tibia_joint.z - self.tibia_attachment.z) /
                        self.config['tibia_spring_to_joint_dst'] *
                        (self.config['tibia_length']-self.config['tibia_spring_to_joint_dst']))

        self.tibia_foot = Point(self.tibia_x_offset, tibia_foot_y, tibia_foot_z, self.model_scale)
        self.tibia_foot2 = Point(self.tibia_x_offset, tibia_foot_y-0.2, tibia_foot_z-0.4, self.model_scale)

    def offset_by(self, x, y, z):

        self.femur_joint.offset_by(x, y, z)
        self.femur_tibia_joint.offset_by(x, y, z)
        self.femur_attachment.offset_by(x, y, z)
        self.tibia_attachment.offset_by(x, y, z)
        self.tibia_foot.offset_by(x, y, z)
        self.tibia_foot2.offset_by(x, y, z)

    def generate_xml_leg(self, x, y, z):

        self.offset_by(x, y, z)

        leg = etree.Element('body')
        femur_geom = etree.Element('geom', type='capsule',
                                   fromto=self.femur_joint.get_rescaled_text() + ' ' +
                                          self.femur_tibia_joint.get_rescaled_text())
        femur_joint = etree.Element('joint', pos=self.femur_joint.get_rescaled_text(),
                                    name='shoulder_' + str(self.leg_id), damping=str(self.config['hip_damping']))
        femur_site = etree.Element('site', name='s'+str(self.leg_id)+'_1',
                                   pos=self.femur_attachment.get_rescaled_text())

        tibia = etree.Element('body')
        tibia_geom = etree.Element('geom', type='capsule',
                                   fromto=self.tibia_attachment.get_rescaled_text() +
                                          ' ' + self.tibia_foot.get_rescaled_text())
        tibia_joint = etree.Element('joint', limited='true', range='-60 0',
                                    pos=self.femur_tibia_joint.get_rescaled_text(),
                                    damping=str(self.config['knee_damping']))
        tibia_site = etree.Element('site', name='s'+str(self.leg_id)+'_2',
                                   pos=self.tibia_attachment.get_rescaled_text())

        foot = etree.Element('body', pos=self.tibia_foot.get_rescaled_text())
        foot_geom = etree.Element('geom', friction='1 0.005 0.0001', type='capsule',
                                  fromto=self.tibia_foot.get_rescaled_text() + ' ' +
                                         self.tibia_foot2.get_rescaled_text())
        foot_site = etree.Element('site', name='sensor_'+str(self.leg_id), pos=self.tibia_foot.get_rescaled_text())

        foot.extend([foot_geom, foot_site])
        tibia.extend([tibia_geom, tibia_joint, tibia_site, foot])
        leg.extend([femur_geom, femur_joint, femur_site, tibia])

        tendon_min = self.femur_attachment.distance_to(self.tibia_attachment) / 10
        tendon_max = tendon_min*10

        tendon = etree.Element('tendon')
        spatial = etree.Element('spatial', stiffness=str(self.config['spring_stiffness']),
                                range="{0:.5f} {1:.5f}".format(tendon_min, tendon_max))
        site1 = etree.Element('site', site='s'+str(self.leg_id)+'_1')
        site2 = etree.Element('site', site='s'+str(self.leg_id)+'_2')

        spatial.extend([site1, site2])
        tendon.append(spatial)
        self.tendons.append(tendon)

        self.actuators.append(etree.Element('position', forcelimited="true", forcerange="-50 50",
                                            joint="shoulder_"+str(self.leg_id), gear="1",
                                            kp=str(self.config['actuator_kp'])))
        self.sensors.append(etree.Element('force', site='sensor_'+str(self.leg_id)))

        return leg

    def get_height(self):

        return -self.tibia_foot.z

class MotorLeg(Leg):
    """
    Create the XML for a single leg and add the motors
    """

    def __init__(self, leg_id, config, tendons, actuators, sensors, model_scale=1):

        self.leg_id = leg_id
        self.config = config
        self.tendons = tendons
        self.actuators = actuators
        self.sensors = sensors
        self.model_scale = model_scale
        self.model_scale = model_scale
        super(MotorLeg, self).__init__(leg_id, config, tendons, actuators, sensors, model_scale)

    def generate_xml(self, torso_width, y, z):

        # x and y are the center of the top plane of motor

        width, length, height = self.config['motor']['width'], \
                                self.config['motor']['length'], self.config['motor']['height']

        # leg_height = self.get_height_with_motor()
        leg_xml = self.generate_xml_leg((-1)**((self.leg_id-1) % 2) *
                                        (torso_width + 0.51), y, z - self.config['motor']['leg_attachment_height'])

        motor = etree.Element('body')
        motor_geom = etree.Element('geom', type="box", mass="0.067",
                                   size="{0:.5f} {1:.5f} {2:.5f}".format(width/2/self.model_scale,
                                                                         length/2/self.model_scale,
                                                                         height/2/self.model_scale),
                                   pos="{0:.5f} {1:.5f} {2:.5f}".format((-1)**((self.leg_id-1)%2) *
                                                                        (torso_width - width/2) / self.model_scale, y /
                                                                        self.model_scale, (z-height/2) /
                                                                        self.model_scale))

        motor.extend([motor_geom, leg_xml])
        return motor

    def get_height_with_motor(self):

        return self.get_height() + self.config['motor']['height'] / 2

class MJCFileGenerator(object):
    """
    Generate a MJFC XML model file. Deprecated!
    """

    def __init__(self, model_config, filename='model.xml', model_scale=1, VERSION=2):

        # Options
        self.model_config = model_config
        self.filename = filename
        self.model_scale = model_scale
        self.VERSION = VERSION

        # Element lists
        self.tendons = []
        self.actuators = []
        self.sensors = []

        # XML anchors
        self.xml_root = None
        self.xml_default = None
        self.xml_compiler = None
        self.xml_option = None
        self.xml_assets = None
        self.xml_body = None
        self.xml_actuators = None
        self.xml_sensors = None

    def generate_xml_header(self):
        """ Generate the XML for all the header fields """

        self.xml_default = etree.Element('default')
        self.xml_root = etree.Element('mujoco', model='quadruped')

        geom = etree.Element('geom', rgba=".9 .7 .1 1", size="0.05", density="1")
        site = etree.Element('site', type="sphere", rgba=".9 .9 .9 1", size="0.005")
        joint = etree.Element('joint', type="hinge", axis="1 0 0", limited="true",
                              range="-100 100", solimplimit="0.95 0.95 0.1")
        tendon = etree.Element('tendon', width="0.02", rgba=".95 .3 .3 1", limited="true",
                               range="0.25 1",  stiffness="4000")
        compiler_settings = {'inertiafromgeom': 'true', 'angle': 'degree', 'coordinate': "global"}

        self.xml_compiler = etree.Element('compiler', **compiler_settings)
        self.xml_option = etree.Element('option', gravity='0 0 -98.1')

        self.xml_default.extend([geom, site, joint, tendon])

    def generate_xml_assets(self):
        """ Generate the XML for all the assets like surface and materials """

        self.xml_assets = etree.Element('asset')

        skybox = etree.Element('texture', type="skybox", builtin="gradient", width="128", height="128",
                               rgb1=".4 .6 .8", rgb2="0 0 0")
        texgeom = etree.Element('texture', name="texgeom", type="cube", builtin="flat", mark="cross", width="127",
                                height="1278", rgb1="0.8 0.6 0.4", rgb2="0.8 0.6 0.4", markrgb="1 1 1", random="0.01")
        texplane = etree.Element('texture', name="texplane", type="2d", builtin="checker", rgb1=".2 .3 .4",
                                 rgb2=".1 0.15 0.2", width="512", height="512")
        matplane = etree.Element('material', name='MatPlane', reflectance='0', texture="texplane",
                                 texrepeat="1 1", texuniform="true")
        geom_material = etree.Element('material', name='geom', texture="texgeom", texuniform="true")

        self.xml_assets.extend([skybox, texgeom, texplane, matplane, geom_material])

    def get_box_geom(self, x, y, z, width, length, height, mass, name=None):
        """ Return the geometry element for a box """

        s = self.model_scale
        el = etree.Element('geom',
                           type="box",
                           mass="{0:.5f}".format(mass),
                           size="{0:.5f} {1:.5f} {2:.5f}".format(width/2/s, length/2/s, height/2/s),
                           pos="{0:.5f} {1:.5f} {2:.5f}".format(x/s, y/s, z/s))

        if name:
            el.set('name', name)

        return el

    def generate_body(self):
        """ Generate the XML for the body """

        leg1 = MotorLeg(1, self.model_config['legs']['FL'], self.tendons,
                        self.actuators, self.sensors, self.model_scale)
        leg2 = MotorLeg(2, self.model_config['legs']['FR'], self.tendons,
                        self.actuators, self.sensors, self.model_scale)
        leg3 = MotorLeg(3, self.model_config['legs']['BL'], self.tendons,
                        self.actuators, self.sensors, self.model_scale)
        leg4 = MotorLeg(4, self.model_config['legs']['BR'], self.tendons,
                        self.actuators, self.sensors, self.model_scale)

        front_conf = model_config['body']['front']
        middle_conf = model_config['body']['middle']
        hind_conf = model_config['body']['hind']

        front_center_y = -(front_conf['length']/2 + middle_conf['length']/2)
        hind_center_y = hind_conf['length']/2 + middle_conf['length']/2

        leg_height = max(leg1.get_height_with_motor(), leg2.get_height_with_motor(),
                         leg3.get_height_with_motor(), leg4.get_height_with_motor())

        self.xml_body = etree.Element('worldbody')
        floor = etree.Element('geom', name='floor', pos='0 0 0', size='50 50 .125',
                              type='plane', material="MatPlane", condim='3')

        torso = etree.Element('body', name='torso')
        torso_geom_f = self.get_box_geom(0, front_center_y, leg_height+front_conf['height']/2, front_conf['width'],
                                         front_conf['length'], front_conf['height'], front_conf['mass'])
        torso_geom_m = self.get_box_geom(0, 0, leg_height+middle_conf['height']/2, middle_conf['width'],
                                         middle_conf['length'], middle_conf['height'], middle_conf['mass'],
                                         name="torso_geom")
        torso_geom_h = self.get_box_geom(0, hind_center_y, leg_height+hind_conf['height']/2, hind_conf['width'],
                                         hind_conf['length'], hind_conf['height'], hind_conf['mass'])

        torso_joint = etree.Element('joint', type='free', limited='false', name='gravity')
        torso_sensor_site = etree.Element('site', name='sensor_torso',
                                          pos="0 0 {0:.5f}".format(leg_height / self.model_scale))

        torso.extend([torso_geom_f, torso_geom_m, torso_geom_h, torso_joint, torso_sensor_site])

        torso.append(leg1.generate_xml(front_conf['width'] / 2, front_center_y, leg_height))
        torso.append(leg2.generate_xml(front_conf['width'] / 2, front_center_y, leg_height))
        torso.append(leg3.generate_xml(hind_conf['width'] / 2, hind_center_y, leg_height))
        torso.append(leg4.generate_xml(hind_conf['width'] / 2, hind_center_y, leg_height))

        battery_weight = model_config.get('battery_weight', 0)

        if battery_weight > 0:
            battery = etree.Element('body', name='battery_body')
            # battery_geom = etree.Element('geom', type="box", size=".17 .37 .105",
            # mass="{0:.5f}".format(battery_weight), pos="0 {0:.5f} {1:.5f}".format(-(length/2)/model_scale +
            # .37, leg_height/model_scale - .105), name='battery_geom')
            battery_geom = self.get_box_geom(0, -(middle_conf['length']/2 +
                                                  front_conf['length'] - 3.7), leg_height - 1.05, 1.7, 3.7, 1.05,
                                             battery_weight)
            battery.append(battery_geom)
            torso.append(battery)

        self.xml_body.extend([floor, torso])

    def generate_actuators(self):
        """ Generate the xml for the actuators """

        self.xml_actuators = etree.Element('actuator')
        self.xml_actuators.extend(self.actuators)

    def generate_sensors(self):
        """ Generate the XML for the sensors """

        self.xml_sensors = etree.Element('sensor')
        self.sensors.append(etree.Element('accelerometer', site='sensor_torso'))

        self.xml_sensors.extend(self.sensors)

    def generate(self, config=None):
        """ Main function called to generate a model """

        # Eventually update model configuration
        self.tendons = []
        self.actuators = []
        self.sensors = []
        if config is not None:
            for key, value in config.items():
                config[key] = value

        # Generate all elements
        self.generate_xml_header()
        self.generate_xml_assets()
        self.generate_body()
        self.generate_actuators()
        self.generate_sensors()

        # Construct the elements tree
        self.xml_root.append(self.xml_compiler)
        self.xml_root.append(self.xml_option)
        self.xml_root.append(self.xml_default)
        self.xml_root.append(self.xml_assets)
        self.xml_root.append(self.xml_body)
        for tendon in self.tendons:
            self.xml_root.append(tendon)
        self.xml_root.append(self.xml_actuators)
        self.xml_root.append(self.xml_sensors)

        # Create the XML file
        f = open(self.filename, 'w')
        f.write(etree.tostring(self.xml_root, pretty_print=True).decode('utf-8'))
        f.flush()
        f.close()

    def get_model_generator_version(self):

        return self.VERSION

class SDFileGenerator(object):
    """
    Generate a SDF XML model file
    """

    def __init__(self, model_config, filename='model.sdf', gazebo=False, model_scale=1, VERSION=2):

        # Options
        self.model_config = model_config
        self.filename = filename
        self.model_scale = model_scale
        self.VERSION = VERSION
        self.gazebo = gazebo

        # XML anchors
        self.xml_world = None
        self.xml_sdf = None
        self.xml_model = None
        self.head_array = []
        self.model_configs_array = []
        self.links_array = []
        self.joints_array = []
        self.plugins_array = []

        self.red = "1 0 0 1"
        self.green = "0 1 0 1"
        self.blue = "0 0 1 1"
        self.white = "1 1 1 1"
        self.black = "0 0 0 0"

        # Parameters
        self.z_offset = self.model_config["default_height"] / self.model_scale
        self.density = self.model_config["default_density"]
        self.front_y = (self.model_config["body"]["middle"]["length"] +
                        self.model_config["body"]["front"]["length"]) / 2 / self.model_scale
        self.back_y = - (self.model_config["body"]["middle"]["length"] +
                         self.model_config["body"]["hind"]["length"]) / 2 / self.model_scale
        self.front_x = self.model_config["body"]["front"]["width"] / 2 / self.model_scale
        self.back_x = self.model_config["body"]["hind"]["width"] / 2 / self.model_scale
        self.back_knee_y = 0
        self.back_knee_z = 0
        self.front_knee_y = 0
        self.front_knee_z = 0
        self.knee_angle = 0

    def generate_xml_header(self):
        """ Generate the XML for all the header fields """

        # Nothing here yet

    def generate_xml_model_configs(self):
        """ Generate the XML for the model config """

        xml_static = etree.Element('static')
        xml_static.text = "False"

        xml_collide = etree.Element('self_collide')
        xml_collide.text = "False"

        self.model_configs_array.extend([xml_static, xml_collide])

    def _create_body(self):
        """ Generate the XML for the body link """

        body = []

        for part in ["front", "middle", "hind"]:

            name = "body_" + part

            w = self.model_config["body"][part]["width"] / self.model_scale
            h = self.model_config["body"][part]["height"] / self.model_scale
            l = self.model_config["body"][part]["length"] / self.model_scale
            # vol = w * l * h
            m = self.model_config["body"][part]["mass"] / (self.model_scale * self.model_scale * self.model_scale)
            ih = m / 12 * (l**2 + w**2)  # z
            iw = m / 12 * (h**2 + l**2)  # x
            il = m / 12 * (h**2 + w**2)  # y

            elem = etree.Element('link', name=name)
            col = etree.Element('collision', name=name + "col")
            vis = etree.Element('visual', name=name + "vis")
            pose_elem = etree.Element('pose')
            inertial = etree.Element('inertial')
            mass = etree.Element('mass')
            inertia = etree.Element('inertia')
            ixx = etree.Element('ixx')
            ixy = etree.Element('ixy')
            ixz = etree.Element('ixz')
            iyy = etree.Element('iyy')
            iyz = etree.Element('iyz')
            izz = etree.Element('izz')

            geometry = etree.Element('geometry')
            box = etree.Element('box')
            size = etree.Element('size')

            material = etree.Element('material')
            ambient = etree.Element('ambient')
            diffuse = etree.Element('diffuse')
            specular = etree.Element('specular')
            emissive = etree.Element('emissive')

            size.text = str(w) + " " + str(l) + " " + str(h)
            mass.text = str(m)
            ixx.text = str(iw)
            ixy.text = str(0)
            ixz.text = str(0)
            iyy.text = str(il)
            iyz.text = str(0)
            izz.text = str(ih)

            if part == "middle":
                ambient.text = self.blue
                diffuse.text = self.blue
                specular.text = self.blue
            else:
                ambient.text = self.red
                diffuse.text = self.red
                specular.text = self.red
            emissive.text = self.black

            if part == "middle":
                pose_elem.text = "0 0 " + str(h / 2 + self.z_offset) + " 0 0 0"
            elif part == "front":
                pose_elem.text = "0 " + str(self.front_y) + " " + str(h / 2 + self.z_offset) + " 0 0 0"
            else:
                pose_elem.text = "0 " + str(self.back_y) + " " + str(h / 2 + self.z_offset) + " 0 0 0"

            inertia.extend([ixx, ixy, ixz, iyy, iyz, izz])
            inertial.extend([mass, inertia])
            box.extend([size])
            geometry.extend([box])
            material.extend([ambient, diffuse, specular, emissive])
            vis.extend([geometry, material])
            col.extend([copy(geometry)])
            elem.extend([pose_elem, inertial, col, vis])

            # Add a fixed joint
            if part != "middle":
                joint = etree.Element("joint", name=name + "_J", type="fixed")
                j_parent = etree.Element("parent")
                j_child = etree.Element("child")
                j_parent.text = "body_middle"
                j_child.text = name
                joint.extend([j_parent, j_child])
                self.joints_array.append(joint)

            body.extend([elem])

        return body

    def _create_leg_motor(self, name, leg_id, config):
        """ Generate the XML for a the down leg link """

        name = name + "M"

        motor_w = config["motor"]["width"] / self.model_scale   # x
        motor_l = config["motor"]["length"] / self.model_scale  # y
        motor_h = config["motor"]["height"] / self.model_scale  # z
        m = config["motor"]["mass"] / (self.model_scale * self.model_scale * self.model_scale)
        # vol = motor_w * motor_l * motor_h

        if leg_id == 1 or leg_id == 2:  # Front
            y = self.front_y
            x = (-1)**(leg_id % 2) * (self.front_x - motor_w / 2)
        else:  # Back
            y = self.back_y
            x = (-1)**(leg_id % 2) * (self.back_x - motor_w / 2)
        z = - motor_h / 2 + self.z_offset

        iw = m/12 * (motor_h**2 + motor_l**2)  # x
        il = m/12 * (motor_h**2 + motor_w**2)  # y
        ih = m/12 * (motor_l**2 + motor_w**2)  # z

        motor = etree.Element('link', name=name)
        pose = etree.Element('pose')
        inertial = etree.Element('inertial')
        mass = etree.Element('mass')
        inertia = etree.Element('inertia')
        ixx = etree.Element('ixx')
        ixy = etree.Element('ixy')
        ixz = etree.Element('ixz')
        iyy = etree.Element('iyy')
        iyz = etree.Element('iyz')
        izz = etree.Element('izz')
        collision = etree.Element('collision', name=name + "_col")
        geometry = etree.Element('geometry')
        box = etree.Element('box')
        size = etree.Element('size')
        visual = etree.Element('visual', name=name + "_vis")
        material = etree.Element('material')
        ambient = etree.Element('ambient')
        diffuse = etree.Element('diffuse')
        specular = etree.Element('specular')
        emissive = etree.Element('emissive')

        pose.text = str(x) + " " + str(y) + " " + str(z) + " 0 0 0"
        size.text = str(motor_w) + " " + str(motor_l) + " " + str(motor_h)
        mass.text = str(m)
        ixx.text = str(iw)
        ixy.text = str(0)
        ixz.text = str(0)
        iyy.text = str(il)
        iyz.text = str(0)
        izz.text = str(ih)
        ambient.text = self.green
        diffuse.text = self.green
        specular.text = self.green
        emissive.text = self.black

        inertia.extend([ixx, ixy, ixz, iyy, iyz, izz])
        inertial.extend([mass, inertia])
        box.extend([size])
        geometry.extend([box])
        material.extend([ambient, diffuse, specular, emissive])
        visual.extend([geometry, material])
        collision.extend([copy(geometry)])
        motor.extend([pose, inertial, collision, visual])

        # Joint
        joint_m = etree.Element("joint", name=name + "J", type="fixed")
        jm_parent = etree.Element("parent")
        jm_child = etree.Element("child")
        if leg_id == 1 or leg_id == 2:  # Front
            jm_parent.text = "body_front"
        else:  # Back
            jm_parent.text = "body_hind"
        jm_child.text = name
        joint_m.extend([jm_parent, jm_child])
        self.joints_array.append(joint_m)

        return motor

    def _create_leg_up(self, name, leg_id, config):
        """ Generate the XML for an upper leg link """

        name_m = name + "M"
        name = name + "U"

        h_attach = config["motor"]["leg_attachment_height"] / self.model_scale  # z
        l = config["femur_length"] / self.model_scale
        r = config["radius"] / self.model_scale

        slack = config["joint_slack"] / self.model_scale
        a = config['femur_angle'] / 360 * 2 * math.pi
        vol = l * math.pi * r**2

        # Attachment point 1
        z_1 = - h_attach + self.z_offset
        if leg_id == 1 or leg_id == 2:  # Front
            y_1 = self.front_y
            x_1 = (-1)**(leg_id % 2) * (self.front_x + r + slack)
        else:  # Back
            y_1 = self.back_y
            x_1 = (-1)**(leg_id % 2) * (self.back_x + r + slack)

        # Center point
        # x = x_1
        # y = y_1 + l * math.sin(a) / 2
        # z = z_1 - l * math.cos(a) / 2

        # Attachment point 2
        if leg_id == 1 or leg_id == 2:  # Front
            self.front_knee_y = y_1 + l * math.sin(a)
            self.front_knee_z = z_1 - l * math.cos(a)
        else:  # Back
            self.back_knee_y = y_1 + l * math.sin(a)
            self.back_knee_z = z_1 - l * math.cos(a)

        m = self.density * vol
        il = m / 2 * r**2
        ir = m / 12 * (3 * r**2 + l**2)
        damp = config['hip_damping']

        femur = etree.Element('link', name=name)
        pose = etree.Element('pose')
        pose_vis_col_in = etree.Element('pose')
        inertial = etree.Element('inertial')
        mass = etree.Element('mass')
        inertia = etree.Element('inertia')
        ixx = etree.Element('ixx')
        ixy = etree.Element('ixy')
        ixz = etree.Element('ixz')
        iyy = etree.Element('iyy')
        iyz = etree.Element('iyz')
        izz = etree.Element('izz')
        collision = etree.Element('collision', name=name + "_col")
        geometry = etree.Element('geometry')
        if self.gazebo:
            cylinder = etree.Element('cylinder')
            radius = etree.Element('radius')
            length = etree.Element('length')
        else:
            cylinder = etree.Element('cylinder', radius=str(r), length=str(l))
        visual = etree.Element('visual', name=name + "_vis")
        material = etree.Element('material')
        ambient = etree.Element('ambient')
        diffuse = etree.Element('diffuse')
        specular = etree.Element('specular')
        emissive = etree.Element('emissive')

        pose.text = str(x_1) + " " + str(y_1) + " " + str(z_1) + " " + str(a) + " 0 0"
        pose_vis_col_in.text = "0 0 " + str(-l / 2) + " 0 0 0"
        mass.text = str(m)
        ixx.text = str(ir)
        ixy.text = str(0)
        ixz.text = str(0)
        iyy.text = str(ir)
        iyz.text = str(0)
        izz.text = str(il)
        ambient.text = self.blue
        diffuse.text = self.blue
        specular.text = self.blue
        emissive.text = self.black
        if self.gazebo:
            radius.text = str(r)
            length.text = str(l)
            cylinder.extend([radius, length])

        inertia.extend([ixx, ixy, ixz, iyy, iyz, izz])
        inertial.extend([copy(pose_vis_col_in), mass, inertia])
        geometry.extend([cylinder])
        material.extend([ambient, diffuse, specular, emissive])
        visual.extend([copy(pose_vis_col_in), geometry, material])
        collision.extend([copy(pose_vis_col_in), copy(geometry)])
        femur.extend([pose, inertial, collision, visual])

        # Joint
        joint = etree.Element("joint", name=name + "J", type="revolute")
        j_parent = etree.Element("parent")
        j_child = etree.Element("child")
        j_axis = etree.Element("axis")
        j_xyz = etree.Element("xyz")
        j_dynamics = etree.Element("dynamics")
        j_damping = etree.Element("damping")
        j_limit = etree.Element("limit")
        j_lower = etree.Element("lower")
        j_upper = etree.Element("upper")

        j_parent.text = name_m
        j_child.text = name
        j_xyz.text = "1 0 0"
        j_damping.text = str(damp)
        j_lower.text = str(-math.pi / 2 - a)
        j_upper.text = str(math.pi / 2 - a)

        j_dynamics.extend([j_damping])
        j_limit.extend([j_lower, j_upper])
        j_axis.extend([j_xyz, j_dynamics])
        joint.extend([j_parent, j_child, j_axis])
        self.joints_array.append(joint)

        return femur

    def _create_leg_down(self, name, leg_id, config):
        """ Generate the XML for a down leg link """

        name_u = name + "U"
        name = name + "D"

        # Tibia values
        e = config["spring_length"] / self.model_scale
        f = config["femur_spring_tibia_joint_dst"] / self.model_scale
        g = config["tibia_spring_to_joint_dst"] / self.model_scale
        a_femur = config['femur_angle'] / 360 * 2 * math.pi
        l = config["tibia_length"] / self.model_scale
        r = config["radius"] / self.model_scale
        slack = config["joint_slack"] / self.model_scale
        compression_tol = config["spring_comp_tol"]
        a_radius = math.acos((f**2 + g**2 - e**2) / (2 * f * g))
        a = a_femur + a_radius
        self.knee_angle = a
        k_spring = config["spring_stiffness"]
        vol = l * math.pi * r**2
        m = self.density * vol
        il = m / 2 * r**2
        ir = m / 12 * (3 * r**2 + l**2)
        damp = config['hip_damping']

        # Foot values
        foot_r = config["foot"]["radius"] / self.model_scale
        # foot_vol = 4 * math.pi * r**3 / 3
        # foot_m = self.density * vol
        mu1 = config["foot"]["mu1"]
        mu2 = config["foot"]["mu2"]

        # Attachment point
        if leg_id == 1 or leg_id == 2:  # Front
            x_1 = (-1)**(leg_id % 2) * (self.front_x + r + slack)
            y_1 = self.front_knee_y
            z_1 = self.front_knee_z
        else:  # Back
            x_1 = (-1)**(leg_id % 2) * (self.back_x + r + slack)
            y_1 = self.back_knee_y
            z_1 = self.back_knee_z

        # Center point
        # x = x_1
        # y = y_1 + (l / 2 - g) * math.sin(a)
        # z = z_1 - (l / 2 - g) * math.cos(a)

        leg_down = etree.Element('link', name=name)
        pose = etree.Element('pose')
        tibia_pose = etree.Element('pose')
        tibia_inertial = etree.Element('inertial')
        tibia_mass = etree.Element('mass')
        tibia_inertia = etree.Element('inertia')
        tibia_ixx = etree.Element('ixx')
        tibia_ixy = etree.Element('ixy')
        tibia_ixz = etree.Element('ixz')
        tibia_iyy = etree.Element('iyy')
        tibia_iyz = etree.Element('iyz')
        tibia_izz = etree.Element('izz')
        tibia_collision = etree.Element('collision', name=name + "T_col")
        tibia_geometry = etree.Element('geometry')
        if self.gazebo:
            tibia_cylinder = etree.Element('cylinder')
            tibia_radius = etree.Element('radius')
            tibia_length = etree.Element('length')
        else:
            tibia_cylinder = etree.Element('cylinder', radius=str(r), length=str(l))

        tibia_visual = etree.Element('visual', name=name + "T_vis")
        tibia_material = etree.Element('material')
        tibia_ambient = etree.Element('ambient')
        tibia_diffuse = etree.Element('diffuse')
        tibia_specular = etree.Element('specular')
        tibia_emissive = etree.Element('emissive')

        pose.text = str(x_1) + " " + str(y_1) + " " + str(z_1) + " " + str(a) + " 0 0"
        tibia_pose.text = "0 0 " + str(l / 2 - g) + " 0 0 0"
        tibia_mass.text = str(m)
        tibia_ixx.text = str(ir)
        tibia_ixy.text = str(0)
        tibia_ixz.text = str(0)
        tibia_iyy.text = str(ir)
        tibia_iyz.text = str(0)
        tibia_izz.text = str(il)
        tibia_ambient.text = self.green
        tibia_diffuse.text = self.green
        tibia_specular.text = self.green
        tibia_emissive.text = self.black
        if self.gazebo:
            tibia_radius.text = str(r)
            tibia_length.text = str(l)
            tibia_cylinder.extend([tibia_radius, tibia_length])

        tibia_inertia.extend([tibia_ixx, tibia_ixy, tibia_ixz, tibia_iyy, tibia_iyz, tibia_izz])
        tibia_inertial.extend([copy(tibia_pose), tibia_mass, tibia_inertia])
        tibia_geometry.extend([tibia_cylinder])
        tibia_material.extend([tibia_ambient, tibia_diffuse, tibia_specular, tibia_emissive])
        tibia_visual.extend([copy(tibia_pose), tibia_geometry, tibia_material])
        tibia_collision.extend([copy(tibia_pose), copy(tibia_geometry)])

        # Foot
        foot_pose = etree.Element('pose')
        foot_collision = etree.Element('collision', name=name + "F_col")
        foot_geometry = etree.Element('geometry')
        if self.gazebo:
            foot_sphere = etree.Element('sphere')
            foot_radius = etree.Element('radius')
        else:
            foot_sphere = etree.Element('cylinder', radius=str(foot_r))
        foot_surface = etree.Element("surface")
        foot_friction = etree.Element("friction")
        foot_ode = etree.Element("ode")
        foot_mu1 = etree.Element("mu")
        foot_mu2 = etree.Element("mu2")

        foot_visual = etree.Element('visual', name=name + "F_vis")
        foot_material = etree.Element('material')
        foot_ambient = etree.Element('ambient')
        foot_diffuse = etree.Element('diffuse')
        foot_specular = etree.Element('specular')
        foot_emissive = etree.Element('emissive')

        foot_pose.text = " 0 0 " + str((-g)) + " 0 0 0"
        foot_mu1.text = str(mu1)
        foot_mu2.text = str(mu2)
        foot_ambient.text = self.green
        foot_diffuse.text = self.green
        foot_specular.text = self.green
        foot_emissive.text = self.black
        if self.gazebo:
            foot_radius.text = str(foot_r)
            foot_sphere.extend([foot_radius])

        foot_ode.extend([foot_mu1, foot_mu2])
        foot_friction.extend([foot_ode])
        foot_surface.extend([foot_friction])
        foot_geometry.extend([foot_sphere])
        foot_material.extend([foot_ambient, foot_diffuse, foot_specular, foot_emissive])
        foot_visual.extend([copy(foot_pose), foot_geometry, foot_material])
        foot_collision.extend([copy(foot_pose), copy(foot_geometry), copy(foot_surface)])

        leg_down.extend([pose, tibia_inertial, tibia_collision, tibia_visual, foot_collision, foot_visual])

        # Joint
        joint = etree.Element("joint", name=name + "J", type="revolute")
        j_parent = etree.Element("parent")
        j_child = etree.Element("child")
        j_axis = etree.Element("axis")
        j_xyz = etree.Element("xyz")
        j_dynamics = etree.Element("dynamics")
        j_damping = etree.Element("damping")
        j_spring_stiffness = etree.Element("spring_stiffness")
        j_limit = etree.Element("limit")
        j_lower = etree.Element("lower")
        j_upper = etree.Element("upper")

        j_parent.text = name_u
        j_child.text = name
        j_xyz.text = "1 0 0"
        j_damping.text = str(damp)
        j_spring_stiffness.text = str(k_spring * g * math.acos((e**2 + g**2 - f**2) / (2 * e * g)))
        j_lower.text = str(- compression_tol)
        j_upper.text = str(math.pi/2)

        j_dynamics.extend([j_damping, j_spring_stiffness])
        j_limit.extend([j_lower, j_upper])
        j_axis.extend([j_xyz, j_dynamics])
        joint.extend([j_parent, j_child, j_axis])
        self.joints_array.append(joint)

        return leg_down

    def generate_xml_links_and_joints(self):
        """ Generate the XML for the model links and joints"""

        body_array = self._create_body()

        legs_array = []
        for key in self.model_config["legs"]:
            leg_id = 0
            if key.find("FL") == 0:
                leg_id = 1
            if key.find("FR") == 0:
                leg_id = 2
            if key.find("BL") == 0:
                leg_id = 3
            if key.find("BR") == 0:
                leg_id = 4
            legs_array.append(self._create_leg_motor(key, leg_id, self.model_config["legs"][key]))
            legs_array.append(self._create_leg_up(key, leg_id, self.model_config["legs"][key]))
            legs_array.append(self._create_leg_down(key, leg_id, self.model_config["legs"][key]))

        self.links_array.extend(body_array)
        self.links_array.extend(legs_array)

    def generate_xml_plugins(self):
        """ Generate the XML for the model plugins """

        xml_plugin = etree.Element('plugin', name='tigrillo_controller', filename='libtigrillo_plugin.so')

        param_p = etree.Element("p")
        param_i = etree.Element("i")
        param_d = etree.Element("d")

        param_p.text = "10.0"
        param_i.text = "30.0"
        param_d.text = "1.0"

        xml_plugin.append(param_p)
        xml_plugin.append(param_i)
        xml_plugin.append(param_d)

        self.plugins_array.extend([xml_plugin])

    def generate_xml_sdf(self):
        """ Generate the XML for all the sdf model """

        if not self.gazebo:
            self.xml_world = etree.Element('world', name="default")
        self.xml_sdf = etree.Element('sdf', version="1.4")
        self.xml_model = etree.Element("model", name="tigrillo")

        self.generate_xml_model_configs()
        self.generate_xml_links_and_joints()
        self.generate_xml_plugins()

        self.xml_model.extend(self.model_configs_array)
        self.xml_model.extend(self.links_array)
        self.xml_model.extend(self.joints_array)

        if self.gazebo:
            self.xml_model.extend(self.plugins_array)
            self.xml_sdf.append(self.xml_model)
        else:
            self.xml_world.append(self.xml_model)
            self.xml_sdf.append(self.xml_world)

    def generate(self):
        """ Main function called to generate a model """

        # Generate all elements
        self.generate_xml_header()
        self.generate_xml_sdf()

        # Create the XML file
        f = open(self.filename, 'w')
        for i, l in enumerate(self.head_array):
            if i == 0:
                f.write(etree.tostring(l, pretty_print=True, xml_declaration=True).decode('utf-8'))
            else:
                f.write(etree.tostring(l, pretty_print=True).decode('utf-8'))
        f.write(etree.tostring(self.xml_sdf, pretty_print=True).decode('utf-8'))
        f.flush()
        f.close()

    def get_model_generator_version(self):

        return self.VERSION

if __name__ == '__main__':

    # MODEL PARAMETERS
    model_config = {
        'default_height': 17,
        'default_density': 1,
        'body': {
            'front': {
                'width': 14,
                'height': 3,
                'length': 8,
                'mass': 2500,
            },
            'middle': {
                'width': 5,
                'height': 0.25,
                'length': 8,
                'mass': 300,
            },
            'hind': {
                'width': 9,
                'height': 2.5,
                'length': 6,
                'mass': 1500,
            },
        },
        'battery_mass': 1170,
        'legs': {
            'FL': {
                'motor': {
                    'width': 3.6,
                    'length': 3.6,
                    'height': 5.06,
                    'leg_attachment_height': 3.3,
                    'mass': 670
                },
                'foot': {
                    'radius': 0.6,
                    'mu1': 1,
                    'mu2': 1,
                },
                'radius': 0.4,
                'joint_slack': 0.5,
                'position': 'front',
                'femur_length': 7,
                'femur_angle': 25,
                'tibia_length': 8.5,
                'spring_length': 2.5,
                'femur_spring_tibia_joint_dst': 4.5,
                'tibia_spring_to_joint_dst': 3.5,
                'hip_damping': 0.2,
                'knee_damping': 0.2,
                'spring_stiffness': 500,
                'spring_comp_tol': 0.1,
                'actuator_kp': 254,
            },
            'FR': {
                'motor': {
                    'width': 3.6,
                    'length': 3.6,
                    'height': 5.06,
                    'leg_attachment_height': 3.3,
                    'mass': 670
                },
                'foot': {
                    'radius': 0.6,
                    'mu1': 1,
                    'mu2': 1,
                },
                'radius': 0.4,
                'joint_slack': 0.5,
                'position': 'front',
                'femur_length': 7,
                'femur_angle': 25,
                'tibia_length': 8.5,
                'spring_length': 2.5,
                'femur_spring_tibia_joint_dst': 4.5,
                'tibia_spring_to_joint_dst': 3.5,
                'hip_damping': 0.2,
                'knee_damping': 0.2,
                'spring_stiffness': 500,
                'spring_comp_tol': 0.1,
                'actuator_kp': 254,
            },
            'BL': {
                'motor': {
                    'width': 3.6,
                    'length': 3.6,
                    'height': 5.06,
                    'leg_attachment_height': 3.3,
                    'mass': 670
                },
                'foot': {
                    'radius': 0.6,
                    'mu1': 1,
                    'mu2': 1,
                },
                'radius': 0.4,
                'joint_slack': 0.5,
                'position': 'back',
                'femur_length': 7,
                'femur_angle': 25,
                'tibia_length': 8.5,
                'spring_length': 2.5,
                'femur_spring_tibia_joint_dst': 4.5,
                'tibia_spring_to_joint_dst': 3.5,
                'hip_damping': 0.2,
                'knee_damping': 0.2,
                'spring_stiffness': 500,
                'spring_comp_tol': 0.1,
                'actuator_kp': 254,
            },
            'BR': {
                'motor': {
                    'width': 3.6,
                    'length': 3.6,
                    'height': 5.06,
                    'leg_attachment_height': 3.3,
                    'mass': 670
                },
                'foot': {
                    'radius': 0.6,
                    'mu1': 1,
                    'mu2': 1,
                },
                'radius': 0.4,
                'joint_slack': 0.5,
                'position': 'back',
                'femur_length': 7,
                'femur_angle': 25,
                'tibia_length': 8.5,
                'spring_length': 2.5,
                'femur_spring_tibia_joint_dst': 4.5,
                'tibia_spring_to_joint_dst': 3.5,
                'hip_damping': 0.2,
                'knee_damping': 0.2,
                'spring_stiffness': 500,
                'spring_comp_tol': 0.1,
                'actuator_kp': 254,
            },
        },
    }

    folder = "../data/robots/"
    gazebo_folder = "/home/gabs48/.gazebo/models/tigrillo/"
    gazebo_folder_small = "/home/gabs48/.gazebo/models/tigrillo_small/"

    # Generate the model as an MJCF file
    fg = MJCFileGenerator(model_config, folder + "tigrillo.mjcf")
    fg.generate()

    # Generate the model as a SDF file
    fg2 = SDFileGenerator(model_config, folder + "tigrillo.sdf")
    fg2.generate()

    # Generate the model as a SDF file
    fg3 = SDFileGenerator(model_config, gazebo_folder + "model.sdf", model_scale=10, gazebo=True)
    fg3.generate()

    # Generate the small model as a SDF file
    fg4 = SDFileGenerator(model_config, gazebo_folder_small + "model.sdf", model_scale=100, gazebo=True)
    fg4.generate()


text:
import random
import sys
from os.path import isfile

import numpy as np
import pandas as pd
from deap import creator, base, tools, algorithms, benchmarks
from deap.benchmarks.tools import hypervolume
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score, train_test_split
from sklearn.preprocessing import LabelEncoder

from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.utils import Bunch, check_random_state

import coop_base

IND_SIZE = coop_base.IND_SIZE  # kích thước một cá thể trong loài
SPECIES_SIZE = coop_base.SPECIES_SIZE  # số cá thể trong loài
TARGET_SIZE = 30  # số cá thể trong target
NUM_SPECIES = 1  # số loài ban đầu

def avg(l):
    """
    Returns the average between list elements
    """
    return sum(l) / float(len(l))

def fetch_datasets(
        data_home=None,
        filter_data=None,
        download_if_missing=True,
        random_state=None,
        shuffle=True,
        verbose=False,

):
    # filename = "datasets\\Landsat7neighbour.txt"
    # filename = "datasets\\landsatImg.txt"
    filename = "datasets\\sat.all.txt"
    # filename = "datasets\\ulc.txt"

    available = isfile(filename)

    df = pd.read_table(filename, header=None, sep=" ")
    # df.to_numpy()

    # encode labels column to numbers
    le = LabelEncoder()
    le.fit(df.iloc[:, -1])
    y = le.transform(df.iloc[:, -1])  # label
    X = df.iloc[:, :-1].to_numpy()  # data
    # X = df.iloc[:, :-1]  # data

    # data = np.load(filename)
    # print(data)
    # X, y = X["data"], y["label"]

    # numpy.set_printoptions(threshold=sys.maxsize)
    # print(X,y)
    # cnt=0
    # lst = data.files
    # for item in lst:
    #     cnt+=1
    #     print(item)
    #     print(data[item])
    #     print(cnt)

    if shuffle:
        ind = np.arange(X.shape[0])
        rng = check_random_state(random_state)
        rng.shuffle(ind)
        X = X[ind]
        y = y[ind]

    dataset = Bunch(data=X, target=y)

    return dataset

def eaGAGA(toolbox, cxpb, mutpb, npop, ngen, stats=None,
           halloffame=None, verbose=__debug__):
    target_set = []

    logbook = tools.Logbook()
    logbook.header = "gen", "species", "evals", "std", "min", "avg", "max"

    # Tạo quần thể có MU cá thể
    # pop = toolbox.population(n=npop)
    target_set = toolbox.population(n=npop)

    # Tạo ngẫu nhiên target set (loài đích) có 30 cá thể
    # for i in range(len(schematas)):
    #     target_set.extend(toolbox.target_set(schematas[i], int(TARGET_SIZE / len(schematas))))

    # tạo NUM_SPECIES (loài), trong mỗi loài có IND_SIZE (cá thể)
    species = [toolbox.species() for _ in range(NUM_SPECIES)]

    # Init with random a representative for each species
    # Tạo ngẫu nhiên một cá thể ưu tú cho mỗi loài
    representatives = [random.choice(s) for s in species]

    g = 0
    # Begin the generational process
    while g < ngen:
        # Initialize a container for the next generation representatives
        # Tạo thùng chứa cá thể ưu tú của thế hệ tiếp theo
        next_repr = [None] * len(species)
        for i, s in enumerate(species):
            # Vary the species individuals
            # Biến đổi các cá thể trong loài
            s = algorithms.varAnd(s, toolbox, 0.6, 1.0)

            r = representatives[:i] + representatives[i + 1:]
            for ind in s:
                ind.fitness.values = toolbox.evaluate([ind] + r, target_set)

            record = stats.compile(s)
            logbook.record(gen=g, species=i, evals=len(s), **record)

            if verbose:
                print(logbook.stream)

            # Select the individuals
            # Lựa chọn các cá thể
            species[i] = toolbox.select(s, len(s))  # Tournament selection
            next_repr[i] = toolbox.get_best(s)[0]  # Best selection

            g += 1

        representatives = next_repr

    # return pop, logbook
    return representatives, logbook

def geneticAlgorithm(X, y, n_population, n_generation):
    """
    Deap global variables
    Initialize variables to use eaSimple
    """
    # create individual
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individual", list, fitness=creator.FitnessMax)

    # create toolbox
    toolbox = base.Toolbox()
    # toolbox.register("attr_bool", random.randint, 0, 1)
    toolbox.register("bit", random.randint, 0, 1)
    # toolbox.register("individual", tools.initRepeat,
    #                  creator.Individual, toolbox.attr_bool, len(X.columns))
    toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.bit, IND_SIZE)
    toolbox.register("species", tools.initRepeat, list, toolbox.individual, SPECIES_SIZE)
    # toolbox.register("target_set", initTargetSet)

    toolbox.register("population", tools.initRepeat, list,
                     toolbox.individual)
    # toolbox.register("evaluate", getFitness, X=X, y=y)
    # toolbox.register("evaluate", benchmarks.rmsClass, X=X, y=y)

    # toolbox.register("mate", tools.cxOnePoint)
    toolbox.register("mate", tools.cxTwoPoint)
    # toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
    toolbox.register("mutate", tools.mutFlipBit, indpb=1. / IND_SIZE)
    toolbox.register("select", tools.selTournament, tournsize=3)
    toolbox.register("get_best", tools.selBest, k=1)
    toolbox.register("evaluate", coop_base.matchSetStrength)

    # initialize parameters
    # pop = toolbox.population(n=n_population)
    hof = tools.HallOfFame(n_population * n_generation)

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)  # trung bình cộng (avg)
    stats.register("std", np.std)  # độ lệch chuẩn
    stats.register("min", np.min)
    stats.register("max", np.max)

    # genetic algorithm
    pop, log = eaGAGA(toolbox, cxpb=0.9, mutpb=0.2,
                      npop=n_population, ngen=n_generation, stats=stats, halloffame=hof,
                      verbose=True)

    # return hall of fame
    return hof

def bestIndividual(hof, X, y):
    """
    Get the best individual
    """
    maxAccurcy = 0.0
    for individual in hof:
        # if (individual.fitness.values > maxAccurcy):
        if individual.fitness.values[0] > maxAccurcy:
            maxAccurcy = individual.fitness.values
            _individual = individual

    _individualHeader = [list(X)[i] for i in range(
        len(_individual)) if _individual[i] == 1]
    return _individual.fitness.values, _individual, _individualHeader

if __name__ == '__main__':
    n_pop = 4
    n_gen = 50  # số thế hệ

    satimage = fetch_datasets()
    X, y = satimage.data, satimage.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)

    X_train_df = pd.DataFrame(data=X_train[0:, 0:], )  # values
    # index = X_train[1:, 0],  # 1st column as index
    # columns = X_train[0, 1:])  # 1st row as the column names

    X_test_df = pd.DataFrame(data=X_test[0:, 0:], )  # values

    X_df = pd.DataFrame(data=X[0:, 0:], )

    # get accuracy with all features
    # individual = [1 for i in range(X.shape[1])]
    individual = [1 for i in range(len(X_df.columns))]
    # print("Train with all features: \t" +
    #       str(getFitness(individual, X_train_df, y_train)) + "\n")
    #
    # print("Test with all features: \t" +
    #       str(getFitness(individual, X_test_df, y_test)) + "\n")

    # apply genetic algorithm
    hof = geneticAlgorithm(X_train_df, y_train, n_pop, n_gen)

    # select the best individual
    accuracy, individual, header = bestIndividual(hof, X, y)
    print('Best Accuracy: \t' + str(accuracy))
    print('Number of Features in Subset: \t' + str(individual.count(1)))
    print('Individual: \t\t' + str(individual))
    print('Feature Subset\t: ' + str(header))

    # print("Test with subset features: \t" +
    #       str(getFitness(individual, X_test_df, y_test)) + "\n")

max_stars_repo_name: tsalems/EvolutionaryAlgorithms

max_issues_repo_name: tsalems/EvolutionaryAlgorithms

max_forks_repo_name: tsalems/EvolutionaryAlgorithms