def box_iterator(self): b = maptbx.boxes( n_real = self.atom_map_asu.focus(), fraction = self.box_size_as_fraction, max_boxes= self.max_boxes, log = self.log) def get_wide_box(s,e): # define wide box: neutral + phased volumes if(self.neutral_volume_box_cushion_width>0): sh = self.neutral_volume_box_cushion_width ss = [max(s[i]-sh,0) for i in [0,1,2]] ee = [min(e[i]+sh,n_real_asu[i]) for i in [0,1,2]] else: ss,ee = s,e return ss,ee n_real_asu = b.n_real n_boxes = len(b.starts) i_box = 0 for s,e in zip(b.starts, b.ends): i_box+=1 sw,ew = get_wide_box(s=s,e=e) fmodel_omit = self.omit_box(start=sw, end=ew) r = fmodel_omit.r_work() self.r.append(r) # for tests only if(self.log): print >> self.log, "r(curr,min,max,mean)=%6.4f %6.4f %6.4f %6.4f"%(r, flex.min(self.r), flex.max(self.r), flex.mean(self.r)), i_box, n_boxes omit_map_data = self.asu_map_from_fmodel( fmodel=fmodel_omit, map_type=self.map_type) maptbx.copy_box( map_data_from = omit_map_data, map_data_to = self.map_result_asu, start = s, end = e) self.map_result_asu.reshape(self.acc_asu)
def __init__(self, crystal_gridding, fmodel, map_type, box_size_as_fraction=0.03, max_boxes=100, n_debias_cycles=2, neutral_volume_box_cushion_width=1, full_resolution_map=True, log=sys.stdout): self.crystal_gridding = crystal_gridding # assert compatibility of symops with griding assert self.crystal_gridding._symmetry_flags is not None self.sgt = fmodel.f_obs().space_group().type() self.zero_cmpl_ma = fmodel.f_calc().customized_copy( data=flex.complex_double(fmodel.f_calc().size(), 0)) # embedded utility functions def get_map(fmodel, map_type, crystal_gridding, asu=True): f_map = fmodel.electron_density_map().map_coefficients( map_type=map_type, isotropize=True, exclude_free_r_reflections=True, fill_missing=False) fft_map = cctbx.miller.fft_map(crystal_gridding=crystal_gridding, fourier_coefficients=f_map) if (asu): return asu_map_ext.asymmetric_map( self.sgt, fft_map.real_map_unpadded()).data() else: return fft_map.real_map_unpadded() # f_model map f_model_map_data = fmodel.f_model_scaled_with_k1().fft_map( symmetry_flags=maptbx.use_space_group_symmetry, crystal_gridding=self.crystal_gridding).real_map_unpadded() self.n_real = f_model_map_data.focus() # extract asu map from full P1 f_model_map_data_asu = asu_map_ext.asymmetric_map( self.sgt, f_model_map_data).data() self.acc = f_model_map_data_asu.accessor() f_model_map_data_asu = f_model_map_data_asu.shift_origin() # set up boxes b = maptbx.boxes(n_real=f_model_map_data_asu.focus(), fraction=box_size_as_fraction, max_boxes=max_boxes, log=log) self.map_result_asu = flex.double(flex.grid(b.n_real)) assert f_model_map_data_asu.focus() == b.n_real assert b.n_real == self.map_result_asu.focus() n_real_asu = b.n_real self.r = flex.double() # for regression test only n_boxes = len(b.starts) i_box = 0 for s, e in zip(b.starts, b.ends): i_box += 1 # define wide box: neutral + phased volumes if (neutral_volume_box_cushion_width > 0): sh = neutral_volume_box_cushion_width ss = [max(s[i] - sh, 0) for i in [0, 1, 2]] ee = [min(e[i] + sh, n_real_asu[i]) for i in [0, 1, 2]] else: ss, ee = s, e # omit wide box from f_model map, repeat n_debias_cycles times f_model_map_data_asu_ = f_model_map_data_asu.deep_copy() for i in range(n_debias_cycles): f_model_omit, f_model_map_data_asu_ = self.omit_box( s=ss, e=ee, md_asu=f_model_map_data_asu_) # get fmodel for omit map calculation fmodel_ = mmtbx.f_model.manager(f_obs=fmodel.f_obs(), r_free_flags=fmodel.r_free_flags(), f_calc=f_model_omit, f_mask=self.zero_cmpl_ma) rw = fmodel_.r_work() self.r.append(rw) # for regression test only f_map_data_asu = get_map(fmodel=fmodel_, map_type=map_type, crystal_gridding=self.crystal_gridding) f_map_data_asu = f_map_data_asu.shift_origin() if (log): print("box %2d of %2d:" % (i_box, n_boxes), s, e, "%6.4f" % rw, file=log) assert f_map_data_asu.focus() == self.map_result_asu.focus() maptbx.copy_box(map_data_from=f_map_data_asu, map_data_to=self.map_result_asu, start=s, end=e) # result self.map_result_asu.reshape(self.acc) self.asu_map_omit = asu_map_ext.asymmetric_map(self.sgt, self.map_result_asu, self.n_real) self.map_coefficients = self.zero_cmpl_ma.customized_copy( indices=self.zero_cmpl_ma.indices(), data=self.asu_map_omit.structure_factors( self.zero_cmpl_ma.indices())) # full resolution map (reflections in sphere, not in box!) if (full_resolution_map): cs = self.zero_cmpl_ma.complete_set( d_min=self.zero_cmpl_ma.d_min()) asu_map_omit = asu_map_ext.asymmetric_map(self.sgt, self.map_result_asu, self.n_real) fill = self.zero_cmpl_ma.customized_copy( indices=cs.indices(), data=asu_map_omit.structure_factors(cs.indices())) self.map_coefficients = self.map_coefficients.complete_with( other=fill, scale=True)
def exercise_copy(): for flex_type in flex_types(): m = flex_type((1,2,3,4)) m.resize(flex.grid(2,2)) c = maptbx.copy(map=m, result_grid=m.accessor()) assert tuple(m) == tuple(c) c = maptbx.copy(map=m, result_grid=flex.grid(2,3).set_focus(2,2)) assert approx_equal(tuple(c), (1,2,0,3,4,0)) n = maptbx.copy(c, result_grid=m.accessor()) assert approx_equal(tuple(m), tuple(n)) c = maptbx.copy(m, flex.grid(3,2).set_focus(2,2)) assert approx_equal(tuple(c), (1,2,3,4,0,0)) n = maptbx.copy(c, m.accessor()) assert approx_equal(tuple(m), tuple(n)) m = flex_type((1,2,3,4,5,6)) m.resize(flex.grid((1,2),(3,5))) c = maptbx.copy(m, m.accessor()) assert approx_equal(tuple(m), tuple(c)) c = maptbx.copy(m, flex.grid((1,2),(3,6)).set_focus(3,5)) assert approx_equal(tuple(c), (1,2,3,0,4,5,6,0)) n = maptbx.copy(c, m.accessor()) assert approx_equal(tuple(m), tuple(n)) c = maptbx.copy(m, flex.grid((1,2),(4,5)).set_focus(3,5)) assert approx_equal(tuple(c), (1,2,3,4,5,6,0,0,0)) n = maptbx.copy(c, m.accessor()) assert approx_equal(tuple(m), tuple(n)) # m = flex_type() for i in xrange(2): for j in xrange(3): for k in xrange(5): m.append(i*100+j*10+k) m.resize(flex.grid(2,3,5).set_focus((2,3,4))) for i in xrange(-5,5): for j in xrange(-5,5): for k in xrange(-5,5): c = maptbx.copy(map_unit_cell=m, first=(i,j,k), last=(i,j,k)) assert c.size() == 1 assert c[(i,j,k)] == m[(i%2,j%3,k%4)] c = maptbx.copy(map_unit_cell=m, first=(-1,1,-2), last=(1,2,0)) assert list(c) == [112, 113, 110, 122, 123, 120, 12, 13, 10, 22, 23, 20, 112, 113, 110, 122, 123, 120] # m2 = m.deep_copy() grid = flex.grid( (-1,-1,-1), (1,2,4) ).set_focus( (1,2,3) ) m2.resize(grid) for i in xrange(-1,1): for j in xrange(-1,2): for k in xrange(-1,3): # aperiodic copy c = maptbx.copy_box(map=m2, first=(i,j,k), last=(i,j,k)) assert c.size() == 1 ind = ((i+1)%2-1,(j+1)%3-1,(k+1)%4-1) assert c[(i,j,k)] == m2[ind] c = maptbx.copy_box(map=m2, first=(-1,0,-1), last=(0,1,2)) assert list(c) == [10, 11, 12, 13, 20, 21, 22, 23, 110, 111, 112, 113, 120, 121, 122, 123] # for n0 in xrange(4): for n1 in xrange(4): for n2 in xrange(4): for d2 in xrange(3): g = flex.grid((n0,n1,n2+d2)).set_focus((n0,n1,n2)) map1 = flex_type(range(1,1+g.size_1d())) map1.resize(g) map2 = map1.deep_copy() maptbx.unpad_in_place(map=map2) assert map2.all() == (n0,n1,n2) assert not map2.is_padded() if (n0*n1*n2 != 0): for i in flex.nested_loop((n0,n1,n2)): assert map2[i] == map1[i] n0,n1,n2,d2 = 2,3,4,1 g = flex.grid((n0,n1,n2+d2)).set_focus((n0,n1,n2)) map1 = flex_type(range(1,1+g.size_1d())) map1.resize(g) map2 = map1.deep_copy() maptbx.unpad_in_place(map=map2) assert map2.all() == (n0,n1,n2) assert not map2.is_padded() assert list(map2) == [ 1, 2, 3, 4, 6, 7, 8, 9, 11,12,13,14, 16,17,18,19, 21,22,23,24, 26,27,28,29]
def __init__( self, crystal_gridding, fmodel, map_type, box_size_as_fraction=0.03, max_boxes=100, n_debias_cycles=2, neutral_volume_box_cushion_width=1, full_resolution_map=True, log=sys.stdout): self.crystal_gridding = crystal_gridding # assert compatibility of symops with griding assert self.crystal_gridding._symmetry_flags is not None self.sgt = fmodel.f_obs().space_group().type() self.zero_cmpl_ma = fmodel.f_calc().customized_copy( data = flex.complex_double(fmodel.f_calc().size(), 0)) # embedded utility functions def get_map(fmodel, map_type, crystal_gridding, asu=True): f_map = fmodel.electron_density_map().map_coefficients( map_type = map_type, isotropize = True, exclude_free_r_reflections = True, fill_missing = False) fft_map = cctbx.miller.fft_map( crystal_gridding = crystal_gridding, fourier_coefficients = f_map) if(asu): return asu_map_ext.asymmetric_map(self.sgt, fft_map.real_map_unpadded()).data() else: return fft_map.real_map_unpadded() # f_model map f_model_map_data = fmodel.f_model_scaled_with_k1().fft_map( symmetry_flags = maptbx.use_space_group_symmetry, crystal_gridding = self.crystal_gridding).real_map_unpadded() self.n_real = f_model_map_data.focus() # extract asu map from full P1 f_model_map_data_asu=asu_map_ext.asymmetric_map( self.sgt, f_model_map_data).data() self.acc = f_model_map_data_asu.accessor() f_model_map_data_asu = f_model_map_data_asu.shift_origin() # set up boxes b = maptbx.boxes( n_real = f_model_map_data_asu.focus(), fraction = box_size_as_fraction, max_boxes= max_boxes, log = log) self.map_result_asu = flex.double(flex.grid(b.n_real)) assert f_model_map_data_asu.focus()==b.n_real assert b.n_real==self.map_result_asu.focus() n_real_asu = b.n_real self.r = flex.double() # for regression test only n_boxes = len(b.starts) i_box = 0 for s,e in zip(b.starts, b.ends): i_box+=1 # define wide box: neutral + phased volumes if(neutral_volume_box_cushion_width>0): sh = neutral_volume_box_cushion_width ss = [max(s[i]-sh,0) for i in [0,1,2]] ee = [min(e[i]+sh,n_real_asu[i]) for i in [0,1,2]] else: ss,ee = s,e # omit wide box from f_model map, repeat n_debias_cycles times f_model_map_data_asu_ = f_model_map_data_asu.deep_copy() for i in xrange(n_debias_cycles): f_model_omit, f_model_map_data_asu_ = self.omit_box(s=ss, e=ee, md_asu=f_model_map_data_asu_) # get fmodel for omit map calculation fmodel_ = mmtbx.f_model.manager( f_obs = fmodel.f_obs(), r_free_flags = fmodel.r_free_flags(), f_calc = f_model_omit, f_mask = self.zero_cmpl_ma) rw = fmodel_.r_work() self.r.append(rw) # for regression test only f_map_data_asu = get_map(fmodel=fmodel_, map_type=map_type, crystal_gridding=self.crystal_gridding) f_map_data_asu = f_map_data_asu.shift_origin() if(log): print >> log, "box %2d of %2d:"%(i_box, n_boxes), s, e, "%6.4f"%rw assert f_map_data_asu.focus() == self.map_result_asu.focus() maptbx.copy_box( map_data_from = f_map_data_asu, map_data_to = self.map_result_asu, start = s, end = e) # result self.map_result_asu.reshape(self.acc) self.asu_map_omit = asu_map_ext.asymmetric_map( self.sgt, self.map_result_asu, self.n_real) self.map_coefficients = self.zero_cmpl_ma.customized_copy( indices = self.zero_cmpl_ma.indices(), data = self.asu_map_omit.structure_factors(self.zero_cmpl_ma.indices())) # full resolution map (reflections in sphere, not in box!) if(full_resolution_map): cs = self.zero_cmpl_ma.complete_set(d_min=self.zero_cmpl_ma.d_min()) asu_map_omit = asu_map_ext.asymmetric_map( self.sgt,self.map_result_asu,self.n_real) fill = self.zero_cmpl_ma.customized_copy( indices = cs.indices(), data = asu_map_omit.structure_factors(cs.indices())) self.map_coefficients = self.map_coefficients.complete_with( other=fill, scale=True)