/// <summary> /// Loads <see cref="NdArray{T}"/> instance from .npy binary stream with using implicit cast. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> /// <exception cref="InvalidCastException"></exception> public static NdArray <T> LoadAs <T>(Stream stream) { var header = NpyHeader.LoadHeader(stream); switch (header.NumpyType.TypeKind) { case TypeKind.Boolean: return(LoadCore <bool>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.UInt8: return(LoadCore <byte>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.UInt16: return(LoadCore <ushort>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.UInt32: return(LoadCore <uint>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.UInt64: return(LoadCore <ulong>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Int8: return(LoadCore <sbyte>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Int16: return(LoadCore <short>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Int32: return(LoadCore <int>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Int64: return(LoadCore <long>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Float16: throw new NotImplementedException(); case TypeKind.Float32: return(LoadCore <float>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Float64: return(LoadCore <double>(header, stream).Select(x => (T)(dynamic)x)); case TypeKind.Unicode: return(LoadCore <string>(header, stream).Select(x => (T)(dynamic)x)); default: throw new InvalidOperationException(); } }
/// <summary> /// Loads <see cref="NdArray{T}"/> instance from .npy binary stream with strict type. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> /// <exception cref="InvalidCastException"></exception> public static NdArray <T> Load <T>(Stream stream) => LoadCore <T>(NpyHeader.LoadHeader(stream), stream);