/// For algorithm details, see http://wiki.openstreetmap.org/wiki/Relation:multipolygon/Algorithm
void MultipolygonProcessor::process() {
  bool allClosed = true;
  // NOTE do not count multipolygon tag itself
  bool hasNoTags = relation_.tags.size() < 2;
  Ints outerIndecies;
  Ints innerIndecies;
  CoordinateSequences sequences;
  for (const auto &member : members_) {

    if (member.type!="w")
      continue;

    Coordinates coordinates;
    auto wayPair = context_.wayMap.find(member.refId);
    if (wayPair!=context_.wayMap.end()) {
      coordinates = wayPair->second->coordinates;
    } else {
      auto areaPair = context_.areaMap.find(member.refId);
      if (areaPair!=context_.areaMap.end()) {
        coordinates = areaPair->second->coordinates;

        // NOTE make coordinates to be closed ring
        coordinates.push_back(coordinates[0]);

        // NOTE merge tags to relation
        // hasNoTags prevents the case where relation has members with their own tags
        // which should be processed independently (geometry reusage)
        if (member.role=="outer" && hasNoTags)
          mergeTags(areaPair->second->tags);
      } else {
        auto relationPair = context_.relationMap.find(member.refId);
        if (relationPair==context_.relationMap.end())
          return; //  NOTE cannot fill relation: incomplete data

        resolve_(*relationPair->second);
      }
    }

    if (coordinates.empty())
      continue;

    if (member.role=="outer")
      outerIndecies.push_back(static_cast<int>(sequences.size()));
    else if (member.role=="inner")
      innerIndecies.push_back(static_cast<int>(sequences.size()));
    else
      continue;

    auto sequence = std::make_shared<CoordinateSequence>(member.refId, coordinates);
    if (!sequence->isClosed())
      allClosed = false;

    sequences.push_back(sequence);
  }

  if (outerIndecies.size()==1 && allClosed)
    simpleCase(sequences, outerIndecies, innerIndecies);
  else
    complexCase(sequences);
}
Exemplo n.º 2
0
// see http://wiki.openstreetmap.org/wiki/Relation:multipolygon/Algorithm
void MultipolygonProcessor::process()
{
    bool allClosed = true;

    Ints outerIndecies;
    Ints innerIndecies;
    CoordinateSequences sequences;
    for (const auto& member : members_) {

        if (member.type != "way") 
            continue;

        Coordinates coordinates;
        auto wayPair = context_.wayMap.find(member.refId);
        if (wayPair != context_.wayMap.end()) {
            coordinates = wayPair->second->coordinates;
        }
        else {
            auto areaPair = context_.areaMap.find(member.refId);
            if (areaPair != context_.areaMap.end()) {
                coordinates = areaPair->second->coordinates;
                // NOTE merge tags to relation
                if (member.role == "outer")
                    mergeTags(areaPair->second->tags);
            }
            else {
                auto relationPair = context_.relationMap.find(member.refId);
                if (relationPair == context_.relationMap.end())
                    return; //  NOTE cannot fill relation: incomplete data

                resolve_(*relationPair->second);
            }
        }

        if (coordinates.empty()) 
            continue;

        if (member.role == "outer")
            outerIndecies.push_back(static_cast<int>(sequences.size()));
        else if (member.role == "inner")
            innerIndecies.push_back(static_cast<int>(sequences.size()));
        else
            continue;

        auto sequence = std::make_shared<CoordinateSequence>(member.refId, coordinates);
        if (!sequence->isClosed()) 
            allClosed = false;

        sequences.push_back(sequence);
    }

    if (outerIndecies.size() == 1 && allClosed)
        simpleCase(sequences, outerIndecies, innerIndecies);
    else
        complexCase(sequences);
}
Exemplo n.º 3
0
void ClusterSet::do_join_clusters(unsigned int cluster_id1,
                                  unsigned int cluster_id2,
                                  double distance_between_clusters) {
    IMP_LOG_VERBOSE("Joining clusters " << cluster_id1 << " and " << cluster_id2
                    << std::endl);

    joined_ids1_.push_back(cluster_id1);
    joined_ids2_.push_back(cluster_id2);
    cluster_distances_.push_back(distance_between_clusters);

    Ints ids;
    ids.push_back(cluster_id1);
    ids.push_back(cluster_id2);
    Ints new_cluster;
    for (unsigned int i = 0; i < 2; ++i) {
        if ((unsigned int)ids[i] < n_elements_) {
            new_cluster.push_back(ids[i]);
        } else {
            unsigned int s = get_step_from_id(ids[i]);
            new_cluster.insert(new_cluster.end(), clusters_elements_[s].begin(),
                               clusters_elements_[s].end());
        }
    }
    clusters_elements_.push_back(new_cluster);
    steps_++;
}
Exemplo n.º 4
0
IMPEM2D_BEGIN_NAMESPACE

Ints ClusterSet::get_clusters_below_cutoff(double cutoff) const {
    Ints clusters;
    std::vector<bool> is_active(steps_, true);

    for (int i = steps_ - 1; i >= 0; --i) {
        if (is_active[i] == true && cluster_distances_[i] < cutoff) {
            clusters.push_back(get_id_for_cluster_at_step(i));
            // Deactivate all the members of the linkage matrix that contain
            // the elements of this cluster
            Ints to_deactivate;
            unsigned int id1 = joined_ids1_[i];
            unsigned int id2 = joined_ids2_[i];
            if (id1 >= n_elements_) to_deactivate.push_back(id1);
            if (id2 >= n_elements_) to_deactivate.push_back(id2);

            while (to_deactivate.size() > 0) {
                int id = to_deactivate.back();
                to_deactivate.pop_back();
                int j = get_step_from_id(id);  // new row to deactivate
                is_active[j] = false;
                unsigned int jid1 = joined_ids1_[j];
                unsigned int jid2 = joined_ids2_[j];
                if (jid1 >= n_elements_) to_deactivate.push_back(jid1);
                if (jid2 >= n_elements_) to_deactivate.push_back(jid2);
            }
        }
        is_active[i] = false;  // deactivate after considering it
    }
    return clusters;
}
void Test_example_KeepIf_performance()
{
    using namespace fplus;

    typedef std::vector<int> Ints;
    auto run_loop = [&](const Ints values)
    {
        Ints odds;
        for (int x : values)
            if (is_odd_int(x))
                odds.push_back(x);
        return odds;
    };
    auto run_stl = [&](const Ints values)
    {
        Ints odds;
        std::copy_if(std::begin(values), std::end(values),
                std::back_inserter(odds), is_odd_int);
        return odds;
    };
    auto run_FunctionalPlus = [&](const Ints values)
        { return keep_if(is_odd_int, values); };

    // make debug runs faster
#if defined NDEBUG || defined _DEBUG
    std::size_t numRuns = 10;
#else
    std::size_t numRuns = 20000;
#endif

    Ints values = generate<Ints>(rand, 5000);
    run_n_times(run_loop, numRuns, "Hand-written for loop", values);
    run_n_times(run_stl, numRuns, "std::copy_if", values);
    run_n_times(run_FunctionalPlus, numRuns, "FunctionalPlus::keep_if", values);
}
Exemplo n.º 6
0
IMPMULTIFITEXPORT
IntsList get_anchor_indices_matching_secondary_structure(
    const AnchorsData &ad, const atom::SecondaryStructureResidues &match_ssrs,
    Float max_rmsd) {

  atom::SecondaryStructureResidues anchor_ssrs =
      ad.get_secondary_structure_particles();
  IMP_USAGE_CHECK(anchor_ssrs.size() == ad.points_.size(),
                  "Anchors do not have enough SSEs set");

  // double loop unfortunately: for each match_ps check all anchors
  IntsList all_matches;
  for (atom::SecondaryStructureResidues::const_iterator it_match =
           match_ssrs.begin();
       it_match != match_ssrs.end(); ++it_match) {
    Ints matches;
    int count = 0;
    for (atom::SecondaryStructureResidues::const_iterator it_anchor =
             anchor_ssrs.begin();
         it_anchor != anchor_ssrs.end(); ++it_anchor) {
      float match_score =
          atom::get_secondary_structure_match_score(*it_anchor, *it_match);
      if (match_score < max_rmsd) matches.push_back(count);
      count++;
    }
    all_matches.push_back(matches);
  }
  return all_matches;
}
Exemplo n.º 7
0
Hierarchy create_protein(Model *m, std::string name, double resolution,
                         int number_of_residues, int first_residue_index,
                         double volume, bool ismol) {
  double mass =
      atom::get_mass_from_number_of_residues(number_of_residues) / 1000;
  if (volume < 0) {
    volume = atom::get_volume_from_mass(mass * 1000);
  }
  // assume a 20% overlap in the beads to make the protein not too bumpy
  double overlap_frac = .2;
  std::pair<int, double> nr = compute_n(volume, resolution, overlap_frac);
  Hierarchy pd = Hierarchy::setup_particle(new Particle(m));
  Ints residues;
  for (int i = 0; i < number_of_residues; ++i) {
    residues.push_back(i + first_residue_index);
  }
  atom::Fragment::setup_particle(pd, residues);
  if (ismol) Molecule::setup_particle(pd);
  pd->set_name(name);
  for (int i = 0; i < nr.first; ++i) {
    Particle *pc;
    if (nr.first > 1) {
      pc = new Particle(m);
      std::ostringstream oss;
      oss << name << "-" << i;
      pc->set_name(oss.str());
      atom::Fragment pcd = atom::Fragment::setup_particle(pc);
      Ints indexes;
      for (int j = i * (number_of_residues / nr.first) + first_residue_index;
           j < (i + 1) * (number_of_residues / nr.first) + first_residue_index;
           ++j) {
        indexes.push_back(j);
      }
      pcd.set_residue_indexes(indexes);
      pd.add_child(pcd);
    } else {
      pc = pd;
    }

    core::XYZR xyzd = core::XYZR::setup_particle(pc);
    xyzd.set_radius(nr.second);
    atom::Mass::setup_particle(pc, mass / nr.first);
  }
  IMP_INTERNAL_CHECK(pd.get_is_valid(true), "Invalid hierarchy produced "
                                                << pd);
  return pd;
}
Exemplo n.º 8
0
Ints ReplicaExchange::create_exarray()
{
 Ints exarray;
 for(int i=0;i<nproc_-1;++i) {
  exarray.push_back(0);
 }
 return exarray;
}
Exemplo n.º 9
0
Ints Fragment::get_residue_indexes() const {
  IntPairs ranges = get_residue_index_ranges();
  Ints ret;
  for (unsigned int i = 0; i < ranges.size(); ++i) {
    for (int j = ranges[i].first; j < ranges[i].second; ++j) {
      ret.push_back(j);
    }
  }
  return ret;
}
Exemplo n.º 10
0
em::DensityMap* remove_background(em::DensityMap *dmap,
                                  float threshold,float edge_threshold) {
  DensitySegmentationByCommunities ds(dmap,threshold);
  ds.build_density_graph(edge_threshold);
  IntsList cc_inds=ds.calculate_connected_components();
  //get the largest cc:
  Ints sizes;
  int max_ind=0;
  for(int i=0;i<(int)cc_inds.size();i++) {
    sizes.push_back(cc_inds[i].size());
    if (i>1){if (sizes[i]>sizes[max_ind]) max_ind=i;}
  }
  return get_segment_by_indexes(dmap,cc_inds[max_ind]);
}
Exemplo n.º 11
0
void KMCentersTree::get_assignments(Ints &close_center)
{
  IMP_LOG(VERBOSE,"KMCentersTree::get_assignments for "
  << centers_->get_number_of_centers() << " centers "<<std::endl);
  close_center.clear();
  Ints candidate_centers;
  for (int j = 0; j < centers_->get_number_of_centers(); j++) {
    candidate_centers.push_back(j);
  }
  close_center.clear();
  for(int i=0;i<data_points_->get_number_of_points();i++) {
    close_center.push_back(0);
  }
  root_->get_assignments(candidate_centers,close_center);
}
Exemplo n.º 12
0
NNGraph MSConnectivityScore::build_subgraph_from_assignment(
    NNGraph &G, Assignment const &assignment) const {
  unsigned int num_particles = restraint_.particle_matrix_.size();
  Ints vertices;
  for (unsigned int i = 0; i < assignment.size(); ++i)
    if (!assignment[i].empty()) {
      Ints const &conf = assignment[i].get_tuple();
      for (unsigned int j = 0; j < conf.size(); ++j)
        vertices.push_back(conf[j]);
    }
  boost::property_map<NNGraph, boost::vertex_name_t>::type vertex_id =
      boost::get(boost::vertex_name, G);
  boost::property_map<NNGraph, boost::edge_weight_t>::type dist =
      boost::get(boost::edge_weight, G);
  NNGraph ng(vertices.size());
  boost::property_map<NNGraph, boost::vertex_name_t>::type new_vertex_id =
      boost::get(boost::vertex_name, ng);
  boost::property_map<NNGraph, boost::edge_weight_t>::type new_dist =
      boost::get(boost::edge_weight, ng);
  for (unsigned int i = 0; i < vertices.size(); ++i)
    boost::put(new_vertex_id, i, vertices[i]);
  Ints vertex_id_to_idx(num_particles, -1);
  for (unsigned int i = 0; i < vertices.size(); ++i)
    vertex_id_to_idx[vertices[i]] = i;
  NNGraph::edge_iterator e, end;
  for (boost::tie(e, end) = edges(G); e != end; ++e) {
    unsigned int source_id = boost::get(vertex_id, source(*e, G));
    unsigned int dest_id = boost::get(vertex_id, target(*e, G));
    unsigned int p_src = vertex_id_to_idx[source_id];
    unsigned int p_dst = vertex_id_to_idx[dest_id];
    if (p_src == static_cast<unsigned int>(-1) ||
        p_dst == static_cast<unsigned int>(-1))
      continue;
    NNGraph::edge_descriptor ed = boost::add_edge(p_src, p_dst, ng).first;
    double d = boost::get(dist, *e);
    boost::put(new_dist, ed, d);
  }
  return ng;
}
void Test_example_KeepIf()
{
    typedef std::vector<int> Ints;
    Ints values = { 24, 11, 65, 44, 80, 18, 73, 90, 69, 18 };

    { // Version 1: hand written range based for loop
        Ints odds;
        for (int x : values)
            if (is_odd_int(x))
                odds.push_back(x);
    }

    { // Version 2: STL
        Ints odds;
        std::copy_if(std::begin(values), std::end(values),
                std::back_inserter(odds), is_odd_int);
    }

    { // Version : FunctionalPlus
        auto odds = fplus::keep_if(is_odd_int, values);
    }
}
Exemplo n.º 14
0
InferenceStatistics::Data InferenceStatistics::get_data(
    const Subset &, AssignmentContainer *iss) const {
  Assignments ss =
      iss->get_assignments(IntRange(0, iss->get_number_of_assignments()));
  Data ret;
  ret.size = iss->get_number_of_assignments();
  Ints sample;
  for (int i = 0; i < ret.size; ++i) {
    if (sample.size() < sample_size) {
      sample.push_back(i);
    } else {
      double prob = static_cast<double>(sample_size) / i;
      if (select_(base::random_number_generator) < prob) {
        int replace = place_(base::random_number_generator);
        sample[replace] = i;
      }
    }
  }
  ret.sample.resize(sample.size());
  for (unsigned int i = 0; i < sample.size(); ++i) {
    ret.sample[i] = iss->get_assignment(sample[i]);
  }
  return ret;
}
Exemplo n.º 15
0
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;
}
Exemplo n.º 16
0
Ints ReplicaExchange::create_indexes()
{
 Ints index;
 for(int i=0;i<nproc_;++i){index.push_back(i);}
 return index;
}