text:
"""
Arrows
"""
#*****************************************************************************
#       Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>,
#                          William Stein <wstein@gmail.com>,
#                     2008 Mike Hansen <mhansen@gmail.com>,
#                     2009 Emily Kirkman
#
#  Distributed under the terms of the GNU General Public License (GPL)
#
#    This code is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#  The full text of the GPL is available at:
#
#                  http://www.gnu.org/licenses/
#*****************************************************************************
from sage.plot.primitive import GraphicPrimitive
from sage.misc.decorators import options, rename_keyword
from sage.plot.colors import to_mpl_color

class CurveArrow(GraphicPrimitive):
    def __init__(self, path, options):
        """
        Returns an arrow graphics primitive along the provided path (bezier curve).

        EXAMPLES::

            sage: from sage.plot.arrow import CurveArrow
            sage: b = CurveArrow(path=[[(0,0),(.5,.5),(1,0)],[(.5,1),(0,0)]],options={})
            sage: b
            CurveArrow from (0, 0) to (0, 0)
        """
        import numpy as np
        self.path = path
        codes = [1] + (len(self.path[0])-1)*[len(self.path[0])]
        vertices = self.path[0]
        for curve in self.path[1:]:
            vertices += curve
            codes += (len(curve))*[len(curve)+1]
        self.codes = codes
        self.vertices = np.array(vertices, np.float)
        GraphicPrimitive.__init__(self, options)

    def get_minmax_data(self):
        """
        Returns a dictionary with the bounding box data.

        EXAMPLES::

            sage: from sage.plot.arrow import CurveArrow
            sage: b = CurveArrow(path=[[(0,0),(.5,.5),(1,0)],[(.5,1),(0,0)]],options={})
            sage: d = b.get_minmax_data()
            sage: d['xmin']
            0.0
            sage: d['xmax']
            1.0
        """
        return {'xmin': self.vertices[:,0].min(),
                'xmax': self.vertices[:,0].max(),
                'ymin': self.vertices[:,1].min(),
                'ymax': self.vertices[:,1].max()}

    def _allowed_options(self):
        """
        Return the dictionary of allowed options for the curve arrow graphics primitive.

        EXAMPLES::

             sage: from sage.plot.arrow import CurveArrow
             sage: list(sorted(CurveArrow(path=[[(0,0),(2,3)]],options={})._allowed_options().iteritems()))
             [('arrowsize', 'The size of the arrowhead'),
             ('arrowstyle', 'todo'),
             ('head', '2-d only: Which end of the path to draw the head (one of 0 (start), 1 (end) or 2 (both)'),
             ('hue', 'The color given as a hue.'),
             ('legend_color', 'The color of the legend text.'),
             ('legend_label', 'The label for this item in the legend.'),
             ('linestyle', "2d only: The style of the line, which is one of
             'dashed', 'dotted', 'solid', 'dashdot', or '--', ':', '-', '-.',
             respectively."),
             ('rgbcolor', 'The color as an RGB tuple.'),
             ('thickness', 'The thickness of the arrow.'),
             ('width', 'The width of the shaft of the arrow, in points.'),
             ('zorder', '2-d only: The layer level in which to draw')]
        """
        return {'width':'The width of the shaft of the arrow, in points.',
                'rgbcolor':'The color as an RGB tuple.',
                'hue':'The color given as a hue.',
                'legend_label':'The label for this item in the legend.',
                'legend_color':'The color of the legend text.',
                'arrowstyle': 'todo',
                'arrowsize':'The size of the arrowhead',
                'thickness':'The thickness of the arrow.',
                'zorder':'2-d only: The layer level in which to draw',
                'head':'2-d only: Which end of the path to draw the head (one of 0 (start), 1 (end) or 2 (both)',
                'linestyle':"2d only: The style of the line, which is one of "
                "'dashed', 'dotted', 'solid', 'dashdot', or '--', ':', '-', '-.', "
                "respectively."}

    def _repr_(self):
        """
        Text representation of an arrow graphics primitive.

        EXAMPLES::

            sage: from sage.plot.arrow import CurveArrow
            sage: CurveArrow(path=[[(0,0),(1,4),(2,3)]],options={})._repr_()
            'CurveArrow from (0, 0) to (2, 3)'
        """
        return "CurveArrow from %s to %s"%(self.path[0][0],self.path[-1][-1])

    def _render_on_subplot(self, subplot):
        """
        Render this arrow in a subplot.  This is the key function that
        defines how this arrow graphics primitive is rendered in
        matplotlib's library.

        EXAMPLES::

        This function implicitly ends up rendering this arrow on a matplotlib subplot:
            sage: arrow(path=[[(0,1), (2,-1), (4,5)]])
            Graphics object consisting of 1 graphics primitive
        """
        from sage.plot.misc import get_matplotlib_linestyle

        options = self.options()
        width = float(options['width'])
        head = options.pop('head')
        if head == 0: style = '<|-'
        elif head == 1: style = '-|>'
        elif head == 2: style = '<|-|>'
        else: raise KeyError('head parameter must be one of 0 (start), 1 (end) or 2 (both).')
        arrowsize = float(options.get('arrowsize',5))
        head_width=arrowsize
        head_length=arrowsize*2.0
        color = to_mpl_color(options['rgbcolor'])
        from matplotlib.patches import FancyArrowPatch
        from matplotlib.path import Path
        bpath = Path(self.vertices, self.codes)
        p = FancyArrowPatch(path=bpath,
                            lw=width, arrowstyle='%s,head_width=%s,head_length=%s'%(style,head_width, head_length),
                            fc=color, ec=color)
        p.set_linestyle(get_matplotlib_linestyle(options['linestyle'],return_type='long'))
        p.set_zorder(options['zorder'])
        p.set_label(options['legend_label'])
        subplot.add_patch(p)
        return p

class Arrow(GraphicPrimitive):
    """
    Primitive class that initializes the (line) arrow graphics type

    EXAMPLES:

    We create an arrow graphics object, then take the 0th entry
    in it to get the actual Arrow graphics primitive::

        sage: P = arrow((0,1), (2,3))[0]
        sage: type(P)
        <class 'sage.plot.arrow.Arrow'>
        sage: P
        Arrow from (0.0,1.0) to (2.0,3.0)
    """
    def __init__(self, xtail, ytail, xhead, yhead, options):
        """
        Create an arrow graphics primitive.

        EXAMPLES::

            sage: from sage.plot.arrow import Arrow
            sage: Arrow(0,0,2,3,{})
            Arrow from (0.0,0.0) to (2.0,3.0)
        """
        self.xtail = float(xtail)
        self.xhead = float(xhead)
        self.ytail = float(ytail)
        self.yhead = float(yhead)
        GraphicPrimitive.__init__(self, options)

    def get_minmax_data(self):
        """
        Returns a bounding box for this arrow.

        EXAMPLES::

            sage: d = arrow((1,1), (5,5)).get_minmax_data()
            sage: d['xmin']
            1.0
            sage: d['xmax']
            5.0
        """
        return {'xmin': min(self.xtail, self.xhead),
                'xmax': max(self.xtail, self.xhead),
                'ymin': min(self.ytail, self.yhead),
                'ymax': max(self.ytail, self.yhead)}

    def _allowed_options(self):
        """
        Return the dictionary of allowed options for the line arrow graphics primitive.

        EXAMPLES::

             sage: from sage.plot.arrow import Arrow
             sage: list(sorted(Arrow(0,0,2,3,{})._allowed_options().iteritems()))
             [('arrowshorten', 'The length in points to shorten the arrow.'),
             ('arrowsize', 'The size of the arrowhead'),
             ('head',
             '2-d only: Which end of the path to draw the head (one of 0 (start), 1 (end) or 2 (both)'),
             ('hue', 'The color given as a hue.'),
             ('legend_color', 'The color of the legend text.'),
             ('legend_label', 'The label for this item in the legend.'),
             ('linestyle',
             "2d only: The style of the line, which is one of 'dashed',
             'dotted', 'solid', 'dashdot', or '--', ':', '-', '-.',
             respectively."),
             ('rgbcolor', 'The color as an RGB tuple.'),
             ('thickness', 'The thickness of the arrow.'),
             ('width', 'The width of the shaft of the arrow, in points.'),
             ('zorder', '2-d only: The layer level in which to draw')]
        """
        return {'width':'The width of the shaft of the arrow, in points.',
                'rgbcolor':'The color as an RGB tuple.',
                'hue':'The color given as a hue.',
                'arrowshorten':'The length in points to shorten the arrow.',
                'arrowsize':'The size of the arrowhead',
                'thickness':'The thickness of the arrow.',
                'legend_label':'The label for this item in the legend.',
                'legend_color':'The color of the legend text.',
                'zorder':'2-d only: The layer level in which to draw',
                'head':'2-d only: Which end of the path to draw the head (one of 0 (start), 1 (end) or 2 (both)',
                'linestyle':"2d only: The style of the line, which is one of "
                "'dashed', 'dotted', 'solid', 'dashdot', or '--', ':', '-', '-.', "
                "respectively."}

    def _plot3d_options(self, options=None):
        """
        Translate 2D plot options into 3D plot options.

        EXAMPLES::

            sage: P = arrow((0,1), (2,3), width=5)
            sage: p=P[0]; p
            Arrow from (0.0,1.0) to (2.0,3.0)
            sage: q=p.plot3d()
            sage: q.thickness
            5
        """
        if options is None:
            options = self.options()
        options = dict(self.options())
        options_3d = {}
        if 'width' in options:
            options_3d['thickness'] = options['width']
            del options['width']
        # ignore zorder and head in 3d plotting
        if 'zorder' in options:
            del options['zorder']
        if 'head' in options:
            del options['head']
        if 'linestyle' in options:
            del options['linestyle']
        options_3d.update(GraphicPrimitive._plot3d_options(self, options))
        return options_3d

    def plot3d(self, ztail=0, zhead=0, **kwds):
        """
        Takes 2D plot and places it in 3D.

        EXAMPLES::

            sage: A = arrow((0,0),(1,1))[0].plot3d()
            sage: A.jmol_repr(A.testing_render_params())[0]
            'draw line_1 diameter 2 arrow {0.0 0.0 0.0}  {1.0 1.0 0.0} '

        Note that we had to index the arrow to get the Arrow graphics
        primitive.  We can also change the height via the plot3d method
        of Graphics, but only as a whole::

            sage: A = arrow((0,0),(1,1)).plot3d(3)
            sage: A.jmol_repr(A.testing_render_params())[0][0]
            'draw line_1 diameter 2 arrow {0.0 0.0 3.0}  {1.0 1.0 3.0} '

        Optional arguments place both the head and tail outside the
        `xy`-plane, but at different heights.  This must be done on
        the graphics primitive obtained by indexing::

            sage: A=arrow((0,0),(1,1))[0].plot3d(3,4)
            sage: A.jmol_repr(A.testing_render_params())[0]
            'draw line_1 diameter 2 arrow {0.0 0.0 3.0}  {1.0 1.0 4.0} '
        """
        from sage.plot.plot3d.shapes2 import line3d
        options = self._plot3d_options()
        options.update(kwds)
        return line3d([(self.xtail, self.ytail, ztail), (self.xhead, self.yhead, zhead)], arrow_head=True, **options)

    def _repr_(self):
        """
        Text representation of an arrow graphics primitive.

        EXAMPLES::

            sage: from sage.plot.arrow import Arrow
            sage: Arrow(0,0,2,3,{})._repr_()
            'Arrow from (0.0,0.0) to (2.0,3.0)'
        """
        return "Arrow from (%s,%s) to (%s,%s)"%(self.xtail, self.ytail, self.xhead, self.yhead)

    def _render_on_subplot(self, subplot):
        r"""
        Render this arrow in a subplot.  This is the key function that
        defines how this arrow graphics primitive is rendered in
        matplotlib's library.

        EXAMPLES:

        This function implicitly ends up rendering this arrow on
        a matplotlib subplot::

            sage: arrow((0,1), (2,-1))
            Graphics object consisting of 1 graphics primitive

        TESTS:

        The length of the ends (shrinkA and shrinkB) should not depend
        on the width of the arrow, because Matplotlib already takes
        this into account. See :trac:`12836`::

            sage: fig = Graphics().matplotlib()
            sage: sp = fig.add_subplot(1,1,1)
            sage: a = arrow((0,0), (1,1))
            sage: b = arrow((0,0), (1,1), width=20)
            sage: p1 = a[0]._render_on_subplot(sp)
            sage: p2 = b[0]._render_on_subplot(sp)
            sage: p1.shrinkA == p2.shrinkA
            True
            sage: p1.shrinkB == p2.shrinkB
            True

        Dashed arrows should have solid arrowheads,
        :trac:`12852`. This test saves the plot of a dashed arrow to
        an EPS file. Within the EPS file, ``stroke`` will be called
        twice: once to draw the line, and again to draw the
        arrowhead. We check that both calls do not occur while the
        dashed line style is enabled::

            sage: a = arrow((0,0), (1,1), linestyle='dashed')
            sage: filename = tmp_filename(ext='.eps')
            sage: a.save(filename=filename)
            sage: with open(filename, 'r') as f:
            ....:     contents = f.read().replace('\n', ' ')
            sage: two_stroke_pattern = r'setdash.*stroke.*stroke.*setdash'
            sage: import re
            sage: two_stroke_re = re.compile(two_stroke_pattern)
            sage: two_stroke_re.search(contents) is None
            True
        """
        from sage.plot.misc import get_matplotlib_linestyle

        options = self.options()
        head = options.pop('head')
        if head == 0: style = '<|-'
        elif head == 1: style = '-|>'
        elif head == 2: style = '<|-|>'
        else: raise KeyError('head parameter must be one of 0 (start), 1 (end) or 2 (both).')
        width = float(options['width'])
        arrowshorten_end = float(options.get('arrowshorten',0))/2.0
        arrowsize = float(options.get('arrowsize',5))
        head_width=arrowsize
        head_length=arrowsize*2.0
        color = to_mpl_color(options['rgbcolor'])
        from matplotlib.patches import FancyArrowPatch
        p = FancyArrowPatch((self.xtail, self.ytail), (self.xhead, self.yhead),
                            lw=width, arrowstyle='%s,head_width=%s,head_length=%s'%(style,head_width, head_length),
                            shrinkA=arrowshorten_end, shrinkB=arrowshorten_end,
                            fc=color, ec=color)
        p.set_linestyle(get_matplotlib_linestyle(options['linestyle'],return_type='long'))
        p.set_zorder(options['zorder'])
        p.set_label(options['legend_label'])

        if options['linestyle']!='solid':
            # The next few lines work around a design issue in matplotlib. Currently, the specified
            # linestyle is used to draw both the path and the arrowhead.  If linestyle is 'dashed', this
            # looks really odd.  This code is from Jae-Joon Lee in response to a post to the matplotlib mailing
            # list.  See http://sourceforge.net/mailarchive/forum.php?thread_name=CAG%3DuJ%2Bnw2dE05P9TOXTz_zp-mGP3cY801vMH7yt6vgP9_WzU8w%40mail.gmail.com&forum_name=matplotlib-users

            import matplotlib.patheffects as pe
            class CheckNthSubPath(object):
                def __init__(self, patch, n):
                    """
                    creates an callable object that returns True if the provided
                    path is the n-th path from the patch.
                    """
                    self._patch = patch
                    self._n = n

                def get_paths(self, renderer):
                    self._patch.set_dpi_cor(renderer.points_to_pixels(1.))
                    paths, fillables = self._patch.get_path_in_displaycoord()
                    return paths

                def __call__(self, renderer, gc, tpath, affine, rgbFace):
                    path = self.get_paths(renderer)[self._n]
                    vert1, code1 = path.vertices, path.codes
                    import numpy as np

                    return np.array_equal(vert1, tpath.vertices) and np.array_equal(code1, tpath.codes)

            class ConditionalStroke(pe.RendererBase):

                def __init__(self, condition_func, pe_list):
                    """
                    path effect that is only applied when the condition_func
                    returns True.
                    """
                    super(ConditionalStroke, self).__init__()
                    self._pe_list = pe_list
                    self._condition_func = condition_func

                def draw_path(self, renderer, gc, tpath, affine, rgbFace):

                    if self._condition_func(renderer, gc, tpath, affine, rgbFace):
                        for pe1 in self._pe_list:
                            pe1.draw_path(renderer, gc, tpath, affine, rgbFace)

            pe1 = ConditionalStroke(CheckNthSubPath(p, 0),[pe.Stroke()])
            pe2 = ConditionalStroke(CheckNthSubPath(p, 1),[pe.Stroke(linestyle="solid")])
            p.set_path_effects([pe1, pe2])

        subplot.add_patch(p)
        return p

