tensorcore

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2025 License: BSD-3-Clause Imports: 30 Imported by: 6

README

tensorcore

tensorcore provides GUI Views of table.Table and tensor.Tensor structures as Cogent Core Widgets.

Add this to import to get these views to be registered with the Cogent Core Value system:

	_ "cogentcore.org/lab/tensorcore" // include to get GUI views
  • Table provides a row-and-column tabular GUI interface, similar to a spreadsheet, for viewing and editing Table data. Any higher-dimensional tensor columns are shown as TensorGrid elements that can be clicked to open a TensorView editor with actual numeric values in a similar spreadsheet-like GUI.

  • TensorView provides a spreadsheet-like GUI for viewing and editing tensor data.

  • TensorGrid provides a 2D colored grid display of tensor data, collapsing any higher dimensions down to 2D. Different core.ColorMaps can be used to translate values into colors. Also supports labels for rows and / or columns. Use context menu to edit settings, and double-click to pull up values in a TensorView.

Documentation

Overview

Package tensorcore provides GUI Views of table Table and Tensor structures using the Cogent Core views framework.

* Table provides a row-and-column tabular GUI interface, similar to a spreadsheet, for viewing and editing Table data. Any higher-dimensional tensor columns are shown as TensorGrid elements that can be clicked to open a TensorView editor with actual numeric values in a similar spreadsheet- like GUI.

* TensorView provides a spreadsheet-like GUI for viewing and editing tensor data.

* TensorGrid provides a 2D colored grid display of tensor data, collapsing any higher dimensions down to 2D. Different core.ColorMaps can be used to translate values into colors.

Package tensorcore provides GUI Cogent Core widgets for tensor types.

Package tensorcore provides GUI Cogent Core widgets for tensor types.

Index

Constants

View Source
const LabelSpace = 8

LabelSpace is space after label in dot pixels.

Variables

This section is empty.

Functions

func AddGridStylerTo

func AddGridStylerTo(obj any, f func(s *GridStyle))

AddGridStylerTo adds the given [GridStyler] function into given object's metadata.

func RepeatsToBlank added in v0.1.2

func RepeatsToBlank(str []string) []string

RepeatsToBlank returns string slice with any sequentially repeated strings set to blank (empty string), which drives grouping in the TensorGrid labels.

func SetGridStylersTo

func SetGridStylersTo(obj any, st GridStylers)

SetGridStylersTo sets the GridStylers into given object's metadata.

Types

type GridStyle

type GridStyle struct {
	Layout

	// Range to plot
	Range minmax.Range64 `display:"inline"`

	// MinMax has the actual range of data, if not using fixed Range.
	MinMax minmax.F64 `display:"inline"`

	// ColorMap is the name of the color map to use in translating values to colors.
	ColorMap core.ColorMapName

	// GridFill sets proportion of grid square filled by the color block:
	// 1 = all, .5 = half, etc.
	GridFill float32 `min:"0.1" max:"1" step:"0.1" default:"0.9,1"`

	// DimExtra is the amount of extra space to add at dimension boundaries,
	// as a proportion of total grid size.
	DimExtra float32 `min:"0" max:"1" step:"0.02" default:"0.1,0.3"`

	// Size sets the minimum and maximum size for grid squares.
	Size minmax.F32 `display:"inline"`

	// TotalSize sets the total preferred display size along largest dimension.
	// Grid squares will be sized to fit within this size,
	// subject to the Size.Min / Max constraints, which have precedence.
	TotalSize float32

	// FontSize is the font size in standard Dp units for labels.
	FontSize float32

	// ColumnRotation is the rotation angle in degrees for column labels
	ColumnRotation float32 `default:"90"`
}

GridStyle are options for displaying tensors

func NewGridStyle

func NewGridStyle() *GridStyle

NewGridStyle returns a new GridStyle with defaults.

func (*GridStyle) ApplyStylersFrom

func (gs *GridStyle) ApplyStylersFrom(obj any)

func (*GridStyle) Defaults

func (gs *GridStyle) Defaults()

Defaults sets defaults for values that are at nonsensical initial values

func (*GridStyle) SetColorMap

func (t *GridStyle) SetColorMap(v core.ColorMapName) *GridStyle

SetColorMap sets the [GridStyle.ColorMap]: ColorMap is the name of the color map to use in translating values to colors.

func (*GridStyle) SetColumnRotation added in v0.1.2

func (t *GridStyle) SetColumnRotation(v float32) *GridStyle

SetColumnRotation sets the [GridStyle.ColumnRotation]: ColumnRotation is the rotation angle in degrees for column labels

func (*GridStyle) SetDimExtra

func (t *GridStyle) SetDimExtra(v float32) *GridStyle

SetDimExtra sets the [GridStyle.DimExtra]: DimExtra is the amount of extra space to add at dimension boundaries, as a proportion of total grid size.

func (*GridStyle) SetFontSize

func (t *GridStyle) SetFontSize(v float32) *GridStyle

SetFontSize sets the [GridStyle.FontSize]: FontSize is the font size in standard Dp units for labels.

func (*GridStyle) SetGridFill

func (t *GridStyle) SetGridFill(v float32) *GridStyle

SetGridFill sets the [GridStyle.GridFill]: GridFill sets proportion of grid square filled by the color block: 1 = all, .5 = half, etc.

func (*GridStyle) SetMinMax

func (t *GridStyle) SetMinMax(v minmax.F64) *GridStyle

SetMinMax sets the [GridStyle.MinMax]: MinMax has the actual range of data, if not using fixed Range.

func (*GridStyle) SetRange

func (t *GridStyle) SetRange(v minmax.Range64) *GridStyle

SetRange sets the [GridStyle.Range]: Range to plot

func (*GridStyle) SetSize

func (t *GridStyle) SetSize(v minmax.F32) *GridStyle

SetSize sets the [GridStyle.Size]: Size sets the minimum and maximum size for grid squares.

func (*GridStyle) SetTotalSize

func (t *GridStyle) SetTotalSize(v float32) *GridStyle

SetTotalSize sets the [GridStyle.TotalSize]: TotalSize sets the total preferred display size along largest dimension. Grid squares will be sized to fit within this size, subject to the Size.Min / Max constraints, which have precedence.

type GridStylers

type GridStylers []func(s *GridStyle)

GridStylers is a list of styling functions that set GridStyle properties. These are called in the order added.

func GetGridStylersFrom

func GetGridStylersFrom(obj any) GridStylers

GetGridStylersFrom returns GridStylers from given object's metadata. Returns nil if none or no metadata.

func (*GridStylers) Add

func (st *GridStylers) Add(f func(s *GridStyle))

Add Adds a styling function to the list.

func (*GridStylers) Run

func (st *GridStylers) Run(s *GridStyle)

Run runs the list of styling functions on given GridStyle object.

type Layout

type Layout struct {

	// OddRow means that even-numbered dimensions are displayed as Y*X rectangles.
	// This determines along which dimension to display any remaining
	// odd dimension: OddRow = true = organize vertically along row
	// dimension, false = organize horizontally across column dimension.
	OddRow bool

	// TopZero means that the Y=0 coordinate is displayed from the top-down;
	// otherwise the Y=0 coordinate is displayed from the bottom up,
	// which is typical for emergent network patterns.
	TopZero bool

	// Image will display the data as a bitmap image. If a 2D tensor, then it will
	// be a greyscale image. If a 3D tensor with size of either the first
	// or last dim = either 3 or 4, then it is a RGB(A) color image.
	Image bool
}

Layout are layout options for displaying tensors.

func (*Layout) SetImage

func (t *Layout) SetImage(v bool) *Layout

SetImage sets the [Layout.Image]: Image will display the data as a bitmap image. If a 2D tensor, then it will be a greyscale image. If a 3D tensor with size of either the first or last dim = either 3 or 4, then it is a RGB(A) color image.

func (*Layout) SetOddRow

func (t *Layout) SetOddRow(v bool) *Layout

SetOddRow sets the [Layout.OddRow]: OddRow means that even-numbered dimensions are displayed as Y*X rectangles. This determines along which dimension to display any remaining odd dimension: OddRow = true = organize vertically along row dimension, false = organize horizontally across column dimension.

