def rotate_image(self,data,angle_deg,sigma=1.0): # we take an image, rotate, interpolate it back onto a regular grid and return it # # There is however the problem that some pixels will never be seen. those pixels will be filled up by interpolation data = data*self.mask cost = smath.cos( angle_deg*smath.pi/180.0 ) sint = smath.sin( angle_deg*smath.pi/180.0 ) new_image = flex.double(self.grid,0) visited = flex.bool(self.grid,False) nx_array = flex.double() ny_array = flex.double() val_array = flex.double() for ix in range(self.np): for iy in range(self.np): x = ix - self.ox y = iy - self.oy nx = cost*x - sint*y ny = sint*x + cost*y jx = int(nx+self.ox+0.5) jy = int(ny+self.oy+0.5) if jx >= self.np : jx = self.np-1 if jy >= self.np : jy = self.np-1 if jx<0: jx=0 if jy<0: jy=0 new_image[ (jx,jy) ] = data[ (ix,iy) ] visited[ (jx,jy) ] = True nx_array.append( nx+self.ox ) ny_array.append( ny+self.oy ) val_array.append( data[ (ix,iy) ] ) assert nx_array.size() == self.mask.size() not_visited = ~visited not_visited = not_visited.iselection() # now we need to loop over all non-visited pixel values for pixel in not_visited: #for ix in range(self.np): # for iy in range(self.np): nv = 0 if self.mask[(ix,iy)]>-0.1: nv = -9 index = n_dim_index_from_one_dim(pixel, [self.np,self.np] ) nvx = index[1] nvy = index[0] dx = nx_array-nvx dy = ny_array-nvy dd = (dx*dx+dy*dy) ss = flex.exp( -dd/(sigma*sigma) )*self.mask nv = flex.sum(ss*val_array)/(1e-12+flex.sum(ss)) new_image[ (ix,iy) ] = nv new_image = new_image*self.mask return new_image
def enlarge(data,factor,sigma=0.1,full=False): n,n = data.focus() m = int(n*factor) x = flex.double() y = flex.double() vals = flex.double() sigma=sigma*factor new_data = flex.double( flex.grid(m,m), -9 ) visited = flex.bool( flex.grid(m,m), False ) oo = n/2.0 for ii in range(n): for jj in range(n): dd = smath.sqrt( (ii-oo)**2.0 + (jj-oo)**2.0 ) if dd <= oo: nx = ii*factor ny = jj*factor x.append( nx ) y.append( ny ) vals.append( data[ (ii,jj) ] ) new_data[ (int(nx), int(ny)) ] = data[ (ii,jj) ] if not full: visited[ (int(nx), int(ny)) ] = True not_visited = ~visited not_visited = not_visited.iselection() # now we need to loop over all non-visited pixel values for pixel in not_visited: nv = -9 index = n_dim_index_from_one_dim(pixel, [m,m] ) nvx = index[1] nvy = index[0] dx = x-nvx dy = y-nvy dd = (dx*dx+dy*dy) ss = flex.exp( -dd/(sigma*sigma) ) nv = flex.sum(ss*vals)/(1e-12+flex.sum(ss)) new_data[ (nvx,nvy) ] = nv visited[ (nvx,nvy) ] = True #print nvx, nvy, nv not_visited = ~visited not_visited = not_visited.iselection() #print not_visited.size() return new_data oo=m/2.0 for ii in range(m): for jj in range(m): dd = smath.sqrt( (ii-oo)**2.0 + (jj-oo)**2.0 ) new_data[ (ii,jj) ] = new_data[ (ii,jj) ]/(1+smath.sqrt(dd)) return new_data
def exercise(space_group_info, redundancy_counter=0): n_real = (12,12,12) miller_max = (2,2,2) gt = maptbx.grid_tags(n_real) uc = space_group_info.any_compatible_unit_cell(volume=1000) fl = sgtbx.search_symmetry_flags(use_space_group_symmetry=True) gt.build(space_group_info.type(), fl) fft = fftpack.real_to_complex_3d(n_real) map0 = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()), 0) weight_map = map0.deep_copy() map = map0.deep_copy() ta = gt.tag_array() order_z = space_group_info.group().order_z() problems_expected = (redundancy_counter != 0) for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): xyz = [i/n for i,n in zip(ijk, n_real)] ss = sgtbx.site_symmetry( unit_cell=uc, space_group=space_group_info.group(), original_site=xyz, min_distance_sym_equiv=1e-5) m = space_group_info.group().multiplicity( site=boost.rational.vector(ijk, n_real)) assert m == ss.multiplicity() w = m / order_z weight_map[ijk] = w map[ijk] = w elif (redundancy_counter != 0): redundancy_counter -= 1 ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t map[ijk] = map[ijk_asu] sf_map = fft.forward(map) del map mi = miller.index_generator( space_group_info.type(), False, miller_max).to_array() assert mi.size() != 0 from_map = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map, conjugate_flag=True) sf = [iround(abs(f)) for f in from_map.data()] if (sf != [0]*len(sf)): assert problems_expected return else: not problems_expected # map_p1 = map0.deep_copy() map_sw = map0.deep_copy() for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): v = random.random()*2-1 map_p1[ijk] = v map_sw[ijk] = v * weight_map[ijk] else: ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t assert map_p1[ijk_asu] != 0 map_p1[ijk] = map_p1[ijk_asu] # # fft followed by symmetry summation in reciprocal space sf_map_sw = fft.forward(map_sw) del map_sw sf_sw = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_sw, conjugate_flag=True).data() del sf_map_sw # # symmetry expansion in real space (done above already) followed fft sf_map_p1 = fft.forward(map_p1) del map_p1 sf_p1 = maptbx.structure_factors.from_map( space_group=sgtbx.space_group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_p1, conjugate_flag=True).data() del sf_map_p1 # corr = flex.linear_correlation(x=flex.abs(sf_sw), y=flex.abs(sf_p1)) assert corr.is_well_defined assert approx_equal(corr.coefficient(), 1)
def exercise(space_group_info, redundancy_counter=0): n_real = (12, 12, 12) miller_max = (2, 2, 2) gt = maptbx.grid_tags(n_real) uc = space_group_info.any_compatible_unit_cell(volume=1000) fl = sgtbx.search_symmetry_flags(use_space_group_symmetry=True) gt.build(space_group_info.type(), fl) fft = fftpack.real_to_complex_3d(n_real) map0 = flex.double(flex.grid(fft.m_real()).set_focus(fft.n_real()), 0) weight_map = map0.deep_copy() map = map0.deep_copy() ta = gt.tag_array() order_z = space_group_info.group().order_z() problems_expected = (redundancy_counter != 0) for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): xyz = [i / n for i, n in zip(ijk, n_real)] ss = sgtbx.site_symmetry(unit_cell=uc, space_group=space_group_info.group(), original_site=xyz, min_distance_sym_equiv=1e-5) m = space_group_info.group().multiplicity( site=boost.rational.vector(ijk, n_real)) assert m == ss.multiplicity() w = m / order_z weight_map[ijk] = w map[ijk] = w elif (redundancy_counter != 0): redundancy_counter -= 1 ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t map[ijk] = map[ijk_asu] sf_map = fft.forward(map) del map mi = miller.index_generator(space_group_info.type(), False, miller_max).to_array() assert mi.size() != 0 from_map = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map, conjugate_flag=True) sf = [iround(abs(f)) for f in from_map.data()] if (sf != [0] * len(sf)): assert problems_expected return else: not problems_expected # map_p1 = map0.deep_copy() map_sw = map0.deep_copy() for ijk in flex.nested_loop(n_real): t = ta[ijk] if (t < 0): v = random.random() * 2 - 1 map_p1[ijk] = v map_sw[ijk] = v * weight_map[ijk] else: ijk_asu = n_dim_index_from_one_dim(i1d=t, sizes=n_real) assert ta.accessor()(ijk_asu) == t assert map_p1[ijk_asu] != 0 map_p1[ijk] = map_p1[ijk_asu] # # fft followed by symmetry summation in reciprocal space sf_map_sw = fft.forward(map_sw) del map_sw sf_sw = maptbx.structure_factors.from_map( space_group=space_group_info.group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_sw, conjugate_flag=True).data() del sf_map_sw # # symmetry expansion in real space (done above already) followed fft sf_map_p1 = fft.forward(map_p1) del map_p1 sf_p1 = maptbx.structure_factors.from_map(space_group=sgtbx.space_group(), anomalous_flag=False, miller_indices=mi, complex_map=sf_map_p1, conjugate_flag=True).data() del sf_map_p1 # corr = flex.linear_correlation(x=flex.abs(sf_sw), y=flex.abs(sf_p1)) assert corr.is_well_defined assert approx_equal(corr.coefficient(), 1)
def exercise_grid_indices_around_sites(): unit_cell = uctbx.unit_cell((5,5,5)) fft_n_real = (5,5,5) fft_m_real = (5,5,5) site_radii = flex.double([0.5*3**0.5+1e-6]) def get(): grid_indices = maptbx.grid_indices_around_sites( unit_cell=unit_cell, fft_n_real=fft_n_real, fft_m_real=fft_m_real, sites_cart=sites_cart, site_radii=site_radii) return list(grid_indices) sites_cart = flex.vec3_double([(0.5,0.5,0.5)]) assert get() == [0, 1, 5, 6, 25, 26, 30, 31] sites_cart = flex.vec3_double([(1.5,1.5,1.5)]) assert get() == [31, 32, 36, 37, 56, 57, 61, 62] def sample(): for i in xrange(-2,7): for j in xrange(-2,7): for k in xrange(-2,7): sites_cart = flex.vec3_double([(i+.5,j+.5,k+.5)]) assert len(get()) == 8 sample() # unit_cell = uctbx.unit_cell((5,6,7)) fft_n_real = (5,6,7) fft_m_real = (5,6,7) sites_cart = flex.vec3_double([(0.5,0.5,0.5)]) assert get() == [0, 1, 7, 8, 42, 43, 49, 50] fft_m_real = (5,6,8) assert get() == [0, 1, 8, 9, 48, 49, 56, 57] fft_m_real = (5,7,8) assert get() == [0, 1, 8, 9, 56, 57, 64, 65] sample() # site_radii = flex.double([2]) assert len(get()) == 8 + 6*4 site_radii = flex.double([1000]) assert len(get()) == 5*6*7 # unit_cell = uctbx.unit_cell((18,26,27)) fft_n_real = (18,26,27) fft_m_real = (18,27,28) for ish in xrange(5): x = 2*ish+.5 sites_cart = flex.vec3_double([[x]*3]) sh = 3**0.5*(ish+0.5) site_radii = flex.double([sh-1e-6]) s1 = set(get()) site_radii = flex.double([sh+1e-6]) s2 = set(get()) for gi in sorted(s2-s1): i,j,k = n_dim_index_from_one_dim(gi, fft_m_real) assert approx_equal(abs(matrix.col((i-x,j-x,k-x))), sh) assert len(s1) == [0, 56, 304, 912, 1904][ish] assert len(s2) == [8, 88, 360, 968, 2008][ish] # unit_cell = uctbx.unit_cell((8,9,7,80,100,110)) fft_n_real = (11,13,15) fft_m_real = (18,26,19) sites_cart = flex.vec3_double([(3,11,5)]) ls = [] prev = 0 for r in itertools.count(1): site_radii = flex.double([r]) l = len(get()) assert l > prev ls.append(l) if (l == 11*13*15): break assert r < 7 prev = l assert ls == [18, 155, 524, 1225, 1940, 2139, 2145] # fft_m_real = (1073741824, 1073741824, 1073741824) try: maptbx.grid_indices_around_sites( unit_cell=unit_cell, fft_n_real=fft_n_real, fft_m_real=fft_m_real, sites_cart=sites_cart, site_radii=site_radii) except RuntimeError, e: assert str(e).startswith("product of fft_m_real")