Skip to content

Commit

Permalink
Create a warning if the section type changes without a bifurcation
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed Sep 11, 2024
1 parent 7bdb495 commit f58f391
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion binds/python/bind_enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ void bind_enums(py::module& m) {
.value("write_empty_morphology", morphio::enums::WRITE_EMPTY_MORPHOLOGY)
.value("zero_diameter", morphio::enums::Warning::ZERO_DIAMETER)
.value("soma_non_contour", morphio::enums::Warning::SOMA_NON_CONTOUR)
.value("soma_non_cylinder_or_point", morphio::enums::Warning::SOMA_NON_CYLINDER_OR_POINT);
.value("soma_non_cylinder_or_point", morphio::enums::Warning::SOMA_NON_CYLINDER_OR_POINT)
.value("type_changed_within_section", morphio::enums::Warning::SECTION_TYPE_CHANGED);

py::enum_<morphio::enums::SomaType>(m, "SomaType", py::arithmetic())
.value("SOMA_UNDEFINED", morphio::enums::SomaType::SOMA_UNDEFINED)
Expand Down
1 change: 1 addition & 0 deletions include/morphio/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum Warning {
ZERO_DIAMETER, //!< Zero section diameter
SOMA_NON_CONTOUR, //!< Soma must be a contour for ASC and H5
SOMA_NON_CYLINDER_OR_POINT, //!< Soma must be stacked cylinders or a point
SECTION_TYPE_CHANGED, //!< In SWC, the type changed within a section, not post bifurcation
};

enum AnnotationType {
Expand Down
16 changes: 16 additions & 0 deletions include/morphio/warning_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ struct ZeroDiameter: public WarningMessage {
uint64_t lineNumber;
};

struct SectionTypeChanged: public WarningMessage {
SectionTypeChanged(std::string uri_, uint64_t lineNumber_)
: WarningMessage(std::move(uri_))
, lineNumber(lineNumber_) {}
morphio::enums::Warning warning() const final {
return Warning::SECTION_TYPE_CHANGED;
}
morphio::readers::ErrorLevel errorLevel = morphio::readers::ErrorLevel::WARNING;
std::string msg() const final {
static const char* description = "Warning: Type changed within section, without bifurcation";
return "\n" + details::errorLink(uri, lineNumber, errorLevel) + description;
}

uint64_t lineNumber;
};

struct DisconnectedNeurite: public WarningMessage {
DisconnectedNeurite(std::string uri_, uint64_t lineNumber_)
: WarningMessage(std::move(uri_))
Expand Down
5 changes: 3 additions & 2 deletions src/readers/morphologySWC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ class SWCBuilder
const SWCSample* sample = &samples_.at(id);

// create duplicate point if needed
if (!is_root && sample->point != start_point /*|| sample->diameter != start_diameter */) {
//if (!(is_root && sample->type == SECTION_SOMA) && sample->point != start_point /*|| sample->diameter != start_diameter */) {
if (!is_root && sample->point != start_point) {
points.push_back(start_point);
diameters.push_back(start_diameter);
}
Expand All @@ -478,6 +477,8 @@ class SWCBuilder
while (children_count == 1) {
sample = &samples_.at(id);
if(sample->type != samples_.at(children_.at(id)[0]).type){
warning_handler_->emit(std::make_unique<SectionTypeChanged>(
path_, sample->lineNumber));
break;
}
points.push_back(sample->point);
Expand Down
7 changes: 6 additions & 1 deletion tests/test_1_swc.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ def test_multi_type_section():
3 7 0 0 3 0.5 2 # <- type 7
4 8 0 0 4 0.5 3 # <- type 8
5 9 0 0 5 0.5 4''') # <- type 9
n = Morphology(contents, "swc")
warnings = morphio.WarningHandlerCollector()
n = Morphology(contents, "swc", warning_handler=warnings)
assert_array_equal(n.soma.points, [[0, 4, 0]])
assert_array_equal(n.soma.diameters, [6.0])
assert len(n.root_sections) == 1
Expand All @@ -583,6 +584,10 @@ def test_multi_type_section():
np.array([[0, 0, 2], ]))
assert len(n.sections) == 4
assert_array_equal(n.section_offsets, [0, 1, 3, 5, 7])
warnings = [f.warning for f in warnings.get_all()]
assert len(warnings) == 3 # type 7, 8, and 9
for warning in warnings:
assert warning.warning() == Warning.type_changed_within_section


def test_missing_parent():
Expand Down

0 comments on commit f58f391

Please sign in to comment.