# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project
# All rights reserved.
#
# This file is part of NeuroM <https://github.com/BlueBrain/NeuroM>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#     3. Neither the name of the copyright holder nor the names of
#        its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
from neurom.check import structural_checks as chk
from neurom import io
from nose import tools as nt


_path = os.path.dirname(os.path.abspath(__file__))
DATA_PATH = os.path.join(_path, '../../../test_data')
SWC_PATH = os.path.join(DATA_PATH, 'swc')
H5V1_PATH = os.path.join(DATA_PATH, 'h5/v1')


class TestIOCheckFST(object):
    def setup(self):
        self.load_data = io.load_data

    def test_has_sequential_ids_good_data(self):

        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical_no_soma.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc',
                           'Neuron_zero_radius.swc',
                           'sequential_trunk_off_0_16pt.swc',
                           'sequential_trunk_off_1_16pt.swc',
                           'sequential_trunk_off_42_16pt.swc',
                           'Neuron_no_missing_ids_no_zero_segs.swc']
                 ]

        for f in files:
            ok = chk.has_sequential_ids(self.load_data(f))
            nt.ok_(ok)
            nt.ok_(len(ok.info) == 0)

    def test_has_sequential_ids_bad_data(self):

        f = os.path.join(SWC_PATH, 'Neuron_missing_ids.swc')

        ok = chk.has_sequential_ids(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 217, 428, 639])

    def test_has_increasing_ids_good_data(self):

        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical_no_soma.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc',
                           'Neuron_zero_radius.swc',
                           'sequential_trunk_off_0_16pt.swc',
                           'sequential_trunk_off_1_16pt.swc',
                           'sequential_trunk_off_42_16pt.swc',
                           'Neuron_no_missing_ids_no_zero_segs.swc']
                 ]

        for f in files:
            ok = chk.has_increasing_ids(self.load_data(f))
            nt.ok_(ok)
            nt.ok_(len(ok.info) == 0)

    def test_has_increasing_ids_bad_data(self):

        f = os.path.join(SWC_PATH, 'non_increasing_trunk_off_1_16pt.swc')

        ok = chk.has_increasing_ids(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 12])

    def test_is_single_tree_bad_data(self):

        f = os.path.join(SWC_PATH, 'Neuron_disconnected_components.swc')

        ok = chk.is_single_tree(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 217, 428, 639])

    def test_is_single_tree_good_data(self):

        f = os.path.join(SWC_PATH, 'Neuron.swc')

        ok = chk.is_single_tree(self.load_data(f))
        nt.ok_(ok)
        nt.eq_(len(ok.info), 0)

    def test_has_no_missing_parents_bad_data(self):

        f = os.path.join(SWC_PATH, 'Neuron_missing_parents.swc')

        ok = chk.no_missing_parents(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 217, 428, 639])

    def test_has_no_missing_parents_good_data(self):

        f = os.path.join(SWC_PATH, 'Neuron.swc')

        ok = chk.no_missing_parents(self.load_data(f))
        nt.ok_(ok)
        nt.eq_(len(ok.info), 0)

    def test_has_soma_points_good_data(self):
        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc']]

        files.append(os.path.join(H5V1_PATH, 'Neuron_2_branch.h5'))

        for f in files:
            nt.ok_(chk.has_soma_points(self.load_data(f)))

    def test_has_soma_points_bad_data(self):
        f = os.path.join(SWC_PATH, 'Single_apical_no_soma.swc')
        nt.ok_(not chk.has_soma_points(self.load_data(f)))

    def test_has_valid_soma_good_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Neuron.swc'))
        nt.ok_(chk.has_valid_soma(dw))
        dw = self.load_data(os.path.join(H5V1_PATH, 'Neuron.h5'))
        nt.ok_(chk.has_valid_soma(dw))

    def test_has_valid_soma_bad_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Single_apical_no_soma.swc'))
        nt.ok_(not chk.has_valid_soma(dw))

    def test_has_finite_radius_neurites_good_data(self):
        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc']]

        files.append(os.path.join(H5V1_PATH, 'Neuron_2_branch.h5'))

        for f in files:
            ok = chk.has_all_finite_radius_neurites(self.load_data(f))
            nt.ok_(ok)
            nt.ok_(len(ok.info) == 0)

    def test_has_finite_radius_neurites_bad_data(self):
        f = os.path.join(SWC_PATH, 'Neuron_zero_radius.swc')
        ok = chk.has_all_finite_radius_neurites(self.load_data(f))
        nt.ok_(not ok)
        nt.ok_(list(ok.info) == [194, 210, 246, 304, 493])

    def test_has_no_missing_parents_bad_data(self):
        try:
            return super(TestIOCheckFST, self).test_has_no_missing_parents_bad_data()
        except Exception:
            return False

    def test_has_sequential_ids_bad_data(self):
        try:
            return super(TestIOCheckFST, self).test_has_sequential_ids_bad_data()
        except Exception:
            return False

    def test_has_valid_neurites_good_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Neuron.swc'))
        nt.ok_(chk.has_valid_neurites(dw))
        dw = self.load_data(os.path.join(H5V1_PATH, 'Neuron.h5'))
        nt.ok_(chk.has_valid_neurites(dw))

    def test_has_valid_neurites_bad_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Soma_origin.swc'))
        nt.ok_(not chk.has_valid_neurites(dw))

# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project
# All rights reserved.
#
# This file is part of NeuroM <https://github.com/BlueBrain/NeuroM>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#     3. Neither the name of the copyright holder nor the names of
#        its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
from neurom.check import structural_checks as chk
from neurom import io
from nose import tools as nt


_path = os.path.dirname(os.path.abspath(__file__))
DATA_PATH = os.path.join(_path, '../../../test_data')
SWC_PATH = os.path.join(DATA_PATH, 'swc')
H5V1_PATH = os.path.join(DATA_PATH, 'h5/v1')


class TestIOCheckFST(object):
    def setup(self):
        self.load_data = io.load_data

    def test_has_sequential_ids_good_data(self):

        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical_no_soma.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc',
                           'Neuron_zero_radius.swc',
                           'sequential_trunk_off_0_16pt.swc',
                           'sequential_trunk_off_1_16pt.swc',
                           'sequential_trunk_off_42_16pt.swc',
                           'Neuron_no_missing_ids_no_zero_segs.swc']
                 ]

        for f in files:
            ok = chk.has_sequential_ids(self.load_data(f))
            nt.ok_(ok)
            nt.ok_(len(ok.info) == 0)

    def test_has_sequential_ids_bad_data(self):

        f = os.path.join(SWC_PATH, 'Neuron_missing_ids.swc')

        ok = chk.has_sequential_ids(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 217, 428, 639])

    def test_has_increasing_ids_good_data(self):

        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical_no_soma.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc',
                           'Neuron_zero_radius.swc',
                           'sequential_trunk_off_0_16pt.swc',
                           'sequential_trunk_off_1_16pt.swc',
                           'sequential_trunk_off_42_16pt.swc',
                           'Neuron_no_missing_ids_no_zero_segs.swc']
                 ]

        for f in files:
            ok = chk.has_increasing_ids(self.load_data(f))
            nt.ok_(ok)
            nt.ok_(len(ok.info) == 0)

    def test_has_increasing_ids_bad_data(self):

        f = os.path.join(SWC_PATH, 'non_increasing_trunk_off_1_16pt.swc')

        ok = chk.has_increasing_ids(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 12])

    def test_is_single_tree_bad_data(self):

        f = os.path.join(SWC_PATH, 'Neuron_disconnected_components.swc')

        ok = chk.is_single_tree(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 217, 428, 639])

    def test_is_single_tree_good_data(self):

        f = os.path.join(SWC_PATH, 'Neuron.swc')

        ok = chk.is_single_tree(self.load_data(f))
        nt.ok_(ok)
        nt.eq_(len(ok.info), 0)

    def test_has_no_missing_parents_bad_data(self):

        f = os.path.join(SWC_PATH, 'Neuron_missing_parents.swc')

        ok = chk.no_missing_parents(self.load_data(f))
        nt.ok_(not ok)
        nt.eq_(list(ok.info), [6, 217, 428, 639])

    def test_has_no_missing_parents_good_data(self):

        f = os.path.join(SWC_PATH, 'Neuron.swc')

        ok = chk.no_missing_parents(self.load_data(f))
        nt.ok_(ok)
        nt.eq_(len(ok.info), 0)

    def test_has_soma_points_good_data(self):
        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc']]

        files.append(os.path.join(H5V1_PATH, 'Neuron_2_branch.h5'))

        for f in files:
            nt.ok_(chk.has_soma_points(self.load_data(f)))

    def test_has_soma_points_bad_data(self):
        f = os.path.join(SWC_PATH, 'Single_apical_no_soma.swc')
        nt.ok_(not chk.has_soma_points(self.load_data(f)))

    def test_has_valid_soma_good_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Neuron.swc'))
        nt.ok_(chk.has_valid_soma(dw))
        dw = self.load_data(os.path.join(H5V1_PATH, 'Neuron.h5'))
        nt.ok_(chk.has_valid_soma(dw))

    def test_has_valid_soma_bad_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Single_apical_no_soma.swc'))
        nt.ok_(not chk.has_valid_soma(dw))

    def test_has_finite_radius_neurites_good_data(self):
        files = [os.path.join(SWC_PATH, f)
                 for f in ['Neuron.swc',
                           'Single_apical.swc',
                           'Single_basal.swc',
                           'Single_axon.swc']]

        files.append(os.path.join(H5V1_PATH, 'Neuron_2_branch.h5'))

        for f in files:
            ok = chk.has_all_finite_radius_neurites(self.load_data(f))
            nt.ok_(ok)
            nt.ok_(len(ok.info) == 0)

    def test_has_finite_radius_neurites_bad_data(self):
        f = os.path.join(SWC_PATH, 'Neuron_zero_radius.swc')
        ok = chk.has_all_finite_radius_neurites(self.load_data(f))
        nt.ok_(not ok)
        nt.ok_(list(ok.info) == [194, 210, 246, 304, 493])

    def test_has_no_missing_parents_bad_data(self):
        try:
            return super(TestIOCheckFST, self).test_has_no_missing_parents_bad_data()
        except Exception:
            return False

    def test_has_sequential_ids_bad_data(self):
        try:
            return super(TestIOCheckFST, self).test_has_sequential_ids_bad_data()
        except Exception:
            return False

    def test_has_valid_neurites_good_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Neuron.swc'))
        nt.ok_(chk.has_valid_neurites(dw))
        dw = self.load_data(os.path.join(H5V1_PATH, 'Neuron.h5'))
        nt.ok_(chk.has_valid_neurites(dw))

    def test_has_valid_neurites_bad_data(self):
        dw = self.load_data(os.path.join(SWC_PATH, 'Soma_origin.swc'))
        nt.ok_(not chk.has_valid_neurites(dw))

# Copyright (c) 2015, Ecole Polytechnique Federale de Lausanne, Blue Brain Project # All rights reserved. # # This file is part of NeuroM <https://github.com/BlueBrain/NeuroM> # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # #     1. Redistributions of source code must retain the above copyright #        notice, this list of conditions and the following disclaimer. #     2. Redistributions in binary form must reproduce the above copyright #        notice, this list of conditions and the following disclaimer in the #        documentation and/or other materials provided with the distribution. #     3. Neither the name of the copyright holder nor the names of #        its contributors may be used to endorse or promote products #        derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.