|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using Serilog.Debugging; |
| 3 | +using System; |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace Tensorflow.NumPy.Pickle |
| 9 | +{ |
| 10 | + public class MultiArrayPickleWarpper |
| 11 | + { |
| 12 | + public Shape reconstructedShape { get; set; } |
| 13 | + public TF_DataType reconstructedDType { get; set; } |
| 14 | + public NDArray reconstructedNDArray { get; set; } |
| 15 | + public Array reconstructedMultiArray { get; set; } |
| 16 | + public MultiArrayPickleWarpper(Shape shape, TF_DataType dtype) |
| 17 | + { |
| 18 | + reconstructedShape = shape; |
| 19 | + reconstructedDType = dtype; |
| 20 | + } |
| 21 | + public void __setstate__(object[] args) |
| 22 | + { |
| 23 | + if (args.Length != 5) |
| 24 | + throw new InvalidArgumentError($"Invalid number of arguments in NDArray.__setstate__. Expected five arguments. Given {args.Length} arguments."); |
| 25 | + |
| 26 | + var version = (int)args[0]; // version |
| 27 | + |
| 28 | + var arg1 = (object[])args[1]; |
| 29 | + var dims = new int[arg1.Length]; |
| 30 | + for (var i = 0; i < arg1.Length; i++) |
| 31 | + { |
| 32 | + dims[i] = (int)arg1[i]; |
| 33 | + } |
| 34 | + var _ShapeLike = new Shape(dims); // shape |
| 35 | + |
| 36 | + TF_DataType _DType_co = (DTypePickleWarpper)args[2]; // DType |
| 37 | + |
| 38 | + var F_continuous = (bool)args[3]; // F-continuous |
| 39 | + if (F_continuous) |
| 40 | + throw new InvalidArgumentError("Fortran Continuous memory layout is not supported. Please use C-continuous layout or check the data format."); |
| 41 | + |
| 42 | + var data = args[4]; // Data |
| 43 | + /* |
| 44 | + * If we ever need another pickle format, increment the version |
| 45 | + * number. But we should still be able to handle the old versions. |
| 46 | + */ |
| 47 | + if (version < 0 || version > 4) |
| 48 | + throw new ValueError($"can't handle version {version} of numpy.dtype pickle"); |
| 49 | + |
| 50 | + // TODO: Implement the missing details and checks from the official Numpy C code here. |
| 51 | + // https://github.com/numpy/numpy/blob/2f0bd6e86a77e4401d0384d9a75edf9470c5deb6/numpy/core/src/multiarray/descriptor.c#L2761 |
| 52 | + |
| 53 | + if (data.GetType() == typeof(ArrayList)) |
| 54 | + { |
| 55 | + Reconstruct((ArrayList)data); |
| 56 | + } |
| 57 | + else |
| 58 | + throw new NotImplementedException(""); |
| 59 | + } |
| 60 | + private void Reconstruct(ArrayList arrayList) |
| 61 | + { |
| 62 | + int ndim = 1; |
| 63 | + var subArrayList = arrayList; |
| 64 | + while (subArrayList.Count > 0 && subArrayList[0] != null && subArrayList[0].GetType() == typeof(ArrayList)) |
| 65 | + { |
| 66 | + subArrayList = (ArrayList)subArrayList[0]; |
| 67 | + ndim += 1; |
| 68 | + } |
| 69 | + var type = subArrayList[0].GetType(); |
| 70 | + if (type == typeof(int)) |
| 71 | + { |
| 72 | + if (ndim == 1) |
| 73 | + { |
| 74 | + int[] list = (int[])arrayList.ToArray(typeof(int)); |
| 75 | + Shape shape = new Shape(new int[] { arrayList.Count }); |
| 76 | + reconstructedMultiArray = list; |
| 77 | + reconstructedNDArray = new NDArray(list, shape); |
| 78 | + } |
| 79 | + if (ndim == 2) |
| 80 | + { |
| 81 | + int secondDim = 0; |
| 82 | + foreach (ArrayList subArray in arrayList) |
| 83 | + { |
| 84 | + secondDim = subArray.Count > secondDim ? subArray.Count : secondDim; |
| 85 | + } |
| 86 | + int[,] list = new int[arrayList.Count, secondDim]; |
| 87 | + for (int i = 0; i < arrayList.Count; i++) |
| 88 | + { |
| 89 | + var subArray = (ArrayList?)arrayList[i]; |
| 90 | + if (subArray == null) |
| 91 | + throw new NullReferenceException(""); |
| 92 | + for (int j = 0; j < subArray.Count; j++) |
| 93 | + { |
| 94 | + var element = subArray[j]; |
| 95 | + if (element == null) |
| 96 | + throw new NoNullAllowedException("the element of ArrayList cannot be null."); |
| 97 | + list[i, j] = (int)element; |
| 98 | + } |
| 99 | + } |
| 100 | + Shape shape = new Shape(new int[] { arrayList.Count, secondDim }); |
| 101 | + reconstructedMultiArray = list; |
| 102 | + reconstructedNDArray = new NDArray(list, shape); |
| 103 | + } |
| 104 | + if (ndim > 2) |
| 105 | + throw new NotImplementedException("can't handle ArrayList with more than two dimensions."); |
| 106 | + } |
| 107 | + else |
| 108 | + throw new NotImplementedException(""); |
| 109 | + } |
| 110 | + public static implicit operator Array(MultiArrayPickleWarpper arrayWarpper) |
| 111 | + { |
| 112 | + return arrayWarpper.reconstructedMultiArray; |
| 113 | + } |
| 114 | + public static implicit operator NDArray(MultiArrayPickleWarpper arrayWarpper) |
| 115 | + { |
| 116 | + return arrayWarpper.reconstructedNDArray; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments