#!/usr/bin/env python
#########################################################################################
#
# Test function for sct_get_centerline script
#
#   replace the shell test script in sct 1.0
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2014 Polytechnique Montreal <www.neuro.polymtl.ca>
# Author: Augustin Roux
# modified: 2014/09/28
#
# About the license: see the file LICENSE.TXT
#########################################################################################

import commands

def test(path_data):

    # parameters
    folder_data = 't2/'
    file_data = ['t2.nii.gz', 't2_centerline_init.nii.gz', 't2_centerline_labels.nii.gz']

    output = ''
    status = 0

    # define command
    cmd = 'sct_get_centerline -i ' + path_data + folder_data + file_data[0] \
        + ' -method auto' \
        + ' -t t2 ' \
        + ' -v 1'
    output += '\n====================================================================================================\n'+cmd+'\n====================================================================================================\n\n'  # copy command
    s, o = commands.getstatusoutput(cmd)
    status += s
    output += o

    # define command: DOES NOT RUN IT BECAUSE REQUIRES FSL FLIRT
    # cmd = 'sct_get_centerline -i ' + path_data + folder_data + file_data[0] \
    #     + ' -method point' \
    #     + ' -p ' + path_data + folder_data + file_data[1] \
    #     + ' -g 1'\
    #     + ' -k 4'
    # output += '\n====================================================================================================\n'+cmd+'\n====================================================================================================\n\n'  # copy command
    # s, o = commands.getstatusoutput(cmd)
    # status += s
    # output += o

    # define command
    cmd = 'sct_get_centerline -i ' + path_data + folder_data + file_data[0] \
        + ' -method labels ' \
        + ' -l ' + path_data + folder_data + file_data[2] \
        + ' -v 1'
    output += '\n====================================================================================================\n'+cmd+'\n====================================================================================================\n\n'  # copy command
    s, o = commands.getstatusoutput(cmd)
    status += s
    output += o

    return status, output


if __name__ == "__main__":
    # call main function
    test()
@@ -561,16 +561,140 @@
 ommands%0A
+from msct_image import Image%0Afrom sct_get_centerline import ind2sub%0Aimport math%0Aimport sct_utils as sct%0Aimport numpy as np%0A%0A
 %0Adef tes
@@ -837,16 +837,40 @@
 .nii.gz'
+, 't2_seg_manual.nii.gz'
 %5D%0A%0A    o
@@ -1377,32 +1377,1752 @@
 %0A    output += o
+%0A    # small integrity test on scad%0A    try :%0A        if status == 0:%0A            manual_seg = Image(path_data + folder_data + file_data%5B3%5D)%0A            centerline_scad = Image(path_data + folder_data + file_data%5B0%5D)%0A            centerline_scad.change_orientation()%0A            manual_seg.change_orientation()%0A%0A            from scipy.ndimage.measurements import center_of_mass%0A            # find COM%0A            iterator = range(manual_seg.data.shape%5B2%5D)%0A            com_x = %5B0 for ix in iterator%5D%0A            com_y = %5B0 for iy in iterator%5D%0A%0A            for iz in iterator:%0A                com_x%5Biz%5D, com_y%5Biz%5D = center_of_mass(manual_seg.data%5B:, :, iz%5D)%0A            max_distance = %7B%7D%0A            distance = %7B%7D%0A            for iz in range(1, centerline_scad.data.shape%5B2%5D-1):%0A                ind1 = np.argmax(centerline_scad.data%5B:, :, iz%5D)%0A                X,Y = ind2sub(centerline_scad.data%5B:, :, iz%5D.shape,ind1)%0A                com_phys = np.array(manual_seg.transfo_pix2phys(%5B%5Bcom_x%5Biz%5D, com_y%5Biz%5D, iz%5D%5D))%0A                scad_phys = np.array(centerline_scad.transfo_pix2phys(%5B%5BX, Y, iz%5D%5D))%0A                distance_magnitude = np.linalg.norm(%5Bcom_phys%5B0%5D%5B0%5D-scad_phys%5B0%5D%5B0%5D, com_phys%5B0%5D%5B1%5D-scad_phys%5B0%5D%5B1%5D, 0%5D)%0A                if math.isnan(distance_magnitude):%0A                    print %22Value is nan%22%0A                else:%0A                    distance%5Biz%5D = distance_magnitude%0A%0A            max_distance = max(distance.values())%0A            #if max_distance %3E 5:%0A                #sct.printv(%22Max distance between scad and manual centerline is greater than 5 mm%22, type=%22warning%22)%0A%0A    except Exception, e:%0A        sct.printv(%22Exception found while testing scad integrity%22)%0A        sct.printv(e.message, type=%22error%22)
 %0A%0A    # define c

TEST : Added small integrity test for sct_get_centerline