-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.py
1706 lines (1490 loc) · 74.1 KB
/
experiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Run computer simulation experiments and configure them.
This scripts provides a general framework for setting up computer experiments
with logging, results dump, resuming, separation into phases, batch-training
mdp networks and a command line or interactive console interface to start a run.
The basic idea is to separate the program logic of a computer simulation
experiment with all its phases (from preparation, over training data generation
and training to testing/applying and visualization) from the specific
configuration of parameters.
The parameters are stored in a separate python file which contains a
experiments.Config instance. Common parameters are the number of training
samples, number of iterations and so on.
A common problem is to keep track of different runs with different parameters.
The classes here help to easily define the parameters and storing
all the settings and results to files.
Features:
* Nicely formatted logging to stderr and a log file with different options.
* Command line interface or interactive usage (e.g. from IPython)
* Separate different steps of your experiment into phases.
* Phases can be auto-wrapped around functions.
* Phases can optionally implement needsrun() to skip it if not needed.
* Phases can optionally implement is_allowed_to_run() for (simple) dependency.
* Results are dumped after each phase. If something went wrong, you can
investigate and continue the already written results.
* Printing the total amount of memory currently used by this python process.
* Exception-robust: An exception in a phase does not break the whole
experiment and later phases can still be executed. Results are dumped.
* The name of the experiment is used to create a dir for the log and
result file.
* Additional custom command line options can be added.
* Optionally call pylab.show() at the end.
* You can continue an older run and pick up the results computed there
and continue to run the remaining needed phases. Thereby you can brake
dwon a large experiment in separate runs (for each phase one).
* Version number support.
* Report of some configuration setting of the current machine.
* A list of matplotlib colors and markers to ease some plotting needs
when you iterate over some visual object clases or whatever.
* Experimental: loadnet. Load an mdp network
* Experimental: batch_train
Experimental:
If you run a bunch of experiments that differ only in a certain parameter (or
few parameters, you can create something like "baseconfig.py"
that describes all the parameters for an experiment. Then for a specific run
you can overwrite some of the setting with another "config.py"
(names arbitrary).
@copyright:
2009-2011, Samuel John
@author:
Samuel John.
@contact:
www.SamuelJohn.de
@license:
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@todo: Add tutorial
@todo: Use the Buffer logging handler that stores pickeled versions of the
LogRecords and also add some viewer tools that can display log messages
with on-the-fly filtering (e.g. based on logger hierarchy)
Is there
@todo: Switch to python 2.7's argparse
@todo: Python3 support
@todo: Add a support function to copy some stuff (possibly over the network)
to the CWD (or localdisk / or tmp)
@todo Doc: Add a very simple config.py, run.py to show how cool this is :-)
@todo Test and support to use custom options to the command line. (use argpase)
'''
#from experiments.phase import Phase
#from experiments.config import Config
#from experiments.loop_unroller import Unroller
from phase import Phase, DependsOnAnotherPhase
from config import Config
from loop_unroller import Unroller
from contextlib import closing
from subprocess import Popen, PIPE
import collections
import copy
import inspect
import logging
import os
import shelve
import shutil
import sys
import time
import unittest
import platform
import pickle
def time_string():
'''Shorthand for getting the current time in a nice, consistent format.'''
# todo: Make this function
return time.strftime('%Y-%h-%d__%H-%M-%S')
def host_string():
'''Return the network name of this machine.
Might be handy if run on a cluster to know the host.'''
return platform.node().split('.')[0]
def timehost_string():
'''Get a unique string compose of the current time and host.'''
return time_string() + '_on_' + host_string()
def get_version_string( version_tuple ):
''''''
if isinstance(version_tuple, int):
version_tuple= (version_tuple,0,0)
elif isinstance(version_tuple, float):
f = version_tuple
import math
version_tuple= (int(f.__trunc__()),
int(math.floor((f - f.__trunc__()+0.00000001) * 10)),
0 )
s = "%i" % version_tuple[0]
while len(version_tuple) < 3:
version_tuple = list(version_tuple) + [0]
for d in version_tuple[1:]:
s += ".%i" % d
return s
def enable_logging(loglevel=logging.INFO, stream=None):
'''Shorthand to globally enable the logging to stderr.'''
rootLogger = logging.getLogger()
rootLogger.setLevel(loglevel)
if len(rootLogger.handlers)==0:
rootLogger.setLevel(loglevel)
#rootLogger.handlers = []
loggingStderrHandler = logging.StreamHandler(stream)
#loggingStderrHandler.setLevel(loglevel)
formatter = logging.Formatter('%(levelname)-7s %(name)10s: %(message)s')
loggingStderrHandler.setFormatter(formatter)
rootLogger.addHandler(loggingStderrHandler)
else:
pass
#print('enable_logging(): StreamHandler not added, because logging.getLogger().handlers was not empty:', rootLogger.handlers)
class Experiment(object):
'''
Experiment. A support class to configure and run python experiments with
different phases and configuration options.
Usage in your program:
from experiments import *
ex = Experiment( interactive=False if __name__ == '__main__' else True,
phases=(GenTrainData(), Train(), ShowOutputs()),
version=(1,0,2),
greeting='Learn a SFA hierarchy on moving and zooming 1d patterns.',
author='Your Name')
#Then:
ex.log.debug('foo') # easy access to logger
ex.config.whatever... # access the config file
ex.__options.somecommandlineoption # access the extracted & processed
# command line arguments
ex.version # The version of the experiment.
ex.unique_ending # A unique string (date, host...)
ex.saveresult # Should the resultfile be saved
ex.resultfile # the resultfile
ex.matplotlib_backend # The backend for matplotlib
ex.NET # optional the mdp network
ex.markers # some matplotlib markers
ex.colors # matplotlib ready colors
============================================================================
In interactive mode, your next steps assuming an Experiment has been
created and named ex:
ex.set_config("path/to/config.py", name="testrun", reusefile='old.RESULT')
ex.phases
ex.run(phases=["GenTrainData","Train"])
ex.memory()
ex.result.keys()
ex.log.info("That was fun!")
ex.save()
'''
def __init__(self,
phases=[],
config=None,
options=[],
greeting= 'A new experiment',
author=None, # -> will be os.envrion['USER']
version=(0,0,0),
name='run', # don't change the default without adapting the code below!
logfile=True,
loglevel='DEBUG',
stderrloglevel='INFO',
fileloglevel='DEBUG',
reusefile='',
resultfile='',
saveresult=True,
showplots=False, # setting this here to true lets pop up windows during unittests.
loadnet='',
interactive=True,
matplotlib_backend=''
):
'''
Sets up a new experiment.
@param phases:
The different available phases for this experiment. A list of
object of the class *Phase*.
@param interactive:
If True (default) do not perform command line parsing. This is
good, if you want to use it from ipython in interactive mode.
All the parameters overwrite the information, given at the command line
if interactive=False:
@param config:
Path of the config file to load. Must point to a python script.
The file extension .py is optional. The imported module
has to provide an object named "config".
That object should be a Mappable. Mostly this will be a
<experiments.Config> type.
@param options:
A list of optparse.Option or support_experiment.Option objects.
The same as if you would call self.__parser.add_option() for each
element in the list.
@param greeting:
The greeting text (should be one line with the name of the prog.)
Default: "A new experiment"
@param author:
The name of the author (you can put copyright statement here, too).
It will be shown just below the greeting line after "Written by " .
Defaults to os.environ['USER'].
@param version:
A tuple (major, minor, patch, ...) that identifies the version
of your program. Can also be an int or an float.
@param name:
An optional name for this experimental run. Will overwrite
the name defined in the config.py's config.NAME.
And in turn both will be overwritten from the command line
option --name.
@param logfile:
path (and name) of the logfile or True, if automatic naming should
be used (default). None or False to disable logging to file.
@param loglevel:
The global (root) log level. Must be a string out of 'CRITICAL'
'ERROR', 'WARNING', 'INFO', 'DEBUG' or 'ALL'.
@param fileloglevel:
Log level for the logfile.
@param stderrloglevel:
The log level for the stderr.
@param reusefile:
A string (filename) of a shelve file to load and use the
stuff in there to continue this experiment. (still experimental)
@param resultfile:
A string (filename) of an shelve to create for storing all the
results of this experimental run. If not given, a default unique
name will be used (unless saveresult==False).
@param saveresult:
True for storing the results to a shelve file. Default: True.
@param loadnet:
Load (a possibly trained) MDP network instead of using the one
from the config file. (experimental). The network then added
as the key 'NET' to the result dict.
@param showplots:
call matplotlib.pyplot.show() at the end.
'''
self.unique_ending = timehost_string()
self.loglevels = {'critical':50, 'error':40, 'warning':30, 'warn':30, 'info':20,
'debug':10, 'all':0, 'notset':0 }
if logfile is True:
logfile = self.unique_ending + ".log"
self.version = version
if author is None:
try:
author = os.environ['USER']
except KeyError:
pass
self.interactive = interactive
self.author = author
self.greeting = greeting
self.__logfile = logfile
self.__loglevel = loglevel
self.__stderrloglevel = stderrloglevel
self.__fileloglevel = fileloglevel
# command arg parsing --------------------------------------------------
if not self.interactive:
if True:#sys.version_info[0]==2 and sys.version_info[1] < 7: # todo: use argparse
import optparse
from optparse import OptionParser, Option
self.__parser = OptionParser(usage='python %prog [--config FILE] [moreoptions] [[PHASE1][ PHASE2],...]' +
'\n\nAvailable PHASEs:\n o '+
'\n o '.join(str(p) for p in phases) +
'\n\nIf no phases are given, all will be run.\n\n' ,
version=get_version_string(self.version) )
og=optparse.OptionGroup(self.__parser, 'General experiment options' )
if options is not None and len(options) > 0:
for o in options:
og.add_option(o)
og.add_option('--config', '-c', '--setup',
default=config,
type=str, dest='config', metavar='FILE',
help='The config python script that defines'
' the settings for this experiment.')
og.add_option('--name',
default=name,
type=str, dest='name', metavar='STRING',
help='Optional. The speaking name for this'
' experimental run. Will be used '
'for the logger, the logfile '
'and to create the resultfile. '
'Can also be defined in the '
'config file itself but will be over'
'written if given here.')
og.add_option('--reuse',
default=reusefile,
type=str, dest='reusefile', metavar='SHELVEFILE',
help='Use another (older) shelve file '
'instead of generating all results anew. '
'Depending on what is in SHELVEFILE, '
'it is inferred, what has still to be '
'done. Everything that is found in the '
'shelve is reused (if the phases support '
'the needsrun() method) unless you explicitly '
'specify certain phases to be run. '
'Nothing from the SHELVEFILE is '
'overwritten, instead results are written '
'to --result if given or suppressed with '
' --dontsaveresult. However, --reuse and '
'--result can point to the same file '
'and in that case the file _is_ '
'overwritten.' )
og.add_option('--result',
default=resultfile,
type=str, dest='resultfile', metavar='SHELVEFILE',
help='Store results to a shelve file with the '
'given name. Otherwise, a default unique '
'file name will be created. If --reuse '
'FILE1 and --result FILE2 point to the same '
'file, then in effect FILE1 is overwritten '
'with the new results (unless suppressed by '
'--dontsaveresults).'
'If SHELVEFILE already exists, it will '
'be backed up.' )
og.add_option('--dontsaveresult', action='store_false',
default=saveresult,
dest='saveresult',
help='Do not store results to file. Note that '
'you can still use --reuse to continue a '
'previous run but --result will be ignored.')
og.add_option('--dontshowplots',
default=showplots, action='store_false',
dest='showplots', metavar='BOOL',
help='If given, matplotlib results are '
'not shown.')
og.add_option('--loadnet',
default=loadnet,
type=str, dest='loadnet', metavar='FILE',
help='Should the MDP network be loaded from '
'a pickle FILE? This may perhaps be '
'necessary, if you want a different MDP '
'network to be used with a reused '
'SHELVEFILE.')
og.add_option('--matplotlib_backend',
default=matplotlib_backend, type=str,
dest='matplotlib_backend', metavar='BACKEND_NAME',
help='If given, matplotlib attempts to use that '
'specific backend. For example pdf output in '
'instead of GUIs popping up.')
og.add_option('--listphases',
default=False, action='store_true',
dest='listphases',
help='If given, does nothing more than listing'
' all available phases and ignored all'
' other given arguments.')
logGroup = optparse.OptionGroup(self.__parser, 'Logging Options',
'These options control the log level and log file. '
'The log levels can be integer numbers or names. '
'The int numbers should be in the range from 0 to 50. '
'The understood names and their level are (case does '
'not matter here): '
'CRITICAL=50, ERROR=40, WARNING=30, INFO=20, '
'DEBUG=10, ALL=0.')
logGroup.add_option('--loglevel',
type='str', dest='loglevel', metavar='LEVEL',
default=loglevel,
help='The overall log level. '
'Nothing below this level will be logged anywhere, '
'despite what is set in the other loglevel arguments.'
'Default: "DEBUG"')
logGroup.add_option('--fileloglevel',
type='str', dest='fileloglevel', metavar='LEVEL',
default=fileloglevel,
help='The log level for stderr. '
'Default: "DEBUG"')
logGroup.add_option('--stderrloglevel',
type='str', dest='stderrloglevel', metavar='LEVEL',
default=stderrloglevel,
help='An integer value or name for the log level of stderr.'
'Default: "INFO"')
logGroup.add_option('--logfile',
type='str', dest='logfile', metavar='FILE',
help='Path (and name) of the log file to create. '
'If there is already a logfile, ".old" will '
'be append to the older one. '
'Default: ' + str(logfile))
self.__parser.add_option_group(og)
self.__parser.add_option_group(logGroup)
self.__options, self.__args = self.__parser.parse_args()
else:
raise NotImplementedError('I should impl. argpasrse for python 2.7')
name = self.__options.name
config = self.__options.config
try:
self.__loglevel = self.__options.loglevel.lower().strip()
except:
self.__loglevel = int(self.__options.loglevel)
if self.__options.logfile:
self.__logfile = self.__options.logfile
try:
self.__stderrloglevel = self.__options.stderrloglevel.lower().strip()
except:
self.__stderrloglevel = int(self.__options.stderrloglevel)
try:
self.__fileloglevel = self.__options.fileloglevel.lower().strip()
except:
self.__fileloglevel = int(self.__options.fileloglevel)
loadnet = self.__options.loadnet
reusefile = self.__options.reusefile
self.showplots = self.__options.showplots
self.resultfile = self.__options.resultfile
self.saveresult = self.__options.saveresult
self.matplotlib_backend = self.__options.matplotlib_backend
if self.__options.listphases:
print ('Available Phases:\n ' +
'\n '.join(str(p) + ('\n "' + p.__doc__ +'"' if p.__doc__ else '') for p in phases) +'\n' )
exit(0)
else:
# interactive mode -------------------------------------------------
print ('Experiment in interactive mode (no sys.argv parsing).')
try:
self.__loglevel = self.__loglevel.lower().strip()
except:
self.__loglevel = int(self.__loglevel)
try:
self.__stderrloglevel = self.__stderrloglevel.lower().strip()
except:
self.__stderrloglevel = int(self.__stderrloglevel)
try:
self.__fileloglevel = self.__fileloglevel.lower().strip()
except:
self.__fileloglevel = int(self.__fileloglevel)
self.showplots = showplots
self.resultfile = resultfile
self.saveresult = saveresult
self.matplotlib_backend = matplotlib_backend
self.__args = [] # This will lead to no phases run initially
# Stderr and file logging-----------------------------------------------
if self.__loglevel in self.loglevels.keys():
self.__loglevel = self.loglevels[self.__loglevel]
self.__loglevel = int(self.__loglevel)
if self.__fileloglevel in self.loglevels.keys():
self.__fileloglevel = self.loglevels[self.__fileloglevel]
self.__fileloglevel = int(self.__fileloglevel)
if self.__stderrloglevel in self.loglevels.keys():
self.__stderrloglevel = self.loglevels[self.__stderrloglevel]
self.__stderrloglevel = int(self.__stderrloglevel)
# Loading config -------------------------------------------------------
print 'Setting up experiment...'
sys.stdout.flush()
self.__raw_phases = phases
self.set_config(config, name=name, reusefile=reusefile)
# A logger for the user ------------------------------------------------
self.log = logging.getLogger(self.config.NAME)
self.log.info('Experiment setup finished.')
#if self.interactive:
# rootLogger.info(self.__doc__)
self.log.info('\n================================================================================\n\n\n')
# load a mdp net -------------------------------------------------------
if loadnet:
self.log.warn('Loading MDP net %s into self.result.NET', loadnet)
import cPickle
try:
self.result.NET
pass # ok there is no NET already in the current result
except (KeyError,AttributeError) as e:
self.log.warn('Overwriting result.NET from loaded result file with the NET from %s',loadnet)
with open(loadnet, 'rb') as f:
self.result.NET = cPickle.load(f)
# Trying to set matplotlib back end ------------------------------------
if self.matplotlib_backend:
self.log.debug('Setting matplotlib back end to '+self.matplotlib_backend)
import matplotlib
matplotlib.use(self.matplotlib_backend)
# provide matplotlib compatible colors ---------------------------------
self.colors = [ (40/255., 91/255., 1. ),
(1., 50/255., 49/255. ),
(13/255., 125/255., 32/255. ),
(35/255., 195/255., 240/255.),
(235/255., 163/255., 92/255. ),
(99/255., 199/255., 21/255. ),
(153/255., 25/255., 255/255.),
(229/255., 82/255., 255/255.),
(149/255., 123/255., 54/255. ),
(255/255., 212/255., 0. )
]
self.markers = ['s', # square
'o', # circle
'^', # triangle up
'v', # triangle down
'd' ]# diamond
# Add phases as methods with the same name for easy interactive calling
for p in self.phases:
n = p.name.lower().strip()
if n in self.__dict__: continue
class Proxy(object):
def __init__(self, ex, name):
self.__ex = ex
self.__phase_name = name
def __call__(self,**kws):
return self.__ex.run(self.__phase_name, **kws)
temp = Proxy(ex=self,name=n)
temp.__doc__ = p.__doc__
self.__dict__[n] = temp
# If positional __args were given, run the specified phases now --------
if len(self.__args) > 0 :
self.log.debug('Phases to run now (as given by command line): '+str(self.__args))
self.run(phases=self.__args, force=True)
elif len(self.__args) == 0 and not interactive:
self.log.debug('Phases to run now: all.')
self.run(phases='all', force=False)
elif interactive:
self.log.debug('Running no phases automatically. You have to call ex.run("name_of_phase").')
def __str__(self):
try:
return '<Experiment ' + self.config.NAME + ': "' + self.greeting + '" with phases [' + ', '.join( str(p) for p in self.phases ) + ']>'
except:
return '<Experiment "' + self.greeting + '" yet without a config.>'
__repr__ = __str__
@property
def stderrloglevel(self):
return self.__stderrloglevel
@stderrloglevel.setter
def stderrloglevel(self,l):
if isinstance(l,str):
ll = l.lower().strip()
if ll in self.loglevels.keys():
ll = self.loglevels[ll]
self.__stderrloglevel = int(ll)
else:
self.__stderrloglevel = int(l)
self._setup_logging()
self.log.info('stderrloglevel is %s',logging.getLevelName(self.__stderrloglevel))
@property
def fileloglevel(self):
return self.__stderrloglevel
@fileloglevel.setter
def fileloglevel(self,l):
if isinstance(l,str):
ll = l.lower().strip()
if ll in self.loglevels.keys():
ll = self.loglevels[ll]
self.__fileloglevel = int(ll)
else:
self.__fileloglevel = int(l)
self._setup_logging()
self.log.info('fileloglevel is %s',logging.getLevelName(self.__fileloglevel))
@property
def loglevel(self):
return self.__stderrloglevel
@loglevel.setter
def loglevel(self,l):
if isinstance(l,str):
ll = l.lower().strip()
if ll in self.loglevels.keys():
ll = self.loglevels[ll]
self.__loglevel = int(ll)
else:
self.__loglevel = int(l)
self._setup_logging()
self.log.info('loglevel is %s',logging.getLevelName(self.__loglevel))
def _setup_logging(self):
rootLogger = logging.getLogger()
rootLogger.setLevel(self.__loglevel)
if len(rootLogger.handlers) == 0:
loggingStderrHandler = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('%(levelname)-7s %(name)10s: %(message)s')
loggingStderrHandler.setFormatter(formatter)
rootLogger.addHandler(loggingStderrHandler)
for h in rootLogger.handlers:
if isinstance(h,logging.FileHandler):
h.setLevel(self.__fileloglevel)
elif isinstance(h, logging.StreamHandler):
h.setLevel(self.__stderrloglevel)
return
def set_config(self, config=None, name='run', reusefile=None):
'''Set a config for this experiment.
@param config:
Can be a file path, a mappable (like dict) or None (an empty
configuration will be created.
@param name:
A name you can assign for this experimental run. (default: 'run')
A directory in the current dir with this name will be created to
store the log and result files. Older files will not be overwritten.
'''
self._setup_logging()
rootLogger = logging.getLogger()
rootLogger.info('set_config(config=%s):',str(config))
# Populating self.phases -----------------------------------------------
if self.__raw_phases is None:
self.phases = []
else:
self.phases = []
for p in self.__raw_phases:
if inspect.isfunction(p):
rootLogger.debug(' Phase %s was given as a function.', p)
self.phases.append( Phase(wrap_function=p) )
elif inspect.isclass(p):
rootLogger.debug(' Phase %s was given as a class.', p)
self.phases.append( p() )
else:
rootLogger.debug(' Phase %s was given as an instance', p)
self.phases.append( p )
if config is not None: # makes only sense to show this if a config is loaded
rootLogger.info('Available phases: \n' +
'\n '.join(str(p) + ('\n "' + p.__doc__ +'"\n' if p.__doc__ else '') for p in self.phases) +'\n' )
# Loading/Setting config -----------------------------------------------
if config is None:
rootLogger.info('No config specified. Creating an empty config obj. You may want to call ex.set_config("path_to_config.py")')
self.config = Config()
elif isinstance(config, collections.Mapping):
self.config = Config(config)
rootLogger.info('Using mapping ' + str(config.__class__.__name__) + ' as config.')
else:
rootLogger.info('Assuming %s is a path to a python file that contains a "config" object or a dict with that name.', config)
if os.path.exists(config):
global_dict = {}
execfile(config, global_dict)
try:
# Is there an item named "config"
self.config = global_dict['config']
rootLogger.info('Using item "config" from %s', config)
except KeyError, ke:
rootLogger.warning('No item named "config" found in %s. Now searching for an instance of experiments.Config... ', config)
for i in global_dict:
if isinstance(i, Config):
rootLogger.info('Found and instance of experiments.Config: %s ', i)
self.config = i
else:
rootLogger.error('Specified config file %s does not exist.', config)
raise IOError('Config file does not exist')
# Setting NAME and UNIQUE_NAME -----------------------------------------
if name != 'run' and not None:
self.config.NAME = name
elif not self.config.has_key('NAME'):
self.config.NAME = name
self.config.NAME = self.config.NAME.replace(' ', '_')
self.config.NAME = self.config.NAME.replace('/', '_')
self.config.NAME = self.config.NAME.replace('\\', '_')
self.unique_name = self.config['NAME'] + "_" + self.unique_ending
self.config.UNIQUE_NAME = self.unique_name
# Creating dir for this experiment -------------------------------------
if not os.path.exists(os.curdir + os.sep + self.config.NAME):
os.mkdir(os.curdir + os.sep + self.config.NAME)
rootLogger.info('Output for this experiment is put into the new directory: \n\t\t"%s"', os.path.abspath(os.curdir+os.sep+self.config.NAME))
else:
rootLogger.info('Output for this experiment is put into the existing directory \n\t\t"%s".', os.path.abspath(os.curdir+os.sep+self.config.NAME))
# Adding logfile -------------------------------------------------------
if self.__logfile:
logfile = os.curdir + os.sep + self.config.NAME + os.sep + self.__logfile
backupFile(logfile, '.bak')
# Remove any other (older) FileHandlers
rootLogger.handlers = [h for h in rootLogger.handlers if not isinstance(h,logging.FileHandler)]
loggingFileHandler = logging.FileHandler( logfile, mode='w' )
loggingFileHandler.setLevel( self.__fileloglevel )
loggingFileHandler.setFormatter(logging.Formatter('%(levelname)-7s %(name)10s: %(message)s'))
rootLogger.addHandler(loggingFileHandler)
rootLogger.info( 'logfile: ' + str(logfile) )
# Print/log infos ------------------------------------------------------
welcome =('''
================================================================================
%s
Experiment written by %s. (version %s)
================================================================================''') % \
(str(self.greeting), str(self.author), get_version_string(self.version))
rootLogger.info( welcome)
rootLogger.info( time.asctime() )
try:
rootLogger.info( 'On %s', os.uname()[1] )
rootLogger.info( 'Architecture %s', os.uname()[-1] )
except:
pass
if not self.interactive:
commandline = ""
for a in sys.argv:
commandline += ' ' + a
rootLogger.info( 'Command line args were: ' + commandline)
# Init result ----------------------------------------------------------
self.result = Config()
# REUSE ----------------------------------------------------------------
self.reuse(reusefile)
# SAVE (almost empty result to have the result.config stored) ----------
self.result.config = copy.deepcopy(self.config)
rootLogger.info('Storing the current cofing to result.config.')
def reuse(self, reusefile):
'''Loading and re-using an old result file.'''
logger = logging.getLogger('reuse')
self.unique_ending = timehost_string()
logger.info('Setting unique_name to %s', self.unique_name)
if not reusefile and self.config.has_key('REUSE_FILE'):
reusefile = self.config['REUSE_FILE']
if reusefile:
self.config['REUSE_FILE'] = reusefile
logger.warn('Reusing old results from %s', self.config.REUSE_FILE)
if not os.path.exists(reusefile):
raise ValueError('File "%s" does not exist.', reusefile)
try:
tmp = shelve.open(reusefile)
for k,v in tmp.items():
self.result[k] = copy.deepcopy(v)
logger.debug('Reusing '+ k)
except Exception as e:
logger.exception('Failed using old result file.')
finally:
tmp.close()
else:
logger.debug('Not reusing old results.')
def _find_phase(self, phase):
'''Helper to find a phase by its (case insensitive) name.'''
if isinstance(phase,str):
#self.log.debug(' find_phase: Name of phase given as str (%s)...', phase)
for p in self.phases:
if p.name.lower().strip() == phase.lower().strip():
return p
if p.name.lower().strip()+'-phase' == phase.lower().strip():
return p
elif isinstance(phase,Phase):
for p in self.phases:
if phase is p:
return p
return None # no phase found
def run(self, phases='all', stopOnException=False, force=False, nosave=False, **kwargs):
'''Actually run this experiment or more precisely the phases.
@param phases:
If 'all', then all phases, defined in self.phases are executed.
Otherwise only the phases of the list of strings *phases*.
Each phase to be run is a string
with the name of a class (internally phases[i].__class__.__name__
is used) or of a function from self.phases.'''
retval = []
if phases == 'all':
phases = [ str(p.name) for p in self.phases ]
if isinstance(phases,str):
phases = [phases]
if isinstance(phases,Phase):
phases = [phases]
if len(phases) > 0:
self.log.info('Requested to run phases: \n%s', phases)
for phase in phases:
if self._find_phase(phase) is None:
raise ValueError('Phase %s not found. Available are %s' % (str(phase),str(self.phases)) )
for phase in phases:
# Checking dependencies and constraints
P = self._find_phase(phase) # get Phase obj from str if in self.phases
tic = time.time()
self.log.info('Starting %s ...', P)
if P.has_wrap_function:
try:
self.log.info(' ' + P.wrap_function.__doc__)
except: pass
elif P.__doc__:
self.log.info(' ' + P.__doc__)
try:
if force:
needsrun = True
self.log.info('Forced to run this phase (skipping needsrun() and is_allowed_to_run() check).')
else:
try:
needsrun = P.needsrun(ex=self, config=self.config, result=self.result)
except KeyError, e:
needsrun = True
self.log.debug('Need to run %s because an item was probably not found in ex.result. (KeyError in needsrun())', P)
except AttributeError, e:
needsrun = True
self.log.debug('Need to run %s because an item was probably not found in ex.result. (KeyError in needsrun())', P)
except AssertionError, e:
needsrun = True
self.log.debug('Need to run %s because of: %s', str(e))
# "allowed" is stronger than "force"
try:
allowed = P.is_allowed_to_run(ex=self, config=self.config, result=self.result)
except KeyError, e:
self.log.debug('Assuming to forbid run %s because an item was probably not found in ex.result. (KeyError in is_allowed_to_run().)', P)
allowed = False
if needsrun and allowed:
# A local function for the recursive running of dependencies in case of DependsOnAnotherPhase Exception
def run_a_phase(the_phase, retval=[], extra_keywords={}):
try:
retval.append( the_phase(ex=self, config=self.config, result=self.result, **extra_keywords) )
except DependsOnAnotherPhase as err:
self.log.info('\n================================================================================')
self.log.info('The phase %s depends on %s. Now executing that one first...', P, err.depends_on)
tic=time.time()
run_a_phase(self._find_phase(err.depends_on),extra_keywords={})
tac=time.time()
self.log.info('Finished %s. Took %g seconds.', err.depends_on, tac-tic)
self.log.info('\n================================================================================')
self.log.info('And now trying to run %s again...', the_phase)
retval.append(run_a_phase(the_phase, extra_keywords=extra_keywords))
run_a_phase(P, retval, extra_keywords=kwargs)
elif not allowed:
self.log.error('The %s is not allowed to be run at this point.',P)
elif not needsrun:
self.log.warn('%s needs not to be run. Using cached results.', P)
except BaseException, e:
self.log.exception('Exception in %s', P)
if stopOnException:
tac = time.time()
self.log.info(str(e))
self.log.warn('ABORT: Breaking %s because stopOnException==True.', P, tac-tic)
self.log.info('\n================================================================================\n\n\n')
break # but still write the results
else:
self.log.exception('Continuing with next phase and ignoring this Exception: ' + str(e))
tac = time.time()
self.log.info('Finished %s. Took %g seconds.', P, tac-tic)
self.log.info('\n================================================================================\n\n\n')
if not nosave:
self.save()
if self.showplots:
try:
import matplotlib.pyplot as plt
if self.interactive:
plt.draw()
else:
plt.show()
except AttributeError as ae:
self.log.debug('It seems as no plot has been drawn with matplotlib.')
except UserWarning as uw:
self.log.debug('This matplotlib backend does not have a show() method.')
except Exception as e:
self.log.exception('Exception in pylab.show()...')
self.log.info('All Finished.')
return retval
__call__ = run
def save(self, protocol=None):
'''If self.saveresult, then writing the current state of self.result to a shelve.'''
tic = time.time()
if self.saveresult:
if self.resultfile is None and self.config.has_key('RESULT_FILE'):
self.log.info('Using RESULT_FILE "%s" specified in the config.', self.config.RESULT_FILE)
if self.resultfile:
self.config['RESULT_FILE'] = self.resultfile
else:
self.config['RESULT_FILE'] = os.curdir + os.sep + self.config.NAME + os.sep + self.unique_ending + "_RESULT.shelve"
self.log.info('No --result FILE was specified and the config does not contain a RESULT_FILE entry, too.')
self.log.info('Creating a unique name for the result file.')
#if not self.config['RESULT_FILE'].beginswith(self.config.NAME + os.sep):
# self.config['RESULT_FILE'] = self.config.NAME + os.sep + self.config['RESULT_FILE']
if (os.path.exists(self.config['RESULT_FILE']) and
self.config.has_key('REUSE_FILE') and
self.config['RESULT_FILE']==self.config['REUSE_FILE'] ):
self.log.info('Reusing result file %s and updating it. The old one is backed up as ".old".', self.config['RESULT_FILE'])
backupFile(self.config['RESULT_FILE'], ".old")
backupFile(self.config['RESULT_FILE']+'.dir', ".old")
backupFile(self.config['RESULT_FILE']+'.dat', ".old")
backupFile(self.config['RESULT_FILE']+'.bak', ".old")
with closing( shelve.open(self.config['RESULT_FILE'], protocol=protocol) ) as res:
for k,v in self.result.items():
try: # Todo: How to avoid that a NET which fails during train leads to TypeError here
res[k] = v
except TypeError as e:
self.log.error('Cannot save %s (%s) to result file.', str(k), str(v))
except RuntimeError as e:
self.log.error('Cannot save %s (%s) to result file.', str(k), str(v))
except pickle.PicklingError as e:
self.log.error('Cannot save %s (%s) to result file.', str(k), str(v))
self.log.warn('==> Saving new results to %s', self.config.RESULT_FILE)
else:
self.config.RESULT_FILE = None
self.log.info('Not requested to save results. (--dontsaveresult)')
tac = time.time()
self.log.debug('Saving result file took %i seconds.', tac-tic)
def memory(self, pid=None):
"""Return int containing memory in Kilobytes used by the current process."""
try:
if not pid:
pid = os.getpid()
process = Popen("ps -o rss= -p %i" % pid,
shell=True,
stdout=PIPE )
stdout_list = process.communicate()[0].split('\n')
mem = int(stdout_list[0])
except Exception, e:
print("Cannot get amount of memory used by this process." + str(e))
return -1