Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining a DType struct in C# for use in np.fromfile #131

Open
yolothedino opened this issue Dec 10, 2024 · 1 comment
Open

Defining a DType struct in C# for use in np.fromfile #131

yolothedino opened this issue Dec 10, 2024 · 1 comment

Comments

@yolothedino
Copy link

How would I transpose the below code chunk to C#? I'm trying to convert the following python code to be used in C#. The point of this function is to parse a binary file's data into a collection of defined objects.

dtype = np.dtype([
	("reading_1", np.float64),
	("reading_2", np.float32),
	("reading_3", np.float64)
# and so on
])
...
# file is a binary file of the structured data defined above
datachunks = np.fromfile(file, dtype)
# do things with arrays of reading_1, reading_2, reading_3 from datachunks

My roadblock right now is that I do not know how to write the 'dtype' segment in C#. I have tried to declare a struct of my own and using it as a parameter for np.fromfile, which I know will not work because np.fromfile requires a Dtype.

[StructLayout(LayoutKind.Sequential)]
public struct ReadingData
{
    public double reading_1;
    public float reading_2;
    public double reading_1;
}
...
NDArray datachunks = np.fromfile(filename, ReadingData);
// syntax error as ReadingData is not a dtype

I can't find any examples on writing a dtype struct in SciSharp's C# Numpy, so I am stumped.

@henon
Copy link
Contributor

henon commented Dec 10, 2024

I am pretty sure what you are trying to do is not supported, at least the way you are doing it. I would need a minimal python snippet that creates a small file and then reads it back in, you know, just a few numbers in each reading. From that I can try to come up with an equivalent in Numpy.NET.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants