|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace Tensorflow.NumPy |
| 7 | +{ |
| 8 | + public class NDArrayRender |
| 9 | + { |
| 10 | + public static string ToString(NDArray array) |
| 11 | + { |
| 12 | + Shape shape = array.shape; |
| 13 | + if (shape.IsScalar) |
| 14 | + return Render(array); |
| 15 | + |
| 16 | + var s = new StringBuilder(); |
| 17 | + s.Append("array("); |
| 18 | + Build(s, array); |
| 19 | + s.Append(")"); |
| 20 | + return s.ToString(); |
| 21 | + } |
| 22 | + |
| 23 | + static void Build(StringBuilder s, NDArray array) |
| 24 | + { |
| 25 | + var shape = array.shape; |
| 26 | + |
| 27 | + if (shape.Length == 1) |
| 28 | + { |
| 29 | + s.Append("["); |
| 30 | + s.Append(Render(array)); |
| 31 | + s.Append("]"); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + var len = shape[0]; |
| 36 | + s.Append("["); |
| 37 | + |
| 38 | + if (len <= 10) |
| 39 | + { |
| 40 | + for (int i = 0; i < len; i++) |
| 41 | + { |
| 42 | + Build(s, array[i]); |
| 43 | + if (i < len - 1) |
| 44 | + { |
| 45 | + s.Append(", "); |
| 46 | + s.AppendLine(); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + for (int i = 0; i < 5; i++) |
| 53 | + { |
| 54 | + Build(s, array[i]); |
| 55 | + if (i < len - 1) |
| 56 | + { |
| 57 | + s.Append(", "); |
| 58 | + s.AppendLine(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + s.Append(" ... "); |
| 63 | + s.AppendLine(); |
| 64 | + |
| 65 | + for (int i = (int)len - 5; i < len; i++) |
| 66 | + { |
| 67 | + Build(s, array[i]); |
| 68 | + if (i < len - 1) |
| 69 | + { |
| 70 | + s.Append(", "); |
| 71 | + s.AppendLine(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + s.Append("]"); |
| 77 | + } |
| 78 | + |
| 79 | + static string Render(NDArray array) |
| 80 | + { |
| 81 | + if (array.buffer == IntPtr.Zero) |
| 82 | + return "<null>"; |
| 83 | + |
| 84 | + var dtype = array.dtype; |
| 85 | + var shape = array.shape; |
| 86 | + |
| 87 | + if (dtype == TF_DataType.TF_STRING) |
| 88 | + { |
| 89 | + if (array.rank == 0) |
| 90 | + return "'" + string.Join(string.Empty, array.StringBytes()[0] |
| 91 | + .Take(25) |
| 92 | + .Select(x => x < 32 || x > 127 ? "\\x" + x.ToString("x") : Convert.ToChar(x).ToString())) + "'"; |
| 93 | + else |
| 94 | + return $"['{string.Join("', '", array.StringData().Take(25))}']"; |
| 95 | + } |
| 96 | + else if (dtype == TF_DataType.TF_VARIANT) |
| 97 | + { |
| 98 | + return "<unprintable>"; |
| 99 | + } |
| 100 | + else if (dtype == TF_DataType.TF_RESOURCE) |
| 101 | + { |
| 102 | + return "<unprintable>"; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + return dtype switch |
| 107 | + { |
| 108 | + TF_DataType.TF_BOOL => Render(array.ToArray<bool>(), array.shape), |
| 109 | + TF_DataType.TF_INT8 => Render(array.ToArray<sbyte>(), array.shape), |
| 110 | + TF_DataType.TF_INT32 => Render(array.ToArray<int>(), array.shape), |
| 111 | + TF_DataType.TF_INT64 => Render(array.ToArray<long>(), array.shape), |
| 112 | + TF_DataType.TF_FLOAT => Render(array.ToArray<float>(), array.shape), |
| 113 | + TF_DataType.TF_DOUBLE => Render(array.ToArray<double>(), array.shape), |
| 114 | + _ => Render(array.ToArray<byte>(), array.shape) |
| 115 | + }; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + static string Render<T>(T[] array, Shape shape) |
| 120 | + { |
| 121 | + if (array == null) |
| 122 | + return "<null>"; |
| 123 | + |
| 124 | + if (array.Length == 0) |
| 125 | + return "<empty>"; |
| 126 | + |
| 127 | + if (shape.IsScalar) |
| 128 | + return array[0].ToString(); |
| 129 | + |
| 130 | + var display = ""; |
| 131 | + if (array.Length <= 10) |
| 132 | + display += string.Join(", ", array); |
| 133 | + else |
| 134 | + display += string.Join(", ", array.Take(5)) + ", ..., " + string.Join(", ", array.Skip(array.Length - 5)); |
| 135 | + return display; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments