def test_half_bt24cell_geom(): cellquatang = 2 * np.arccos( np.clip(np.sum(half_bt24cell_faces * half_bt24cell_faces[:, None], -1), -1, 1)) cellquatang = np.minimum(cellquatang, 2 * np.pi - cellquatang) cellcen = homog.quat.quat_to_xform(half_bt24cell_faces) cellcenang = homog.angle_of(homog.hinv(cellcen) @ cellcen[:, None]) cellcenanground = np.round(cellcenang * 180 / np.pi, 5) # print(np.unique(cellcenanground)) assert np.allclose(cellcenang, cellquatang) # np.fill_diagonal(cellcenang, 10) q = homog.quat.rand_quat((1000, )) qang = 2 * np.arccos( np.clip(np.abs(np.sum(q * half_bt24cell_faces[:, None], -1)), -1, 1)) minqang = np.min(qang, 0) # print('Qang to closest', # np.percentile(minqang * 180 / np.pi, np.arange(0, 101, 10))) assert np.max(minqang) < 62.81 / 180 * np.pi x = homog.rand_xform(1000) cellxform = homog.quat.quat_to_xform(half_bt24cell_faces) xang = homog.angle_of(x @ homog.hinv(cellxform)[:, None]) minxang = np.min(xang, 0) # print('Xang to closest:', # np.percentile(minxang * 180 / np.pi, np.arange(0, 101, 10))) assert np.max(minqang) < 62.81 / 180 * np.pi
def checkpoint(self): """TODO: Summary """ print('MakeXIndexAccumulator checkpoint', end='') sys.stdout.flush() if len(self.tmp) is 0: return sc = np.concatenate([x[0] for x in self.tmp]) indices = np.concatenate([x[1] for x in self.tmp])[sc <= self.thresh] assert np.all(indices < self.sizes) positions = np.concatenate([x[2] for x in self.tmp])[sc <= self.thresh] from_pos = positions[:, self.from_seg] to_pos = positions[:, self.to_seg] xtgt = hinv(from_pos) @ to_pos bin_idx = self.binner.get_bin_index(xtgt) for k, v in zip(bin_idx, indices): self.xindex[k].append(v) # self.xindex = {**{k: v for k, v in zip(bin_idx, indices)}, # **self.xindex} # print('IndexAcculator checkpoint, xindex size:', len(self.xindex)) self.tmp = [] print('done, xindex size:', len(self.xindex), sum(len(v) for v in self.xindex.values())) sys.stdout.flush()
def xform_between_chains(chain1, chain2, sele='name CA', state=1): assert chain1 != chain2 atoms1 = cmd.get_model(f'chain {chain1} and ({sele})', state).atom atoms2 = cmd.get_model(f'chain {chain2} and ({sele})', state).atom assert len(atoms1) > 2 assert len(atoms1) == len(atoms2) atoms1 = atoms1[0], atoms1[int(len(atoms1) / 2)], atoms1[-1] atoms2 = atoms2[0], atoms2[int(len(atoms2) / 2)], atoms2[-1] crds1 = [a.coord for a in atoms1] crds2 = [a.coord for a in atoms2] stub1 = hm.hstub(*crds1) stub2 = hm.hstub(*crds2) xform = stub2 @ hm.hinv(stub1) return xform
def primary_xform_commutator(units, state=1, **kw): print('stub 0:') print(units[0].stub) closest = None for j in range(1, 20): x = hm.hinv(units[0].stub) @ units[j].stub # print(f'stub {j}:') # print(units[j].stub) # print(x) axis, ang, cen = hm.axis_ang_cen_of(x) helical = hm.hdot(axis, x[:, 3]) print(j, helical, x[2, 3]) if abs(helical) > 0.01: return x return None
def test_XformBinner_covrad(): niter = 30 nsamp = 10000 for i in range(niter): cart_resl = np.random.rand() * 10 + 0.1 ori_resl = np.random.rand() * 50 + 2.5 xforms = homog.rand_xform(nsamp) xb = XformBinner(cart_resl, ori_resl) idx = xb.get_bin_index(xforms) cen, f6 = xb.get_bin_center(idx, debug=True) cart_dist = np.linalg.norm(xforms[..., :3, 3] - cen[..., :3, 3], axis=-1) ori_dist = homog.angle_of(homog.hinv(cen) @ xforms) # if not np.all(cart_dist < cart_resl): # print('ori_resl', ori_resl, 'nside:', xb.ori_nside, # 'max(cart_dist):', np.max(cart_dist), cart_resl) # if not np.all(cart_dist < cart_resl): # print('ori_resl', ori_resl, 'nside:', xb.ori_nside, # 'max(ori_dist):', np.max(ori_dist)) assert np.sum(cart_dist < cart_resl) > nsamp * 0.99 assert np.sum(ori_dist < ori_resl / 180 * np.pi) > nsamp * 0.99
def sort_and_calc_neighbors(units, sele='all', state=1, window=6, discut=8, **kw): print('sort_and_calc_neighbors') units.sort() xprimary = primary_xform_commutator(units, state=state) if xprimary is not None: primary_neighbor = {} other_neighbor = {} else: neighbors = [] for i in range(len(units)): isele = units[i].sele for j in range(i + 1, min(i + window + 1, len(units))): # if i == j: continue xcomm = hm.hinv(units[i].stub) @ units[j].stub jsele = units[j].sele isect_sele = (f'({isele} and name CA) within {discut} of ' + f'({jsele} and name CA)') n = cmd.select(isect_sele, state=state) if n > 0: if xprimary is not None: if np.allclose(xcomm, xprimary, atol=1e-2, rtol=1e-2): assert i not in primary_neighbor primary_neighbor[i] = j else: assert j not in other_neighbor other_neighbor[i] = j else: neighbors.append((i, j)) if xprimary is not None: assert len(primary_neighbor) > max(0, len(units) - 10) assert len(other_neighbor) > max(0, len(units) - 10) return primary_neighbor, other_neighbor else: return neighbors, None
def checkpoint(self): """TODO: Summary """ if len(self.temporary) is 0: return ntmp = sum(len(tmp[0]) for tmp in self.temporary) print('XIndexedAccumulator checkpoint... ncandidates:', ntmp, end=' ') sys.stdout.flush() if hasattr(self, 'scores'): sc, li, lp = [self.scores], [self.lowidx], [self.lowpos] else: sc, li, lp = [], [], [] scores = np.concatenate([x[0] for x in self.temporary]) if scores.shape[0] is 0: return assert np.all(scores == 0) lowidx = np.concatenate([x[1] for x in self.temporary]) lowpos = np.concatenate([x[2] for x in self.temporary]) scores = scores[:self.max_results] lowpos = lowpos[:self.max_results] lowidx = lowidx[:self.max_results] from_pos = lowpos[:, -1] to_pos = self.cyclic_xform @ from_pos xtgt = hinv(from_pos) @ to_pos bin_idx = self.binner.get_bin_index(xtgt) # head_idx = np.stack([self.xindex[i] for i in bin_idx]) lowidxtmp, headidxtmp = [], [] for i, b in enumerate(bin_idx): for headidx in self.xindex[b]: lowidxtmp.append(lowidx[i]) headidxtmp.append(headidx) lowidx = np.stack(lowidxtmp) head_idx = np.stack(headidxtmp) join_idx, valid = self.segments[self.splitpoint].merge_idx( self.tail[-1], lowidx[:, -1], self.head[0], head_idx[:, 0]) lowidx = lowidx[valid][join_idx >= 0] head_idx = head_idx[valid][join_idx >= 0] join_idx = join_idx[join_idx >= 0] # join_idx = self.segments[self.splitpoint].merge_idx_slow( # self.tail[-1], lowidx[:, -1], # self.head[0], head_idx[:, 0]) # lowidx = lowidx[join_idx >= 0] # head_idx = head_idx[join_idx >= 0] # join_idx = join_idx[join_idx >= 0] lowidx = np.concatenate( [lowidx[:, :-1], join_idx[:, None], head_idx[:, 1:]], axis=1) ifrom, ito = lowidx[:, self.from_seg], lowidx[:, self.to_seg] site1 = self.segments[self.from_seg].entrysiteid[ifrom] site2 = self.segments[self.from_seg].exitsiteid[ifrom] site3 = self.segments[self.to_seg].entrysiteid[ito] ok = (site1 != site2) * (site1 != site3) * (site2 != site3) # print('!!!!!!!!', np.sum(ok), ok.shape) # print('site1', *['%6i' % np.sum(site1 == i) for i in range(10)]) # print('site2', *['%6i' % np.sum(site2 == i) for i in range(10)]) # print('site3', *['%6i' % np.sum(site3 == i) for i in range(10)]) lowidx = lowidx[ok] if hasattr(self, 'lowidx'): self.lowidx = np.concatenate([self.lowidx, lowidx]) else: self.lowidx = lowidx self.temporary = [] print('done, total pre-err =', len(self.lowidx)) sys.stdout.flush()
import homog with redirect_stdout(open(os.devnull,'w')): from xbin import * ori_nside = int(sys.argv[1]) Nsamp = 1000000 Nptiles = 101 ptiles = np.concatenate([np.arange(Nptiles), 100 - 10**np.linspace(2, -6, Nptiles)]) estimate = np.zeros_like(ptiles) coherent = np.zeros_like(ptiles) while True: xforms = homog.rand_xform(Nsamp) xb = XformBinner(ori_nside=ori_nside) idx = xb.get_bin_index(xforms) cen = xb.get_bin_center(idx) ori_dist = homog.angle_of(homog.hinv(cen) @ xforms) ehat = np.percentile(ori_dist, ptiles) old = estimate estimate = np.maximum(estimate, ehat) if np.any(old != estimate): print('independent', Nsamp, ori_nside, str(estimate.tolist()).replace(',','')[1:-1]) sys.stdout.flush() if np.sum(ehat) > np.sum(coherent): coherent = ehat print('coherent', Nsamp, ori_nside, str(coherent.tolist()).replace(',','')[1:-1]) sys.stdout.flush()