Skip to content
Snippets Groups Projects
Commit b76711fb authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Tools: let getexp handle script dir and missing dirs

parent 38f1979a
No related branches found
No related tags found
No related merge requests found
......@@ -587,6 +587,13 @@ class ExpConfig(ConfigObj):
self.experiment_id = pre_config[ExpConfig.id_name]
self.experiment_kind = re.sub(r'-\w+$', '', experiment_type)
dir_names = ['DATA_DIR', 'WORK_DIR']
cfg_dir_names = pre_config.get('EXP_DIR_NAMES', [])
if not isinstance(cfg_dir_names, (list, tuple)):
cfg_dir_names = [cfg_dir_names]
dir_names.extend(d for d in cfg_dir_names if d not in dir_names)
self.exp_dir_names = [d for d in dir_names if d in pre_config]
# Re-read merged config with interpolation set.
# This works around incomprehensible inheritance of interpolation with
# merge. Make sure that all values are interpolated
......
......@@ -97,6 +97,14 @@ try:
except ExpConfigError as error:
die(error.message, status=2)
exp_dir_names = []
if 'MODEL_DIR' in config:
exp_dir_names.append('MODEL_DIR')
if 'SCRIPT_DIR' not in config:
config['SCRIPT_DIR'] = config.experiment_id
exp_dir_names.append('SCRIPT_DIR')
exp_dir_names.extend(config.exp_dir_names)
if args.readme:
print(config['EXP_DESCRIPTION'])
elif args.key:
......@@ -122,14 +130,9 @@ elif args.verbose == 1:
value = u'"{0}"'.format(u'" "'.join(value))
print(u"{0}='{1}'".format(key, value.replace("'",'"')))
else:
print("EXP_ID='{0}'".format(config.experiment_id))
print("MODEL_DIR='{0}'".format(config['MODEL_DIR']))
print("SCRIPT_DIR='{0}'".format(config['SCRIPT_DIR']))
print("WORK_DIR='{0}'".format(config['WORK_DIR']))
print("DATA_DIR='{0}'".format(config['DATA_DIR']))
if not (os.path.isdir(config['MODEL_DIR']) and
os.path.isdir(config['SCRIPT_DIR']) and
os.path.isdir(config['WORK_DIR']) and
os.path.isdir(config['DATA_DIR'])):
print(f"EXP_ID='{config.experiment_id}'")
for exp_dir_name in exp_dir_names:
print(f"{exp_dir_name}='{config[exp_dir_name]}'")
if not all(os.path.isdir(config[d]) for d in exp_dir_names):
info("data for experiment '{0}' does not exist".format(config.experiment_id))
......@@ -423,13 +423,6 @@ except ExpConfigError as error:
def cut_dir_variable(directory):
return re.sub(os.sep + r'[^' + os.sep + r'$]*\$\{\w+\}.*$', '', directory)
def list1(x, keep_empty=False):
if isinstance(x, (list, tuple, Iterator)):
return x
if not keep_empty and x == '':
return []
return [x]
# Create directory for scripts if it doesn't exist
script_dir = config.get('SCRIPT_DIR', config.experiment_id)
print(f"Script directory: '{script_dir}'")
......@@ -446,26 +439,20 @@ else:
os.makedirs(backup_dir)
# Create directories for experiment if they don't exist
exp_dir_names = ['DATA_DIR', 'WORK_DIR']
exp_dir_names.extend(name for name in list1(config.get('EXP_DIR_NAMES', []))
if name not in exp_dir_names)
for exp_dir_name in exp_dir_names:
try:
exp_dir = config[exp_dir_name]
exp_cut = cut_dir_variable(exp_dir)
name = exp_dir_name.lower().removesuffix('_dir')
Name = name.capitalize()
exists = os.path.isdir(exp_cut)
print(f"{Name} directory: '{exp_dir}'"+(
" (already exists)" if exists else
" (not created)" if not args.make_dirs else ""))
if not exists and args.make_dirs:
if exp_dir != exp_cut:
feedback.warning("only considering non-variable part"
" of directory")
os.makedirs(exp_cut)
except KeyError:
pass
for exp_dir_name in config.exp_dir_names:
exp_dir = config[exp_dir_name]
exp_cut = cut_dir_variable(exp_dir)
name = exp_dir_name.lower().removesuffix('_dir')
Name = name.capitalize()
exists = os.path.isdir(exp_cut)
print(f"{Name} directory: '{exp_dir}'"+(
" (already exists)" if exists else
" (not created)" if not args.make_dirs else ""))
if not exists and args.make_dirs:
if exp_dir != exp_cut:
feedback.warning("only considering non-variable part"
" of directory")
os.makedirs(exp_cut)
# Create config dump for reference
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment