<commit_before>#!/usr/bin/env python
#
# multi-label registration of spinal cord internal structure
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2013 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: <NAME>
# Modified: 2015-05-20
#
# About the license: see the file LICENSE.TXT
#########################################################################################
import sys, os, time
from msct_parser import Parser
from msct_image import Image
import sct_utils as sct
#TODO: add remove tmp

class Param:
    def __init__(self):
        self.thr = 0.5
        self.gap = (100, 200)
        self.smooth = 0.8

        self.param_reg = 'step=1,algo=slicereg,metric=MeanSquares,step=2,algo=syn,metric=MeanSquares,iter=2:step=3,algo=bsplinesyn,metric=MeanSquares,iter=5,smooth=1'

        self.output_folder = './'
        self.verbose = 1
        self.remove_tmp = 1
        self.qc = 1


class MultiLabelRegistration:
    def __init__(self, fname_gm, fname_wm, path_template, fname_warp_template, fname_target, param=None):
        if param is None:
            self.param = Param()
        else:
            self.param = param
        self.im_gm = Image(fname_gm)
        self.im_wm = Image(fname_wm)
        self.path_template = sct.slash_at_the_end(path_template, 1)
        self.im_template_gm = Image(self.path_template+'template/MNI-Poly-AMU_GM.nii.gz')
        self.im_template_wm = Image(self.path_template+'template/MNI-Poly-AMU_WM.nii.gz')

        self.fname_target = fname_target
        self.fname_warp_template2target = fname_warp_template

        self.path_new_template = 'label_wm_corrected_multilabel/'

    def register(self):
        # accentuate separation WM/GM
        self.im_gm = thr_im(self.im_gm, 0.01, self.param.thr)
        self.im_wm = thr_im(self.im_wm, 0.01, self.param.thr)
        self.im_template_gm = thr_im(self.im_template_gm, 0.01, self.param.thr)
        self.im_template_wm = thr_im(self.im_template_wm, 0.01, self.param.thr)

        # create multilabel images
        im_automatic_ml = self.im_gm.copy()
        im_template_ml = self.im_template_gm.copy()

        im_automatic_ml.data = self.param.gap[1]*self.im_gm.data + self.param.gap[0]*self.im_wm.data
        im_template_ml.data = self.param.gap[1]*self.im_template_gm.data + self.param.gap[0]*self.im_template_wm.data

        fname_automatic_ml = 'multilabel_automatic_seg.nii.gz'
        fname_template_ml = 'multilabel_template_seg.nii.gz'

        path_automatic_ml, file_automatic_ml, ext_automatic_ml = sct.extract_fname(fname_automatic_ml)
        path_template_ml, file_template_ml, ext_template_ml = sct.extract_fname(fname_template_ml)
        path_target, file_target, ext_target = sct.extract_fname(fname_target)
        path_warp_template, file_warp_template, ext_warp_template = sct.extract_fname(fname_warp_template)

        im_automatic_ml.setFileName(fname_automatic_ml)
        im_template_ml.setFileName(fname_template_ml)

        # Create temporary folder and put files in it
        tmp_dir = sct.tmp_create()

        # sct.run('cp '+fname_target+' '+tmp_dir+file_target+ext_target)
        # sct.run('cp '+fname_warp_template+' '+tmp_dir+file_warp_template+ext_warp_template)

        from sct_convert import convert
        convert(fname_target, tmp_dir+file_target+ext_target)
        convert(fname_warp_template, tmp_dir+file_warp_template+ext_warp_template, squeeze_data=0)

        os.chdir(tmp_dir)
        # TODO assert RPI, if not, change orientation
        im_automatic_ml.save()
        im_template_ml.save()

        fname_automatic_ml_smooth = file_automatic_ml+'_smooth'+ext_automatic_ml
        sct.run('sct_maths -i '+fname_automatic_ml+' -smooth '+str(self.param.smooth)+','+str(self.param.smooth)+',0 -o '+fname_automatic_ml_smooth)
        fname_automatic_ml = fname_automatic_ml_smooth
        path_automatic_ml, file_automatic_ml, ext_automatic_ml = sct.extract_fname(fname_automatic_ml)

        # Register multilabel images together
        sct.run('sct_register_multimodal -i '+fname_template_ml+' -d '+fname_automatic_ml+' -p '+self.param.param_reg) #TODO: complete params
        sct.run('sct_concat_transfo -w '+file_warp_template+ext_warp_template+',warp_'+file_template_ml+'2'+file_automatic_ml+'.nii.gz -d '+file_target+ext_target+' -o warp_template2'+file_target+'_wm_corrected_multilabel.nii.gz')
        sct.run('sct_warp_template -d '+fname_target+' -w warp_template2'+file_target+'_wm_corrected_multilabel.nii.gz')
        os.chdir('..')

        sct.run('cp -r '+tmp_dir+'label  '+self.param.output_folder+self.path_new_template)

        sct.generate_output_file(tmp_dir+'warp_'+file_template_ml+'2'+file_automatic_ml+'.nii.gz', self.param.output_folder+'warp_'+file_template_ml+'2'+file_automatic_ml+'.nii.gz')<commit_msg>WIP todo: add crop and pad<commit_after>#!/usr/bin/env python
#
# multi-label registration of spinal cord internal structure
#
# ---------------------------------------------------------------------------------------
# Copyright (c) 2013 Polytechnique Montreal <www.neuro.polymtl.ca>
# Authors: <NAME>
# Modified: 2015-05-20
#
# About the license: see the file LICENSE.TXT
#########################################################################################
import sys, os, time
from msct_parser import Parser
from msct_image import Image
import sct_utils as sct
#TODO: add remove tmp

