def create_sliceinfo_w(images_fn, labels_fn, regint_fn, kernels, i, slice_no=None): # figure out the biggest slice if slice_no == None: slice_no = find_biggest_slice(path + labels_fn[i]) print("Creating Weighted Slice Info... {}/{}: Slice {}".format( i + 1, len(images_fn), slice_no)) # get the slice, label, and associated orientations slice_im, slice_im_or = get_nifti_slice(path + images_fn[i], slice_no) slice_lb, slice_lb_or = get_nifti_slice(path + labels_fn[i], slice_no) slice_ro, slice_ro_or = get_nifti_slice(path + regint_fn[i], slice_no) # if crop, we crop the image down if crop: crop_x, crop_y = crop start_x = slice_im.shape[0] / 2 - crop_x / 2 end_x = start_x + crop_x start_y = slice_im.shape[1] / 2 - crop_y / 2 end_y = start_y + crop_y slice_im = slice_im[start_x:end_x, start_y:end_y] slice_lb = slice_lb[start_x:end_x, start_y:end_y] slice_ro = slice_ro[start_x:end_x, start_y:end_y] # figure out the principal patches pc_payload = (slice_im, slice_lb) patches_m_pc, patches_n_pc, vals_m, vals_n = create_pc_patches_w( *pc_payload) # compute gabor features for the patches feats_m = [] feats_n = [] intens_m = [] intens_n = [] for patch in patches_m_pc: feats_m.append(compute_feats(patch, kernels)) intens_m.append(compute_intens(patch)) for patch in patches_n_pc: feats_n.append(compute_feats(patch, kernels)) intens_n.append(compute_intens(patch)) # package it into a SliceInfo object si_payload = (images_fn[i], slice_no, slice_im, slice_im_or, slice_lb, slice_lb_or, slice_ro, slice_ro_or, patches_m_pc, patches_n_pc, feats_m, feats_n, intens_m, intens_n, vals_m, vals_n) return SliceInfo(*si_payload)
def create_sliceinfo_w(images_fn, labels_fn, regint_fn, kernels, i, slice_no=None): # figure out the biggest slice if slice_no == None: slice_no = find_biggest_slice(path + labels_fn[i]) print("Creating Weighted Slice Info... {}/{}: Slice {}".format(i+1, len(images_fn), slice_no)) # get the slice, label, and associated orientations slice_im, slice_im_or = get_nifti_slice(path + images_fn[i], slice_no) slice_lb, slice_lb_or = get_nifti_slice(path + labels_fn[i], slice_no) slice_ro, slice_ro_or = get_nifti_slice(path + regint_fn[i], slice_no) # if crop, we crop the image down if crop: crop_x, crop_y = crop start_x = slice_im.shape[0] / 2 - crop_x / 2 end_x = start_x + crop_x start_y = slice_im.shape[1] / 2 - crop_y / 2 end_y = start_y + crop_y slice_im = slice_im[start_x:end_x, start_y:end_y] slice_lb = slice_lb[start_x:end_x, start_y:end_y] slice_ro = slice_ro[start_x:end_x, start_y:end_y] # figure out the principal patches pc_payload = (slice_im, slice_lb) patches_m_pc, patches_n_pc, vals_m, vals_n = create_pc_patches_w(*pc_payload) # compute gabor features for the patches feats_m = [] feats_n = [] intens_m = [] intens_n = [] for patch in patches_m_pc: feats_m.append(compute_feats(patch, kernels)) intens_m.append(compute_intens(patch)) for patch in patches_n_pc: feats_n.append(compute_feats(patch, kernels)) intens_n.append(compute_intens(patch)) # package it into a SliceInfo object si_payload = (images_fn[i], slice_no, slice_im, slice_im_or, slice_lb, slice_lb_or, slice_ro, slice_ro_or, patches_m_pc, patches_n_pc, feats_m, feats_n, intens_m, intens_n, vals_m, vals_n) return SliceInfo(*si_payload)
def classify_patch_group(fn_rf, fn_kern, fn_patch_info): t0 = time() RF = dill.load(open(fn_rf, 'rb')) # unpack the RF classifier kernels = dill.load(open(fn_kern, 'rb')) # unpack the kernels patch_info = dill.load(open(fn_patch_info, 'rb')) # unpack the patch info a, b, c = patch_info[0] # get the bounds of the set patches_a = patch_info[1] # grab the set to classify patches_r = patch_info[2] # grab the set to compare with (atlas mask) results = [] for i, patch in enumerate(patches_a): # go through each patch if np.all(patches_r[i]): # if the patch is entirely masked feat = compute_feats(patch, kernels).flatten().reshape(1, -1) intens = np.array(compute_intens(patch)).flatten().reshape(1, -1) feat = np.concatenate((feat, intens), axis=1) prediction = RF.predict(feat) #print("Classifying patch {}/{}: {}".format(i, len(patches), prediction)) results.append(np.full(patch.shape, prediction)) else: # the associated ROI patch is totally zero results.append(np.zeros(patch.shape)) dt = time() - t0 print("Classified group {}-{}/{} in {:.2f} time".format(a, b, c, dt)) return results
def classify_patch_group(fn_rf, fn_kern, fn_patch_info): t0 = time() RF = dill.load(open(fn_rf, "rb")) # unpack the RF classifier kernels = dill.load(open(fn_kern, "rb")) # unpack the kernels patch_info = dill.load(open(fn_patch_info, "rb")) # unpack the patch info a, b, c = patch_info[0] # get the bounds of the set patches_a = patch_info[1] # grab the set to classify patches_r = patch_info[2] # grab the set to compare with (atlas mask) results = [] for i, patch in enumerate(patches_a): # go through each patch if np.all(patches_r[i]): # if the patch is entirely masked feat = compute_feats(patch, kernels).flatten().reshape(1, -1) intens = np.array(compute_intens(patch)).flatten().reshape(1, -1) feat = np.concatenate((feat, intens), axis=1) prediction = RF.predict(feat) # print("Classifying patch {}/{}: {}".format(i, len(patches), prediction)) results.append(np.full(patch.shape, prediction)) else: # the associated ROI patch is totally zero results.append(np.zeros(patch.shape)) dt = time() - t0 print("Classified group {}-{}/{} in {:.2f} time".format(a, b, c, dt)) return results