Timber bucking demo#

Notebook Objectives#

  • Show reproducible bucking workflows with timber objects, taper functions, and price lists.

  • Provide runnable, copy-safe snippets that work in the docs build environment.

Prerequisites#

  • Python environment with pyforestry installed from this repository.

  • Execute cells in order; random components should use fixed seeds where shown.

Sources#

  • Näslund (1985) bucking workflow implementation exposed in pyforestry.base.timber_bucking.

  • Package-level timber/taper abstractions in pyforestry.base.timber and pyforestry.base.taper.

This notebook demonstrates timber objects, tapers, pricelists, the Nasberg bucking algorithm and how solution cubes store many results in xarray.

[1]:
from pyforestry.base.pricelist import create_pricelist_from_data
from pyforestry.base.pricelist.solutioncube import SolutionCube
from pyforestry.base.timber_bucking.nasberg_1985 import BuckingConfig, Nasberg_1985_BranchBound
from pyforestry.sweden.pricelist.data.mellanskog_2013 import MELLANSKOG_2013_PRICE_DATA
from pyforestry.sweden.taper import EdgrenNylinder1949
from pyforestry.sweden.timber.swe_timber import SweTimber

Create timber object#

[2]:
timber = SweTimber(species="pinus sylvestris", diameter_cm=18, height_m=25)
timber
[2]:
<pyforestry.sweden.timber.swe_timber.SweTimber at 0x7f46faabbe90>

Load pricelist#

[3]:
pricelist = create_pricelist_from_data(
    MELLANSKOG_2013_PRICE_DATA, species_to_load="pinus sylvestris"
)
pricelist.Timber.keys()
[3]:
dict_keys(['pinus sylvestris'])

Run bucking optimizer#

[4]:
optimizer = Nasberg_1985_BranchBound(timber, pricelist, EdgrenNylinder1949)
result = optimizer.calculate_tree_value(
    min_diam_dead_wood=99, config=BuckingConfig(save_sections=True)
)
result
[4]:
BuckingResult(species_group='pinus sylvestris', total_value=np.float32(116.55982), top_proportion=0.0, dead_wood_proportion=0.0, high_stump_volume_proportion=0.0, high_stump_value_proportion=0.0, last_cut_relative_height=np.float64(1.106), volume_per_quality=[0.0, 0.1061747827409899, 0.10089169267225448, 0.0931144950816689, 0.0, 0.03325533384575725, 0.0], timber_price_by_quality=[0.0, np.float32(374.95026), np.float32(395.25204), np.float32(175.33333), 0.0, 0.0, 0.0], vol_fub_5cm=0.3315022903380483, vol_sk_ub=0.33342746304095644, dbh_cm=np.float64(18.000000000000004), height_m=25, stump_height_m=0.25, diameter_stump_cm=np.float64(20.503363467016907), taper_diameters_cm=[20.50336265563965, 19.994277954101562, 19.61321449279785, 19.30855369567871, 19.054716110229492, 18.83714485168457, 18.646760940551758, 18.47751808166504, 18.325185775756836, 18.186691284179688, 18.059728622436523, 17.942522048950195, 17.833681106567383, 17.732088088989258, 17.6368408203125, 17.547189712524414, 17.46251678466797, 17.382295608520508, 17.306081771850586, 17.233495712280273, 17.16420555114746, 17.097925186157227, 17.034404754638672, 16.97342300415039, 16.914785385131836, 16.858318328857422, 16.803865432739258, 16.751291275024414, 16.70046615600586, 16.651281356811523, 16.6036319732666, 16.557424545288086, 16.512577056884766, 16.46900749206543, 16.426651000976562, 16.38543701171875, 16.345306396484375, 16.306203842163086, 16.26807975769043, 16.230884552001953, 16.1945743560791, 16.159109115600586, 16.124448776245117, 16.090559005737305, 16.057403564453125, 16.02495574951172, 15.993182182312012, 15.962057113647461, 15.931553840637207, 15.90164852142334, 15.872318267822266, 15.843542098999023, 15.81529712677002, 15.787566184997559, 15.760330200195312, 15.727702140808105, 15.703722953796387, 15.679622650146484, 15.655400276184082, 15.631054878234863, 15.606584548950195, 15.581988334655762, 15.557265281677246, 15.532414436340332, 15.50743293762207, 15.482320785522461, 15.457075119018555, 15.431696891784668, 15.406182289123535, 15.380531311035156, 15.354741096496582, 15.328811645507812, 15.302741050720215, 15.276528358459473, 15.25016975402832, 15.223666191101074, 15.197013854980469, 15.170212745666504, 15.143260955810547, 15.116156578063965, 15.088896751403809, 15.061481475830078, 15.033907890319824, 15.006174087524414, 14.978279113769531, 14.950220108032227, 14.921995162963867, 14.893603324890137, 14.865041732788086, 14.836308479309082, 14.807401657104492, 14.778319358825684, 14.749058723449707, 14.719618797302246, 14.689995765686035, 14.660187721252441, 14.630193710327148, 14.60000991821289, 14.569634437561035, 14.539064407348633, 14.508298873901367, 14.477333068847656, 14.446166038513184, 14.414793968200684, 14.38321590423584, 14.35142707824707, 14.319425582885742, 14.287208557128906, 14.254773139953613, 14.222116470336914, 14.18923568725586, 14.156126976013184, 14.122786521911621, 14.089213371276855, 14.055401802062988, 14.021349906921387, 13.987054824829102, 13.952510833740234, 13.917716026306152, 13.882665634155273, 13.847357749938965, 13.811785697937012, 13.775948524475098, 13.739839553833008, 13.70345687866211, 13.666794776916504, 13.629850387573242, 13.59261703491211, 13.555092811584473, 13.517271041870117, 13.479147911071777, 13.440718650817871, 13.401978492736816, 13.362921714782715, 13.323542594909668, 13.28383731842041, 13.243800163269043, 13.203424453735352, 13.162704467773438, 13.121635437011719, 13.08021068572998, 13.038424491882324, 12.996269226074219, 12.953739166259766, 12.91082763671875, 12.867527961730957, 12.823832511901855, 12.77973461151123, 12.722068786621094, 12.649435997009277, 12.576272964477539, 12.502571105957031, 12.428321838378906, 12.353517532348633, 12.27815055847168, 12.202211380004883, 12.125691413879395, 12.048582077026367, 11.970873832702637, 11.892557144165039, 11.813623428344727, 11.734062194824219, 11.653862953186035, 11.573016166687012, 11.491511344909668, 11.40933609008789, 11.326481819152832, 11.242935180664062, 11.158685684204102, 11.07371997833252, 10.988027572631836, 10.901595115661621, 10.814409255981445, 10.726457595825195, 10.637725830078125, 10.548199653625488, 10.457866668701172, 10.366710662841797, 10.2747163772583, 10.181868553161621, 10.088150978088379, 9.993548393249512, 9.898042678833008, 9.801615715026855, 9.704252243041992, 9.605931282043457, 9.506634712219238, 9.406343460083008, 9.305037498474121, 9.2026948928833, 9.099294662475586, 8.994815826416016, 8.889233589172363, 8.782526969909668, 8.67466926574707, 8.56563663482666, 8.455403327941895, 8.343942642211914, 8.231225967407227, 8.117225646972656, 8.001911163330078, 7.885253429412842, 7.767219543457031, 7.6477766036987305, 7.526891231536865, 7.40452766418457, 7.280649185180664, 7.155218601226807, 7.028195858001709, 6.899539947509766, 6.769208908081055, 6.637157917022705, 6.503341197967529, 6.367711067199707, 6.230217933654785, 6.090808868408203, 5.9494309425354, 5.806026935577393, 5.660538196563721, 5.512903213500977, 5.363057613372803, 5.210934162139893, 5.05646276473999, 4.899569988250732, 4.740178108215332, 4.578206539154053, 4.413570404052734, 4.246181011199951, 4.075943470001221, 3.9027605056762695, 3.726527690887451, 3.547135829925537, 3.364469528198242, 3.1784071922302246, 2.9888200759887695, 2.79557204246521, 2.5985188484191895, 2.397507667541504, 2.1923763751983643, 1.9829522371292114, 1.7690519094467163, 1.5504800081253052, 1.327027678489685, 1.098471999168396, 0.8645745515823364, 0.6250795722007751, 0.3797125220298767, 0.12817826867103577, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], taper_heights_m=[0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.8500000000000001, 0.9500000000000001, 1.05, 1.15, 1.25, 1.35, 1.4500000000000002, 1.55, 1.6500000000000001, 1.75, 1.85, 1.9500000000000002, 2.05, 2.1500000000000004, 2.25, 2.35, 2.45, 2.5500000000000003, 2.6500000000000004, 2.75, 2.85, 2.95, 3.0500000000000003, 3.1500000000000004, 3.25, 3.35, 3.45, 3.5500000000000003, 3.6500000000000004, 3.75, 3.85, 3.95, 4.050000000000001, 4.15, 4.25, 4.3500000000000005, 4.45, 4.55, 4.65, 4.75, 4.8500000000000005, 4.95, 5.050000000000001, 5.15, 5.25, 5.3500000000000005, 5.45, 5.550000000000001, 5.65, 5.75, 5.8500000000000005, 5.95, 6.050000000000001, 6.15, 6.25, 6.3500000000000005, 6.45, 6.550000000000001, 6.65, 6.75, 6.8500000000000005, 6.95, 7.050000000000001, 7.15, 7.25, 7.3500000000000005, 7.45, 7.550000000000001, 7.65, 7.75, 7.8500000000000005, 7.95, 8.05, 8.15, 8.25, 8.35, 8.450000000000001, 8.55, 8.65, 8.75, 8.85, 8.950000000000001, 9.05, 9.15, 9.25, 9.35, 9.450000000000001, 9.55, 9.65, 9.75, 9.850000000000001, 9.950000000000001, 10.05, 10.15, 10.25, 10.350000000000001, 10.450000000000001, 10.55, 10.65, 10.75, 10.850000000000001, 10.950000000000001, 11.05, 11.15, 11.25, 11.350000000000001, 11.450000000000001, 11.55, 11.65, 11.75, 11.850000000000001, 11.950000000000001, 12.05, 12.15, 12.25, 12.350000000000001, 12.450000000000001, 12.55, 12.65, 12.75, 12.850000000000001, 12.950000000000001, 13.05, 13.15, 13.25, 13.350000000000001, 13.450000000000001, 13.55, 13.65, 13.75, 13.850000000000001, 13.950000000000001, 14.05, 14.15, 14.25, 14.350000000000001, 14.450000000000001, 14.55, 14.65, 14.75, 14.850000000000001, 14.950000000000001, 15.05, 15.15, 15.25, 15.350000000000001, 15.450000000000001, 15.55, 15.65, 15.75, 15.850000000000001, 15.950000000000001, 16.05, 16.15, 16.25, 16.35, 16.45, 16.55, 16.650000000000002, 16.75, 16.85, 16.95, 17.05, 17.150000000000002, 17.25, 17.35, 17.45, 17.55, 17.650000000000002, 17.75, 17.85, 17.95, 18.05, 18.150000000000002, 18.25, 18.35, 18.45, 18.55, 18.650000000000002, 18.75, 18.85, 18.95, 19.05, 19.150000000000002, 19.25, 19.35, 19.450000000000003, 19.55, 19.650000000000002, 19.75, 19.85, 19.950000000000003, 20.05, 20.150000000000002, 20.25, 20.35, 20.450000000000003, 20.55, 20.650000000000002, 20.75, 20.85, 20.950000000000003, 21.05, 21.150000000000002, 21.25, 21.35, 21.450000000000003, 21.55, 21.650000000000002, 21.75, 21.85, 21.950000000000003, 22.05, 22.150000000000002, 22.25, 22.35, 22.450000000000003, 22.55, 22.650000000000002, 22.75, 22.85, 22.950000000000003, 23.05, 23.150000000000002, 23.25, 23.35, 23.450000000000003, 23.55, 23.650000000000002, 23.75, 23.85, 23.950000000000003, 24.05, 24.150000000000002, 24.25, 24.35, 24.450000000000003, 24.55, 24.650000000000002, 24.75, 24.85, 24.950000000000003, 25.05, 25.150000000000002, 25.25, 25.35, 25.450000000000003, 25.55, 25.650000000000002, 25.75, 25.85, 25.950000000000003, 26.05, 26.150000000000002, 26.25, 26.35, 26.450000000000003, 26.55, 26.650000000000002, 26.75, 26.85, 26.950000000000003, 27.05, 27.150000000000002, 27.25, 27.35, 27.450000000000003, 27.55, 27.650000000000002, 27.75, 27.85, 27.950000000000003, 28.05, 28.150000000000002, 28.25, 28.35, 28.450000000000003, 28.55, 28.650000000000002, 28.75, 28.85, 28.950000000000003, 29.05, 29.150000000000002, 29.25, 29.35, 29.450000000000003, 29.55, 29.650000000000002, 29.75, 29.85, 29.950000000000003, 30.05, 30.150000000000002, 30.25, 30.35, 30.450000000000003, 30.55, 30.650000000000002, 30.75, 30.85, 30.950000000000003, 31.05, 31.150000000000002, 31.25, 31.35, 31.450000000000003, 31.55, 31.650000000000002, 31.75, 31.85, 31.950000000000003, 32.05, 32.150000000000006, 32.25, 32.35, 32.45, 32.550000000000004, 32.65, 32.75, 32.85, 32.95, 33.050000000000004, 33.15, 33.25, 33.35, 33.45, 33.550000000000004, 33.65, 33.75, 33.85, 33.95, 34.050000000000004, 34.15, 34.25, 34.35, 34.45, 34.550000000000004, 34.65, 34.75, 34.85, 34.95, 35.050000000000004, 35.15, 35.25, 35.35, 35.45, 35.550000000000004, 35.65, 35.75, 35.85, 35.95, 36.050000000000004, 36.15, 36.25, 36.35, 36.45, 36.550000000000004, 36.65, 36.75, 36.85, 36.95, 37.050000000000004, 37.15, 37.25, 37.35, 37.45, 37.550000000000004, 37.65, 37.75, 37.85, 37.95, 38.050000000000004, 38.15, 38.25, 38.35, 38.45, 38.550000000000004, 38.650000000000006, 38.75, 38.85, 38.95, 39.050000000000004, 39.150000000000006, 39.25, 39.35, 39.45, 39.550000000000004, 39.650000000000006, 39.75, 39.85, 39.95, 40.050000000000004, 40.150000000000006, 40.25], sections=[CrossCutSection(start_point=np.int16(168), end_point=274, volume=0.03325533384575725, top_diameter=np.float32(0.0), value=np.float32(12.637021), species_group='pinus sylvestris', timber_proportion=0.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.LogCull: 5>), CrossCutSection(start_point=np.int16(100), end_point=np.int16(168), volume=0.0931144950816689, top_diameter=np.float32(11.158686), value=np.float32(24.234894), species_group='pinus sylvestris', timber_proportion=1.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.TopLog: 3>), CrossCutSection(start_point=np.int16(45), end_point=np.int16(100), volume=0.10089169267225448, top_diameter=np.float32(14.508299), value=np.float32(39.877647), species_group='pinus sylvestris', timber_proportion=1.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.MiddleLog: 2>), CrossCutSection(start_point=np.int16(0), end_point=np.int16(45), volume=0.1061747827409899, top_diameter=np.float32(16.024956), value=np.float32(39.81026), species_group='pinus sylvestris', timber_proportion=1.0, pulp_proportion=0.0, cull_proportion=0.0, fuelwood_proportion=0.0, quality=<QualityType.ButtLog: 1>)])
[5]:
result.plot()
../_images/notebooks_timber_bucking_demo_10_0.png

Generate a small solution cube#

[6]:
cube = SolutionCube.generate(
    pricelist_data=MELLANSKOG_2013_PRICE_DATA,
    taper_model=EdgrenNylinder1949,
    species_list=["picea abies"],
    dbh_range=(20, 22),
    height_range=(15, 15.2),
    dbh_step=2,
    height_step=0.2,
    workers=1,
)
cube.dataset
Generating Solution Cube using 1 parallel processes...
Pricelist hash: 4058af6a860784c4a6ce94f561a4f484a0401624c2f3cc8405c3074570164174
Total trees to process: 4
Generating Solution Cube:   0%|          | 0/4 [00:00<?, ?it/s]/home/runner/work/pyforestry/pyforestry/src/pyforestry/base/pricelist/solutioncube.py:156: UserWarning: Error processing picea abies DBH=20 H=15.0: Provided object is not an instance of Timber.
  (worker_func(task) for task in tasks),
/home/runner/work/pyforestry/pyforestry/src/pyforestry/base/pricelist/solutioncube.py:156: UserWarning: Error processing picea abies DBH=20 H=15.2: Provided object is not an instance of Timber.
  (worker_func(task) for task in tasks),
/home/runner/work/pyforestry/pyforestry/src/pyforestry/base/pricelist/solutioncube.py:156: UserWarning: Error processing picea abies DBH=22 H=15.0: Provided object is not an instance of Timber.
  (worker_func(task) for task in tasks),
