-
Notifications
You must be signed in to change notification settings - Fork 6
/
rnndb.py
86 lines (66 loc) · 2.02 KB
/
rnndb.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
import pymongo
from config import config
con=pymongo.MongoClient(config['rnndb'],27017)
#db=con.drop_database('rnn')
db=con['rnn']
from tempfile import NamedTemporaryFile as ntf
import os
import pickle
from bson.binary import Binary
import theanets #todo: set a version/commit
_mydir,_=os.path.split(os.path.realpath(__file__))
def find(ts_id
,params):
tbl=db[ts_id]
pc=tomongotypes(params)
return tbl.find(pc)
def distinct_iters(ts_id
,params):
found=find(ts_id,params)
return found.distinct('iter')
def get_net(ts_id
,params
,i=-1): # n -1 gets the last one interted
"""get str rep from db and put it into a file"""
tbl=col=db[ts_id] #'collection'
pc=tomongotypes(params)
found=list(tbl.find(pc))
#if len(found)>1: raise Exception('more than one model matched params')
if len(found)==0: return None
found=found[i] # get's the last one inserted
tfp=ntf(dir=_mydir,suffix='.tmp',delete=False)
try:
with open(tfp.name,'wb') as f: f.write(found['net'])
tfp.close()
net=theanets.Network.load(tfp.name)
finally: #cleanup
os.remove(tfp.name)
return net
def save_net(ts_id
,params,net
,run_id=None):
tbl=col=db[ts_id] #'collection'
if run_id != None: params['run_id']=run_id
pc=tomongotypes(params)
tfp=ntf(suffix='.tmp',delete=False)
try:
tfp.close();#b/c it looks like .save() tries to do that
net.save(tfp.name)
tfp.close() #just in case
pc['net']=pickle.dumps(net.load(tfp.name))
tfp.close()
finally: #cleanup
os.remove(tfp.name)
o= tbl.insert_one(pc).inserted_id
return o
def tomongotypes(params):
mt={}
for ap in params:
try: #chk for number
params[ap]/1.0
#just using ints but could use floats for flexibility
mt[ap]=float(params[ap])
except:
mt[ap]=params[ap]
return mt
# todo: climate loggind stdout