Best Python code snippet using locust
get_bounds_test.py
Source: get_bounds_test.py 
1# coding=utf-82# Copyright 2021 The Google Research Authors.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15"""Tests for aqt.jax.get_bounds."""16from absl.testing import absltest17from absl.testing import parameterized18import flax19from jax import random20import jax.numpy as jnp21import numpy as onp22from aqt.jax import get_bounds23from aqt.jax import quant_config24from aqt.jax import test_utils25test_utils.configure_jax()26class GetBoundsTest(parameterized.TestCase):27  def setUp(self):28    super(GetBoundsTest, self).setUp()29    self.rng = random.PRNGKey(0)30    key1, key2 = random.split(self.rng)31    self.key2 = key232    self.x = random.normal(key1, (4, 3, 2))33    self.x2 = jnp.ones((4, 3, 2))34    self.hyperparam = get_bounds.GetBounds.Hyper(35        initial_bound=6.0,36        stddev_coeff=2.0,37        absdev_coeff=1.5,38        mix_coeff=0.7,39        reset_stats=False,40        granularity=quant_config.QuantGranularity.per_channel)41  def init_model(self,42                 update_bounds,43                 update_stats=True,44                 reset_stats=False,45                 use_cams=False,46                 granularity=quant_config.QuantGranularity.per_tensor,47                 ema_coeff=None):48    self.hyperparam = get_bounds.GetBounds.Hyper(49        initial_bound=self.hyperparam.initial_bound,50        stddev_coeff=self.hyperparam.stddev_coeff,51        absdev_coeff=self.hyperparam.absdev_coeff,52        mix_coeff=self.hyperparam.mix_coeff,53        reset_stats=reset_stats,54        use_cams=use_cams,55        ema_coeff=ema_coeff,56        granularity=granularity)57    gb_bounds_params = get_bounds.GetBounds.Params(58        update_bounds=update_bounds, update_stats=update_stats)59    bounds_module = get_bounds.GetBounds(hyper=self.hyperparam)60    init_state = bounds_module.init(61        self.key2, self.x, bounds_params=gb_bounds_params)62    return bounds_module, init_state, gb_bounds_params63  # TODO(shivaniagrawal): parametrize test for different values of axis64  @parameterized.named_parameters(65      dict(testcase_name='update_bound', update_bound=True),66      dict(testcase_name='do_not_update', update_bound=False),67  )68  def test_get_bounds_init(self, update_bound):69    _, init_state, _ = self.init_model(update_bound)70    init_state_stats = init_state['get_bounds']['stats']71    onp.testing.assert_array_equal(init_state_stats.n, 0)72    onp.testing.assert_array_equal(init_state_stats.mean, 0)73    onp.testing.assert_array_equal(init_state_stats.mean_abs, 0)74    onp.testing.assert_array_equal(init_state_stats.mean_sq, 0)75    onp.testing.assert_array_equal(init_state['get_bounds']['bounds'], 6.)76  # TODO(shivaniagrawal): more elaborate testing here as follows:77  # - run with (update_stats, update_bounds) = (False, False)78  # check that neither state changed79  # - run with (update_stats, update_bounds) = (True, False)80  # check that stats.n increased, bound unchanged81  # - run with (update_stats, update_bounds) = (False, True)82  # check that stats.n unchanged but bound updated.83  # - run again with (update_stats, update_bounds) = (False, True)84  # check that both unchanged (update_bounds is idempotent)85  # - run again with (update_stats, update_bounds) = (True, True)86  # check that both changed.87  @parameterized.named_parameters(88      dict(89          testcase_name='update_bound_reset_stats',90          update_bound=True,91          reset_stats=True),92      dict(93          testcase_name='no_update_bound_reset_stats',94          update_bound=False,95          reset_stats=True),96      dict(97          testcase_name='update_bound_no_reset_stats',98          update_bound=True,99          reset_stats=False),100      dict(101          testcase_name='no_update_bound_no_reset_stats',102          update_bound=False,103          reset_stats=False),104  )105  def test_update_stats(self, update_bound, reset_stats):106    model, init_state, params = self.init_model(107        update_bound,108        reset_stats=reset_stats,109        granularity=quant_config.QuantGranularity.per_tensor)110    _, state_0 = model.apply(111        init_state, self.x, bounds_params=params, mutable='get_bounds')112    stats_0_stats = state_0['get_bounds']['stats']113    if reset_stats and update_bound:114      onp.testing.assert_array_equal(stats_0_stats.n, 0)115    else:116      onp.testing.assert_array_equal(stats_0_stats.n, 1)117    _, state = model.apply(118        state_0, self.x2, bounds_params=params, mutable='get_bounds')119    stats = state['get_bounds']['stats']120    if reset_stats and update_bound:121      onp.testing.assert_array_equal(stats.n, 0)122      expected_updated_mean = 0.123      onp.testing.assert_array_equal(expected_updated_mean, stats.mean)124    else:125      onp.testing.assert_array_equal(stats.n, 2)126      expected_updated_mean = 1 / 2 * (1 + (stats_0_stats.mean))127      onp.testing.assert_array_equal(expected_updated_mean, stats.mean)128  @parameterized.named_parameters(129      dict(130          testcase_name='update_bound_reset_stats',131          update_bound=True,132          reset_stats=True),133      dict(134          testcase_name='no_update_bound_reset_stats',135          update_bound=False,136          reset_stats=True),137      dict(138          testcase_name='update_bound_no_reset_stats',139          update_bound=True,140          reset_stats=False),141      dict(142          testcase_name='no_update_bound_no_reset_stats',143          update_bound=False,144          reset_stats=False),145  )146  def test_update_stats_false(self, update_bound, reset_stats):147    model, init_state, params = self.init_model(148        update_bound, update_stats=False, reset_stats=reset_stats)149    _, state_0 = model.apply(150        init_state, self.x, bounds_params=params, mutable='get_bounds')151    stats_0_stats = state_0['get_bounds']['stats']152    onp.testing.assert_array_equal(stats_0_stats.n, 0)153    _, state = model.apply(154        state_0, self.x2, bounds_params=params, mutable='get_bounds')155    onp.testing.assert_array_equal(state['get_bounds']['stats'].n, 0)156    expected_updated_mean = 0.157    onp.testing.assert_array_equal(expected_updated_mean,158                                   state['get_bounds']['stats'].mean)159  @parameterized.named_parameters(160      dict(161          testcase_name='update_bounds_true',162          update_stats=False,163          update_bounds=True),164      dict(165          testcase_name='update_stats_true',166          update_stats=True,167          update_bounds=False),168      dict(testcase_name='both_true', update_stats=True, update_bounds=True),169  )170  def test_update_state_with_mutable_false_context_raises_error(171      self, update_stats, update_bounds):172    model, init_state, _ = self.init_model(True)173    with self.assertRaises(flax.errors.ModifyScopeVariableError):174      model.apply(175          init_state,176          self.x2,177          bounds_params=get_bounds.GetBounds.Params(178              update_stats=update_stats, update_bounds=update_bounds),179          mutable=False)180  @parameterized.named_parameters(181      dict(182          testcase_name='update_bound_no_ucb',183          update_bound=True,184          use_cams=False),185      dict(186          testcase_name='update_bound_with_ucb',187          update_bound=True,188          use_cams=True),189      dict(testcase_name='do_not_update', update_bound=False, use_cams=False),190  )191  def test_get_bounds_update_bounds(self, update_bound, use_cams=False):192    model, init_state, params = self.init_model(update_bound, use_cams=use_cams)193    y, state_0 = model.apply(194        init_state, self.x, bounds_params=params, mutable='get_bounds')195    if not update_bound:196      onp.testing.assert_array_equal(state_0['get_bounds']['bounds'], 6.)197      onp.testing.assert_array_equal(y, 6.)198    else:199      stats_0_stats = state_0['get_bounds']['stats']200      if use_cams:201        expected_y = onp.abs(onp.mean(202            self.x)) + self.hyperparam.stddev_coeff * onp.std(self.x)203      else:204        expected_y = (205            self.hyperparam.stddev_coeff * self.hyperparam.mix_coeff *206            jnp.sqrt(stats_0_stats.mean_sq) + self.hyperparam.absdev_coeff *207            (1 - self.hyperparam.mix_coeff) * stats_0_stats.mean_abs)208      onp.testing.assert_array_equal(state_0['get_bounds']['bounds'], y)209      onp.testing.assert_allclose(expected_y, y)210    y2, state = model.apply(211        state_0, self.x2, bounds_params=params, mutable='get_bounds')212    onp.testing.assert_array_equal(state['get_bounds']['bounds'], y2)213  @parameterized.named_parameters(214      dict(testcase_name='no_ema', ema_coeff=None),215      dict(testcase_name='ema_.8', ema_coeff=0.8),216      dict(testcase_name='ema_.1', ema_coeff=0.1))217  def test_ema_coeff(self, ema_coeff):218    x1 = jnp.array(1.0)219    x2 = jnp.array(-2.0)220    model, state, params = self.init_model(False, ema_coeff=ema_coeff)221    _, state1 = model.apply(222        state, x1, bounds_params=params, mutable='get_bounds')223    _, state2 = model.apply(224        state1, x2, bounds_params=params, mutable='get_bounds')225    stats = state2['get_bounds']['stats']226    def compute_ema_two_steps(x1, x2, alpha):227      initial_value = 0.0228      ema_step_1 = initial_value + alpha * (x1 - initial_value)229      ema_step_2 = ema_step_1 + alpha * (x2 - ema_step_1)230      return ema_step_2231    if ema_coeff is None:232      exp_mean = (x1 + x2) / 2233      exp_mean_sq = (x1**2 + x2**2) / 2234      exp_mean_abs = (jnp.abs(x1) + jnp.abs(x2)) / 2235    else:236      exp_mean = compute_ema_two_steps(x1, x2, ema_coeff)237      exp_mean_sq = compute_ema_two_steps(x1**2, x2**2, ema_coeff)238      exp_mean_abs = compute_ema_two_steps(jnp.abs(x1), jnp.abs(x2), ema_coeff)239    onp.testing.assert_allclose(stats.mean, exp_mean)240    onp.testing.assert_allclose(stats.mean_sq, exp_mean_sq)241    onp.testing.assert_allclose(stats.mean_abs, exp_mean_abs)242    print(stats)243    return244if __name__ == '__main__':...game_stats.py
Source: game_stats.py 
...9#10#     def __init__(self, ai_settings):11#         """åå§åç»è®¡ä¿¡æ¯"""12#         self.ai_settings = ai_settings13#         self.reset_stats()14#15#         # 游æåå¯å¨æ¶å¤äºæ´»å¨ç¶æ16#         self.game_active = True17#18#     def reset_stats(self):19#         """åå§å卿¸¸æè¿è¡æé´å¯è½ååçç»è®¡ä¿¡æ¯"""20#         self.ships_left = self.ai_settings.ship_limit21# ------------------ example01 ------------------22# print("*" * 20)23# ------------------ example02 ------------------24# class GameStats():25#     """è·è¸ªæ¸¸æçç»è®¡ä¿¡æ¯"""26#27#     def __init__(self, ai_settings):28#         """åå§åç»è®¡ä¿¡æ¯"""29#         self.ai_settings = ai_settings30#         self.reset_stats()31#32#         # 让游æä¸å¼å§å¤äºéæ´»å¨ç¶æ33#         self.game_active = False34#35#     def reset_stats(self):36#         """åå§å卿¸¸æè¿è¡æé´å¯è½ååçç»è®¡ä¿¡æ¯"""37#         self.ships_left = self.ai_settings.ship_limit38# ------------------ example02 ------------------39# print("*" * 20)40# ------------------ example03 ------------------41# class GameStats():42#     """è·è¸ªæ¸¸æçç»è®¡ä¿¡æ¯"""43#44#     def __init__(self, ai_settings):45#         """åå§åç»è®¡ä¿¡æ¯"""46#         self.ai_settings = ai_settings47#         self.reset_stats()48#49#         # 让游æä¸å¼å§å¤äºéæ´»å¨ç¶æ50#         self.game_active = False51#52#     def reset_stats(self):53#         """åå§å卿¸¸æè¿è¡æé´å¯è½ååçç»è®¡ä¿¡æ¯"""54#         self.ships_left = self.ai_settings.ship_limit55#         self.score = 056# ------------------ example03 ------------------57# print("*" * 20)58# ------------------ example04 ------------------59# class GameStats():60#     """è·è¸ªæ¸¸æçç»è®¡ä¿¡æ¯"""61#62#     def __init__(self, ai_settings):63#         """åå§åç»è®¡ä¿¡æ¯"""64#         self.ai_settings = ai_settings65#         self.reset_stats()66#67#         # 让游æä¸å¼å§å¤äºéæ´»å¨ç¶æ68#         self.game_active = False69#70#         # å¨ä»»ä½æ
åµä¸é½ä¸åºéç½®æé«å¾å71#         self.high_score = 072#73#     def reset_stats(self):74#         """åå§å卿¸¸æè¿è¡æé´å¯è½ååçç»è®¡ä¿¡æ¯"""75#         self.ships_left = self.ai_settings.ship_limit76#         self.score = 077# ------------------ example04 ------------------78# print("*" * 20)79# ------------------ example05 ------------------80class GameStats():81    """è·è¸ªæ¸¸æçç»è®¡ä¿¡æ¯"""82    def __init__(self, ai_settings):83        """åå§åç»è®¡ä¿¡æ¯"""84        self.ai_settings = ai_settings85        self.reset_stats()86        # 让游æä¸å¼å§å¤äºéæ´»å¨ç¶æ87        self.game_active = False88        # å¨ä»»ä½æ
åµä¸é½ä¸åºéç½®æé«å¾å89        self.high_score = 090    def reset_stats(self):91        """åå§å卿¸¸æè¿è¡æé´å¯è½ååçç»è®¡ä¿¡æ¯"""92        self.ships_left = self.ai_settings.ship_limit93        self.score = 094        self.level = 1...Check out the latest blogs from LambdaTest on this topic:
The rapid shift in the use of technology has impacted testing and quality assurance significantly, especially around the cloud adoption of agile development methodologies. With this, the increasing importance of quality and automation testing has risen enough to deliver quality work.
Hey LambdaTesters! We’ve got something special for you this week. ????
I routinely come across test strategy documents when working with customers. They are lengthy—100 pages or more—and packed with monotonous text that is routinely reused from one project to another. Yawn once more— the test halt and resume circumstances, the defect management procedure, entrance and exit criteria, unnecessary generic risks, and in fact, one often-used model replicates the requirements of textbook testing, from stress to systems integration.
As everyone knows, the mobile industry has taken over the world and is the fastest emerging industry in terms of technology and business. It is possible to do all the tasks using a mobile phone, for which earlier we had to use a computer. According to Statista, in 2021, smartphone vendors sold around 1.43 billion smartphones worldwide. The smartphone penetration rate has been continuously rising, reaching 78.05 percent in 2020. By 2025, it is expected that almost 87 percent of all mobile users in the United States will own a smartphone.
Anyone who has worked in the software industry for a while can tell you stories about projects that were on the verge of failure. Many initiatives fail even before they reach clients, which is especially disheartening when the failure is fully avoidable.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