func (*Layout) SetTopZero

func (t *Layout) SetTopZero(v bool) *Layout

SetTopZero sets the [Layout.TopZero]: TopZero means that the Y=0 coordinate is displayed from the top-down; otherwise the Y=0 coordinate is displayed from the bottom up, which is typical for emergent network patterns.

type Table

type Table struct {
	core.ListBase

	// Table is the table that we're a view of.
	Table *table.Table `set:"-"`

	// GridStyle has global grid display styles. GridStylers on the Table
	// are applied to this on top of defaults.
	GridStyle GridStyle `set:"-"`

	// ColumnGridStyle has per column grid display styles.
	ColumnGridStyle map[int]*GridStyle `set:"-"`

	// current sort index.
	SortIndex int

	// whether current sort order is descending.
	SortDescending bool
	// contains filtered or unexported fields
}

Table provides a GUI widget for representing table.Table values.

func NewTable

func NewTable(parent ...tree.Node) *Table

NewTable returns a new Table with the given optional parent: Table provides a GUI widget for representing table.Table values.

func (*Table) AsyncUpdateTable

func (tb *Table) AsyncUpdateTable()

AsyncUpdateTable updates the display for asynchronous updating from other goroutines. Also updates indexview (calling Sequential).

func (*Table) CopySelectToMime

func (tb *Table) CopySelectToMime() mimedata.Mimes

CopySelectToMime copies selected rows to mime data

func (*Table) DeleteAt

func (tb *Table) DeleteAt(idx int)

DeleteAt deletes element at given index from slice

func (*Table) EditGlobalGridStyle

func (tb *Table) EditGlobalGridStyle()

EditGlobalGridStyle shows an editor dialog for global grid styles.

func (*Table) EditGridStyle

func (tb *Table) EditGridStyle(col int)

EditGridStyle shows an editor dialog for grid style for given column index.

func (*Table) FromMimeData

func (tb *Table) FromMimeData(md mimedata.Mimes) [][]string

FromMimeData returns records from csv of mime data

func (*Table) GetColumnGridStyle

func (tb *Table) GetColumnGridStyle(col int) *GridStyle

GetColumnGridStyle gets grid style for given column.

func (*Table) HasStyler

func (tb *Table) HasStyler() bool

func (*Table) Init

func (tb *Table) Init()

func (*Table) MakeHeader

func (tb *Table) MakeHeader(p *tree.Plan)

func (*Table) MakeRow

func (tb *Table) MakeRow(p *tree.Plan, i int)

func (*Table) MakeToolbar

func (tb *Table) MakeToolbar(p *tree.Plan)

func (*Table) MimeDataType

func (tb *Table) MimeDataType() string

func (*Table) NewAt

func (tb *Table) NewAt(idx int)

NewAt inserts a new blank element at given index in the slice -- -1 means the end

func (*Table) PasteAssign

func (tb *Table) PasteAssign(md mimedata.Mimes, idx int)

PasteAssign assigns mime data (only the first one!) to this idx

func (*Table) PasteAtIndex

func (tb *Table) PasteAtIndex(md mimedata.Mimes, idx int)

PasteAtIndex inserts object(s) from mime data at (before) given slice index adds to end of table

func (*Table) RowFirstVisWidget

func (tb *Table) RowFirstVisWidget(row int) (*core.WidgetBase, bool)

RowFirstVisWidget returns the first visible widget for given row (could be index or not) -- false if out of range

func (*Table) RowGrabFocus

func (tb *Table) RowGrabFocus(row int) *core.WidgetBase

RowGrabFocus grabs the focus for the first focusable widget in given row -- returns that element or nil if not successful -- note: grid must have already rendered for focus to be grabbed!

func (*Table) RowWidgetNs

func (tb *Table) RowWidgetNs() (nWidgPerRow, idxOff int)

RowWidgetNs returns number of widgets per row and offset for index label

func (*Table) SelectedColumnStrings

func (tb *Table) SelectedColumnStrings(colName string) []string

SelectedColumnStrings returns the string values of given column name.

func (*Table) SetSlice

func (tb *Table) SetSlice(sl any) *Table

