teachcompute.ext_test_case¶
Various helpers to help develop the package.
ExtTestCase¶
- class teachcompute.ext_test_case.ExtTestCase(methodName='runTest')[source][source]¶
- assertAlmostEqual(expected: ndarray, value: ndarray, atol: float = 0, rtol: float = 0)[source][source]¶
Fail if the two objects are unequal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is more than the given delta.
Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).
If the two objects compare equal then they will automatically compare almost equal.
- assertIns(sub: Tuple[Any, ...], s: str)[source][source]¶
Checks that one of the substrings in sub is part of s.
- assertNotAlmostEqual(expected: ndarray, value: ndarray, atol: float = 0, rtol: float = 0)[source][source]¶
Fail if the two objects are equal as determined by their difference rounded to the given number of decimal places (default 7) and comparing to zero, or by comparing that the difference between the two objects is less than the given delta.
Note that decimal places (from zero) are usually not the same as significant digits (measured from the most significant digit).
Objects that are equal automatically fail.
- capture(fct: Callable)[source][source]¶
Runs a function and capture standard output and error.
- Paramètres:
fct – function to run
- Renvoie:
result of fct, output, error
- classmethod tearDownClass()[source][source]¶
Hook method for deconstructing the class fixture after running all tests in the class.
- tryCall(fct: Callable, msg: str | None = None, none_if: str | None = None) Any | None [source][source]¶
Calls the function, catch any error.
- Paramètres:
fct – function to call
msg – error message to display if failing
none_if – returns None if this substring is found in the error message
- Renvoie:
output of fct
ignore_warnings¶
measure_time¶
- teachcompute.ext_test_case.measure_time(stmt: str | Callable, context: Dict[str, Any] | None = None, repeat: int = 10, number: int = 50, warmup: int = 1, div_by_number: bool = True, max_time: float | None = None) Dict[str, str | int | float] [source][source]¶
Measures a statement and returns the results as a dictionary.
- Paramètres:
stmt – string or callable
context – variable to know in a dictionary
repeat – average over repeat experiment
number – number of executions in one row
warmup – number of iteration to do before starting the real measurement
div_by_number – divide by the number of executions
max_time – execute the statement until the total goes beyond this time (approximatively), repeat is ignored, div_by_number must be set to True
- Renvoie:
dictionary
<<<
from pprint import pprint from math import cos from teachcompute.ext_test_case import measure_time res = measure_time(lambda: cos(0.5)) pprint(res)
>>>
{'average': np.float64(8.709600660949947e-08), 'context_size': 64, 'deviation': np.float64(1.0390855975509226e-08), 'max_exec': np.float64(9.890005458146333e-08), 'min_exec': np.float64(7.40600808057934e-08), 'number': 50, 'repeat': 10, 'ttime': np.float64(8.709600660949946e-07), 'warmup_time': 2.6419947971589863e-06}
See Timer.repeat for a better understanding of parameter repeat and number. The function returns a duration corresponding to number times the execution of the main statement.
measure_time_dim¶
- teachcompute.ext_test_case.measure_time_dim(stmt, contexts, repeat=10, number=50, div_by_number=True, verbose=0)[source][source]¶
Measures a statement multiple time with function
measure_time_dim()
.- Paramètres:
stmt – string
contexts – variable to know in a dictionary, every context must include field “x_name”, which is copied in the result
repeat – average over repeat experiment
number – number of executions in one row
div_by_number – divide by the number of executions
verbose – if > 0, use tqdm to display progress
- Renvoie:
yield dictionary
<<<
import pprint import numpy from teachcompute.ext_test_case import measure_time_dim res = list( measure_time_dim( "cos(x)", contexts=[ dict(cos=numpy.cos, x=numpy.arange(10), x_name=10), dict(cos=numpy.cos, x=numpy.arange(100), x_name=100), ], ) ) pprint.pprint(res)
>>>
[{'average': np.float64(2.5645519926911224e-06), 'context_size': 232, 'deviation': np.float64(1.6599623069358361e-06), 'max_exec': np.float64(6.37731995084323e-06), 'min_exec': np.float64(8.029399032238871e-07), 'number': 50, 'repeat': 10, 'ttime': np.float64(2.5645519926911223e-05), 'warmup_time': 3.941699833376333e-05, 'x_name': 10}, {'average': np.float64(1.721335982438177e-06), 'context_size': 232, 'deviation': np.float64(1.3864582987686495e-08), 'max_exec': np.float64(1.758439902914688e-06), 'min_exec': np.float64(1.7080199904739857e-06), 'number': 50, 'repeat': 10, 'ttime': np.float64(1.721335982438177e-05), 'warmup_time': 7.237002137117088e-06, 'x_name': 100}]
See Timer.repeat for a better understanding of parameter repeat and number. The function returns a duration corresponding to number times the execution of the main statement.