pyglove 0.5.0.dev202510010810__py3-none-any.whl → 0.5.0.dev202510030810__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyglove might be problematic. Click here for more details.

@@ -19,11 +19,13 @@ programs.
19
19
 
20
20
  import abc
21
21
  import collections
22
+ import contextlib
22
23
  import math
23
24
  import threading
25
+ import time
24
26
  import typing
25
- from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
26
-
27
+ from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Type, Union
28
+ from pyglove.core.utils import error_utils
27
29
 
28
30
  try:
29
31
  import numpy # pylint: disable=g-import-not-at-top
@@ -194,6 +196,38 @@ class Scalar(Metric):
194
196
  The distribution of the scalar.
195
197
  """
196
198
 
199
+ @contextlib.contextmanager
200
+ def record_duration(
201
+ self,
202
+ *,
203
+ scale: int = 1000,
204
+ error_parameter: str = 'error',
205
+ **parameters) -> Iterator[None]:
206
+ """Context manager that records the duration of code block to the scalar.
207
+
208
+ Args:
209
+ scale: The scale of the duration.
210
+ error_parameter: The parameter name for recording the error. If the name
211
+ is not defined as a parameter for the scalar, the error tag will not be
212
+ recorded.
213
+ **parameters: Parameters for parameterized scalars.
214
+ """
215
+ start_time = time.time()
216
+ error = None
217
+ try:
218
+ yield
219
+ except BaseException as e:
220
+ error = e
221
+ raise e
222
+ finally:
223
+ duration = (time.time() - start_time) * scale
224
+ if error_parameter in self._parameter_definitions:
225
+ parameters[error_parameter] = (
226
+ error_utils.ErrorInfo.from_exception(error).tag
227
+ if error is not None else ''
228
+ )
229
+ self.record(int(duration), **parameters)
230
+
197
231
 
198
232
  class MetricCollection(metaclass=abc.ABCMeta):
199
233
  """Base class for counter collections."""
@@ -12,8 +12,10 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ import time
15
16
  import unittest
16
17
  from pyglove.core import monitoring
18
+ from pyglove.core.symbolic import error_info # pylint: disable=unused-import
17
19
 
18
20
 
19
21
  class MetricCollectionTest(unittest.TestCase):
@@ -205,6 +207,11 @@ class InMemoryScalarTest(unittest.TestCase):
205
207
  scalar.distribution()
206
208
  self.assertEqual(dist.count, 3)
207
209
 
210
+ scalar = collection.get_scalar('scalar2', 'scalar description')
211
+ with scalar.record_duration():
212
+ time.sleep(0.1)
213
+ self.assertGreaterEqual(scalar.distribution().mean, 100)
214
+
208
215
  def test_scalar_with_parameters(self):
209
216
  collection = monitoring.InMemoryMetricCollection('/test')
210
217
  scalar = collection.get_scalar(
@@ -225,6 +232,18 @@ class InMemoryScalarTest(unittest.TestCase):
225
232
  dist = scalar.distribution(field1='bar')
226
233
  self.assertEqual(dist.count, 1)
227
234
 
235
+ scalar = collection.get_scalar(
236
+ 'scalar2', 'scalar description', {'error': str}
237
+ )
238
+ with self.assertRaises(ValueError):
239
+ with scalar.record_duration():
240
+ time.sleep(0.1)
241
+ raise ValueError()
242
+ self.assertGreaterEqual(scalar.distribution(error='ValueError').mean, 100)
243
+ with scalar.record_duration():
244
+ time.sleep(0.1)
245
+ self.assertGreaterEqual(scalar.distribution(error='').mean, 100)
246
+
228
247
 
229
248
  if __name__ == '__main__':
230
249
  unittest.main()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyglove
3
- Version: 0.5.0.dev202510010810
3
+ Version: 0.5.0.dev202510030810
4
4
  Summary: PyGlove: A library for manipulating Python objects.
5
5
  Home-page: https://github.com/google/pyglove
6
6
  Author: PyGlove Authors
@@ -2,8 +2,8 @@ pyglove/__init__.py,sha256=LP1HNk_VVWMHaakX3HZ0NeZ2c4lq2uJaRbalvT8haOg,1352
2
2
  pyglove/core/__init__.py,sha256=WPTvhQXv63f4-AkYH__RwVKQyr8vHr5e3tGqT2OxRGM,9774
3
3
  pyglove/core/logging.py,sha256=zTNLGnWrl6kkjjEjTphQMeNf9FfqFtl1G9VCh2LhnHg,4614
4
4
  pyglove/core/logging_test.py,sha256=ioDbmf4S6xQXeyGWihvk6292wfYdldBw_TK1WXvIq5k,1648
5
- pyglove/core/monitoring.py,sha256=6QWbnCBC9HOc6_lnPmoDi-JTsusNwlXz4_LPnwWjjsw,15143
6
- pyglove/core/monitoring_test.py,sha256=47zjsNiYEMKOOdYpUue7_BPJYi1z9eJN_t6cw65TB-4,8558
5
+ pyglove/core/monitoring.py,sha256=RRA-pOne-UuMi6oSMB-z0_N-NRw7cB9qaDfEvElnmug,16221
6
+ pyglove/core/monitoring_test.py,sha256=8QwmpYgKlxxQNAUWsqZMNCurtwcpMyBJGDj_dPINJBU,9267
7
7
  pyglove/core/coding/__init__.py,sha256=tuHIg19ZchtkOQbdFVTVLkUpBa5f1eo66XtnKw3lcIU,1645
8
8
  pyglove/core/coding/errors.py,sha256=aP3Y4amBzOKdlb5JnESJ3kdoijQXbiBiPDMeA88LNrk,3310
9
9
  pyglove/core/coding/errors_test.py,sha256=fwOR8vLiRvLadubsccyE19hLHj-kThlCQt88qmUYk9M,2311
@@ -218,8 +218,8 @@ pyglove/ext/scalars/randoms.py,sha256=LkMIIx7lOq_lvJvVS3BrgWGuWl7Pi91-lA-O8x_gZs
218
218
  pyglove/ext/scalars/randoms_test.py,sha256=nEhiqarg8l_5EOucp59CYrpO2uKxS1pe0hmBdZUzRNM,2000
219
219
  pyglove/ext/scalars/step_wise.py,sha256=IDw3tuTpv0KVh7AN44W43zqm1-E0HWPUlytWOQC9w3Y,3789
220
220
  pyglove/ext/scalars/step_wise_test.py,sha256=TL1vJ19xVx2t5HKuyIzGoogF7N3Rm8YhLE6JF7i0iy8,2540
221
- pyglove-0.5.0.dev202510010810.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
222
- pyglove-0.5.0.dev202510010810.dist-info/METADATA,sha256=owYy54yQlinPA3GYlyO2yfqxgmsfsUzFdFjXMNaIVX4,7089
223
- pyglove-0.5.0.dev202510010810.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
224
- pyglove-0.5.0.dev202510010810.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
225
- pyglove-0.5.0.dev202510010810.dist-info/RECORD,,
221
+ pyglove-0.5.0.dev202510030810.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
222
+ pyglove-0.5.0.dev202510030810.dist-info/METADATA,sha256=XK5-HtxQBDSCT-wGE07SOGTvQ5eNtCqW-HTgzeD-2Ik,7089
223
+ pyglove-0.5.0.dev202510030810.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
224
+ pyglove-0.5.0.dev202510030810.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
225
+ pyglove-0.5.0.dev202510030810.dist-info/RECORD,,