+ moved file to module directory
[qpalma.git] / qpalma / SettingsParser.py
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
5 #
6 # Written (W) 2008 Fabio De Bona
7 # Copyright (C) 2008 Max-Planck-Society
8
9 #
10 # This file contains the main interface to the QPalma pipeline.
11 #
12
13 import os
14 import os.path
15 import sys
16
17 jp = os.path.join
18
19
20 def parseSettingsFile(filename):
21 """
22 This function parse all key value pairs from the given filename
23 """
24 settings = {}
25 for line in open(filename):
26 if (not line.strip()) or line.startswith('#'):
27 pass
28 else:
29 key, val = line.strip().replace(' ', '').split('=')
30 settings[key] = val
31
32 return settings
33
34
35 def makeSettings(settings):
36 """
37
38 """
39 assert os.path.exists(settings['result_dir']),'Error: You have to specify a existing result directory!'
40 result_dir = settings['result_dir']
41 settings['approximation_dir'] = jp(result_dir, 'approximation')
42 settings['dataset_dir'] = jp(result_dir, 'dataset')
43 settings['preproc_dir'] = jp(result_dir, 'preprocessing')
44 settings['postproc_dir'] = jp(result_dir, 'postprocessing')
45 settings['prediction_dir'] = jp(result_dir, 'prediction')
46 settings['training_dir'] = jp(result_dir, 'training')
47
48 for dir_name in ['approximation_dir','dataset_dir', 'preproc_dir', 'postproc_dir',\
49 'prediction_dir', 'training_dir']:
50 try:
51 os.mkdir(settings[dir_name])
52 continue
53 except:
54 print ('Error: There was a problem generating the subdirectory: %s' % dir_name)
55
56 ddir = settings['dataset_dir']
57 settings['prediction_dataset_fn'] = jp(ddir,'prediction_data.pickle')
58 settings['prediction_dataset_keys_fn'] = jp(ddir,'prediction_data.keys.pickle')
59 settings['training_dataset_fn'] = jp(ddir,'training_data.pickle')
60 settings['training_dataset_keys_fn'] = jp(ddir,'training_data.keys.pickle')
61
62
63 try:
64 os.mkdir(settings['global_log_fn'])
65 except:
66 print 'Error: There was a problem generating the logfile %s' % settings['global_log_fn']
67
68 try:
69 settings['num_splits'] = int(settings['num_splits'])
70 except:
71 print 'Error: num_splits has to be a positive integer'
72
73 settings['allowed_fragments'] = eval(settings['allowed_fragments'])
74 settings['half_window_size'] = int(settings['half_window_size'])
75
76 return settings
77
78
79 def checkSettings(settings):
80 for (key, val,) in settings.items():
81 if key.endswith('_fn'):
82 assert os.path.exists(val), 'Error: Path/File %s with value %s does not seem to exist!' % (key,val)
83 if key.endswith('_dir'):
84 assert os.path.exists(val), 'Error: Path/File %s with value %s does not seem to exist!' % (key,val)
85
86 return True
87
88
89 def parseSettings(filename):
90 settings = parseSettingsFile(filename)
91 settings = makeSettings(settings)
92 assert checkSettings(settings),'Check your settings some entries were invalid!'
93
94 return settings