From 1c2886ad95b4d3aa9064e9de5ce6442bcf2b587e Mon Sep 17 00:00:00 2001 From: "Christopher J. Steele" Date: Mon, 12 Apr 2021 15:46:02 -0400 Subject: [PATCH 1/6] changed prt, tot, pct outputs to float32, PNR to test --- TractREC/TractREC.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/TractREC/TractREC.py b/TractREC/TractREC.py index afd5fd9..5e88509 100644 --- a/TractREC/TractREC.py +++ b/TractREC/TractREC.py @@ -1318,7 +1318,7 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B if not (os.path.isfile(seg_idx_fname)) or CLOBBER: # if the idx file exists, don't bother doing this again if not BY_SLICE: - data_list = [nb.load(fn).get_data()[..., np.newaxis] for fn in files] # load all of the files + data_list = [nb.load(fn).get_fdata()[..., np.newaxis] for fn in files] # load all of the files combined = np.concatenate(data_list, axis=-1) # concatenate all of the input data combined = np.concatenate((np.zeros_like(data_list[0]), combined), @@ -1334,8 +1334,8 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B axis=-1) == 0] = 0 # where there is no difference between volumes, this should be the mask, set to 0 ##%% create soft segmentation to show strength of the dominant tract in each voxel - seg_part = np.zeros_like(hard_seg) - seg_temp = np.zeros_like(hard_seg) + seg_part = np.zeros_like(hard_seg,dtype=np.float32) + seg_temp = np.zeros_like(hard_seg,dtype=np.float32) seg_total = combined.sum(axis=-1) idx = 1 @@ -1383,6 +1383,7 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B # seg_pct2[seg_pct2==-1]=0 #remove those -1s in the regions that used to be 0 ##%%save + ## we are assuming that aff = nb.load(files[0]).affine header = nb.load(files[0]).header @@ -1390,12 +1391,12 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B new_nii.set_data_dtype('uint32') new_nii.to_filename(seg_idx_fname) - new_nii = nb.Nifti1Image(seg_total.astype('uint32'), aff, header) - new_nii.set_data_dtype('uint32') + new_nii = nb.Nifti1Image(seg_total.astype('float32'), aff, header) + new_nii.set_data_dtype('float32') new_nii.to_filename(seg_tot_fname) - new_nii = nb.Nifti1Image(seg_part.astype('uint32'), aff, header) - new_nii.set_data_dtype('uint32') + new_nii = nb.Nifti1Image(seg_part.astype('float32'), aff, header) + new_nii.set_data_dtype('float32') new_nii.to_filename(seg_prt_fname) """ @@ -1427,7 +1428,7 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B hard_seg_full = np.zeros(data_shape) seg_part_full = np.zeros(data_shape) seg_total_full = np.zeros(data_shape) - seg_pct_full = np.zeros_like(hard_seg_full) + seg_pct_full = np.zeros_like(hard_seg_full,dtype=np.float32) print("Data shape (single image): " + str(data_shape)) print("Slice: "), @@ -1436,7 +1437,7 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B for slice_idx in np.arange(0, data_shape[-1]): print(slice_idx), - data_list = [nb.load(fn).get_data()[:, :, slice_idx, np.newaxis] for fn in + data_list = [nb.load(fn).get_fdata()[:, :, slice_idx, np.newaxis] for fn in files] # load all of the files combined = np.concatenate(data_list, axis=-1) # concatenate all of the input data combined = np.concatenate((np.zeros_like(data_list[0]), combined), @@ -1497,12 +1498,12 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B new_nii.set_data_dtype('uint32') new_nii.to_filename(seg_idx_fname) - new_nii = nb.Nifti1Image(seg_total_full.astype('uint32'), aff, header) - new_nii.set_data_dtype('uint32') + new_nii = nb.Nifti1Image(seg_total_full.astype('float32'), aff, header) + new_nii.set_data_dtype('float32') new_nii.to_filename(seg_tot_fname) - new_nii = nb.Nifti1Image(seg_part_full.astype('uint32'), aff, header) - new_nii.set_data_dtype('uint32') + new_nii = nb.Nifti1Image(seg_part_full.astype('float32'), aff, header) + new_nii.set_data_dtype('float32') new_nii.to_filename(seg_prt_fname) """ From 0a18afec83c0660f29124f11c483625a8e97e2e6 Mon Sep 17 00:00:00 2001 From: "Christopher J. Steele" Date: Mon, 12 Apr 2021 21:38:31 -0400 Subject: [PATCH 2/6] two more cases that should be float32 matrices --- TractREC/TractREC.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TractREC/TractREC.py b/TractREC/TractREC.py index 5e88509..e612713 100644 --- a/TractREC/TractREC.py +++ b/TractREC/TractREC.py @@ -1456,8 +1456,8 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B seg_total_full[:, :, slice_idx] = combined.sum(axis=-1) # declare empty matrices for this loop for partial and temp for calculating the partial (num of winning seg) file - seg_part = np.zeros_like(hard_seg) - seg_temp = np.zeros_like(hard_seg) + seg_part = np.zeros_like(hard_seg,dtype=np.float32) + seg_temp = np.zeros_like(hard_seg,dtype=np.float32) idx = 1 for seg in files: From 4de3f6700636bfcd9bb2605532c5cc2d1c23ba65 Mon Sep 17 00:00:00 2001 From: "Christopher J. Steele" Date: Sat, 26 Feb 2022 08:56:37 -0500 Subject: [PATCH 3/6] update print statement --- TractREC/TractREC.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TractREC/TractREC.py b/TractREC/TractREC.py index e612713..c7cf200 100644 --- a/TractREC/TractREC.py +++ b/TractREC/TractREC.py @@ -312,7 +312,7 @@ def map_values_to_label_file(values_label_lut_csv_fname, label_img_fname, for idx,index in enumerate(indices): if VERBOSE: - print index, values[idx] + print("{}, {}".format(index, values[idx])) d_out[d==index] = values[idx] niiSave(out_mapped_label_fname,d_out,a,header=h) From 53600134f2f9f65829f355b4bc53638464433283 Mon Sep 17 00:00:00 2001 From: "Christopher J. Steele" Date: Sat, 26 Feb 2022 09:01:20 -0500 Subject: [PATCH 4/6] more print issues --- TractREC/TractREC.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/TractREC/TractREC.py b/TractREC/TractREC.py index c7cf200..0b76123 100644 --- a/TractREC/TractREC.py +++ b/TractREC/TractREC.py @@ -837,19 +837,19 @@ def extract_quantitative_metric(metric_files, label_files, IDs=None, label_df=No label_file = [s for s in label_files if ID in s] # make sure our label file is in the list that was passed if len(metric_file) > 1: print("") - print "OH SHIT, too many metric files. This should not happen!" + print("OH SHIT, too many metric files. This should not happen!") elif len(metric_file) == 0: print("") - print "OH SHIT, no matching metric file for: " + ID + print("OH SHIT, no matching metric file for: " + ID) print("This subject has not been processed") DATA_EXISTS = False if len(label_file) > 1: print("") - print "OH SHIT, too many label files. This should not happen!" + print("OH SHIT, too many label files. This should not happen!") elif len(label_file) == 0: print("") - print "OH SHIT, no matching label file for: " + ID + print("OH SHIT, no matching label file for: " + ID) print("This subject has not been processed") DATA_EXISTS = False else: #files should already be ordered @@ -866,11 +866,11 @@ def extract_quantitative_metric(metric_files, label_files, IDs=None, label_df=No # is in the list that was passed if len(thresh_mask_fname) > 1: print("") - print "OH SHIT, too many threshold mask files. This should not happen!" + print("OH SHIT, too many threshold mask files. This should not happen!") elif len(thresh_mask_fname) == 0: print("") - print "OH SHIT, no matching threshold mask file for: " + ID + print("OH SHIT, no matching threshold mask file for: " + ID) DATA_EXISTS = False else: thresh_mask_fname = thresh_mask_files[idx] @@ -884,10 +884,10 @@ def extract_quantitative_metric(metric_files, label_files, IDs=None, label_df=No ROI_mask_fname = [s for s in ROI_mask_files if ID in s] # make sure our label file is in the list that was passed if len(ROI_mask_fname) > 1: - print "OH SHIT, too many threshold mask files. This should not happen!" + print("OH SHIT, too many threshold mask files. This should not happen!") elif len(ROI_mask_fname) == 0: - print "OH SHIT, no matching ROI mask file for: " + ID + print("OH SHIT, no matching ROI mask file for: " + ID) DATA_EXISTS = False else: ROI_mask_fname = ROI_mask_files[idx] @@ -986,7 +986,7 @@ def extract_quantitative_metric(metric_files, label_files, IDs=None, label_df=No print("##=====================================================================##") print("Darn! There is something wrong with: " + ID) print("##=====================================================================##") - print "" + print("") if metric is not 'data': return df_4d else: @@ -1250,18 +1250,18 @@ def qcheck(user='stechr', delay=5 * 60): import subprocess print(time.strftime("%Y_%m_%d %H:%M:%S")) - print "=== start time ===", + print("=== start time ===") start = time.time() print(start) try: while len(subprocess.check_output(['qstat', '-u', user, '|', 'grep', user], shell=True)) > 0: - print ". ", + print(". ") # print(len(subprocess.check_output(['qstat', '-u', 'tachr'],shell=True))) time.sleep(delay) except: pass - print "=== end time ===", + print("=== end time ===") print(time.time()) print(time.strftime("%Y_%m_%d %H:%M:%S")) duration = time.time() - start @@ -1355,7 +1355,7 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B hard_seg_indexed[hard_seg == idx] = seg_val idx += 1 else: - print "" + print("") print("====== YOU DID NOT ENTER THE CORRECT NUMBER OF VALUES FOR segmentation_index ======") return @@ -1478,7 +1478,7 @@ def tract_seg3(files, out_basename='', segmentation_index=None, CLOBBER=False, B hard_seg_indexed[hard_seg == idx] = seg_val idx += 1 else: - print "" + print("") print("====== YOU DID NOT ENTER THE CORRECT NUMBER OF VALUES FOR segmentation_index ======") return None @@ -1602,15 +1602,15 @@ def dki_dke_prep_data_bvals_bvecs(data_fname, bvals_file, bvecs_file, out_dir=No os.path.basename(data_fname).split(".nii")[0] + "_bval" + str(bval) + ".nii.gz") vol_list = str([i for i, v in enumerate(bvals) if v == bval]).strip('[]').replace(" ", "") cmd_input = ['fslselectvols', '-i', data_fname, '-o', out_fname, '--vols=' + vol_list] - print "" - print " ".join(cmd_input) + print("") + print(" ".join(cmd_input)) cmd_txt.append(cmd_input) if not os.path.isfile(out_fname) or CLOBBER: if RUN_LOCALLY: subprocess.call(cmd_input) if bval == 0: # we mean this value if we are working with b=0 file cmd_input = ['fslmaths', out_fname, '-Tmean', out_fname] - print " ".join(cmd_input) + print(" ".join(cmd_input)) cmd_txt.append(cmd_input) if RUN_LOCALLY: subprocess.call(cmd_input) # no CLOBBER check here, since we actually want to overwrite this file @@ -1630,8 +1630,8 @@ def dki_dke_prep_data_bvals_bvecs(data_fname, bvals_file, bvecs_file, out_dir=No cmd_input = ['fslmerge', '-t', out_fname] for fname in fname_list: cmd_input = cmd_input + [fname] - print "" - print " ".join(cmd_input) + print("") + print(" ".join(cmd_input)) cmd_txt.append(cmd_input) if not os.path.isfile(out_fname) or CLOBBER: if RUN_LOCALLY: From 9f4ca7bde7485b85120a32c068dd99f7912dd7db Mon Sep 17 00:00:00 2001 From: "Christopher J. Steele" Date: Sat, 26 Feb 2022 09:03:55 -0500 Subject: [PATCH 5/6] another print statement --- TractREC/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TractREC/utils.py b/TractREC/utils.py index 7d38243..0c05684 100644 --- a/TractREC/utils.py +++ b/TractREC/utils.py @@ -535,7 +535,7 @@ def combine_connectome_matrices_sparse_hdf5(connectome_files_list, connectome_fi for row in lookup_row: #for col in lookup_col: dset[row, lookup_col] = np.ndarray.flatten(submat[row,:]) - print idx + print(idx) idx+=1 dset[lookup_row,lookup_col] = submat From 99a27c7f655af88e12772b7f18875153d07666e8 Mon Sep 17 00:00:00 2001 From: "Christopher J. Steele" Date: Sat, 26 Feb 2022 09:06:15 -0500 Subject: [PATCH 6/6] more print statements --- TractREC/preprocessing.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TractREC/preprocessing.py b/TractREC/preprocessing.py index 0b6215f..bc988a7 100644 --- a/TractREC/preprocessing.py +++ b/TractREC/preprocessing.py @@ -348,10 +348,10 @@ def run_diffusion_kurtosis_estimator_dipy(data_fnames,bvals_fnames,bvecs_fnames, create_dir(out_dir) #check that we are pulling the correct files if len(fname)>1 or len(bvals)>1 or len(bvecs)>1: - print "OH SHIT, too many possible files. This should not happen!" + print("OH SHIT, too many possible files. This should not happen!") DATA_EXISTS=False elif len(fname)<1 or len(bvals)<1 or len(bvecs)<1: - print "OH SHIT, no matching file for this ID: " + ID + print("OH SHIT, no matching file for this ID: " + ID) DATA_EXISTS=False else: fname=fname[0] #break it out of the list of one @@ -434,7 +434,7 @@ def run_amico_noddi_dipy(subject_root_dir,out_root_dir,subject_dirs=None,b0_thr= code=["#!/usr/bin/python","","import sys","sys.path.append('{0}')".format(caller_path),"sys.path.append('{0}')".format(spams_path),"import spams","import amico"] code.append("import spams" ) code.append("") - code.append("print amico.__file__") + code.append("print(amico.__file__)") code.append("amico.core.setup()") code.append("ae=amico.Evaluation('{subject_root_dir}','{ID}',output_path='{output_dir}')".format(subject_root_dir=subject_root_dir,ID=ID,output_dir=out_dir)) code.append("amico.util.fsl2scheme('{bvals_fname}', '{bvecs_fname}','{scheme_fname}',{bStep})".format(bvals_fname=bvals_fname, bvecs_fname=bvecs_fname,scheme_fname=scheme_fname,bStep=bStep)) @@ -547,7 +547,7 @@ def run_amico_noddi_dipy_v2(subject_root_dir,dwi_fnames,brain_mask_fnames,bvals_ code=["#!/usr/bin/python","","import sys","sys.path.append('{0}')".format(caller_path),"sys.path.append('{0}')".format(spams_path),"import spams","import amico"] code.append("import spams" ) code.append("") - code.append("print amico.__file__") + code.append("print(amico.__file__)") code.append("amico.core.setup()") code.append("ae=amico.Evaluation('{subject_root_dir}','{ID}',output_path='{output_dir}')".format(subject_root_dir=subject_root_dir,ID=ID,output_dir=out_dir)) code.append("amico.util.fsl2scheme('{bvals_fname}', '{bvecs_fname}','{scheme_fname}',{bStep})".format(bvals_fname=bvals_fname, bvecs_fname=bvecs_fname,scheme_fname=scheme_fname,bStep=bStep)) @@ -610,7 +610,7 @@ def interp_discrete_hist_peaks(input_fname, output_fname=None, value_count_cutof for idx, mybin in enumerate(left): #vox_vals = np.unique(d[np.logical_and(d >= left[idx], d <= right[idx])]) - print mybin + print(mybin) # should only be one value, but loop over it just in case... # loop across vox_vals #for vox_val in vox_vals: