text:
import io

import numpy as np
from tensorflow.keras.applications import ResNet50, imagenet_utils
from tensorflow.keras.preprocessing.image import img_to_array

import flask
from PIL import Image

app = flask.Flask(__name__)

MODEL = ResNet50(weights="imagenet")

def prep_img(image, target):
    if image.mode != "RGB":
        image = image.convert("RGB")

    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = imagenet_utils.preprocess_input(image)
    return image

@app.route("/predict", methods=["POST"])
def predict():
    data = {"success": False}
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))
            image = prep_img(image, target=(224, 224))
            preds = MODEL.predict(image)
            results = imagenet_utils.decode_predictions(preds)
            data["predictions"] = []
            for _, label, prob in results[0]:
                r = {"label": label, "probability": float(prob)}
                data["predictions"].append(r)

            data["success"] = True

    return flask.jsonify(data)

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0")

max_stars_repo_name: Jason-Adam/flask-demo

max_issues_repo_name: Jason-Adam/flask-demo

max_forks_repo_name: Jason-Adam/flask-demo


text:
import unittest
from SimPEG import *
from SimPEG import EM
import sys
from scipy.constants import mu_0

def getFDEMProblem(fdemType, comp, SrcList, freq, verbose=False):
    cs = 5.
    ncx, ncy, ncz = 6, 6, 6
    npad = 3
    hx = [(cs,npad,-1.3), (cs,ncx), (cs,npad,1.3)]
    hy = [(cs,npad,-1.3), (cs,ncy), (cs,npad,1.3)]
    hz = [(cs,npad,-1.3), (cs,ncz), (cs,npad,1.3)]
    mesh = Mesh.TensorMesh([hx,hy,hz],['C','C','C'])

    mapping = Maps.ExpMap(mesh)

    x = np.array([np.linspace(-30,-15,3),np.linspace(15,30,3)]) #don't sample right by the source
    XYZ = Utils.ndgrid(x,x,np.r_[0.])
    Rx0 = EM.FDEM.Rx(XYZ, comp)

    Src = []

    for SrcType in SrcList:
        if SrcType is 'MagDipole':
            Src.append(EM.FDEM.Src.MagDipole([Rx0], freq=freq, loc=np.r_[0.,0.,0.]))
        elif SrcType is 'MagDipole_Bfield':
            Src.append(EM.FDEM.Src.MagDipole_Bfield([Rx0], freq=freq, loc=np.r_[0.,0.,0.]))
        elif SrcType is 'CircularLoop':
            Src.append(EM.FDEM.Src.CircularLoop([Rx0], freq=freq, loc=np.r_[0.,0.,0.]))
        elif SrcType is 'RawVec':
            if fdemType is 'e' or fdemType is 'b':
                S_m = np.zeros(mesh.nF)
                S_e = np.zeros(mesh.nE)
                S_m[Utils.closestPoints(mesh,[0.,0.,0.],'Fz') + np.sum(mesh.vnF[:1])] = 1.
                S_e[Utils.closestPoints(mesh,[0.,0.,0.],'Ez') + np.sum(mesh.vnE[:1])] = 1.
                Src.append(EM.FDEM.Src.RawVec([Rx0], freq, S_m, S_e))

            elif fdemType is 'h' or fdemType is 'j':
                S_m = np.zeros(mesh.nE)
                S_e = np.zeros(mesh.nF)
                S_m[Utils.closestPoints(mesh,[0.,0.,0.],'Ez') + np.sum(mesh.vnE[:1])] = 1.
                S_e[Utils.closestPoints(mesh,[0.,0.,0.],'Fz') + np.sum(mesh.vnF[:1])] = 1.
                Src.append(EM.FDEM.Src.RawVec([Rx0], freq, S_m, S_e))

    if verbose:
        print '  Fetching %s problem' % (fdemType)

    if fdemType == 'e':
        survey = EM.FDEM.Survey(Src)
        prb = EM.FDEM.Problem_e(mesh, mapping=mapping)

    elif fdemType == 'b':
        survey = EM.FDEM.Survey(Src)
        prb = EM.FDEM.Problem_b(mesh, mapping=mapping)

    elif fdemType == 'j':
        survey = EM.FDEM.Survey(Src)
        prb = EM.FDEM.Problem_j(mesh, mapping=mapping)

    elif fdemType == 'h':
        survey = EM.FDEM.Survey(Src)
        prb = EM.FDEM.Problem_h(mesh, mapping=mapping)

    else:
        raise NotImplementedError()
    prb.pair(survey)

    try:
        from pymatsolver import MumpsSolver
        prb.Solver = MumpsSolver
    except ImportError, e:
        pass

    return prb

ext: py

max_stars_repo_name: KyuboNoh/HY

max_issues_repo_name: KyuboNoh/HY

max_forks_repo_name: KyuboNoh/HY

reason: from scipy


text:
'''
Runs unit tests of spectral file I/O functions.

The unit tests in this module assume the example file "92AV3C.lan" is in the
spectral data path.  After the file is opened it is saved in various formats
(different combinations of byte order, interleave, and data type) and for each
file written, the new file is opened and known data values are read and checked
to verify they are read properly.

To run the unit tests, type the following from the system command line:

    # python -m spectral.tests.spyfile
'''

from __future__ import division, print_function, unicode_literals

import itertools
import numpy as np
import os

import spectral as spy
from spectral.io.spyfile import find_file_path, FileNotFoundError, SpyFile
from spectral.tests import testdir
from spectral.tests.spytest import SpyTest

def assert_almost_equal(a, b, **kwargs):
    if not np.allclose(a, b, **kwargs):
        raise Exception('NOPE')

class SpyFileTest(SpyTest):
    '''Tests that SpyFile methods read data correctly from files.'''
    def __init__(self, file, datum, value):
        '''
        Arguments:

            `file` (str or `SpyFile`):

                The SpyFile to be tested.  This can be either the name of the
                file or a SpyFile object that has already been opened.

            `datum` (3-tuple of ints):

                (i, j, k) are the row, column and band of the datum to be
                tested. 'i' and 'j' should be at least 10 pixels away from the
                edge of the associated image and `k` should have at least 10
                bands above and below it in the image.

            `value` (int or float):

                The scalar value associated with location (i, j, k) in
                the image.
        '''
        self.file = file
        self.datum = datum
        self.value = value

    def setup(self):
        if isinstance(self.file, SpyFile):
            self.image = self.file
        else:
            self.image = spy.open_image(self.file)

    def test_read_datum(self):
        assert_almost_equal(self.image.read_datum(*self.datum, use_memmap=True),
                            self.value)
        assert_almost_equal(self.image.read_datum(*self.datum, use_memmap=False),
                            self.value)

    def test_read_pixel(self):
        (i, j, k) = self.datum
        assert_almost_equal(self.image.read_pixel(i, j, use_memmap=True)[k],
                            self.value)
        assert_almost_equal(self.image.read_pixel(i, j, use_memmap=False)[k],
                            self.value)

    def test_read_band(self):
        (i, j, k) = self.datum
        assert_almost_equal(self.image.read_band(k, use_memmap=True)[i, j],
                            self.value)
        assert_almost_equal(self.image.read_band(k, use_memmap=False)[i, j],
                            self.value)

    def test_read_bands(self):
        (i, j, k) = self.datum
        bands = (k - 5, k - 2, k, k + 1)
        assert_almost_equal(self.image.read_bands(bands,
                                                  use_memmap=True)[i, j, 2],
                            self.value)
        assert_almost_equal(self.image.read_bands(bands,
                                                  use_memmap=False)[i, j, 2],
                            self.value)

    def test_read_bands_nonascending(self):
        (i, j, k) = self.datum
        bands = (k - 2, k + 1, k, k - 5)
        assert_almost_equal(self.image.read_bands(bands,
                                                  use_memmap=True)[i, j, 2],
                            self.value)
        assert_almost_equal(self.image.read_bands(bands,
                                                  use_memmap=False)[i, j, 2],
                            self.value)

    def test_read_bands_duplicates(self):
        (i, j, k) = self.datum
        bands = (k - 5, k - 5, k, k -5)
        assert_almost_equal(self.image.read_bands(bands,
                                                  use_memmap=True)[i, j, 2],
                            self.value)
        assert_almost_equal(self.image.read_bands(bands,
                                                  use_memmap=False)[i, j, 2],
                            self.value)

    def test_read_subregion(self):
        (i, j, k) = self.datum
        region = self.image.read_subregion((i - 5, i + 9),
                                           (j - 3, j + 4), use_memmap=True)
        assert_almost_equal(region[5, 3, k], self.value)
        region = self.image.read_subregion((i - 5, i + 9),
                                           (j - 3, j + 4), use_memmap=False)
        assert_almost_equal(region[5, 3, k], self.value)

    def test_read_subimage(self):
        (i, j, k) = self.datum
        subimage = self.image.read_subimage([0, 3, i, 5],
                                            [1, j, 4, 7],
                                            [3, 7, k], use_memmap=True)
        assert_almost_equal(subimage[2, 1, 2], self.value)
        subimage = self.image.read_subimage([0, 3, i, 5],
                                            [1, j, 4, 7],
                                            [3, 7, k], use_memmap=False)
        assert_almost_equal(subimage[2, 1, 2], self.value)

        subimage = self.image.read_subimage([0, 3, i, 5],
                                            [1, j, 4, 7], use_memmap=True)
        assert_almost_equal(subimage[2, 1, k], self.value)
        subimage = self.image.read_subimage([0, 3, i, 5],
                                            [1, j, 4, 7], use_memmap=False)
        assert_almost_equal(subimage[2, 1, k], self.value)

    def test_load(self):
        (i, j, k) = self.datum
        data = self.image.load()
        spyf = self.image

        load_assert = np.allclose
        load_assert(data[i, j, k], self.value)
        first_band = spyf[:, :, 0]
        load_assert(data[:, :, 0], first_band)
        # This is checking if different ImageArray and SpyFile indexing
        # results are the same shape, so we can't just reuse the already
        # loaded first band.
        load_assert(data[:, 0, 0], spyf[:, 0, 0])
        load_assert(data[0, 0, 0], spyf[0, 0, 0])
        load_assert(data[0, 0], spyf[0, 0])
        load_assert(data[-1, -1, -1], spyf[-1, -1, -1])
        load_assert(data[-1, -3:-1], spyf[-1, -3:-1])
        load_assert(data[(6, 25)], spyf[(6, 25)])

        # The following test would currently fail, because
        # SpyFile.__get_item__ treats [6,25] the same as (6,25).

        # load_assert(data[[6, 25]],
        #             spyf[[6, 25]])

        load_assert(data.read_band(0), spyf.read_band(0))
        load_assert(data.read_bands([0, 1]), spyf.read_bands([0, 1]))
        load_assert(data.read_pixel(1, 2), spyf.read_pixel(1, 2))
        load_assert(data.read_subregion([0, 3], [1, 2]),
                    spyf.read_subregion([0, 3], [1, 2]))
        load_assert(data.read_subregion([0, 3], [1, 2], [0, 1]),
                    spyf.read_subregion([0, 3], [1, 2], [0, 1]))
        load_assert(data.read_subimage([0, 2, 4], [6, 3]),
                    spyf.read_subimage([0, 2, 4], [6, 3]))
        load_assert(data.read_subimage([0, 2], [6, 3], [0, 1]),
                    spyf.read_subimage([0, 2], [6, 3], [0, 1]))
        load_assert(data.read_datum(1,2,8), spyf.read_datum(1,2,8))

        ufunc_result = data + 1
        assert isinstance(ufunc_result, np.ndarray)
        assert not isinstance(ufunc_result, type(data))
        non_ufunc_result = data.diagonal()
        assert isinstance(non_ufunc_result, np.ndarray)
        assert not isinstance(non_ufunc_result, type(data))

    def test_getitem_i_j_k(self):
        (i, j, k) = self.datum
        assert_almost_equal(self.image[i, j, k], self.value)

    def test_getitem_i_j(self):
        (i, j, k) = self.datum
        assert_almost_equal(self.image[i, j][k], self.value)

    def test_getitem_i_j_kslice(self):
        (i, j, k) = self.datum
        assert_almost_equal(self.image[i, j, k-2:k+3:2][0, 0, 1], self.value)

    def test_getitem_islice_jslice(self):
        (i, j, k) = self.datum
        assert_almost_equal(self.image[i-3:i+3, j-3:j+3][3, 3, k], self.value)

def assert_same_shape_almost_equal(obj1, obj2, decimal=7, err_msg='',
                                   verbose=True):
    """
    Assert that two objects are almost equal and have the same shape.

    numpy.testing.assert_almost_equal does test for shape, but considers
    arrays with one element and a scalar to be the same.
    """
    # Types might be different since ImageArray stores things as
    # floats by default.
    if np.isscalar(obj1):
        assert np.isscalar(obj2), err_msg
    else:
        assert obj1.shape == obj2.shape, err_msg

    assert_almost_equal(obj1, obj2, decimal=decimal, err_msg=err_msg,
                        verbose=verbose)

class SpyFileTestSuite(object):
    '''Tests reading by byte orders, data types, and interleaves. For a
    specified image file name, the test suite will verify proper reading of
    data for various combinations of data type, interleave (BIL, BIP, BSQ),
    and  byte order (little- and big-endian). A new file is created
    for each combination of parameters for testing.
    '''
    def __init__(self, filename, datum, value, **kwargs):
        '''
        Arguments:

            `filename` (str):

                Name of the image file to be tested.

            `datum` (3-tuple of ints):

                (i, j, k) are the row, column and band of the datum  to be
                tested. 'i' and 'j' should be at least 10 pixels away from the
                edge of the associated image and `k` should have at least 10
                bands above and below it in the image.

            `value` (int or float):

                The scalar value associated with location (i, j, k) in
                the image.

        Keyword Arguments:

            `dtypes` (tuple of numpy dtypes):

                The file will be tested for all of the dtypes given. If not
                specified, only float32 an float64 will be tested.
        '''
        self.filename = filename
        self.datum = datum
        self.value = value
        self.dtypes = kwargs.get('dtypes', ('f4', 'f8'))
        self.dtypes = [np.dtype(d).name for d in self.dtypes]

    def run(self):
        print('\n' + '-' * 72)
        print('Running SpyFile read tests.')
        print('-' * 72)

        if not os.path.isdir(testdir):
            os.mkdir(testdir)
        image = spy.open_image(self.filename)
        basename = os.path.join(testdir,
                                os.path.splitext(self.filename)[0])
        interleaves = ('bil', 'bip', 'bsq')
        ends = ('big', 'little')
        cases = itertools.product(interleaves, self.dtypes, ends)
        for (inter, dtype, endian) in cases:
            fname = '%s_%s_%s_%s.hdr' % (basename, inter, dtype,
                                         endian)
            spy.envi.save_image(fname, image, interleave=inter,
                                dtype=dtype, byteorder=endian)
            msg = 'Running SpyFile read tests on %s %s %s-endian file ' \
                % (inter.upper(), np.dtype(dtype).name, endian)
            testimg = spy.open_image(fname)
            if testimg.using_memmap is True:
                print('\n' + '-' * 72)
                print(msg + 'using memmap...')
                print('-' * 72)
                test = SpyFileTest(testimg, self.datum, self.value)
                test.run()
                print('\n' + '-' * 72)
                print(msg + 'without memmap...')
                print('-' * 72)
                testimg._disable_memmap()
                test = SpyFileTest(testimg, self.datum, self.value)
                test.run()
            else:
                print('\n' + '-' * 72)
                print(msg + 'without memmap...')
                print('-' * 72)
                test = SpyFileTest(testimg, self.datum, self.value)
                test.run()

def run():
    tests = [('92AV3C.lan', (99, 99, 99), 2057.0)]
#    tests = [('92AV3C.lan', (99, 99, 99), 2057.0),
#             ('f970619t01p02_r02_sc04.a.rfl', (99, 99, 99), 0.2311),
#             ('cup95eff.int.hdr', (99, 99, 33), 0.1842)]
    for (fname, datum, value) in tests:
        try:
            check = find_file_path(fname)
            suite = SpyFileTestSuite(fname, datum, value,
                                     dtypes=('i2', 'i4', 'f4', 'f8'))
            suite.run()
        except FileNotFoundError:
            print('File "%s" not found. Skipping.' % fname)

if __name__ == '__main__':
    run()

max_stars_repo_name: wwlswj/spectral

max_issues_repo_name: wwlswj/spectral

max_forks_repo_name: wwlswj/spectral

reason: import numpy


text:
# Module to run tests on initializing IGMGalaxyField

# TEST_UNICODE_LITERALS

import pytest
import numpy as np

from astropy.coordinates import SkyCoord

from pyigm.clustering.clustering_field import ClusteringField
from pyigm.clustering.tests import utils

'''
def data_path(filename):
    data_dir = os.path.join(os.path.dirname(__file__), 'files')
    return os.path.join(data_dir, filename)
'''
seed = 101

def test_init_cfield():

    rand_gal = utils.make_rand_gal(nrand=1000)

    # Now Clustering
    cf = ClusteringField(SkyCoord(ra=200.5, dec=25.5, unit='deg'))
    cf.galaxies = rand_gal

    # Add em
    gal_idx = rand_gal['ZGAL'] > 0.31
    cf.addGal(gal_idx)

    # Test
    assert len(cf.galrand) == 10*np.sum(gal_idx)

max_stars_repo_name: pyigm/pyigm

max_issues_repo_name: pyigm/pyigm

max_forks_repo_name: pyigm/pyigm

reason: import numpy,from astropy


text:
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import math
from matplotlib import pyplot as plt

from sklearn.preprocessing import LabelEncoder

feature_dict = {i:label for i,label in zip(
                range(4),
                  ('sepal length in cm',
                  'sepal width in cm',
                  'petal length in cm',
                  'petal width in cm', ))}

df = pd.io.parsers.read_csv(
    filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
    header=None,
    sep=','
    )
print (feature_dict.items())

df.columns = [l for i,l in sorted(feature_dict.items())] + ['class label']
df.dropna(how="all", inplace=True) # to drop the empty line at file-end

df.tail()

X = df[['sepal length in cm','sepal width in cm', 'petal length in cm',  'petal width in cm']].values
y = df['class label'].values

enc = LabelEncoder()
label_encoder = enc.fit(y)
y = label_encoder.transform(y) + 1

label_dict = {1: 'Setosa', 2: 'Versicolor', 3:'Virginica'}

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12,6))

for ax,cnt in zip(axes.ravel(), range(4)):

    # set bin sizes
    min_b = math.floor(np.min(X[:,cnt]))
    max_b = math.ceil(np.max(X[:,cnt]))
    bins = np.linspace(min_b, max_b, 25)

    # plottling the histograms
    for lab,col in zip(range(1,4), ('blue', 'red', 'green')):
        ax.hist(X[y==lab, cnt],
                   color=col,
                   label='class %s' %label_dict[lab],
                   bins=bins,
                   alpha=0.5,)
    ylims = ax.get_ylim()

    # plot annotation
    leg = ax.legend(loc='upper right', fancybox=True, fontsize=8)
    leg.get_frame().set_alpha(0.5)
    ax.set_ylim([0, max(ylims)+2])
    ax.set_xlabel(feature_dict[cnt])
    ax.set_title('Iris histogram #%s' %str(cnt+1))

    # hide axis ticks
    ax.tick_params(axis="both", which="both", bottom="off", top="off",
            labelbottom="on", left="off", right="off", labelleft="on")

    # remove axis spines
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)

axes[0][0].set_ylabel('count')
axes[1][0].set_ylabel('count')

fig.tight_layout()

plt.show()

np.set_printoptions(precision=4)

mean_vectors = []
for cl in range(1,4):
    mean_vectors.append(np.mean(X[y==cl], axis=0))
    print('Mean Vector class %s: %s\n' %(cl, mean_vectors[cl-1]))

S_W = np.zeros((4,4))
for cl,mv in zip(range(1,4), mean_vectors):
    class_sc_mat = np.zeros((4,4))                  # scatter matrix for every class
    for row in X[y == cl]:
        row, mv = row.reshape(4,1), mv.reshape(4,1) # make column vectors
        class_sc_mat += (row-mv).dot((row-mv).T)
    S_W += class_sc_mat                             # sum class scatter matrices
print('within-class Scatter Matrix:\n', S_W)

overall_mean = np.mean(X, axis=0)

S_B = np.zeros((4,4))
for i,mean_vec in enumerate(mean_vectors):
    n = X[y==i+1,:].shape[0]
    mean_vec = mean_vec.reshape(4,1) # make column vector
    overall_mean = overall_mean.reshape(4,1) # make column vector
    S_B += n * (mean_vec - overall_mean).dot((mean_vec - overall_mean).T)

print('between-class Scatter Matrix:\n', S_B)

eig_vals, eig_vecs = np.linalg.eig(np.linalg.inv(S_W).dot(S_B))

for i in range(len(eig_vals)):
    eigvec_sc = eig_vecs[:,i].reshape(4,1)
    print('\nEigenvector {}: \n{}'.format(i+1, eigvec_sc.real))
    print('Eigenvalue {:}: {:.2e}'.format(i+1, eig_vals[i].real))

for i in range(len(eig_vals)):
    eigv = eig_vecs[:,i].reshape(4,1)
    np.testing.assert_array_almost_equal(np.linalg.inv(S_W).dot(S_B).dot(eigv),
                                         eig_vals[i] * eigv,
                                         decimal=6, err_msg='', verbose=True)
print('ok')

# Make a list of (eigenvalue, eigenvector) tuples
eig_pairs = [(np.abs(eig_vals[i]), eig_vecs[:,i]) for i in range(len(eig_vals))]

# Sort the (eigenvalue, eigenvector) tuples from high to low
eig_pairs = sorted(eig_pairs, key=lambda k: k[0], reverse=True)

# Visually confirm that the list is correctly sorted by decreasing eigenvalues

print('Eigenvalues in decreasing order:\n')
for i in eig_pairs:
    print(i[0])

print('Variance explained:\n')
eigv_sum = sum(eig_vals)
for i,j in enumerate(eig_pairs):
    print('eigenvalue {0:}: {1:.2%}'.format(i+1, (j[0]/eigv_sum).real))

W = np.hstack((eig_pairs[0][1].reshape(4,1), eig_pairs[1][1].reshape(4,1)))
print('Matrix W:\n', W.real)

X_lda = X.dot(W)
assert X_lda.shape == (150,2), "The matrix is not 150x2 dimensional."

def plot_step_lda():

    ax = plt.subplot(111)
    for label,marker,color in zip(
        range(1,4),('^', 's', 'o'),('blue', 'red', 'green')):

        plt.scatter(x=X_lda[:,0].real[y == label],
                y=X_lda[:,1].real[y == label],
                marker=marker,
                color=color,
                alpha=0.5,
                label=label_dict[label]
                )

    plt.xlabel('LD1')
    plt.ylabel('LD2')

    leg = plt.legend(loc='upper right', fancybox=True)
    leg.get_frame().set_alpha(0.5)
    plt.title('LDA: Iris projection onto the first 2 linear discriminants')

    # hide axis ticks
    plt.tick_params(axis="both", which="both", bottom="off", top="off",
            labelbottom="on", left="off", right="off", labelleft="on")

    # remove axis spines
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)

    plt.grid()
    plt.tight_layout
    plt.show()

plot_step_lda()


text:
"""Embedding model class inherited the interface from gensim"""
from skills_ml.storage import FSStore
from skills_ml.algorithms.embedding.base import ModelStorage

from gensim.models import Doc2Vec, Word2Vec
from gensim.models.fasttext import FastText as FT_gensim

from sklearn.base import BaseEstimator, TransformerMixin

import numpy as np
import logging

class Word2VecModel(ModelStorage, Word2Vec):
    """The Word2VecModel inherited from gensim's Word2Vec model (
    https://radimrehurek.com/gensim/models/word2vec.html) for training,
    using and evaluating word embedding with extension methods.

    Example:
    ```
    from skills_ml.algorithms.embedding.models import Word2VecModel

    word2vec_model = Word2VecModel()
    ```
    """
    def __init__(self, *args, **kwargs):
        ModelStorage.__init__(self, storage=kwargs.pop('storage', None))
        Word2Vec.__init__(self, *args, **kwargs)
        self.model_name = ""
        self.model_type = "word2vec"
        self._metadata = None

    def infer_vector(self, doc_words, warning=False):
        """
        Average all the word-vectors together and ignore the unseen words
        Arg:
            doc_words (list): a list of tokenized words
        Returns:
            a vector representing a whole doc/sentence
        """
        sum_vector = np.zeros(self.vector_size)
        words_in_vocab = []
        for token in doc_words:
            try:
                sum_vector += self[token]
                words_in_vocab.append(token)
            except KeyError as e:
                if warning:
                    logging.warning("".join([str(e), ". Ignore the word."]))
                pass

        if len(words_in_vocab) == 0:
            logging.warning("None of the words is in vocabulary.")
            return np.random.rand(self.vector_size)
        sentence_vector = sum_vector / len(words_in_vocab)
        return sentence_vector

