Skip to content

Commit

Permalink
key fix to permuted binary min dist: dist must be on flat subspace! o…
Browse files Browse the repository at this point in the history
…ther misc fixes and name rows etc.
  • Loading branch information
rcoreilly committed Dec 30, 2024
1 parent 461c6d9 commit 9431933
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 86 deletions.
30 changes: 29 additions & 1 deletion lab/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (fn *FileNode) PlotFiles() { //types:add
})
}

// PlotFile pulls up this file in a texteditor.
// PlotFile creates a plot of data.
func (fn *FileNode) PlotFile() {
ts := fn.Tabber()
if ts == nil {
Expand All @@ -267,6 +267,30 @@ func (fn *FileNode) PlotFile() {
ts.AsLab().PlotTable(ptab, dt)
}

// todo: this is too redundant -- need a better soln

// GridFiles calls GridFile on selected files
func (fn *FileNode) GridFiles() { //types:add
fn.SelectedFunc(func(sn *filetree.Node) {
if sfn, ok := sn.This.(*FileNode); ok {
sfn.GridFile()
}
})
}

// GridFile creates a grid view of data.
func (fn *FileNode) GridFile() {
ts := fn.Tabber()
if ts == nil {
return
}
d := TensorFS(fn.AsNode())
if d != nil {
ts.AsLab().GridTensorFS(d)
return
}
}

// DiffDirs displays a browser with differences between two selected directories
func (fn *FileNode) DiffDirs() { //types:add
var da, db *filetree.Node
Expand Down Expand Up @@ -315,6 +339,10 @@ func (fn *FileNode) ContextMenu(m *core.Scene) {
Styler(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Cat != fileinfo.Data, states.Disabled)
})
core.NewFuncButton(m).SetFunc(fn.GridFiles).SetText("Grid").SetIcon(icons.Edit).
Styler(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Cat != fileinfo.Data, states.Disabled)
})
core.NewFuncButton(m).SetFunc(fn.DiffDirs).SetText("Diff Dirs").SetIcon(icons.Edit).
Styler(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || !fn.IsDir(), states.Disabled)
Expand Down
11 changes: 11 additions & 0 deletions lab/tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ func (ts *Tabs) TensorGrid(label string, tsr tensor.Tensor) *tensorcore.TensorGr
return tv
}

// GridTensorFS recycles a tab with a Grid of given [tensorfs.Node].
func (ts *Tabs) GridTensorFS(dfs *tensorfs.Node) *tensorcore.TensorGrid {
label := fsx.DirAndFile(dfs.Path()) + " Grid"
if dfs.IsDir() {
core.MessageSnackbar(ts, "Use Edit instead of Grid to view a directory")
return nil
}
tsr := dfs.Tensor
return ts.TensorGrid(label, tsr)
}

// PlotTable recycles a tab with a Plot of given table.Table.
func (ts *Tabs) PlotTable(label string, dt *table.Table) *plotcore.PlotEditor {
pl := NewTab(ts, label, func(tab *core.Frame) *plotcore.PlotEditor {
Expand Down
2 changes: 1 addition & 1 deletion lab/typegen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions patterns/patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,16 @@ func TestABAC(t *testing.T) {
// fmt.Println(mix)
assert.Equal(t, exmix, mix.String())
}

func TestNameRows(t *testing.T) {
dir, _ := tensorfs.NewDir("test")
nr := dir.StringValue("Name", 12)
NameRows(nr, "AB_", 2)
// fmt.Println(nr)

exnm := `Name [12] AB_00 AB_01 AB_02 AB_03 AB_04 AB_05 AB_06 AB_07 AB_08 AB_09 AB_10
AB_11
`

assert.Equal(t, exnm, nr.String())
}
2 changes: 1 addition & 1 deletion patterns/permuted.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func PermutedBinaryMinDiff(tsr tensor.Values, nOn int, onVal, offVal float64, mi
r1v := tsr.SubSpace(r1)
for r2 := r1 + 1; r2 < rows; r2++ {
r2v := tsr.SubSpace(r2)
dst := metric.Hamming(r1v, r2v).Float1D(0)
dst := metric.Hamming(tensor.As1D(r1v), tensor.As1D(r2v)).Float1D(0)
df := int(math.Round(float64(.5 * dst)))
if df < minDiff {
nunder[r1]++
Expand Down
10 changes: 10 additions & 0 deletions patterns/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ func PctActInTensor(trow tensor.Values) float32 {

// Note: AppendFrom can be used to concatenate tensors.

// NameRows sets strings as prefix + row number with given number
// of leading zeros.
func NameRows(tsr tensor.Values, prefix string, nzeros int) {
ft := fmt.Sprintf("%s%%0%dd", prefix, nzeros)
rows := tsr.DimSize(0)
for i := range rows {
tsr.SetString1D(fmt.Sprintf(ft, i), i)
}
}

// Shuffle returns a [tensor.Rows] view of the given source tensor
// with the outer row-wise dimension randomly shuffled (permuted).
func Shuffle(src tensor.Values) *tensor.Rows {
Expand Down
10 changes: 6 additions & 4 deletions tensor/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"slices"

"cogentcore.org/core/base/errors"
"cogentcore.org/core/base/metadata"
"cogentcore.org/core/base/num"
"cogentcore.org/core/base/reflectx"
Expand Down Expand Up @@ -306,23 +307,24 @@ func (tsr *Bool) CopyFrom(frm Values) {
// It uses and optimized implementation if the other tensor
// is of the same type, and otherwise it goes through
// appropriate standard type.
func (tsr *Bool) AppendFrom(frm Values) error {
func (tsr *Bool) AppendFrom(frm Values) Values {
rows, cell := tsr.shape.RowCellSize()
frows, fcell := frm.Shape().RowCellSize()
if cell != fcell {
return fmt.Errorf("tensor.AppendFrom: cell sizes do not match: %d != %d", cell, fcell)
errors.Log(fmt.Errorf("tensor.AppendFrom: cell sizes do not match: %d != %d", cell, fcell))
return tsr
}
tsr.SetNumRows(rows + frows)
st := rows * cell
fsz := frows * fcell
if fsm, ok := frm.(*Bool); ok {
copy(tsr.Values[st:st+fsz], fsm.Values)
return nil
return tsr
}
for i := 0; i < fsz; i++ {
tsr.Values.Set(Float64ToBool(frm.Float1D(i)), st+i)
}
return nil
return tsr
}

// CopyCellsFrom copies given range of values from other tensor into this tensor,
Expand Down
10 changes: 6 additions & 4 deletions tensor/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strconv"

"cogentcore.org/core/base/errors"
"cogentcore.org/core/base/num"
"cogentcore.org/core/base/reflectx"
)
Expand Down Expand Up @@ -281,23 +282,24 @@ func (tsr *Number[T]) CopyFrom(frm Values) {
// It uses and optimized implementation if the other tensor
// is of the same type, and otherwise it goes through
// appropriate standard type.
func (tsr *Number[T]) AppendFrom(frm Values) error {
func (tsr *Number[T]) AppendFrom(frm Values) Values {
rows, cell := tsr.shape.RowCellSize()
frows, fcell := frm.Shape().RowCellSize()
if cell != fcell {
return fmt.Errorf("tensor.AppendFrom: cell sizes do not match: %d != %d", cell, fcell)
errors.Log(fmt.Errorf("tensor.AppendFrom: cell sizes do not match: %d != %d", cell, fcell))
return tsr
}
tsr.SetNumRows(rows + frows)
st := rows * cell
fsz := frows * fcell
if fsm, ok := frm.(*Number[T]); ok {
copy(tsr.Values[st:st+fsz], fsm.Values)
return nil
return tsr
}
for i := 0; i < fsz; i++ {
tsr.Values[st+i] = T(frm.Float1D(i))
}
return nil
return tsr
}

// CopyCellsFrom copies given range of values from other tensor into this tensor,
Expand Down
9 changes: 5 additions & 4 deletions tensor/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,23 +214,24 @@ func (tsr *String) CopyFrom(frm Values) {
// It uses and optimized implementation if the other tensor
// is of the same type, and otherwise it goes through
// appropriate standard type.
func (tsr *String) AppendFrom(frm Values) error {
func (tsr *String) AppendFrom(frm Values) Values {
rows, cell := tsr.shape.RowCellSize()
frows, fcell := frm.Shape().RowCellSize()
if cell != fcell {
return fmt.Errorf("tensor.AppendFrom: cell sizes do not match: %d != %d", cell, fcell)
errors.Log(fmt.Errorf("tensor.AppendFrom: cell sizes do not match: %d != %d", cell, fcell))
return tsr
}
tsr.SetNumRows(rows + frows)
st := rows * cell
fsz := frows * fcell
if fsm, ok := frm.(*String); ok {
copy(tsr.Values[st:st+fsz], fsm.Values)
return nil
return tsr
}
for i := 0; i < fsz; i++ {
tsr.Values[st+i] = Float64ToString(frm.Float1D(i))
}
return nil
return tsr
}

// CopyCellsFrom copies given range of values from other tensor into this tensor,
Expand Down
2 changes: 1 addition & 1 deletion tensor/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Values interface {
// AppendFrom appends all values from other tensor into this tensor, with an
// optimized implementation if the other tensor is of the same type, and
// otherwise it goes through the appropriate standard type (Float, Int, String).
AppendFrom(from Values) error
AppendFrom(from Values) Values
}

// New returns a new n-dimensional tensor of given value type
Expand Down
72 changes: 5 additions & 67 deletions yaegilab/gui/cogentcore_org-lab-lab.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions yaegilab/nogui/cogentcore_org-lab-tensor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion yaegilab/nogui/cogentcore_org-lab-tensorfs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9431933

Please sign in to comment.