#!/usr/bin/env bash

#calculate tensors
foreach * : dwi2tensor IN/5_dwi/dwi_den_unr_pre_unbia_up.mif -mask IN/5_dwi/mask_up.mif IN/5_dwi/tensor_up.mif

#calculate fa
foreach * : tensor2metric IN/5_dwi/tensor_up.mif -mask IN/5_dwi/mask_up.mif -fa IN/5_dwi/fa_up.mif

#calculate adc
foreach * : tensor2metric IN/5_dwi/tensor_up.mif -mask IN/5_dwi/mask_up.mif -adc IN/5_dwi/adc_up.mif

#sample resampled tractograms with fa and save as .csv (#check out mean/median options if necessary)
foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/fa_up.mif IN/5_dwi/fa_tms_hand_l_resampled_100.csv;
foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/fa_up.mif IN/5_dwi/fa_tms_hand_r_resampled_100.csv;

#sample resampled tractograms with adc and save as .csv (#check out mean/median options if necessary)
foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/adc_up.mif IN/5_dwi/adc_tms_hand_l_resampled_100.csv;
foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/adc_up.mif IN/5_dwi/adc_tms_hand_r_resampled_100.csv;

#Perform segmentation of continuous Fibre Orientation Distributions (FODs) to produce discrete fixels and use option flag '-afd image'
foreach * : fod2fixel -afd fd.mif IN/5_dwi/wm.mif IN/5_dwi/fixel_output

foreach * : tck2fixel IN/5_dwi/tms_hand_l.tck IN/5_dwi/fixel_output IN/5_dwi/fixel_output/tck2fixel_hand_l.mif
foreach * : tck2fixel IN/5_dwi/tms_hand_r.tck IN/5_dwi/fixel_output IN/5_dwi/fixel_output/tck2fixel_hand_r.mif

#convert a fixel-based sparse-data image into some form of scalar image, with option flag '-weighted fixel_in'
#"to weight the contribution of each fixel to the per-voxel result according to its volume.
foreach * : fixel2voxel IN/5_dwi/fixel_output/tck2fixel_hand_l.mif max IN/5_dwi/fixel_output/fixel2voxel_hand_l_max.mif;
foreach * : fixel2voxel IN/5_dwi/fixel_output/tck2fixel_hand_r.mif max IN/5_dwi/fixel_output/fixel2voxel_hand_r_max.mif;

foreach * : fixel2voxel IN/5_dwi/fixel_output/tck2fixel_hand_l.mif mean IN/5_dwi/fixel_output/fixel2voxel_hand_l_mean_weighted.mif -weighted IN/5_dwi/fixel_output/fd.mif;
foreach * : fixel2voxel IN/5_dwi/fixel_output/tck2fixel_hand_r.mif mean IN/5_dwi/fixel_output/fixel2voxel_hand_r_mean_weighted.mif -weighted IN/5_dwi/fixel_output/fd.mif;

foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/fixel_norm_output/fixel2voxel_hand_l_max.mif IN/5_dwi/fixel_norm_output/hand_l_norm_resampled_100_max.csv
foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/fixel_norm_output/fixel2voxel_hand_r_max.mif IN/5_dwi/fixel_norm_output/hand_r_norm_resampled_100_max.csv

foreach * : tcksample IN/5_dwi/tms_hand_l_norm_resampled_100.tck IN/5_dwi/fixel_norm_output/fixel2voxel_hand_l_mean_weighted.mif IN/5_dwi/fixel_norm_output/hand_l_norm_resampled_100_mean_weighted.csv
foreach * : tcksample IN/5_dwi/tms_hand_r_norm_resampled_100.tck IN/5_dwi/fixel_norm_output/fixel2voxel_hand_r_mean_weighted.mif IN/5_dwi/fixel_norm_output/hand_r_norm_resampled_100_mean_weighted.csv