class Doc2VecModel(ModelStorage, Doc2Vec):
    """The Doc2VecModel inherited from gensim's Doc2Vec model (
    https://radimrehurek.com/gensim/models/doc2vec) for training,
    using and evaluating word embedding with extension methods.

    Example:
    ```
    from skills_ml.algorithms.embedding.models import Doc2VecModel

    doc2vec_model = Doc2VecModel()
    ```
    """
    def __init__(self, *args, **kwargs):
        ModelStorage.__init__(self, storage=kwargs.pop('storage', None))
        Doc2Vec.__init__(self, *args, **kwargs)
        self.model_name = ""
        self.model_type = "doc2vec"
        self.lookup_dict = None

class FastTextModel(ModelStorage, FT_gensim):
    """The FastTextModel inhereited from gensim's FastText model (
    https://radimrehurek.com/gensim/models/fasttext.html) for training,
    using and evaluating word embedding with extension methods.

    Example:
        ```
        from skills_ml.algorithms.embedding.models import import FastTextModel

        fasttext = FastTextModel()
        ```
    """
    def __init__(self, *args, **kwargs):
        ModelStorage.__init__(self, storage=kwargs.pop('storage', None))
        FT_gensim.__init__(self, *args, **kwargs)
        self.model_name = ""
        self.model_type = "fasttext"

    def infer_vector(self, doc_words, warning=False):
         """
         Average all the word-vectors together and ignore the unseen words
         """
         sum_vector = np.zeros(self.vector_size)
         words_in_vocab = []
         for token in doc_words:
             try:
                 sum_vector += self[token]
                 words_in_vocab.append(token)
             except KeyError as e:
                 if warning:
                    logging.warning("".join([str(e), ". Ignore the word."]))
                 pass

         if len(words_in_vocab) == 0:
             logging.warning("None of the words is in vocabulary.")
             return np.random.rand(self.vector_size)
         sentence_vector = sum_vector / len(words_in_vocab)
         return sentence_vector

class EmbeddingTransformer(BaseEstimator, TransformerMixin):
    def __init__(self, embedding_model):
        self.embedding_model = embedding_model

    def fit(self, X, y):
        return self

    def transform(self, X):
        trans_X = []
        for x in X:
            trans_X.append(self.embedding_model.infer_vector(x))
        return trans_X

    def fit_transform(self, X, y=None):
        self.fit(X, y)
        return self.transform(X)

max_stars_repo_name: varshantdhar/skills-ml

max_issues_repo_name: varshantdhar/skills-ml

max_forks_repo_name: varshantdhar/skills-ml


text:
'''
This script provides utility functions estimating angles and stretch of a elastic double pendulum from image.
Step 1. Extract each of the two pendulums from the image.
Step 2. Do a rectangle fitting of each pendulum.
Step 3. Estimate the angles of the pendulums and the stretch of the elastic one.
Certain images will be rejected if any pendulum does not exist, is hidden, or has a wrong shape.
'''

import cv2
import os
import numpy as np

'''
Extract the two pendulums from the image.
Args:
    img: elastic double pendulum image in BGR format
Returns:
    seg_1: segmentation of the first pendulum
    seg_2: segmentation of the second pendulum
'''
def seg_from_img(img):
    # pixel thresholds (in HSV)
    v_min_1 = (0, 0, 0)
    v_max_1 = (255, 255, 140)
    v_min_2 = (60, 0, 0)
    v_max_2 = (255, 255, 255)
    # background color in HSV (RGB: [215, 205, 192])
    bg_color = [17, 27, 215]

    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # extract the first pendulum
    seg_1 = cv2.inRange(img_hsv, v_min_1, v_max_1)
    # remove the first pendulum by replacing it with background pixels
    img_hsv[seg_1==255] = bg_color
    # extract the second pendulum
    seg_2 = cv2.inRange(img_hsv, v_min_2, v_max_2)

    return seg_1, seg_2