def arrow(tailpoint=None, headpoint=None, **kwds):
    """
    Returns either a 2-dimensional or 3-dimensional arrow depending
    on value of points.

    For information regarding additional arguments, see either arrow2d?
    or arrow3d?.

    EXAMPLES::

        sage: arrow((0,0), (1,1))
        Graphics object consisting of 1 graphics primitive
        sage: arrow((0,0,1), (1,1,1))
        Graphics3d Object
    """
    try:
        return arrow2d(tailpoint, headpoint, **kwds)
    except ValueError:
        from sage.plot.plot3d.shapes import arrow3d
        return arrow3d(tailpoint, headpoint, **kwds)

@rename_keyword(color='rgbcolor')
@options(width=2, rgbcolor=(0,0,1),zorder=2, head = 1, linestyle='solid', legend_label=None)
def arrow2d(tailpoint=None, headpoint=None, path=None, **options):
    """
    If tailpoint and headpoint are provided, returns an arrow from (xmin, ymin)
    to (xmax, ymax).  If tailpoint or headpoint is None and path is not None,
    returns an arrow along the path.  (See further info on paths in bezier_path).

    INPUT:

    - ``tailpoint`` - the starting point of the arrow

    - ``headpoint`` - where the arrow is pointing to

    - ``path`` - the list of points and control points (see bezier_path for detail) that
      the arrow will follow from source to destination

    - ``head`` - 0, 1 or 2, whether to draw the head at the start (0), end (1) or both (2)
      of the path (using 0 will swap headpoint and tailpoint).  This is ignored
      in 3D plotting.

    - ``linestyle`` - (default: ``'solid'``) The style of the line, which is one of
      ``'dashed'``, ``'dotted'``, ``'solid'``, ``'dashdot'``, or ``'--'``, ``':'``,
      ``'-'``, ``'-.'``, respectively.

    - ``width`` - (default: 2) the width of the arrow shaft, in points

    - ``color`` - (default: (0,0,1)) the color of the arrow (as an RGB tuple or a string)

    - ``hue`` - the color of the arrow (as a number)

    - ``arrowsize`` - the size of the arrowhead

    - ``arrowshorten`` - the length in points to shorten the arrow (ignored if using path
      parameter)

    - ``legend_label`` - the label for this item in the legend

    - ``legend_color`` - the color for the legend label

    - ``zorder`` - the layer level to draw the arrow-- note that this is ignored in 3D
      plotting.

    EXAMPLES:

    A straight, blue arrow::

       sage: arrow2d((1, 1), (3, 3))
       Graphics object consisting of 1 graphics primitive

    Make a red arrow::

       sage: arrow2d((-1, -1), (2, 3), color=(1,0,0))
       Graphics object consisting of 1 graphics primitive
       sage: arrow2d((-1, -1), (2, 3), color='red')
       Graphics object consisting of 1 graphics primitive

    You can change the width of an arrow::

        sage: arrow2d((1, 1), (3, 3), width=5, arrowsize=15)
        Graphics object consisting of 1 graphics primitive

    Use a dashed line instead of a solid one for the arrow::

        sage: arrow2d((1, 1), (3, 3), linestyle='dashed')
        Graphics object consisting of 1 graphics primitive
        sage: arrow2d((1, 1), (3, 3), linestyle='--')
        Graphics object consisting of 1 graphics primitive

    A pretty circle of arrows::

        sage: sum([arrow2d((0,0), (cos(x),sin(x)), hue=x/(2*pi)) for x in [0..2*pi,step=0.1]])
        Graphics object consisting of 63 graphics primitives

    If we want to draw the arrow between objects, for example, the
    boundaries of two lines, we can use the arrowshorten option
    to make the arrow shorter by a certain number of points::

        sage: line([(0,0),(1,0)],thickness=10)+line([(0,1),(1,1)], thickness=10)+arrow2d((0.5,0),(0.5,1), arrowshorten=10,rgbcolor=(1,0,0))
        Graphics object consisting of 3 graphics primitives

    If BOTH headpoint and tailpoint are None, then an empty plot is returned::

        sage: arrow2d(headpoint=None, tailpoint=None)
        Graphics object consisting of 0 graphics primitives

    We can also draw an arrow with a legend::

        sage: arrow((0,0), (0,2), legend_label='up', legend_color='purple')
        Graphics object consisting of 1 graphics primitive

    Extra options will get passed on to show(), as long as they are valid::

        sage: arrow2d((-2, 2), (7,1), frame=True)
        Graphics object consisting of 1 graphics primitive
        sage: arrow2d((-2, 2), (7,1)).show(frame=True)
    """
    from sage.plot.all import Graphics
    g = Graphics()
    g._set_extra_kwds(Graphics._extract_kwds_for_show(options))

    if headpoint is not None and tailpoint is not None:
        xtail, ytail = tailpoint
        xhead, yhead = headpoint
        g.add_primitive(Arrow(xtail, ytail, xhead, yhead, options=options))
    elif path is not None:
        g.add_primitive(CurveArrow(path, options=options))
    elif tailpoint is None and headpoint is None:
        return g
    else:
        raise TypeError('Arrow requires either both headpoint and tailpoint or a path parameter.')
    if options['legend_label']:
        g.legend(True)
        g._legend_colors = [options['legend_color']]
    return g

max_stars_repo_name: defeo/sage

max_issues_repo_name: boothby/sage

max_forks_repo_name: boothby/sage

reason: import numpy,from sage


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 17 19:39:09 2021

@author: joannamoraza
"""

#Importing libraries

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

#Importing the dataset

dataset = pd.read_csv('50_Startups.csv')
x = dataset.iloc[:,:-1].values
y = dataset.iloc[:,-1].values

print(x)
print(y)

##Encoding the independent variables

from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
CT = ColumnTransformer(transformers=[('encoder',OneHotEncoder(),[3])],remainder='passthrough')
x = np.array(CT.fit_transform(x))
print(x)

#Splitting the training set and test set

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 1)

print(x_train)
print(x_test)
print(y_train)
print(y_test)

##Note, feature scaling is not necessary with Multiple Regression b/c every variable
##coefficient will compensate for differences in range and put everything on same scale.

#Creating the Multiple Linear Regression Model and Training it

from sklearn.linear_model import LinearRegression
MLR = LinearRegression()
MLR.fit(x_train, y_train)

#Predicting the Test Results

y_pred = MLR.predict(x_test)

#Comparing the Test Results with the Model's Predictions

np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1),y_test.reshape(len(y_test),1)),axis=1))

#BONUS

#How to predict the profit of a startup in California with R&D Spend = $160,000
##and Admin Spend = $130,000 and Marketing = $300,000

print(MLR.predict([[1,0,0, 160000, 130000, 300000]]))

#How to obtain the coefficients of the model.
print(MLR.coef_)
print(MLR.intercept_)
## Profit = -0.0285*Dummy Var1 + 0.0298*Dummy Var2 - 0.0124*Dummy Var3 + 0.774*R&D Spend
## - 0.0094*Admin Spend + 0.0289*Marketing Spend + 49834(intercept)

ext: py

max_stars_repo_path: Machine Learning A-Z/Part 2 - Regression/Section 5 - Multiple Linear Regression/Python/multiple_linear_regression_jm.py

max_stars_repo_name: JMoraza/TodayILearned

max_issues_repo_name: JMoraza/TodayILearned

max_forks_repo_name: JMoraza/TodayILearned

reason: import numpy


text:
from generator import Generator
from statemachine import StateMachine, State, Transition
from tranzycja import *
from nx import *
import networkx as nx
import matplotlib.pyplot as plt

# define states for a master (way of passing args to class)

options = [
    {"name": "Obiekt w podajniku", "initial": True, "value": "obiekt_w_podajniku"}, #0
    {"name": "Przenoszenie do CNC", "initial": False, "value": "przenoszenie_do_cnc"}, #1
    {"name": "CNC", "initial": False, "value": "cnc"}, #2
    {"name": "Kontrola jakosci", "initial": False, "value": "kontrola_jakosci"}, #3
    {"name": "Element odrzucony", "initial": False, "value": "element_odrzucony"}, #4
    {"name": "Przeniesienie obiektu do pudelek", "initial": False, "value": "przeniesienie_obiektu_do_pudelek"}, #5
    {"name": "Odbior", "initial": False, "value": "odbior"}, #6
    {"name": "Proces zatrzymany", "initial": False, "value": "proces_zatrzymany"} #7

]

options_slave = [
    {"name": "Obiekt w CNC", "initial": True, "value": "obiekt_w_cnc"},  # 0
    {"name": "Obrobka", "initial": False, "value": "obrobka"},  # 1
    {"name": "Odlozenie", "initial": False, "value": "odlozenie"}  # 2
]

# create State objects for a master
# ** -> unpack dict to args
master_states = [State(**opt) for opt in options]
slave_states = [State(**opt) for opt in options_slave]

# valid transitions for a master (indices of states from-to)
from_to = [
    [0, [1, 7]],
    [1, [2]],
    [2, [3]],
    [3, [4, 5]],
    [4, [0]],
    [5, [6, 0]],
    [6, [7]],
    [7, [0]],
]

from_to_slave = [
    [0, [1]],
    [1, [2]],
    [2, [0]],
]

slave_states, slave_transitions = setTransition(from_to_slave, slave_states, 's')
master_states, master_transitions = setTransition(from_to, master_states, 'r')

# create a supervisor
supervisor = Generator.create_master(master_states, master_transitions)
slave = Generator.create_master(slave_states, slave_transitions)

print("********START************")
print("Jestes w stanie startowym: ")
print(supervisor.current_state.name)
print("--------------------------")
print("Aby dokonac przejscia podaj wartosc z ktorej tranzycji_do ktorej tranzycji, aby wyjsc wpisz quit")
print("Twoje obecne mozliwe tranzycje to:")
for t in from_to[0][1]:
    too_long = master_transitions[f'0_{t}'].identifier
    if too_long == "0_1":
        print(too_long, " - Otrzymano sygnal o nowym elemencie")
    if too_long == "0_7":
        print(too_long, " - Zatrzymanie procesu")

while 1:

    draw_graph(supervisor.current_state.name)

    print("-------------------")
    x = input("Wpisz wybrana tranzycje: ")
    if x == str('quit'):

        break

    # przejscia slave - master, master - slave

    elif x == str('1_2'):
        supervisor.current_state.name = "CNC"
        draw_graph(supervisor.current_state.name)
        slave.current_state.name = "Obiekt w CNC"
        draw_graph_slave(slave.current_state.name)

    elif x == str('2_0'):
        slave.current_state.name = "Obiekt w CNC"
        draw_graph_slave(slave.current_state.name)

    master_transitions[x]._run(supervisor)
    print("-------------------")
    print("Twoj obecny stan to: ", supervisor.current_state.name)

    print("Obecne mozliwe tranzycje to: ")
    for t in from_to[int(x[2])][1]:
        too_long = master_transitions[f"{int(x[2])}_{t}"].identifier
        state_now = supervisor.current_state.name

        if state_now == "CNC":
            while True:
                if slave.current_state._initial == True:

                    print("0_1 - Zamknięcie drzwi elementu podrzednego")
                a = input("Wpisz wybrana tranzycje: ")
                if a == "2_0":
                    break
                slave_transitions[a]._run(slave)
                draw_graph_slave(slave.current_state.name)

                print("-------------------")
                print("Twoj obecny stan to: ", slave.current_state.name)
                print("Obecne mozliwe tranzycje to: ")

                for y in from_to_slave[int(a[2])][1]:

                    s_too_long = slave_transitions[f"{int(a[2])}_{y}"].identifier
                    if s_too_long == "0_1":
                        print(s_too_long, " - Zamknięcie drzwi, gotowy do obróbki")
                    if s_too_long == "1_2":
                        print(s_too_long, " - Otwarcie drzwi, koniec obróbki")
                    if s_too_long == "2_0":
                        print(s_too_long, " - Odłożenie gotowego elementu i powrot do procesu nadrzednego")

        #print(master_transitions[f"{int(x[2])}_{t}"].identifier)
        if too_long == "0_1":
            print(too_long, " - Otrzymano sygnal o nowym elemencie")
        if too_long == "0_7":
            print(too_long, " - Zatrzymanie procesu")
        if too_long == "1_2":

            print(too_long, " - Odlozenie elementu")
        if too_long == "2_3":
            print(too_long, " - Element do kontroli jakosci")
        if too_long == "3_4":
            print(too_long, " - Element ma wade - odrzuc")
        if too_long == "3_5":
            print(too_long, " - Element bez wad - przyjmij")
        if too_long == "4_0":
            print(too_long, " - Element odrzucony - powrot do poczatku")
        if too_long == "5_0":
            print(too_long, " - Potrzebuje wiecej elementow w pudelku!")
        if too_long == "5_6":
            print(too_long, " - Mam wystarczajaco elementow w pudelku!")
        if too_long == "6_7":
            print(too_long, " - Odebrano elementy, wstrzymaj proces")
        if too_long == "7_0":
            print(too_long, " - Wznowienie procesu")

print("-------------------")
print("Nastapilo wyjscie z programu")
print("-------------------")

reason: import networkx


text:
import numpy as np
import math
import matplotlib.pyplot as plt
from frag_pele.Growing.template_fragmenter import Atom, TemplateOPLS2005, Phi, Theta, Bond

NUM_AVOG = 6.022e23
DIEL_CNST = 1/(4*np.pi*1)
ELECTRON_CHARGE = 1.6022e-19  # C

MAGIC_NUMBER = 10e10/(4.184*1000)
Ein = 1  # ??
Esolv = 80.0  # ??
Evac = 1.257 * 10**(-6)  # H/m
I = 0.15
T = 1000  # K
R = 8.314  # J / K**-1 mol**-1

class ComputeNBONEnergies:
    def __init__(self, epsilon1=0, epsilon2=0, sigma1=0, sigma2=0, charge1=0, charge2=0, radnpSGB1=0, sgbnpType1=0,
                 radnpSGB2=0, sgbnpType2=0, radii=0):
        self.epsilon1 = epsilon1
        self.epsilon2 = epsilon2
        self.sigma1 = sigma1
        self.sigma2 = sigma2
        self.charge1 = charge1
        self.charge2 = charge2
        self.radnpSGB1 = radnpSGB1
        self.sgbnpType1 = sgbnpType1
        self.radnpSGB2 = radnpSGB2
        self.sgbnpType2 = sgbnpType2
        self.radii = radii

    def energy_vdw(self):
        energy = float(2*np.sqrt(self.epsilon1))*float(2*np.sqrt(self.epsilon2))*(((float(self.sigma1)**12) /
                 (float(self.radii)**12)) - ((float(self.sigma2)**6)/(float(self.radii)**6)))
        return energy

    def charge_function(self):
        factor = np.sqrt((DIEL_CNST)*(NUM_AVOG)*(ELECTRON_CHARGE**2)*(MAGIC_NUMBER))
        charge1 = self.charge1 * factor
        charge2 = self.charge2 * factor
        energy = (charge1*charge2)/(1*self.radii)
        return energy

    def solv_pol_energy(self):
        K = 0.73*10**-10*(math.sqrt((2*NUM_AVOG*ELECTRON_CHARGE**2*I)/(Esolv*Evac*R*T)))
        alpha = math.sqrt(self.sgbnpType1*self.sgbnpType2)
        D = (self.radnpSGB1**2/(2*alpha)**2)
        fGB = math.sqrt(self.radnpSGB1**2 + alpha**2 * math.exp(-D))
        energy = ((1/Ein) - (math.exp(-K*fGB) / Esolv)) * ((self.charge1 * self.charge2) / fGB) \
                 - ((1/Ein) - (math.exp(-K*self.sgbnpType1)/Esolv))*(self.charge1**2/self.sgbnpType1)
        return energy

class PlotEnergies:
    def __init__(self, template, atom1, atom2):
        self.template = template
        self.atom1 = atom1
        self.atom2 = atom2
        self.nbon_energy = ComputeNBONEnergies(epsilon1=self.template.list_of_atoms[self.atom1].epsilon,
                                               epsilon2=self.template.list_of_atoms[self.atom2].epsilon,
                                               sigma1=self.template.list_of_atoms[self.atom1].sigma,
                                               sigma2=self.template.list_of_atoms[self.atom2].sigma,
                                               charge1=self.template.list_of_atoms[self.atom2].charge,
                                               charge2=self.template.list_of_atoms[self.atom2].charge,
                                               radnpSGB1=self.template.list_of_atoms[self.atom1].radnpSGB,
                                               radnpSGB2=self.template.list_of_atoms[self.atom2].radnpSGB,
                                               sgbnpType1=self.template.list_of_atoms[self.atom1].sgbnpType,
                                               sgbnpType2=self.template.list_of_atoms[self.atom2].sgbnpType)

    def plot_vdw_function(self):
        t1 = np.arange(0.01, 5.0, 0.001)
        energies = []
        for radii in t1:
            self.nbon_energy.radii = radii
            energy = self.nbon_energy.energy_vdw()
            energies.append(energy)
        return plt.plot(t1, energies, lw=2)

    def plot_charge_function(self):
        t1 = np.arange(0.01, 5.0, 0.001)
        energies = []
        for radii in t1:
            self.nbon_energy.radii = radii
            energy = self.nbon_energy.charge_function()
            energies.append(energy)
        return plt.plot(t1, energies, lw=2)

    def plot_solv_function(self):
        t1 = np.arange(0.01, 5.0, 0.001)
        energies = []
        for radii in t1:
            self.nbon_energy.radii = radii
            energy = self.nbon_energy.solv_pol_energy()
            energies.append(energy)
        return plt.plot(t1, energies, lw=2)

    def plot_nbon_function(self):
        t1 = np.arange(0.01, 5.0, 0.001)
        energies = []
        for radii in t1:
            self.nbon_energy.radii = radii
            energy = self.nbon_energy.energy_vdw()
            energy += self.nbon_energy.charge_function()
            energy += self.nbon_energy.solv_pol_energy()
            energies.append(energy)
        return plt.plot(t1, energies, lw=2)

    def show_plots(self):
        plt.figure(1)
        plt.subplot(221)
        self.plot_vdw_function()
        plt.subplot(222)
        self.plot_charge_function()
        plt.subplot(223)
        self.plot_solv_function()
        plt.subplot(224)
        self.plot_nbon_function()
        plt.show()


text:
# Author: Tomas Hodan (hodantom@cmp.felk.cvut.cz)
# Center for Machine Perception, Czech Technical University in Prague

# Visualizes the object models at the ground truth poses.

import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import cv2

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pysixd import inout, misc, renderer
from params.dataset_params import get_dataset_params

# dataset = 'hinterstoisser'
dataset = 'tless'
# dataset = 'tudlight'
# dataset = 'rutgers'
# dataset = 'tejani'
# dataset = 'doumanoglou'
# dataset = 'toyotalight'

# Dataset parameters
dp = get_dataset_params(dataset)

# Select IDs of scenes, images and GT poses to be processed.
# Empty list [] means that all IDs will be used.
scene_ids = []
im_ids = []
gt_ids = []

# Indicates whether to render RGB image
vis_rgb = True

# Indicates whether to resolve visibility in the rendered RGB image (using
# depth renderings). If True, only the part of object surface, which is not
# occluded by any other modeled object, is visible. If False, RGB renderings
# of individual objects are blended together.
vis_rgb_resolve_visib = True

# Indicates whether to render depth image
vis_depth = False

# If to use the original model color
vis_orig_color = False

# Define new object colors (used if vis_orig_colors == False)
colors = inout.load_yaml('../data/colors.yml')

# Path masks for output images
vis_rgb_mpath = '../output/vis_gt_poses_{}/{:02d}/{:04d}.jpg'
vis_depth_mpath = '../output/vis_gt_poses_{}/{:02d}/{:04d}_depth_diff.jpg'

# Whether to consider only the specified subset of images
use_image_subset = True

# Subset of images to be considered
if use_image_subset:
    im_ids_sets = inout.load_yaml(dp['test_set_fpath'])
else:
    im_ids_sets = None

scene_ids_curr = range(1, dp['scene_count'] + 1)
if scene_ids:
    scene_ids_curr = set(scene_ids_curr).intersection(scene_ids)
for scene_id in scene_ids_curr:
    misc.ensure_dir(os.path.dirname(vis_rgb_mpath.format(dataset, scene_id, 0)))

    # Load scene info and gt poses
    scene_info = inout.load_info(dp['scene_info_mpath'].format(scene_id))
    scene_gt = inout.load_gt(dp['scene_gt_mpath'].format(scene_id))

    # Load models of objects that appear in the current scene
    obj_ids = set([gt['obj_id'] for gts in scene_gt.values() for gt in gts])
    models = {}
    for obj_id in obj_ids:
        models[obj_id] = inout.load_ply(dp['model_mpath'].format(obj_id))

    # Considered subset of images for the current scene
    if im_ids_sets is not None:
        im_ids_curr = im_ids_sets[scene_id]
    else:
        im_ids_curr = sorted(scene_info.keys())

    if im_ids:
        im_ids_curr = set(im_ids_curr).intersection(im_ids)

    # Visualize GT poses in the selected images
    for im_id in im_ids_curr:
        print('scene: {}, im: {}'.format(scene_id, im_id))

        # Load the images
        rgb = inout.load_im(dp['test_rgb_mpath'].format(scene_id, im_id))
        depth = inout.load_depth(dp['test_depth_mpath'].format(scene_id, im_id))
        depth = depth.astype(np.float32) # [mm]
        depth *= dp['cam']['depth_scale'] # to [mm]

        # Render the objects at the ground truth poses
        im_size = (depth.shape[1], depth.shape[0])
        ren_rgb = np.zeros(rgb.shape, np.float32)
        ren_rgb_info = np.zeros(rgb.shape, np.uint8)
        ren_depth = np.zeros(depth.shape, np.float32)

        gt_ids_curr = range(len(scene_gt[im_id]))
        if gt_ids:
            gt_ids_curr = set(gt_ids_curr).intersection(gt_ids)
        for gt_id in gt_ids_curr:
            gt = scene_gt[im_id][gt_id]
            obj_id = gt['obj_id']
            if vis_orig_color:
                color = (1, 1, 1)
            else:
                color = tuple(colors[(obj_id - 1) % len(colors)])
            color_uint8 = tuple([int(255 * c) for c in color])

            model = models[gt['obj_id']]
            K = scene_info[im_id]['cam_K']
            R = gt['cam_R_m2c']
            t = gt['cam_t_m2c']

            # Rendering
            if vis_rgb:
                if vis_orig_color:
                    m_rgb = renderer.render(model, im_size, K, R, t, mode='rgb')
                else:
                    m_rgb = renderer.render(model, im_size, K, R, t, mode='rgb',
                                            surf_color=color)

            if vis_depth or (vis_rgb and vis_rgb_resolve_visib):
                m_depth = renderer.render(model, im_size, K, R, t, mode='depth')

                # Get mask of the surface parts that are closer than the
                # surfaces rendered before
                visible_mask = np.logical_or(ren_depth == 0, m_depth < ren_depth)
                mask = np.logical_and(m_depth != 0, visible_mask)

                ren_depth[mask] = m_depth[mask].astype(ren_depth.dtype)

            # Combine the RGB renderings
            if vis_rgb:
                if vis_rgb_resolve_visib:
                    ren_rgb[mask] = m_rgb[mask].astype(ren_rgb.dtype)
                else:
                    ren_rgb += m_rgb.astype(ren_rgb.dtype)

                # Draw 2D bounding box and info
                if True:
                    obj_mask = np.sum(m_rgb > 0, axis=2)
                    ys, xs = obj_mask.nonzero()
                    if len(ys):
                        # bbox = misc.calc_2d_bbox(xs, ys, im_size)
                        # im_size = (obj_mask.shape[1], obj_mask.shape[0])
                        # ren_rgb_bbs = misc.draw_rect(ren_rgb_bbs, bbox, color_uint8)

                        txt = '{},{}'.format(obj_id, gt_id)
                        txt_offset = 0
                        # txt_offset = 5
                        p_id = np.argmin(ys)
                        tex_loc = (xs[p_id], ys[p_id] - 5)
                        # tex_loc = (bbox[0], bbox[1])
                        cv2.putText(ren_rgb_info, txt, tex_loc,
                                    cv2.FONT_HERSHEY_PLAIN, 0.9, color_uint8, 1)

        # Save RGB visualization
        if vis_rgb:
            vis_im_rgb = 0.5 * rgb.astype(np.float32) +\
                         0.5 * ren_rgb + \
                         1.0 * ren_rgb_info
            vis_im_rgb[vis_im_rgb > 255] = 255
            inout.save_im(vis_rgb_mpath.format(dataset, scene_id, im_id),
                          vis_im_rgb.astype(np.uint8))

        # Save image of depth differences
        if vis_depth:
            # Calculate the depth difference at pixels where both depth maps
            # are valid
            valid_mask = (depth > 0) * (ren_depth > 0)
            depth_diff = valid_mask * (depth - ren_depth.astype(np.float32))

            f, ax = plt.subplots(1, 1)
            cax = ax.matshow(depth_diff)
            ax.axis('off')
            ax.set_title('measured - GT depth [mm]')
            f.colorbar(cax, fraction=0.03, pad=0.01)
            f.tight_layout(pad=0)
            plt.savefig(vis_depth_mpath.format(dataset, scene_id, im_id), pad=0,
                        bbox_inches='tight')
            plt.close()


text:
"""

Potential Field based path planner

author: Atsushi Sakai (@Atsushi_twi)

Ref:
https://www.cs.cmu.edu/~motionplanning/lecture/Chap4-Potential-Field_howie.pdf

"""

import numpy as np
import matplotlib.pyplot as plt

# Parameters
KP = 5.0  # attractive potential gain
ETA = 100.0  # repulsive potential gain
AREA_WIDTH = 30.0  # potential area width [m]

show_animation = True

def calc_potential_field(gx, gy, ox, oy, reso, rr):
    minx = min(ox) - AREA_WIDTH / 2.0
    miny = min(oy) - AREA_WIDTH / 2.0
    maxx = max(ox) + AREA_WIDTH / 2.0
    maxy = max(oy) + AREA_WIDTH / 2.0
    xw = int(round((maxx - minx) / reso))
    yw = int(round((maxy - miny) / reso))

    # calc each potential
    pmap = [[0.0 for i in range(yw)] for i in range(xw)]

    for ix in range(xw):
        x = ix * reso + minx

        for iy in range(yw):
            y = iy * reso + miny
            ug = calc_attractive_potential(x, y, gx, gy)
            uo = calc_repulsive_potential(x, y, ox, oy, rr)
            uf = ug + uo
            pmap[ix][iy] = uf

    return pmap, minx, miny

def calc_attractive_potential(x, y, gx, gy):
    return 0.5 * KP * np.hypot(x - gx, y - gy)

def calc_repulsive_potential(x, y, ox, oy, rr):
    # search nearest obstacle
    minid = -1
    dmin = float("inf")
    for i, _ in enumerate(ox):
        d = np.hypot(x - ox[i], y - oy[i])
        if dmin >= d:
            dmin = d
            minid = i

    # calc repulsive potential
    dq = np.hypot(x - ox[minid], y - oy[minid])

    if dq <= rr:
        if dq <= 0.1:
            dq = 0.1

        return 0.5 * ETA * (1.0 / dq - 1.0 / rr) ** 2
    else:
        return 0.0

def get_motion_model():
    # dx, dy
    motion = [[1, 0],
              [0, 1],
              [-1, 0],
              [0, -1],
              [-1, -1],
              [-1, 1],
              [1, -1],
              [1, 1]]

    return motion

def potential_field_planning(sx, sy, gx, gy, ox, oy, reso, rr):

    # calc potential field
    pmap, minx, miny = calc_potential_field(gx, gy, ox, oy, reso, rr)

    # search path
    d = np.hypot(sx - gx, sy - gy)
    ix = round((sx - minx) / reso)
    iy = round((sy - miny) / reso)
    gix = round((gx - minx) / reso)
    giy = round((gy - miny) / reso)

    if show_animation:
        draw_heatmap(pmap)
        plt.plot(ix, iy, "*k")
        plt.plot(gix, giy, "*m")

    rx, ry = [sx], [sy]
    motion = get_motion_model()
    while d >= reso:
        minp = float("inf")
        minix, miniy = -1, -1
        for i, _ in enumerate(motion):
            inx = int(ix + motion[i][0])
            iny = int(iy + motion[i][1])
            if inx >= len(pmap) or iny >= len(pmap[0]):
                p = float("inf")  # outside area
            else:
                p = pmap[inx][iny]
            if minp > p:
                minp = p
                minix = inx
                miniy = iny
        ix = minix
        iy = miniy
        xp = ix * reso + minx
        yp = iy * reso + miny
        d = np.hypot(gx - xp, gy - yp)
        rx.append(xp)
        ry.append(yp)

        if show_animation:
            plt.plot(ix, iy, ".r")
            plt.pause(0.01)

    print("Goal!!")

    return rx, ry

def draw_heatmap(data):
    data = np.array(data).T
    plt.pcolor(data, vmax=100.0, cmap=plt.cm.Blues)

def main():
    print("potential_field_planning start")

    sx = 0.0  # start x position [m]
    sy = 10.0  # start y positon [m]
    gx = 30.0  # goal x position [m]
    gy = 30.0  # goal y position [m]
    grid_size = 0.5  # potential grid size [m]
    robot_radius = 5.0  # robot radius [m]

    ox = [15.0, 5.0, 20.0, 25.0]  # obstacle x position list [m]
    oy = [25.0, 15.0, 26.0, 25.0]  # obstacle y position list [m]

    if show_animation:
        plt.grid(True)
        plt.axis("equal")

    # path generation
    rx, ry = potential_field_planning(
        sx, sy, gx, gy, ox, oy, grid_size, robot_radius)

    if show_animation:
        plt.show()

if __name__ == '__main__':
    print(__file__ + " start!!")
    main()
    print(__file__ + " Done!!")

max_stars_repo_name: irl-at-ncai/PythonRobotics

max_issues_repo_name: irl-at-ncai/PythonRobotics

max_forks_repo_name: irl-at-ncai/PythonRobotics


text:
import numpy as np
import torch
import random
import time
import datetime
import yaml
import pickle

from buffers import ExperienceBuffer

def numpy2torch(np_array, device='cuda'):
    return torch.FloatTensor(np_array).to(device)

def updateNet(target, source, tau):
    for target_param, source_param in zip(target.parameters(), source.parameters()):
        target_param.data.copy_(target_param.data * (1.0 - tau) + source_param.data * tau)

def scale_action(a, min, max):
    return (0.5*(a+1.0)*(max-min) + min)

def vectorized_multinomial(prob_matrix):
    items = np.arange(prob_matrix.shape[1])
    s = prob_matrix.cumsum(axis=1)
    r = np.random.rand(prob_matrix.shape[0],1)
    k = (s < r).sum(axis=1)
    return items[k]

def one_hot_embedding(labels, num_classes, device='cuda'):
    y = torch.eye(num_classes).to(device)
    return y[labels]

def set_seed(n_seed=0, device='cuda'):
    random.seed(n_seed)
    np.random.seed(n_seed)
    torch.manual_seed(n_seed)
    if device == 'cuda': torch.cuda.manual_seed(n_seed)

def is_float(x):
    return isinstance(x, float)

def is_tensor(x):
    return isinstance(x, torch.FloatTensor) or isinstance(x, torch.Tensor)

def time_stamp():
    time_in_seconds = time.time()
    stamp = datetime.datetime.fromtimestamp(time_in_seconds).strftime('%Y-%m-%d_%H-%M-%S')
    return stamp

def cat_state_task(observation):
    state = observation['state']
    task = observation['task']
    obs = np.concatenate((state, task))
    return obs

def load_env_model_pairs(file):
    yaml_file = open(file, 'r')
    try:
        env_model_pairs = yaml.load(yaml_file, Loader=yaml.FullLoader)['env_model_pairs']
        assert isinstance(env_model_pairs, dict)
    except:
        raise RuntimeError('Invalid file. It should be a dictionary with name "env_model_pairs"')
    return env_model_pairs

def load_database(n_parts, LOAD_PATH, DB_ID, buffer_size ,level):
    database = ExperienceBuffer(buffer_size, level)
    for i in range(0, n_parts):
        PATH = LOAD_PATH + DB_ID + '/SAC_training_level2_database_part_' + str(i) + '.p'
        database.buffer += pickle.load(open(PATH, 'rb'))
    return database

def separate_database(database):
    train_database = database
    train_database.sort()
    test_database = ExperienceBuffer(database._capacity, database._level)
    test_steps = [train_database.buffer.pop() for x in range(0,database._capacity//10)]
    test_database.buffer.extend(test_steps)
    return train_database, test_database

def temperature_search(
    q_values: torch.Tensor,
    desired_entropy: float,
    tolerance = 1e-2,
    max_iter = 100,
    c_plus: float = 100,
    c_minus: float = 0.01,
    verbose: bool = False) -> torch.Tensor:
    '''
    Find the temperatures that ensure that the distributions

            p(a|s) = exp(q(s,a)/temperature(s))/Z(s)

    have the desired entropy, with a margin of error given the
    tolerance. It is supposed that the last dimension correspond
    to the possible discrete actions.
    '''
    # if len(q_values.shape) != 2:
    #     raise RuntimeError("Q-values should be matrices, i.e., 2d tensors")

    n_actions = q_values.shape[-1]

    log_alpha_plus = torch.log(
        c_plus * q_values.abs().sum(-1, keepdim=True) + 1e-10)

    log_alpha_minus = torch.log(
        c_minus * q_values.abs().min(-1, keepdim=True)[0] + 1e-10)

    worst_entropy_difference = np.log(n_actions)
    n_iter = 0
    while worst_entropy_difference > tolerance and n_iter < max_iter:
        # Set log-temperature as the average of the plus and minus estimates
        log_alpha = 0.5 * (log_alpha_plus + log_alpha_minus)
        alpha = torch.exp(log_alpha)

        # Calculate entropies
        q_normalized = q_values / alpha
        q = q_normalized - q_normalized.max(-1, keepdim=True)[0]
        distribution_ = torch.exp(q)+1e-10
        distribution_ /= distribution_.sum(-1, keepdim=True)
        entropy = -(
            distribution_ * torch.log(distribution_)
        ).sum(-1, keepdim=True)

        # Update temperature estimates depending on whether the entropy
        # difference is positive or negative
        entropy_difference = entropy - desired_entropy
        log_alpha_plus = torch.where(
            entropy_difference > 0, log_alpha, log_alpha_plus)
        log_alpha_minus = torch.where(
            entropy_difference <= 0, log_alpha, log_alpha_minus)

        worst_entropy_difference = entropy_difference.abs().max()
        n_iter += 1

        if verbose:
            print(
                f'Max temp: {np.exp(log_alpha_plus.max().item()):.3e}, ' +
                f'Min temp: {np.exp(log_alpha_minus.min()):.3e},' +
                f'Max entropy: {entropy.max():.3f}, ' +
                f'Min entropy: {entropy.min():.3f},' +
                f'Worst entropy delta: {worst_entropy_difference:.3f}'
            )

    return 0.5 * (log_alpha_plus + log_alpha_minus), n_iter


text:
import pathlib
import unittest

import numpy as np
import pandas as pd

from process_improve.experiments.models import lm
from process_improve.experiments.structures import c, create_names, gather

class TestStructures(unittest.TestCase):
    """
    Test the data structures.
    """

    def setUp(self):
        self.A = [-1, +1, -1, +1, 0, +1]
        self.B = [-1, -1, +1, +1, 0, 0]
        self.C = [4, 5, 6, 4, 6]
        self.D = [0, 1, "green"]
        self.y = [52, 74, 62, 80, 50, 65]

    def test_create_names(self):
        """Creating factor names." """

        self.assertListEqual(create_names(5), ["A", "B", "C", "D", "E"])

        self.assertListEqual(create_names(3, letters=False), ["X1", "X2", "X3"])

        self.assertListEqual(
            create_names(3, letters=False, prefix="Q", start_at=9, padded=True),
            ["Q09", "Q10", "Q11"],
        )

        self.assertListEqual(
            create_names(4, letters=False, prefix="Z", start_at=99, padded=True),
            ["Z099", "Z100", "Z101", "Z102"],
        )

    def test_create_factors(self):
        A1 = c(*(self.A))
        A2 = c(*(self.A), index=["lo", "hi", "lo", "hi", "cp", "hi"])
        B = c(*(self.B), name="B")
        C1 = c(*(self.C), range=(4, 6))
        C2 = c(*(self.C), center=5, range=(4, 6))
        C3 = c(*(self.C), lo=4, hi=6)
        C4 = c(*(self.C), lo=4, hi=6, name="C4")
        C5 = c([4, 5, 6, 4, 6], lo=4, hi=6, name="C5")
        C6 = c(*(self.C), lo=5, hi=6, name="C6")
        y = c(*(self.y), name="conversion", units="%")

        self.assertTrue(isinstance(A1, pd.Series))
        self.assertTrue(A1.shape == (6,))
        self.assertTrue(hasattr(A1, "pi_index"))
        self.assertTrue(hasattr(A1, "name"))

        self.assertTrue(A1.name == "Unnamed [coded]")
        self.assertTrue(hasattr(A1, "pi_lo"))
        self.assertTrue(A1.pi_lo == -1)
        self.assertTrue(hasattr(A1, "pi_hi"))
        self.assertTrue(A1.pi_hi == +1)
        self.assertTrue(hasattr(A1, "pi_range"))
        self.assertTrue(A1.pi_range[0] == -1)
        self.assertTrue(A1.pi_range[1] == +1)
        self.assertTrue(hasattr(A1, "pi_center"))
        self.assertTrue(A1.pi_center == 0)

        self.assertTrue(isinstance(A2.index, pd.Index))
        self.assertTrue(hasattr(A2, "pi_index"))
        self.assertTrue(A2.name == "Unnamed [coded]")

        with self.assertRaises(IndexError):
            A2 = c(*(self.A), index=["lo", "hi", "lo", "hi", "cp"])

        self.assertTrue(B.shape == (6,))
        self.assertTrue(B.name == "B [coded]")

        self.assertTrue(C1.pi_range == (4, 6))
        self.assertTrue(C2.pi_center == 5)
        self.assertTrue(C2.pi_range == (4, 6))
        self.assertTrue(C3.pi_lo == 4)
        self.assertTrue(C3.pi_hi == 6)
        self.assertTrue(C4.pi_lo == 4)
        self.assertTrue(C4.pi_hi == 6)
        self.assertTrue(C5.pi_hi == 6)
        self.assertTrue(C5.name == "C5")

        # User says the low is 5, but the minimum is actually different
        self.assertTrue(C6.pi_lo == 5)
        self.assertTrue(C6.pi_range == (5, 6))

        D = c(*(self.D))
        self.assertTrue(D.pi_numeric is True)

        self.assertTrue(len(y) == 6)
        self.assertTrue(y.name == "conversion [%]")

    def test_column_math(self):
        self.A = [-1, +1, -1, +1, 0, +1]
        self.B = [-1, -1, +1, +1, 0, 0]
        A = c(*(self.A))
        B = c(*(self.B), name="B")
        C1 = A * B
        C2 = B * A

        self.assertTrue(np.all(C1.values == [+1, -1, -1, +1, 0, 0]))
        self.assertTrue(np.all(C1.index == A.index))
        self.assertTrue(np.all(C2.values == [+1, -1, -1, +1, 0, 0]))
        self.assertTrue(np.all(C2.index == A.index))

    def test_gather(self):
        A = c(*(self.A))
        B = c(*(self.B))
        y = c(*(self.y), name="conversion")

        expt = gather(A=A, B=B, y=y)
        self.assertTrue(expt.shape == (6, 3))

        expt = gather(A=A, B=B, y=y, title="Testing expt name")

        # Eventually this method must go to the "DF" class; currently in the
        # model class; not really appropriate there.
        self.assertTrue(expt.get_title() == "Testing expt name")