class Param:
    def __init__(self):
        self.thr = 0.5
        self.gap = (100, 200)
        self.smooth = 0.8

        self.param_reg = 'step=1,algo=slicereg,metric=MeanSquares,step=2,algo=syn,metric=MeanSquares,iter=2:step=3,algo=bsplinesyn,metric=MeanSquares,iter=5,smooth=1'

        self.output_folder = './'
        self.verbose = 1
        self.remove_tmp = 1
        self.qc = 1


class MultiLabelRegistration:
    def __init__(self, fname_gm, fname_wm, path_template, fname_warp_template, fname_target, param=None):
        if param is None:
            self.param = Param()
        else:
            self.param = param
        self.im_gm = Image(fname_gm)
        self.im_wm = Image(fname_wm)
        self.path_template = sct.slash_at_the_end(path_template, 1)
        self.im_template_gm = Image(self.path_template+'template/MNI-Poly-AMU_GM.nii.gz')
        self.im_template_wm = Image(self.path_template+'template/MNI-Poly-AMU_WM.nii.gz')

        self.fname_target = fname_target
        self.fname_warp_template2target = fname_warp_template

        self.path_new_template = 'label_wm_corrected_multilabel/'

    def register(self):
        # accentuate separation WM/GM
        self.im_gm = thr_im(self.im_gm, 0.01, self.param.thr)
        self.im_wm = thr_im(self.im_wm, 0.01, self.param.thr)
        self.im_template_gm = thr_im(self.im_template_gm, 0.01, self.param.thr)
        self.im_template_wm = thr_im(self.im_template_wm, 0.01, self.param.thr)

        # create multilabel images
        im_automatic_ml = self.im_gm.copy()
        im_template_ml = self.im_template_gm.copy()

        im_automatic_ml.data = self.param.gap[1]*self.im_gm.data + self.param.gap[0]*self.im_wm.data
        im_template_ml.data = self.param.gap[1]*self.im_template_gm.data + self.param.gap[0]*self.im_template_wm.data

        fname_automatic_ml = 'multilabel_automatic_seg.nii.gz'
        fname_template_ml = 'multilabel_template_seg.nii.gz'

        path_automatic_ml, file_automatic_ml, ext_automatic_ml = sct.extract_fname(fname_automatic_ml)
        path_template_ml, file_template_ml, ext_template_ml = sct.extract_fname(fname_template_ml)
        path_target, file_target, ext_target = sct.extract_fname(fname_target)
        path_warp_template, file_warp_template, ext_warp_template = sct.extract_fname(fname_warp_template)

        im_automatic_ml.setFileName(fname_automatic_ml)
        im_template_ml.setFileName(fname_template_ml)

        # Create temporary folder and put files in it
        tmp_dir = sct.tmp_create()

        # sct.run('cp '+fname_target+' '+tmp_dir+file_target+ext_target)
        # sct.run('cp '+fname_warp_template+' '+tmp_dir+file_warp_template+ext_warp_template)

        from sct_convert import convert
        convert(fname_target, tmp_dir+file_target+ext_target)
        convert(fname_warp_template, tmp_dir+file_warp_template+ext_warp_template, squeeze_data=0)

        os.chdir(tmp_dir)
        # TODO assert RPI, if not, change orientation
        im_automatic_ml.save()
        im_template_ml.save()

        # sct.run('sct_create_mask -i '+name+' -mcenterline,'+name+' -f box -s '+size)
        # status, output_crop = sct.run('sct_crop_image -i '+name+' -m '+mask)
        #TODO get dim from output_crop

        fname_automatic_ml_smooth = file_automatic_ml+'_smooth'+ext_automatic_ml
        sct.run('sct_maths -i '+fname_automatic_ml+' -smooth '+str(self.param.smooth)+','+str(self.param.smooth)+',0 -o '+fname_automatic_ml_smooth)
        fname_automatic_ml = fname_automatic_ml_smooth
        path_automatic_ml, file_automatic_ml, ext_automatic_ml = sct.extract_fname(fname_automatic_ml)

        # Register multilabel images together
        sct.run('sct_register_multimodal -i '+fname_template_ml+' -d '+fname_automatic_ml+' -p '+self.param.param_reg) #TODO: complete params

        # TODO pad back warping fields : sct_image -i warp -pad-asym dimxi,dimxf, ...
        # TODO with dimxf = dimx -cropxf -1, dimxi = cropxi

        sct.run('sct_concat_transfo -w '+file_warp_template+ext_warp_template+',warp_'+file_template_ml+'2'+file_automatic_ml+'.nii.gz -d '+file_target+ext_target+' -o warp_template2'+file_target+'_wm_corrected_multilabel.nii.gz')
        sct.run('sct_warp_template -d '+fname_target+' -w warp_template2'+file_target+'_wm_corrected_multilabel.nii.gz')
        os.chdir('..')

        sct.run('cp -r '+tmp_dir+'label  '+self.param.output_folder+self.path_new_template)

        sct.generate_output_file(tmp_dir+'warp_'+file_template_ml+'2'+file_automatic_ml+'.nii.gz', self.param.output_folder+'warp_'+file_template_ml+'2'+file_automatic_ml+'.nii.gz')
{'fraction_non_alphanumeric': 0.14229053049082796, 'fraction_numerical': 0.011204759543877046, 'mean_word_length': 3.5432432432432432, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5815467', 'n_tokens_mistral': 3752, 'n_tokens_neox': 3500, 'n_words': 524}