Hi Nat,
  Thanks for your reply! I have tried the first method you mentioned--adding an index value to the return method. It did work if I only needed a single array for future calculation. But I found it's not very convenient for my program. It's convenient and more efficient if other classes in it could process all the one-dimensional arrays at the same time. Otherwise, I needed to write more control code in python, and the c++ part could only deal with a single array now. Therefore, I hoped that I could pass such a multidimensional array in my program.

Sincerely,
Zhenwei


On Sun, Dec 8, 2013 at 2:00 PM, <cctbxbb-request@phenix-online.org> wrote:
Send cctbxbb mailing list submissions to
        cctbxbb@phenix-online.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://phenix-online.org/mailman/listinfo/cctbxbb
or, via email, send a message with subject or body 'help' to
        cctbxbb-request@phenix-online.org

You can reach the person managing the list at
        cctbxbb-owner@phenix-online.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of cctbxbb digest..."


Today's Topics:

   1. Passing array between python and C++ (Zhenwei Luo)
   2. Re: Passing array between python and C++ (Nathaniel Echols)


----------------------------------------------------------------------

Message: 1
Date: Sat, 7 Dec 2013 22:25:08 -0600
From: Zhenwei Luo <tluozhenwei@gmail.com>
To: cctbxbb@phenix-online.org
Subject: [cctbxbb] Passing array between python and C++
Message-ID: <90518A19-AC1B-493B-8B27-79D31EDDF055@gmail.com>
Content-Type: text/plain; charset=us-ascii

Hello Everyone,

        I am writing a refinement program using the cctbx. I have encountered with a problem when I wanted to pass an array between c++ and python. The type of array I used in my c++ source file is af::shared<af::shared<vec3> >, I would like to pass it into my python script, thus I could do some other operations to this array.
I wrote a method in my class to return this array.

 af::shared<af::shared<vec3<double> > return_modes( ) { return modes; }

I wrote another method in the *_ext file to wrap this method to make it callable from python.

.def("return_modes", &nm_init::return_modes)

But when I use it in python,
eigenvector = nm_init_manager.return_modes()

It gave me an error:

TypeError: No to_python (by-value) converter found for c++ type: scitbx::af::shared<scitbx::af::shared<scitbx::vec3<double> > >

What I want to realize is just creating an two dimensional array using vec3 as its element. I can also using the select method  in this array to select some elements from its column, just like in the af::shared<vec3<double> > array. Namely, the column of this array should have some same properties like af::shared<vec3<double> >. Can anyone help me figure out what I should do?

Thank you very much! Any help will be greatly appreciated.

Sincerely,
Zhenwei Luo
Rice University


------------------------------

Message: 2
Date: Sat, 7 Dec 2013 20:45:10 -0800
From: Nathaniel Echols <nechols@lbl.gov>
To: cctbx mailing list <cctbxbb@phenix-online.org>
Subject: Re: [cctbxbb] Passing array between python and C++
Message-ID:
        <CALeAa1MOL1wbW5Vb=M=jXsnGTaCBQNJjcOzDwMhM27x5PP+XNA@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Sat, Dec 7, 2013 at 8:25 PM, Zhenwei Luo <tluozhenwei@gmail.com> wrote:

>         I am writing a refinement program using the cctbx. I have
> encountered with a problem when I wanted to pass an array between c++ and
> python. The type of array I used in my c++ source file is
> af::shared<af::shared<vec3> >, I would like to pass it into my python
> script, thus I could do some other operations to this array.
> I wrote a method in my class to return this array.
>
>  af::shared<af::shared<vec3<double> > return_modes( ) { return modes; }
>
> I wrote another method in the *_ext file to wrap this method to make it
> callable from python.
>
> .def("return_modes", &nm_init::return_modes)
>
> But when I use it in python,
> eigenvector = nm_init_manager.return_modes()
> It gave me an error:
> TypeError: No to_python (by-value) converter found for c++ type:
> scitbx::af::shared<scitbx::af::shared<scitbx::vec3<double> > >
>

What that error means is that boost.python doesn't know how to present this
new type to Python.  For the data structures that we pass back and forth,
we have to explicitly provide wrappers for them - including new
instantiations of scitbx::array_family templates.

What I want to realize is just creating an two dimensional array using vec3
> as its element. I can also using the select method  in this array to select
> some elements from its column, just like in the af::shared<vec3<double> >
> array. Namely, the column of this array should have some same properties
> like af::shared<vec3<double> >. Can anyone help me figure out what I should
> do?
>

In general the scitbx::array_family templates aren't really intended to
every be multidimensional; for inherently 2D and 3D data types like images
and maps, we use flex.grid to provide a multi-dimensional accessor on top
of the 1D array.  The grid may not be the most appropriate in your case but
if I'm making the correct assumptions about the data you're trying to pass
back and forth, I think it might be easiest to simply define a custom class
that contains a std::vector of af::shared<vec3> arrays, and add an index
parameter to the return_modes method, which would be modified to return
only a single array of modes at a time.  You can also do things like
wrapping a method as __getitem__ with boost.python, which would mimic a 2D
array.  (I can probably dig up/cook up some code examples tomorrow if this
is unclear, since it was certainly not intuitive to me when I started
working with CCTBX and it would be nice to have documentation for the
future.)

-Nat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://phenix-online.org/pipermail/cctbxbb/attachments/20131207/91a774e3/attachment.html>

------------------------------

_______________________________________________
cctbxbb mailing list
cctbxbb@phenix-online.org
http://phenix-online.org/mailman/listinfo/cctbxbb


End of cctbxbb Digest, Vol 59, Issue 1
**************************************