Exemple #1
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
def test_refresh_text():
    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)

    new_classes = np.array(['D', 'E', 'F'])
    new_properties = {
        'class': new_classes,
        'confidence': np.array([0.5, 0.3, 1]),
    }
    text_manager.refresh_text(new_properties)
    np.testing.assert_equal(new_classes, text_manager.values)
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)