8000 feat: complete Sonneveld recipe search, preparation and feedback by rafatahmed · Pull Request #266 · rafatahmed/Flahax · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ jobs:
matrix:
# Current CI acceptance covers Linux and Windows; macOS is excluded (#256).
os: [ubuntu-latest, windows-latest]
shard: [0, 1, 2, 3]
include:
- os: windows-latest
shard: 4
- os: windows-latest
shard: 5
shard: [0, 1, 2, 3, 4, 5]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
Expand All @@ -128,7 +123,7 @@ jobs:
run: |
python -m tools.ci_coverage record --candidate "$CANDIDATE_SHA" --output build/coverage
set +e
python -m coverage run -m tools.ci_parallel --output build/coverage --shard-index ${{ matrix.shard }} --shard-count ${{ matrix.os == 'ubuntu-latest' && 4 || 6 }} --workers 3 2>&1 | tee build/coverage/tests.log
python -m coverage run -m tools.ci_parallel --output build/coverage --shard-index ${{ matrix.shard }} --shard-count 6 --workers 3 2>&1 | tee build/coverage/tests.log
test_status=${PIPESTATUS[0]}
set -e
if [ ! -f build/coverage/tests.json ]; then
Expand Down
64 changes: 58 additions & 6 deletions docs/site/agronomist/batch-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,20 @@ flahax batch --formula "Tomato (Howard Resh)" --water well.json --water-share 8
- Put any additional fertilizers you have available in a product-record JSON
array and pass it with `--alternative-products`. If the first recipe misses a
nutrient target, batch refits the original and additional products together
with the same water, pH, targets and ceilings. The evaluated alternative and
its doses are saved in the remedy snapshot in the batch result. Review that
recipe before using it. To exclude a product, leave it out of both lists.
with the same water, pH, targets and ceilings. If the expanded fit still
misses, it can reuse a distinct joint starting point from the original fit
and try focused eligible product additions within
`--alternative-search-budget` (default 4). Repeat
`--alternative-search-preference` to prioritize relative residual,
incidental mg/L or product count after target attainment. The result retains
all evaluated recipes and identifies the best candidate found in this search.
For joint-fit misses, the saved calculation can include a lower bound on
analytical nutrient error for the attempted reagent and product pool. A
positive bound excludes an exact match in that pool's linear inventory model.
A zero bound leaves the question open. This check omits charge and phases
and leaves the normal pH and nutrient tolerance checks in force.
Review the proposed doses before using them. Leave excluded products out of
both product lists.
- `--volume` sets the batch size (default 1000 L), `--ph` the target pH
(default 5.8) and `--ratio` the injection ratio (default 1:100).

Expand Down Expand Up @@ -173,6 +184,34 @@ and the exact targets used. The input EC sets the paper's design strength.
The batch pH and modeled or measured EC are separate quantities. Use a feed
baseline appropriate to the crop and growing system. Root-zone measurements
can inform corrections chosen by the grower.

#### Use a root-zone sample to review the next feed

Add `root_zone_feedback` to the same design JSON. The following measurements
are illustrative. Use the nutrient concentrations from your root-zone sample
and the guide values for your crop and growing system.

```json
{
"sample_id": "tomato-bed-1",
"sampled_on": "2026-11-17",
"measured_mmol_l": {"K": 6.5, "Ca": 10.2, "Mg": 4.0},
"guide_mmol_l": {"K": 8.0, "Ca": 10.0, "P": 1.0}
}
```

1. Enter the proposed changes in `adjustments_mmol_l` and run the design with
`--preview-targets`. The preview shows measured minus guide, unmatched
nutrients, and the effect of the proposed changes on the full feed target.
For example, measured K of 6.5 against a guide of 8.0 gives a root-zone
difference of -1.5 mmol/L. The grower chooses the feed correction.
2. Review Columns A–D. When satisfied, add an `approval` object **inside**
`root_zone_feedback` with `approved_by`, `reason`, and the exact
`design_sha256` value from the preview.
3. Run the normal batch with the approved design and a separate source-water
analysis. Changing the sample, guide values, or feed design requires a fresh
preview and approval. The result stores the sample, comparison, and approval
alongside the targets it used.
### Rounded volumetric doses

Pass `--volumetric-stocks dosing.json` when some fertilizer doses are measured
Expand Down Expand Up @@ -203,9 +242,22 @@ The stock carrier replaces an equal volume of source water in the calculation.
The model retains the declared final solvent mass. The JSON result includes
the rounded doses, bounds on analytical nutrients, sampled pH, estimated EC
and the nominal saturation screen. The supplied nutrient ceilings still apply.
The stock bounds cover the stated concentration and dispensing errors. Dry
weighing accuracy, reagent assay uncertainty and stock stability require
separate evidence. The printable sheet gives the rounded quantities to review.
The printable sheet gives the rounded quantities to review. If the first sheet
misses a check, the batch can try adjacent dispensing increments and declared
stock strengths. Set `alternative_stock_concentrations_g_l` to a map from
stocked product names to lists of additional strengths. The search is bounded.
Set `nutrient_ceiling_reserve` to `0.05` to leave 5% headroom below each declared
nutrient ceiling in this rounded preparation check.

Use the optional `uncertainty` object to supply measured preparation bounds.
`dry_product_error_g` and `dry_product_assay_relative_error` are maps keyed by
dry product name. The latter applies the declared relative error uniformly to
that product's disclosed components. `reagent_mass_error_g` and
`reagent_volume_error_ml` describe dispensing error.
`reagent_mass_fraction_bounds` and `reagent_density_kg_l_bounds` are
two-element intervals for the liquid reagent. Use separately prepared stocks.
Stock dissolution, concentrate stability, and the actual mixed batch require
physical validation.
FlahaX writes three files into the new folder:
- `batch-sheet.html`: one printable page for the person mixing;
- `report.html`: the full report with charts;
Expand Down
6 changes: 6 additions & 0 deletions F438 docs/site/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ usage: flahax batch [-h] [--formula FORMULA | --crop CROP | --targets TARGETS |
[--limit SYMBOL=MG_PER_L] [--max-reagent-litres MAX_REAGENT_LITRES]
[--volumetric-stocks VOLUMETRIC_STOCKS]
[--alternative-products ALTERNATIVE_PRODUCTS]
[--alternative-search-preference {relative_residual,incidental_mg_l,product_count}]
[--alternative-search-budget ALTERNATIVE_SEARCH_BUDGET]
[--confirmed-product CATALOGUE_ID]
[--max-solid-reagent-grams MAX_SOLID_REAGENT_GRAMS] [--products PRODUCTS]
[--reagents REAGENTS] [--rules RULES] [--output OUTPUT]
Expand Down Expand Up @@ -153,6 +155,10 @@ options:
--alternative-products ALTERNATIVE_PRODUCTS
JSON array of available products to combine when nutrient targets are
missed
--alternative-search-preference {relative_residual,incidental_mg_l,product_count}
ranking preference after target attainment, repeat in priority order
--alternative-search-budget ALTERNATIVE_SEARCH_BUDGET
maximum expanded and focused alternative-product fits (default 4)
--confirmed-product CATALOGUE_ID
rerun an available excluded catalogue fertilizer as a separate remedy
--max-solid-reagent-grams MAX_SOLID_REAGENT_GRAMS
Expand Down
39 changes: 36 additions & 3 deletions src/flahax/batch_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ class BatchOptions:
max_solid_reagent_grams: float | None = None
confirmed_products: list[str] = field(default_factory=list)
alternative_products: str | None = None
alternative_search_preferences: tuple[str, ...] = ('relative_residual',)
alternative_search_budget: int = 4
volumetric_stocks: str | None = None
sonneveld_targets: str | None = None
reagents: str | None = None
Expand Down Expand Up @@ -620,7 +622,9 @@ def run_batch(options):
if options.sonneveld_targets is not None:
if any(value is not None for value in (options.targets, options.formula, options.crop)):
raise DeliveryError('invalid_value', 'Choose a single source of batch targets')
target_design = design_targets(read_json(options.sonneveld_targets, 'Sonneveld targets'))
target_design = design_targets(
read_json(options.sonneveld_targets, 'Sonneveld targets'), require_approval=True
)
targets = target_design['targets_mg_l']
title = options.title or Path(options.sonneveld_targets).stem
elif options.targets:
Expand Down Expand Up @@ -664,7 +668,19 @@ def run_batch(options):
if alternative_products:
from .evaluated_remedies import available_product_search

remedies.insert(0, available_product_search(alternative_products))
if joint is not None:
joint['check_inventory_relaxation'] = True
if (
type(options.alternative_search_budget) is not int
or options.alternative_search_budget < 1
):
raise DeliveryError('invalid_remedy', 'The alternative search budget must be positive')
remedies.insert(
0,
available_product_search(
alternative_products, preferences=options.alternative_search_preferences
),
)
additional_ids = [*options.confirmed_products, *(p['id'] for p in alternative_products)]
if joint is not None and additional_ids:
joint['additional_product_steps'] = dict.fromkeys(additional_ids, JOINT_DOSE_STEP_G_PER_L)
Expand Down Expand Up @@ -696,7 +712,8 @@ def run_batch(options):
else None
),
remedy_options=remedies,
max_remedy_evaluations=len(options.confirmed_products) + bool(alternative_products),
max_remedy_evaluations=len(options.confirmed_products)
+ (options.alternative_search_budget if alternative_products else 0),
)
summary = summarize_batch(
workflow,
Expand Down Expand Up @@ -792,6 +809,10 @@ def build(args):
max_solid_reagent_grams=args.max_solid_reagent_grams,
confirmed_products=args.confirmed_product,
alternative_products=args.alternative_products,
alternative_search_preferences=tuple(
args.alternative_search_preference or ['relative_residual']
),
alternative_search_budget=args.alternative_search_budget,
volumetric_stocks=args.volumetric_stocks,
ec=args.ec,
products=args.products,
Expand Down Expand Up @@ -885,6 +906,18 @@ def main(argv=None):
'--alternative-products',
help='JSON array of available products to combine when nutrient targets are missed',
)
parser.add_argument(
'--alternative-search-preference',
action='append',
choices=('relative_residual', 'incidental_mg_l', 'product_count'),
help='ranking preference after target attainment, repeat in priority order',
)
parser.add_argument(
'--alternative-search-budget',
type=int,
default=4,
help='maximum expanded and focused alternative-product fits (default 4)',
)
parser.add_argument(
'--confirmed-product',
action='append',
Expand Down
Loading
Loading
0