class MaskIntersection(AbstractParallelRoutine): def __init__(self, config, in_folder, out_img_path, file_list_txt): super().__init__(config, in_folder, file_list_txt) self._out_img_path = out_img_path self._ref_img = ScanWrapper(self._in_data_folder.get_first_path()) def run(self): result_list = self.run_parallel() out_mask = self._get_intersect_mask(result_list) self._ref_img.save_scan_same_space(self._out_img_path, out_mask) def _run_single_scan(self, idx): in_img = ScanWrapper(self._in_data_folder.get_file_path(idx)) return in_img.get_data() def _run_chunk(self, chunk_list): result_list = [] inter_mask = np.ones(self._ref_img.get_shape()) for idx in chunk_list: self._in_data_folder.print_idx(idx) mask = self._run_single_scan(idx) inter_mask = np.multiply(inter_mask, mask) result_list.append(inter_mask) return result_list def _get_intersect_mask(self, mask_list): inter_mask = np.ones(self._ref_img.get_shape()) for mask in mask_list: inter_mask = np.multiply(inter_mask, mask) return inter_mask
class AverageValidRegion(AbstractParallelRoutine): def __init__(self, config, in_scan_folder, in_region_mask_folder, file_list_txt, out_average_img, ambient=-5000): super().__init__(config, in_scan_folder, file_list_txt) self._in_region_mask_folder = DataFolder.get_data_folder_obj( config, in_region_mask_folder, data_list_txt=file_list_txt) self._out_img_path = out_average_img self._ref_img = ScanWrapper(self._in_data_folder.get_first_path()) self._ambient = ambient def run_get_average(self): result_list = self.run_parallel() im_shape = self._ref_img.get_shape() sum_image = np.zeros(im_shape) region_count = np.zeros(im_shape) for result in result_list: sum_image += result['sum_image'] region_count += result['region_count'] sum_image = np.divide(sum_image, region_count, out=sum_image, where=region_count > 0.5) sum_image_ma = np.ma.masked_array(sum_image, mask=region_count == 0) self._ref_img.save_scan_same_space(self._out_img_path, sum_image_ma.filled(self._ambient)) self._ref_img.save_scan_same_space( self._out_img_path + '_region_count.nii.gz', region_count) def _run_chunk(self, chunk_list): result_list = [] im_shape = self._ref_img.get_shape() sum_image_union = np.zeros(im_shape) region_mask_count_image = np.zeros(im_shape) for idx in chunk_list: self._in_data_folder.print_idx(idx) img_obj = ScanWrapper(self._in_data_folder.get_file_path(idx)) mask_obj = ScanWrapper( self._in_region_mask_folder.get_file_path(idx)) sum_image_union += img_obj.get_data() region_mask_count_image += mask_obj.get_data() result = { 'sum_image': sum_image_union, 'region_count': region_mask_count_image } result_list.append(result) return result_list
def get_average_map(file_list, save_path): first_img = ScanWrapper(file_list[0]) im_shape = first_img.get_shape() sum_map = np.zeros(im_shape, dtype=float) for idx_image in range(len(file_list)): img = ScanWrapper(file_list[idx_image]) img_data = img.get_data() print(f'Adding {file_list[idx_image]} ({idx_image} / {len(file_list)})') print(f'Max intensity {np.max(img_data)}') sum_map += img_data average_map = sum_map / float(len(file_list)) print(f'Average map max int {np.max(average_map)}') first_img.save_scan_same_space(save_path, average_map)