SetSlice sets the source table to a table.NewSliceTable from the given slice.

func (*Table) SetSortDescending

func (t *Table) SetSortDescending(v bool) *Table

SetSortDescending sets the [Table.SortDescending]: whether current sort order is descending.

func (*Table) SetSortFieldName

func (tb *Table) SetSortFieldName(nm string)

SetSortField sets sorting to happen on given field and direction -- see SortFieldName for details

func (*Table) SetSortIndex

func (t *Table) SetSortIndex(v int) *Table

SetSortIndex sets the [Table.SortIndex]: current sort index.

func (*Table) SetTable

func (tb *Table) SetTable(dt *table.Table) *Table

SetTable sets the source table that we are viewing, using a sequential view, and then configures the display

func (*Table) SizeFinal

func (tb *Table) SizeFinal()

func (*Table) SliceHeader

func (tb *Table) SliceHeader() *core.Frame

SliceHeader returns the Frame header for slice grid

func (*Table) SliceIndex

func (tb *Table) SliceIndex(i int) (si, vi int, invis bool)

func (*Table) SortColumn

func (tb *Table) SortColumn(fldIndex int)

SortColumn sorts the slice for given column index. Toggles ascending vs. descending if already sorting on this dimension.

func (*Table) SortFieldName

func (tb *Table) SortFieldName() string

SortFieldName returns the name of the field being sorted, along with :up or :down depending on descending

func (*Table) StyleRow

func (tb *Table) StyleRow(w core.Widget, idx, fidx int)

func (*Table) StyleValue

func (tb *Table) StyleValue(w core.Widget, s *styles.Style, row, col int)

StyleValue performs additional value widget styling

func (*Table) UpdateMaxWidths

func (tb *Table) UpdateMaxWidths()

func (*Table) UpdateSliceSize

func (tb *Table) UpdateSliceSize() int

type TableButton

type TableButton struct {
	core.Button
	Table *table.Table
}

TableButton presents a button that pulls up the Table viewer for a table.Table.

func NewTableButton

func NewTableButton(parent ...tree.Node) *TableButton

NewTableButton returns a new TableButton with the given optional parent: TableButton presents a button that pulls up the Table viewer for a table.Table.

func (*TableButton) Init

func (tb *TableButton) Init()

func (*TableButton) SetTable

func (t *TableButton) SetTable(v *table.Table) *TableButton

SetTable sets the [TableButton.Table]

func (*TableButton) WidgetValue

func (tb *TableButton) WidgetValue() any

type TensorButton

type TensorButton struct {
	core.Button
	Tensor tensor.Tensor
}

TensorButton represents a Tensor with a button for making a TensorGrid viewer for an tensor.Tensor.

func NewTensorButton

func NewTensorButton(parent ...tree.Node) *TensorButton

NewTensorButton returns a new TensorButton with the given optional parent: TensorButton represents a Tensor with a button for making a TensorGrid viewer for an tensor.Tensor.

func (*TensorButton) Init

func (tb *TensorButton) Init()

func (*TensorButton) SetTensor

func (t *TensorButton) SetTensor(v tensor.Tensor) *TensorButton

SetTensor sets the [TensorButton.Tensor]

func (*TensorButton) WidgetValue

func (tb *TensorButton) WidgetValue() any

type TensorEditor

type TensorEditor struct {
	core.ListBase

	// the tensor that we're a view of
	Tensor tensor.Tensor `set:"-"`

	// overall layout options for tensor display
	Layout Layout `set:"-"`

	// number of columns in table (as of last update)
	NCols int `edit:"-"`

	// blank values for out-of-range rows
	BlankString string
	BlankFloat  float64
	// contains filtered or unexported fields
}

TensorEditor provides a GUI widget for representing tensor.Tensor values.

func NewTensorEditor

func NewTensorEditor(parent ...tree.Node) *TensorEditor

NewTensorEditor returns a new TensorEditor with the given optional parent: TensorEditor provides a GUI widget for representing tensor.Tensor values.

func (*TensorEditor) ColumnHeader

func (tb *TensorEditor) ColumnHeader(col int) string

func (*TensorEditor) CopySelectToMime

func (tb *TensorEditor) CopySelectToMime() mimedata.Mimes

CopySelectToMime copies selected rows to mime data

func (*TensorEditor) FromMimeData

func (tb *TensorEditor) FromMimeData(md mimedata.Mimes) [][]string

FromMimeData returns records from csv of mime data

func (*TensorEditor) HasStyler

func (tb *TensorEditor) HasStyler() bool

func (*TensorEditor) Init

func (tb *TensorEditor) Init()

func (*TensorEditor) MakeHeader

func (tb *TensorEditor) MakeHeader(p *tree.Plan)

func (*TensorEditor) MakeRow

func (tb *TensorEditor) MakeRow(p *tree.Plan, i int)

func (*TensorEditor) MakeToolbar

func (tb *TensorEditor) MakeToolbar(p *tree.Plan)

func (*TensorEditor) MimeDataType

func (tb *TensorEditor) MimeDataType() string

func (*TensorEditor) OpenCSV

func (tb *TensorEditor) OpenCSV(filename core.Filename) error

OpenTSV reads a tensor from a tab-separated-values (TSV) file. using the Go standard encoding/csv reader conforming to the official CSV standard. Reads all values and assigns as many as fit.

func (*TensorEditor) PasteAssign

func (tb *TensorEditor) PasteAssign(md mimedata.Mimes, idx int)

PasteAssign assigns mime data (only the first one!) to this idx

func (*TensorEditor) PasteAtIndex

func (tb *TensorEditor) PasteAtIndex(md mimedata.Mimes, idx int)

PasteAtIndex inserts object(s) from mime data at (before) given slice index adds to end of table

func (*TensorEditor) RowFirstVisWidget

func (tb *TensorEditor) RowFirstVisWidget(row int) (*core.WidgetBase, bool)

RowFirstVisWidget returns the first visible widget for given row (could be index or not) -- false if out of range

func (*TensorEditor) RowGrabFocus

func (tb *TensorEditor) RowGrabFocus(row int) *core.WidgetBase

RowGrabFocus grabs the focus for the first focusable widget in given row -- returns that element or nil if not successful -- note: grid must have already rendered for focus to be grabbed!

func (*TensorEditor) RowWidgetNs

func (tb *TensorEditor) RowWidgetNs() (nWidgPerRow, idxOff int)

RowWidgetNs returns number of widgets per row and offset for index label

func (*TensorEditor) SaveCSV

func (tb *TensorEditor) SaveCSV(filename core.Filename) error

SaveTSV writes a tensor to a tab-separated-values (TSV) file. Outer-most dims are rows in the file, and inner-most is column -- Reading just grabs all values and doesn't care about shape.

func (*TensorEditor) SetBlankFloat

func (t *TensorEditor) SetBlankFloat(v float64) *TensorEditor

SetBlankFloat sets the [TensorEditor.BlankFloat]

func (*TensorEditor) SetBlankString

func (t *TensorEditor) SetBlankString(v string) *TensorEditor

SetBlankString sets the [TensorEditor.BlankString]: blank values for out-of-range rows

func (*TensorEditor) SetNCols

func (t *TensorEditor) SetNCols(v int) *TensorEditor

SetNCols sets the [TensorEditor.NCols]: number of columns in table (as of last update)

func (*TensorEditor) SetTensor

func (tb *TensorEditor) SetTensor(et tensor.Tensor) *TensorEditor

SetTensor sets the source tensor that we are viewing, and then configures the display.

func (*TensorEditor) SizeFinal

func (tb *TensorEditor) SizeFinal()

func (*TensorEditor) SliceHeader

func (tb *TensorEditor) SliceHeader() *core.Frame

SliceHeader returns the Frame header for slice grid

func (*TensorEditor) SliceIndex

func (tb *TensorEditor) SliceIndex(i int) (si, vi int, invis bool)

func (*TensorEditor) StyleRow

func (tb *TensorEditor) StyleRow(w core.Widget, idx, fidx int)

func (*TensorEditor) StyleValue

func (tb *TensorEditor) StyleValue(w core.Widget, s *styles.Style, row, col int)

StyleValue performs additional value widget styling

func (*TensorEditor) UpdateMaxWidths

func (tb *TensorEditor) UpdateMaxWidths()

func (*TensorEditor) UpdateSliceSize

func (tb *TensorEditor) UpdateSliceSize() int

type TensorGrid

type TensorGrid struct {
	core.WidgetBase

	// Tensor is the tensor that we view.
	Tensor tensor.Tensor `set:"-"`

	// GridStyle has grid display style properties.
	GridStyle GridStyle

	// ColorMap is the colormap displayed (based on)
	ColorMap *colormap.Map

	// RowLabels are optional labels for each row of the 2D shape.
	// Empty strings cause grouping with rendered lines.
	RowLabels []string

	// ColumnLabels are optional labels for each column of the 2D shape.
	// Empty strings cause grouping with rendered lines.
	ColumnLabels []string
	// contains filtered or unexported fields
}

TensorGrid is a widget that displays tensor values as a grid of colored squares. Higher-dimensional data is projected into 2D using tensor.Projection2DShape and related functions.

func NewTensorGrid

func NewTensorGrid(parent ...tree.Node) *TensorGrid

NewTensorGrid returns a new TensorGrid with the given optional parent: TensorGrid is a widget that displays tensor values as a grid of colored squares. Higher-dimensional data is projected into 2D using tensor.Projection2DShape and related functions.

func (*TensorGrid) Color

func (tg *TensorGrid) Color(val float64) (norm float64, clr color.Color)

func (*TensorGrid) EditStyle

func (tg *TensorGrid) EditStyle()

func (*TensorGrid) EnsureColorMap

func (tg *TensorGrid) EnsureColorMap()

EnsureColorMap makes sure there is a valid color map that matches specified name

func (*TensorGrid) Init

func (tg *TensorGrid) Init()

func (*TensorGrid) MakeToolbar added in v0.1.2

func (tg *TensorGrid) MakeToolbar(p *tree.Plan)

func (*TensorGrid) MinSize

func (tg *TensorGrid) MinSize() math32.Vector2

MinSize returns minimum size based on tensor and display settings

func (*TensorGrid) Render

func (tg *TensorGrid) Render()

func (*TensorGrid) SetColorMap

func (t *TensorGrid) SetColorMap(v *colormap.Map) *TensorGrid

SetColorMap sets the [TensorGrid.ColorMap]: ColorMap is the colormap displayed (based on)

func (*TensorGrid) SetColumnLabels added in v0.1.2

func (t *TensorGrid) SetColumnLabels(v ...string) *TensorGrid

SetColumnLabels sets the [TensorGrid.ColumnLabels]: ColumnLabels are optional labels for each column of the 2D shape. Empty strings cause grouping with rendered lines.

func (*TensorGrid) SetGridStyle

func (t *TensorGrid) SetGridStyle(v GridStyle) *TensorGrid

SetGridStyle sets the [TensorGrid.GridStyle]: GridStyle has grid display style properties.

func (*TensorGrid) SetRowLabels added in v0.1.2

func (t *TensorGrid) SetRowLabels(v ...string) *TensorGrid

SetRowLabels sets the [TensorGrid.RowLabels]: RowLabels are optional labels for each row of the 2D shape. Empty strings cause grouping with rendered lines.

func (*TensorGrid) SetTensor

func (tg *TensorGrid) SetTensor(tsr tensor.Tensor) *TensorGrid

SetTensor sets the tensor. Must call Update after this.

func (*TensorGrid) SetWidgetValue

func (tg *TensorGrid) SetWidgetValue(value any) error

func (*TensorGrid) SizeLabel added in v0.1.2

func (tg *TensorGrid) SizeLabel(lbs []string, col bool) (minBlank, ngps int, sz math32.Vector2)

func (*TensorGrid) TensorEditor

func (tg *TensorGrid) TensorEditor()

TensorEditor pulls up a TensorEditor of our tensor

func (*TensorGrid) UpdateRange

func (tg *TensorGrid) UpdateRange()

func (*TensorGrid) WidgetValue

func (tg *TensorGrid) WidgetValue() any

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL