aboutsummaryrefslogtreecommitdiff
path: root/nerv/init.lua
blob: 320987ec6bb4b5a2cc358ff18e6240a026a7214b (plain) (blame)
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
--- NERV: a Lua-based toolkit for high-performance deep learning.
-- This file contains misc utility functions of NERV and finally initializes
-- NERV by including `init.lua` of other basic modules.
-- @author Ted Yin <ted.sybil@gmail.com>
-- @module nerv

require 'libnerv'

--- Dummy function.
-- Display a friendly error message when user attempts to invoke a
-- non-implemented function.
function nerv.error_method_not_implemented()
    nerv.error("method not implemented");
end

function nerv.set_logfile(filename)
    nerv._logfile = io.open(filename, "w")
end

--- Format a string just like `sprintf` in C.
-- @param fmt the format string
-- @param ... args, the data to be formatted
-- @return the formatted string
function nerv.sprintf(fmt, ...)
    return string.format(fmt, ...)
end

--- Print a formatted string to stdout.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.printf(fmt, ...)
    local line = nerv.sprintf(fmt, ...)
    io.stderr:write(line)
    -- duplicate the all output to the log file, if set
    if nerv._logfile then
        nerv._logfile:write(line)
        nerv._logfile:flush()
    end
end

--- Raise an global error with the formatted message.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.error(fmt, ...)
    error(nerv.sprintf("[nerv] internal error: " .. fmt .. "\n", ...))
end

--- Print a notification message that begins with "info" and a timestamp.
-- Instead of using `nerv.printf`, normal users should use this to print any
-- notification information.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.info(fmt, ...)
    nerv.printf(
        string.format("(%s)[nerv] info: %s\n",
            os.date("%H:%M:%S %F"), fmt), ...)
end

--- Print a warning message that begins with "warning" and a timestamp.
-- Instead of using `nerv.printf`, normal users should use this to print any
-- warnings.
-- @param fmt the format string
-- @param ... args, the data to be formatted
function nerv.warning(fmt, ...)
    nerv.printf(
        string.format("(%s)[nerv] warning: %s\n",
            os.date("%H:%M:%S %F"), fmt), ...)
end

--- Create a class (Torch-compatible).
-- Use this to create a class in NERV.
-- @param tname the class name
-- @param parenttname the parent class name (from which it inherits)
-- @return the created class
function nerv.class(tname, parenttname)

   local function constructor(...)
      local self = {}
      nerv.setmetatable(self, tname)
      if self.__init then
         self:__init(...)
      end
      return self
   end

   local function factory()
      local self = {}
      nerv.setmetatable(self, tname)
      return self
   end

   local mt = nerv.newmetatable(tname, parenttname, constructor, nil, factory)
   local mpt
   if parenttname then
      mpt = nerv.getmetatable(parenttname)
   end
   return mt, mpt
end

function table.val_to_str(v)
    if "string" == type(v) then
        v = string.gsub(v, "\n", "\\n")
        if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
            return "'" .. v .. "'"
        end
        return '"' .. string.gsub(v,'"', '\\"') .. '"'
    else
        return "table" == type(v) and table.tostring(v) or
                    (("number" == type(v) or
                    "string" == type(v) or
                    "boolean" == type(v)) and tostring(v)) or
                    "nil" -- failed to serialize
    end
end

function table.key_to_str (k)
    if "string" == type(k) and string.match(k, "^[_%a][_%a%d]*$") then
        return k
    else
        return "[" .. table.val_to_str(k) .. "]"
    end
end

--- Get the string representation of a table, which can be executed as a valid
-- piece of Lua code.
-- @param tbl the table
-- @return the string representation which will result in a Lua table entity
-- when evaluated
function table.tostring(tbl)
    local result, done = {}, {}
    for k, v in ipairs(tbl) do
        table.insert(result, table.val_to_str(v))
        done[k] = true
    end
    for k, v in pairs(tbl) do
        if not done[k] then
            table.insert(result,
            table.key_to_str(k) .. "=" .. table.val_to_str(v))
        end
    end
    return "{" .. table.concat(result, ",") .. "}"
end

--- Get the class by name.
-- @param tname the name of the class
-- @return the class entity
function nerv.get_type(tname)
    return assert(loadstring("return " .. tname))()
end

--- Check if the object is of the certain class.
-- @param obj the object ("class instance")
-- @param tname the class name ("type name")
function nerv.is_type(obj, tname)
    local mt0 = nerv.getmetatable(tname)
    local mt = getmetatable(obj)
    while mt do
        if mt == mt0 then
            return true
        end
        mt = getmetatable(mt)
    end
    return false
end

--- Strip last component from file name.
-- @param filename the path to a file
-- @return the path to the containing directory
function nerv.dirname(filename)
    if filename:match(".-/.-") then
        local name = string.gsub(filename, "(.*/)(.*)", "%1")
        return name
    else
        return ''
    end
end

--- Include a script file (chunk) into the current script.
-- An analogy to `#include` in C. Note that the effect is the same as executing
-- `dofile(filename)` at the current line.
-- @param filename the path to a file
-- @return all values returned by the chunk
function nerv.include(filename)
    local caller = debug.getinfo(2, "S").source:sub(2)
    return dofile(nerv.dirname(caller) .. filename)
end

--- Parse the command-line options and arguments
-- @param argv the argrument list to parsed
-- @param options The specification of options, should be a list of tables,
-- each one for exactly one available option, say `v`, with `v[1]`, `v[2]`,
-- `v[3]` indicating the full name of the option, the short form of the option
-- (when it is a boolean option) and the type of the value controlled by the
-- option. `default` and `desc` keys can also be specified to set the default
-- value and description of the option.
--
-- An example of specification:
-- {{"aaa", "a", "boolean", default = false, desc = "an option called aaa"},
-- {"bbb", "b", "boolean", default = true, desc = "bbb is set to be true if --bbb=no does not present"},
-- {"ccc", nil, "int", default = 0, desc = "ccc expects an integeral value"}}`
--
-- @return args, opts The non-option arguments and parsed options. `opts` is
-- again a list of tables, each of which corresponds to one table in parameter
-- `options`. The parsed value could be accessed by `opts["aaa"].val` (which is
-- `true` if "--aaa" or "-a" is specified).
function nerv.parse_args(argv, options, unordered)
    local is_opt_exp = "^[-](.*)$"
    local sim_opt_exp = "^[-]([a-z]+)$"
    local opt_exp = "^[-][-]([^=]+)$"
    local opt_with_val_exp = "^[-][-]([^=]+)=([^=]+)$"
    local opts = {}
    local sopts = {}
    local args = {}
    local arg_start = false
    local function err()
        nerv.error("invalid format of option specification")
    end
    for _, v in ipairs(options) do
        if type(v) ~= "table" or
            (v[1] == nil and v[2] == nil) or
            v[3] == nil then
            err()
        end
        local opt_full = v[1]
        local opt_short = v[2]
        local opt_type = v[3]
        local opt_meta = {type = opt_type,
                            desc = v.desc or "",
                            val = v.default,
                            specified = false}
        if opt_short ~= nil then
            if type(opt_short) ~= "string" or #opt_short ~= 1 then err() end
            if opt_type ~= "boolean" then
                nerv.error("only boolean option could have short form")
            end
            sopts[opt_short] = opt_meta
        end
        if opt_full ~= nil then
            if type(opt_full) ~= "string" then err() end
            opts[opt_full] = opt_meta
        end
    end
    for _, token in ipairs(argv) do
        if ((not arg_start) or unordered) and token:match(is_opt_exp) then
            local k = token:match(sim_opt_exp)
            if k then
                for c in k:gmatch"." do
                    if sopts[c] then
                        sopts[c].val = true
                        sopts[c].specified = true
                    else
                        nerv.error("invalid option -%s", c)
                    end
                end
            else
                local k = token:match(opt_exp)
                if k then
                    if opts[k] == nil then
                        nerv.error("invalid option %s", token)
                    end
                    if opts[k].type ~= "boolean" then
                        nerv.error("invalid option --%s: " ..
                                    "a %s value needs to be specified",
                                    k, opts[k].type)
                    else
                        opts[k].val = true
                        opts[k].specified = true
                    end
                else
                    local k, v = token:match(opt_with_val_exp)
                    if k then
                        if opts[k] == nil then
                            nerv.error("invalid option %s", token)
                        end
                        opts[k].specified = true
                        if opts[k].type == "boolean" then
                            if v == "yes" then
                                opts[k].val = true
                            elseif v == "no" then
                                opts[k].val = false
                            else
                                nerv.error("boolean value should be \"yes\" or \"no\"")
                            end
                        elseif opts[k].type == "int" then
                            local t = tonumber(v)
                            opts[k].val = t
                            if t == nil or math.floor(t) ~= t then
                                nerv.error("int value is expected")
                            end
                        elseif opts[k].type == "number" then
                            local t = tonumber(v)
                            opts[k].val = t
                            if t == nil then
                                nerv.error("numeric value is expected")
                            end
                        elseif opts[k].type == "string" then
                            opts[k].val = v
                        else
                            nerv.error("unrecognized type %s", opts[k].type)
                        end
                    else
                        nerv.error("unrecognized option %s", token)
                    end
                end
            end
        else
            table.insert(args, token)
            arg_start = true
        end
    end
    return args, opts
end

--- Print usage information of the command-line options
-- @param options the list of options used in `parse_args`
function nerv.print_usage(options)
    local full_maxlen = 0
    local type_maxlen = 0
    local default_maxlen = 0
    for _, v in ipairs(options) do
        local opt_full = v[1]
        local opt_short = v[2]
        local opt_type = v[3]
        full_maxlen = math.max(full_maxlen, #opt_full or 0)
        type_maxlen = math.max(full_maxlen, #opt_type or 0)
        default_maxlen = math.max(full_maxlen, #tostring(v.default) or 0)
    end
    local function pattern_gen()
        return string.format("\t%%-%ds\t%%-2s\t%%-%ds\t%%-%ds\t%%s\n",
                            full_maxlen, type_maxlen, default_maxlen)
    end
    nerv.printf("\n")
    nerv.printf(pattern_gen(), "Option", "Abbr.", "Type", "Default", "Desc.")
    for _, v in ipairs(options) do
        local opt_full = v[1]
        local opt_short = v[2]
        local opt_type = v[3]
        nerv.printf(pattern_gen(),
                    (