Comparing with a source#
boki.testing sets a render beside figures taken from the original spreadsheet. expect fails on a mismatch;
compare shows every number with its difference. approx, above, below and between make a reference that is a range
instead of one number.
- boki.expect(result, expected, tol=0.01, rel=0.0001)#
Raise an error that lists every value that differs from the original spreadsheet.
Use it to verify a model against the source’s figures. It does the same comparison as compare, which shows every number without failing.
- Parameters:
- resultResult
The render to read the model’s values from.
- expecteddict
Maps what to compare to the source’s value, as the
referenceof compare: scalar series or whole series as keys, and numbers, lists with one value per period, a Series, dates, quantities or bounds as values.- tolfloat, default=0.01
Absolute tolerance, in the base unit of the compared series.
- relfloat, default=1e-4
Relative tolerance, a fraction of the larger of the two values.
- Raises:
- AssertionError
At evaluation, if any value differs. The message lists each mismatch with the series, period, expected value and the model’s value.
- ValueError
At evaluation, if a list has a different number of values than the model has periods.
- UnitError
At evaluation, if a reference Quantity has other dimensions than the series.
See also
boki.compareShow every number without failing.
boki.approxReference within a tolerance.
Examples
Verify a series, then see how a mismatch is reported:
>>> import boki as b >>> m = b.Model("Source", start="2027-01", periods=3, step=b.year) >>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 130}) >>> r = m.render() >>> b.expect(r, {sales: [100, 120, 130]}) >>> try: ... b.expect(r, {sales: [100, 121, 130]}) ... except AssertionError as error: ... print(error) values differ from the source: Sales [2028]: expected 121, got 120
- boki.compare(result, reference, tol=0.01, rel=0.0001)#
Return the model’s values beside reference values taken from the original spreadsheet.
Use it to see every number with its difference; expect is the same comparison that fails on a mismatch.
- Parameters:
- resultResult
The render to read the model’s values from.
- referencedict
Maps what to compare to the source’s value. A key is a scalar series (
revenue[5],lib.npv(...)) or a whole series. For a whole series, the value is a list with one reference per period, a Series of references, or a single value that is compared in every period. A list has one value per period of the model, withNonefor a period the source has no value for (it is not compared). A scalar key takes one value. A value can be a Bound (approx, above, below, between), a date (a date series compares as dates), or a Quantity such as28_032_000 * kWh, which is compared in its unit.- tolfloat, default=0.01
Absolute tolerance, in the base unit of the compared series (dollars, Wh, months), not in the unit shown in tables.
- relfloat, default=1e-4
Relative tolerance, a fraction of the larger of the two values. A pair agrees when it is within
tolor withinrel, whichever is wider.
- Returns:
- Comparison
One entry for each compared number. A constant series compared with a list of period values is read as that constant in every period.
- Raises:
- ValueError
At evaluation, if a list has a different number of values than the model has periods.
- UnitError
At evaluation, if a reference Quantity has other dimensions than the series.
See also
boki.expectFail on any mismatch.
boki.approxReference within a tolerance.
Notes
The model’s values are read from the render, so errors from evaluating the series (an unknown period, a division by zero) are raised here too. See comparing with the source.
Examples
List the entries that disagree with a source sheet’s figures:
>>> import boki as b >>> m = b.Model("Source", start="2027-01", periods=3, step=b.year) >>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 130}) >>> bcr = m.series("BCR", 1.3412) >>> r = m.render() >>> comparison = b.compare(r, {sales: [100, 121, 130], bcr: b.approx(1.34, tol=0.005)}) >>> [(e.label, e.period, e.got, e.want) for e in comparison.mismatches] [('Sales', '2028', 120, 121)]
A single value is compared in every period, and a quantity in its own unit:
>>> from boki.units import MWh >>> gen = m.series("Generation", data={0: 400 * MWh, 1: 400 * MWh, 2: 400 * MWh}) >>> r = m.render() >>> [e.ok for e in b.compare(r, {gen: 400 * MWh}).entries] [True, True, True]
- boki.approx(value, tol=None, rel=None)#
Return a reference that accepts values within a tolerance of
value.Use it as the reference value in compare and expect when the source gives a rounded figure: a ratio shown as 1.34 is
b.approx(1.34, tol=0.005).- Parameters:
- valuefloat or Quantity
The centre of the range, such as
1.34or100 * USD. A Quantity is compared in its unit.- tolfloat or Quantity, optional
Absolute half-width of the range, in the same unit as
value(tol=1 * USDwith100 * USD).- relfloat, optional
Relative half-width: a fraction of the size of
value, sorel=0.01accepts 1% either way. When bothtolandrelare given, the wider range applies.
- Returns:
- Bound
A reference for the range
value - widthtovalue + width, both ends included.
- Raises:
- TypeError
At construction, if neither
tolnorrelis given. For an exact number, use the plain value.
See also
boki.aboveMore than a value.
boki.betweenFrom one value to another, both included.
boki.expectFail on a value outside its reference.
Examples
A source ratio rounded to two decimals:
>>> import boki as b >>> m = b.Model("Ratio", start="2027-01", periods=2, step=b.year) >>> bcr = m.series("BCR", 1.3412) >>> r = m.render() >>> b.expect(r, {bcr: b.approx(1.34, tol=0.005)}) >>> str(b.approx(1.34, tol=0.005)), str(b.approx(100, rel=0.01)) ('1.34 ± 0.005', '100 ± 1')
- boki.above(value)#
Return a reference that accepts values greater than
value.Use it as the reference value in compare and expect when the source gives only a lower limit, such as “over 50%”. The limit itself does not pass.
- Parameters:
- valuefloat or Quantity
The limit. A Quantity is compared in its unit.
- Returns:
- Bound
A reference for values strictly above
value.
See also
boki.belowLess than a value.
boki.betweenFrom one value to another, both included.
boki.approxA value within a tolerance.
Examples
>>> import boki as b >>> m = b.Model("Share", start="2027-01", periods=2, step=b.year) >>> share = m.series("Share", 0.62) >>> r = m.render() >>> b.expect(r, {share: b.above(0.5)})
- boki.below(value)#
Return a reference that accepts values less than
value.Use it as the reference value in compare and expect when the source gives only an upper limit. The limit itself does not pass.
- Parameters:
- valuefloat or Quantity
The limit. A Quantity is compared in its unit.
- Returns:
- Bound
A reference for values strictly below
value.
See also
boki.aboveMore than a value.
boki.betweenFrom one value to another, both included.
Examples
>>> import boki as b >>> m = b.Model("Leverage", start="2027-01", periods=2, step=b.year) >>> ratio = m.series("Debt ratio", 2.4) >>> r = m.render() >>> b.expect(r, {ratio: b.below(3)})
- boki.between(lo, hi)#
Return a reference that accepts values from
lotohi, both included.Use it as the reference value in compare and expect when the source gives a range.
- Parameters:
- lofloat or Quantity
Lower end of the range. A Quantity is compared in its unit.
- hifloat or Quantity
Upper end of the range, in the same unit as
lo.
- Returns:
- Bound
A reference for values from
lotohi.
See also
boki.approxA value within a tolerance.
boki.aboveMore than a value.
boki.belowLess than a value.
Examples
>>> import boki as b >>> m = b.Model("Range", start="2027-01", periods=2, step=b.year) >>> irr = m.series("IRR", 0.12) >>> r = m.render() >>> b.expect(r, {irr: b.between(0.1, 0.15)})
- class boki.testing.Comparison(entries)#
The model’s values beside the reference values, one entry for each compared number.
compare returns it. Printing it (or showing it in a notebook) writes a table with the columns period, model, source and diff, and marks every disagreement with
≠.mismatcheslists the disagreeing entries.- Parameters:
- entrieslist[Entry]
The compared numbers, in the order of the reference keys and then of the periods.
- Attributes:
- entrieslist[Entry]
All compared numbers, agreeing or not.
See also
boki.compareReturns a comparison.
boki.expectFail on any mismatch instead of listing.
Examples
>>> import boki as b >>> m = b.Model("Compare", start="2027-01", periods=3, step=b.year) >>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 130}) >>> r = m.render() >>> comparison = b.compare(r, {sales: [100, 121, 130]}) >>> len(comparison.entries), len(comparison.mismatches) (3, 1)
- property mismatches#
The entries where the model and the reference disagree.
- Returns:
- list[Entry]
A new list of the entries whose
okis false, in comparison order; empty when everything agrees.
See also
boki.expectRaise when this list is not empty.
Examples
>>> import boki as b >>> m = b.Model("Mismatch", start="2027-01", periods=2, step=b.year) >>> sales = m.series("Sales", data={0: 100, 1: 120}) >>> r = m.render() >>> [(e.label, e.period, e.got, e.want) for e in b.compare(r, {sales: [100, 125]}).mismatches] [('Sales', '2028', 120, 125)]
- class boki.testing.Entry(label, period, got, want, ok)#
One compared number: the model’s value beside the reference value, for one series and period.
compare returns one entry for each number it compares. An entry is a plain record; read
okto see whether the two agreed within the tolerance.- Parameters:
- labelstr
Name of the compared series, or
name[period]for a value picked withx[period].- periodstr
Label of the period, or
""for a scalar series.- gotfloat | date | None
The model’s value in base units, or
Noneif the model has no value there.- wantfloat | date | Bound
The reference value in base units, or the Bound it must lie within.
- okbool
Whether the model’s value agrees with the reference.
- Attributes:
- labelstr
Name of the compared series.
- periodstr
Label of the period, or
""for a scalar series.- gotfloat, date, or None
The model’s value.
- wantfloat, date, or Bound
The reference value or bound.
- okbool
Whether the model’s value agrees with the reference.
See also
boki.compareReturns the entries.
boki.testing.ComparisonA list of entries.
Examples
>>> import boki as b >>> m = b.Model("Entries", start="2027-01", periods=2, step=b.year) >>> sales = m.series("Sales", data={0: 100, 1: 120}) >>> r = m.render() >>> entry = b.compare(r, {sales: [100, 121]}).entries[1] >>> entry.label, entry.period, entry.got, entry.want, entry.ok, entry.diff ('Sales', '2028', 120, 121, False, -1)
- property diff#
Model value minus reference value.
- Returns:
- float or None
got - want, in base units.Nonewhen the model has no value, or when the reference is a date or a Bound, which have no single number to subtract.
Examples
>>> import boki as b >>> m = b.Model("Diff", start="2027-01", periods=2, step=b.year) >>> sales = m.series("Sales", data={0: 100, 1: 120}) >>> r = m.render() >>> [entry.diff for entry in b.compare(r, {sales: [100, 125]}).entries] [0, -5]
- class boki.testing.Bound(text, lo=None, hi=None, strict=False)#
Reference value that is a range instead of one number.
Make one with boki.approx, boki.above, boki.below or boki.between, and use it as the reference value in boki.compare and boki.expect. It is never built directly.
- Parameters:
- textstr
How the bound reads in a comparison table, such as
"> 0.5"or"1.34 ± 0.005".- lofloat | Quantity | None
Lower end of the range in base units, or
Nonefor no lower end.- hifloat | Quantity | None
Upper end of the range in base units, or
Nonefor no upper end.- strictbool
Whether the ends are excluded (
True) or included (False).
See also
boki.approxA value within a tolerance.
boki.aboveMore than a value.
boki.belowLess than a value.
boki.betweenFrom one value to another.
Examples
>>> import boki as b >>> str(b.between(1, 2)), str(b.above(0.5)) ('1 to 2', '> 0.5')