class TestModels(unittest.TestCase):
    def setUp(self):

        A = c(-1, +1, -1, +1)
        B = c(-1, -1, +1, +1)
        C = A * B
        y = c(41, 27, 35, 20, name="Stability", units="days")
        self.expt = gather(A=A, B=B, C=C, y=y, title="Half-fraction, using C = A*B")
        self.model_stability_poshalf = lm("y ~ A*B*C", self.expt)

        """
        Results from R:
        A = c(-1, +1, -1, +1)
        B = c(-1, -1, +1, +1)
        C = A*B
        y = c(41, 27, 35, 20)
        data = data.frame(A=A, B=B, C=C, y=y)
        model = lm(y ~ A*B*C, data=data)
        summary(model)

        Call:
            lm(formula = y ~ A * B * C, data = data)

            Residuals:
            ALL 4 residuals are 0: no residual degrees of freedom!

            Coefficients: (4 not defined because of singularities)
                        Estimate Std. Error t value Pr(>|t|)
            (Intercept)    30.75         NA      NA       NA
            A              -7.25         NA      NA       NA
            B              -3.25         NA      NA       NA
            C              -0.25         NA      NA       NA
            A:B               NA         NA      NA       NA
            A:C               NA         NA      NA       NA
            B:C               NA         NA      NA       NA
            A:B:C             NA         NA      NA       NA

            Residual standard error: NaN on 0 degrees of freedom
            Multiple R-squared:      1,	Adjusted R-squared:    NaN
            F-statistic:   NaN on 3 and 0 DF,  p-value: NA
        """

    def test_half_fraction(self):
        """
        Testing attributes for the half-fraction model.
        """
        self.assertTrue(self.model_stability_poshalf.nobs == 4)
        self.assertTrue(self.model_stability_poshalf.df_resid == 0)
        beta = self.model_stability_poshalf.get_parameters(drop_intercept=False)

        self.assertAlmostEqual(beta["A"], -7.25)
        self.assertAlmostEqual(beta["B"], -3.25)
        self.assertAlmostEqual(beta["C"], -0.25)
        self.assertAlmostEqual(beta["Intercept"], 30.75)
        self.assertTrue(
            self.model_stability_poshalf.get_aliases()
            == ["A + B:C", "B + A:C", "C + A:B"]
        )

        for resid in self.model_stability_poshalf.residuals:
            self.assertAlmostEqual(resid, 0.0)

