Skip to content

Commit

Permalink
Reorganize checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Oct 7, 2022
1 parent f7f382c commit f0a3128
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 47 deletions.
15 changes: 2 additions & 13 deletions neurots/generate/algorithms/tmdgrower.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from neurots.generate.algorithms.common import section_data
from neurots.morphmath import sample
from neurots.morphmath.utils import norm
from neurots.preprocess.relevancy_checkers import check_min_bar_length

L = logging.getLogger(__name__)

Expand Down Expand Up @@ -65,19 +66,7 @@ def __init__(
self.persistence_length = self.barcode.get_persistence_length()
# Validate parameters and distributions
if not skip_validation:
self.check_min_bar_length(params, input_data)

@staticmethod
def check_min_bar_length(params, distrs):
"""Consistency check between parameters - persistence diagram."""
barSZ = distrs["min_bar_length"]
stepSZ = params["step_size"]["norm"]["mean"]
if stepSZ >= barSZ:
L.warning(
"Selected step size %f is too big for bars of size %f",
stepSZ,
barSZ,
)
check_min_bar_length(params, input_data, start_point, context)

def select_persistence(self, input_data, random_generator=np.random):
"""Select the persistence.
Expand Down
20 changes: 19 additions & 1 deletion neurots/preprocess/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
"""Functions used for preprocessing."""
from neurots.preprocess import checkers # noqa

# Copyright (C) 2022 Blue Brain Project, EPFL
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from neurots.preprocess import relevancy_checkers # noqa
from neurots.preprocess import validity_checkers # noqa
from neurots.preprocess.utils import preprocess_inputs # noqa
from neurots.preprocess.utils import register_preprocess # noqa
from neurots.preprocess.utils import register_validator # noqa
30 changes: 0 additions & 30 deletions neurots/preprocess/checkers.py

This file was deleted.

28 changes: 28 additions & 0 deletions neurots/preprocess/preprocessors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Functions to modify the given parameters and distributions before running the algorithm.
The functions used as preprocesses should have a name like 'preprocess_*' and have the following
signature: `check_something(params, distrs)`. These functions should be very generic and should
not depend on any context in which the related grower is used.
The preprocesses should be registered to be executed in the preprocess step using the
`@register_preprocess` decorator.
"""

# Copyright (C) 2022 Blue Brain Project, EPFL
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

# TODO: remove the next line and the 'noqa' when a preprocess is added
# pylint: disable=unused-import
from neurots.preprocess.utils import register_preprocess # noqa
39 changes: 39 additions & 0 deletions neurots/preprocess/relevancy_checkers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Functions to check that the given parameters and distributions will given relevant results.
The functions used as relevancy checkers should have a name like 'check_*' and have the following
signature: `check_something(params, distrs, start_point=None, context=None)`. The `start_point` and
`context` parameters should always be optional, as they will not be known during the preprocessing
step.
"""

# Copyright (C) 2022 Blue Brain Project, EPFL
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging

L = logging.getLogger(__name__)


def check_min_bar_length(params, distrs, start_point=None, context=None):
"""Consistency check between parameters - persistence diagram."""
# pylint: disable=unused-argument
barSZ = distrs["min_bar_length"]
stepSZ = params["step_size"]["norm"]["mean"]
if stepSZ >= barSZ:
L.warning(
"Selected step size %f is too big for bars of size %f",
stepSZ,
barSZ,
)
40 changes: 37 additions & 3 deletions neurots/preprocess/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
"""Some utils for preprocessing."""

# Copyright (C) 2022 Blue Brain Project, EPFL
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from collections import defaultdict
from copy import deepcopy
from itertools import chain

from neurots.validator import validate_neuron_distribs
from neurots.validator import validate_neuron_params

_PREPROCESS_FUNCTIONS = defaultdict(set)
_PREPROCESS_FUNCTIONS = {
"preprocess": defaultdict(set),
"validator": defaultdict(set),
}


def register_preprocess(*growth_methods):
"""Register a preprocess function."""

def inner(func):
for i in growth_methods:
_PREPROCESS_FUNCTIONS[i].add(func)
_PREPROCESS_FUNCTIONS["preprocess"][i].add(func)
return func

return inner


def register_validator(*growth_methods):
"""Register a validation function."""

def inner(func):
for i in growth_methods:
_PREPROCESS_FUNCTIONS["validator"][i].add(func)
return func

return inner
Expand All @@ -28,7 +59,10 @@ def preprocess_inputs(params, distrs):

for grow_type in params["grow_types"]:
growth_method = params[grow_type]["growth_method"]
for preprocess_func in _PREPROCESS_FUNCTIONS[growth_method]:
for preprocess_func in chain(
_PREPROCESS_FUNCTIONS["validator"][growth_method],
_PREPROCESS_FUNCTIONS["preprocess"][growth_method],
):
preprocess_func(params[grow_type], distrs[grow_type])

return params, distrs
54 changes: 54 additions & 0 deletions neurots/preprocess/validity_checkers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Functions to check that the given parameters and distributions will not break the algorithm.
The functions used as validity checkers should have a name like 'check_*' and have the following
signature: `check_something(params, distrs)`. These functions should be very generic and should
not depend on any context in which the related grower is used.
The checkers should be registered to be executed in the preprocess step using the
`@register_validator` decorator.
"""

# Copyright (C) 2022 Blue Brain Project, EPFL
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from neurots.preprocess.relevancy_checkers import check_min_bar_length
from neurots.preprocess.utils import register_validator


@register_validator("trunk")
def check_num_seg(params, distrs):
"""Check that params contains a 'num_seg' entry."""
# pylint: disable=unused-argument
if "num_seg" not in params:
raise KeyError(
"The parameters must contain a 'num_seg' entry when the "
"'growth_method' entry in parameters is 'trunk'."
)


@register_validator("tmd", "tmd_apical", "tmd_gradient")
def check_bar_length(params, distrs):
"""Check consistency between parameters and persistence diagram."""
if "min_bar_length" not in distrs:
raise KeyError(
"The distributions must contain a 'min_bar_length' entry when the "
"'growth_method' entry in parameters is in ['tmd', 'tmd_apical', 'tmd_gradient']."
)
if "mean" not in params.get("step_size", {}).get("norm", {}):
raise KeyError(
"The parameters must contain a 'step_size' entry when the "
"'growth_method' entry in parameters is in ['tmd', 'tmd_apical', 'tmd_gradient']."
)
check_min_bar_length(params, distrs)

0 comments on commit f0a3128

Please sign in to comment.