forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.fromfile.cs
More file actions
64 lines (62 loc) · 3.86 KB
/
np.fromfile.cs
File metadata and controls
64 lines (62 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.IO;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Construct an array from data in a text or binary file.
/// A highly efficient way of reading binary data with a known data-type, as well as parsing simply formatted text files. Data written using the tofile method can be read using this function.
/// </summary>
/// <param name="file">filename.</param>
/// <param name="dtype">Data type of the returned array. For binary files, it is used to determine the size and byte-order of the items in the file.</param>
/// <returns></returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.fromfile.html</remarks>
public static NDArray fromfile(string file, NPTypeCode dtype)
{
return fromfile(file, dtype.AsType());
}
/// <summary>
/// Construct an array from data in a text or binary file.
/// A highly efficient way of reading binary data with a known data-type, as well as parsing simply formatted text files. Data written using the tofile method can be read using this function.
/// </summary>
/// <param name="file">filename.</param>
/// <param name="dtype">Data type of the returned array. For binary files, it is used to determine the size and byte-order of the items in the file.</param>
/// <returns></returns>
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.fromfile.html</remarks>
public static NDArray fromfile(string file, Type dtype)
{
unsafe
{
var bytes = File.ReadAllBytes(file);
switch (dtype.GetTypeCode())
{
#if _REGEN
%foreach supported_dtypes, supported_dtypes_lowercase%
case NPTypeCode.#1: return new NDArray(new ArraySlice<#2>(UnmanagedMemoryBlock<#2>.FromBuffer(bytes, false)));
%
default:
throw new NotSupportedException();
#else
case NPTypeCode.Boolean: return new NDArray(new ArraySlice<bool>(UnmanagedMemoryBlock<bool>.FromBuffer(bytes, false)));
case NPTypeCode.Byte: return new NDArray(new ArraySlice<byte>(UnmanagedMemoryBlock<byte>.FromBuffer(bytes, false)));
case NPTypeCode.Int16: return new NDArray(new ArraySlice<short>(UnmanagedMemoryBlock<short>.FromBuffer(bytes, false)));
case NPTypeCode.UInt16: return new NDArray(new ArraySlice<ushort>(UnmanagedMemoryBlock<ushort>.FromBuffer(bytes, false)));
case NPTypeCode.Int32: return new NDArray(new ArraySlice<int>(UnmanagedMemoryBlock<int>.FromBuffer(bytes, false)));
case NPTypeCode.UInt32: return new NDArray(new ArraySlice<uint>(UnmanagedMemoryBlock<uint>.FromBuffer(bytes, false)));
case NPTypeCode.Int64: return new NDArray(new ArraySlice<long>(UnmanagedMemoryBlock<long>.FromBuffer(bytes, false)));
case NPTypeCode.UInt64: return new NDArray(new ArraySlice<ulong>(UnmanagedMemoryBlock<ulong>.FromBuffer(bytes, false)));
case NPTypeCode.Char: return new NDArray(new ArraySlice<char>(UnmanagedMemoryBlock<char>.FromBuffer(bytes, false)));
case NPTypeCode.Double: return new NDArray(new ArraySlice<double>(UnmanagedMemoryBlock<double>.FromBuffer(bytes, false)));
case NPTypeCode.Single: return new NDArray(new ArraySlice<float>(UnmanagedMemoryBlock<float>.FromBuffer(bytes, false)));
case NPTypeCode.Decimal: return new NDArray(new ArraySlice<decimal>(UnmanagedMemoryBlock<decimal>.FromBuffer(bytes, false)));
default:
throw new NotSupportedException();
#endif
}
}
}
}
}