libweightedit.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # Copyright (C) 2005, 2006 Frank Michler, Philipps-University Marburg, Germany
  2. # This program is free software; you can redistribute it and/or
  3. # modify it under the terms of the GNU General Public License
  4. # as published by the Free Software Foundation; either version 2
  5. # of the License, or (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program; if not, write to the Free Software
  12. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. proc TestLibWeightEdit {} {
  14. puts "LibWeightEdit, Version 0.01"
  15. }
  16. proc ReadCSimWeightFile {WeightFileName} {
  17. puts $WeightFileName
  18. set fh [open $WeightFileName "r"]
  19. set tt 0
  20. fconfigure $fh -encoding binary -translation binary
  21. binary scan [read $fh 12] "iii" NSource SNx SNy
  22. binary scan [read $fh 12] "iii" NTarget TNx TNy
  23. binary scan [read $fh 12] "iii" M MaximumDelay MaxNPre
  24. array set WeightStructure {}
  25. set WeightStructure("NSource") $NSource
  26. set WeightStructure("SNx") $SNx
  27. set WeightStructure("SNy") $SNy
  28. set WeightStructure("NTarget") $NTarget
  29. set WeightStructure("TNx") $TNx
  30. set WeightStructure("TNy") $TNy
  31. set Post {}
  32. for {set i 0} {$i < $NSource} {incr i} {
  33. binary scan [read $fh [expr $M*4]] "i$M" tmprow
  34. lappend Post $tmprow
  35. }
  36. set Weights {}
  37. for {set i 0} {$i < $NSource} {incr i} {
  38. binary scan [read $fh [expr $M*4]] "f$M" tmprow
  39. lappend Weights $tmprow
  40. }
  41. set Delays_Length {}
  42. for {set i 0} {$i < $NSource} {incr i} {
  43. binary scan [read $fh [expr $MaximumDelay*2]] "s$MaximumDelay" tmprow
  44. lappend Delays_Length $tmprow
  45. }
  46. set Delays {}
  47. for {set i 0} {$i < $NSource} {incr i} {
  48. set SecondTmp {}
  49. for {set j 0} {$j < $MaximumDelay} {incr j} {
  50. binary scan [read $fh [expr $M*2]] "s$M" tmprow
  51. lappend SecondTmp $tmprow
  52. }
  53. lappend Delays $SecondTmp
  54. }
  55. # puts $Delays
  56. close $fh
  57. #calculate 2d weight and delay matrices
  58. set DelayMatrix2D [MakeList2D $NSource $NTarget $::None]
  59. set WeightMatrix2D [MakeList2D $NSource $NTarget $::None]
  60. for {set i 0} {$i < $NSource} {incr i} {
  61. for {set j 0} {$j < $MaximumDelay} {incr j} {
  62. for {set k 0} {$k < [lindex $Delays_Length $i $j]} {incr k} {
  63. lset DelayMatrix2D $i [lindex $Post $i [lindex $Delays $i $j $k]] $j
  64. lset WeightMatrix2D $i [lindex $Post $i [lindex $Delays $i $j $k]] [lindex $Weights $i [lindex $Delays $i $j $k]]
  65. }
  66. }
  67. }
  68. set WeightStructure("DelayMatrix") $DelayMatrix2D
  69. set WeightStructure("WeightMatrix") $WeightMatrix2D
  70. return [array get WeightStructure]
  71. }
  72. proc WriteCSimWeightFile {WeightFileArr {WeightFileName "editweight.dat"}} {
  73. # puts $WeightFileName
  74. array set WeightFile $WeightFileArr
  75. set ns $WeightFile("NSource")
  76. set nt $WeightFile("NTarget")
  77. set weights $WeightFile("WeightMatrix")
  78. set dmatrix $WeightFile("DelayMatrix")
  79. # calc Post array
  80. set M 0
  81. set MaxNPre 0
  82. set MaximumDelay 0
  83. set NPost {}
  84. array set NPre {}
  85. for {set t 0} {$t < $nt} {incr t} {
  86. set NPre($t) 0
  87. }
  88. for {set s 0} {$s < $ns} {incr s} {
  89. set CurNPost 0
  90. for {set t 0} {$t < $nt} {incr t} {
  91. if {[lindex $weights $s $t] != $::None} {
  92. incr CurNPost
  93. if {[lindex $dmatrix $s $t] > $MaximumDelay} {
  94. set MaximumDelay [lindex $dmatrix $s $t]
  95. }
  96. incr NPre($t)
  97. }
  98. }
  99. lappend NPost $CurNPost
  100. if {$CurNPost > $M} {set M $CurNPost}
  101. }
  102. for {set t 0} {$t < $nt} {incr t} {
  103. if {$NPre($t) > $MaxNPre} {
  104. set MaxNPre $NPre($t)
  105. }
  106. }
  107. incr MaximumDelay
  108. # puts "M= $M MaximumDelay=$MaximumDelay MaxNPre=$MaxNPre"
  109. set delays [MakeList3D $ns $MaximumDelay $M]
  110. set delays_length [MakeList2D $ns $MaximumDelay]
  111. set Post [MakeList2D $ns $M]
  112. for {set s 0} {$s < $ns} {incr s} {
  113. set CurNPost 0
  114. for {set t 0} {$t < $nt} {incr t} {
  115. # puts "$s $t $CurNPost [lindex $dmatrix $s $t] [lindex $delays_length $s [lindex $dmatrix $s $t]]"
  116. if {[lindex $weights $s $t] != $::None} {
  117. lset Post $s $CurNPost $t
  118. lset delays $s [lindex $dmatrix $s $t] [lindex $delays_length $s [lindex $dmatrix $s $t]] $CurNPost
  119. incr CurNPost
  120. L2Dincr delays_length $s [lindex $dmatrix $s $t]
  121. }
  122. }
  123. }
  124. set fh [open $WeightFileName "w"]
  125. fconfigure $fh -encoding binary -translation binary
  126. puts -nonewline $fh [binary format "iii" $ns $WeightFile("SNx") $WeightFile("SNy")]
  127. puts -nonewline $fh [binary format "iii" $nt $WeightFile("TNx") $WeightFile("TNy")]
  128. puts -nonewline $fh [binary format "iii" $M $MaximumDelay $MaxNPre]
  129. #write Post
  130. for {set s 0} {$s < $ns} {incr s} {
  131. for {set m 0} {$m < $M} {incr m} {
  132. puts -nonewline $fh [binary format "i" [lindex $Post $s $m]]
  133. }
  134. }
  135. #write Weights
  136. for {set s 0} {$s < $ns} {incr s} {
  137. for {set m 0} {$m < $M} {incr m} {
  138. puts -nonewline $fh [binary format "f" [lindex $weights $s [lindex $Post $s $m]]]
  139. }
  140. }
  141. #write Delays_Length
  142. for {set s 0} {$s < $ns} {incr s} {
  143. for {set m 0} {$m < $MaximumDelay} {incr m} {
  144. puts -nonewline $fh [binary format "s" [lindex $delays_length $s $m]]
  145. }
  146. }
  147. #write Delays
  148. for {set s 0} {$s < $ns} {incr s} {
  149. for {set d 0} {$d < $MaximumDelay} {incr d} {
  150. for {set m 0} {$m < $M} {incr m} {
  151. puts -nonewline $fh [binary format "s" [lindex $delays $s $d $m]]
  152. }
  153. }
  154. }
  155. close $fh
  156. }
  157. set KillingList {}
  158. proc Destroy {} {
  159. foreach w $::KillingList {
  160. destroy $w
  161. }
  162. }
  163. proc WeightSelectionListBox {{ParentFrame {}}} {
  164. ConnectionNames
  165. set curframe ${ParentFrame}.weightsel
  166. frame $curframe
  167. pack $curframe
  168. frame ${curframe}.weightshow
  169. pack ${curframe}.weightshow
  170. set curframe1 ${curframe}.weightshow
  171. frame ${curframe1}.lbframe
  172. pack ${curframe1}.lbframe -side bottom
  173. set curframe3 ${curframe1}.lbframe
  174. set ::LBname ${curframe3}.lb
  175. set ::SBname ${curframe3}.sb
  176. listbox ${::LBname} -selectmode single -height 5 -listvariable ::ConnectionList -activestyle dotbox
  177. scrollbar ${::SBname} -command [list ${::LBname} yview]
  178. ${::LBname} configure -yscrollcommand [list ${::SBname} set]
  179. pack ${::SBname} ${::LBname} -in ${curframe3} -side right -expand 1 -fill both
  180. set ::LastWeightSelection 1
  181. ${::LBname} selection set 1
  182. bind ${::LBname} <<ListboxSelect>> "ShowWM1 $ParentFrame"
  183. }
  184. proc ShowWeightMatrix {{ParentFrame {}}} {
  185. set gframe ${ParentFrame}.gf
  186. frame $gframe
  187. pack $gframe
  188. lappend ::KillingList $gframe
  189. set ::SetVal 2
  190. # grid [button ${gframe}.setallweights -command "set ::vals(0,0) 42" -text "SetAllWeights"] -column 0 -row 0
  191. grid [button ${gframe}.setallweights -command "SetWholeArray2D ::vals \$::SetVal" -text "SetAllWeights"] -column 0 -row 0
  192. grid [button ${gframe}.setdiaweights -command "SetDiagonal2D ::vals \$::SetVal" -text "SetDiagonalWeights"] -column 0 -row 1
  193. grid [button ${gframe}.setalldelays -command "SetWholeArray2D ::vdelay \$::SetVal" -text "SetAllDelays"] -column 0 -row 2
  194. grid [button ${gframe}.setdiadelays -command "SetDiagonal2D ::vdelay \$::SetVal" -text "SetDiagonalDelays"] -column 0 -row 3
  195. # grid [button ${gframe}.clearall -command "ClearAllWeights" -text "ClearAllWeights"] -column 0 -row 4
  196. grid [button ${gframe}.dump -command "Save" -text "Save Weight File"] -column 0 -row 4
  197. # pack [button ${ParentFrame}.dump -command "Save" -text "Save Weight File"] -anchor sw
  198. # lappend ::KillingList ${ParentFrame}.setallweights
  199. grid [entry ${gframe}.settoval -width 7 -bg white -relief sunken -border 1 -highlightcolor green -textvariable ::SetVal] -column 1 -row 0
  200. set ::SetVal 0
  201. array set ::vals [List2DToArray2D $::WeightFile("WeightMatrix")]
  202. array set ::vdelay [List2DToArray2D $::WeightFile("DelayMatrix")]
  203. set nr_rows $::WeightFile("NSource")
  204. set nr_cols $::WeightFile("NTarget")
  205. set FileWithoutPath [regexp -inline "/\[^/]*dat$" $::CurWeightFile]
  206. label ${ParentFrame}.filenameLabel -text "Current Weight File: $FileWithoutPath"
  207. lappend ::KillingList ${ParentFrame}.filenameLabel
  208. pack ${ParentFrame}.filenameLabel -anchor sw
  209. # pack [button ${ParentFrame}.changewf -command "ShowWM1 $ParentFrame" -text $FileWithoutPath] -anchor sw
  210. # lappend ::KillingList ${ParentFrame}.changewf
  211. label ${ParentFrame}.weightLabel -text "Weights"
  212. lappend ::KillingList ${ParentFrame}.weightLabel
  213. pack ${ParentFrame}.weightLabel -anchor sw
  214. set WeightFrame ${ParentFrame}.weightframe
  215. frame $WeightFrame
  216. pack $WeightFrame
  217. lappend ::KillingList $WeightFrame
  218. set picpath "$::env(HOME)/prog/objsim/data/pics"
  219. pack [label ${WeightFrame}.gsourcelabel -image [image create photo -file $picpath/source.gif]] -side left
  220. pack [label ${WeightFrame}.gtargetlabel -image [image create photo -file $picpath/target.gif]] -side top
  221. set WeightGrid ${WeightFrame}.weightgridframe
  222. frame $WeightGrid
  223. pack $WeightGrid -side left
  224. lappend ::KillingList $WeightGrid
  225. # Beschriftung
  226. for {set i 0} {$i < $nr_rows} {incr i} {
  227. set mylab [label ${WeightGrid}.glaby${i} -text "$i"]
  228. grid $mylab -column 1 -row [expr $i+2]
  229. }
  230. for {set i 0} {$i < $nr_cols} {incr i} {
  231. set mylab [label ${WeightGrid}.glabx${i} -text "$i"]
  232. grid $mylab -column [expr $i+2] -row 1
  233. }
  234. for {set i 0} {$i < $nr_rows} {incr i} {
  235. for {set j 0} {$j < $nr_cols} {incr j} {
  236. set mybut [entry ${WeightGrid}.gbut${i}x$j -textvariable vals($i,$j) -width 5 -bg white -relief flat -border 5 -highlightcolor green]
  237. grid $mybut -column [expr $j+2] -row [expr $i+2]
  238. }
  239. }
  240. label ${ParentFrame}.delayLabel -text "Delays (max=119)"
  241. lappend ::KillingList ${ParentFrame}.delayLabel
  242. pack ${ParentFrame}.delayLabel -anchor sw
  243. set DelayFrame ${ParentFrame}.delayframe
  244. frame $DelayFrame
  245. pack $DelayFrame
  246. lappend ::KillingList $DelayFrame
  247. # puts "HOME= $env(HOME)"
  248. pack [label ${DelayFrame}.gsourcelabel -image [image create photo -file $picpath/source.gif]] -side left
  249. pack [label ${DelayFrame}.gtargetlabel -image [image create photo -file $picpath/target.gif]] -side top
  250. set DelayGrid ${DelayFrame}.delaygridframe
  251. frame $DelayGrid
  252. pack $DelayGrid
  253. lappend ::KillingList $DelayGrid
  254. puts "rows: $nr_rows cols: $nr_cols"
  255. # Beschriftung
  256. for {set i 0} {$i < $nr_rows} {incr i} {
  257. set mylab [label ${DelayGrid}.glaby${i} -text "$i"]
  258. grid $mylab -column 1 -row [expr $i+2]
  259. }
  260. for {set i 0} {$i < $nr_cols} {incr i} {
  261. set mylab [label ${DelayGrid}.glabx${i} -text "$i"]
  262. grid $mylab -column [expr $i+2] -row 1
  263. }
  264. for {set i 0} {$i < $nr_rows} {incr i} {
  265. for {set j 0} {$j < $nr_cols} {incr j} {
  266. set mybut [entry ${DelayGrid}.gbut2${i}x$j -textvariable vdelay($i,$j) -width 5 -bg white -relief flat -border 5 -highlightcolor green]
  267. grid $mybut -column [expr $j+2] -row [expr $i+2]
  268. }
  269. }
  270. # pack [button ${ParentFrame}.dump -command "Save" -text "Save Weight File"] -anchor sw
  271. pack [button ${ParentFrame}.clearall -command "ClearAllWeights" -text "ClearAllWeights"] -anchor sw
  272. lappend ::KillingList ${ParentFrame}.clearall
  273. }
  274. proc ClearAllWeights {} {
  275. foreach CurCon $::WeightFileList {
  276. set ::CurWeightFile $CurCon
  277. SetWholeArray2D ::vals 0
  278. SetWholeArray2D ::vdelay 0
  279. Save
  280. }
  281. }
  282. proc Save {} {
  283. set ::WeightFile("WeightMatrix") [Array2DToList2D [array get ::vals]]
  284. set ::WeightFile("DelayMatrix") [Array2DToList2D [array get ::vdelay]]
  285. WriteCSimWeightFile [array get ::WeightFile] $::CurWeightFile
  286. }
  287. proc NextWeightFile {} {
  288. incr ::CurWeightFileNr
  289. if {$::CurWeightFileNr < $::NWeightFiles} {
  290. } else {set ::CurWeightFileNr 0}
  291. set ::CurWeightFile [lindex $::WeightFileList $::CurWeightFileNr]
  292. set ::CurWeightFile "$::SimDataDir[lindex $::ConnectionList [ReadLbSelection ${::LBname}]]weights.dat"
  293. #[ReadLbSelection ${::LBname}]]
  294. #[lindex $::ConnectionList [ReadLbSelection ${::LBname}]]weights.dat"
  295. puts "curweightfile= $::CurWeightFile"
  296. }
  297. proc ShowWM1 {{ParentFrame {}}} {
  298. NextWeightFile
  299. array set ::WeightFile [ReadCSimWeightFile "$::CurWeightFile"]
  300. Destroy
  301. ShowWeightMatrix $ParentFrame
  302. }