Skip to content

Numpy array to C# array #564

Closed
Closed

Description

@bobred

Environment

  • Pythonnet version: 2.4.0
  • Python version: 3.6 (anaconda)
  • Operating System: Windows 7 (x86)

Details

I am using the code below to use some of the features of pylinac in C#. I would like to know how to convert the dynamic array in to a C# array. In the example below the array structure is 60 rows and 4000 columns.

{[[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
...,
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]
[ 0. 0. 0. ..., 0. 0. 0.]]}

       `PythonEngine.PythonHome = @"C:\Anaconda3";
        
        using (Py.GIL())
        {
            try
            {
                dynamic pl = Py.Import("pylinac");
                dynamic np = Py.Import("numpy");
                dynamic tlog = pl.TrajectoryLog.from_demo();
                tlog.fluence.gamma.calc_map();
                dynamic array = tlog.fluence.actual.array.astype(np.float32, Py.kw("copy",  false));
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }`

Also when debugging in VS 2017 the process slows down when importing Py.Import("pylinac"), any ideas, should this be in a separate post?

Activity

  1. den-run-ai commented on Nov 2, 2017

    @den-run-ai
    Contributor

    @bobred the slowdown during import is likely a performance issue with the debugger and nothing to do with pythonnet.
    For converting numpy arrays, search this issue tracker. There are multiple methods, e.g. some listed here.:

    #514

  2. dmitriyse commented on Nov 2, 2017

    @dmitriyse
    Contributor

    #528 this PR addresses this issue.
    Debug is slow due to unoptimized pythonnet Implicit assembly loading technique.

  3. den-run-ai commented on Nov 2, 2017

    @den-run-ai
    Contributor

    We should probably supporting this conversion syntax from IronPython.

  4. filmor commented on Jun 14, 2022

    @filmor
    Member

    There are some tests that do the conversion manually, apart from that, we are open to additional codecs that implement this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions