Archived
175 lines
6.0 KiB
Python
175 lines
6.0 KiB
Python
# --------------------------------------------------------------------------
|
|
# Authors:
|
|
# ------ MATlAB ------
|
|
# 2010-03-16: Etienne Gaudrain <etienne.gaudrain@mrc-cbu.cam.ac.uk>
|
|
# 2013-02-24: Etienne Gaudrain <e.p.c.gaudrain@rug.nl>
|
|
# 2017-03-29: Etienne Gaudrain <etienne.gaudrain@cnrs.fr>
|
|
# ------ Python ------
|
|
# 2020-01-28: Luke Meyer < l.meyer.5@student.rug.nl >
|
|
|
|
# Copyright MRC-CBSU(UK), CNRS(FR), UMCG(NL) and the author(s).
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY
|
|
# without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see < http: // www.gnu.org/licenses/>.
|
|
# --------------------------------------------------------------------------
|
|
import sys
|
|
import os
|
|
import expe_robot
|
|
import ctypes
|
|
import time
|
|
import logging
|
|
import datetime
|
|
import expe_build_conditions
|
|
import expe_options
|
|
import tools.settings as settings
|
|
from scipy.io import savemat
|
|
|
|
from naoqi import ALBroker
|
|
from tools.exit_condition import ExitExperimentModule
|
|
|
|
VERSION = 20202801
|
|
|
|
expe_exit = None
|
|
|
|
|
|
def setup_logger(name, log_file, level=logging.INFO):
|
|
"""To setup as many loggers as you want"""
|
|
formatter = logging.Formatter('%(levelname)s:%(message)s')
|
|
handler = logging.FileHandler(log_file)
|
|
handler.setFormatter(formatter)
|
|
|
|
logger = logging.getLogger(name)
|
|
logger.setLevel(level)
|
|
logger.addHandler(handler)
|
|
logger.propagate = False
|
|
|
|
return logger
|
|
|
|
|
|
def main(options, subject, phase, lang, p_exists, ip='127.0.0.1', port=9559):
|
|
'''
|
|
Runs the experiment.
|
|
|
|
SUBJECT is a string identifying the subject.
|
|
PHASE is either 'training' or 'test'
|
|
IP is the ipaddress of Sam when connected to a relevant network
|
|
PORT is the port for connection to Sam
|
|
|
|
'''
|
|
options["subject_name"] = subject
|
|
options["language"] = lang
|
|
|
|
options = expe_options.expe_options(options)
|
|
|
|
# Adds necessary paths. So far not in use.
|
|
added_path = []
|
|
added_path.append(options["path"]["audio"])
|
|
added_path.append(options["path"]["vocoder"])
|
|
|
|
# Create the results directory in the current directory if it does not already exist
|
|
if not os.path.exists(options["result_path"]):
|
|
os.mkdir(options["result_path"])
|
|
|
|
# Create the logs directory in the current directory if it does not already exist
|
|
if not os.path.exists(options["logs_path"]):
|
|
os.mkdir(options["logs_path"])
|
|
|
|
# Create the subject's file path
|
|
res_filename = os.path.join(
|
|
options["result_path"],
|
|
"{}{}_{}_robot.mat".format(options["result_prefix"], lang.lower(),
|
|
subject))
|
|
options["res_filename"] = res_filename
|
|
|
|
res_tracker = os.path.join(options["result_path"],
|
|
'{}_tracker.mat'.format(subject))
|
|
options["res_tracker"] = res_tracker
|
|
if not os.path.exists(res_tracker):
|
|
track = {}
|
|
track["experiment_begin"] = 1
|
|
|
|
track["experiments"] = {}
|
|
track["experiments"]["fishy"] = 0
|
|
track["experiments"]["gender"] = 0
|
|
track["experiments"]["emotion"] = 0
|
|
track["experiments"]["crm"] = 0
|
|
|
|
savemat(res_tracker, mdict={"tracker": track})
|
|
|
|
# Create various loggers to track different elements
|
|
log_filename = os.path.join(
|
|
options["logs_path"],
|
|
"{}{}_{}_robot.mat".format(options["result_prefix"], lang.lower(),
|
|
subject))
|
|
|
|
options["log_filename"] = log_filename
|
|
|
|
# tracks participants progress
|
|
expe_logger = setup_logger("expe_logger", options["log_filename"])
|
|
# tracks general information
|
|
gen_logger = setup_logger("gen_logger", "./logs/general.log")
|
|
time_logger = setup_logger("time_logger", "./logs/time_check.log")
|
|
# gen_logger = setup_logger("gen_logger", gen_logs)
|
|
|
|
# Check if the subject's file already exists, otherwise create it
|
|
if p_exists is None:
|
|
if not os.path.exists(res_filename):
|
|
msg = "The subject '{}' does not exist. Create it?".format(subject)
|
|
print msg
|
|
ans = raw_input("(Y/n): ")
|
|
|
|
if ans.lower() == 'y':
|
|
expe_build_conditions.expe_build_conditions(options, lang)
|
|
# -- Create new logging file
|
|
expe_logger.info(
|
|
"====== Beginning new log for subject: {} ======".format(
|
|
subject))
|
|
expe_logger.info("====== Date of creation: {} \n\n".format(
|
|
datetime.datetime.today()))
|
|
elif ans.lower() == 'n':
|
|
sys.exit()
|
|
else:
|
|
raise "that is not a valid choice"
|
|
|
|
else:
|
|
msg = "Found {}. Use this file?".format(subject)
|
|
print msg
|
|
ans = raw_input("(Y/n): ")
|
|
|
|
expe_logger.info(
|
|
"===== Continuing previous created experiment =====")
|
|
expe_logger.info("===== Date: {}\n\n".format(
|
|
datetime.datetime.today()))
|
|
|
|
if ans.lower() == 'n':
|
|
sys.exit()
|
|
elif not p_exists:
|
|
expe_build_conditions.expe_build_conditions(options)
|
|
|
|
# run the robot experiment
|
|
gen_logger.info("***** Starting general logging *****\n\n")
|
|
time_logger.info("***** Starting time check logging *****\n\n")
|
|
# Used when testing the time it takes to generate stimulus files
|
|
matlab = True
|
|
if matlab:
|
|
time_logger.info("=== Using matlab stimulus generation ===\n")
|
|
else:
|
|
time_logger.info("=== Using PyWorld stimulus generation === \n")
|
|
expe_robot.expe_robot(ip, port, lang, options, phase, subject)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
options = {}
|
|
main(options, "test", "test", "en")
|