class ScanFolderFlatReader(AbstractParallelRoutine): def __init__(self, config, in_folder, ref_img): super().__init__(config, in_folder) self._ref_img = ScanWrapper(ref_img) self._data_matrix = [] def read_data(self): print(f'Reading scan from folder {self._in_data_folder.get_folder()}') tic = time.perf_counter() self._data_matrix = self._init_data_matrix() self.run_non_parallel() # self.run_parallel() toc = time.perf_counter() print(f'Done. {toc - tic:0.4f} (s)') def get_data_matrix(self): return self._data_matrix def _run_single_scan(self, idx): in_file_path = self._in_data_folder.get_file_path(idx) in_data = ScanWrapper(in_file_path) in_img = in_data.get_data() # self._data_matrix[idx, :] = in_img.reshape(in_data.get_number_voxel()) self._data_matrix[idx, :] = convert_3d_2_flat(in_img) def _init_data_matrix(self): num_features = self._ref_img.get_number_voxel() num_sample = self.num_files() data_matrix = np.zeros((num_sample, num_features)) return data_matrix
class ScanFolderConcatBatchReader(AbstractParallelRoutine): def __init__(self, config, in_ori_folder, in_jac_folder, batch_size, file_list_txt=None): super().__init__(config, in_ori_folder, file_list_txt) self._in_jac_folder = DataFolder(in_jac_folder, file_list_txt) self._ref_ori = ScanWrapper(self._in_data_folder.get_file_path(0)) self._ref_jac = ScanWrapper(self._in_jac_folder.get_file_path(0)) self._chunk_list = self._in_data_folder.get_chunks_list_batch_size( batch_size) self._data_matrix = [] self._cur_idx = 0 def read_data(self, idx_batch): self._reset_cur_idx() print(f'Reading scans from folder {self._in_data_folder.get_folder()}', flush=True) tic = time.perf_counter() cur_batch = self._chunk_list[idx_batch] self._init_data_matrix(len(cur_batch)) self.run_non_parallel(cur_batch) toc = time.perf_counter() print(f'Done. {toc - tic:0.4f} (s)', flush=True) def num_batch(self): return len(self._chunk_list) def get_data_matrix(self): return self._data_matrix def save_flat_data(self, data_array, idx, out_folder): out_path_ori = os.path.join(out_folder, f'pc_ori_{idx}.nii.gz') out_path_jac = os.path.join(out_folder, f'pc_jac_{idx}.nii.gz') ori_data_flat = data_array[:self._ref_ori.get_number_voxel()] jac_data_flat = data_array[self._ref_ori.get_number_voxel():] self._ref_ori.save_scan_flat_img(ori_data_flat, out_path_ori) self._ref_jac.save_scan_flat_img(jac_data_flat, out_path_jac) def get_ref(self): return self._ref_ori def _run_single_scan(self, idx): in_ori_data = ScanWrapper( self._in_data_folder.get_file_path(idx)).get_data() in_jac_data = ScanWrapper( self._in_jac_folder.get_file_path(idx)).get_data() self._data_matrix[self._cur_idx, :self._ref_ori.get_number_voxel( )] = convert_3d_2_flat(in_ori_data) self._data_matrix[ self._cur_idx, self._ref_ori.get_number_voxel():] = convert_3d_2_flat(in_jac_data) self._cur_idx += 1 def _init_data_matrix(self, num_sample): num_features = self._get_number_of_voxel() del self._data_matrix self._data_matrix = np.zeros((num_sample, num_features)) def _get_number_of_voxel(self): return self._get_number_of_voxel_ori() + self._get_number_of_voxel_jac( ) def _get_number_of_voxel_ori(self): return self._ref_ori.get_number_voxel() def _get_number_of_voxel_jac(self): return self._ref_jac.get_number_voxel() def _reset_cur_idx(self): self._cur_idx = 0
class ScanFolderBatchReader(AbstractParallelRoutine): def __init__(self, config, in_folder, ref_img, batch_size, file_list=None): super().__init__(config, in_folder, file_list) self._ref_img = ScanWrapper(self._in_data_folder.get_file_path(0)) self._chunk_list = self._in_data_folder.get_chunks_list_batch_size( batch_size) self._data_matrix = [] self._cur_idx = 0 def read_data(self, idx_batch): self._reset_cur_idx() print(f'Reading scans from folder {self._in_data_folder.get_folder()}', flush=True) tic = time.perf_counter() cur_batch = self._chunk_list[idx_batch] self._init_data_matrix(len(cur_batch)) self.run_non_parallel(cur_batch) toc = time.perf_counter() print(f'Done. {toc - tic:0.4f} (s)', flush=True) def num_batch(self): return len(self._chunk_list) def get_data_matrix(self): return self._data_matrix def get_batch_idx_list(self, idx_batch): return self._chunk_list[idx_batch] def save_flat_data(self, data_array, idx, out_folder): out_path_ori = os.path.join(out_folder, f'pc_{idx}.nii.gz') self._ref_img.save_scan_flat_img(data_array, out_path_ori) def get_ref(self): return self._ref_img def get_batch_file_name_list(self, idx_batch): batch_idx_list = self._chunk_list[idx_batch] file_name_list = [ self._in_data_folder.get_file_name(file_idx) for file_idx in batch_idx_list ] return file_name_list def _run_single_scan(self, idx): in_file_path = self._in_data_folder.get_file_path(idx) in_data = ScanWrapper(in_file_path) in_img = in_data.get_data() # self._data_matrix[idx, :] = in_img.reshape(in_data.get_number_voxel()) # self._data_matrix[idx, :] = convert_3d_2_flat(in_img) self._data_matrix[self._cur_idx, :] = convert_3d_2_flat(in_img) self._cur_idx += 1 def _init_data_matrix(self, num_sample): num_features = self._get_number_of_voxel() del self._data_matrix self._data_matrix = np.zeros((num_sample, num_features)) def _reset_cur_idx(self): self._cur_idx = 0 def _get_number_of_voxel(self): return self._ref_img.get_number_voxel()