text:
# Import packages
import numpy as np
import matplotlib.pyplot as plt

import json
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
from helper_functions import load_checkpoint, process_image, label_mapping
from model_functions import predict

# Load model checkpoint from path
print('Loading checkpoint...')
checkpoint = 'checkpoint_cli.pth'
model, optimizer, epochs = load_checkpoint(checkpoint)

# Process image to run through model
input_image = 'test_image.jpg'
image = process_image(input_image)

# Load dictionary mapping labels to category names
cat_to_name = 'cat_to_name.json'
label_dict = label_mapping(cat_to_name)

# Run the image through the model and obtain the topk predictions
topk = 5
# Identify the device on which the prediction will be performed
#GPU = arg_dict['gpu']
GPU = False
if GPU:
    device = torch.device('cuda')
else:
    device = torch.device('cpu')

probs, classes = predict(image, model, topk, device)

# Create list of labels for the topk classes
labels = []
for cls in classes:
    labels.append(label_dict[cls])

# Plot the cropped input image and the topk classes with probabilities
fig, (ax1,ax2) = plt.subplots(2,1,figsize=(4,8))
# Undo transposition of dimensions required by PyTorch
image = image.transpose((1, 2, 0))
# Undo preprocessing
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image = std * image + mean

# Image needs to be clipped between 0 and 1 or it looks like noise when displayed
image = np.clip(image, 0, 1)

# Turn off axes labels for cleaner image
#ax1.set_title(cat_to_name['1'])
ax1.tick_params(axis='both', length=0)
ax1.set_xticklabels('')
ax1.set_yticklabels('')

ax1.imshow(image)

ax2.barh(np.arange(len(probs)),probs)
ax2.invert_yaxis()
ax2.set_yticks(np.arange(len(probs)))
ax2.set_yticklabels(labels)
plt.show()


text:
import gym.spaces
import numpy as np

from . import util

ALIGNMENT = 64

TEST_SPACES = [
    gym.spaces.Dict(
        [
            ("s1", gym.spaces.Box(shape=(8, 8), low=0, high=128, dtype=np.uint8)),
            (
                "s2",
                gym.spaces.Box(
                    shape=(16, 16, 16), low=-1.0, high=1.0, dtype=np.float64
                ),
            ),
        ]
    ),
    gym.spaces.Dict([("s1", gym.spaces.MultiBinary(10))]),
    gym.spaces.Dict([("s1", gym.spaces.Discrete(12))]),
    gym.spaces.Dict(
        [("s1", gym.spaces.Box(shape=(8, 8), low=0, high=128, dtype=np.uint8))]
    ),
]

def test_alignment():
    spaces = [
        gym.spaces.Dict(
            [
                ("s1", gym.spaces.Box(shape=(8, 8), low=0, high=128, dtype=np.uint8)),
                (
                    "s2",
                    gym.spaces.Box(
                        shape=(16, 16, 16), low=-1.0, high=1.0, dtype=np.float64
                    ),
                ),
            ]
        ),
        gym.spaces.Dict([("s1", gym.spaces.MultiBinary(10))]),
        gym.spaces.Dict([("s1", gym.spaces.Discrete(12))]),
        gym.spaces.Dict(
            [("s1", gym.spaces.Box(shape=(8, 8), low=0, high=128, dtype=np.uint8))]
        ),
    ]
    for space in spaces:
        for buf in [bytearray(2 ** 20), None]:
            arrays, _buf = util.create_space_arrays(3, space, align=ALIGNMENT, buf=buf)
            for arr in arrays.values():
                assert arr.ctypes.data % ALIGNMENT == 0

ext: py

max_stars_repo_name: cshesse/netenv

max_issues_repo_name: cshesse/netenv

max_forks_repo_name: cshesse/netenv

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 13 18:35:28 2021

@author: zayn
"""

import sys
sys.path.append('../../software/algorithms/')

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

plt.close('all')

print('plotting data')
df=pd.read_csv('../../data/lreg_data1.txt', names=['population', 'profit'])

data=df.values

X=data[:,0]
y=data[:,1]

fig1, ax1 = plt.subplots()
ltrd=ax1.plot(X,y,'bo', label='Training data')
ax1.set_xlabel('population')
ax1.set_ylabel('profit')

# first_legend = plt.legend(handles=[ltrd], loc='upper right')

print('Program paused. Press enter to continue.\n')
# input()
print('Running Gradient Descent...\n')

m=len(y)
size = 1
for dim in np.shape(X): size *= dim
n=size//m

X.shape = (m, n)
# y.shape=(m,1)

X=np.append(np.ones([m,1]),X, axis=1) # Add a column of ones to x

theta=np.zeros(n+1) # initialize fitting parameters
# Some gradient descent settings
lreg=lir.LReg()
iterations=1500
alpha=0.01
# compute and display initial cost
J=lreg.computeCost(X, y, theta)
print(J)

theta, J_history = lreg.gradientDescent(X, y, theta, alpha, iterations)

print('Theta found by gradient descent: ')

print('{:0.5f},  {:0.5f}'.format(theta[0], theta[1]))

# Plot the linear fit
predicton=X@theta

lregpl=ax1.plot(X[:,1],predicton, '-r', label='Linear regression')
ax1.legend(framealpha=1, frameon=True);

# %% ============= Part 4: Visualizing J(theta_0, theta_1) =============

print('Visualizing J(theta_0, theta_1) ...\n')

#  Grid over which we will calculate J
theta0_vals = np.linspace(-10, 10, 100);
theta1_vals = np.linspace(-1, 4, 110);

# initialize J_vals to a matrix of 0's
J_vals = np.zeros([len(theta0_vals), len(theta1_vals)]);

# Fill out J_vals
t=np.zeros(n+1)
for i in range(len(theta0_vals)):
    t[0]=theta0_vals[i]
    for j in  range(len(theta1_vals)):
        t[1]=theta1_vals[j]

        J_vals[i,j] = lreg.computeCost(X, y, t);

T0, T1 = np.meshgrid(theta0_vals, theta1_vals)

fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection='3d')

ax2.plot_surface(T0, T1, J_vals.T)

ax2.set_xlabel('\u03F4_0')
ax2.set_ylabel('\u03F4_1')
ax2.set_zlabel('cost')

df=pd.read_csv('../../data/lreg_data2.txt', names=['p1', 'p2','p3'])

data=df.values.astype(np.float64)

X=data[:,:-1]
y=data[:,-1]
m=len(y)

size = 1
for dim in np.shape(X): size *= dim
n=size//m

X.shape = (m, n)

X,mu,sigma = lreg.featureNormalize(X)

X=np.append(np.ones([m,1]), X, axis=1)
alpha = 0.005
num_iters = 4000

# Init Theta and Run Gradient Descent
theta = np.zeros(n+1);
theta, J_history = lreg.gradientDescent(X, y, theta, alpha, num_iters);

fig3, ax3 = plt.subplots()
ltrd=ax3.plot(J_history,'b-', label='Cost History')
ax3.set_xlabel('#Num of Iterations')
ax3.set_ylabel('Cost')

xtest=np.array([1650., 3.])
xtestn=xtest-mu
xtestn=xtestn/sigma
xtestne=np.insert(xtestn, 0,1)

predict_price=xtestne.T@theta

print('lreg steepest descent preicted price is \n')

print('{:0.2f}'.format(predict_price))

theta_theo=lreg.normalEqn(X, y)

xteste=np.insert(xtest, 0,1)

theo_price=(xtestne.T@theta)

print('lreg theor. preicted price is \n')

print('{:0.2f}'.format(theo_price))


text:
from flask import Flask, render_template, request
import pickle
import numpy as np

app = Flask(__name__)
model = pickle.load(open("model.pkl", 'rb'))
vector = pickle.load(open('vectorizer.pkl', 'rb'))

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/predict', methods=["POST", "GET"])
def predict():
    msg = request.form['message']
    msg_cv = vector.transform([msg])
    classification = model.predict(msg_cv)
    if classification[0]==0:
        prediction = "This is a spam message"
    else:
        prediction = "This is a non-spam message"
    return render_template('index.html',prediction=prediction)

if __name__ == '__main__':
    app.run()


text:
"""This module contains the core classes and functions.

Internally, the package format for a point is a :class:`numpy.ndarray` with shape (2,). This simplifies calculations
that treat points as vectors in two dimensions. To avoid forcing users to input points in this format, user-facing
functions and methods convert single input points to package format using :func:`to_point`, and convert sequences of
points using :func:`to_point_list`. The docstrings for such functions that accept two-element indexable objects
specify this using type 'indexable' and specify iterables of such objects using type 'iterable[indexable]'. If the
inputs must already be converted to package format, and to describe return types in package format, the documentation
uses 'point' and 'list[point]'.
"""
from __future__ import absolute_import, division, print_function
import warnings

import gdspy
from matplotlib.font_manager import FontProperties
from matplotlib.textpath import TextPath
import numpy as np

# The maximum number of polygon vertices in the original GDSII specification is 200. This is the (conservative) default
# value used by gdspy.
SAFE_GDSII_POLYGON_POINTS = 199

# The maximum number of polygon vertices allowed by the GDSII file structure is 8191. Some GDSII readers may open files
# that contain more points, but, anecdotally, some will fail to load such designs. This value is the package default.
MAX_GDSII_POLYGON_POINTS = 8190

# Both of the limits above are one less than the maximum, possibly because the vertex that closes the polygon is stored
# in the file but does not have to be passed to gdspy, which closes polygons automatically.
# The value below is the default used when drawing polygons throughout this package.
MAX_POINTS = MAX_GDSII_POLYGON_POINTS
# It can be changed for a single `draw` call by passing a different value of `max_points` or it can be changed globally:
# from transmission_line import transmission_line as tl
# tl.MAX_POINTS = tl.SAFE_GDSII_POLYGON_POINTS  # For use with old GDSII readers
# tl.MAX_POINTS = 0  # gdspy treats 0 as infinity, and will not fracture polygons to reduce the number of vertices.

# This is the default number of points per degree of arc used by :func:`smooth` to bend transmission lines.
POINTS_PER_DEGREE = 1

# These are the default font properties used by :func:`polygon_text`, which renders text with matplotlib.
FONT_PROPERTIES = {
    'family': 'sans-serif',
    'style': 'normal'
}

def to_point(indexable):
    """Return a package-format point, a :class:`numpy.ndarray` with shape (2,) containing (x, y) coordinates.

    :param indexable indexable: an indexable object with integer indices 0 and 1, such as a two-element tuple.
    :return: an array with shape (2,) containing the values at these two indices.
    :rtype: point
    """
    return np.array([indexable[0], indexable[1]])

def to_point_list(iterable):
    """Return a list of package-format points from using :func:`to_point` on each element of the given iterable.

    This function accepts, for example, a list of two-element tuples, or a single ndarray with shape ``(N, 2)``, as used
    by gdspy for polygons.

    :param iterable[indexable] iterable: an iterable of indexable objects that all have integer indices 0 and 1.
    :return: a list of arrays with shape ``(2,)`` containing (x, y) coordinates.
    :rtype: list[point]
    """
    return [to_point(point) for point in iterable]

def from_increments(increments, origin=(0, 0)):
    """Return a list of points starting from the given origin and separated by the given increments, treated as vectors.

    Specify a path in terms of the differences between points instead of the absolute values::

        >>> from_increments(increments=[(200, 0), (0, 300)], origin=(100, 0))
        [np.array([100, 0]), np.array([300, 0]), np.array([300, 300])]

    :param iterable[indexable] increments: the vector differences between consecutive points.
    :param point origin: the starting point.
    :return: a list of points in package format.
    :rtype: list[point]
    """
    points = [to_point(origin)]
    for increment in to_point_list(increments):
        points.append(points[-1] + increment)
    return points

def polygon_text(text, size, position, layer=0, datatype=0, font_properties=None, tolerance=0.1):
    """Return the given text as polygons.

    If the text size is small, holes in letters can create structures that do not lift off easily. To avoid this,
    one option is to pass `family='stencil'`, which draws the text in an uppercase-only stencil font (available on
    Windows) with no holes.

    :param str text: the text to render as polygons.
    :param float size: the approximate size of the text in user units.
    :param indexable position: the coordinates are (left_edge, baseline), so the text may descend below the baseline.
    :param int layer: the GDSII layer.
    :param int datatype: the GDSII datatype.
    :param font_properties: if None, use :attr:`FONT_PROPERTIES` in this module; if dict, update these defaults;
                            see :module:`matplotlib.font_manager` for valid keys.
    :type font_properties: dict or None
    :param float tolerance: this has something to do with the number of points used to draw the polygon; the default
                            seems fine.
    :return: polygons representing the text.
    :rtype: gdspy.PolygonSet
    """
    fp = FONT_PROPERTIES.copy()
    if font_properties is not None:
        fp.update(font_properties)
    polygons = _render_text(text=text, size=size, position=position, font_prop=FontProperties(**fp),
                            tolerance=tolerance)
    return gdspy.PolygonSet(polygons=polygons, layer=layer, datatype=datatype)

def _render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1):
    """This function is copied from https://gdspy.readthedocs.io/en/stable/gettingstarted.html#using-system-fonts"""
    path = TextPath(position, text, size=size, prop=font_prop)
    polys = []
    xmax = position[0]
    for points, code in path.iter_segments():
        if code == path.MOVETO:
            c = gdspy.Curve(*points, tolerance=tolerance)
        elif code == path.LINETO:
            c.L(*points)
        elif code == path.CURVE3:
            c.Q(*points)
        elif code == path.CURVE4:
            c.C(*points)
        elif code == path.CLOSEPOLY:
            poly = c.get_points()
            if poly.size > 0:
                if poly[:, 0].min() < xmax:
                    i = len(polys) - 1
                    while i >= 0:
                        if gdspy.inside(
                                poly[:1], [polys[i]], precision=0.1 * tolerance
                        )[0]:
                            p = polys.pop(i)
                            poly = gdspy.boolean(
                                [p],
                                [poly],
                                "xor",
                                precision=0.1 * tolerance,
                                max_points=0,
                            ).polygons[0]
                            break
                        elif gdspy.inside(
                                polys[i][:1], [poly], precision=0.1 * tolerance
                        )[0]:
                            p = polys.pop(i)
                            poly = gdspy.boolean(
                                [p],
                                [poly],
                                "xor",
                                precision=0.1 * tolerance,
                                max_points=0,
                            ).polygons[0]
                        i -= 1
                xmax = max(xmax, poly[:, 0].max())
                polys.append(poly)
    return polys

# ToDo: warn if consecutive points are too close together to bend properly.
def smooth(points, radius, points_per_degree=POINTS_PER_DEGREE, already_package_format=False):
    """Return a list of smoothed points constructed by adding points to change the given corners into circular arcs.

    At each corner, the original point is replaced by points that form circular arcs that are tangent to the original
    straight sections. Because this process replaces all of the interior points, the returned path will not contain any
    of the given points except for the start and end.

    If the given radius is too large compared to the length of the straight sections there is no way to make this
    work, and the results will be ugly. **No warning is currently given when this happens, so you need to inspect
    your design.** The given radius should be smaller than about half the length of the shortest straight section. If
    several points lie on the same line, the redundant ones are removed.

    :param iterable[indexable] points: a list of points forming the outline of the path to smooth.
    :param float radius: the radius of the circular arcs used to connect the straight segments.
    :param float points_per_degree: the number of points per degree of arc; the default of 1 is usually enough.
    :param bool already_package_format: if True, skip the conversion of the points to package format (used internally
                                        by :class:`SmoothedSegment` to avoid double-conversion).
    :return: four lists with length equal to the number of bends (i.e., two less than the number of given points)
             that contain, for each bend, (1) a list of points in that bend, (2) the bend angle in radians, (3) the
             corner point  from the original list, and (4) the vector offset of the bend arc center relative to the
             corner point.
    :rtype: tuple[list]
    """
    bends = list()
    angles = list()
    corners = list()
    offsets = list()
    if not already_package_format:
        points = to_point_list(points)
    for before, current, after in zip(points[:-2], points[1:-1], points[2:]):
        before_to_current = current - before
        current_to_after = after - current
        # The angle at which the path bends at the current point, in (-pi, pi)
        bend_angle = np.angle(np.inner(before_to_current, current_to_after) +
                              1j * np.cross(before_to_current, current_to_after))
        if np.abs(bend_angle) > 0:  # If the three points are co-linear then drop the current point
            # The distance from the corner point to the arc center
            h = radius / np.cos(bend_angle / 2)
            # The absolute angle of the arc center point, in (-pi, pi)
            theta = (np.arctan2(before_to_current[1], before_to_current[0]) +
                     bend_angle / 2 + np.sign(bend_angle) * np.pi / 2)
            # The offset of the arc center relative to the corner
            offset = h * np.array([np.cos(theta), np.sin(theta)])
            # The absolute angles of the new points (at least two), using the absolute center as origin
            arc_angles = (theta + np.pi
                          + np.linspace(-bend_angle / 2, bend_angle / 2,
                                        int(np.ceil(np.degrees(np.abs(bend_angle)) * points_per_degree) + 1)))
            bend = [current + offset + radius * np.array([np.cos(phi), np.sin(phi)]) for phi in arc_angles]
            bends.append(bend)
            angles.append(bend_angle)
            corners.append(current)
            offsets.append(offset)
    return bends, angles, corners, offsets

class SegmentList(list):
    """A list subclass to contain Segments that can be drawn sequentially to form a path.

    Slicing a SegmentList returns a SegmentList so that intermediate points can be computed easily:
    ``segment_list[:4].end`` gives the endpoint of the first four elements joined head to tail.
    """

    def draw(self, cell, origin, flatten=True, individual_keywords=None, **global_keywords):
        """Draw all of the segments contained in this SegmentList into the given cell, connected head to tail, and
        and return the drawn structures.

        The segments are drawn as follows: the origin of the first segment is the given origin, and the origin of
        each subsequent segment is the end of the previous segment.

        :param cell: The cell into which the result is drawn, if not None.
        :type cell: gdspy.Cell or None
        :param indexable origin: The point to use for the origin of the first Segment.
        :param bool flatten: if False, return a list of tuples of the objects returned by the :meth:`draw` methods of
                             each :class:`Segment`, in order; if True, flatten the returned list so that its elements
                             are the structures themselves.
        :param individual_keywords: keys are integer indices and values are dicts of parameters that update the
                                    the :meth:`draw` call for the Segment at that index; use this to override the global
                                    keywords or to pass keywords that not all Segments accept.
        :type individual_keywords: dict or None
        :param global_keywords: keyword arguments passed to every :meth:`Segment.draw`.
        :return: the drawn structures ordered from start to end; see the `flatten` keyword.
        :rtype: list
        """
        if individual_keywords is not None and (min(individual_keywords.keys()) < 0 or
                                                max(individual_keywords.keys()) > len(self)):
            raise ValueError("Index in individual_keywords is outside valid range.")
        drawn = list()
        # It's crucial to avoiding input modification that this also makes a copy.
        point = to_point(origin)
        for index, segment in enumerate(self):
            keywords = global_keywords.copy()
            if individual_keywords is not None:
                keywords.update(individual_keywords.get(index, dict()))
            tuple_of_structures = segment.draw(cell=cell, origin=point, **keywords)
            if flatten:
                try:
                    drawn.extend(tuple_of_structures)
                except TypeError:
                    warnings.warn(f"Appending instead of flattening the non-iterable object returned by Segment at"
                                  f" index {index:d}: {tuple_of_structures!r}")
                    drawn.append(tuple_of_structures)
            else:
                drawn.append(tuple_of_structures)
            # NB: using += produces an error when casting int to float.
            point = point + segment.end
        return drawn

    def __getitem__(self, item):
        """Re-implement this method so that slices return SegmentLists."""
        if isinstance(item, slice):
            return self.__class__(super().__getitem__(item))
        else:
            return super().__getitem__(item)

    @property
    def start(self):
        """The start point of the first element in this SegmentList, assuming its origin is (0, 0)."""
        return self[0].start

    @property
    def end(self):
        """The end point of the last element in this SegmentList, assuming its origin is (0, 0)."""
        return np.sum(np.vstack([element.end for element in self]), axis=0)

    @property
    def span(self):
        """The difference between start and end points: span = end - start, in the vector sense."""
        return self.end - self.start

    @property
    def length(self):
        """The sum of the lengths of the Segments in this SegmentList.

        The calculation sums the `length` properties of all the Segments, and it does **not** check that the Segments
        are all connected head-to-tail.
        """
        return np.sum([element.length for element in self])

    @property
    def points(self):
        """Return a list of lists each containing the points of one Segment in this SegmentList.

        The calculation assumes that the first element starts at (0, 0) and that subsequent elements are placed
        head-to-tail, as when they are drawn, so the points in all lists after the initial one are not the same as the
        points of the corresponding element.

        :return: the as-drawn points of each Segment.
        :rtype: list[list[point]]
        """
        point_lists = list()
        end = to_point((0, 0))
        for element in self:
            point_lists.append([point + end for point in element.points])
            end += element.end
        return point_lists

    @property
    def bounds(self):
        """Return the lower left and upper right points of the smallest rectangle that encloses the points of all
        elements in the SegmentList.

        The calculation assumes that the first element starts at (0, 0) and that subsequent elements are placed
        head-to-tail, as when they are drawn; see :meth:`points`.

        :return: the lower left and upper right points.
        :rtype: tuple[point]
        """
        all_points = [p for element_points in self.points for p in element_points]  # Make a flat list of all points
        xy = np.vstack(all_points).T  # points.shape = (2, num_points)
        return to_point((np.min(xy[0]), np.min(xy[1]))), to_point((np.max(xy[0]), np.max(xy[1])))

class Segment(object):
    """An element in a SegmentList that can draw itself into a cell."""

    def __init__(self, points, round_to=None):
        """The given points are saved as :attr:`_points`, and should generally not be modified.

        :param iterable[indexable] points: the points that form the Segment.
        :param round_to: if not None, the coordinates of each point are rounded to this value; useful for ensuring that
                         all the points in a design lie on a grid (larger than the database unit size).
        :type round_to: float, int, or None
        """
        points = to_point_list(points)
        if round_to is not None:
            points = [round_to * np.round(p / round_to) for p in points]
        self._points = points

    @property
    def points(self):
        """The points (``list[numpy.ndarray]``) in this Segment, rounded to ``round_to`` (read-only)."""
        return self._points

    @property
    def start(self):
        """The start point (``numpy.ndarray``) of the Segment (read-only)."""
        return self._points[0]

    @property
    def end(self):
        """The end point (``numpy.ndarray``) of the Segment (read-only)."""
        return self._points[-1]

    @property
    def span(self):
        """The difference between start and end points: span = end - start, in the vector sense."""
        return self.end - self.start

    @property
    def x(self):
        """A ``numpy.ndarray`` containing the x-coordinates of all points (read-only)."""
        return np.array([point[0] for point in self.points])

    @property
    def y(self):
        """A ``numpy.ndarray`` containing the y-coordinates of all points (read-only)."""
        return np.array([point[1] for point in self.points])

    @property
    def length(self):
        """The length of the Segment, calculating by adding the lengths of straight lines connecting the points."""
        return np.sum(np.hypot(np.diff(self.x), np.diff(self.y)))

    def draw(self, cell, origin, max_points=MAX_POINTS, **keywords):
        """Create and return polygons or other structures and add them to the given cell, if one is specified.

        Subclasses implement this method to draw themselves. They should return an iterable of the drawn structure(
        s), and these structures should not contain more than `max_points` points. Passing `cell=None` should draw
        the structures without adding them to a call, which is useful for temporary structures used for boolean
        operations.

        :param cell: the cell into which this Segment will be drawn, if not None.
        :type cell: gdspy.Cell or None
        :param indexable origin: draw the Segment relative to this point, meaning that point (0, 0) of the Segment is
                                 placed here.
        :return: subclasses should return an iterable of the drawn structure(s).
        :rtype: tuple
        """
        return ()

class SmoothedSegment(Segment):
    """An element in a SegmentList that can draw itself into a cell, with corners smoothed using :func:`smooth`."""

    def __init__(self, outline, radius, points_per_degree, round_to=None):
        """The given outline points are passed to :func:`smooth` and the result is stored in the instance attributes
        :attr:`bends`, :attr:`angles`, :attr:`corners`, and :attr:`offsets`.

        :param iterable[indexable] outline: the outline points, before smoothing.
        :param float radius: the radius of the circular arcs used to connect the straight segments; see :func:`smooth`.
        :param int points_per_degree: the number of points per degree of arc; see :func:`smooth`.
        :param round_to: if not None, the coordinates of each outline point are rounded to this value **before
                         smoothing**; useful for ensuring that all the points in a design lie on a grid larger than the
                         database unit size.
        :type round_to: float, int, or None
        """
        super(SmoothedSegment, self).__init__(points=outline, round_to=round_to)
        self.radius = radius
        self.points_per_degree = points_per_degree
        self.bends, self.angles, self.corners, self.offsets = smooth(self._points, radius, points_per_degree,
                                                                     already_package_format=True)

    @property
    def points(self):
        """The smoothed points (``list[numpy.ndarray]``); the original outline points are ``_points`` (read-only)."""
        p = [self.start]
        for bend in self.bends:
            p.extend(bend)
        p.append(self.end)
        return p

class AbstractTransmissionLine(object):
    """An abstract transmission line.

    Subclasses should implement the properties given here to calculate inductance and capacitance per unit length.
    """

    @property
    def capacitance_per_unit_length(self):
        """Return the capacitance per unit length in farads per meter."""
        pass

    @property
    def inductance_per_unit_length(self):
        """Return the inductance per unit length in henries per meter."""
        pass

    @property
    def characteristic_impedance(self):
        """Return the characteristic impedance in ohms."""
        return (self.inductance_per_unit_length / self.capacitance_per_unit_length) ** (1 / 2)

    @property
    def phase_velocity(self):
        """Return the phase velocity in meters per second."""
        return (self.inductance_per_unit_length * self.capacitance_per_unit_length) ** (-1 / 2)

max_stars_repo_name: danielflanigan/transmission-line

max_issues_repo_name: danielflanigan/transmission-line

max_forks_repo_name: danielflanigan/transmission-line


text:
import pandas as pd
import numpy as np

DATADIR = 'UCI HAR Dataset'

SIGNALS = [
    "body_acc_x",
    "body_acc_y",
    "body_acc_z",
    "body_gyro_x",
    "body_gyro_y",
    "body_gyro_z",
    "total_acc_x",
    "total_acc_y",
    "total_acc_z"
]

def _read_csv(filename):
    return pd.read_csv(filename, delim_whitespace=True, header=None)

def load_signals(subset):
    signals_data = []

    for signal in SIGNALS:
        filename = f'{DATADIR}/{subset}/Inertial Signals/{signal}_{subset}.txt'
        signals_data.append(
            _read_csv(filename).as_matrix()
        )

    # Transpose is used to change the dimensionality of the output,
    # aggregating the signals by combination of sample/timestep.
    # Resultant shape is (7352 train/2947 test samples, 128 timesteps, 9 signals)
    return np.transpose(signals_data, (1, 2, 0))
#               original: np.transpose(0, 1, 2)

def load_y(subset):
    """
    The objective that we are trying to predict is a integer, from 1 to 6,
    that represents a human activity. We return a binary representation of
    every sample objective as a 6 bits vector using One Hot Encoding
    (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html)
    """
    filename = f'{DATADIR}/{subset}/y_{subset}.txt'
    y = _read_csv(filename)[0]

    return pd.get_dummies(y).as_matrix()

def load_data():
    """
    Obtain the dataset from multiple files.
    Returns: X_train, X_test, y_train, y_test
    """
    X_train, X_test = load_signals('train'), load_signals('test')
    y_train, y_test = load_y('train'), load_y('test')

    return X_train, X_test, y_train, y_test


text:
import argparse
import cv2
import sys
import numpy as np
import subprocess as sp
from inference import Network

INPUT_STREAM = "videos/smile_pal.mp4"
CPU_EXTENSION = "/media/anilsathyan7/work/vino/openvino/deployment_tools/inference_engine/lib/intel64/libcpu_extension_sse4.so"#"/opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libcpu_extension_sse4.so"

# Ffmpeg command for encoding output video
command = [ 'ffmpeg',
        '-f', 'rawvideo',
        '-vcodec','rawvideo',
        '-s', '1024x576',
        '-pix_fmt', 'bgr24',
        '-re',
        '-i', '-',
        '-r', '15',
        '-an',
        '-c:v', 'libx264',
        '-preset', 'ultrafast',
        '-f', 'flv',
        'rtmp://0.0.0.0:1935/LiveApp/segme live=1' ]

# Pipe the model output to server
pipe = sp.Popen( command, stdin=sp.PIPE, stderr=sp.PIPE)

def get_args():
    '''
    Gets the arguments from the command line.
    '''
    parser = argparse.ArgumentParser("Run inference on an input video")
    # -- Create the descriptions for the commands
    m_desc = "The location of the model XML file"
    i_desc = "The location of the input file"
    d_desc = "The device name, if not 'CPU'"

    # -- Add required and optional groups
    parser._action_groups.pop()
    required = parser.add_argument_group('required arguments')
    optional = parser.add_argument_group('optional arguments')

    # -- Create the arguments
    required.add_argument("-m", help=m_desc, required=True)
    optional.add_argument("-i", help=i_desc, default=INPUT_STREAM)
    optional.add_argument("-d", help=d_desc, default='CPU')
    args = parser.parse_args()

    return args

def infer_on_video(args):
    # Initialize the Inference Engine
    plugin = Network()

    # Load the network model into the IE
    plugin.load_model(args.m, args.d, CPU_EXTENSION)
    net_input_shape = plugin.get_input_shape()

    # Get and open video capture
    cap = cv2.VideoCapture(args.i)
    cap.open(args.i)

    # Grab the shape of the input
    width = int(cap.get(3))
    height = int(cap.get(4))

    # Create a video writer for the output video
    fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
    out = cv2.VideoWriter('out.mp4', fourcc, 15, (width,height))
    frame_count = 0

    # Process frames until the video ends, or process is exited
    while cap.isOpened():
        # Read the next frame
        flag, input_frame = cap.read()

        if not flag:
            break
        key_pressed=cv2.waitKey(60)

        # Pre-process the frame
        input_frame = cv2.cvtColor(input_frame, cv2.COLOR_BGR2RGB)
        p_frame = cv2.resize(input_frame, (net_input_shape[3], net_input_shape[2]))
        p_frame = p_frame.transpose((2,0,1))
        p_frame = p_frame.reshape(1, *p_frame.shape)

        # Perform inference on the frame
        plugin.async_inference(p_frame)

        # Get the output of inference
        if plugin.wait() == 0:
            result = plugin.extract_output()
            result = result.transpose((1,2,0))

            # Get semantic mask for person class
            person_mask = np.uint8(result==15)
            person_mask = np.dstack([person_mask,person_mask,person_mask])
            person_mask = cv2.resize(person_mask, (width, height))

            # Create the overlay mask
            overlay = np.zeros_like(person_mask)
            overlay[:] = (127, 0, 0)

            # Add overlay-mask over input frame
            overlay_mask = person_mask * overlay
            assert person_mask.shape==overlay_mask.shape, "Raw person mask and overlay mask should be of same dimensions"
            output_frame = cv2.addWeighted(input_frame, 1, overlay_mask, 0.9, 0)

            # Write output frames to video
            output_frame=cv2.cvtColor(output_frame, cv2.COLOR_RGB2BGR)
            out.write(output_frame)

            # Pipe output frames to server
            pipe.stdin.write( output_frame.tostring() )

            # Show frame counter
            frame_count=frame_count+1
            print('Frame count: '+str(frame_count))

        # Break on keyboard interrupt
        if key_pressed == 27:
            break

    # Release the capture and destroy any OpenCV windows
    cap.release()
    out.release()
    cv2.destroyAllWindows()

def main():
    args = get_args()
    infer_on_video(args)

if __name__ == "__main__":
    main()

max_issues_repo_name: steveseguin/Portrait-Segmentation

max_forks_repo_name: steveseguin/Portrait-Segmentation


text:
#!/usr/bin/env python3

import csv
import io
import logging
import multiprocessing
import numpy as np
import os
import pandas as pd
from PIL import Image
import requests

logging.basicConfig(level=logging.DEBUG)
Image.MAX_IMAGE_PIXELS = int(3e9)

download_dir = 'data/images'

positive_dir = os.path.join(download_dir, 'positive')
if not os.path.exists(positive_dir):
    os.makedirs(positive_dir)

negative_dir = os.path.join(download_dir, 'negative')
if not os.path.exists(negative_dir):
    os.makedirs(negative_dir)

images_info = pd.read_csv('data/vgdb_2016.csv')

artists = np.unique([row['Artist'] for _, row in images_info.iterrows()])
logging.debug('artists: {}'.format(artists))

def download_row(row_num, row):
    try:
        logging.debug('processing row #: {}'.format(row_num))

        # for now, ignore controversial & disputed paintings
        artist_name = row['Artist'].lower()

        if 'vincent van gogh' in artist_name:
            if 'controversial' in artist_name or 'disputed' in artist_name:
                logging.info('skipping: controversial van gogh painting')
                return

            target_dir = positive_dir
        else:
            target_dir = negative_dir

        # download image
        image_url = row['ImageURL']
        logging.debug('saving image from URL: {}'.format(image_url))

        response = requests.get(image_url)
        img = Image.open(io.BytesIO(response.content))

        if img.mode != 'RGB':
            img = img.convert('RGB')

        img.save(os.path.join(target_dir, 'image_{}.jpg'.format(row_num)))
    except Exception:
        logging.exception('Encountered exception on row #: {}'.format(row_num))

pool = multiprocessing.Pool(8)
pool.starmap(download_row, images_info.iterrows())
pool.close()

# rows = list(images_info.iterrows())
# for i, row in rows:
#     if i % 10 == 0:
#         logging.debug('done {} / {}'.format(i, len(rows)))

#     download_row(i, row)

max_stars_repo_name: JamesonWeng/VanGoghOrNo

max_issues_repo_name: JamesonWeng/VanGoghOrNo

max_forks_repo_name: JamesonWeng/VanGoghOrNo


text:
import os
import sys
import csv
import random
import unicodedata
import pandas as pd
import numpy as np
import math

import store_data

_thisDir = os.path.dirname(os.path.abspath(__file__)).decode(sys.getfilesystemencoding())
_parentDir = os.path.abspath(os.path.join(_thisDir, os.pardir))
dataDir = _parentDir + '/data/'

"""
Returns a list of all stimuli in the stim folder (assuming all the .jpg files are stimulus images)
"""
def get_stimuli(folder, prepend, fileextension):
	'''
	get array of stimuli from the directory name stim
	'''
	stimuli = []
	for stimulusFile in os.listdir(_parentDir + folder):
		if stimulusFile.endswith(fileextension):
			stimuli.append(prepend + stimulusFile[:-4])
	return stimuli

def get_foodq():
	info = pd.read_csv(_thisDir + '/FoodQuestions.csv')
	questions=info['Question']
	leftText=info['LeftText']
	middleText=info['MiddleText']
	rightText=info['RightText']
	new_info=[]
	for i in range(0,len(questions)):
		q = questions[i]
		l=leftText[i]
		if type(l) == float:
			l=''
		m=middleText[i]
		if type(m) == float:
			m=''
		r=rightText[i]
		if type(r) == float:
			r=''
		new_info.append({'question':q,'leftText':l,'middleText':m,'rightText':r})
	return new_info

"""
Creates list of dictionaries where each dictionary holds the variables for one trial
"""
def get_ratingtask_expVariables(stimFolder, demo):
	stimuli = get_stimuli(stimFolder,'','.jpg')

	expVariables = [] # array of dictionaries

	foodq = get_foodq()
	random.shuffle(foodq);

	for j in range(0, len(foodq)):
		random.shuffle(stimuli)
		for i in range(0,len(stimuli)):
			question = foodq[j]['question']
			leftRatingText = foodq[j]['leftText']
			middleRatingText = foodq[j]['middleText']
			rightRatingText = foodq[j]['rightText']
			rs_min=0
			rs_max=10
			rs_tickIncrement=1
			rs_increment=0.01
			rs_labelNames=["", "", "", "", "", "", "", "", "", "", ""]
			expVariables.append({'stimulus':stimuli[i], 'fullStimName':stimuli[i]+'.jpg', 'question':question, 'leftRatingText':leftRatingText, 'middleRatingText':middleRatingText, 'rightRatingText':rightRatingText, 'rs_min':rs_min, 'rs_max':rs_max, 'rs_tickIncrement':rs_tickIncrement, 'rs_increment':rs_increment, 'rs_labelNames':rs_labelNames})
	return expVariables

"""
Get demographic questions
Returns dictionary with keys as index of question
Values are dictionaries with questions as keys and the values are a list of options
"""
def get_demographicq():
	info = pd.read_csv(_thisDir + '/DemographicQuestions.csv')
	questions=info['Question']
	info=info.set_index('Question')
	new_info=[]
	for j in range(0,len(questions)):
		q = questions[j]
		tmp=info.loc[q].values
		options=[]
		for i in tmp:
			if type(i) != float: # remove nan values
				options.append(i)
		new_info.append({q:options})
	return new_info

def get_TFEQr18():
	info = pd.read_csv(_thisDir + '/TFEQr18.csv')
	questions=info['Question']
	option1=info['Option1']
	option2=info['Option2']
	option3=info['Option3']
	option4=info['Option4']
	return questions, option1, option2, option3, option4

def get_EAT26():
	info = pd.read_csv(_thisDir + '/EAT26.csv')
	questions=info['Question']
	option1=info['Option1']
	option2=info['Option2']
	option3=info['Option3']
	option4=info['Option4']
	option5=info['Option5']
	option6=info['Option6']
	return questions, option1, option2, option3, option4, option5, option6

neutralDimensions = {'taste':'How TASTY is this food?','health':'How HEALTHY is this food?'}

"""
Returns a dictionary of the stimuli as keys and their bids as values
csv_name is the location + name of the csv file where the bids are located
The csv must have the stimulus name in a column with the heading "stimulus"
	the bid must be in a column with the heading "rating"
# dim is dimension - key in neutralDimensions
"""
def get_bid_responses(dim, csv_name):
	'''
	from a csv file generated by the auction task, generate a dictionary of the stimuli and the participant's bids
	'''
	df = pd.read_csv(csv_name)
	df = df.loc[df['question']==neutralDimensions[dim]]
	stim = df.loc[:,'stimulus']
	bids = df.loc[:,'rating']
	stimBidDF = pd.concat([bids,stim],axis=1)
	stimBidDF = stimBidDF.rename(index=str, columns={"rating": "rating", "stimulus": "stimulus"})
	return stimBidDF

def find_common_items(l1,l2):
	print l1
	print l2
	commonItems=[]
	for item in l1:
		if item in l2:
			commonItems.append(item)
	print commonItems
	return commonItems

def get_common_item(l1,l2):
	commonItems=find_common_items(l1,l2)
	if len(commonItems) != 0: # found reference item based on HEALTH and +1 positive on TASTE
		random.shuffle(commonItems)
		referenceItem=commonItems[0]
		return referenceItem
	return None

def get_choicetask_expVariables(expID, subjectID, stimFolder, defaultRefItem, demo):
	if demo:
		refItem = 'tomato'
	else:
		# check if get_reference_item was already called when instructions display was created
		if os.path.exists(os.path.join(dataDir,expID,subjectID,subjectID+'_ReferenceItem.csv')):
			print "EXISTS"
			df=pd.read_csv(os.path.join(dataDir,expID,subjectID,subjectID+'_ReferenceItem.csv'))
			refItem=df['reference_item'].values[-1]
		else:
			refItem = get_reference_item(expID,subjectID,defaultRefItem)
	stimuli = get_stimuli(stimFolder,'','.jpg')
	random.shuffle(stimuli)
	expVariables = []
	for stim in stimuli:
		if stim != refItem:
			leftRatingText = 'Strongly prefer reference'
			middleRatingText = 'Neutral'
			rightRatingText = 'Strongly prefer food'
			rs_min=0
			rs_max=10
			rs_tickIncrement=1
			rs_increment=0.01
			rs_labelNames=["", "", "", "", "", "", "", "", "", "", ""]
			expVariables.append({"question":"Do you prefer the reference item on the left or the food on the right?", "referenceItem":refItem,"secondFoodItem":stim,"fullReferenceItemName":refItem+".jpg", "fullSecondFoodItemName":stim+".jpg", 'leftRatingText':leftRatingText, 'middleRatingText':middleRatingText, 'rightRatingText':rightRatingText, 'rs_min':rs_min, 'rs_max':rs_max, 'rs_tickIncrement':rs_tickIncrement, 'rs_increment':rs_increment, 'rs_labelNames':rs_labelNames})
	return expVariables

def get_reference_item(expID,subjectID,defaultRefItem):
	taste=get_bid_responses('taste', dataDir + '/' + expID + '/' + subjectID+'/'+subjectID+'_RatingsResults.csv')
	health=get_bid_responses('health', dataDir + '/' + expID + '/' + subjectID+'/'+subjectID+'_RatingsResults.csv')
	taste=taste.sort_values("rating")
	health=health.sort_values("rating")
	taste.reset_index(drop=True,inplace=True)
	health.reset_index(drop=True,inplace=True)
	sorted_taste=taste['stimulus'].values
	sorted_health=health['stimulus'].values
	nStim=len(taste)
	binSize = int(math.ceil(nStim / 5.0)) # round up
	midBin = int(nStim/binSize)/2 # round down

	midBinIndices = [binSize*midBin,binSize*(midBin+1)]
	neutral_taste=sorted_taste[midBinIndices[0]:midBinIndices[1]]
	neutral_health=sorted_health[midBinIndices[0]:midBinIndices[1]]

	refItemInfo = []

	referenceItem = None
	if referenceItem == None:
		# neutral TASTE and HEALTH
		print 'neutral on TASTE and HEALTH'
		referenceItem=get_common_item(neutral_taste,neutral_health)
		refItemInfo.append({'bin_1_type':'TASTE','bin_2_type':'HEALTH','bin_1_items':neutral_taste,'bin_2_items':neutral_health,'reference_item':referenceItem})

	if referenceItem == None:
		# find neutral on HEALTH and +1 on TASTE
		bin_plus1Indices=midBinIndices = [binSize*(midBin+1),binSize*(midBin+2)]
		taste_plus1=sorted_taste[midBinIndices[0]:midBinIndices[1]]
		print 'neutral on HEALTH and +1 on TASTE'
		referenceItem=get_common_item(taste_plus1,neutral_health)
		refItemInfo.append({'bin_1_type':'+1 TASTE','bin_2_type':'HEALTH','bin_1_items':taste_plus1,'bin_2_items':neutral_health,'reference_item':referenceItem})

	if referenceItem == None:
		# neutral HEALTH & -1 on TASTE
		bin_minus1Indices=midBinIndices = [binSize*(midBin-1),binSize*(midBin)]
		taste_minus1=sorted_taste[midBinIndices[0]:midBinIndices[1]]
		print 'neutral HEALTH & -1 on TASTE'
		referenceItem=get_common_item(taste_minus1,neutral_health)
		refItemInfo.append({'bin_1_type':'-1 TASTE','bin_2_type':'HEALTH','bin_1_items':taste_minus1,'bin_2_items':neutral_health,'reference_item':referenceItem})

	if referenceItem == None:
		# neutral TASTE & +1 HEALTH
		print 'neutral TASTE & +1 HEALTH'
		bin_plus1Indices=midBinIndices = [binSize*(midBin+1),binSize*(midBin+2)]
		health_plus1=sorted_health[midBinIndices[0]:midBinIndices[1]]
		referenceItem=get_common_item(neutral_taste,health_plus1)
		refItemInfo.append({'bin_1_type':'TASTE','bin_2_type':'+1 HEALTH','bin_1_items':neutral_taste,'bin_2_items':health_plus1,'reference_item':referenceItem})

	if referenceItem == None:
		# neutral TASTE, -1 on HEALTH
		print 'neutral TASTE, -1 on HEALTH'
		bin_minus1Indices=midBinIndices = [binSize*(midBin-1),binSize*(midBin)]
		health_minus1=sorted_taste[midBinIndices[0]:midBinIndices[1]]
		referenceItem=get_common_item(neutral_taste,health_minus1)
		refItemInfo.append({'bin_1_type':'TASTE','bin_2_type':'-1 HEALTH','bin_1_items':neutral_taste,'bin_2_items':health_minus1,'reference_item':referenceItem})

	if referenceItem == None:
		# otherwise return saltines
		referenceItem = defaultRefItem
		refItemInfo.append({'bin_1_type':'None','bin_2_type':'None','bin_1_items':[],'bin_2_items':[],'reference_item':referenceItem})

	if subjectID.startswith(expID):
		filePath = dataDir + expID + '/' + subjectID + '/'
		store_data.results_to_csv(expID, subjectID, filePath, 'ReferenceItem.csv', refItemInfo, {})
	return referenceItem

"""
Checks request.args has assignmentId, hitId, turkSubmitTo, workerId, live - all but live is passed by MTurk
live refers to whether HIT is live or in sandbox
"""
def contains_necessary_args(args):
	if 'workerId' in args and 'assignmentId' in args and 'hitId' in args and 'turkSubmitTo' in args and 'live' in args:
		return True
	else:
		return False

"""
Retrieve necessary args: assignmentId, hitId, turkSubmitTo, workerId, live
"""
def get_necessary_args(args):
	workerId = args.get('workerId')
	assignmentId = args.get('assignmentId')
	hitId = args.get('hitId')
	turkSubmitTo = args.get('turkSubmitTo')
	live = args.get('live') == "True"
	return [workerId, assignmentId, hitId, turkSubmitTo, live]

max_stars_repo_name: alicexue/MTurk

max_issues_repo_name: alicexue/MTurk

max_forks_repo_name: alicexue/MTurk


text:
import numpy as np
from matplotlib.axes import Axes

def align_yaxis(ax1: Axes, ax2: Axes) -> None:
    """Align zeros of the two axes, zooming them out by same ratio."""
    axes = np.array([ax1, ax2])
    extrema = np.array([ax.get_ylim() for ax in axes])
    tops = extrema[:, 1] / (extrema[:, 1] - extrema[:, 0])
    # Ensure that plots (intervals) are ordered bottom to top:
    if tops[0] > tops[1]:
        axes, extrema, tops = [a[::-1] for a in (axes, extrema, tops)]

    # How much would the plot overflow if we kept current zoom levels?
    tot_span = tops[1] + 1 - tops[0]

    extrema[0, 1] = extrema[0, 0] + tot_span * (extrema[0, 1] - extrema[0, 0])
    extrema[1, 0] = extrema[1, 1] + tot_span * (extrema[1, 0] - extrema[1, 1])
    [axes[i].set_ylim(*extrema[i]) for i in range(2)]

max_stars_repo_name: mscheltienne/neurotin-analysis

max_issues_repo_name: mscheltienne/neurotin-analysis

max_forks_repo_name: mscheltienne/neurotin-analysis


text:
from urllib.parse import urlparse

import allrank.models.losses as losses
import numpy as np
import os
import torch
from allrank.config import Config
from allrank.data.dataset_loading import load_libsvm_dataset, create_data_loaders
from allrank.models.model import make_model
from allrank.models.model_utils import get_torch_device, CustomDataParallel
from allrank.training.train_utils import fit
from allrank.utils.command_executor import execute_command
from allrank.utils.experiments import dump_experiment_result, assert_expected_metrics
from allrank.utils.file_utils import create_output_dirs, PathsContainer, copy_local_to_gs
from allrank.utils.ltr_logging import init_logger
from allrank.utils.python_utils import dummy_context_mgr
from argparse import ArgumentParser, Namespace
from attr import asdict
from functools import partial
from pprint import pformat
from torch import optim

def parse_args() -> Namespace:
    parser = ArgumentParser("allRank")
    parser.add_argument("--job-dir", help="Base output path for all experiments", required=True)
    parser.add_argument("--run-id", help="Name of this run to be recorded (must be unique within output dir)",
                        required=True)
    parser.add_argument("--config-file-name", required=True, type=str, help="Name of json file with config")

    return parser.parse_args()

def parse_args_test() -> Namespace:
    parser = ArgumentParser("allRank")
    parser.add_argument("model_path")
    parser.add_argument("config_path")
    parser.add_argument("test_file_dir")

    return parser.parse_args()

def run():
    # reproducibility
    torch.manual_seed(42)
    torch.cuda.manual_seed_all(42)
    np.random.seed(42)

    args = parse_args()

    paths = PathsContainer.from_args(args.job_dir, args.run_id, args.config_file_name)

    create_output_dirs(paths.output_dir)

    logger = init_logger(paths.output_dir)
    logger.info(f"created paths container {paths}")

    # read config
    config = Config.from_json(paths.config_path)
    logger.info("Config:\n {}".format(pformat(vars(config), width=1)))

    output_config_path = os.path.join(paths.output_dir, "used_config.json")
    execute_command("cp {} {}".format(paths.config_path, output_config_path))

    # train_ds, val_ds
    train_ds, val_ds = load_libsvm_dataset(
        input_path=config.data.path,
        slate_length=config.data.slate_length,
        validation_ds_role=config.data.validation_ds_role,
    )

    n_features = train_ds.shape[-1]
    assert n_features == val_ds.shape[-1], "Last dimensions of train_ds and val_ds do not match!"

    # train_dl, val_dl
    train_dl, val_dl = create_data_loaders(
        train_ds, val_ds, num_workers=config.data.num_workers, batch_size=config.data.batch_size)

    # gpu support
    dev = get_torch_device()
    logger.info("Model training will execute on {}".format(dev.type))

    # instantiate model
    model = make_model(n_features=n_features, **asdict(config.model, recurse=False))
    if torch.cuda.device_count() > 1:
        model = CustomDataParallel(model)
        logger.info("Model training will be distributed to {} GPUs.".format(torch.cuda.device_count()))
    model.to(dev)

    # load optimizer, loss and LR scheduler
    optimizer = getattr(optim, config.optimizer.name)(params=model.parameters(), **config.optimizer.args)
    loss_func = partial(getattr(losses, config.loss.name), **config.loss.args)
    if config.lr_scheduler.name:
        scheduler = getattr(optim.lr_scheduler, config.lr_scheduler.name)(optimizer, **config.lr_scheduler.args)
    else:
        scheduler = None

    with torch.autograd.detect_anomaly() if config.detect_anomaly else dummy_context_mgr():  # type: ignore
        # run training
        result = fit(
            model=model,
            loss_func=loss_func,
            optimizer=optimizer,
            scheduler=scheduler,
            train_dl=train_dl,
            valid_dl=val_dl,
            config=config,
            device=dev,
            output_dir=paths.output_dir,
            tensorboard_output_path=paths.tensorboard_output_path,
            **asdict(config.training)
        )

    dump_experiment_result(args, config, paths.output_dir, result)

    if urlparse(args.job_dir).scheme == "gs":
        copy_local_to_gs(paths.local_base_output_path, args.job_dir)

    assert_expected_metrics(result, config.expected_metrics)

def predict():
    from allrank.data.dataset_loading import load_libsvm_dataset_role
    from allrank.models.model_utils import get_torch_device, CustomDataParallel, load_state_dict_from_file
    from allrank.inference.inference_utils import rank_slates

    args = parse_args_test()
    model_path, config_path, test_file_dir = args.model_path, args.config_path, args.test_file_dir

    # read config
    config = Config.from_json(config_path)

    # train_ds, val_ds
    test_ds = load_libsvm_dataset_role("test", test_file_dir, config.data.slate_length, n_features=162)

    # gpu support
    dev = get_torch_device()

    # instantiate model
    model = make_model(n_features=test_ds.n_features, **asdict(config.model, recurse=False))
    if torch.cuda.device_count() > 1:
        model = CustomDataParallel(model)
    model.load_state_dict(load_state_dict_from_file(model_path, dev))

    model.to(dev)

    ranked_slates = rank_slates({"test": test_ds}, model, config)

    x, y = ranked_slates["test"]
    print(x.shape, y.shape)

if __name__ == "__main__":
    #run()
    run()

max_stars_repo_name: almajo/allRank

max_issues_repo_name: almajo/allRank

max_forks_repo_name: almajo/allRank


text:
from __future__ import print_function
import argparse
import os
import sys
import random
import torch.optim as optim
import torch.utils.data
from pointnet.dataset import HO3DDataset, JointSet
from pointnet.model import *
import torch.nn.functional as F
import numpy as np
import open3d as o3d
import pickle
import transforms3d as tf3d
import copy
import cv2
import error_def

OBJECT_TRANSLATION_THRESHOLD = 0.1
HAND_VALID_THRESHOLD = 76.
NUM_POINTS = 21
THRESHOLD = 0.5
MAX_FRAMES = 100
wrist_indices = [0]
thumb_indices = [13, 14, 15, 16]
index_indices = [1, 2, 3, 17]
middle_indices = [4, 5, 6, 18]
ring_indices = [10, 11, 12, 19]
pinky_indices = [7, 8, 9, 20]
all_indices = [wrist_indices, thumb_indices, index_indices, middle_indices, ring_indices, pinky_indices]
finger_colors = [[0.0, 0.0, 0.0], [1.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, 1.0, 0.0], [1.0, 0.0, 0.0]]
finger_colors_box = [[0.2, 0.2, 0.2], [0.8, 0.2, 0.8], [0.2, 0.2, 0.8], [0.2, 0.8, 0.2], [0.8, 0.8, 0.2], [0.8, 0.2, 0.2]]
joint_sizes = [0.008, 0.006, 0.004, 0.004]

class GraspLearner:
    def __init__(self, args):
        # Get the arguments
        self.dataset_directory = args.dataset
        self.target_name = args.target
        self.pix2pose = args.pix2pose
        self.hand_offset = args.hand_offset
        if self.pix2pose:
            self.hand_directory = os.path.join(self.dataset_directory, 'hand_tracker')
            self.object_directory = os.path.join(self.dataset_directory, 'object_pose')
            self.rgb_directory = os.path.join(self.dataset_directory, 'rgb')
            self.depth_directory = os.path.join(self.dataset_directory, 'depth')
        else:
            self.hand_directory = os.path.join(self.dataset_directory, self.target_name, 'hand_tracker')
            self.object_directory = os.path.join(self.dataset_directory, self.target_name, 'meta')
            self.rgb_directory = os.path.join(self.dataset_directory, self.target_name, 'rgb')
            self.depth_directory = os.path.join(self.dataset_directory, self.target_name, 'depth')
        self.ground_truth_prefix = args.ground_truth_prefix
        self.start_frame = args.start_frame
        self.save = args.save
        self.combine_grasp_files = args.combine_grasp_files

        if self.combine_grasp_files:
            self.combine_grasps()
            return

        # Load the regression model
        f_args = parse_model_filename(os.path.basename(args.pointnet_model))
        k_out = 9
        dropout_p = f_args['dropout_p']
        average_pool = f_args['average_pool']
        arch = f_args['arch']
        #joint_set = f_args['joint_set']

        manualSeed = random.randint(1, 10000)  # fix seed
        print("Random Seed: ", manualSeed)
        random.seed(manualSeed)
        torch.manual_seed(manualSeed)

        # Set up the model
        self.regressor = load_regression_model(arch, k_out, dropout_p, average_pool, model=args.pointnet_model)
        if self.regressor is None:
            print('Unknown architecture specified')
            sys.exit(0)

        self.regressor.cuda()
        self.regressor = self.regressor.eval()

        # Setup the hand pose estimator
        self.hand_estimator = HandPoseEstimator(args)

        # Load the point cloud of the object model
        self.obj_cloud_base = None
        if opt.object_model != '':
            self.obj_cloud_base = np.loadtxt(os.path.join(args.object_model, 'points.xyz'))

        # Load the point cloud of the robot gripper
        # self.gripper_cloud_base = np.loadtxt('hand_open_symmetric.xyz')
        self.gripper_cloud_base = np.loadtxt('hand_open.xyz')

        # Get the demonstrated grasp
        self.learn_grasp()

    def learn_grasp(self):
        # For each file
        pose_history = []
        frame_id = self.start_frame
        while True:
            # Load the estimated hand and object poses
            frame_name = str(frame_id).zfill(4)
            hand_pose_filename = os.path.join(self.hand_directory, frame_name + '.pkl')
            object_pose_filename = os.path.join(self.object_directory, frame_name + '.pkl')

            if len(pose_history) > 0 and not os.path.exists(object_pose_filename):
                print('No pose available, object is occluded and grasp can be computed from previous frame')
                break

            # If both the hand and object are valid
            if os.path.exists(hand_pose_filename) and os.path.exists(object_pose_filename):
                print('-- {}'.format(hand_pose_filename))
                # Get the hand points, score and object pose
                hand_pts, hand_score, object_pose = self.load_hand_object_poses(hand_pose_filename,
                                                                                object_pose_filename)
                # If the hand is valid
                if not np.isnan(hand_pts).all():
                    # If no pose has been recorded yet
                    if not pose_history:
                        print('Start frame: {}'.format(frame_name))
                        pose_history.append((hand_pts, hand_score, object_pose, frame_id))
                    # Otherwise, get the difference in pose of the object in this frame compared to the first frame
                    else:
                        translation_delta = np.linalg.norm(pose_history[0][2][:3, 3] - object_pose[:3, 3])
                        print('{}: {}'.format(frame_id, translation_delta))
                        if translation_delta > OBJECT_TRANSLATION_THRESHOLD:
                            print('Detected object movement {} at frame {}'.format(translation_delta, frame_id))
                            break
                        else:
                            pose_history.append((hand_pts, hand_score, object_pose, frame_id))
                    #if self.pix2pose:
                    #    self.visualize_scene(frame_id, object_pose, hand_pts)
            else:
                if not os.path.exists(hand_pose_filename):
                    print('{} does not exit'.format(hand_pose_filename))
                if not os.path.exists(object_pose_filename):
                    print('{} does not exit'.format(object_pose_filename))

            frame_id = frame_id + 1
            if frame_id >= self.start_frame + MAX_FRAMES:
                print('Object did not move within {} frames'.format(MAX_FRAMES))
                if len(pose_history) < 2:
                    print('No pose estimates available in the window')
                else:
                    print('Maximum distance is {:2f} cm'.format(translation_delta * 100))
                return

        print('End frame: {}'.format(str(frame_id).zfill(4)))

        # Find the first valid hand pose
        frame_end_idx = len(pose_history) - 1
        frame_valid_idx = frame_end_idx
        for i, e in reversed(list(enumerate(pose_history))):
            print(e[1])
            if e[1] < HAND_VALID_THRESHOLD:
                frame_valid_idx = i
                break
        print('Valid hand frame: {}'.format(str(pose_history[frame_valid_idx][-1]).zfill(4)))

        # Estimate the grasp at the end and valid frames
        frame_end_idx_tf = self.predict_robot_grasp(pose_history[frame_end_idx][0])
        frame_valid_idx_tf = self.predict_robot_grasp(pose_history[frame_valid_idx][0])

        # Estimate the final grasp using the approach vector
        estimated_tf, hand_joints = self.estimate_final_grasp_pose(
            frame_valid_idx_tf, pose_history[frame_valid_idx][0], pose_history[frame_valid_idx][-1],
            pose_history[frame_end_idx][-1])
        #if self.pix2pose:
        #    hand_joints = (pose_history[frame_valid_idx][0], pose_history[frame_valid_idx][0])

        # Transform to the object coordinate system
        object_frame_tf = np.matmul(np.linalg.inv(pose_history[frame_end_idx][2]), estimated_tf)

        # Get the ground truth grasp pose
        if self.ground_truth_prefix != '':
            gt_filename = os.path.join(self.dataset_directory, self.target_name,
                self.ground_truth_prefix + str(pose_history[frame_end_idx][-1]).zfill(4) + '.pkl')
            ground_truth_tf = load_pickle_data(gt_filename).reshape(4, 4)
            # Calculate the error
            self.compute_error(ground_truth_tf, estimated_tf)
        else:
            ground_truth_tf = None

        # Save
        if self.save:
            self.save_to_file(object_frame_tf)

        # Visualize the results
        self.visualize_learned_grasp(
            (frame_valid_idx_tf, frame_end_idx_tf, estimated_tf, object_frame_tf, ground_truth_tf),
            pose_history[frame_valid_idx][2], hand_joints)

    def load_hand_object_poses(self, hand_pose_filename, object_pose_filename):
        # Get the joints of the hand
        hand_pkl = load_pickle_data(hand_pose_filename)
        hand_pts = hand_pkl['handJoints3D'].astype(np.float32)
        hand_pts[:, 2] = hand_pts[:, 2] + self.hand_offset
        hand_score = hand_pkl['score']

        # Get the object transformation matrix
        object_pkl = load_pickle_data(object_pose_filename)
        obj_tf_mat = np.eye(4)
        rot = object_pkl['objRot']
        trans = object_pkl['objTrans']
        if rot.shape == (3, 3):
            obj_tf_mat[:3, :3] = rot
        else:
            obj_tf_mat[:3, :3] = cv2.Rodrigues(rot)[0].T
        obj_tf_mat[:3, 3] = trans

        if self.obj_cloud_base is None:
            if self.pix2pose:
                self.obj_cloud_base = np.asarray(o3d.read_point_cloud(
                    os.path.join('/home/tpatten/Data/ycbv/models', object_pkl['objName'] + '.ply')).points) * 0.001
            else:
                self.obj_cloud_base = np.loadtxt(
                    os.path.join('/home/tpatten/Data/Hands/HO3D/models', object_pkl['objName'], 'points.xyz'))

        return hand_pts, hand_score, obj_tf_mat

    def predict_robot_grasp(self, hand_pts):
        # Prepare the input for the regression model
        point_set = np.copy(hand_pts)
        # Center
        offset = np.expand_dims(np.mean(point_set, axis=0), 0)
        point_set -= offset
        # Scale
        dist = np.max(np.sqrt(np.sum(point_set ** 2, axis=1)), 0)
        point_set /= dist

        # Create tensor
        points_batch = np.zeros((1, point_set.shape[0], point_set.shape[1]), dtype=np.float32)
        points_batch[0, :, :] = point_set
        points_batch = torch.from_numpy(points_batch)

        # Predict
        points_batch = points_batch.transpose(2, 1)
        points_batch = points_batch.cuda()
        pred = self.regressor(points_batch)
        pred_np = pred.data.cpu().numpy()[0]
        predicted_gripper_transform = error_def.to_transform(pred_np, offset, dist)

        return predicted_gripper_transform

    def estimate_final_grasp_pose(self, grasp_pose, hand_pts, frame_valid, frame_end):
        rgb_val, depth_val = get_images(os.path.join(self.rgb_directory, str(frame_valid).zfill(4) + '.png'),
                                        os.path.join(self.depth_directory, str(frame_valid).zfill(4) + '.png'),
                                        self.pix2pose)
        pts_val = self.hand_estimator.process(rgb_val, depth_val)

        rgb_end, depth_end = get_images(os.path.join(self.rgb_directory, str(frame_end).zfill(4) + '.png'),
                                        os.path.join(self.depth_directory, str(frame_end).zfill(4) + '.png'),
                                        self.pix2pose)
        pts_end = self.hand_estimator.process(rgb_end, depth_end)

        approach_vec = self.estimate_approach_vector(pts_val, pts_end)

        final_grasp_pose = np.copy(grasp_pose)
        final_grasp_pose[:3, 3] += approach_vec

        final_hand_pts = np.copy(hand_pts)
        final_hand_pts += approach_vec

        return final_grasp_pose, (hand_pts, final_hand_pts)

    @staticmethod
    def estimate_approach_vector(pts_val, pts_end):
        approach_vector = np.zeros((3, ))
        #return approach_vector
        pair_count = 0
        for i in range(pts_val.shape[0]):
            if not np.isnan(pts_val[i][0]) and not np.isnan(pts_end[i][0]):
                approach_vector += pts_end[i][0] - pts_val[i][0]
                pair_count += 1

        if pair_count == 0:
            print('No available pairs between frames')
            return approach_vector

        print('Approach vector\n', approach_vector / float(pair_count))

        return approach_vector / float(pair_count)

    def compute_error(self, gt_tf, est_tf):
        add_error = error_def.add_error(gt_tf, est_tf, self.gripper_cloud_base)
        adds_error = error_def.add_symmetric_error(gt_tf, est_tf, self.gripper_cloud_base)
        t_error = error_def.translation_error(gt_tf, est_tf)
        r_error = error_def.rotation_error(gt_tf, est_tf)

        print('ERROR')
        print('ADD {:.4f}'.format(add_error))
        print('ADD Symmetric {:.4f}'.format(adds_error))
        print('Translation {:.4f} cm'.format(t_error * 100))
        print('Rotation {:.2f} deg'.format(np.degrees(r_error[0])))
        print('Rx {:.2f} Ry {:.2f} Rz {:.2f}'.format(
            np.degrees(r_error[1]), np.degrees(r_error[2]), np.degrees(r_error[3])))

        return add_error, adds_error, t_error, r_error

    def visualize_learned_grasp(self, grasp_pose, object_transform, hand_joints=None):
        # Get the three grasp poses
        frame_valid_idx_tf, frame_end_idx_tf, estimated_tf, object_frame_tf, ground_truth_tf = grasp_pose

        # Get the object point cloud
        obj_pcd = o3d.geometry.PointCloud()
        obj_pcd.points = o3d.utility.Vector3dVector(self.obj_cloud_base)
        obj_pcd.transform(object_transform)

        # End frame grasp
        gripper_pcd_end = o3d.geometry.PointCloud()
        gripper_pcd_end.points = o3d.utility.Vector3dVector(self.gripper_cloud_base)
        gripper_pcd_end.transform(frame_end_idx_tf)

        # Latest valid grasp
        gripper_pcd_valid = o3d.geometry.PointCloud()
        gripper_pcd_valid.points = o3d.utility.Vector3dVector(self.gripper_cloud_base)
        gripper_pcd_valid.transform(frame_valid_idx_tf)

        # Final estimated grasp
        gripper_pcd_estimated = o3d.geometry.PointCloud()
        gripper_pcd_estimated.points = o3d.utility.Vector3dVector(self.gripper_cloud_base)
        gripper_pcd_estimated.transform(estimated_tf)

        # Ground truth grasp
        if ground_truth_tf is not None:
            gripper_pcd_gt = o3d.geometry.PointCloud()
            gripper_pcd_gt.points = o3d.utility.Vector3dVector(self.gripper_cloud_base)
            gripper_pcd_gt.transform(ground_truth_tf)

        # Visualize
        vis = o3d.visualization.Visualizer()
        vis.create_window()

        # Coordinate frame
        vis.add_geometry(o3d.create_mesh_coordinate_frame(size=0.05))

        # Object cloud
        obj_pcd.paint_uniform_color([0.5, 0.5, 0.5])
        vis.add_geometry(obj_pcd)

        # Final frame grasp pose
        #gripper_pcd_end.paint_uniform_color([0., 0., 1.])
        #vis.add_geometry(gripper_pcd_end)

        # Latest valid frame grasp pose
        #gripper_pcd_valid.paint_uniform_color([0., 1., 1.])
        #vis.add_geometry(gripper_pcd_valid)

        # Final estimated grasp pose
        gripper_pcd_estimated.paint_uniform_color([1., 0., 0.])
        vis.add_geometry(gripper_pcd_estimated)

        # Ground truth grasp pose
        if ground_truth_tf is not None:
            gripper_pcd_gt.paint_uniform_color([0., 1., 0.])
            vis.add_geometry(gripper_pcd_gt)

        # Hand joints
        if hand_joints is not None:
            pts_val, pts_end = hand_joints
            for i in range(len(all_indices)):
                for j in range(len(all_indices[i])):
                    if not np.isnan(pts_val[all_indices[i][j]][0]):
                        mm = o3d.create_mesh_sphere(radius=joint_sizes[j])
                        mm.compute_vertex_normals()
                        mm.paint_uniform_color(finger_colors[i])
                        trans3d = pts_val[all_indices[i][j]]
                        tt = np.eye(4)
                        tt[0:3, 3] = trans3d
                        mm.transform(tt)
                        vis.add_geometry(mm)
                    if not np.isnan(pts_end[all_indices[i][j]][0]):
                        mm = o3d.create_mesh_box(width=joint_sizes[j], height=joint_sizes[j], depth=joint_sizes[j])
                        mm.compute_vertex_normals()
                        mm.paint_uniform_color(finger_colors[i])
                        trans3d = pts_end[all_indices[i][j]]
                        tt = np.eye(4)
                        tt[0:3, 3] = trans3d
                        mm.transform(tt)
                        vis.add_geometry(mm)

        '''
        # Visualize the grasp in the object's frame
        obj_pcd_origin = o3d.geometry.PointCloud()
        obj_pcd_origin.points = o3d.utility.Vector3dVector(self.obj_cloud_base)
        obj_pcd_origin.paint_uniform_color([0.5, 0.5, 0.5])
        vis.add_geometry(obj_pcd_origin)

        object_frame_grasp_pcd = o3d.geometry.PointCloud()
        object_frame_grasp_pcd.points = o3d.utility.Vector3dVector(self.gripper_cloud_base)
        object_frame_grasp_pcd.transform(object_frame_tf)
        object_frame_grasp_pcd.paint_uniform_color([1., 0., 0.])
        vis.add_geometry(object_frame_grasp_pcd)
        '''

        '''
        # Visualize some random grasps
        for i in range(5):
            # Random object pose
            random_tf = np.eye(4)
            random_tf[:3, :3] = tf3d.euler.euler2mat(np.random.uniform(-np.pi, np.pi),
                                                     np.random.uniform(-np.pi, np.pi),
                                                     np.random.uniform(-np.pi, np.pi))
            random_tf[:3, 3] = np.random.uniform(-1, 1, (3,))

            # Object cloud
            obj_pcd_rand = o3d.geometry.PointCloud()
            obj_pcd_rand.points = o3d.utility.Vector3dVector(self.obj_cloud_base)
            obj_pcd_rand.transform(random_tf)
            obj_pcd_rand.paint_uniform_color([0.7, 0.7, 0.7])
            vis.add_geometry(obj_pcd_rand)

            # Gripper cloud
            gripper_pcd_rand = o3d.geometry.PointCloud()
            gripper_pcd_rand.points = o3d.utility.Vector3dVector(np.copy(self.gripper_cloud_base))
            grasp_tf = np.matmul(random_tf, object_frame_tf)
            gripper_pcd_rand.transform(grasp_tf)
            if i == 0:
                gripper_pcd_rand.paint_uniform_color([0.7, 0.3, 0.3])
            elif i == 1:
                gripper_pcd_rand.paint_uniform_color([0.3, 0.7, 0.3])
            elif i == 2:
                gripper_pcd_rand.paint_uniform_color([0.3, 0.3, 0.7])
            elif i == 3:
                gripper_pcd_rand.paint_uniform_color([0.7, 0.7, 0.3])
            elif i == 4:
                gripper_pcd_rand.paint_uniform_color([0.7, 0.3, 0.7])
            vis.add_geometry(gripper_pcd_rand)
        '''

        # End
        vis.run()
        vis.destroy_window()

    def visualize_scene(self, frame_id, object_transform, hand_joints=None):
        # Get the object point cloud
        obj_pcd = o3d.geometry.PointCloud()
        obj_pcd.points = o3d.utility.Vector3dVector(self.obj_cloud_base)
        obj_pcd.transform(object_transform)
        #coord_change_mat = np.array([[1., 0., 0.], [0, -1., 0.], [0., 0., -1.]], dtype=np.float32)
        #obj_pcd.points = o3d.utility.Vector3dVector(np.asarray(obj_pcd.points).dot(coord_change_mat.T))

        # Visualize
        vis = o3d.visualization.Visualizer()
        vis.create_window()

        # Coordinate frame
        vis.add_geometry(o3d.create_mesh_coordinate_frame(size=0.05))

        # Object cloud
        obj_pcd.paint_uniform_color([0.5, 0.5, 0.5])
        vis.add_geometry(obj_pcd)

        # Hand joints
        if hand_joints is not None:
            for i in range(len(all_indices)):
                for j in range(len(all_indices[i])):
                    if not np.isnan(hand_joints[all_indices[i][j]][0]):
                        mm = o3d.create_mesh_sphere(radius=joint_sizes[j])
                        mm.compute_vertex_normals()
                        mm.paint_uniform_color(finger_colors[i])
                        trans3d = hand_joints[all_indices[i][j]]
                        tt = np.eye(4)
                        tt[0:3, 3] = trans3d
                        mm.transform(tt)
                        vis.add_geometry(mm)

        ux = 315.3074696331638
        uy = 233.0483557773859
        fx = 538.391033533567
        fy = 538.085452058436
        i_fx = 1 / fx
        i_fy = 1 / fy
        _, depth_image = get_images(os.path.join(self.rgb_directory, str(frame_id).zfill(4) + '.png'),
                                    os.path.join(self.depth_directory, str(frame_id).zfill(4) + '.png'),
                                    self.pix2pose)
        points_3D = np.zeros((depth_image.shape[0] * depth_image.shape[1], 3))
        p_count = 0
        for u in range(depth_image.shape[0]):
            for v in range(depth_image.shape[1]):
                points_3D[p_count, 2] = depth_image[u, v]
                points_3D[p_count, 1] = (u - ux) * points_3D[p_count, 2] * i_fx
                points_3D[p_count, 0] = (v - uy) * points_3D[p_count, 2] * i_fy
                p_count += 1
        #coord_change_mat = np.array([[1., 0., 0.], [0, -1., 0.], [0., 0., -1.]], dtype=np.float32)
        #points_3D = points_3D.dot(coord_change_mat.T)

        scene_pcd = o3d.geometry.PointCloud()
        scene_pcd.points = o3d.utility.Vector3dVector(points_3D)
        scene_pcd.paint_uniform_color([0.5, 0.5, 0.9])
        vis.add_geometry(scene_pcd)

        # End
        vis.run()
        vis.destroy_window()

    def save_to_file(self, transform):
        save_path = copy.deepcopy(self.dataset_directory)
        if save_path[-1] == '/':
            save_path = save_path[0:-1]
        save_filename = save_path + '.npy'
        print('Saving transform\n{}\nto file {}'.format(transform, save_filename))
        np.save(save_filename, transform)

    def combine_grasps(self):
        # Get the name of the directory to search
        dir_path = copy.deepcopy(self.dataset_directory)
        if dir_path[-1] == '/':
            dir_path = save_path[0:-1]
        slash_idx = dir_path.rfind('/')
        dir_path = dir_path[0:slash_idx]

        # Get all grasp files
        filelist = []
        for f in os.listdir(dir_path):
            if f.endswith('.npy'):
                filelist.append(os.path.join(dir_path, f))
        filelist.sort()

        # Load each file and combine into one grasp file
        all_grasps = []
        for f in filelist:
            print(f)
            all_grasps.append(np.load(f))

        # Save as a new grasp file
        save_filename = dir_path + '.npy'
        print('Saving grasp transformations\n{}\nto file {}'.format(np.asarray(all_grasps), save_filename))
        np.save(save_filename, np.array(all_grasps))

class HandPoseEstimator:
    def __init__(self, args):
        self.proto_file = args.proto_file
        self.weights_file = args.weights_file
        self.pix2pose = args.pix2pose
        self.width = 640
        self.height = 480
        self.net = cv2.dnn.readNetFromCaffe(self.proto_file, self.weights_file)

    def process(self, rgb_image, depth_image):
        # Get image properties
        self.width = rgb_image.shape[1]
        self.height = rgb_image.shape[0]
        aspect_ratio = self.width / self.height

        # Input image dimensions for the network
        in_height = 368
        in_width = int(((aspect_ratio * in_height) * 8) // 8)
        net_input = cv2.dnn.blobFromImage(rgb_image, 1.0 / 255, (in_width, in_height), (0, 0, 0),
                                          swapRB=False, crop=False)

        # Get the prediction
        self.net.setInput(net_input)
        pred = self.net.forward()

        # Get the 3D coordinates
        points = self.get_keypoints(pred)
        points3D = self.get_world_coordinates(points, depth_image)
        if not self.pix2pose:
            coord_change_mat = np.array([[1., 0., 0.], [0, -1., 0.], [0., 0., -1.]], dtype=np.float32)
            points3D = points3D.dot(coord_change_mat.T)
        points3D = to_iccv_format(points3D)

        return points3D

    def get_keypoints(self, pred):
        # Empty list to store the detected keypoints
        points = []

        for i in range(NUM_POINTS):
            # confidence map of corresponding body's part.
            prob_map = pred[0, i, :, :]
            prob_map = cv2.resize(prob_map, (self.width, self.height))

            # Find global maxima of the probability map
            min_val, prob, min_loc, point = cv2.minMaxLoc(prob_map)

            if prob > THRESHOLD:
                # Add the point to the list if the probability is greater than the threshold
                points.append((int(point[0]), int(point[1])))
            else:
                points.append(None)

        return points

    def get_world_coordinates(self, points, depth_image):
        points_3D = np.zeros((len(points), 3))

        if self.pix2pose:
            ux = 315.3074696331638
            uy = 233.0483557773859
            fx = 538.391033533567
            fy = 538.085452058436
        else:
            ux = 312.42
            uy = 241.42
            fx = 617.343
            fy = 617.343
        i_fx = 1 / fx
        i_fy = 1 / fy

        for i in range(len(points)):
            if points[i] is not None:
                points_3D[i, 2] = depth_image[points[i][1], points[i][0]]
                points_3D[i, 0] = (points[i][0] - ux) * points_3D[i, 2] * i_fx
                points_3D[i, 1] = (points[i][1] - uy) * points_3D[i, 2] * i_fy
            else:
                points_3D[i, :] = np.nan

        return points_3D

def load_pickle_data(f_name):
    with open(f_name, 'rb') as f:
        try:
            pickle_data = pickle.load(f, encoding='latin1')
        except:
            pickle_data = pickle.load(f)
    return pickle_data

def get_images(rgb_filename, dep_filename, ros_image=False):
    rgb_image = cv2.imread(rgb_filename)
    if ros_image:
        dep_image = cv2.imread(dep_filename, cv2.CV_16UC1).astype(np.int16)
        depth_scale = 0.001
    else:
        dep_image = cv2.imread(dep_filename)
        depth_scale = 0.00012498664727900177
        dep_image = dep_image[:, :, 2] + dep_image[:, :, 1] * 256
    dep_image = dep_image * depth_scale

    return rgb_image, dep_image

def to_iccv_format(joints):
    # MONOHAND [Wrist (0),
    #           TMCP (1), TPIP (2), TDIP (3), TTIP (4),
    #           IMCP (5), IPIP (6), IDIP (7), ITIP (8),
    #           MMCP (9), MPIP (10), MDIP (11), MTIP (12),
    #           RMCP (13), RPIP (14), RDIP (15), RTIP (16),
    #           PMCP (17), PPIP (18), PDIP (19), PTIP (20)]

    # ICCV     [Wrist,
    #           TMCP, IMCP, MMCP, RMCP, PMCP,
    #           TPIP, TDIP, TTIP,
    #           IPIP, IDIP, ITIP,
    #           MPIP, MDIP, MTIP,
    #           RPIP, RDIP, RTIP,
    #           PPIP, PDIP, PTIP]

    # joint_map = [0, 1, 5, 9, 13, 17, 2, 3, 4, 6, 7, 8, 10, 11, 12, 14, 15, 16, 18, 19, 20]

    # HO3D     [Wrist,
    #           IMCP, IPIP, IDIP,
    #           MMCP, MPIP, MDIP,
    #           PMCP, PPIP, PDIP
    #           RMCP, RPIP, RDIP,
    #           TMCP, TPIP, TDIP,
    #           TTIP, ITIP, MTIP, RTIP, PTIP]

    joint_map = [0, 5, 6, 7, 9, 10, 11, 17, 18, 19, 13, 14, 15, 1, 2, 3, 4, 8, 12, 16, 20]

    iccv_joints = np.zeros(joints.shape)
    for i in range(len(joints)):
        iccv_joints[i, :] = joints[joint_map[i], :]

    return iccv_joints

def load_offset(offsets_file, dataset_path):
    x = dataset_path.split('/')
    object_name = x[-2]
    bag_name = x[-1]
    print('Object: {}  Bag: {}'.format(object_name, bag_name))

    offset = 0.045
    f = open(offsets_file, "r")
    for line in f:
        split_line = line.split()
        x = split_line[0].split('/')
        if x[-2] == object_name and x[-1] == bag_name:
            offset = float(split_line[1])
            print('Found offset of {}'.format(offset))
            break
    f.close()

    return offset

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    #parser.add_argument('--pointnet_model', type=str, required=True, help='path to the pointnet model to load')
    #parser.add_argument('--hand', type=str, required=True, help="path to the hand pose estimates")
    #parser.add_argument('--object', type=str, required=True, help="path to the object pose estimates")
    #parser.add_argument('--object_model', type=str, required=True,
    #                    help="path to the directory that stores the object model")

    opt = parser.parse_args()
    opt.pointnet_model = '/home/tpatten/Data/ICAS2020/Models/ALL_batch64_nEpoch150_regLoss_dropout0-3_augmentation_splitLoss_symmetry_lr0-01_ls20_lg0-5_arch14_149.pth'
    opt.object_model = ''
    opt.proto_file = '/home/tpatten/Code/hand-tracking/caffe_models/pose_deploy.prototxt'
    opt.weights_file = '/home/tpatten/Code/hand-tracking/caffe_models/pose_iter_102000.caffemodel'
    opt.verbose = True
    '''
    opt.target = 'ABF10'
    opt.hand_offset = 0.045
    opt.start_frame = 0
    opt.dataset = '/home/tpatten/Data/Hands/HO3D/train'
    opt.ground_truth_prefix = 'meta/grasp_bl_'
    opt.alternate_start = False
    opt.pix2pose = False
    opt.save = False
    '''
    #'''
    opt.target = ''
    opt.ground_truth_prefix = ''
    opt.start_frame = 0
    opt.dataset = '/home/tpatten/Data/ICAS2020/Rosbags/035_power_drill/2020-06-30-14-21-30'
    opt.hand_offset = load_offset('/home/tpatten/Data/ICAS2020/Rosbags/offsets.txt', opt.dataset)
    opt.alternate_start = False
    opt.pix2pose = True
    opt.save = False
    opt.combine_grasp_files = True
    #'''

    if opt.target == 'BB10':
        opt.start_frame = 502

    if opt.alternate_start:
        if opt.target == 'BB10':
            opt.start_frame = 973
        elif opt.target == 'GPMF10':
            opt.start_frame = 412
        elif opt.target == 'GSF10':
            opt.start_frame = 614
        elif opt.target == 'MDF10':
            opt.start_frame = 1005

    print(opt)

    learner = GraspLearner(opt)

    # Files
    # ---
    # 004_sugar_box/2020-06-16-11-56-35
    # 004_sugar_box/2020-06-16-14-38-15
    # 004_sugar_box/2020-06-16-15-01-01
    # 004_sugar_box/2020-06-16-15-01-39
    # ---
    # 005_tomato_soup_can/2020-06-16-11-46-35
    # 005_tomato_soup_can/2020-06-16-11-47-00
    # 005_tomato_soup_can/2020-06-16-14-41-58
    # 005_tomato_soup_can/2020-06-16-14-42-26
    # ---
    # 006_mustard_bottle/2020-06-16-11-48-42
    # 006_mustard_bottle/2020-06-16-14-24-59
    # 006_mustard_bottle/2020-06-16-14-25-29
    # 006_mustard_bottle/2020-06-16-14-44-13
    # ---
    # 025_mug/2020-06-16-11-52-00
    # 025_mug/2020-06-16-11-52-25
    # 025_mug/2020-06-16-14-28-01
    # 025_mug/2020-06-16-14-46-37
    # ---
    # 035_power_drill/2020-06-16-11-54-44
    # 035_power_drill/2020-06-16-14-36-06
    # 035_power_drill/2020-06-16-14-59-07
    # ---


text:
import os
import numpy as np
import matplotlib as mpl
import seaborn as sns
import matplotlib.pyplot as plt
import dynalysis.basics as bcs
import dynalysis.classes as clss
from dynalysis.scalebar import add_scalebar

def colorFader(c1,c2,mix=0): #fade (linear interpolate) from color c1 (at mix=0) to c2 (mix=1)
    c1=np.array(mpl.colors.to_rgb(c1))
    c2=np.array(mpl.colors.to_rgb(c2))
    return mpl.colors.to_hex((1-mix)*c1 + mix*c2)

def palette(name,num):
	return sns.color_palette(name,num)

c1='#1f77b4' #blue
c4='white'
c2='white'
c3 = 'firebrick'
scale = (1,50)

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
data = bcs.readcolumn('Frate.txt')
time = bcs.to_float(data[0])
for neu in range(1,75+1): plt.plot(time,bcs.to_float(data[neu]),color=palette("BrBG",75)[neu-1],linewidth=1.5)
ax.axis('off')
scalebar_h = add_scalebar(ax, scale[0], '', 'upper right', matchx=False, matchy=False)
scalebar_v = add_scalebar(ax, scale[1], '', 'lower left', matchx=False, matchy=False, horizontal=False)
ax.add_artist(scalebar_v)
ax.add_artist(scalebar_h)
plt.savefig('exc.png',dpi=300); plt.clf() #colorFader(c4,c1,neu/75)

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
for neu in range(76,101): plt.plot(time,bcs.to_float(data[neu]),color=palette("BrBG",25)[neu-76],linewidth=1.5)
ax.axis('off')
scalebar_h = add_scalebar(ax, scale[0], '', 'upper right', matchx=False, matchy=False)
scalebar_v = add_scalebar(ax, scale[1], '', 'lower left', matchx=False, matchy=False, horizontal=False)
ax.add_artist(scalebar_v)
ax.add_artist(scalebar_h)
plt.savefig('inh.png',dpi=300); plt.clf() #colorFader(c2,c3,(neu-76)/25)


text:
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Andre Anjos <andre.anjos@idiap.ch>
# Mon 16 Apr 08:18:08 2012 CEST

from setuptools import setup, find_packages, dist
dist.Distribution(dict(setup_requires=['numpy', 'bob.extension']))
import numpy
from bob.extension import Extension, build_ext

from bob.extension.utils import load_requirements
build_requires = load_requirements()

# Define package version
version = open("version.txt").read().rstrip()

# Local include directory
import os
package_dir = os.path.dirname(os.path.realpath(__file__))
include_dir = os.path.join(package_dir, 'bob', 'blitz', 'include')

# Add numpy includes
system_include_dirs = [numpy.get_include()]

# NumPy API macros necessary?
define_macros=[
    ("PY_ARRAY_UNIQUE_SYMBOL", "BOB_BLITZ_NUMPY_C_API"),
    ("NO_IMPORT_ARRAY", "1"),
    ]
from distutils.version import LooseVersion
if LooseVersion(numpy.__version__) >= LooseVersion('1.7'):
  define_macros.append(("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION"))

# Pkg-config requirements
packages = [
    'blitz >= 0.10',
    'boost', # any version will do, only need headers
    ]

# The only thing we do in this file is to call the setup() function with all
# parameters that define our package.
setup(

    name='bob.blitz',
    version=version,
    description='Bindings for Blitz++ (a C++ array template library)',
    url='http://gitlab.idiap.ch/bob/bob.blitz',
    license='BSD',
    author='Andre Anjos',
    author_email='andre.anjos@idiap.ch',

    long_description=open('README.rst').read(),

    packages=find_packages(),
    include_package_data=True,
    zip_safe=False,

    setup_requires = build_requires,
    install_requires = build_requires,

    ext_modules = [
      Extension("bob.blitz.version",
        [
          "bob/blitz/version.cpp",
        ],
        packages=packages,
        version=version,
        define_macros=define_macros,
        include_dirs=[include_dir],
        system_include_dirs=system_include_dirs,
      ),

      Extension("bob.blitz._library",
        [
          "bob/blitz/api.cpp",
          "bob/blitz/array.cpp",
          "bob/blitz/main.cpp",
        ],
        packages=packages,
        version=version,
        define_macros=define_macros,
        include_dirs=[include_dir],
        system_include_dirs=system_include_dirs,
      ),
    ],

    cmdclass = {
      'build_ext': build_ext
    },

    classifiers = [
      'Framework :: Bob',
      'Development Status :: 4 - Beta',
      'Intended Audience :: Developers',
      'License :: OSI Approved :: BSD License',
      'Natural Language :: English',
      'Programming Language :: Python',
      'Programming Language :: Python :: 3',
      'Topic :: Software Development :: Libraries :: Python Modules',
    ],

  )