From 2132da8f97eedf0c7f6d8ad7b5fa20b2eb05a17d Mon Sep 17 00:00:00 2001 From: Eleftherios Zisis Date: Thu, 25 Apr 2024 14:41:28 +0200 Subject: [PATCH] Add check --- neurom/core/morphology.py | 7 +++++++ tests/core/test_neuron.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/neurom/core/morphology.py b/neurom/core/morphology.py index fd99e3f3..d4f35ee8 100644 --- a/neurom/core/morphology.py +++ b/neurom/core/morphology.py @@ -42,6 +42,7 @@ from neurom.core.soma import make_soma from neurom.core.types import NeuriteIter, NeuriteType from neurom.utils import flatten +from neurom.exceptions import NeuroMError class Section: @@ -546,6 +547,12 @@ def __init__(self, morphio_morph, name=None, process_subtrees=False): name (str): an optional morphology name process_subtrees (bool): enable mixed tree processing if set to True """ + if not isinstance(morphio_morph, (morphio.Morphology, morphio.mut.Morphology)): + raise NeuroMError( + f"Expected morphio Morphology object but got: {morphio_morph}.\n" + f"Use neurom.load_morphology() to load from file." + ) + self._morphio_morph = morphio_morph self.name = name if name else 'Morphology' diff --git a/tests/core/test_neuron.py b/tests/core/test_neuron.py index 110aa0f0..6fc9efe4 100644 --- a/tests/core/test_neuron.py +++ b/tests/core/test_neuron.py @@ -29,11 +29,13 @@ from copy import copy, deepcopy from pathlib import Path +import pytest import neurom as nm import numpy as np import morphio from neurom.core.morphology import Morphology, graft_morphology, iter_segments from numpy.testing import assert_array_equal +from neurom.exceptions import NeuroMError SWC_PATH = Path(__file__).parent.parent / 'data/swc/' @@ -137,3 +139,8 @@ def test_str(): n = nm.load_morphology(SWC_PATH / 'simple.swc') assert 'Morphology' in str(n) assert 'Section' in str(n.neurites[0].root_node) + + +def test_morphology_raises_wrong_argument(): + with pytest.raises(NeuroMError, match="Expected morphio Morphology object but got: my-path"): + Morphology("my-path")