Commit 8dd99577 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix unecessary pass in correlation histogram

It is not necessary to gather the data range if the bin size is
constant. This also changes the histogram code, to better handle the
constant-size bin situation.
parent 29f83ea6
......@@ -20,7 +20,6 @@
#include <tr1/unordered_set>
#include "shared_map.hh"
#include "histogram.hh"
#include "graph_util.hh"
namespace graph_tool
......
......@@ -205,19 +205,24 @@ struct get_correlation_histogram
// find the data range
pair<type1,type1> range1;
pair<type2,type2> range2;
typename graph_traits<Graph>::vertex_iterator vi,vi_end;
range1.first = boost::numeric::bounds<type1>::highest();
range1.second = boost::numeric::bounds<type1>::lowest();
range2.first = boost::numeric::bounds<type2>::highest();
range2.second = boost::numeric::bounds<type2>::lowest();
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
range1.first = range1.second = bins[0][0];
range2.first = range2.second = bins[1][0];
if (bins[0].size() == 1 || bins[1].size() == 1)
{
type1 v1 = deg1(*vi, g);
type2 v2 = deg2(*vi, g);
range1.first = min(range1.first, v1);
range1.second = max(range1.second, v1);
range2.first = min(range2.first, v2);
range2.second = max(range2.second, v2);
typename graph_traits<Graph>::vertex_iterator vi,vi_end;
range1.first = boost::numeric::bounds<type1>::highest();
range1.second = boost::numeric::bounds<type1>::lowest();
range2.first = boost::numeric::bounds<type2>::highest();
range2.second = boost::numeric::bounds<type2>::lowest();
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
type1 v1 = deg1(*vi, g);
type2 v2 = deg2(*vi, g);
range1.first = min(range1.first, v1);
range1.second = max(range1.second, v1);
range2.first = min(range2.first, v2);
range2.second = max(range2.second, v2);
}
}
boost::array<pair<val_type, val_type>, 2> data_range;
......@@ -285,14 +290,18 @@ struct get_avg_correlation
// find the data range
array<pair<val_type,val_type>,1> data_range;
typename graph_traits<Graph>::vertex_iterator vi,vi_end;
data_range[0].first = boost::numeric::bounds<type1>::highest();
data_range[0].second = boost::numeric::bounds<type1>::lowest();
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
data_range[0].first = data_range[0].second = _bins[0];
if (bins.size() == 1)
{
val_type v1 = deg1(*vi, g);
data_range[0].first = min(data_range[0].first, v1);
data_range[0].second = max(data_range[0].second, v1);
typename graph_traits<Graph>::vertex_iterator vi, vi_end;
data_range[0].first = boost::numeric::bounds<type1>::highest();
data_range[0].second = boost::numeric::bounds<type1>::lowest();
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
{
val_type v1 = deg1(*vi, g);
data_range[0].first = min(data_range[0].first, v1);
data_range[0].second = max(data_range[0].second, v1);
}
}
sum_t sum(bins, data_range);
......@@ -317,7 +326,7 @@ struct get_avg_correlation
s_sum2.Gather();
s_count.Gather();
for (size_t i = 0; i < s_sum.GetArray().size(); ++i)
for (size_t i = 0; i < sum.GetArray().size(); ++i)
{
sum.GetArray()[i] /= count.GetArray()[i];
sum2.GetArray()[i] =
......
......@@ -55,28 +55,11 @@ public:
Histogram(const boost::array<std::vector<ValueType>, Dim>& bins,
const boost::array<std::pair<ValueType,ValueType>,Dim>&
data_range)
: _bins(bins), _data_range(data_range), _grow(Dim, false)
: _bins(bins), _data_range(data_range)
{
bin_t new_shape;
for (size_t j = 0; j < Dim; ++j)
if (_bins[j].size() == 1) // constant bin width
{
if (_data_range[j].first == _data_range[j].second)
{
_grow[j] = true;
new_shape[j] == 1;
}
else
{
new_shape[j] = floor((_data_range[j].second -
_data_range[j].first) /
_bins[j][0]) + 1;
}
}
else
{
new_shape[j] = _bins[j].size();
}
new_shape[j] = _bins[j].size();
_counts.resize(new_shape);
}
......@@ -88,14 +71,15 @@ public:
if (_bins[i].size() == 1) // constant bin width
{
bin[i] = (v[i] - _data_range[i].first)/_bins[i][0];
if (_grow[i] && (bin[i] >= _counts.shape()[i]))
if (bin[i] >= _counts.shape()[i])
{
boost::array<size_t, Dim> new_shape;
for (size_t j = 0; j < Dim; ++j)
for (size_t j = 0; j < Dim; ++j)
new_shape[j] = _counts.shape()[j];
new_shape[i] = bin[i]+1;
new_shape[i] = bin[i] + 1;
_counts.resize(new_shape);
_data_range[i].second = max(v[i], _data_range[i].second);
if (v[i] > _data_range[i].second)
_data_range[i].second = v[i];
}
}
else // arbitrary bins. do a binary search
......@@ -111,7 +95,7 @@ public:
{
bin[i] = iter - bins.begin();
if (bin[i] == 0)
return; // falls off from fist bin
return; // falls off from fist bin, do not count
else
--bin[i];
}
......@@ -122,6 +106,10 @@ public:
boost::multi_array<CountType,Dim>& GetArray() { return _counts; }
boost::array<std::pair<ValueType,ValueType>,Dim>& GetDataRange()
{ return _data_range; }
boost::array<std::vector<ValueType>, Dim> GetBins()
{
boost::array<std::vector<ValueType>, Dim> bins;
......@@ -130,9 +118,7 @@ public:
{
for (ValueType i = _data_range[j].first;
i <= _data_range[j].second; i += _bins[j][0])
{
bins[j].push_back(i);
}
}
else
{
......@@ -145,7 +131,6 @@ protected:
boost::multi_array<CountType,Dim> _counts;
boost::array<std::vector<ValueType>, Dim> _bins;
boost::array<std::pair<ValueType,ValueType>,Dim> _data_range;
std::vector<bool> _grow;
};
......@@ -172,12 +157,20 @@ public:
#pragma omp critical
{
typename Histogram::bin_t shape;
for (size_t i = 0; i < shape.size(); ++i)
shape[i] = this->_counts.shape()[i];
for (size_t i = 0; i < this->_counts.num_dimensions(); ++i)
shape[i] = max(this->_counts.shape()[i],
_sum->GetArray().shape()[i]);
_sum->GetArray().resize(shape);
for (size_t i = 0; i < this->_counts.num_elements(); ++i)
{
_sum->GetArray().data()[i] += this->_counts.data()[i];
for (size_t i = 0; i < Histogram::dim::value; ++i)
{
_sum->GetDataRange()[i].first =
min(this->_data_range[i].first,
_sum->GetDataRange()[i].first);
_sum->GetDataRange()[i].second =
max(this->_data_range[i].second,
_sum->GetDataRange()[i].second);
}
}
_sum = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment