<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Fractional Anisotropy Maps - Steps and Results
On Thursday, we showed Greg the output of the first step of the CAPTURE pipeline - namely, after modifying the CAPTURE MATLAB pipeline to accept TIFF files (originally it only took TIFs), we were able to generate two structure tensors from a TIFF stack of Aut1367 originally for use in Ilastik analysis.  The main steps for the generation of the structure tensors are explained in a separate viewer (we showed Greg this) on Thursday
Step2:  Subsampling
Step3:  Results
<ASSISTANT_TASK:>
Python Code:
from dipy.reconst.dti import fractional_anisotropy, color_fa
from argparse import ArgumentParser
from scipy import ndimage
import os
import re
import numpy as np
import nibabel as nb
import sys
import matplotlib
matplotlib.use('Agg')  # very important above pyplot import
import matplotlib.pyplot as plt
import vtk
from dipy.reconst.dti import from_lower_triangular
img = nb.load('../../../../../Desktop/result/dogsig1_gausig2.3/v100_ch0_tensorfsl_dogsig1_gausig2.3.nii')
data = img.get_data()
# Output is the structure tensor generated from a lower triangular structure tensor (which data is)
output = from_lower_triangular(data)
Explanation: Fractional Anisotropy Maps - Steps and Results
On Thursday, we showed Greg the output of the first step of the CAPTURE pipeline - namely, after modifying the CAPTURE MATLAB pipeline to accept TIFF files (originally it only took TIFs), we were able to generate two structure tensors from a TIFF stack of Aut1367 originally for use in Ilastik analysis.  The main steps for the generation of the structure tensors are explained in a separate viewer (we showed Greg this) on Thursday:  http://nbviewer.jupyter.org/github/NeuroDataDesign/seelviz/blob/gh-pages/Tony/ipynb/Generating%20Structure%20Tensors.ipynb  
There were two separate structure tensors generated by the CAPTURE pipeline - one was "DTK" (which could be used later in the Diffusion ToolKit process) and the other was "FSL" (an alternate file format).  We realized at office hours that the structure tensors (which were 5000 x 5000 x 5 x 6) each were the "lower triangular" values from the structures.
From there, we first tried to use the DTK file directly inside Diffusion ToolKit, but were informed that the "file appeared to be corrupted/missing data".  Only the FSL format seemed to have properly saved all the image data (likely because it was run first during the MATLAB script, and because generating the structure tensors froze Tony's computer, so the DTK file format was corrupted.  Thus, all analysis was done on the FSL file.  
From there, we followed the DiPy tutorial/ndmg code that was suitable for generating FA maps (as recommended by Greg).
End of explanation
output_ds = output[4250:4300, 250:300, :, :, :]
print output.shape
print output_ds.shape
FA = fractional_anisotropy(output_ds)
FA = np.clip(FA, 0, 1)
FA[np.isnan(FA)] = 0
print FA.shape
from dipy.reconst.dti import decompose_tensor
evalues, evectors = decompose_tensor(output_ds)
print evectors[..., 0, 0].shape
print evectors.shape[-2:]
print FA[:, :, :, 0].shape
## To satisfy requirements for RGB
RGB = color_fa(FA[:, :, :, 0], evectors)
nb.save(nb.Nifti1Image(np.array(255 * RGB, 'uint8'), img.get_affine()), 'tensor_rgb_upper.nii.gz')
print('Computing tensor ellipsoids in a random part')
from dipy.data import get_sphere
sphere = get_sphere('symmetric724')
from dipy.viz import fvtk
ren = fvtk.ren()
evals = evalues[:, :, :]
evecs = evectors[:, :, :]
print "printing evals:"
print evals
print "printing evecs"
print evecs
cfa = RGB[:, :, :]
cfa = cfa / cfa.max()
print "printing cfa"
print cfa
fvtk.add(ren, fvtk.tensor(evals, evecs, cfa, sphere))
from IPython.display import Image
def vtk_show(renderer, width=400, height=300):
    
    Takes vtkRenderer instance and returns an IPython Image with the rendering.
    
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
    renderWindow.AddRenderer(renderer)
    renderWindow.SetSize(width, height)
    renderWindow.Render()
     
    windowToImageFilter = vtk.vtkWindowToImageFilter()
    windowToImageFilter.SetInput(renderWindow)
    windowToImageFilter.Update()
     
    writer = vtk.vtkPNGWriter()
    writer.SetWriteToMemory(1)
    writer.SetInputConnection(windowToImageFilter.GetOutputPort())
    writer.Write()
    data = str(buffer(writer.GetResult()))
    
    return Image(data)
Explanation: Subsampling:
We added this step because the calculation of RGB/eigenvalues/eigenvectors took much too long on the full file.  Even still, with small sizes like 25x25, the last VTK rendering step took significant amounts of time.  In the pipeline we'll have to think of a more optimal way to compute these, and we're guessing we're missing something (since why is this taking so long)?
End of explanation
# x = 4250:4300, y = 250:300, z = : on Tony's computer (doesn't show anything)
# Thus, all results were displayed after running on Albert's computer
vtk_show(ren)
Explanation: Results:
End of explanation
<END_TASK>