This repository was archived by the owner on Feb 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write more tests so we increase the test coverage
- Loading branch information
Showing
4 changed files
with
153 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |