void Fragment::set_residue_indexes(kernel::Model *m, kernel::ParticleIndex pi, Ints o) { if (o.empty()) { set_residue_indexes(m, pi, IntPairs()); return; } std::sort(o.begin(), o.end()); o.erase(std::unique(o.begin(), o.end()), o.end()); IntPairs pairs; int begin = 0; for (unsigned int i = 1; i < o.size(); ++i) { if (o[i] != o[i - 1] + 1) { pairs.push_back(IntPair(o[begin], o[i - 1] + 1)); begin = i; } } pairs.push_back(IntPair(o[begin], o.back() + 1)); set_residue_indexes(m, pi, pairs); using IMP::operator<<; IMP_IF_CHECK(USAGE) { for (unsigned int i = 0; i < o.size(); ++i) { IMP_INTERNAL_CHECK(Fragment(m, pi).get_contains_residue(o[i]), "Residue index not found after addition: " << o << " became " << pairs); } } }
bool PymolWriter::handle_polygon(PolygonGeometry *g, Color color, std::string name) { setup(name, TRIANGLES); if (!open_type_) { get_stream() << "BEGIN, TRIANGLES, "; open_type_ = TRIANGLES; } Ints tris = internal::get_triangles(g); algebra::Vector3Ds normals = internal::get_normals(tris, g->get_geometry()); for (unsigned int i = 0; i < tris.size() / 3; ++i) { write_triangle(tris.begin() + 3 * i, tris.begin() + 3 * i + 3, g->get_geometry(), normals, color, get_stream()); } return true; }
void KNNData::fill_nearest_neighbors_v(const algebra::VectorKD &g, unsigned int k, double eps, Ints &ret) const { VectorWithIndex d(std::numeric_limits<int>::max(), g); RealRCTree:: K_neighbor_search search(dynamic_cast<RealRCTree*>(tree_.get()) ->tree, d, k, eps); IMP_IF_CHECK(base::USAGE_AND_INTERNAL) { int nump=std::distance(dynamic_cast<RealRCTree*>(tree_.get()) ->tree.begin(), dynamic_cast<RealRCTree*>(tree_.get()) ->tree.end()); int realk=std::min<int>(nump, k); IMP_CHECK_VARIABLE(realk); IMP_INTERNAL_CHECK(std::distance(search.begin(), search.end()) == static_cast<int>(realk), "Got the wrong number of points out from CGAL neighbor" << " search. Expected " << realk << " got " << std::distance(search.begin(), search.end())); } Ints::iterator rit = ret.begin(); for ( RealRCTree::K_neighbor_search::iterator it = search.begin(); it != search.end(); ++it) { *rit= it->first; ++rit; } }
void KMCentersNode::compute_close_centers(const Ints &candidate_centers_inds, Ints *close_centers_inds) { const KMPoint *l, *h; l = bnd_box_.get_point(0); h = bnd_box_.get_point(1); // first we calculate the center that is closest to the middle of the // bounding box int mid_center_ind = mid_center(candidate_centers_inds); KMPoint *mid_cen = (*centers_)[mid_center_ind]; double box_dot = 0.; // holds (p-c').(c-c') double cc_dot = 0.; // holds (c-c').(c-c') for (Ints::const_iterator it = candidate_centers_inds.begin(); it != candidate_centers_inds.end(); it++) { if (*it == mid_center_ind) { close_centers_inds->push_back(*it); } else { KMPoint *cand_cen = (*centers_)[*it]; KMPoint closest_vertex = bnd_box_.find_closest_vertex(*cand_cen); box_dot = 0.; cc_dot = 0.; for (int d = 0; d < bnd_box_.get_dim(); d++) { double cc_comp = (*cand_cen)[d] - (*mid_cen)[d]; cc_dot += cc_comp * cc_comp; // increment dot product if (cc_comp > 0) { box_dot += cc_comp * ((*h)[d] - (*mid_cen)[d]); } else { box_dot += cc_comp * ((*l)[d] - (*mid_cen)[d]); } } if (cc_dot < 2 * box_dot) { close_centers_inds->push_back(*it); } } } }
bool PymolWriter::handle_surface(SurfaceMeshGeometry *g, Color color, std::string name) { setup(name, TRIANGLES); if (!open_type_) { get_stream() << "BEGIN, TRIANGLES, "; open_type_ = TRIANGLES; } Ints triangles = internal::get_triangles(g); algebra::Vector3Ds normals = internal::get_normals(triangles, g->get_vertexes()); IMP_INTERNAL_CHECK(triangles.size() % 3 == 0, "The returned triangles aren't triangles"); for (unsigned int i = 0; i < triangles.size() / 3; ++i) { write_triangle(triangles.begin() + 3 * i, triangles.begin() + 3 * i + 3, g->get_vertexes(), normals, color, get_stream()); } return true; }
algebra::Vector3Ds get_normals(const Ints &faces, const algebra::Vector3Ds &vertices) { IMP_INTERNAL_CHECK(faces.size() % 3 == 0, "Not triangles"); IMP_INTERNAL_CHECK(faces.size() > 0, "No faces"); Ints count(vertices.size()); algebra::Vector3Ds sum(vertices.size(), algebra::get_zero_vector_d<3>()); for (unsigned int i = 0; i < faces.size() / 3; ++i) { algebra::Vector3D n = get_normal(faces.begin() + 3 * i, faces.begin() + 3 * i + 3, vertices); IMP_INTERNAL_CHECK(!IMP::isnan(n[0]), "Nan found"); for (unsigned int j = 0; j < 3; ++j) { int v = faces[3 * i + j]; sum[v] += n; count[v] += 1; } } for (unsigned int i = 0; i < count.size(); ++i) { sum[i] /= count[i]; IMP_INTERNAL_CHECK(!IMP::isnan(sum[i][0]), "Nan found at end:" << count[i] << " on " << i); } return sum; }
Ints get_triangles(SurfaceMeshGeometry *sg) { Ints ret; Ints::const_iterator it = sg->get_faces().begin(); while (it != sg->get_faces().end()) { Ints::const_iterator eit = std::find( it, static_cast<Ints::const_iterator>(sg->get_faces().end()), -1); IMP_INTERNAL_CHECK(eit != sg->get_faces().end(), "No trailing -1"); Ints cur(it, eit); Ints tris = get_triangulation_of_face(cur, sg->get_vertexes()); ret.insert(ret.end(), tris.begin(), tris.end()); it = eit; ++it; } return ret; }
void RestraintCache::save_cache(const kernel::ParticlesTemp &particle_ordering, const kernel::RestraintsTemp &restraints, RMF::HDF5::Group group, unsigned int max_entries) { RMF::HDF5::FloatDataSet1Ds scores; RMF::HDF5::IntDataSet2Ds assignments; base::map<kernel::Restraint *, int> restraint_index; ParticleIndex particle_index = get_particle_index(particle_ordering); Orders orders = get_orders(known_restraints_, restraints, particle_ordering); // create data sets for restraints for (unsigned int i = 0; i < restraints.size(); ++i) { kernel::Restraint *r = restraints[i]; RestraintID rid = get_restraint_id(particle_index, known_restraints_.find(r)->second, restraint_index_.find(r)->second); RMF::HDF5::Group g = group.add_child_group(r->get_name()); g.set_attribute<RMF::HDF5::IndexTraits>( "restraint", RMF::HDF5::Indexes(1, rid.get_restraint_index())); g.set_attribute<RMF::HDF5::IndexTraits>( "particles", RMF::HDF5::Indexes(rid.get_particle_indexes().begin(), rid.get_particle_indexes().end())); scores.push_back(g.add_child_data_set<RMF::HDF5::FloatTraits, 1>("scores")); assignments.push_back( g.add_child_data_set<RMF::HDF5::IntTraits, 2>("assignments")); restraint_index[r] = i; } // finally start saving them unsigned int count = 0; for (Cache::ContentIterator it = cache_.contents_begin(); it != cache_.contents_end(); ++it) { int ri = restraint_index.find(it->key.get_restraint())->second; Ints ord = orders[ri].get_list_ordered(it->key.get_assignment()); double score = it->value; RMF::HDF5::DataSetIndexD<2> asz = assignments[ri].get_size(); RMF::HDF5::DataSetIndexD<1> row(asz[0]); asz[1] = ord.size(); ++asz[0]; assignments[ri].set_size(asz); assignments[ri].set_row(row, RMF::HDF5::Ints(ord.begin(), ord.end())); RMF::HDF5::DataSetIndexD<1> ssz = scores[ri].get_size(); RMF::HDF5::DataSetIndexD<1> nsz = ssz; ++nsz[0]; scores[ri].set_size(nsz); scores[ri].set_value(ssz, score); ++count; if (count > max_entries) break; } }
PartitionalClustering * RecursivePartitionalClusteringMetric::create_full_clustering( PartitionalClustering *center_cluster) { IMP::base::Vector<Ints> clusters(center_cluster->get_number_of_clusters()); Ints reps(clusters.size()); for (unsigned int i = 0; i < clusters.size(); ++i) { Ints outer = center_cluster->get_cluster(i); reps[i] = clustering_->get_cluster_representative( center_cluster->get_cluster_representative(i)); for (unsigned int j = 0; j < outer.size(); ++j) { Ints inner = clustering_->get_cluster(outer[j]); clusters[i].insert(clusters[i].end(), inner.begin(), inner.end()); } } IMP_NEW(internal::TrivialPartitionalClustering, ret, (clusters, reps)); validate_partitional_clustering(ret, metric_->get_number_of_items()); return ret.release(); }
double get_slack_estimate(const ParticlesTemp& ps, double upper_bound, double step, const RestraintsTemp &restraints, bool derivatives, Optimizer *opt, ClosePairContainer *cpc) { std::vector<Data> datas; for (double slack=0; slack< upper_bound; slack+= step) { IMP_LOG(VERBOSE, "Computing for " << slack << std::endl); datas.push_back(Data()); datas.back().slack=slack; { boost::timer imp_timer; int count=0; base::SetLogState sl(opt->get_model(), SILENT); do { cpc->set_slack(slack); cpc->update(); ++count; } while (imp_timer.elapsed()==0); datas.back().ccost= imp_timer.elapsed()/count; IMP_LOG(VERBOSE, "Close pair finding cost " << datas.back().ccost << std::endl); } { boost::timer imp_timer; double score=0; int count=0; int iters=1; base::SetLogState sl(opt->get_model(), SILENT); do { for ( int j=0; j< iters; ++j) { for (unsigned int i=0; i< restraints.size(); ++i) { score+=restraints[i]->evaluate(derivatives); // should restore } } count += iters; iters*=2; } while (imp_timer.elapsed()==0); datas.back().rcost= imp_timer.elapsed()/count; IMP_LOG(VERBOSE, "Restraint evaluation cost " << datas.back().rcost << std::endl); } } int ns=100; int last_ns=1; int opt_i=-1; std::vector<Floats > dists(1, Floats(1,0.0)); std::vector< std::vector<algebra::Vector3D> > pos(1, std::vector<algebra::Vector3D>(ps.size())); for (unsigned int j=0; j< ps.size(); ++j) { pos[0][j]=core::XYZ(ps[j]).get_coordinates(); } do { IMP_LOG(VERBOSE, "Stepping from " << last_ns << " to " << ns << std::endl); dists.resize(ns, Floats(ns, 0.0)); for ( int i=0; i< last_ns; ++i) { dists[i].resize(ns, 0.0); } pos.resize(ns, std::vector<algebra::Vector3D>(ps.size())); base::SetLogState sl(opt->get_model(), SILENT); for ( int i=last_ns; i< ns; ++i) { opt->optimize(1); for (unsigned int j=0; j< ps.size(); ++j) { pos[i][j]=core::XYZ(ps[j]).get_coordinates(); } } for ( int i=last_ns; i< ns; ++i) { for ( int j=0; j< i; ++j) { double md=0; for (unsigned int k=0; k< ps.size(); ++k) { md= std::max(md, algebra::get_distance(pos[i][k], pos[j][k])); } dists[i][j]=md; dists[j][i]=md; } } // estimate lifetimes from slack for (unsigned int i=0; i< datas.size(); ++i) { Ints deaths; for ( int j=0; j< ns; ++j) { for ( int k=j+1; k < ns; ++k) { if (dists[j][k]> datas[i].slack) { deaths.push_back(k-j); break; } } } std::sort(deaths.begin(), deaths.end()); // kaplan meier estimator double ml=0; if (deaths.empty()) { ml= ns; } else { //double l=1; IMP_INTERNAL_CHECK(deaths.size() < static_cast<unsigned int>(ns), "Too much death"); double S=1; for (unsigned int j=0; j< deaths.size(); ++j) { double n= ns-j; double t=(n-1.0)/n; ml+=(S-t*S)*deaths[j]; S*=t; } } datas[i].lifetime=ml; IMP_LOG(VERBOSE, "Expected life of " << datas[i].slack << " is " << datas[i].lifetime << std::endl); } /** C(s) is cost to compute R(s) is const to eval restraints L(s) is lifetime of slack minimize C(s)/L(s)+R(s) */ // smooth for (unsigned int i=1; i< datas.size()-1; ++i) { datas[i].rcost=(datas[i].rcost+ datas[i-1].rcost+datas[i+1].rcost)/3.0; datas[i].ccost=(datas[i].ccost+ datas[i-1].ccost+datas[i+1].ccost)/3.0; datas[i].lifetime=(datas[i].lifetime + datas[i-1].lifetime+datas[i+1].lifetime)/3.0; } double min= std::numeric_limits<double>::max(); for (unsigned int i=0; i< datas.size(); ++i) { double v= datas[i].rcost+ datas[i].ccost/datas[i].lifetime; IMP_LOG(VERBOSE, "Cost of " << datas[i].slack << " is " << v << " from " << datas[i].rcost << " " << datas[i].ccost << " " << datas[i].lifetime << std::endl); if (v < min) { min=v; opt_i=i; } } last_ns=ns; ns*=2; IMP_LOG(VERBOSE, "Opt is " << datas[opt_i].slack << std::endl); // 2 for the value, 2 for the doubling // if it more than 1000, just decide that is enough } while (datas[opt_i].lifetime > ns/4.0 && ns <1000); return datas[opt_i].slack; }
static void eraseElements(Ints& vect, int val) { Ints::iterator begin = vect.begin(); Ints::iterator end = vect.end(); vect.erase(std::remove(begin, end, val), end); }