<div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">Hi Fabio,</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">it looks like we will have to wait somewhat until VS writers get around to implement <a href="https://developercommunity.visualstudio.com/content/problem/22196/static-assert-cannot-compile-constexprs-method-tha.html" target="_blank">__builtin_offsetof</a></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">I don't know why it doesn't trigger when Python wrapping is OFF. I will look into it more.</div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small">Regards,</div><div class="gmail_default"><font face="verdana, sans-serif">Dženan Zukić, PhD, Senior R&D Engineer, Kitware (Carrboro, N.C.)</font></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 12, 2017 at 4:26 PM, Francois Budin <span dir="ltr"><<a href="mailto:francois.budin@kitware.com" target="_blank">francois.budin@kitware.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Fabio,<br><br></div>To add a little bit of extra information about using BridgeNumpy:<br><br></div><div>From NumPy to ITK, the memory is never copied, and always shared between the NumPy array and the ITK image.<br></div><div>From ITK to NumPy, the memory is not copied at first, if the data is not changed from the NumPy side (e.g. if the ITK image is changed, the NumPy array will be modified too), but if you modify the NumPy array directly, NumPy will create a copy of the data. This also means that if you were to modify the NumPy array, the modifications would not be visible in the ITK image.<br><br></div><div>A limited workaround to this is:<br><br># Get NumPy array from image<br>arr_image = itk.GetArrayFromImage(image)<br># Create copy of array that can be modified in-place<br>arr = arr_image.copy()<br># Modify array<br>…<br># Update image in-place with new array<br>arr_image.setfield(arr,arr_<wbr>image.dtype)<br><br></div><div>The advantage of this method is that the image will be updated. However, there is an explicit copy of your data.<br></div><div>If the copy step is skipped and the original NumPy array is modified, a copy of the data will be performed by NumPy and you may not be aware of it until you see that the data in the NumPy array and the image do not match anymore.<br><br></div><div>Hope this helps,<br></div><div>Francois<br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Apr 12, 2017 at 4:02 PM, D'Isidoro Fabio <span dir="ltr"><<a href="mailto:fisidoro@ethz.ch" target="_blank">fisidoro@ethz.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div link="blue" vlink="purple" lang="DE-CH">
<div class="m_-6335364007187202607m_-2257658807006854379WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d" lang="EN-US">Ok, thanks. Moe specifically I am using now
<u>Visual Studio 2017 Preview</u>.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d" lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d" lang="EN-US">Fabio.</span><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u><u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Dženan Zukić [mailto:<a href="mailto:dzenanz@gmail.com" target="_blank">dzenanz@gmail.com</a>]
<br>
<b>Sent:</b> Mittwoch, 12. </span><span style="font-size:11.0pt;font-family:"Calibri",sans-serif" lang="EN-US">April 2017 21:54<span><br>
<b>To:</b> D'Isidoro Fabio <<a href="mailto:fisidoro@ethz.ch" target="_blank">fisidoro@ethz.ch</a>><br>
</span><b>Cc:</b> Matt McCormick <<a href="mailto:matt.mccormick@kitware.com" target="_blank">matt.mccormick@kitware.com</a>>; <a href="mailto:insight-users@itk.org" target="_blank">insight-users@itk.org</a></span></p><div><div class="m_-6335364007187202607h5"><br>
<b>Subject:</b> Re: [ITK-users] ITK Python: numpy to itk image (and viceversa)<u></u><u></u></div></div><p></p><div><div class="m_-6335364007187202607h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif">Hi Fabio,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif">let me give it a try with VS2017 and Wrapping+NumPy. I will report back when I have an update.<u></u><u></u></span></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif">Regards,<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Verdana",sans-serif">Dženan Zukić, PhD, Senior R&D Engineer, Kitware (Carrboro, N.C.)<u></u><u></u></span></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Wed, Apr 12, 2017 at 2:16 PM, D'Isidoro Fabio <<a href="mailto:fisidoro@ethz.ch" target="_blank">fisidoro@ethz.ch</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal">Thank you. I am trying to build ITK Wrap Python with Module_BridgeNumPy=ON with Visual Studio 2017.<br>
<br>
I get the following type of errors:<br>
<br>
12>C:/Program Files (x86)/Microsoft Visual Studio/Preview/Community/VC/To<wbr>ols/MSVC/14.10.25017/include\<wbr>xstring(1905,26): error G3F63BFAE: constexpr variable '_Memcpy_move_offset' must be initialized by a constant expression<br>
12> static constexpr size_t _Memcpy_move_offset = offsetof(_Mydata_t, _Bx);<br>
12> ^ ~~~~~~~~~~~~~~~~~~~~~~~~<br>
12>C:/Program Files (x86)/Microsoft Visual Studio/Preview/Community/VC/To<wbr>ols/MSVC/14.10.25017/include\<wbr>stdexcept:23:21: note: in instantiation of template class 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested here<br>
12> : _Mybase(_Message.c_str())<br>
12> ^<br>
12>C:/Program Files (x86)/Microsoft Visual Studio/Preview/Community/VC/To<wbr>ols/MSVC/14.10.25017/include\<wbr>xstring:1905:48: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression<br>
12> static constexpr size_t _Memcpy_move_offset = offsetof(_Mydata_t, _Bx);<br>
12> ^<br>
12>C:/Program Files (x86)/Windows Kits/10/Include/10.0.10586.0/u<wbr>crt\stddef.h:38:32: note: expanded from macro 'offsetof'<br>
12> #define offsetof(s,m) ((size_t)&reinterpret_cast<cha<wbr>r const volatile&>((((s*)0)->m)))<br>
<br>
<br>
The build worked with Visual Studio 2015 instead.<br>
<br>
Can I fix this issue with Visual Studio 2017 somehow?<br>
<br>
Thank you.<br>
<br>
------------------------------<wbr>------------------------------<wbr>----------<br>
Fabio D’Isidoro - PhD Student<br>
Institute of Biomechanics<br>
HPP O 14<br>
Hönggerbergring 64<br>
8093 Zürich, Switzerland<u></u><u></u></p>
<div>
<div>
<p class="MsoNormal"><br>
-----Original Message-----<br>
From: Matt McCormick [mailto:<a href="mailto:matt.mccormick@kitware.com" target="_blank">matt.mccormick@kitware<wbr>.com</a>]<br>
Sent: Montag, 3. April 2017 20:41<br>
To: D'Isidoro Fabio <<a href="mailto:fisidoro@ethz.ch" target="_blank">fisidoro@ethz.ch</a>><br>
Cc: <a href="mailto:insight-users@itk.org" target="_blank">insight-users@itk.org</a><br>
Subject: Re: [ITK-users] ITK Python: numpy to itk image (and viceversa)<br>
<br>
Hallo Fabio,<br>
<br>
<br>
> I use ITK with Python Wrap. I need to interface my Python code with a<br>
> Cython-wrapped C++ code that takes only numpy array as input and<br>
> returns numpy array as output.<br>
<br>
Cool. By the way, you may be interested in scikit-build [1], which is a good way to build Cython-wrapped C++ code. We are using it for the ITK and SimpleITK Python packages, and it has good Cython and CMake support.<br>
<br>
<br>
> Hence, I need to convert the Python itk images into numpy array to be<br>
> given as input to the wrapped C++ code, and then convert the numpy<br>
> array in output from the wrapped C++ code back into python itk images.<br>
><br>
><br>
><br>
> Question 1) How can I do that in an efficient way? I found some posts<br>
> on itk.PyBuffer but I could not find anywhere any reference on how to<br>
> install it on my itk wrap build.<br>
<br>
Yes, itk.PyBuffer works great for that. Please review a PR for some additional documentation:<br>
<br>
<a href="https://github.com/InsightSoftwareConsortium/ITKBridgeNumPy/pull/18" target="_blank">
https://github.com/InsightSoft<wbr>wareConsortium/ITKBridgeNumPy/<wbr>pull/18</a><br>
<br>
This has been available in ITK for a few releases as a Remote module, which can be enabled by setting<br>
<br>
Module_BridgeNumPy=ON<br>
<br>
in ITK's CMake configuration.<br>
<br>
<br>
Since ITK 4.11.0, it is easier to build since it does not require the NumPy headers.<br>
<br>
<br>
In current ITK Git master (to be 4.12.0) the module is enabled by default.<br>
<br>
<br>
Nightly ITK Python packages for ITK Git master are now being built:<br>
<br>
<a href="https://github.com/InsightSoftwareConsortium/ITKPythonPackage" target="_blank">
https://github.com/InsightSoft<wbr>wareConsortium/ITKPythonPackag<wbr>e</a><br>
<br>
macOS and Linux are available. Windows packages will be available over the coming weeks.<br>
<br>
<br>
<br>
> Question 2) The purpose of writing a part of my algorithm in C++ is to<br>
> speed up the code. If the conversion between python itk images and<br>
> numpy arrays is slow, I would lose all the speed gain obtained with the C++ implementation.<br>
> Are there better ways to deal with that?<br>
<br>
The newer versions ITKBridgeNumPy use a NumPy array view, which does not do any copies during the conversion, and it is very fast.<br>
<br>
<br>
HTH,<br>
Matt<br>
<br>
<br>
[1] <a href="http://scikit-build.org/" target="_blank">http://scikit-build.org/</a><br>
______________________________<wbr>_______<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a href="http://www.kitware.com/products/protraining.php" target="_blank">http://www.kitware.com/product<wbr>s/protraining.php</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FA<wbr>Q</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/insight-users" target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/insight-users</a><u></u><u></u></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div></div></div>
</div>
<br>______________________________<wbr>_______<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a href="http://www.kitware.com/products/protraining.php" rel="noreferrer" target="_blank">http://www.kitware.com/product<wbr>s/protraining.php</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a href="http://www.itk.org/Wiki/ITK_FAQ" rel="noreferrer" target="_blank">http://www.itk.org/Wiki/ITK_FA<wbr>Q</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/insight-users" rel="noreferrer" target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/insight-users</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>