'''
Fit pendulum in a rectangle.
Args:
    seg: segmentation of the pendulum
Returns:
    rej: (True/False) if the image is rejected
    rect: (Box2D structure) the fitted rectangle
'''
def fit_pendulum(seg):
    # find all contours
    contours, _ = cv2.findContours(seg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # reject if no contours found
    if len(contours) == 0:
        return True, None
    # find the contour with the maximum area
    cnt = max(contours, key=cv2.contourArea)
    area = cv2.contourArea(cnt)
    # reject if the contour is too small
    if area < 60:
        return True, None
    # rectangle fitting
    rect = cv2.minAreaRect(cnt)
    _, (width, height), _ = rect
    # reject if the rectangle is too close to a square
    if abs(height-width) < 4:
        return True, None
    # reject if the rectangle does not properly fit the contour
    if height*width > 1.8 * area:
        return True, None

    return False, rect

'''
Estimate the angle of pendulum from its fitted rectangle.
Args:
    rect: (Box2D structure) the fitted rectangle
    ref_pt: reference point to determine the arrow direction
Returns:
    angle: the estimated angle in radians in range (0, 2*pi)
    box: box points of the rectangle
    arrow: the arrow pointing along the angle
'''
def estimate_angle(rect, ref_pt):
    # box points
    box = cv2.boxPoints(rect)
    # center, width and height
    (cx, cy), (width, height), _ = rect
    # specify the direction vector of the pendulum
    if width < height:
        v_d = box[0] - box[1]
    else:
        v_d = box[0] - box[3]
    # reference vector
    v_ref = np.array([cx, cy]) - ref_pt
    # choose the one from v_d and -v_d whose angle with v_ref
    # is less than pi
    if np.dot(v_d, v_ref) < 0:
        v_d = -v_d
    # counterclockwise angle from (0,-1) to v_d
    angle = np.arctan2(-v_d[1], v_d[0]) + np.pi/2
    if angle < 0:
        angle += 2*np.pi
    arrow = ((int(cx), int(cy)), (int(cx+v_d[0]), int(cy+v_d[1])))
    return angle, box, arrow

'''
Obtain the angles and stretch of elastic double pendulum from image
Args:
    img: elastic double pendulum image in BGR format
Returns:
    rej: (True/False) if the image is rejected
    angles: the estimated angles in radians in range (0, 2*pi)
    stretch: the estimated stretch of the elastic pendulum (unit: m)
    img_marked: image marked with the fitted rectangles,
    the direction vectors and the estimated angles and stretch (BGR format)
'''
def obtain_angle_stretch(img):
    img_marked = img.copy()
    seg_1, seg_2 = seg_from_img(img)
    rej_1, rect_1 = fit_pendulum(seg_1)
    rej_2, rect_2 = fit_pendulum(seg_2)
    if rej_1:
        rej = True
        angles = []
        stretch = None
        cv2.putText(img_marked, 'Reject arm 1', (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
    elif rej_2:
        rej = True
        angles = []
        stretch = None
        cv2.putText(img_marked, 'Reject arm 2', (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
    else:
        rej = False
        img_center = np.array([64, 64])
        angle_1, box_1, arrow_1 = estimate_angle(rect_1, img_center)
        (cx, cy), (width, height), _ = rect_1
        pd_1_end = 2 * np.array([cx, cy]) - img_center
        angle_2, box_2, arrow_2 = estimate_angle(rect_2, pd_1_end)
        angles = [angle_1, angle_2]
        # compute the stretch of the elastic pendulum (unit: m)
        # max(width, height): stretched length of the elastic pendulum in pixels
        L_0_img = 24  # unstretched length of the elastic pendulum in pixels
        L_0 = 0.205   # unstretched length of the elastic pendulum (m)
        stretch = (max(width, height) - L_0_img) / L_0_img * L_0
        # mark the fitted rectangles
        cv2.drawContours(img_marked, [np.int0(box_1)], 0, (0,0,255), 2)
        cv2.drawContours(img_marked, [np.int0(box_2)], 0, (0,0,255), 2)
        # mark the direction vectors
        cv2.arrowedLine(img_marked, arrow_1[0], arrow_1[1], (0, 0, 255), 1, tipLength=0.25)
        cv2.arrowedLine(img_marked, arrow_2[0], arrow_2[1], (0, 0, 255), 1, tipLength=0.25)
        # mark the estimated angles (degrees) and stretch (m)
        text = str(round(angle_1*180/np.pi)) + ' ' + str(round(angle_2*180/np.pi)) + ' ' + ('%.2f' % stretch)
        cv2.putText(img_marked, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 0), 1)

    return rej, angles, stretch, img_marked

max_stars_repo_name: BoyuanChen/neural-state-variables

max_issues_repo_name: BoyuanChen/neural-state-variables

max_forks_repo_name: BoyuanChen/neural-state-variables


text:
# EIA_CBECS_Land.py (flowsa)
# !/usr/bin/env python3
# coding=utf-8

import pandas as pd
import numpy as np
import io
from flowsa.common import *
from flowsa.flowbyfunctions import assign_fips_location_system

"""
2012 Commercial Buildings Energy Consumption Survey (CBECS)
https://www.eia.gov/consumption/commercial/reports/2012/energyusage/index.php
Last updated: Monday, August 17, 2020
"""

def eia_cbecs_URL_helper(build_url, config, args):
    """This helper function uses the "build_url" input from flowbyactivity.py, which is a base url for coa cropland data
    that requires parts of the url text string to be replaced with info specific to the usda nass quickstats API.
    This function does not parse the data, only modifies the urls from which data is obtained. """
    # initiate url list for coa cropland data
    urls = []
    # replace "__xlsx_name__" in build_url to create three urls
    for x in config['xlsx']:
        url = build_url
        url = url.replace("__xlsx__", x)
        urls.append(url)
      #  elif:

    return urls

def eia_cbecs_call(url, cbesc_response, args):
    # Convert response to dataframe
    df_raw_data = pd.io.excel.read_excel(io.BytesIO(cbesc_response.content), sheet_name='data').dropna()
    df_raw_rse = pd.io.excel.read_excel(io.BytesIO(cbesc_response.content), sheet_name='rse').dropna()

    if("b5.xlsx" in url):
        # skip rows and remove extra rows at end of dataframe
        df_data = pd.DataFrame(df_raw_data.loc[17:34]).reindex()
        df_rse = pd.DataFrame(df_raw_rse.loc[17:34]).reindex()

        df_data.columns = ["Name", "All buildings", "New England", "Middle Atlantic", "East North Central",
                          "West North Central", "South Atlantic",
                          "East South Central", "West South Central",
                          "Mountain", "Pacific"]
        df_rse.columns = ["Name", "All buildings", "New England", "Middle Atlantic", "East North Central",
                          "West North Central", "South Atlantic",
                          "East South Central", "West South Central",
                          "Mountain", "Pacific"]

        df_rse = df_rse.melt(id_vars=["Name"],
                    var_name="Location",
                    value_name="Spread")
        df_data =df_data.melt(id_vars=["Name"],
                    var_name="Location",
                    value_name="FlowAmount")
    elif("b12.xlsx" in url):
        # skip rows and remove extra rows at end of dataframe
        df_data = pd.DataFrame(df_raw_data.loc[46:50]).reindex()
        df_rse = pd.DataFrame(df_raw_rse.loc[46:50]).reindex()

        df_data.columns = ["Compartment", "All buildings", "Office", "Warehouse and storage", "Service",
                           "Mercantile", "Religious worship",
                           "Education", "Public assembly"]
        df_rse.columns = ["Compartment", "All buildings", "Office", "Warehouse and storage", "Service",
                           "Mercantile", "Religious worship",
                           "Education", "Public assembly"]
        df_rse = df_rse.melt(id_vars=["Compartment"],
                    var_name="Name",
                    value_name="Spread")
        df_data =df_data.melt(id_vars=["Compartment"],
                    var_name="Name",
                    value_name="FlowAmount")
    elif ("b14.xlsx" in url):
        # skip rows and remove extra rows at end of dataframe
        df_data = pd.DataFrame(df_raw_data.loc[27:31]).reindex()
        df_rse = pd.DataFrame(df_raw_rse.loc[27:31]).reindex()

        df_data.columns = ["Compartment", "All buildings", "Food service", "Food sales", "Lodging",
                           "Health care In-Patient", "Health care Out-Patient",
                           "Public order and safety"]
        df_rse.columns = ["Compartment", "All buildings", "Food service", "Food sales", "Lodging",
                           "Health care In-Patient", "Health care Out-Patient",
                           "Public order and safety"]
        df_rse = df_rse.melt(id_vars=["Compartment"],
                             var_name="Name",
                             value_name="Spread")
        df_data = df_data.melt(id_vars=["Compartment"],
                               var_name="Name",
                               value_name="FlowAmount")

    df = pd.merge(df_rse, df_data)
    return df

def eia_cbecs_parse(dataframe_list, args):
    # concat dataframes
    df_array = []
    for dataframes in dataframe_list:
        # rename column(s)
        dataframes = dataframes.rename(columns={'Name': 'ActivityConsumedBy'})
        if "Location" not in list(dataframes):
            dataframes["Location"] = "00000"
            dataframes["LocationSystem"] = "FIPS_2010"
            dataframes = dataframes.drop(dataframes[dataframes.Compartment == "Any elevators"].index)
            dataframes["Compartment"] = dataframes["Compartment"] + " floors"
        else:
            dataframes = dataframes.drop(dataframes[dataframes.ActivityConsumedBy == "Before 1920"].index)
            dataframes["Location"] = get_region_and_division_codes()["Division"]
            dataframes['LocationSystem'] = "Census_Division"
            dataframes["Compartment"] = "All Buildings"
            dataframes['Location'] = dataframes['Location'].replace(float("NaN"), '00000')

            dataframes.loc[dataframes.Location == "00000", "LocationSystem"] = "FIPS_2010"

        df_array.append(dataframes)
    df = pd.concat(df_array, sort=False)

    # replace withdrawn code
    df.loc[df['FlowAmount'] == "Q", 'FlowAmount'] = withdrawn_keyword
    df.loc[df['FlowAmount'] == "N", 'FlowAmount'] = withdrawn_keyword
    df["Class"] = 'Land'
    df["SourceName"] = 'EIA_CBECS_Land'
    df['Year'] = args["year"]
    df['FlowName'] = "Total Floorspace"
    df['Unit'] = "million square feet"
    df['MeasureofSpread'] = "RSE"
    return df


text:
# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np

def xyxy_to_xywh(xyxy):
    """Convert [x1 y1 x2 y2] box format to [x1 y1 w h] format."""
    if isinstance(xyxy, (list, tuple)):
        # Single box given as a list of coordinates
        assert len(xyxy) == 4
        x1, y1 = xyxy[0], xyxy[1]
        w = xyxy[2] - x1 + 1
        h = xyxy[3] - y1 + 1
        return (x1, y1, w, h)
    elif isinstance(xyxy, np.ndarray):
        # Multiple boxes given as a 2D ndarray
        return np.hstack((xyxy[:, 0:2], xyxy[:, 2:4] - xyxy[:, 0:2] + 1))
    else:
        raise TypeError("Argument xyxy must be a list, tuple, or numpy array.")

def expand_boxes(boxes, scale):
    """Expand an array of boxes by a given scale."""
    box_dim = boxes.shape[1]
    if box_dim == 4:
        w_half = (boxes[:, 2] - boxes[:, 0]) * 0.5
        h_half = (boxes[:, 3] - boxes[:, 1]) * 0.5
        x_c = (boxes[:, 2] + boxes[:, 0]) * 0.5
        y_c = (boxes[:, 3] + boxes[:, 1]) * 0.5

        w_half *= scale
        h_half *= scale

        boxes_exp = np.zeros(boxes.shape)
        boxes_exp[:, 0] = x_c - w_half
        boxes_exp[:, 2] = x_c + w_half
        boxes_exp[:, 1] = y_c - h_half
        boxes_exp[:, 3] = y_c + h_half
    elif box_dim == 5:
        boxes_exp = boxes.copy()
        boxes_exp[:, 2:4] *= scale
    else:
        raise Exception("Unsupported box dimension: {}".format(box_dim))

    return boxes_exp

max_stars_repo_name: luluseptember/inference

max_issues_repo_name: luluseptember/inference

max_forks_repo_name: luluseptember/inference


text:
"""
File: examples/jumping/jumping_distribution_sum.py
Author: Keith Tauscher
Date: 26 Feb 2019

Description: Example script showing the creation and use of a
             JumpingDistributionSum, which allows for multiple
             JumpingDistribution classes to be used according to a set of
             discrete probabilities.
"""
import os, time
import numpy as np
import matplotlib.pyplot as pl
from distpy import GaussianJumpingDistribution, UniformJumpingDistribution,\
    JumpingDistributionSum, load_jumping_distribution_from_hdf5_file

ndraw = int(1e5)
source = 1.

variances = (1 / 9., 1.)
jumping_distributions = [GaussianJumpingDistribution(1.),\
    UniformJumpingDistribution(1 / 3.)]
weights = np.array([1., 2.])

jumping_distribution = JumpingDistributionSum(jumping_distributions, weights)
hdf5_file_name = 'testing_jumping_distribution_sum_class_DELETE_THIS.hdf5'

try:
    jumping_distribution.save(hdf5_file_name)
    assert(jumping_distribution ==\
        load_jumping_distribution_from_hdf5_file(hdf5_file_name))
    assert(jumping_distribution == JumpingDistributionSum.load(hdf5_file_name,\
        *map(lambda x : x.__class__, jumping_distributions)))
except:
    os.remove(hdf5_file_name)
    raise
else:
    os.remove(hdf5_file_name)

start_time = time.time()
draws = jumping_distribution.draw(source, shape=ndraw)
end_time = time.time()
duration = end_time - start_time
print(("It took {0:.5f} s to draw a sample of size {1:d} from a " +\
    "JumpingDistributionSum containing a GaussianJumpingDistribution and a " +\
    "UniformJumpingDistribution.").format(duration, ndraw))

xs = np.linspace(source - 4, source + 4, 1000)
ys = np.array([np.exp(jumping_distribution.log_value(source, x)) for x in xs])

pl.hist(draws, histtype='stepfilled', density=True, label='sampled', bins=xs)
pl.plot(xs, ys, label='exp(log_value)')
pl.legend()
pl.show()

max_stars_repo_name: CU-NESS/distpy

max_issues_repo_name: CU-NESS/distpy

max_forks_repo_name: CU-NESS/distpy


text:
import unittest

import numpy as np
import numpy.testing as np_test

from pgmpy.factors.discrete import DiscreteFactor, TabularCPD
from pgmpy.models import BayesianModel
from pgmpy.inference import Inference
from pgmpy.inference import VariableElimination

class TestStateNameInit(unittest.TestCase):
    def setUp(self):
        self.sn2 = {
            "grade": ["A", "B", "F"],
            "diff": ["high", "low"],
            "intel": ["poor", "good", "very good"],
        }
        self.sn1 = {
            "speed": ["low", "medium", "high"],
            "switch": ["on", "off"],
            "time": ["day", "night"],
        }

        self.sn2_no_names = {"grade": [0, 1, 2], "diff": [0, 1], "intel": [0, 1, 2]}
        self.sn1_no_names = {"speed": [0, 1, 2], "switch": [0, 1], "time": [0, 1]}

        self.phi1 = DiscreteFactor(["speed", "switch", "time"], [3, 2, 2], np.ones(12))
        self.phi2 = DiscreteFactor(
            ["speed", "switch", "time"], [3, 2, 2], np.ones(12), state_names=self.sn1
        )

        self.cpd1 = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
        )
        self.cpd2 = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
            state_names=self.sn2,
        )

        student = BayesianModel([("diff", "grade"), ("intel", "grade")])
        diff_cpd = TabularCPD("diff", 2, [[0.2, 0.8]])
        intel_cpd = TabularCPD("intel", 2, [[0.3, 0.7]])
        grade_cpd = TabularCPD(
            "grade",
            3,
            [[0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1], [0.8, 0.8, 0.8, 0.8]],
            evidence=["diff", "intel"],
            evidence_card=[2, 2],
        )
        student.add_cpds(diff_cpd, intel_cpd, grade_cpd)
        self.model1 = Inference(student)
        self.model2 = Inference(student)

    def test_factor_init_statename(self):
        self.assertEqual(self.phi1.state_names, self.sn1_no_names)
        self.assertEqual(self.phi2.state_names, self.sn1)

    def test_cpd_init_statename(self):
        self.assertEqual(self.cpd1.state_names, self.sn2_no_names)
        self.assertEqual(self.cpd2.state_names, self.sn2)

class StateNameDecorator(unittest.TestCase):
    def setUp(self):
        self.sn2 = {
            "grade": ["A", "B", "F"],
            "diff": ["high", "low"],
            "intel": ["poor", "good", "very good"],
        }
        self.sn1 = {
            "speed": ["low", "medium", "high"],
            "switch": ["on", "off"],
            "time": ["day", "night"],
        }

        self.phi1 = DiscreteFactor(["speed", "switch", "time"], [3, 2, 2], np.ones(12))
        self.phi2 = DiscreteFactor(
            ["speed", "switch", "time"], [3, 2, 2], np.ones(12), state_names=self.sn1
        )

        self.cpd1 = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
        )
        self.cpd2 = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
            state_names=self.sn2,
        )

        student = BayesianModel([("diff", "grade"), ("intel", "grade")])
        student_state_names = BayesianModel([("diff", "grade"), ("intel", "grade")])

        diff_cpd = TabularCPD("diff", 2, [[0.2, 0.8]])
        intel_cpd = TabularCPD("intel", 2, [[0.3, 0.7]])
        grade_cpd = TabularCPD(
            "grade",
            3,
            [[0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1], [0.8, 0.8, 0.8, 0.8]],
            evidence=["diff", "intel"],
            evidence_card=[2, 2],
        )

        diff_cpd_state_names = TabularCPD(
            variable="diff",
            variable_card=2,
            values=[[0.2, 0.8]],
            state_names={"diff": ["high", "low"]},
        )
        intel_cpd_state_names = TabularCPD(
            variable="intel",
            variable_card=2,
            values=[[0.3, 0.7]],
            state_names={"intel": ["poor", "good", "very good"]},
        )
        grade_cpd_state_names = TabularCPD(
            "grade",
            3,
            [[0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1], [0.8, 0.8, 0.8, 0.8]],
            evidence=["diff", "intel"],
            evidence_card=[2, 2],
            state_names=self.sn2,
        )

        student.add_cpds(diff_cpd, intel_cpd, grade_cpd)
        student_state_names.add_cpds(
            diff_cpd_state_names, intel_cpd_state_names, grade_cpd_state_names
        )

        self.model_no_state_names = VariableElimination(student)
        self.model_with_state_names = VariableElimination(student_state_names)

    def test_assignment_statename(self):
        req_op1 = [
            [("speed", "low"), ("switch", "on"), ("time", "night")],
            [("speed", "low"), ("switch", "off"), ("time", "day")],
        ]
        req_op2 = [
            [("speed", 0), ("switch", 0), ("time", 1)],
            [("speed", 0), ("switch", 1), ("time", 0)],
        ]

        self.assertEqual(self.phi1.assignment([1, 2]), req_op2)
        self.assertEqual(self.phi2.assignment([1, 2]), req_op1)

    def test_factor_reduce_statename(self):
        phi = DiscreteFactor(
            ["speed", "switch", "time"], [3, 2, 2], np.ones(12), state_names=self.sn1
        )
        phi.reduce([("speed", "medium"), ("time", "day")])
        self.assertEqual(phi.variables, ["switch"])
        self.assertEqual(phi.cardinality, [2])
        np_test.assert_array_equal(phi.values, np.array([1, 1]))

        phi = DiscreteFactor(
            ["speed", "switch", "time"], [3, 2, 2], np.ones(12), state_names=self.sn1
        )
        phi = phi.reduce([("speed", "medium"), ("time", "day")], inplace=False)
        self.assertEqual(phi.variables, ["switch"])
        self.assertEqual(phi.cardinality, [2])
        np_test.assert_array_equal(phi.values, np.array([1, 1]))

        phi = DiscreteFactor(["speed", "switch", "time"], [3, 2, 2], np.ones(12))
        phi.reduce([("speed", 1), ("time", 0)])
        self.assertEqual(phi.variables, ["switch"])
        self.assertEqual(phi.cardinality, [2])
        np_test.assert_array_equal(phi.values, np.array([1, 1]))

        phi = DiscreteFactor(["speed", "switch", "time"], [3, 2, 2], np.ones(12))
        phi = phi.reduce([("speed", 1), ("time", 0)], inplace=False)
        self.assertEqual(phi.variables, ["switch"])
        self.assertEqual(phi.cardinality, [2])
        np_test.assert_array_equal(phi.values, np.array([1, 1]))

    def test_reduce_cpd_statename(self):
        cpd = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
            state_names=self.sn2,
        )
        cpd.reduce([("diff", "high")])
        self.assertEqual(cpd.variable, "grade")
        self.assertEqual(cpd.variables, ["grade", "intel"])
        np_test.assert_array_equal(
            cpd.get_values(),
            np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.8, 0.8, 0.8]]),
        )

        cpd = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
        )
        cpd.reduce([("diff", 0)])
        self.assertEqual(cpd.variable, "grade")
        self.assertEqual(cpd.variables, ["grade", "intel"])
        np_test.assert_array_equal(
            cpd.get_values(),
            np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.8, 0.8, 0.8]]),
        )

        cpd = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
            state_names=self.sn2,
        )
        cpd = cpd.reduce([("diff", "high")], inplace=False)
        self.assertEqual(cpd.variable, "grade")
        self.assertEqual(cpd.variables, ["grade", "intel"])
        np_test.assert_array_equal(
            cpd.get_values(),
            np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.8, 0.8, 0.8]]),
        )

        cpd = TabularCPD(
            "grade",
            3,
            [
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8],
            ],
            evidence=["diff", "intel"],
            evidence_card=[2, 3],
        )
        cpd = cpd.reduce([("diff", 0)], inplace=False)
        self.assertEqual(cpd.variable, "grade")
        self.assertEqual(cpd.variables, ["grade", "intel"])
        np_test.assert_array_equal(
            cpd.get_values(),
            np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.8, 0.8, 0.8]]),
        )

    def test_inference_query_statename(self):
        inf_op1 = self.model_with_state_names.query(
            ["grade"], evidence={"intel": "poor"}
        )
        inf_op2 = self.model_no_state_names.query(["grade"], evidence={"intel": 0})
        req_op = DiscreteFactor(["grade"], [3], np.array([0.1, 0.1, 0.8]))

        self.assertEqual(inf_op1, req_op)
        self.assertEqual(inf_op1, req_op)

        inf_op1 = self.model_with_state_names.map_query(
            ["grade"], evidence={"intel": "poor"}
        )
        inf_op2 = self.model_no_state_names.map_query(["grade"], evidence={"intel": 0})
        req_op1 = {"grade": "F"}
        req_op2 = {"grade": 2}

        self.assertEqual(inf_op1, req_op1)
        self.assertEqual(inf_op2, req_op2)

