示例#1
0
 def get_segment_cop_matrix(segment_df, sheet=None):
     segment = segment_df.iloc[0, 0]
     if segment in ['trunk', 'pelvis']:
         axis_1_range = Presenter.__array_to_range(segment_df['x_offset'].as_matrix())
         axis_2_range = Presenter.__array_to_range(segment_df['z_offset'].as_matrix())
     else:
         axis_1_range = Presenter.__array_to_range(segment_df['theta_offset'].as_matrix())
         axis_2_range = Presenter.__array_to_range(segment_df['z_offset'].as_matrix())
     axis_1_len = axis_1_range.__len__()
     axis_2_len = axis_2_range.__len__()
     total_matrix_number = SUB_NUM * SPEEDS.__len__()
     cop_matrix = np.zeros([axis_2_len, axis_1_len, total_matrix_number])
     i_matrix = 0
     for i_sub in range(SUB_NUM):
         for speed in SPEEDS:
             trial_df = segment_df[(segment_df['subject_id'] == i_sub) & (segment_df['speed'] == float(speed))]
             center_df = trial_df[(trial_df['x_offset'] == 0) & (trial_df['y_offset'] == 0) &
                                  (trial_df['z_offset'] == 0) & (trial_df['theta_offset'] == 0)]
             cop_center_score = np.mean(center_df[DatabaseInfo.get_cop_column_names()].as_matrix())
             cop_scores = np.mean(trial_df[DatabaseInfo.get_cop_column_names()].as_matrix(), axis=1)
             cop_matrix[:, :, i_matrix] = Presenter.__get_decrease_matrix(cop_scores, cop_center_score, axis_1_range,
                                                                          axis_2_range)
             i_matrix += 1
     cop_mean_matrix = np.mean(cop_matrix, axis=2)
     cop_std_matrix = np.std(cop_matrix, axis=2)
     cop_title = segment + ', cop, mean and one standard deviation of R2 decrease'
     if sheet:
         Presenter.__store_matrix(cop_mean_matrix, cop_std_matrix, cop_title, axis_1_range, axis_2_range, sheet,
                                  segment)
     return cop_matrix, axis_1_range, axis_2_range
示例#2
0
    def __init__(self, users, dungeon):

        self.users = users
        self.current_user = None
        self.current_hero = None
        self.input = None
        self.databaseInfo = DatabaseInfo()

        self.dungeon = dungeon

        self.login_command = None
        self.hero_select_command = None
        self.instruction = None
        self.direction = None
示例#3
0
    def __init__(self, subject_data, segment_name):
        self.__segment_name = segment_name
        self.__center_marker_names = DatabaseInfo.get_center_marker_names(
            segment_name)
        self.__segment_cali_data = {}
        self.__segment_walking_1_data = {}
        self.__segment_walking_2_data = {}
        self.__marker_cali_matrix = {}
        self.__center_point_mean = {}
        for speed in SPEEDS:
            self.__segment_cali_data[
                speed] = subject_data.get_cali_segment_data(
                    speed, segment_name)
            self.__segment_walking_1_data[
                speed] = subject_data.get_walking_1_segment_data(
                    speed, segment_name)
            self.__segment_walking_2_data[
                speed] = subject_data.get_walking_2_segment_data(
                    speed, segment_name)

            marker_pos = self.__segment_cali_data[speed].as_matrix()
            marker_pos_mean = np.mean(marker_pos, axis=0)
            segment_marker_num = int(marker_pos.shape[1] / 3)
            marker_matrix = marker_pos_mean.reshape([segment_marker_num, 3])
            self.__marker_cali_matrix[speed] = marker_matrix

            center_point = self.__segment_cali_data[speed][
                self.__center_marker_names].as_matrix()
            center_point_mean = np.mean(center_point, axis=0)
            center_marker_num = int(center_point_mean.shape[0] / 3)
            if center_marker_num != 1:
                center_point_mean = (center_point_mean[0:3] +
                                     center_point_mean[3:6]) / 2
            self.__center_point_mean[speed] = center_point_mean
示例#4
0
    def show_segment_result(segment_df, date):
        segment = segment_df.iloc[0, 0]
        if segment in ['trunk', 'pelvis']:
            axis_1_range = Presenter.__array_to_range(segment_df['x_offset'].as_matrix())
            axis_2_range = Presenter.__array_to_range(segment_df['z_offset'].as_matrix())
            axis_1_label, axis_2_label = 'x offset to center', 'z offset to center'

        else:
            axis_1_range = Presenter.__array_to_range(segment_df['theta_offset'].as_matrix())
            axis_2_range = Presenter.__array_to_range(segment_df['z_offset'].as_matrix())
            axis_1_label, axis_2_label = 'theta offset to center', 'z offset to center'

        axis_1_len = axis_1_range.__len__()
        axis_2_len = axis_2_range.__len__()
        average_force_im = np.zeros([axis_2_len, axis_1_len])
        average_cop_im = np.zeros([axis_2_len, axis_1_len])
        for i_sub in range(SUB_NUM):
            for speed in SPEEDS:
                trial_df = segment_df[(segment_df['subject_id'] == i_sub) & (segment_df['speed'] == float(speed))]

                force_scores = np.mean(trial_df[DatabaseInfo.get_force_column_names()].as_matrix(), axis=1)
                sub_force_im = Presenter.__get_score_im(force_scores, axis_1_range, axis_2_range)
                average_force_im += sub_force_im

                cop_scores = np.mean(trial_df[DatabaseInfo.get_cop_column_names()].as_matrix(), axis=1)
                sub_cop_im = Presenter.__get_score_im(cop_scores, axis_1_range, axis_2_range)
                average_cop_im += sub_cop_im
        total_number = SUB_NUM * SPEEDS.__len__()
        average_force_im = average_force_im / total_number
        average_force_title = segment + ', average force'
        Presenter.__show_score_im(average_force_im, axis_1_range, axis_2_range, average_force_title, axis_1_label,
                                  axis_2_label, date)

        average_cop_im = average_cop_im / total_number
        average_cop_title = segment + ', average COP'
        Presenter.__show_score_im(average_cop_im, axis_1_range, axis_2_range, average_cop_title, axis_1_label,
                                  axis_2_label, date)
示例#5
0
 def check_virtual_marker(virtual_marker, walking_df, segment_name):
     center_marker_name = DatabaseInfo.get_center_marker_names(segment_name)
     center_marker_num = int(center_marker_name.__len__() / 3)
     # check the virtual marker data
     real_marker_pos = walking_df[center_marker_name].as_matrix()
     if center_marker_num != 1:
         real_marker_pos = (real_marker_pos[:, 0:3] + real_marker_pos[:, 3:6])/2
     error = virtual_marker - real_marker_pos
     error_combined = np.linalg.norm(error, axis=1)
     plt.figure()
     plt.plot(error_combined)
     plt.title('difference between real marker and virtual marker')
     plt.xlabel('frame')
     plt.ylabel('mm')
     plt.show()
示例#6
0
class Input:
    def __init__(self, users, dungeon):

        self.users = users
        self.current_user = None
        self.current_hero = None
        self.input = None
        self.databaseInfo = DatabaseInfo()

        self.dungeon = dungeon

        self.login_command = None
        self.hero_select_command = None
        self.instruction = None
        self.direction = None



    ''' #==============================================================================
    
                                    Main Input Loop
    
    ''' #==============================================================================

    # Handle all the Inputs from each player
    def handle_input(self):

        for user in self.users:

            if user.connected:

                self.current_user = user
                self.current_hero = user.current_hero

                # Check to see if an input has actually been sent
                if user.input_queue.qsize() > 0:

                    # Login State instructions
                    if user.state == user.LoginPanel:

                        # Splits up the delimited input
                        self.input = user.input_queue.get()
                        self.input = self.input.split('#')
                        self.login_command = self.input[0]
                        print(self.login_command)

                        if self.login_command == "VerifySalt":
                            self.verify_salt(self.current_user, self.input)
                        elif self.login_command == "CheckLogin":
                            self.login_method(self.current_user, self.input)
                        elif self.login_command == "CreateAccount":
                            self.new_account(self.current_user, self.input)

                        else:
                            user.add_to_output_queue("ERROR - login error")

                    # Player selection game state instructions
                    elif user.state == user.HeroSelectScreen:
                        # splits up the input and makes it all lower case
                        self.input = user.input_queue.get()
                        self.input = self.input.lower()
                        self.input = self.input.split(" ", 1)
                        self.hero_select_command = self.input[0]

                        if self.hero_select_command == "name":

                            self.new_hero(self.current_user, self.input)

                        elif self.hero_select_command == "pick" or \
                            self.hero_select_command == "select" or \
                            self.hero_select_command == "choose" :

                            self.choose_hero(self.current_user, self.input)

                        else:
                            user.add_to_output_queue(
                                "ERROR - hero select error")

                    # Actual game state
                    elif user.state == user.InGame:
                        # Splits up the input and makes it all lower case
                        self.input = user.input_queue.get()
                        self.input = self.input.lower()
                        self.input = self.input.split(" ", 1)
                        self.instruction = self.input[0]

                        if self.instruction == "move" or self.instruction == "go":

                            self.move_hero(self.current_user, self.input)

                        elif self.instruction == "look" or self.instruction == "survey":

                            self.look_at(self.current_user)

                        elif self.instruction == "map":

                            self.use_map(self.current_user)

                        elif self.instruction == "help":

                            self.help_instruction(self.current_user)

                        elif self.instruction == "say" or self.instruction == "/s":

                            self.room_speech(self.input)

                        elif self.instruction == "stone" or self.instruction == "/y":

                            self.use_stone(self.input)

                        else:

                            user.add_to_output_queue(
                                "ERROR - instruction error")

            else:

                self.users.remove(user)
                self.output_to_all(user.username + " has left the game.")
                print(user.username + " has disconnected.")


    ''' #==============================================================================
    
                                    Functions for messaging multiple clients 
    
    ''' #==============================================================================

    # Send a message to every player in the dungeon
    def output_to_all(self, message):

        for user in self.users:

            if user.state == user.InGame:

                user.add_to_output_queue(message)

    # Send a message to every player in the same room
    def output_to_room(self, message, heroes_in_room):

        for hero in heroes_in_room:

            hero.user.add_to_output_queue(message)


    ''' #==============================================================================
    
                                    Login functions
    
    ''' #==============================================================================

    # Verify the users salt
    def verify_salt(self, user, input):

        if self.databaseInfo.check_account(
                input[1]):  # Check if username is in database
            # Send the salt back to client is username exists
            user.add_to_output_queue(
                "SALT#" + self.databaseInfo.fetch_salt_in_database(input[1]),
                True)

        else:

            user.add_to_output_queue("Failed to login!\n"
                                     "User Account: " + input[1] +
                                     " does not exist.")

    # Login user
    def login_method(self, user, input):

        logged_in = False

        for u in self.users:

            if u.username == input[1]:

                logged_in = True

        # Check username and password
        if self.databaseInfo.check_login_details(input[1], input[2]):

            if not logged_in:

                user.add_to_output_queue("Login successful.")

                # Send command to hide the login panel
                user.add_to_output_queue("LOGIN_SUCCESS", True)

                user.username = input[1]

                # This changes the user state to player selection
                self.start_hero_selection(user)

            else:

                user.add_to_output_queue(
                    "Failed to login!\n"
                    "There is already a user logged in with this account.")
        else:

            user.add_to_output_queue("Failed to login!\n"
                                     "Incorrect username or password.")

    # Create a new account
    def new_account(self, hero, input):

        hero.add_to_output_queue("Creating account...")

        # Create an account using the username, password, and salt
        if self.databaseInfo.create_account(input[1], input[2], input[3]):

            hero.add_to_output_queue("Account created.")

        else:

            hero.add_to_output_queue("Failed to create an account.\n"
                                     "This user account may already exist.")

    ''' #==============================================================================
    
                                    Hero selection functions
     
    ''' #==============================================================================

    # Start hero selection
    def start_hero_selection(self, user):
        # Sets the user state to player select
        user.state = user.HeroSelectScreen
        user.add_to_output_queue(
            "\nTo create a new hero: name <HeroName>\n"
            "To choose an existing hero: select <HeroName>\n")

        self.show_heroes(user)

    # Get the heroes the user currently has in the database
    def show_heroes(self, user):

        user.add_to_output_queue("Current heroes:")
        hero_list = self.databaseInfo.list_of_heroes(user.username)

        if len(hero_list) < 1:

            user.add_to_output_queue(
                "\nCreate a new hero with: name <HeroName>")

        else:

            for hero in hero_list:
                # get the hero name and hero location
                user.add_to_output_queue("- " + str(hero[2]) + " in " +
                                         str(hero[3]))

    def choose_hero(self, user, input):

        hero = user.current_hero

        chosen_hero = self.databaseInfo.choose_hero(user.username, input[1])

        if chosen_hero:

            user.add_to_output_queue(str(chosen_hero[2]) + " is selected\n")

            hero.hero_name = str(chosen_hero[2])

            # Adds the hero name to the window UI
            user.add_to_output_queue(
                "UPDATE_HERO_NAME#" + user.current_hero.hero_name, True)

            user.add_to_output_queue("\nJoining game...")

            current_room_in_database = self.databaseInfo.get_current_room(
                user.current_hero.hero_name)

            # add user to the dungeon
            user.current_hero.current_room = user.current_hero.current_dungeon.rooms_dict[
                current_room_in_database]
            user.current_hero.current_room.heroes.append(user.current_hero)

            # Updates current room name in the QT window
            user.add_to_output_queue(
                "UPDATE_ROOM#" + user.current_hero.current_room.name, True)

            # notifies everyone in game that a new player has joined
            self.output_to_all(user.current_hero.hero_name +
                               " has joined the game!")

            # Go to next input state
            user.state = user.InGame

        elif chosen_hero is None:

            user.add_to_output_queue("This hero doesn't exist")

        else:

            user.add_to_output_queue("ERROR! - Hero selection error")

    def new_hero(self, user, input):

        user.add_to_output_queue("\nCreating new hero...")

        hero_name = input[1]

        if self.databaseInfo.create_hero(user.username, hero_name,
                                         "Entry Gate"):

            user.add_to_output_queue(
                "New hero " + hero_name + " has been created!\n"
                "To choose an existing hero: select <HeroName>")

        else:

            user.add_to_output_queue("A hero with that name already exists!")

        self.show_heroes(user)


    ''' #==============================================================================
    
                                    General game functions
    
    ''' #==============================================================================

    # Help command displays all the commands the player can write
    def help_instruction(self, hero):

        hero.add_to_output_queue(
            "\n"
            "------------------------------------------\n"
            "List Of Commands: \n"
            "Go <direction> : Can move north, south, east or west\n"
            "Look           : Survey area\n"
            "Say <speech>   : Speak to other heroes in the room\n"
            "Stone <speech> : Use the stone of farspeech to talk to everyone in the dungeon\n"
            "Map            : Display map \n"
            "------------------------------------------\n")

    # Open/Close the map
    def use_map(self, hero):

        hero.add_to_output_queue("OPEN_MAP", True)

    # Describes location
    def look_at(self, hero):

        hero.add_to_output_queue(self.current_hero.current_room.description)

    # Send a message to every player in the same room
    def room_speech(self, input):

        for hero in self.current_hero.current_room.heroes:

            hero.user.add_to_output_queue(self.current_hero.hero_name +
                                          " said: " + input[1])

    # Send a message to every player in dungeon
    def use_stone(self, input):

        for hero in self.users:

            hero.add_to_output_queue(self.current_hero.hero_name +
                                     " used the stone and said: " + input[1])

    # Command that allows the player to move around the different rooms
    def move_hero(self, user, input):

        self.direction = input[1]

        if self.direction in self.current_hero.current_room.connected_rooms:

            old_room = self.current_hero.current_room
            new_room = self.current_hero.current_room.connected_rooms[
                self.direction]

            # removes the player from the current room
            self.current_hero.current_room.heroes.remove(self.current_hero)

            # announce that a player has left the current room
            self.output_to_room(
                self.current_hero.hero_name + " has left the room",
                old_room.heroes)

            # Move the player to the new Room
            self.current_hero.change_room(self.dungeon.rooms_dict[new_room])
            self.databaseInfo.update_hero_room(
                self.current_hero.hero_name,
                self.current_hero.current_room.name)
            user.add_to_output_queue("You have moved to: " +
                                     self.current_hero.current_room.name)

            # announce that a player has entered a new room
            self.output_to_room(
                self.current_hero.hero_name + " entered the room",
                self.current_hero.current_room.heroes)

            # adds the player to the new room
            self.current_hero.current_room.heroes.append(self.current_hero)

            # update room textbox on client
            user.add_to_output_queue(
                "UPDATE_ROOM#" + user.current_hero.current_room.name, True)

        else:

            user.add_to_output_queue("Nothing lies that way...")
示例#7
0
import h5py
from DatabaseInfo import DatabaseInfo
import utils.DataPreprocessing as datapre
import utils.Training_Test_Split as ttsplit
import cnn_main

# parse parameters
# training or prediction
lTrain = True
lSave = False # save intermediate test, training sets
with open('config' + os.sep + 'param.yml', 'r') as ymlfile:
    cfg = yaml.safe_load(ymlfile)

# initiate info objects
# default database: MRPhysics with ['newProtocol','dicom_sorted']
dbinfo = DatabaseInfo(cfg['MRdatabase'],cfg['subdirs'])


# load/create input data
patchSize = cfg['patchSize']
if cfg['sSplitting'] == 'normal':
    sFSname = 'normal'
elif cfg['sSplitting'] == 'crossvalidation_data':
    sFSname = 'crossVal_data'
elif cfg['sSplitting'] == 'crossvalidation_patient':
    sFSname = 'crossVal'

sOutsubdir = cfg['subdirs'][2] #sOutsubdir: 'testout'
#sOutPath CNN/Headcross/4040/testout建立新的文件夹testout
sOutPath = cfg['selectedDatabase']['pathout'] + os.sep + ''.join(map(str,patchSize)).replace(" ", "") + os.sep + sOutsubdir # + str(ind_split) + '_' + str(patchSize[0]) + str(patchSize[1]) + '.h5'
#.../4040/crossVal4040.h5
    'l_foot_acc_y',
    'l_foot_acc_z',
    'r_foot_acc_x',
    'r_foot_acc_y',
    'r_foot_acc_z',
    # 'trunk_gyr_x', 'trunk_gyr_y', 'trunk_gyr_z',
    # 'pelvis_gyr_x', 'pelvis_gyr_y', 'pelvis_gyr_z',
    # 'l_thigh_gyr_x', 'l_thigh_gyr_y', 'l_thigh_gyr_z',
    # 'r_thigh_gyr_x', 'r_thigh_gyr_y', 'r_thigh_gyr_z',
    # 'l_shank_gyr_x', 'l_shank_gyr_y', 'l_shank_gyr_z',
    # 'r_shank_gyr_x', 'r_shank_gyr_y', 'r_shank_gyr_z',
    # 'l_foot_gyr_x', 'l_foot_gyr_y', 'l_foot_gyr_z',
    # 'r_foot_gyr_x', 'r_foot_gyr_y', 'r_foot_gyr_z',
]
# moved_segments = MOVED_SEGMENT_NAMES
my_database_info = DatabaseInfo()
total_score_df = EvaluationUni.initialize_result_df(total_result_columns)

# evaluators = ensemble.RandomForestRegressor(n_jobs=4)
# evaluators = ensemble.RandomForestRegressor(n_estimators=200, random_state=0, n_jobs=4)
# evaluators = SVR(C=200, epsilon=0.02, gamma=0.1, max_iter=4000000)
model = ensemble.GradientBoostingRegressor(learning_rate=0.1,
                                           min_impurity_decrease=0.001,
                                           min_samples_split=6,
                                           n_estimators=500)
x_scalar, y_scalar = preprocessing.StandardScaler(
), preprocessing.StandardScaler()

# # for test
# SUB_NUM = 2
示例#9
0
 def get_walking_2_segment_data(self, speed, segment):
     segment_names = DatabaseInfo.get_segment_marker_names(segment)
     return self.__walking_2_data[speed][segment_names]
示例#10
0
# initalize stuff
sTypeVis = 'weights'  # deep: lukas implementation, else: weights of first layer
lShow = False

# parse parameters

folderPath = os.path.dirname(os.path.split(os.path.abspath(__file__))[0])
cfgPath = os.path.dirname(os.path.dirname(__file__))
cfgPath = os.path.join(cfgPath, 'config', 'param.yml')

with open(cfgPath, 'r') as ymlfile:
    cfg = yaml.safe_load(ymlfile)

# default database: MRPhysics with ['newProtocol','dicom_sorted']
dbinfo = DatabaseInfo(cfg['MRdatabase'], cfg['subdirs'], folderPath)

patchSize = cfg['patchSize']
batchSize = cfg['batchSize'][0]

sOutsubdir = cfg['subdirs'][2]
sOutPath = cfg['selectedDatabase']['pathout'] + os.sep + ''.join(
    map(str, patchSize)).replace(" ", "") + os.sep + sOutsubdir
sNetworktype = cfg['network'].split("_")
model_name = cfg['selectedDatabase']['bestmodel'][sNetworktype[2]]

model_path = sOutPath + model_name + '_model.h5'
model = load_model(model_path)
# model = load_model('/no_backup/d1240/CNNArt/results/4040/testout4040_lr_0.001_bs_128_model.h5')

plot_model(model, to_file='model.png', show_layer_names=True, rankdir='TB')
示例#11
0
config.gpu_options.allow_growth = True
tf.keras.backend.set_session(tf.Session(config=config))

import tensorflow.keras.optimizers as optimizers

from tensorflow.keras.callbacks import (
    CSVLogger,
    ModelCheckpoint
)

if __name__ == '__main__':

    with open('config' + os.sep + 'param_IQA.yml', 'r') as ymlfile:
        cfg = yaml.safe_load(ymlfile)

    dbinfo = DatabaseInfo(cfg['MRdatabase'], cfg['subdirs'], cfg['sDatabaseRootPath'])

    # check if there are tfrecord files existing, if not, creating corresponding one
    original_dataset_path = os.path.join(cfg['sDatabaseRootPath'], cfg['MRdatabase'])
    print('original database path: ', original_dataset_path)
    for ipat, pat in enumerate(dbinfo.lPats):
        # the expected tfrecord saved format: med_data/NAKO/NAKO_IQA/Q1/....tfrecord

        if not os.path.exists(os.path.join(cfg['tfrecordsPath'], pat, dbinfo.sSubDirs[0])):
            if (pat == 'Results'):
                continue

            # tfrecords not yet created
            for iseq, seq in enumerate(dbinfo.lImgData):
                # create tfrecords
示例#12
0
import threading
import dungeon
from DatabaseInfo import DatabaseInfo
from clientInfo import ClientInfo
from input import Input

running = True

testing_server = True  # Change when setting up server online

heroes = []
clients = []
clients_lock = threading.Lock()

# SQL database
database_information = DatabaseInfo()

# Current dungeon
my_dungeon = dungeon.DungeonRooms
my_dungeon.setup_dungeon(dungeon.DungeonRooms, "Refuge")

# Main input
input = Input(clients, my_dungeon)



''' #==============================================================================

                                Socket Accept Thread

''' #==============================================================================