-
Notifications
You must be signed in to change notification settings - Fork 61
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
ASCII art for segment tree #2224
Merged
thorstenhater
merged 14 commits into
arbor-sim:master
from
thorstenhater:feat/ascii-art-segment-tree
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c69ba65
Add ASCII `show` for segment tree.
thorstenhater 454e196
Add Python ad docs.
thorstenhater 2cc501f
Remove formatter.
thorstenhater eb0200b
Remove debug print.
thorstenhater 2cad334
Shuffle meothds to dedicated header. Add morphology support.
thorstenhater 0585a8e
Add the headers.
thorstenhater 2e9d720
Merge remote-tracking branch 'origin/master' into feat/ascii-art-segm…
thorstenhater 1894f89
Clean-up headers.
thorstenhater dd15026
Start cleaning up code. Snapshot to test.
thorstenhater 4f3930c
Merge remote-tracking branch 'origin/master' into feat/ascii-art-segm…
thorstenhater 6ebd9ea
Simplify code, add tests, polish docs.
thorstenhater 9510a25
Redundant.
thorstenhater bda3d06
Extra \n.
thorstenhater 208df76
Add missing test.
thorstenhater File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,99 @@ | ||
#include <arborio/debug.hpp> | ||
|
||
#include <arbor/morph/primitives.hpp> | ||
|
||
#include <map> | ||
#include <numeric> | ||
|
||
namespace arborio { | ||
|
||
template <typename T, typename P> | ||
std::vector<std::string> render(const T& tree, | ||
arb::msize_t root, | ||
const std::multimap<arb::msize_t, arb::msize_t>& children, | ||
P print) { | ||
// ASCII art elements | ||
// TODO these could be customizable, but need conformant lengths | ||
const std::string vline = " | "; | ||
const std::string hline = "---"; | ||
const std::string blank = " "; | ||
const std::string split = "-+-"; | ||
const std::string start = " +-"; | ||
|
||
auto n_child = children.count(root); | ||
auto seg = print(root, tree); | ||
if (0 == n_child) return {seg}; | ||
|
||
auto sep = std::string(seg.size(), ' '); | ||
const auto& [beg, end] = children.equal_range(root); | ||
|
||
std::vector res = {seg}; | ||
arb::msize_t cdx = 0; | ||
for (auto it = beg; it != end; ++it) { | ||
const auto& [parent, child] = *it; | ||
auto rows = render(tree, child, children, print); | ||
auto rdx = 0; | ||
for (const auto& row: rows) { | ||
// Append the first row directly onto our segments, this [- -] -- [- -] | ||
if (rdx == 0) { | ||
// The first child of a node may span a sub-tree | ||
if (cdx == 0) { | ||
res.back() += split + row; | ||
} else { | ||
// Other children get connected to the vertical line | ||
res.push_back(sep + start + row); | ||
} | ||
cdx++; | ||
} else { | ||
// If there are more children, extend the subtree by showing a | ||
// vertical line | ||
res.push_back(sep + (cdx < n_child ? vline : blank) + row); | ||
} | ||
++rdx; | ||
} | ||
} | ||
// res.push_back(sep); | ||
return res; | ||
} | ||
|
||
ARB_ARBORIO_API std::string default_segment_printer(const arb::msize_t id, const arb::segment_tree&) { | ||
auto lbl = (id == arb::mnpos) ? "(root)" : std::to_string(id); | ||
return "[-- id=" + lbl + " --]" ; | ||
} | ||
|
||
std::string ARB_ARBORIO_API default_branch_printer(const arb::msize_t id, const arb::morphology& mrf) { | ||
auto lbl = (id == arb::mnpos) ? std::string("(root)") : std::to_string(id); | ||
return "<-- id=" + std::to_string(id) + " len=" + std::to_string(mrf.branch_segments(id).size()) + " -->" ; | ||
} | ||
|
||
ARB_ARBORIO_API std::string show(const arb::segment_tree& tree) { | ||
if (tree.empty()) return ""; | ||
|
||
std::multimap<arb::msize_t, arb::msize_t> children; | ||
const auto& ps = tree.parents(); | ||
for (arb::msize_t idx = 0; idx < tree.size(); ++idx) { | ||
auto parent = ps[idx]; | ||
children.emplace(parent, idx); | ||
} | ||
|
||
auto res = render(tree, 0, children, default_segment_printer); | ||
return std::accumulate(res.begin(), res.end(), | ||
std::string{}, | ||
[](auto lhs, auto rhs) { return lhs + rhs + "\n"; }); | ||
} | ||
|
||
ARB_ARBORIO_API std::string show(const arb::morphology& mrf) { | ||
if (mrf.empty()) return ""; | ||
|
||
std::multimap<arb::msize_t, arb::msize_t> children; | ||
for (arb::msize_t idx = 0; idx < mrf.num_branches(); ++idx) { | ||
auto parent = mrf.branch_parent(idx); | ||
children.emplace(parent, idx); | ||
} | ||
|
||
auto res = render(mrf, 0, children, default_branch_printer); | ||
return std::accumulate(res.begin(), res.end(), | ||
std::string{}, | ||
[](auto lhs, auto rhs) { return lhs + rhs + "\n"; }); | ||
} | ||
} |
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,16 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <functional> | ||
#include <vector> | ||
|
||
#include <arbor/export.hpp> | ||
#include <arborio/export.hpp> | ||
|
||
#include <arbor/morph/segment_tree.hpp> | ||
#include <arbor/morph/morphology.hpp> | ||
|
||
namespace arborio { | ||
ARB_ARBORIO_API std::string show(const arb::segment_tree&); | ||
ARB_ARBORIO_API std::string show(const arb::morphology&); | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documentation under cpp? |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documentation under python? |
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
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
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,49 @@ | ||
#include <arbor/morph/morphology.hpp> | ||
#include <arbor/morph/segment_tree.hpp> | ||
|
||
#include <arborio/debug.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
TEST(debug_io, single) { | ||
arb::segment_tree tree; | ||
arb::msize_t par = arb::mnpos; | ||
tree.append(par, {0, 0, 0, 5}, {0, 0, 10, 5}, 42); | ||
|
||
EXPECT_EQ("[-- id=0 --]\n", arborio::show(tree)); | ||
EXPECT_EQ("<-- id=0 len=1 -->\n", arborio::show(arb::morphology{tree})); | ||
} | ||
|
||
TEST(debug_io, fork) { | ||
arb::segment_tree tree; | ||
arb::msize_t par = arb::mnpos; | ||
par = tree.append(par, {0, 0, 0, 5}, {0, 0, 10, 5}, 42); | ||
tree.append(par, {0, 0, 10, 5}, {0, 1, 10, 5}, 23); | ||
tree.append(par, {0, 0, 10, 5}, {0, -1, 10, 5}, 23); | ||
|
||
EXPECT_EQ("[-- id=0 --]-+-[-- id=1 --]\n" | ||
" +-[-- id=2 --]\n", | ||
arborio::show(tree)); | ||
EXPECT_EQ("<-- id=0 len=1 -->-+-<-- id=1 len=1 -->\n" | ||
" +-<-- id=2 len=1 -->\n", | ||
arborio::show(arb::morphology{tree})); | ||
} | ||
|
||
TEST(debug_io, complex) { | ||
arb::segment_tree tree; | ||
arb::msize_t lvl0 = arb::mnpos; | ||
lvl0 = tree.append(lvl0, {0, 0, 0, 5}, {0, 0, 10, 5}, 42); | ||
tree.append(lvl0, {0, 0, 10, 5}, {0, 1, 10, 5}, 23); | ||
auto lvl1 = tree.append(lvl0, {0, 0, 10, 5}, {0, -1, 10, 5}, 23); | ||
tree.append(lvl1, {0, -1, 10, 5}, { 1, -1, 10, 5}, 23); | ||
tree.append(lvl1, {0, -1, 10, 5}, {-1, -1, 10, 5}, 23); | ||
|
||
EXPECT_EQ("[-- id=0 --]-+-[-- id=1 --]\n" | ||
" +-[-- id=2 --]-+-[-- id=3 --]\n" | ||
" +-[-- id=4 --]\n", | ||
arborio::show(tree)); | ||
EXPECT_EQ("<-- id=0 len=1 -->-+-<-- id=1 len=1 -->\n" | ||
" +-<-- id=2 len=1 -->-+-<-- id=3 len=1 -->\n" | ||
" +-<-- id=4 len=1 -->\n", | ||
arborio::show(arb::morphology{tree})); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed anymore?