# Path: src/utils.py
# class concat_df(BaseInterface):
#     input_spec =  concat_dfInput 
#     output_spec = concat_dfOutput 
# 
#     def _run_interface(self, runtime):
#         df=pd.DataFrame([])
#         test = self.inputs.test
# 
#         for f in self.inputs.in_list:
#             dft = pd.read_csv(f)
#             df = pd.concat([df, dft], axis=0)
#         #if test : print df
#         df.to_csv(self.inputs.out_file, index=False)
#         return(runtime)
# 
#     def _list_outputs(self):
#         outputs = self.output_spec().get()
#         outputs["out_file"] = os.getcwd() + os.sep + self.inputs.out_file
#         return outputs
# 
# 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
# 
# Path: src/qc.py
# def load_3d(fn, t=0):
# def get_spacing(aff, i) : 
# def group_level_qc(opts, args):
# def pvc_mse(pvc_fn, pve_fn, fwhm):
# def _IsolationForest(X):
# def _LocalOutlierFactor(X):
# def _OneClassSVM(X):
# def _dbscan(X):
#     def _gen_output(self, sid, ses, task,run,trc,rec, fname ="pvc_qc_metric.csv"):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
#     def _gen_output(self, sid, ses, task, run, rec, trc, fname ="distance_metric.csv"):
#     def _run_interface(self, runtime):
#         def image_read(fn) : 
#     def _list_outputs(self):
#     def _gen_output(self, basefile="metrics.png"):
#     def _parse_inputs(self, skip=None):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
#     def _gen_output(self, fname ="measures.csv"):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
# def groupLevel_visual_qc(opts, args):
#     def _gen_output(self, fname):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
# def get_slices(vol,  dim, i) :
#     def __init__(self,in_fn,out_fn, overlay_fn=None, alpha=[1.], dpi=100, duration=100, cmap1=plt.cm.nipy_spectral, cmap2=plt.cm.gray, colorbar=False, edge_1=-1, edge_2=-1,nframes=15, time_frames=1, ndim=3):
#     def load_isotropic(self,in_fn,t=0):
#     def volume2gif(self):
#         def apply_tfm(img, sigma):
#             def animate(i):
#     def __init__(self, targetDir, fn_list):
#     def sidebar(self, vol_list):
#     def get_stage_list(self, vol_list,base):
#     def build(self) :
#     def start(self):
#     def vol(self, src, d, html_file, h1=None, h2=None):
#     def end(self, html_file):
# __NBINS=-1
#     X = np.array(X)
#         X=X.reshape(-1,1)
#     X = np.array(X)
#         X=X.reshape(-1,1)
# class pvc_qc_metricsOutput(TraitedSpec):
# class pvc_qc_metricsInput(BaseInterfaceInputSpec):
# class pvc_qc_metrics(BaseInterface):
# class coreg_qc_metricsOutput(TraitedSpec):
# class coreg_qc_metricsInput(BaseInterfaceInputSpec):
# class coreg_qc_metricsCommand(BaseInterface):
# class plot_qcOutput(TraitedSpec):
# class plot_qcInput(BaseInterfaceInputSpec):
# class plot_qcCommand (BaseInterface):
# class outlier_measuresOutput(TraitedSpec):
# class outlier_measuresInput(BaseInterfaceInputSpec):
# class outlier_measuresCommand(BaseInterface):
# class visual_qcOutput(TraitedSpec):
# class visual_qcInput(BaseInterfaceInputSpec):
# class visual_qcCommand(BaseInterface):
# class ImageParam():
# class QCHTML() :


import os
import re
import nipype
import json
import pandas as pd
import numpy as np
import nipype.pipeline.engine as pe
import nipype.interfaces.io as nio
import nipype.interfaces.utility as util
import nipype.interfaces.utility as niu
import nibabel as nib
from nipype.interfaces.base import (TraitedSpec, File, traits, InputMultiPath, CommandLine, CommandLineInputSpec, BaseInterface, OutputMultiPath, BaseInterfaceInputSpec, isdefined)
from scipy.integrate import simps
from src.utils import concat_df, splitext
from src.qc import metric_columns
        sub_task_df.to_csv(self.inputs.sub_ses, index=False)
        return runtime
    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs["ses"] = self.inputs.ses
        outputs["task"] = self.inputs.task
        outputs["sub"] = self.inputs.sub
        outputs["sub_task"] = self.inputs.sub_task
        outputs["sub_ses"] = self.inputs.sub_ses
        return outputs

    def _gen_output(self, in_file, label):
        ii =  splitext(os.path.basename(in_file))[0]
        out_file = os.getcwd() + os.sep + ii + "_"+label+".csv"
        return out_file

### TKA metrics
class integrate_TACInput(CommandLineInputSpec):   
    in_file = traits.File(desc="Input file ")
    header = traits.File(desc="PET Header file ")
    out_file = traits.File(desc="Output file ")

class integrate_TACOutput(TraitedSpec):
    out_file = traits.File(desc="Output file ")

class integrate_TACCommand( BaseInterface):
    input_spec = integrate_TACInput
    output_spec = integrate_TACOutput

    def _gen_output(self, in_file):