max_stars_repo_name: echoyi/pgmpy

max_issues_repo_name: echoyi/pgmpy

max_forks_repo_name: echoyi/pgmpy


text:
import numpy as np
from edflow.data.dataset_mixin import DatasetMixin
from edflow.util import PRNGMixin
from edflow.util import retrieve
from edflow.main import get_obj_from_str

def JoinedDataset(dataset, key, n_joins):
    """Concat n_joins random samples based on the condition that
    example_i[key] == example_j[key] for all i,j. Key must be in labels of
    dataset."""
    labels = np.asarray(dataset.labels[key])
    unique_labels = np.unique(labels)
    index_map = dict()
    for value in unique_labels:
        index_map[value] = np.nonzero(labels == value)[0]
    join_indices = [list(range(len(dataset)))]  # example_0 is original example
    prng = np.random.RandomState(1)
    for k in range(n_joins - 1):
        indices = [prng.choice(index_map[value]) for value in labels]
        join_indices.append(indices)
    datasets = [SubDataset(dataset, indices) for indices in join_indices]
    dataset = ExampleConcatenatedDataset(*datasets)

    return dataset

def getDebugDataset(config):
    """Loads a dataset from the config and makes ist reasonably small.
    The config syntax works as in :func:`getSeqDataset`. See there for
    more extensive documentation.

    Parameters
    ----------
    config : dict
	An edflow config, with at least the keys
            ``debugdataset`` and nested inside it ``dataset``,
            ``debug_length``, defining the basedataset and its size.

    Returns
    -------
    :class:`SubDataset`:
        A dataset based on the basedataset of the specifed length.
    """

    ks = "debugdataset"
    base_dset = get_implementations_from_config(config[ks], ["dataset"])["dataset"]
    base_dset = base_dset(config=config)
    indices = np.arange(config[ks]["debug_length"])

    return SubDataset(base_dset, indices)

class RandomlyJoinedDataset(DatasetMixin, PRNGMixin):
    """
    Load multiple examples which have the same label.

    Required config parameters:
        :RandomlyJoinedDataset/dataset:    The dataset from which to load
                                            examples.
        :RandomlyJoinedDataset/key:        The key of the label to join on.

    Optional config parameters:
        :test_mode=False:                            If True, behaves deterministic.
        :RandomlyJoinedDataset/n_joins=2:            How many examples to load.
        :RandomlyJoinedDataset/balance=False:        If True and not in test_mode,
                                                      sample join labels uniformly.
        :RandomlyJoinedDataset/avoid_identity=True:  If True and not in test_mode,
                                                      never return a pair containing the same
                                                      image if possible.

    The i-th example returns:
        :'examples': A list of examples, where each example has the same label
         as specified by key. If data_balancing is `False`, the first element of
         the list will be the `i-th` example of the dataset.

    The dataset's labels are the same as that of dataset. Be careful,
    `examples[j]` of the i-th example does not correspond to the i-th entry of
    the labels but to the `examples[j]["index_"]`-th entry.
    """

    def __init__(self, config):
        self.dataset = retrieve(config, "RandomlyJoinedDataset/dataset")
        self.dataset = get_obj_from_str(self.dataset)
        self.dataset = self.dataset(config)
        self.key = retrieve(config, "RandomlyJoinedDataset/key")
        self.n_joins = retrieve(config, "RandomlyJoinedDataset/n_joins", default=2)

        self.test_mode = retrieve(config, "test_mode", default=False)
        self.avoid_identity = retrieve(
            config, "RandomlyJoinedDataset/avoid_identity", default=True
        )
        self.balance = retrieve(config, "RandomlyJoinedDataset/balance", default=False)

        # self.index_map is used to select a partner for each example.
        # In test_mode it is a list containing a single partner index for each
        # example, otherwise it is a dict containing all indices for a given
        # join label
        self.join_labels = np.asarray(self.dataset.labels[self.key])
        unique_labels = np.unique(self.join_labels)
        self.index_map = dict()
        for value in unique_labels:
            self.index_map[value] = np.nonzero(self.join_labels == value)[0]
        if self.test_mode:
            prng = np.random.RandomState(0)
            self.index_map = [
                prng.choice(self.index_map[self.join_labels[i]], self.n_joins - 1)
                for i in range(len(self.dataset))
            ]

    def __len__(self):
        return len(self.dataset)

    @property
    def labels(self):
        """Careful this can only give labels of the original item, not the
        joined ones. Use 'examples[j]["index\_"]' to get the correct label
        index."""
        return self.dataset.labels

    def get_example(self, i):
        if self.test_mode:
            join_indices = self.index_map[i]
        else:
            if self.balance:
                label_id = self.prng.choice(list(self.index_map.keys()))
                i = self.prng.choice(self.index_map[label_id])
            join_value = self.join_labels[i]
            choices = self.index_map[join_value]
            replace = True
            if self.avoid_identity:
                if len(choices) > 1:
                    choices = [idx for idx in choices if not idx == i]
                if len(choices) >= self.n_joins - 1:
                    replace = False
            join_indices = self.prng.choice(choices, self.n_joins - 1, replace=replace)
        join_indices = np.concatenate([[i], join_indices])

        return {"examples": self.dataset[join_indices]}

class DataFolder(DatasetMixin):
    """Given the root of a possibly nested folder containing datafiles and a
    Callable that generates the labels to the datafile from its full name, this
    class creates a labeled dataset.

    A filtering of unwanted Data can be achieved by having the ``label_fn``
    return ``None`` for those specific files. The actual files are only
    read when ``__getitem__`` is called.

    If for example ``label_fn`` returns a dict with the keys ``['a', 'b',
    'c']`` and ``read_fn`` returns one with keys ``['d', 'e']`` then the dict
    returned by ``__getitem__`` will contain the keys ``['a', 'b', 'c', 'd',
    'e', 'file_path_', 'index_']``.
    """

    def __init__(
        self,
        image_root,
        read_fn,
        label_fn,
        sort_keys=None,
        in_memory_keys=None,
        legacy=True,
        show_bar=False,
    ):
        """
        Parameters
        ----------
        image_root : str
            Root containing the files of interest.
        read_fn : Callable
            Given the path to a file, returns the datum as a dict.
        label_fn : Callable
            Given the path to a file, returns a dict of
            labels. If ``label_fn`` returns ``None``, this file is ignored.
        sort_keys : list
            A hierarchy of keys by which the data in this Dataset are sorted.
        in_memory_keys : list
            keys which will be collected from examples when the dataset is cached.
        legacy : bool
            Use the old read ethod, where only the path to the
            current file is passed to the reader. The new version will
            see all labels, that have been previously collected.
        show_bar : bool
            Show a loading bar when loading labels.
        """

        self.root = image_root
        self.read = read_fn
        self.label_fn = label_fn
        self.sort_keys = sort_keys

        self.legacy = legacy
        self.show_bar = show_bar

        if in_memory_keys is not None:
            assert isinstance(in_memory_keys, list)
            self.in_memory_keys = in_memory_keys

        self._read_labels()

    def _read_labels(self):
        import operator

        if self.show_bar:
            n_files = 0
            for _ in os.walk(self.root):
                n_files += 1

            iterator = tqdm(os.walk(self.root), total=n_files, desc="Labels")
        else:
            iterator = tqdm(os.walk(self.root))

        self.data = []
        self.labels = {}
        for root, dirs, files in iterator:
            for f in files:
                path = os.path.join(root, f)
                labels = self.label_fn(path)

                if labels is not None:
                    datum = {"file_path_": path}
                    datum.update(labels)
                    self.data += [datum]

        if self.sort_keys is not None:
            self.data.sort(key=operator.itemgetter(*self.sort_keys))

        for datum in self.data:
            for k, v in datum.items():
                if k not in self.labels:
                    self.labels[k] = []
                self.labels[k] += [v]

    def get_example(self, i):
        """Load the files specified in example ``i``."""
        datum = self.data[i]
        path = datum["file_path_"]

        if self.legacy:
            file_content = self.read(path)
        else:
            file_content = self.read(**datum)

        example = dict()
        example.update(datum)
        example.update(file_content)

        return example

max_stars_repo_name: ffeldmann/edflow

max_issues_repo_name: ffeldmann/edflow

max_forks_repo_name: ArWeHei/edflow


text:
'''
Bibliotecas utilizadas
'''
import pandas as pd
import matplotlib.pyplot as plt
import re
import numpy as np

'''
Funções Complementares
'''

def limpezaFinal(txt):
  chars = "=*|>!:,[]'\()"
  for chars in chars:
    txt = txt.replace(chars, '')
  txt = txt.replace('"', ' ')
  txt = txt.replace('n', ' ')
  return txt

