MCPcopy Create free account
hub / github.com/cogentcore/core / WriteCSV

Function WriteCSV

tensor/io.go:54–84  ·  view source on GitHub ↗

WriteCSV WriteCSV writes a tensor to a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg). Outer-most dims are rows in the file, and inner-most is column -- Reading just grabs all values and doesn't care about shape.

(tsr Tensor, w io.Writer, delim rune)

Source from the content-addressed store, hash-verified

52// Outer-most dims are rows in the file, and inner-most is column --
53// Reading just grabs all values and doesn't care about shape.
54func WriteCSV(tsr Tensor, w io.Writer, delim rune) error {
55 prec := -1
56 if ps, ok := tsr.MetaData("precision"); ok {
57 prec, _ = strconv.Atoi(ps)
58 }
59 cw := csv.NewWriter(w)
60 if delim != 0 {
61 cw.Comma = delim
62 }
63 nrow := tsr.DimSize(0)
64 nin := tsr.Len() / nrow
65 rec := make([]string, nin)
66 str := tsr.IsString()
67 for ri := 0; ri < nrow; ri++ {
68 for ci := 0; ci < nin; ci++ {
69 idx := ri*nin + ci
70 if str {
71 rec[ci] = tsr.String1D(idx)
72 } else {
73 rec[ci] = strconv.FormatFloat(tsr.Float1D(idx), 'g', prec, 64)
74 }
75 }
76 err := cw.Write(rec)
77 if err != nil {
78 log.Println(err)
79 return err
80 }
81 }
82 cw.Flush()
83 return nil
84}
85
86// ReadCSV reads a tensor from a comma-separated-values (CSV) file
87// (where comma = any delimiter, specified in the delim arg),

Callers 1

SaveCSVFunction · 0.85

Calls 9

PrintlnMethod · 0.80
MetaDataMethod · 0.65
DimSizeMethod · 0.65
LenMethod · 0.65
IsStringMethod · 0.65
String1DMethod · 0.65
Float1DMethod · 0.65
WriteMethod · 0.65
FlushMethod · 0.45

Tested by

no test coverage detected