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