comparison pylearn/dbdict/newstuff.py @ 589:6a7f3d83c72b

dbdict stuff
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 17 Dec 2008 15:39:30 -0500
parents 5020b12e87ee
children f8d29730f146
comparison
equal deleted inserted replaced
588:990fa151bb10 589:6a7f3d83c72b
9 9
10 ################################################################################ 10 ################################################################################
11 ### misc 11 ### misc
12 ################################################################################ 12 ################################################################################
13 13
14 class DD(defaultdict): 14 class DD(dict):
15 def __getattr__(self, attr): 15 def __getattr__(self, attr):
16 return self[attr] 16 return self[attr]
17 def __setattr__(self, attr, value): 17 def __setattr__(self, attr, value):
18 self[attr] = value 18 self[attr] = value
19 def __str__(self): 19 def __str__(self):
41 return eval(obj, {}, {}) 41 return eval(obj, {}, {})
42 except (NameError, SyntaxError): 42 except (NameError, SyntaxError):
43 return obj 43 return obj
44 44
45 def flatten(obj): 45 def flatten(obj):
46 """nested dictionary -> flat dictionary with '.' notation """
46 d = {} 47 d = {}
47 def helper(d, prefix, obj): 48 def helper(d, prefix, obj):
48 if isinstance(obj, (str, int, float)): 49 if isinstance(obj, (str, int, float)):
49 d[prefix] = obj #convert(obj) 50 d[prefix] = obj #convert(obj)
50 else: 51 else:
58 helper(d, pfx, v) 59 helper(d, pfx, v)
59 helper(d, '', obj) 60 helper(d, '', obj)
60 return d 61 return d
61 62
62 def expand(d): 63 def expand(d):
63 def dd(): 64 """inverse of flatten()"""
64 return DD(dd) 65 #def dd():
65 struct = dd() 66 #return DD(dd)
67 struct = DD()
66 for k, v in d.iteritems(): 68 for k, v in d.iteritems():
67 if k == '': 69 if k == '':
68 raise NotImplementedError() 70 raise NotImplementedError()
69 else: 71 else:
70 keys = k.split('.') 72 keys = k.split('.')
71 current = struct 73 current = struct
72 for k2 in keys[:-1]: 74 for k2 in keys[:-1]:
73 current = current[k2] 75 current = current.setdefault(k2, DD())
74 current[keys[-1]] = v #convert(v) 76 current[keys[-1]] = v #convert(v)
75 return struct 77 return struct
76 78
77 def realize(d): 79 def realize(d):
78 if not isinstance(d, dict): 80 if not isinstance(d, dict):
155 # greenlet = None 157 # greenlet = None
156 158
157 159
158 class Channel(object): 160 class Channel(object):
159 161
160 COMPLETE = None 162 COMPLETE = property(lambda s:None,
161 INCOMPLETE = True 163 doc=("Experiments should return this value to "
164 "indicate that they are done (if not done, return `Incomplete`"))
165 INCOMPLETE = property(lambda s:True,
166 doc=("Experiments should return this value to indicate that "
167 "they are not done (if done return `COMPLETE`)"))
162 168
163 START = 0 169 START = property(lambda s: 0,
164 """dbdict.status == START means a experiment is ready to run""" 170 doc="dbdict.status == START means a experiment is ready to run")
165 RUNNING = 1 171 RUNNING = property(lambda s: 1,
166 """dbdict.status == RUNNING means a experiment is running on dbdict_hostname""" 172 doc="dbdict.status == RUNNING means a experiment is running on dbdict_hostname")
167 DONE = 2 173 DONE = property(lambda s: 2,
168 """dbdict.status == DONE means a experiment has completed (not necessarily successfully)""" 174 doc="dbdict.status == DONE means a experiment has completed (not necessarily successfully)")
169 175
170 # Methods to be used by the experiment to communicate with the channel 176 # Methods to be used by the experiment to communicate with the channel
171 177
172 def save(self): 178 def save(self):
173 """ 179 """
502 stopper::pylearn.stopper.nsteps \\ # use pylearn.stopper.nsteps 508 stopper::pylearn.stopper.nsteps \\ # use pylearn.stopper.nsteps
503 stopper.n=10000 \\ # the argument "n" of nsteps is 10000 509 stopper.n=10000 \\ # the argument "n" of nsteps is 10000
504 lr=0.03 510 lr=0.03
505 """ 511 """
506 state = expand(parse(*strings)) 512 state = expand(parse(*strings))
507 state.dbdict.experiment = experiment 513 state.setdefault('dbdict', DD()).experiment = experiment
508 experiment = resolve(experiment) 514 experiment = resolve(experiment)
509 workdir = options.workdir or format_d(state, sep=',', space = False) 515 workdir = options.workdir or format_d(state, sep=',', space = False)
510 channel = StandardChannel(workdir, 516 channel = StandardChannel(workdir,
511 experiment, state, 517 experiment, state,
512 redirect_stdout = options.redirect or options.redirect_stdout, 518 redirect_stdout = options.redirect or options.redirect_stdout,