em::FittingSolutions pca_based_rigid_fitting( ParticlesTemp ps, em::DensityMap *em_map,Float threshold, FloatKey , algebra::PrincipalComponentAnalysis dens_pca_input) { //find the pca of the density algebra::PrincipalComponentAnalysis dens_pca; if (dens_pca_input != algebra::PrincipalComponentAnalysis()){ dens_pca=dens_pca_input; } else{ algebra::Vector3Ds dens_vecs = em::density2vectors(em_map,threshold); dens_pca = algebra::get_principal_components(dens_vecs); } //move the rigid body to the center of the map core::XYZs ps_xyz = core::XYZs(ps); algebra::Transformation3D move2center_trans = algebra::Transformation3D( algebra::get_identity_rotation_3d(), dens_pca.get_centroid()-core::get_centroid(core::XYZs(ps_xyz))); for(unsigned int i=0;i<ps_xyz.size();i++){ ps_xyz[i].set_coordinates( move2center_trans.get_transformed(ps_xyz[i].get_coordinates())); } //find the pca of the protein algebra::Vector3Ds ps_vecs; for (core::XYZs::iterator it = ps_xyz.begin(); it != ps_xyz.end(); it++) { ps_vecs.push_back(it->get_coordinates()); } algebra::PrincipalComponentAnalysis ps_pca = algebra::get_principal_components(ps_vecs); IMP_IF_LOG(VERBOSE) { IMP_LOG(VERBOSE,"in pca_based_rigid_fitting, density PCA:"<<std::endl); IMP_LOG_WRITE(VERBOSE,dens_pca.show()); IMP_LOG(VERBOSE,"particles PCA:"<<std::endl); IMP_LOG_WRITE(VERBOSE,ps_pca.show()); } algebra::Transformation3Ds all_trans = algebra::get_alignments_from_first_to_second(ps_pca,dens_pca); em::FittingSolutions fs = em::compute_fitting_scores(ps,em_map, all_trans,false); fs.sort(); //compose the center translation to the results em::FittingSolutions returned_fits; for (int i=0;i<fs.get_number_of_solutions();i++){ returned_fits.add_solution( algebra::compose(fs.get_transformation(i),move2center_trans), fs.get_score(i)); } //move protein to the center of the map algebra::Transformation3D move2center_inv = move2center_trans.get_inverse(); for(unsigned int i=0;i< ps_xyz.size();i++){ ps_xyz[i].set_coordinates( move2center_inv.get_transformed(ps_xyz[i].get_coordinates())); } return returned_fits; }
void KMCentersNodeSplit::get_neighbors(const Ints &cands, KMPointArray *sums, KMPoint *sum_sqs, Ints *weights) { if (cands.size() == 1) { IMP_LOG_VERBOSE("KMCentersNodeSplit::get_neighbors the data points are" << " associated to center : " << cands[0] << std::endl); // post points as neighbors post_neighbor(sums, sum_sqs, weights, cands[0]); } // get cloest candidate to the box represented by the node else { Ints new_cands; IMP_LOG_VERBOSE( "KMCentersNodeSplit::get_neighbors compute close centers for node:\n"); IMP_LOG_WRITE(VERBOSE, show(IMP_STREAM)); compute_close_centers(cands, &new_cands); for (unsigned int i = 0; i < new_cands.size(); i++) { IMP_LOG_VERBOSE(new_cands[i] << " | "); } IMP_LOG_VERBOSE("\nKMCentersNodeSplit::get_neighbors call left child with " << new_cands.size() << " candidates\n"); children_[0]->get_neighbors(new_cands, sums, sum_sqs, weights); IMP_LOG_VERBOSE("KMCentersNodeSplit::get_neighbors call right child with " << new_cands.size() << " candidates\n"); children_[1]->get_neighbors(new_cands, sums, sum_sqs, weights); } }
InteractionGraph get_triangulated(const InteractionGraph &ig) { InteractionGraph cig; boost::copy_graph(ig, cig); /*std::cout << "Input graph is " << std::endl; IMP::internal::show_as_graphviz(ig, std::cout);*/ triangulate(cig); IMP_LOG_VERBOSE("Triangulated graph is " << std::endl); IMP_LOG_WRITE(VERBOSE, show_as_graphviz(cig, IMP_STREAM)); return cig; }
void RestraintCache::add_restraints(const kernel::RestraintsAdaptor &rs) { IMP_OBJECT_LOG; if (rs.empty()) return; kernel::Model *m = rs[0]->get_model(); DependencyGraph dg = get_dependency_graph(m); ParticleStatesTable *pst = cache_.get_generator().get_particle_states_table(); DepMap dependencies; kernel::ParticlesTemp allps = pst->get_particles(); DependencyGraphVertexIndex index = IMP::get_vertex_index(dg); for (unsigned int i = 0; i < allps.size(); ++i) { kernel::ParticlesTemp depp = get_dependent_particles(allps[i], allps, dg, index); for (unsigned int j = 0; j < depp.size(); ++j) { dependencies[depp[j]].push_back(allps[i]); } dependencies[allps[i]].push_back(allps[i]); IMP_LOG_TERSE("Particle " << Showable(allps[i]) << " controls " << dependencies[allps[i]] << std::endl); } for (unsigned int i = 0; i < rs.size(); ++i) { base::Pointer<kernel::Restraint> r = rs[i]->create_decomposition(); IMP_IF_LOG(TERSE) { IMP_LOG_TERSE("Before:" << std::endl); IMP_LOG_WRITE(TERSE, show_restraint_hierarchy(rs[i])); } if (r) { IMP_LOG_TERSE("after:" << std::endl); IMP_LOG_WRITE(TERSE, show_restraint_hierarchy(r)); add_restraint_internal(r, next_index_, nullptr, std::numeric_limits<double>::max(), Subset(), dependencies); } ++next_index_; } IMP_IF_LOG(TERSE) { IMP_LOG_WRITE(TERSE, show_restraint_information(IMP_STREAM)); } }
void KMData::sample_centers(KMPointArray *sample, int k, double offset, bool allow_duplicate) { clear_points(sample); IMP_LOG_VERBOSE("KMData::sample_centers size: " << sample->size() << std::endl); if (!allow_duplicate) { IMP_INTERNAL_CHECK(((unsigned int)k) <= points_->size(), "not enough points to sample from"); } Ints sampled_ind; for (int i = 0; i < k; i++) { int ri = internal::random_int(points_->size()); if (!allow_duplicate) { bool dup_found; do { dup_found = false; // search for duplicates for (int j = 0; j < i; j++) { if (sampled_ind[j] == ri) { dup_found = true; ri = internal::random_int(points_->size()); break; } } } while (dup_found); } sampled_ind.push_back(ri); KMPoint *p = new KMPoint(); KMPoint *copied_p = (*points_)[ri]; for (int j = 0; j < dim_; j++) { p->push_back((*copied_p)[j] + internal::random_uniform(-1., 1) * offset); } sample->push_back(p); } IMP_LOG_VERBOSE("KMData::sampled centers : " << std::endl); for (int i = 0; i < k; i++) { IMP_LOG_WRITE(VERBOSE, print_point(*((*sample)[i]))); } IMP_LOG_VERBOSE("\nKMData::sample_centers end size : " << sample->size() << std::endl); }
algebra::Vector3Ds CoarseCC::calc_derivatives(const DensityMap *em_map, const DensityMap *model_map, const Particles &model_ps, const FloatKey &w_key, KernelParameters *kernel_params, const float &scalefac, const algebra::Vector3Ds &dv) { algebra::Vector3Ds dv_out; dv_out.insert(dv_out.end(), dv.size(), algebra::Vector3D(0., 0., 0.)); double tdvx = 0., tdvy = 0., tdvz = 0., tmp, rsq; int iminx, iminy, iminz, imaxx, imaxy, imaxz; const DensityHeader *model_header = model_map->get_header(); const DensityHeader *em_header = em_map->get_header(); const float *x_loc = em_map->get_x_loc(); const float *y_loc = em_map->get_y_loc(); const float *z_loc = em_map->get_z_loc(); IMP_INTERNAL_CHECK(model_ps.size() == dv.size(), "input derivatives array size does not match " << "the number of particles in the model map\n"); core::XYZRs model_xyzr = core::XYZRs(model_ps); // this would go away once we have XYZRW decorator const emreal *em_data = em_map->get_data(); float lim = kernel_params->get_lim(); long ivox; // validate that the model and em maps are not empty IMP_USAGE_CHECK(em_header->rms >= EPS, "EM map is empty ! em_header->rms = " << em_header->rms); // it may be that CG takes a too large step, which causes the particles // to go outside of the density // if (model_header->rms <= EPS){ // IMP_WARN("Model map is empty ! model_header->rms = " << model_header->rms // <<" derivatives are not calculated. the model centroid is : " << // core::get_centroid(core::XYZs(model_ps))<< // " the map centroid is " << em_map->get_centroid()<< // "number of particles in model:"<<model_ps.size() //<<std::endl); // return; // } // Compute the derivatives int nx = em_header->get_nx(); int ny = em_header->get_ny(); // int nz=em_header->get_nz(); IMP_INTERNAL_CHECK(em_map->get_rms_calculated(), "RMS should be calculated for calculating derivatives \n"); long nvox = em_header->get_number_of_voxels(); double lower_comp = 1. * nvox * em_header->rms * model_header->rms; for (unsigned int ii = 0; ii < model_ps.size(); ii++) { float x, y, z; x = model_xyzr[ii].get_x(); y = model_xyzr[ii].get_y(); z = model_xyzr[ii].get_z(); IMP_IF_LOG(VERBOSE) { algebra::Vector3D vv(x, y, z); IMP_LOG_VERBOSE( "start value:: (" << x << "," << y << "," << z << " ) " << em_map->get_value(x, y, z) << " : " << em_map->get_dim_index_by_location(vv, 0) << "," << em_map->get_dim_index_by_location(vv, 1) << "," << em_map->get_dim_index_by_location(vv, 2) << std::endl); } const RadiusDependentKernelParameters ¶ms = kernel_params->get_params(model_xyzr[ii].get_radius()); calc_local_bounding_box( // em_map, model_map, x, y, z, params.get_kdist(), iminx, iminy, iminz, imaxx, imaxy, imaxz); IMP_LOG_WRITE(VERBOSE, params.show()); IMP_LOG_VERBOSE("local bb: [" << iminx << "," << iminy << "," << iminz << "] [" << imaxx << "," << imaxy << "," << imaxz << "] \n"); tdvx = .0; tdvy = .0; tdvz = .0; for (int ivoxz = iminz; ivoxz <= imaxz; ivoxz++) { for (int ivoxy = iminy; ivoxy <= imaxy; ivoxy++) { ivox = ivoxz * nx * ny + ivoxy * nx + iminx; for (int ivoxx = iminx; ivoxx <= imaxx; ivoxx++) { /* if (em_data[ivox]<EPS) { ivox++; continue; }*/ float dx = x_loc[ivox] - x; float dy = y_loc[ivox] - y; float dz = z_loc[ivox] - z; rsq = dx * dx + dy * dy + dz * dz; rsq = EXP(-rsq * params.get_inv_sigsq()); tmp = (x - x_loc[ivox]) * rsq; if (std::abs(tmp) > lim) { tdvx += tmp * em_data[ivox]; } tmp = (y - y_loc[ivox]) * rsq; if (std::abs(tmp) > lim) { tdvy += tmp * em_data[ivox]; } tmp = (z - z_loc[ivox]) * rsq; if (std::abs(tmp) > lim) { tdvz += tmp * em_data[ivox]; } ivox++; } } } tmp = model_ps[ii]->get_value(w_key) * 2. * params.get_inv_sigsq() * scalefac * params.get_normfac() / lower_comp; IMP_LOG_VERBOSE("for particle:" << ii << " (" << tdvx << "," << tdvy << "," << tdvz << ")" << std::endl); dv_out[ii][0] = tdvx * tmp; dv_out[ii][1] = tdvy * tmp; dv_out[ii][2] = tdvz * tmp; } // particles return dv_out; }
void KMLProxy::run(Particles *initial_centers) { IMP_INTERNAL_CHECK(is_init_,"The proxy was not initialized"); IMP_LOG(VERBOSE,"KMLProxy::run start \n"); //use the initial centers if provided KMPointArray *kmc=nullptr; if (initial_centers != nullptr) { IMP_INTERNAL_CHECK(kcenters_ == initial_centers->size(), "the number of initial points differs from the number of required" <<" centers\n"); IMP_LOG(VERBOSE,"KMLProxy::run initial centers provided : \n"); kmc = allocate_points(kcenters_,atts_.size()); for (unsigned int i=0;i<kcenters_;i++){ Particle *cen=(*initial_centers)[i]; for(unsigned int j=0;j<atts_.size();j++) { (*(*kmc)[i])[j]=cen->get_value(atts_[j]); } } } IMP_LOG(VERBOSE,"KMLProxy::run load initial guess \n"); //load the initail guess KMFilterCenters ctrs(kcenters_, data_, kmc,damp_factor_); //apply lloyd search IMP_LOG(VERBOSE,"KMLProxy::run load lloyd \n"); lloyd_alg_ = new KMLocalSearchLloyd(&ctrs,&term_); log_header(); IMP_CHECK_CODE(clock_t start = clock()); IMP_LOG(VERBOSE,"KMLProxy::run excute lloyd \n"); lloyd_alg_->execute(); IMP_LOG(VERBOSE,"KMLProxy::run analyse \n"); KMFilterCentersResults best_clusters = lloyd_alg_->get_best(); IMP_CHECK_CODE(Float exec_time = elapsed_time(start)); // print summary IMP_LOG_WRITE(TERSE,log_summary(&best_clusters,exec_time)); IMP_LOG_WRITE(TERSE,best_clusters.show(IMP_STREAM)); IMP_INTERNAL_CHECK(kcenters_ == (unsigned int) best_clusters.get_number_of_centers(), "The final number of centers does not match the requested one"); IMP_INTERNAL_CHECK (dim_ == (unsigned int) best_clusters.get_dim(), "The dimension of the final clusters is wrong"); //TODO clear the centroids list //set the centroids: Particle *p; IMP_LOG(VERBOSE,"KMLProxy::run load best results \n"); for (unsigned int ctr_ind = 0; ctr_ind < kcenters_; ctr_ind++) { KMPoint *kmp = best_clusters[ctr_ind]; //create a new particle p = new Particle(m_); centroids_.push_back(p); for (unsigned int att_ind = 0; att_ind < dim_; att_ind++) { p->add_attribute(atts_[att_ind],(*kmp)[att_ind],false); } } //set the assignment of particles to centers //array of number of all points //TODO - return this IMP_LOG(VERBOSE,"KMLProxy::run get assignments \n"); const Ints *close_center = best_clusters.get_assignments(); IMP_LOG(VERBOSE,"KMLProxy::run get assignments 2\n"); for (int i=0;i<data_->get_number_of_points();i++) { //std::cout<<"ps number i: " << i << " close center : " //<< (*close_center)[i] << std::endl; assignment_[ps_[i]]=(*close_center)[i]; } }