# Path: samri/pipelines/utils.py
# TRANSFORM_PHASES = {
# 	"rigid":{
# 		"transforms":"Rigid",
# 		"transform_parameters":(0.1,),
# 		"number_of_iterations":[3000,3000,3000,3000],
# 		"metric":"GC",
# 		"metric_weight":1,
# 		"radius_or_number_of_bins":64,
# 		"sampling_strategy":"Regular",
# 		"sampling_percentage":0.2,
# 		"convergence_threshold":1.e-10,
# 		"convergence_window_size":20,
# 		"smoothing_sigmas":[3,2,1,0],
# 		"sigma_units":"vox",
# 		"shrink_factors":[8,4,2,1],
# 		"use_estimate_learning_rate_once":False,
# 		"use_histogram_matching":True,
# 		},
# 	"affine":{
# 		"transforms":"Affine",
# 		"transform_parameters":(0.1,),
# 		"number_of_iterations":[500,500,250],
# 		"metric":"MI",
# 		"metric_weight":1,
# 		"radius_or_number_of_bins":16,
# 		"sampling_strategy":None,
# 		"sampling_percentage":0.3,
# 		"convergence_threshold":1.e-16,
# 		"convergence_window_size":20,
# 		"smoothing_sigmas":[2,1,0],
# 		"sigma_units":"vox",
# 		"shrink_factors":[4,2,1],
# 		"use_estimate_learning_rate_once":False,
# 		"use_histogram_matching":True,
# 		},
# 	"syn":{
# 		"transforms":"SyN",
# 		"transform_parameters":(0.1, 2.0, 0.2),
# 		"number_of_iterations":[500,500,500,250],
# 		"metric":"MI",
# 		"metric_weight":1,
# 		"radius_or_number_of_bins":16,
# 		"sampling_strategy":None,
# 		"sampling_percentage":0.3,
# 		"convergence_threshold":1.e-16,
# 		"convergence_window_size":20,
# 		"smoothing_sigmas":[3,2,1,0],
# 		"sigma_units":"vox",
# 		"shrink_factors":[8,4,2,1],
# 		"use_estimate_learning_rate_once":False,
# 		"use_histogram_matching":True,
# 		},
# 	}
# 
# Path: samri/utilities.py
# def iter_collapse_by_path(in_files, out_files,
# 	n_jobs=None,
# 	n_jobs_percentage=0.75,
# 	):
# 	"""Patalellized iteration of `samri.utilities.collapse_by_path`."""
# 	if not n_jobs:
# 		n_jobs = max(int(round(mp.cpu_count()*n_jobs_percentage)),2)
# 	out_files = Parallel(n_jobs=n_jobs, verbose=0, backend="threading")(map(delayed(collapse_by_path),
# 		in_files,
# 		out_files,
# 		))
# 	return out_files


import nipype.interfaces.ants as ants
import os
import re
from samri.pipelines.utils import TRANSFORM_PHASES
from samri.utilities import iter_collapse_by_path


def collapse_nifti(in_dir, out_dir,
	**kwargs):
	in_dir = os.path.abspath(os.path.expanduser(in_dir))
	out_dir = os.path.abspath(os.path.expanduser(out_dir))
	in_files = []
	for root, dirs, files in os.walk(in_dir):
		_in_files = [os.path.join(root,f) for f in files]
		_in_files = [i for i in _in_files if os.path.isfile(i)]
		# Only NIfTI files:
		_in_files = [i for i in _in_files if '.nii' in i]
		in_files.extend(_in_files)
	# Make relative to `in_dir`:
	out_files = [re.sub(r'^' + re.escape(in_dir), '', i) for i in in_files]
	out_files = [os.path.join(out_dir,i[1:]) for i in out_files]
	out_files = iter_collapse_by_path(in_files, out_files,
		**kwargs
		)

def transform_feature(feature, source_reference, target_reference,
	target_mask='',
	phase_dictionary=TRANSFORM_PHASES,
	phases=['rigid','affine','syn'],
	num_threads=4,
	output_path='transformed_feature.nii.gz',
	interpolation='NearestNeighbor',
	debug=False,

# Path: samri/pipelines/utils.py
# TRANSFORM_PHASES = {
# 	"rigid":{
# 		"transforms":"Rigid",
# 		"transform_parameters":(0.1,),
# 		"number_of_iterations":[3000,3000,3000,3000],
# 		"metric":"GC",
# 		"metric_weight":1,
# 		"radius_or_number_of_bins":64,
# 		"sampling_strategy":"Regular",
# 		"sampling_percentage":0.2,
# 		"convergence_threshold":1.e-10,
# 		"convergence_window_size":20,
# 		"smoothing_sigmas":[3,2,1,0],
# 		"sigma_units":"vox",
# 		"shrink_factors":[8,4,2,1],
# 		"use_estimate_learning_rate_once":False,
# 		"use_histogram_matching":True,
# 		},
# 	"affine":{
# 		"transforms":"Affine",
# 		"transform_parameters":(0.1,),
# 		"number_of_iterations":[500,500,250],
# 		"metric":"MI",
# 		"metric_weight":1,
# 		"radius_or_number_of_bins":16,
# 		"sampling_strategy":None,
# 		"sampling_percentage":0.3,
# 		"convergence_threshold":1.e-16,
# 		"convergence_window_size":20,
# 		"smoothing_sigmas":[2,1,0],
# 		"sigma_units":"vox",
# 		"shrink_factors":[4,2,1],
# 		"use_estimate_learning_rate_once":False,
# 		"use_histogram_matching":True,
# 		},
# 	"syn":{
# 		"transforms":"SyN",
# 		"transform_parameters":(0.1, 2.0, 0.2),
# 		"number_of_iterations":[500,500,500,250],
# 		"metric":"MI",
# 		"metric_weight":1,
# 		"radius_or_number_of_bins":16,
# 		"sampling_strategy":None,
# 		"sampling_percentage":0.3,
# 		"convergence_threshold":1.e-16,
# 		"convergence_window_size":20,
# 		"smoothing_sigmas":[3,2,1,0],
# 		"sigma_units":"vox",
# 		"shrink_factors":[8,4,2,1],
# 		"use_estimate_learning_rate_once":False,
# 		"use_histogram_matching":True,
# 		},
# 	}
# 
# Path: samri/utilities.py
# def iter_collapse_by_path(in_files, out_files,
# 	n_jobs=None,
# 	n_jobs_percentage=0.75,
# 	):
# 	"""Patalellized iteration of `samri.utilities.collapse_by_path`."""
# 	if not n_jobs:
# 		n_jobs = max(int(round(mp.cpu_count()*n_jobs_percentage)),2)
# 	out_files = Parallel(n_jobs=n_jobs, verbose=0, backend="threading")(map(delayed(collapse_by_path),
# 		in_files,
# 		out_files,
# 		))
# 	return out_files


# Path: samri/pipelines/manipulations.py
import nipype.interfaces.ants as ants
import os
import re
from samri.pipelines.utils import TRANSFORM_PHASES
from samri.utilities import iter_collapse_by_path



def collapse_nifti(in_dir, out_dir,
	**kwargs):
	in_dir = os.path.abspath(os.path.expanduser(in_dir))
	out_dir = os.path.abspath(os.path.expanduser(out_dir))
	in_files = []
	for root, dirs, files in os.walk(in_dir):
		_in_files = [os.path.join(root,f) for f in files]
		_in_files = [i for i in _in_files if os.path.isfile(i)]
		# Only NIfTI files:
		_in_files = [i for i in _in_files if '.nii' in i]
		in_files.extend(_in_files)
	# Make relative to `in_dir`:
	out_files = [re.sub(r'^' + re.escape(in_dir), '', i) for i in in_files]
	out_files = [os.path.join(out_dir,i[1:]) for i in out_files]
	out_files = iter_collapse_by_path(in_files, out_files,
		**kwargs
		)

def transform_feature(feature, source_reference, target_reference,
	target_mask='',
	phase_dictionary=TRANSFORM_PHASES,
	phases=['rigid','affine','syn'],
	num_threads=4,
	output_path='transformed_feature.nii.gz',
	interpolation='NearestNeighbor',
	debug=False,

	):