text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun  8 16:15:35 2020

@author: nooteboom
"""
from numba import jit
import numpy as np
import math
from pandas import read_csv
#%% To read datasets
def readForamset(name):
    file = open(name)
    data = []
    line = file.readline()
    while(len(line)>0):
        ldat = []
        j = 0
        for l in range(len(line)):
            if(line[j:l][-1:] in ['\t','\n']):
                ldat.append(line[j:l-1])
                j = l
        line = file.readline()
        data.append(ldat)

    data = np.array(data)
    return data

def readForamset_lonlats(name):
    file = open(name)
    data = []
    line = file.readline()
    while(len(line)>0):
        tabcount = 0
        ldat = []
        j = 0
        for l in range(len(line)):
            if(line[j:l][-1:] in ['\t','\n']):
                tabcount+=1
                if(tabcount in [5,6]):
                    ldat.append(line[j:l-1])
                j = l
        line = file.readline()
        data.append(ldat)

    data = np.array(data)
    return data[:,0].astype(np.float), data[:,1].astype(np.float)

def readDinoset(name):
    lons = read_csv(name, usecols=[0]).values[:,0]
    lats = read_csv(name, usecols=[1]).values[:,0]
    data = read_csv(name).values[:,2:]
    data[np.isnan(data)] = 0
    row_sum = data.sum(axis=1)
    data = data / row_sum[:,np.newaxis]

    return lons, lats, data
#%% To calculate similarity measures

#@jit(nopython=True)
def Euclidean_distance(x,y):
    res = 0
    for i in range(len(x)):
        res += (x[i] - y[i]) ** 2
    if(res==0):
        res = 1 / math.sqrt(res / 4)
    return 1 / math.sqrt(res / 4)

@jit(nopython=True)
def Cos_sim(x,y):
    res = 0
    normx = 0
    normy = 0
    for i in range(len(x)):
        res += (x[i]* y[i])
        normx += x[i]**2
        normy += y[i]**2
    return ((res / math.sqrt(normx*normy)))#/2

@jit(nopython=True)
def Similarity_data(data):
    res = np.ones((data.shape[0],data.shape[0]))
    for i in range(res.shape[0]):
        for j in range(i):
             EC = Cos_sim(data[i],data[j])
             res[i,j] = EC
             res[j,i] = res[i,j]
    return res

#%%

@jit(nopython=True)
def get_edges(TM):
    res = []
    for i in range(TM.shape[0]):
        for j in range(TM.shape[1]):
            if(TM[i,j]>0):
                res.append((i,-j))
    return res

@jit(nopython=True)
def get_weighted_edges(TM):
    res = []
    for i in range(TM.shape[0]):
        for j in range(TM.shape[1]):
            res.append((i,-j, TM[i,j]))
    return res

max_stars_repo_name: pdnooteboom/ClusterSinkingParticles

max_issues_repo_name: pdnooteboom/ClusterSinkingParticles

max_forks_repo_name: pdnooteboom/ClusterSinkingParticles

reason: import numpy,from numba


text:
import torch
import numpy as np
import torch.nn as nn

# First checking if GPU is available
train_on_gpu = torch.cuda.is_available()

if(train_on_gpu):
    print('Training on GPU.')
else:
    print('No GPU available, training on CPU.')

batch_size = 50

# loss and optimization functions
lr = 0.001

criterion = nn.BCELoss()

# load trained model
net = torch.load('models/torch_rnn_trained.model')

# load test dataloader
test_loader = torch.load('data/processed/torch_rnn_test.loader')

test_losses = []  # track loss
num_correct = 0

# init hidden state
h = net.init_hidden(batch_size)

# set to eval mode
net.eval()

print('starting testing...')
# iterate over test data
for inputs, labels in test_loader:

    # create new vars for hidden state
    h = tuple([each.data for each in h])

    if train_on_gpu:
        inputs, labels = inputs.cuda(), labels.cuda()

    # get predicted outputs
    output, h = net(inputs, h)

    # calculate loss
    test_loss = criterion(output.squeeze(), labels.float())
    test_losses.append(test_loss.item())

    # convert output probabilities to predicted class (0 or 1)
    pred = torch.round(output.squeeze())

    # compare predictions to true label
    correct_tensor = pred.eq(labels.float().view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(
        correct_tensor.cpu().numpy())
    num_correct += np.sum(correct)

# Get test data loss and accuracy

test_losses = []  # track loss
num_correct = 0

# init hidden state
h = net.init_hidden(batch_size)

net.eval()
# iterate over test data
for inputs, labels in test_loader:

    # Creating new variables for the hidden state, otherwise
    # we'd backprop through the entire training history
    h = tuple([each.data for each in h])

    if(train_on_gpu):
        inputs, labels = inputs.cuda(), labels.cuda()

    # get predicted outputs
    output, h = net(inputs, h)

    # calculate loss
    test_loss = criterion(output.squeeze(), labels.float())
    test_losses.append(test_loss.item())

    # convert output probabilities to predicted class (0 or 1)
    pred = torch.round(output.squeeze())  # rounds to the nearest integer

    # compare predictions to true label
    correct_tensor = pred.eq(labels.float().view_as(pred))
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(
        correct_tensor.cpu().numpy())
    num_correct += np.sum(correct)

# -- stats! -- ##
# avg test loss
print("Test loss: {:.3f}".format(np.mean(test_losses)))

# accuracy over all test data
test_acc = num_correct/len(test_loader.dataset)
print("Test accuracy: {:.3f}".format(test_acc))

ext: py

reason: import numpy


text:
import streamlit as st
import pandas as pd
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.figure_factory as ff
import pickle

#!pip install plotly

st.title('Heart Disease or Attack Checkup')
st.sidebar.header('Patient Data')

data_cols = ['Diabetes', 'HighBP', 'HighChol', 'CholCheck', 'BMI', 'Smoker','Stroke', 'PhysActivity', 'Fruits','Veggies', 'Alcohol_Consumption', 'Healthcare_Coverage', 'Consulted_Doc','GenHlth', 'MentHlth', 'PhysHlth', 'DiffWalk', 'Sex', 'Age','Education', 'Income']
len(data_cols)
st.write("""
# Heart Heart Disease or Attack Prediction App

This app predicts whether the patient is suffering from heart related disease or heart attack using diabetes data.
""")

uploaded_file = st.sidebar.file_uploader(
                                          "Upload your input CSV file",
                                           type=["csv"]
                                         )
st.subheader('Training Data Stats')

def user_input_features():
    Diabetes = st.sidebar.radio('Diabetes: 0:Non diabetic, 1:Prediabetic, 2:Diabetic',(0,1,2))
    HighBP = st.sidebar.radio('High Blood Pressure: 1:Yes, 0:No',(0,1))
    HighChol = st.sidebar.radio('High Cholestrol: 1:Yes, 0:No',(0,1))
    CholCheck = st.sidebar.radio('Cholestrol check in 5 years: 1:Yes, 0:No',(0,1))
    BMI = st.sidebar.slider('BMI', 0,100, 20 )
    Smoker = st.sidebar.radio('Smoker: 1:Yes, 0:No',(0,1))
    Stroke = st.sidebar.radio('Did you ever had a stroke: 1:Yes, 0:No',(0,1))
    PhysActivity = st.sidebar.radio('Physical Activity in past 30 days: 1:Yes, 0:No',(0,1))
    Fruits = st.sidebar.radio('Consume fruits atleast once a day: 1:Yes, 0:No',(0,1))
    Veggies = st.sidebar.radio('Consume vegetables atleast once a day: 1:Yes, 0:No',(0,1))
    Alcohol_Consumption = st.sidebar.radio('Consume alcohol atleast once per week: 1:Yes, 0:No',(0,1))
    Healthcare_Coverage = st.sidebar.radio('Do you have a healthcare coverage plan: 1:Yes, 0:No',(0,1))
    Consulted_Doc = st.sidebar.radio('Did you face difficulty in consulting the doctor: 1:Yes, 0:No',(0,1))
    GenHlth =  st.sidebar.slider('Your general health is: scale 1-5 1 = excellent 2 = very good 3 = good 4 = fair 5 = poor',1,5,3)
    MentHlth = st.sidebar.slider('for how many days during the past 30 days was your mental health not good? scale 1-30 days', 0,30,5)
    PhysHlth = st.sidebar.slider('for how many days during the past 30 days was your physical health not good? scale 1-30 days', 0,30,5)
    DiffWalk = st.sidebar.radio('Do you have serious difficulty walking or climbing stairs? 1:Yes, 0:No', (0,1))
    Sex =  st.sidebar.radio('Gender: Male:1, Female:0',(0,1))
    Age = st.sidebar.slider('Age: [1:18 to 24],[2:25 to 31],[3:32 to 38],[4:39 to 45],[5:46 to 52],[6:53 to 59],[7:60 to 66],[8:67 to 73],[9:74 to 80],[10:81 to 87],[11:88 to 90],[12:91 to 92],[13:92 or above]', 1,13, 4 )
    Education = st.sidebar.slider('Education level: scale 1-6 1 = Never attended school 2 = Grades 1-8 3 = Grades 9-11 4 = Grade 12 5 = College 1-3 years 6 = College 4+',1,6,3)
    Income = st.sidebar.slider('Income Level: scale 1-8 1 = less than $10,000 5 = less than $35,000 8 = $75,000 or more ',1,8,3)

    user_report_data = {
                        'Diabetes':Diabetes,
                        'HighBP':HighBP,
                        'HighChol':HighChol,
                        'CholCheck':CholCheck,
                        'BMI':BMI,
                        'Smoker':Smoker,
                        'Stroke':Stroke,
                        'PhysActivity':PhysActivity,
                        'Fruits':Fruits,
                        'Veggies':Veggies,
                        'Alcohol_Consumption':Alcohol_Consumption,
                        'Healthcare_Coverage':Healthcare_Coverage,
                        'Consulted_Doc':Consulted_Doc,
                        'GenHlth':GenHlth,
                        'MentHlth':MentHlth,
                        'PhysHlth':PhysHlth,
                        'DiffWalk':DiffWalk,
                        'Sex':Sex,
                        'Age':Age,
                        'Education':Education,
                        'Income':Income
                        }

    return user_report_data

if uploaded_file is not None:
    input_df = pd.read_csv(uploaded_file)

else:
    input_df = pd.DataFrame( [tuple([np.nan] * len(data_cols))] , columns = data_cols )
    user_report_data = user_input_features()
    input_df['Diabetes'] =user_report_data['Diabetes']
    input_df['HighBP'] =user_report_data['HighBP']
    input_df['HighChol'] =user_report_data['HighChol']
    input_df['CholCheck'] =user_report_data['CholCheck']
    input_df['BMI'] =user_report_data['BMI']
    input_df['Smoker'] =user_report_data['Smoker']
    input_df['Stroke'] =user_report_data['Stroke']
    input_df['PhysActivity'] =user_report_data['PhysActivity']
    input_df['Fruits'] =user_report_data['Fruits']
    input_df['Veggies'] =user_report_data['Veggies']
    input_df['Alcohol_Consumption'] =user_report_data['Alcohol_Consumption']
    input_df['Healthcare_Coverage'] =user_report_data['Healthcare_Coverage']
    input_df['Consulted_Doc'] =user_report_data['Consulted_Doc']
    input_df['GenHlth'] =user_report_data['GenHlth']
    input_df['MentHlth'] =user_report_data['MentHlth']
    input_df['PhysHlth'] =user_report_data['PhysHlth']
    input_df['DiffWalk'] =user_report_data['DiffWalk']
    input_df['Sex'] =user_report_data['Sex']
    input_df['Age'] =user_report_data['Age']
    input_df['Education'] =user_report_data['Education']
    input_df['Income'] =user_report_data['Income']

st.write(input_df.describe())

if uploaded_file is not None:
    st.write(input_df)
else:
    st.write('Awaiting CSV file to be uploaded. Currently using example input parameters (shown below).')
    st.write(input_df)

load_clf = pickle.load(open('health_indicators_model.pkl', 'rb'))

prediction = load_clf.predict(input_df)
prediction_proba = load_clf.predict_proba(input_df)

st.subheader('Your Report: ')
output=''
if prediction[0]==0:
  output = 'You are Healthy'
else:
  output = 'You may be having a heart disease or attack'

st.title(output)

st.subheader('Prediction Probability')
st.write(prediction_proba)

st.title('Visualised Patient Report')
if prediction[0]==0:
  color = 'blue'
else:
  color = 'red'

st.header('BMI Value Graph (Others vs Yours)')
fig_bmi = plt.figure()
ax11 = sns.scatterplot(x = 'Age', y = 'BMI', data = input_df, palette='rainbow')
ax12 = sns.scatterplot(x = input_df['Age'], y = input_df['BMI'], s = 150, color = color)
plt.xticks(np.arange(0,13,1))
plt.yticks(np.arange(0,100,5))
st.pyplot(fig_bmi)

st.header('BMI Value Graph (Others vs Yours)')
fig_edu = plt.figure()
ax11 = sns.scatterplot(x = 'Age', y = 'Education', data = input_df, palette='rainbow')
ax12 = sns.scatterplot(x = input_df['Age'], y = input_df['Education'], s = 150, color = color)
plt.xticks(np.arange(0,13,1))
plt.yticks(np.arange(0,6,1))
st.pyplot(fig_edu)

st.header('BMI Value Graph (Others vs Yours)')
fig_inc = plt.figure()
ax11 = sns.scatterplot(x = 'Age', y = 'Income', data = input_df, palette='rainbow')
ax12 = sns.scatterplot(x = input_df['Age'], y = input_df['Income'], s = 150, color = color)
plt.xticks(np.arange(0,13,1))
plt.yticks(np.arange(0,8,1))
st.pyplot(fig_inc)


text:
import cv2
import time
import numpy as np
import pandas as pd
import os
import math

MODE = "BODY25"
input_source = "demo1.mp4"
# demo1.mp4
# demo2.mp4

output_destination ='./destination/' + input_source.split('.')[0] + '.avi'
OUTPUT_CSV = './destination/Air Squat with Chris Spealler.mp4.csv'
FRAMES_TO_TAKE = 3

if MODE is "COCO":
    protoFile = "C:/Users/romanrosh/openpose-1.4.0-win64-gpu-binaries/models/pose/coco/pose_deploy_linevec.prototxt"
    weightsFile = "C:/Users/romanrosh/openpose-1.4.0-win64-gpu-binaries/models/pose/coco/pose_iter_440000.caffemodel"
    nPoints = 18
    POSE_PAIRS = [[1, 0], [1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], [9, 10], [1, 11], [11, 12],
                  [12, 13], [0, 14], [0, 15], [14, 16], [15, 17]]

elif MODE is "MPI":
    protoFile = "C:/Users/romanrosh/openpose-1.4.0-win64-gpu-binaries/models/pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
    weightsFile = "C:/Users/romanrosh/openpose-1.4.0-win64-gpu-binaries/models/pose/mpi/pose_iter_160000.caffemodel"
    nPoints = 15
    POSE_PAIRS = [[0, 1], [1, 2], [2, 3], [3, 4], [1, 5], [5, 6], [6, 7], [1, 14], [14, 8], [8, 9], [9, 10], [14, 11],
                  [11, 12], [12, 13]]

elif MODE is "BODY25":
    protoFile = "C:/Users/romanrosh/openpose-1.4.0-win64-gpu-binaries/models/pose/body_25/pose_deploy.prototxt"
    weightsFile = "C:/Users/romanrosh/openpose-1.4.0-win64-gpu-binaries/models/pose/body_25/pose_iter_584000.caffemodel"
    nPoints = 25
    POSE_PAIRS = [[1, 0], [1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], [9, 10], [1, 11], [11, 12],
                  [12, 13], [0, 14], [0, 15], [14, 16], [15, 17],
                  [10, 11], [8, 12], [12, 13], [13, 14], [1, 0], [0, 15], [0, 16], [16, 18], [2, 17], [5, 18], [14, 19],
                  [19, 20], [14, 21], [11, 22], [22, 23], [11, 24]]

    BODY_25_COLUMNS = ["0-XNose",       "0-YNose",
                       "1-XNeck",       "1-YNeck",
                       "2-XRShoulder",  "2-YRShoulder",
                       "3-XRElbow",     "3-YRElbow",
                       "4-XRWrist",     "4-YRWrist",
                       "5-XLShoulder",  "5-YLShoulder",
                       "6-XLElbow",     "6-YLElbow",
                       "7-XLWrist",     "7-YLWrist",
                       "8-XMidHip",     "8-YMidHip",
                       "9-XRHip",       "9-YRHip",
                       "10-XRKnee",     "10-YRKnee",
                       "11-XRAnkle",    "11-YRAnkle",
                       "12-XLHip",      "12-YLHip",
                       "13-XLKnee",     "13-YLKnee",
                       "14-XLAnkle",    "14-YLAnkle",
                       "15-XREye",      "15-YREye",
                       "16-XLEye",      "16-YLEye",
                       "17-XREar",      "17-YREar",
                       "18-XLEar",      "18-YLEar",
                       "19-XLBigToe",   "19-YLBigToe",
                       "20-XLSmallToe", "20-YLSmallToe",
                       "21-XLHeel",     "21-YLHeel",
                       "22-XRBigToe",   "22-YRBigToe",
                       "23-XRSmallToe", "23-YRSmallToe",
                       "24-XRHeel",     "24-YRHeel"]

inWidth = 368
inHeight = 368
threshold = 0.1

cap = cv2.VideoCapture(input_source)
hasFrame, frame = cap.read()

vid_writer = cv2.VideoWriter(output_destination, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10,
                             (frame.shape[1], frame.shape[0]))

net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
counter = 0
df_is_empty = True
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

print('frames to retrieve', length)

while cv2.waitKey(1) < 0:
    if counter > length:
        break
    t = time.time()
    hasFrame, frame = cap.read()
    counter += 1
    if np.mod(counter, FRAMES_TO_TAKE) != 0:
        continue
    if counter < 25:
        continue
    print('frame', counter)
    frameCopy = np.copy(frame)
    if not hasFrame:
        cv2.waitKey()
        break

    frameWidth = frame.shape[1]
    frameHeight = frame.shape[0]

    inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
                                    (0, 0, 0), swapRB=False, crop=False)
    net.setInput(inpBlob)
    output = net.forward()

    H = output.shape[2]
    W = output.shape[3]
    # Empty list to store the detected keypoints
    points = []

    for i in range(nPoints):
        # confidence map of corresponding body's part.
        probMap = output[0, i, :, :]

        # Find global maxima of the probMap.
        minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)

        # Scale the point to fit on the original image
        x = (frameWidth * point[0]) / W
        y = (frameHeight * point[1]) / H

        if prob > threshold:
            cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
            cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1,  #change to 1
                        lineType=cv2.LINE_AA)

            # Add the point to the list if the probability is greater than the threshold
            points.append((int(x), int(y)))
        else:
            points.append(None)
    flat_array = []
    for point in points:
        if point is None:
            flat_array.append(None)
            flat_array.append(None)
        else:
            for feature in point:
                flat_array.append(feature)
    flat_array = pd.Series(np.array(flat_array))
    # flat_array = np.array([feature for point in points for feature in point])
    if df_is_empty:
        df = pd.DataFrame([flat_array])
        df_is_empty = False
    else:
        df = df.append(flat_array, ignore_index=True)
    print('dataframe size', len(df))
    # Draw Skeleton
    for pair in POSE_PAIRS:
        partA = pair[0]
        partB = pair[1]

        if points[partA] and points[partB]:
            cv2.line(frame, points[partA], points[partB], (0, 255, 255), 3)
            cv2.circle(frame, points[partA], 8, (0, 0, 255))
            cv2.circle(frame, points[partB], 8, (0, 0, 255))
        # print(points)
    # cv2.putText(frame, "time taken = {:.2f} sec".format(time.time() - t), (50, 50), cv2.FONT_HERSHEY_COMPLEX, .8,
    #             (255, 50, 0), 2, lineType=cv2.LINE_AA)
    # cv2.putText(frame, "OpenPose using OpenCV", (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 50, 0), 2, lineType=cv2.LINE_AA)
    # cv2.imshow('Output-Keypoints', frameCopy)
    cv2.imshow('Output-Skeleton', frame)
    vid_writer.write(frame)
    if hasFrame == False:
        break
vid_writer.release()

df.columns = BODY_25_COLUMNS
df.fillna(value=pd.np.nan, inplace=True)

def nans_list(df):
    import math
    nans = []
    for i in np.arange(df.shape[0]):
        for j in np.arange(df.shape[1]):
            if math.isnan(df.iloc[i,j]):
                nans.append((i,j))
    return nans

nans = nans_list(df)
print(f"NaNs left to deal with: {len(nans)}")

## for points not at the edge, if NaN take the mean around them

persistent_nans = []
for point in nans:
    if point[0]>4 and point[0]<df.shape[0]-5:
        df.iloc[point[0], point[1]] = np.nanmean(df.iloc[point[0]-5:point[0]+6,point[1] ])

print(f"NaNs left to deal with at the edges: {len(nans_list(df))}")

print(nans_list(df))

reverse_top = np.arange(0,15)[::-1]

for row in reverse_top:
    for col in np.arange(df.shape[1]):
        print(row, col)
        if math.isnan(df.iloc[row, col]):
            df.iloc[row, col] = df.iloc[row+1, col]

for row in np.arange(df.shape[0]-10, df.shape[0]):
    for col in np.arange(df.shape[1]):
        if math.isnan(df.iloc[row, col]):
            df.iloc[row, col] = df.iloc[row-1, col]

print(f"NaNs left to deal with: {len(nans_list(df))}")

def anomaly_detector(arr):
    array_mean = np.mean(arr)
    array_std = np.std(arr)
    mid_point = arr.iloc[int((arr.shape[0]+1)/2 - 1)]
    if array_std>0 and np.abs(mid_point-array_mean)/array_std > 2:
#         print('anomaly handled')
        return array_mean, True
    return mid_point, False

iterations = 0
found_anomalies = True
while found_anomalies:
    iterations += 1
    found_anomalies = False
    for row in np.arange(5,df.shape[0]-5):
        for col in np.arange(df.shape[1]):
            result = anomaly_detector(df.iloc[row-5:row+6, col])
            df.iloc[row, col] = result[0]
            if result[1]:
                found_anomalies = True
print(f"{iterations} iterations required")

## anomalies on edges (top/bottom 5 rows)

def edge_anomaly_detector(arr, val):
    array_mean = np.mean(arr)
    array_std = np.std(arr)
    #     mid_point = arr.iloc[int((arr.shape[0]+1)/2 - 1)]
    if array_std > 0 and np.abs(val - array_mean) / array_std > 2:
        print('anomaly handled')
        return array_mean, True
    return val, False

iterations = 0
found_anomalies = True
while found_anomalies:
    iterations += 1
    found_anomalies = False
    for row in np.arange(5):
        for col in np.arange(df.shape[1]):
            result = edge_anomaly_detector(df.iloc[:5, col], df.iloc[row, col])
            df.iloc[row, col] = result[0]
            if result[1]:
                found_anomalies = True
    for row in np.arange(df.shape[0]):
        for col in np.arange(df.shape[1]):
            result = edge_anomaly_detector(df.iloc[df.shape[0]-5:, col], df.iloc[row, col])
            df.iloc[row, col] = result[0]
            if result[1]:
                found_anomalies = True
print(f"{iterations} iterations required")

print(df)

for i in range(len(df)):
    # RIGHT KNEE
    u = (df.loc[i, '9-XRHip'] - df.loc[i, '10-XRKnee'], df.loc[i, '9-YRHip'] - df.loc[i, '10-YRKnee'])
    v = (df.loc[i, '11-XRAnkle'] - df.loc[i, '10-XRKnee'], df.loc[i, '11-YRAnkle'] - df.loc[i, '10-YRKnee'])
    c = np.dot(u, v) / np.linalg.norm(u) / np.linalg.norm(v)
    df.loc[i, 'RightKneeAngle'] = np.arccos(np.clip(c, -1, 1)) * 180 / np.pi
    # LEFT KNEE
    u = (df.loc[i, '12-XLHip'] - df.loc[i, '13-XLKnee'], df.loc[i, '12-YLHip'] - df.loc[i, '13-YLKnee'])
    v = (df.loc[i, '14-XLAnkle'] - df.loc[i, '13-XLKnee'], df.loc[i, '14-YLAnkle'] - df.loc[i, '13-YLKnee'])
    c = np.dot(u, v) / np.linalg.norm(u) / np.linalg.norm(v)
    df.loc[i, 'LeftKneeAngle'] = np.arccos(np.clip(c, -1, 1)) * 180 / np.pi
    # heel ankle toe knee left side
    toes_x = (df.loc[i, "19-XLBigToe"] + df.loc[i, "20-XLSmallToe"]) / 2
    toes_y = (df.loc[i, "19-YLBigToe"] + df.loc[i, "20-YLSmallToe"]) / 2
    heel_angle_x = (df.loc[i, "21-XLHeel"] + df.loc[i, "14-XLAnkle"]) / 2
    heel_angle_y = (df.loc[i, "21-YLHeel"] + df.loc[i, "14-XLAnkle"]) / 2
    u = (toes_x - heel_angle_x, toes_y - heel_angle_y)
    v = (heel_angle_x - df.loc[i, '13-XLKnee'], heel_angle_y - df.loc[i, '13-YLKnee'])
    c = np.dot(u, v) / np.linalg.norm(u) / np.linalg.norm(v)
    df.loc[i, 'LeftHeelAngleAngle'] = np.arccos(np.clip(c, -1, 1)) * 180 / np.pi
    # heel ankle toe knee right side
    toes_x = (df.loc[i, "22-XRBigToe"] + df.loc[i, "23-XRSmallToe"]) / 2
    toes_y = (df.loc[i, "22-YRBigToe"] + df.loc[i, "23-YRSmallToe"]) / 2
    heel_angle_x = (df.loc[i, "24-XRHeel"] + df.loc[i, "11-XRAnkle"]) / 2
    heel_angle_y = (df.loc[i, "24-YRHeel"] + df.loc[i, "11-YRAnkle"]) / 2
    u = (toes_x - heel_angle_x, toes_y - heel_angle_y)
    v = (heel_angle_x - df.loc[i, '10-XRKnee'], heel_angle_y - df.loc[i, '10-YRKnee'])
    c = np.dot(u, v) / np.linalg.norm(u) / np.linalg.norm(v)
    df.loc[i, 'RightHeelAngleAngle'] = np.arccos(np.clip(c, -1, 1)) * 180 / np.pi
    # hip neck knee
    knee_x = (df.loc[i, "10-XRKnee"] + df.loc[i, "13-XLKnee"]) / 2
    knee_y = (df.loc[i, "10-YRKnee"] + df.loc[i, "13-YLKnee"]) / 2
    u = (knee_x - df.loc[i, '8-XMidHip'], knee_y - df.loc[i, '8-YMidHip'])
    v = (df.loc[i, '1-XNeck'] - df.loc[i, '8-XMidHip'], df.loc[i, '1-YNeck'] - df.loc[i, '8-YMidHip'])
    c = np.dot(u, v) / np.linalg.norm(u) / np.linalg.norm(v)
    df.loc[i, 'HipAngle'] = np.arccos(np.clip(c, -1, 1)) * 180 / np.pi

exists = os.path.isfile(OUTPUT_CSV)

if exists:
    with open(OUTPUT_CSV, 'a') as f:
        df.to_csv(f, header=False)
else:
    df.to_csv(OUTPUT_CSV)


text:
# Copyright 2020 The Kubric Authors
#
# 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
#
#    https://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.

import numpy as np
import sklearn.utils

def mm3hash(name):
  """ Compute the uint32 hash that Blenders Cryptomatte uses.
  https://github.com/Psyop/Cryptomatte/blob/master/specification/cryptomatte_specification.pdf
  """
  hash_32 = sklearn.utils.murmurhash3_32(name, positive=True)
  exp = hash_32 >> 23 & 255
  if (exp == 0) or (exp == 255):
    hash_32 ^= 1 << 23
  return hash_32

max_stars_repo_name: Xtuden-com/kubric

max_issues_repo_name: Xtuden-com/kubric

max_forks_repo_name: Xtuden-com/kubric


text:
import numpy as np
import matplotlib.pyplot as plt
import pyfits as pf
import sampler_new

import h5py
import matplotlib
matplotlib.use('Agg')
from matplotlib import rc
rc('font',**{'family':'serif','serif':'Computer Modern Roman','size':12})
rc('text', usetex=True)
from matplotlib import cm
import numpy as np
from matplotlib.colors import LogNorm
from mpl_toolkits.axes_grid1 import make_axes_locatable
#anderson = pf.open("anderson.fits")[0].data
#X = np.exp(np.loadtxt("noregwfc_mean_iter_2.txt")).reshape(100,100)
#X1 = np.exp(np.loadtxt("noregwfc_mean_iter_1.txt")).reshape(100,100)
fl=1e-5
f = .05
g = .01
H = 3
X = np.exp(np.loadtxt("superb_wfc_mean_iter_5_nfl.txt"))

Z = (X).reshape(25*H,25*H)

print Z.sum()
import c3

x0 , y0 = c3.find_centroid(Z)
print x0, y0
import scipy
from scipy import ndimage
Z2 =  scipy.ndimage.interpolation.shift(Z, [-x0,-y0], output=None, order=3, mode='nearest', prefilter=True)

k = np.array([[0, 1, 0],
              [1, 1, 1],
              [0, 1, 0]])

Z3 = ndimage.convolve(Z2, k, mode='nearest')
Z3 = Z3 * Z.sum() /Z3.sum()

#F = np.loadtxt("noregwfc_flux_iter_22.txt")
#B = np.loadtxt("noregwfc_background_iter_22.txt")
#plt.hist(F , bins = 10)
#plt.show()

data = pf.open("wfc3_f160w_clean_central100.fits")[0].data[0:120,:]
mask = pf.open("wfc3_f160w_clean_central100.fits")[1].data[0:120,:]
cx = np.loadtxt("wfc3_f160w_clean_central100_matchedfilterpoly_cx.txt")[0:120]
cy = np.loadtxt("wfc3_f160w_clean_central100_matchedfilterpoly_cy.txt")[0:120]

F = np.loadtxt("superb_wfc_flux_iter_5_nfl.txt")#.reshape(75,75)
B = np.loadtxt("superb_wfc_bkg_iter_5_nfl.txt")#.reshape(75,75)

#MS = h5py.File('masked_samplerx3.hdf5','r')
#MD = h5py.File('masked_data.hdf5','r')
#masked_samplers = MS["masked_samplerx3"]#[random]
#masked_data = MD["masked_data"]#[random]

#res = []
#brightness = []
import sampler

def lnlike(theta , p):
         x, y = theta
         Kp = sampler.imatrix_new(25, H, x, y)
         #Kp = sampler_new.imatrix_new(25, H , cx[p] , cy[p])
         model = F[p]*np.dot(fl+X,Kp) + B[p]
         resi = (data[p,:] - model).reshape(25,25)
         res = (data[p,:] - model)*100/data[p,:]
         res = res.reshape(25,25)
         chi = (data[p,:] - model)**2./(f+g*np.abs(model)) + np.log(f+g*np.abs(model))
         maskp = mask[p,:]
         chi = chi[maskp==0]
         return np.sum(chi)

import scipy.optimize as op
nll = lambda *args: lnlike(*args)

import sampler
for p in range(1,120):

         result = op.minimize(nll, [cx[p], cy[p]], args=(p))
         x_ml, y_ml, = result["x"]

         #X = Z2.flatten()
         #Kp = sampler.imatrix_new(25, H, cx[p], cy[p])
         Kp = sampler.imatrix_new(25, H, x_ml, y_ml)
         #Kp = sampler_new.imatrix_new(25, H , cx[p] , cy[p])
         model = F[p]*np.dot(fl+X,Kp) + B[p]
         resi = (data[p,:] - model).reshape(25,25)
         res = (data[p,:] - model)*100/data[p,:]
         res = res.reshape(25,25)
         chi = (data[p,:] - model)/(f+g*np.abs(model))**.5
         chi = chi.reshape(25,25)
         maskp = mask[p,:].reshape(25,25)
	 res[maskp!=0]=np.nan
         chi[maskp!=0]=np.nan
         resi[maskp!=0]=np.nan
         mi = min(data[p].min(), model.min())
         ma = max(data[p].max(), model.max())

         plt.figure(figsize=(10,10))

         plt.subplot(2,3,1)
         plt.set_cmap('RdBu')
	 ax = plt.gca()
    	 im = ax.imshow(data[p,:].reshape(25,25), interpolation = "None" , origin = "lower",norm=LogNorm())
         plt.title("SR PSF Model")
         divider = make_axes_locatable(ax)
	 cax = divider.append_axes("right", size="5%", pad=0.2)
         plt.colorbar(im , cax = cax)
	 ax.set_xticks(())
	 ax.set_yticks(())

         plt.subplot(2,3,2)

	 ax = plt.gca()
    	 im = ax.imshow(model.reshape(25,25) , interpolation = "None" , origin = "lower",norm=LogNorm())
         plt.title("Model")
         divider = make_axes_locatable(ax)
	 cax = divider.append_axes("right", size="5%", pad=0.2)
         plt.colorbar(im , cax = cax)
	 ax.set_xticks(())
	 ax.set_yticks(())

         plt.subplot(2,3,3)
	 ax = plt.gca()
    	 im = ax.imshow(Z2 , interpolation = "None" , origin = "lower",norm=LogNorm())
         plt.title("SR PSF Model")
         divider = make_axes_locatable(ax)
	 cax = divider.append_axes("right", size="5%", pad=0.2)
         plt.colorbar(im , cax = cax)
	 ax.set_xticks(())
	 ax.set_yticks(())

         plt.subplot(2,3,5)

	 ax = plt.gca()
    	 im = ax.imshow(resi.reshape(25,25) , interpolation = "None" , origin = "lower")
         plt.title("Residual")
         divider = make_axes_locatable(ax)
	 cax = divider.append_axes("right", size="5%", pad=0.2)
         plt.colorbar(im , cax = cax)
	 ax.set_xticks(())
	 ax.set_yticks(())

         plt.subplot(2,3,4)

	 ax = plt.gca()
    	 im = ax.imshow(chi.reshape(25,25) , interpolation =  "None", origin = "lower")
	 plt.title("chi")
         divider = make_axes_locatable(ax)
	 cax = divider.append_axes("right", size="5%", pad=0.2)
         plt.colorbar(im , cax = cax)
	 ax.set_xticks(())
	 ax.set_yticks(())

         #plt.subplot(3,3,6)

	 #ax = plt.gca()
    	 #im = ax.imshow(res.reshape(25,25) , interpolation =  "None", origin = "lower")
	 #plt.title("(residual)x100/data")
         #divider = make_axes_locatable(ax)
	 #cax = divider.append_axes("right", size="5%", pad=0.2)
         #plt.colorbar(im , cax = cax)
	 #ax.set_xticks(())
	 #ax.set_yticks(())

         plt.subplot(2,3,6)

	 ax = plt.gca()
    	 im = ax.imshow(maskp.reshape(25,25) , interpolation =  "None", origin = "lower")
	 plt.title("Data Quality")
         divider = make_axes_locatable(ax)
	 cax = divider.append_axes("right", size="5%", pad=0.2)
         plt.colorbar(im , cax = cax)
	 ax.set_xticks(())
	 ax.set_yticks(())

         plt.tight_layout()
         plt.show()

         #plt.savefig("all_newstars_%d.png"%(p))
         plt.close()

max_stars_repo_name: mjvakili/supermean

max_issues_repo_name: mjvakili/supermean

max_forks_repo_name: mjvakili/supermean

reason: import numpy,import scipy,from scipy


text:
import pandas as pd
import numpy as np
import nltk
from nltk import stem
from nltk.tokenize import RegexpTokenizer
from utils.datareader import Datareader
from tqdm import tqdm
from scipy import sparse
from difflib import SequenceMatcher
from difflib import get_close_matches
from utils.pre_processing import *
from recommenders.similarity.dot_product import dot_product
from recommenders.similarity.s_plus import tversky_similarity
from utils.evaluator import Evaluator
from utils.post_processing import *
from personal.Tommaso.NLP.NLP import NLP
from utils.definitions import *

# Datareader
dr = Datareader(mode='online', only_load=True)
#ev = Evaluator(dr)

# Dataframe with interactions
df_train = dr.get_df_train_interactions()
df_test = dr.get_df_test_interactions()
df = pd.concat([df_train, df_test], axis=0, join='outer')

playlists = df['pid'].as_matrix()
tracks = df['tid'].as_matrix()
dictionary = dr.get_track_to_artist_dict()

pids = list(dr.get_train_pids()) + list(dr.get_test_pids())

# URM
urm = dr.get_urm()
urm = urm[pids]
print(urm.shape)

print('artists...')
artists = [dictionary[t] for t in tracks]

print('ucm...')
ucm = sparse.csr_matrix((np.ones(len(playlists)), (playlists, artists)), shape=(1049361, len(dr.get_artists())))
ucm = ucm.tocsr()
ucm = ucm[pids]
print(ucm.shape)

ucm = bm25_row(ucm)

print('similarity..')
sim = tversky_similarity(ucm, ucm.T, shrink=200, alpha=0.1, beta=1, k=800, verbose=1, binary=False)
sim = sim.tocsr()

test_pids = list(dr.get_test_pids())

eurm = dot_product(sim, urm, k=750)
eurm = eurm.tocsr()
eurm = eurm[test_pids, :]
sparse.save_npz('eurm_artists.npz', eurm)

#ev.evaluate(eurm_to_recommendation_list(eurm), name='cbf_user_artist', show_plot=False)

exit()

max_stars_repo_name: edervishaj/spotify-recsys-challenge

max_issues_repo_name: kiminh/spotify-recsys-challenge

max_forks_repo_name: kiminh/spotify-recsys-challenge

reason: import numpy,from scipy


text:
"""
Root pandas accessors for DataFrames. It exposes the ActiviyData as DataFrames

"""
import warnings

import numpy as np
import pandas as pd
import runpandas.reader
from pandas.core.frame import DataFrame
from runpandas.types import columns

class Activity(pd.DataFrame):
    """
    An Activity object is a pandas.DataFrame that provides useful
    methods and has specific columns with special functionalities.
    In addition to the standard DataFrame constructor arguments,
    Activity also accepts the following optional arguments:

    Parameters
    ----------
    cspecs : dict of series (optional)
        Column Specifications (cspecs) of the ``Activity``
    start : str, datetime (optional)
        Start timestamp of the ``Activity``
    """

    # properties to propagate
    _metadata = ["start"]

    def __init__(self, *args, **kwargs):
        cspecs = kwargs.pop("cspecs", None)
        start = kwargs.pop("start", None)

        super().__init__(*args, **kwargs)

        if cspecs is not None:
            self.set_specs(cspecs, inplace=True)
        if start is not None:
            self.start = start

    def set_specs(self, cspecs=None, inplace=False):
        """
        Set the Column Specifications (cspecs) of the ``Activity``.
        Set the specifed column on Activity.

        Parameters
        ----------
        cspecs : dict of series
        inplace : bool, default False
            If True, the cspecs of the Activity will be changed in place
            (while still returning the result) instead of making a copy of
            the Activity.
        """
        if not inplace:
            df = self.copy()
        else:
            df = self
        for old_key, column_cls in cspecs.items():
            try:
                old_column = df.pop(old_key)  # no default
            except KeyError:
                warnings.warn("The specified key %s not found." % old_key, UserWarning)
                continue

            new = column_cls(old_column)
            df[new.colname] = new
        if not inplace:
            return df

    @property
    def _constructor(self):
        return Activity

    def to_pandas(self):
        """
        Returns:
            Casted Pandas Dataframe from Activity
        """
        return DataFrame(self)

    def __finalize__(self, other, method=None, **kwargs):
        """Propagate metadata from other to self."""
        for name in self._metadata:
            object.__setattr__(self, name, getattr(other, name, None))
        return self

    def __getitem__(self, key):
        """
        If the result is a column containing only activity measures, return a
        MeasuredSeries. If it's a DataFrame with a 'Measures' column, return a
        Activity.
        """
        result = super(Activity, self).__getitem__(key)

        if isinstance(key, str) and key in columns.ColumnsRegistrator.REGISTRY:
            result.__class__ = columns.ColumnsRegistrator.REGISTRY[key]
        elif isinstance(result, pd.DataFrame):
            result.__class__ = Activity

        if isinstance(result, columns.Gradient):
            result._set_attrs(
                _rise=self["alt"].diff().values, _run=self["dist"].diff().values
            )

        return result

    @classmethod
    def from_file(cls, file_path, **kwargs):
        """
        Alternate constructor to create a ``Activity`` from a file.

        It is recommended to use :func:`runpandas.reader._read_file` instead.

        Parameters
        ----------
        file_path : str
            File path or file handle to read from. Depending on which kwargs
            are included, the content of filename may vary.
        kwargs : key-word arguments
            These arguments are passed to reader._read_file
        """
        return runpandas.reader._read_file(file_path)

    @property
    def ellapsed_time(self):
        """
        Returns:
            The duration of activity in `pandas.TimedeltaIndex` object.

        Raises:
            AttributeError if dataframe index is not an instance of TimedeltaIndex

        """
        if isinstance(self.index, pd.TimedeltaIndex):
            return self.index[-1]
        raise AttributeError("index is not TimedeltaIndex")

    @property
    def moving_time(self):
        """
        Returns:
            The moving time of activity in `pandas.TimedeltaIndex` object.
            It measures only the periods that you were active based on
            internal calculations.

        Raises:
            AttributeError if dataframe index is not an instance of TimedeltaIndex
            or the `moving` column is not found computed from
            `runpandas.acessors.moving.only_moving` acessor.

        """
        if not isinstance(self.index, pd.TimedeltaIndex):
            raise AttributeError("index is not TimedeltaIndex")
        if "moving" not in self.columns:
            raise AttributeError("moving column not found in activity.")

        total_time = (
            self.index.to_series().diff().fillna(self.index[0])
        ) / np.timedelta64(1, "s")
        merged_df = total_time.to_frame().join(self["moving"].to_frame())
        return pd.Timedelta(
            seconds=total_time.sum()
            - merged_df[~merged_df["moving"].astype(bool)]["time"].sum()
        )

    @property
    def distance(self):
        """
        Returns:
            The total distance of the activity in meters as a float number.
        """
        try:
            return self["dist"].max()
        except KeyError:
            return self["distpos"].sum()

    def mean_speed(self, only_moving=False, smoothing=True):
        """
        It calculates the average speed based on the speed trace.

        Parameters
        ----------
        only_moving : boolean, optional. It considers only the active moviment of the activity.
        Default is False.

        smoothing: boolean, optional. If True, it calculates average speed based on total
        distance divided by total time. Default is True.

        Returns:
        --------
            The average speed in meters/second for the activity.
        """
        if "speed" not in self.columns:
            raise AttributeError("speed column not found in activity.")

        if only_moving:
            total_time = self.moving_time
            activity = self[self["moving"]]
        else:
            total_time = self.ellapsed_time
            activity = self

        if smoothing:
            time_diff = (
                self.index.to_series().diff().fillna(self.index[0])
            ) / np.timedelta64(1, "s")
            total_distance = (activity["speed"] * time_diff).sum()
        else:
            total_distance = activity.distance

        return total_distance / total_time.total_seconds()

    def mean_pace(self, only_moving=False, smoothing=True):
        """
        It calculates the average pace based on the speed trace.

        Parameters
        ----------
        only_moving : boolean, optional. It considers only the active moviment of the activity.
        Default is False.

        smoothing: boolean, optional. If True, it calculates average pace based on total
        distance divided by total time. Default is True.

        Returns:
        --------
            The average pace in sec/m for the activity.
        """
        speed = self.mean_speed(only_moving, smoothing)
        return pd.Timedelta(seconds=1 / speed)

    def mean_heart_rate(self, only_moving=False):
        """
        It calculates the average heart rate based on the heart rate tracked
        by HeartRate Series column.

        Parameters
        ----------
        only_moving : boolean, optional. It considers only the active moviment of the activity.
        Default is False.

        Returns:
        --------
            The average heart zone in bpm for the activity.
        """
        if "hr" not in self.columns:
            raise AttributeError("heart rate column not found in activity.")

        if only_moving:
            activity = self[self["moving"]]
        else:
            activity = self

        return activity["hr"].mean()

    def mean_cadence(self, only_moving=False):
        """
        It calculates the average cadence based on the cadence tracked
        by Cadence Series column.

        Parameters
        ----------
        only_moving : boolean, optional. It considers only the active moviment of the activity.
        Default is False.

        Returns:
        --------
            The average cadence in rotation per minute (rpm) for the activity.
        """
        if "cad" not in self.columns:
            raise AttributeError("Cadence column not found in activity.")

        if only_moving:
            activity = self[self["moving"]]
        else:
            activity = self

        return activity["cad"].mean()

    def summary(self):
        """
        Display the common basic statistics for the activity.

        Returns:
        --------
            pandas.Dataframe:  A pandas DataFrame containing the summary statistics, which
            inclues estimates of the total distance covered, the total duration,
            the time spent moving, and many others.

        """
        return runpandas.types.summary.activity_summary(self)

max_stars_repo_name: pnposch/runpandas

max_issues_repo_name: pnposch/runpandas

max_forks_repo_name: pnposch/runpandas


text:
import glob
import os
import sys
import h5py
import numpy as np
import torch

output = open('deeprank_predictions.txt', 'w')
output.write('pdb class p_crystal p_bio \n')

for hdf5 in glob.glob('*/'):
    name=hdf5.split('/')[0]

    if os.path.exists('{}test_data.hdf5'.format(hdf5)):

        f = h5py.File('{}test_data.hdf5'.format(hdf5),'r')

        results = f['epoch_0000']['test']['outputs'][()]
        res = results[0]
        softmax = torch.nn.Softmax(dim=0)
        res_softmax = softmax(torch.Tensor(res)).numpy()

        bin_class_res = np.argmax(res)

        if bin_class_res1 == 0 :
            pred = 'crystal'

        else :
            pred = 'biological'

        output.write(name+" "+pred+" "+str(bin_class_res)+" "+str(round(res_softmax[0],3))+" "+str(round(res_softmax[1],3))+"\n")

    else:
        output.write(name+" None None None None \n")

output.close()


text:
# -*- coding: utf-8 -*-
# @Time    : 2020/3/24 10:47
# @Author  : Dai PuWei
# @Email   : 771830171@qq.com
# @File    : vgg16.py
# @Software: PyCharm

import os
import cv2
import datetime
import numpy as np
import tensorflow as tf
from tensorflow import keras as K

from utils.model_utils import AverageMeter
from utils.model_utils import categorical_accuracy
from utils.model_utils import categorical_crossentropy_loss

from utils.plot_utils import plot_loss
from utils.plot_utils import plot_accuracy
from utils.plot_utils import make_summary

class VGG16(object):

    def __init__(self,cfg):
        """
        这是VGG16的初始化函数
        :param cfg: 参数配置类
        """
        self.cfg = cfg

        # 定义相关变量
        self.real_label = tf.placeholder(tf.float32, shape=(None, self.cfg.label_num))          # 真丝标签
        self.global_step = tf.Variable(tf.constant(0), trainable=False)                         # 当前迭代次数
        self.init_learning_rate = tf.Variable(tf.constant(self.cfg.init_learning_rate))         # 初始学习率

        # 定义VGG16的前向传播过程
        self.build_model()
        """
        self.model.compile(optimizer=K.optimizers.SGD(learning_rate=self.cfg.init_learning_rate,
                                                      momentum=self.cfg.momentum_rate),
                           loss=categorical_crossentropy_loss)
        """
        self.model.compile(optimizer=K.optimizers.Adam(learning_rate=self.cfg.init_learning_rate),
                           loss="categorical_crossentropy")

        # 定义损失函数与精度
        self.loss = categorical_crossentropy_loss(self.real_label, self.pred_label)
        self.accuracy = categorical_accuracy(self.real_label, self.pred_label)

        # 定义学习率、优化器和训练过程
        """
        self.learning_rate = tf.train.exponential_decay(learning_rate=self.init_learning_rate,
                                                        global_step=self.global_step,
                                                        decay_steps=self.cfg.decay_step,
                                                        decay_rate=self.cfg.decay_rate,
                                                        staircase=True)
        self.optimizer = tf.train.MomentumOptimizer(learning_rate=self.learning_rate,
                                                    momentum=self.cfg.momentum_rate)
        """
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.cfg.init_learning_rate)
        self.train_op = self.optimizer.minimize(self.loss, global_step=self.global_step)

        # 定义模型保存类与加载类
        self.saver_save = tf.train.Saver(max_to_keep=100)  # 设置最大保存检测点个数为周期数

    def build_model(self):
        """
        这是VGG16网络的搭建函数
        :return:
        """
        self.image_input = K.layers.Input(shape=self.cfg.image_input_shape,name="image_input")
        # Block 1
        x = K.layers.Conv2D(64, (3, 3),activation='relu',padding='same',name='block1_conv1')(self.image_input)
        x = K.layers.Conv2D(64, (3, 3),activation='relu',padding='same',name='block1_conv2')(x)
        x = K.layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

        # Block 2
        x = K.layers.Conv2D(128, (3, 3),activation='relu',padding='same',name='block2_conv1')(x)
        x = K.layers.Conv2D(128, (3, 3),activation='relu',padding='same',name='block2_conv2')(x)
        x = K.layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

        # Block 3
        x = K.layers.Conv2D(256, (3, 3),activation='relu',padding='same',name='block3_conv1')(x)
        x = K.layers.Conv2D(256, (3, 3),activation='relu',padding='same',name='block3_conv2')(x)
        x = K.layers.Conv2D(256, (3, 3),activation='relu',padding='same',name='block3_conv3')(x)
        x = K.layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

        # Block 4
        x = K.layers.Conv2D(512, (3, 3),activation='relu',padding='same',name='block4_conv1')(x)
        x = K.layers.Conv2D(512, (3, 3),activation='relu',padding='same',name='block4_conv2')(x)
        x = K.layers.Conv2D(512, (3, 3),activation='relu',padding='same',name='block4_conv3')(x)
        x = K.layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

        # Block 5
        x = K.layers.Conv2D(512, (3, 3),activation='relu',padding='same',name='block5_conv1')(x)
        x = K.layers.Conv2D(512, (3, 3),activation='relu',padding='same',name='block5_conv2')(x)
        x = K.layers.Conv2D(512, (3, 3),activation='relu',padding='same',name='block5_conv3')(x)
        x = K.layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

        # classification block
        x = K.layers.Flatten(name='flatten')(x)
        x = K.layers.Dense(4096, activation='relu', name='fc1')(x)
        x = K.layers.Dense(4096, activation='relu', name='fc2')(x)
        self.pred_label = K.layers.Dense(self.cfg.label_num, activation='softmax', name='pred_label')(x)

        self.model = K.Model(self.image_input,self.pred_label)
        self.model.summary()

    def train(self,train_datagen,val_datagen,train_iter_num,val_iter_num,interval,pre_model_path=None):
        """
        这是VGG16的训练函数
        :param train_datagen: 训练数据集生成器
        :param val_datagen: 验证数据集生成器
        :param train_iter_num: 一个epoch训练迭代次数
        :param val_iter_num: 一个epoch验证迭代次数
        :param interval: 验证间隔
        :param pre_model_path: 预训练模型地址,与训练模型为ckpt文件，注意文件路径只需到.ckpt即可。
        """
        # 初始化相关文件目录路径
        time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        checkpoint_dir = os.path.join(self.cfg.checkpoints_dir,time)
        if not os.path.exists(checkpoint_dir):
            os.mkdir(checkpoint_dir)

        log_dir = os.path.join(self.cfg.logs_dir, time)
        if not os.path.exists(log_dir):
            os.mkdir(log_dir)

        result_dir = os.path.join(self.cfg.result_dir, time)
        if not os.path.exists(result_dir):
            os.mkdir(result_dir)

        self.cfg.save_config(time)

        # 初始化训练损失和精度数组
        train_loss_results = []                     # 保存训练loss值
        train_accuracy_results = []                 # 保存训练accuracy值

        # 初始化验证损失和精度数组，验证最大精度
        val_ep = []
        val_loss_results = []                     # 保存验证loss值
        val_accuracy_results = []                 # 保存验证accuracy值
        val_acc_max = 0                           # 最大验证精度

        with tf.Session() as sess:
            # 初始化变量
            sess.run(tf.global_variables_initializer())

            # 加载预训练模型
            if pre_model_path is not None:
                if ".ckpt" in pre_model_path:           # pre_model_path的地址写到.ckpt
                    saver_restore = tf.train.import_meta_graph(pre_model_path+".meta")
                    saver_restore.restore(sess,pre_model_path)
                    print(1)
                elif ".h5" in pre_model_path:
                    self.model.load_weights(pre_model_path,by_name=True)
                    print(2)
                print("restore model from : %s" % (pre_model_path))

            self.merged = tf.summary.merge_all()
            self.writer = tf.summary.FileWriter(log_dir, sess.graph)
            self.writer1 = tf.summary.FileWriter(os.path.join("./tf_dir"), sess.graph)

            print('\n----------- start to train -----------\n')

            total_global_step = self.cfg.epoch * train_iter_num
            for ep in np.arange(self.cfg.epoch):
                # 初始化每次迭代的训练损失与精度平均指标类
                epoch_loss_avg = AverageMeter()
                epoch_accuracy = AverageMeter()

                # 初始化精度条
                progbar = K.utils.Progbar(train_iter_num)
                print('Epoch {}/{}'.format(ep+1, self.cfg.epoch))
                batch_domain_labels = np.vstack([np.tile([1., 0.], [self.cfg.batch_size // 2, 1]),
                                           np.tile([0., 1.], [self.cfg.batch_size // 2, 1])])
                for i in np.arange(1,train_iter_num+1):
                    # 获取小批量数据集及其图像标签与域标签
                    batch_images, batch_labels = train_datagen.__next__()#train_source_datagen.next_batch()

                    # 前向传播,计算损失及其梯度
                    op,train_loss,train_acc = \
                                    sess.run([self.train_op,self.loss,self.accuracy],
                                                feed_dict={self.image_input:batch_images,
                                                            self.real_label:batch_labels})
                    #self.writer.add_summary(make_summary('learning_rate', learning_rate),global_step=global_step)
                    #self.writer1.add_summary(make_summary('learning_rate', learning_rate), global_step=global_step)

                    # 更新训练损失与训练精度
                    epoch_loss_avg.update(train_loss,1)
                    epoch_accuracy.update(train_acc,1)

                    # 更新进度条
                    progbar.update(i, [('train_loss', train_loss),("train_acc",train_acc)])

                # 保存相关损失与精度值，可用于可视化
                train_loss_results.append(epoch_loss_avg.average)
                train_accuracy_results.append(epoch_accuracy.average)

                self.writer.add_summary(make_summary('train/train_loss', epoch_loss_avg.average),global_step=ep+1)
                self.writer.add_summary(make_summary('accuracy/train_accuracy', epoch_accuracy.average),global_step=ep+1)

                self.writer1.add_summary(make_summary('train/train_loss', epoch_loss_avg.average),global_step=ep+1)
                self.writer1.add_summary(make_summary('accuracy/train_accuracy', epoch_accuracy.average),global_step=ep+1)

                if (ep+1) % interval == 0:
                    # 评估模型在验证集上的性能
                    val_ep.append(ep)
                    val_loss,val_accuracy = self.eval_on_val_dataset(sess,val_datagen,val_iter_num,ep+1)
                    val_loss_results.append(val_loss)
                    val_accuracy_results.append(val_accuracy)
                    str =  "Epoch{:03d}_val_loss_{:.3f},val_accuracy_{:.3%}".format(ep+1,val_loss,val_accuracy)
                    print(str)

                    if val_accuracy >= val_acc_max:              # 验证精度达到当前最大，保存模型
                        val_acc_max = val_accuracy
                        self.saver_save.save(sess,os.path.join(checkpoint_dir,str+".ckpt"))
                        print(1)
                        self.model.save(os.path.join(checkpoint_dir,str+".h5"))
                        print(2)

            # 保存训练与验证结果
            path = os.path.join(result_dir, "loss.jpg")
            plot_loss(np.arange(1, len(train_loss_results) + 1),
                        [np.array(train_loss_results), np.array(val_loss_results)],path)

            train_acc_results = np.array(train_accuracy_results)[np.array(val_ep)]
            path = os.path.join(result_dir, "accuracy.jpg")
            plot_accuracy(np.array(val_ep) + 1, [train_acc_results, val_accuracy_results], path)

            # 保存最终的模型
            model_path = os.path.join(checkpoint_dir,"trained_model.ckpt")
            self.saver_save.save(sess,model_path)
            print("Train model finshed. The model is saved in : ", model_path)
            print('\n----------- end to train -----------\n')

    def eval_on_val_dataset(self,sess,val_datagen,val_batch_num,ep):
        """
        这是评估模型在验证集上的性能的函数
        :param sess: tf的会话变量
        :param val_datagen: 验证集数据集生成器
        :param val_batch_num: 验证集数据集批量个数
        :param ep: 当前周期数
        """
        epoch_loss_avg = AverageMeter()
        epoch_accuracy = AverageMeter()
        for i in np.arange(1, val_batch_num + 1):
            # 获取小批量数据集及其图像标签与域标签
            batch_images, batch_labels = val_datagen.__next__()

            # 在验证阶段只利用目标域数据及其标签进行测试,计算模型在验证集上相关指标的值
            val_loss, val_acc = sess.run([self.loss, self.accuracy],
                                            feed_dict={self.image_input: batch_images,
                                                    self.real_label: batch_labels})
            # 更新损失与精度的平均值
            epoch_loss_avg.update(val_loss, 1)
            epoch_accuracy.update(val_acc, 1)

        self.writer.add_summary(make_summary('val/val_loss', epoch_loss_avg.average),global_step=ep)
        self.writer.add_summary(make_summary('accuracy/val_accuracy', epoch_accuracy.average),global_step=ep)

        self.writer1.add_summary(make_summary('val/val_loss', epoch_loss_avg.average),global_step=ep)
        self.writer1.add_summary(make_summary('accuracy/val_accuracy', epoch_accuracy.average),global_step=ep)
        return epoch_loss_avg.average,epoch_accuracy.average

    def test_single_image(self,image_path,weight_path):
        """
        这是对一张图像进行分割的函数
        :param image_path: 图像路径
        :param weight_path: 模型路径
        :return:
        """
        with tf.Session() as sess:
            # 初始化变量
            sess.run(tf.global_variables_initializer())

            # 加载预训练模型
            if weight_path is not None:
                if ".ckpt" in weight_path:          # pre_model_path的地址写到.ckpt
                    saver_restore = tf.train.import_meta_graph(weight_path + ".meta")
                    saver_restore.restore(sess, weight_path)
                    print(1)
                elif ".h5" in weight_path:
                    self.model.load_weights(weight_path, by_name=True)
                    print(2)
                print("restore model from : %s" % (weight_path))

            # 导入图像,并扩充一维以满足tf中输入张量形状要求
            image = cv2.imread(image_path)
            image = cv2.resize(image,(self.cfg.image_size,self.cfg.image_size))
            batch_image = np.expand_dims(image,axis=0)
            print(np.shape(batch_image))

            # 运行模型，获取分类结果
            pred_label = sess.run([self.pred_label],feed_dict={self.image_input:batch_image})
            pred_label = np.argmax(pred_label)
            print("%s is predited as %s" %(image_path,self.cfg.label_dict[pred_label]))

    def test_batch_images(self,image_dir,weight_path):
        """
        这是对一张图像进行分割的函数
        :param image_dir: 图像保存目录
        :param weight_path: 模型路径
        :return:
        """
        with tf.Session() as sess:
            # 初始化变量
            sess.run(tf.global_variables_initializer())

            # 加载预训练模型
            if weight_path is not None:
                if ".ckpt" in weight_path:          # pre_model_path的地址写到.ckpt
                    saver_restore = tf.train.import_meta_graph(weight_path + ".meta")
                    saver_restore.restore(sess, weight_path)
                    print(1)
                elif ".h5" in weight_path:
                    self.model.load_weights(weight_path, by_name=True)
                    print(2)
                print("restore model from : %s" % (weight_path))

            # 导入图像,并扩充一维以满足tf中输入张量形状要求
            batch_images = []
            batch_image_paths = []
            for image_name in os.listdir(image_dir):
                image_path = os.path.join(image_dir,image_name)
                image = cv2.resize(image, (self.cfg.image_size,self.cfg.image_size))
                batch_image_paths.append(image_path)
                batch_images.append(cv2.imread(image_path))
            batch_images = np.array(batch_images)
            print(np.shape(batch_images))

            # 运行模型，获取分类结果
            batch_pred_labels = sess.run([self.pred_label],feed_dict={self.image_input:batch_images})
            batch_pred_labels = np.argmax(batch_pred_labels,axis=-1).flatten()
            for image_path,pred_label in zip(batch_image_paths,batch_pred_labels):
                print("%s is predited as %s" % (image_path, self.cfg.label_dict[pred_label]))

    def eval_on_dataset(self,datagen,batch_num,weight_path):
        """
        这是评估模型在验证集上的性能的函数
        :param sess: tf的会话变量
        :param datagen: 数据集生成器
        :param batch_num: 数据集批量个数
        :param weight_path: 模型路径
        """
        with tf.Session() as sess:
            # 初始化变量
            sess.run(tf.global_variables_initializer())

            # 加载预训练模型
            if weight_path is not None:
                if ".ckpt" in weight_path:          # pre_model_path的地址写到.ckpt
                    saver_restore = tf.train.import_meta_graph(weight_path + ".meta")
                    saver_restore.restore(sess, weight_path)
                    print(1)
                elif ".h5" in weight_path:
                    self.model.load_weights(weight_path, by_name=True)
                    print(2)
                print("restore model from : %s" % (weight_path))

            loss = AverageMeter()
            accuracy = AverageMeter()
            for i in np.arange(batch_num):
                # 获取小批量数据集及其图像标签与域标签
                batch_images, batch_labels = datagen.__next__()

                # 在验证阶段只利用目标域数据及其标签进行测试,计算模型在验证集上相关指标的值
                val_loss, val_acc = sess.run([self.loss, self.accuracy],
                                                feed_dict={self.image_input: batch_images,
                                                        self.real_label: batch_labels})
                # 更新损失与精度的平均值
                loss.update(val_loss, 1)
                accuracy.update(val_acc, 1)

            print("eval on this dataset, loss is {:.3f} , accuracy is {:.3f%}".format(loss.average,accuracy.average))


text:
import subprocess
from subprocess import Popen, PIPE
import signal
import numpy
import time

L = 1
max_L = 128
threshold = 1000
prime_numbers = [2,   3,   5,   7,  11,  13,  17,  19,  23,  29,  31,  37,
                 41,  43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97,
                 101, 103, 107, 109, 113, 127, 131, 137]
results = []
stop = False

def stop_benchmark(signal, frame):
    global stop
    print("Benchmark is stopped!")
    stop = True

signal.signal(signal.SIGINT, stop_benchmark)

log = open("1st_step.log", "w")
branches = open("branches", "w")
branches.write("if(i % L == 0) a = 0;\n")
branches.close()

#first step
print("1st step start!\n")
while not stop and L <= max_L:

    L_file = open("declarations", "w")
    L_file.write("long long int a = 1;\n")
    L_file.write("int L = " + str(L) + ";")
    L_file.close()

    subprocess.call(["g++", "benchmark.cpp", "-lpapi"])

    with Popen(["./a.out"], stdout=PIPE) as test:
        try:
            mpr = float(test.stdout.read().decode("ascii"))
        except:
            continue
        log.write(str(L) + " " + str(mpr) + "\n")
        results.append(mpr)
    L = L + 1

log.close()

L_r = numpy.argmax(results)
while results[L_r] / results[L_r - 1] < threshold:
    L_r = L_r - 1

L = L_r
print("MAXIMUM LENGTH OF HISTORY: " + str(L))
print("MPR(MAX_LENGTH) = " + str(results[L - 1]) + " %")
print("MPR(MAX_LENGTH + 1) = " + str(results[L]) + " %")

# second step
print("\n2nd step start!\n")

L_file = open("declarations", "w")
L_file.write("long long int a = 1;\n")
L_file.write("int L = " + str(L_r) + ";")
L_file.close()

L = 2 * (L_r - 1)

branches = open("branches", "w")
for i in range(1, L + 1):
    branches.write("if(i < 0) a = 1;\n")
branches.write("if(i % L == 0) a = 0;\n")
branches.close()

subprocess.call(["g++", "benchmark.cpp", "-lpapi"])

mpr = 0.0
with Popen(["./a.out"], stdout=PIPE) as test:
    try:
        mpr = float(test.stdout.read().decode("ascii"))
    except:
        print("Step 2: something went wrong!")

print("MPR = " + str(mpr) + " %")

# third step
print("\n3rd step start!\n")
L = L_r
i = 0
while prime_numbers[i] < L:
    i = i + 1
L1 = prime_numbers[i - 2]
L2 = prime_numbers[i - 1]

L_file = open("declarations", "w")
L_file.write("int L1 = " + str(L1) + ";\n")
L_file.write("int L2 = " + str(L2) + ";\n")
L_file.write("int a, b, c;\n")
L_file.close()

branches = open("branches", "w")
branches.write("if(i % L1 == 0) a = 1; else a = 0;\n")
branches.write("if(i % L2 == 0) b = 1; else b = 0;\n")
branches.write("if(a * b == 1) c = 1;\n")
branches.close()

subprocess.call(["g++", "benchmark.cpp", "-lpapi"])

with Popen(["./a.out"], stdout=PIPE) as test:
    try:
        mpr = float(test.stdout.read().decode("ascii"))
    except:
        print("Step 3: something went wrong!")

print("MPR = " + str(mpr) + " %")

# fifth step
print("\n5th step start!\n")
L3 = L_r + 1

L_file = open("declarations", "w")
L_file.write("int L3 = " + str(L3) + ";\n")
L_file.write("int a;\n")
L_file.close()

branches = open("branches", "w")
branches.write("if(i % L3 == 0) a = 1;\n")
branches.close()

subprocess.call(["g++", "benchmark.cpp", "-lpapi"])

with Popen(["./a.out"], stdout=PIPE) as test:
    try:
        mpr = float(test.stdout.read().decode("ascii"))
    except:
        print("Step 5: something went wrong!")

print("MPR for 1 branch = " + str(mpr) + " %")

branches = open("branches", "w")
branches.write("if(i % L3 == 0) a = 1;\n")
branches.write("if(i % L3 == 0) a = 1;\n")
branches.close()

subprocess.call(["g++", "benchmark.cpp", "-lpapi"])

with Popen(["./a.out"], stdout=PIPE) as test:
    try:
        mpr = float(test.stdout.read().decode("ascii"))
    except:
        print("Step 5: something went wrong!")

print("MPR for 2 branches = " + str(mpr) + " %")

# delay
print("\nDefine a delay!\n")

L_file = open("declarations", "w")
L_file.write("int L = " + str(L_r) + ";")
L_file.close()

branches = open("branches", "w")
branches.write("if(i % " + str(L_r) + ") a = 1;\n")
branches.close()

subprocess.call(["g++", "delay.cpp", "-lpapi"])

mp_cycles = 0
with Popen(["./a.out"], stdout=PIPE) as test:
    try:
        mp_cycles = float(test.stdout.read().decode("ascii"))
    except:
        print("Delay: something went wrong!")

branches = open("branches", "w")
branches.write("if(i % " + str(L_r - 1) + ") a = 1;\n")
branches.close()

subprocess.call(["g++", "delay.cpp", "-lpapi"])

p_cycles = 0
with Popen(["./a.out"], stdout=PIPE) as test:
    try:
        p_cycles = float(test.stdout.read().decode("ascii"))
    except:
        print("Delay: something went wrong!")

print("Delay = " + str(int((mp_cycles - p_cycles) * (L_r - 1))) + " cycles")


text:
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
from functools import partial

import mmcv
import numpy as np
import onnxruntime as rt
import torch
import os
from mmcv.onnx import register_extra_symbolics
from mmcv.runner import load_checkpoint

from mmcls.models import build_classifier

torch.manual_seed(3)

def _demo_mm_inputs(input_shape, num_classes):
    """Create a superset of inputs needed to run test or train batches.

    Args:
        input_shape (tuple):
            input batch dimensions
        num_classes (int):
            number of semantic classes
    """
    (N, C, H, W) = input_shape
    rng = np.random.RandomState(0)
    imgs = rng.rand(*input_shape)
    gt_labels = rng.randint(
        low=0, high=num_classes, size=(N, 1)).astype(np.uint8)
    mm_inputs = {
        'imgs': torch.FloatTensor(imgs).requires_grad_(True),
        'gt_labels': torch.LongTensor(gt_labels),
    }
    return mm_inputs

def pytorch2onnx(model,
                 input_shape,
                 opset_version=11,
                 dynamic_export=False,
                 show=False,
                 output_file='tmp.onnx',
                 do_simplify=False,
                 verify=False):
    """Export Pytorch model to ONNX model and verify the outputs are same
    between Pytorch and ONNX.

    Args:
        model (nn.Module): Pytorch model we want to export.
        input_shape (tuple): Use this input shape to construct
            the corresponding dummy input and execute the model.
        opset_version (int): The onnx op version. Default: 11.
        show (bool): Whether print the computation graph. Default: False.
        output_file (string): The path to where we store the output ONNX model.
            Default: `tmp.onnx`.
        verify (bool): Whether compare the outputs between Pytorch and ONNX.
            Default: False.
    """
    model.cpu().eval()

    if hasattr(model.head, 'num_classes'):
        num_classes = model.head.num_classes
    # Some backbones use `num_classes=-1` to disable top classifier.
    elif getattr(model.backbone, 'num_classes', -1) > 0:
        num_classes = model.backbone.num_classes
    else:
        raise AttributeError('Cannot find "num_classes" in both head and '
                             'backbone, please check the config file.')

    mm_inputs = _demo_mm_inputs(input_shape, num_classes)

    imgs = mm_inputs.pop('imgs')
    img_list = [img[None, :] for img in imgs]

    # replace original forward function
    origin_forward = model.forward
    model.forward = partial(model.forward, img_metas={}, return_loss=False)

    # register_extra_symbolics(opset_version)

    # support dynamic shape export
    if dynamic_export:
        dynamic_axes = {
            'input': {
                0: 'batch',
                2: 'width',
                3: 'height'
            },
            'probs': {
                0: 'batch'
            }
        }
    else:
        dynamic_axes = {}

    with torch.no_grad():
        torch.onnx.export(
            model,
            (img_list, ),
            output_file,
            verbose=False,
            enable_onnx_checker=False,
            do_constant_folding=False,
            opset_version=opset_version,
            operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK
        )
        print(f'Successfully exported ONNX model: {output_file}')
    model.forward = origin_forward

    if do_simplify:
        from mmcv import digit_version
        import onnxsim
        import onnx

        min_required_version = '0.3.0'
        assert digit_version(mmcv.__version__) >= digit_version(
            min_required_version
        ), f'Requires to install onnx-simplify>={min_required_version}'

        if dynamic_axes:
            input_shape = (input_shape[0], input_shape[1], input_shape[2] * 2,
                           input_shape[3] * 2)
        else:
            input_shape = (input_shape[0], input_shape[1], input_shape[2],
                           input_shape[3])
        imgs = _demo_mm_inputs(input_shape, model.head.num_classes).pop('imgs')
        input_dic = {'input': imgs.detach().cpu().numpy()}
        input_shape_dic = {'input': list(input_shape)}

        model_opt, check_ok = onnxsim.simplify(
            output_file,
            input_shapes=input_shape_dic,
            input_data=input_dic,
            dynamic_input_shape=dynamic_export)
        if check_ok:
            onnx.save(model_opt, output_file)
            print(f'Successfully simplified ONNX model: {output_file}')
        else:
            print('Failed to simplify ONNX model.')
    if verify:
        # check by onnx
        import onnx
        onnx_model = onnx.load(output_file)
        onnx.checker.check_model(onnx_model)

        # test the dynamic model
        if dynamic_export:
            dynamic_test_inputs = _demo_mm_inputs(
                (input_shape[0], input_shape[1], input_shape[2] * 2,
                 input_shape[3] * 2), model.head.num_classes)
            imgs = dynamic_test_inputs.pop('imgs')
            img_list = [img[None, :] for img in imgs]

        # check the numerical value
        # get pytorch output
        pytorch_result = model(img_list, img_metas={}, return_loss=False)[0]

        # get onnx output
        input_all = [node.name for node in onnx_model.graph.input]
        input_initializer = [
            node.name for node in onnx_model.graph.initializer
        ]
        net_feed_input = list(set(input_all) - set(input_initializer))
        assert (len(net_feed_input) == 1)
        sess = rt.InferenceSession(output_file)
        onnx_result = sess.run(
            None, {net_feed_input[0]: img_list[0].detach().numpy()})[0]
        if not np.allclose(pytorch_result, onnx_result):
            raise ValueError(
                'The outputs are different between Pytorch and ONNX')
        print('The outputs are same between Pytorch and ONNX')

def parse_args():
    parser = argparse.ArgumentParser(description='Convert MMCls to ONNX')
    parser.add_argument('config', help='test config file path')
    parser.add_argument('--checkpoint', help='checkpoint file', default=None)
    parser.add_argument('--show', action='store_true', help='show onnx graph')
    parser.add_argument(
        '--verify', action='store_true', help='verify the onnx model')
    parser.add_argument('--output-file', type=str, default=None)
    parser.add_argument('--opset-version', type=int, default=9)
    parser.add_argument(
        '--simplify',
        action='store_true',
        help='Whether to simplify onnx model.')
    parser.add_argument(
        '--shape',
        type=int,
        nargs='+',
        default=[224, 224],
        help='input image size')
    parser.add_argument(
        '--dynamic-export',
        action='store_true',
        help='Whether to export ONNX with dynamic input shape. \
            Defaults to False.')
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()

    if len(args.shape) == 1:
        input_shape = (1, 3, args.shape[0], args.shape[0])
    elif len(args.shape) == 2:
        input_shape = (
            1,
            3,
        ) + tuple(args.shape)
    else:
        raise ValueError('invalid input shape')

    cfg = mmcv.Config.fromfile(args.config)
    cfg.model.pretrained = None

    # build the model and load checkpoint
    classifier = build_classifier(cfg.model)

    if args.checkpoint:
        load_checkpoint(classifier, args.checkpoint, map_location='cpu')

    if args.output_file is None:
        save_dir, f_name = os.path.split(args.checkpoint)
        end_fix = os.path.splitext(f_name)[-1]
        args.output_file = os.path.join(save_dir, f_name.replace(end_fix, '.onnx'))

    # convert model to onnx file
    pytorch2onnx(
        classifier,
        input_shape,
        opset_version=args.opset_version,
        show=args.show,
        dynamic_export=args.dynamic_export,
        output_file=args.output_file,
        do_simplify=args.simplify,
        verify=args.verify)


text:
"""
Functions to calculate acoustic and elastic impedance from well logs
"""
import numpy as np

def ai(vp: float, rho: float):
    """
    Calculates the acoustic impedance of a layer given velocities and densities.

    :param vp: P-wave velocity (m/s)
    :param rho: Density (g/cc)
    :returns:
        z: Acoustic Impedance
    """

    z = vp * rho
    return z

def ei(vp: float, vs: float, rho: float, ang: int):
    """
    Computes the elastic impedance of a layer given velocities, densities and incidence angle.

    :param vp: P-wave velocity (m/s)
    :param vs: S-wave velocity (m/s)
    :param rho: Density (g/cc)
    :param ang: Angle of incidence (deg)
    :returns:
        ei: Elastic impedance

    Reference:
    Connolly, P., 1999, Elastic impedance: The Leading Edge, 18, 438–452.
    """
    theta = np.radians(ang)
    k = (vs / vp) ** 2

    x = 1 + np.tan(theta) ** 2
    y = -8 * k * np.sin(theta) ** 2
    z = 1 - 4 * k * np.sin(theta) ** 2

    ei = (vp ** x) * (vs ** y) * (rho ** z)
    return ei

def norm_ei(vp: float, vs: float, rho: float, vp_sh: float,
            vs_sh: float, rho_sh: float, ang: int):
    """
    Computes the normalized elastic impedance.

    :param vp: P-wave velocity. (m/s)
    :param vs: S-wave velocity. (m/s)
    :param rho: Density. (g/cc)
    :param vp_sh: P-wave velocity in shale (m/s)
    :param vs_sh: S-wave velocity in shale (m/s)
    :param rho_sh: Shale reference density.
    :param ang: Angle of incidence
    :returns:
        nei: Normalized elastic impedance.

    Reference:
        Whitcombe, D, 2002, Elastic impedance normalization, Geophysics, 67 (1), 60–62.
    """

    theta = np.radians(ang)
    k = (vs / vp) ** 2

    x = 1 + np.tan(theta) ** 2
    y = -8 * k * np.sin(theta) ** 2
    z = 1 - 4 * k * np.sin(theta) ** 2

    nei = vp_sh * rho_sh * ((vp / vp_sh) ** x) * ((vs / vs_sh) ** y) * ((rho / rho_sh) ** z)
    return nei

def lame(vp: float, vs: float, rho: float):
    """
    Computes Lamé parameters - lambda_rho and mu_rho.

    :param vp: P-wave velocity (m/s)
    :param vs: S-wave velocity (m/s)
    :param rho: Density (g/cc)
    :returns:
        lambda_rho: Lamé first parameter,
        mu_rho: Lamé second parameter

    Reference:
    Goodway, B., T. Chen, and J. Downton, 1997, Improved AVO fluid detection
    and lithology discrimination using Lamé petrophysical parameters; “λρ”,
    “μρ”, & “λ/μ fluid stack” from P and S inversions: 67th Annual
    International Meeting, SEG, Expanded Abstracts, 183-186.
    """

    vp_imp = vp * rho
    vs_imp = vs * rho
    lambda_rho = vp_imp ** 2 - 2 * vs_imp ** 2
    mu_rho = vs_imp ** 2

    return lambda_rho, mu_rho

max_stars_repo_name: TolaAbiodun/pyavo

max_issues_repo_name: TolaAbiodun/pyavo

max_forks_repo_name: TolaAbiodun/pyavo


text:
import numpy as np

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from scipy import stats

#TODO what does this function mean?
def pred_bbox_by_F(bbox, F, show_flag, img1, img2):
    # Create figure and axes
    if show_flag==1:
        fig1, ax1 = plt.subplots(1)
    # Display the image
    if show_flag==1:
        ax1.imshow(img1)

    pred_bbox = np.zeros((len(bbox),4))
    for n in range(len(bbox)):
        xmin = bbox[n,0]
        ymin = bbox[n,1]
        xmax = bbox[n,2]+bbox[n,0]
        ymax = bbox[n,3]+bbox[n,1]
        w = bbox[n,2]
        h = bbox[n,3]
        if show_flag==1:
            rect = patches.Rectangle((xmin,ymin), w, h, linewidth=1, edgecolor='#FF0000', facecolor='none')
            ax1.add_patch(rect)

    if show_flag==1:
        plt.show()

    # Display the image
    if show_flag==1:
        fig2, ax2 = plt.subplots(1)
        ax2.imshow(img2)

    for n in range(len(bbox)):
        xmin = bbox[n,0]
        ymin = bbox[n,1]
        xmax = bbox[n,2] + bbox[n,0]
        ymax = bbox[n,3] + bbox[n,1]
        w = bbox[n,2]
        h = bbox[n,3]

        temp_A = np.zeros((4,2))
        temp_b = np.zeros((4,1))
        temp_pt = np.zeros((1,3))
        temp_pt[0,:] = np.array([xmin,ymin,1])
        A1 = np.matmul(temp_pt, np.transpose(F))

        temp_A[0,0] = A1[0,0]
        temp_A[0,1] = A1[0,1]
        temp_b[0,0] = -A1[0,2]

        temp_pt[0,:] = np.array([xmax,ymin,1])
        A2 = np.matmul(temp_pt, np.transpose(F))
        temp_A[1,0] = A2[0,0]
        temp_A[1,1] = A2[0,1]
        temp_b[1,0] = -w * A2[0,0] - A2[0,2]

        temp_pt[0,:] = np.array([xmin,ymax,1])
        A3 = np.matmul(temp_pt, np.transpose(F))
        temp_A[2,0] = A3[0,0]
        temp_A[2,1] = A3[0,1]
        temp_b[2,0] = -h * A3[0,1] - A3[0,2]

        temp_pt[0,:] = np.array([xmax,ymax,1])
        A4 = np.matmul(temp_pt, np.transpose(F))
        temp_A[3,0] = A4[0,0]
        temp_A[3,1] = A4[0,1]
        temp_b[3,0] = -w * A4[0,0] - h * A4[0,1] - A4[0,2]

        new_loc = np.matmul(np.linalg.pinv(temp_A),temp_b)
        xmin = new_loc[0,0]
        ymin = new_loc[1,0]
        xmax = new_loc[0,0] + w
        ymax = new_loc[1,0] + h

        pred_bbox[n,0] = xmin
        pred_bbox[n,1] = ymin
        pred_bbox[n,2] = w
        pred_bbox[n,3] = h

        if show_flag==1:
            rect = patches.Rectangle((xmin,ymin),w,h,linewidth=1,edgecolor='#FF0000', facecolor='none')
            ax2.add_patch(rect)

    if show_flag==1:
        plt.show()

    return pred_bbox

def linear_pred(x):
    if len(x)==1:
        return x
    else:
        y = np.array(range(len(x)))
        slope, intercept, _, _, _ = stats.linregress(x, y)
        return slope * len(y) + intercept

def linear_pred_v2(tr_t, tr_y, ts_t):
    """use (tr_t, tr_y) pred ts_y when ts_t in a linear way
    """
    ts_y = np.ones(len(ts_t))
    if len(tr_t)==1:
        ts_y = ts_y*tr_y
    else:
        slope, intercept, _, _, _ = stats.linregress(tr_t, tr_y)
        ts_y = slope * ts_t + intercept
    return ts_y


text:
# -*- coding: utf-8 -*-
# 다중 레이어의 RNN 과 더 효율적인 RNN 학습을 위해 텐서플로우에서 제공하는 Dynamic RNN 을 사용해봅니다.

import tensorflow as tf
import numpy as np

num_arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
num_dic = {n: i for i, n in enumerate(num_arr)}
dic_len = len(num_dic)

# 크기가 다른 순차열을 같이 학습시켜봅니다.
# 123 -> X, 4 -> Y
# 12 -> X, 3 -> Y
seq_data = ['1234', '2345', '3456', '4567', '5678', '6789', '7890']
seq_data2 = ['123', '234', '345', '456', '567', '678', '789', '890']

def one_hot_seq(seq_data):
    x_batch = []
    y_batch = []
    for seq in seq_data:
        x_data = [num_dic[n] for n in seq[:-1]]
        y_data = num_dic[seq[-1]]
        x_batch.append(np.eye(dic_len)[x_data])
        # 이 예제에서 사용할 손실 함수인 sparse_softmax_cross_entropy_with_logits 는
        # one-hot 인코딩을 사용하지 않으므로 index 를 그냥 넘겨주면 됩니다.
        y_batch.append([y_data])

    return x_batch, y_batch

#########
# 옵션 설정
######
n_input = 10
n_classes = 10
n_hidden = 128
# RNN 셀을 다중 레이어로 사용해봅니다.
n_layers = 3

#########
# 신경망 모델 구성
######
# 다양한 길이의 시퀀스를 다루기 위해 time steps 크기를 None 으로 둡니다.
# [batch size, time steps, input size]
X = tf.placeholder(tf.float32, [None, None, n_input])
# 비용함수에 sparse_softmax_cross_entropy_with_logits 을 사용하므로
# 출력값과의 계산을 위한 원본값의 형태는 다음과 같다.
# [batch size, time steps]
Y = tf.placeholder(tf.int32, [None, 1])

W = tf.Variable(tf.random_normal([n_hidden, n_classes]))
b = tf.Variable(tf.random_normal([n_classes]))

# tf.nn.dynamic_rnn 옵션에서 time_major 값을 True 로 설정하면
# 입력값을 적게 변형해도 되므로 다음과 같이 간단하게 사용할 수 있습니다.
# [batch_size, n_steps, n_input] -> Tensor[n_steps, batch_size, n_input]
X_t = tf.transpose(X, [1, 0, 2])

# RNN 셀을 생성합니다.
# 다중 레이어와 과적합 방지를 위한 Dropout 기법을 사용합니다.
cell = tf.nn.rnn_cell.BasicRNNCell(n_hidden)
cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=0.5)
# 다중 레이어 구성을 다음과 같이 아주 간단하게 만들 수 있습니다.
cell = tf.nn.rnn_cell.MultiRNNCell([cell] * n_layers)

# tf.nn.dynamic_rnn 함수를 이용해 순환 신경망을 만듭니다.
outputs, states = tf.nn.dynamic_rnn(cell, X_t, dtype=tf.float32, time_major=True)

# logits 는 one-hot 인코딩을 사용합니다.
logits = tf.matmul(outputs[-1], W) + b
# sparse_softmax_cross_entropy_with_logits 함수의 labels 는
# one-hot 인코딩을 사용하지 않기 때문에, 1차원 배열로 넘겨줍니다. (time step 이 1이기 때문)
# (logits 의 랭크가 2 이므로 [batch_size, n_classes])
labels = tf.reshape(Y, [-1])

cost = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels))

train_op = tf.train.RMSPropOptimizer(learning_rate=0.01).minimize(cost)

#########
# 신경망 모델 학습
######
sess = tf.Session()
sess.run(tf.global_variables_initializer())

x_batch, y_batch = one_hot_seq(seq_data)
x_batch2, y_batch2 = one_hot_seq(seq_data2)

for epoch in range(30):
    _, loss4 = sess.run([train_op, cost], feed_dict={X: x_batch, Y: y_batch})
    _, loss3 = sess.run([train_op, cost], feed_dict={X: x_batch2, Y: y_batch2})

    print 'Epoch:', '%04d' % (epoch + 1), 'cost =', \
        'bucket[4] =', '{:.6f}'.format(loss4), \
        'bucket[3] =', '{:.6f}'.format(loss3)

print '최적화 완료!'

#########
# 결과 확인
######
# 테스트 데이터를 받아 결과를 예측해보는 함수
def prediction(seq_data):
    prediction = tf.cast(tf.argmax(logits, 1), tf.int32)
    prediction_check = tf.equal(prediction, labels)
    accuracy = tf.reduce_mean(tf.cast(prediction_check, tf.float32))

    x_batch_t, y_batch_t = one_hot_seq(seq_data)
    real, predict, accuracy_val = sess.run([labels, prediction, accuracy],
                                           feed_dict={X: x_batch_t, Y: y_batch_t})

    print "\n=== 예측 결과 ==="
    print '순차열:', seq_data
    print '실제값:', [num_arr[i] for i in real]
    print '예측값:', [num_arr[i] for i in predict]
    print '정확도:', accuracy_val

# 학습 데이터에 있던 시퀀스로 테스트
seq_data_test = ['123', '345', '789']
prediction(seq_data_test)

seq_data_test = ['1234', '2345', '7890']
prediction(seq_data_test)

# 학습시키지 않았던 시퀀스를 테스트 해 봅니다.
seq_data_test = ['23', '78', '90']
prediction(seq_data_test)

seq_data_test = ['12345', '34567', '67890']
prediction(seq_data_test)

max_stars_repo_path: reference/tutorial_korean/06 - RNN, ChatBot/02 - Dynamic RNN.py

max_stars_repo_name: KangByungWook/tensorflow

max_issues_repo_name: KangByungWook/tensorflow

max_forks_repo_name: KangByungWook/tensorflow


text:
import numpy as np

def is_nondecreasing_sequence (seq):
    """Not a publicly offered function."""
    return all(s0 <= s1 for (s0,s1) in zip(seq[:-1],seq[1:]))

def is_subsequence_of_nondecreasing_sequence (subseq, seq):
    """Not a publicly offered function."""
    assert is_nondecreasing_sequence(seq), 'seq is not nondecreasing'

    subseq_index = seq_index = 0
    while True:
        # If we've matched all elements of subseq, then we have a subsequence.
        if subseq_index == len(subseq):
            return True
        # This condition is guaranteed to occur at some point, guaranteeing that the function will return.
        elif seq_index == len(seq):
            return False

        # If the values at the cursors match, then so far so good, increment both cursors.
        if subseq[subseq_index] == seq[seq_index]:
            subseq_index += 1
        # Increment the sequence read cursor regardless.
        seq_index += 1

def compose_index_maps (*index_vv):
    """
    A list is a way to represent a map.  Assume list L has N elements.  Then L can be considered to
    map the elements of the sequence (0,1,...,N-1) to the elements of the sequence (L[0],L[1],...,L[N-1])
    respectively.

    This function composes a sequence of lists-as-functions, returning a list which represents
    the composition.
    """

    for domain_index_v,codomain_index_v in zip(reversed(index_vv[1:]),reversed(index_vv[:-1])):
        assert all(0 <= domain_index < len(codomain_index_v) for domain_index in domain_index_v), 'domain error; elements of index list {0} must map into the index range [0,...,{1}) (right endpoint excluded), which index the list {2}'.format(domain_index_v, len(codomain_index_v), codomain_index_v)

    current = np.arange(len(index_vv[-1]))
    for index_v in reversed(index_vv):
        current[:] = [index_v[i] for i in current]
    return current

def index_map_inverse (index_map_v, index_count):
    """
    Let index_map_inverse_v denote the return value of this function.  It will satisfy:

        np.all(compose_index_maps(index_map_inverse_v, index_map_v) == np.arange(len(index_map_v)))
    """
    index_map_list_v = list(index_map_v) if type(index_map_v) is not list else index_map_v
    assert len(frozenset(index_map_v)) == len(index_map_v), 'index_map_v (which is {0}) must have nonrepeating elements (i.e. it must be injective as a map)'.format(index_map_v)
    assert all(0 <= i < index_count for i in index_map_v), 'index_map_v (which is {0}) must have elements in the range [0,{1}) (right endpoint excluded)'.format(index_map_v, index_count)
    return np.array([index_map_list_v.index(i) if i in index_map_v else 0 for i in range(index_count)])

max_stars_repo_name: vdods/vorpy

max_issues_repo_name: vdods/vorpy

max_forks_repo_name: vdods/vorpy


text:
# --------------------------------------------------------
# Licensed under The MIT License [see LICENSE for details]
# --------------------------------------------------------

import pybullet as p
import numpy as np
import IPython

try:
    from OMG.omg.config import cfg as planner_cfg
    from OMG.omg.core import PlanningScene
except:
    pass
from core.utils import *

class EnvPlanner():
    """
    Class for franka panda environment with YCB objects.
    """

    def __init__(self, env=None):
        self.env = env
        self._planner_setup = False
        self.env.planner_reach_grasps = []
        self.env.planner_goal_set = []
        self.planner_change_goal = False

    def plan_to_conf(self, step, start_conf, goal_conf, vis=False):
        """
        Plan to a fix configuration
        """
        planner_cfg.timesteps = step # 20
        planner_cfg.get_global_param(step)
        self.planner_scene.traj.start = start_conf
        dummy_goal_set = planner_cfg.goal_set_proj
        setattr(planner_cfg, 'goal_set_proj', False)
        planner_cfg.get_global_param()
        self.planner_scene.traj.end = goal_conf
        self.planner_scene.update_planner()
        info = self.planner_scene.step()
        setattr(planner_cfg, 'goal_set_proj', dummy_goal_set)
        planner_cfg.get_global_param()
        plan = self.planner_scene.planner.history_trajectories[-1]
        return plan

    def setup_expert_scene(self):
        """
        Load all meshes once and then update pose
        """
        # parameters
        print('set up expert scene ...')
        for key, val in self.env._omg_config.items():
            setattr(planner_cfg, key, val)

        planner_cfg.get_global_param(planner_cfg.timesteps)
        planner_cfg.get_global_path()

        # load obstacles
        self.planner_scene = PlanningScene(planner_cfg)
        self.planner_scene.traj.start = np.array(self.env._panda.getJointStates()[0])
        self.planner_scene.env.clear()

        obj_names, obj_poses = self.env.get_env_info(self.env._cur_scene_file)
        object_lists = [name.split('/')[-1].strip() for name in obj_names]
        object_poses = [pack_pose(pose) for pose in obj_poses]

        for i, name in enumerate(self.env.obj_path[:-2]):
            name = name.split('/')[-2]
            trans, orn = self.env.placed_object_poses[i]
            self.planner_scene.env.add_object(name, trans, tf_quat(orn), compute_grasp=True)

        self.planner_scene.env.add_plane(np.array([0.05, 0, -0.17]), np.array([1,0,0,0])) # never moved
        self.planner_scene.env.add_table(np.array([0.55, 0, -0.17]), np.array([0.707, 0.707, 0., 0]))
        self.planner_scene.env.combine_sdfs()
        self._planner_setup = True

    def expert_plan_grasp(self, step=-1, check_scene=False, checked=False, fix_goal=False):
        """
        Generate expert to grasp the target object
        """
        joint_pos = self.env._panda.getJointStates()[0]
        self.planner_scene.traj.start = np.array(joint_pos)
        self.planner_scene.env.set_target(self.env.obj_path[self.env.target_idx].split('/')[-2]) #scene.env.names[0])
        info = []
        if step > 0: # continued plan
            self.get_planning_scene_target().compute_grasp = False
            planner_cfg.timesteps = step # 20
            if fix_goal:
                planner_cfg.goal_idx = self.planner_scene.traj.goal_idx
                planner_cfg.ol_alg = "Baseline"
            planner_cfg.get_global_param(planner_cfg.timesteps)
            self.planner_scene.reset(lazy=True)
            self.prev_planner_goal_idx = self.planner_scene.traj.goal_idx

            if  not check_scene:
                info = self.planner_scene.step()

            planner_cfg.timesteps = self.env._expert_step # set back
            planner_cfg.get_global_param(planner_cfg.timesteps)
            if  fix_goal: # reset
                planner_cfg.goal_idx = -1
                planner_cfg.ol_alg = "MD"
            self.planner_change_goal = self.prev_planner_goal_idx != self.planner_scene.traj.goal_idx
            self.prev_planner_goal_idx = self.planner_scene.traj.goal_idx

        else:
            if checked:
                self.get_planning_scene_target().compute_grasp = False

            self.planner_scene.reset(lazy=True)
            if not check_scene:
                info = self.planner_scene.step()
            self.planner_change_goal = False
            self.prev_planner_goal_idx = -1

        return info

    def expert_plan(self, step=-1, return_success=False, check_scene=False, checked=False, fix_goal=True,
                    joints=None, ef_pose=None, pts=None, vis=False):
        """
        Run OMG planner for the current scene
        """
        if not self._planner_setup:
            self.setup_expert_scene()

        obj_names, obj_poses = self.env.get_env_info(self.env._cur_scene_file)
        object_lists = [name.split('/')[-1].strip() for name in obj_names]
        object_poses = [pack_pose(pose) for pose in obj_poses]
        exists_ids = []
        placed_poses = []
        placed_obj_names = []
        plan = []

        if self.env.target_idx == -1 or self.env.target_name == 'noexists':
            if not return_success:
                return [], np.zeros(0)
            return [], np.zeros(0), False

        for i, name in enumerate(object_lists[:-2]):  # buffer
            self.planner_scene.env.update_pose(name, object_poses[i])
            idx = self.env.obj_path[:-2].index(os.path.join(self.env.root_dir, 'data/objects/' + name + '/'))
            exists_ids.append(idx)
            trans, orn = self.env.placed_object_poses[idx]
            placed_poses.append(np.hstack([trans, ros_quat(orn)]))
            placed_obj_names.append(name)

        planner_cfg.disable_collision_set = [name.split('/')[-2] for idx, name in enumerate(self.env.obj_path[:-2])
                                             if idx not in exists_ids]

        info = self.expert_plan_grasp(step, check_scene, checked, fix_goal)

        if planner_cfg.vis: self.planner_scene.fast_debug_vis(collision_pt=False)

        for i, name in enumerate(placed_obj_names): # reset
            self.planner_scene.env.update_pose(name, placed_poses[i])

        if check_scene:
            return len(self.planner_scene.traj.goal_set) >= 15

        if  hasattr(self.planner_scene, 'planner') and len(self.planner_scene.planner.history_trajectories) >= 1: #
            plan = self.planner_scene.planner.history_trajectories[-1]
            if not hasattr(self.env, 'robot'):
                self.env.robot = require_robot()
            ef_pose = self.env.get_base_pose().dot(self.env.robot.forward_kinematics_parallel(
                                wrap_value(plan[-1])[None], offset=False)[0][-3]) # world coordinate

            pos, orn = p.getBasePositionAndOrientation(self.env._objectUids[self.env.target_idx]) # to target
            obj_pose = list(pos) + [orn[3], orn[0], orn[1], orn[2]]
            self.env.cur_goal = se3_inverse(unpack_pose(obj_pose)).dot(ef_pose)

        success = info[-1]['terminate'] if len(info) > 1 else False
        failure = info[-1]['failure_terminate'] if len(info) > 1 else True
        self.env.planner_reach_grasps = self.get_planning_scene_target().reach_grasps
        self.env.planner_goal_set = self.planner_scene.traj.goal_set

        if not return_success:
            return plan, failure
        return plan, failure, success

    def set_planner_scene_file(self, scene_path, scene_file):
        planner_cfg.traj_init = "scene"
        planner_cfg.scene_path = scene_path
        planner_cfg.scene_file = scene_file

    def get_planning_scene_target(self):
        return self.planner_scene.env.objects[self.planner_scene.env.target_idx]

    def _get_init_info(self):
        """
        Get environment information
        """
        return [self.obj_names, self.obj_poses, self.placed_object_target_idx,
                np.array(self._panda.getJointStates()[0]),
                self.planner_scene.traj.goal_set,
                self.get_planning_scene_target().reach_grasps]

    def goal_num_full(self):
        """
        Check if the scene has enough goals
        """
        return not self._planner_setup or len(self.planner_scene.traj.goal_set) >= 6

max_stars_repo_name: liruiw/HCG

max_issues_repo_name: liruiw/HCG

max_forks_repo_name: liruiw/HCG