class Test_API_usage(unittest.TestCase):
    def setUp(self):
        # TODO: replace unittest -> pytest
        # TODO: use path
        folder = (
            pathlib.Path(__file__).parents[1]
            / "process_improve"
            / "datasets"
            / "experiments"
        )
        self.df1 = pd.read_csv(folder / "test_doe1.csv")
        self.df1.set_index("Run order", inplace=True)

    def test_case_1(self):

        index = self.df1.index
        C = c(
            self.df1["C"],
            lo=self.df1["C"].min(),
            hi=self.df1["C"].max(),
            index=index,
            name="C",
        )
        M = c(self.df1["M"], levels=self.df1["M"].unique(), name="M")
        V = c(self.df1["V"], lo=self.df1["V"].min(), hi=self.df1["V"].max(), name="V")
        B = c(self.df1["B"], name="B")
        self.assertTrue(B.pi_levels["B"] == ["Ard", "Eme"])

        y = self.df1["y"]

        expt = gather(C=C, M=M, V=V, B=B, y=y)
        self.assertTrue(np.all(C.index == M.index))

        _ = lm("np.log10(y) ~ C*M*B*V", expt)
        # summary(model)
        # pareto_plot(model)
        # contour_plot(model, C, M)
        # contour_plot(model, "C", "M")
        # predict_plot(model)

    def test_aliasing(self):
        d4 = c(24, 48, 36, 36, 60, units="hours", lo=24, high=48)
        y4 = c(31, 65, 52, 54, 69)
        expt4 = gather(d=d4, y=y4, title="RW units")
        model4 = lm("y ~ d + I(np.power(d, 2))", data=expt4)
        self.assertEqual(len(model4.aliasing), 0)

        model5 = lm("y ~ d + I(np.power(d, 2))", data=expt4, alias_threshold=0.99)
        self.assertEqual(len(model5.aliasing), 2)

    def test_realworld_coded(self):
        """
        Test conversion between real-world and coded units.
        """
        c1 = c(2.5, 3, 2.5, 3, center=2.75, range=[2.5, 3], name="cement")
        c1_coded = c1.to_coded()
        self.assertListEqual(c1_coded.to_list(), [-1.0, 1.0, -1.0, 1.0])

        c2 = c(
            [-1.0, -1.0, +1.0, +1.0],
            center=2.75,
            range=[2.5, 3],
            name="cement",
            coded=True,
        )
        c2_rw = c2.to_realworld()
        self.assertListEqual(c2_rw.to_list(), [2.5, 2.5, 3.0, 3.0])

if __name__ == "__main__":
    unittest.main()

max_stars_repo_name: kgdunn/process-improve

max_issues_repo_name: kgdunn/process-improve

max_forks_repo_name: kgdunn/process-improve


text:
"""An implementation of the k-fingerprinting classifier from:

    Hayes, Jamie, and George Danezis. "k-fingerprinting: A robust
    scalable website fingerprinting technique." 25th {USENIX} Security
    Symposium ({USENIX} Security 16). 2016.

The original can be found at https://github.com/jhayes14/k-FP.
"""
import logging
import warnings
from typing import Optional, TypeVar, Union

import numpy as np
import sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import NearestNeighbors
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.utils import check_array
from sklearn.utils.multiclass import unique_labels

Element = TypeVar('Element')

class KFingerprintingClassifier(BaseEstimator, ClassifierMixin):
    """k-fingerprinting website classifier.

    Utilises a RandomForestClassifier to gather leaf indices then
    predicts a class or indeterminate using k-nearest neighbour and the
    Hamming distance between the leaf indices of the training data and
    that of the test data.

    Does not currently support 2-dimensional labels.

    Parameters
    ----------
    forest :
        Specifies the random forest classifier to use as the underlying
        forest.  If absent, one is created with 150 trees and
        oob_scoring, as well as using the n_jobs and random_state
        provided.  If provided in addtition to n_jobs and random_state,
        they are ignored.

    n_neighbours :
        The number of nearest neighbours to use for the prediction.

    unknown_label :
        The label to use for unknown predictions.  This is by default
        -1.  A warning is issues if the type of the unknown label does
        not match the type of the labels.

    References
    ----------
    Hayes, Jamie, and George Danezis. "k-fingerprinting: A robust
    scalable website fingerprinting technique." 25th {USENIX} Security
    Symposium ({USENIX} Security 16). 2016.
    """
    # pylint: disable=too-many-arguments,too-many-instance-attributes
    def __init__(self, forest: Optional[RandomForestClassifier] = None,
                 n_neighbours: int = 2, n_jobs=None, random_state=None,
                 unknown_label: Union[str, int] = -1):
        self.forest = forest
        self.n_neighbours = n_neighbours
        self.n_jobs = n_jobs
        self.random_state = random_state
        self.unknown_label = unknown_label

    def fit(self, X, y):  # pylint: disable=invalid-name
        """Fit the estimator according to the given training data."""
        X = check_array(X, accept_sparse=False, dtype=None)
        y = check_array(y, accept_sparse=False, ensure_2d=False, dtype=None)

        # Check that the unknown label is compatible with the other labels
        if not np.can_cast(self.unknown_label, y.dtype):
            unknown_label_type = np.dtype(type(self.unknown_label))
            warnings.warn(
                f"The datatype of the unknown label ({unknown_label_type}) "
                f"does not match the datatype of the classes ({y.dtype}). A "
                "conversion or error may occur.")

        logger = logging.getLogger(__name__)
        logger.debug("Fitting the random forest on %d samples.", len(X))

        # pylint: disable=attribute-defined-outside-init
        self.classes_ = unique_labels(y)
        self.forest_ = (sklearn.base.clone(self.forest)
                        if self.forest is not None
                        else RandomForestClassifier(
                            n_estimators=150, oob_score=True,
                            n_jobs=self.n_jobs, random_state=self.random_state))

        self.forest_.fit(X, y)

        logger.debug("Fitting the nearest neighbor graph.")
        self.graph_ = NearestNeighbors(n_neighbors=self.n_neighbours,
                                       metric='hamming', n_jobs=self.n_jobs)

        self.graph_.fit(self.forest_.apply(X))

        # We need the labels because the graph returns indices into the
        # population matrix, which are the same indices associated with
        # the labeles
        assert isinstance(y, np.ndarray)
        self.labels_ = y

        logger.debug("Model fitting complete.")
        return self

    def _predict(self, X, n_neighbors: Optional[int] = None):
        sklearn.utils.validation.check_is_fitted(
            self, ['graph_', 'labels_', 'forest_'])

        logger = logging.getLogger(__name__)
        logger.debug("Determining leaves for the prediction.")

        leaves = self.forest_.apply(X)

        logger.debug("Identifying neighbours of the leaves.")
        neighbourhoods = self.graph_.kneighbors(
            leaves, return_distance=False, n_neighbors=n_neighbors)

        return (self.labels_[neighbourhoods.reshape((-1, ))]
                .reshape((-1, n_neighbors or self.n_neighbours)))

    def predict(self, X, n_neighbors: Optional[int] = None):
        """Predict the class for X by taking the class with the highest
        frequency in the in the neighbours of each sample.
        """
        probabilities = self.predict_proba(X, n_neighbors)
        return self.classes_[np.argmax(probabilities, axis=1)]

    def predict_unanimous(self, X, n_neighbors: Optional[int] = None):
        """Predict the class for X.

        The predicted class is the unanimous label of the k-closest neighbours
        or None.
        """
        X = check_array(X, accept_sparse=False, dtype=None)
        neighbourhoods = self._predict(X, n_neighbors)

        logger = logging.getLogger(__name__)
        logger.debug("Formulating decision.")

        first_column = neighbourhoods[:, 0].reshape((-1, 1))
        all_match = np.all(neighbourhoods == first_column, axis=1)
        return np.where(all_match, neighbourhoods[:, 0], self.unknown_label)

    def predict_proba(self, X, n_neighbors: Optional[int] = None):
        """Predict the probability for each class, sorted by the class
        labels.

        For the probability of a class we use the number of times that
        class appears in the the neighbourhood, divided by the the number
        of neighbours.
        """
        X = check_array(X, accept_sparse=False, dtype=None)
        neighbourhoods = self._predict(X, n_neighbors)
        n_neighbors = neighbourhoods.shape[1]

        result = np.zeros((len(X), len(self.classes_)), dtype=float)
        for i, class_ in enumerate(self.classes_):
            result[:, i] = np.sum(neighbourhoods == class_, axis=1)
        return result / n_neighbors

max_stars_repo_name: jpcsmith/wf-tools

max_issues_repo_name: jpcsmith/wf-tools

max_forks_repo_name: jpcsmith/wf-tools


text:
from dash.dependencies import Input, Output, State
from app import app
from pages.home.home_data import get_dataset
from dash import dcc
from dash import html as dhc
import dash
import dash_bootstrap_components as dbc
import traceback
from microbiome.enumerations import EmbeddingModelType
import numpy as np

@app.callback(
    Output("page-2-display-value-1", "children"),
    Input("microbiome-dataset-location", "data"),
    Input("abundances-number-of-columns", "value"),
)
def display_value(dataset_path, number_of_columns):
    results = []
    if dataset_path:
        try:
            dataset = get_dataset(dataset_path)

            result = dataset.plot_bacteria_abundances(
                number_of_columns=number_of_columns,
            )

            results = [
                dcc.Graph(figure=result["fig"], config=result["config"]),
            ]
        except Exception as e:
            results = dbc.Alert(
                children=[
                    dcc.Markdown("Microbiome trajectory error: " + str(e)),
                    dcc.Markdown(traceback.format_exc()),
                    dcc.Markdown("Open an [issue on GitHub](https://github.com/JelenaBanjac/microbiome-toolbox/issues) or send an [email](msjelenabanjac@gmail.com)."),
                ],
                color="danger",
            )
    return results

@app.callback(
    Output("page-2-display-value-2", "children"),
    Input("microbiome-dataset-location", "data"),
    Input("heatmap-relative-absolute-values", "value"),
    Input("heatmap-fillna", "value"),
    Input("heatmap-avg-fn", "value"),
)
def display_value(dataset_path, relative_values, fillna, avg_fn):
    results = []
    if dataset_path:
        try:
            dataset = get_dataset(dataset_path)

            result = dataset.plot_bacteria_abundance_heatmaps(
                relative_values=relative_values=="relative",
                fillna=len(fillna)>0,
                avg_fn=np.median if avg_fn=="median" else np.mean,

            )

            results = [
                dcc.Graph(figure=result["fig"], config=result["config"]),
            ]
        except Exception as e:
            results = [
                dbc.Alert(
                    children=[
                        dcc.Markdown("Dataset/Plot error: " + str(e)),
                        dcc.Markdown(traceback.format_exc()),
                        dcc.Markdown(
                            "Open an [issue on GitHub](https://github.com/JelenaBanjac/microbiome-toolbox/issues) or send an [email](msjelenabanjac@gmail.com)."
                        ),
                    ],
                    color="danger",
                )
            ]
    return results

