Chargement du reste de cette comparaison.

sct_preproces.py: Before/after @sandrinebedard's changes

Créé Le diff n'expire jamais
37 suppressions
603 lignes
43 ajouts
595 lignes
#!/usr/bin/env python3
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
"""
"""
Created on Wednesday 24 may 2023
Created on Wednesday 24 may 2023


@author: evefra
@author: evefra
"""
"""


# import libraries
# import libraries
import os
import os
import shutil
import shutil
import subprocess
import subprocess
import fnmatch
import fnmatch
import nibabel as nib
import nibabel as nib
import numpy as np
import numpy as np


# work in conda env, where SCT is installed. For me: conda activate venv_sct
# work in conda env, where SCT is installed. For me: conda activate venv_sct


######################################################################################
######################################################################################
# INDICATE WHAT TO DO#
# INDICATE WHAT TO DO#
######################################################################################
######################################################################################


# FUNCTIONAL
# FUNCTIONAL
remove_volumes = 0
remove_volumes = 0
motion_correction = 0
motion_correction = 0
# MATLAB: slice time correction
# MATLAB: slice time correction
# MATLAB: mean slice time corrected image
# MATLAB: mean slice time corrected image
segment_mean = 0
segment_mean = 0


# ANATOMICAL T1
# ANATOMICAL T1
correct_qform = 0
correct_qform = 0
first_segment_t1 = 0
first_segment_t1 = 0
smooth = 0
smooth = 0
second_segment_t1 = 0
second_segment_t1 = 0
label_t1 = 0
label_t1 = 0
register_t1_temp = 0
register_t1_temp = 0


# ANATOMICAL T2*
# ANATOMICAL T2*
# MATLAB: transfer from dicom to nii
# MATLAB: transfer from dicom to nii
first_segment_t2 = 0
first_segment_t2 = 0


# COREGISTERING
# COREGISTERING
register_t2_t1 = 0
register_t2_t1 = 0
register_fmri_t2 = 0
register_fmri_t2 = 0
t2_to_t1_mult_t1_to_temp = 0
t2_to_t1_mult_t1_to_temp = 0
correct_qform_sc = 0
correct_qform_sc = 0
gm_wm_segment = 0
gm_wm_segment = 0
gm_segment_add = 1
gm_segment_add = 1
epi_to_t2_mult_t2_to_temp = 1
epi_to_t2_mult_t2_to_temp = 0
final_warp_apply = 1
final_warp_apply = 0
final_warp_apply_old = 1
final_warp_apply_old = 0


# LOOP OVER SUB NUMBERS
# LOOP OVER SUB NUMBERS
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------


sub_list = [18, 20, 21, 24]
sub_list = [10]
for sub in sub_list:
for sub in sub_list:


# data path pre subject unspecific
# data path pre subject unspecific
data_pre = "/project/3023009.09/evefra"
data_pre = "./data/"
subdirectory = f"sub-{sub:03d}"
subdirectory = f"sub-{sub:03d}"


# random testing
# random testing


#warp = '/project/3023009.09/evefra/sub-010/spinalcord/concat_t2_t1_temp/gm_warp_t22temp.nii.gz'
#warp = '/project/3023009.09/evefra/sub-010/spinalcord/concat_t2_t1_temp/gm_warp_t22temp.nii.gz'
#des_pam = '/home/affneu/evefra/sct_5.8/data/PAM50/template/PAM50_t1.nii.gz'
#des_pam = '/home/affneu/evefra/sct_5.8/data/PAM50/template/PAM50_t1.nii.gz'
# des_t1 = '/project/3023009.09/evefra/sub-013/anat/s302300909_sub013-0017-00001-000192-01.qform.nii'
# des_t1 = '/project/3023009.09/evefra/sub-013/anat/s302300909_sub013-0017-00001-000192-01.qform.nii'
#des_t2 = '/project/3023009.09/evefra/sub-013/anat_sc/s302300909_sub013-0009-00001-000001-01.nii'
#des_t2 = '/project/3023009.09/evefra/sub-013/anat_sc/s302300909_sub013-0009-00001-000001-01.nii'
# input_im = '/project/3023009.09/evefra/sub-013/anat_sc/s302300909_sub013-0009-00001-000001-01.nii'
# input_im = '/project/3023009.09/evefra/sub-013/anat_sc/s302300909_sub013-0009-00001-000001-01.nii'
# des_mean = '/project/3023009.09/evefra/sub-013/spinalcord/slice_time_corrected/spinacord_slc_mean.nii'
# des_mean = '/project/3023009.09/evefra/sub-013/spinalcord/slice_time_corrected/spinacord_slc_mean.nii'


#command = f"sct_apply_transfo -i {des_t2} -d {des_pam} -w {warp}"
#command = f"sct_apply_transfo -i {des_t2} -d {des_pam} -w {warp}"
#process = subprocess.Popen(command, shell=True)
#process = subprocess.Popen(command, shell=True)
#process.wait()
#process.wait()


#################################################################################
#################################################################################
# FUNCTIONAL prep #
# FUNCTIONAL prep #
#################################################################################
#################################################################################


# try:
# try:
# REMOVE FIRST TWO VOLUMES
# REMOVE FIRST TWO VOLUMES
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
if remove_volumes == 1:
if remove_volumes == 1:
directory = os.path.join(
directory = os.path.join(
data_pre, subdirectory, "spinalcord", "niftie")
data_pre, subdirectory, "spinalcord", "niftie")
removed_directory = os.path.join(
removed_directory = os.path.join(
data_pre, subdirectory, "spinalcord", "removed_volumes")
data_pre, subdirectory, "spinalcord", "removed_volumes")
file_extension = ".nii"
file_extension = ".nii"


# Check if the removed volumes directory exists, create it if necessary
# Check if the removed volumes directory exists, create it if necessary
if not os.path.exists(removed_directory):
if not os.path.exists(removed_directory):
os.makedirs(removed_directory)
os.makedirs(removed_directory)


# Get a list of all files in the directory with the specified extension
# Get a list of all files in the directory with the specified extension
file_list = [f for f in os.listdir(
file_list = [f for f in os.listdir(
directory) if f.endswith(file_extension)]
directory) if f.endswith(file_extension)]


# Sort the file list alphabetically
# Sort the file list alphabetically
file_list.sort()
file_list.sort()


# Move the first two files to the removed volumes directory
# Move the first two files to the removed volumes directory
files_to_move = file_list[:2]
files_to_move = file_list[:2]