# 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

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs["output_image"] =  self.inputs.output_image
        outputs["output_image_inverse"] =  self.inputs.output_image_inverse

        return outputs

    def _parse_inputs(self, skip=None):
        if skip is None:
            skip = []
        return super(APPIANApplyTransforms, self)._parse_inputs(skip=skip)


class APPIANConcatenateTransformsInputSpec(BaseInterfaceInputSpec) :
    transform_1 = traits.File(mandatory=True, exists=True) 
    transform_2 = traits.File(mandatory=True, exists=True)
    #reference_image = traits.File()
    out_file = traits.File(desc="Composite transorfmation matrix")

class APPIANConcatenateTransformsOutputSpec(TraitedSpec):
    out_file = traits.File(desc="Composite transorfmation matrix")

class APPIANConcatenateTransforms(BaseInterface):
    input_spec = APPIANConcatenateTransformsInputSpec
    output_spec= APPIANConcatenateTransformsOutputSpec
    
    def _run_interface(self, runtime):
        #Get extension for input transformation files
        ext_1=splitext(self.inputs.transform_1)[1]

# 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


    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs["output_image"] =  self.inputs.output_image
        outputs["output_image_inverse"] =  self.inputs.output_image_inverse

        return outputs

    def _parse_inputs(self, skip=None):
        if skip is None:
            skip = []
        return super(APPIANApplyTransforms, self)._parse_inputs(skip=skip)


class APPIANConcatenateTransformsInputSpec(BaseInterfaceInputSpec) :
    transform_1 = traits.File(mandatory=True, exists=True) 
    transform_2 = traits.File(mandatory=True, exists=True)
    #reference_image = traits.File()
    out_file = traits.File(desc="Composite transorfmation matrix")

class APPIANConcatenateTransformsOutputSpec(TraitedSpec):
    out_file = traits.File(desc="Composite transorfmation matrix")

class APPIANConcatenateTransforms(BaseInterface):
    input_spec = APPIANConcatenateTransformsInputSpec
    output_spec= APPIANConcatenateTransformsOutputSpec
    
    def _run_interface(self, runtime):
        #Get extension for input transformation files
        ext_1=splitext(self.inputs.transform_1)[1]

        ext_2=splitext(self.inputs.transform_2)[1]