Skip to content

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

Open
@yolothedino

Description

@yolothedino

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions