def ssnr_sequential___individual_data_collect(self, r, op): if (self is None): get_mrc_func = IV.get_mrc else: get_mrc_func = self.cache.get_mrc v = get_mrc_func(r['subtomogram']) if 'angle' in r: v = GR.rotate_pad_mean(v, angle=N.array(r['angle'], dtype=N.float), loc_r=N.array(r['loc'], dtype=N.float)) if (op is not None) and ('segmentation_tg' in op) and ('template' in r) and ('segmentation' in r['template']): phi = IV.read_mrc_vol(r['template']['segmentation']) phi_m = (phi > 0.5) del phi (ang_inv, loc_inv) = AAL.reverse_transform_ang_loc(r['angle'], r['loc']) phi_mr = GR.rotate(phi_m, angle=ang_inv, loc_r=loc_inv, default_val=0) del phi_m del ang_inv, loc_inv import aitom.tomominer.pursuit.multi.util as PMU v_s = PMU.template_guided_segmentation(v=v, m=phi_mr, op=op['segmentation_tg']) del phi_mr if v_s is not None: v_f = N.isfinite(v_s) if v_f.sum() > 0: v_s[N.logical_not(v_f)] = v_s[v_f].mean() v = v_s del v_s v = NF.fftshift(NF.fftn(v)) m = get_mrc_func(r['mask']) if 'angle' in r: m = GR.rotate_mask(m, angle=N.array(r['angle'], dtype=N.float)) v[(m < op['mask_cutoff'])] = 0.0 return {'v': v, 'm': m, }
def data_matrix_collect(dj, templates, img_db): # print 'data_matrix_collect()' mat = None for i, d in enumerate(dj): v = img_db[d['subtomogram']] vm = img_db[d['mask']] # print(v.shape) # print(d['angle'],d['loc']) v_r = GR.rotate_pad_mean(v, angle=d['angle'], loc_r=d['loc']); assert N.all(N.isfinite(v_r)) vm_r = GR.rotate_mask(vm, angle=d['angle']); assert N.all(N.isfinite(vm_r)) from aitom.tomominer.pursuit.multi.util import impute_aligned_vols if 'template' in d: vi = CU.impute_aligned_vols(t=templates[d['template']]['v'], v=v_r, vm=(vm_r > 0.5)) else: vi = v_r vi = vi.flatten() if mat is None: mat = N.zeros([len(dj), vi.size]) mat[i, :] = vi # print ('\r', i, ' ') sys.stdout.flush() return mat
def vol_avg(dj, op, img_db): if len(dj) < op['mask_count_threshold']: return None vol_sum = None mask_sum = None # temporary collection of local volume, and mask. for d in dj: v = img_db[d['subtomogram']] vm = img_db[d['mask']] v_r = GR.rotate_pad_mean(v, angle=d['angle'], loc_r=d['loc']); assert N.all(N.isfinite(v_r)) vm_r = GR.rotate_mask(vm, angle=d['angle']); assert N.all(N.isfinite(vm_r)) if vol_sum is None: vol_sum = N.zeros(v_r.shape, dtype=N.float64, order='F') vol_sum += v_r if mask_sum is None: mask_sum = N.zeros(vm_r.shape, dtype=N.float64, order='F') mask_sum += vm_r ind = mask_sum >= op['mask_count_threshold'] if ind.sum() <= 0: return None vol_sum = NF.fftshift(NF.fftn(vol_sum)) avg = N.zeros(vol_sum.shape, dtype=N.complex) avg[ind] = vol_sum[ind] / mask_sum[ind] avg = N.real(NF.ifftn(NF.ifftshift(avg))) return {'v': avg, 'm': mask_sum / float(len(dj))}
def average(dj, mask_count_threshold): vol_sum = None mask_sum = None for d in dj: v = IF.read_mrc_vol(d['subtomogram']) if not N.all(N.isfinite(v)): raise Exception('error loading', d['subtomogram']) vm = IF.read_mrc_vol(d['mask']) v_r = GR.rotate_pad_mean(v, angle=d['angle'], loc_r=d['loc']) assert N.all(N.isfinite(v_r)) vm_r = GR.rotate_mask(vm, angle=d['angle']) assert N.all(N.isfinite(vm_r)) if vol_sum is None: vol_sum = N.zeros(v_r.shape, dtype=N.float64, order='F') vol_sum += v_r if mask_sum is None: mask_sum = N.zeros(vm_r.shape, dtype=N.float64, order='F') mask_sum += vm_r ind = (mask_sum >= mask_count_threshold) vol_sum_fft = NF.fftshift(NF.fftn(vol_sum)) avg = N.zeros(vol_sum_fft.shape, dtype=N.complex) avg[ind] = (vol_sum_fft[ind] / mask_sum[ind]) avg = N.real(NF.ifftn(NF.ifftshift(avg))) return { 'v': avg, 'm': (mask_sum / len(dj)), }
def var__local(self, data_json, labels=None, mask_cutoff=0.5, return_key=True, segmentation_tg_op=None): if labels is None: labels = ([0] * len(data_json)) sum_v = {} prod_sum_v = {} mask_sum = {} for (i, r) in enumerate(data_json): if (self is not None) and (self.work_queue is not None) and self.work_queue.done_tasks_contains( self.task.task_id): raise Exception('Duplicated task') v = IV.read_mrc_vol(r['subtomogram']) v = GR.rotate_pad_mean(v, angle=N.array(r['angle'], dtype=N.float), loc_r=N.array(r['loc'], dtype=N.float)) m = IV.read_mrc_vol(r['mask']) m = GR.rotate_mask(m, N.array(r['angle'], dtype=N.float)) if (segmentation_tg_op is not None) and ('template' in r) and ('segmentation' in r['template']): phi = IV.read_mrc(r['template']['segmentation'])['value'] import aitom.tomominer.pursuit.multi.util as PMU v_s = PMU.template_guided_segmentation(v=v, m=(phi > 0.5), op=segmentation_tg_op) if v_s is not None: v = v_s del v_s v_t = N.zeros(v.shape) v_f = N.isfinite(v) v_t[v_f] = v[v_f] v_t[N.logical_not(v_f)] = v[v_f].mean() v = v_t del v_f, v_t v = NF.fftshift(NF.fftn(v)) v[(m < mask_cutoff)] = 0.0 if labels[i] not in sum_v: sum_v[labels[i]] = v else: sum_v[labels[i]] += v if labels[i] not in prod_sum_v: prod_sum_v[labels[i]] = (v * N.conj(v)) else: prod_sum_v[labels[i]] += (v * N.conj(v)) if labels[i] not in mask_sum: mask_sum[labels[i]] = N.zeros(m.shape, dtype=N.int) mask_sum[labels[i]][(m >= mask_cutoff)] += 1 re = {'sum': sum_v, 'prod_sum': prod_sum_v, 'mask_sum': mask_sum, } if return_key: re_key = self.cache.save_tmp_data(re, fn_id=self.task.task_id) assert (re_key is not None) return {'key': re_key, } else: return re