123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- # Copyright (C) 2005, 2006 Frank Michler, Philipps-University Marburg, Germany
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- proc TestLibWeightEdit {} {
- puts "LibWeightEdit, Version 0.01"
- }
- proc ReadCSimWeightFile {WeightFileName} {
- puts $WeightFileName
- set fh [open $WeightFileName "r"]
- set tt 0
- fconfigure $fh -encoding binary -translation binary
- binary scan [read $fh 12] "iii" NSource SNx SNy
- binary scan [read $fh 12] "iii" NTarget TNx TNy
- binary scan [read $fh 12] "iii" M MaximumDelay MaxNPre
-
- array set WeightStructure {}
- set WeightStructure("NSource") $NSource
- set WeightStructure("SNx") $SNx
- set WeightStructure("SNy") $SNy
- set WeightStructure("NTarget") $NTarget
- set WeightStructure("TNx") $TNx
- set WeightStructure("TNy") $TNy
-
- set Post {}
- for {set i 0} {$i < $NSource} {incr i} {
- binary scan [read $fh [expr $M*4]] "i$M" tmprow
- lappend Post $tmprow
- }
- set Weights {}
- for {set i 0} {$i < $NSource} {incr i} {
- binary scan [read $fh [expr $M*4]] "f$M" tmprow
- lappend Weights $tmprow
- }
- set Delays_Length {}
- for {set i 0} {$i < $NSource} {incr i} {
- binary scan [read $fh [expr $MaximumDelay*2]] "s$MaximumDelay" tmprow
- lappend Delays_Length $tmprow
- }
- set Delays {}
- for {set i 0} {$i < $NSource} {incr i} {
- set SecondTmp {}
- for {set j 0} {$j < $MaximumDelay} {incr j} {
- binary scan [read $fh [expr $M*2]] "s$M" tmprow
- lappend SecondTmp $tmprow
- }
- lappend Delays $SecondTmp
- }
- # puts $Delays
- close $fh
- #calculate 2d weight and delay matrices
- set DelayMatrix2D [MakeList2D $NSource $NTarget $::None]
- set WeightMatrix2D [MakeList2D $NSource $NTarget $::None]
- for {set i 0} {$i < $NSource} {incr i} {
- for {set j 0} {$j < $MaximumDelay} {incr j} {
- for {set k 0} {$k < [lindex $Delays_Length $i $j]} {incr k} {
- lset DelayMatrix2D $i [lindex $Post $i [lindex $Delays $i $j $k]] $j
- lset WeightMatrix2D $i [lindex $Post $i [lindex $Delays $i $j $k]] [lindex $Weights $i [lindex $Delays $i $j $k]]
- }
- }
- }
-
- set WeightStructure("DelayMatrix") $DelayMatrix2D
- set WeightStructure("WeightMatrix") $WeightMatrix2D
- return [array get WeightStructure]
- }
- proc WriteCSimWeightFile {WeightFileArr {WeightFileName "editweight.dat"}} {
- # puts $WeightFileName
- array set WeightFile $WeightFileArr
- set ns $WeightFile("NSource")
- set nt $WeightFile("NTarget")
- set weights $WeightFile("WeightMatrix")
- set dmatrix $WeightFile("DelayMatrix")
-
- # calc Post array
- set M 0
- set MaxNPre 0
- set MaximumDelay 0
- set NPost {}
- array set NPre {}
- for {set t 0} {$t < $nt} {incr t} {
- set NPre($t) 0
- }
- for {set s 0} {$s < $ns} {incr s} {
- set CurNPost 0
- for {set t 0} {$t < $nt} {incr t} {
- if {[lindex $weights $s $t] != $::None} {
- incr CurNPost
- if {[lindex $dmatrix $s $t] > $MaximumDelay} {
- set MaximumDelay [lindex $dmatrix $s $t]
- }
- incr NPre($t)
- }
- }
- lappend NPost $CurNPost
- if {$CurNPost > $M} {set M $CurNPost}
- }
- for {set t 0} {$t < $nt} {incr t} {
- if {$NPre($t) > $MaxNPre} {
- set MaxNPre $NPre($t)
- }
- }
- incr MaximumDelay
- # puts "M= $M MaximumDelay=$MaximumDelay MaxNPre=$MaxNPre"
- set delays [MakeList3D $ns $MaximumDelay $M]
- set delays_length [MakeList2D $ns $MaximumDelay]
- set Post [MakeList2D $ns $M]
- for {set s 0} {$s < $ns} {incr s} {
- set CurNPost 0
- for {set t 0} {$t < $nt} {incr t} {
- # puts "$s $t $CurNPost [lindex $dmatrix $s $t] [lindex $delays_length $s [lindex $dmatrix $s $t]]"
- if {[lindex $weights $s $t] != $::None} {
- lset Post $s $CurNPost $t
- lset delays $s [lindex $dmatrix $s $t] [lindex $delays_length $s [lindex $dmatrix $s $t]] $CurNPost
- incr CurNPost
- L2Dincr delays_length $s [lindex $dmatrix $s $t]
- }
- }
- }
- set fh [open $WeightFileName "w"]
- fconfigure $fh -encoding binary -translation binary
- puts -nonewline $fh [binary format "iii" $ns $WeightFile("SNx") $WeightFile("SNy")]
- puts -nonewline $fh [binary format "iii" $nt $WeightFile("TNx") $WeightFile("TNy")]
- puts -nonewline $fh [binary format "iii" $M $MaximumDelay $MaxNPre]
- #write Post
- for {set s 0} {$s < $ns} {incr s} {
- for {set m 0} {$m < $M} {incr m} {
- puts -nonewline $fh [binary format "i" [lindex $Post $s $m]]
- }
- }
- #write Weights
- for {set s 0} {$s < $ns} {incr s} {
- for {set m 0} {$m < $M} {incr m} {
- puts -nonewline $fh [binary format "f" [lindex $weights $s [lindex $Post $s $m]]]
- }
- }
- #write Delays_Length
- for {set s 0} {$s < $ns} {incr s} {
- for {set m 0} {$m < $MaximumDelay} {incr m} {
- puts -nonewline $fh [binary format "s" [lindex $delays_length $s $m]]
- }
- }
- #write Delays
- for {set s 0} {$s < $ns} {incr s} {
- for {set d 0} {$d < $MaximumDelay} {incr d} {
- for {set m 0} {$m < $M} {incr m} {
- puts -nonewline $fh [binary format "s" [lindex $delays $s $d $m]]
- }
- }
- }
- close $fh
- }
- set KillingList {}
- proc Destroy {} {
- foreach w $::KillingList {
- destroy $w
- }
- }
- proc WeightSelectionListBox {{ParentFrame {}}} {
- ConnectionNames
- set curframe ${ParentFrame}.weightsel
- frame $curframe
- pack $curframe
- frame ${curframe}.weightshow
- pack ${curframe}.weightshow
- set curframe1 ${curframe}.weightshow
-
- frame ${curframe1}.lbframe
- pack ${curframe1}.lbframe -side bottom
- set curframe3 ${curframe1}.lbframe
- set ::LBname ${curframe3}.lb
- set ::SBname ${curframe3}.sb
- listbox ${::LBname} -selectmode single -height 5 -listvariable ::ConnectionList -activestyle dotbox
- scrollbar ${::SBname} -command [list ${::LBname} yview]
- ${::LBname} configure -yscrollcommand [list ${::SBname} set]
- pack ${::SBname} ${::LBname} -in ${curframe3} -side right -expand 1 -fill both
-
- set ::LastWeightSelection 1
- ${::LBname} selection set 1
- bind ${::LBname} <<ListboxSelect>> "ShowWM1 $ParentFrame"
- }
- proc ShowWeightMatrix {{ParentFrame {}}} {
- set gframe ${ParentFrame}.gf
- frame $gframe
- pack $gframe
- lappend ::KillingList $gframe
-
- set ::SetVal 2
- # grid [button ${gframe}.setallweights -command "set ::vals(0,0) 42" -text "SetAllWeights"] -column 0 -row 0
- grid [button ${gframe}.setallweights -command "SetWholeArray2D ::vals \$::SetVal" -text "SetAllWeights"] -column 0 -row 0
- grid [button ${gframe}.setdiaweights -command "SetDiagonal2D ::vals \$::SetVal" -text "SetDiagonalWeights"] -column 0 -row 1
- grid [button ${gframe}.setalldelays -command "SetWholeArray2D ::vdelay \$::SetVal" -text "SetAllDelays"] -column 0 -row 2
- grid [button ${gframe}.setdiadelays -command "SetDiagonal2D ::vdelay \$::SetVal" -text "SetDiagonalDelays"] -column 0 -row 3
- # grid [button ${gframe}.clearall -command "ClearAllWeights" -text "ClearAllWeights"] -column 0 -row 4
- grid [button ${gframe}.dump -command "Save" -text "Save Weight File"] -column 0 -row 4
- # pack [button ${ParentFrame}.dump -command "Save" -text "Save Weight File"] -anchor sw
- # lappend ::KillingList ${ParentFrame}.setallweights
- grid [entry ${gframe}.settoval -width 7 -bg white -relief sunken -border 1 -highlightcolor green -textvariable ::SetVal] -column 1 -row 0
- set ::SetVal 0
- array set ::vals [List2DToArray2D $::WeightFile("WeightMatrix")]
- array set ::vdelay [List2DToArray2D $::WeightFile("DelayMatrix")]
- set nr_rows $::WeightFile("NSource")
- set nr_cols $::WeightFile("NTarget")
- set FileWithoutPath [regexp -inline "/\[^/]*dat$" $::CurWeightFile]
- label ${ParentFrame}.filenameLabel -text "Current Weight File: $FileWithoutPath"
- lappend ::KillingList ${ParentFrame}.filenameLabel
- pack ${ParentFrame}.filenameLabel -anchor sw
- # pack [button ${ParentFrame}.changewf -command "ShowWM1 $ParentFrame" -text $FileWithoutPath] -anchor sw
- # lappend ::KillingList ${ParentFrame}.changewf
- label ${ParentFrame}.weightLabel -text "Weights"
- lappend ::KillingList ${ParentFrame}.weightLabel
- pack ${ParentFrame}.weightLabel -anchor sw
- set WeightFrame ${ParentFrame}.weightframe
- frame $WeightFrame
- pack $WeightFrame
- lappend ::KillingList $WeightFrame
- set picpath "$::env(HOME)/prog/objsim/data/pics"
- pack [label ${WeightFrame}.gsourcelabel -image [image create photo -file $picpath/source.gif]] -side left
- pack [label ${WeightFrame}.gtargetlabel -image [image create photo -file $picpath/target.gif]] -side top
- set WeightGrid ${WeightFrame}.weightgridframe
- frame $WeightGrid
- pack $WeightGrid -side left
- lappend ::KillingList $WeightGrid
- # Beschriftung
- for {set i 0} {$i < $nr_rows} {incr i} {
- set mylab [label ${WeightGrid}.glaby${i} -text "$i"]
- grid $mylab -column 1 -row [expr $i+2]
- }
- for {set i 0} {$i < $nr_cols} {incr i} {
- set mylab [label ${WeightGrid}.glabx${i} -text "$i"]
- grid $mylab -column [expr $i+2] -row 1
- }
- for {set i 0} {$i < $nr_rows} {incr i} {
- for {set j 0} {$j < $nr_cols} {incr j} {
- set mybut [entry ${WeightGrid}.gbut${i}x$j -textvariable vals($i,$j) -width 5 -bg white -relief flat -border 5 -highlightcolor green]
- grid $mybut -column [expr $j+2] -row [expr $i+2]
- }
- }
-
- label ${ParentFrame}.delayLabel -text "Delays (max=119)"
- lappend ::KillingList ${ParentFrame}.delayLabel
- pack ${ParentFrame}.delayLabel -anchor sw
- set DelayFrame ${ParentFrame}.delayframe
- frame $DelayFrame
- pack $DelayFrame
- lappend ::KillingList $DelayFrame
-
- # puts "HOME= $env(HOME)"
- pack [label ${DelayFrame}.gsourcelabel -image [image create photo -file $picpath/source.gif]] -side left
- pack [label ${DelayFrame}.gtargetlabel -image [image create photo -file $picpath/target.gif]] -side top
- set DelayGrid ${DelayFrame}.delaygridframe
- frame $DelayGrid
- pack $DelayGrid
- lappend ::KillingList $DelayGrid
- puts "rows: $nr_rows cols: $nr_cols"
- # Beschriftung
- for {set i 0} {$i < $nr_rows} {incr i} {
- set mylab [label ${DelayGrid}.glaby${i} -text "$i"]
- grid $mylab -column 1 -row [expr $i+2]
- }
- for {set i 0} {$i < $nr_cols} {incr i} {
- set mylab [label ${DelayGrid}.glabx${i} -text "$i"]
- grid $mylab -column [expr $i+2] -row 1
- }
- for {set i 0} {$i < $nr_rows} {incr i} {
- for {set j 0} {$j < $nr_cols} {incr j} {
- set mybut [entry ${DelayGrid}.gbut2${i}x$j -textvariable vdelay($i,$j) -width 5 -bg white -relief flat -border 5 -highlightcolor green]
- grid $mybut -column [expr $j+2] -row [expr $i+2]
- }
- }
- # pack [button ${ParentFrame}.dump -command "Save" -text "Save Weight File"] -anchor sw
- pack [button ${ParentFrame}.clearall -command "ClearAllWeights" -text "ClearAllWeights"] -anchor sw
- lappend ::KillingList ${ParentFrame}.clearall
- }
- proc ClearAllWeights {} {
- foreach CurCon $::WeightFileList {
- set ::CurWeightFile $CurCon
- SetWholeArray2D ::vals 0
- SetWholeArray2D ::vdelay 0
- Save
- }
- }
- proc Save {} {
- set ::WeightFile("WeightMatrix") [Array2DToList2D [array get ::vals]]
- set ::WeightFile("DelayMatrix") [Array2DToList2D [array get ::vdelay]]
- WriteCSimWeightFile [array get ::WeightFile] $::CurWeightFile
- }
- proc NextWeightFile {} {
- incr ::CurWeightFileNr
- if {$::CurWeightFileNr < $::NWeightFiles} {
- } else {set ::CurWeightFileNr 0}
- set ::CurWeightFile [lindex $::WeightFileList $::CurWeightFileNr]
- set ::CurWeightFile "$::SimDataDir[lindex $::ConnectionList [ReadLbSelection ${::LBname}]]weights.dat"
- #[ReadLbSelection ${::LBname}]]
- #[lindex $::ConnectionList [ReadLbSelection ${::LBname}]]weights.dat"
- puts "curweightfile= $::CurWeightFile"
- }
- proc ShowWM1 {{ParentFrame {}}} {
- NextWeightFile
- array set ::WeightFile [ReadCSimWeightFile "$::CurWeightFile"]
- Destroy
- ShowWeightMatrix $ParentFrame
- }
|