/home/runner/work/pyforestry/pyforestry/src/pyforestry/base/pricelist/solutioncube.py:156: UserWarning: Error processing picea abies DBH=22 H=15.2: Provided object is not an instance of Timber.
  (worker_func(task) for task in tasks),
Generating Solution Cube: 100%|██████████| 4/4 [00:00<00:00, 598.63it/s]

Finished parallel computation in 0.01 seconds.
Successfully created xarray Dataset.

[6]:
<xarray.Dataset> Size: 104B
Dimensions:            (species: 1, height: 2, dbh: 2)
Coordinates:
  * species            (species) object 8B 'picea abies'
  * height             (height) float64 16B 15.0 15.2
  * dbh                (dbh) int64 16B 20 22
Data variables:
    total_value        (species, height, dbh) float64 32B nan nan nan nan
    solution_sections  (species, height, dbh) object 32B '[]' '[]' '[]' '[]'
Attributes:
    pricelist_hash:     4058af6a860784c4a6ce94f561a4f484a0401624c2f3cc8405c30...
    taper_model:        EdgrenNylinder1949
    creation_date_utc:  2026-08-11T18:25:42.087472+00:00
    dbh_range:          20-22 cm
    height_range:       15-15.2 m
[7]:
cube.lookup(species="picea abies", dbh=20.0, height=15.0)
[7]:
(0.0, [])