@app.callback(
    Output("page-2-display-value-3", "children"),
    Input("microbiome-dataset-location", "data"),
    Input("longitudinal-number-of-columns", "value"),
    Input("longitudinal-number-of-bacteria", "value"),
)
def display_value(dataset_path, number_of_columns, number_of_bacteria):
    results = []
    if dataset_path:
        try:
            dataset = get_dataset(dataset_path)

            result = dataset.plot_ultradense_longitudinal_data(
                number_of_columns=number_of_columns,
                number_of_bacteria=number_of_bacteria,
            )

            results = [
                dcc.Graph(figure=result["fig"], config=result["config"]),
            ]
        except Exception as e:
            results = dbc.Alert(
                children=[
                    dcc.Markdown("Microbiome trajectory error: " + str(e)),
                    dcc.Markdown(traceback.format_exc()),
                    dcc.Markdown("Open an [issue on GitHub](https://github.com/JelenaBanjac/microbiome-toolbox/issues) or send an [email](msjelenabanjac@gmail.com)."),
                ],
                color="danger",
            )
    return results

@app.callback(
    Output("page-2-display-value-4", "children"),
    Input("microbiome-dataset-location", "data"),
    Input("embedding-dimension", "value"),
    Input("embedding-method-type", "value"),
)
def display_value(dataset_path, embedding_dimension, embedding_method_type):
    results = []
    if dataset_path:
        try:
            dataset = get_dataset(dataset_path)

            embedding_model = EmbeddingModelType[embedding_method_type].value(n_components=embedding_dimension)

            result = dataset.embedding_to_latent_space(
                embedding_dimension=embedding_dimension,
                embedding_model=embedding_model,
            )

            results = [
                dcc.Graph(figure=result["fig"], config=result["config"]),
            ]
        except Exception as e:
            results = dbc.Alert(
                children=[
                    dcc.Markdown("Microbiome trajectory error: " + str(e)),
                    dcc.Markdown(traceback.format_exc()),
                    dcc.Markdown("Open an [issue on GitHub](https://github.com/JelenaBanjac/microbiome-toolbox/issues) or send an [email](msjelenabanjac@gmail.com)."),
                ],
                color="danger",
            )
    return results

@app.callback(
    Output("page-2-display-value-5", "children"),
    Input("microbiome-dataset-location", "data"),
    Input("embedding-method-type-interactive", "value"),
)
def display_value(dataset_path, embedding_method_type):
    results = []
    if dataset_path:
        try:
            dataset = get_dataset(dataset_path)

            embedding_model = EmbeddingModelType[embedding_method_type].value(n_components=2)

            vbox = dataset.embeddings_interactive_selection_notebook(embedding_model=embedding_model)

            results = [
                dcc.Graph(figure=vbox.children[0], id="interactive-embeddings"),
                dhc.Div(id="interactive-embeddings-info"),
                dhc.Br(),
            ]
        except Exception as e:
            results = dbc.Alert(
                children=[
                    dcc.Markdown("Microbiome trajectory error: " + str(e)),
                    dcc.Markdown(traceback.format_exc()),
                    dcc.Markdown("Open an [issue on GitHub](https://github.com/JelenaBanjac/microbiome-toolbox/issues) or send an [email](msjelenabanjac@gmail.com)."),
                ],
                color="danger",
            )
    return results

@app.callback(
    Output("interactive-embeddings-info", "children"),
    [
        Input("interactive-embeddings", "selectedData"),
        Input("microbiome-dataset-location", "data"),
    ],
    [State("interactive-embeddings", "figure")],
)
def display_value(selectedData, dataset_path, fig):
    import plotly.graph_objects as go

    results = []
    if dataset_path:
        try:
            dataset = get_dataset(dataset_path)
            selection = None
            # Update selection based on which event triggered the update.
            trigger = dash.callback_context.triggered[0]["prop_id"]
            # if trigger == 'graph.clickData':
            #     selection = [point["pointNumber"] for point in clickData["points"]]
            if trigger == "interactive-embeddings.selectedData":
                selection = [point["pointIndex"] for point in selectedData["points"]]

            if selection is not None:
                # Update scatter selection
                fig["data"][0]["selectedpoints"] = selection

                # Create a table FigureWidget that updates on selection from points in the scatter plot of f
                t = go.FigureWidget(
                    [
                        go.Table(
                            header=dict(
                                values=["sampleID", "subjectID"],
                                fill=dict(color="#C2D4FF"),
                                align=["left"] * 5,
                            ),
                            cells=dict(
                                values=[
                                    dataset.df[col] for col in ["sampleID", "subjectID"]
                                ],
                                fill=dict(color="#F5F8FF"),
                                align=["left"] * 5,
                            ),
                        )
                    ]
                )

                results = dataset.selection_embeddings(t)(
                    None, fig["data"][0]["selectedpoints"], None
                )

                ffig = results["fig"]
                img_src = results["img_src"]
                acccuracy = results["accuracy"]
                f1score = results["f1score"]
                img_src.update_layout(height=400, width=400)
                confusion_matrix = dcc.Graph(figure=img_src)

                results = [
                    dcc.Graph(figure=t),
                    dcc.Graph(figure=ffig),
                    dhc.Br(),
                    dbc.Container(
                        dbc.Row(
                            [
                                dbc.Col(
                                    [
                                        dhc.Br(),
                                        dhc.H5("Groups discrimination performance results"),
                                        dhc.Br(),
                                        dhc.Br(),
                                        dhc.P(
                                            "The ideal separation between two groups (reference vs. non-reference) will have 100% of values detected on the second diagonal. This would mean that the two groups can be easily separated knowing their taxa abundamces and metadata information."
                                        ),
                                        dhc.P(f"Accuracy: {acccuracy:.2f}"),
                                        dhc.P(f"F1-score: {f1score:.2f}"),
                                    ]
                                ),
                                dbc.Col(confusion_matrix),
                            ]
                        )
                    ),
                    dhc.Br(),
                ]
        except Exception as e:
            results = dbc.Alert(
                children=[
                    dcc.Markdown("Microbiome trajectory error: " + str(e)),
                    dcc.Markdown(traceback.format_exc()),
                    dcc.Markdown("Open an [issue on GitHub](https://github.com/JelenaBanjac/microbiome-toolbox/issues) or send an [email](msjelenabanjac@gmail.com)."),
                ],
                color="danger",
            )
    return results

max_stars_repo_name: JelenaBanjac/microbiome-toolbox

max_issues_repo_name: JelenaBanjac/microbiome-toolbox

max_forks_repo_name: JelenaBanjac/microbiome-toolbox


text:
import numpy as np
import torch as th
from torch import nn
from torch.nn import init
from torch.nn.functional import elu

from braindecode.models.base import BaseModel
from braindecode.torch_ext.init import glorot_weight_zero_bias
from braindecode.torch_ext.modules import Expression
from braindecode.torch_ext.util import np_to_var

class Conv2dWithConstraint(nn.Conv2d):
    def __init__(self, *args, max_norm=1, **kwargs):
        self.max_norm = max_norm
        super(Conv2dWithConstraint, self).__init__(*args, **kwargs)

    def forward(self, x):
        self.weight.data = th.renorm(self.weight.data, p=2, dim=0,
                                     maxnorm=self.max_norm)
        return super(Conv2dWithConstraint, self).forward(x)

