text:
#Testing of Hypothesis by Urmil Shroff

try:
    import math
    from scipy import stats

except ImportError:
    print("\n***WARNING: THIS PROGRAM MAY NOT RUN CORRECTLY***\n\n1. Please make sure you have properly installed Python 3.6 from www.python.org\n2. After doing so, run 'python' in terminal\n3. Then type: 'pip install scipy'\n4. Now try again! If the problem still persists, close your computer and go back to sleep.")

def start():
    num=int(input("Enter number of Samples:\n"))

    if (num==1):
        n=float(input("Enter size of the Sample:\n"))

        if (n>=30):
            large_sample(1,n,0) #single large sample
        else:
            small_sample(1,n,0) #single small sample

    elif (num==2):
        n1=float(input("Enter size of the first Sample:\n"))
        n2=float(input("Enter size of the second Sample:\n"))

        if (n1>=30) and (n2>=30):
            large_sample(2,n1,n2) #double large sample
        else:
            small_sample(2,n1,n2) #double small sample

    else:
        print("Sorry, only one or two samples can be tested!")

def large_sample(num,n1,n2):
    if(num==1): #single large sample
        print("\nLarge Sample Test (one Sample)")
        n=n1
        x=float(input("Enter mean of the Sample:\n"))
        u=float(input("Enter mean of the Population:\n"))
        sd=float(input("Enter Standard Deviation:\n"))

        z_a=los_calc_z(int(input("Enter LOS:\n")),int(input("Is the hypothesis one tailed or two tailed?\n")))

        z=((x-u)/(sd/math.sqrt(n)))
        print("\nZ =",z,"\nZa =",z_a)

        if(z<0):
            z=-z

        if(z>z_a):
            print("\nNull hypothesis rejected, alternate hypothesis accepted!\n")
        elif(z<z_a):
            print("\nNull hypothesis accepted, alternate hypothesis rejected!\n")

    elif(num==2): #double large sample
        print("\nLarge Sample Test (two Samples)")

        yesno=input("Is Population SD the same for both populations? Y/N:\n")
        if(yesno=="y") or (yesno=="Y"):

            yesno=input("Is Population SD known? Y/N:\n")

            if(yesno=="y") or (yesno=="Y"): #case 2

                x1=float(input("Enter mean of the first Sample:\n"))
                x2=float(input("Enter mean of the second Sample:\n"))

                popsd=float(input("Enter common SD of the Populations:\n"))

                z=case_z2(n1,n2,x1,x2,popsd)

            else: #case 3

                x1=float(input("Enter mean of the first Sample:\n"))
                x2=float(input("Enter mean of the second Sample:\n"))

                s1=float(input("Enter SD of the first Sample/Population:\n"))
                s2=float(input("Enter SD of the second Sample/Population:\n"))

                z=case_z3(n1,n2,x1,x2,s1,s2)

        else: #case 1

            x1=float(input("Enter mean of the first Sample:\n"))
            x2=float(input("Enter mean of the second Sample:\n"))

            s1=float(input("Enter SD of the first Sample/Population:\n"))
            s2=float(input("Enter SD of the second Sample/Population:\n"))

            z=case_z1(n1,n2,x1,x2,s1,s2)

        z_a=los_calc_z(int(input("Enter LOS:\n")),int(input("Is the hypothesis one tailed or two tailed?\n")))

        if(z<0):
            z=-z

        print("\nZ =",z,"\nZa =",z_a)

        if(z>z_a):
            print("\nNull hypothesis rejected, alternate hypothesis accepted!\n")
        elif(z<z_a):
            print("\nNull hypothesis accepted, alternate hypothesis rejected!\n")

    else:
        print("Error, only 1 or 2 Samples can be predicted!")

def small_sample(num,n1,n2):
    if(num==1):
        print("\nSmall Sample Test (one Sample)")
        n=n1
        x=float(input("Enter mean of the Sample:\n"))
        u=float(input("Enter mean of the Population:\n"))
        sd=float(input("Enter Standard Deviation:\n"))

        t_a=los_calc_t(int(input("Enter LOS:\n")),n-1,num)
        t=((x-u)/(sd/math.sqrt(n-1)))

        if(t<0):
            t=-t

        print("\nT =",t,"\nTa =",t_a)

        if(t>t_a):
            print("\nNull hypothesis rejected, alternate hypothesis accepted!\n")
        elif(t<t_a):
            print("\nNull hypothesis accepted, alternate hypothesis rejected!\n")

    elif(num==2): # TODO: fix this
        print("\nSorry, small sample testing for two samples doesn't work yet! Submit a pull request if you can use SciPy to make it work.")

    else:
        print("Error, only 1 or 2 Samples can be predicted!")

def los_calc_z(los,tails): #LOS calculator for z-test (used for Large Samples)
    if(los==5):
        if(tails==2):
            return 1.96 #two-tailed @ 5% LOS
        else:
            return 1.65 #one-tailed @ 5% LOS

    elif(los==1):
        if(tails==2):
            return 2.58 #two-tailed @ 1% LOS
        else:
            return 2.33 #one-tailed @ 1% LOS

def los_calc_t(los,n,num): #LOS calculator for t-test (used for Small Samples)
    if(num==1):
        return stats.t.ppf((1-(los/100)),n)

    elif(num==2): #TODO: fix this
        print("WTF how did you call me?")
        #small double population formula: t(n1+n2-2,t_a)

def case_z1(n1,n2,x1,x2,s1,s2): #population sds not known, so we take s1, s2
    return ((x1-x2)/(math.sqrt(((s1**2)/n1)+((s2**2)/n2))))

def case_z2(n1,n2,x1,x2,popsd): #population sds are known and they are the same
    return ((x1-x2)/(popsd*(math.sqrt((1/n1)+(1/n2)))))

def case_z3(n1,n2,x1,x2,s1,s2): #population sds not known but they are said to be the same
    return ((x1-x2)/(math.sqrt(((s1**2)/n2)+((s2**2)/n1))))

print("\nTesting of Hypothesis by Urmil Shroff\n")
start()

reason: from scipy


text:
# Copyright 2019-2020 QuantumBlack Visual Analytics Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
# NONINFRINGEMENT. IN NO EVENT WILL THE LICENSOR OR OTHER CONTRIBUTORS
# BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# The QuantumBlack Visual Analytics Limited ("QuantumBlack") name and logo
# (either separately or in combination, "QuantumBlack Trademarks") are
# trademarks of QuantumBlack. The License does not grant you any right or
# license to the QuantumBlack Trademarks. You may not use the QuantumBlack
# Trademarks or any confusingly similar mark as a trademark for your product,
#     or use the QuantumBlack Trademarks in any other manner that might cause
# confusion in the marketplace, including but not limited to in advertising,
# on websites, or on software.
#
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import pandas as pd
import pytest
import torch

from causalnex.structure.pytorch.dist_type import DistTypeBinary, DistTypeContinuous
from causalnex.structure.pytorch.notears import from_numpy, from_pandas

class TestDistTypeClasses:
    @pytest.mark.parametrize("dist_type", [DistTypeBinary, DistTypeContinuous])
    def test_default_init(self, dist_type):
        idx = 1
        dt = dist_type(idx=idx)

        assert dt.idx == idx

    @pytest.mark.parametrize(
        "dist_type, X, X_hat",
        [
            (
                DistTypeContinuous,
                torch.from_numpy(np.random.normal(size=(5, 2))),
                torch.from_numpy(np.random.normal(size=(5, 2))),
            ),
            (
                DistTypeBinary,
                torch.from_numpy(np.random.randint(2, size=(5, 2))).float(),
                torch.from_numpy(np.random.randint(2, size=(5, 2))).float(),
            ),
        ],
    )
    def test_loss(self, dist_type, X, X_hat):
        dist_types = [dist_type(idx=idx) for idx in np.arange(X.shape[1])]
        loss = 0.0
        with torch.no_grad():
            for dt in dist_types:
                loss = loss + dt.loss(X, X_hat)

        assert isinstance(loss, torch.Tensor)
        assert loss.shape == torch.Size([])

class TestDistTypeNotears:
    def test_schema_mismatch_error(self):
        X = np.ones(shape=(10, 2))
        schema = {0: "cont", 1: "cont", 2: "cont"}
        with pytest.raises(ValueError):
            from_numpy(X, schema)

    @pytest.mark.parametrize(
        "X, schema",
        [
            (np.random.normal(size=(10, 3)), {0: "cont", 1: "cont", 2: "cont"}),
            (np.random.randint(2, size=(10, 3)), {0: "bin", 1: "bin", 2: "bin"}),
            (
                np.hstack(
                    [np.random.normal(size=(5, 2)), np.random.randint(2, size=(5, 2))]
                ),
                {0: "cont", 1: "cont", 2: "bin", 3: "bin"},
            ),
        ],
    )
    def test_numpy_notears_with_schema(self, X, schema):
        from_numpy(X, schema)

    @pytest.mark.parametrize(
        "X, schema",
        [
            (np.random.normal(size=(10, 3)), {0: "cont", 1: "cont", 2: "cont"}),
            (np.random.randint(2, size=(10, 3)), {0: "bin", 1: "bin", 2: "bin"}),
            (
                np.hstack(
                    [np.random.normal(size=(5, 2)), np.random.randint(2, size=(5, 2))]
                ),
                {0: "cont", 1: "cont", 2: "bin", 3: "bin"},
            ),
        ],
    )
    def test_pandas_notears_with_schema(self, X, schema):
        X = pd.DataFrame(X)
        from_pandas(X, schema)

ext: py

reason: import numpy


text:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from random_walk import RandWalk

# pars
num_agents = 10
side = 100
time_steps = 400

randw = RandWalk(side, 4)

# Make initial array
arr = randw.create_init(num_agents)

# Run simulation
arrays = randw.run(arr, time_steps)

# Display arrays
fig, ax = plt.subplots()
ax.set_xticks([])
ax.set_yticks([])

def animate():
    ims = []
    for a in arrays:
        a = a.astype('float64')
        a[a == 0] = np.nan
        im = ax.imshow(a, vmin=0)
        ims.append([im])
    return ims

ani = animation.ArtistAnimation(fig, animate())
# ani.save('animation_random_walk.mp4')

plt.show()


text:
#%% Import libraries
import os
import numpy as np
from keras.models import load_model
import siamese_network as SN
import helper

#%% Define some params
DATA_PATH = "./dataset/faces"
MODEL_PATH = './models'
MODEL_NAME = 'siamese-face-model.h5'
NUM_TRIALS = 500 ## Trials for Testing Accuracy

#%% Main
## load the model
model = load_model(os.path.join(MODEL_PATH, MODEL_NAME),
                   custom_objects={'contrastive_loss': SN.contrastive_loss})

## test with many randomly selected images
matching = 0
for tiral in range(NUM_TRIALS):
    print("Now trial #%d of %d" % (tiral, NUM_TRIALS))

    pathlist = os.listdir(DATA_PATH)
    category = np.random.randint(len(pathlist))

    cur_path = os.path.join(DATA_PATH, pathlist[category])
    filelist = os.listdir(cur_path)
    index = np.random.randint(len(filelist))

    ref_image = helper.get_image(DATA_PATH, category, index)

    results = []
    for cat in range(len(pathlist)):
        filelist = os.listdir(os.path.join(DATA_PATH, pathlist[cat]))
        idx = np.random.randint(len(filelist))
        cur_image = helper.get_image(DATA_PATH, cat, idx)

        dist = model.predict([ref_image, cur_image])[0][0]
        results.append(dist)

    if category == np.argmin(results):
        matching += 1

print("Accuracy: %5.2f %%\n" % (100.0 * matching / NUM_TRIALS))

## select an image randomly (with only 1 image)
print("\n.... Now predict with the randomly selected image ....")
pathlist = os.listdir(DATA_PATH)
category = np.random.randint(len(pathlist))
cur_path = os.path.join(DATA_PATH, pathlist[category])
filelist = os.listdir(cur_path)
index = np.random.randint(len(filelist))

ref_image = helper.get_image(DATA_PATH, category, index)

results = []
for cat in range(len(pathlist)):
    filelist = os.listdir(os.path.join(DATA_PATH, pathlist[cat]))
    idx = np.random.randint(len(filelist))
    cur_image = helper.get_image(DATA_PATH, cat, idx)

    dist = model.predict([ref_image, cur_image])[0][0]
    results.append(dist)

print("Selected Category: %d" % (category))
print("Predicted Category: %d" % (np.argmin(results)))


text:
"""
June 17th, 2019. This python file combines past data with updated 2019 data points for further analysis.

"""

from fileLoading import loadExcel
from dateConv import noaaDateConv, decToDatetime
from scipy import stats

import pandas as pd
import datetime as dt
import calendar
import numpy as np

def methane():

    # import original dataset and new datasets
    methanePrev = loadExcel(r"C:\Users\ARL\Desktop\Jashan\Summit\analyses\Data\Methane.xlsx")
    methane2018 = loadExcel(r'C:\Users\ARL\Desktop\SUM_CH4_insitu_2018.xlsx')
    methane2019 = loadExcel(r'C:\Users\ARL\Desktop\Summit_GC_2019\CH4_results\SUM_CH4_insitu_2019.xlsx')

    # identify column names we want to keep
    goodcol = ['Decimal Year', 'Run median']                                                # good columns
    badcol = [x for x in methane2018.columns if x not in goodcol]                           # bad columns
    newnames = ['DecYear', 'MR']
    for sheet in [methane2018, methane2019]:
        sheet.drop(badcol, axis=1, inplace=True)                                            # drop bad columns
        sheet.dropna(how='any', axis=0, inplace=True)                                       # drop NaN rows
        sheet.columns = newnames                                                            # assign same col names

    methanePrev = methanePrev[methanePrev['DecYear'] < 2018]                                # remove some pre 2018 vals

    comb = [methanePrev, methane2018, methane2019]                                          # create combination frame
    methaneFinal = pd.concat(comb)                                                          # concat

    # trim extreme outliers
    values = methaneFinal['MR'].values
    z = np.abs(stats.zscore(values))
    thresh = 5
    methaneFinal = methaneFinal[~(z > thresh)]

    dates = decToDatetime(methaneFinal['DecYear'].values)                                        # conv to datetime
    methaneFinal['datetime'] = dates                                                        # add to dataframe

    noaaMethane = pd.DataFrame(columns=['datetime', 'MR'])
    noaaMethane['datetime'], noaaMethane['MR'] = dates, methaneFinal['MR'].values           # noaa version
    noaaMethane = noaaDateConv(noaaMethane)

    noaaMethane.to_csv('methane2019updated.txt', header=None, index=None, sep=' ', mode='w+')

    return methaneFinal

if __name__ == '__main__':
    methane()

max_stars_repo_name: ARLlab/Summit

max_issues_repo_name: ARLlab/Summit

max_forks_repo_name: ARLlab/Summit

reason: import numpy,from scipy


text:
# -*- coding: UTF-8 -*-
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddlepalm.reader.base_reader import Reader
from paddlepalm.reader.utils.reader4ernie import MaskLMReader as MLMReader
import numpy as np

class MaskLMReader(Reader):

    def __init__(self, vocab_path, max_len, tokenizer='wordpiece', \
             lang='en', seed=None, do_lower_case=False, phase='train', dev_count=1, print_prefix=''):
        """
        Args:
            phase: train, eval, pred
        """

        Reader.__init__(self, phase)

        assert lang.lower() in ['en', 'cn', 'english', 'chinese'], "supported language: en (English), cn (Chinese)."
        assert phase in ['train', 'predict'], "supported phase: train, predict."

        for_cn = lang.lower() == 'cn' or lang.lower() == 'chinese'

        self._register.add('mask_pos')
        if phase == 'train':
            self._register.add('mask_label')
        self._is_training = phase == 'train'

        mlm_reader = MLMReader(vocab_path,
                                max_seq_len=max_len,
                                do_lower_case=do_lower_case,
                                for_cn=for_cn,
                                random_seed=seed)
        self._reader = mlm_reader

        self._phase = phase
        self._dev_count = dev_count

    @property
    def outputs_attr(self):
        attrs = {"token_ids": [[-1, -1], 'int64'],
                "position_ids": [[-1, -1], 'int64'],
                "segment_ids": [[-1, -1], 'int64'],
                "input_mask": [[-1, -1, 1], 'float32'],
                "task_ids": [[-1, -1], 'int64'],
                "mask_label": [[-1], 'int64'],
                "mask_pos": [[-1], 'int64']
                }

        return self._get_registed_attrs(attrs)

    def load_data(self, input_file, batch_size, num_epochs=None, \
                  file_format='csv', shuffle_train=True):
        self._batch_size = batch_size
        self._num_epochs = num_epochs
        self._data_generator = self._reader.data_generator( \
            input_file, batch_size, num_epochs if self._phase == 'train' else 1, \
            shuffle=shuffle_train if self._phase == 'train' else False, \
            phase=self._phase)

    def _iterator(self):

        names = ['token_ids', 'position_ids', 'segment_ids', 'input_mask',
            'task_ids', 'mask_label', 'mask_pos']
        for batch in self._data_generator():
            outputs = {n: i for n,i in zip(names, batch)}
            ret = {}
            # TODO: move runtime shape check here
            for attr in self.outputs_attr.keys():
                ret[attr] = outputs[attr]

            yield ret

    def get_epoch_outputs(self):
        return {'examples': self._reader.get_examples(self._phase),
                'features': self._reader.get_features(self._phase)}

    @property
    def num_examples(self):
        return self._reader.get_num_examples(phase=self._phase)

    @property
    def num_epochs(self):
        return self._num_epochs

max_stars_repo_name: baajur/PALM

max_issues_repo_name: baajur/PALM

max_forks_repo_name: baajur/PALM


text:
#!/usr/bin/env python

import numpy as np
import random
import copy

import cPickle as pkl

def load_pickle(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)
import sys
sys.path.insert(0, '../lib_py')

import optparse
import lib_pyrender_br_savefig as libPyRender
from opendr.renderer import ColoredRenderer
from opendr.renderer import DepthRenderer
from opendr.lighting import LambertianPointLight
from opendr.camera import ProjectPoints
from smpl.smpl_webuser.serialization import load_model
from cv_bridge import CvBridge, CvBridgeError

from smpl.smpl_webuser.serialization import load_model as load_smpl_model

def load_pickle(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)

# some_file.py

#volumetric pose gen libraries
from multipose_lib_br import ArTagLib
from multipose_lib_br import VizLib

from visualization_lib_br import VisualizationLib
from preprocessing_lib_br import PreprocessingLib
from tensorprep_lib_br import TensorPrepLib
from unpack_batch_lib_br import UnpackBatchLib

from time import sleep
import rospy
import roslib
from sensor_msgs.msg import PointCloud2
from hrl_msgs.msg import FloatArrayBare
from ar_track_alvar_msgs.msg import AlvarMarkers
import sensor_msgs.point_cloud2
from scipy.stats import mode
import os.path as osp
import imutils

from scipy.ndimage.filters import gaussian_filter
DATASET_CREATE_TYPE = 1

import cv2
from camera import Camera

import math
from random import shuffle
import torch
import torch.nn as nn

import tensorflow as tensorflow
import cPickle as pickle
VERT_CUT, HORIZ_CUT = 0, 50
pre_VERT_CUT = 40

#IKPY
from ikpy.chain import Chain
from ikpy.link import OriginLink, URDFLink

#MISC
import time as time
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D

#hmr
from hmr.src.tf_smpl.batch_smpl import SMPL

SHOW_SMPL_EST = True
#PARTICIPANT = "S196"#"S151"
MAT_SIZE = (64, 27)

PC_WRT_ARTAG_ADJ = [0.11, -0.02, 0.07]
ARTAG_WRT_PMAT = [0.08, 0.05, 0.0]

DROPOUT = False
CAM_BED_DIST = 1.66

import sys

sys.path.insert(0, '/home/henry/git/volumetric_pose_gen/convnets')
import convnet_br as convnet
from torch.autograd import Variable

if False:#torch.cuda.is_available():
    # Use for GPU
    GPU = True
    dtype = torch.cuda.FloatTensor
    print '######################### CUDA is available! #############################'
else:
    # Use for CPU
    GPU = False
    dtype = torch.FloatTensor
    print '############################## USING CPU #################################'

class Viz3DPose():
    def __init__(self, opt):

        if opt.viz == '3D':
            self.pyRender = libPyRender.pyRenderMesh(render = True)
        else:
            self.pyRender = libPyRender.pyRenderMesh(render = False)

        ##load participant info
        participant_info = load_pickle("/media/henry/multimodal_data_2/CVPR2020_study/"+PARTICIPANT+"/participant_info.p")
        for entry in participant_info:
            print entry, participant_info[entry]

        self.gender = participant_info['gender']
        self.height_in = participant_info['height_in']
        self.weight_lbs = participant_info['weight_lbs']
        self.adj_2 = participant_info['adj_2']
        self.pose_type_list = participant_info['pose_type']
        self.calibration_optim_values = participant_info['cal_func']
        self.tf_corners = participant_info['corners']

        self.opt = opt

        self.index_queue = []
        if self.gender == "m":
            model_path = '../../../git/SMPL_python_v.1.0.0/smpl/models/basicModel_m_lbs_10_207_0_v1.0.0.pkl'
        else:
            model_path = '../../../git/SMPL_python_v.1.0.0/smpl/models/basicModel_f_lbs_10_207_0_v1.0.0.pkl'

        self.reset_pose = False
        self.m = load_model(model_path)

        self.pressure = None

        self.CTRL_PNL = {}
        self.CTRL_PNL['batch_size'] = 1
        self.CTRL_PNL['loss_vector_type'] = 'anglesDC'
        self.CTRL_PNL['verbose'] = False
        self.CTRL_PNL['num_epochs'] = 101
        self.CTRL_PNL['incl_inter'] = True
        self.CTRL_PNL['shuffle'] = False
        self.CTRL_PNL['incl_ht_wt_channels'] = opt.htwt
        self.CTRL_PNL['loss_root'] = opt.loss_root
        self.CTRL_PNL['omit_cntct_sobel'] = opt.omit_cntct_sobel
        self.CTRL_PNL['use_hover'] = opt.use_hover
        self.CTRL_PNL['incl_pmat_cntct_input'] = True
        self.CTRL_PNL['num_input_channels'] = 2
        self.CTRL_PNL['GPU'] = GPU
        self.CTRL_PNL['dtype'] = dtype
        self.CTRL_PNL['repeat_real_data_ct'] = 1
        self.CTRL_PNL['regr_angles'] = 1
        self.CTRL_PNL['dropout'] = DROPOUT
        self.CTRL_PNL['depth_map_labels'] = False
        self.CTRL_PNL['depth_map_output'] = True
        self.CTRL_PNL['depth_map_input_est'] = False#opt.pmr  # rue #do this if we're working in a two-part regression
        self.CTRL_PNL['adjust_ang_from_est'] = False#self.CTRL_PNL['depth_map_input_est']  # holds betas and root same as prior estimate
        self.CTRL_PNL['clip_sobel'] = True
        self.CTRL_PNL['clip_betas'] = True
        self.CTRL_PNL['mesh_bottom_dist'] = True
        self.CTRL_PNL['full_body_rot'] = True  # False
        self.CTRL_PNL['normalize_per_image'] = True
        if self.CTRL_PNL['normalize_per_image'] == False:
            self.CTRL_PNL['normalize_std'] = True
        else:
            self.CTRL_PNL['normalize_std'] = False
        self.CTRL_PNL['all_tanh_activ'] = True  # False
        self.CTRL_PNL['L2_contact'] = True  # False
        self.CTRL_PNL['pmat_mult'] = int(1)
        self.CTRL_PNL['cal_noise'] = opt.calnoise
        self.CTRL_PNL['cal_noise_amt'] = 0.1
        self.CTRL_PNL['output_only_prev_est'] = False
        self.CTRL_PNL['double_network_size'] = False
        self.CTRL_PNL['first_pass'] = True
        self.CTRL_PNL['align_procr'] = False

        if self.CTRL_PNL['cal_noise'] == True:
            self.CTRL_PNL[
                'incl_pmat_cntct_input'] = False  # if there's calibration noise we need to recompute this every batch
            self.CTRL_PNL['clip_sobel'] = False

        if self.CTRL_PNL['incl_pmat_cntct_input'] == True:
            self.CTRL_PNL['num_input_channels'] += 1
        if self.CTRL_PNL['depth_map_input_est'] == True:  # for a two part regression
            self.CTRL_PNL['num_input_channels'] += 3
        self.CTRL_PNL['num_input_channels_batch0'] = np.copy(self.CTRL_PNL['num_input_channels'])
        if self.CTRL_PNL['incl_ht_wt_channels'] == True:
            self.CTRL_PNL['num_input_channels'] += 2
        if self.CTRL_PNL['cal_noise'] == True:
            self.CTRL_PNL['num_input_channels'] += 1

        pmat_std_from_mult = ['N/A', 11.70153502792190, 19.90905848383454, 23.07018866032369, 0.0, 25.50538629767412]
        if self.CTRL_PNL['cal_noise'] == False:
            sobel_std_from_mult = ['N/A', 29.80360490415032, 33.33532963163579, 34.14427844692501, 0.0,
                                   34.86393494050921]
        else:
            sobel_std_from_mult = ['N/A', 45.61635847182483, 77.74920396659292, 88.89398421073700, 0.0,
                                   97.90075708182506]

        self.CTRL_PNL['norm_std_coeffs'] = [1. / 41.80684362163343,  # contact
                                            1. / 16.69545796387731,  # pos est depth
                                            1. / 45.08513083167194,  # neg est depth
                                            1. / 43.55800622930469,  # cm est
                                            1. / pmat_std_from_mult[int(self.CTRL_PNL['pmat_mult'])],  # pmat x5
                                            1. / sobel_std_from_mult[int(self.CTRL_PNL['pmat_mult'])],  # pmat sobel
                                            1. / 1.0,  # bed height mat
                                            1. / 1.0,  # OUTPUT DO NOTHING
                                            1. / 1.0,  # OUTPUT DO NOTHING
                                            1. / 30.216647403350,  # weight
                                            1. / 14.629298141231]  # height

        if self.CTRL_PNL['normalize_std'] == False:
            for i in range(10):
                self.CTRL_PNL['norm_std_coeffs'][i] *= 0.
                self.CTRL_PNL['norm_std_coeffs'][i] += 1.

        if self.CTRL_PNL['depth_map_output'] == True:  # we need all the vertices if we're going to regress the depth maps
            self.verts_list = "all"

        self.TPL = TensorPrepLib()

        self.count = 0

        self.CTRL_PNL['filepath_prefix'] = '/home/henry/'
        self.CTRL_PNL['aws'] = False
        self.CTRL_PNL['lock_root'] = False

        self.color, self.depth_r, self.pressure = 0, 0, 0

        self.kinect_im_size = (960, 540)

        self.final_dataset = {}
        self.setup_allmodes()

    def load_new_participant_info(self, participant_directory):
        ##load participant info
        participant_info = load_pickle(participant_directory+"/participant_info_red.p")
        print "participant directory: ", participant_directory
        for entry in participant_info:
            print entry, participant_info[entry]

        self.gender = participant_info['gender']
        self.height_in = participant_info['height_in']
        self.weight_lbs = participant_info['weight_lbs']

    def setup_allmodes(self):

        self.reset_pose = False

        self.marker0, self.marker1, self.marker2, self.marker3 = None, None, None, None
        self.pressure = None
        self.markers = [self.marker0, self.marker1, self.marker2, self.marker3]
        self.point_cloud_array = np.array([[0., 0., 0.]])
        self.pc_isnew = False

        self.bridge = CvBridge()
        self.color, self.depth_r, self.pressure = 0, 0, 0

        self.kinect_im_size = (960, 540)
        self.pressure_im_size = (64, 27)
        self.pressure_im_size_required = (64, 27)

        # initialization of kinect and thermal cam calibrations from YAML files
        dist_model = 'rational_polynomial'
        self.kcam = Camera('kinect', self.kinect_im_size, dist_model)
        self.kcam.init_from_yaml(osp.expanduser('~/catkin_ws/src/multimodal_pose/calibrations/kinect.yaml'))

        # we are at qhd not hd so need to cut the focal lengths and centers in half
        self.kcam.K[0:2, 0:3] = self.kcam.K[0:2, 0:3] / 2

       # print self.kcam.K

        self.new_K_kin, roi = cv2.getOptimalNewCameraMatrix(self.kcam.K, self.kcam.D, self.kinect_im_size, 1,
                                                            self.kinect_im_size)

        #print self.new_K_kin

        self.drawing = False  # true if mouse is pressed
        self.mode = True  # if True, draw rectangle. Press 'm' to toggle to curve
        self.ix, self.iy = -1, -1
        self.label_index = 0
        self.coords_from_top_left = [0, 0]
        self.overall_image_scale_amount = 0.85
        self.depthcam_midpixel = [0, 0]
        self.depthcam_midpixel2 = [0, 0]
        self.select_new_calib_corners = {}
        self.select_new_calib_corners["lay"] = True
        self.select_new_calib_corners["sit"] = True
        self.calib_corners = {}
        self.calib_corners["lay"] = 8 * [[0, 0]]
        self.calib_corners["sit"] = 8 * [[0, 0]]

        self.final_dataset = {}

        self.filler_taxels = []
        for i in range(28):
            for j in range(65):
                self.filler_taxels.append([i - 1, j - 1, 20000])
        self.filler_taxels = np.array(self.filler_taxels).astype(int)

    def load_next_file(self, newpath):

        print "loading existing npy files in the new path...."
        time_orig = time.time()
        self.color_all = np.load(newpath+"/color.npy")
        self.depth_r_all = np.load(newpath+"/depth_r.npy")
        self.pressure_all = np.load(newpath+"/pressure.npy")
        self.bedstate_all = np.load(newpath+"/bedstate.npy")
        self.markers_all = np.load(newpath+"/markers.npy", allow_pickle=True)
        self.time_stamp_all = np.load(newpath+"/time_stamp.npy")
        self.point_cloud_autofil_all = np.load(newpath+"/point_cloud.npy")
        #self.config_code_all = np.load(newpath+"/config_code.npy")
        print "Finished. Time taken: ", time.time() - time_orig

    def transform_selected_points(self, image, camera_alpha_vert, camera_alpha_horiz, angle, right, up, h_scale_cut, v_scale_cut, coords_subset):
        h_scale = h_scale_cut[0]
        h_cut = h_scale_cut[1]
        v_scale = v_scale_cut[0]
        v_cut = v_scale_cut[1]
        tf_coords_subset = np.copy(coords_subset)
        #print camera_alpha_vert, camera_alpha_horiz, HORIZ_CUT, VERT_CUT, pre_VERT_CUT, right

        h = VizLib().get_new_K_kin_homography(camera_alpha_vert, camera_alpha_horiz, self.new_K_kin, flip_vert=-1)

        for i in range(4):

            new_coords = np.matmul(h, np.array([tf_coords_subset[i, 1]+pre_VERT_CUT, tf_coords_subset[i, 0]+HORIZ_CUT, 1]))
            new_coords = new_coords/new_coords[2]
            tf_coords_subset[i, 0] = new_coords[1] - HORIZ_CUT
            tf_coords_subset[i, 1] = new_coords[0] - pre_VERT_CUT

            tf_coords_subset[i, 1] = (tf_coords_subset[i, 1] - image.shape[0] / 2) * np.cos(np.deg2rad(angle)) - (
                        tf_coords_subset[i, 0] - image.shape[1] / 2) * np.sin(np.deg2rad(angle)) + image.shape[
                                  0] / 2 - up
            tf_coords_subset[i, 0] = (tf_coords_subset[i, 1] - image.shape[0] / 2) * np.sin(np.deg2rad(angle)) + (
                        tf_coords_subset[i, 0] - image.shape[1] / 2) * np.cos(np.deg2rad(angle)) + image.shape[
                                  1] / 2 - right

            tf_coords_subset[i, 0] = h_scale * (tf_coords_subset[i][0] + h_cut) - h_cut
            tf_coords_subset[i, 1] = v_scale * (tf_coords_subset[i][1] + v_cut) - v_cut

            image[int(tf_coords_subset[i][1] + 0.5) - 2:int(tf_coords_subset[i][1] + 0.5) + 2,
            int(tf_coords_subset[i][0] + 0.5) - 2:int(tf_coords_subset[i][0] + 0.5) + 2, :] = 255

        return tf_coords_subset, image

    def rotate_selected_head_points(self, pressure_im_size_required, u_c_pmat, v_c_pmat, u_p_bend, v_p_bend, u_p_bend_calib, v_p_bend_calib):

        low_vert = np.rint(v_c_pmat[2]).astype(np.uint16)
        low_horiz = np.rint(u_c_pmat[1]).astype(np.uint16)
        legs_bend_loc2 = pressure_im_size_required[0]*20/64 + low_horiz

        HEAD_BEND_TAXEL = 41  # measured from the bottom of the pressure mat
        LEGS_BEND2_TAXEL = 20 #measured from the bottom of the pressure mat
        head_bend_loc = pressure_im_size_required[0]*HEAD_BEND_TAXEL/64 + low_horiz

        head_points_L = [np.rint(v_p_bend_calib[0]).astype(np.uint16) - 3 - HORIZ_CUT + 4,
                         380-np.rint(u_p_bend_calib[0] - head_bend_loc - 3).astype(np.uint16) - pre_VERT_CUT + 4]  # np.copy([head_points1[2][0] - decrease_from_orig_len, head_points1[2][1] - increase_across_pmat])
        head_points_R = [np.rint(v_p_bend_calib[1]).astype(np.uint16) + 4 - HORIZ_CUT - 4,
                         380-np.rint(u_p_bend_calib[1] - head_bend_loc - 3).astype(np.uint16) - pre_VERT_CUT + 4]  # np.copy([head_points1[3][0] - decrease_from_orig_len, head_points1[3][1] + increase_across_pmat])
        legs_points_pre = [pressure_im_size_required[0] * 64 / 64 - pressure_im_size_required[0] * (64 - LEGS_BEND2_TAXEL) / 64, low_vert]  # happens at legs bend2

        legs_points_L = [np.rint(v_p_bend[4]).astype(np.uint16) - 3 - HORIZ_CUT + 4,
                         head_bend_loc - pressure_im_size_required[0] * HEAD_BEND_TAXEL / 64 + 560]  # happens at legs bottom
        legs_points_R = [np.rint(v_p_bend[5]).astype(np.uint16) + 4 - HORIZ_CUT - 4,
                         head_bend_loc - pressure_im_size_required[0] * HEAD_BEND_TAXEL / 64 + 560]  # happens at legs bottom

        return [head_points_L, head_points_R, legs_points_L, legs_points_R]

    def get_pc_from_depthmap(self, bed_angle, zero_location):

       # print zero_location, 'zero loc'

        #transform 3D pc using homography!
        #bed_angle = 0.
        #x and y are pixel selections

        zero_location += 0.5
        zero_location = zero_location.astype(int)

        x = np.arange(0, 440).astype(float)
        x = np.tile(x, (880, 1))
        y = np.arange(0, 880).astype(float)
        y = np.tile(y, (440, 1)).T

        x_coord_from_camcenter = x - self.depthcam_midpixel[0]
        y_coord_from_camcenter = y - self.depthcam_midpixel[1]

        #here try transforming the 2D representation before we move on to 3D

        depth_value = self.depth_r_orig.astype(float) / 1000

        f_x, f_y, c_x, c_y = self.new_K_kin[0, 0], self.new_K_kin[1, 1], self.new_K_kin[0, 2], self.new_K_kin[1, 2]
        X = (x_coord_from_camcenter) * depth_value / f_y
        Y = (y_coord_from_camcenter) * depth_value / f_x

        x_coord_from_camcenter_single = zero_location[0] - self.depthcam_midpixel[0]
        y_coord_from_camcenter_single = zero_location[1] - self.depthcam_midpixel[1]
        X_single = (x_coord_from_camcenter_single) * CAM_BED_DIST / f_y
        Y_single = (y_coord_from_camcenter_single) * CAM_BED_DIST / f_x

        #print X_single, Y_single, 'Y single'
        X -= X_single
        Y -= (Y_single)

        Y = -Y
        Z = -depth_value + CAM_BED_DIST

        point_cloud = np.stack((Y, X, -Z))
        point_cloud = np.swapaxes(point_cloud, 0, 2)
        point_cloud = np.swapaxes(point_cloud, 0, 1)

        point_cloud_red = np.zeros((point_cloud.shape[0]/10, point_cloud.shape[1]/10, 3))
        for j in range(point_cloud_red.shape[0]):
            for i in range(point_cloud_red.shape[1]):
                point_cloud_red[j, i, :] = np.median(np.median(point_cloud[j*10:(j+1)*10, i*10:(i+1)*10, :], axis = 0), axis = 0)
        self.point_cloud_red = point_cloud_red.reshape(-1, 3)
        self.point_cloud = point_cloud.reshape(-1, 3)
        self.point_cloud[:, 0] += PC_WRT_ARTAG_ADJ[0] + ARTAG_WRT_PMAT[0]
        self.point_cloud[:, 1] += PC_WRT_ARTAG_ADJ[1] + ARTAG_WRT_PMAT[1]
        self.point_cloud[:, 2] += PC_WRT_ARTAG_ADJ[2] + ARTAG_WRT_PMAT[2]
        #print point_cloud.shape, 'pc shape'
        #print point_cloud_red.shape

        return X, Y, Z

    def trim_pc_sides(self, tf_corners, camera_alpha_vert, camera_alpha_horiz, h, kinect_rot_cw):

        f_x, f_y, c_x, c_y = self.new_K_kin[0, 0], self.new_K_kin[1, 1], self.new_K_kin[0, 2], self.new_K_kin[1, 2]
        #for i in range(3):
        #    print np.min(self.point_cloud_autofil[:, i]), np.max(self.point_cloud_autofil[:, i])

        self.point_cloud_autofil[:, 0] = self.point_cloud_autofil[:, 0]# - 0.17 - 0.036608

        #CALIBRATE THE POINT CLOUD HERE

        pc_autofil_red = np.copy(self.point_cloud_autofil)

        if pc_autofil_red.shape[0] == 0:
            pc_autofil_red = np.array([[0.0, 0.0, 0.0]])

        #warp it by the homography i.e. rotate a bit
        pc_autofil_red -=[0.0, 0.0, CAM_BED_DIST]

        theta_1 = np.arctan((camera_alpha_vert-1)*CAM_BED_DIST/(270*CAM_BED_DIST/f_y))/2 #short side
        short_side_rot = np.array([[1.0, 0.0, 0.0], [0.0, np.cos(theta_1), -np.sin(theta_1)], [0.0, np.sin(theta_1), np.cos(theta_1)]])
        pc_autofil_red = np.matmul(pc_autofil_red, short_side_rot)#[0:3, :]

        theta_2 = np.arctan((1-camera_alpha_horiz)*CAM_BED_DIST/(270*CAM_BED_DIST/f_x))/2 #long side
        long_side_rot = np.array([[np.cos(theta_2), 0.0, np.sin(theta_2)], [0.0, 1.0, 0.0], [-np.sin(theta_2), 0.0, np.cos(theta_2)]])
        pc_autofil_red = np.matmul(pc_autofil_red, long_side_rot)#[0:3, :]

        pc_autofil_red +=[0.0, 0.0, CAM_BED_DIST]

        #add the warping translation
        X_single1 = h[0, 2] * CAM_BED_DIST / f_y
        Y_single1 = h[1, 2] * CAM_BED_DIST / f_x

        #print X_single1, Y_single1
        pc_autofil_red += [-Y_single1/2, -X_single1/2, 0.0]

        #rotate normal to the bed
        angle = kinect_rot_cw*np.pi/180.
        z_rot_mat = np.array([[np.cos(angle), -np.sin(angle), 0], [np.sin(angle), np.cos(angle), 0], [0.0, 0.0, 1.0]])
        pc_autofil_red = np.matmul(pc_autofil_red, z_rot_mat)#[0:3, :]

        #translate by the picture shift amount in the x and y directions

        #print np.min(pc_autofil_red[:, 0]), np.max(pc_autofil_red[:, 0]), "Y min max"
        #print self.tf_corners[2], self.depthcam_midpixel2

        #translate from the 0,0 being the camera to 0,0 being the left corner of the bed measured by the clicked point
        zero_location = np.copy(self.tf_corners[2]) #TF corner needs to be manipulated!
        x_coord_from_camcenter_single = zero_location[0] - self.depthcam_midpixel2[0]
        y_coord_from_camcenter_single = zero_location[1] - self.depthcam_midpixel2[1]
        X_single2 = (x_coord_from_camcenter_single) * CAM_BED_DIST / f_y #shift dim
        Y_single2 = (y_coord_from_camcenter_single) * CAM_BED_DIST / f_x #long dim
        pc_autofil_red += [Y_single2, -X_single2, -CAM_BED_DIST]

        #adjust to fit to the lower left corner step 2
        pc_autofil_red += [self.adj_2[0], self.adj_2[1], 0.0]

        #pc_autofil_red = np.swapaxes(np.array(self.pc_all).reshape(3, 440*880), 0, 1)

        #print np.min(pc_autofil_red[:, 0]), np.max(pc_autofil_red[:, 0]), "Y min max"

        #cut off everything that's not overlying the bed.
        pc_autofil_red = pc_autofil_red[pc_autofil_red[:, 1] > 0.0, :]
        pc_autofil_red = pc_autofil_red[pc_autofil_red[:, 1] < 0.0286 * 27, :]

        pc_autofil_red = pc_autofil_red[pc_autofil_red[:, 0] > 0.0, :] #up and down bed
        pc_autofil_red = pc_autofil_red[pc_autofil_red[:, 0] < 0.0286 * 64 * 1.04, :] #up and down bed

        #adjust it by a half taxel width
        #pc_autofil_red += [0.0143, 0.0143, 0.0]

        return pc_autofil_red

    def estimate_pose(self, pmat, bedangle, markers_c, model, model2, tf_corners, camera_alpha_vert, camera_alpha_horiz, h, kinect_rot_cw, color_im):

        bedangle = 0

        mat_size = (64, 27)

        pmat = np.fliplr(np.flipud(np.clip(pmat.reshape(MAT_SIZE) * float(self.CTRL_PNL['pmat_mult']), a_min=0, a_max=100)))
        pmat = np.clip(pmat.reshape(MAT_SIZE) * float(self.CTRL_PNL['pmat_mult']), a_min=0, a_max=100)

        if self.CTRL_PNL['cal_noise'] == False:
            pmat = gaussian_filter(pmat, sigma=0.5)

        pmat_stack = PreprocessingLib().preprocessing_create_pressure_angle_stack([pmat], mat_size, self.CTRL_PNL)[0]

        if self.CTRL_PNL['cal_noise'] == False and self.CTRL_PNL['normalize_per_image'] == False:
            pmat_stack = np.clip(pmat_stack, a_min=0, a_max=100)

        pmat_stack = np.expand_dims(np.array(pmat_stack), 0)
        print pmat_stack.shape

        if self.CTRL_PNL['incl_pmat_cntct_input'] == True:
            pmat_contact = np.copy(pmat_stack[:, 0:1, :, :])
            pmat_contact[pmat_contact > 0] = 100
            pmat_stack = np.concatenate((pmat_contact, pmat_stack), axis=1)

        weight_input = self.weight_lbs / 2.20462
        height_input = (self.height_in * 0.0254 - 1) * 100

        batch1 = np.zeros((1, 162))
        if self.gender == 'f':
            batch1[:, 157] += 1
        elif self.gender == 'm':
            batch1[:, 158] += 1
        batch1[:, 160] += weight_input
        batch1[:, 161] += height_input

        if self.CTRL_PNL['normalize_std'] == True:
            self.CTRL_PNL['depth_map_input_est'] = False
            pmat_stack = self.TPL.normalize_network_input(pmat_stack, self.CTRL_PNL)
            batch1 = self.TPL.normalize_wt_ht(batch1, self.CTRL_PNL)

        pmat_array_input = np.copy(pmat_stack)[0, 0, :, :]

        pmat_stack = torch.Tensor(pmat_stack)
        batch1 = torch.Tensor(batch1)

        if DROPOUT == True:
            pmat_stack = pmat_stack.repeat(25, 1, 1, 1)
            batch1 = batch1.repeat(25, 1)

        batch = []
        batch.append(pmat_stack)
        batch.append(batch1)

        NUMOFOUTPUTDIMS = 3
        NUMOFOUTPUTNODES_TRAIN = 24
        self.output_size_train = (NUMOFOUTPUTNODES_TRAIN, NUMOFOUTPUTDIMS)

        self.CTRL_PNL['adjust_ang_from_est'] = False
        scores, INPUT_DICT, OUTPUT_DICT = UnpackBatchLib().unpack_batch(batch, is_training=False, model=model,
                                                                                CTRL_PNL = self.CTRL_PNL)

        mdm_est_pos = OUTPUT_DICT['batch_mdm_est'].clone().unsqueeze(1)# / 16.69545796387731
        mdm_est_neg = OUTPUT_DICT['batch_mdm_est'].clone().unsqueeze(1)# / 45.08513083167194
        mdm_est_pos[mdm_est_pos < 0] = 0
        mdm_est_neg[mdm_est_neg > 0] = 0
        mdm_est_neg *= -1
        cm_est = OUTPUT_DICT['batch_cm_est'].clone().unsqueeze(1) * 100# / 43.55800622930469

        # 1. / 16.69545796387731,  # pos est depth
        # 1. / 45.08513083167194,  # neg est depth
        # 1. / 43.55800622930469,  # cm est

        sc_sample1 = OUTPUT_DICT['batch_targets_est'].clone()
        sc_sample1 = sc_sample1[0, :].squeeze() / 1000
        sc_sample1 = sc_sample1.view(self.output_size_train)
        # print sc_sample1

        if model2 is not None:
            print "Using model 2"
            batch_cor = []

            if self.CTRL_PNL['cal_noise'] == False:
                batch_cor.append(torch.cat((pmat_stack[:, 0:1, :, :],
                                            mdm_est_pos.type(torch.FloatTensor),
                                            mdm_est_neg.type(torch.FloatTensor),
                                            cm_est.type(torch.FloatTensor),
                                            pmat_stack[:, 1:, :, :]), dim=1))
            else:
                if self.opt.pmr == True:
                    batch_cor.append(torch.cat((mdm_est_pos.type(torch.FloatTensor),
                                                mdm_est_neg.type(torch.FloatTensor),
                                                cm_est.type(torch.FloatTensor),
                                                pmat_stack[:, 0:, :, :]), dim=1))
                else:
                    batch_cor.append(pmat_stack)

            if self.CTRL_PNL['full_body_rot'] == False:
                batch_cor.append(torch.cat((batch1,
                                            OUTPUT_DICT['batch_betas_est'].cpu(),
                                            OUTPUT_DICT['batch_angles_est'].cpu(),
                                            OUTPUT_DICT['batch_root_xyz_est'].cpu()), dim=1))
            elif self.CTRL_PNL['full_body_rot'] == True:
                batch_cor.append(torch.cat((batch1,
                                            OUTPUT_DICT['batch_betas_est'].cpu(),
                                            OUTPUT_DICT['batch_angles_est'].cpu(),
                                            OUTPUT_DICT['batch_root_xyz_est'].cpu(),
                                            OUTPUT_DICT['batch_root_atan2_est'].cpu()), dim=1))

            self.CTRL_PNL['adjust_ang_from_est'] = True

            if self.opt.pmr == True:
                self.CTRL_PNL['num_input_channels_batch0'] += 3

            print self.CTRL_PNL['num_input_channels_batch0'], batch_cor[0].size()

            scores, INPUT_DICT, OUTPUT_DICT = UnpackBatchLib().unpack_batch(batch_cor, is_training=False, model=model2,
                                                                                CTRL_PNL = self.CTRL_PNL)
            if self.opt.pmr == True:
                self.CTRL_PNL['num_input_channels_batch0'] -= 3

        self.CTRL_PNL['first_pass'] = False

        # print betas_est, root_shift_est, angles_est
        if self.CTRL_PNL['dropout'] == True:
            #print OUTPUT_DICT['verts'].shape
            smpl_verts = np.mean(OUTPUT_DICT['verts'], axis = 0)
            dropout_variance = np.std(OUTPUT_DICT['verts'], axis=0)
            dropout_variance = np.linalg.norm(dropout_variance, axis = 1)
        else:
            smpl_verts = OUTPUT_DICT['verts'][0, :, :]
            dropout_variance = None

        self.RESULTS_DICT['betas'].append(OUTPUT_DICT['batch_betas_est_post_clip'].cpu().numpy()[0])

        smpl_verts = np.concatenate((smpl_verts[:, 1:2] - 0.286 + 0.0143, smpl_verts[:, 0:1] - 0.286 + 0.0143, 0.0 -smpl_verts[:, 2:3]), axis = 1)
        smpl_faces = np.array(self.m.f)

        pc_autofil_red = self.trim_pc_sides(tf_corners, camera_alpha_vert, camera_alpha_horiz, h, kinect_rot_cw) #this is the point cloud

        q = OUTPUT_DICT['batch_mdm_est'].data.numpy().reshape(OUTPUT_DICT['batch_mdm_est'].size()[0], 64, 27) * -1
        q = np.mean(q, axis = 0)

        camera_point = [1.09898028, 0.46441343, -CAM_BED_DIST]

        if SHOW_SMPL_EST == False:
            smpl_verts *= 0.001

        #print smpl_verts

        viz_type = "3D"

        self.RESULTS_DICT['body_roll_rad'].append(float(OUTPUT_DICT['batch_angles_est'][0, 1]))

        if viz_type == "2D":
            from visualization_lib import VisualizationLib
            if model2 is not None:
                self.im_sample = INPUT_DICT['batch_images'][0, 4:,:].squeeze() * 20.  # normalizing_std_constants[4]*5.  #pmat
            else:
                self.im_sample = INPUT_DICT['batch_images'][0, 1:,:].squeeze() * 20.  # normalizing_std_constants[4]*5.  #pmat
            self.im_sample_ext = INPUT_DICT['batch_images'][0, 0:, :].squeeze() * 20.  # normalizing_std_constants[0]  #pmat contact
            # self.im_sample_ext2 = INPUT_DICT['batch_images'][im_display_idx, 2:, :].squeeze()*20.#normalizing_std_constants[4]  #sobel
            self.im_sample_ext3 = OUTPUT_DICT['batch_mdm_est'][0, :, :].squeeze().unsqueeze(0) * -1  # est depth output

            # print scores[0, 10:16], 'scores of body rot'

            # print self.im_sample.size(), self.im_sample_ext.size(), self.im_sample_ext2.size(), self.im_sample_ext3.size()

            # self.publish_depth_marker_array(self.im_sample_ext3)

            self.tar_sample = INPUT_DICT['batch_targets']
            self.tar_sample = self.tar_sample[0, :].squeeze() / 1000
            sc_sample = OUTPUT_DICT['batch_targets_est'].clone()
            sc_sample = sc_sample[0, :].squeeze() / 1000

            sc_sample = sc_sample.view(self.output_size_train)

            VisualizationLib().visualize_pressure_map(self.im_sample, sc_sample1, sc_sample,
                                                         # self.im_sample_ext, None, None,
                                                          self.im_sample_ext3, None, None, #, self.tar_sample_val, self.sc_sample_val,
                                                          block=False)

            time.sleep(4)

        elif viz_type == "3D":
            #render everything

            self.RESULTS_DICT = self.pyRender.render_mesh_pc_bed_pyrender_everything(smpl_verts, smpl_faces, camera_point,
                                                                  bedangle, self.RESULTS_DICT,
                                                                  pc = pc_autofil_red, pmat = pmat_array_input, smpl_render_points = False,
                                                                  markers = [[0.0, 0.0, 0.0],[0.0, 1.5, 0.0],[0.0, 0.0, 0.0],[0.0, 0.0, 0.0]],
                                                                                dropout_variance = dropout_variance, color_im = color_im,
                                                                                tf_corners=tf_corners, current_pose_type_ct=self.current_pose_type_ct,
                                                                                participant = PARTICIPANT)

            time.sleep(1)
            self.point_cloud_array = None

    def evaluate_data(self, dat, filename1, filename2=None):

        self.pyRender = libPyRender.pyRenderMesh(render = True)

        #model = torch.load(filename1, map_location={'cuda:5': 'cuda:0'})
        if GPU == True:
            for i in range(0, 8):
                try:
                    model = torch.load(filename1, map_location={'cuda:'+str(i):'cuda:0'})
                    if self.CTRL_PNL['dropout'] == True:
                        model = model.cuda().train()
                    else:
                        model = model.cuda().eval()
                    break
                except:
                    pass
            if filename2 is not None:
                for i in range(0, 8):
                    try:
                        model2 = torch.load(filename2, map_location={'cuda:'+str(i):'cuda:0'})
                        if self.CTRL_PNL['dropout'] == True:
                            model2 = model2.cuda().train()
                        else:
                            model2 = model2.cuda().eval()
                        break
                    except:
                        pass
            else:
                model2 = None
        else:
            model = torch.load(filename1, map_location='cpu')
            if self.CTRL_PNL['dropout'] == True:
                model = model.train()
            else:
                model = model.eval()
            if filename2 is not None:
                model2 = torch.load(filename2, map_location='cpu')
                if self.CTRL_PNL['dropout'] == True:
                    model2 = model2.train()
                else:
                    model2 = model2.eval()
            else:
                model2 = None

        #function_input = np.array(function_input)*np.array([10, 10, 10, 10, 10, 10, 0.1, 0.1, 0.1, 0.1, 1])
        #function_input += np.array([2.2, 32, -1, 1.2, 32, -5, 1.0, 1.0, 0.96, 0.95, 0.8])
        function_input = np.array(self.calibration_optim_values)*np.array([10, 10, 10, 0.1, 0.1, 0.1, 0.1])
        function_input += np.array([1.2, 32, -5, 1.0, 1.0, 0.96, 0.95])

        kinect_rotate_angle = function_input[3-3]
        kinect_shift_up = int(function_input[4-3])# - 40
        kinect_shift_right = int(function_input[5-3])# - 20
        camera_alpha_vert = function_input[6-3]
        camera_alpha_horiz = function_input[7-3]
        pressure_horiz_scale = function_input[8-3]
        pressure_vert_scale = function_input[9-3]
        #head_angle_multiplier = function_input[10-3]

        #print kinect_shift_up, kinect_shift_right, "SHIFT UP RIGHT"
        #print pressure_horiz_scale, pressure_vert_scale, "PRESSURE SCALES" #1.04 for one too far to left

        #file_dir = "/media/henry/multimodal_data_1/all_hevans_data/0905_2_Evening/0255"
        #file_dir_list = ["/media/henry/multimodal_data_2/test_data/data_072019_0001/"]
        blah = True

        #file_dir = "/media/henry/multimodal_data_2/test_data/data_072019_0007"
        #file_dir = "/media/henry/multimodal_data_2/test_data/data_072019_0006"
        #file_dir = "/home/henry/ivy_test_data/data_102019_kneeup_0000"
        #file_dir = "/media/henry/multimodal_data_1/CVPR2020_study/P000/data_102019_kneeup_0000"

        if PARTICIPANT == "P106":
            #file_dir = "/media/henry/multimodal_data_1/CVPR2020_study/"+PARTICIPANT+"/data_"+PARTICIPANT+"_000"
            file_dir = "/home/henry/Desktop/CVPR2020_study/"+PARTICIPANT+"/data_"+PARTICIPANT+"_000"
            file_dirs = [#file_dir+str(0),
                         file_dir+str(1),
                         file_dir+str(2),
                         file_dir+str(3),
                         file_dir+str(4),
                         file_dir+str(5)]
        else:
            #file_dir = "/media/henry/multimodal_data_1/CVPR2020_study/"+PARTICIPANT+"/data_"+PARTICIPANT+"-2_000"
            file_dir = "/media/henry/multimodal_data_2/CVPR2020_study/"+PARTICIPANT+"/data_checked_"+PARTICIPANT+"-"+POSE_TYPE
            file_dirs = [file_dir]
            #file_dir = "/media/henry/multimodal_data_1/CVPR2020_study/"+PARTICIPANT+"/data_"+PARTICIPANT+"-2_000"
            #file_dir = "/media/henry/multimodal_data_2/CVPR2020_study/"+PARTICIPANT+"/data_"+PARTICIPANT+"-C_0000"
            #file_dirs = [file_dir]

        self.RESULTS_DICT = {}
        self.RESULTS_DICT['body_roll_rad'] = []
        self.RESULTS_DICT['v_to_gt_err'] = []
        self.RESULTS_DICT['v_limb_to_gt_err'] = []
        self.RESULTS_DICT['gt_to_v_err'] = []
        self.RESULTS_DICT['precision'] = []
        self.RESULTS_DICT['recall'] = []
        self.RESULTS_DICT['overlap_d_err'] = []
        self.RESULTS_DICT['all_d_err'] = []
        self.RESULTS_DICT['betas'] = []

        init_time = time.time()

        for file_dir in file_dirs:
            V3D.load_next_file(file_dir)

            start_num = 0
            #print self.color_all.shape

            #for im_num in range(29, 100):
            for im_num in range(start_num, self.color_all.shape[0]):

                #For P188: skip 5. 13 good cross legs

                print "NEXT IM!", im_num, " ", time.time() - init_time

                if PARTICIPANT == "S114" and POSE_TYPE == "2"  and im_num in [26, 29]: continue #these don't have point clouds
                if PARTICIPANT == "S165" and POSE_TYPE == "2" and im_num in [1, 3, 15]: continue #these don't have point clouds
                if PARTICIPANT == "S188" and POSE_TYPE == "2"  and im_num in [5, 17, 21]: continue

                if POSE_TYPE == "2":
                    im_num_ct = im_num + 1
                else:
                    im_num_ct = float(im_num)

                if POSE_TYPE == "1":
                    self.current_pose_type_ct = '{:02}'.format(im_num_ct)+'_natural_'+NETWORK_2

                elif POSE_TYPE == "2":
                    self.current_pose_type_ct = '{:02}'.format(im_num_ct)+'_'+self.pose_type_list[im_num]+'_'+NETWORK_2

                #good picks: 103 - 6 good for what info is there
                            #151 11 is  good
                            #179 - 7 is great
                            #187 natural poses very good
                            #196 - 11 has great smile :)

                self.overall_image_scale_amount = 0.85

                half_w_half_l = [0.4, 0.4, 1.1, 1.1]

                all_image_list = []
                self.label_single_image = []

                self.label_index = 0

                self.color = self.color_all[im_num]
                self.depth_r = self.depth_r_all[im_num]
                self.pressure = self.pressure_all[im_num]
                self.bed_state = self.bedstate_all[im_num]

                if self.point_cloud_autofil_all[im_num].shape[0] == 0:
                    self.point_cloud_autofil_all[im_num] = np.array([[0.0, 0.0, 0.0]])
                self.point_cloud_autofil = self.point_cloud_autofil_all[im_num] + self.markers_all[im_num][2]#[0.0, 0.0, 0.0]#0.1]
                #print self.markers_all[im_num]
                #print self.point_cloud_autofil.shape, 'PC AUTOFIL ORIG'

                self.bed_state[0] = self.bed_state[0]*0.0#*head_angle_multiplier
                self.bed_state *= 0
                #self.bed_state += 60.
                #print self.bed_state, np.shape(self.pressure)

                if im_num == start_num and blah == True:
                    markers_c = []
                    markers_c.append(self.markers_all[im_num][0])
                    markers_c.append(self.markers_all[im_num][1])
                    markers_c.append(self.markers_all[im_num][2])
                    markers_c.append(self.markers_all[im_num][3])
                    #for idx in range(4):
                        #if markers_c[idx] is not None:
                            #markers_c[idx] = np.array(markers_c[idx])*213./228.
                blah = False

                #print markers_c, 'Markers C'

                # Get the marker points in 2D on the color image
                u_c, v_c = ArTagLib().color_2D_markers(markers_c, self.new_K_kin)

                # Get the marker points dropped to the height of the pressure mat
                u_c_drop, v_c_drop, markers_c_drop = ArTagLib().color_2D_markers_drop(markers_c, self.new_K_kin)

                #print markers_c_drop, self.new_K_kin, self.pressure_im_size_required, self.bed_state, half_w_half_l
                # Get the geometry for sizing the pressure mat
                pmat_ArTagLib = ArTagLib()
                self.pressure_im_size_required, u_c_pmat, v_c_pmat, u_p_bend, v_p_bend, half_w_half_l = \
                    pmat_ArTagLib.p_mat_geom(markers_c_drop, self.new_K_kin, self.pressure_im_size_required, self.bed_state, half_w_half_l)

                tf_corners = np.zeros((8, 2))
                tf_corners[0:8,:] = np.copy(self.tf_corners)

                #COLOR
                #if self.color is not 0:
                color_reshaped, color_size = VizLib().color_image(self.color, self.kcam, self.new_K_kin,
                                                                  u_c, v_c, u_c_drop, v_c_drop, u_c_pmat, v_c_pmat, camera_alpha_vert, camera_alpha_horiz)
                color_reshaped = imutils.rotate(color_reshaped, kinect_rotate_angle)
                color_reshaped = color_reshaped[pre_VERT_CUT+kinect_shift_up:-pre_VERT_CUT+kinect_shift_up,  HORIZ_CUT+kinect_shift_right : 540 - HORIZ_CUT+kinect_shift_right, :]
                tf_corners[0:4, :], color_reshaped = self.transform_selected_points(color_reshaped,
                                                                                camera_alpha_vert,
                                                                                camera_alpha_horiz,
                                                                                kinect_rotate_angle,
                                                                                kinect_shift_right,
                                                                                kinect_shift_up, [1.0, 0],
                                                                                [1.0, 0],
                                                                                np.copy(self.tf_corners[0:4][:]))

                all_image_list.append(color_reshaped)

                #DEPTH
                h = VizLib().get_new_K_kin_homography(camera_alpha_vert, camera_alpha_horiz, self.new_K_kin)

                depth_r_orig = cv2.warpPerspective(self.depth_r, h, (self.depth_r.shape[1], self.depth_r.shape[0]))
                depth_r_orig = imutils.rotate(depth_r_orig, kinect_rotate_angle)
                depth_r_orig = depth_r_orig[HORIZ_CUT + kinect_shift_right: 540 - HORIZ_CUT + kinect_shift_right, pre_VERT_CUT - kinect_shift_up:-pre_VERT_CUT - kinect_shift_up]
                depth_r_reshaped, depth_r_size, depth_r_orig = VizLib().depth_image(depth_r_orig, u_c, v_c)
                self.depth_r_orig = depth_r_orig
                self.depthcam_midpixel = [self.new_K_kin[1, 2] - HORIZ_CUT - kinect_shift_right, (960-self.new_K_kin[0, 2]) - pre_VERT_CUT - kinect_shift_up]
                self.depthcam_midpixel2 = [self.new_K_kin[1, 2] - HORIZ_CUT, (960-self.new_K_kin[0, 2]) - pre_VERT_CUT]

                #print h, "H" #warping perspective
                #print kinect_rotate_angle #the amount to rotate counterclockwise about normal vector to the bed
                #print kinect_shift_right, kinect_shift_up #pixel shift of depth im. convert this to meters based on depth of

                depth_r_orig_nowarp = imutils.rotate(self.depth_r, 0)
                depth_r_orig_nowarp = depth_r_orig_nowarp[HORIZ_CUT + 0: 540 - HORIZ_CUT + 0, pre_VERT_CUT - 0:-pre_VERT_CUT - 0]
                depth_r_reshaped_nowarp, depth_r_size, depth_r_orig_nowarp = VizLib().depth_image(depth_r_orig_nowarp, u_c, v_c) #this just does two rotations

                all_image_list.append(depth_r_reshaped)
                all_image_list.append(depth_r_reshaped_nowarp)

                X,Y,Z = self.get_pc_from_depthmap(self.bed_state[0], tf_corners[2, :])

                #print self.pressure_im_size_required, color_size, u_c_drop, v_c_drop, u_c_pmat, v_c_pmat, u_p_bend, v_p_bend

                #PRESSURE
                #pressure_vert_scale = 1.0
                #pressure_horiz_scale = 1.0
                #self.pressure = np.clip(self.pressure*4, 0, 100)
                pressure_reshaped, pressure_size, coords_from_top_left = VizLib().pressure_image(self.pressure, self.pressure_im_size,
                                                                           self.pressure_im_size_required, color_size,
                                                                           u_c_drop, v_c_drop, u_c_pmat, v_c_pmat,
                                                                           u_p_bend, v_p_bend)
                pressure_shape = pressure_reshaped.shape
                pressure_reshaped = cv2.resize(pressure_reshaped, None, fx=pressure_horiz_scale,
                                              fy=pressure_vert_scale)[0:pressure_shape[0],
                                              0:pressure_shape[1], :]

                if pressure_horiz_scale < 1.0 or pressure_vert_scale < 1.0:
                    pressure_reshaped_padded = np.zeros(pressure_shape).astype(np.uint8)
                    pressure_reshaped_padded[0:pressure_reshaped.shape[0], 0:pressure_reshaped.shape[1], :] += pressure_reshaped
                    pressure_reshaped = np.copy(pressure_reshaped_padded)

                coords_from_top_left[0] -= coords_from_top_left[0]*(1-pressure_horiz_scale)
                coords_from_top_left[1] += (960 - coords_from_top_left[1])*(1-pressure_vert_scale)

                pressure_reshaped = pressure_reshaped[pre_VERT_CUT:-pre_VERT_CUT,  HORIZ_CUT : 540 - HORIZ_CUT, :]

                all_image_list.append(pressure_reshaped)

                self.all_images = np.zeros((960-np.abs(pre_VERT_CUT)*2, 1, 3)).astype(np.uint8)
                for image in all_image_list:
                    #print image.shape
                    self.all_images = np.concatenate((self.all_images, image), axis = 1)

                self.all_images = self.all_images[VERT_CUT : 960 - VERT_CUT, :, :]

                is_not_mult_4 = True
                while is_not_mult_4 == True:
                    is_not_mult_4 = cv2.resize(self.all_images, (0, 0), fx=self.overall_image_scale_amount, fy=self.overall_image_scale_amount).shape[1]%4
                    self.overall_image_scale_amount+= 0.001

                coords_from_top_left[0] -= (HORIZ_CUT)
                coords_from_top_left[1] = 960 - pre_VERT_CUT - coords_from_top_left[1]
                self.coords_from_top_left = (np.array(coords_from_top_left) * self.overall_image_scale_amount)
                #print self.coords_from_top_left

                self.all_images = cv2.resize(self.all_images, (0, 0), fx=self.overall_image_scale_amount, fy=self.overall_image_scale_amount)
                self.cursor_shift = self.all_images.shape[1]/4

                self.all_images_clone = self.all_images.copy()

                cv2.imshow('all_images', self.all_images)
                k = cv2.waitKey(1)
                #cv2.waitKey(0)

                self.pc_all= [Y,X,-Z]
                #print np.shape(self.pc_all), "PC ALL SHAPE"

                #print self.tf_corners
                #print kinect_shift_up
                self.estimate_pose(self.pressure, self.bed_state[0], markers_c, model, model2, tf_corners, camera_alpha_vert,
                                   camera_alpha_horiz, h, kinect_rotate_angle, color_reshaped)

        #pkl.dump(self.RESULTS_DICT, open('/media/henry/multimodal_data_2/data/final_results/results_real_46K_'+PARTICIPANT+'_'+POSE_TYPE+'_'+NETWORK_2+'.p', 'wb'))

if __name__ ==  "__main__":

    import optparse

    p = optparse.OptionParser()

    p.add_option('--hd', action='store_true', dest='hd', default=False,
                 help='Read and write to data on an external harddrive.')

    #p.add_option('--net', action='store', type = 'int', dest='net', default=0,
    #             help='Choose a network.')

    p.add_option('--pose_type', action='store', type='string', dest='pose_type', default='none',
                 help='Choose a pose type, either `prescribed` or `p_select`.')

    p.add_option('--p_idx', action='store', type='int', dest='p_idx', default=0,
                 # PMR parameter to adjust loss function 2
                 help='Choose a participant. Enter a number from 1 to 20.')

    p.add_option('--pmr', action='store_true', dest='pmr', default=False,
                 help='Run PMR on input plus precomputed spatial maps.')

    p.add_option('--small', action='store_true', dest='small', default=False,
                 help='Make the dataset 1/4th of the original size.')

    p.add_option('--htwt', action='store_true', dest='htwt', default=False,
                 help='Include height and weight info on the input.')

    p.add_option('--calnoise', action='store_true', dest='calnoise', default=False,
                 help='Apply calibration noise to the input to facilitate sim to real transfer.')

    p.add_option('--viz', action='store', dest='viz', default='None',
                 help='Visualize training. specify `2D` or `3D`.')

    p.add_option('--go200', action='store_true', dest='go200', default=False,
                 help='Run network 1 for 100 to 200 epochs.')

    p.add_option('--loss_root', action='store_true', dest='loss_root', default=False,
                 help='Use root in loss function.')

    p.add_option('--use_hover', action='store_true', dest='use_hover', default=False,
                 help='Use hovermap for pmr input.')

    p.add_option('--omit_cntct_sobel', action='store_true', dest='omit_cntct_sobel', default=False,
                 help='Cut contact and sobel from input.')

    p.add_option('--half_shape_wt', action='store_true', dest='half_shape_wt', default=False,
                 help='Half betas.')

    opt, args = p.parse_args()

    participant_list = ["S103",
                        "S104",
                        "S107",
                        "S114",
                        "S118",
                        "S121",
                        "S130",
                        "S134",
                        "S140",
                        "S141",

                        "S145",
                        "S151",
                        "S163",
                        "S165",
                        "S170",
                        "S179",
                        "S184",
                        "S187",
                        "S188",
                        "S196",
                        ]

    if opt.p_idx != 0:
        participant_list = [participant_list[opt.p_idx - 1]]

    for PARTICIPANT in participant_list:

        if opt.hd == False:
            FILEPATH_PREFIX = "../../../data_BR"
        else:
            FILEPATH_PREFIX = "/media/henry/multimodal_data_2/data_BR"

        participant_directory = FILEPATH_PREFIX + "/real/" + PARTICIPANT
        #participant_directory = "/media/henry/multimodal_data_2/data_BR/real/"+PARTICIPANT
        #participant_directory = "/home/henry/Desktop/CVPR2020_study/"+PARTICIPANT
        V3D = Viz3DPose(opt)

        V3D.load_new_participant_info(participant_directory)

        if opt.pose_type == "prescribed":
            dat = load_pickle(participant_directory+"/prescribed.p")
            POSE_TYPE = "2"
        elif opt.pose_type == "p_select":
            dat = load_pickle(participant_directory+"/p_select.p")
            POSE_TYPE = "1"
        else:
            print "Please choose a pose type - either prescribed poses, " \
                  "'--pose_type prescribed', or participant selected poses, '--pose_type p_select'."
            sys.exit()

        if opt.small == True:
            NETWORK_1 = "46000ct_"
            NETWORK_2 = "46000ct_"
        else:
            NETWORK_1 = "184000ct_"
            NETWORK_2 = "184000ct_"

        NETWORK_1 += "128b_x1pm_tnh"

        if opt.go200 == True:
            NETWORK_2 += "128b_x1pm_tnh"
        elif opt.pmr == True:
            NETWORK_2 += "128b_x1pm_0.5rtojtdpth_depthestin_angleadj_tnh"
        else:
            NETWORK_2 += "128b_x1pm_angleadj_tnh"

        if opt.htwt == True:
            NETWORK_1 += "_htwt"
            NETWORK_2 += "_htwt"

        if opt.calnoise == True:
            NETWORK_1 += "_clns20p"
            NETWORK_2 += "_clns20p"

        if opt.loss_root == True:
            NETWORK_1 += "_rt"
            NETWORK_2 += "_rt"

        if opt.omit_cntct_sobel == True:
            NETWORK_1 += "_ocs"
            NETWORK_2 += "_ocs"

        if opt.use_hover == True:
            NETWORK_2 += "_uh"

        if opt.half_shape_wt == True:
            NETWORK_1 += "_hsw"
            NETWORK_2 += "_hsw"

        if opt.go200 == False:
            filename1 = FILEPATH_PREFIX+"/convnets_camready/convnet_1_anglesDC_" + NETWORK_1 + "_100e_2e-05lr.pt"
            filename2 = FILEPATH_PREFIX+"/convnets_camready/convnet_2_anglesDC_" + NETWORK_2 + "_100e_2e-05lr.pt"
        else:
            filename1 = FILEPATH_PREFIX+"/convnets_camready/convnet_1_anglesDC_" + NETWORK_1 + "_200e_2e-05lr.pt"
            filename2 = None

        F_eval = V3D.evaluate_data(dat, filename1, filename2)

max_stars_repo_name: henryclever/bodies-at-rest

max_issues_repo_name: henryclever/bodies-at-rest

max_forks_repo_name: henryclever/bodies-at-rest


text:
import os
import time
import logging
import numpy as np
import pandas as pd

from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report

from .helpers import save_model
from .helpers import md5checksum
from .helpers import relpath
from .helpers import param_str
from .helpers import read_predictions
from .helpers import DATA_DIR, ROOT_DIR

def tune_model(model, data, targets, param_grid, n_splits, n_iter):
    """"""
    rs = np.random.randint(100)
    logging.info(f'> Tuning classfier using stratified {n_splits}-fold CV and random search')
    logging.info(f'  Tune using randomized search (n_iter={n_iter}, random_state={rs})')
    logging.info(f'  Tuned params: {list(param_grid.keys())}')
    t0 = time.time()

    # Tune!
    tuner = RandomizedSearchCV(
        estimator=model,
        param_distributions=param_grid,
        scoring=['accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted'],
        cv=StratifiedKFold(n_splits=n_splits),
        refit=False,
        n_iter=n_iter,
        n_jobs=-1,
        return_train_score=True,
        random_state=rs)

    tuner.fit(data, targets)
    cv_results = pd.DataFrame(tuner.cv_results_).sort_values('rank_test_accuracy')
    cv_results.index.name = 'id'

    # Log The best results
    best = cv_results.iloc[0, :]
    logging.info(f'  Best params: {best.params}')
    logging.info( '  Mean cross validation statistics for best params:')

    train_acc = best['mean_train_accuracy']
    test_acc = best['mean_test_accuracy']
    logging.info(f'  - Train/test accuracy: {train_acc:.2%}%/{test_acc:.2%}% (rank {best.rank_test_accuracy})')

    train_prec = best['mean_train_precision_weighted']
    test_prec = best['mean_test_precision_weighted']
    logging.info(f'  - Train/test precision: {train_prec:.2%}%/{test_prec:.2%}% (rank {best.rank_test_precision_weighted})')

    train_rec = best['mean_train_recall_weighted']
    test_rec = best['mean_test_recall_weighted']
    logging.info(f'  - Train/test recall: {train_rec:.2%}%/{test_rec:.2%}% (rank {best.rank_test_recall_weighted})')

    train_f1 = best['mean_train_f1_weighted']
    test_f1 = best['mean_test_f1_weighted']
    logging.info(f'  - Train/test f1: {train_f1:.2%}%/{test_f1:.2%}% (rank {best.rank_test_f1_weighted})')

    # Update model parameters
    logging.info('  Setting model parameters to highest-accuracy params')
    model.set_params(**best.params)

    duration = time.time() - t0
    logging.info(f'  Tuning done in {duration:.2f}s')
    return cv_results

def train_model(model,
                train_data, train_targets, test_data, test_targets,
                param_grid, n_splits, n_iter,
                basepath):
    """"""
    # Tune the model
    cv_results = tune_model(
        model=model,
        data=train_data,
        targets=train_targets,
        param_grid=param_grid,
        n_splits=n_splits,
        n_iter=n_iter)

    # Train the model
    t0 = time.time()
    model.fit(train_data, train_targets)
    duration = time.time() - t0
    logging.info(f'> Training done in {duration:.2f}s')

    # Serialize trained model
    model_fn = f'{basepath}.model'
    save_model(model, model_fn)
    logging.info(f'> Serialized model to {relpath(model_fn)}')
    logging.info(f'  md5 checksum: {md5checksum(model_fn)}')

    # Store tuning results
    tuning_fn = f'{basepath}-tuning.csv'
    cv_results.to_csv(tuning_fn)
    logging.info(f'> Stored tuning results to {relpath(tuning_fn)}')
    logging.info(f'  md5 checksum: {md5checksum(tuning_fn)}')

    # Train predictions and accuracy
    train_pred_fn = f'{basepath}-train-pred.txt'
    train_pred = model.predict(train_data)
    train_pred.tofile(train_pred_fn, sep='\n')
    logging.info(f'> Stored train predictions to {relpath(train_pred_fn)}')
    logging.info(f'  md5 checksum: {md5checksum(train_pred_fn)}')
    train_acc = accuracy_score(train_targets, train_pred)
    logging.info(f'  Train accuracy: {train_acc:.2%}')

    # Test predictions and accuracy
    test_pred_fn = f'{basepath}-test-pred.txt'
    test_pred = model.predict(test_data)
    test_pred.tofile(test_pred_fn, sep='\n')
    logging.info(f'> Stored test predictions to {relpath(test_pred_fn)}')
    logging.info(f'  md5 checksum: {md5checksum(test_pred_fn)}')
    test_acc = accuracy_score(test_targets, test_pred)
    logging.info(f'  Test accuracy: {test_acc:.2%}')

    logging.info(f'> Done.')
    logging.info('*' * 60)

def get_scores(train_pred_fn, test_pred_fn, genre, subset, data_dir=DATA_DIR,
               **kwargs):
    """Evaluate model predictions and return evaluation scores as a dict"""
    train_fn = os.path.join(data_dir, genre, subset, f'train-chants.csv')
    test_fn = os.path.join(data_dir, genre, subset, f'test-chants.csv')
    train_targets = pd.read_csv(train_fn)['mode']
    test_targets = pd.read_csv(test_fn)['mode']
    train_predictions = read_predictions(train_pred_fn)
    test_predictions = read_predictions(test_pred_fn)
    train_report = classification_report(
        train_targets, train_predictions, output_dict=True)
    test_report = classification_report(
        test_targets, test_predictions, output_dict=True)

    return dict(
        genre=genre,
        subset=subset,
        train_accuracy = accuracy_score(train_targets, train_predictions),
        test_accuracy = accuracy_score(test_targets, test_predictions),
        train_weighted_f1 = train_report['weighted avg']['f1-score'],
        train_weighted_precision = train_report['weighted avg']['precision'],
        train_weighted_recall = train_report['weighted avg']['recall'],
        test_weighted_f1 = test_report['weighted avg']['f1-score'],
        test_weighted_precision = test_report['weighted avg']['precision'],
        test_weighted_recall = test_report['weighted avg']['recall'],
    )


text:
"""
Reading NUML, CSV and TSV data from DataDescriptions
"""
from __future__ import print_function, absolute_import
import os
import logging
import warnings
import tempfile
import numpy as np
import pandas as pd

import urllib.request

try:
    import libnuml
except ImportError:
    import tenuml as libnuml

log = logging.getLogger('sedml-data')

class DataDescriptionParser(object):
    """ Class for parsing DataDescriptions. """

    FORMAT_URN = "urn:sedml:format:"
    FORMAT_NUML = "urn:sedml:format:numl"
    FORMAT_CSV = "urn:sedml:format:csv"
    FORMAT_TSV = "urn:sedml:format:tsv"

    # supported formats
    SUPPORTED_FORMATS = [FORMAT_NUML, FORMAT_CSV, FORMAT_TSV]

    @classmethod
    def parse(cls, dd, workingDir=None):
        """ Parses single DataDescription.

        Returns dictionary of data sources {DataSource.id, slice_data}

        :param dd: SED-ML DataDescription
        :param workingDir: workingDir relative to which the sources are resolved
        :return:
        """
        did = dd.getId()
        name = dd.getName()
        source = dd.getSource()

        # -------------------------------
        # Resolve source
        # -------------------------------
        # FIXME: this must work for absolute paths and URL paths
        if workingDir is None:
            workingDir = '.'

        # TODO: refactor in general resource module (for resolving anyURI and resource)
        tmp_file = None
        if source.startswith('http') or source.startswith('HTTP'):
            webURL = urllib.request.urlopen(source)
            data = webURL.read()
            try:
                file_str = str(data.decode("utf-8"))
            except:
                file_str = str(data)

            tmp_file = tempfile.NamedTemporaryFile("w", delete=False)
            tmp_file.write(file_str)
            tmp_file.close()
            source_path = tmp_file.name
        else:
            source_path = os.path.join(workingDir, source)

        # -------------------------------
        # Find the format
        # -------------------------------
        format = None
        if hasattr(dd, "getFormat"):
            format = dd.getFormat()
        format = cls._determine_format(source_path=source_path, format=format)

        # log data description
        log.info('-' * 80)
        log.info('DataDescription: :', dd)
        log.info('\tid:', did)
        log.info('\tname:', name)
        log.info('\tsource', source)
        log.info('\tformat', format)

        # -------------------------------
        # Parse DimensionDescription
        # -------------------------------
        # FIXME: uses the data_types to check the actual data type
        dim_description = dd.getDimensionDescription()
        if dim_description is not None:
            data_types = cls.parse_dimension_description(dim_description)
        else:
            data_types = None

        # -------------------------------
        # Load complete data
        # -------------------------------
        data = None
        if format == cls.FORMAT_CSV:
            data = cls._load_csv(path=source_path)
        elif format == cls.FORMAT_TSV:
            data = cls._load_tsv(path=source_path)
        elif format == cls.FORMAT_NUML:
            data = cls._load_numl(path=source_path)

        # log data
        log.info("-" * 80)
        log.info("Data")
        log.info("-" * 80)
        if format in [cls.FORMAT_CSV, cls.FORMAT_TSV]:
            log.info(data.head(10))
        elif format == cls.FORMAT_NUML:
            # multiple result components via id
            for result in data:
                log.info(result[0])           # rc id
                log.info(result[1].head(10))  # DataFrame
        log.info("-" * 80)

        # -------------------------------
        # Process DataSources
        # -------------------------------
        data_sources = {}
        for k, ds in enumerate(dd.getListOfDataSources()):

            dsid = ds.getId()

            # log DataSource
            log.info('\n\t*** DataSource:', ds)
            log.info('\t\tid:', ds.getId())
            log.info('\t\tname:', ds.getName())
            log.info('\t\tindexSet:', ds.getIndexSet())
            log.info('\t\tslices')

            # CSV/TSV
            if format in [cls.FORMAT_CSV, cls.FORMAT_TSV]:
                if len(ds.getIndexSet()) > 0:
                    # if index set we return the index
                    data_sources[dsid] = pd.Series(data.index.tolist())
                else:
                    sids = []
                    for slice in ds.getListOfSlices():
                        # FIXME: this does not handle multiple slices for rows
                        # print('\t\t\treference={}; value={}'.format(slice.getReference(), slice.getValue()))
                        sids.append(slice.getValue())

                    # slice values are columns from data frame
                    try:
                        data_sources[dsid] = data[sids].values
                    except KeyError as e:
                        # something does not fit between data and data sources
                        print("-" * 80)
                        print("Format:", format)
                        print("Source:", source_path)
                        print("-" * 80)
                        print(data)
                        print("-" * 80)
                        raise

            # NUML
            elif format == cls.FORMAT_NUML:
                # Using the first results component only in SED-ML L1V3
                rc_id, rc, data_types = data[0]

                index_set = ds.getIndexSet()
                if ds.getIndexSet() and len(ds.getIndexSet()) != 0:
                    # data via indexSet
                    data_source = rc[index_set].drop_duplicates()
                    data_sources[dsid] = data_source
                else:
                    # data via slices
                    for slice in ds.getListOfSlices():
                        reference = slice.getReference()
                        value = slice.getValue()
                        df = rc.loc[rc[reference] == value]
                        # select last column with values
                        data_sources[dsid] = df.iloc[:, -1]

        # log data sources
        log.info("-" * 80)
        log.info("DataSources")
        log.info("-" * 80)
        for key, value in data_sources.items():
            log.info('{} : {}; shape={}'.format(key, type(value), value.shape))
        log.info("-" * 80)

        # cleanup
        # FIXME: handle in finally
        if tmp_file is not None:
            os.remove(tmp_file.name)

        return data_sources

    @classmethod
    def _determine_format(cls, source_path, format=None):
        """

        :param source_path: path of file
        :param format: format given in the DataDescription
        :return:
        """
        if format is None or format == "":
            is_xml = False
            with open(source_path) as unknown_file:
                start_str = unknown_file.read(1024)
                start_str = start_str.strip()
                if start_str.startswith('<'):
                    is_xml = True

            if is_xml:
                # xml format is numl
                format = cls.FORMAT_NUML  # defaults to numl
            else:
                # format is either csv or tsv
                df_csv = cls._load_csv(source_path)
                df_tsv = cls._load_tsv(source_path)
                if df_csv.shape[1] >= df_tsv.shape[1]:
                    format = cls.FORMAT_CSV
                else:
                    format = cls.FORMAT_TSV

        # base format
        if format.startswith(cls.FORMAT_NUML):
            format = cls.FORMAT_NUML

        # check supported formats
        if format not in cls.SUPPORTED_FORMATS:
            raise NotImplementedError("Format '{}' not supported for DataDescription. Format must be in: {}".format(format, cls.SUPPORTED_FORMATS))

        return format

    @classmethod
    def _load_csv(cls, path):
        """ Read CSV data from file.

        :param path: path of file
        :return: returns pandas DataFrame with data
        """
        return cls._load_sv(path, separator=",")

    @classmethod
    def _load_tsv(cls, path):
        """ Read TSV data from file.

                :param path: path of file
                :return: returns pandas DataFrame with data
                """
        return cls._load_sv(path, separator="\t")

    @classmethod
    def _load_sv(cls, path, separator):
        """ Helper function for loading data file from given source.

        CSV files must have a header. Handles file and online resources.

        :param path: path of file.
        :return: pandas data frame
        """
        df = pd.read_csv(path, sep=separator,
                         index_col=False,
                         skip_blank_lines=True,
                         quotechar='"',
                         comment="#",
                         skipinitialspace=True,
                         na_values="nan")
        return df

    @classmethod
    def read_numl_document(cls, path):
        """ Helper to read numl document and checking errors

        :param path: path of file
        :return:
        """
        doc_numl = libnuml.readNUMLFromFile(path)  # type: libnuml.NUMLDocument

        # check for errors
        errorlog = doc_numl.getErrorLog()
        msg = "NUML ERROR in '{}': {}".format(path, errorlog.toString())
        if errorlog.getNumFailsWithSeverity(libnuml.LIBNUML_SEV_ERROR) > 0:
            raise IOError(msg)
        if errorlog.getNumFailsWithSeverity(libnuml.LIBNUML_SEV_FATAL) > 0:
            raise IOError(msg)
        if errorlog.getNumFailsWithSeverity(libnuml.LIBNUML_SEV_WARNING) > 0:
            warnings.warn(msg)
        if errorlog.getNumFailsWithSeverity(libnuml.LIBNUML_SEV_SCHEMA_ERROR) > 0:
            warnings.warn(msg)
        if errorlog.getNumFailsWithSeverity(libnuml.LIBNUML_SEV_GENERAL_WARNING) > 0:
            warnings.warn(msg)

        return doc_numl

    @classmethod
    def _load_numl(cls, path):
        """ Reading NuML data from file.

        This loads the complete numl data.
        For more information see: https://github.com/numl/numl

        :param path: NuML path
        :return: data
        """
        doc_numl = DataDescriptionParser.read_numl_document(path)

        # reads all the resultComponents from the numl file
        results = []

        Nrc = doc_numl.getNumResultComponents()
        rcs = doc_numl.getResultComponents()

        log.info('\nNumResultComponents:', Nrc)
        for k in range(Nrc):
            rc = rcs.get(k)  # parse ResultComponent
            rc_id = rc.getId()

            # dimension info
            description = rc.getDimensionDescription()
            data_types = cls.parse_dimension_description(description)

            # data
            dimension = rc.getDimension()
            assert (isinstance(dimension, libnuml.Dimension))
            data = [cls._parse_dimension(dimension.get(k)) for k in range(dimension.size())]

            # create data frame
            flat_data = []
            for entry in data:
                for part in entry:
                    flat_data.append(part)

            # column ids from DimensionDescription
            column_ids = []
            for entry in data_types:
                for cid, dtype in entry.items():
                    column_ids.append(cid)

            df = pd.DataFrame(flat_data, columns=column_ids)

            # convert data types to actual data types
            for entry in data_types:
                for cid, dtype in entry.items():
                    if dtype == 'double':
                        df[cid] = df[cid].astype(np.float64)
                    elif dtype == 'string':
                        df[cid] = df[cid].astype(str)

            # convert all the individual columns to the corresponding data types
            # df = df.apply(pd.to_numeric, errors="ignore")

            results.append([rc_id, df, data_types])

        return results

    @classmethod
    def parse_dimension_description(cls, description):
        """ Parses the given dimension description.

        Returns dictionary of { key: dtype }

        :param description:
        :return:
        """
        assert (isinstance(description, libnuml.DimensionDescription))
        info = [cls._parse_description(description.get(k)) for k in range(description.size())]

        flat_info = []
        for entry in info:
            for part in entry:
                flat_info.append(part)

        return flat_info

    @classmethod
    def _parse_description(cls, d, info=None, entry=None):
        """ Parses the recursive DimensionDescription, TupleDescription, AtomicDescription.

        This gets the dimension information from NuML.

          <dimensionDescription>
            <compositeDescription indexType="double" id="time" name="time">
              <compositeDescription indexType="string" id="SpeciesIds" name="SpeciesIds">
                <atomicDescription valueType="double" id="Concentrations" name="Concentrations" />
              </compositeDescription>
            </compositeDescription>
          </dimensionDescription>

        :param d:
        :param info:
        :return:
        """
        if info is None:
            info = []
        if entry is None:
            entry = []

        type_code = d.getTypeCode()
        # print('typecode:', type_code)

        if type_code == libnuml.NUML_COMPOSITEDESCRIPTION:
            content = {d.getId(): d.getIndexType()}
            info.append(content)
            # print('\t* CompositeDescription:', content)
            if d.isContentCompositeDescription():
                for k in range(d.size()):
                    info = cls._parse_description(d.getCompositeDescription(k), info, list(entry))
            elif d.isContentAtomicDescription():
                info = cls._parse_description(d.getAtomicDescription(), info, entry)

        elif type_code == libnuml.NUML_ATOMICDESCRIPTION:
            content = {d.getId(): d.getValueType()}
            info.append(content)
            # print('\t* AtomicDescription:', content)

        elif type_code == libnuml.NUML_TUPLEDESCRIPTION:
            tuple_des = d.getTupleDescription()
            Natomic = d.size()
            valueTypes = []
            for k in range(Natomic):
                atomic = tuple_des.getAtomicDescription(k)
                valueTypes.append(atomic.getValueType())

            info.append(valueTypes)
            # print('\t* TupleDescription:', valueTypes)

        else:
            raise NotImplementedError("Type code: {}".format(type_code))

        return info

    @classmethod
    def _parse_dimension(cls, d, data=None, entry=None):
        """ Parses the recursive CompositeValue, Tuple, AtomicValue.

        This gets the actual data from NuML.

        :param d:
        :param data:
        :return:
        """
        if data is None:
            data = []
        if entry is None:
            entry = []

        type_code = d.getTypeCode()
        # print('typecode:', type_code)

        if type_code == libnuml.NUML_COMPOSITEVALUE:
            indexValue = d.getIndexValue()
            entry.append(indexValue)
            # print('\t* CompositeValue:', indexValue)

            if d.isContentCompositeValue():
                for k in range(d.size()):
                    # make copy, so every entry is own entry
                    data = cls._parse_dimension(d.getCompositeValue(k), data, list(entry))
            elif d.isContentAtomicValue():
                data = cls._parse_dimension(d.getAtomicValue(), data, entry)

        elif type_code == libnuml.NUML_ATOMICVALUE:
            # Data is converted to correct
            # value = d.getDoubleValue()
            value = d.getValue()
            entry.append(value)
            # entry finished, we are appending
            data.append(entry)
            # print('\t* AtomicValue:', value)

        elif type_code == libnuml.NUML_TUPLE:
            tuple = d.getTuple()
            Natomic = d.size()
            values = []
            for k in range(Natomic):
                atomic = tuple.getAtomicValue(k)
                values.append(atomic.getDoubleValue())

            data.append(values)
            # print('\t* TupleDescription:', values)

        else:
            raise NotImplementedError

        return data

max_stars_repo_name: sys-bio/tellurium

max_issues_repo_name: sys-bio/tellurium

max_forks_repo_name: sys-bio/tellurium


text:
import numpy as np
import streamlit as st
from PIL import Image

from app import ClassifyWear

from PIL import Image # Required to show images
import pandas as pd
logo = Image.open("data/logo.png")
st.sidebar.image(logo, width=250)

# Text/Title
st.title("Fashion Prediction ")

st.sidebar.header("Jack & Jones Team")
st.sidebar.text("Team members")
st.sidebar.write("""
# Anthony
# Sai
# Harsha""")

model_pth = 'data/best_model.pt'

uploaded_file = st.file_uploader('Upload Image File')
if uploaded_file is not None:
    image = Image.open(uploaded_file).convert('L')
    st.image(image, channels="BGR", width=100)
    prob, predicted = ClassifyWear(model_pth).predict(uploaded_file)
    st.write(f'The image passed is {predicted} with a probability of {prob}%')
else:
    st.write(f'Please upload a file')

max_stars_repo_name: TonyMTH/fashion-classifier

max_issues_repo_name: TonyMTH/fashion-classifier

max_forks_repo_name: TonyMTH/fashion-classifier