Skip to content

Commit eb985d7

Browse files
committed
arbitrary attributes: fixes and bringing lua up to date
1 parent c84ccd2 commit eb985d7

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

results/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ This stores the reported training and test objective and accuracy from each step
1616
These are stored in the STEPS table.
1717
The time for the first 10 steps (excluding the first) is remembered in the steps table.
1818

19+
#### `results.addResultAttribute`
20+
Stores a value of an attribute for the run, which must not have a value already.
21+
1922
Similar functions are also available in lua after something like this.
2023
```
2124
results = require 'results'

results/results.lua

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local results = {}
66
filename = os.getenv("JR_RESULTSFILE") or "results.sqlite"
77
con = sqlite3.open(filename)
88
filecontents = ""
9+
oldColumns=False
910

1011
function append_file_to_filecontents_string(f)
1112
local ff = io.open(f,"r")
@@ -24,43 +25,93 @@ end
2425
--resultless(sql, param1, param2) executes it with the given parameters
2526
function resultless(st, ...)
2627
local s = con:prepare(st)
27-
assert(s)
28+
if not s then
29+
error(con:errmsg())
30+
end
2831
s:bind_values(...)
2932
local res = s:step()
33+
if res==sqlite3.ROW then
34+
error("resultless: the query returned something")
35+
end
3036
if res~=sqlite3.DONE then
3137
error(con:errmsg())
3238
end
3339
s:finalize()
3440
end
41+
function singleValuedQuery(st, ...)
42+
local s = con:prepare(st)
43+
if not s then
44+
error(con:errmsg())
45+
end
46+
s:bind_values(...)
47+
local res = s:step()
48+
if res==sqlite3.DONE then
49+
error("the query returned nothing")
50+
end
51+
if res~=sqlite3.ROW then
52+
error(con:errmsg())
53+
end
54+
if s:columns()~= 1 then
55+
error("the query returned multiple columns")
56+
end
57+
local o = s:get_value(0)
58+
res=s:step()
59+
if res~=sqlite3.DONE then
60+
error("perhaps the query returned more than one row")
61+
end
62+
s:finalize()
63+
return o
64+
end
3565

36-
function results.startRun(repnname, repnlength, continuation, batchTr, batchTe, layertype, layers, width, architecture, solver, callerFilePath)
66+
function results.startRun(continuation, attribs, architecture, solver, callerFilePath)
3767
if "table" == type(callerFilePath) then
3868
for i,name in ipairs(callerFilePath) do
3969
append_file_to_filecontents_string(name)
4070
end
4171
else
4272
append_file_to_filecontents_string(callerFilePath)
4373
end
44-
local setup=[[create table if not exists RUNS(COUNT INTEGER PRIMARY KEY, TIME TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL, REPN TEXT, REPNLENGTH INT, CONTINUATION TEXT, BATCHTR INT, BATCHTE INT, LAYERTYPE TEXT, LAYERS INT, WIDTH INT, ARCHITECTURE TEXT, SOLVER TEXT, CODE TEXT) ;
45-
create table if not exists STEPS(STEP INTEGER PRIMARY KEY, RUN int, OBJECTIVE real, TRAINACC real, TESTOBJECTIVE real, TESTACC REAL );
46-
create table if not exists TIMES(RUN INT, TIME real)
74+
local runColList="(COUNT INTEGER PRIMARY KEY, TIME TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL, REPN TEXT, REPNLENGTH INT, CONTINUATION TEXT, BATCHTR INT, BATCHTE INT, LAYERTYPE TEXT, LAYERS INT, WIDTH INT, ARCHITECTURE TEXT, SOLVER TEXT, CODE TEXT)"
75+
if not oldColumns then
76+
runColList="(COUNT INTEGER PRIMARY KEY, TIME TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL, CONTINUATION TEXT, ARCHITECTURE TEXT, SOLVER TEXT, CODE TEXT)"
77+
end
78+
local setup="create table if not exists RUNS" .. runColList .. [[;
79+
create table if not exists STEPS(STEP INTEGER PRIMARY KEY, RUN int, OBJECTIVE real, TRAINACC real, TESTOBJECTIVE real, TESTACC REAL );
80+
create table if not exists TIMES(RUN INT, TIME real);
81+
create table if not exists ATTRIBS(RUN INT, NAME TEXT, ISRESULT INT, VALUE TEXT)
4782
]]
4883
check(con:exec(setup))
49-
local infoquery = "insert into RUNS (REPN, REPNLENGTH, CONTINUATION, BATCHTR, BATCHTE, LAYERTYPE, LAYERS, WIDTH, ARCHITECTURE, SOLVER, CODE) VALUES (?,?,?,?,?,?,?,?,?,?,?)"
50-
local info = {repnname, repnlength, continuation, batchTr, batchTe, layertype, layers, width, architecture, solver, filecontents}
84+
local infoquery = "insert into RUNS (CONTINUATION, ARCHITECTURE, SOLVER, CODE) VALUES (?,?,?,?)"
85+
local info = {continuation, architecture, solver, filecontents}
86+
local attribquery = "insert into ATTRIBS(RUN, NAME, ISRESULT, VALUE) VALUES (?,?,0,?)"
5187
resultless(infoquery,unpack(info))
5288
nrun = con:last_insert_rowid()
53-
sys.tic()
89+
for k,v in pairs(attribs) do
90+
resultless(attribquery,nrun,k,v)
91+
end
5492
nsteps = 0
5593
filecontents = nil
5694
end
5795

5896
function results.step(obj,train,objte,test)
5997
nsteps = 1+nsteps
6098
resultless("insert into steps values (NULL, ?, ?, ?, ?, ?)", nrun,obj,train,objte, test)
61-
if nsteps == 10 then
99+
if nsteps == 1 then
100+
sys.tic()
101+
end
102+
if nsteps == 11 then
62103
resultless("insert into TIMES VALUES (?,?)", nrun, sys.toc())
63104
end
64105
end
65106

107+
function results.addResultAttribute(name, value)
108+
if 1 then
109+
count = singleValuedQuery("select count(*) from ATTRIBS where run = ? and name = ?",nrun,name)
110+
if count > 0 then
111+
error("attribute already set:" .. name)
112+
end
113+
end
114+
resultless("insert into ATTRIBS(RUN, NAME, ISRESULT, VALUE) VALUES (?,?,1,?)",nrun,name,value)
115+
end
116+
66117
return results

results/results.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ def runList():
157157
print ( tabulate.tabulate([i for i in b],headers=["","","steps", "start","obj","trainAcc","testObj","testAcc","Bad"]) )
158158
#return a,b
159159

160+
def _getUseOldColumns():
161+
return oldColumns and c.execute("select sql like '%LAYERTYPE%' from sqlite_master where tbl_name='RUNS'").fetchone()[0]
162+
160163
def runList1():
161164
c=con.cursor()
162-
if not oldColumns:
165+
if not _getUseOldColumns():
163166
raise RuntimeError("runList1 not available in the new format")
164167
b=c.execute("select repnlength, repn, count(step), min(objective), max(trainacc), max(testacc) from runs r left join steps s on r.count=s.run group by count").fetchall()
165168
for j in b:
@@ -192,8 +195,9 @@ def runList2(avgLength=None, doPrint = True, runFrom=None, architectureLike=None
192195
b=[]
193196
archClause = ""
194197
masterQueryArgs = None
198+
useOldColumns=_getUseOldColumns()
195199
if architectureLike is not None:
196-
if not oldColumns:
200+
if not useOldColumns:
197201
raise RuntimeError("no architecture search with new format")
198202
# architectureStr = c.execute("select architecture from runs where count = ?",(architectureLike,)).fetchone()[0]
199203
# archClause = "where architecture = ?"
@@ -203,7 +207,7 @@ def runList2(avgLength=None, doPrint = True, runFrom=None, architectureLike=None
203207
if runFrom is not None:
204208
archClause = archClause + " and count >= " + str(runFrom)
205209
runFrom = None
206-
masterQuery = "select count, "+("repnlength, repn," if oldColumns else "") +" case when length(continuation)>0 then '+' else '' end || count(step), (select time from times where run = r.count) from runs r left join steps s on r.count=s.run %s group by count" % (archClause if runFrom is None else ("where count>= "+str(runFrom)))
210+
masterQuery = "select count, "+("repnlength, repn," if useOldColumns else "") +" case when length(continuation)>0 then '+' else '' end || count(step), (select time from times where run = r.count) from runs r left join steps s on r.count=s.run %s group by count" % (archClause if runFrom is None else ("where count>= "+str(runFrom)))
207211
if masterQueryArgs is None:
208212
bb=c.execute(masterQuery).fetchall()
209213
else:
@@ -217,9 +221,9 @@ def bestAvg(idx,highIsBad):
217221
s=replaceSeriesWithMovingAverage_([i[idx] for i in values],avgLength,highIsBad)
218222
return (numpy.amin if highIsBad else numpy.amax)(s)
219223
attribValues=tuple(_getAttribAsStr(rec[0],att) for att in attribs)
220-
b.append(rec[:4] + (bestAvg(0,True),bestAvg(1,False),bestAvg(2,False))+attribValues)
224+
b.append(rec[:-1] + (bestAvg(0,True),bestAvg(1,False),bestAvg(2,False),rec[-1])+attribValues)
221225
if doPrint:
222-
if oldColumns:
226+
if useOldColumns:
223227
headers=["","repLen","repn","steps","objective","trainAcc","testAcc","10StepTime"]+attribs
224228
else:
225229
headers=["", "steps","objective","trainAcc","testAcc","10StepTime"]+attribs

0 commit comments

Comments
 (0)