weightedit.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #!/usr/bin/tclsh
  2. package require Tk
  3. set SimDataDir "$env(HOME)/data/sim/csim/basecircuits/"
  4. puts "$env(DISPLAY)"
  5. set SimDir "$env(HOME)/prog/objsim/basecircuits/"
  6. set SimName ${SimDir}simbasecircuits
  7. puts "SimName= $SimName"
  8. puts "DataDirectory= $SimDataDir"
  9. set FileName "mydump.dat"
  10. set WeightFileName "outputweight.dat"
  11. set WeightFileName2 "conExInputweights.dat"
  12. set WeightFileList "$WeightFileName $WeightFileName2"
  13. set FilePattern "$SimDataDir*weight*"
  14. puts $FilePattern
  15. set filelist [glob $FilePattern]
  16. puts "WeightFiles:"
  17. foreach f $filelist {
  18. puts $f
  19. }
  20. wm minsize . 350 400
  21. set WeightFileList $filelist
  22. set CurWeightFileNr 0
  23. set CurWeightFile [lindex $WeightFileList $CurWeightFileNr]
  24. set NWeightFiles [llength $WeightFileList]
  25. proc NextWeightFile {} {
  26. incr ::CurWeightFileNr
  27. if {$::CurWeightFileNr < $::NWeightFiles} {
  28. } else {set ::CurWeightFileNr 0}
  29. set ::CurWeightFile [lindex $::WeightFileList $::CurWeightFileNr]
  30. }
  31. set None -9999
  32. proc MakeList2D {x y {DefaultValue 0}} {
  33. set tmprow {}
  34. for {set i 0} {$i < $y} {incr i} {
  35. lappend tmprow $DefaultValue
  36. }
  37. set arr2d {}
  38. for {set j 0} {$j < $x} {incr j} {
  39. lappend arr2d $tmprow
  40. }
  41. return $arr2d
  42. }
  43. proc MakeList3D {x y z {DefaultValue 0}} {
  44. set tmprow [MakeList2D $y $z]
  45. set arr3d {}
  46. for {set j 0} {$j < $x} {incr j} {
  47. lappend arr3d $tmprow
  48. }
  49. return $arr3d
  50. }
  51. proc List2DToArray2D {OriList} {
  52. set i 0
  53. array set OutputArray {}
  54. foreach Row $OriList {
  55. set j 0
  56. foreach Elm $Row {
  57. set OutputArray($i,$j) $Elm
  58. incr j
  59. }
  60. incr i
  61. }
  62. set OutputArray(x) $i
  63. set OutputArray(y) $j
  64. return [array get OutputArray]
  65. }
  66. proc Array2DToList2D {ParaArray} {
  67. array set OriArray $ParaArray
  68. set OutputList {}
  69. for {set i 0} {$i < $OriArray(x)} {incr i} {
  70. set tmprow {}
  71. for {set j 0} {$j < $OriArray(y)} {incr j} {
  72. lappend tmprow $OriArray($i,$j)
  73. }
  74. lappend OutputList $tmprow
  75. }
  76. return $OutputList
  77. }
  78. proc L1Dincr {MyList x} {
  79. upvar $MyList UpList
  80. lset UpList $x [expr [lindex $UpList $x]+1]
  81. }
  82. proc L2Dincr {MyList x y} {
  83. upvar $MyList UpList
  84. lset UpList $x $y [expr [lindex $UpList $x $y]+1]
  85. }
  86. #nicht effizient, aber allgemein
  87. # evtl. mit case-Abfrage die Länge von args berücksichtigen
  88. proc lincr {MyList args} {
  89. upvar $MyList UpList
  90. eval lset [concat UpList $args [expr [eval lindex [concat [list $UpList] $args]] +1]]
  91. }
  92. proc ReadCSimWeightFile {WeightFileName} {
  93. puts $WeightFileName
  94. set fh [open $WeightFileName "r"]
  95. set tt 0
  96. fconfigure $fh -encoding binary -translation binary
  97. binary scan [read $fh 12] "iii" NSource SNx SNy
  98. binary scan [read $fh 12] "iii" NTarget TNx TNy
  99. binary scan [read $fh 12] "iii" M MaximumDelay MaxNPre
  100. array set WeightStructure {}
  101. set WeightStructure("NSource") $NSource
  102. set WeightStructure("SNx") $SNx
  103. set WeightStructure("SNy") $SNy
  104. set WeightStructure("NTarget") $NTarget
  105. set WeightStructure("TNx") $TNx
  106. set WeightStructure("TNy") $TNy
  107. set Post {}
  108. for {set i 0} {$i < $NSource} {incr i} {
  109. binary scan [read $fh [expr $M*4]] "i$M" tmprow
  110. lappend Post $tmprow
  111. }
  112. set Weights {}
  113. for {set i 0} {$i < $NSource} {incr i} {
  114. binary scan [read $fh [expr $M*4]] "f$M" tmprow
  115. lappend Weights $tmprow
  116. }
  117. set Delays_Length {}
  118. for {set i 0} {$i < $NSource} {incr i} {
  119. binary scan [read $fh [expr $MaximumDelay*2]] "s$MaximumDelay" tmprow
  120. lappend Delays_Length $tmprow
  121. }
  122. set Delays {}
  123. for {set i 0} {$i < $NSource} {incr i} {
  124. set SecondTmp {}
  125. for {set j 0} {$j < $MaximumDelay} {incr j} {
  126. binary scan [read $fh [expr $M*2]] "s$M" tmprow
  127. lappend SecondTmp $tmprow
  128. }
  129. lappend Delays $SecondTmp
  130. }
  131. # puts $Delays
  132. close $fh
  133. #calculate 2d weight and delay matrices
  134. set DelayMatrix2D [MakeList2D $NSource $NTarget $::None]
  135. set WeightMatrix2D [MakeList2D $NSource $NTarget $::None]
  136. for {set i 0} {$i < $NSource} {incr i} {
  137. for {set j 0} {$j < $MaximumDelay} {incr j} {
  138. for {set k 0} {$k < [lindex $Delays_Length $i $j]} {incr k} {
  139. lset DelayMatrix2D $i [lindex $Post $i [lindex $Delays $i $j $k]] $j
  140. lset WeightMatrix2D $i [lindex $Post $i [lindex $Delays $i $j $k]] [lindex $Weights $i [lindex $Delays $i $j $k]]
  141. }
  142. }
  143. }
  144. set WeightStructure("DelayMatrix") $DelayMatrix2D
  145. set WeightStructure("WeightMatrix") $WeightMatrix2D
  146. return [array get WeightStructure]
  147. }
  148. proc WriteCSimWeightFile {WeightFileArr {WeightFileName "editweight.dat"}} {
  149. # puts $WeightFileName
  150. array set WeightFile $WeightFileArr
  151. set ns $WeightFile("NSource")
  152. set nt $WeightFile("NTarget")
  153. set weights $WeightFile("WeightMatrix")
  154. set dmatrix $WeightFile("DelayMatrix")
  155. # calc Post array
  156. set M 0
  157. set MaxNPre 0
  158. set MaximumDelay 0
  159. set NPost {}
  160. array set NPre {}
  161. for {set t 0} {$t < $nt} {incr t} {
  162. set NPre($t) 0
  163. }
  164. for {set s 0} {$s < $ns} {incr s} {
  165. set CurNPost 0
  166. for {set t 0} {$t < $nt} {incr t} {
  167. if {[lindex $weights $s $t] != $::None} {
  168. incr CurNPost
  169. if {[lindex $dmatrix $s $t] > $MaximumDelay} {
  170. set MaximumDelay [lindex $dmatrix $s $t]
  171. }
  172. incr NPre($t)
  173. }
  174. }
  175. lappend NPost $CurNPost
  176. if {$CurNPost > $M} {set M $CurNPost}
  177. }
  178. for {set t 0} {$t < $nt} {incr t} {
  179. if {$NPre($t) > $MaxNPre} {
  180. set MaxNPre $NPre($t)
  181. }
  182. }
  183. incr MaximumDelay
  184. # puts "M= $M MaximumDelay=$MaximumDelay MaxNPre=$MaxNPre"
  185. set delays [MakeList3D $ns $MaximumDelay $M]
  186. set delays_length [MakeList2D $ns $MaximumDelay]
  187. set Post [MakeList2D $ns $M]
  188. for {set s 0} {$s < $ns} {incr s} {
  189. set CurNPost 0
  190. for {set t 0} {$t < $nt} {incr t} {
  191. # puts "$s $t $CurNPost [lindex $dmatrix $s $t] [lindex $delays_length $s [lindex $dmatrix $s $t]]"
  192. if {[lindex $weights $s $t] != $::None} {
  193. lset Post $s $CurNPost $t
  194. lset delays $s [lindex $dmatrix $s $t] [lindex $delays_length $s [lindex $dmatrix $s $t]] $CurNPost
  195. incr CurNPost
  196. L2Dincr delays_length $s [lindex $dmatrix $s $t]
  197. }
  198. }
  199. }
  200. set fh [open $WeightFileName "w"]
  201. fconfigure $fh -encoding binary -translation binary
  202. puts -nonewline $fh [binary format "iii" $ns $WeightFile("SNx") $WeightFile("SNy")]
  203. puts -nonewline $fh [binary format "iii" $nt $WeightFile("TNx") $WeightFile("TNy")]
  204. puts -nonewline $fh [binary format "iii" $M $MaximumDelay $MaxNPre]
  205. #write Post
  206. for {set s 0} {$s < $ns} {incr s} {
  207. for {set m 0} {$m < $M} {incr m} {
  208. puts -nonewline $fh [binary format "i" [lindex $Post $s $m]]
  209. }
  210. }
  211. #write Weights
  212. for {set s 0} {$s < $ns} {incr s} {
  213. for {set m 0} {$m < $M} {incr m} {
  214. puts -nonewline $fh [binary format "f" [lindex $weights $s [lindex $Post $s $m]]]
  215. }
  216. }
  217. #write Delays_Length
  218. for {set s 0} {$s < $ns} {incr s} {
  219. for {set m 0} {$m < $MaximumDelay} {incr m} {
  220. puts -nonewline $fh [binary format "s" [lindex $delays_length $s $m]]
  221. }
  222. }
  223. #write Delays
  224. for {set s 0} {$s < $ns} {incr s} {
  225. for {set d 0} {$d < $MaximumDelay} {incr d} {
  226. for {set m 0} {$m < $M} {incr m} {
  227. puts -nonewline $fh [binary format "s" [lindex $delays $s $d $m]]
  228. }
  229. }
  230. }
  231. close $fh
  232. }
  233. array set vals {}
  234. array set vdelay {}
  235. set KillingList {}
  236. proc Destroy {} {
  237. foreach w $::KillingList {
  238. destroy $w
  239. }
  240. }
  241. proc ShowWeightMatrix {} {
  242. Destroy
  243. array set ::vals [List2DToArray2D $::WeightFile("WeightMatrix")]
  244. array set ::vdelay [List2DToArray2D $::WeightFile("DelayMatrix")]
  245. set nr_rows $::WeightFile("NSource")
  246. set nr_cols $::WeightFile("NTarget")
  247. set FileWithoutPath [regexp -inline "/\[^/]*dat$" $::CurWeightFile]
  248. label .filenameLabel -text "Switch to next WeightFile"
  249. lappend ::KillingList .filenameLabel
  250. pack .filenameLabel -anchor sw
  251. pack [button .changewf -command "ShowWM1" -text $FileWithoutPath] -anchor sw
  252. lappend ::KillingList .changewf
  253. label .weightLabel -text "Weights"
  254. lappend ::KillingList .weightLabel
  255. pack .weightLabel -anchor sw
  256. for {set i 0} {$i < $nr_rows} {incr i} {
  257. frame .row$i
  258. set curframe .row$i
  259. lappend ::KillingList $curframe
  260. for {set j 0} {$j < $nr_cols} {incr j} {
  261. # set vals($i,$j) 0
  262. # set mybut [button ${curframe}.but${i}x$j -text "$i $j"]
  263. set mybut [entry ${curframe}.but${i}x$j -textvariable vals($i,$j) -width 7 -bg white -relief flat -border 5 -highlightcolor green]
  264. pack $mybut -side left
  265. }
  266. pack $curframe -side top
  267. }
  268. label .delayLabel -text "Delays (max=119)"
  269. lappend ::KillingList .delayLabel
  270. pack .delayLabel -anchor sw
  271. for {set i 0} {$i < $nr_rows} {incr i} {
  272. destroy .delayrow$i
  273. frame .delayrow$i
  274. set curframe .delayrow$i
  275. lappend ::KillingList $curframe
  276. for {set j 0} {$j < $nr_cols} {incr j} {
  277. # set vdelay($i,$j) 0
  278. # set mybut [button ${curframe}.but${i}x$j -text "$i $j"]
  279. set mybut [entry ${curframe}.but2${i}x$j -textvariable vdelay($i,$j) -width 7 -bg white -relief flat -border 5 -highlightcolor green]
  280. pack $mybut -side left
  281. }
  282. pack $curframe -side top
  283. }
  284. pack [button .dump -command "Save" -text "Save Weight File"] -anchor sw
  285. lappend ::KillingList .dump
  286. }
  287. proc ShowWM1 {} {
  288. NextWeightFile
  289. array set ::WeightFile [ReadCSimWeightFile "$::CurWeightFile"]
  290. ShowWeightMatrix
  291. }
  292. set NType 0
  293. set InputStrength 0.03
  294. proc Sim {} {
  295. puts $::SimName
  296. # open "$::SimName -L" "r+"
  297. set ::inout [open "|$::SimName -L --NeuronType $::NType --InputStrength $::InputStrength" "r+"]
  298. puts "Neuronentyp: $::NType"
  299. fconfigure $::inout
  300. # puts "Hallo"
  301. puts [read $::inout]
  302. ShowSim
  303. }
  304. # look at http://wiki.tcl.tk/1241
  305. proc OpenIDL {} {
  306. set ::idl [open "|idl-console-start" "w"]
  307. fconfigure $::idl -blocking 0 -buffering line -translation crlf -eofchar {}
  308. puts "OpenIDL"
  309. }
  310. proc ShowSim {} {
  311. set ShowOptions {}
  312. if {$::NType == 0} {
  313. append ShowOptions ",CutSpikes=$::CutSpikePot "
  314. }
  315. # set Command "csim_showsimspikes, dirname=\"$::SimDataDir\", /sec \n"
  316. # set Command "csim_showsimspikes, dirname=\"$::SimDataDir\", /sec $ShowOptions \n"
  317. set Command "gklearn_showsimspikes, /showspikes, showpotentials=1, dirname=\"$::SimDataDir\", /sec"
  318. puts $Command
  319. puts $::idl $Command
  320. }
  321. proc ResetIDL {} {
  322. puts $::idl "retall"
  323. puts $::idl "widget_control,/reset"
  324. puts $::idl "close,/all"
  325. puts $::idl "heap_gc,/verbose"
  326. # close $::idl
  327. # OpenIDL
  328. }
  329. proc RestartIDL {} {
  330. close $::idl
  331. OpenIDL
  332. }
  333. OpenIDL
  334. # pack [button .dummy -command "ReadIDL" -text "ReadIDL"]
  335. # pack [button .showsim -command "ShowSim" -text "ShowSim"]
  336. frame .but0 -width 1500 -height 100
  337. pack .but0
  338. set curframe .but0
  339. # pack [button ${curframe}.test -command "ShowWM1" -text "ChangeWeightFile"] -side left
  340. # pack [button ${curframe}.test2 -command "ShowWM1" -text "ChangeWeightFile"] -side left
  341. #pack [button ${curframe}.change -command "ShowWM1" -text "ChangeWeightFile"] -side left
  342. pack [button ${curframe}.sim -command "Sim" -text "Sim"] -side left
  343. pack [button ${curframe}.resetidl -command "ResetIDL" -text "ResetIDL"] -side left
  344. frame .modelchose
  345. pack .modelchose
  346. pack [radiobutton .modelchose.one -value 0 -text "Izhikevich-Neuron" -variable NType] -anchor sw
  347. pack [radiobutton .modelchose.two -value 1 -text "Marburg-Model-Neuron" -variable NType] -anchor sw
  348. #pack [checkbutton .modelchose.yrange
  349. set CutSpikePot 30
  350. label .modelchose.spcutlabel -text "CutSpikes="
  351. pack .modelchose.spcutlabel -anchor sw -side left
  352. set mybut [entry .modelchose.spcut -textvariable CutSpikePot -width 7 -bg white -relief flat -border 5 -highlightcolor green]
  353. pack $mybut -side left -side left
  354. label .modelchose.istrlabel -text "InputStrength="
  355. pack .modelchose.istrlabel -anchor sw -side left
  356. set mybut [entry .modelchose.setistrength -textvariable InputStrength -width 7 -bg white -relief flat -border 5 -highlightcolor green]
  357. pack $mybut -side left -side left
  358. ShowWM1
  359. frame .but
  360. pack .but
  361. frame .but2
  362. pack .but2
  363. proc Save {} {
  364. set ::WeightFile("WeightMatrix") [Array2DToList2D [array get ::vals]]
  365. set ::WeightFile("DelayMatrix") [Array2DToList2D [array get ::vdelay]]
  366. WriteCSimWeightFile [array get ::WeightFile] $::CurWeightFile
  367. }