diff --git a/src/functions/constants.py b/src/functions/constants.py index 6877a8b..40ebfeb 100644 --- a/src/functions/constants.py +++ b/src/functions/constants.py @@ -62,8 +62,8 @@ # Resolution LOW_RESOLUTION_CTX = "5k" HIGH_RESOLUTION_CTX = "32k" -LOW_RESOLUTION_HIP = "0p5mm" -HIGH_RESOLUTION_HIP = "2mm" +LOW_RESOLUTION_HIP = "2mm" +HIGH_RESOLUTION_HIP = "0p5mm" map_resolution_ctx = {"low": LOW_RESOLUTION_CTX, "high": HIGH_RESOLUTION_CTX} map_resolution_hip = {"low": LOW_RESOLUTION_HIP, "high": HIGH_RESOLUTION_HIP} diff --git a/src/functions/niidcm.py b/src/functions/niidcm.py index 75fb3d2..189806c 100644 --- a/src/functions/niidcm.py +++ b/src/functions/niidcm.py @@ -180,7 +180,9 @@ def transfer_nii_hdr_series_tags(ds, nii2dcm_parameters, file_meta): ds.PatientName = nii2dcm_parameters["PatientName"] -def transfer_nii_hdr_instance_tags(ds, nii2dcm_parameters, instance_index, isbase=False): +def transfer_nii_hdr_instance_tags( + ds, nii2dcm_parameters, instance_index, isbase=False +): """ Transfer NIfTI header parameters applicable to Instance @@ -289,12 +291,13 @@ def convert_nifti_to_dicom( print("NIfTI header series tags transferred.") from pydicom.uid import ExplicitVRLittleEndian, ImplicitVRLittleEndian + if not os.path.exists(out_dir): + os.makedirs(out_dir) + print("DICOM slices written.") if compat == True: for i in range(nii2dcm_parameters["NumberOfInstances"]): - write_slice_wrapper( - (ds, array, i, out_dir, nii2dcm_parameters) - ) + write_slice_wrapper((ds, array, i, out_dir, nii2dcm_parameters)) else: useFloat = False diff --git a/src/functions/run_proc.py b/src/functions/run_proc.py index 9b1da00..bd4cd8b 100644 --- a/src/functions/run_proc.py +++ b/src/functions/run_proc.py @@ -655,6 +655,31 @@ def run( f"{BIDS_ID}_hemi-{hemi}_space-T1w_den-0p5mm_label-hipp_{surf}.surf.gii", ), ) + shutil.copyfile( + os.path.join( + subject_micapipe_dir, + "xfm", + f"{BIDS_ID}_from-nativepro_brain_to-MNI152_0.8mm_mode-image_desc-SyN_0GenericAffine.mat", + ), + os.path.join( + subject_output_dir, + "structural", + f"{BIDS_ID}_from-nativepro_brain_to-MNI152_0.8mm_mode-image_desc-SyN_0GenericAffine.mat", + ), + ) + shutil.copyfile( + os.path.join( + subject_micapipe_dir, + "xfm", + f"{BIDS_ID}_from-nativepro_brain_to-MNI152_0.8mm_mode-image_desc-SyN_1Warp.nii.gz", + ), + os.path.join( + subject_output_dir, + "structural", + f"{BIDS_ID}_from-nativepro_brain_to-MNI152_0.8mm_mode-image_desc-SyN_1Warp.nii.gz", + ), + ) + # do the mapping for feat in features: if structure == "cortex": diff --git a/src/functions/surface_to_volume.py b/src/functions/surface_to_volume.py index 4563c77..fe9408a 100644 --- a/src/functions/surface_to_volume.py +++ b/src/functions/surface_to_volume.py @@ -16,14 +16,14 @@ settozero = True -def fixmatrix(path, subject, session, temppath, wb_path, rootzbrainfolder): +def fixmatrix(subject, session, temppath, wb_path, rootzbrainfolder): # Load the .mat file mat = scipy.io.loadmat( os.path.join( - path, - "xfm", + rootzbrainfolder, + "structural", f"{subject}_{session}_from-nativepro_brain_to-MNI152_0.8mm_mode-image_desc-SyN_0GenericAffine.mat", - ) + ), ) # Extract variables from the .mat file @@ -56,15 +56,16 @@ def fixmatrix(path, subject, session, temppath, wb_path, rootzbrainfolder): "-convert-warpfield", "-from-itk", os.path.join( - path, - "xfm", + rootzbrainfolder, + "structural", f"{subject}_{session}_from-nativepro_brain_to-MNI152_0.8mm_mode-image_desc-SyN_1Warp.nii.gz", ), "-to-world", os.path.join(temppath, "real_warp.nii.gz"), ] - subprocess.run(command) - command3 = [ + # command = [f'"{arg}"' for arg in command] + subprocess.run(" ".join(command), shell=True) + command2 = [ os.path.join(wb_path, "wb_command"), "-volume-resample", f"{rootzbrainfolder}/structural/{subject}_{session}_space-nativepro_T1w_brain.nii.gz", @@ -76,7 +77,7 @@ def fixmatrix(path, subject, session, temppath, wb_path, rootzbrainfolder): "-warp", os.path.join(temppath, "real_warp.nii.gz"), ] - subprocess.run(command3) + subprocess.run(" ".join(command2), shell=True) command3 = [ os.path.join(wb_path, "wb_command"), "-volume-resample", @@ -90,7 +91,7 @@ def fixmatrix(path, subject, session, temppath, wb_path, rootzbrainfolder): os.path.join(temppath, "real_warp.nii.gz"), ] - subprocess.run(command3) + subprocess.run(" ".join(command3), shell=True) def float_array_to_hot_nonrgb(array): @@ -1054,7 +1055,7 @@ def surface_to_volume( # dicomify_base(outdir, rootzbrainfolder, subj=subj, ses=ses, px_demo=px_demo) micapiperootfolder = os.path.join(rootfolder, micapipename, subj, ses) - fixmatrix(micapiperootfolder, subj, ses, tmp, workbench_path, rootzbrainfolder) + fixmatrix(subj, ses, tmp, workbench_path, rootzbrainfolder) Parallel(n_jobs=n_jobs)( delayed(process)( feature, @@ -1103,7 +1104,7 @@ def surface_to_volume( ) if dicoms == 1: os.makedirs(f"{outdir}/DICOM", exist_ok=True) - dicomify_base(outdir, subj, ses, tmp, thresh, px_demo=px_demo) + dicomify_base(outdir, rootzbrainfolder, subj, ses, px_demo=px_demo) print("Converting to DICOM") timepre = time() Parallel(n_jobs=n_jobs)( diff --git a/src/zbrains.py b/src/zbrains.py index 76b19cf..5369606 100644 --- a/src/zbrains.py +++ b/src/zbrains.py @@ -523,9 +523,10 @@ def check_sub(args, sub, ses=None): hippunfold_path = os.path.join(hippunfold_path, ses) if not os.path.isdir(micapipe_path): # print(f'Non proccessable micapipe output at {micapipe_path} for {sub}{f"-{ses}" if ses else ""}, skipping') - return False + if "proc" in args.run: + return False if "proc" in args.run: - + if "cortex" in args.struct or "subcortex" in args.struct: if not os.path.exists(micapipe_path) or not os.path.isdir(micapipe_path): print( @@ -540,7 +541,6 @@ def check_sub(args, sub, ses=None): f'No hippunfold at {hippunfold_path} for {sub}{f"-{ses}" if ses else ""}, skipping' ) return False - return True @@ -560,7 +560,7 @@ def create_jobs(args, subs, ses, run_type): if check_sub(args, sub, s): job = copy.copy(args) job.sub, job.ses, job.run = sub, s, run_type - if args.patient_prefix in job.sub or run_type == "proc": + if args.control_prefix not in job.sub or run_type == "proc": jobs.append(job) return jobs @@ -714,7 +714,7 @@ def print_help(self): type=str, default="/data/mica1/01_programs/workbench-1.4.2/bin_linux64", ) - parser.add_argument("--patient_prefix", type=str, default="PX") + parser.add_argument("--control_prefix", type=str, default="HC") parser.add_argument("--verbose", type=int, default=-1) parser.add_argument("--version", action="version", version="1.0.0") parser.add_argument("--dicoms", type=int, default=1)