# Path: src/utils.py
# class concat_df(BaseInterface):
#     input_spec =  concat_dfInput 
#     output_spec = concat_dfOutput 
# 
#     def _run_interface(self, runtime):
#         df=pd.DataFrame([])
#         test = self.inputs.test
# 
#         for f in self.inputs.in_list:
#             dft = pd.read_csv(f)
#             df = pd.concat([df, dft], axis=0)
#         #if test : print df
#         df.to_csv(self.inputs.out_file, index=False)
#         return(runtime)
# 
#     def _list_outputs(self):
#         outputs = self.output_spec().get()
#         outputs["out_file"] = os.getcwd() + os.sep + self.inputs.out_file
#         return outputs
# 
# 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
# 
# Path: src/qc.py
# def load_3d(fn, t=0):
# def get_spacing(aff, i) : 
# def group_level_qc(opts, args):
# def pvc_mse(pvc_fn, pve_fn, fwhm):
# def _IsolationForest(X):
# def _LocalOutlierFactor(X):
# def _OneClassSVM(X):
# def _dbscan(X):
#     def _gen_output(self, sid, ses, task,run,trc,rec, fname ="pvc_qc_metric.csv"):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
#     def _gen_output(self, sid, ses, task, run, rec, trc, fname ="distance_metric.csv"):
#     def _run_interface(self, runtime):
#         def image_read(fn) : 
#     def _list_outputs(self):
#     def _gen_output(self, basefile="metrics.png"):
#     def _parse_inputs(self, skip=None):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
#     def _gen_output(self, fname ="measures.csv"):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
# def groupLevel_visual_qc(opts, args):
#     def _gen_output(self, fname):
#     def _run_interface(self, runtime):
#     def _list_outputs(self):
# def get_slices(vol,  dim, i) :
#     def __init__(self,in_fn,out_fn, overlay_fn=None, alpha=[1.], dpi=100, duration=100, cmap1=plt.cm.nipy_spectral, cmap2=plt.cm.gray, colorbar=False, edge_1=-1, edge_2=-1,nframes=15, time_frames=1, ndim=3):
#     def load_isotropic(self,in_fn,t=0):
#     def volume2gif(self):
#         def apply_tfm(img, sigma):
#             def animate(i):
#     def __init__(self, targetDir, fn_list):
#     def sidebar(self, vol_list):
#     def get_stage_list(self, vol_list,base):
#     def build(self) :
#     def start(self):
#     def vol(self, src, d, html_file, h1=None, h2=None):
#     def end(self, html_file):
# __NBINS=-1
#     X = np.array(X)
#         X=X.reshape(-1,1)
#     X = np.array(X)
#         X=X.reshape(-1,1)
# class pvc_qc_metricsOutput(TraitedSpec):
# class pvc_qc_metricsInput(BaseInterfaceInputSpec):
# class pvc_qc_metrics(BaseInterface):
# class coreg_qc_metricsOutput(TraitedSpec):
# class coreg_qc_metricsInput(BaseInterfaceInputSpec):
# class coreg_qc_metricsCommand(BaseInterface):
# class plot_qcOutput(TraitedSpec):
# class plot_qcInput(BaseInterfaceInputSpec):
# class plot_qcCommand (BaseInterface):
# class outlier_measuresOutput(TraitedSpec):
# class outlier_measuresInput(BaseInterfaceInputSpec):
# class outlier_measuresCommand(BaseInterface):
# class visual_qcOutput(TraitedSpec):
# class visual_qcInput(BaseInterfaceInputSpec):
# class visual_qcCommand(BaseInterface):
# class ImageParam():
# class QCHTML() :


# Path: src/results.py
import os
import re
import nipype
import json
import pandas as pd
import numpy as np
import nipype.pipeline.engine as pe
import nipype.interfaces.io as nio
import nipype.interfaces.utility as util
import nipype.interfaces.utility as niu
import nibabel as nib
from nipype.interfaces.base import (TraitedSpec, File, traits, InputMultiPath, CommandLine, CommandLineInputSpec, BaseInterface, OutputMultiPath, BaseInterfaceInputSpec, isdefined)
from scipy.integrate import simps
from src.utils import concat_df, splitext
from src.qc import metric_columns

        sub_task_df.to_csv(self.inputs.sub_ses, index=False)
        return runtime
    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs["ses"] = self.inputs.ses
        outputs["task"] = self.inputs.task
        outputs["sub"] = self.inputs.sub
        outputs["sub_task"] = self.inputs.sub_task
        outputs["sub_ses"] = self.inputs.sub_ses
        return outputs

    def _gen_output(self, in_file, label):
        ii =  splitext(os.path.basename(in_file))[0]
        out_file = os.getcwd() + os.sep + ii + "_"+label+".csv"
        return out_file

### TKA metrics
class integrate_TACInput(CommandLineInputSpec):   
    in_file = traits.File(desc="Input file ")
    header = traits.File(desc="PET Header file ")
    out_file = traits.File(desc="Output file ")

class integrate_TACOutput(TraitedSpec):
    out_file = traits.File(desc="Output file ")

class integrate_TACCommand( BaseInterface):
    input_spec = integrate_TACInput
    output_spec = integrate_TACOutput

    def _gen_output(self, in_file):

        ii =  splitext(os.path.basename(in_file))[0]