Volumetric Registration

In this tutorial we align a mouse brain to the Allen Referece Atlas (ARA). Thus the ARA is our reference image, it consists of image data stored in the default "average" channel and corresponding annotations stored in a separate "annotation" channel. We begin by importing the relevant modules


In [2]:
%matplotlib inline

In [3]:
from ndreg import *
import matplotlib
#import ndio.remote.neurodata as neurodata
from intern.remote.boss import BossRemote
from intern.resource.boss.resource import *
from requests import HTTPError
import time
import configparser
startTime = time.time()

We define the server and our user token


In [4]:
matplotlib.rcParams['figure.figsize'] = (10.0, 8.0)

In [6]:
# Assume a valid configuration file exists at ~/.intern/intern.cfg.
cfg_file = '/run/data/.intern/intern.cfg'
if cfg_file.startswith('~'): 
    cfg_file = os.path.expanduser('~') + cfg_file[1:]
config = configparser.ConfigParser()
config.read_file(file(cfg_file))
TOKEN = config['Default']['token']
rmt = BossRemote(cfg_file_or_dict=cfg_file)

First we'll download the atlas image


In [7]:
REFERENCE_COLLECTION = 'ara_2016'
REFERENCE_EXPERIMENT = 'sagittal_50um'
REFERENCE_COORDINATE_FRAME = 'ara_2016' 
REFERENCE_CHANNEL = 'average_50um'
# Set/Modify these parameters
REFERENCE_RESOLUTION = 0
REFERENCE_ISOTROPIC = True

In [8]:
(ref_exp_resource, ref_coord_resource, ref_channel_resource) = setup_channel_boss(rmt, REFERENCE_COLLECTION, REFERENCE_EXPERIMENT, REFERENCE_CHANNEL)
refImg = imgDownload_boss(rmt, ref_channel_resource, ref_coord_resource, resolution=REFERENCE_RESOLUTION, isotropic=REFERENCE_ISOTROPIC)

In [9]:
refThreshold = imgPercentile(refImg, 0.99)

In [10]:
refImg.GetSize()


Out[10]:
(264, 160, 228)

In [11]:
refImg.GetSpacing()


Out[11]:
(0.05, 0.05, 0.05)

Next we'll visualize the image. To ensure that the visuization is has good contrast we'll only show intensity values below the 99th percentile.

When given an image volume imgShow displays it as a set of evenly spaced z-slices (1st column), y-slices (2nd column) and x slices (3rd column).


In [12]:
imgShow(refImg, vmax=refThreshold)


Now we download the corresponding annotations


In [13]:
REFERENCE_ANNOTATION_COLLECTION = 'ara_2016'
REFERENCE_ANNOTATION_EXPERIMENT = 'sagittal_50um'
REFERENCE_ANNOTATION_COORDINATE_FRAME = 'ara_2016' 
REFERENCE_ANNOTATION_CHANNEL = 'annotation_50um'
REFERENCE_ANNOTATION_RESOLUTION = REFERENCE_RESOLUTION
REFERENCE_ANNOTATION_ISOTROPIC = True

In [14]:
(refAnnotation_exp_resource, refAnnotation_coord_resource, refAnnotation_channel_resource) = setup_channel_boss(rmt, REFERENCE_ANNOTATION_COLLECTION, REFERENCE_ANNOTATION_EXPERIMENT, REFERENCE_ANNOTATION_CHANNEL)
refAnnotationImg = imgDownload_boss(rmt, refAnnotation_channel_resource, refAnnotation_coord_resource, resolution=REFERENCE_ANNOTATION_RESOLUTION, isotropic=REFERENCE_ANNOTATION_ISOTROPIC)

In [15]:
imgShow(refAnnotationImg, vmax=1000)


It's usually better to visuaize a set of annotations in color. Let's display the atlas annotations using a random colormap. We begin by creating a 1000x3 array of random values. Since we always want the backgrond (label 0) to be dark we make the 0th row is [0,0,0].


In [16]:
randValues = np.random.rand(1000,3)
randValues = np.concatenate(([[0,0,0]],randValues))

Now we can display the annotations.


In [17]:
randCmap = matplotlib.colors.ListedColormap(randValues)
imgShow(refAnnotationImg, vmax=1000, cmap=randCmap)


Let's overlay these annotations on the atlas image using alpha transparency. To do this we must set newFig to False so that matplotlib appends the annotation data to the current figure instead of creating new one.


In [18]:
imgShow(refImg, vmax=refThreshold, newFig=False)
imgShow(refAnnotationImg, vmax=1000, cmap=randCmap, alpha=0.3, newFig=False)
plt.show()


Remove missing parts of brain


In [110]:
remove_regions = [507, 212, 220, 228, 236, 244, 151, 188, 196, 204]

In [111]:
refAnnoImg = sitk.GetArrayFromImage(refAnnotationImg)
remove_indices = np.isin(refAnnoImg, remove_regions)

In [112]:
refAnnoImg[remove_indices] = 0

In [113]:
# adjust annotations
refAnnoImg_adj = sitk.GetImageFromArray(refAnnoImg)
refAnnoImg_adj.SetSpacing(refAnnotationImg.GetSpacing())
refAnnotationImg = refAnnoImg_adj
# adjust atlas with corresponding indices
# refImg_adj = sitk.GetArrayFromImage(refImg)
# refImg_adj[remove_indices] = 0
# refImg_adj = sitk.GetImageFromArray(refImg_adj)
# refImg_adj.SetSpacing(refImg.GetSpacing())
# refImg = refImg_adj

In [114]:
imgShow(refAnnotationImg, vmax=1000, cmap=randCmap, alpha=1, newFig=False)



In [30]:
imgShow(refImg, vmax=refThreshold)


Downloading input image


In [24]:
# Modify these parameters for your specific experiment
SAMPLE_COLLECTION = 'ailey-dev'
SAMPLE_EXPERIMENT = 's3617'
SAMPLE_COORDINATE_FRAME = 'aileydev_s3617'
SAMPLE_CHANNEL = 'channel1'
SAMPLE_RESOLUTION = 4
SAMPLE_ISOTROPIC = False

In [25]:
sample_exp_resource, sample_coord_resource, sample_channel_resource = setup_channel_boss(rmt, SAMPLE_COLLECTION, SAMPLE_EXPERIMENT, SAMPLE_CHANNEL)

In [26]:
sampleImg = imgDownload_boss(rmt, sample_channel_resource, sample_coord_resource, resolution=SAMPLE_RESOLUTION, isotropic=SAMPLE_ISOTROPIC)



KeyboardInterruptTraceback (most recent call last)
<ipython-input-26-7c46e5a65d66> in <module>()
----> 1 sampleImg = imgDownload_boss(rmt, sample_channel_resource, sample_coord_resource, resolution=SAMPLE_RESOLUTION, isotropic=SAMPLE_ISOTROPIC)

/root/.local/lib/python2.7/site-packages/ndreg-0.0.1-py2.7.egg/ndreg/ndreg.pyc in imgDownload_boss(remote, channel_resource, coordinate_frame_resource, resolution, size, start, isotropic)
    209 
    210     # Download all image data from specified channel
--> 211     array = remote.get_cutout(channel_resource, resolution, [start[0], size[0]], [start[1], size[1]], [start[2], size[2]])
    212 
    213     # Cast downloaded image to server's data type

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/remote/remote.pyc in get_cutout(self, resource, resolution, x_range, y_range, z_range, time_range, id_list)
    149             raise RuntimeError('Resource incompatible with the volume service.')
    150         return self._volume.get_cutout(
--> 151             resource, resolution, x_range, y_range, z_range, time_range, id_list)
    152 
    153     def create_cutout(self, resource, resolution, x_range, y_range, z_range, data, time_range=None):

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/volume.pyc in wrapper(*args, **kwargs)
     35                     'ChannelResource not fully initialized.  Use intern.remote.BossRemote.get_channel({}, {}, {})'.format(
     36                         args[1].name, args[1].coll_name, args[1].exp_name))
---> 37         return fcn(*args, **kwargs)
     38 
     39     return wrapper

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/volume.pyc in get_cutout(self, resource, resolution, x_range, y_range, z_range, time_range, id_list)
    102         return self.service.get_cutout(
    103             resource, resolution, x_range, y_range, z_range, time_range, id_list,
--> 104             self.url_prefix, self.auth, self.session, self.session_send_opts)
    105 
    106     @check_channel

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/v1/volume.pyc in get_cutout(self, resource, resolution, x_range, y_range, z_range, time_range, id_list, url_prefix, auth, session, send_opts)
    143                 _data = self.get_cutout(
    144                     resource, resolution, b[0], b[1], b[2],
--> 145                     time_range, id_list, url_prefix, auth, session, send_opts
    146                 )
    147 

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/v1/volume.pyc in get_cutout(self, resource, resolution, x_range, y_range, z_range, time_range, id_list, url_prefix, auth, session, send_opts)
    163         # Hack in Accept header for now.
    164         prep.headers['Accept'] = 'application/blosc'
--> 165         resp = session.send(prep, **send_opts)
    166 
    167         if resp.status_code == 200:

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/sessions.pyc in send(self, request, **kwargs)
    626 
    627         if not stream:
--> 628             r.content
    629 
    630         return r

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/models.pyc in content(self)
    753                     self._content = None
    754                 else:
--> 755                     self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
    756 
    757             except AttributeError:

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/models.pyc in generate()
    674             if hasattr(self.raw, 'stream'):
    675                 try:
--> 676                     for chunk in self.raw.stream(chunk_size, decode_content=True):
    677                         yield chunk
    678                 except ProtocolError as e:

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/packages/urllib3/response.pyc in stream(self, amt, decode_content)
    351         """
    352         if self.chunked:
--> 353             for line in self.read_chunked(amt, decode_content=decode_content):
    354                 yield line
    355         else:

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/packages/urllib3/response.pyc in read_chunked(self, amt, decode_content)
    500         with self._error_catcher():
    501             while True:
--> 502                 self._update_chunk_length()
    503                 if self.chunk_left == 0:
    504                     break

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/packages/urllib3/response.pyc in _update_chunk_length(self)
    448         line = self._fp.fp.readline()
    449         line = line.split(b';', 1)[0]
--> 450         try:
    451             self.chunk_left = int(line, 16)
    452         except ValueError:

KeyboardInterrupt: 

We'll only use intensity values below the 99th percentile in the visualization.


In [ ]:
sampleThreshold = imgPercentile(sampleImg, .999)
imgShow(sampleImg, vmax=sampleThreshold)

In [23]:
sampleImg.GetSize()


Out[23]:
(922, 1311, 79)

In [36]:
sampleImg.GetSpacing()


Out[36]:
(0.01872, 0.01872, 0.16)

In [122]:
#sitk.WriteImage(sampleImg, SAMPLE_COLLECTION + '_' + SAMPLE_EXPERIMENT + '_' + SAMPLE_CHANNEL + '_' + 'res' + str(SAMPLE_RESOLUTION) + '.tif')

Reorienting input image

You may have noticed that the input brain is not oriented in the same way as the atlas. Let's look at the atlas.


In [146]:
imgShow(refImg, vmax=refThreshold)


By examining the the z slices in the first column its clear that the x-axis goes from Right to Left side of the brain. The y-axis varies from Superior to Inferior and the z-axis goes from Anterior to posterior. Thus it is in RSA orientation. Looking at the the input brain...


In [147]:
imgShow(sampleImg, vmax=sampleThreshold)


...we see that the x-axis goes from Right to Left, the y-axis goes from Posterior to Anterior and the Z axis goes from Inferior to Superior. Thus it's in RPI orientation. Therefore we reorient the input image from RPI to RSA

Compare the above to the Atlas. The slices should now correspond with the atlas. Let's make a copy of the reorianted image size and spacing because we'll need it later.


In [148]:
# modify sampleOrient based on your image orientation
sampleOrient = "RPI"
refOrient = "ASR"

In [149]:
sampleImgReoriented = imgReorient(sampleImg, sampleOrient, refOrient)
imgShow(sampleImgReoriented, vmax=sampleThreshold)


Downsample images

Now we downsample the input and reference images to a more manageable size


In [156]:
DOWNSAMPLE_SPACING = 0.010 # millimeters
spacing = [DOWNSAMPLE_SPACING,DOWNSAMPLE_SPACING,DOWNSAMPLE_SPACING]

In [166]:
# if spacing == refImg.GetSpacing(): 
#     refImg_ds = sitk.Clamp(refImg, upperBound=refThreshold)
# else:
refImg_ds = sitk.Clamp(imgResample(refImg, spacing), upperBound=refThreshold)
imgShow(refImg_ds)



In [167]:
sampleImg_ds = sitk.Clamp(imgResample(sampleImgReoriented, spacing), upperBound=sampleThreshold)
imgShow(sampleImg_ds)



In [168]:
sampleImgSize_reorient = sampleImgReoriented.GetSize()
sampleImgSpacing_reorient= sampleImgReoriented.GetSpacing()

Affine Registration

Now we compute the affine transform. Unlike in the basic registration tutiorial we'll use the imgAffineComposite instead of imgAffine. imgAffine simply computes the affine transform between the input and reference images. imgAffineComposite computes a translation then a rigid then an affine transformation. It's output is the composition of those three transforms. We use Mutual Information since the input and reference images have very differnt intensity profiles. We also enable the verbose option so that each iteration is printed.


In [ ]:
affine = imgAffineComposite(sampleImg_ds, refImg_ds, iterations=200, useMI=True, verbose=True)


Step translation:
0.	 -0.17878450386
1.	 -0.188644515258
2.	 -0.197649636094
3.	 -0.207277777686
4.	 -0.217873702705
5.	 -0.227502876285
6.	 -0.238703528706
7.	 -0.25157478535

Now we apply the affine transform to the input image and mask


In [34]:
print(affine)
sampleImg_affine = imgApplyAffine(sampleImgReoriented, affine, size=refImg.GetSize(), spacing=refImg.GetSpacing())
imgShow(sampleImg_affine, vmax=sampleThreshold)


[0.9541681325365168, 0.05835207944758628, 0.018533339701541366, 0.004790034693515006, 0.9445756442232279, -0.006499685213981909, 0.02874263266693851, 0.054653388717895664, 0.8829932089315535, -1.324585647376747, -0.33155805155454415, -1.180287518508166]

We can evaluate the affine registration by generating a checkerboard of the reference and input images. In this method the input image is placed on the black squares of the metaphorical checkerboard while the reference image is placed on the red squares. Idealy anatomical sturctures should line up across squares.


In [35]:
sampleImg_affine_bounded = sitk.Clamp(sampleImg_affine,upperBound=sampleThreshold)
refImg_bounded = sitk.Clamp(refImg, upperBound=refThreshold)
imgShow(imgChecker(sampleImg_affine_bounded, refImg_bounded))


LDDMM registration

Now we run LDDMM registration. Here we use imgMetamorphosisComposite. Unlike imgMetamorphosis introduced in the 2D registration notebook, this function runs LDDMM in multiple steps using the alpha values specified by in alphaList. The field and invField outputs are the composition of all steps. Once agan we use a Mutual Information cost because the input brain and reference image have very differnt intensity profiles.


In [36]:
(field, invField) = imgMetamorphosisComposite(sampleImg_ds, refImg_ds, alphaList=[0.2, 0.1, 0.05],
                                              scaleList = 1.0, useMI=True, iterations=100, verbose=True)


Step 0: alpha=0.2, beta=0.05, scale=1.0
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
0.	-1.12558e+10, 176.499, 0, -1.12558e+10 (97.3469%), 1.100000e-03
1.	-1.17443e+10, 273.021, 0, -1.17443e+10 (96.9769%), 1.210000e-03
2.	-1.28051e+10, 409.674, 0, -1.28051e+10 (96.1733%), 1.331000e-03
3.	-1.30051e+10, 519.411, 0, -1.30051e+10 (96.0218%), 1.464100e-03
4.	-1.34724e+10, 623.059, 0, -1.34724e+10 (95.6678%), 1.610510e-03
5.	-1.41715e+10, 861.708, 0, -1.41715e+10 (95.1382%), 1.771561e-03
6.	-1.45744e+10, 1083.87, 0, -1.45744e+10 (94.833%), 1.948717e-03
7.	-1.48357e+10, 1345.92, 0, -1.48357e+10 (94.6351%), 2.143589e-03
8.	-1.51896e+10, 1675.09, 0, -1.51896e+10 (94.367%), 2.357948e-03
9.	-1.52852e+10, 1852.66, 0, -1.52852e+10 (94.2946%), 2.593742e-03
10.	-1.58677e+10, 2234.67, 0, -1.58677e+10 (93.8533%), 2.853117e-03
11.	-1.6112e+10, 2430.8, 0, -1.6112e+10 (93.6683%), 1.569214e-03
12.	-1.62555e+10, 2519.47, 0, -1.62555e+10 (93.5596%), 1.726136e-03
13.	-1.63601e+10, 2664.2, 0, -1.63601e+10 (93.4803%), 9.493746e-04
14.	-1.64198e+10, 2763.66, 0, -1.64198e+10 (93.4351%), 1.044312e-03
15.	-1.64303e+10, 2893.97, 0, -1.64303e+10 (93.4272%), 1.148743e-03
16.	-1.65285e+10, 2872.77, 0, -1.65285e+10 (93.3528%), 6.318088e-04
17.	-1.66151e+10, 2872.23, 0, -1.66151e+10 (93.2871%), 6.949897e-04
18.	-1.66153e+10, 2871.95, 0, -1.66153e+10 (93.287%), 4.778054e-05
19.	-1.66153e+10, 2871.95, 0, -1.66153e+10 (93.287%), 5.255859e-05
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
20.	-1.66159e+10, 2872.31, 0, -1.66159e+10 (93.2865%), 5.781445e-05
21.	-1.6616e+10, 2872.44, 0, -1.6616e+10 (93.2865%), 1.589897e-05
E = -1.6616e+10 (93.2865%)
Length = 160.71
Time = 251.763s (4.19606m)

Step 1: alpha=0.1, beta=0.05, scale=1.0
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
0.	-2.50757e+10, 12.0853, 0, -2.50757e+10 (98.288%), 1.100000e-03
1.	-2.54662e+10, 42.5715, 0, -2.54662e+10 (97.9533%), 1.210000e-03
2.	-2.55188e+10, 73.6297, 0, -2.55188e+10 (97.9082%), 1.331000e-03
3.	-2.66609e+10, 139.803, 0, -2.66609e+10 (96.9294%), 1.464100e-03
4.	-2.66926e+10, 199.826, 0, -2.66926e+10 (96.9023%), 1.610510e-03
5.	-2.67983e+10, 259.788, 0, -2.67983e+10 (96.8117%), 1.771561e-03
6.	-2.70601e+10, 328.568, 0, -2.70601e+10 (96.5873%), 1.948717e-03
7.	-2.71817e+10, 455.527, 0, -2.71817e+10 (96.4831%), 2.143589e-03
8.	-2.75146e+10, 603, 0, -2.75146e+10 (96.1978%), 2.357948e-03
9.	-2.79457e+10, 740.962, 0, -2.79457e+10 (95.8283%), 2.593742e-03
10.	-2.86299e+10, 1009.8, 0, -2.86299e+10 (95.242%), 2.853117e-03
11.	-2.87158e+10, 1107.96, 0, -2.87158e+10 (95.1684%), 1.569214e-03
12.	-2.89202e+10, 1220.39, 0, -2.89202e+10 (94.9932%), 1.726136e-03
13.	-2.91471e+10, 1349.7, 0, -2.91471e+10 (94.7988%), 1.898749e-03
14.	-2.93412e+10, 1490.15, 0, -2.93412e+10 (94.6324%), 2.088624e-03
15.	-2.94511e+10, 1664.01, 0, -2.94511e+10 (94.5382%), 2.297486e-03
16.	-2.96332e+10, 1911.27, 0, -2.96332e+10 (94.3822%), 2.527235e-03
17.	-2.99693e+10, 2143.69, 0, -2.99693e+10 (94.0941%), 2.779959e-03
18.	-3.04514e+10, 2653.76, 0, -3.04514e+10 (93.681%), 3.057955e-03
19.	-3.06401e+10, 2907.35, 0, -3.06401e+10 (93.5193%), 3.363750e-03
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
20.	-3.10289e+10, 3355.32, 0, -3.10289e+10 (93.1861%), 3.700125e-03
21.	-3.14182e+10, 3878.85, 0, -3.14182e+10 (92.8524%), 4.070137e-03
22.	-3.17209e+10, 4093.62, 0, -3.17209e+10 (92.593%), 2.238576e-03
23.	-3.19948e+10, 4479.09, 0, -3.19948e+10 (92.3583%), 2.462433e-03
24.	-3.20791e+10, 4605.08, 0, -3.20791e+10 (92.2861%), 1.354338e-03
25.	-3.2197e+10, 4772.09, 0, -3.2197e+10 (92.185%), 1.489772e-03
26.	-3.2321e+10, 4947.72, 0, -3.2321e+10 (92.0788%), 1.638749e-03
27.	-3.24541e+10, 5180.83, 0, -3.24541e+10 (91.9647%), 1.802624e-03
28.	-3.26275e+10, 5416.2, 0, -3.26275e+10 (91.816%), 1.982887e-03
29.	-3.27924e+10, 5673.65, 0, -3.27924e+10 (91.6747%), 2.181175e-03
30.	-3.29906e+10, 6019.93, 0, -3.29906e+10 (91.5049%), 2.399293e-03
31.	-3.32557e+10, 6388.85, 0, -3.32557e+10 (91.2777%), 2.639222e-03
32.	-3.35345e+10, 6934.76, 0, -3.35345e+10 (91.0388%), 2.903144e-03
33.	-3.36417e+10, 7088.03, 0, -3.36417e+10 (90.9469%), 1.596729e-03
34.	-3.38601e+10, 7450.76, 0, -3.38601e+10 (90.7597%), 1.756402e-03
35.	-3.39586e+10, 7670.18, 0, -3.39586e+10 (90.6753%), 1.932043e-03
36.	-3.39668e+10, 8165.31, 0, -3.39669e+10 (90.6682%), 2.125247e-03
37.	-3.42245e+10, 8197.68, 0, -3.42246e+10 (90.4474%), 1.168886e-03
38.	-3.43513e+10, 8450.9, 0, -3.43513e+10 (90.3388%), 1.285774e-03
39.	-3.44577e+10, 8665.9, 0, -3.44577e+10 (90.2476%), 1.414352e-03
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
40.	-3.45573e+10, 8848.73, 0, -3.45573e+10 (90.1623%), 1.555787e-03
41.	-3.46498e+10, 9112.45, 0, -3.46499e+10 (90.0829%), 1.711366e-03
42.	-3.47711e+10, 9372.13, 0, -3.47711e+10 (89.979%), 1.882502e-03
43.	-3.4834e+10, 9730.63, 0, -3.4834e+10 (89.9251%), 2.070752e-03
44.	-3.49851e+10, 9894.07, 0, -3.49852e+10 (89.7956%), 1.138914e-03
45.	-3.50574e+10, 10082.7, 0, -3.50574e+10 (89.7337%), 1.252805e-03
46.	-3.51172e+10, 10256, 0, -3.51172e+10 (89.6824%), 1.378086e-03
47.	-3.5263e+10, 10495.8, 0, -3.5263e+10 (89.5574%), 1.515894e-03
48.	-3.5321e+10, 10717.9, 0, -3.5321e+10 (89.5078%), 1.667484e-03
49.	-3.53968e+10, 11074.3, 0, -3.53969e+10 (89.4427%), 1.834232e-03
50.	-3.5501e+10, 11170, 0, -3.5501e+10 (89.3534%), 1.008828e-03
51.	-3.55709e+10, 11358.8, 0, -3.55709e+10 (89.2935%), 1.109710e-03
52.	-3.56228e+10, 11542.4, 0, -3.56228e+10 (89.2491%), 1.220681e-03
53.	-3.56984e+10, 11749.8, 0, -3.56984e+10 (89.1843%), 1.342750e-03
54.	-3.57762e+10, 11977.5, 0, -3.57762e+10 (89.1177%), 1.477025e-03
55.	-3.58624e+10, 12261.2, 0, -3.58624e+10 (89.0438%), 1.624727e-03
56.	-3.59259e+10, 12543.6, 0, -3.59259e+10 (88.9893%), 1.787200e-03
57.	-3.60304e+10, 12726.7, 0, -3.60304e+10 (88.8998%), 9.829598e-04
58.	-3.60974e+10, 12897, 0, -3.60974e+10 (88.8424%), 1.081256e-03
59.	-3.61597e+10, 13117.5, 0, -3.61597e+10 (88.789%), 1.189381e-03
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
60.	-3.62437e+10, 13384.3, 0, -3.62437e+10 (88.717%), 1.308320e-03
61.	-3.63064e+10, 13673.8, 0, -3.63064e+10 (88.6632%), 1.439151e-03
62.	-3.63658e+10, 13794.2, 0, -3.63658e+10 (88.6124%), 7.915333e-04
63.	-3.64004e+10, 13972.9, 0, -3.64004e+10 (88.5827%), 8.706867e-04
64.	-3.64486e+10, 14137, 0, -3.64486e+10 (88.5414%), 9.577553e-04
65.	-3.65109e+10, 14331.1, 0, -3.65109e+10 (88.488%), 1.053531e-03
66.	-3.65719e+10, 14546.9, 0, -3.65719e+10 (88.4357%), 1.158884e-03
67.	-3.66383e+10, 14798.9, 0, -3.66383e+10 (88.3788%), 1.274772e-03
68.	-3.66611e+10, 15024.5, 0, -3.66611e+10 (88.3593%), 1.402250e-03
69.	-3.67411e+10, 15210.5, 0, -3.67412e+10 (88.2907%), 7.712373e-04
70.	-3.67684e+10, 15340.9, 0, -3.67684e+10 (88.2673%), 8.483610e-04
71.	-3.68145e+10, 15512.6, 0, -3.68145e+10 (88.2278%), 9.331971e-04
72.	-3.6861e+10, 15643.1, 0, -3.6861e+10 (88.1879%), 1.026517e-03
73.	-3.69016e+10, 15898.1, 0, -3.69016e+10 (88.1531%), 1.129168e-03
74.	-3.69627e+10, 16088.1, 0, -3.69627e+10 (88.1008%), 1.242085e-03
75.	-3.6979e+10, 16380.7, 0, -3.6979e+10 (88.0868%), 1.366294e-03
76.	-3.69951e+10, 16623.4, 0, -3.69952e+10 (88.073%), 1.502923e-03
77.	-3.70618e+10, 16816.2, 0, -3.70618e+10 (88.0159%), 8.266078e-04
78.	-3.7134e+10, 16994, 0, -3.7134e+10 (87.954%), 9.092686e-04
79.	-3.71775e+10, 17182.8, 0, -3.71775e+10 (87.9167%), 1.000195e-03
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
80.	-3.72315e+10, 17377.2, 0, -3.72316e+10 (87.8704%), 1.100215e-03
81.	-3.72806e+10, 17589, 0, -3.72807e+10 (87.8283%), 1.210236e-03
82.	-3.73136e+10, 17818.4, 0, -3.73136e+10 (87.8001%), 1.331260e-03
83.	-3.73644e+10, 18069.4, 0, -3.73644e+10 (87.7566%), 1.464386e-03
84.	-3.74067e+10, 18224.9, 0, -3.74067e+10 (87.7203%), 8.054124e-04
85.	-3.74326e+10, 18356.6, 0, -3.74326e+10 (87.6981%), 8.859536e-04
86.	-3.74665e+10, 18553.5, 0, -3.74665e+10 (87.6691%), 9.745490e-04
87.	-3.75169e+10, 18720.4, 0, -3.75169e+10 (87.6259%), 1.072004e-03
88.	-3.75341e+10, 18955.5, 0, -3.75341e+10 (87.6111%), 1.179204e-03
89.	-3.75928e+10, 19168.1, 0, -3.75929e+10 (87.5608%), 1.297125e-03
90.	-3.76279e+10, 19498.5, 0, -3.76279e+10 (87.5308%), 1.426837e-03
91.	-3.77007e+10, 19576.1, 0, -3.77007e+10 (87.4683%), 7.847604e-04
92.	-3.77429e+10, 19791, 0, -3.77429e+10 (87.4322%), 8.632365e-04
93.	-3.77721e+10, 19917, 0, -3.77721e+10 (87.4071%), 9.495601e-04
94.	-3.78207e+10, 20172.9, 0, -3.78207e+10 (87.3655%), 1.044516e-03
95.	-3.78436e+10, 20324.1, 0, -3.78436e+10 (87.3459%), 1.148968e-03
96.	-3.78771e+10, 20632.4, 0, -3.78771e+10 (87.3172%), 1.263865e-03
97.	-3.79266e+10, 20691.6, 0, -3.79266e+10 (87.2747%), 6.951255e-04
98.	-3.79752e+10, 20846.3, 0, -3.79752e+10 (87.2331%), 7.646380e-04
99.	-3.80025e+10, 21007.1, 0, -3.80025e+10 (87.2097%), 8.411018e-04
E = -3.80025e+10 (87.2097%)
Length = 162.436
Time = 878.475s (14.6412m)

Step 2: alpha=0.05, beta=0.05, scale=1.0
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
0.	-3.75312e+10, 24.3, 0, -3.75312e+10 (98.9849%), 1.100000e-03
1.	-3.80309e+10, 16.1649, 0, -3.80309e+10 (98.5011%), 1.210000e-03
2.	-3.83467e+10, 30.2452, 0, -3.83467e+10 (98.1953%), 1.331000e-03
3.	-3.84189e+10, 57.0148, 0, -3.84189e+10 (98.1254%), 1.464100e-03
4.	-3.85223e+10, 94.6981, 0, -3.85223e+10 (98.0253%), 1.610510e-03
5.	-3.89396e+10, 104.802, 0, -3.89396e+10 (97.6213%), 8.857805e-04
6.	-3.89818e+10, 111.048, 0, -3.89818e+10 (97.5803%), 4.871793e-04
7.	-3.90281e+10, 122.388, 0, -3.90281e+10 (97.5355%), 5.358972e-04
8.	-3.90771e+10, 137.187, 0, -3.90771e+10 (97.4881%), 5.894869e-04
9.	-3.91206e+10, 154.821, 0, -3.91206e+10 (97.446%), 6.484356e-04
10.	-3.91863e+10, 177.114, 0, -3.91863e+10 (97.3824%), 7.132792e-04
11.	-3.91905e+10, 201.783, 0, -3.91905e+10 (97.3782%), 7.846071e-04
12.	-3.92553e+10, 233.413, 0, -3.92553e+10 (97.3156%), 8.630678e-04
13.	-3.92751e+10, 247.368, 0, -3.92751e+10 (97.2963%), 4.746873e-04
14.	-3.93078e+10, 267.542, 0, -3.93078e+10 (97.2648%), 5.221560e-04
15.	-3.93586e+10, 288.892, 0, -3.93586e+10 (97.2156%), 5.743716e-04
16.	-3.93973e+10, 313.3, 0, -3.93973e+10 (97.178%), 6.318088e-04
17.	-3.94447e+10, 339.421, 0, -3.94447e+10 (97.1322%), 6.949897e-04
18.	-3.94722e+10, 353.84, 0, -3.94722e+10 (97.1056%), 3.822443e-04
19.	-3.95246e+10, 370.166, 0, -3.95246e+10 (97.0548%), 4.204687e-04
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
20.	-3.95384e+10, 379.368, 0, -3.95384e+10 (97.0414%), 2.312578e-04
21.	-3.95552e+10, 389.413, 0, -3.95552e+10 (97.0251%), 2.543836e-04
22.	-3.95759e+10, 400.995, 0, -3.95759e+10 (97.0051%), 2.798220e-04
23.	-3.96011e+10, 413.854, 0, -3.96011e+10 (96.9807%), 3.078041e-04
24.	-3.96253e+10, 428.618, 0, -3.96253e+10 (96.9573%), 3.385846e-04
25.	-3.96537e+10, 443.74, 0, -3.96537e+10 (96.9298%), 3.724430e-04
26.	-3.96854e+10, 462.412, 0, -3.96854e+10 (96.8991%), 4.096873e-04
27.	-3.97094e+10, 482.548, 0, -3.97094e+10 (96.8759%), 4.506561e-04
28.	-3.97436e+10, 507.129, 0, -3.97436e+10 (96.8428%), 4.957217e-04
29.	-3.97694e+10, 534.48, 0, -3.97694e+10 (96.8178%), 5.452938e-04
30.	-3.98169e+10, 567.403, 0, -3.98169e+10 (96.7718%), 5.998232e-04
31.	-3.98412e+10, 600.058, 0, -3.98412e+10 (96.7483%), 6.598055e-04
32.	-3.9881e+10, 619.035, 0, -3.9881e+10 (96.7097%), 3.628930e-04
33.	-3.99012e+10, 636.276, 0, -3.99012e+10 (96.6902%), 3.991823e-04
34.	-3.99328e+10, 659.62, 0, -3.99328e+10 (96.6596%), 4.391006e-04
35.	-3.99671e+10, 683.603, 0, -3.99671e+10 (96.6264%), 4.830106e-04
36.	-3.99819e+10, 712.959, 0, -3.99819e+10 (96.6121%), 5.313117e-04
37.	-4.00038e+10, 740.52, 0, -4.00038e+10 (96.5908%), 5.844429e-04
38.	-4.0058e+10, 758.15, 0, -4.0058e+10 (96.5384%), 3.214436e-04
39.	-4.00772e+10, 775.768, 0, -4.00772e+10 (96.5198%), 3.535879e-04
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
40.	-4.01116e+10, 797.046, 0, -4.01116e+10 (96.4864%), 3.889467e-04
41.	-4.01576e+10, 819.82, 0, -4.01576e+10 (96.4419%), 4.278414e-04
42.	-4.01909e+10, 844.745, 0, -4.01909e+10 (96.4097%), 4.706255e-04
43.	-4.02324e+10, 873.495, 0, -4.02324e+10 (96.3695%), 5.176881e-04
44.	-4.02512e+10, 906.562, 0, -4.02512e+10 (96.3513%), 5.694569e-04
45.	-4.03058e+10, 923.102, 0, -4.03058e+10 (96.2984%), 3.132013e-04
46.	-4.03168e+10, 945.138, 0, -4.03168e+10 (96.2878%), 3.445214e-04
47.	-4.03418e+10, 965.928, 0, -4.03418e+10 (96.2635%), 3.789736e-04
48.	-4.03533e+10, 991.572, 0, -4.03533e+10 (96.2524%), 4.168709e-04
49.	-4.0391e+10, 1017.28, 0, -4.0391e+10 (96.2159%), 4.585580e-04
50.	-4.03922e+10, 1033.81, 0, -4.03922e+10 (96.2148%), 2.522069e-04
51.	-4.04158e+10, 1050.22, 0, -4.04158e+10 (96.1919%), 2.774276e-04
52.	-4.04355e+10, 1070.61, 0, -4.04355e+10 (96.1729%), 3.051704e-04
53.	-4.04595e+10, 1091.01, 0, -4.04595e+10 (96.1496%), 3.356874e-04
54.	-4.0479e+10, 1115.57, 0, -4.0479e+10 (96.1307%), 3.692561e-04
55.	-4.05074e+10, 1142.89, 0, -4.05074e+10 (96.1032%), 4.061818e-04
56.	-4.0519e+10, 1172.03, 0, -4.0519e+10 (96.092%), 4.467999e-04
57.	-4.05291e+10, 1202.56, 0, -4.05291e+10 (96.0822%), 4.914799e-04
58.	-4.0534e+10, 1221.45, 0, -4.0534e+10 (96.0775%), 2.703140e-04
59.	-4.05511e+10, 1238.73, 0, -4.05511e+10 (96.0609%), 2.973454e-04
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
60.	-4.05633e+10, 1260.56, 0, -4.05633e+10 (96.0491%), 3.270799e-04
61.	-4.05832e+10, 1283.21, 0, -4.05832e+10 (96.0299%), 3.597879e-04
62.	-4.05909e+10, 1310.58, 0, -4.05909e+10 (96.0223%), 3.957667e-04
63.	-4.06085e+10, 1337.81, 0, -4.06085e+10 (96.0054%), 4.353433e-04
64.	-4.06087e+10, 1355.36, 0, -4.06087e+10 (96.0051%), 2.394388e-04
65.	-4.06296e+10, 1372.36, 0, -4.06296e+10 (95.9849%), 2.633827e-04
66.	-4.06306e+10, 1391.87, 0, -4.06306e+10 (95.9839%), 2.897210e-04
67.	-4.06504e+10, 1412.86, 0, -4.06504e+10 (95.9648%), 3.186931e-04
68.	-4.0651e+10, 1418.84, 0, -4.0651e+10 (95.9642%), 8.764060e-05
69.	-4.06528e+10, 1425.42, 0, -4.06528e+10 (95.9625%), 9.640466e-05
70.	-4.06554e+10, 1432.58, 0, -4.06554e+10 (95.9599%), 1.060451e-04
71.	-4.06599e+10, 1440.33, 0, -4.06599e+10 (95.9555%), 1.166496e-04
72.	-4.06651e+10, 1448.62, 0, -4.06651e+10 (95.9506%), 1.283146e-04
73.	-4.06697e+10, 1457.55, 0, -4.06697e+10 (95.9461%), 1.411461e-04
74.	-4.06751e+10, 1467.11, 0, -4.06751e+10 (95.9409%), 1.552607e-04
75.	-4.06826e+10, 1477.54, 0, -4.06826e+10 (95.9336%), 1.707867e-04
76.	-4.06891e+10, 1488.99, 0, -4.06891e+10 (95.9273%), 1.878654e-04
77.	-4.06948e+10, 1501.84, 0, -4.06948e+10 (95.9218%), 2.066519e-04
78.	-4.07017e+10, 1516.29, 0, -4.07017e+10 (95.9151%), 2.273171e-04
79.	-4.07098e+10, 1531.93, 0, -4.07098e+10 (95.9073%), 2.500489e-04
	E, E_velocity, E_rate, E_image (E_image %), LearningRate
80.	-4.0716e+10, 1549.46, 0, -4.0716e+10 (95.9013%), 2.750537e-04
81.	-4.0724e+10, 1569.1, 0, -4.0724e+10 (95.8935%), 3.025591e-04
82.	-4.07326e+10, 1591.27, 0, -4.07326e+10 (95.8852%), 3.328150e-04
83.	-4.07402e+10, 1615.68, 0, -4.07402e+10 (95.8778%), 3.660965e-04
84.	-4.07574e+10, 1643.06, 0, -4.07574e+10 (95.8612%), 4.027062e-04
85.	-4.07624e+10, 1674.54, 0, -4.07624e+10 (95.8563%), 4.429768e-04
86.	-4.07851e+10, 1708.37, 0, -4.07851e+10 (95.8343%), 4.872745e-04
87.	-4.07901e+10, 1745.58, 0, -4.07901e+10 (95.8295%), 5.360019e-04
88.	-4.08279e+10, 1787.73, 0, -4.08279e+10 (95.7929%), 5.896021e-04
89.	-4.08421e+10, 1833.85, 0, -4.08421e+10 (95.7792%), 6.485623e-04
90.	-4.08793e+10, 1884.75, 0, -4.08793e+10 (95.7432%), 7.134186e-04
91.	-4.08864e+10, 1911.73, 0, -4.08864e+10 (95.7363%), 3.923802e-04
92.	-4.0913e+10, 1945.41, 0, -4.0913e+10 (95.7105%), 4.316182e-04
93.	-4.09328e+10, 1981.23, 0, -4.09328e+10 (95.6914%), 4.747801e-04
94.	-4.09493e+10, 2026.09, 0, -4.09493e+10 (95.6754%), 5.222581e-04
95.	-4.09654e+10, 2062.61, 0, -4.09654e+10 (95.6597%), 5.744839e-04
96.	-4.09843e+10, 2087.3, 0, -4.09843e+10 (95.6415%), 3.159661e-04
97.	-4.10159e+10, 2109.64, 0, -4.10159e+10 (95.6108%), 3.475627e-04
E = -4.10159e+10 (95.6108%)
Length = 63.329
Time = 912.603s (15.2101m)

We now create a composite of the affine and displacement fields


In [37]:
affineField = affineToField(affine, field.GetSize(), field.GetSpacing())
fieldComposite = fieldApplyField(field, affineField)

invAffineField = affineToField(affineInverse(affine), invField.GetSize(), invField.GetSpacing())
invFieldComposite = fieldApplyField(invAffineField, invField)

Now we apply the displacement field


In [38]:
sampleImg_lddmm = imgApplyField(sampleImgReoriented, fieldComposite, size=refImg.GetSize(), spacing=refImg.GetSpacing())
imgShow(sampleImg_lddmm, vmax=sampleThreshold)


Evaluation LDDMM registration

Now we evaluate the deformable registration using a checkerboard image


In [39]:
sampleImg_lddmm_ds = imgResample(sampleImg_lddmm, spacing=spacing, size=refImg_ds.GetSize())
imgShow(imgChecker(sampleImg_lddmm, refImg), vmax=refThreshold)


We can also evaluate the registration by overlaying the atlas annotations over the deformed input image.


In [40]:
imgShow(sampleImg_lddmm, vmax=sampleThreshold, newFig=False)
imgShow(refAnnotationImg, vmax=1000, cmap=randCmap, alpha=0.3, newFig=False)
plt.show()


Deform annotation to sample space and upload to Boss


In [149]:
SAMPLE_COLLECTION = 's3617_to_ara_coll'
SAMPLE_EXPERIMENT = 's3617_to_ara_exp'
SAMPLE_CHANNEL = 's3617_to_ara_10um_affine'
NEW_CHANNEL_NAME = 'annotation_ara_s3617_test4'
SAMPLE_RESOLUTION = 0
CHANNEL_TYPE = 'annotation'
DATATYPE = 'uint64'

In [89]:
refAnnotationImg_lddmm = imgApplyField(refAnnotationImg, invFieldComposite, size=sampleImgReoriented.GetSize(), spacing=sampleImgReoriented.GetSpacing(), useNearest=True)

In [90]:
# convert the reference image to the same orientation as the input image
refAnnotationImg_lddmm = imgReorient(refAnnotationImg_lddmm, refOrient, sampleOrient)

In [91]:
imgShow(refAnnotationImg_lddmm, vmax=1000, cmap=randCmap)



In [150]:
new_channel_resource = ChannelResource(NEW_CHANNEL_NAME, SAMPLE_COLLECTION, SAMPLE_EXPERIMENT, type=CHANNEL_TYPE, 
                                    base_resolution=SAMPLE_RESOLUTION, sources=[SAMPLE_CHANNEL], datatype=DATATYPE)
new_rsc = rmt.create_project(new_channel_resource)
#channel = rmt.get_channel(NEW_CHANNEL_NAME, SAMPLE_COLLECTION, SAMPLE_EXPERIMENT)

In [151]:
new_rsc.cutout_ready


Out[151]:
True

In [25]:
import tifffile as tf

In [36]:
data = tf.imread('s3617_to_ara_affine_10um.tif')
data.dtype


Out[36]:
dtype('uint16')

In [37]:
data.shape


Out[37]:
(1140, 800, 1320)

In [144]:
data = sitk.GetArrayFromImage(refAnnotationImg)

In [145]:
data.shape


Out[145]:
(1140, 800, 1320)

In [95]:
size = sampleImg.GetSize()
refAnnotationImg_lddmm_np = sitk.GetArrayFromImage(refAnnotationImg_lddmm)
refAnnotationImg_lddmm_np = refAnnotationImg_lddmm_np.transpose()
refAnnotationImg_lddmm_np = refAnnotationImg_lddmm_np.copy(order='C')

In [96]:
plt.imshow(refAnnotationImg_lddmm_np[:,:,150], vmax=1000, cmap=randCmap)


Out[96]:
<matplotlib.image.AxesImage at 0x7fcbc5473810>

In [146]:
data.flags['C_CONTIGUOUS']


Out[146]:
True

In [152]:
def upload_to_boss(data, channel_resource, resolution=0):
    Z_LOC = 2
    size = data.shape
    for i in range(0, data.shape[Z_LOC], 16):
        last_z = i+16
        if last_z > data.shape[Z_LOC]:
            last_z = data.shape[Z_LOC]
        print(resolution, [i, last_z], [0, size[1]], [0, size[0]])
        rmt.create_cutout(channel_resource, resolution, [i, last_z], [0, size[1]], [0, size[0]], np.asarray(data[:,:,i:last_z], order='C'))

In [153]:
upload_to_boss(data, new_rsc)


(0, [0, 16], [0, 800], [0, 1140])
(0, [16, 32], [0, 800], [0, 1140])
(0, [32, 48], [0, 800], [0, 1140])
(0, [48, 64], [0, 800], [0, 1140])
(0, [64, 80], [0, 800], [0, 1140])

KeyboardInterruptTraceback (most recent call last)
<ipython-input-153-30eb83690834> in <module>()
----> 1 upload_to_boss(data, new_rsc)

<ipython-input-152-fedb33262d7f> in upload_to_boss(data, channel_resource, resolution)
      7             last_z = data.shape[Z_LOC]
      8         print(resolution, [i, last_z], [0, size[1]], [0, size[0]])
----> 9         rmt.create_cutout(channel_resource, resolution, [i, last_z], [0, size[1]], [0, size[0]], np.asarray(data[:,:,i:last_z], order='C'))

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/remote/remote.pyc in create_cutout(self, resource, resolution, x_range, y_range, z_range, data, time_range)
    173             raise RuntimeError('Resource incompatible with the volume service.')
    174         return self._volume.create_cutout(
--> 175             resource, resolution, x_range, y_range, z_range, data, time_range)
    176 
    177     def reserve_ids(self, resource, num_ids):

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/volume.pyc in wrapper(*args, **kwargs)
     35                     'ChannelResource not fully initialized.  Use intern.remote.BossRemote.get_channel({}, {}, {})'.format(
     36                         args[1].name, args[1].coll_name, args[1].exp_name))
---> 37         return fcn(*args, **kwargs)
     38 
     39     return wrapper

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/volume.pyc in create_cutout(self, resource, resolution, x_range, y_range, z_range, numpyVolume, time_range)
     78         return self.service.create_cutout(
     79             resource, resolution, x_range, y_range, z_range, time_range, numpyVolume,
---> 80             self.url_prefix, self.auth, self.session, self.session_send_opts)
     81 
     82     @check_channel

/usr/local/lib/python2.7/dist-packages/intern-0.9.4-py2.7.egg/intern/service/boss/v1/volume.pyc in create_cutout(self, resource, resolution, x_range, y_range, z_range, time_range, numpyVolume, url_prefix, auth, session, send_opts)
     83             resolution, x_range, y_range, z_range, time_range, numpyVolume=compressed)
     84         prep = session.prepare_request(req)
---> 85         resp = session.send(prep, **send_opts)
     86 
     87         if resp.status_code == 201:

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/sessions.pyc in send(self, request, **kwargs)
    594 
    595         # Send the request
--> 596         r = adapter.send(request, **kwargs)
    597 
    598         # Total elapsed time of the request (approximately)

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/adapters.pyc in send(self, request, stream, timeout, verify, cert, proxies)
    421                     decode_content=False,
    422                     retries=self.max_retries,
--> 423                     timeout=timeout
    424                 )
    425 

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/packages/urllib3/connectionpool.pyc in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)
    593                                                   timeout=timeout_obj,
    594                                                   body=body, headers=headers,
--> 595                                                   chunked=chunked)
    596 
    597             # If we're going to release the connection in ``finally:``, then

/usr/local/lib/python2.7/dist-packages/requests-2.11.1-py2.7.egg/requests/packages/urllib3/connectionpool.pyc in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    384         try:
    385             try:  # Python 2.7, use buffering of HTTP responses
--> 386                 httplib_response = conn.getresponse(buffering=True)
    387             except TypeError:  # Python 2.6 and older, Python 3
    388                 try:

/usr/lib/python2.7/httplib.pyc in getresponse(self, buffering)
   1134 
   1135         try:
-> 1136             response.begin()
   1137             assert response.will_close != _UNKNOWN
   1138             self.__state = _CS_IDLE

/usr/lib/python2.7/httplib.pyc in begin(self)
    451         # read until we get a non-100 response
    452         while True:
--> 453             version, status, reason = self._read_status()
    454             if status != CONTINUE:
    455                 break

/usr/lib/python2.7/httplib.pyc in _read_status(self)
    407     def _read_status(self):
    408         # Initialize with Simple-Response defaults
--> 409         line = self.fp.readline(_MAXLINE + 1)
    410         if len(line) > _MAXLINE:
    411             raise LineTooLong("header line")

/usr/lib/python2.7/socket.pyc in readline(self, size)
    478             while True:
    479                 try:
--> 480                     data = self._sock.recv(self._rbufsize)
    481                 except error, e:
    482                     if e.args[0] == EINTR:

/usr/lib/python2.7/ssl.pyc in recv(self, buflen, flags)
    754                     "non-zero flags not allowed in calls to recv() on %s" %
    755                     self.__class__)
--> 756             return self.read(buflen)
    757         else:
    758             return self._sock.recv(buflen, flags)

/usr/lib/python2.7/ssl.pyc in read(self, len, buffer)
    641                 v = self._sslobj.read(len, buffer)
    642             else:
--> 643                 v = self._sslobj.read(len)
    644             return v
    645         except SSLError as x:

KeyboardInterrupt: 

In [58]:
print(refAnnotationImg_lddmm_np.shape[-1])
for i in range(0, refAnnotationImg_lddmm_np.shape[-1], 16):
    last_z = i+16
    if last_z > refAnnotationImg_lddmm_np.shape[-1]:
        last_z = refAnnotationImg_lddmm_np.shape[-1]
#     tmp_array = np.zeros(refAnnotationImg_lddmm_np[:,:,i:last_z].shape, order='C')
#     tmp_array[:,:,:] = refAnnotationImg_lddmm_np[:,:,i:last_z]
#     tmp_array = np.ascontiguousarray(refAnnotationImg_lddmm_np[:,:,i:last_z])
    tmp_array = refAnnotationImg_lddmm_np[:,:,i:last_z].copy(order='C')
    #print('c contiguous?: ' + str(tmp_array.flags['C_CONTIGUOUS']))
    print(SAMPLE_RESOLUTION, [0, size[0]], [0, size[1]], [i, last_z])
    rmt.create_cutout(new_rsc, SAMPLE_RESOLUTION, [0, size[0]], [0, size[1]], [i, last_z], tmp_array)


356
c contiguous?: True
(0, [0, 540], [0, 640], [0, 16])
c contiguous?: True
(0, [0, 540], [0, 640], [16, 32])
c contiguous?: True
(0, [0, 540], [0, 640], [32, 48])
c contiguous?: True
(0, [0, 540], [0, 640], [48, 64])
c contiguous?: True
(0, [0, 540], [0, 640], [64, 80])
c contiguous?: True
(0, [0, 540], [0, 640], [80, 96])
c contiguous?: True
(0, [0, 540], [0, 640], [96, 112])
c contiguous?: True
(0, [0, 540], [0, 640], [112, 128])
c contiguous?: True
(0, [0, 540], [0, 640], [128, 144])
c contiguous?: True
(0, [0, 540], [0, 640], [144, 160])
c contiguous?: True
(0, [0, 540], [0, 640], [160, 176])
c contiguous?: True
(0, [0, 540], [0, 640], [176, 192])
c contiguous?: True
(0, [0, 540], [0, 640], [192, 208])
c contiguous?: True
(0, [0, 540], [0, 640], [208, 224])
c contiguous?: True
(0, [0, 540], [0, 640], [224, 240])
c contiguous?: True
(0, [0, 540], [0, 640], [240, 256])
c contiguous?: True
(0, [0, 540], [0, 640], [256, 272])
c contiguous?: True
(0, [0, 540], [0, 640], [272, 288])
c contiguous?: True
(0, [0, 540], [0, 640], [288, 304])
c contiguous?: True
(0, [0, 540], [0, 640], [304, 320])
c contiguous?: True
(0, [0, 540], [0, 640], [320, 336])
c contiguous?: True
(0, [0, 540], [0, 640], [336, 352])
c contiguous?: True
(0, [0, 540], [0, 640], [352, 356])