[med-svn] [cnrun] 03/05: rework example1.lua
andrei zavada
hmmr-guest at moszumanska.debian.org
Wed Dec 28 17:57:00 UTC 2016
This is an automated email from the git hooks/post-receive script.
hmmr-guest pushed a commit to branch WIP
in repository cnrun.
commit 9ca4a6010fd2ebb9ae5bbd0d958b9df666324695
Author: Andrei Zavada <johnhommer at gmail.com>
Date: Wed Dec 28 02:14:22 2016 +0200
rework example1.lua
---
upstream/doc/examples/example1.lua | 360 +++++++++++++++++++------------------
1 file changed, 186 insertions(+), 174 deletions(-)
diff --git a/upstream/doc/examples/example1.lua b/upstream/doc/examples/example1.lua
index bf4900c..e991f44 100644
--- a/upstream/doc/examples/example1.lua
+++ b/upstream/doc/examples/example1.lua
@@ -26,53 +26,70 @@ local M = require("cnrun")
local res, ult, result
local C, model
-M.dump_available_units ()
+function Pw (str) print ("\027[01;1m" .. str .. "\027[00m") end
+function Pg (str) print ("\027[01;32m" .. str .. "\027[00m") end
+function Pr (str) print ("\027[01;31m" .. str .. "\027[00m") end
+function Pok ()
+ print ("\027[01;34m" .. "ok" .. "\027[00m")
+ print ()
+end
-res, ult = M.get_context ()
-if res == nil then
- print (ult)
- return
+function ASSERT (a1, a2)
+ res, ult = a1, a2
+ if res == nil then
+ Pr (ult)
+ os.exit (2)
+ end
+ Pok ()
+end
+
+function ASSERT_FALSE (a1, a2)
+ res, ult = a1, a2
+ if res ~= nil then
+ Pr (res)
+ Pr (ult)
+ os.exit (2)
+ end
+ Pg ("ok (" .. ult .. ")")
+ print ()
end
-C = ult
+
function print_units (model_name, ult, unit_regex)
- print ("There are " .. #ult .. " unit(s) matching \"" .. unit_regex .. "\"")
+ Pg ("There are " .. #ult .. " unit(s) matching \"" .. unit_regex .. "\"")
local unit_list = ult
local fmt = " %-10s %-16s %-16s %-12s %-16s %-6s"
- print (string.format(
- fmt,
- "label", "class", "family", "species", "has_sources", "is_altered"))
- print (string.rep('-', 87))
+ Pg (string.format(
+ fmt,
+ "label", "class", "family", "species", "has_sources", "is_altered"))
+ Pg (string.rep('-', 87))
for _, u in ipairs(unit_list) do
result = {M.get_unit_properties (C, model_name, u)}
res, ult = result[1], {effective_unpack(result, 2)}
local b = function (x) if x then return "yes" else return "no" end end
- print (string.format(
- fmt,
- ult[1], ult[2], ult[3], ult[4], b(ult[5]), b(ult[6])))
+ Pg (string.format(
+ fmt,
+ ult[1], ult[2], ult[3], ult[4], b(ult[5]), b(ult[6])))
end
print()
end
+
function compare_models (model_name1, model_name2)
local result1 = {M.get_units_matching(C, model_name1, ".*")}
local result2 = {M.get_units_matching(C, model_name2, ".*")}
res, ult1 = result[1], {effective_unpack(result1, 2)}
- if res == nil then
- print (ult1)
- return
- end
+ ASSERT (res, ult1)
res, ult2 = result[1], {effective_unpack(result2, 2)}
- if res == nil then
- print (ult2)
- return
- end
+ ASSERT (res, ult2)
+
local relevant_list1 = get_relevant_unit_params (model_name1, ult1)
local relevant_list2 = get_relevant_unit_params (model_name2, ult2)
return relevant_list1 == relevant_list2
end
+
function get_relevant_unit_params (model_name, unit_list)
local rup = {}
for _, u in ipairs(unit_list) do
@@ -84,201 +101,196 @@ function get_relevant_unit_params (model_name, unit_list)
end
+-- main
+
local mname = "FAFA"
-res, ult = M.new_model (C, mname)
-if res == nil then
- print (ult)
- return
+local verbosely = 3
+
+do
+ Pw ("Dump units")
+ M.dump_available_units ()
end
+
+Pw ("Obtain context")
+ASSERT (M.get_context ())
+C = ult
+
+Pw ("Create model")
+ASSERT (M.new_model (C, mname))
model = ult
-print ("Created model")
-local verbosely = 3
-print ("Setting verbosely to " .. verbosely)
M.set_model_parameter (C, mname, "verbosely", verbosely)
result = {M.list_models (C)}
-res, ult = result[1], {effective_unpack(result, 2)}
-if res == nil then
- print (ult)
- return
+ASSERT (result[1], {effective_unpack(result, 2)})
+
+do
+ Pg ("Model(s):")
+ local model_list = ult
+ Pg (table.concat(model_list))
+ print ()
end
-print ()
-print ("Model(s):")
-local model_list = ult
-print (table.concat(model_list))
-print ()
+ASSERT (M.import_nml (C, mname, "m.nml"))
-res, ult = M.import_nml (C, mname, "m.nml")
-if res == nil then
- print (ult)
- -- return
-end
-print ()
+do
+ Pw ("Testing NML export")
+ local mname2 = "FAFA2"
+ local f2 = "/tmp/m2.nml"
+ Pw ("Exporting to " .. f2)
+ ASSERT (
+ M.export_nml (C, mname, f2))
-print ("Testing NML export")
-local mname2 = "FAFA2"
-local f2 = "/tmp/m2.nml"
+ M.new_model (C, mname2)
+ Pw ("Importing into new model from " .. f2)
+ ASSERT (
+ M.import_nml (C, mname2, f2))
-print ("Exporting to " .. f2)
-res, ult = M.export_nml (C, mname, f2)
-if res == nil then
- print (ult)
- return
-end
+ Pw ("Comparing old and new models")
+ if compare_models (mname, mname2) then
+ print ()
+ Pg ("Models appear to be different:")
+ Pg ("Original model:")
+ print_units (mname, ult1, ".*")
+ Pg ("Reimported model:")
+ print_units (mname2, ult2, ".*")
+ return 1
+ end
+ Pg ("Models are pretty much the same")
+ print ()
-M.new_model (C, mname2)
-print ("Importing into new model from " .. f2)
-res, ult = M.import_nml (C, mname2, f2)
-if res == nil then
- print (ult)
- return
+ ASSERT (M.delete_model (C, mname2))
end
-print ()
-print ("Comparing old and new models")
-if compare_models (mname, mname2) then
+
+do
+ Pg ("Host parmeters:")
+ local parameters = {
+ "verbosely", "integration_dt_min",
+ "integration_dt_max", "integration_dt_cap",
+ "listen_dt", "listen_mode",
+ "sxf_start_delay", "sxf_period", "sdf_sigma"
+ }
+ local fmt = " %22s: %-q"
+ for i,p in ipairs(parameters) do
+ res, ult = M.get_model_parameter (C, mname, p)
+ Pg (string.format (fmt, p, ult))
+ end
print ()
- print ("Models appear to be different:")
- print ("Original model:")
- print_units (mname, ult1, ".*")
- print ("Reimported model:")
- print_units (mname2, ult2, ".*")
- return
end
-print ("Models are pretty much the same")
-print()
-M.delete_model (C, mname2)
-if res == nil then
- print (ult)
- return
+
+do
+ Pw ("Delete nonexistent model")
+ ASSERT_FALSE (M.delete_model (C, "fafa moo"))
end
-print ("Host parmeters:")
-local parameters = {
- "verbosely", "integration_dt_min",
- "integration_dt_max", "integration_dt_cap",
- "listen_dt", "listen_mode",
- "sxf_start_delay", "sxf_period", "sdf_sigma"
-}
-local fmt = " %22s: %-q"
-for i,p in ipairs(parameters) do
- res, ult = M.get_model_parameter (C, mname, p)
- print (string.format (fmt, p, ult))
+do
+ local regexp = "L.*"
+ Pw ("Get units matching \"" .. regexp .. "\"")
+ result = {M.get_units_matching(C, mname, regexp)}
+ ASSERT (
+ result[1], {effective_unpack(result, 2)})
+ print_units (mname, ult, "L.*")
end
-print ()
-res, ult = M.delete_model (C, "fafa moo")
-if res == nil then
- print (ult .. " (ignored)")
- -- return
+
+do
+ Pw ("Advance 10 sec")
+ ASSERT (M.advance (C, mname, 10000))
end
-result = {M.get_units_matching(C, mname, "L.*")}
-res, ult = result[1], {effective_unpack(result, 2)}
-if res == nil then
- print (ult)
- return
-end
+do
+ Pw ("Get parameter")
+ local u, p, v0, v9, vr = "LNz.0", "gNa"
+ ASSERT (M.get_unit_parameter (C, mname, u, p))
+ v0 = ult
-print_units (mname, ult, "L.*")
+ Pw ("Set new value")
+ ASSERT (M.set_unit_parameter (C, mname, u, p, v0 * 2))
+ Pw ("Get again to check")
+ ASSERT (M.get_unit_parameter (C, mname, u, p))
+ v9 = ult
-print ("Advancing 10 sec:")
-res, ult = M.advance (C, mname, 10000)
+ Pw ("Revert to stock")
+ ASSERT (M.revert_matching_unit_parameters (C, mname, u))
+ local count_reset = ult
+ ASSERT (M.get_unit_parameter (C, mname, u, p))
+ vr = ult
+ Pg (string.format(
+ ".. changed %s of %s from %g to %g, then reset (%d affected) to %g\n",
+ p, u, v0, v9, count_reset, vr))
+end
-print ("Modify parameter:")
-local u, p, v0, v9, vr = "LNz.0", "gNa"
-_, ult = M.get_unit_parameter (C, mname, u, p)
-v0 = ult
-_, ult = M.set_unit_parameter (C, mname, u, p, v0 * 2)
-_, ult = M.get_unit_parameter (C, mname, u, p)
-v9 = ult
--- with a revert
-res, ult = M.revert_matching_unit_parameters (C, mname, u)
-if res == nil then
- print (ult)
- return
+do
+ Pw ("Modify parameter in bulk:")
+ local us, ut, gsyn = "LNz.0", "LN1.0"
+ ASSERT (M.set_matching_synapse_parameter (C, mname, us, ut, "gsyn", 4.2))
+ Pg (string.format(
+ ".. changed gsyn of synapse connecting %s to %s, to %g\n",
+ us, ut, 4.2))
end
-local count_reset = ult
-_, ult = M.get_unit_parameter (C, mname, u, p)
-vr = ult
-print (string.format(
- ".. changed %s of %s from %g to %g, then reset (%d affected) to %g\n",
- p, u, v0, v9, count_reset, vr))
-
-
-print ("Modify parameter in bulk:")
-local us, ut, gsyn = "LNz.0", "LN1.0"
-_, ult = M.set_matching_synapse_parameter (C, mname, us, ut, "gsyn", 4.2)
-if res == nil then
- print (ult)
- return
+
+
+do
+ Pw ("Describe")
+ ASSERT (M.describe_model (C, mname))
end
-print (string.format(
- ".. changed gsyn of synapse connecting %s to %s, to %g\n",
- us, ut, 4.2))
-res, ult = M.describe_model (C, mname)
+do
+ Pw ("Advance, check state variables")
+ for i = 1, 6, 1 do
+ M.advance (C, mname, 1000)
+ result = {M.get_unit_vars (C, mname, "LNz.0")}
+ res, ult = result[1], {effective_unpack(result, 2)}
+ Pg (table.concat(ult, '; '))
+ end
+ print()
+end
-print ("State variables:")
-for i = 1, 6, 1 do
- M.advance (C, mname, 1000)
- result = {M.get_unit_vars (C, mname, "LNz.0")}
+
+do
+ Pw ("Putout")
+ local affected, remaining
+ result = {M.get_units_matching(C, mname, ".*")}
res, ult = result[1], {effective_unpack(result, 2)}
- print (table.concat(ult, '; '))
-end
-print()
-
-
-local affected, remaining
-print ("Putout:")
-result = {M.get_units_matching(C, mname, ".*")}
-res, ult = result[1], {effective_unpack(result, 2)}
-local unit_list = ult
-math.randomseed(os.time())
-local deleting = unit_list[math.random(1, #unit_list)]
--- deleting, _ = string.gsub(deleting, ".", "\\.")
-res, ult = M.putout (C, mname, deleting)
-if res == nil then
- print (ult)
- return
-end
-print (string.format(".. deleted unit %s", deleting))
-print()
-
-print ("Decimate:")
-res, ult = M.decimate (C, mname, "L.*", 0.3)
-if res == nil then
- print (nil)
- return
+ local unit_list = ult
+ math.randomseed(os.time())
+ local deleting = unit_list[math.random(1, #unit_list)]
+ -- deleting, _ = string.gsub(deleting, ".", "\\.")
+ ASSERT (M.putout (C, mname, deleting))
+ Pg (string.format(".. deleted unit %s", deleting))
+ print()
end
-affected, remaining = ult
-remaining = #{M.get_units_matching (C, mname, ".*")} - 1
-print (string.format(
+
+
+do
+ Pw ("Decimate")
+ local affected, remaining
+ ASSERT (M.decimate (C, mname, "L.*", 0.3))
+ affected, remaining = ult
+ remaining = #{M.get_units_matching (C, mname, ".*")} - 1
+ Pg (string.format(
".. %d units gone, %d remaining",
affected, remaining))
-print()
+ print()
+end
-res, ult = M.delete_model (C, mname)
-if res == nil then
- print ("Error: Failed to delete model: ", ult)
- return
-end
-print ("Model ".. ult .. " deleted")
+do
+ Pw ("Delete model")
+ ASSERT (M.delete_model (C, mname))
+ Pg ("Model ".. ult .. " deleted")
-res, ult = M.drop_context (C)
-if res == nil then
- print ("Error: Failed to drop context: ", ult)
- return
+ Pw ("Drop context")
+ ASSERT (M.drop_context (C))
end
-print ("Context dropped: " .. ult)
+
+print ("Test passed")
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/cnrun.git
More information about the debian-med-commit
mailing list