class CENIPA_API:
	'''
	Esta superclasse representa um extrator com finalidade de utilizar dados públicos para entender características e correlações aeronáuticas da aviação civil brasileira nos últimos 10 anos
	'''
	def __init__(self):
		'''
		Inicializador
  		'''
		self.df = pd.read_csv('https://raw.githubusercontent.com/brunabellini/CENIPA_API/main/Data/df_final_aeronautico.csv', sep=';')

	def get_table(self):
		'''
		Essa função tem o objetivo de retornar e mostrar o Dataset dentro do terminal
  		'''
		return self.df

	def get_acidente(self):
		'''
		Essa função tem o objetivo de retornar um dataset com as colunas relevantes para analise sobre os acidentes
		'''
		cols=['aeronave_tipo_veiculo', 'aeronave_fabricante','aeronave_modelo','aeronave_motor_tipo','aeronave_assentos','aeronave_ano_fabricacao','aeronave_registro_segmento','aeronave_voo_origem','aeronave_voo_destino','aeronave_fase_operacao','codigo_ocorrencia','ocorrencia_cidade','ocorrencia_uf','ocorrencia_hora','fator_aspecto','fator_condicionante','ocorrencia_tipo']
		self.df_acidente = self.df[cols]
		return self.df_acidente

	def get_fator_contribuinte(self):
		'''
        Essa função tem o objetivo de retornar um dataset com os fatores contribuintes para o acidente
        '''
		cols=['aeronave_ano_fabricacao','aeronave_fabricante','fator_nome','fator_aspecto','fator_condicionante','fator_area','ocorrencia_tipo','ocorrencia_tipo_categoria']
		self.df_contribuinte = self.df[cols]
		return self.df_contribuinte

	def get_aeronave(self):
		'''
        Essa função tem o objetivo de retornar um dataset para a analise das fatores das aeronaves
        '''
		cols=['aeronave_tipo_veiculo','aeronave_fabricante','aeronave_modelo','aeronave_tipo_icao','aeronave_motor_tipo','aeronave_ano_fabricacao','aeronave_pais_fabricante','aeronave_pais_registro','aeronave_registro_categoria','aeronave_registro_segmento','aeronave_voo_origem','aeronave_voo_destino','aeronave_fase_operacao','aeronave_tipo_operacao','aeronave_nivel_dano', 'aeronave_fatalidades_total']
		self.df_aero = self.df[cols]
		return self.df_aero

	def get_recomendacao(self):
		'''
		Essa função tem objetivo de retornar um dataset para a analise das recomendações do casos
        '''
		cols = ['recomendacao_dia_encaminhamento', 'recomendacao_conteudo', 'recomendacao_status','recomendacao_destinatario_sigla','recomendacao_destinatario']
		self.df_recom = self.df[cols]
		return self.df_recom

class Insights(CENIPA_API):
	'''
	Essa classe representa uma variedade de insights retirados e pensados a partir do dataframe de acidentes aeronáuticos apresentado na classe CENIPA_API
	'''
	def moda_coluna(self, col):
		'''
		Essa função tem o objetivo de retornar o valor mais recorrente em uma coluna escolhida do dataset
		'''
		try:
			result = self.df[col].value_counts()
		except KeyError:
			raise ColunaInexistente('Essa coluna não existe')

		restr = str(result)
		res = re.search(r'\w[^\n]+',restr)
		reestr = str(res)
		a = re.findall(r"\='[^\'']+",reestr)
		astr = str(a)
		self.resultado = limpezaFinal(astr)
		return self.resultado

	def mediana_coluna(self, col):
		'''
		Essa função tem o objetivo de retornar a mediana de uma coluna escolhida do dataset
		'''
		result = self.df[col].median()
		restr = str(result)
		res = re.search(r'\w[^\n]+',restr)
		reestr = str(res)
		a = re.findall(r"\='[^\'']+",reestr)
		astr = str(a)
		self.resultado = limpezaFinal(astr)
		return self.resultado

	def media_coluna(self,col):
		'''
		Essa função tem o objetivo de retornar a média de uma coluna escolhida do dataset
		'''
		try:
			self.result = self.df[col].mean()
		except ValueError:
		    raise ColunaNaoCalculavel('Essa coluna não é de um tipo calculável.')
		return self.result

	def variancia_coluna(self,col):
		'''
		Essa função tem o objetivo de retornar a variancia de uma coluna escolhida do dataset
		'''
		try:
			self.result = np.var(self.df[col])
		except ValueError:
			raise ColunaNaoCalculavel('Essa coluna não é de um tipo calculável.')
		return self.result

	def desvio_padrao_coluna(self,col):
		'''
		Essa função tem o objetivo de retornar o desvio padrão de uma coluna escolhida do dataset
		'''
		try:
			self.result = self.df[col].std()
		except ValueError:
			raise ColunaNaoCalculavel('Essa coluna não é de um tipo calculável.')
		return self.result

	def gravidade_acidentes(self):
		'''
		Essa função tem o objetivo de gerar o número de mortes totais e alguns gráficos com insights sobrer nível de dano, fatalidade e classificação da ocorrência
  		'''
		df_ta = self.df[['aeronave_nivel_dano', 'ocorrencia_classificacao', 'aeronave_fatalidades_total']]
		num_mortes = df_ta['aeronave_fatalidades_total'].sum()
		print(f'Houveram \033[91m{num_mortes}\033[0m mortes neste período de 10 anos de análise de ocorrências')

		fig, ax = plt.subplots(figsize=(10,10), subplot_kw=dict(aspect='equal'))
		labels = ['Dano substancial','Destruída','Dano leve','Nenhum dano']
		sizes = [df_ta['aeronave_nivel_dano'].value_counts()[0], df_ta['aeronave_nivel_dano'].value_counts()[1], df_ta['aeronave_nivel_dano'].value_counts()[2], df_ta['aeronave_nivel_dano'].value_counts()[3]]
		colors = ['darkorange','gold','tomato','lightgreen']
		wedges, texts, pct = ax.pie(sizes, startangle=90, colors=colors, pctdistance=1.1, autopct=lambda p : '{:.2f}%\n({:.0f})'.format(p,p * sum(sizes)/100))
		plt.setp(pct, size=18)
		plt.rcParams['font.size'] = 13
		plt.legend(labels, title= 'Legenda de nível:')
		ax.set_title('Porcentagem por nível de dano')
		plt.show()

		fig, ax = plt.subplots(figsize=(10,10), subplot_kw=dict(aspect='equal'))
		labels = ['Dano substancial','Dano leve','Destruída','Nenhum dano']
		sizes = [df_ta.loc[df_ta['aeronave_nivel_dano'] == 'SUBSTANCIAL', 'aeronave_fatalidades_total'].sum(), df_ta.loc[df_ta['aeronave_nivel_dano'] == 'LEVE', 'aeronave_fatalidades_total'].sum(), df_ta.loc[df_ta['aeronave_nivel_dano'] == 'DESTRUÍDA', 'aeronave_fatalidades_total'].sum(), df_ta.loc[df_ta['aeronave_nivel_dano'] == 'NENHUM', 'aeronave_fatalidades_total'].sum()]
		colors = ['deepskyblue','darkblue','lightblue','dodgerblue']
		explode = (0, 0, 0.1, 0)
		wedges, texts, pct = ax.pie(sizes, explode=explode, startangle=90, colors=colors, pctdistance=1.1, autopct=lambda p : '{:.2f}%\n({:.0f})'.format(p,p * sum(sizes)/100))
		plt.setp(pct, size=18)
		plt.rcParams['font.size'] = 13
		plt.legend(labels, title= 'Legenda de nível:')
		ax.set_title('Porcentagem por quantidade de fatalidade em cada nível de dano')
		plt.show()

		fig, ax = plt.subplots(figsize=(10,10), subplot_kw=dict(aspect='equal'))
		labels = ['Acidente','Incidente','Incidente grave']
		sizes = [df_ta['ocorrencia_classificacao'].value_counts()[0], df_ta['ocorrencia_classificacao'].value_counts()[1], df_ta['ocorrencia_classificacao'].value_counts()[2]]
		colors = ['darkorange','lightgreen','gold']
		wedges, texts, pct = ax.pie(sizes, startangle=90, colors=colors, pctdistance=1.1, autopct=lambda p : '{:.2f}%\n({:.0f})'.format(p,p * sum(sizes)/100))
		plt.setp(pct, size=18)
		plt.rcParams['font.size'] = 13
		plt.legend(labels, title= 'Legenda de nível:')
		ax.set_title('Porcentagem por classificação da ocorrência')
		plt.show()

	def tipos_mortes_aeronaves(self):
		'''
		Essa função tem o objetivo de retornar os tipos de aeronaves e a porcentagem de ocorrencias fatais nas aeronaves listadas com ocorrências
  		'''
		print(self.df['aeronave_registro_categoria'].value_counts())
		df_tpa = self.df[['aeronave_registro_categoria', 'aeronave_fatalidades_total']]
		ma = (df_tpa.loc[df_tpa['aeronave_registro_categoria'] == 'AVIÃO', 'aeronave_fatalidades_total'].sum()*100) / df_tpa['aeronave_registro_categoria'].value_counts()[0]
		mh = (df_tpa.loc[df_tpa['aeronave_registro_categoria'] == 'HELICÓPTERO', 'aeronave_fatalidades_total'].sum()*100) / df_tpa['aeronave_registro_categoria'].value_counts()[1]
		mp =  (df_tpa.loc[df_tpa['aeronave_registro_categoria'] == 'PLANADOR', 'aeronave_fatalidades_total'].sum()*100) / df_tpa['aeronave_registro_categoria'].value_counts()[2]
		mu = (df_tpa.loc[df_tpa['aeronave_registro_categoria'] == 'ULTRALEVE', 'aeronave_fatalidades_total'].sum()*100) / df_tpa['aeronave_registro_categoria'].value_counts()[3]
		maf = (df_tpa.loc[df_tpa['aeronave_registro_categoria'] == 'ANFÍBIO', 'aeronave_fatalidades_total'].sum()*100) / df_tpa['aeronave_registro_categoria'].value_counts()[4]
		return f'\033[1mEntre os tipos de aeronaves, encontram-se a porcentagem de ocorrências fatais:\033[0m \n   Avião {ma:.2f}%\n   Helicóptero {mh:.2f}%\n   Planador {mp:.2f}%\n   Ultraleve {mu:.2f}%\n   Anfíbio {maf:.2f}%'

	def top_fatores_contribuintes(self):
		'''
		Essa função tem o objetivo de retornar os top fatores contribuintes
  		'''
		n1= self.df['fator_nome'].value_counts().index.tolist()[0]
		n2= self.df['fator_nome'].value_counts().index.tolist()[1]
		n3= self.df['fator_nome'].value_counts().index.tolist()[2]
		a1= self.df['fator_aspecto'].value_counts().index.tolist()[0]
		a2= self.df['fator_aspecto'].value_counts().index.tolist()[1]
		a3= self.df['fator_aspecto'].value_counts().index.tolist()[2]
		c1= self.df['fator_condicionante'].value_counts().index.tolist()[0]
		c2= self.df['fator_condicionante'].value_counts().index.tolist()[1]
		c3= self.df['fator_condicionante'].value_counts().index.tolist()[2]
		ar1= self.df['fator_area'].value_counts().index.tolist()[0]
		ar2= self.df['fator_area'].value_counts().index.tolist()[1]
		return f'Top fatores contribuintes: \nNome do fator: {n1}, {n2}, {n3}\nAspecto: {a1}, {a2}, {a3}\nCondicionante: {c1}, {c2}, {c3}\nArea: {ar1} e {ar2}'

	def ocorrencia_tipo(self):
		'''
		Essa função tem o objetivo de visualizar quais são as ocorrências mais frequentes
  		'''
		return self.df['ocorrencia_tipo'].value_counts()

	def ocorrencia_estados(self, nome_UF):
		'''
		Essa função tem o objetivo de filtrar ocorrências pela sigla dos estados
  		'''
		estado = self.df.loc[(self.df['ocorrencia_uf']==nome_UF.upper())]
		if len(estado)==0:
			raise ExceçãoEstadoInexistente('Não há ocorrências para esse estado')
		return estado

	def ocorrencia_cidade(self, nome_cidade):
		'''
		Essa função tem o objetivo de filtrar ocorrências pelo nome de cidades
		'''
		cidade = self.df.loc[(self.df['ocorrencia_cidade']==nome_cidade.upper())]
		if len(cidade)==0:
			raise ExceçãoCidadeInexistente('Não há ocorrências para essa cidade')
		return cidade

	def informacoes_acidentes(self):
		'''
		Essa função tem o objetivo de retornar informações sobre a gravidade e detalhes de fatalidade do acidente
  		'''
		return self.df[['aeronave_nivel_dano', 'ocorrencia_classificacao', 'aeronave_fatalidades_total']]

	def ocorrencia_MesHora(self):
		'''
		Essa função tem o objetivo de apresentar os meses e horários que mais existem ocorrências
  		'''
		df_mh = self.df[['ocorrencia_dia','ocorrencia_hora']]
		novo_df = df_mh['ocorrencia_dia'].str.split("/", n=2, expand= True).astype(int)
		df_mh['mes_ocorrencia']= novo_df[1]
		df_mh['ano_ocorrencia']= novo_df[2]
		df_mh.drop('ocorrencia_dia', axis=1, inplace= True)
		m = df_mh['mes_ocorrencia'].value_counts().index.tolist()[0]
		a = df_mh['ano_ocorrencia'].value_counts().index.tolist()[0]
		h = df_mh['ocorrencia_hora'].value_counts().index.tolist()[0]
		return f'Top mês, dia e hora das ocorrências: \nMês: {m}\nAno: {a}\nHora: {h}'

	def ocorrencia_ano(self, ano):
		'''
		Essa função tem o objetivo de apresentar as informações de acordo com o ano selecionado
  		'''
		col_ano = self.df['ocorrencia_dia'].str.split("/", n=2, expand= True).astype(int)
		self.df['ano_ocorrencia']= col_ano[2]
		pesq_ano = self.df.loc[(self.df['ano_ocorrencia']==ano)]
		if len(pesq_ano)==0:
			raise ExceçãoAnoInexistente('Não há ocorrências para esse ano')
		return pesq_ano

