Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit

Permalink
write more tests so we increase the test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed Mar 17, 2022
1 parent 24073d9 commit 34bff1a
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 100 deletions.
3 changes: 2 additions & 1 deletion src/morphology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ morphio::SomaType getSomaType(long unsigned int num_soma_points) {
return morphio::SOMA_SINGLE_POINT;
case 2:
return morphio::SOMA_UNDEFINED;
default:;
default:
break;
}
return morphio::SOMA_SIMPLE_CONTOUR;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
set(TESTS_SRC
main.cpp
test_morphology_readers.cpp
test_immutable_morphology.cpp
test_mitochondria.cpp
test_morphology_readers.cpp
test_mutable_morphology.cpp
test_vasculature_morphology.cpp
)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>

static bool almost_equal(morphio::floatType a, double expected, double epsilon) {
#ifdef MORPHIO_USE_DOUBLE
bool res = std::abs(a - expected) < epsilon;
#else
bool res = std::abs(static_cast<double>(a) - expected) < epsilon;
#endif
if (!res) {
std::cerr << "Failed almost equal: " << a << " != " << expected
<< " (expected) with epsilon of " << epsilon << '\n';
}
return res;
}

static bool array_almost_equal(const std::vector<morphio::floatType>& a,
const std::vector<double>& expected,
double epsilon) {
if (a.size() != expected.size()) {
return false;
}
for (size_t i = 0; i < a.size(); i++) {
if (!almost_equal(a.at(i), expected.at(i), epsilon)) {
return false;
}
}
return true;
}
99 changes: 1 addition & 98 deletions tests/test_immutable_morphology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,15 @@

#include <morphio/endoplasmic_reticulum.h>
#include <morphio/glial_cell.h>
#include <morphio/mito_section.h>
#include <morphio/mitochondria.h>
#include <morphio/morphology.h>
#include <morphio/mut/morphology.h>
#include <morphio/properties.h>
#include <morphio/section.h>
#include <morphio/soma.h>

#include "test_helpers.h"

namespace {
bool almost_equal(morphio::floatType a, double expected, double epsilon) {
#ifdef MORPHIO_USE_DOUBLE
bool res = std::abs(a - expected) < epsilon;
#else
bool res = std::abs(static_cast<double>(a) - expected) < epsilon;
#endif
if (!res) {
std::cerr << "Failed almost equal: " << a << " != " << expected
<< " (expected) with epsilon of " << epsilon << '\n';
}
return res;
}

bool array_almost_equal(const std::vector<morphio::floatType>& a,
const std::vector<double>& expected,
double epsilon) {
if (a.size() != expected.size()) {
return false;
}
for (size_t i = 0; i < a.size(); i++) {
if (!almost_equal(a.at(i), expected.at(i), epsilon)) {
return false;
}
}
return true;
}


class Files
{
public:
Expand Down Expand Up @@ -252,74 +223,6 @@ TEST_CASE("connectivity", "[immutableMorphology]") {
}
}

TEST_CASE("mitochondria", "[immutableMorphology]") {
morphio::Morphology morph = morphio::Morphology("data/h5/v1/mitochondria.h5");
morphio::Mitochondria mito = morph.mitochondria();
REQUIRE(mito.rootSections().size() == 2);
morphio::MitoSection rootSection = mito.rootSections().at(0);
REQUIRE(rootSection.id() == 0);
auto diameters = rootSection.diameters();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(diameters.begin(), diameters.end()),
std::vector<double>{10.0, 20.0},
0.01));
auto relativePathLength = rootSection.relativePathLengths();
auto res = std::vector<morphio::floatType>(relativePathLength.begin(),
relativePathLength.end());

REQUIRE(almost_equal(res.at(0), 0.5, 0.001));
REQUIRE(almost_equal(res.at(1), 0.6000000238, 0.001));

auto neuriteSectionIds = rootSection.neuriteSectionIds();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(neuriteSectionIds.begin(),
neuriteSectionIds.end()),
std::vector<double>{0.0, 0.0},
0.01));
REQUIRE(rootSection.children().size() == 1);

auto child = rootSection.children().at(0);
REQUIRE(child.parent().id() == rootSection.id());

diameters = child.diameters();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(diameters.begin(), diameters.end()),
std::vector<double>{20.0, 30.0, 40.0, 50.0},
0.01));
relativePathLength = child.relativePathLengths();

REQUIRE(array_almost_equal(std::vector<morphio::floatType>(relativePathLength.begin(),
relativePathLength.end()),
std::vector<double>{0.6, 0.7, 0.8, 0.9},
0.01));

neuriteSectionIds = child.neuriteSectionIds();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(neuriteSectionIds.begin(),
neuriteSectionIds.end()),
std::vector<double>{3.0, 4.0, 4.0, 5.0},
0.01));
rootSection = mito.rootSections().at(1);
diameters = rootSection.diameters();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(diameters.begin(), diameters.end()),
std::vector<double>{5.0, 6.0, 7.0, 8.0},
0.01));
relativePathLength = rootSection.relativePathLengths();

REQUIRE(array_almost_equal(std::vector<morphio::floatType>(relativePathLength.begin(),
relativePathLength.end()),
std::vector<double>{0.6, 0.7, 0.8, 0.9},
0.01));

neuriteSectionIds = rootSection.neuriteSectionIds();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(neuriteSectionIds.begin(),
neuriteSectionIds.end()),
std::vector<double>{0.0, 1.0, 1.0, 2.0},
0.01));
REQUIRE(rootSection.children().empty());

{
morphio::Morphology morph0 = morphio::Morphology("data/h5/v1/mitochondria.h5");
morphio::Morphology morph1 = morphio::Morphology("data/h5/v1/mitochondria.h5");
REQUIRE(morph0.rootSections().at(0).hasSameShape(morph1.rootSections().at(0)));
}
}


TEST_CASE("endoplasmic_reticulum", "[immutableMorphology]") {
Expand Down
122 changes: 122 additions & 0 deletions tests/test_mitochondria.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <catch2/catch.hpp>

#include <morphio/mito_section.h>
#include <morphio/mitochondria.h>
#include <morphio/section.h>

#include <algorithm>
#include <iterator>
#include <vector>

#include "test_helpers.h"


TEST_CASE("mitochondria", "[mitochondria]") {
const auto mito = morphio::Morphology("data/h5/v1/mitochondria.h5").mitochondria();

REQUIRE(mito.rootSections().size() == 2);

auto rootSection = mito.rootSections().at(0);
REQUIRE(rootSection.id() == 0);

auto diameters = rootSection.diameters();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(diameters.begin(), diameters.end()),
std::vector<double>{10.0, 20.0},
0.01));
auto relativePathLength = rootSection.relativePathLengths();
auto res = std::vector<morphio::floatType>(relativePathLength.begin(),
relativePathLength.end());

REQUIRE(almost_equal(res.at(0), 0.5, 0.001));
REQUIRE(almost_equal(res.at(1), 0.6000000238, 0.001));

auto neuriteSectionIds = rootSection.neuriteSectionIds();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(neuriteSectionIds.begin(),
neuriteSectionIds.end()),
std::vector<double>{0.0, 0.0},
0.01));
REQUIRE(rootSection.children().size() == 1);

auto child = rootSection.children().at(0);
REQUIRE(child.parent().id() == rootSection.id());

diameters = child.diameters();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(diameters.begin(), diameters.end()),
std::vector<double>{20.0, 30.0, 40.0, 50.0},
0.01));
relativePathLength = child.relativePathLengths();

REQUIRE(array_almost_equal(std::vector<morphio::floatType>(relativePathLength.begin(),
relativePathLength.end()),
std::vector<double>{0.6, 0.7, 0.8, 0.9},
0.01));

neuriteSectionIds = child.neuriteSectionIds();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(neuriteSectionIds.begin(),
neuriteSectionIds.end()),
std::vector<double>{3.0, 4.0, 4.0, 5.0},
0.01));
rootSection = mito.rootSections().at(1);
diameters = rootSection.diameters();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(diameters.begin(), diameters.end()),
std::vector<double>{5.0, 6.0, 7.0, 8.0},
0.01));
relativePathLength = rootSection.relativePathLengths();

REQUIRE(array_almost_equal(std::vector<morphio::floatType>(relativePathLength.begin(),
relativePathLength.end()),
std::vector<double>{0.6, 0.7, 0.8, 0.9},
0.01));

neuriteSectionIds = rootSection.neuriteSectionIds();
REQUIRE(array_almost_equal(std::vector<morphio::floatType>(neuriteSectionIds.begin(),
neuriteSectionIds.end()),
std::vector<double>{0.0, 1.0, 1.0, 2.0},
0.01));
REQUIRE(rootSection.children().empty());
}

TEST_CASE("mitochondria.sections", "[mitochondria]") {
const auto mito = morphio::Morphology("data/h5/v1/mitochondria.h5").mitochondria();
auto sections = mito.sections();

std::vector<std::size_t> res;
std::transform(sections.begin(),
sections.end(),
std::back_inserter(res),
[](const morphio::MitoSection& s) { return s.id(); });
REQUIRE(res == std::vector<size_t>{0, 1, 2});
}

TEST_CASE("mitochondria.iteration", "[mitochondria]") {
const auto mito = morphio::Morphology("data/h5/v1/mitochondria.h5").mitochondria();

const auto rootSection = mito.rootSections().at(0);

std::vector<std::size_t> res;
std::transform(rootSection.depth_begin(),
rootSection.depth_end(),
std::back_inserter(res),
[](const morphio::MitoSection& s) { return s.id(); });
REQUIRE(res == std::vector<size_t>{0, 1});

res.clear();
std::transform(rootSection.breadth_begin(),
rootSection.breadth_end(),
std::back_inserter(res),
[](const morphio::MitoSection& s) { return s.id(); });
REQUIRE(res == std::vector<size_t>{0, 1});

res.clear();
std::transform(rootSection.upstream_begin(),
rootSection.upstream_end(),
std::back_inserter(res),
[](const morphio::MitoSection& s) { return s.id(); });
REQUIRE(res == std::vector<size_t>{0});
}

TEST_CASE("mitochondria.hasSameShape", "[mitochondria]") {
morphio::Morphology morph0 = morphio::Morphology("data/h5/v1/mitochondria.h5");
morphio::Morphology morph1 = morphio::Morphology("data/h5/v1/mitochondria.h5");
REQUIRE(morph0.rootSections().at(0).hasSameShape(morph1.rootSections().at(0)));
}

0 comments on commit 34bff1a

Please sign in to comment.