# Path: src/utils.py
# def splitext(s):
#     try :
#         ssplit = os.path.basename(s).split('.')
#         ext='.'+'.'.join(ssplit[1:])
#         basepath= re.sub(ext,'',s)
#         return [basepath, ext]
#     except TypeError :  
#         return s
# 
# def cmd(command):
#     try:
#         output = subprocess.check_output(command,stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
#     except subprocess.CalledProcessError as exc:
#         print("Status : FAIL", exc.returncode, exc.output)
#         exit(1)
#     else:
#         print("Output: \n{}\n".format(output))


from nipype.interfaces.base import (TraitedSpec, File, traits, InputMultiPath, 
        BaseInterface, OutputMultiPath, BaseInterfaceInputSpec, isdefined)
from nipype.interfaces.ants import registration, segmentation
from nipype.interfaces.ants.segmentation import Atropos
from nipype.interfaces.ants import Registration, ApplyTransforms
from nipype.interfaces.utility import Rename
from nipype.interfaces.ants.registration import CompositeTransformUtil, CompositeTransformUtilInputSpec
from nipype.interfaces.ants.resampling import ApplyTransformsInputSpec
from nipype.interfaces.base import InputMultiPath
from src.utils import splitext, cmd
from scipy.io import loadmat
from scipy.ndimage import center_of_mass
import numpy as np
import nibabel as nib
import nipype.pipeline.engine as pe
import SimpleITK as sitk
import os
import re
        if not os.path.exists(self.inputs.warped_image) :
            cmdline = "antsApplyTransforms -e 3 -d 3 -n Linear  -i "+self.inputs.moving_image+" -t "+ self.inputs.out_matrix +" -r "+self.inputs.fixed_image+" -o "+self.inputs.warped_image
            print(cmdline)
            cmd( cmdline  )

        if not os.path.exists(self.inputs.out_matrix_inverse) :
            cmdline = "antsApplyTransforms -e 3 -d 3  -n Linear -i "+self.inputs.moving_image+" -t "+self.inputs.out_matrix +" -r "+self.inputs.fixed_image+" -o Linear["+self.inputs.out_matrix_inverse+",1]"
            print(cmdline)
            cmd( cmdline  )

        def mat2txt(self, ii_fn, oo_fn):
            print(ii_fn, oo_fn)
            tfm=sitk.ReadTransform(ii_fn)
            sitk.WriteTransform( tfm, oo_fn  )
            return 0 

    def _run_interface(self, runtime):
        normalization_type = self.inputs.normalization_type

        #Setup ANTs command line arguments
        if isdefined(self.inputs.user_ants_command):
            cmdline, self.inputs.normalization_type = self.read_user_command_line()
            self._set_outputs()
            cmdline = self.replace_user_command_line(cmdline)
        else :
            self._set_outputs()
            cmdline = self.default_command_line()
        print(self.inputs); 
        #Run antsRegistration on command line
        print("Ants command line:\n", cmdline)

# Path: src/utils.py
# def splitext(s):
#     try :
#         ssplit = os.path.basename(s).split('.')
#         ext='.'+'.'.join(ssplit[1:])
#         basepath= re.sub(ext,'',s)
#         return [basepath, ext]
#     except TypeError :  
#         return s
# 
# def cmd(command):
#     try:
#         output = subprocess.check_output(command,stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
#     except subprocess.CalledProcessError as exc:
#         print("Status : FAIL", exc.returncode, exc.output)
#         exit(1)
#     else:
#         print("Output: \n{}\n".format(output))


# Path: src/ants.py
from nipype.interfaces.base import (TraitedSpec, File, traits, InputMultiPath, 
        BaseInterface, OutputMultiPath, BaseInterfaceInputSpec, isdefined)
from nipype.interfaces.ants import registration, segmentation
from nipype.interfaces.ants.segmentation import Atropos
from nipype.interfaces.ants import Registration, ApplyTransforms
from nipype.interfaces.utility import Rename
from nipype.interfaces.ants.registration import CompositeTransformUtil, CompositeTransformUtilInputSpec
from nipype.interfaces.ants.resampling import ApplyTransformsInputSpec
from nipype.interfaces.base import InputMultiPath
from src.utils import splitext, cmd
from scipy.io import loadmat
from scipy.ndimage import center_of_mass
import numpy as np
import nibabel as nib
import nipype.pipeline.engine as pe
import SimpleITK as sitk
import os
import re

        if not os.path.exists(self.inputs.warped_image) :
            cmdline = "antsApplyTransforms -e 3 -d 3 -n Linear  -i "+self.inputs.moving_image+" -t "+ self.inputs.out_matrix +" -r "+self.inputs.fixed_image+" -o "+self.inputs.warped_image
            print(cmdline)
            cmd( cmdline  )

        if not os.path.exists(self.inputs.out_matrix_inverse) :
            cmdline = "antsApplyTransforms -e 3 -d 3  -n Linear -i "+self.inputs.moving_image+" -t "+self.inputs.out_matrix +" -r "+self.inputs.fixed_image+" -o Linear["+self.inputs.out_matrix_inverse+",1]"
            print(cmdline)
            cmd( cmdline  )

        def mat2txt(self, ii_fn, oo_fn):
            print(ii_fn, oo_fn)
            tfm=sitk.ReadTransform(ii_fn)
            sitk.WriteTransform( tfm, oo_fn  )
            return 0 

    def _run_interface(self, runtime):
        normalization_type = self.inputs.normalization_type

        #Setup ANTs command line arguments
        if isdefined(self.inputs.user_ants_command):
            cmdline, self.inputs.normalization_type = self.read_user_command_line()
            self._set_outputs()
            cmdline = self.replace_user_command_line(cmdline)
        else :
            self._set_outputs()
            cmdline = self.default_command_line()
        print(self.inputs); 
        #Run antsRegistration on command line
        print("Ants command line:\n", cmdline)

        p = cmd(cmdline)	