Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more testing of the data access methods and some fixing #838

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions include/nix/DataView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class NIXAPI DataView : public DataSet {
public:
DataView(DataArray da, NDSize count, NDSize offset)
: array(std::move(da)), offset(std::move(offset)), count(std::move(count)) {

if (this->offset.size() != array.dataExtent().size()) {
throw IncompatibleDimensions("DataView offset dimensionality does not match dimensionality of data", "nix::DataView");
}
Expand All @@ -28,7 +27,7 @@ class NIXAPI DataView : public DataSet {
}
if (this->offset + this->count > array.dataExtent()) {
throw OutOfBounds("Trying to create DataView which is out of bounds");
}
}
}

// the DataIO interface implementation
Expand Down Expand Up @@ -59,4 +58,4 @@ class NIXAPI DataView : public DataSet {

} // nix::

#endif // DATA_VIEW_HPP
#endif // DATA_VIEW_HPP
12 changes: 6 additions & 6 deletions include/nix/util/dataAccess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,23 +397,23 @@ NIXAPI DEPRECATED DataView retrieveData(const Tag &tag, const DataArray &array,
/**
* @brief Checks whether a given position is in the extent of the given DataArray.
*
* @param data The data array.
* @param data_size The extent of the data array.
* @param position The position.
*
* @return True if the position is in the extent of the data array, false otherwise.
*/
NIXAPI bool positionInData(const DataArray &data, const NDSize &position);
NIXAPI bool positionInData(const NDSize &data_size, const NDSize &position);

/**
* @brief Checks whether a given position plus count is in the extent of the given DataArray.
* @brief Checks whether a given offset plus count is in the extent of the given DataArray.
*
* @param data The DataArray.
* @param position The position
* @param offset The offset
* @param count The number of elements per dimension.
*
* @return True if position and count are in the extent of the data array, false otherwise.
* @return True if offset and offset + count are in the extent of the data array, false otherwise.
*/
NIXAPI bool positionAndExtentInData(const DataArray &data, const NDSize &position, const NDSize &count);
NIXAPI bool positionAndExtentInData(const DataArray &data, const NDSize &offset, const NDSize &count);

/**
* @brief Retruns the feature data associated with a Tag.
Expand Down
23 changes: 13 additions & 10 deletions src/Dimensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,15 @@ boost::optional<std::pair<ndsize_t, ndsize_t>> SampledDimension::indexOf(const d
boost::optional<std::pair<ndsize_t, ndsize_t>> SampledDimension::indexOf(double start, double end, const double sampling_interval,
const double offset, const RangeMatch match) const {
boost::optional<std::pair<ndsize_t, ndsize_t>> indices;
PositionMatch pos_match = match == RangeMatch::Inclusive ? PositionMatch::LessOrEqual : PositionMatch::Less;

if (start > end) {
return indices;
}
PositionMatch end_matching;
end_matching = (match == RangeMatch::Inclusive) ? PositionMatch::LessOrEqual : PositionMatch::Less;

boost::optional<ndsize_t> si = getSampledIndex(start, offset, sampling_interval, PositionMatch::GreaterOrEqual);
boost::optional<ndsize_t> ei = getSampledIndex(end, offset, sampling_interval, pos_match);
boost::optional<ndsize_t> ei = getSampledIndex(end, offset, sampling_interval, end_matching);

if (si && ei && *si <= *ei) {
indices = std::pair<ndsize_t, ndsize_t>(*si, *ei);
}
Expand Down Expand Up @@ -453,10 +454,12 @@ boost::optional<std::pair<ndsize_t, ndsize_t>> SetDimension::indexOf(double star
if (start > end) {
return index;
}
PositionMatch end_match = match == RangeMatch::Inclusive ? PositionMatch::LessOrEqual : PositionMatch::Less;

PositionMatch end_matching;
end_matching = (match == RangeMatch::Inclusive) ? PositionMatch::LessOrEqual : PositionMatch::Less;

boost::optional<ndsize_t> si = getSetIndex(start, set_labels, PositionMatch::GreaterOrEqual);
boost::optional<ndsize_t> ei = getSetIndex(end, set_labels, end_match);
boost::optional<ndsize_t> ei = getSetIndex(end, set_labels, end_matching);

if (si && ei && *si <= *ei) {
index = std::pair<ndsize_t, ndsize_t>(*si, *ei);
}
Expand Down Expand Up @@ -662,8 +665,9 @@ boost::optional<std::pair<ndsize_t, ndsize_t>> RangeDimension::indexOf(double st
if (!si) {
return range;
}
PositionMatch endMatching = (match == RangeMatch::Inclusive) ? PositionMatch::LessOrEqual : PositionMatch::Less;
boost::optional<ndsize_t> ei = getIndex(end, ticks, endMatching);
PositionMatch end_matching;
end_matching = (match == RangeMatch::Inclusive) ? PositionMatch::LessOrEqual : PositionMatch::Less;
boost::optional<ndsize_t> ei = getIndex(end, ticks, end_matching);
if (ei && *si <= *ei) {
range = std::pair<ndsize_t, ndsize_t>(*si, *ei);
}
Expand Down Expand Up @@ -716,7 +720,6 @@ std::vector<boost::optional<std::pair<ndsize_t, ndsize_t>>> RangeDimension::inde
if (start_positions.size() != end_positions.size()) {
throw runtime_error("Dimension::IndexOf - Number of start and end positions must match!");
}

std::vector<boost::optional<std::pair<ndsize_t, ndsize_t>>> indices;
vector<double> ticks = this->ticks();
for (size_t i = 0; i < start_positions.size(); ++i) {
Expand Down
38 changes: 22 additions & 16 deletions src/util/dataAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,16 @@ void getOffsetAndCount(const MultiTag &tag, const DataArray &array, const vector
vector<vector<optional<pair<ndsize_t, ndsize_t>>>> data_indices;
for (size_t dim_index = 0; dim_index < dimensions.size(); ++dim_index) {
vector<string> temp_units(start_positions[dim_index].size(), units[dim_index]);
vector<optional<pair<ndsize_t, ndsize_t>>> ranges = positionToIndex(start_positions[dim_index], end_positions[dim_index],
temp_units, match, dimensions[dim_index]);

vector<optional<pair<ndsize_t, ndsize_t>>> ranges;
RangeMatch rm; // The range match needs to be adjusted if the extent is 0.0 in that case we want have an inclusive matching
if (start_positions[dim_index] == end_positions[dim_index]) {
rm = RangeMatch::Inclusive;
} else {
rm = match;
}
ranges = positionToIndex(start_positions[dim_index], end_positions[dim_index],
temp_units, rm, dimensions[dim_index]);

data_indices.push_back(ranges);
}
// at this point we do have all the start and end indices of the tagged positions that the caller wants the data of.
Expand All @@ -497,15 +504,17 @@ void getOffsetAndCount(const MultiTag &tag, const DataArray &array, const vector
ndsize_t count = (*opt_range).second - (*opt_range).first;
data_count[dim_index] += count;
} else {
data_count[dim_index] = 0;
if (end_positions[dim_index][i] == start_positions[dim_index][i]) {
optional<ndsize_t> ofst = positionToIndex(end_positions[dim_index][i], units[dim_index], PositionMatch::GreaterOrEqual, dimensions[dim_index]);
if (!ofst) {
throw nix::OutOfBounds("util::offsetAndCount:An invalid range was encountered!");
}
temp_offset[i] = *ofst;
temp_offset[i] = *ofst; // I do not remember what this might be good for...
}
}
}

offsets.push_back(data_offset);
counts.push_back(data_count);
}
Expand All @@ -519,24 +528,24 @@ void getOffsetAndCount(const MultiTag &tag, const DataArray &array, ndsize_t ind
}


bool positionInData(const DataArray &data, const NDSize &position) {
NDSize data_size = data.dataExtent();
bool positionInData(const NDSize &data_size, const NDSize &position) {
bool valid = true;

if (!(data_size.size() == position.size())) {
return false;
}
for (size_t i = 0; i < data_size.size(); ++i) {
valid &= position[i] < data_size[i];
valid &= position[i] <= data_size[i];
}
return valid;
}


bool positionAndExtentInData(const DataArray &data, const NDSize &position, const NDSize &count) {
NDSize pos = position + count;
pos -= 1;
return positionInData(data, pos);
bool positionAndExtentInData(const DataArray &data, const NDSize &offset, const NDSize &count) {
NDSize end_pos = offset + count;
NDSize data_size = data.dataExtent();
bool valid = positionInData(data_size, offset);
valid &= positionInData(data_size, end_pos);
return valid;
}


Expand Down Expand Up @@ -675,6 +684,7 @@ vector<DataView> taggedData(const MultiTag &tag, vector<ndsize_t> &position_indi
vector<NDSize> counts, offsets;
vector<DataView> views;

// if no positions are given, we will return dataviews for all positions
if (position_indices.size() < 1) {
size_t pos_count = check::fits_in_size_t(tag.positions().dataExtent()[0],
"Number of positions > size_t.");
Expand All @@ -683,11 +693,7 @@ vector<DataView> taggedData(const MultiTag &tag, vector<ndsize_t> &position_indi
}

getOffsetAndCount(tag, array, position_indices, offsets, counts, match);

for (size_t i = 0; i < offsets.size(); ++i) {
if (!positionAndExtentInData(array, offsets[i], counts[i])) {
throw OutOfBounds("References data slice out of the extent of the DataArray!", 0);
}
DataView io = DataView(array, counts[i], offsets[i]);
views.push_back(io);
}
Expand Down
23 changes: 15 additions & 8 deletions test/BaseTestDataAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ void BaseTestDataAccess::testPositionToIndexSetDimension() {

CPPUNIT_ASSERT_THROW(util::positionToIndex({5.0, 0.}, {10.5}, RangeMatch::Inclusive, setDim), std::runtime_error);

vector<optional<pair<ndsize_t, ndsize_t>>> ranges = util::positionToIndex({5.0, 0.}, {10.5, 1.0}, RangeMatch::Inclusive, setDim);
vector<optional<pair<ndsize_t, ndsize_t>>> ranges;
ranges = util::positionToIndex({5.0, 0.}, {10.5, 1.0}, RangeMatch::Inclusive, setDim);
CPPUNIT_ASSERT(ranges.size() == 2);
CPPUNIT_ASSERT(!ranges[0]);
CPPUNIT_ASSERT(ranges[1] && (*ranges[1]).first == 0 && (*ranges[1]).second == 1);
Expand Down Expand Up @@ -288,19 +289,18 @@ void BaseTestDataAccess::testOffsetAndCount() {
CPPUNIT_ASSERT_THROW(util::getOffsetAndCount(multi_tag, data_array, -1, offsets, counts), nix::OutOfBounds); // not a valid position index
CPPUNIT_ASSERT_THROW(util::getOffsetAndCount(multi_tag, data_array, 3, offsets, counts), nix::OutOfBounds); // not a valid position index

util::getOffsetAndCount(multi_tag, data_array, 0, offsets, counts);
util::getOffsetAndCount(multi_tag, data_array, 0, offsets, counts, RangeMatch::Inclusive);
CPPUNIT_ASSERT(offsets.size() == 4);
CPPUNIT_ASSERT(counts.size() == 4);
CPPUNIT_ASSERT(offsets[0] == 0 && offsets[1] == 3 && offsets[2] == 2 && offsets[3] == 1);
CPPUNIT_ASSERT(counts[0] == 1 && counts[1] == 7 && counts[2] == 2 && counts[3] == 3);

util::getOffsetAndCount(multi_tag, data_array, 0, offsets, counts, RangeMatch::Exclusive);
CPPUNIT_ASSERT(offsets.size() == 4);
CPPUNIT_ASSERT(counts.size() == 4);
CPPUNIT_ASSERT(offsets[0] == 0 && offsets[1] == 3 && offsets[2] == 2 && offsets[3] == 1);
CPPUNIT_ASSERT(counts[0] == 1 && counts[1] == 6 && counts[2] == 2 && counts[3] == 2);
CPPUNIT_ASSERT(counts[0] == 1 && counts[1] == 7 && counts[2] == 2 && counts[3] == 3);

util::getOffsetAndCount(multi_tag, data_array, 1, offsets, counts);
util::getOffsetAndCount(multi_tag, data_array, 1, offsets, counts, RangeMatch::Inclusive);
CPPUNIT_ASSERT(offsets.size() == 4);
CPPUNIT_ASSERT(counts.size() == 4);
CPPUNIT_ASSERT(offsets[0] == 0 && offsets[1] == 8 && offsets[2] == 1 && offsets[3] == 1);
Expand All @@ -317,11 +317,19 @@ void BaseTestDataAccess::testOffsetAndCount() {
void BaseTestDataAccess::testPositionInData() {
NDSize offsets, counts;
util::getOffsetAndCount(multi_tag, data_array, 0, offsets, counts);
CPPUNIT_ASSERT(util::positionInData(data_array, offsets));
CPPUNIT_ASSERT(util::positionInData(data_array.dataExtent(), offsets));
CPPUNIT_ASSERT(util::positionAndExtentInData(data_array, offsets, counts));

util::getOffsetAndCount(multi_tag, data_array, 1, offsets, counts);
CPPUNIT_ASSERT(util::positionInData(data_array, offsets));
CPPUNIT_ASSERT(util::positionInData(data_array.dataExtent(), offsets));
CPPUNIT_ASSERT(!util::positionAndExtentInData(data_array, offsets, counts));

util::getOffsetAndCount(multi_tag, data_array, 0, offsets, counts, RangeMatch::Exclusive);
CPPUNIT_ASSERT(util::positionInData(data_array.dataExtent(), offsets));
CPPUNIT_ASSERT(util::positionAndExtentInData(data_array, offsets, counts));

util::getOffsetAndCount(multi_tag, data_array, 1, offsets, counts, RangeMatch::Exclusive);
CPPUNIT_ASSERT(util::positionInData(data_array.dataExtent(), offsets));
CPPUNIT_ASSERT(!util::positionAndExtentInData(data_array, offsets, counts));
}

Expand Down Expand Up @@ -454,7 +462,6 @@ void BaseTestDataAccess::testMultiTagFeatureData() {
}
}
index_data.setData(data1);

DataArray tagged_data = block.createDataArray("tagged feature data", "test", nix::DataType::Double, {10, 20, 10, 10});
dim1 = tagged_data.appendSampledDimension(1.0);
dim1.unit("ms");
Expand Down
7 changes: 6 additions & 1 deletion test/BaseTestDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ void BaseTestDimension::testRangeDimUnit() {

std::vector<double> ticks;
for (size_t i = 0; i < 5; i++) {
ticks.push_back(i * boost::math::constants::pi<double>());
ticks.push_back(i * boost::math::constants::pi<double>());
}
Dimension d = data_array.appendRangeDimension(ticks);
CPPUNIT_ASSERT(d.dimensionType() == DimensionType::Range);
Expand Down Expand Up @@ -822,6 +822,11 @@ void BaseTestDimension::testRangeDimIndexOf() {
CPPUNIT_ASSERT(!range);
range = rd.indexOf(100., -100., {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(!range);
range = rd.indexOf(-100., -100, {}, RangeMatch::Inclusive);
CPPUNIT_ASSERT(range);
CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 0);
range = rd.indexOf(-100., -100, {}, RangeMatch::Exclusive);
CPPUNIT_ASSERT(!range);

range = rd.indexOf(100., -100., rd.ticks(), RangeMatch::Exclusive);
CPPUNIT_ASSERT(!range);
Expand Down