<commit_before>"""Unit tests for the jicbioimage.segment package."""

import unittest

import numpy as np


class SegmentTests(unittest.TestCase):

    def setUp(self):
        from jicbioimage.core.io import AutoWrite
        AutoWrite.on = False

    def test_version_is_string(self):
        import jicbioimage.segment
        self.assertTrue(isinstance(jicbioimage.segment.__version__, str))

    def test_connected_components(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [0, 0, 2, 2, 2],
                       [0, 0, 2, 2, 2]], dtype=np.uint8)
        segmentation = connected_components(ar)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2, 3]))

    def test_connected_components_background_option(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [0, 0, 2, 2, 2],
                       [0, 0, 2, 2, 2]], dtype=np.uint8)
        segmentation = connected_components(ar, background=1)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2]))

    def test_connected_components_connectivity_option(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 1, 1, 1],
                       [0, 0, 1, 1, 1],
                       [0, 0, 1, 1, 1]], dtype=np.uint8)

        segmentation = connected_components(ar, connectivity=1)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2, 3, 4]))

        segmentation = connected_components(ar, connectivity=2)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2]))

    def test_connected_components_acts_like_a_transform(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import Image
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [0, 0, 2, 2, 2],
                       [0, 0, 2, 2, 2]], dtype=np.uint8)
        im = Image.from_array(ar)
        self.assertEqual(len(im.history), 1)
        segmentation = connected_components(im)
        self.assertEqual(len(segmentation.history), 2)
        self.assertEqual(segmentation.history[-1],
                         "Applied connected_components transform")
<commit_msg>Added more structure to the test suite.<commit_after>"""Unit tests for the jicbioimage.segment package."""

import unittest

import numpy as np

class GenericUnitTests(unittest.TestCase):

    def test_version_is_string(self):
        import jicbioimage.segment
        self.assertTrue(isinstance(jicbioimage.segment.__version__, str))

class ConnectedComponentsTests(unittest.TestCase):

    def setUp(self):
        from jicbioimage.core.io import AutoWrite
        AutoWrite.on = False

    def test_connected_components(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [0, 0, 2, 2, 2],
                       [0, 0, 2, 2, 2]], dtype=np.uint8)
        segmentation = connected_components(ar)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2, 3]))

    def test_connected_components_background_option(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [0, 0, 2, 2, 2],
                       [0, 0, 2, 2, 2]], dtype=np.uint8)
        segmentation = connected_components(ar, background=1)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2]))

    def test_connected_components_connectivity_option(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import SegmentedImage
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 1, 1, 1],
                       [0, 0, 1, 1, 1],
                       [0, 0, 1, 1, 1]], dtype=np.uint8)

        segmentation = connected_components(ar, connectivity=1)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2, 3, 4]))

        segmentation = connected_components(ar, connectivity=2)
        self.assertTrue(isinstance(segmentation, SegmentedImage))
        self.assertEqual(segmentation.identifiers, set([1, 2]))

    def test_connected_components_acts_like_a_transform(self):
        from jicbioimage.segment import connected_components
        from jicbioimage.core.image import Image
        ar = np.array([[1, 1, 0, 0, 0],
                       [1, 1, 0, 0, 0],
                       [0, 0, 0, 0, 0],
                       [0, 0, 2, 2, 2],
                       [0, 0, 2, 2, 2]], dtype=np.uint8)
        im = Image.from_array(ar)
        self.assertEqual(len(im.history), 1)
        segmentation = connected_components(im)
        self.assertEqual(len(segmentation.history), 2)
        self.assertEqual(segmentation.history[-1],
                         "Applied connected_components transform")

{'fraction_non_alphanumeric': 0.11313098522967051, 'fraction_numerical': 0.039279337769842555, 'mean_word_length': 2.1390728476821192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2239158', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1773, 'n_words': 452}