<commit_before>#!/usr/bin/env python
#########################################################################################
#
# Asman et al. groupwise multi-atlas segmentation method implementation
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2014 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: <NAME>, <NAME>
# Modified: 2014-11-20
#
# About the license: see the file LICENSE.TXT
#########################################################################################
from msct_parser import *
import sys
from msct_image import Image


class Param:
    def __init__(self):
        self.debug = 0


def main():
    fname_out = ''
    square = 0
    parser = Parser(__file__)
    parser.usage.set_description('Crop the image depending the mask \n'
                                 'The mask and the image must have the same dimensions\n')
    parser.add_option(name="-i",
                      type_value="file",
                      description="Image you want to crop",
                      mandatory=True,
                      example='t2.nii.gz')
    parser.add_option(name="-mask",
                      type_value="file",
                      description="Mask you want to crop with",
                      mandatory=True,
                      example='t2_mask.nii.gz')
    parser.add_option(name="-o",
                      type_value="str",
                      description="Output file name",
                      mandatory=False,
                      example='t2_croped.nii.gz')
    parser.add_option(name="-square",
                      type_value="int",
                      description="Crop from a square mask (the image dimension will be changed), default is 0\n"
                                  "WARNING : if this argument is used,the returned fil is oriented in IRP",
                      mandatory=False,
                      example=1)

    # Getting the arguments
    arguments = parser.parse(sys.argv[1:])
    fname_input = arguments["-i"]
    fname_mask = arguments["-mask"]
    if "-o" in arguments:
        fname_out = arguments["-o"]
    if "-square" in arguments:
        square = arguments["-square"]

    input_img = Image(fname_input)
    if len(input_img.data.shape) == 3:
        if input_img.data.shape[2] == 1:
            input_img.data = input_img.data.reshape(input_img.data.shape[:-1])

    if square:
        if len(input_img.data.shape) == 3:
            status, orientation_init = sct.run('sct_orientation -i ' + fname_input)
            orientation_init = orientation_init[4:7]
            print 'orientation init : ', orientation_init

            if orientation_init != 'IRP':
                status, output = sct.run('sct_orientation -i ' + fname_input + ' -s IRP ')
                fname_input = sct.extract_fname(fname_input)[1] + '_IRP.nii.gz'
                input_img = Image(fname_input)
                status, output = sct.run('sct_orientation -i ' + fname_mask + ' -s IRP ')
                fname_mask = sct.extract_fname(fname_mask)[1] + '_IRP.nii.gz'
            elif sct.run('sct_orientation -i ' + fname_mask)[1][4:7] != 'IRP':
                fname_mask = sct.extract_fname(fname_mask)[1] + '_IRP.nii.gz'



    mask = Image(fname_mask)

    if square:
        input_img.crop_from_square_mask(mask)
    else:
        input_img.crop_from_mask(mask)

    input_img.path = './'

    if fname_out == '':
        fname_out = input_img.file_name + '_smartly_cropped_over_mask'


    if fname_out[-7:] == '.nii.gz':
        input_img.file_name = fname_out[:-7]
    else:
        input_img.file_name = fname_out

    print 'shape output',input_img.data.shape
    input_img.save()


    '''
    if square and orientation_init != 'IRP':
        status, output = sct.run('sct_orientation -i ' + fname_out + '.nii.gz -s ' + orientation_init + ' ')
    '''


if __name__ == "__main__":
    # call main function<commit_msg>BUG: adapt crop_over_mask to 2D image with shape (x,y,1)<commit_after>#!/usr/bin/env python
#########################################################################################
#
# Asman et al. groupwise multi-atlas segmentation method implementation
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2014 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: <NAME>, <NAME>
# Modified: 2014-11-20
#
# About the license: see the file LICENSE.TXT
#########################################################################################
from msct_parser import *
import sys
from msct_image import Image


class Param:
    def __init__(self):
        self.debug = 0


def main():
    fname_out = ''
    square = 0
    parser = Parser(__file__)
    parser.usage.set_description('Crop the image depending the mask \n'
                                 'The mask and the image must have the same dimensions\n')
    parser.add_option(name="-i",
                      type_value="file",
                      description="Image you want to crop",
                      mandatory=True,
                      example='t2.nii.gz')
    parser.add_option(name="-mask",
                      type_value="file",
                      description="Mask you want to crop with",
                      mandatory=True,
                      example='t2_mask.nii.gz')
    parser.add_option(name="-o",
                      type_value="str",
                      description="Output file name",
                      mandatory=False,
                      example='t2_croped.nii.gz')
    parser.add_option(name="-square",
                      type_value="int",
                      description="Crop from a square mask (the image dimension will be changed), default is 0\n"
                                  "WARNING : if this argument is used,the returned fil is oriented in IRP",
                      mandatory=False,
                      example=1)

    # Getting the arguments
    arguments = parser.parse(sys.argv[1:])
    fname_input = arguments["-i"]
    fname_mask = arguments["-mask"]
    if "-o" in arguments:
        fname_out = arguments["-o"]
    if "-square" in arguments:
        square = arguments["-square"]

    input_img = Image(fname_input)
    if len(input_img.data.shape) == 3:
        if input_img.data.shape[2] == 1:
            input_img.data = input_img.data.reshape(input_img.data.shape[:-1])

    if square:
        if len(input_img.data.shape) == 3:
            status, orientation_init = sct.run('sct_orientation -i ' + fname_input)
            orientation_init = orientation_init[4:7]
            print 'orientation init : ', orientation_init

            if orientation_init != 'IRP':
                status, output = sct.run('sct_orientation -i ' + fname_input + ' -s IRP ')
                fname_input = sct.extract_fname(fname_input)[1] + '_IRP.nii.gz'
                input_img = Image(fname_input)
                status, output = sct.run('sct_orientation -i ' + fname_mask + ' -s IRP ')
                fname_mask = sct.extract_fname(fname_mask)[1] + '_IRP.nii.gz'
            elif sct.run('sct_orientation -i ' + fname_mask)[1][4:7] != 'IRP':
                fname_mask = sct.extract_fname(fname_mask)[1] + '_IRP.nii.gz'



    mask = Image(fname_mask)
    if len(mask.data.shape) == 3:
        if mask.data.shape[2] == 1:
            mask.data = mask.data.reshape(mask.data.shape[:-1])


    if square:
        input_img.crop_from_square_mask(mask)
    else:
        input_img.crop_from_mask(mask)

    input_img.path = './'

    if fname_out == '':
        fname_out = input_img.file_name + '_smartly_cropped_over_mask'


    if fname_out[-7:] == '.nii.gz':
        input_img.file_name = fname_out[:-7]
    else:
        input_img.file_name = fname_out

    print 'shape output',input_img.data.shape
    input_img.save()


    '''
    if square and orientation_init != 'IRP':
        status, output = sct.run('sct_orientation -i ' + fname_out + '.nii.gz -s ' + orientation_init + ' ')
    '''


if __name__ == "__main__":
    # call main function
{'fraction_non_alphanumeric': 0.17556214479861626, 'fraction_numerical': 0.009389671361502348, 'mean_word_length': 1.990395271518286, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2109143', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2333, 'n_words': 509}