1 """@namespace IMP.isd.gmm_tools
2 Tools for handling Gaussian Mixture Models.
15 import sklearn.mixture
19 from math
import exp,sqrt,copysign
22 ''' read the output from write_gmm_to_text, decorate as Gaussian and Mass'''
28 weight=float(fields[2])
29 center=map(float,fields[3].split())
30 covar=np.array(map(float,fields[4].split())).reshape((3,3))
37 rmax=sqrt(max(g.get_variances()))*radius_scale
39 if not transform
is None:
44 '''write density map from GMM. input can be either particles or gaussians'''
48 ps=[g.get_particle()
for g
in to_draw]
50 print 'ps must be Particles or Gaussians'
52 print 'will write GMM map to',out_fn
53 if bounding_box
is None:
75 '''write density map directly from sklearn GMM (kinda slow) '''
77 if not dmap_model
is None:
83 print 'getting coords'
84 nvox=d1.get_number_of_voxels()
85 apos=[list(d1.get_location_by_voxel(nv))
for nv
in xrange(nvox)]
88 scores=gmm.score(apos)
91 map(
lambda nv: d1.set_value(nv,exp(scores[nv])),xrange(nvox))
92 print 'will write GMM map to',out_fn
95 def draw_points(pts,out_fn,trans=IMP.algebra.get_identity_transformation_3d(),
97 ''' given some points (and optional transform), write them to chimera 'bild' format
98 colors flag only applies to ellipses, otherwise it'll be weird'''
105 outf.write(
'.color 1 0 0\n.dotat %.2f %.2f %.2f\n' %(pt[0],pt[1],pt[2]))
110 outf.write(
'.color 0 1 0\n')
111 colors=[
'0 1 0',
'0 0 1',
'0 1 1']
112 for nt,t
in enumerate(pts[start:]):
113 if use_colors
and nt%2==0:
114 outf.write(
'.color %s\n' % colors[nt/2])
116 outf.write(
'.dotat %.2f %.2f %.2f\n' %(pt[0],pt[1],pt[2]))
121 def create_gmm_for_bead(mdl,
124 sampled_points=100000,
126 print 'fitting bead with',n_components,
'gaussians'
130 pts=IMP.isd.sample_points_from_density(dmap,sampled_points)
137 return density_particles,dmap
139 def sample_and_fit_to_particles(model,
142 sampled_points=1000000,
146 covariance_type=
'full',
147 multiply_by_total_mass=
True,
151 if multiply_by_total_mass:
153 print 'add_component_density: will multiply by mass',mass_multiplier
156 print 'add_component_density: sampling points'
162 pts=IMP.isd.sample_points_from_density(dmap,sampled_points)
165 print 'add_component_density: fitting GMM to',len(pts),
'points'
167 n_components=num_components,
169 ps=density_particles,
171 covariance_type=covariance_type,
172 mass_multiplier=mass_multiplier)
174 if not output_txt
is None:
175 write_gmm_to_text(density_particles,output_txt)
176 if not output_map
is None:
179 voxel_size=voxel_size,
182 return density_particles
189 covariance_type=
'full',
193 mass_multiplier=1.0):
194 '''fit a GMM to some points. Will return core::Gaussians.
195 if no particles are provided, they will be created
197 points: list of coordinates (python)
198 n_components: number of gaussians to create
200 ps: list of particles to be decorated. if empty, will add
201 num_iter: number of EM iterations
202 covariance_type: covar type for the gaussians. options: 'full', 'diagonal', 'sphereical'
203 init_centers: initial coordinates of the GMM
204 force_radii: fix the radii (spheres only)
205 force_weight: fix the weights
206 mass_multiplier: multiply the weights of all the gaussians by this value
207 dirichlet: use the DGMM fitting (can reduce number of components, takes longer)
211 import sklearn.mixture
215 if force_radii==-1.0:
219 covariance_type=
'spherical'
220 print 'forcing spherical with radii',force_radii
222 if force_weight==-1.0:
226 print 'forcing weights to be',force_weight
228 print 'creating GMM with params',params,
'init params',init_params,
'n_components',n_components,
'n_iter',num_iter,
'covar type',covariance_type
229 gmm=sklearn.mixture.GMM(n_components=n_components,
231 covariance_type=covariance_type,
233 init_params=init_params)
235 if force_weight!=-1.0:
236 gmm.weights_=np.array([force_weight]*n_components)
237 if force_radii!=-1.0:
238 gmm.covars_=np.array([[force_radii]*3
for i
in xrange(n_components)])
240 gmm.means_=init_centers
244 print '>>> GMM score',gmm.score(points)
247 for ng
in xrange(n_components):
248 covar=gmm.covars_[ng]
250 covar=np.diag(covar).tolist()
253 center=list(gmm.means_[ng])
254 weight=mass_multiplier*gmm.weights_[ng]
267 covariance_type=
'full',
268 mass_multiplier=1.0):
269 '''fit a GMM to some points. Will return core::Gaussians.
270 if no particles are provided, they will be created
272 points: list of coordinates (python)
273 n_components: number of gaussians to create
275 ps: list of particles to be decorated. if empty, will add
276 num_iter: number of EM iterations
277 covariance_type: covar type for the gaussians. options: 'full', 'diagonal', 'sphereical'
278 init_centers: initial coordinates of the GMM
279 force_radii: fix the radii (spheres only)
280 force_weight: fix the weights
281 mass_multiplier: multiply the weights of all the gaussians by this value
285 import sklearn.mixture
288 print 'using dirichlet prior'
289 gmm=sklearn.mixture.DPGMM(n_components=n_components,
291 covariance_type=covariance_type)
295 print '>>> GMM score',gmm.score(points)
301 for ng
in xrange(n_components):
302 invcovar=gmm.precs_[ng]
303 covar=np.linalg.inv(invcovar)
305 covar=np.diag(covar).tolist()
308 center=list(gmm.means_[ng])
309 weight=mass_multiplier*gmm.weights_[ng]
static FloatKey get_mass_key()
DensityMap * create_density_map(const algebra::GridD< 3, algebra::DenseGridStorageD< 3, float >, float > &grid)
BoundingBoxD< 3 > get_bounding_box(const Geometry &)
Compute the bounding box of any geometric object.
double get_mass(ResidueType c)
Get the mass from the residue type.
DenseGrid3D< float > get_rasterized(const Gaussian3Ds &gmm, const Floats &weights, double cell_width, const BoundingBox3D &bb)
Rasterize the Gaussians to a grid.
void write_map(DensityMap *m, std::string filename)
Write a density map to a file.
Class for sampling a density map from particles.
static XYZR setup_particle(kernel::Model *m, ParticleIndex pi)
The standard decorator for manipulating molecular structures.
void transform(XYZ a, const algebra::Transformation3D &tr)
Apply a transformation to the particle.
Basic utilities for handling cryo-electron microscopy 3D density maps.
A decorator for a particle with x,y,z coordinates.
Class to handle individual model particles.
static Mass setup_particle(kernel::Model *m, ParticleIndex pi, Float mass)
algebra::BoundingBoxD< 3 > get_bounding_box(const DensityMap *m)
Basic functionality that is expected to be used by a wide variety of IMP users.
General purpose algebraic and geometric methods that are expected to be used by a wide variety of IMP...
Gaussian3D get_gaussian_from_covariance(const IMP_Eigen::Matrix3d &covariance, const Vector3D ¢er)
Return a Gaussian centered at the origin from a covariance matrix.
Sphere3D get_enclosing_sphere(const Vector3Ds &ss)
Return a sphere containing the listed spheres.
A decorator for helping deal with a hierarchy.
A decorator for a rigid body.
Functionality for loading, creating, manipulating and scoring atomic structures.
static Gaussian setup_particle(kernel::Model *m, ParticleIndex pi)