<jupyter_start><jupyter_text>Registration of zebrafish brains to a reference Z-Brainusing [ANTs](http://stnava.github.io/ANTs/) registration tools.Zebrafish brains expressing GCaMP6f were imaged live with [Zebrascope](https://www.nature.com/nmeth/journal/v11/n9/full/nmeth.3040.html) light-sheet microscope and down-sampled (5 microns/px in XYZ) to speed up calculations.The reference Z-Brains were downloaded from [Z-Brain official page](http://engertlab.fas.harvard.edu/Z-Brain/about.html), down-sampled and converted to NRRD format using [ImageJ/FIJI](https://fiji.sc/).The code was tested on Windows 7, [Anaconda](https://docs.continuum.io/anaconda/install) Python 2.7 distribution.Unzip files  `ZBrains.zip` and `raw_brains.zip` before running the code.<jupyter_code>import subprocess
import os
import numpy as np
#Your working directory containing the cloned code
parentDir = 'C:/Users/username/Documents/ZebrafishBrainRegistration_Zbrain_ANTs/'

#Path to the ANTs exe-files (Windows)
binPath = parentDir + 'ANTs_2.1.0_Windows/'

#Path to down-sampled Z-brain stacks (5 um isotropic resolution)
pathZbrain_H2B = parentDir + 'ZBrains/zbrain-H2B-RFP_6dpf_mean10fish_8bit_xyz5um.nrrd'
pathZbrain_Cyto = parentDir + 'ZBrains/zbrain-tERK-8bit_xyz5um.nrrd'
# nMLF ablations, Cyto fish,
ablationName = 'nMLF'
h2b = False #set True if aligning (nuclear) H2B-expressing markers, False is cytosolically expressed marker

rawDirList =  ['2016-10-18fish1/pre/',\
               '2016-10-18fish1/post/',\
               
               '2016-10-18fish3/pre/',\
               '2016-10-18fish3/post/',\
               
               '2016-10-24fish1/pre/',\
               '2016-10-24fish1/post/'           
              ]
signalType = 'Fstim' 
procDirs = []
for dirName in rawDirList:
    procDirs.append(parentDir + 'raw_brains/' + dirName)<jupyter_output><empty_output><jupyter_text>Align post- to pre-ablation using down-sampled Ref maps<jupyter_code>for i_exp in range(1,len(procDirs),2):
    procDir = procDirs[i_exp]
    print str(1+ (i_exp-1)/2) + ' of ' + str(len(procDirs)/2)  
    os.chdir(procDir)
    title = os.path.basename(os.path.split(os.path.split(os.path.split(procDir)[0])[0])[0])

    filename_fixed = procDirs[i_exp - 1] + 'ref_xyz5um.nrrd' #pre-ablation file, fixed (target)
    filename_movingNRRD = procDir + 'ref_xyz5um.nrrd' #post-ablation (moving)

    filename_aligned = procDir +'refpost2pre_xyz5um.nrrd'

    args = binPath + 'antsRegistration.exe '  + \
            ' --float 0 ' + \
            ' --dimensionality 3 ' + \
            ' --interpolation BSpline ' + \
            ' --use-histogram-matching 1 ' +\
            ' --winsorize-image-intensities [0.005,0.995] ' +\
            ' --output [refpost2pre_xyz5um_,' + filename_aligned + '] ' + \
            ' --initial-moving-transform [' + filename_fixed + ',' + filename_movingNRRD + ',1] ' +\
            ' --transform Rigid[0.1]' +\
            ' --metric MI[' + filename_fixed + ',' + filename_movingNRRD + ',1,32,Regular,1.0]' +\
            ' --convergence [500x250x100,1e-6,10]' +\
            ' --shrink-factors 4x2x1' +\
            ' --smoothing-sigmas 2x1x0vox' +\
            ' --transform Affine[0.1] ' +\
            ' --metric MI[' + filename_fixed + ',' + filename_movingNRRD + ',1,32,Regular,1.0] ' +\
            ' --convergence [500x250x100,1e-6,10] ' +\
            ' --shrink-factors 4x2x1 ' +\
            ' --smoothing-sigmas 2x1x0vox' +\
            ' --transform SyN[0.1,3,0]' +\
            ' --metric CC[' + filename_fixed + ',' + filename_movingNRRD + ',1,4] ' +\
            ' --convergence [200x100x50,1e-6,10]' +\
            ' --shrink-factors 3x2x1' +\
            ' --smoothing-sigmas 2x1x0vox'
    logStr = subprocess.check_output(args)<jupyter_output>1 of 3
2 of 3
3 of 3<jupyter_text>Apply warping to Post-dFF activity  and Ref files<jupyter_code>for i_exp in range(1,len(procDirs),2):
    procDir = procDirs[i_exp]
    print str(1+ (i_exp-1)/2) + ' of ' + str(len(procDirs)/2)    
    os.chdir(procDir)
    
    title = os.path.basename(os.path.split(os.path.split(procDir)[0])[0])

    # pre- to -post
    transform1 = procDir + 'refpost2pre_xyz5um_1Warp.nii.gz'
    transform0 = procDir + 'refpost2pre_xyz5um_0GenericAffine.mat'

    # Sub-sampled dFF activity file warping
    filename_fixed = procDirs[i_exp - 1] + 'ref_xyz5um.nrrd' #pre-ablation file, fixed (target)
    filename_moving = procDir  + title + '_meanActivity' + signalType + '_xyz5um.nrrd'
    filename_warped = procDir + title + '_meanActivity' + signalType + '_xyz5um_warp2pre.nrrd'
    
    argsTransform = binPath + 'antsApplyTransforms.exe' + \
                    ' --dimensionality 3 ' + \
                    ' --input ' + filename_moving + \
            ' --reference-image ' + filename_fixed + \
            ' --output ' + filename_warped + \
            ' --interpolation Linear ' + \
            ' --transform ' + transform1 + \
            ' --transform ' + transform0 + \
            ' --default-value 0'
    logStrTransform = subprocess.check_output(argsTransform)<jupyter_output>1 of 3
2 of 3
3 of 3<jupyter_text>Subtract activity pre- (warped)post<jupyter_code>for i_exp in range(1,len(procDirs),2):
    procDir = procDirs[i_exp]
    title = os.path.basename(os.path.split(os.path.split(procDir)[0])[0])
    
    stack_pre, options = nrrd.read(procDirs[i_exp - 1] + title + '_meanActivity'+signalType+'_xyz5um.nrrd')
    stack_post, options = nrrd.read(procDir + title + '_meanActivity' + signalType + '_xyz5um_warp2pre.nrrd')
    
    stackDiff = stack_pre.astype('float32') - stack_post.astype('float32') 
    
    nrrd.write(procDir  + title + '_'+signalType+'_pre-post_warp_xyz5um.nrrd',stackDiff,options)<jupyter_output><empty_output><jupyter_text>Align reference pre-ablation files to Zbrain<jupyter_code>%%time
if h2b:
    filename_fixed = pathZbrain_H2B
else: 
    filename_fixed = pathZbrain_Cyto
    
for i_exp in range(1,len(procDirs),2):
    procDir = procDirs[i_exp - 1]
    print str(1+ (i_exp-1)/2) + ' of ' + str(len(procDirs)/2)     
    os.chdir(procDir)

    filename_movingNRRD = procDir + 'ref_xyz5um.nrrd' #ref file in pre-ablation

    filename_aligned = procDir + 'ref_2ZBrainDownsampled.nrrd'

    args = binPath + 'antsRegistration.exe '  + \
            ' --float 0 ' + \
            ' --dimensionality 3 ' + \
            ' --interpolation BSpline ' + \
            ' --use-histogram-matching 1 ' +\
            ' --winsorize-image-intensities [0.005,0.995] ' +\
            ' --output [ref_pre_2ZBrainDownsampled_,' + filename_aligned + '] ' + \
            ' --initial-moving-transform [' + filename_fixed + ',' + filename_movingNRRD + ',1] ' +\
            ' --transform Rigid[0.1]' +\
            ' --metric MI[' + filename_fixed + ',' + filename_movingNRRD + ',1,32,Regular,1.0]' +\
            ' --convergence [500x250x100,1e-6,10]' +\
            ' --shrink-factors 4x2x1' +\
            ' --smoothing-sigmas 2x1x0vox' +\
            ' --transform Affine[0.1] ' +\
            ' --metric MI[' + filename_fixed + ',' + filename_movingNRRD + ',1,32,Regular,1.0] ' +\
            ' --convergence [500x250x100,1e-6,10] ' +\
            ' --shrink-factors 4x2x1 ' +\
            ' --smoothing-sigmas 2x1x0vox' +\
            ' --transform SyN[0.1,3,0]' +\
            ' --metric MI[' + filename_fixed + ',' + filename_movingNRRD + ',1,32,Regular,1.0] ' +\
            ' --convergence [200x100x50,1e-6,10]' +\
            ' --shrink-factors 4x2x1' +\
            ' --smoothing-sigmas 2x1x0vox'
    logStr = subprocess.check_output(args)<jupyter_output>1 of 3
2 of 3
3 of 3
Wall time: 21min 12s<jupyter_text>Apply warping to Pre-Post dFF files to align with Z-brain<jupyter_code>if h2b:
    filename_fixed = pathZbrain_H2B
else: 
    filename_fixed = pathZbrain_Cyto
    
for i_exp in range(1,len(procDirs),2):
    procDir = procDirs[i_exp]
    print str(1+ (i_exp-1)/2) + ' of ' + str(len(procDirs)/2)    
    os.chdir(procDir)
    
    title = os.path.basename(os.path.split(os.path.split(procDir)[0])[0])

    # pre- to Zbrain
    transform1 = procDirs[i_exp - 1] + 'ref_pre_2ZBrainDownsampled_1Warp.nii.gz'
    transform0 = procDirs[i_exp - 1] + 'ref_pre_2ZBrainDownsampled_0GenericAffine.mat'

    filename_moving = procDir  + title + '_'+signalType+'_pre-post_warp_xyz5um.nrrd'
    filename_warped = procDir + title + '_'+signalType+'_pre-post_xyz5um_warp2Zbrain.nrrd'

    argsTransform = binPath + 'antsApplyTransforms.exe' + \
                    ' --dimensionality 3 ' + \
                    ' --input ' + filename_moving + \
            ' --reference-image ' + filename_fixed + \
            ' --output ' + filename_warped + \
            ' --interpolation Linear ' + \
            ' --transform ' + transform1 + \
            ' --transform ' + transform0 + \
            ' --default-value 0'
    logStrTransform = subprocess.check_output(argsTransform)<jupyter_output>1 of 3
2 of 3
3 of 3
{'fraction_non_alphanumeric': 0.14343253524278363, 'fraction_numerical': 0.04240322219735959, 'mean_word_length': 2.569888178913738, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '653208', 'license': 'MIT', 'n_tokens_mistral': 3500, 'n_tokens_neox': 3204, 'n_words': 544, 'path': 'registration_ZBrain-ANTs_nMLF_ablations.ipynb', 'repo_name': 'nvladimus/ZebrafishBrainRegistration_Zbrain_ANTs', 'size': '8938'}