700c896b977621e5658038afbc63e5949e3f1cd6
[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 settings['alignment_dir'] = jp(result_dir, 'alignment')
48
49 for dir_name in ['approximation_dir','dataset_dir', 'preproc_dir', 'postproc_dir',\
50 'prediction_dir', 'training_dir', 'alignment_dir']:
51 try:
52 os.mkdir(settings[dir_name])
53 continue
54 except:
55 print ('Error: There was a problem generating the subdirectory: %s' % dir_name)
56
57 #assert checkSettings(settings),'Check your settings some entries were invalid!'
58
59 ddir = settings['dataset_dir']
60 settings['prediction_dataset_fn'] = jp(ddir,'prediction_data.pickle')
61 settings['prediction_dataset_keys_fn'] = jp(ddir,'prediction_data.keys.pickle')
62 settings['training_dataset_fn'] = jp(ddir,'training_data.pickle')
63 settings['training_dataset_keys_fn'] = jp(ddir,'training_data.keys.pickle')
64
65 try:
66 os.mknod(settings['global_log_fn'])
67 except:
68 print 'Error: There was a problem generating the logfile %s' % settings['global_log_fn']
69
70 try:
71 settings['num_splits'] = int(settings['num_splits'])
72 except:
73 print 'Error: num_splits has to be a positive integer'
74
75 try:
76 settings['prb_offset'] = int(settings['prb_offset'])
77 except:
78 print 'Error: prb_offset has to be a positive integer'
79
80 settings['allowed_fragments'] = eval(settings['allowed_fragments'])
81 settings['half_window_size'] = int(settings['half_window_size'])
82
83 return settings
84
85
86 def checkSettings(settings):
87 for (key, val,) in settings.items():
88 if key.endswith('_fn') or key.endswith('_dir'):
89 assert os.path.exists(val), 'Error: Path/File %s with value %s does not seem to exist!' % (key,val)
90 return True
91
92
93 def parseSettings(filename):
94 settings = parseSettingsFile(filename)
95 settings = makeSettings(settings)
96 #assert checkSettings(settings)
97 return settings