text:
#!/usr/bin/env python

import sklearn.datasets as datasets
try:
    import sklearn.model_selection as ms
except ImportError:
    pass
import sklearn.svm as svm
import sklearn.naive_bayes as nb

import numpy as np
import pandas as pd
import pandas_ml as pdml
import pandas_ml.util.testing as tm

class TestModelSelectionBase(tm.TestCase):

    def setUp(self):
        if not pdml.compat._SKLEARN_ge_018:
            import nose
            raise nose.SkipTest()

class TestModelSelection(TestModelSelectionBase):

    def test_objectmapper(self):
        df = pdml.ModelFrame([])

        # Splitter Classes
        self.assertIs(df.model_selection.KFold, ms.KFold)
        self.assertIs(df.model_selection.GroupKFold, ms.GroupKFold)
        self.assertIs(df.model_selection.StratifiedKFold, ms.StratifiedKFold)

        self.assertIs(df.model_selection.LeaveOneGroupOut, ms.LeaveOneGroupOut)
        self.assertIs(df.model_selection.LeavePGroupsOut, ms.LeavePGroupsOut)
        self.assertIs(df.model_selection.LeaveOneOut, ms.LeaveOneOut)
        self.assertIs(df.model_selection.LeavePOut, ms.LeavePOut)

        self.assertIs(df.model_selection.ShuffleSplit, ms.ShuffleSplit)
        self.assertIs(df.model_selection.GroupShuffleSplit,
                      ms.GroupShuffleSplit)
        # self.assertIs(df.model_selection.StratifiedShuffleSplit,
        #               ms.StratifiedShuffleSplit)
        self.assertIs(df.model_selection.PredefinedSplit, ms.PredefinedSplit)
        self.assertIs(df.model_selection.TimeSeriesSplit, ms.TimeSeriesSplit)

        # Splitter Functions

        # Hyper-parameter optimizers
        self.assertIs(df.model_selection.GridSearchCV, ms.GridSearchCV)
        self.assertIs(df.model_selection.RandomizedSearchCV, ms.RandomizedSearchCV)
        self.assertIs(df.model_selection.ParameterGrid, ms.ParameterGrid)
        self.assertIs(df.model_selection.ParameterSampler, ms.ParameterSampler)

        # Model validation

    def test_objectmapper_abbr(self):
        df = pdml.ModelFrame([])

        # Splitter Classes
        self.assertIs(df.ms.KFold, ms.KFold)
        self.assertIs(df.ms.GroupKFold, ms.GroupKFold)
        self.assertIs(df.ms.StratifiedKFold, ms.StratifiedKFold)

        self.assertIs(df.ms.LeaveOneGroupOut, ms.LeaveOneGroupOut)
        self.assertIs(df.ms.LeavePGroupsOut, ms.LeavePGroupsOut)
        self.assertIs(df.ms.LeaveOneOut, ms.LeaveOneOut)
        self.assertIs(df.ms.LeavePOut, ms.LeavePOut)

        self.assertIs(df.ms.ShuffleSplit, ms.ShuffleSplit)
        self.assertIs(df.ms.GroupShuffleSplit,
                      ms.GroupShuffleSplit)
        # self.assertIs(df.ms.StratifiedShuffleSplit,
        #               ms.StratifiedShuffleSplit)
        self.assertIs(df.ms.PredefinedSplit, ms.PredefinedSplit)
        self.assertIs(df.ms.TimeSeriesSplit, ms.TimeSeriesSplit)

        # Splitter Functions

        # Hyper-parameter optimizers
        self.assertIs(df.ms.GridSearchCV, ms.GridSearchCV)
        self.assertIs(df.ms.RandomizedSearchCV, ms.RandomizedSearchCV)
        self.assertIs(df.ms.ParameterGrid, ms.ParameterGrid)
        self.assertIs(df.ms.ParameterSampler, ms.ParameterSampler)

        # Model validation

class TestSplitter(TestModelSelectionBase):

    def test_iterate(self):
        df = pdml.ModelFrame(datasets.load_iris())
        kf = df.model_selection.KFold(4, random_state=self.random_state)
        for train_df, test_df in df.model_selection.iterate(kf):
            self.assertIsInstance(train_df, pdml.ModelFrame)
            self.assertIsInstance(test_df, pdml.ModelFrame)
            self.assert_index_equal(df.columns, train_df.columns)
            self.assert_index_equal(df.columns, test_df.columns)

            self.assertTrue(df.shape[0], train_df.shape[0] + test_df.shape[1])

    def test_iterate_keep_index(self):
        df = pdml.ModelFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8],
                              'B': [1, 2, 3, 4, 5, 6, 7, 8]},
                             index='a b c d e f g h'.split(' '))
        kf = df.model_selection.KFold(3, random_state=self.random_state)
        folded = [f for f in df.model_selection.iterate(kf)]
        self.assertEqual(len(folded), 3)
        self.assert_frame_equal(folded[0][0], df.iloc[3:, :])
        self.assert_frame_equal(folded[0][1], df.iloc[:3, :])
        self.assert_frame_equal(folded[1][0], df.iloc[[0, 1, 2, 6, 7], :])
        self.assert_frame_equal(folded[1][1], df.iloc[3:6, :])
        self.assert_frame_equal(folded[2][0], df.iloc[:6, :])
        self.assert_frame_equal(folded[2][1], df.iloc[6:, :])

        folded = [f for f in df.model_selection.iterate(kf, reset_index=True)]
        self.assertEqual(len(folded), 3)
        self.assert_frame_equal(folded[0][0],
                                df.iloc[3:, :].reset_index(drop=True))
        self.assert_frame_equal(folded[0][1],
                                df.iloc[:3, :].reset_index(drop=True))
        self.assert_frame_equal(folded[1][0],
                                df.iloc[[0, 1, 2, 6, 7], :].reset_index(drop=True))
        self.assert_frame_equal(folded[1][1],
                                df.iloc[3:6, :].reset_index(drop=True))
        self.assert_frame_equal(folded[2][0],
                                df.iloc[:6, :].reset_index(drop=True))
        self.assert_frame_equal(folded[2][1],
                                df.iloc[6:, :].reset_index(drop=True))

    def test_train_test_split(self):

        df = pdml.ModelFrame(datasets.load_digits())
        self.assertIsInstance(df, pdml.ModelFrame)

        train_df, test_df = df.model_selection.train_test_split()
        self.assert_index_equal(df.columns, train_df.columns)
        self.assert_index_equal(df.columns, test_df.columns)

        self.assertTrue(df.shape[0], train_df.shape[0] + test_df.shape[1])
        self.assertTrue(df.shape[1], train_df.shape[1])
        self.assertTrue(df.shape[1], test_df.shape[1])

        self.assert_index_equal(df.columns, train_df.columns)
        self.assert_index_equal(df.columns, test_df.columns)

        df = pdml.ModelFrame(datasets.load_digits())
        df.target_name = 'xxx'

        train_df, test_df = df.model_selection.train_test_split()
        self.assert_index_equal(df.columns, train_df.columns)
        self.assert_index_equal(df.columns, test_df.columns)
        self.assertEqual(train_df.target_name, 'xxx')
        self.assertEqual(test_df.target_name, 'xxx')

    def test_train_test_split_abbr(self):

        df = pdml.ModelFrame(datasets.load_digits())
        self.assertIsInstance(df, pdml.ModelFrame)

        train_df, test_df = df.ms.train_test_split()
        self.assert_index_equal(df.columns, train_df.columns)
        self.assert_index_equal(df.columns, test_df.columns)

        self.assertTrue(df.shape[0], train_df.shape[0] + test_df.shape[1])
        self.assertTrue(df.shape[1], train_df.shape[1])
        self.assertTrue(df.shape[1], test_df.shape[1])

        self.assert_index_equal(df.columns, train_df.columns)
        self.assert_index_equal(df.columns, test_df.columns)

        df = pdml.ModelFrame(datasets.load_digits())
        df.target_name = 'xxx'

        train_df, test_df = df.ms.train_test_split()
        self.assert_index_equal(df.columns, train_df.columns)
        self.assert_index_equal(df.columns, test_df.columns)
        self.assertEqual(train_df.target_name, 'xxx')
        self.assertEqual(test_df.target_name, 'xxx')

    def test_train_test_split_keep_index(self):
        df = pdml.ModelFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8],
                              'B': [1, 2, 3, 4, 5, 6, 7, 8]},
                             index='a b c d e f g h'.split(' '))
        tr, te = df.ms.train_test_split(random_state=self.random_state)
        self.assert_frame_equal(tr, df.loc[['g', 'a', 'e', 'f', 'd', 'h']])
        self.assert_frame_equal(te, df.loc[['c', 'b']])

        tr, te = df.ms.train_test_split(random_state=self.random_state, reset_index=True)
        self.assert_frame_equal(tr, df.loc[['g', 'a', 'e', 'f', 'd', 'h']].reset_index(drop=True))
        self.assert_frame_equal(te, df.loc[['c', 'b']].reset_index(drop=True))

        df = pdml.ModelFrame({'A': [1, 2, 3, 4, 5, 6, 7, 8],
                              'B': [1, 2, 3, 4, 5, 6, 7, 8]},
                             index='a b c d e f g h'.split(' '),
                             target=[1, 2, 3, 4, 5, 6, 7, 8])
        tr, te = df.ms.train_test_split(random_state=self.random_state)
        self.assert_frame_equal(tr, df.loc[['g', 'a', 'e', 'f', 'd', 'h']])
        self.assert_numpy_array_equal(tr.target.values, np.array([7, 1, 5, 6, 4, 8]))
        self.assert_frame_equal(te, df.loc[['c', 'b']])
        self.assert_numpy_array_equal(te.target.values, np.array([3, 2]))

        tr, te = df.ms.train_test_split(random_state=self.random_state, reset_index=True)
        self.assert_frame_equal(tr, df.loc[['g', 'a', 'e', 'f', 'd', 'h']].reset_index(drop=True))
        self.assert_numpy_array_equal(tr.target.values, np.array([7, 1, 5, 6, 4, 8]))
        self.assert_frame_equal(te, df.loc[['c', 'b']].reset_index(drop=True))
        self.assert_numpy_array_equal(te.target.values, np.array([3, 2]))

    def test_cross_val_score(self):
        import sklearn.svm as svm
        digits = datasets.load_digits()

        df = pdml.ModelFrame(digits)
        clf = svm.SVC(kernel=str('linear'), C=1)
        result = df.model_selection.cross_val_score(clf, cv=5)
        expected = ms.cross_val_score(clf, X=digits.data, y=digits.target, cv=5)
        self.assert_numpy_array_almost_equal(result, expected)

    def test_permutation_test_score(self):
        import sklearn.svm as svm
        iris = datasets.load_iris()

        df = pdml.ModelFrame(iris)
        clf = svm.SVC(kernel=str('linear'), C=1)
        result = df.model_selection.permutation_test_score(clf, cv=5)
        expected = ms.permutation_test_score(clf, iris.data, y=iris.target, cv=5)

        self.assertEqual(len(result), 3)
        self.assertEqual(result[0], expected[0])
        self.assert_numpy_array_almost_equal(result[1], expected[1])
        self.assertEqual(result[2], expected[2])

    def test_check_cv(self):
        iris = datasets.load_iris()

        df = pdml.ModelFrame(iris)
        result = df.model_selection.check_cv(cv=5)
        self.assertIsInstance(result, ms.KFold)

    def test_StratifiedShuffleSplit(self):
        iris = datasets.load_iris()
        df = pdml.ModelFrame(iris)
        sf1 = df.model_selection.StratifiedShuffleSplit(random_state=self.random_state)
        sf2 = ms.StratifiedShuffleSplit(random_state=self.random_state)

        # consume generator
        ind1 = [x for x in sf1.split(df.data.values, df.target.values)]
        ind2 = [x for x in sf2.split(iris.data, iris.target)]

        for i1, i2 in zip(ind1, ind2):
            self.assertIsInstance(i1, tuple)
            self.assertEqual(len(i1), 2)
            self.assertIsInstance(i2, tuple)
            self.assertEqual(len(i2), 2)
            self.assert_numpy_array_equal(i1[0], i1[0])
            self.assert_numpy_array_equal(i1[1], i2[1])

        sf1 = df.model_selection.StratifiedShuffleSplit(random_state=self.random_state)
        with tm.assert_produces_warning(UserWarning):
            # StratifiedShuffleSplit is not a subclass of BaseCrossValidator
            for train_df, test_df in df.model_selection.iterate(sf1):
                self.assertIsInstance(train_df, pdml.ModelFrame)
                self.assertIsInstance(test_df, pdml.ModelFrame)
                self.assert_index_equal(df.columns, train_df.columns)
                self.assert_index_equal(df.columns, test_df.columns)

                self.assertTrue(df.shape[0], train_df.shape[0] + test_df.shape[1])

    def test_nested_cross_validation(self):
        # http://scikit-learn.org/stable/auto_examples/model_selection/plot_nested_cross_validation_iris.html#sphx-glr-auto-examples-model-selection-plot-nested-cross-validation-iris-py

        # Number of random trials
        NUM_TRIALS = 30

        # Load the dataset
        iris = datasets.load_iris()
        X_iris = iris.data
        y_iris = iris.target

        p_grid = {"C": [1, 10, 100],
                  "gamma": [.01, .1]}

        svr = svm.SVC(kernel="rbf")
        expected = np.zeros(NUM_TRIALS)

        for i in range(NUM_TRIALS):
            inner_cv = ms.KFold(n_splits=4, shuffle=True, random_state=i)
            outer_cv = ms.KFold(n_splits=4, shuffle=True, random_state=i)

            clf = ms.GridSearchCV(estimator=svr, param_grid=p_grid, cv=inner_cv)
            clf.fit(X_iris, y_iris)

            nested_score = ms.cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv)
            expected[i] = nested_score.mean()

        df = pdml.ModelFrame(datasets.load_iris())
        svr = df.svm.SVC(kernel="rbf")

        result = np.zeros(NUM_TRIALS)

        for i in range(NUM_TRIALS):
            inner_cv = df.ms.KFold(n_splits=4, shuffle=True, random_state=i)
            outer_cv = df.ms.KFold(n_splits=4, shuffle=True, random_state=i)

            clf = df.ms.GridSearchCV(estimator=svr, param_grid=p_grid, cv=inner_cv)
            df.fit(clf)

            nested_score = df.ms.cross_val_score(clf, cv=outer_cv)
            result[i] = nested_score.mean()

        tm.assert_numpy_array_equal(result, expected)

