def set_mask(self, mask): assert type(mask) is MedicalVolume, "mask for femoral cartilage must be of type MedicalVolume" msk = np.asarray(nlm.largest_cc(mask.volume), dtype=np.uint8) mask_copy = MedicalVolume(msk, mask.pixel_spacing) self.regions_mask = None super().set_mask(mask_copy)
def set_mask(self, mask: MedicalVolume): # get the largest connected component from the mask - we expect femoral cartilage to be a smooth volumes msk = np.asarray(nlm.largest_cc(mask.volume), dtype=np.uint8) mask_copy = deepcopy(mask) mask_copy.volume = msk super().set_mask(mask_copy) self.regions_mask, self.theta_bins, self.ML_BOUNDARY, self.ACP_BOUNDARY = self.split_regions( self.__mask__.volume)
def set_mask(self, mask: MedicalVolume): """Set mask for tissue. Mask is cleaned by selecting the largest connected component from the mask. Femoral cartilage is expected to be single connected tissue. Args: mask (MedicalVolume): Binary mask of segmented tissue. """ msk = np.asarray(nlm.largest_cc(mask.volume), dtype=np.uint8) mask_copy = deepcopy(mask) mask_copy.volume = msk super().set_mask(mask_copy) self.regions_mask, self.theta_bins, self.ML_BOUNDARY, self.ACP_BOUNDARY = self.split_regions( self.__mask__.volume)
tc = tc["tc_vox"] pca = PCA(n_components=10) pca.fit(tc.T) if sub == subList[0]: tc_group = preprocessing.standardize(pca.transform(tc.T)) else: tc_group = np.hstack((tc_group, preprocessing.standardize(pca.transform(tc.T)))) print("Concatenating subject" + sub + "'s timecourses") #io.savemat(os.path.join(BASE_DIR, "group/tc_rest_pca_vox.mat"), {"tc_group": tc_group}) # Perform parcellation on PCA-ed timecourses brain_img = as_volume_img("/volatile/bernardng/templates/spm8/rgrey.nii") brain = brain_img.get_data() dim = np.shape(brain) brain = brain > 0.2 # Generate brain mask brain = mask_utils.largest_cc(brain) mem = Memory(cachedir='.', verbose=1) # Define connectivity based on brain mask A = grid_to_graph(n_x=brain.shape[0], n_y=brain.shape[1], n_z=brain.shape[2], mask=brain) # Create ward object ward = WardAgglomeration(n_clusters=500, connectivity=A, memory=mem) tc_group = tc_group.reshape((dim[0], dim[1], dim[2], -1)) n_tpts = tc_group.shape[-1] for t in np.arange(n_tpts): tc_group[:,:,:,t] = gaussian_filter(tc_group[:,:,:,t], sigma=5) tc_group = tc_group.reshape((-1, n_tpts)) tc_group = tc_group[brain.ravel()==1, :] print("Performing Ward Clustering") ward.fit(tc_group.T) template = np.zeros((dim[0], dim[1], dim[2])) template[brain==1] = ward.labels_ + 1 # Previously processed data did not include +1
# Paths to all EPI volumes with name matching wildcard epi_files = sorted(glob.glob(os.path.join(BASE_DIR, 'fmri', 'swf*.hdr'))) # Load gm_img and epi_img objects gm_img = as_volume_img(gm_file) epi_ref_img = as_volume_img(epi_ref_file) # Resample tissue mask to grid of epi gm_img = gm_img.resampled_to_img(epi_ref_img) # Extract tissue mask gm = gm_img.get_data() # Normalize the mask to [0,1] gm -= gm.min() gm /= gm.max() # Threshold tissue mask gm_mask = (gm > .5) # Find largest connected component gm_mask = mask_utils.largest_cc(gm_mask) # Extract graymatter voxel timecourses time_series_gm, header_gm = mask_utils.series_from_mask(epi_files, gm_mask) time_series_gm = preprocessing.standardize(time_series_gm).T n_tpts = time_series_gm.shape[0] # Load motion regressors motion_regressor = np.loadtxt(os.path.join(BASE_DIR, "fmri", "rp_fga070108233-0004-00002-000002-01.txt")) motion_regressor = preprocessing.standardize(motion_regressor) # Bandpass filter to remove DC offset and low frequency drifts beta, _, _, _ = linalg.lstsq(motion_regressor,time_series_gm) time_series_gm -= np.dot(motion_regressor,beta) f_cut = np.array([0.01, 0.1]) tr = 2.4 samp_freq = 1 / tr w_cut = f_cut * 2 / samp_freq
final_prob_map=final_prob_map/len(model_list) else: sum_predict=(sum_predict*1.0)/(1.0*len(model_list)) ##vote if not hard_ensemble: ## get result from essembled prob. pred = np.argmax(final_prob_map, axis=1) pred = np.uint8(pred) else: ## majority vote sum_predict[sum_predict>=0.5] = 1 sum_predict[sum_predict < 0.5] = 0 pred=np.uint8(sum_predict*255) print(np.sum(pred)) mask = sitk.GetImageFromArray(pred) mask = sitk.BinaryDilate(mask, 2) mask = sitk.BinaryErode(mask, 2) pred = sitk.GetArrayFromImage(mask) voted_mask_binary = largest_cc(pred) pred = pred * voted_mask_binary t+=time()-start_time save_nrrd_result(file_path=os.path.join(patient_img_dir, 'EMMA_soft.nrrd'), data=pred, reference_img=sitk_image) print ('average time:', t/count)
pca = PCA(n_components=10) pca.fit(tc.T) if sub == subList[0]: tc_group = preprocessing.standardize(pca.transform(tc.T)) else: tc_group = np.hstack( (tc_group, preprocessing.standardize(pca.transform(tc.T)))) print("Concatenating subject" + sub + "'s timecourses") #io.savemat(os.path.join(BASE_DIR, "group/tc_rest_pca_vox.mat"), {"tc_group": tc_group}) # Perform parcellation on PCA-ed timecourses brain_img = as_volume_img("/volatile/bernardng/templates/spm8/rgrey.nii") brain = brain_img.get_data() dim = np.shape(brain) brain = brain > 0.2 # Generate brain mask brain = mask_utils.largest_cc(brain) mem = Memory(cachedir='.', verbose=1) # Define connectivity based on brain mask A = grid_to_graph(n_x=brain.shape[0], n_y=brain.shape[1], n_z=brain.shape[2], mask=brain) # Create ward object ward = WardAgglomeration(n_clusters=500, connectivity=A, memory=mem) tc_group = tc_group.reshape((dim[0], dim[1], dim[2], -1)) n_tpts = tc_group.shape[-1] for t in np.arange(n_tpts): tc_group[:, :, :, t] = gaussian_filter(tc_group[:, :, :, t], sigma=5) tc_group = tc_group.reshape((-1, n_tpts)) tc_group = tc_group[brain.ravel() == 1, :] print("Performing Ward Clustering")
def predict_img(sequence, if_mip, if_gamma, if_clahe, n_classes, full_img, batch_size=4, if_force_norm=False, sequence_length=1, post_process=False): ''' predict a whole 3D image data of a patient :param sequence: Boolean: if the network is desined to process with sequence images :param if_mip: Boolean: if use MIP as preprocess :param if_gamma: Boolean: if use automatic gamma augmentation as preprocess :param if_clahe: Boolean: if use clahe as preprocess :param n_classes: int: num of predicted class :param full_img: np array: original data from one patient (N*H*W) :param batch_size: int: the size of batch for prediction :param if_force_norm: boolean: do std norm after all preprocess :param sequence_length: int: the sequence length if use sequence data :param post_process: boolean: if true, then do morphological operations and keep the largest component for final prediction. :return:result: N*H_new*W_new original_space_result:N*orig_h*orig_w transformed_img: result_prob_map ''' global net ## predictor ## prepare data if sequence: transformed_img, origin_h, origin_w = preprocess_sequence_image( sequence_length, if_gamma, if_clahe, full_img, if_force_norm) else: transformed_img, origin_h, origin_w = preprocess_image( if_mip, if_gamma, if_clahe, full_img, if_force_norm) print(transformed_img.shape) data_size = transformed_img.shape[0] n_batch = int(np.round(data_size / batch_size)) index_tuple = [i for i in range(data_size)] if n_batch * batch_size < data_size: n_batch += 1 for i in range(n_batch * batch_size - data_size): index_tuple.append(data_size - 1) print(index_tuple) result = np.zeros( (data_size, transformed_img.shape[2], transformed_img.shape[3]), dtype=np.uint8) result_prob_map = np.zeros((data_size, n_classes, transformed_img.shape[2], transformed_img.shape[3]), dtype=np.float32) original_space_result = np.zeros((data_size, origin_h, origin_w), dtype=np.uint8) print('dsa', n_batch) print('dsa', batch_size) RCP = ReverseCropPad(origin_h, origin_w) ### prediction: for i in range(n_batch): batch_data = transformed_img[i * batch_size:(i + 1) * batch_size, :, :, :] print(batch_data.shape) torch_batch_data = torch.from_numpy(batch_data).float() print('tor', torch_batch_data.shape) if torch.cuda.is_available(): net.cuda(0) images = Variable(torch_batch_data.cuda(0), volatile=True) else: images = Variable(torch_batch_data, volatile=True) ## predict if isinstance(net, torch.nn.DataParallel): net_name = net.module.get_net_name() with torch.no_grad(): outputs = net.module.predict(images) else: with torch.no_grad(): outputs = net.predict(images) pred = outputs.data.max(1)[1].cpu().numpy() result[i * batch_size:(i + 1) * batch_size] = pred result_prob_map[i * batch_size:(i + 1) * batch_size] = outputs.data.cpu().numpy() ## save prediction to its original size rescale_result = RCP(pred) original_space_result[i * batch_size:(i + 1) * batch_size] = rescale_result print(result.shape) if post_process: if np.sum(original_space_result) > 0: mask = sitk.GetImageFromArray(original_space_result) mask = sitk.BinaryDilate(mask, 2) mask = sitk.BinaryErode(mask, 2) original_space_result = sitk.GetArrayFromImage(mask) voted_mask_binary = largest_cc(original_space_result) original_space_result = original_space_result * voted_mask_binary return result, original_space_result, result_prob_map