Understanding the inner workings of the radon function (from skimage)

Juan Nunez-Iglesias jni.soma at gmail.com
Fri Apr 11 22:57:04 EDT 2014


Hey Guillaume, Kaloyan,


On Fri, Apr 11, 2014 at 9:53 PM, Guillaume Gay <
guillaume at mitotic-machine.org> wrote:
>
>
>
>  The docstring is incorrect, and you can see that in _wrap_fast:
>
> ```python
> cdef double[:, ::1] M = np.ascontiguousarray(H)
> ```
>
> meaning that the array passed to the loop will be read as a 'flat' array.
>

No, flat is referred to as flat. =) Contiguous just means that the contents
of the array are contiguous in memory, but the comma inside the [] brackets
means that M has two dimensions. However, because they are contiguous, the
actual data is laid out in one contiguous block of memory of size 9 times
the dtype of the array. Notice that it's not M that is passed to the
_matrix_transform function, but &M[0, 0], that is, the memory address of
the first element of the array. Because the memory is guaranteed to be
contiguous, you can access those elements using pointer arithmetic. So, in
_matrix_transform, H[3] actually points to M[1, 0], H[5] points to M[1, 2],
and so on.

I guess the comment should read something like `contiguous array from an
> array of shape (3,3)`
>

Yeah, that's certainly true. The docstring for _matrix_transform itself is
confusing and your suggestion is a good one. Wanna submit a PR? ;)

>
>
>  My guess is `&M[0, 0]` is the pointer to the (flat) matrix M
>

Yes, as detailed above. Cython is a mixture of Python and C so you have to
draw on the syntax of both.

>
>    - last but not least, the type of img is defined to cnp.ndarray, and
>    then one converts it to a contiguous array; what is the purpose/advantage
>    of doing this conversion?
>
>  My guess: it's more efficient, and less cumbersome (1D arrays are easier
> to understand from a C point of view)
>

Pretty close. As mentioned above, contiguous is not the same as flat.
Contiguous arrays are more efficient and easier to deal with (no need to
think about memory strides). Flat arrays are *sometimes* easier to deal
with. That depends on the context.

>
>    - I do have a _warps_cy.so on my harddrive, but I don't know what
>    application it can be opened it; could you let me know about that?
>
>  This is a shared object (akin to a DLL in windows) so it's binary,
> compiled from C code generated by cython from the .pyx file, you have no
> reason to open it.
>

Also correct -- you also have _warps_cy.pyx, right?

Hope this all helps!

Juan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20140412/1de5f3f2/attachment.html>


More information about the scikit-image mailing list