class TestHyperParameterOptimizer(TestModelSelectionBase):

    def test_grid_search(self):
        tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
                             'C': [1, 10, 100]},
                            {'kernel': ['linear'], 'C': [1, 10, 100]}]

        df = pdml.ModelFrame(datasets.load_digits())
        cv = df.model_selection.GridSearchCV(df.svm.SVC(C=1), tuned_parameters, cv=5)

        with tm.RNGContext(1):
            df.fit(cv)

        result = df.model_selection.describe(cv)
        expected = pd.DataFrame({'mean': [0.97161937, 0.9476906, 0.97273233, 0.95937674, 0.97273233,
                                          0.96271564, 0.94936004, 0.94936004, 0.94936004],
                                 'std': [0.01546977, 0.0221161, 0.01406514, 0.02295168, 0.01406514,
                                         0.01779749, 0.01911084, 0.01911084, 0.01911084],
                                 'C': [1, 1, 10, 10, 100, 100, 1, 10, 100],
                                 'gamma': [0.001, 0.0001, 0.001, 0.0001, 0.001, 0.0001,
                                           np.nan, np.nan, np.nan],
                                 'kernel': ['rbf'] * 6 + ['linear'] * 3},
                                columns=['mean', 'std', 'C', 'gamma', 'kernel'])
        self.assertIsInstance(result, pdml.ModelFrame)
        self.assert_frame_equal(result, expected)

class TestModelValidation(TestModelSelectionBase):

    def test_learning_curve(self):
        digits = datasets.load_digits()
        df = pdml.ModelFrame(digits)

        result = df.learning_curve.learning_curve(df.naive_bayes.GaussianNB())
        expected = ms.learning_curve(nb.GaussianNB(), digits.data, digits.target)

        self.assertEqual(len(result), 3)
        self.assert_numpy_array_almost_equal(result[0], expected[0])
        self.assert_numpy_array_almost_equal(result[1], expected[1])
        self.assert_numpy_array_almost_equal(result[2], expected[2])

    def test_validation_curve(self):
        digits = datasets.load_digits()
        df = pdml.ModelFrame(digits)

        param_range = np.logspace(-2, -1, 2)

        svc = df.svm.SVC(random_state=self.random_state)
        result = df.model_selection.validation_curve(svc, 'gamma',
                                                     param_range)
        expected = ms.validation_curve(svm.SVC(random_state=self.random_state),
                                       digits.data, digits.target,
                                       'gamma', param_range)

        self.assertEqual(len(result), 2)
        self.assert_numpy_array_almost_equal(result[0], expected[0])
        self.assert_numpy_array_almost_equal(result[1], expected[1])

max_stars_repo_name: wangyum/anaconda

max_issues_repo_name: wangyum/anaconda

max_forks_repo_name: wangyum/anaconda


text:
import numpy as np

from jive.lazymatpy.templates.ones import Ones, OnesOuterVec
from jive.lazymatpy.templates.tests.help_fun import get_tst_mats

from numpy.testing import assert_allclose

class TestOnes(object):

    def test_basic(self):
        # shape = (10, 5)

        shapes = [(10, 5), (1, 5), (10, 1)]
        for shape in shapes:

            A = Ones(shape)
            M = np.ones(shape)

            V = np.random.normal(size=(shape[1], 3))
            v1 = np.random.normal(size=(shape[1], ))
            v2 = np.random.normal(size=(shape[1], 1))

            U = np.random.normal(size=(shape[0], 3))
            u1 = np.random.normal(size=(shape[0], ))
            u2 = np.random.normal(size=(shape[0], 1))

            assert_allclose(A.dot(V), M.dot(V))
            assert_allclose(A.dot(v1), M.dot(v1))
            assert_allclose(A.dot(v2), M.dot(v2))

            assert_allclose(A.T.dot(U), M.T.dot(U))
            assert_allclose(A.T.dot(u1), M.T.dot(u1))
            assert_allclose(A.T.dot(u2), M.T.dot(u2))

class TestOnesOuterVec(object):

    def test_basic(self):
        vecs = [[1, 2, 3],
                [2],
                np.array([1, 2, 3]).reshape(1, -1),
                np.array([1, 2, 3]).reshape(-1, 1)]

        num_ones_list = [4, 1]

        for vec in vecs:
            for num_ones in num_ones_list:

                A = OnesOuterVec(num_ones=num_ones, vec=vec)
                M = np.outer([1]*num_ones, vec)

                V, v1, v2, U, u1, u2 = get_tst_mats(M.shape)

                assert_allclose(A.dot(V), M.dot(V))
                assert_allclose(A.dot(v1), M.dot(v1))
                assert_allclose(A.dot(v2), M.dot(v2))

                assert_allclose(A.T.dot(U), M.T.dot(U))
                assert_allclose(A.T.dot(u1), M.T.dot(u1))
                assert_allclose(A.T.dot(u2), M.T.dot(u2))

ext: py

reason: import numpy,from numpy


text:
"""
This module computes the lower level of the hierarchical model as described in Sarkis + 2020.
The model computes the internal luminosity of the planet
given the planet mass, radius, stellar luminosity, and semimajor axis.
We also assume that the distribution of the heavy elements
is based on the relation between the planet-mass -- heavy element mass of Thorngren+2016.

"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time as tt
import os

import emcee

from . import physical_params as pp
from . import plots
from . import utils
from . import relations

class SinglePlanetModel:
    def __init__(self, data,
                lumi_linear=True,
                name=None, outdir=None,
                interp_func_radius=None, interp_func_tint=None,
                interp_func_prcb=None, interp_func_trcb=None,
                nwalkers=50, nburn=500, nsteps=500, thin=1,
                verbose=True, progress=True,
                sample_prior=False,
                save_chains=True,
                diagnostic_plots=True, save_plots=True, \
                show_traces=False):

        self.data               = data
        self.name               = name
        self.outdir             = outdir

        self.interp_func_radius = utils.read_interp_func('data/interpFn_radius')
        self.interp_func_tint   = utils.read_interp_func('data/interpFn_tint')
        self.interp_func_prcb   = utils.read_interp_func('data/interpFn_prcb')
        self.interp_func_trcb   = utils.read_interp_func('data/interpFn_trcb')

        self.nwalkers           = nwalkers
        self.nburn              = nburn
        self.nsteps             = nsteps
        self.thin               = thin

        self.verbose            = verbose
        self.progress           = progress
        self.sample_prior       = sample_prior
        self.diagnostic_plots   = diagnostic_plots
        self.save_chains        = save_chains
        self.save_plots         = save_plots
        self.show_traces        = show_traces

        self.ln_normal_func = relations.ln_normal

        self.lumi_linear = lumi_linear
        if lumi_linear:
            self.lumi_lower_bound = 1
            self.lumi_upper_bound = 1e5
            self.labels = ['Lumi', 'Mstar', 'Mp', 'Zhomo']
        else:
            self.lumi_lower_bound = np.log10(1)
            self.lumi_upper_bound = np.log10(1e5)
            self.labels = ['Log10(Lumi)', 'Mstar', 'Mp', 'Zhomo']

        ## values from Thorngren+2016 in MJ
        self.alpha = 57.9/317.828
        self.beta  = 0.61
        self.sigma = 10**1.82/317.828

        if self.outdir is None:
            self.outdir = self.data['System'] + '/'
        else:
            self.outdir = outdir

        self._get_identifier()

    def _get_identifier(self):
        if self.lumi_linear:
            aa = 'lumilinear'
        else:
            aa = 'lumilog'

        if self.sample_prior:
            bb = 'prior'
        else:
            bb = 'posterior'

        if self.name is None:
            self.identifier = '{0}_{1}_{2}'.format(self.data['System'], aa, bb)
        else:
            self.identifier = '{0}_{1}_{2}_{3}'.format(self.data['System'], self.name, aa, bb)

    def opening_info(self):
        if self.verbose:
            print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
            print('           {0}'.format(self.identifier))
            print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
            print('')

            print('Running MCMC -- {0} walkers -- {1} nburn -- {2} nsteps ... '\
                    .format(self.nwalkers, self.nburn, self.nsteps))
            print('')

    def _mass_heavy_element_mass_relation(self, theta):
        '''
        Computes the fraction of heavy elements based on
        the relation estimated in Thorngren+2016
        '''
        _, _, mp_true, _ = theta

        self.z_relation = self.alpha * mp_true ** (self.beta - 1)

    def lnprior(self, theta):
        '''
        Computes the prior probability in log
        '''

        lumi, mstar_true, mp_true, zenv_true = theta

        lprior = 0.

        # lumi prior
        if (lumi >= self.lumi_lower_bound and lumi <= self.lumi_upper_bound):
                lprior =+ 0.
        else:
            lprior = -np.inf

        lprior += self.ln_normal_func(mstar_true, self.data['Mstar_completo'], self.data['Mstarerr_completo'])
        lprior += self.ln_normal_func(mp_true, self.data['Mp'], self.data['Mperr'])

        self._mass_heavy_element_mass_relation(theta)
        lprior += self.ln_normal_func(zenv_true, self.z_relation, self.sigma)
        # lprior += self.ln_normal_func(zenv_true, self.data['Zhomo'], 0.05)

        return lprior

    def lnlike(self, radius_theoretical):
        '''
        Computes the likelihood probability in log
        '''

        return self.ln_normal_func(radius_theoretical, self.data['Rp'], self.data['Rperr'])

    def lnprob(self, theta, sample_prior=False):
        '''
        Computes the posterior distribution
        if sample_prior is True, then only sample the prior by ignoring the likelihood
        This is useful if we want to sample the prior to check the prior distributions.
        '''

        if not np.all(np.isfinite(theta)):
            return -np.inf, -np.inf, -np.inf, -np.inf

        lumi, mstar_true, mp_true, zenv_true = theta

        # parameter space where the interpolation function is defined
        if self.lumi_lower_bound <= lumi <= self.lumi_upper_bound and \
            0.4 <= mstar_true < 1.90 and 0.3 <= mp_true < 13.0 and \
            0 <= zenv_true <= 0.95 and 0.01 < self.data['sma'] < 0.1:

            if self.lumi_linear:
                lumi_true = lumi
            else:
                lumi_true = 10**(lumi)

            # first calculate tint
            tint = self.interp_func_tint((mp_true, zenv_true, mstar_true, lumi_true, self.data['sma']))

            if tint > 1000:
                return -np.inf, -np.inf, -np.inf, -np.inf

            else:
                radius_theoretical = self.interp_func_radius((mp_true, zenv_true, mstar_true, lumi_true, self.data['sma']))
                epsilon = pp.lumi_to_epsilon(lumi_true, radius_theoretical, self.data['sma'], self.data['Tstar'], self.data['Rstar'])

                if epsilon > 5:
                    return -np.inf, -np.inf, -np.inf, -np.inf
                else:
                    if not sample_prior:
                        lprob = self.lnprior(theta) + self.lnlike(radius_theoretical)
                    else:
                        lprob = self.lnprior(theta)

                return lprob, epsilon, radius_theoretical, tint

        else:
            return -np.inf, -np.inf, -np.inf, -np.inf

    def run_mcmc(self):
        '''
        Runs MCMC to the data given and returns the samples as an object
        '''
        if self.verbose:
            self.opening_info()

        # setup the initial guess for the MCMC sampler
        if self.lumi_linear:
            zenv_ig = self.alpha * self.data['Mp'] ** (self.beta - 1) # MJ / MJ
            lumi_ig = utils.lumi_given_str(self.data['Mp'], zenv_ig, self.data['Mstar_completo'], \
                    self.data['sma'], self.data['Rp'], self.interp_func_radius)
            initial_guess = np.array([ lumi_ig, self.data['Mstar_completo'], self.data['Mp'], zenv_ig ])
        else:
            initial_guess = np.array([ np.log10(100), self.data['Mstar_completo'], self.data['Mp'], 0.5 ])

        ndim = len(initial_guess)

        # check that the initial guess makes sense
        # otherwise, fix it until all the parameters
        # are in the accepted parameter space
        if self.lumi_linear:
            zenv_ig = initial_guess[3]
            lumi_ig = initial_guess[0]

            tint_ig      = self.interp_func_tint((self.data['Mp'], zenv_ig, self.data['Mstar_completo'], lumi_ig, self.data['sma']))
            radius_ig    = self.interp_func_radius((self.data['Mp'], zenv_ig, self.data['Mstar_completo'], lumi_ig, self.data['sma']))
            epsilon_ig   = pp.lumi_to_epsilon(lumi_ig, radius_ig, self.data['sma'], self.data['Tstar'], self.data['Rstar'])

            while (epsilon_ig > 4.5) or (tint_ig > 900):
                lumi_ig    = lumi_ig - 50

                tint_ig    = self.interp_func_tint((self.data['Mp'], zenv_ig, self.data['Mstar_completo'], lumi_ig, self.data['sma']))
                radius_ig  = self.interp_func_radius((self.data['Mp'], zenv_ig, self.data['Mstar_completo'], lumi_ig, self.data['sma']))
                epsilon_ig = pp.lumi_to_epsilon(lumi_ig, radius_ig, self.data['sma'], self.data['Tstar'], self.data['Rstar'])

                if self.verbose:
                    print('fixing initial guess for', self.identifier, ' ... ', epsilon_ig, lumi_ig, tint_ig)

                initial_guess = np.array([ lumi_ig, self.data['Mstar_completo'], self.data['Mp'], zenv_ig ])

        # Initialize the "walkers" in a ball around some random initial guess
        p0 = np.vstack( [initial_guess + 1e-8 * np.random.randn(self.nwalkers, ndim)] )

        # Set up blobs and the sampler
        dtype = [("epsilon", float), ("radius", float), ("tint", float)]
        sampler = emcee.EnsembleSampler(self.nwalkers, ndim, self.lnprob, args=(self.sample_prior,), blobs_dtype=dtype)

        if self.verbose:
            print("Running burn-in ...")

        # run burn-in
        pos, lp, _, _ = sampler.run_mcmc(p0, self.nburn, progress=self.progress)

        print('Acceptance ratio in the burn-in stage ... {0:.4f}'.format(np.mean(sampler.acceptance_fraction)))
        print('')

        if self.diagnostic_plots:
            plots.trace_plot(sampler.chain, self.labels, outfile=self.identifier, save_plots=False, show_traces=self.show_traces)
            plt.suptitle('{0} -- burn-in'.format(self.identifier))
            if self.save_plots:
                plt.savefig(self.outdir + '{0}_burnin.pdf'.format(self.identifier))
                plt.close()

        # reset sampler and run production chain
        if self.verbose:
            print("Running production chain...")

        sampler.reset()
        sampler.run_mcmc(pos, self.nsteps, thin=self.thin, progress=self.progress)

        print('Acceptance ratio {0}'.format(np.mean(sampler.acceptance_fraction)))

        if self.verbose:
            print('MCMC DONE !')

        if self.diagnostic_plots:
            plots.trace_plot(sampler.chain, self.labels,
                    outdir = self.outdir, outfile=self.identifier, \
                    save_plots=self.save_plots, show_traces=self.show_traces)
            plt.close()

        self.sampler = sampler

        self.prepare_samples()

        if self.save_chains:
            self.save_samples()

        if self.show_traces:
            plt.show()

        return self.dataset

    def prepare_samples(self):
        # get blobs
        blobs = self.sampler.get_blobs(flat=True, thin=self.thin)

        # flatten the samples
        chains = self.sampler.get_chain(flat=True, thin=self.thin)

        # derive other parameters:
        # Prcb, Trcb, Lstar, Teq
        if self.lumi_linear:
                lumi = chains[:,0]
        else:
            lumi = 10**(chains[:,0])

        Prcb = self.interp_func_prcb((chains[:,2], chains[:,3], chains[:,1], lumi, self.data['sma']))
        Trcb = self.interp_func_trcb((chains[:,2], chains[:,3], chains[:,1], lumi, self.data['sma']))

        Teq, Lstar = pp.stellarparams_completo(chains[:,1], self.data['sma'])

        dataset = pd.DataFrame({
            'Lumi'   : chains[:, 0],
            'Mstar'  : chains[:, 1],
            'Mp'     : chains[:, 2],
            'Zhomo'  : chains[:, 3],
            'Epsilon': blobs['epsilon'],
            'Rp'     : blobs['radius'],
            'Lstar'  : Lstar,
            'Teq'    : Teq,
            'Tint'   : blobs['tint'],
            'Prcb'   : Prcb,
            'Trcb'   : Trcb,
            'lnProba': self.sampler.get_log_prob(flat=True, thin=self.thin)
            })

        self.dataset = dataset

    def save_samples(self):
        self.dataset.to_csv(self.outdir + '{0}_chains.csv'.format(self.identifier))

        if self.verbose:
            print('Saving samples ... DONE !')

max_stars_repo_name: psarkis/bloatedHJs

max_issues_repo_name: psarkis/bloatedHJs

max_forks_repo_name: psarkis/bloatedHJs

reason: import numpy


text:
# Copyright 2017-2021 Lawrence Livermore National Security, LLC and other
# CallFlow Project Developers. See the top-level LICENSE file for details.
#
# SPDX-License-Identifier: MIT

"""
CallFlow operation to calculate 2-dimensional projections based on run's parameter information.
"""

import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.manifold import TSNE, MDS
from sklearn.cluster import KMeans

from callflow.algorithms import KMedoids
from callflow.datastructures.metrics import TIME_COLUMNS

class ParameterProjection:
    """
    Calculate Parameter projections using common projection techniques like MDS, t-SNE.
    """

    def __init__(self, sg, selected_runs=None, n_cluster=3):
        """
        Constructor for the

        :param sg:
        :param selected_runs:
        :param n_cluster:
        """
        assert len(selected_runs) > 0
        assert isinstance(n_cluster, int)

        self.projection = "MDS"
        self.clustering = "k_means"
        self.n_cluster = int(n_cluster)
        self.proxy_columns = sg.proxy_columns
        self.time_columns = [self.proxy_columns.get(_, _) for _ in TIME_COLUMNS]

        if len(selected_runs) >= self.n_cluster:
            self.result = self.compute(sg, selected_runs)
        else:
            self.result = pd.DataFrame({})

    def add_df_params(self, sg, dataset):
        """
        Add information from the df about the dataset.
        :param dataset: dataset tag
        :return: dict comprising of "max_inclusive_time", "max_exclusive_time", and "rank_count".
        """
        # TODO: Research what more properties can be appended to the dataframe.
        ret = {}
        ret["max_inclusive_time"] = sg.dataframe.loc[
            sg.dataframe["dataset"] == dataset
        ][self.time_columns[0]].mean()
        ret["max_exclusive_time"] = sg.dataframe.loc[
            sg.dataframe["dataset"] == dataset
        ][self.time_columns[1]].mean()
        ret["rank_count"] = len(
            sg.dataframe.loc[sg.dataframe["dataset"] == dataset]["rank"].unique()
        )
        return ret

    def compute(self, sg, selected_runs):
        """
        Compute the 2D projection of the the provided data after processing.

        :return: (Dict) {
            "dataset:
            "
        }
        """
        rows = []
        for dataset in selected_runs:
            df_params = self.add_df_params(sg, dataset)
            rows.append(df_params)

        df = pd.DataFrame(rows)

        # TODO: Remove all string columns from the dataframe.
        if "dataset" in df.columns:
            df = df.drop(columns=["dataset"])
        x = df.values  # returns a numpy array

        # Scale the values to value between 0 to 1
        min_max_scaler = preprocessing.MinMaxScaler()
        x_scaled = min_max_scaler.fit_transform(x)
        df = pd.DataFrame(x_scaled)
        X = np.vstack([df.values])
        # X = np.vstack([df.values.tolist()])

        random_number = 20150101
        if self.projection == "MDS":
            proj = MDS(random_state=random_number).fit_transform(X)

        elif self.projection == "TSNE":
            proj = TSNE(random_state=random_number).fit_transform(X)

        ret = pd.DataFrame(proj, columns=list("xy"))
        ret["dataset"] = selected_runs

        if self.clustering == "k_medoids":
            self.clusters = KMedoids(n_cluster=self.n_cluster)
            ret["label"] = self.clusters.fit(X)
        elif self.clustering == "k_means":
            self.clusters = KMeans(
                n_clusters=self.n_cluster, random_state=random_number
            )
            ret["label"] = self.clusters.fit(X).labels_

        return ret

max_stars_repo_name: LLNL/CallFlow

max_issues_repo_name: LLNL/CallFlow

max_forks_repo_name: LLNL/CallFlow


text:
from .._database.collection import Crud
import numpy as np

class Statistics:

    def __init__(self, measurements):
        super(Statistics, self).__init__()

        self._collected_measurements = measurements

    @property
    def raw_data(self) -> list:
        """

        Returns
        -------

        """
        return self._collected_measurements

    @property
    def normalized_response_times(self) -> list:
        """

        Returns
        -------

        """
        measurements = np.array(self._collected_measurements)
        return measurements[abs(measurements - np.mean(measurements)) < 2 * np.std(measurements)]

    @property
    def average_response_time(self) -> float:
        """

        Returns
        -------

        """
        return float(sum(self._collected_measurements) / len(self._collected_measurements))

    @property
    def maximum_outlier(self) -> float:
        """

        Returns
        -------

        """
        return float(max(self._collected_measurements))

    @property
    def minimum_outlier(self) -> float:
        """

        Returns
        -------

        """
        return float(min(self._collected_measurements))

    @property
    def percentile_5th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 5))

    @property
    def percentile_10th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 10))

    @property
    def percentile_15th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 15))

    @property
    def percentile_20th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 20))

    @property
    def percentile_25th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 25))

    @property
    def percentile_30th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 30))

    @property
    def percentile_35th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 35))

    @property
    def percentile_40th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 40))

    @property
    def percentile_45th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 45))

    @property
    def percentile_50th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 50))

    @property
    def percentile_55th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 55))

    @property
    def percentile_60th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 60))

    @property
    def percentile_65th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 65))

    @property
    def percentile_70th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 70))

    @property
    def percentile_75th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 75))

    @property
    def percentile_80th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 80))

    @property
    def percentile_85th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 85))

    @property
    def percentile_90th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 90))

    @property
    def percentile_95th(self) -> float:
        """

        Returns
        -------

        """
        return float(np.percentile(np.array(self._collected_measurements), 95))

class CodePaths(Crud):

    def __init__(self):
        super().__init__()

        self._matched_code_path = None

    def _map_out_hierarchical_stack_relationships(self, url: str, tcn: str, sample_id: str) -> dict:
        """
        Will map out the parent child relationships for each function to form hierarchical data structure.
        This structure can than be used to generate D3 flame graphs.

        (Function uses recursion to travel through the hierarchical
        JSON stack until no more row in the collected stack trace can be found.)

        :param url: The _database connection url
        :param tcn: The test case name
        :param sample_id: The found sample id
        :return: An hierarchical data structure in JSON format.
        """
        stack = {}
        collected_stack = self.select_benchmark_call_stack_by_sample_id(url, tcn, sample_id)
        for line in collected_stack:

            if line["parent_function_name"] == collected_stack[0]['parent_function_name']:
                stack["name"] = line["parent_function_name"]
                stack["children"] = [
                    {
                        "name": line["child_function_name"],
                        "children": []
                    }
                ]

            else:
                self._recursively_update_number_of_parent_child_relationship(
                    stack=stack,
                    parent=line['parent_function_name'],
                    child=line['child_function_name']
                )
        return stack

    def _recursively_update_number_of_parent_child_relationship(self, stack: dict, parent: str, child: str) -> None:
        """
        Helps map out the call stack by extending or updating the
        hierarchical stack with new members.

        (Function is recursive until there are no more objects in the stack.)

        :param stack: The hierarchical JSON call stack.
        :param parent: The name of the parent function.
        :param child: The name of the child function.
        """
        if stack['name'] == parent:
            stack['children'].append(
                {
                    "name": child,
                    "children": []
                }
            )

        else:
            for item in stack['children']:
                self._recursively_update_number_of_parent_child_relationship(item, parent, child)

    def _recursively_search_hierarchical_stack(self, hierarchical_stack, parent, child, history):
        """

        :param hierarchical_stack:
        :param parent:
        :return:
        """
        if len(hierarchical_stack["children"]) != 0 and hierarchical_stack["name"] not in history:
            history.append(hierarchical_stack["name"])
        if hierarchical_stack["name"] == parent:
            matched_code_path = history
            matched_code_path.append(child)
            return matched_code_path

        else:
            for current_point_in_stack in hierarchical_stack["children"]:
                match = self._recursively_search_hierarchical_stack(
                    current_point_in_stack,
                    parent,
                    child,
                    history
                )
                if match is not None:
                    return match

                else:
                    continue

max_stars_repo_name: JoeyHendricks/PyBench

max_issues_repo_name: JoeyHendricks/PyBench

max_forks_repo_name: JoeyHendricks/python-micro-benchmarks


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 20 15:39:01 2015

@author: dataquanty
"""

import numpy as np
import pandas as pd
from vectorizer import vectorize
import scipy
from sklearn.grid_search import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from operator import itemgetter
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.metrics import mean_squared_error

mat = pd.read_csv('data.csv',parse_dates=['quote_date'])

mat.drop('tube_assembly_id',inplace=True,axis=1)
mat['year']=mat['quote_date'].apply(lambda row: row.year)
mat['month']=mat['quote_date'].apply(lambda row: row.month)
mat.drop('quote_date',inplace=True,axis=1)
mat['quantity_tr']=np.sqrt(np.log(mat['quantity']))

cols = ['material_id','supplier']
vectorSpec = vectorize(mat,cols,0.001)
mat = pd.concat([mat,vectorSpec],axis=1)
mat.drop(cols,axis=1,inplace=True)

colRemoved = ['cost','id']
cols = mat.drop(colRemoved,axis=1).columns
mat[cols]=mat[cols].fillna(0)
#mat[cols]=np.nan_to_num(np.array(mat[cols]))

"""
for c in cols:
    try:
        mat[c]=pd.cut(mat[c],bins=32,labels=False)
    except:
        print c

for c in cols:
    try:
        mat[c]=mat[c].astype(np.int8)
    except:
        print c
"""
cols = ['end_a','end_x']
for c in cols:
    mat[c]=mat[c].apply(lambda row: hash(row))

X = mat[np.isnan(mat['id'])].drop(colRemoved,axis=1)
Y = mat[np.isnan(mat['id'])]['cost']
Y = np.log1p(Y)
Xtest = mat[~np.isnan(mat['id'])].drop(colRemoved,axis=1)

X, Y = shuffle(X,Y)

offset = int(X.shape[0] * 0.8)
X_train, y_train = X[:offset], Y[:offset]
X_test, y_test = X[offset:], Y[offset:]

n_est = 200

#test_score_roc = np.zeros((n_est,), dtype=np.float64)
test_score = np.zeros((n_est,), dtype=np.float64)
#train_score = np.zeros((n_est,), dtype=np.float64)

params = {'n_estimators': n_est,
          'max_depth': 12,
          'min_samples_split': 1,
          'learning_rate': 0.05,
          'subsample':0.9,
#          'max_features':'sqrt'
          }

clf = GradientBoostingRegressor(**params)

clf.fit(X_train, y_train)

print np.sqrt(mean_squared_error(y_test,clf.predict(X_test)))

"""
train_score[i:(i+1)*10] = (mean_squared_error(y_train,clf.predict(X_train)))
test_score_roc[i:(i+1)*10] = (mean_squared_error(y_test, clf.predict(X_test)))
print("iteration ",i*10)

np.sqrt(mean_squared_error(Y,clf.predict(X)))

plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.title('Area under ROC curve')
plt.plot(np.arange(n_est) + 1, train_score, 'b-',
         label='Training Set ROC')
plt.plot(np.arange(n_est) + 1, test_score_roc, 'r-',
         label='Test Set ROC')
plt.legend(loc='lower right')
plt.xlabel('Boosting Iterations')
plt.ylabel('Deviance')
"""

test_score = np.zeros((params['n_estimators'],), dtype=np.float64)

for i, y_pred in enumerate(clf.staged_decision_function(X_test)):
    test_score[i] = clf.loss_(y_test, y_pred)

plt.subplot(1, 1, 1)
plt.title('Deviance')
plt.plot(np.arange(params['n_estimators']) + 1, clf.train_score_, 'b-',
         label='Training Set Deviance')
plt.plot(np.arange(params['n_estimators']) + 1, test_score, 'r-',
         label='Test Set Deviance')
plt.legend(loc='upper right')
plt.xlabel('Boosting Iterations')
plt.ylabel('Deviance')

cols = np.array(mat.drop(colRemoved,axis=1).columns)
importance = clf.feature_importances_
featImport = pd.concat((pd.DataFrame(cols),pd.DataFrame(importance)),axis=1)
featImport.columns=['f','v']
featImport.sort('v',ascending=False,inplace=True)
featImport.set_index('f',inplace=True)
featImport.plot(kind='bar')
plt.subplots_adjust(bottom = 0.3)
plt.show()

max_stars_repo_name: dataquanty/Caterpillar

max_issues_repo_name: dataquanty/Caterpillar

max_forks_repo_name: dataquanty/Caterpillar

reason: import numpy,import scipy


text:
# coding: utf-8

# # Principal Component Regression (PCR) on Sensory and Fluorescence data

# This notebook illustrates how to use the **hoggorm** package to carry out principal component regression (PCR) on multivariate data. Furthermore, we will learn how to visualise the results of the PCR using the **hoggormPlot** package.

# ---

# ### Import packages and prepare data

# First import **hoggorm** for analysis of the data and **hoggormPlot** for plotting of the analysis results. We'll also import **pandas** such that we can read the data into a data frame. **numpy** is needed for checking dimensions of the data.

# In[1]:

import hoggorm as ho
import hoggormplot as hop
import pandas as pd
import numpy as np

# Next, load the data that we are going to analyse using **hoggorm**. After the data has been loaded into the pandas data frame, we'll display it in the notebook.

# In[2]:

# Load fluorescence data
X_df = pd.read_csv('cheese_fluorescence.txt', index_col=0, sep='\t')
X_df

# In[3]:

# Load sensory data
Y_df = pd.read_csv('cheese_sensory.txt', index_col=0, sep='\t')
Y_df

# The ``nipalsPCR`` class in hoggorm accepts only **numpy** arrays with numerical values and not pandas data frames. Therefore, the pandas data frames holding the imported data need to be "taken apart" into three parts:
# * two numpy array holding the numeric values
# * two Python list holding variable (column) names
# * two Python list holding object (row) names.
#
# The numpy arrays with values will be used as input for the ``nipalsPCR`` class for analysis. The Python lists holding the variable and row names will be used later in the plotting function from the **hoggormPlot** package when visualising the results of the analysis. Below is the code needed to access both data, variable names and object names.

# In[5]:

# Get the values from the data frame
X = X_df.values
Y = Y_df.values

# Get the variable or columns names
X_varNames = list(X_df.columns)
Y_varNames = list(Y_df.columns)

# Get the object or row names
X_objNames = list(X_df.index)
Y_objNames = list(Y_df.index)

# ---

# ### Apply PCR to our data

# Now, let's run PCR on the data using the ``nipalsPCR`` class. The documentation provides a [description of the input parameters](https://hoggorm.readthedocs.io/en/latest/pcr.html). Using input paramter ``arrX`` and ``arrY`` we define which numpy array we would like to analyse. ``arrY`` is what typically is considered to be the response matrix, while the measurements are typically defined as ``arrX``. By setting input parameter ``Xstand=False`` and ``Ystand=False`` we make sure that the variables are only mean centered, not scaled to unit variance, if this is what you want. This is the default setting and actually doesn't need to expressed explicitly. Setting paramter ``cvType=["loo"]`` we make sure that we compute the PCR model using full cross validation. ``"loo"`` means "Leave One Out". By setting paramter ``numpComp=4`` we ask for four components to be computed.

# In[6]:

model = ho.pcr.nipalsPCR(arrX=X, Xstand=False,
                      arrY=Y, Ystand=False,
                      cvType=["loo"],
                      numComp=4)

# That's it, the PCR model has been computed. Now we would like to inspect the results by visualising them. We can do this using plotting functions of the separate [**hoggormPlot** package](https://hoggormplot.readthedocs.io/en/latest/). If we wish to plot the results for component 1 and component 2, we can do this by setting the input argument ``comp=[1, 2]``. The input argument ``plots=[1, 2, 3, 4, 6]`` lets the user define which plots are to be plotted. If this list for example contains value ``1``, the function will generate the scores plot for the model. If the list contains value ``2``, then the loadings plot will be plotted. Value ``3`` stands for correlation loadings plot and value ``4`` stands for bi-plot and ``6`` stands for explained variance plot. The hoggormPlot documentation provides a [description of input paramters](https://hoggormplot.readthedocs.io/en/latest/mainPlot.html).

# In[7]:

hop.plot(model, comp=[1, 2],
         plots=[1, 2, 3, 4, 6],
         objNames=X_objNames,
         XvarNames=X_varNames,
         YvarNames=Y_varNames)

# Plots can also be called separately.

# In[8]:

# Plot cumulative explained variance (both calibrated and validated) using a specific function for that.
hop.explainedVariance(model)

# In[9]:

# Plot cumulative validated explained variance for each variable in Y
hop.explainedVariance(model, individual = True)

# In[10]:

# Plot cumulative validated explained variance in X.
hop.explainedVariance(model, which='X')

# In[11]:

hop.scores(model)

# In[12]:

hop.correlationLoadings(model)

# In[15]:

# Plot X loadings in line plot
hop.loadings(model, weights=False, line=True)

# In[16]:

# Plot regression coefficients
hop.coefficients(model, comp=3)

# ---

# ### Accessing numerical results

# Now that we have visualised the PCR results, we may also want to access the numerical results. Below are some examples. For a complete list of accessible results, please see this part of the documentation.

# In[17]:

# Get X scores and store in numpy array
X_scores = model.X_scores()

# Get scores and store in pandas dataframe with row and column names
X_scores_df = pd.DataFrame(model.X_scores())
X_scores_df.index = X_objNames
X_scores_df.columns = ['PC{0}'.format(x+1) for x in range(model.X_scores().shape[1])]
X_scores_df

# In[19]:

help(ho.pcr.nipalsPCR.X_scores)

# In[20]:

# Dimension of the X_scores
np.shape(model.X_scores())

# We see that the numpy array holds the scores for all countries and OECD (35 in total) for four components as required when computing the PCA model.

# In[21]:

# Get X loadings and store in numpy array
X_loadings = model.X_loadings()

# Get X loadings and store in pandas dataframe with row and column names
X_loadings_df = pd.DataFrame(model.X_loadings())
X_loadings_df.index = X_varNames
X_loadings_df.columns = ['PC{0}'.format(x+1) for x in range(model.X_loadings().shape[1])]
X_loadings_df

# In[22]:

help(ho.pcr.nipalsPCR.X_loadings)

# In[23]:

np.shape(model.X_loadings())

# Here we see that the array holds the loadings for the 10 variables in the data across four components.

# In[24]:

# Get Y loadings and store in numpy array
Y_loadings = model.Y_loadings()

# Get Y loadings and store in pandas dataframe with row and column names
Y_loadings_df = pd.DataFrame(model.Y_loadings())
Y_loadings_df.index = Y_varNames
Y_loadings_df.columns = ['PC{0}'.format(x+1) for x in range(model.Y_loadings().shape[1])]
Y_loadings_df

# In[25]:

# Get X correlation loadings and store in numpy array
X_corrloadings = model.X_corrLoadings()

# Get X correlation loadings and store in pandas dataframe with row and column names
X_corrloadings_df = pd.DataFrame(model.X_corrLoadings())
X_corrloadings_df.index = X_varNames
X_corrloadings_df.columns = ['PC{0}'.format(x+1) for x in range(model.X_corrLoadings().shape[1])]
X_corrloadings_df

# In[26]:

help(ho.pcr.nipalsPCR.X_corrLoadings)

# In[27]:

# Get Y loadings and store in numpy array
Y_corrloadings = model.X_corrLoadings()

# Get Y loadings and store in pandas dataframe with row and column names
Y_corrloadings_df = pd.DataFrame(model.Y_corrLoadings())
Y_corrloadings_df.index = Y_varNames
Y_corrloadings_df.columns = ['PC{0}'.format(x+1) for x in range(model.Y_corrLoadings().shape[1])]
Y_corrloadings_df

# In[28]:

help(ho.pcr.nipalsPCR.Y_corrLoadings)

# In[29]:

# Get calibrated explained variance of each component in X
X_calExplVar = model.X_calExplVar()

# Get calibrated explained variance in X and store in pandas dataframe with row and column names
X_calExplVar_df = pd.DataFrame(model.X_calExplVar())
X_calExplVar_df.columns = ['calibrated explained variance in X']
X_calExplVar_df.index = ['PC{0}'.format(x+1) for x in range(model.X_loadings().shape[1])]
X_calExplVar_df

# In[30]:

help(ho.pcr.nipalsPCR.X_calExplVar)

# In[31]:

# Get calibrated explained variance of each component in Y
Y_calExplVar = model.Y_calExplVar()

# Get calibrated explained variance in Y and store in pandas dataframe with row and column names
Y_calExplVar_df = pd.DataFrame(model.Y_calExplVar())
Y_calExplVar_df.columns = ['calibrated explained variance in Y']
Y_calExplVar_df.index = ['PC{0}'.format(x+1) for x in range(model.Y_loadings().shape[1])]
Y_calExplVar_df

# In[32]:

help(ho.pcr.nipalsPCR.Y_calExplVar)

# In[33]:

# Get cumulative calibrated explained variance in X
X_cumCalExplVar = model.X_cumCalExplVar()

# Get cumulative calibrated explained variance in X and store in pandas dataframe with row and column names
X_cumCalExplVar_df = pd.DataFrame(model.X_cumCalExplVar())
X_cumCalExplVar_df.columns = ['cumulative calibrated explained variance in X']
X_cumCalExplVar_df.index = ['PC{0}'.format(x) for x in range(model.X_loadings().shape[1] + 1)]
X_cumCalExplVar_df

# In[34]:

help(ho.pcr.nipalsPCR.X_cumCalExplVar)

# In[35]:

# Get cumulative calibrated explained variance in Y
Y_cumCalExplVar = model.Y_cumCalExplVar()

# Get cumulative calibrated explained variance in Y and store in pandas dataframe with row and column names
Y_cumCalExplVar_df = pd.DataFrame(model.Y_cumCalExplVar())
Y_cumCalExplVar_df.columns = ['cumulative calibrated explained variance in Y']
Y_cumCalExplVar_df.index = ['PC{0}'.format(x) for x in range(model.Y_loadings().shape[1] + 1)]
Y_cumCalExplVar_df

# In[36]:

help(ho.pcr.nipalsPCR.Y_cumCalExplVar)

# In[37]:

# Get cumulative calibrated explained variance for each variable in X
X_cumCalExplVar_ind = model.X_cumCalExplVar_indVar()

# Get cumulative calibrated explained variance for each variable in X and store in pandas dataframe with row and column names
X_cumCalExplVar_ind_df = pd.DataFrame(model.X_cumCalExplVar_indVar())
X_cumCalExplVar_ind_df.columns = X_varNames
X_cumCalExplVar_ind_df.index = ['PC{0}'.format(x) for x in range(model.X_loadings().shape[1] + 1)]
X_cumCalExplVar_ind_df

# In[38]:

help(ho.pcr.nipalsPCR.X_cumCalExplVar_indVar)

# In[39]:

# Get cumulative calibrated explained variance for each variable in Y
Y_cumCalExplVar_ind = model.Y_cumCalExplVar_indVar()

# Get cumulative calibrated explained variance for each variable in Y and store in pandas dataframe with row and column names
Y_cumCalExplVar_ind_df = pd.DataFrame(model.Y_cumCalExplVar_indVar())
Y_cumCalExplVar_ind_df.columns = Y_varNames
Y_cumCalExplVar_ind_df.index = ['PC{0}'.format(x) for x in range(model.Y_loadings().shape[1] + 1)]
Y_cumCalExplVar_ind_df

# In[40]:

help(ho.pcr.nipalsPCR.Y_cumCalExplVar_indVar)

# In[41]:

# Get calibrated predicted Y for a given number of components

# Predicted Y from calibration using 1 component
Y_from_1_component = model.Y_predCal()[1]

# Predicted Y from calibration using 1 component stored in pandas data frame with row and columns names
Y_from_1_component_df = pd.DataFrame(model.Y_predCal()[1])
Y_from_1_component_df.index = Y_objNames
Y_from_1_component_df.columns = Y_varNames
Y_from_1_component_df

# In[42]:

# Get calibrated predicted Y for a given number of components

# Predicted Y from calibration using 4 component
Y_from_4_component = model.Y_predCal()[4]

# Predicted Y from calibration using 1 component stored in pandas data frame with row and columns names
Y_from_4_component_df = pd.DataFrame(model.Y_predCal()[4])
Y_from_4_component_df.index = Y_objNames
Y_from_4_component_df.columns = Y_varNames
Y_from_4_component_df

# In[43]:

help(ho.pcr.nipalsPCR.X_predCal)

# In[44]:

# Get validated explained variance of each component X
X_valExplVar = model.X_valExplVar()

# Get calibrated explained variance in X and store in pandas dataframe with row and column names
X_valExplVar_df = pd.DataFrame(model.X_valExplVar())
X_valExplVar_df.columns = ['validated explained variance in X']
X_valExplVar_df.index = ['PC{0}'.format(x+1) for x in range(model.X_loadings().shape[1])]
X_valExplVar_df

# In[45]:

help(ho.pcr.nipalsPCR.X_valExplVar)

# In[46]:

# Get validated explained variance of each component Y
Y_valExplVar = model.Y_valExplVar()

# Get calibrated explained variance in X and store in pandas dataframe with row and column names
Y_valExplVar_df = pd.DataFrame(model.Y_valExplVar())
Y_valExplVar_df.columns = ['validated explained variance in Y']
Y_valExplVar_df.index = ['PC{0}'.format(x+1) for x in range(model.Y_loadings().shape[1])]
Y_valExplVar_df

# In[47]:

help(ho.pcr.nipalsPCR.Y_valExplVar)

# In[48]:

# Get cumulative validated explained variance in X
X_cumValExplVar = model.X_cumValExplVar()

# Get cumulative validated explained variance in X and store in pandas dataframe with row and column names
X_cumValExplVar_df = pd.DataFrame(model.X_cumValExplVar())
X_cumValExplVar_df.columns = ['cumulative validated explained variance in X']
X_cumValExplVar_df.index = ['PC{0}'.format(x) for x in range(model.X_loadings().shape[1] + 1)]
X_cumValExplVar_df

# In[49]:

help(ho.pcr.nipalsPCR.X_cumValExplVar)

# In[50]:

# Get cumulative validated explained variance in Y
Y_cumValExplVar = model.Y_cumValExplVar()

# Get cumulative validated explained variance in Y and store in pandas dataframe with row and column names
Y_cumValExplVar_df = pd.DataFrame(model.Y_cumValExplVar())
Y_cumValExplVar_df.columns = ['cumulative validated explained variance in Y']
Y_cumValExplVar_df.index = ['PC{0}'.format(x) for x in range(model.Y_loadings().shape[1] + 1)]
Y_cumValExplVar_df

# In[51]:

help(ho.pcr.nipalsPCR.Y_cumValExplVar)

# In[52]:

# Get cumulative validated explained variance for each variable in Y
Y_cumCalExplVar_ind = model.Y_cumCalExplVar_indVar()

# Get cumulative validated explained variance for each variable in Y and store in pandas dataframe with row and column names
Y_cumValExplVar_ind_df = pd.DataFrame(model.Y_cumValExplVar_indVar())
Y_cumValExplVar_ind_df.columns = Y_varNames
Y_cumValExplVar_ind_df.index = ['PC{0}'.format(x) for x in range(model.Y_loadings().shape[1] + 1)]
Y_cumValExplVar_ind_df

# In[53]:

help(ho.pcr.nipalsPCR.X_cumValExplVar_indVar)

# In[54]:

# Get validated predicted Y for a given number of components

# Predicted Y from validation using 1 component
Y_from_1_component_val = model.Y_predVal()[1]

# Predicted Y from calibration using 1 component stored in pandas data frame with row and columns names
Y_from_1_component_val_df = pd.DataFrame(model.Y_predVal()[1])
Y_from_1_component_val_df.index = Y_objNames
Y_from_1_component_val_df.columns = Y_varNames
Y_from_1_component_val_df

# In[51]:

# Get validated predicted Y for a given number of components

# Predicted Y from validation using 3 components
Y_from_3_component_val = model.Y_predVal()[3]

# Predicted Y from calibration using 3 components stored in pandas data frame with row and columns names
Y_from_3_component_val_df = pd.DataFrame(model.Y_predVal()[3])
Y_from_3_component_val_df.index = Y_objNames
Y_from_3_component_val_df.columns = Y_varNames
Y_from_3_component_val_df

# In[55]:

help(ho.pcr.nipalsPCR.Y_predVal)

# In[56]:

# Get predicted scores for new measurements (objects) of X

# First pretend that we acquired new X data by using part of the existing data and overlaying some noise
import numpy.random as npr
new_X = X[0:4, :] + npr.rand(4, np.shape(X)[1])
np.shape(X)

# Now insert the new data into the existing model and compute scores for two components (numComp=2)
pred_X_scores = model.X_scores_predict(new_X, numComp=2)

# Same as above, but results stored in a pandas dataframe with row names and column names
pred_X_scores_df = pd.DataFrame(model.X_scores_predict(new_X, numComp=2))
pred_X_scores_df.columns = ['PC{0}'.format(x+1) for x in range(2)]
pred_X_scores_df.index = ['new object {0}'.format(x+1) for x in range(np.shape(new_X)[0])]
pred_X_scores_df

# In[57]:

help(ho.pcr.nipalsPCR.X_scores_predict)

# In[58]:

# Predict Y from new X data
pred_Y = model.Y_predict(new_X, numComp=2)

# Predict Y from nex X data and store results in a pandas dataframe with row names and column names
pred_Y_df = pd.DataFrame(model.Y_predict(new_X, numComp=2))
pred_Y_df.columns = Y_varNames
pred_Y_df.index = ['new object {0}'.format(x+1) for x in range(np.shape(new_X)[0])]
pred_Y_df


text:
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
from rclpy.qos import qos_profile_sensor_data
from sensor_msgs.msg import Imu
import numpy as np
import math

def euler_from_quaternion(x, y, z, w):
        """
        Convert a quaternion into euler angles (roll, pitch, yaw)
        roll is rotation around x in radians (counterclockwise)
        pitch is rotation around y in radians (counterclockwise)
        yaw is rotation around z in radians (counterclockwise)
        """
        t0 = +2.0 * (w * x + y * z)
        t1 = +1.0 - 2.0 * (x * x + y * y)
        roll_x = math.atan2(t0, t1)

        t2 = +2.0 * (w * y - z * x)
        t2 = +1.0 if t2 > +1.0 else t2
        t2 = -1.0 if t2 < -1.0 else t2
        pitch_y = math.asin(t2)

        t3 = +2.0 * (w * z + x * y)
        t4 = +1.0 - 2.0 * (y * y + z * z)
        yaw_z = math.atan2(t3, t4)

        return roll_x, pitch_y, yaw_z # in radians

class ImuSubscriber(Node):

    def __init__(self):
        super().__init__('imu_subscriber')
        self.subscription = self.create_subscription(
            Imu,
            '/imu',
            self.listener_callback,
            qos_profile_sensor_data)
        # self.subscription

    def listener_callback(self, scan):
        x = scan.orientation.x
        y = scan.orientation.y
        z = scan.orientation.z
        w = scan.orientation.w

        ax = scan.linear_acceleration.x
        ay = scan.linear_acceleration.y
        az = scan.linear_acceleration.z

        rate = 180/np.pi

        roll_x, pitch_y, yaw_z = euler_from_quaternion(x, y, z, w)
        # self.get_logger().info('orientation: "%s"' % str((roll_x, pitch_y, yaw_z)))
        # self.get_logger().info('linear_acceleration: "%s"' % str((ax, ay, az)))
        self.get_logger().info('rate: "%s"' % str(int(yaw_z*rate)))
        pass

def main(args=None):
    rclpy.init(args=args)
    imu_subcriber = ImuSubscriber()
    rclpy.spin(imu_subcriber)

    imu_subcriber.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()


text:
'''
Evaluation file for various models.

Aim of this file is to be as generic as possible so training any model is a breeze
all the major functions are already present here and the only thing that should can
be changed is the network file.
'''

# dependencies
import argparse
import numpy as np
import os
from glob import glob

# custom model
import network
from utils import add_padding

# batch iterator
def get_batch(iteratable, n = 20):
    iter_len = len(iteratable)
    for ndx in range(0, iter_len, n):
        temp = iteratable[ndx:min(ndx + n, iter_len)]
        if len(temp) < n:
            temp = iteratable[ndx:min(ndx + n, iter_len)].copy()
            gap = n - len(temp)
            temp.extend(iteratable[:gap])
        yield temp

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--qnqp-file', type = str, help = 'path to numpy dumps')
    parser.add_argument('--embedding-path', type = str, help = 'path to embedding .npy file')
    parser.add_argument('--model-path', type = str, help = 'path to folder where saving model')
    parser.add_argument('--results-path', type = str, help = 'path to folder where saving results')
    parser.add_argument('--model-name', type = str, default = 'final1', help = 'scope name')
    parser.add_argument('--batch-size', type = int, default = 1024, help = 'size of minibatch')
    parser.add_argument('--seqlen', type = int, default = 80, help = 'length of sequences')
    args = parser.parse_args()

    '''
    Step 1: Before the models is built and setup, do the preliminary work
    '''

    # make the folders
    if not os.path.exists(args.results_path):
        os.makedirs(args.results_path)
        print('[!] Model saving folder could not be found, making folder, {args.results_path}')

    # We need to get the list of all the q, p and l files that were generated
    qn_paths = sorted(glob(args.qnqp_file + '_n*.npy'))
    q_paths = sorted(glob(args.qnqp_file + '_q*.npy'))
    p_paths = sorted(glob(args.qnqp_file + '_p*.npy'))

    print(qn_paths)
    print(q_paths)
    print(p_paths)

    '''
    Step 2: All the checks are done, make the model
    '''

    print('[*] Data loading ...')
    # load the training numpy matrix
    for i in range(len(q_paths)):
        print('... loading file number:', i)
        if i == 0:
            eval_qn = np.load(qn_paths[i])
            eval_q = np.load(q_paths[i])
            eval_p = np.load(p_paths[i])
        else:
            q_ = np.load(qn_paths[i])
            p_ = np.load(q_paths[i])
            l_ = np.load(p_paths[i])
            eval_qn = np.concatenate([eval_qn, q_])
            eval_q = np.concatenate([eval_q, p_])
            eval_p = np.concatenate([eval_p, l_])

    # load embedding matrix
    print('... loading embedding matrix')
    embedding_matrix = np.load(args.embedding_path)

    print('[*] ... Data loading complete!')

    # load the model, this is one line that will be changed for each case
    print('[*] Making model')
    model = network.TransformerNetwork(scope = args.model_name,
                                       save_folder = args.model_path,
                                       pad_id = len(embedding_matrix),
                                       is_training = False,
                                       dim_model = embedding_matrix.shape[-1])

    # build the model
    print('[*] Building model (for details look at the stack below)')
    model.build_model(emb = embedding_matrix,
                      seqlen = args.seqlen,
                      batch_size = args.batch_size,
                      print_stack = True)

    print('[*] Loading the model from saved weights')
    model.start_sess_loader()

    '''
    Step 3: get results from model
    '''
    # make an index range
    idx_ = np.arange(len(qn_)).tolist()

    # make list of results
    all_preds = []

    num_batches = 0
    proc_batches = 0

    for _ in get_batch(idx_, n = 512):
        num_batches += 1

    print('[!] number of batches:', num_batches)

    # iterate
    for x in get_batch(idx_, n = 512):
        proc_batches += 1
        q = add_padding(queries_[x], model.pad_id, 80)
        p = add_padding(passages_[x], model.pad_id, 80)

        feed_dict = {model.query_input: q, model.passage_input: p}
        preds = model.sess.run(model.net_op, feed_dict = feed_dict)
        all_preds.append(preds)

    '''
    Step 4: convert to proper list and write the answers.tsv file
    '''
    all_pred_ = np.reshape(all_preds, [-1])
    all_pred_ = all_pred_[:len(qn_)]
    all_pred_ = np.reshape(all_pred_, [-1, 10])

    # converting to the required dump format
    res_path = '../final1_saves/res1/final_26.tsv'

    # first convert the entirity to text
    text = ''
    for i in range(len(all_pred_)):
        tt = str(int(qn_[i*10]))
        for p in all_pred_[i]:
            tt += '\t' + ("%.6f" % p)
        tt += '\n'
        text += tt

    with open(res_path, 'w') as f:
        f.write(text)

    print('... Done! File saved at:', res_path)


text:
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 17 20:18:44 2022

@author: mahom
"""
import numpy as np
import torch
from to_torch import to_torch
def correcting_factor_cov_gamma(model,Ws,Yval,Xval,option_lv,scalerY_K,Var_ErrorNMF,Std_nmf):
    # W: T x K
    # Yval: Nval x T
    # Xval  Nval x F
    # Kh : Nval x T  times  N x T

    Nval = Yval.size(0)
    n_task = len(model)
    yval = Yval.reshape(-1,1)
    W = Std_nmf*Ws
    K = W.size(1)
    VarK = scalerY_K.var_
    StdK = torch.sqrt(torch.Tensor(scalerY_K.var_)).reshape(-1,1)
    meanK = torch.Tensor(scalerY_K.mean_).reshape(-1,1)
    if option_lv == "ind" or option_lv == "mt":
        SigmaH = model.likelihood(model.forward(Xval)).covariance_matrix
    if option_lv == "gp_ind_ori":
        SigmaF_list = []
        SigmaH_list = []
        invSigmaH_list = []
        meansH_list = []
        for task in range(0,n_task):
            mk = model['task{}'.format(task+1)]
            SigmaH_k = VarK[task]*(mk.likelihood(mk.forward(Xval)).covariance_matrix) # Varianzas solo en lugar de Cov? Rearrange de manera que se puedan operar diferente.
            SigmaH_list.append(SigmaH_k)
            invSigmaH_k = torch.inverse(SigmaH_k)
            invSigmaH_list.append(invSigmaH_k)
            meansH_list.append(model['task{}'.format(task+1)].mean_module.constant)
        invSigmaH = torch.block_diag(*invSigmaH_list)
        SigmaH = torch.block_diag(*SigmaH_list)
        for hour in range(0,24):
            w = W[hour,:]
            SigmaHf = torch.Tensor(sum([torch.Tensor([a])*b for a,b in zip(w.tolist(),SigmaH_list)]))
            A = [torch.Tensor([a])*b for a,b in zip(w.tolist(),SigmaH_list)]
            SigmaF_list.append(torch.inverse( SigmaHf  + Var_ErrorNMF[task]*torch.eye(Nval)))

    invSigmaF =  torch.block_diag(*SigmaF_list)
 # Las matrices deberian ser positivas definidas.
    meansH = torch.Tensor(meansH_list).reshape(-1,1)
    Mstd = meansH.repeat(1,Nval)
    M = (W@(Mstd*StdK+meanK)).T  # N x 24
    m = M.reshape(-1,1)

    yval_cent = yval - m
    a = (yval_cent.T@invSigmaF@yval_cent)/(Nval*24)

    return a

max_stars_repo_name: Miguel-Hombrados/GPK-pytorch

max_issues_repo_name: Miguel-Hombrados/GPK-pytorch

max_forks_repo_name: Miguel-Hombrados/GPK-pytorch


text:
#!/usr/bin/env python3

import json
import os
import subprocess

import joblib
import networkx as nx
import numpy as np
import pandas as pd

import src

filepath_inspections_raw = 'data/raw/inspections.csv'
filepath_portcalls_raw = 'data/raw/portcalls.csv'
filepath_flag_performance = 'data/raw/flag_performance.json'
filepath_inspections_cleaned = 'data/cleaned/inspections.pkl'
filepath_portcalls_cleaned = 'data/cleaned/portcalls.pkl'
filepath_un_locode_cleaned = 'data/cleaned/un-locode.pkl'
filepath_inspections_processed = 'data/processed/inspections.pkl'
filepath_portcalls_processed = 'data/processed/portcalls.pkl'
filepath_ships_network = 'models/ships_network.npy'
filepath_ships_classification = 'models/ships_classification.npy'
filepath_network = 'models/network.pkl'
filepath_folds = 'models/folds.pkl'
filepath_x = 'models/x.pkl'
filepath_y = 'models/y.pkl'
filepath_s = 'models/s.pkl'
filepath_outer_folds = 'models/outer_folds.pkl'
filepath_inner_folds = 'models/inner_folds.pkl'
filepath_performance_folds = 'models/performance_folds.pkl'

def run():
    src.logger.info('#0 Check installs')
    if not os.path.isfile('teexgraph/teexgraph'):
        subprocess.run(['make', 'install'])

    src.logger.info("#1 Import inspections.")
    if not os.path.isfile(filepath_inspections_cleaned):
        inspections_cleaned = src.import_inspections(filepath_inspections_raw)
        inspections_cleaned.to_pickle(filepath_inspections_cleaned)

    src.logger.info('#2 Import portcalls.')
    if not os.path.isfile(filepath_portcalls_cleaned):
        with open(filepath_flag_performance) as file:
            flag_performance = json.load(file)

        portcalls_cleaned = src.import_portcalls(filepath_portcalls_raw,
                                                 flag_performance)
        portcalls_cleaned.to_pickle(filepath_portcalls_cleaned)

    src.logger.info('#3 Import UN Locode.')
    if not os.path.isfile(filepath_un_locode_cleaned):
        un_locode_cleaned = src.import_un_locode(
            [f'data/raw/2021-1 UNLOCODE CodeListPart{i}.csv' for i in (1,2,3)])
        un_locode_cleaned.to_pickle(filepath_un_locode_cleaned)

    src.logger.info('#4 Process inspections.')
    if not os.path.isfile(filepath_inspections_processed):
        inspections_cleaned = pd.read_pickle(filepath_inspections_cleaned)
        inspections_processed = src.process_inspections(inspections_cleaned)
        inspections_processed.to_pickle(filepath_inspections_processed)

    src.logger.info('#5 Process portcalls.')
    if not os.path.isfile(filepath_portcalls_processed):
        portcalls_cleaned = pd.read_pickle(filepath_portcalls_cleaned)
        portcalls_processed = src.process_portcalls(portcalls_cleaned)
        portcalls_processed.to_pickle(filepath_portcalls_processed)

    src.logger.info('#6 Divide ships into classification and network set.')
    ships_classification_exists = os.path.isfile(filepath_ships_classification)
    ships_network_exists = os.path.isfile(filepath_ships_network)
    if not (ships_classification_exists and ships_network_exists):
        portcalls_processed = pd.read_pickle(filepath_portcalls_processed)
        inspections_processed = pd.read_pickle(filepath_inspections_processed)
        ships_classification, ships_network = src.divide_ships(
            portcalls_processed, inspections_processed)
        np.save(filepath_ships_classification, ships_classification)
        np.save(filepath_ships_network, ships_network)

    src.logger.info('#7 Construct network.')
    if not os.path.isfile(filepath_network):
        ships_network = np.load(filepath_ships_network, allow_pickle=True)
        portcalls_processed = pd.read_pickle(filepath_portcalls_processed)
        portcalls_network = portcalls_processed.loc[
            lambda x: x['ship'].isin(ships_network)]
        network = src.construct_network(portcalls_network)
        nx.write_gpickle(network, filepath_network)

    src.logger.info('#8 Get features.')
    if not os.path.isfile(filepath_x):
        portcalls_processed = pd.read_pickle(filepath_portcalls_processed)
        ships_network = np.load(filepath_ships_network, allow_pickle=True)
        portcalls_network = portcalls_processed.loc[
            lambda x: x['ship'].isin(ships_network)]
        ships_classification = np.load(filepath_ships_classification,
                                       allow_pickle=True)
        portcalls_classification = portcalls_processed.loc[
            lambda x: x['ship'].isin(ships_classification)]
        network = nx.read_gpickle(filepath_network)
        X = src.get_features(portcalls_network, portcalls_classification,
                             network)
        X.to_pickle(filepath_x)

    src.logger.info('#9 Get targets.')
    if not os.path.isfile(filepath_y):
        inspections_processed = pd.read_pickle(filepath_inspections_processed)
        ships_classification = np.load(filepath_ships_classification,
                                       allow_pickle=True)
        inspections_classification = inspections_processed.loc[
            lambda x: x['IMO'].isin(ships_classification)]
        y = src.get_targets(inspections_classification, ships_classification)
        y.to_pickle(filepath_y)

    src.logger.info('#10 Get sensitive group.')
    if not os.path.isfile(filepath_s):
        portcalls_processed = pd.read_pickle(filepath_portcalls_processed)
        ships_classification = np.load(filepath_ships_classification,
                                       allow_pickle=True)
        portcalls_classification = portcalls_processed.loc[
            lambda x: x['ship'].isin(ships_classification)]
        s = src.get_sensitive_group(portcalls_classification,
                                    ships_classification)
        s.to_pickle(filepath_s)

    src.logger.info('#11 Get folds.')
    if not os.path.isfile(filepath_folds):
        x = pd.read_pickle(filepath_x)
        y = pd.read_pickle(filepath_y)
        s = pd.read_pickle(filepath_s)
        outer_folds, inner_folds = src.get_folds(x, y, s)
        joblib.dump(outer_folds, filepath_outer_folds)
        joblib.dump(inner_folds, filepath_inner_folds)

    src.logger.info('#12 Get performance over every fold.')
    X = pd.read_pickle(filepath_x)
    y = pd.read_pickle(filepath_y)
    s = pd.read_pickle(filepath_s)
    outer_folds = joblib.load(filepath_outer_folds)
    inner_folds = joblib.load(filepath_inner_folds)
    performance_folds = src.learn(X, y, s, outer_folds, inner_folds)
    performance_folds.to_pickle(filepath_performance_folds)

if __name__ == '__main__':
    run()

reason: import numpy,import networkx


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

import matplotlib.pyplot as plt
import numpy as np
from scipy.linalg import eigvals
import pickle
import parameters

from pyvib.nlforce import NL_force, NL_polynomial
from pyvib.forcing import sineForce, randomPeriodic, sineSweep, toMDOF
from pyvib.newmark import newmark_beta_nl as newmark_beta

plot = True
savedata = True
savefig = False

dof = 0

par = parameters.par

M = par['M']
C = par['C']
K = par['K']

ftype = 'sweep'
fdof = 0
vrms = 2
f1 = 0.001/2/np.pi
f2 = 5/np.pi
fs = 20*f2
nper = 2
nsper = 10000
vsweep = 0.01
inctype = 'lin'

inl = par['inl']
enl = par['enl']
knl = par['knl']
nl_pol = NL_polynomial(inl, enl, knl)
nl = NL_force(nl_pol)

x0, y0 = 0, 0
ndof = M.shape[0]
# get external forcing
if ftype == 'multisine':
    u, t = randomPeriodic(vrms, fs, f1, f2, nsper, nper)
elif ftype == 'sweep':
    u, t, finst = sineSweep(vrms, fs, f1, f2, vsweep, nper, inctype)
    # we dont use the first value, as it is not repeated when the force is
    # generated. This is taken care of in randomPeriodic.
    nsper = (len(u)-1) // nper
elif ftype == 'sine':
    u, t = sineForce(vrms, f=f1, fs=fs, ns=nsper)
else:
    raise ValueError('Wrong type of forcing', ftype)
fext = toMDOF(u, ndof, fdof)

dt = t[1] - t[0]
x, xd, xdd = newmark_beta(M, C, K, x0, y0, dt, fext, nl, sensitivity=False)

plt.figure()
plt.clf()
plt.plot(t, x[0], '-k', label=r'$x_1$')
plt.plot(t, x[1], '-r', label=r'$x_2$')
plt.xlabel('Time (t)')
plt.ylabel('Displacement (m)')
plt.title('Force type: {}, periods:{:d}'.format(ftype, nper))
plt.legend()

plt.show()

max_stars_repo_name: pawsen/pyvib

max_issues_repo_name: pawsen/vib

max_forks_repo_name: pawsen/vib

reason: import numpy,from scipy


text:
import torch.utils.data as data

import pdb
from PIL import Image
import os
import os.path
import numpy as np

IMG_EXTENSIONS = [
    ".jpg",
    ".JPG",
    ".jpeg",
    ".JPEG",
    ".png",
    ".PNG",
    ".ppm",
    ".PPM",
    ".bmp",
    ".BMP",
]

def is_image_file(filename):
    return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)

def dataloader(filepath, typ="train"):

    left_fold = "image_2/"
    right_fold = "image_3/"
    disp_L = "disp_occ_0/"

    image = [img for img in os.listdir(filepath + left_fold) if img.find("_10") > -1]
    image = sorted(image)
    imglist = [
        1,
        3,
        6,
        20,
        26,
        35,
        38,
        41,
        43,
        44,
        49,
        60,
        67,
        70,
        81,
        84,
        89,
        97,
        109,
        119,
        122,
        123,
        129,
        130,
        132,
        134,
        141,
        144,
        152,
        158,
        159,
        165,
        171,
        174,
        179,
        182,
        184,
        186,
        187,
        196,
    ]
    if typ == "train":
        train = [image[i] for i in range(200) if i not in imglist] * 100
    elif typ == "trainval":
        train = [image[i] for i in range(200)] * 80
    val = [image[i] for i in imglist]

    left_train = [filepath + left_fold + img for img in train]
    right_train = [filepath + right_fold + img for img in train]
    disp_train_L = [filepath + disp_L + img for img in train]

    left_val = [filepath + left_fold + img for img in val]
    right_val = [filepath + right_fold + img for img in val]
    disp_val_L = [filepath + disp_L + img for img in val]

    return left_train, right_train, disp_train_L, left_val, right_val, disp_val_L


text:
# AUTOGENERATED! DO NOT EDIT! File to edit: 02_naive_bayes.ipynb (unless otherwise specified).

__all__ = ['NaiveBayes']

# Cell
import pandas as pd
import numpy as np
from collections import defaultdict, Counter

# Cell
class NaiveBayes:
    """Naive Bayes Algorithm for Binary Classification"""

    def __init__(self):
        self.class1_term_cf = defaultdict(int) #term frequency in class 1
        self.class2_term_cf = defaultdict(int) #term frequency in class 2
        self.classes = []

    def fit(self, X, y):
        """ Train Naive Bayes.

        Args:

            X (nested list): nested list of tokenized samples.
            y (list): list of corresponding lables.
        """

        self._get_priors(y)
        self._get_term_class_freq(X, y)
        self._get_vocab_length()

    def _get_priors(self, y_train):
        """Calculate the priors for both classes"""

        self.priors = Counter(y_train)
        self.classes = list(self.priors.keys())
        total = sum(self.priors.values())
        for class_name in self.priors.keys():
            self.priors[class_name] /= total

    def _get_term_class_freq(self, x_train, y_train):
        """Calculate term frequency in each class"""

        for index , sample in enumerate(x_train):
            for term in sample:
                if   y_train[index] == self.classes[0]:
                    self.class1_term_cf[term] += 1
                elif y_train[index] == self.classes[1]:
                    self.class2_term_cf[term]  += 1

        self.class1_cf = sum(self.class1_term_cf.values())
        self.class2_cf = sum(self.class2_term_cf.values())

    def _get_vocab_length(self):
        self.vocab_length = len(set(list(self.class1_term_cf.keys()) + \
                                    list(self.class2_term_cf.keys())))

    def predict(self, X):
        """ Predict the labels for samples.

        Args:

            X(nested list): nested list of tokenized samples.

        Returns:

            list of predicted label.
        """

        predictions = []
        for sample in X:
            predictions.append(self._predict_label(sample))
        return predictions

    def _predict_label(self, sample):
        """predict label for one sample / instance"""

        summation_class1 = np.sum([np.log(self._prob_term_given_label(term , label = self.classes[0]))\
                                 for term in sample])
        summation_class2 = np.sum([np.log(self._prob_term_given_label(term , label = self.classes[1]))\
                                 for term in sample])
        prob_class1  = np.log(self.priors[self.classes[0]]) + summation_class1
        prob_class2  = np.log(self.priors[self.classes[1]]) + summation_class2

        prediction = self.classes[0] if prob_class1 > prob_class2 else self.classes[1]
        return prediction

    def _prob_term_given_label(self, term , label):
        """calculate probability of term given some label / class ,laplace smoothing applied"""

        if   label == self.classes[0]:
            return (self.class1_term_cf[term] + 1) / (self.class1_cf + self.vocab_length)
        elif label == self.classes[1]:
            return (self.class2_term_cf[term] + 1)  / (self.class2_cf + self.vocab_length)

max_stars_repo_name: Faizan-E-Mustafa/scratch

max_issues_repo_name: Faizan-E-Mustafa/scratch

max_forks_repo_name: Faizan-E-Mustafa/scratch


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

import warnings
import pandas as pd
import pickle
import os
import csv
import decimal
import shutil

allClass = [ 'background', 'aeroplane', 'bicycle', 'bird', 'boat',
			 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
			 'horse', 'motorbike', 'person', 'potted plant', 'sheep', 'sofa', 'train',
			 'tv/monitor']

items = []
folders = ['AMBIGUOUS', 'anger', 'anticipation', 'disgust', 'fear', 'joy', 'sadness', 'surprise', 'trust']
colorKeywords = ['Red', 'Yellow', 'Green', 'Cyan', 'Blue', 'Magenta']
columns = []
columns.append('filename')
columns.extend(allClass)

# Extend color to columns.
for color in colorKeywords:
	columns.append(color)
	columns.append('n_' + color.lower())

columns.append('emotion')
row = []

def generate(logfile):
	global allClass
	global row
	# ds = input('Directory path: ')

	file = logfile
	filename = os.fsdecode(file)
	fn = filename[:-4]
	currentRow = [0] * len(columns)
	currentRow[0] = fn
	# checkFile = open(os.getcwd()+'\\output_new_instances\\'+filename+'.log')
	checkFile = open('static/output_new_instances/'+filename+'.log')
	lines = checkFile.readlines()

	# Iterate allClass.
	for line in lines:
		# In label list.
		if any(classes in line for classes in allClass):
			currentLine = line.split()
			cl = currentLine[2]
			currentLabel = cl[:-1]
			currentValue = currentLine[3]
			index = columns.index(currentLabel)
			currentRow[index] = currentValue
		# In color family list.
		elif any(cfamily in line for cfamily in colorKeywords):
			currentLine = line.split()
			cf = currentLine[0]
			currentFamily = cf[:-1]
			currentPercentage = currentLine[1]
			numberOfInstances = currentLine[3]
			index = columns.index(currentFamily)
			currentRow[index] = currentPercentage
			index = columns.index('n_' + currentFamily.lower())
			currentRow[index] = numberOfInstances
		# Not matching any keywords.
		else:
			continue
	emotionOfPainting = getEmotionLabel(filename)
	indexOfEmotion = columns.index('emotion')
	currentRow[indexOfEmotion] = emotionOfPainting
	row.append(currentRow)

	createFile()

def createFile():
	# filename = input('Enter new name: ')
	filename = "emotions"
	# Change to desired output directory.
	# mainDirectory = r'C:\\Users\\Jasper\\Documents\\College\\Thesis\\Thesis II - Demo\\demo2\\'
	# mainDirectory = os.getcwd()

	# Create file.
	# originally having 'mainDirectory+'.csv'
	with open(filename+'.csv', 'w', newline='') as file:
		wr = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
		cl = [x.lower() for x in columns]
		wr.writerow(cl)

		for rows in row:
			wr.writerow(rows)
		print('CSV created!')

def getEmotionLabel(painting):
	# Code here for getting the annotated emotion and append it to row.
	global items
	global folders
	i = 0
	for x in items:
		temp = [w.replace('.jpg', '.log') for w in x]
		# print(temp)
		if painting in temp:
			return folders[i]
		i = i + 1
	return ''

# FLASK PART

app = Flask(__name__)
# , static_url_path='', static_folder='/static', template_folder='/templates'

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

@app.route("/getInterpretation", methods=['GET', 'POST'])
def getInterpretation():
	logfile = request.form['logfile']
	warnings.simplefilter("ignore", UserWarning)
	warnings.simplefilter("ignore", category=PendingDeprecationWarning)

	# print("Loading models...")
	# base-only rfc model
	rfc_bo_1_file = "final_ssl_rf_1.0.pickle"
	rfc_bo_1 = pickle.load(open(rfc_bo_1_file, "rb"))

	# base-iteration rfc model
	rfc_bi_2_file = "final_ssl_rf_2.0.pickle"
	rfc_bi_2 = pickle.load(open(rfc_bi_2_file, "rb"))

	# os.system('cls')
	# print("Successfully loaded models.\n")
	generate(logfile)
	emotion_input = 'emotions.csv'
	# emotion_input = "emotions.csv"
	emotion = pd.read_csv(emotion_input)

	# split emotion data to X and y
	emotion_o = emotion.copy()
	emotion = emotion.drop("filename", axis=1)
	num = len(emotion)
	X_emotion = emotion.drop('emotion', axis=1)
	y_emotion = emotion['emotion']

	# prediction of top 1 base-only model
	list_ = list(emotion_o['filename'])
	predictions = []
	for idx, val in enumerate(list_):

	# 	print("Prediction of {} from the top 1 base-only model: {}".format(val, rfc_bo_1.predict(X_emotion[idx:idx+1])))

	# 	# prediction of base-iteration model
	# 	print("Prediction of {} from the base-iteration model: {}\n".format(val, rfc_bi_2.predict(X_emotion[idx:idx+1])))
		predictions = [rfc_bo_1.predict(X_emotion[idx:idx+1])[0], rfc_bi_2.predict(X_emotion[idx:idx+1])[0]]
		# predictions = list(map(str, predictions))
	# print("\nPress any button to exit...")
	# prediction = [ada.predict(instance)[0], rfc.predict(instance)[0], svm.predict(instance)[0]]
	return jsonify(predictions=predictions)

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

max_stars_repo_name: butchersix/Thesis-Website

max_issues_repo_name: butchersix/Thesis-Website

max_forks_repo_name: butchersix/Thesis-Website


text:
import tensorflow as tf
from tensorflow.contrib import layers
import numpy as np

class Model(object):
    def __init__(self, config):
        self.config = config
        self.global_step = tf.Variable(0, trainable=False, name="global_step")
        self.regularizer = layers.l2_regularizer(self.config.l2_lambda)
        self.sess = None
        self.saver = None
        self._build_model()

    def _build_model(self):
        hidden_layers = [16,32]
        self.add_placeholder()
        # get auto-regression
        self.input = tf.reshape(self.input_x, [-1, self.config.nsteps * self.config.nfeatures])
        #pred = self.mlp_net(self.input, hidden_layers, 1, name="mlp_net")
        self.input = tf.layers.dense(self.input, 32, None, use_bias = True)
        #self.input = tf.layers.dense(self.input, 32, None, use_bias = True)

        results = tf.squeeze(tf.layers.dense(self.input, 1, None, use_bias=False))
        #ar, ar_loss = self.auto_regressive(self.input_x, self.config.ar_lambda)
        self.predictions = results
        #self.predictions = tf.squeeze(tf.layers.dense(ar, 1))
        self.loss = tf.losses.mean_squared_error(labels=self.targets, predictions=self.predictions)

        error = tf.reduce_sum((self.targets - self.predictions) ** 2) ** 0.5
        denom = tf.reduce_sum((self.targets - tf.reduce_mean(self.targets)) ** 2) ** 0.5
        self.rmse = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.targets, self.predictions))))
        self.rse = error / denom
        self.mae = tf.reduce_mean(tf.abs(self.targets - self.predictions))
        self.mape = tf.reduce_mean(tf.abs((self.targets - self.predictions) / self.targets))
        self.smape = tf.reduce_mean(2*tf.abs(self.targets - self.predictions)/(tf.abs(self.targets)+tf.abs(self.predictions)))

        '''
        if self.config.l2_lambda > 0:
            reg_vars = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
            reg_term = layers.apply_regularization(self.regularizer, reg_vars)
            self.loss += reg_term
        '''
        #self.loss += ar_loss
        #self.loss = ar_loss
        self.add_train_op()
        self.initialize_session()

    def add_placeholder(self):
        self.input_x = tf.placeholder(shape=[None, self.config.nsteps, self.config.nfeatures], dtype=tf.float32,
                                      name="x")
        self.targets = tf.placeholder(shape=[None, ], dtype=tf.float32, name="targets")
        self.dropout = tf.placeholder(dtype=tf.float32, name="dropout")

    def auto_regressive(self, inputs, ar_lambda):
        # y_t,d = sum_i (w_i * y_i,d) + b_d
        w = tf.get_variable(shape=[self.config.nsteps, self.config.nfeatures],
                            initializer=layers.xavier_initializer(),
                            name="w")
        bias = tf.get_variable(shape=[self.config.nfeatures],
                               initializer=tf.zeros_initializer(),
                               name="bias")
        w_ = tf.expand_dims(w, axis=0)
        weighted = tf.reduce_sum(inputs * w_, axis=1) + bias
        ar_loss = ar_lambda * tf.reduce_sum(tf.square(w))
        return weighted, ar_loss

    def fully_connect(self, x_tensor, num_outputs):
        """
        Apply a fully connection layer
        """
        # shape_list = x_tensor.get_shape().as_list()
        with tf.name_scope("fully"):
            result = tf.layers.dense(inputs = x_tensor,
                                    units = num_outputs,
                                    activation = tf.nn.relu,
                                    # activation = tf.nn.elu,
                                    kernel_initializer = tf.truncated_normal_initializer())
        return result

    def flatten(self, x_tensor, name):
        """
        Flatten input layer
        """
        # without the 1st param, which is Batch Size
        shape = x_tensor.get_shape().as_list()
        flatten_dim = np.prod(shape[1:])
        with tf.name_scope(name):
            result = tf.reshape(x_tensor, [-1, flatten_dim], name=name)
        return result

    def output(self, x_tensor, num_outputs, name):
        """
        Apply a output layer with linear output activation function
        """
        # shape_list = x_tensor.get_shape().as_list()
        # linear output activation function: activation = None
        with tf.name_scope(name):
            result = tf.layers.dense(inputs = x_tensor,
                                    units = num_outputs,
                                    activation = None,
                                    kernel_initializer = tf.truncated_normal_initializer(),
                                    name=name)
        return result

    def mlp_net(self, x, hidden_layers, n_output, name):
        """
        Multilayer Perceptron:
        multiple fully connect layers
        """
        flatten_layer = hidden_layers
        #flatten_layer = flatten(x, name="flatten_layer")
        with tf.name_scope(name):
            if len(hidden_layers) == 1:
                fully_layer = self.fully_connect(flatten_layer, 16)
            else:
                fully_layer = self.fully_connect(flatten_layer, 16)

                for layer in range(1, len(hidden_layers)):
                    fully_layer = self.fully_connect(fully_layer, 32)
        with tf.name_scope("prediction"):
            pred = self.output(fully_layer, n_output, name="output_layer")

        return pred

    def add_train_op(self):
        opt = tf.train.AdamOptimizer(self.config.lr)
        vars = tf.trainable_variables()
        grads = tf.gradients(self.loss, vars)
        clipped_grads, _ = tf.clip_by_global_norm(grads, self.config.clip)
        self.train_op = opt.apply_gradients(zip(grads, vars), global_step=self.global_step)

    def initialize_session(self):
        """Defines self.sess and initialize the variables"""
        if not self.config.allow_gpu:
            config = tf.ConfigProto(device_count={'GPU': 0})
        else:
            config = tf.ConfigProto(allow_soft_placement=True)
            config.gpu_options.allow_growth = True
            config.gpu_options.per_process_gpu_memory_fraction = 0.9
        self.sess = tf.Session(config=config)
        self.sess.run(tf.global_variables_initializer())
        self.saver = tf.train.Saver()

    def save_session(self, model_name):
        """Saves session = weights"""
        self.saver.save(self.sess, model_name)

    def restore_session(self, dir_model):
        """Reload weights into session
        Args:
            sess: tf.Session()
            dir_model: dir with weights
        """
        self.saver.restore(self.sess, tf.train.latest_checkpoint(dir_model))

    def train(self, input_x, targets):
        feed_dict = {
            self.input_x: input_x,
            self.targets: targets,
            self.dropout: self.config.dropout
        }
        output_feed = [self.train_op, self.loss, self.rmse, self.rse, self.smape, self.mae, self.global_step]
        _, loss, rmse, res, smape, mae, step = self.sess.run(output_feed, feed_dict)
        return loss, rmse, res, smape, mae, step

    def eval(self, input_x, targets):
        feed_dict = {
            self.input_x: input_x,
            self.targets: targets,
            self.dropout: 1.0
        }
        output_feed = [self.predictions, self.loss, self.rmse, self.rse, self.smape, self.mae]
        pred, loss, rmse, res, smape, mae = self.sess.run(output_feed, feed_dict)
        return pred, loss, rmse, res, smape, mae

if __name__ == "__main__":
    from config import Config
    config = Config()
    model = Model(config)
    print("done")