Пример #1
0
def test_text_manager_format():
    n_text = 3
    text = 'confidence: {confidence:.2f}'
    classes = np.array(['A', 'B', 'C'])
    properties = {'class': classes, 'confidence': np.array([0.5, 0.3, 1])}
    expected_text = np.array(
        ['confidence: 0.50', 'confidence: 0.30', 'confidence: 1.00']
    )
    text_manager = TextManager(text=text, n_text=n_text, properties=properties)
    np.testing.assert_equal(text_manager.values, expected_text)
    assert text_manager._mode == TextMode.FORMATTED

    # add new text with properties
    new_properties = {'class': np.array(['A']), 'confidence': np.array([0.5])}
    text_manager.add(new_properties, 1)
    expected_text_2 = np.concatenate([expected_text, ['confidence: 0.50']])
    np.testing.assert_equal(text_manager.values, expected_text_2)

    # test getting the text elements when there are none in view
    text_view = text_manager.view_text([])
    np.testing.assert_equal(text_view, [''])

    # test getting the text elements when the first two elements are in view
    text_view = text_manager.view_text([0, 1])
    np.testing.assert_equal(text_view, expected_text_2[0:2])

    text_manager.anchor = 'center'
    coords = np.array([[0, 0], [10, 10], [20, 20]])
    text_coords = text_manager.compute_text_coords(coords, ndisplay=3)
    np.testing.assert_equal(text_coords, (coords, 'center', 'center'))

    # remove the first text element
    text_manager.remove({0})
    np.testing.assert_equal(text_manager.values, expected_text_2[1::])
Пример #2
0
def test_add_with_text_constant_init_empty_then_ignored():
    # TODO: we may choose not to ignore add as part of the properties refactor.
    properties = {'class': np.array(['A', 'B', 'C'])}
    text_manager = TextManager(text='point', n_text=0, properties=properties)

    text_manager.add({'class': np.array(['C'])}, 2)

    assert len(text_manager.values) == 0
Пример #3
0
def test_add_with_text_constant_init_empty():
    properties = {}
    text_manager = TextManager(text={'constant': 'point'},
                               n_text=0,
                               properties=properties)

    text_manager.add({'class': np.array(['C'])}, 2)

    np.testing.assert_array_equal(text_manager.values, ['point'] * 2)
Пример #4
0
class TextManagerSuite:
    """Benchmarks for creating and modifying a text manager."""

    param_names = ['n', 'string']
    params = [
        [2**i for i in range(4, 18, 2)],
        [
            {
                'constant': 'test'
            },
            'string_property',
            'float_property',
            '{string_property}: {float_property:.2f}',
        ],
    ]

    def setup(self, n, string):
        np.random.seed(0)
        categories = ('cat', 'car')
        self.features = pd.DataFrame({
            'string_property':
            pd.Series(
                np.random.choice(categories, n),
                dtype=pd.CategoricalDtype(categories),
            ),
            'float_property':
            np.random.rand(n),
        })
        self.current_properties = self.features.iloc[[-1]].to_dict('list')
        self.manager = TextManager(string=string, features=self.features)
        self.indices_to_remove = list(range(0, n, 2))

    def time_create(self, n, string):
        TextManager(string=string, features=self.features)

    def time_refresh(self, n, string):
        self.manager.refresh_text(self.features)

    def time_add_iteratively(self, n, string):
        for _ in range(512):
            self.manager.add(self.current_properties, 1)

    def time_remove_as_batch(self, n, string):
        self.manager.remove(self.indices_to_remove)

    # `time_remove_as_batch` can only run once per instance;
    # otherwise it fails because the indices were already removed:
    #
    #   IndexError: index 32768 is out of bounds for axis 0 with size 32768
    #
    # Why? ASV will run the same function after setup several times in two
    # occasions: warmup and timing itself. We disable warmup and only
    # allow one execution per state with these method-specific options:
    time_remove_as_batch.number = 1
    time_remove_as_batch.warmup_time = 0
Пример #5
0
def test_add_many_text_formatted():
    properties = {'confidence': np.empty(0, dtype=float)}
    text_manager = TextManager(
        text='confidence: {confidence:.2f}',
        n_text=0,
        properties=properties,
    )

    text_manager.add({'confidence': np.array([0.5])}, 2)

    np.testing.assert_equal(text_manager.values, ['confidence: 0.50'] * 2)
Пример #6
0
def test_add_with_text_constant():
    n_text = 3
    properties = {'class': np.array(['A', 'B', 'C'])}
    text_manager = TextManager(text={'constant': 'point'},
                               n_text=n_text,
                               properties=properties)
    np.testing.assert_array_equal(text_manager.values, ['point'] * 3)

    text_manager.add({'class': np.array(['C'])}, 2)

    np.testing.assert_array_equal(text_manager.values, ['point'] * 5)
Пример #7
0
def test_empty_text_manager_format():
    """Test creating an empty text manager in formatted mode.
    This is for creating an empty layer with text initialized.
    """
    properties = {'confidence': np.empty(0, dtype=float)}
    text = 'confidence: {confidence:.2f}'
    text_manager = TextManager(text=text, n_text=0, properties=properties)
    assert text_manager.values.size == 0

    # add a text element
    new_properties = {'confidence': np.array([0.5])}
    text_manager.add(new_properties, 1)
    np.testing.assert_equal(text_manager.values, ['confidence: 0.50'])
Пример #8
0
def test_empty_text_manager_property():
    """Test creating an empty text manager in property mode.
    This is for creating an empty layer with text initialized.
    """
    properties = {'confidence': np.empty(0, dtype=float)}
    text_manager = TextManager(
        text='confidence', n_text=0, properties=properties
    )
    assert text_manager._mode == TextMode.PROPERTY
    assert text_manager.values.size == 0

    # add a text element
    new_properties = {'confidence': np.array([0.5])}
    text_manager.add(new_properties, 1)
    np.testing.assert_equal(text_manager.values, ['0.5'])
Пример #9
0
def test_text_manager_property():
    n_text = 3
    text = 'class'
    classes = np.array(['A', 'B', 'C'])
    properties = {'class': classes, 'confidence': np.array([0.5, 0.3, 1])}
    text_manager = TextManager(text=text, n_text=n_text, properties=properties)
    np.testing.assert_equal(text_manager.values, classes)

    # add new text with properties
    new_properties = {'class': np.array(['A']), 'confidence': np.array([0.5])}
    text_manager.add(new_properties, 1)
    expected_text_2 = np.concatenate([classes, ['A']])
    np.testing.assert_equal(text_manager.values, expected_text_2)

    # remove the first text element
    text_manager.remove({0})
    np.testing.assert_equal(text_manager.values, expected_text_2[1::])
Пример #10
0
class TextManagerSuite:
    """Benchmarks for creating and modifying a text manager."""

    param_names = ['n', 'text']
    params = [
        [2**i for i in range(4, 18, 2)],
        [
            None,
            'constant',
            'string_property',
            'float_property',
            '{string_property}: {float_property:.2f}',
        ],
    ]

    def setup(self, n, text):
        np.random.seed(0)
        self.properties = {
            'string_property': np.random.choice(('cat', 'car'), n),
            'float_property': np.random.rand(n),
        }
        self.current_properties = {
            k: np.array([v[-1]])
            for k, v in self.properties.items()
        }
        self.manager = TextManager(n_text=n,
                                   properties=self.properties,
                                   text=text)
        self.indices_to_remove = list(range(0, n, 2))

    def time_create(self, n, text):
        TextManager(n_text=n, properties=self.properties, text=text)

    def time_refresh(self, n, text):
        self.manager.refresh_text(self.properties)

    def time_add_iteratively(self, n, text):
        for _ in range(512):
            self.manager.add(self.current_properties, 1)

    def time_remove_as_batch(self, n, text):
        self.manager.remove(self.indices_to_remove)