Skip to content

Commit

Permalink
Fix warnings in tests (#1093)
Browse files Browse the repository at this point in the history
* Replace np.alltrue with np.all
* Remove return statements from test
* Use set_layout_engine instead of set_tight_layout
* Silence precision warnings in comparison test with mock data
* Use importlib instead of pkg_resources
* Silence expected warnings in tests
* Filter cli soma volume warning
  • Loading branch information
eleftherioszisis authored Jan 25, 2024
1 parent 22d3e26 commit 3347693
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 28 deletions.
12 changes: 11 additions & 1 deletion neurom/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Helper code for neurom applications."""
import sys
import logging

from pathlib import Path
import yaml

from neurom.exceptions import ConfigError

if sys.version_info >= (3, 9): # pragma: no cover
import importlib.resources as importlib_resources
else:
import importlib_resources # pragma: no cover

EXAMPLE_CHECK_CONFIG = Path(importlib_resources.files("neurom.apps"), "config", "morph_check.yaml")
EXAMPLE_STATS_CONFIG = Path(importlib_resources.files("neurom.apps"), "config", "morph_stats.yaml")

L = logging.getLogger(__name__)


Expand Down
6 changes: 3 additions & 3 deletions neurom/apps/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import click
import matplotlib.pyplot as plt

from neurom.apps import morph_stats, morph_check
from neurom.apps import morph_stats, morph_check, EXAMPLE_CHECK_CONFIG, EXAMPLE_STATS_CONFIG
from neurom import load_morphology
from neurom.view import matplotlib_impl, matplotlib_utils

Expand Down Expand Up @@ -85,7 +85,7 @@ def view(input_file, is_3d, plane, backend, realistic_diameters):
'https://neurom.readthedocs.io/en/latest/morph_stats.html')
@click.argument('datapath', required=False)
@click.option('-C', '--config', type=click.Path(exists=True, dir_okay=False),
default=morph_stats.EXAMPLE_CONFIG, show_default=True,
default=EXAMPLE_STATS_CONFIG, show_default=True,
help='Configuration File')
@click.option('-o', '--output', type=click.Path(exists=False, dir_okay=False),
help='Path to output file, if it ends in .json, a json file is created,'
Expand All @@ -106,7 +106,7 @@ def stats(datapath, config, output, full_config, as_population, ignored_exceptio
'https://neurom.readthedocs.io/en/latest/morph_check.html')
@click.argument('datapath')
@click.option('-C', '--config', type=click.Path(exists=True, dir_okay=False),
default=morph_check.EXAMPLE_CONFIG, show_default=True,
default=EXAMPLE_CHECK_CONFIG, show_default=True,
help='Configuration File')
@click.option('-o', '--output', type=click.Path(exists=False, dir_okay=False),
help='Path to output json summary file', required=True)
Expand Down
8 changes: 2 additions & 6 deletions neurom/apps/morph_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@
"""Check on morphologies."""

import json
from pathlib import Path
import pkg_resources
from neurom.apps import get_config
from neurom.apps import get_config, EXAMPLE_CHECK_CONFIG
from neurom.check.runner import CheckRunner

EXAMPLE_CONFIG = Path(pkg_resources.resource_filename('neurom.apps', 'config'), 'morph_check.yaml')


def main(datapath, config, output):
"""Main function that checks morphologies.
Expand All @@ -45,7 +41,7 @@ def main(datapath, config, output):
config (str|Path): path to a statistics config file
output (str|Path): path to output the resulted checks file
"""
config = get_config(config, EXAMPLE_CONFIG)
config = get_config(config, EXAMPLE_CHECK_CONFIG)
checker = CheckRunner(config)
summary = checker.run(datapath)
with open(output, 'w') as json_output:
Expand Down
12 changes: 5 additions & 7 deletions neurom/apps/morph_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@
from collections.abc import Sized
from copy import deepcopy
from functools import partial
from pathlib import Path
import pkg_resources

import numpy as np
import pandas as pd
from morphio import SomaError

import neurom as nm
from neurom.apps import get_config
from neurom.apps import get_config, EXAMPLE_STATS_CONFIG
from neurom.core.morphology import Morphology, Neurite
from neurom.core.population import Population
from neurom.exceptions import ConfigError
Expand All @@ -56,7 +55,6 @@

L = logging.getLogger(__name__)

EXAMPLE_CONFIG = Path(pkg_resources.resource_filename('neurom.apps', 'config'), 'morph_stats.yaml')
IGNORABLE_EXCEPTIONS = {'SomaError': SomaError}


Expand Down Expand Up @@ -117,7 +115,7 @@ def extract_dataframe(morphs, config, n_workers=1):
return pd.DataFrame(columns=pd.MultiIndex.from_tuples(columns), data=rows)


extract_dataframe.__doc__ += str(EXAMPLE_CONFIG)
extract_dataframe.__doc__ += str(EXAMPLE_STATS_CONFIG)


def _get_feature_stats(feature_name, morphs, modes, kwargs):
Expand Down Expand Up @@ -234,7 +232,7 @@ def extract_stats(morphs, config):
return dict(stats)


extract_stats.__doc__ += str(EXAMPLE_CONFIG)
extract_stats.__doc__ += str(EXAMPLE_STATS_CONFIG)


def _get_header(results):
Expand Down Expand Up @@ -366,7 +364,7 @@ def main(datapath, config, output_file, is_full_config, as_population, ignored_e
as_population (bool): treat ``datapath`` as directory of morphologies population
ignored_exceptions (list|tuple|None): exceptions to ignore when loading a morphology
"""
config = full_config() if is_full_config else get_config(config, EXAMPLE_CONFIG)
config = full_config() if is_full_config else get_config(config, EXAMPLE_STATS_CONFIG)

if 'neurite' in config and 'neurite_type' not in config:
error = ConfigError('"neurite_type" missing from config, but "neurite" set')
Expand Down
2 changes: 1 addition & 1 deletion neurom/view/matplotlib_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def plot_style(fig, ax, # pylint: disable=too-many-arguments, too-many-locals
ax.set_aspect(aspect_ratio)

if tight:
fig.set_tight_layout(True)
fig.set_layout_engine("tight")


def plot_title(ax, pretitle='', title='Figure', posttitle='', title_fontsize=14, title_arg=None):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
'pyyaml>=3.10',
'scipy>=1.2.0',
'tqdm>=4.8.4',
'importlib_resources>=1.3; python_version < "3.9"',
]
dynamic = ["version"]

Expand Down
2 changes: 2 additions & 0 deletions tests/apps/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from pathlib import Path
import tempfile
import pytest

import pandas as pd
import yaml
Expand Down Expand Up @@ -73,6 +74,7 @@ def test_morph_stat():
'all:max_section_branch_orders', 'morphology:mean_soma_radius'}


@pytest.mark.filterwarnings('ignore::UserWarning')
def test_morph_stat_full_config():
runner = CliRunner()
filename = DATA / 'h5/v1/Neuron.h5'
Expand Down
8 changes: 7 additions & 1 deletion tests/apps/test_morph_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ def test_stats_new_format_set_arg():


def test_extract_stats_scalar_feature():

m = nm.load_morphology(DATA_PATH / 'neurolucida' / 'bio_neuron-000.asc')

config = {
'neurite_type': ['ALL'],
'neurite': {
Expand All @@ -196,7 +198,11 @@ def test_extract_stats_scalar_feature():
'soma_volume': ['sum'],
}
}
res = ms.extract_stats(m, config)
with warnings.catch_warnings():
# silence warning about approximating soma volume with a sphere
warnings.simplefilter("ignore", category=UserWarning)
res = ms.extract_stats(m, config)

assert res == {'all': {'max_number_of_forking_points': 277},
'morphology': {'sum_soma_volume': 1424.4383771584492}}

Expand Down
4 changes: 2 additions & 2 deletions tests/geom/test_geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_bounding_box():
obj = PointObj()
obj.points = pts

assert np.alltrue(geom.bounding_box(obj) == [[-100, -2, -3], [42, 55, 33]])
assert np.all(geom.bounding_box(obj) == [[-100, -2, -3], [42, 55, 33]])


def test_bounding_box_morphology():
Expand All @@ -78,7 +78,7 @@ def test_convex_hull_points():
# This leverages scipy ConvexHull and we don't want
# to re-test scipy, so simply check that the points are the same.
hull = geom.convex_hull(NRN)
assert np.alltrue(hull.points == NRN.points[:, :3])
assert np.all(hull.points == NRN.points[:, :3])


def test_convex_hull_volume():
Expand Down
6 changes: 1 addition & 5 deletions tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

def test_import_neurom():
try:
import neurom
return True
except Exception:
return False
import neurom
5 changes: 4 additions & 1 deletion tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
Since the stats module consists of simple wrappers to scipy.stats functions,
these tests are only sanity checks.
"""

import numpy as np
from neurom import stats as st

Expand Down Expand Up @@ -201,11 +200,15 @@ def test_compare_two_wilcoxon():
assert_almost_equal(results2.dist, 0.0, decimal=5)
assert_almost_equal(results2.pvalue, 0.0, decimal=5)


@pytest.mark.filterwarnings("ignore") # Ignore precision warnings
def test_compare_two_ttest():

results1 = st.compare_two(distr1, distr1, test=st.StatTests.ttest)

assert np.isnan(results1.dist)
assert np.isnan(results1.pvalue)

results2 = st.compare_two(distr1, distr2, test=st.StatTests.ttest)
assert np.isinf(results2.dist)
assert_almost_equal(results2.pvalue, 0.0, decimal=5)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import warnings
import tempfile
from pathlib import Path

Expand All @@ -35,7 +36,14 @@
if 'DISPLAY' not in os.environ: # noqa
matplotlib.use('Agg') # noqa

from neurom import NeuriteType, load_morphology, viewer
from neurom.exceptions import NeuroMDeprecationWarning

with warnings.catch_warnings():
# no need to warn about the deprecated module in this test
warnings.simplefilter("ignore", category=NeuroMDeprecationWarning)
from neurom import viewer

from neurom import NeuriteType, load_morphology
from neurom.view import matplotlib_utils

import pytest
Expand Down

0 comments on commit 3347693

Please sign in to comment.