def make_ibis_qc(): with h5py.File(workdir + 'ibis.hdf5', 'w') as f: f.create_dataset( 'ibis_t1', (total_subjects, target_size[0], target_size[1], target_size[2]), dtype='float32') f.create_dataset('qc_label', (total_subjects, 2), dtype='float32') dt = h5py.special_dtype(vlen=bytes) f.create_dataset('filename', (total_subjects, ), dtype=dt) index = 0 indices = [] labels = [] with open(label_file, 'r') as labels_csv: qc_reader = csv.reader(labels_csv) next(qc_reader) for line in qc_reader: try: t1_filename = line[3][9:] label = line[4] if 'Pass' in label: one_hot = [0.0, 1.0] pass_fail = 1 else: one_hot = [1.0, 0.0] pass_fail = 0 f['qc_label'][index, :] = one_hot t1_data = nib.load(datadir + t1_filename).get_data() if not t1_data.shape == target_size: # print('resizing from', t1_data.shape) t1_data = resize_image_with_crop_or_pad( t1_data, img_size=target_size, mode='constant') f['ibis_t1'][index, ...] = normalise_zero_one(t1_data) f['filename'][index] = t1_filename.split('/')[-1] # plt.imshow(t1_data[96, ...]) # plt.axis('off') # plt.savefig(output_dir + t1_filename[:-4] + '.png', bbox_inches='tight', cmap='gray') indices.append(index) labels.append(pass_fail) index += 1 except Exception as e: print('Error:', e) print('Total subjects we actually have:', index + 1) return indices, labels
def make_abide_subject(line, subject_index, input_path, f): try: t1_filename = line[0] + '.mnc' # register_MINC(input_path + t1_filename, atlas, input_path + '/resampled/' + t1_filename) one_hot = [0, 0, 0] total_labels = 0 if len(line[2]) > 0: label1 = int(line[2]) + 1 # -1, 0, or 1 one_hot[label1] += 1 total_labels += 1 if len(line[3]) > 0: label2 = int(line[3]) + 1 one_hot[label2] += 1 total_labels += 1 # if len(line[4]) > 0: # label3 = int(line[4]) + 1 # one_hot[label3] += 1 # total_labels += 1 one_hot = [one_hot[0], one_hot[1] + one_hot[2] ] # map doubtful and accept to one label one_hot = np.multiply(one_hot, 1 / total_labels) f['qc_label'][subject_index, :] = one_hot # print(t1_filename, one_hot) t1_data = nib.load(input_path + '/resampled/' + t1_filename).get_data() if not t1_data.shape == target_size: t1_data = resize_image_with_crop_or_pad(t1_data, img_size=target_size, mode='constant') f['MRI'][subject_index, ...] = normalise_zero_one(t1_data) f['dataset'][subject_index] = 'ABIDE' # plt.imshow(t1_data[96, ...]) # plt.axis('off') # plt.savefig(output_dir + t1_filename[:-4] + '.png', bbox_inches='tight', cmap='gray') return subject_index except Exception as e: print('Error:', e) return -1
def make_ds030_subject(line, subject_index, input_path, f): try: t1_filename = line[0] + '.nii.gz' label = line[8] if len(label) > 0: t1 = nib.load(input_path + t1_filename) atlas_image = nib.load(atlas) t1_resampled = resample_from_to(t1, atlas_image) t1_data = t1_resampled.get_data() if not t1_data.shape == target_size: print('resizing from', t1_data.shape) t1_data = resize_image_with_crop_or_pad(t1_data, img_size=target_size, mode='constant') f['MRI'][subject_index, ...] = normalise_zero_one(t1_data) one_hot = [0, 0] if 'ok' in label: one_hot = [0, 1] elif 'maybe' in label: one_hot = [0, 1] elif 'exclude' in label: one_hot = [1, 0] else: raise Exception f['qc_label'][subject_index, :] = one_hot f['dataset'][subject_index] = 'ds030' # plt.imshow(t1_data[96, ...]) # plt.axis('off') # plt.savefig(output_dir + t1_filename[:-4] + '.png', bbox_inches='tight', cmap='gray') except Exception as e: print('Error:', e) return -1 return subject_index
def make_ping_subject(line, subject_index, input_path, f): try: t1_filename = line[0][:-4] + '.mnc' label = int(line[1]) # 0, 1, or 2 comment = line[2] # register_MINC(input_path + t1_filename, atlas, input_path + '/resampled/' + t1_filename) one_hot = [0, 0] if label >= 1: one_hot = [0, 1] else: one_hot = [1, 0] f['qc_label'][subject_index, :] = one_hot # print(t1_filename, one_hot) t1_data = nib.load(input_path + '/resampled/' + t1_filename).get_data() if not t1_data.shape == target_size: # print('resizing from', t1_data.shape) t1_data = resize_image_with_crop_or_pad(t1_data, img_size=target_size, mode='constant') # f['comment'][subject_index] = comment f['MRI'][subject_index, ...] = normalise_zero_one(t1_data) f['dataset'][subject_index] = 'PING' # plt.imshow(t1_data[96, ...]) # plt.axis('off') # plt.savefig(output_dir + t1_filename[:-4] + '.png', bbox_inches='tight', cmap='gray') return subject_index except Exception as e: print('Error:', e) return -1
description= "Tests an MRI image for motion and other artifacts and returns a probability of success." ) parser.add_argument("t1image") args, leftovers = parser.parse_known_args() image_file = args.t1image print('Input image file:', image_file) # K.set_image_dim_ordering('th') try: img = nib.load(image_file).get_data() img = resize_image_with_crop_or_pad(img, (168, 256, 244), mode='constant') img = normalise_zero_one(img) # (x_size, y_size, z_size) = img.shape # # print('original image size:', img.shape) # # y_max, z_max = 256, 224 # # while y_size < y_max or z_size < z_max: # img = np.pad(img, 1, 'constant') # (x_size, y_size, z_size) = img.shape # # y_mid, z_mid = y_size/2, z_size/2 # # x_slice = int(x_size/2) # y_start, y_stop = int(y_mid-y_max/2), int(y_mid+y_max/2)
def make_ibis_qc(): total_subjects = 0 with open(label_file, 'r') as labels_csv: qc_reader = csv.reader(labels_csv) next(qc_reader) for line in qc_reader: t1_filename = line[3][9:] try: t1 = nib.load(datadir + t1_filename) total_subjects += 1 except: print('Missing', t1_filename) with h5py.File(workdir + 'ibis_revisited.hdf5', 'w') as f: f.create_dataset( 'ibis_t1', (total_subjects, target_size[0], target_size[1], target_size[2]), dtype='float32') f.create_dataset('qc_label', (total_subjects, 2), dtype='float32') dt = h5py.special_dtype(vlen=bytes) f.create_dataset('filename', (total_subjects, ), dtype=dt) index = 0 indices = [] labels = [] with open(label_file, 'r') as labels_csv: qc_reader = csv.reader(labels_csv) next(qc_reader) for line in qc_reader: try: t1_filename = line[3][9:] label = line[4] if 'Pass' in label: pass_fail = 1 else: pass_fail = 0 f['qc_label'][index] = pass_fail t1_data = nib.load(datadir + t1_filename).get_data() if not t1_data.shape == target_size: # print('resizing from', t1_data.shape) t1_data = resize_image_with_crop_or_pad( t1_data, img_size=target_size, mode='constant') # linear rescaling between 0 and 1 t1_data = np.subtract(t1_data, np.min(t1_data)) t1_data = np.divide(t1_data, np.max(t1_data)) t1_data = np.float32(t1_data) x_max, y_max, z_max = 168, 256, 224 while x_size < x_max or y_size < y_max or z_size < z_max: t1_data = np.pad(t1_data, 1, 'constant') (x_size, y_size, z_size) = t1_data.shape f['ibis_t1'][index, ...] = t1_data[0:x_max, 0:y_max, 0:z_max] f['filename'][index] = t1_filename.split('/')[-1] # plt.imshow(t1_data[96, ...]) # plt.axis('off') # plt.savefig(output_dir + t1_filename[:-4] + '.png', bbox_inches='tight', cmap='gray') indices.append(index) labels.append(pass_fail) index += 1 except Exception as e: print('Error:', e) print('Total subjects we actually have:', index + 1)