class EEGNetv4(BaseModel):
    """
    EEGNet v4 model from [EEGNet4]_.

    Notes
    -----
    This implementation is not guaranteed to be correct, has not been checked
    by original authors, only reimplemented from the paper description.

    References
    ----------

    .. [EEGNet4] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon,
       S. M., Hung, C. P., & Lance, B. J. (2018).
       EEGNet: A Compact Convolutional Network for EEG-based
       Brain-Computer Interfaces.
       arXiv preprint arXiv:1611.08024.
    """

    def __init__(self, in_chans,
                 n_classes,
                 final_conv_length='auto',
                 input_time_length=None,
                 pool_mode='mean',
                 F1=8,
                 D=2,
                 F2=16, # usually set to F1*D (?)
                 kernel_length=64,
                 third_kernel_size=(8, 4),
                 drop_prob=0.25
                 ):

        if final_conv_length == 'auto':
            assert input_time_length is not None
        self.__dict__.update(locals())
        del self.self

    def create_network(self):
        pool_class = dict(max=nn.MaxPool2d, mean=nn.AvgPool2d)[self.pool_mode]
        model = nn.Sequential()
        # b c 0 1
        # now to b 1 0 c
        model.add_module('dimshuffle', Expression(_transpose_to_b_1_c_0))

        model.add_module('conv_temporal', nn.Conv2d(
            1, self.F1, (1, self.kernel_length), stride=1, bias=False,
            padding=(0, self.kernel_length // 2,)))
        model.add_module('bnorm_temporal', nn.BatchNorm2d(
            self.F1, momentum=0.01, affine=True, eps=1e-3), )
        model.add_module('conv_spatial', Conv2dWithConstraint(
            self.F1, self.F1 * self.D, (self.in_chans, 1), max_norm=1, stride=1, bias=False,
            groups=self.F1,
            padding=(0, 0)))

        model.add_module('bnorm_1', nn.BatchNorm2d(
            self.F1 * self.D, momentum=0.01, affine=True, eps=1e-3), )
        model.add_module('elu_1', Expression(elu))

        model.add_module('pool_1', pool_class(
            kernel_size=(1, 4), stride=(1, 4)))
        model.add_module('drop_1', nn.Dropout(p=self.drop_prob))

        # https://discuss.pytorch.org/t/how-to-modify-a-conv2d-to-depthwise-separable-convolution/15843/7
        model.add_module('conv_separable_depth', nn.Conv2d(
            self.F1 * self.D, self.F1 * self.D, (1, 16), stride=1, bias=False, groups=self.F1 * self.D,
            padding=(0, 16 // 2)))
        model.add_module('conv_separable_point', nn.Conv2d(
            self.F1 * self.D, self.F2, (1, 1), stride=1, bias=False,
            padding=(0, 0)))

        model.add_module('bnorm_2', nn.BatchNorm2d(
            self.F2, momentum=0.01, affine=True, eps=1e-3), )
        model.add_module('elu_2', Expression(elu))
        model.add_module('pool_2', pool_class(
            kernel_size=(1, 8), stride=(1, 8)))
        model.add_module('drop_2', nn.Dropout(p=self.drop_prob))

        out = model(np_to_var(np.ones(
            (1, self.in_chans, self.input_time_length, 1),
            dtype=np.float32)))
        n_out_virtual_chans = out.cpu().data.numpy().shape[2]

        if self.final_conv_length == 'auto':
            n_out_time = out.cpu().data.numpy().shape[3]
            self.final_conv_length = n_out_time

        model.add_module('conv_classifier', nn.Conv2d(
            self.F2, self.n_classes,
            (n_out_virtual_chans, self.final_conv_length,), bias=True))
        model.add_module('softmax', nn.LogSoftmax())
        # Transpose back to the the logic of braindecode,
        # so time in third dimension (axis=2)
        model.add_module('permute_back', Expression(_transpose_1_0))
        model.add_module('squeeze', Expression(_squeeze_final_output))

        glorot_weight_zero_bias(model)
        return model

def _transpose_to_b_1_c_0(x):
    return x.permute(0, 3, 1, 2)

def _transpose_1_0(x):
    return x.permute(0, 1, 3, 2)

# remove empty dim at end and potentially remove empty time dim
# do not just use squeeze as we never want to remove first dim
def _squeeze_final_output(x):
    assert x.size()[3] == 1
    x = x[:, :, :, 0]
    if x.size()[2] == 1:
        x = x[:, :, 0]
    return x

class EEGNet(object):
    """
    EEGNet model from [EEGNet]_.

    Notes
    -----
    This implementation is not guaranteed to be correct, has not been checked
    by original authors, only reimplemented from the paper description.

    References
    ----------

    .. [EEGNet] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon,
       S. M., Hung, C. P., & Lance, B. J. (2016).
       EEGNet: A Compact Convolutional Network for EEG-based
       Brain-Computer Interfaces.
       arXiv preprint arXiv:1611.08024.
    """
    def __init__(self, in_chans,
                 n_classes,
                 final_conv_length='auto',
                 input_time_length=None,
                 pool_mode='max',
                 second_kernel_size=(2,32),
                 third_kernel_size=(8,4),
                 drop_prob=0.25
                 ):

        if final_conv_length == 'auto':
            assert input_time_length is not None
        self.__dict__.update(locals())
        del self.self

    def create_network(self):
        pool_class = dict(max=nn.MaxPool2d, mean=nn.AvgPool2d)[self.pool_mode]
        model = nn.Sequential()
        n_filters_1 = 16
        model.add_module('conv_1', nn.Conv2d(
            self.in_chans, n_filters_1, (1, 1), stride=1, bias=True))
        model.add_module('bnorm_1', nn.BatchNorm2d(
            n_filters_1, momentum=0.01, affine=True, eps=1e-3),)
        model.add_module('elu_1', Expression(elu))
        # transpose to examples x 1 x (virtual, not EEG) channels x time
        model.add_module('permute_1', Expression(lambda x: x.permute(0,3,1,2)))

        model.add_module('drop_1', nn.Dropout(p=self.drop_prob))

        n_filters_2 = 4
        # keras padds unequal padding more in front, so padding
        # too large should be ok.
        # Not padding in time so that croped training makes sense
        # https://stackoverflow.com/questions/43994604/padding-with-even-kernel-size-in-a-convolutional-layer-in-keras-theano

        model.add_module('conv_2', nn.Conv2d(
            1, n_filters_2, self.second_kernel_size, stride=1,
            padding=(self.second_kernel_size[0] // 2, 0),
            bias=True))
        model.add_module('bnorm_2',nn.BatchNorm2d(
            n_filters_2, momentum=0.01, affine=True, eps=1e-3),)
        model.add_module('elu_2', Expression(elu))
        model.add_module('pool_2', pool_class(
            kernel_size=(2, 4), stride=(2, 4)))
        model.add_module('drop_2', nn.Dropout(p=self.drop_prob))

        n_filters_3 = 4
        model.add_module('conv_3', nn.Conv2d(
            n_filters_2, n_filters_3, self.third_kernel_size, stride=1,
            padding=(self.third_kernel_size[0] // 2, 0),
            bias=True))
        model.add_module('bnorm_3',nn.BatchNorm2d(
            n_filters_3, momentum=0.01, affine=True, eps=1e-3),)
        model.add_module('elu_3', Expression(elu))
        model.add_module('pool_3', pool_class(
            kernel_size=(2, 4), stride=(2, 4)))
        model.add_module('drop_3', nn.Dropout(p=self.drop_prob))

        out = model(np_to_var(np.ones(
            (1, self.in_chans, self.input_time_length, 1),
            dtype=np.float32)))
        n_out_virtual_chans = out.cpu().data.numpy().shape[2]

        if self.final_conv_length == 'auto':
            n_out_time = out.cpu().data.numpy().shape[3]
            self.final_conv_length = n_out_time

        model.add_module('conv_classifier', nn.Conv2d(
            n_filters_3, self.n_classes,
            (n_out_virtual_chans, self.final_conv_length,), bias=True))
        model.add_module('softmax', nn.LogSoftmax())
        # Transpose back to the the logic of braindecode,
        # so time in third dimension (axis=2)
        model.add_module('permute_2', Expression(lambda x: x.permute(0,1,3,2)))
        model.add_module('squeeze',  Expression(_squeeze_final_output))
        glorot_weight_zero_bias(model)
        return model

max_stars_repo_name: gemeinl/braindecode

max_issues_repo_name: gemeinl/braindecode

max_forks_repo_name: gemeinl/braindecode


text:
#!/usr/bin/env python3

import time
import math
import numpy
import platform, sys, select

from pyctrl.client import Controller
import pyctrl.block as block
import pyctrl.block.logger as logger

# initialize controller
HOST, PORT = "192.168.10.105", 9999

print(72 * '*')
print('*' + 29 * ' ' + 'COSMOS SETUP' + 29 * ' ' + '*')
print(72 * '*')
print('> Confirm parameters:')
host = input('  HOST[{}] = '.format(HOST))
if host:
    HOST = host
port = input('  PORT[{}] = '.format(PORT))
if port:
    PORT = int(port)
print('> Conecting to {}({})...'.format(HOST, PORT))

device = Controller(host = HOST, port = PORT)
device.add_sink('logger',
                logger.Logger(),
                ['clock','encoder1'])
device.add_sink('printer', block.Printer(endln = '\r'),
                ['clock', 'motor1', 'encoder1'])

print(device.info('all'))

# define tests

def test(k, args, query_msg, failed_msg, test_function):
    print('\n> TEST #{}'.format(k))
    passed, retval = test_function(args)
    if not passed:
        print("> Failed test #{}: {}".format(k, retval))
        sys.exit(2)
    time.sleep(.1)
    if query_msg:
        answer = input('\n< ' + query_msg + ' [Y/n]').lower()
        if 'n' in answer:
            print("> Failed test #{}: {}".format(k, failed_msg))
            sys.exit(2)
    return retval

def test_motor_forward(args):

    with device:
        position1 = device.get_signal('encoder1')
        device.set_signal('motor1',100)
        time.sleep(2)
        position2 = device.get_signal('encoder1')
    device.set_signal('motor1',0)
    return True, (position1, position2)

def test_encoder(args):

    position1, position2 = args[0], args[1]
    if position2 == position1:
        return False, 'encoder1 not working'
    elif position2 < position1:
        return False, 'encoder1 reading reversed'
    return True, []

def test_reset_clock(args):

    t1 = device.get_signal('clock')
    device.set_source('clock', reset = True)
    device.set_signal('motor1',0)
    with device:
        time.sleep(.1)
    t2 = device.get_signal('clock')
    if t2 > t1:
        return False, 'clock did not reset ({} > {})'.format(t2,t1)
    return True, []

def test_motor_backward(args):

    with device:
        device.set_source('clock', reset = True)
        position1 = device.get_signal('encoder1')
        device.set_signal('motor1',-100)
        time.sleep(2)
        position2 = device.get_signal('encoder1')
    device.set_signal('motor1',0)
    return True, (position1, position2)

def test_motor_speeds(args):

    with device:
        device.set_source('clock', reset = True)
        device.set_signal('motor1',100)
        time.sleep(2)
        device.set_signal('motor1',50)
        time.sleep(2)
    device.set_signal('motor1',0)
    return True, []

def test_reset_encoder(args):

    with device:
        device.set_signal('motor1',0)
        time.sleep(.1) # sleep to make sure it is stoped
        position1 = device.get_signal('encoder1')
        device.set_source('encoder1', reset = True)
        time.sleep(.1) # sleep to make sure it did not move
        position2 = device.get_signal('encoder1')

    if position2 != 0:
        return False, 'could not reset encoder1 ({} != 0)'.format(position2)
    return True, [position1, position2]

def test_potentiometer(args):

    # Calibrate potentiometer
    KMAX = 600
    TOL = 2
    #device.get_source('analog1', ['full_scale', 'invert'])
    device.set_source('analog1', full_scale = 1, invert = False)
    inverted = False
    full_scale = 100
    print("> Set the potentiometer to the minimum position and hit <ENTER>")
    k = 0
    while k < KMAX:
        pot_min, = device.read_source('analog1')
        print('\r  reading = {:4.1f}'.format(pot_min), end='')
        time.sleep(.1)
        if select.select([sys.stdin], [], [], 0)[0]:
            line = sys.stdin.readline()
            if pot_min > TOL:
                inverted = True
                full_scale = pot_min
            break
        k += 1

    #if pot_min > TOL:
    #    return (False, 'potentiometer did not reach minimum')

    print("\n> Set the potentiometer to the maximum position and hit <ENTER>")
    k = 0
    pot_max = 0
    while k < KMAX:
        pot_max, = device.read_source('analog1')
        print('\r  reading = {:4.1f}'.format(pot_max), end='')
        time.sleep(.1)
        if select.select([sys.stdin], [], [], 0)[0]:
            line = sys.stdin.readline()
            if pot_max > TOL:
                inverted = False
                full_scale = pot_max
            break
        k += 1

    #if pot_max < 100 - TOL:
    #    return (False, 'potentiometer did not reach maximum')

    if inverted:
        print('> Potentiometer is INVERTED [CORRECT]')
    else:
        print('> Potentiometer IS NOT inverted [INCORRECT]')
    print('> Full scale is {}'.format(full_scale))

    return True, [pot_min, pot_max]

def main():

    print('> Initiating Tests...')

    k = 1
    position1, position2 \
        = test('{}: MOTOR FORWARD'.format(k), (),
               'Did the motor spin clockwise for two seconds?',
               'motor1 not working',
               test_motor_forward)

    k += 1
    test('{}: ENCODER FORWARD'.format(k), (position1, position2),
         '',
         '',
         test_encoder)

    k += 1
    test('{}: CLOCK RESET'.format(k), (),
         '',
         '',
         test_reset_clock)

    k += 1
    position1, position2 \
        = test('{}: MOTOR BACKWARD'.format(k), (),
               'Did the motor spin counter-clockwise for two seconds?',
               'motor1 not working',
               test_motor_backward)

    k += 1
    test('{}: ENCODER BACKWARD'.format(k), (position2, position1),
         '',
         '',
         test_encoder)

    k += 1
    test('{}: MOTOR TWO SPEEDS'.format(k), (),
         'Did the motor spin at full speed then slowed down to half speed?',
         'motor1 not working',
         test_motor_speeds)

    k += 1
    position1, position2 \
        = test('{}: ENCODER RESET'.format(k), (),
               '',
               '',
               test_reset_encoder)

    if 'analog1' in device.list_sources():
        k += 1
        position1, position2 \
            = test('{}: POTENTIOMETER RANGE'.format(k), (),
                   '',
                   '',
                   test_potentiometer)

    # Identify motor
    print('> Identifying parameters...')
    with device:
        device.set_signal('motor1',0)
        device.set_source('encoder1', reset = True)

        device.set_sink('logger', reset = True)
        device.set_source('clock', reset = True)
        time.sleep(1)
        device.set_signal('motor1',100)
        time.sleep(5)

        clock = device.get_source('clock', 'period')
        Ts = clock['period']

        log = device.get_sink('logger', 'log')
        t = log['clock']
        position = log['encoder1']
        velocity = numpy.zeros(t.shape, float)
        velocity[1:] = (position[1:]-position[:-1])/(t[1:]-t[:-1])

        max_velocity = numpy.max(velocity)
        ind = numpy.argwhere(velocity > .5*max_velocity)[0]

        #print('ind = {}'.format(ind))
        ind += int(2/Ts)
        #print('>> len = {}'.format(len(velocity)))
        #print('>> ind = {}'.format(ind))
        k = numpy.mean(velocity[ind:])

        print('  period = {}'.format(Ts))
        print('  max velocity = {:5.3f}'.format(max_velocity))

        print('  gain      = {:5.3f}'.format(k))

        ind = numpy.argwhere( (velocity > 0.1*k ) &
                              (velocity < 0.9*k ) )
        t10 = float(t[ind[0]])
        t90 = float(t[ind[-1]])
        tau = (t90 - t10) / 2.2
        print('  rise time = {:5.3f}'.format(t90 - t10))
        print('  lambda    = {:5.3f}'.format(1/tau))

if __name__ == "__main__":
    main()

max_stars_repo_name: ComplexArts/pyctrl-core

max_issues_repo_name: mcdeoliveira/beaglebone

max_forks_repo_name: mcdeoliveira/beaglebone