|
| 1 | +package tfrecord |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ubccr/terf" |
| 5 | + protobuf "github.com/ubccr/terf/protobuf" |
| 6 | +) |
| 7 | + |
| 8 | +func FeatureBool(rec *protobuf.Example, key string) bool { |
| 9 | + return FeatureInt(rec, key) == 1 |
| 10 | +} |
| 11 | + |
| 12 | +func FeatureInt64(rec *protobuf.Example, key string) int64 { |
| 13 | + f, ok := rec.Features.Feature[key] |
| 14 | + if !ok { |
| 15 | + return 0 |
| 16 | + } |
| 17 | + |
| 18 | + val, ok := f.Kind.(*protobuf.Feature_Int64List) |
| 19 | + if !ok { |
| 20 | + return 0 |
| 21 | + } |
| 22 | + |
| 23 | + return val.Int64List.Value[0] |
| 24 | +} |
| 25 | + |
| 26 | +func FeatureInt(rec *protobuf.Example, key string) int { |
| 27 | + return int(FeatureInt64(rec, key)) |
| 28 | +} |
| 29 | + |
| 30 | +func FeatureInt32(rec *protobuf.Example, key string) int32 { |
| 31 | + return int32(FeatureInt64(rec, key)) |
| 32 | +} |
| 33 | + |
| 34 | +func FeatureFloat64(rec *protobuf.Example, key string) float64 { |
| 35 | + return float64(FeatureFloat32(rec, key)) |
| 36 | +} |
| 37 | + |
| 38 | +func FeatureFloat32(rec *protobuf.Example, key string) float32 { |
| 39 | + f, ok := rec.Features.Feature[key] |
| 40 | + if !ok { |
| 41 | + return 0 |
| 42 | + } |
| 43 | + |
| 44 | + val, ok := f.Kind.(*protobuf.Feature_FloatList) |
| 45 | + if !ok { |
| 46 | + return 0 |
| 47 | + } |
| 48 | + |
| 49 | + return val.FloatList.Value[0] |
| 50 | +} |
| 51 | + |
| 52 | +func FeatureBytes(rec *protobuf.Example, key string) []byte { |
| 53 | + return terf.ExampleFeatureBytes(rec, key) |
| 54 | +} |
| 55 | + |
| 56 | +func FeatureString(rec *protobuf.Example, key string) string { |
| 57 | + return string(FeatureBytes(rec, key)) |
| 58 | +} |
| 59 | + |
| 60 | +func FeatureBytesSlice(rec *protobuf.Example, key string) [][]byte { |
| 61 | + // TODO: return error if key is not found? |
| 62 | + f, ok := rec.Features.Feature[key] |
| 63 | + if !ok { |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + val, ok := f.Kind.(*protobuf.Feature_BytesList) |
| 68 | + if !ok { |
| 69 | + return nil |
| 70 | + } |
| 71 | + return val.BytesList.Value |
| 72 | +} |
| 73 | + |
| 74 | +func FeatureStringSlice(rec *protobuf.Example, key string) []string { |
| 75 | + slice := FeatureBytesSlice(rec, key) |
| 76 | + if slice == nil { |
| 77 | + return nil |
| 78 | + } |
| 79 | + |
| 80 | + res := make([]string, len(slice)) |
| 81 | + for ii, val := range slice { |
| 82 | + res[ii] = string(val) |
| 83 | + } |
| 84 | + |
| 85 | + return res |
| 86 | +} |
| 87 | + |
| 88 | +func FeatureInt64Slice(rec *protobuf.Example, key string) []int64 { |
| 89 | + |
| 90 | + f, ok := rec.Features.Feature[key] |
| 91 | + if !ok { |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + val, ok := f.Kind.(*protobuf.Feature_Int64List) |
| 96 | + if !ok { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + return val.Int64List.Value |
| 101 | +} |
| 102 | + |
| 103 | +func FeatureIntSlice(rec *protobuf.Example, key string) []int { |
| 104 | + slice := FeatureInt64Slice(rec, key) |
| 105 | + if slice == nil { |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + res := make([]int, len(slice)) |
| 110 | + for ii, val := range slice { |
| 111 | + res[ii] = int(val) |
| 112 | + } |
| 113 | + |
| 114 | + return res |
| 115 | +} |
| 116 | + |
| 117 | +func FeatureInt32Slice(rec *protobuf.Example, key string) []int32 { |
| 118 | + slice := FeatureInt64Slice(rec, key) |
| 119 | + if slice == nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + res := make([]int32, len(slice)) |
| 124 | + for ii, val := range slice { |
| 125 | + res[ii] = int32(val) |
| 126 | + } |
| 127 | + |
| 128 | + return res |
| 129 | +} |
| 130 | + |
| 131 | +func FeatureFloat64Slice(rec *protobuf.Example, key string) []float64 { |
| 132 | + slice := FeatureFloat32Slice(rec, key) |
| 133 | + if slice == nil { |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + res := make([]float64, len(slice)) |
| 138 | + for ii, val := range slice { |
| 139 | + res[ii] = float64(val) |
| 140 | + } |
| 141 | + |
| 142 | + return res |
| 143 | +} |
| 144 | + |
| 145 | +func FeatureFloat32Slice(rec *protobuf.Example, key string) []float32 { |
| 146 | + f, ok := rec.Features.Feature[key] |
| 147 | + if !ok { |
| 148 | + return nil |
| 149 | + } |
| 150 | + |
| 151 | + val, ok := f.Kind.(*protobuf.Feature_FloatList) |
| 152 | + if !ok { |
| 153 | + return nil |
| 154 | + } |
| 155 | + |
| 156 | + return val.FloatList.Value |
| 157 | +} |
0 commit comments