def create_nifti_corrs_brain(corrs_atlas, sub, paths, SESSION_RESEARCH3T, atlas_name): ses = basename( glob.glob( join(paths.BIDS_DERIVATIVES_STRUCTURAL_MATRICES, f"sub-{sub}", f"ses-{SESSION_RESEARCH3T}"))[0])[4:] save_loc = join(paths.SEIZURE_SPREAD_ATLASES_PROBABILITIES, f"sub-{sub}") utils.checkPathAndMake(save_loc, save_loc, printBOOL=False) atlas_path = glob.glob( join(paths.BIDS_DERIVATIVES_STRUCTURAL_MATRICES, f"sub-{sub}", f"ses-{ses}", "atlas_registration", f"*{atlas_name}.nii.gz"))[0] img, img_data = utils.load_img_and_data(atlas_path) img_data_cors = copy.deepcopy(img_data) img_data_cors[np.where(img_data_cors == 0)] = -1 #loop thru regions and replace regions with correlation data for r in range(len(corrs_atlas)): utils.printProgressBar(r + 1, len(corrs_atlas)) ind = np.where(img_data == int(corrs_atlas[r, 0])) img_data_cors[ind] = float(corrs_atlas[r, 1]) save_loc_atlas = join(save_loc, f"{atlas_name}.nii.gz") utils.save_nib_img_data(img_data_cors, img, save_loc_atlas) T1_path = glob.glob( join(paths.BIDS_DERIVATIVES_STRUCTURAL_MATRICES, f"sub-{sub}", f"ses-{ses}", "atlas_registration", "*desc-preproc_T1w_std.nii.gz"))[0] if utils.checkIfFileDoesNotExist(f"{join(save_loc, basename(T1_path))}"): utils.executeCommand( cmd=f"cp {T1_path} {join(save_loc, basename(T1_path))}")
def freesurferReconAll(T1path, outputpath, overwrite=False, threads=12): utils.checkPathErrorGlob(outputpath) utils.checkPathErrorGlob(T1path) if utils.checkIfFileDoesNotExist(f"{outputpath}/freesurfer/surf/lh.pial", returnOpposite=overwrite): if utils.checkIfFileDoesNotExist( f"{outputpath}/freesurfer/surf/rh.pial", returnOpposite=overwrite): utils.executeCommand(f"mkdir -p {outputpath}/freesurfer/mri/orig") utils.executeCommand( f"mri_convert {T1path} {outputpath}/freesurfer/mri/orig/001.mgz" ) utils.executeCommand( f"recon-all -subjid freesurfer -all -time -log logfile -nuintensitycor-3T -sd {outputpath} -parallel -threads {threads}" )
import os from revellLab.packages.utilities import utils import numpy as np import glob path1 = "/media/arevell/data/linux/data/BIDS/derivatives/structural_connectivity/structural_matrices" path2 = "/home/arevell/Desktop/Berckerle_structure" subs = np.sort(os.listdir(path1)) for i in range(len(subs)): sub = subs[i] print( f"\r{sub} {i+1}/{len(subs)}, {np.round((i+1)/len(subs)*100,1)} ", end="\r") path_sub = os.path.join(path2, sub) utils.checkPathAndMake(path_sub, path_sub, printBOOL=False) sc_path = os.path.join(path1, sub, "ses-research3Tv[0-9][0-9]", "matrices", "*connectogram.txt") utils.checkPathErrorGlob(sc_path) sc_paths_all = glob.glob(sc_path) for sc in range(len(sc_paths_all)): sc_p = sc_paths_all[sc] cmd = f"cp {sc_p} {path_sub}" utils.executeCommand(cmd, printBOOL=False)
print(sub) derivativesOutput = "implantRenders" T00 = join(BIDS, dataset, f"sub-{sub}", f"ses-{implantName}", "anat", f"sub-{sub}_ses-{implantName}_acq-T00_*T1w.nii.gz" ) T00electrodes = join(BIDS, dataset, f"sub-{sub}", f"ses-{implantName}", "ieeg", f"sub-{sub}_ses-{implantName}_space-T00_electrodes.tsv" ) if utils.checkIfFileExistsGlob(T00) and utils.checkIfFileExistsGlob(T00electrodes): #check if freesurfer has been run on preferred session if utils.checkIfFileExistsGlob( join(freesurferReconAllDir, f"sub-{sub}", f"ses-{preferredSurface}", "freesurfer", "surf", "lh.pial" ) ) and utils.checkIfFileExistsGlob( join(freesurferReconAllDir, f"sub-{sub}", f"ses-{preferredSurface}", "freesurfer", "surf", "rh.pial" ) ): sessionPath = np.array(glob( join(freesurferReconAllDir, f"sub-{sub}", f"ses-{preferredSurface}")))[0] session = basename(sessionPath)[4:] outputpath = join(BIDS, "derivatives", f"{derivativesOutput}", f"sub-{sub}", f"ses-{session}") utils.checkPathAndMake(outputpath, join(outputpath, "freesurfer", 'mri' )) utils.checkPathAndMake(outputpath, join(outputpath, "freesurfer", 'surf' )) utils.checkPathAndMake(outputpath, join(outputpath, "html")) utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'mri' , 'orig_nu.mgz')} {join(outputpath, 'freesurfer', 'mri' )} ") utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'mri' , 'brain.mgz')} {join(outputpath, 'freesurfer', 'mri' )} ") utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'surf' , 'lh.pial')} {join(outputpath, 'freesurfer', 'surf')} ") utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'surf' , 'rh.pial')} {join(outputpath, 'freesurfer', 'surf')} ") #check if freesurfer has been run on implant session elif utils.checkIfFileExists( join(freesurferReconAllDir, f"sub-{sub}", f"ses-{implantName}", "freesurfer", "surf", "lh.pial" ) ) and utils.checkIfFileExists( join(freesurferReconAllDir, f"sub-{sub}", f"ses-{implantName}", "freesurfer", "surf", "rh.pial" ) ): sessionPath = np.array(glob( join(freesurferReconAllDir, f"sub-{sub}", f"ses-{implantName}")))[0] session = basename(sessionPath)[4:] outputpath = join(BIDS, "derivatives", f"{derivativesOutput}", f"sub-{sub}", f"ses-{session}") utils.checkPathAndMake(outputpath, join(outputpath, "freesurfer", 'mri' )) utils.checkPathAndMake(outputpath, join(outputpath, "freesurfer", 'surf' )) utils.checkPathAndMake(outputpath, join(outputpath, "html")) utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'mri' , 'orig_nu.mgz')} {join(outputpath, 'freesurfer', 'mri' )} ") utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'mri' , 'brain.mgz')} {join(outputpath, 'freesurfer', 'mri' )} ") utils.executeCommand(f"cp {join(sessionPath, 'freesurfer', 'surf' , 'lh.pial')} {join(outputpath, 'freesurfer', 'surf')} ")
subjectPath = subDir[s] sub = basename(subjectPath)[4:] print(sub) if utils.checkIfFileExistsGlob( join(subjectPath, f"ses-{ses}", "anat", f"sub-{sub}_ses-{ses}_acq-{acq}_T1w.nii.gz")): if sub not in ControlsList: patients.append( glob( join(subjectPath, f"ses-{ses}", "anat", f"sub-{sub}_ses-{ses}_acq-{acq}_T1w.nii.gz"))) file = glob( join(subjectPath, f"ses-{ses}", "anat", f"sub-{sub}_ses-{ses}_acq-{acq}_T1w.nii.gz"))[0] if utils.checkIfFileDoesNotExistGlob(f"{file}"): utils.executeCommand(f"cp {file} {T1wImagesPATEINTSDir}") if sub in ControlsList: controls.append( glob( join(subjectPath, f"ses-{ses}", "anat", f"sub-{sub}_ses-{ses}_acq-{acq}_T1w.nii.gz"))) file = glob( join(subjectPath, f"ses-{ses}", "anat", f"sub-{sub}_ses-{ses}_acq-{acq}_T1w.nii.gz"))[0] if utils.checkIfFileDoesNotExistGlob(f"{file}"): utils.executeCommand(f"cp {file} {T1wImagesCONTROLSDir}") patients = pd.DataFrame(np.array(patients)) controls = pd.DataFrame(np.array(controls))
listOfFiles = list() for (dirpath, dirnames, filenames) in os.walk(BIDSserver): listOfFiles += [join(dirpath, file) for file in filenames] for f in range(len(listOfFiles)): #print(f) SERVERfile = listOfFiles[f] LOCALfile = join( BIDSlocal, relpath(SERVERfile, BIDSserver)) #ignore git if not ".git" in relpath(SERVERfile, BIDSserver): if utils.checkIfFileDoesNotExist(LOCALfile, printBOOL = False): print(f"\n{LOCALfile}") utils.checkPathAndMake( dirname(LOCALfile), dirname(LOCALfile), printBOOL = False ) utils.executeCommand( f"cp -r {SERVERfile} {LOCALfile}", printBOOL = False ) #%% Copy from local to server listOfFiles = list() for (dirpath, dirnames, filenames) in os.walk(BIDSlocal): listOfFiles += [join(dirpath, file) for file in filenames] for f in range(len(listOfFiles)): print(f) LOCALfile = listOfFiles[f] SERVERfile = join( BIDSserver, relpath(LOCALfile, BIDSlocal)) #ignore git if not ".git" in relpath(LOCALfile, BIDSlocal):