class ColunaInexistente(Exception):
    pass

class ColunaNaoCalculavel(Exception):
    pass

class ExceçãoEstadoInexistente(Exception):
    pass

class ExceçãoCidadeInexistente(Exception):
    pass

class ExceçãoAnoInexistente(Exception):
    pass


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division, unicode_literals
import argparse
from rdkit import Chem
import pandas as pd
import numpy as np
import onmt.opts
from tqdm import tqdm

import sys
def canonicalize_smiles(smiles):
    mol = Chem.MolFromSmiles(smiles)
    if mol is not None:
        return Chem.MolToSmiles(mol, isomericSmiles=True)
    else:
        return ''

def canonicalize_smiles_clear_map(smiles):
    mol = Chem.MolFromSmiles(smiles)
    if mol is not None:
        [atom.ClearProp('molAtomMapNumber') for atom in mol.GetAtoms() if atom.HasProp('molAtomMapNumber')]
        return Chem.MolToSmiles(mol, isomericSmiles=True)
    else:
        return ''

def get_rank(row, base, max_rank):
    for i in range(1, max_rank+1):
        if row['target'] == row['{}{}'.format(base, i)]:
            return i
    return 0

def get_rerank(row, base, reranking, max_rank):
    for i in range(1, max_rank+1):
        if row['target'] == row['{}{}'.format(base, i)]:
            return row['{}{}'.format(reranking,i)]
    return 0

def main(opt):
    print('Reading targets from file ...')
    with open(opt.targets, 'r') as f:
        targets = [canonicalize_smiles_clear_map(''.join(line.strip().split(' '))) for line in tqdm(f.readlines())]

    targets = targets[:]
    predictions = [[] for i in range(opt.beam_size)]
    # print(targets[:3])
    # ['C1=COCCC1.COC(=O)CCC(=O)c1ccc(O)cc1O', 'COC(=O)c1cccc(C(=O)O)c1.Nc1cccnc1N', 'CC(C)(C)OC(=O)NC1CCC(C(=O)O)CC1.CNOC']

    test_df = pd.DataFrame(targets)
    test_df.columns = ['target']
    # search_results = []
    # for t in tqdm(test_df['target']): search_results.append([do_one(t)])
    # search_results = np.array([do_one(t) for t in test_df['target']])
    # print([len(x) for x in search_results])

    total = len(test_df)

    print('Reading predictions from file ...')
    with open(opt.predictions, 'r') as f:
        # lines = f.readlines()
        # lines = [''.join(x.strip().split()[1:]) for x in lines]
        # print(lines[1])
        for i, line in enumerate(f.readlines()):
            # if i ==800*10:
            #     break
            predictions[i % opt.beam_size].append(''.join(line.strip().split(' ')))

    print('Computing ranks ...')
    reranking_score = np.zeros(shape=[total, opt.beam_size])
    # for i, preds in enumerate(predictions):
    for i, preds in enumerate(tqdm(predictions)):
        canonical_preds = [canonicalize_smiles_clear_map(x) for x in preds]
        validity_score = np.array([not (x == '') for x in canonical_preds])
        if opt.rerank:
            reranking_score[:,i] = validity_score/(i+1)
        else:
            reranking_score[:,i] = 1./(i+1)
        test_df['prediction_{}'.format(i + 1)] = preds
        test_df['canonical_prediction_{}'.format(i + 1)] = canonical_preds

    # reranking predictions
    print('Computing reranks ...')
    reranking_args = np.zeros(shape=[total, opt.beam_size], dtype = int)
    for s in range(total):
        reranking_args[s,:] = (-reranking_score[s,:]).argsort()+1

    def get_rerank_can_pred(row, pred, rerank, i):
        idx = row['{}{}'.format(rerank,i+1)]
        return row['{}{}'.format(pred, idx)]
    for i in range(opt.beam_size):
        test_df['reranking_args_{}'.format(i+1)] = reranking_args[:,i]
        test_df['reranked_can_pred_{}'.format(i+1)] = test_df.apply(lambda row: get_rerank_can_pred(row, 'canonical_prediction_', 'reranking_args_', i), axis = 1)

    # if opt.invalid_smiles:
    #     test_df['rank'] = test_df.apply(lambda row: get_rank(row, 'canonical_prediction_', opt.beam_size), axis=1)
    # else:
    #     test_df['rank'] = test_df.apply(lambda row: get_rank(row, 'prediction_', opt.beam_size), axis=1)
    test_df['rank'] = test_df.apply(lambda row: get_rank(row, 'reranked_can_pred_', opt.beam_size), axis=1)

    test_df.to_csv('surprise.csv')
    correct = 0
    invalid_smiles = 0
    print('Results:\n')
    # for i in range(1, (opt.beam_size+1)//2 + 1):
    for i in range(1, opt.beam_size + 1):
        correct += (test_df['rank'] == i).sum()
        # invalid_smiles += (test_df['canonical_prediction_{}'.format(i)] == '').sum()
        invalid_smiles += (test_df['reranked_can_pred_{}'.format(i)] == '').sum()
        if opt.invalid_smiles:
            print('Top-{}: {:.1f}% || Invalid SMILES {:.2f}%'.format(i, correct/total*100,
                                                                     invalid_smiles/(total*i)*100))
        else:
            print('Top-{}: {:.1f}%'.format(i, correct / total * 100))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='score_predictions.py',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    onmt.opts.add_md_help_argument(parser)

    parser.add_argument('-beam_size', type=int, default=10,
                       help='Beam size')
    parser.add_argument('-invalid_smiles', action="store_true",
                       help='Show %% of invalid SMILES')
    parser.add_argument('-predictions', type=str, default="experiments/final/predictions_on_Dataset_ClusterSplit_withRX_beam10.txt",
                       help="Path to file containing the predictions")
    parser.add_argument('-targets', type=str, default="data/MIT_mixed_augm_clusterSplit/tgt-valid-RX",
                       help="Path to file containing targets")

    opt = parser.parse_args()

#    opt.beam_size = 10
    opt.invalid_smiles = True
#    opt.predictions = 'experiments/results/predictions_USPTO-50K_model_step_160000.pt_on_USPTO-50K_beam10_smiles.txt'
#    opt.targets = 'data/USPTO-50K/tgt-test_smiles.txt'

    opt.rerank = True

    main(opt)


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 19 19:13:10 2022

@author: jnguy
"""

import unittest
import sys # added!
sys.path.append("..") # added!
from src import stupid_class
import numpy as np

class TestStupidClass(unittest.TestCase):

    def test_stupid_class_correct(self):
        some_val = 3
        stupidclass = stupid_class.StupidClass(some_val)
        msg = "correct"
        self.assertEquals(stupidclass.scale_size(3), 9, msg)

def compute_total_distance(path):
    """compute total sum of distance travelled from path list"""
    path_array = np.diff(np.array(path), axis=0)
    segment_distance = np.sqrt((path_array ** 2).sum(axis=1))

    return np.sum(segment_distance)

if __name__=='__main__':
    unittest.main()
    #pts = [[0, 42908],[1, 3],[1, 69],[1, 11],[0, 1379963888],[0, 1309937401],[0, 1],[0, 3],[0, 3],[0, 77]]
    pts = [[0,0],[1,1], [-3,4]]
    total_dist = compute_total_distance(pts)
    # test = np.array(pts)
    # d = np.diff(test, axis=0)
    # segdists = np.sqrt((d ** 2).sum(axis=1))

    #coords = np.array([x[0] for x in pts])
    #y_val = np.array([y[1] for y in pts])

    #squared_dist = np.sum((p1-p2)**2, axis=0)
    #z_val = [z[2] for z in node_coords]

    #total  = np.sqrt(np.square(x - x.reshape(-1,1)) + np.square(y - y.reshape(-1,1)))


text:
import numpy as np
import torch
from torch.nn.modules.module import Module
from torch.nn import ModuleList
from torch.nn import functional
from torch.nn import Dropout
from torch_geometric.nn import GCNConv

class GCN(Module):
    def __init__(self, num_of_features=4, hid_size=10, num_of_classes=11, activation=functional.relu, dropout = 0):
        super(GCN, self).__init__()
        self._layer1 = GCNConv(num_of_features, hid_size)
        self._activation = activation
        self._layer2 = GCNConv(hid_size, num_of_classes)
        self._dropout = Dropout(p=dropout)

    @staticmethod
    def adj_to_coo(adj, device):
        edges_tensor = np.vstack((adj.row, adj.col))
        return torch.tensor(edges_tensor, dtype=torch.long).to(device)

    def forward(self, x, adj):
        adj = self.adj_to_coo(adj, x.device)
        x = self._layer1(x, adj)
        z = self._activation(x)
        z = self._dropout(z)
        h = self._layer2(z, adj)
        h = self._dropout(h)
        adj=adj.to("cpu")
        x=x.to("cpu")
        return z, torch.softmax(h, dim=1)

max_stars_repo_name: AlfandaryAviv/Transitions

max_issues_repo_name: AlfandaryAviv/Transitions

max_forks_repo_name: AlfandaryAviv/Transitions


text:
import cv2
import numpy as np
import pyautogui

from tkinter import *

start_flag = True

# Magic code. I don't know how it works
def nothing (x):
    pass
#Удаляет все открытые фреймы
def destroyWindows():
    video.release()
    cv2.destroyAllWindows()

video = cv2.VideoCapture(0)

pointX = 0
pointY = 0

img = np.zeros((300, 512, 3), np.uint8)

H = 0
S = 0
V = 0
H_shag = 20
S_shag = 80
V_shag = 60

H_min = 0
H_max = 0
S_min = 0
S_max = 0
V_min = 0
V_max = 0

def ActivateController (H_, S_, V_, H_flag, S_flag, V_flag, StartMouse, height_screen, width_screen):

    global H, S, V, H_min, H_max, S_min, S_max, V_min, V_max

    #Получение видео
    _, frame = video.read()

    width_video = video.get(3)
    height_video = video.get(4)

    #Удаление шумов
    blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
    #преобразование в HSV
    hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)

    print(H_flag, S_flag, V_flag)
    if H_flag:
        H_min = H_
        H_max = H_min + H_shag
        if H_max > 255:
            H_max = 255
    else:
        H_min = 0
        H_max = 255

    if S_flag:
        S_min = S_
        S_max = S_min + S_shag
        if S_max > 255:
            S_max = 255
    else:
        S_min = 0
        S_max = 255

    if V_flag:
        V_min = V_
        V_max = V_min + V_shag
        if V_max > 255:
            V_max = 255
    else:
        V_min = 0
        V_max = 255

    lower_blue = np.array([H_min, S_min, V_min])
    upper_blue = np.array([H_max, S_max, V_max])

    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    for contour in contours:
        area = cv2.cv2.contourArea(contour)
        if area > 400:
            img2 = cv2.drawContours(frame, contour, -1, (0, 255, 3))
            rect = cv2.boundingRect(contour)
            #рисуем прямоугольник
            cv2.rectangle(frame, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 0, 255), 2)
            if StartMouse:
                MouseMove(width_screen, width_video, height_screen, height_video, rect)
                cv2.circle(frame, (pointX, pointY), 5, (0, 255, 0))

    cv2.imshow("Frame", frame)

    cv2.imshow("mask", mask)

def MouseMove(width_screen, width_video, height_screen, height_video, rect):

    global pointX, pointY

    width_video = int(width_video * 0.7)
    height_video = int(height_video * 0.7)

    # Вычисляем коэфиценты для экрана и видео
    k_width = width_screen / width_video
    k_height = height_screen / height_video

    #1 точка прямоугольника
    x1 = int(rect[0])
    y1 = int(rect[1])
    #2 точка прямоугольника
    x2 = int((rect[0] + rect[2]))
    y2 = int((rect[1] + rect[3]))

    if pointX < x1:
        pointX = x1
        if pointX < 0: pointX = 0
    elif pointX > x2:
        pointX = x2
    if pointY < y1:
        pointY = y1
        if pointY < 0: pointY = 0
    elif pointY > y2:
        pointY = y2

    pointMouse = pyautogui.position()
    pointMouseX = int(pointMouse.x)
    pointMouseY = int(pointMouse.y)

    # двигаем мышь
    pyautogui.moveTo(width_screen - (pointX * k_width - width_screen * 0.2), pointY * k_height - height_screen * 0.2)

max_stars_repo_name: TerentevVlad/Web-cam-tracker

max_issues_repo_name: TerentevVlad/Web-cam-tracker

max_forks_repo_name: TerentevVlad/Web-cam-tracker


text:
import os
import time
from collections import OrderedDict

import numpy as np
import matplotlib.pyplot as plt
import scipy.io
from scipy.spatial.transform import Rotation
from ament_index_python import get_package_share_directory

from atlas_ros.atlas_state_publisher import AtlasStatePublisher

class Interpolation():
    def __init__(self, t0, tf, ts):
        self.t0 = t0
        self.tf = tf
        self.ts = ts

    def interpolate(self, t, timeseries):
        t = np.min([np.max([t, self.t0]).item(), self.tf.item()]).item()
        index = int(np.min([np.floor(((t-self.t0) - np.remainder((t-self.t0), self.ts))/self.ts + 0.0001).item(), len(timeseries) - 1]).item())
        return timeseries[index].eval(t)

class Spline():
    def __init__(self, a=0, b=0, c=0, d=0):
        self.a = a
        self.b = b
        self.c = c
        self.d = d

    def eval(self, t):
        return self.a*t**3 + self.b*t**2 + self.c*t + self.d

    def __str__(self):
        return str(self.a) + "*t^3 + " + str(self.b) + "*t^2 + " + str(self.c) + "*t + " + str(self.d)

class Line():
    def __init__(self, m=0, b=0):
        self.m = m
        self.b = b

    def eval(self, t):
        return self.m*t+self.b

    def __str__(self):
        return str(self.m) + "*t + " + str(self.b)

if __name__ == "__main__":

    JointNameLookup = [
        "back_bkz",
        "back_bky",
        "back_bkx",
        "l_arm_shz",
        "l_arm_shx",
        "l_arm_ely",
        "l_arm_elx",
        "l_arm_wry",
        "l_arm_wrx",
        "l_arm_wry2",
        "neck_ry",
        "r_arm_shz",
        "r_arm_shx",
        "r_arm_ely",
        "r_arm_elx",
        "r_arm_wry",
        "r_arm_wrx",
        "r_arm_wry2",
        "l_leg_hpz",
        "l_leg_hpx",
        "l_leg_hpy",
        "l_leg_kny",
        "l_leg_aky",
        "l_leg_akx",
        "r_leg_hpz",
        "r_leg_hpx",
        "r_leg_hpy",
        "r_leg_kny",
        "r_leg_aky",
        "r_leg_akx"]

    traject_file = os.path.join(get_package_share_directory("atlas_ros"), "resource", "output_interpolation.mat")
    output = scipy.io.loadmat(traject_file)

    num_joints = len(JointNameLookup)
    num_states = np.shape(output.get("spline_segments"))[0]
    num_splines = np.shape(output.get("spline_segments"))[1]

    q_offset = 0
    q_dot_offset = num_joints
    r_offset = q_dot_offset + num_joints
    r_dot_offset = r_offset + 3
    r_ddot_offset = r_dot_offset + 3
    h_offset = r_ddot_offset + 3
    h_dot_offset = h_offset + 3
    c1_offset = h_dot_offset + 3
    c2_offset = c1_offset + 3
    f1_offset = c2_offset + 3
    f2_offset = f1_offset + 3
    tht_offset = f2_offset + 3

    num_states = tht_offset + 3

    offset_lookup = {
        "q_offset": q_offset,
        "q_dot_offset": q_dot_offset,
        "r_offset": r_offset,
        "r_dot_offset": r_dot_offset,
        "r_ddot_offset": r_ddot_offset,
        "h_offset": h_offset,
        "h_dot_offset": h_dot_offset,
        "c1_offset": c1_offset,
        "c2_offset": c2_offset,
        "f1_offset": f1_offset,
        "f2_offset": f2_offset,
        "tht_offset" : tht_offset,
    }

    t_total = output.get("t_total")
    timestep = output.get("timestep")
    t_knot = np.arange(0, t_total + timestep, timestep)
    t_cont = np.arange(0, t_total, timestep/1000)

    state_trajectories_cubic = OrderedDict()
    state_trajectories_linear = OrderedDict()

    spline_segments = output.get("spline_segments")
    line_segments = output.get("line_segments")
    pelvis_segments = output.get("pelvis_segments")

    for offset in ["q_offset", "q_dot_offset"]:
        for i in range(num_joints):
            spline_arr = []
            line_arr = []
            for col in range(num_splines):
                offset_index = offset_lookup.get(offset)
                spline_arr.append(Spline(
                    spline_segments[offset_index + i, col][0].item(),
                    spline_segments[offset_index + i, col][1].item(),
                    spline_segments[offset_index + i, col][2].item(),
                    spline_segments[offset_index + i, col][3].item()))

                # line_arr.append(Line(
                #     line_segments[offset_index + i, col][0].item(),
                #     line_segments[offset_index + i, col][1].item(),
                # ))

            key = JointNameLookup[i] if (offset == "q_offset") else JointNameLookup[i] +  "_vel"

            state_trajectories_cubic[key] = spline_arr
            # state_trajectories_linear[key] = line_arr

    dirs = ["x", "y", "z"]
    for offset in ["r_offset", "r_dot_offset", "r_ddot_offset", "h_offset", "h_dot_offset", "f1_offset", "f2_offset"]:
        for i in range(3):
            dir = dirs[i]
            spline_arr = []
            # line_arr = []
            for col in range(num_splines):
                offset_index = offset_lookup.get(offset)
                spline_arr.append(Spline(
                    spline_segments[offset_index + i, col][0].item(),
                    spline_segments[offset_index + i, col][1].item(),
                    spline_segments[offset_index + i, col][2].item(),
                    spline_segments[offset_index + i, col][3].item()
                ))

                # line_arr.append(Line(
                #     line_segments[offset_index + i, col][0].item(),
                #     line_segments[offset_index + i, col][1].item(),
                # ))

            key = offset[0:-6] + dir

            state_trajectories_cubic[key] = spline_arr
            # state_trajectories_linear[key] = line_arr

    dirs = ["x", "y", "z"]
    for offset in ["tht_offset"]:
        for i in range(3):
            dir = dirs[i]
            spline_arr = []
            # line_arr = []
            for col in range(num_splines):
                offset_index = offset_lookup.get(offset)
                spline_arr.append(Spline(
                    spline_segments[offset_index + i, col][0].item(),
                    spline_segments[offset_index + i, col][1].item(),
                    spline_segments[offset_index + i, col][2].item(),
                    spline_segments[offset_index + i, col][3].item()
                ))

                # line_arr.append(Line(
                #     line_segments[offset_index + i, col][0].item(),
                #     line_segments[offset_index + i, col][1].item(),
                # ))

            key = offset[0:-6] + dir

            state_trajectories_cubic[key] = spline_arr
            # state_trajectories_linear[key] = line_arr

    dirs = ["x", "y", "z"]
    for i in range(3):
        dir = dirs[i]
        spline_arr = []
        line_arr = []

        for col in range(num_splines):
            spline_arr.append(Spline(
                pelvis_segments[i, col][0].item(),
                pelvis_segments[i, col][1].item(),
                pelvis_segments[i, col][2].item(),
                pelvis_segments[i, col][3].item()
            ))

        key = "pelvis_pos_" + dir

        state_trajectories_cubic[key] = spline_arr

    Interpolator = Interpolation(0, t_total, timestep)

    cubic_approx = np.zeros([1, len(t_cont)])
    linear_approx = np.zeros([1, len(t_cont)])
    knots = np.zeros([1, len(t_knot)])

    for i in range(len(t_cont)):
        cubic_approx[0, i] = Interpolator.interpolate(t_cont[i], state_trajectories_cubic["pelvis_pos_x"])
        # linear_approx[0, i] = Interpolator.interpolate(t_cont[i], state_trajectories_linear["pelvis_pos_x"])

    for i in range(len(t_knot)):
        knots[0, i] = Interpolator.interpolate(t_knot[i], state_trajectories_cubic["pelvis_pos_x"])

    # plt.figure()
    # plt.plot(t_cont, cubic_approx[0, :])
    # plt.plot(t_cont, linear_approx[0, :], linestyle='--')
    # plt.scatter(t_knot, knots, s=10, c="k", zorder=10)
    # plt.axvline(x=t_knot[output.get("windup_end").item()], color='k', linestyle='--')
    # plt.axvline(x=t_knot[output.get("takeoff_end").item()], color='k', linestyle='--')
    # plt.axvline(x=t_knot[output.get("landing_start").item()], color='k', linestyle='--')

    # plt.show()

    start_time = time.monotonic()
    dt = 1/40
    speed_scale = 3
    ros_state_pub = AtlasStatePublisher()
    t = 0
    offset = 2
    while(1):
        t = (time.monotonic() - start_time - offset)/speed_scale

        robot_state = OrderedDict()
        robot_state["joint_pos"] = OrderedDict()
        robot_state["joint_vel"] = OrderedDict()

        for q_name in JointNameLookup:
            robot_state["joint_pos"][q_name] = Interpolator.interpolate(t, state_trajectories_cubic[q_name])
            robot_state["joint_vel"][q_name] = Interpolator.interpolate(t, state_trajectories_cubic[q_name + "_vel"])

        robot_state["base_joint_pos"] = np.array([
            Interpolator.interpolate(t, state_trajectories_cubic["pelvis_pos_x"]),
            Interpolator.interpolate(t, state_trajectories_cubic["pelvis_pos_y"]),
            Interpolator.interpolate(t, state_trajectories_cubic["pelvis_pos_z"])
            ])

        robot_state["base_joint_lin_vel"] = np.array([
            Interpolator.interpolate(t, state_trajectories_cubic["r_dot_x"]),
            Interpolator.interpolate(t, state_trajectories_cubic["r_dot_y"]),
            Interpolator.interpolate(t, state_trajectories_cubic["r_dot_z"])
            ])

        # robot_state["base_joint_quat"] = np.array([0, 0, 0, 1])
        tht_x = Interpolator.interpolate(t, state_trajectories_cubic["tht_x"])
        tht_y = Interpolator.interpolate(t, state_trajectories_cubic["tht_y"])
        tht_z = Interpolator.interpolate(t, state_trajectories_cubic["tht_z"])
        # Create rotation object from Euler tht
        rot = Rotation.from_euler('xyz', [tht_x, tht_y, tht_z])
        #Convert to quaternions and print
        rot_quat = rot.as_quat()

        robot_state["base_joint_quat"] = np.array([
            rot_quat[0],
            rot_quat[1],
            rot_quat[2],
            rot_quat[3]
       ])

        robot_state["base_joint_ang_vel"] = np.array([
            Interpolator.interpolate(t, state_trajectories_cubic["r_dot_x"]),
            Interpolator.interpolate(t, state_trajectories_cubic["r_dot_y"]),
            Interpolator.interpolate(t, state_trajectories_cubic["r_dot_z"])
            ])

        robot_state["base_com_pos"] = np.array([0, 0, 0])
        robot_state["base_com_quat"] = np.array([0, 0, 0, 1])
        robot_state["base_com_lin_vel"] = np.array([0, 0, 0])
        robot_state["base_com_ang_vel"] = np.array([0, 0, 0])

        ros_state_pub.update_robot_model(robot_state)
        ros_state_pub.publish_robot_state()

        time.sleep(dt)

reason: import numpy,import scipy,from scipy