# Using numpy reslice to get slices of segment

**URL:** <https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257>\
**Category:** Development\
**Created:** [March 7, 2018, 1:42pm UTC](https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257 "2018-03-07T13:42:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [March 7, 2018, 1:42pm UTC](https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257/1 "2018-03-07T13:42:25Z")

</div>

Hey guys (and especially Andras! (@lassoan)

A while back Andras helped me with a super handy python script to calculate the area of a segment’s slices slice-by-slice and put it in a table. I’ve been working on putting this into a general-use module where it uses the new plot functions to make graphs - not sure if it will be useful to anybody but me, but there it is.

The core of the script he provided used reslice() to reformat the segmentation data into an array of slices:.

```
segmentationNode = getNode('Segmentation')
segmentId = 'Segment_1'
vimage = segmentationNode.GetBinaryLabelmapRepresentation(segmentId)

narray = vtk.util.numpy_support.vtk_to_numpy(vimage.GetPointData().GetScalars())
vshape = vimage.GetDimensions()
narrayBySlice = narray.reshape([-1,vshape[1]*vshape[2]])

```

Then the number of nonzero voxels is counted, and so on. For axial, coronal, and saggital I use different indexes into the vshape array for reshaping the array - [0] \* [1], [0] \* [2]. (I’m pretty sure this is where I’m going wrong. But I digress.)

The issue I’m having is that this works great in one direction. As a test case, I made a simple segment with a single sphere. In the axial direction, I get a very smooth curve:

 ![42%20AM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/4/247b17008cce8257c35bb29d67e1e61d1e3fa549.png)

When I switch to the coronal and saggital directions, the curve is very choppy - the values jump up and down around the “correct” curve line:

 ![56%20AM](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/0/c0e3391fce387b647d171cabf8257d0cc8c7a2fa.png)

I’m pretty sure this is just a misunderstanding on my part on how reslice works.

Here’s a snippet of how I’m doing reslice for each direction:

```
  if direction == "Axial":
    narrayBySlice = narray.reshape([-1,vshape[0]*vshape[1]])
  if direction == "Coronal":
    narrayBySlice = narray.reshape([-1,vshape[0]*vshape[2]])
  if direction == "Saggital":
    narrayBySlice = narray.reshape([-1,vshape[1]*vshape[2]])

```

I’ve put the [module code in a gist](https://gist.github.com/hherhold/0492146f70ef0fc511b7b21410264481) for perusal if that helps.

Thank you in advance for any help anyone can give!

-Hollister

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 7, 2018, 10:05pm UTC](https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257/2 "2018-03-07T22:05:40Z")

</div>

To reslice a numpy along a different axis, it is not enough to reshape. You need to swap the axes as well. See [https://stackoverflow.com/questions/35226115/numpy-reshaping-multdimensional-array-through-arbitrary-axis](https://stackoverflow.com/questions/35226115/numpy-reshaping-multdimensional-array-through-arbitrary-axis)

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [March 12, 2018, 12:17pm UTC](https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257/3 "2018-03-12T12:17:44Z")

</div>

Got it - this was very helpful, as always.

I don’t know if this kind of plot would be generally useful, but I’m happy to package it up and upload it if you think others might use it.

Thanks again!!!

-Hollister

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 12, 2018, 1:00pm UTC](https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257/4 "2018-03-12T13:00:24Z")

</div>

Any working examples may be useful for others. You can save it as a github Gist and copy the link here.

---

<div class="post-metadata">

**Author:** ![hherhold](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/hherhold/32/12199_2.png) [@hherhold](https://discourse.slicer.org/u/hherhold)\
**Post date:** [March 12, 2018, 1:21pm UTC](https://discourse.slicer.org/t/using-numpy-reslice-to-get-slices-of-segment/2257/5 "2018-03-12T13:21:10Z")

</div>

Sounds good - [Here it is.](https://gist.github.com/hherhold/0492146f70ef0fc511b7b21410264481)

Thanks again for your help!

-Hollister
