import%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20dataclasses%0A%20%20%20%20import%20time%0A%20%20%20%20import%20warnings%0A%20%20%20%20from%20datetime%20import%20date%2C%20timedelta%0A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20pydantic%0A%20%20%20%20import%20xarray%20as%20xr%0A%20%20%20%20from%20model_foundry.common.quantity_modules%20import%20recipe%0A%20%20%20%20from%20model_foundry.common.season_year%20import%20SeasonYear%2C%20valid_month_day%0A%0A%20%20%20%20from%20labkit.show%20import%20show%20%20%23%20a%20function's%20real%20source%2C%20linked%20to%20the%20pinned%20commit%0A%0A%20%20%20%20print(%22pandas%22%2C%20pd.__version__%2C%20%22%7C%20pydantic%22%2C%20pydantic.__version__%2C%20%22%7C%20xarray%22%2C%20xr.__version__)%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20import%20os%0A%20%20%20%20import%20tempfile%0A%0A%20%20%20%20from%20pydantic%20import%20TypeAdapter%2C%20ValidationError%0A%20%20%20%20from%20tf_data_ml_utils.features.core.season%20import%20create_season_year%0A%20%20%20%20from%20tf_data_ml_utils.growth_stages_ingest.process.stage_fit%20import%20(%0A%20%20%20%20%20%20%20%20allocate%2C%0A%20%20%20%20%20%20%20%20fit_stages%2C%0A%20%20%20%20%20%20%20%20load_commodity%2C%0A%20%20%20%20)%0A%20%20%20%20from%20tf_data_ml_utils.growth_stages_ingest.utils.doy%20import%20arc%2C%20circular_median%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%202.1%20%C2%B7%20Seasons%20and%20calendars%3A%20season%20years%2C%20crop%20calendars%20and%20phenology%20weights%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20How%20Python%20compares%20dates%2C%20strings%20and%20frozen%20dataclasses%2C%20and%20why%20a%20zero-padded%20%60%22MM-DD%22%60%20string%20sorts%20in%20calendar%20order.%0A%20%20%20%202.%20How%20model-foundry's%20%60SeasonYear%60%20turns%20any%20day%20into%20%22which%20crop%20season%20is%20this%3F%22%20in%20two%20lines%2C%20and%20how%20pydantic%20reuses%20its%20validator.%0A%20%20%20%203.%20Why%20the%20season%20*number*%20is%20a%20scientific%20decision%2C%20where%20our%20three%20repos%20disagree%20about%20it%2C%20and%20what%20pandas%20could%20have%20done%20instead.%0A%20%20%20%204.%20How%20crop%20calendars%20and%20phenology%20weights%20are%20built%20on%20a%20circular%20year%2C%20why%20a%20heatwave%20in%20mid-July%20counts%202.7%20times%20one%20in%20late%20June%2C%20and%20where%20production's%20weight%20curve%20is%20cut%20at%20New%20Year.%0A%0A%20%20%20%20**How%20to%20run**%0A%0A%20%20%20%20Follow%20the%20set-up%20in%20%5Bacademy%2FREADME.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md%23setting-up)%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%202-time-and-signals%2F2.1-the-season-year-calendar%2Fnotebook.py%60%3B%20marimo%20runs%20the%20cells%20in%20the%20order%20their%20results%20are%20needed%2C%20and%20re-runs%20the%20cells%20that%20depend%20on%20any%20cell%20you%20change.%20Every%20cell%20runs%20offline.%20model-foundry's%20code%20is%20imported%2C%20not%20copied%3A%20model-foundry-common%20at%20%5B%60f69eef9%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5)%2C%20pinned%20in%20%60pyproject.toml%60.%20tf-data-ml-utils'%20%60features%60%20and%20%60growth_stages_ingest%60%20modules%20are%20imported%20too%2C%20pinned%20at%20%60c0ba9dc%60%2C%20and%20so%20is%20model-foundry's%20%60phenoweight%60%200.5.0%2C%20the%20version%20data-pipelines%20locks.%20Only%20data-pipelines%20can't%20be%20installed%20here%20(it%20needs%20Dagster%20and%20more)%2C%20so%20its%20three%20short%20functions%20and%20two%20weight%20curves%20appear%20as%20verbatim%20copies%2C%20each%20with%20a%20permalink%20to%20the%20exact%20lines.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Part%201%20%C2%B7%20The%20Python%20underneath%0A%0A%20%20%20%20Four%20pieces%20of%20Python%20do%20all%20the%20work.%20Each%20is%20shown%20here%20the%20way%20its%20official%20documentation%20introduces%20it%2C%20before%20we%20look%20at%20our%20code.%0A%0A%20%20%20%20%23%23%23%201.1%20%60datetime.date%60%2C%20%60strftime%60%20and%20%60fromisoformat%60%0A%0A%20%20%20%20A%20%5B%60date%60%5D(https%3A%2F%2Fdocs.python.org%2F3.12%2Flibrary%2Fdatetime.html%23date-objects)%20is%20a%20calendar%20day%20with%20no%20time%20and%20no%20time%20zone.%20%60strftime%60%20formats%20it%20as%20text%20using%20%60%25%60%20codes%2C%20and%20%60fromisoformat%60%20parses%20ISO%208601%20text%20back%20into%20a%20date.%20The%20parser%20**knows%20the%20calendar**%3A%20it%20refuses%20days%20that%20don't%20exist.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_datetime_date_strftime()%3A%0A%20%20%20%20_d%20%3D%20date(2024%2C%202%2C%2029)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20_d%2C%20%22%7C%22%2C%20_d.strftime(%22%25m-%25d%22)%2C%20%22%7C%22%2C%20_d.strftime(%22%25d%20%25B%20%25Y%22)%2C%20%22%7C%20weekday%3A%22%2C%20_d.strftime(%22%25A%22)%0A%20%20%20%20)%0A%20%20%20%20print(date.fromisoformat(%222024-02-29%22)%2C%20%22parsed%20fine%3A%202024%20is%20a%20leap%20year%22)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20date.fromisoformat(%222023-02-29%22)%0A%20%20%20%20except%20ValueError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(%222023-02-29%20rejected%3A%22%2C%20exc)%0A%20%20%20%20print(%22Date%20arithmetic%20uses%20timedelta%3A%22%2C%20date(2024%2C%203%2C%201)%20-%20timedelta(days%3D1))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%201.2%20String%20comparison%20is%20lexicographic%0A%0A%20%20%20%20Python%20compares%20strings%20character%20by%20character%2C%20left%20to%20right%2C%20by%20Unicode%20code%20point%2C%20and%20the%20first%20difference%20decides.%20So%20%60%2210-01%22%20%3C%20%229-30%22%60%2C%20because%20%60%221%22%60%20comes%20before%20%60%229%22%60.%0A%0A%20%20%20%20When%20every%20field%20has%20a%20**fixed%20width**%20(zero-padded)%20and%20the%20**most%20significant%20field%20comes%20first**%2C%20alphabetical%20order%20*is*%20chronological%20order.%20That's%20exactly%20why%20ISO%208601%20writes%20%60YYYY-MM-DD%60%2C%20and%20%5BRFC%203339%20%C2%A75.1%5D(https%3A%2F%2Fdatatracker.ietf.org%2Fdoc%2Fhtml%2Frfc3339%23section-5.1)%20states%20it%20as%20a%20design%20goal.%20Our%20code%20relies%20on%20this%20for%20%60%22MM-DD%22%60.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_string_comparison_lexicographic()%3A%0A%20%20%20%20print('%2209-30%22%20%3C%20%2210-01%22%3A'%2C%20%2209-30%22%20%3C%20%2210-01%22%2C%20%22%20%20(zero-padded%3A%20correct)%22)%0A%20%20%20%20print('%229-30%22%20%20%3C%20%2210-01%22%3A'%2C%20%229-30%22%20%3C%20%2210-01%22%2C%20%22%20(unpadded%3A%20wrong)%22)%0A%0A%20%20%20%20days_2001%20%3D%20pd.date_range(%222001-01-01%22%2C%20%222001-12-31%22%2C%20freq%3D%22D%22)%0A%20%20%20%20_month_days%20%3D%20%5Bd.strftime(%22%25m-%25d%22)%20for%20d%20in%20days_2001%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22All%20365%20month-days%20of%202001%20sorted%20as%20strings%20%3D%3D%20calendar%20order%3A%22%2C%0A%20%20%20%20%20%20%20%20sorted(_month_days)%20%3D%3D%20_month_days%2C%0A%20%20%20%20)%0A%0A%20%20%20%20stamps%20%3D%20%5B%222024-11-05T09%3A00%3A00Z%22%2C%20%222023-12-31T23%3A59%3A59Z%22%2C%20%222024-01-01T00%3A00%3A00Z%22%5D%0A%20%20%20%20print(%22ISO%20timestamps%20sort%20by%20time%20too%3A%22%2C%20sorted(stamps))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%201.3%20%60%40dataclass(frozen%3DTrue)%60%20and%20%60__post_init__%60%0A%0A%20%20%20%20%5B%60dataclasses%60%5D(https%3A%2F%2Fdocs.python.org%2F3.12%2Flibrary%2Fdataclasses.html)%20write%20%60__init__%60%2C%20%60__repr__%60%20and%20%60__eq__%60%20for%20you%20from%20the%20class's%20annotated%20fields.%0A%0A%20%20%20%20-%20%60frozen%3DTrue%60%20makes%20instances%20**immutable**%3A%20assigning%20to%20a%20field%20raises%20%60FrozenInstanceError%60.%20Because%20an%20equal%20object%20can%20never%20change%2C%20Python%20can%20also%20give%20it%20a%20%60__hash__%60%2C%20so%20it%20can%20be%20a%20dictionary%20key%20or%20a%20set%20member.%0A%20%20%20%20-%20%60__post_init__%60%20runs%20straight%20after%20the%20generated%20%60__init__%60.%20It's%20the%20standard%20place%20to%20validate%20fields.%0A%0A%20%20%20%20This%20is%20the%20docs'%20pattern%2C%20with%20one%20validated%20field%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_dataclass_frozen_true()%3A%0A%20%20%20%20%40dataclasses.dataclass(frozen%3DTrue)%0A%20%20%20%20class%20Window%3A%0A%20%20%20%20%20%20%20%20start%3A%20str%0A%0A%20%20%20%20%20%20%20%20def%20__post_init__(self)%20-%3E%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20len(self.start)%20!%3D%205%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22%7Bself.start!r%7D%20is%20not%20MM-DD%22)%0A%0A%20%20%20%20w%20%3D%20Window(%2203-01%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20w%2C%0A%20%20%20%20%20%20%20%20%22%7C%20hashable%3A%22%2C%0A%20%20%20%20%20%20%20%20hash(w)%20%3D%3D%20hash(Window(%2203-01%22))%2C%0A%20%20%20%20%20%20%20%20%22%7C%20usable%20as%20a%20dict%20key%3A%22%2C%0A%20%20%20%20%20%20%20%20%7Bw%3A%20%22spring%22%7D%5BWindow(%2203-01%22)%5D%2C%0A%20%20%20%20)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20w.start%20%3D%20%2204-01%22%0A%20%20%20%20except%20dataclasses.FrozenInstanceError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(%22FrozenInstanceError%3A%22%2C%20exc)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20Window(%223-1%22)%0A%20%20%20%20except%20ValueError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(%22__post_init__%20rejected%3A%22%2C%20exc)%0A%0A%20%20%20%20%40dataclasses.dataclass%0A%20%20%20%20class%20MutableWindow%3A%0A%20%20%20%20%20%20%20%20start%3A%20str%0A%0A%20%20%20%20print(%22A%20plain%20(eq%3DTrue%2C%20not%20frozen)%20dataclass%20sets%20__hash__%20to%3A%22%2C%20MutableWindow.__hash__)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%201.4%20pydantic%20%60Annotated%5B...%2C%20AfterValidator(...)%5D%60%0A%0A%20%20%20%20%5BPydantic%5D(https%3A%2F%2Fgithub.com%2Fpydantic%2Fpydantic%2Fblob%2Fv2.14.3%2Fdocs%2Fconcepts%2Fvalidators.md)%20(2.14.3%2C%20as%20locked)%20validates%20data%20against%20type%20hints.%20%60Annotated%5Bstr%2C%20AfterValidator(f)%5D%60%20means%3A%20first%20check%20that%20the%20value%20is%20a%20%60str%60%2C%20then%20pass%20it%20through%20%60f%60%2C%20which%20returns%20the%20value%20or%20raises.%20The%20result%20is%20a%20reusable%20*type*%20that%20carries%20its%20own%20rule.%20The%20docs'%20first%20example%20is%20%22is%20this%20number%20even%3F%22%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_pydantic_annotated_aftervalidator()%3A%0A%20%20%20%20from%20typing%20import%20Annotated%0A%0A%20%20%20%20from%20pydantic%20import%20AfterValidator%0A%0A%20%20%20%20def%20is_even(value%3A%20int)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20if%20value%20%25%202%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20ValueError(f%22%7Bvalue%7D%20is%20not%20an%20even%20number%22)%0A%20%20%20%20%20%20%20%20return%20value%0A%0A%20%20%20%20EvenNumber%20%3D%20Annotated%5Bint%2C%20AfterValidator(is_even)%5D%0A%20%20%20%20print(TypeAdapter(EvenNumber).validate_python(4))%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20TypeAdapter(EvenNumber).validate_python(3)%0A%20%20%20%20except%20ValidationError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20print(exc.errors()%5B0%5D%5B%22msg%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Part%202%20%C2%B7%20How%20our%20production%20code%20does%20it%0A%0A%20%20%20%20model-foundry%20represents%20a%20crop%20season%20with%20one%2055-line%20file%2C%20%5B%60common%2Fseason_year.py%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fseason_year.py).%20Its%20convention%20is%20that%20season%20year%20%60y%60%20is%20**the%20twelve%20months%20ending%20on%20%60last_day%60%20of%20calendar%20year%20%60y%60**.%20A%20season%20is%20named%20after%20the%20year%20it%20*ends*%20in.%20For%20a%20crop%20that%20is%20the%20harvest%20year%2C%20the%20year%20NASS%20and%20IBGE%20label%20the%20crop%20with.%20The%20official%20numbers%20themselves%20arrive%20months%20later%2C%20which%20is%20why%20vintages%20(1.4)%20matter.%0A%0A%20%20%20%20Here%20is%20the%20real%20source%2C%20imported%20from%20the%20pinned%20package%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_production_code()%3A%0A%20%20%20%20show(valid_month_day)%0A%20%20%20%20show(SeasonYear)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20%60of%60%20slowly.%20It%20is%20all%20of%20Part%201%20in%20two%20lines%3A%0A%0A%20%20%20%201.%20%60day.strftime(%22%25m-%25d%22)%60%20turns%20the%20date%20into%20a%20zero-padded%20month-day%20(1.1).%0A%20%20%20%202.%20%60.replace(%2202-29%22%2C%20%2202-28%22)%60%20folds%20the%20leap%20day%20onto%2028%20February.%0A%20%20%20%203.%20%60month_day%20%3C%3D%20self.last_day%60%20is%20a%20**string%20comparison**%20(1.2).%20If%20the%20day%20is%20on%20or%20before%20the%20season's%20last%20day%2C%20the%20season%20ends%20this%20calendar%20year%3B%20otherwise%20it%20ends%20next%20year.%0A%0A%20%20%20%20%60valid_month_day%60%20validates%20by%20**round%20trip**.%20It%20builds%20%602001-%7Bvalue%7D%60%2C%20parses%20it%2C%20formats%20it%20back%2C%20and%20requires%20the%20exact%20same%20text.%20The%20round%20trip%20matters%20because%20%60fromisoformat%60%20accepts%20more%20than%20%60YYYY-MM-DD%60%3A%20since%20Python%203.11%20it%20parses%20the%20ISO%20*week%20date*%20%602001-W39-1%60%20as%2024%20September%202001%2C%20so%20a%20parse-only%20guard%20would%20accept%20%60%22W39-1%22%60.%20Using%202001%2C%20a%20non-leap%20year%2C%20means%20%60%2202-29%22%60%20can%20never%20be%20a%20season's%20last%20day%20(%5BL9-17%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fseason_year.py%23L9-L17)).%0A%0A%20%20%20%20The%20same%20validator%20becomes%20a%20pydantic%20type%20in%20the%20recipe%20models%20(%5B%60recipe.py%60%20L44%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Frecipe.py%23L44))%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_production_code_2()%3A%0A%20%20%20%20print(%22MonthDay%20%3D%22%2C%20recipe.MonthDay)%0A%20%20%20%20adapter%20%3D%20TypeAdapter(recipe.MonthDay)%0A%20%20%20%20for%20value%20in%20%5B%2208-31%22%2C%20%229-30%22%2C%20%2202-29%22%2C%20%2213-01%22%2C%20%22W39-1%22%5D%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bvalue!r%3A8%7D%20-%3E%22%2C%20adapter.validate_python(value))%0A%20%20%20%20%20%20%20%20except%20ValidationError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%7Bvalue!r%3A8%7D%20-%3E%20ValidationError%3A%22%2C%20exc.errors()%5B0%5D%5B%22msg%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%202.1%20Real%20crop%20calendars%0A%0A%20%20%20%20Recipes%20write%20their%20season%20end%20as%20a%20phenology%20anchor%2C%20such%20as%20%60last_day%3A%20harvest.end-7%60%2C%20which%20is%20resolved%20from%20a%20pinned%20stage%20seed%20on%20S3%20(2.4).%20The%20month-days%20below%20are%20taken%20from%20the%20recipes'%20own%20comments%20at%20%60f69eef9%60%2C%20and%202.4%20resolves%20the%20real%20anchors%20to%20confirm%20them%3A%0A%0A%20%20%20%20-%20US%20corn%2C%20soy%20and%20cotton%3A%20%22season%20y%20is%201%20November%20y-1%20to%2031%20October%20y%22.%0A%20%20%20%20-%20US%20winter%20wheat%3A%20%22season%202025%20is%202%20August%202024%20%E2%80%A6%20to%201%20August%202025%22.%0A%20%20%20%20-%20Brazil%20soy%20yield%3A%20%22season%202024%20is%20May%202023%20to%20April%202024%22.%0A%20%20%20%20-%20UK%20winter%20wheat%3A%20the%20literal%20%60%2209-30%22%60.%0A%0A%20%20%20%20The%20US%20soybean%20marketing%20year%20(1%20September%20to%2031%20August)%20is%20added%20as%20a%20trade%20reference.%20It%20is%20a%20*US*%20trade%20calendar%2C%20not%20a%20crop%20season%3A%20see%20the%20warning%20after%20the%20table.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_real_crop_calendars()%3A%0A%20%20%20%20calendars%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22calendar%20year%22%3A%20SeasonYear(%2212-31%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20corn%20%2F%20soy%20%2F%20cotton%22%3A%20SeasonYear(%2210-31%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20winter%20wheat%22%3A%20SeasonYear(%2208-01%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20(yield)%22%3A%20SeasonYear(%2204-30%22)%2C%0A%20%20%20%20%20%20%20%20%22UK%20winter%20wheat%22%3A%20SeasonYear(%2209-30%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20soy%20marketing%20year%22%3A%20SeasonYear(%2208-31%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20days%20%3D%20%5B%0A%20%20%20%20%20%20%20%20date(2024%2C%201%2C%2015)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%204%2C%2030)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%205%2C%201)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%208%2C%201)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%208%2C%202)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%2011%2C%201)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3A%20%5Bf%22%7Bsy.of(d)%7D%20(%7Bsy.label(sy.of(d))%7D)%22%20for%20d%20in%20days%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20sy%20in%20calendars.items()%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20index%3D%5Bd.isoformat()%20for%20d%20in%20days%5D%2C%0A%20%20%20%20)%0A%20%20%20%20table%0A%20%20%20%20return%20(calendars%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Each%20column%20is%20one%20crop's%20view%20of%20the%20same%20six%20days.%20Look%20at%20the%20Brazil%20column%3A%2030%20April%202024%20closes%20season%20%602024%60%20(%222023%2F24%22)%2C%20and%20the%20next%20day%20already%20belongs%20to%20%602025%60.%20%60label%60%20writes%20the%20trade's%20two-year%20name%2C%20except%20for%20a%2031%20December%20season%2C%20which%20keeps%20a%20single%20year.%0A%0A%20%20%20%20**The%20same%20text%20can%20mean%20two%20different%20seasons.**%20On%2015%20January%202024%20the%20Brazil%20and%20US-marketing-year%20columns%20both%20print%20%602024%20(2023%2F24)%60%2C%20but%20they%20are%20not%20the%20same%20crop%20year%3A%0A%0A%20%20%20%20-%20US%20marketing%20year%202023%2F24%20sells%20the%20**2023**%20US%20harvest%2C%20which%20the%20US%20recipes%20(%60%2210-31%22%60)%20call%20season%20**2023**.%0A%20%20%20%20-%20Brazil's%202023%2F24%20crop%20is%20harvested%20in%20early%202024%2C%20which%20the%20Brazil%20recipe%20calls%20season%20**2024**.%0A%0A%20%20%20%20So%20turning%20%60%222023%2F24%22%60%20into%20an%20integer%20depends%20on%20the%20country.%20Production's%20WASDE%20reader%20takes%20the%20first%20four%20characters%2C%20%60marketing_year.str.slice(0%2C%204)%60%20(%5Byield_module%20%60features%2Fwasde.py%60%20L41%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_module%2Fsrc%2Fyield_module%2Ffeatures%2Fwasde.py%23L41))%2C%20and%20keeps%20only%20%60united_states%60%20rows%20(L26).%20That%20is%20right%20for%20the%20US%20and%20would%20be%20one%20season%20early%20for%20Brazil.%20science.md%20maps%20each%20agency's%20label%20onto%20%60SeasonYear%60%20integers.%0A%0A%20%20%20%20%23%23%23%202.2%20Edge%20cases%3A%20the%20year%20boundary%20and%20the%20leap%20day%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_edge_cases_year()%3A%0A%20%20%20%20sy%20%3D%20SeasonYear(%2202-28%22)%0A%20%20%20%20for%20_d%20in%20%5B%0A%20%20%20%20%20%20%20%20date(2024%2C%202%2C%2028)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%202%2C%2029)%2C%0A%20%20%20%20%20%20%20%20date(2024%2C%203%2C%201)%2C%0A%20%20%20%20%20%20%20%20date(2023%2C%202%2C%2028)%2C%0A%20%20%20%20%20%20%20%20date(2023%2C%203%2C%201)%2C%0A%20%20%20%20%5D%3A%0A%20%20%20%20%20%20%20%20print(_d%2C%20%22-%3E%20season%22%2C%20sy.of(_d))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22end_date(2024)%20for%20a%20'02-28'%20season%3A%22%2C%0A%20%20%20%20%20%20%20%20sy.end_date(2024)%2C%0A%20%20%20%20%20%20%20%20%22(the%20special%20case%20in%20end_date%20keeps%20the%20leap%20day)%22%2C%0A%20%20%20%20)%0A%0A%20%20%20%20new_year%20%3D%20SeasonYear(%2212-31%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%2231%20Dec%202024%20-%3E%22%2C%0A%20%20%20%20%20%20%20%20new_year.of(date(2024%2C%2012%2C%2031))%2C%0A%20%20%20%20%20%20%20%20%22%7C%201%20Jan%202025%20-%3E%22%2C%0A%20%20%20%20%20%20%20%20new_year.of(date(2025%2C%201%2C%201))%2C%0A%20%20%20%20)%0A%0A%20%20%20%20for%20_bad%20in%20%5B%229-30%22%2C%20%2202-29%22%5D%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20SeasonYear(_bad)%0A%20%20%20%20%20%20%20%20except%20ValueError%20as%20exc%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22rejected%3A%22%2C%20exc)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Without%20the%20fold%2C%2029%20February%202024%20would%20compare%20as%20%60%2202-29%22%20%3E%20%2202-28%22%60%20and%20land%20in%20season%20**2025**%2C%20a%20whole%20season%20late%2C%20while%20the%20days%20either%20side%20of%20it%20were%20placed%20correctly.%20Comment%20out%20the%20fold%20in%20your%20head%20and%20predict%20the%20second%20line%20above.%0A%0A%20%20%20%20%23%23%23%202.3%20Dates%2C%20ends%20and%20progress%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_dates_ends_progress(calendars)%3A%0A%20%20%20%20us_my%20%3D%20calendars%5B%22US%20soy%20marketing%20year%22%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22date_of('09-15'%2C%202024)%3A%22%2C%0A%20%20%20%20%20%20%20%20us_my.date_of(%2209-15%22%2C%202024)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20date_of('03-15'%2C%202024)%3A%22%2C%0A%20%20%20%20%20%20%20%20us_my.date_of(%2203-15%22%2C%202024)%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22end_date(2024)%3A%22%2C%20us_my.end_date(2024))%0A%20%20%20%20for%20_d%20in%20%5Bdate(2023%2C%209%2C%201)%2C%20date(2024%2C%203%2C%201)%2C%20date(2024%2C%208%2C%2031)%5D%3A%0A%20%20%20%20%20%20%20%20print(f%22progress_of(%7B_d%7D)%20%3D%20%7Bus_my.progress_of(_d)%3A.6f%7D%22)%0A%20%20%20%20return%20(us_my%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%202.4%20Stage%20anchors%20and%20the%20%60%25%20365%60%20wrap%0A%0A%20%20%20%20%5B%60stage_mmdd%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Frecipe.py%23L134-L150)%20parses%20an%20anchor%20such%20as%20%60ripening.end%2B14%60%20with%20a%20regular%20expression.%20It%20looks%20the%20stage%20up%20in%20the%20pinned%20seed%20and%20converts%20the%20day%20of%20year%20with%20%60date(2001%2C%201%2C%201)%20%2B%20timedelta((day%20-%201)%20%25%20365)%60.%0A%0A%20%20%20%20The%20real%20seed%20is%20on%20S3.%20The%20code%20reads%20%60MI_CURATED_STAGE_SEED%60%20first%2C%20so%20a%20**toy**%20seed%20(made-up%20stage%20days%2C%20labelled%20as%20such)%20lets%20us%20run%20the%20production%20function%20offline%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_stage_anchors_365()%3A%0A%20%20%20%20import%20json%0A%0A%20%20%20%20toy%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22records%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22crop%22%3A%20%22toy_crop%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22country%22%3A%20%22XX%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22stages%22%3A%20%7B%22planting%22%3A%20%5B20%2C%2060%5D%2C%20%22ripening%22%3A%20%5B200%2C%20240%5D%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%0A%20%20%20%20seed_path%20%3D%20os.path.join(tempfile.mkdtemp()%2C%20%22toy_seed.json%22)%0A%20%20%20%20with%20open(seed_path%2C%20%22w%22)%20as%20_fh%3A%0A%20%20%20%20%20%20%20%20json.dump(toy%2C%20_fh)%0A%20%20%20%20os.environ%5B%22MI_CURATED_STAGE_SEED%22%5D%20%3D%20seed_path%0A%20%20%20%20recipe.stage_seed.cache_clear()%0A%0A%20%20%20%20for%20_anchor%20in%20%5B%22ripening.end%2B14%22%2C%20%22planting.start%22%2C%20%22planting.start-49%22%5D%3A%0A%20%20%20%20%20%20%20%20print(f%22%7B_anchor%3A18%7D%20-%3E%20%7Brecipe.stage_mmdd(_anchor%2C%20crop%3D'toy_crop'%2C%20country%3D'XX')%7D%22)%0A%20%20%20%20show(recipe.stage_mmdd)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60planting.start-49%60%20is%20day%2020%20%E2%88%92%2049%20%3D%20%E2%88%9229.%20The%20%60%25%20365%60%20wraps%20that%20backwards%20past%201%20January%20to%202%20December.%20Python's%20%60%25%60%20always%20returns%20a%20non-negative%20result%20for%20a%20positive%20divisor%2C%20which%20is%20what%20makes%20this%20one-liner%20safe.%0A%0A%20%20%20%20%23%23%23%202.5%20%60NON_LEAP_SEASON%20%3D%202002%60%0A%0A%20%20%20%20%5B%60InitDates.in_season%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Frecipe.py%23L101-L107)%20lays%20the%20forecast%20schedule%20out%20once%20in%20a%20reference%20season%20and%20copies%20the%20*month-days*%20into%20every%20real%20season.%20So%20forecasts%20go%20out%20on%20the%20same%20calendar%20dates%20every%20year%2C%20leap%20or%20not.%0A%0A%20%20%20%20The%20reference%20season%20must%20not%20contain%20a%2029%20February.%20Season%202002%20spans%202001%E2%80%932002%2C%20and%20neither%20is%20a%20leap%20year.%20Season%202001%20under%20a%20%60%2201-31%22%60%20season%20spans%20February%202000%2C%20which%20**is**%20a%20leap%20year%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%23%20The%20real%20anchors%2C%20offline%0A%0A%20%20%20%20tf-data-ml-utils%20ships%20a%20copy%20of%20the%20curated%20stage%20seed%20inside%20its%20wheel%20(%60growth_stages_ingest%2Freference%2Fmi_curated_phenology_seed.json%60).%20It%20is%20a%20**copy%2C%20not%20the%20pinned%20S3%20object**%20(%60commodity-growth-stages%2Freference%2F0.0.230%60)%2C%20and%20the%20two%20files%20are%20not%20byte-identical%2C%20but%20they%20resolve%20every%20anchor%20below%20to%20the%20same%20month-day.%20Pointing%20%60MI_CURATED_STAGE_SEED%60%20at%20it%20resolves%20the%20real%20recipes'%20anchors%2C%20as%20written%20in%20their%20yamls%20at%20%60f69eef9%60%20(%5BBrazil%20soy%20L3%2C%20L9-10%20and%20L18%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_soy_brazil%2Fsrc%2Fyield_soy_brazil%2Fsoybeans_bra.yaml%23L1-L18))%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_non_leap_season()%3A%0A%20%20%20%20from%20importlib.resources%20import%20files%0A%0A%20%20%20%20bundled_seed%20%3D%20(%0A%20%20%20%20%20%20%20%20files(%22tf_data_ml_utils.growth_stages_ingest%22)%0A%20%20%20%20%20%20%20%20%2F%20%22reference%22%0A%20%20%20%20%20%20%20%20%2F%20%22mi_curated_phenology_seed.json%22%0A%20%20%20%20)%0A%20%20%20%20os.environ%5B%22MI_CURATED_STAGE_SEED%22%5D%20%3D%20str(bundled_seed)%0A%20%20%20%20recipe.stage_seed.cache_clear()%0A%0A%20%20%20%20anchors%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20last_day%22%3A%20(%22ripening.end%2B14%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20first%20init%20date%22%3A%20(%22ripening.end%2B15%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20z_ndvi_dec_mar%20start%22%3A%20(%22planting.end%2B5%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20z_ndvi_dec_mar%20end%22%3A%20(%22ripening.end-16%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20planting%20stage%20start%22%3A%20(%22planting.start%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20planting%20stage%20end%22%3A%20(%22planting.end%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20harvest%20stage%20start%22%3A%20(%22harvest.start%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20harvest%20stage%20end%22%3A%20(%22harvest.end%22%2C%20%22soybean%22%2C%20%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20corn%20last_day%22%3A%20(%22ripening.end%2B14%22%2C%20%22corn%22%2C%20%22United%20States%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20soy%20last_day%22%3A%20(%22harvest.end-6%22%2C%20%22soybean%22%2C%20%22United%20States%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20cotton%20last_day%22%3A%20(%22ripening.end%2B29%22%2C%20%22cotton%22%2C%20%22United%20States%22)%2C%0A%20%20%20%20%20%20%20%20%22US%20winter%20wheat%20last_day%22%3A%20(%22harvest.end-7%22%2C%20%22wheat%22%2C%20%22United%20States%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20for%20_name%2C%20(_anchor%2C%20crop%2C%20country)%20in%20anchors.items()%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_name%3A34%7D%20%7B_anchor%3A17%7D%20-%3E%20%7Brecipe.stage_mmdd(_anchor%2C%20crop%3Dcrop%2C%20country%3Dcountry)%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20brazil%20%3D%20SeasonYear(recipe.stage_mmdd(%22ripening.end%2B14%22%2C%20crop%3D%22soybean%22%2C%20country%3D%22Brazil%22))%0A%20%20%20%20brazil_inits%20%3D%20recipe.InitDates(%0A%20%20%20%20%20%20%20%20first%3Drecipe.stage_mmdd(%22ripening.end%2B15%22%2C%20crop%3D%22soybean%22%2C%20country%3D%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20last%3Drecipe.stage_mmdd(%22ripening.end%2B14%22%2C%20crop%3D%22soybean%22%2C%20country%3D%22Brazil%22)%2C%0A%20%20%20%20%20%20%20%20every_days%3D7%2C%0A%20%20%20%20)%0A%20%20%20%20schedule%20%3D%20brazil_inits.in_season(brazil%2C%202024)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22Brazil%20soy%20season%202024%3A%20%7Blen(schedule)%7D%20weekly%20init%20dates%2C%20%7Bschedule%5B0%5D%7D%20to%20%7Bschedule%5B-1%5D%7D%22%0A%20%20%20%20)%0A%20%20%20%20return%20brazil%2C%20bundled_seed%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20So%20the%20Brazil%20soy%20yaml's%20comment%20is%20right%3A%20the%20season%20ends%20on%20**30%20April**%2C%20and%20the%20yield%20model%20issues%20**53**%20weekly%20forecasts%20a%20season%2C%20from%201%20May%20to%2030%20April.%20The%20model's%20README%2C%20which%20says%20the%20season%20ends%20on%2031%20August%20with%2035%20init%20dates%20from%201%20September%20to%2027%20April%2C%20is%20stale.%20The%20NDVI%20window%20%60z_ndvi_dec_mar%60%20really%20is%201%20December%20to%2031%20March.%20The%20harvest%20stage%20runs%20from%2016%20April%20to%2025%20May%2C%20so%20the%2030%20April%20season%20boundary%20splits%20it%3B%20science.md%20draws%20the%20season%20on%20a%20timeline.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_non_leap_season_2()%3A%0A%20%20%20%20show(recipe.InitDates)%0A%20%20%20%20print(%22NON_LEAP_SEASON%20%3D%22%2C%20recipe.NON_LEAP_SEASON)%0A%0A%0A%40app.cell%0Adef%20s2_5_non_leap_season_3(us_my)%3A%0A%20%20%20%20jan%20%3D%20SeasonYear(%2201-31%22)%0A%20%20%20%20for%20_reference%20in%20(2001%2C%202002)%3A%0A%20%20%20%20%20%20%20%20_span%20%3D%20jan.date_of(%2203-15%22%2C%20_reference)%20-%20jan.date_of(%2202-01%22%2C%20_reference)%0A%20%20%20%20%20%20%20%20print(f%22reference%20season%20%7B_reference%7D%3A%201%20Feb%20-%3E%2015%20Mar%20is%20%7B_span.days%7D%20days%22)%0A%0A%20%20%20%20init%20%3D%20recipe.InitDates(first%3D%2202-01%22%2C%20last%3D%2204-01%22%2C%20every_days%3D14)%0A%20%20%20%20print(%22init%20dates%2C%20season%202024%20(leap)%3A%22%2C%20%5Bd.isoformat()%20for%20d%20in%20init.in_season(us_my%2C%202024)%5D)%0A%20%20%20%20print(%22init%20dates%2C%20season%202025%20%20%20%20%20%20%20%3A%22%2C%20%5Bd.isoformat()%20for%20d%20in%20init.in_season(us_my%2C%202025)%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Both%20seasons%20get%20the%20same%20five%20month-days.%20Laid%20out%20in%20a%20leap%20season%20instead%2C%20two%20weeks%20after%2015%20February%20would%20be%2029%20February%20and%20every%20March%20date%20would%20move%20a%20day%3B%20the%20reference%20season%202002%20makes%20sure%2029%20February%20is%20never%20issued.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%202.6%20Stage%20calendars%3A%20growth%20stages%20on%20a%20circle%0A%0A%20%20%20%20%60SeasonYear%60%20needs%20one%20date%2C%20the%20day%20a%20season%20ends.%20A%20crop%20**calendar**%20gives%20every%20growth%20stage%20its%20own%20days.%20tf-data-ml-utils%20builds%20these%20calendars%20in%20%60growth_stages_ingest%60%2C%20in%20days%20of%20the%20year%20(DOY)%20on%20a%20365-day%20**circle**%3A%20day%20365%20is%20followed%20by%20day%201.%20The%20stage%20seed%20whose%20anchors%202.4%20resolved%20is%20a%20curated%20calendar%20of%20the%20same%20kind%2C%20shipped%20in%20the%20same%20package.%0A%0A%20%20%20%20Start%20with%20the%20simplest%20question%3A%20what%20is%20the%20typical%20planting%20day%20of%20a%20crop%20planted%20from%201%20December%20to%2018%20January%3F%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_stage_calendars_growth()%3A%0A%20%20%20%20from%20tf_data_ml_utils.growth_stages_ingest.utils.doy%20import%20circular_split%0A%0A%20%20%20%20planting_doys%20%3D%20np.array(%5B335%2C%20345%2C%20352%2C%20360%2C%203%2C%2010%2C%2018%5D)%20%20%23%201%20December%20to%2018%20January%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22np.median%3A%20%7Bnp.median(planting_doys)%3A.0f%7D%20%20%20mean%3A%20%7Bplanting_doys.mean()%3A.1f%7D%22%0A%20%20%20%20%20%20%20%20f%22%20%20%20circular_median%3A%20%7Bcircular_median(planting_doys)%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22arc(300%2C%2060)%20%3D%22%2C%0A%20%20%20%20%20%20%20%20arc(300%2C%2060)%2C%0A%20%20%20%20%20%20%20%20%22days%3B%20arc(60%2C%2060)%20%3D%22%2C%0A%20%20%20%20%20%20%20%20arc(60%2C%2060)%2C%0A%20%20%20%20%20%20%20%20%22(equal%20days%20mean%20a%20full%20year)%22%2C%0A%20%20%20%20)%0A%20%20%20%20two_seasons%20%3D%20np.array(%5B335%2C%20345%2C%20352%2C%20360%2C%203%2C%2010%2C%2018%2C%20150%2C%20160%5D)%0A%20%20%20%20print(%22circular_split%3A%22%2C%20%5Btwo_seasons%5Bi%5D.tolist()%20for%20i%20in%20circular_split(two_seasons)%5D)%0A%20%20%20%20print()%0A%20%20%20%20show(circular_median)%0A%20%20%20%20return%20(planting_doys%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60np.median%60%20returns%20the%20*earliest*%20planting%20(335%2C%201%20December)%2C%20because%20the%20numbers%20restart%20at%203%20after%20360.%20The%20mean%20is%20worse%3A%20203%20is%2022%20July%2C%20the%20middle%20of%20the%20off-season.%20%60circular_median%60%20finds%20the%20largest%20empty%20gap%20on%20the%20circle%20(the%20off-season)%2C%20measures%20every%20day%20from%20the%20end%20of%20that%20gap%2C%20and%20takes%20an%20ordinary%20median%20there%3A%20360%2C%2026%20December.%20%60circular_split%60%20first%20separates%20seasons%20at%20gaps%20of%2060%20days%20or%20more%2C%20and%20rejoins%20the%20two%20pieces%20that%20meet%20across%20New%20Year.%20Note%20%60arc(60%2C%2060)%60%3A%20equal%20days%20mean%20a%20whole%20year%2C%20not%20an%20empty%20span.%0A%0A%20%20%20%20With%20a%20planting%20day%20and%20a%20maturity%20day%2C%20%60fit_stages%60%20lays%20out%20the%20stages%20(%5Bstage_fit.py%20L133-188%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fgrowth_stages_ingest%2Fprocess%2Fstage_fit.py%23L133-L188)).%20It%20picks%20the%20variety%20class%20whose%20cycle%20length%20contains%20the%20span%2C%20scales%20the%20published%20typical%20stage%20lengths%20(FAO-56%20Table%2011%20for%20soybean)%20to%20fit%20it%2C%20and%20labels%20the%20rest%20of%20the%20year%20%60off_season%60.%20A%20Brazil%20soy%20crop%20planted%20on%201%20November%20(DOY%20305)%20and%20mature%20on%2010%20March%20(DOY%2069)%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_stage_calendars_growth_2()%3A%0A%20%20%20%20soybean%20%3D%20load_commodity(%22soybean%22)%0A%20%20%20%20print(%22source%3A%22%2C%20soybean%5B%22selected_source%22%5D)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22variety%20classes%3A%22%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(c%5B%22variety_class%22%5D%2C%20c%5B%22cycle_days_min%22%5D%2C%20c%5B%22cycle_days_max%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20c%20in%20soybean%5B%22variety_classes%22%5D%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%20%20%20%20stage_rows%2C%20why_not%20%3D%20fit_stages(305%2C%2069%2C%20soybean)%0A%20%20%20%20assert%20why_not%20is%20None%2C%20why_not%0A%20%20%20%20print(f%22%5Cnspan%20arc(305%2C%2069)%20%3D%20%7Barc(305%2C%2069)%3A.0f%7D%20days%20-%3E%20%7Bstage_rows%5B0%5D%5B'variety_class'%5D%7D%22)%0A%20%20%20%20for%20stage_row%20in%20stage_rows%3A%0A%20%20%20%20%20%20%20%20stage_first%20%3D%20date(2001%2C%201%2C%201)%20%2B%20timedelta(days%3Dstage_row%5B%22start_doy%22%5D%20-%201)%0A%20%20%20%20%20%20%20%20stage_last%20%3D%20date(2001%2C%201%2C%201)%20%2B%20timedelta(days%3Dstage_row%5B%22end_doy%22%5D%20-%201)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bstage_row%5B'stage'%5D%3A18%7D%20DOY%20%7Bstage_row%5B'start_doy'%5D%3A3d%7D%20-%3E%20%7Bstage_row%5B'end_doy'%5D%3A3d%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20%20%7Bstage_first%3A%25d%20%25b%7D%20-%3E%20%7Bstage_last%3A%25d%20%25b%7D%20%20%20%7Bstage_row%5B'duration_days'%5D%3A3d%7D%20days%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(soybean%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Flowering%20runs%20from%2024%20December%20to%2013%20January%2C%20across%20New%20Year%2C%20with%20no%20special%20case%3A%20%60at()%60%20wraps%20with%20%60%25%60.%20%60allocate%60%20scales%20each%20typical%20length%20by%20span%20%C3%B7%20typical%20total%2C%20clamps%20it%20to%20the%20stage's%20published%20bounds%2C%20then%20moves%20single%20days%20until%20the%20total%20is%20exact.%20Reproductive%20stages%20last%20about%20the%20same%20time%20whatever%20the%20season%20length%2C%20so%20only%20the%20stage%20marked%20%60elastic%60%20(here%20%60vegetative%60)%20may%20grow%20past%20its%20maximum.%20Below%2C%20190%20days%20is%20longer%20than%20this%20class's%20155-day%20maximum%2C%20chosen%20only%20to%20show%20that%20branch%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_stage_calendars_growth_3(soybean)%3A%0A%20%20%20%20mid_group%20%3D%20next(%0A%20%20%20%20%20%20%20%20c%20for%20c%20in%20soybean%5B%22variety_classes%22%5D%20if%20c%5B%22variety_class%22%5D%20%3D%3D%20%22mid_maturity_group%22%0A%20%20%20%20)%0A%20%20%20%20pre_harvest%20%3D%20%5Bs%20for%20s%20in%20mid_group%5B%22stages%22%5D%20if%20s%5B%22stage%22%5D%20!%3D%20%22harvest%22%5D%0A%20%20%20%20for%20s%20in%20pre_harvest%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bs%5B'stage'%5D%3A18%7D%20min%20%7Bs%5B'min_days'%5D%3A3d%7D%20%20typical%20%7Bs%5B'typical_days'%5D%3A3d%7D%20%20max%20%7Bs%5B'max_days'%5D%3A3d%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20elastic%3D%7Bbool(s.get('elastic'))%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20for%20_span%20in%20(129%2C%20190%2C%2060)%3A%0A%20%20%20%20%20%20%20%20print(f%22allocate(%7B_span%7D)%20-%3E%22%2C%20allocate(_span%2C%20pre_harvest))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20-%20**129%20days**%20fits%20inside%20the%20bounds%2C%20so%20every%20stage%20is%20close%20to%20its%20typical%20length%20scaled%20by%20129%20%C3%B7%20126.%0A%20%20%20%20-%20**190%20days**%20is%20longer%20than%20every%20maximum%20added%20up%20(180)%2C%20so%20each%20stage%20takes%20its%20maximum%20and%20%60vegetative%60%20takes%20the%20other%2010%20days.%0A%20%20%20%20-%20**60%20days**%20is%20shorter%20than%20every%20minimum%20added%20up%20(73)%2C%20so%20%60allocate%60%20returns%20%60None%60%2C%20and%20%60fit_stages%60%20returns%20a%20reason%20instead%20of%20a%20calendar.%0A%0A%20%20%20%20%23%23%23%202.7%20Phenology%20weights%0A%0A%20%20%20%20A%20hot%20day%20at%20flowering%20hurts%20a%20crop%20more%20than%20a%20hot%20day%20a%20month%20earlier.%20A%20**phenology%20weight**%20says%20how%20much%20each%20day%20of%20the%20year%20counts.%20model-foundry's%20%60phenoweight%60%20builds%20the%20curve%20from%20a%20few%20bell%20shapes%20and%20normalises%20it%20over%20a%20366-day%20ring.%20These%20are%20the%20two%20functions%20production%20calls%20into%2C%20from%20the%20pinned%20package%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_phenology_weights()%3A%0A%20%20%20%20from%20phenoweight%20import%20Kernel%2C%20Profile%2C%20weights_xr%0A%20%20%20%20from%20phenoweight%20import%20core%20as%20pw_core%0A%20%20%20%20from%20phenoweight%20import%20xr%20as%20pw_xr%0A%0A%20%20%20%20show(pw_core.sum_components)%0A%20%20%20%20show(pw_xr._profile_ring)%0A%20%20%20%20print(%22RING_DAYS%20%3D%22%2C%20pw_xr.RING_DAYS)%0A%20%20%20%20return%20Kernel%2C%20Profile%2C%20pw_core%2C%20pw_xr%2C%20weights_xr%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Each%20bell%20is%20%60amplitude%20*%20exp(-0.5%20*%20((doys%20-%20mu)%20%2F%20sigma)%20**%202)%60%3A%20a%20shape%2C%20not%20a%20probability%20density.%20The%20bells%20are%20added%2C%20**clipped%20to%20%5B0%2C%201%5D**%2C%20and%20divided%20by%20their%20total%20over%20all%20366%20slots%2C%20so%20the%20slots%20sum%20to%201.%20A%20season%20reads%20365%20or%20366%20of%20them%20by%20day%20of%20year%2C%20so%20its%20weights%20sum%20to%20about%201%2C%20and%20a%20weighted%20sum%20of%20daily%20values%20behaves%20like%20a%20weighted%20average%2C%20in%20the%20daily%20values'%20own%20units.%0A%0A%20%20%20%20The%20curves%20themselves%20are%20chosen%20in%20data-pipelines'%20configs%3A%20%5Bera5%2Fconfigs%2Fconus.yaml%20L106-112%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fa332e4c41870deb7daf7425edeae7578ab933ed3%2Fsrc%2Ftreefera_pipelines%2Fpipelines%2Fera5%2Fconfigs%2Fconus.yaml%23L106-L112)%20for%20US%20corn%20and%20%5Bera5%2Fconfigs%2Fbrazil.yaml%20L121-140%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fa332e4c41870deb7daf7425edeae7578ab933ed3%2Fsrc%2Ftreefera_pipelines%2Fpipelines%2Fera5%2Fconfigs%2Fbrazil.yaml%23L121-L140)%20for%20Brazil%20soy.%20data-pipelines%20can't%20be%20installed%20here%2C%20so%20the%20next%20cell%20copies%20two%20of%20them%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_phenology_weights_2(Kernel%2C%20Profile%2C%20pw_xr%2C%20weights_xr)%3A%0A%20%20%20%20def%20make_profile(kernels%3A%20list%5Bdict%5D)%20-%3E%20Profile%3A%0A%20%20%20%20%20%20%20%20%22%22%22A%20phenoweight%20Profile%20from%20config-style%20kernels%2C%20built%20as%20tf-data-ml-utils%20builds%20it.%22%22%22%0A%20%20%20%20%20%20%20%20return%20Profile(%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20variable%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20components%3Dtuple(Kernel(type%3D%22gaussian%22%2C%20params%3Dk)%20for%20k%20in%20kernels)%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20data-pipelines%20a332e4c%3A%20era5%2Fconfigs%2Fconus.yaml%20L111%20and%20brazil.yaml%20L131%0A%20%20%20%20corn_heat%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%20194%2C%20%22sigma%22%3A%209%2C%20%22amplitude%22%3A%201.0%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%20212%2C%20%22sigma%22%3A%2014%2C%20%22amplitude%22%3A%200.45%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%20175%2C%20%22sigma%22%3A%2012%2C%20%22amplitude%22%3A%200.18%7D%2C%0A%20%20%20%20%5D%0A%20%20%20%20bra_deficit%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%2035%2C%20%22sigma%22%3A%2013%2C%20%22amplitude%22%3A%200.95%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%2015%2C%20%22sigma%22%3A%2011%2C%20%22amplitude%22%3A%200.5%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%2055%2C%20%22sigma%22%3A%2012%2C%20%22amplitude%22%3A%200.3%7D%2C%0A%20%20%20%20%20%20%20%20%7B%22mu%22%3A%20290%2C%20%22sigma%22%3A%2016%2C%20%22amplitude%22%3A%200.4%7D%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20year_2023%20%3D%20pd.date_range(%222023-01-01%22%2C%20%222023-12-31%22)%0A%20%20%20%20corn_weights%20%3D%20weights_xr(make_profile(corn_heat)%2C%20year_2023)%0A%20%20%20%20deficit_weights%20%3D%20weights_xr(make_profile(bra_deficit)%2C%20year_2023)%0A%20%20%20%20corn_raw%20%3D%20pw_xr._profile_ring(make_profile(corn_heat)%2C%20normalise%3DFalse)%0A%20%20%20%20flat_top%20%3D%20np.flatnonzero(corn_raw%20%3E%3D%201)%20%2B%201%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22corn%3A%20the%20bells%20sum%20past%201%20on%20DOY%20%7Bflat_top.min()%7D-%7Bflat_top.max()%7D%20(%7Bflat_top.size%7D%20days)%20and%20are%20clipped%20flat%3B%22%0A%20%20%20%20%20%20%20%20f%22%20the%202023%20weights%20sum%20to%20%7Bfloat(corn_weights.sum())%3A.6f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.2))%0A%20%20%20%20_ax.plot(year_2023%2C%20corn_weights%2C%20label%3D%22US%20corn%2C%20EDD%20(conus.yaml)%22)%0A%20%20%20%20_ax.plot(year_2023%2C%20deficit_weights%2C%20label%3D%22Brazil%20soy%2C%20precip%20deficit%20(brazil.yaml)%22)%0A%20%20%20%20_ax.set_ylabel(%22weight%20per%20day%22)%0A%20%20%20%20_ax.set_title(%22Phenology%20weights%20over%202023%22)%0A%20%20%20%20_ax.xaxis.set_major_formatter(plt.matplotlib.dates.DateFormatter(%22%25b%22))%0A%20%20%20%20_ax.legend(fontsize%3D8)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20bra_deficit%2C%20corn_heat%2C%20make_profile%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20US%20corn's%20curve%20is%20centred%20on%20DOY%20194%20(13%20July)%2C%20around%20silking%2C%20but%20its%20side%20bells%20push%20the%20sum%20past%201%20from%207%20to%2021%20July%2C%20so%20the%20clip%20gives%20it%20a%20flat%20top%3A%20those%2015%20days%20count%20the%20same.%20Brazil%20soy's%20curve%20has%20bells%20in%20January%20and%20February%20(flowering%20and%20pod%20fill)%20and%20one%20in%20October%20(planting%20rains).%20Notice%20that%20it%20falls%20to%20almost%20nothing%20on%2031%20December%20and%20jumps%20on%201%20January%3B%203.3%20comes%20back%20to%20that.%0A%0A%20%20%20%20tf-data-ml-utils%20applies%20the%20weights%20in%20%60compute_phenoweight_signal%60%20(%5Bindices.py%20L515-562%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Ffeatures%2Fstages%2Findices%2Findices.py%23L515-L562))%3A%20it%20computes%20the%20daily%20base%20signal%20(here%20EDD%2C%20degrees%20of%20daily%20maximum%20above%2029%20%C2%B0C)%2C%20multiplies%20it%20by%20the%20weights%2C%20and%20returns%20the%20weighted%20daily%20series%20that%20the%20config's%20%60transform%3A%20cumsum%60%20accumulates%20over%20the%20season.%20Its%20feature%20stages%20refuse%20to%20import%20without%20a%20spatial%20registry%2C%20which%20data-pipelines%20owns%3B%20a%20one-entry%20stand-in%20is%20enough%2C%20because%20nothing%20below%20looks%20a%20region%20up.%0A%0A%20%20%20%20Now%20the%20point%20of%20the%20weighting.%20The%20same%20five-day%20heatwave%2C%2035%20%C2%B0C%20maxima%20and%20so%206%20EDD%20a%20day%2C%20at%20two%20dates%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_phenology_weights_3(corn_heat)%3A%0A%20%20%20%20import%20yaml%0A%0A%20%20%20%20registry_path%20%3D%20os.path.join(tempfile.mkdtemp()%2C%20%22registry.yaml%22)%0A%20%20%20%20with%20open(registry_path%2C%20%22w%22)%20as%20_fh%3A%0A%20%20%20%20%20%20%20%20yaml.safe_dump(%7B%22spatial_ids%22%3A%20%7B%22demo%22%3A%20%7B%22bbox%22%3A%20%5B-60.0%2C%20-30.0%2C%20-40.0%2C%20-10.0%5D%7D%7D%7D%2C%20_fh)%0A%20%20%20%20os.environ%5B%22WEATHER_EXTRA_SPATIAL_REGISTRY_PATHS%22%5D%20%3D%20registry_path%0A%20%20%20%20from%20tf_data_ml_utils.features.stages.indices.indices%20import%20compute_phenoweight_signal%0A%0A%20%20%20%20season_2023%20%3D%20pd.date_range(%222023-04-01%22%2C%20%222023-10-31%22)%0A%0A%20%20%20%20def%20heatwave_score(first_day%3A%20str)%20-%3E%20float%3A%0A%20%20%20%20%20%20%20%20%22%22%22Phenology-weighted%20EDD%20of%20a%205-day%2035%20%C2%B0C%20heatwave%20in%20a%2025%20%C2%B0C%20season%2C%20through%20the%20production%20signal.%22%22%22%0A%20%20%20%20%20%20%20%20tmax%20%3D%20np.full(season_2023.size%2C%2025.0)%0A%20%20%20%20%20%20%20%20tmax%5Bseason_2023.isin(pd.date_range(first_day%2C%20periods%3D5))%5D%20%3D%2035.0%0A%20%20%20%20%20%20%20%20weighted%20%3D%20compute_phenoweight_signal(%0A%20%20%20%20%20%20%20%20%20%20%20%20xr.Dataset(%7B%22tasmax%22%3A%20(%22time%22%2C%20tmax)%7D%2C%20coords%3D%7B%22time%22%3A%20season_2023%7D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22tasmax%22%3A%20%22tasmax%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22base_signal%22%3A%20%22edd%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22base_params%22%3A%20%7B%22thresh%22%3A%2029.0%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22kernels%22%3A%20corn_heat%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22anchor%22%3A%20%7B%22type%22%3A%20%22calendar%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20float(weighted.sum())%0A%0A%20%20%20%20for%20_first_day%20in%20(%222023-06-24%22%2C%20%222023-07-11%22)%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22heatwave%20from%20%7B_first_day%7D%3A%20raw%20EDD%2030.0%2C%20phenology-weighted%20%7Bheatwave_score(_first_day)%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20heatwave_score%2C%20season_2023%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20same%2030%20EDD%20counts%202.7%20times%20as%20much%20in%20mid-July%20as%20in%20late%20June.%20A%20season%20total%20of%20raw%20EDD%20can't%20tell%20the%20two%20apart.%0A%0A%20%20%20%20%23%23%23%202.8%20Temporal%20support%3A%20what%20one%20sample%20stands%20for%0A%0A%20%20%20%20Before%20weighting%2C%20%60compute_phenoweight_signal%60%20asks%20which%20days%20each%20sample%20stands%20for%20(%60daily_support%60)%2C%20evaluates%20the%20weights%20on%20those%20days%2C%20and%20totals%20them%20per%20sample%20(%60integrate_to_samples%60).%20For%20daily%20ERA5%20nothing%20changes.%20A%2016-day%20composite%20gets%20the%20sum%20of%20its%2016%20days'%20weights%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_8_temporal_support_sample(bra_deficit%2C%20make_profile%2C%20weights_xr)%3A%0A%20%20%20%20from%20tf_data_ml_utils.features.core.cadence%20import%20(%0A%20%20%20%20%20%20%20%20cadence_days%2C%0A%20%20%20%20%20%20%20%20daily_support%2C%0A%20%20%20%20%20%20%20%20integrate_to_samples%2C%0A%20%20%20%20%20%20%20%20sample_support_days%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(sample_support_days)%0A%20%20%20%20composites%20%3D%20xr.DataArray(%0A%20%20%20%20%20%20%20%20pd.to_datetime(%5B%222023-11-17%22%2C%20%222023-12-03%22%2C%20%222023-12-19%22%2C%20%222024-01-01%22%5D)%2C%20dims%3D%22time%22%0A%20%20%20%20)%0A%20%20%20%20composite_support%20%3D%20daily_support(composites)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%2216-day%20composites%3A%20cadence%22%2C%0A%20%20%20%20%20%20%20%20cadence_days(composites)%2C%0A%20%20%20%20%20%20%20%20%22days%3B%20days%20per%20sample%22%2C%0A%20%20%20%20%20%20%20%20composite_support.per_sample.tolist()%2C%0A%20%20%20%20)%0A%20%20%20%20deficit_on_days%20%3D%20weights_xr(make_profile(bra_deficit)%2C%20composite_support.days)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22Brazil%20soy%20deficit%20weight%20per%20composite%3A%22%2C%0A%20%20%20%20%20%20%20%20integrate_to_samples(deficit_on_days%2C%20composite_support).values.round(5).tolist()%2C%0A%20%20%20%20)%0A%0A%20%20%20%20pentads%20%3D%20xr.DataArray(%0A%20%20%20%20%20%20%20%20pd.to_datetime(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%222023-%7Bmonth%3A02d%7D-%7Bstart%3A02d%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20month%20in%20(1%2C%202%2C%203)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20start%20in%20(1%2C%206%2C%2011%2C%2016%2C%2021%2C%2026)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20dims%3D%22time%22%2C%0A%20%20%20%20)%0A%20%20%20%20pentad_support%20%3D%20daily_support(pentads)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22pentads%2C%20Jan-Mar%202023%3A%20cadence%22%2C%0A%20%20%20%20%20%20%20%20cadence_days(pentads)%2C%0A%20%20%20%20%20%20%20%20%22days%3B%20days%20per%20sample%22%2C%0A%20%20%20%20%20%20%20%20pentad_support.per_sample.tolist()%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22days%20covered%3A%22%2C%0A%20%20%20%20%20%20%20%20pentad_support.days.size%2C%0A%20%20%20%20%20%20%20%20%22of%22%2C%0A%20%20%20%20%20%20%20%20(date(2023%2C%203%2C%2031)%20-%20date(2023%2C%201%2C%201)).days%20%2B%201%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20-%20**The%20cap.**%20Each%20sample%20owns%20the%20days%20up%20to%20the%20next%20sample%2C%20but%20never%20more%20than%20the%20usual%20spacing%20(the%20cadence)%2C%20so%20a%20sample%20after%20missing%20data%20doesn't%20claim%20the%20gap.%20The%20last%20sample%20gets%20one%20cadence.%0A%20%20%20%20-%20**16-day%20composites.**%2019%20December's%20composite%20covers%20only%20the%2013%20days%20to%2031%20December%2C%20because%20the%20series%20restarts%20on%201%20January%3A%20right.%20Its%20Brazil%20soy%20weight%20is%20almost%20zero%2C%20while%201%20January's%20composite%20gets%200.129.%0A%20%20%20%20-%20**Pentads.**%20January's%20sixth%20pentad%20runs%20six%20days%2C%2026%20to%2031%20January%2C%20but%20the%20cap%20gives%20it%20five%2C%20so%20the%2031st's%20weight%20goes%20to%20no%20sample.%20February's%20sixth%20pentad%20has%20three%20days%20and%20gets%20three.%20This%20is%20**latent**%3A%20only%20daily%20ERA5%20feeds%20phenoweight%20heads%20today.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Part%203%20%C2%B7%20The%20science%3A%20one%20integer%20per%20season%2C%20and%20which%20days%20count%0A%0A%20%20%20%20A%20season%20number%20is%20not%20bookkeeping.%20It's%20the%20**join%20key**%20between%20four%20things%20that%20are%20built%20separately%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20features%20%20%20%20%20%20%20%20%20%20%20%20(weather%2C%20NDVI%2C%20%E2%80%A6)%20%20%20%20%E2%94%80%E2%94%90%0A%20%20%20%20truth%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(NASS%2C%20IBGE%2C%20CONAB)%20%20%20%E2%94%80%E2%94%BC%E2%94%80%E2%94%80%20joined%20on%20(geography%2C%20season_year)%0A%20%20%20%20walk-forward%20splits%20(train%20on%20%E2%89%A4%20y%E2%88%921)%20%20%20%20%20%20%E2%94%80%E2%94%A4%0A%20%20%20%20deliveries%20%20%20%20%20%20%20%20%20%20(what%20a%20client%20sees)%20%20%E2%94%80%E2%94%98%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20If%20one%20of%20them%20numbers%20Brazil's%202023%2F24%20crop%20as%20%602023%60%20and%20another%20as%20%602024%60%2C%20the%20join%20still%20succeeds.%20It%20just%20pairs%20the%20wrong%20harvest%20with%20the%20wrong%20weather%2C%20and%20the%20model%20learns%20from%20noise.%20No%20error%20is%20raised.%0A%0A%20%20%20%20%23%23%23%203.1%20Our%20repos%20use%20two%20conventions%2C%20function%20by%20function%0A%0A%20%20%20%20-%20**End%20year%3A**%20model-foundry's%20%60SeasonYear%60%20(above)%3B%20tf-data-ml-utils'%20%5B%60season_year_by_end%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Ffeatures%2Fcore%2Fseason.py%23L107-L110)%3B%20data-pipelines'%20%60last_season%60%20(below).%0A%20%20%20%20-%20**Start%20year%3A**%20tf-data-ml-utils'%20%5B%60create_season_year%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Ffeatures%2Fcore%2Fseason.py%23L67-L75)%2C%20which%20takes%20the%20season's%20*first*%20day%20and%20labels%20days%20before%20it%20%60year%20-%201%60%3B%20data-pipelines'%20%60season_year_valid_time%60%20(below).%0A%0A%20%20%20%20So%20the%20split%20is%20not%20one%20repo%20against%20another%3A%20tf-data-ml-utils%20and%20data-pipelines%20each%20hold%20both.%0A%0A%20%20%20%20The%20features%20module%20ships%20as%20its%20own%20wheel%2C%20%60tf-data-ml-utils-features%60%2C%20installed%20here%20through%20the%20base%20package's%20%60%5Bfeatures%5D%60%20extra.%20The%20next%20cell%20imports%20the%20real%20functions%20and%20prints%20their%20source.%20The%20cell%20after%20it%20runs%20the%20same%20Brazil%20soy%20season%2C%20a%20May%20start%20and%20an%20April%20end%2C%20through%20both%20conventions%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_repos_use_two()%3A%0A%20%20%20%20from%20tf_data_ml_utils.features.core.season%20import%20mmdd_to_doy%2C%20mmdd_to_doy_for_time%0A%0A%20%20%20%20for%20fn%20in%20(mmdd_to_doy%2C%20mmdd_to_doy_for_time%2C%20create_season_year)%3A%0A%20%20%20%20%20%20%20%20show(fn)%0A%0A%0A%40app.cell%0Adef%20s3_1_repos_use_two_2()%3A%0A%20%20%20%20season_days%20%3D%20xr.DataArray(pd.date_range(%222022-05-01%22%2C%20%222024-04-30%22%2C%20freq%3D%22D%22)%2C%20dims%3D%22time%22)%0A%20%20%20%20start_year%20%3D%20create_season_year(season_days%2C%20start_mmdd%3D%2205-01%22).values%0A%20%20%20%20end_year%20%3D%20np.array(%0A%20%20%20%20%20%20%20%20%5BSeasonYear(%2204-30%22).of(d)%20for%20d%20in%20pd.DatetimeIndex(season_days.values).date%5D%0A%20%20%20%20)%0A%20%20%20%20compare%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22date%22%3A%20pd.DatetimeIndex(season_days.values)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tf-data-ml-utils%20(start%20year)%22%3A%20start_year%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22model-foundry%20(end%20year)%22%3A%20end_year%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20print(compare.iloc%5B%5B0%2C%20244%2C%20364%2C%20365%2C%20730%5D%5D.to_string(index%3DFalse))%0A%20%20%20%20print(%22End%20minus%20start%20is%20always%22%2C%20set(end_year%20-%20start_year))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20difference%20is%20exactly%201%20on%20every%20day%20of%20a%20wrapping%20season.%20Both%20functions%20are%20correct%3B%20they%20answer%20to%20different%20conventions.%0A%0A%20%20%20%20data-pipelines%20also%20has%20both.%20Its%20%5B%60season_year_valid_time%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fa332e4c41870deb7daf7425edeae7578ab933ed3%2Fsrc%2Ftreefera_pipelines%2Fcommon%2Fsignals%2Fpublish.py%23L146-L157)%20says%20when%20a%20%60season_year%60%20value%20becomes%20known%2C%20and%20reads%20the%20value%20as%20a%20**start**%20year%3A%20%60end_year%20%3D%20season_year%20%2B%201%20if%20wraps%60.%0A%0A%20%20%20%20data-pipelines%20(Dagster%20and%20more)%20isn't%20installed%20here%2C%20so%20the%20next%20cell%20holds%20a%20**verbatim%20copy**%20of%20those%20lines%2C%20together%20with%20its%20%60last_season%60%20(%5Bconfig.py%20L321-324%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftreefera-data-pipelines%2Fblob%2Fa332e4c41870deb7daf7425edeae7578ab933ed3%2Fsrc%2Ftreefera_pipelines%2Fpipelines%2Fmarket_intelligence_new%2Fconfig.py%23L321-L324))%2C%20which%20says%20it%20mirrors%20%60SeasonYear.of%60%20and%20so%20counts%20by%20**end**%20year%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_repos_use_two_3()%3A%0A%20%20%20%20from%20collections.abc%20import%20Mapping%0A%0A%20%20%20%20def%20_wraps_year(start_mmdd%3A%20str%2C%20end_mmdd%3A%20str)%20-%3E%20bool%3A%0A%20%20%20%20%20%20%20%20%22%22%22True%20when%20a%20season%20crosses%20the%20calendar%20boundary%20(end%20on%2Fbefore%20start).%22%22%22%0A%20%20%20%20%20%20%20%20%23%20KEEP%3A%20mirrors%20the%20wheel's%20SeasonDescriptor.wraps_year%20(end_mmdd%20%3C%3D%20start_mmdd)%3B%20wheel-free.%0A%20%20%20%20%20%20%20%20return%20end_mmdd%20%3C%3D%20start_mmdd%0A%0A%20%20%20%20def%20season_year_valid_time(season_year%3A%20int%2C%20start_mmdd%3A%20str%2C%20end_mmdd%3A%20str)%20-%3E%20date%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20calendar%20date%20a%20%60season_year%60-dimensioned%20value%20becomes%20fully%20known.%22%22%22%0A%20%20%20%20%20%20%20%20wraps%20%3D%20_wraps_year(start_mmdd%2C%20end_mmdd)%0A%20%20%20%20%20%20%20%20end_year%20%3D%20season_year%20%2B%201%20if%20wraps%20else%20season_year%0A%20%20%20%20%20%20%20%20month%2C%20day%20%3D%20int(end_mmdd%5B%3A2%5D)%2C%20int(end_mmdd%5B3%3A%5D)%0A%20%20%20%20%20%20%20%20return%20date(end_year%2C%20month%2C%20day)%0A%0A%20%20%20%20def%20last_season(recipe%3A%20Mapping%2C%20run_date%3A%20date)%20-%3E%20int%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20season%20containing%20%60%60run_date%60%60%2C%20as%20model-foundry's%20%60%60SeasonYear.of%60%60%3A%20the%20next%20to%20end%20on%20or%20after%20it.%22%22%22%0A%20%20%20%20%20%20%20%20last_day%20%3D%20recipe%5B%22season_year%22%5D%5B%22last_day%22%5D%0A%20%20%20%20%20%20%20%20return%20run_date.year%20if%20run_date.strftime(%22%25m-%25d%22)%20%3C%3D%20last_day%20else%20run_date.year%20%2B%201%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22data-pipelines%3A%20Brazil%20soy%20season_year%202023%20is%20fully%20known%20on%22%2C%0A%20%20%20%20%20%20%20%20season_year_valid_time(2023%2C%20%2205-01%22%2C%20%2204-30%22)%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22model-foundry%20%3A%20Brazil%20soy%20season%202023%20ends%20on%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%2C%0A%20%20%20%20%20%20%20%20SeasonYear(%2204-30%22).end_date(2023)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(last_season%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%22Season%202023%22%20is%20a%20year%20apart%20in%20the%20two%20repos%2C%20and%20nothing%20stops%20a%202023%20signal%20joining%20to%20a%202023%20truth.%0A%0A%20%20%20%20The%20%60last_season%60%20copy%20also%20leaves%20out%20the%20leap-day%20fold%20(29%20February%20%E2%86%92%2028%20February).%20Here%20is%20a%20sweep%20over%20every%20valid%20%60last_day%60%20and%20every%20day%20of%20the%20leap%20year%202028%2C%20comparing%20it%20with%20the%20real%20class%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_repos_use_two_4(last_season)%3A%0A%20%20%20%20every_last_day%20%3D%20%5Bd.strftime(%22%25m-%25d%22)%20for%20d%20in%20pd.date_range(%222001-01-01%22%2C%20%222001-12-31%22)%5D%0A%20%20%20%20days_2028%20%3D%20pd.date_range(%222028-01-01%22%2C%20%222028-12-31%22).date%0A%20%20%20%20disagree%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(ld%2C%20d)%0A%20%20%20%20%20%20%20%20for%20ld%20in%20every_last_day%0A%20%20%20%20%20%20%20%20for%20d%20in%20days_2028%0A%20%20%20%20%20%20%20%20if%20last_season(%7B%22season_year%22%3A%20%7B%22last_day%22%3A%20ld%7D%7D%2C%20d)%20!%3D%20SeasonYear(ld).of(d)%0A%20%20%20%20%5D%0A%20%20%20%20print(f%22%7Blen(every_last_day)%20*%20len(days_2028)%3A%2C%7D%20pairs%20checked%3B%20disagreements%3A%20%7Bdisagree%7D%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Exactly%20one%20pair%20disagrees%3A%20a%20%60%2202-28%22%60%20season%20on%2029%20February.%20It's%20latent%20today%2C%20but%20it's%20the%20kind%20of%20bug%20a%20copied%20two-liner%20hides.%0A%0A%20%20%20%20%23%23%23%203.2%20Leap%20years%20move%20a%20%22progress%20of%20season%22%20feature%0A%0A%20%20%20%20%60progress_of%60%20divides%20by%20the%20number%20of%20days%20from%20the%20season's%20first%20day%20to%20its%20last%3A%20364%2C%20or%20365%20when%20the%20season%20contains%20a%2029%20February.%20That%20is%20the%20number%20of%20days%20*after*%20the%20first%2C%20so%20the%20first%20day%20scores%20exactly%200%20and%20the%20last%20exactly%201.%20The%20**same%20calendar%20day**%20is%20therefore%20a%20slightly%20different%20number%20in%20a%20leap%20season%2C%20and%20models%20use%20this%20as%20a%20feature.%0A%0A%20%20%20%20Take%20one%20month-day%20in%20a%20leap%20season%20and%20in%20a%20non-leap%20one%2C%20and%20let%20k%20be%20the%20days%20elapsed%20at%20it%20in%20the%20non-leap%20season%3A%0A%0A%20%20%20%20-%20before%2029%20February%20the%20gap%20is%20k%2F365%20%E2%88%92%20k%2F364%20%3D%20**%E2%88%92k%20%2F%20(364%20%C2%B7%20365)**%3A%20the%20leap%20season%20is%20*behind*%2C%20because%20it%20still%20has%20an%20extra%20day%20to%20come%3B%0A%20%20%20%20-%20from%201%20March%20the%20leap%20season%20has%20counted%20k%20%2B%201%20days%2C%20so%20the%20gap%20is%20(k%20%2B%201)%2F365%20%E2%88%92%20k%2F364%20%3D%20**(364%20%E2%88%92%20k)%20%2F%20(364%20%C2%B7%20365)**%3A%20it%20is%20*ahead*.%0A%0A%20%20%20%20The%20gap%20jumps%20at%2029%20February%20and%20changes%20sign.%20Where%2029%20February%20falls%20in%20the%20season%20decides%20how%20big%20each%20side%20is%2C%20so%20we%20plot%20four%20seasons%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_leap_years_move()%3A%0A%20%20%20%20_month_days%20%3D%20pd.date_range(%222001-01-01%22%2C%20%222001-12-31%22).strftime(%22%25m-%25d%22)%0A%20%20%20%20seasons%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%2208-31%22%3A%20%22US%20soy%20marketing%20year%22%2C%0A%20%20%20%20%20%20%20%20%2210-31%22%3A%20%22US%20corn%2C%20soy%2C%20cotton%22%2C%0A%20%20%20%20%20%20%20%20%2204-30%22%3A%20%22Brazil%20soy%22%2C%0A%20%20%20%20%20%20%20%20%2212-31%22%3A%20%22calendar%20year%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20def%20leap_gap(last_day%3A%20str)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22progress_of%20in%20leap%20season%202024%20minus%20non-leap%20season%202023%2C%20per%20month-day%3B%20checked%20against%20the%20formula.%22%22%22%0A%20%20%20%20%20%20%20%20sy%20%3D%20SeasonYear(last_day)%0A%20%20%20%20%20%20%20%20first_2023%20%3D%20sy.end_date(2022)%20%2B%20timedelta(days%3D1)%0A%20%20%20%20%20%20%20%20gaps%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20md%20in%20_month_days%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20gap%20%3D%20sy.progress_of(sy.date_of(md%2C%202024))%20-%20sy.progress_of(sy.date_of(md%2C%202023))%0A%20%20%20%20%20%20%20%20%20%20%20%20k%20%3D%20(sy.date_of(md%2C%202023)%20-%20first_2023).days%0A%20%20%20%20%20%20%20%20%20%20%20%20before_leap_day%20%3D%20sy.date_of(md%2C%202024)%20%3C%20date(2024%2C%202%2C%2029)%0A%20%20%20%20%20%20%20%20%20%20%20%20formula%20%3D%20-k%20%2F%20(364%20*%20365)%20if%20before_leap_day%20else%20(364%20-%20k)%20%2F%20(364%20*%20365)%0A%20%20%20%20%20%20%20%20%20%20%20%20assert%20abs(gap%20-%20formula)%20%3C%201e-12%2C%20(last_day%2C%20md)%0A%20%20%20%20%20%20%20%20%20%20%20%20gaps.append(gap)%0A%20%20%20%20%20%20%20%20return%20np.array(gaps)%0A%0A%20%20%20%20x%20%3D%20pd.to_datetime(%222001-%22%20%2B%20_month_days)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.5))%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_last_day%2C%20_name%20in%20seasons.items()%3A%0A%20%20%20%20%20%20%20%20gap%20%3D%20leap_gap(_last_day)%0A%20%20%20%20%20%20%20%20_ax.plot(x%2C%20gap%2C%20label%3Df'%22%7B_last_day%7D%22%20%7B_name%7D')%0A%20%20%20%20%20%20%20%20rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22last_day%22%3A%20_last_day%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22season%22%3A%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22gap%20on%2028%20Feb%22%3A%20round(gap%5B_month_days.get_loc(%2202-28%22)%5D%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22gap%20on%201%20Mar%22%3A%20round(gap%5B_month_days.get_loc(%2203-01%22)%5D%2C%206)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22largest%20%7Cgap%7C%20in%20days%20(x%20365)%22%3A%20round(np.abs(gap).max()%20*%20365%2C%202)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22grey%22%2C%20linewidth%3D0.8)%0A%20%20%20%20_ax.axvline(%0A%20%20%20%20%20%20%20%20pd.Timestamp(%222001-02-28%2012%3A00%22)%2C%20color%3D%22grey%22%2C%20linestyle%3D%22%3A%22%2C%20label%3D%2229%20Feb%20(leap%20day)%22%0A%20%20%20%20)%0A%20%20%20%20_ax.set_title(%22Leap%20seasons%20lag%20before%2029%20February%20and%20lead%20after%20it%22)%0A%20%20%20%20_ax.set_xlabel(%22month-day%20(the%20same%20in%20both%20seasons)%22)%0A%20%20%20%20_ax.set_ylabel(%22progress_of(2024)%20%E2%88%92%20progress_of(2023)%22)%0A%20%20%20%20_ax.xaxis.set_major_formatter(plt.matplotlib.dates.DateFormatter(%22%25b%22))%0A%20%20%20%20_ax.legend(fontsize%3D8%2C%20loc%3D%22center%20left%22%2C%20bbox_to_anchor%3D(1.01%2C%200.5))%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20print(pd.DataFrame(rows).to_string(index%3DFalse))%0A%20%20%20%20print(%22formula%20checked%20on%20every%20month-day%20of%20all%20four%20seasons%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Every%20curve%20is%20zero%20at%20both%20ends%20of%20its%20season%20and%20jumps%20at%2029%20February.%20Before%20it%2C%20a%20leap%20season%20is%20behind%3B%20from%201%20March%2C%20it%20is%20ahead.%20The%20largest%20gap%20is%20between%20%C2%BD%20and%201%20day's%20worth%20of%20progress%2C%20depending%20on%20where%2029%20February%20falls%20in%20the%20season%3A%0A%0A%20%20%20%20-%20under%20%60%2208-31%22%60%2C%2029%20February%20is%20near%20the%20middle%2C%20so%20the%20two%20sides%20are%20about%20equal%3B%0A%20%20%20%20-%20for%20Brazil%20soy%20(%60%2204-30%22%60)%2C%2029%20February%20comes%20late%2C%20so%20the%20gap%20is%20mostly%20negative%20and%20is%20largest%20on%2028%20February%2C%20near%20harvest%3B%0A%20%20%20%20-%20for%20a%20calendar-year%20season%20it%20comes%20early%2C%20so%20the%20gap%20is%20mostly%20positive.%0A%0A%20%20%20%20That's%20small%2C%20but%20it%20is%20systematic%2C%20and%20it%20is%20present%20one%20season%20in%20four%20(the%20Gregorian%20rule%20skips%202100).%0A%0A%20%20%20%20The%20same%20thing%20happens%20to%20a%20window's%20calendar%20length%2C%20which%20%60expected_mean%60%20divides%20by.%20Brazil%20soy's%201%20December%20to%2031%20March%20NDVI%20window%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_leap_years_move_2(brazil)%3A%0A%20%20%20%20for%20season%20in%20(2023%2C%202024)%3A%0A%20%20%20%20%20%20%20%20window%20%3D%20pd.date_range(brazil.date_of(%2212-01%22%2C%20season)%2C%20brazil.date_of(%2203-31%22%2C%20season))%0A%20%20%20%20%20%20%20%20print(f%22season%20%7Bseason%7D%3A%20%7Blen(window)%7D%20calendar%20days%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20An%20%60expected_mean%60%20over%20this%20window%20is%20divided%20by%20122%20in%20a%20leap%20season%20and%20121%20otherwise%2C%20a%20rescaling%20of%20under%201%20%25.%0A%0A%20%20%20%20%23%23%23%203.3%20The%20weight%20curve%20is%20cut%20at%20New%20Year%0A%0A%20%20%20%20%60_gaussian%60%20measures%20%60doys%20-%20mu%60%20on%20a%20straight%20line%20from%201%20to%20366.%20Brazil%20soy's%20precip-deficit%20curve%20has%20a%20bell%20at%20%CE%BC%20%3D%2015%20(15%20January)%20with%20%CF%83%20%3D%2011%20days%2C%20which%20should%20reach%20back%20into%20late%20December.%20On%20a%20line%2C%2031%20December%20(DOY%20365)%20is%20350%20days%20from%20%CE%BC%2C%20so%20that%20bell%20gives%20it%20nothing%3B%20only%20the%20far%20tail%20of%20October's%20planting%20bell%20is%20left.%20Measure%20the%20distance%20round%20the%20circle%20instead%2C%20%60(doy%20%E2%88%92%20%CE%BC%20%2B%20183)%20mod%20366%20%E2%88%92%20183%60%2C%20and%20compare%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_weight_curve_cut(bra_deficit%2C%20make_profile%2C%20pw_xr)%3A%0A%20%20%20%20def%20circular_ring(kernels%3A%20list%5Bdict%5D%2C%20normalise%3A%20bool%20%3D%20True)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22The%20same%20bells%20measured%20round%20the%20366-day%20circle%2C%20then%20clipped%20and%20normalised%20as%20phenoweight%20does.%22%22%22%0A%20%20%20%20%20%20%20%20doy%20%3D%20np.arange(1%2C%20pw_xr.RING_DAYS%20%2B%201%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20raw%20%3D%20np.zeros(doy.size)%0A%20%20%20%20%20%20%20%20for%20k%20in%20kernels%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20distance%20%3D%20(doy%20-%20k%5B%22mu%22%5D%20%2B%20183)%20%25%20366%20-%20183%0A%20%20%20%20%20%20%20%20%20%20%20%20raw%20%2B%3D%20k%5B%22amplitude%22%5D%20*%20np.exp(-0.5%20*%20(distance%20%2F%20k%5B%22sigma%22%5D)%20**%202)%0A%20%20%20%20%20%20%20%20raw%20%3D%20np.clip(raw%2C%200%2C%201)%0A%20%20%20%20%20%20%20%20return%20raw%20%2F%20raw.sum()%20if%20normalise%20else%20raw%0A%0A%20%20%20%20as_coded%20%3D%20pw_xr._profile_ring(make_profile(bra_deficit))%0A%20%20%20%20on_circle%20%3D%20circular_ring(bra_deficit)%0A%20%20%20%20december%20%3D%20slice(334%2C%20365)%20%20%23%20DOY%20335-365%3A%201-31%20December%20in%20a%20non-leap%20year%0A%20%20%20%20print(%2225-31%20Dec%2C%20as%20coded%20%3A%22%2C%20%5Bf%22%7Bx%3A.1e%7D%22%20for%20x%20in%20as_coded%5B358%3A365%5D%5D)%0A%20%20%20%20print(%2225-31%20Dec%2C%20on%20circle%3A%22%2C%20%5Bf%22%7Bx%3A.1e%7D%22%20for%20x%20in%20on_circle%5B358%3A365%5D%5D)%0A%20%20%20%20print(f%221%20Jan%3A%20as%20coded%20%7Bas_coded%5B0%5D%3A.5f%7D%2C%20on%20circle%20%7Bon_circle%5B0%5D%3A.5f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22December's%20share%20of%20the%20weight%3A%20as%20coded%20%7Bas_coded%5Bdecember%5D.sum()%3A.2%25%7D%2C%20on%20circle%20%7Bon_circle%5Bdecember%5D.sum()%3A.2%25%7D%22%0A%20%20%20%20)%0A%20%20%20%20lost%20%3D%20(%0A%20%20%20%20%20%20%20%201%0A%20%20%20%20%20%20%20%20-%20pw_xr._profile_ring(make_profile(bra_deficit)%2C%20normalise%3DFalse).sum()%0A%20%20%20%20%20%20%20%20%2F%20circular_ring(bra_deficit%2C%20normalise%3DFalse).sum()%0A%20%20%20%20)%0A%20%20%20%20print(f%22share%20of%20the%20curve%20lost%20off%20the%20year's%20edge%3A%20%7Blost%3A.2%25%7D%22)%0A%20%20%20%20return%20(on_circle%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20So%20a%20dry%20Christmas%20week%20barely%20moves%20Brazil%20soy's%20precip-deficit%20total%3A%20its%20weight%20there%20is%20thousands%20of%20times%20smaller%20than%20the%20circle%20gives%2C%20although%20the%20curve%20puts%20a%20flowering%20bell%20on%2015%20January.%20The%20lost%20share%20is%20about%202%20%25%20of%20the%20curve%2C%20and%20it%20is%20always%20the%20same%20few%20days.%20Over%20every%20calendar-anchored%20head%20in%20data-pipelines'%20ERA5%20configs%2C%2023%20of%20108%20lose%20more%20than%200.1%20%25%3B%20the%20largest%20are%20the%20two%20curves%20the%20Brazil%20soy%20yield%20model%20reads%20(VPD%202.77%20%25%2C%20precip%20deficit%202.08%20%25).%20The%20gridded%20lane%20(%60weights_from_onset%60)%20shifts%20its%20ring%20with%20%60np.mod(...%2C%20366)%60%2C%20so%20the%20shift%20adds%20no%20cut%2C%20but%20it%20inherits%20any%20cut%20already%20in%20the%20ring%3B%20production's%20gridded%20heads%20put%20their%20bells%20mid-ring%2C%20so%20in%20practice%20the%20calendar%20lane%20is%20where%20it%20bites.%20This%20is%20finding%20%5BF18%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F54)%20in%20the%20backlog.%20Model-foundry%20has%20since%20fixed%20it%20upstream%20(%60phenoweight%60%200.6.0%2C%2019%20November)%2C%20but%20production%20still%20builds%20these%20features%20with%200.5.0%2C%20the%20version%20this%20notebook%20runs.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_weight_curve_cut_2(bra_deficit%2C%20make_profile%2C%20on_circle%2C%20weights_xr)%3A%0A%20%20%20%20nov_to_feb%20%3D%20pd.date_range(%222023-11-15%22%2C%20%222024-02-15%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.2))%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20nov_to_feb%2C%0A%20%20%20%20%20%20%20%20weights_xr(make_profile(bra_deficit)%2C%20nov_to_feb)%2C%0A%20%20%20%20%20%20%20%20label%3D%22as%20coded%3A%20(day%20%E2%88%92%20%CE%BC)%20on%20a%20line%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20nov_to_feb%2C%0A%20%20%20%20%20%20%20%20on_circle%5Bnov_to_feb.dayofyear.values%20-%201%5D%2C%0A%20%20%20%20%20%20%20%20linestyle%3D%22--%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22measured%20round%20the%20circle%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.axvline(pd.Timestamp(%222024-01-01%22)%2C%20color%3D%22grey%22%2C%20linestyle%3D%22%3A%22%2C%20label%3D%221%20January%22)%0A%20%20%20%20_ax.set_ylabel(%22weight%20per%20day%22)%0A%20%20%20%20_ax.set_title(%22Brazil%20soy%20precip-deficit%20weights%20across%20New%20Year%22)%0A%20%20%20%20_ax.legend(fontsize%3D8)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.4%20Leap%20years%20in%20the%20weight%20ring%0A%0A%20%20%20%20%60phenoweight.core%60%20keeps%20a%20365-day%20%60YEAR%60%20for%20its%20diagnostics%20(%60peak%60%2C%20%60window%60%2C%20%60check%60)%3B%20the%20xarray%20lane%20uses%20the%20366-day%20ring.%20tf-data-ml-utils%20imports%20only%20the%20xarray%20lane%2C%20so%20**production%20uses%20the%20366-day%20ring**.%20Two%20checks%3A%20the%20weights%20over%20a%20non-leap%20year%2C%20which%20never%20reaches%20DOY%20366%2C%20and%20where%20corn's%20flat%20top%20falls%20in%20a%20leap%20year%2C%20when%20every%20date%20from%201%20March%20has%20a%20day%20of%20year%20one%20higher%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_leap_years_weight(%0A%20%20%20%20corn_heat%2C%0A%20%20%20%20make_profile%2C%0A%20%20%20%20pw_core%2C%0A%20%20%20%20pw_xr%2C%0A%20%20%20%20weights_xr%2C%0A)%3A%0A%20%20%20%20print(%22core.YEAR%20has%22%2C%20len(pw_core.YEAR)%2C%20%22days%3B%20xr.RING_DAYS%20%3D%22%2C%20pw_xr.RING_DAYS)%0A%20%20%20%20for%20year%20in%20(2023%2C%202024)%3A%0A%20%20%20%20%20%20%20%20days_of_year%20%3D%20pd.date_range(f%22%7Byear%7D-01-01%22%2C%20f%22%7Byear%7D-12-31%22)%0A%20%20%20%20%20%20%20%20weights%20%3D%20weights_xr(make_profile(corn_heat)%2C%20days_of_year).values%0A%20%20%20%20%20%20%20%20top%20%3D%20days_of_year%5Bweights%20%3E%3D%20weights.max()%20-%201e-12%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Byear%7D%3A%20%7Bdays_of_year.size%7D%20days%2C%20weights%20sum%20to%20%7Bweights.sum()%3A.6f%7D%2C%20flat%20top%20%7Btop%5B0%5D%3A%25d%20%25b%7D%20to%20%7Btop%5B-1%5D%3A%25d%20%25b%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%202023%20the%20slot%20for%20DOY%20366%20is%20never%20read%2C%20yet%20the%20weights%20still%20sum%20to%201%20to%20six%20decimals%3A%20corn's%20curve%20is%20effectively%20zero%20there%20(the%20largest%20DOY-366%20weight%20of%20any%20production%20head%20is%200.0014).%20In%202024%20the%20flat%20top%20starts%20a%20calendar%20day%20earlier.%20The%20weight%20ring%20is%20keyed%20by%20day%20of%20year%2C%20while%20%60SeasonYear%60%20is%20keyed%20by%20month-day%2C%20so%20in%20a%20leap%20year%20the%20two%20calendars%20in%20this%20unit%20disagree%20by%20a%20day%20from%201%20March.%20With%20every%20production%20%CF%83%207%20days%20or%20more%2C%20that%20shift%20is%20small%20against%20the%20curves'%20width.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Part%204%20%C2%B7%20Paths%20not%20travelled%2C%20tried%20live%0A%0A%20%20%20%20%23%23%23%204.1%20pandas%20annual%20periods%20anchored%20to%20a%20month%0A%0A%20%20%20%20pandas%20already%20knows%20about%20fiscal%20years.%20%60pd.Period(day%2C%20freq%3D%22Y-AUG%22)%60%20is%20the%20year%20**ending%20in%20August**%2C%20numbered%20by%20its%20end%20year%2C%20which%20is%20model-foundry's%20convention%20(%5Bpandas%202.3%20docs%5D(https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3%2Fuser_guide%2Ftimeseries.html%23anchored-offsets)%2C%20as%20locked%3A%202.3.3).%20On%20every%20month-end%20%60last_day%60%2C%20over%2031%20years%2C%20does%20it%20agree%20with%20%60SeasonYear%60%3F%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_pandas_annual_periods()%3A%0A%20%20%20%20days_1%20%3D%20pd.date_range(%222000-01-01%22%2C%20%222030-12-31%22%2C%20freq%3D%22D%22)%0A%20%20%20%20month_end%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%2201-31%22%3A%20%22JAN%22%2C%0A%20%20%20%20%20%20%20%20%2202-28%22%3A%20%22FEB%22%2C%0A%20%20%20%20%20%20%20%20%2203-31%22%3A%20%22MAR%22%2C%0A%20%20%20%20%20%20%20%20%2204-30%22%3A%20%22APR%22%2C%0A%20%20%20%20%20%20%20%20%2205-31%22%3A%20%22MAY%22%2C%0A%20%20%20%20%20%20%20%20%2206-30%22%3A%20%22JUN%22%2C%0A%20%20%20%20%20%20%20%20%2207-31%22%3A%20%22JUL%22%2C%0A%20%20%20%20%20%20%20%20%2208-31%22%3A%20%22AUG%22%2C%0A%20%20%20%20%20%20%20%20%2209-30%22%3A%20%22SEP%22%2C%0A%20%20%20%20%20%20%20%20%2210-31%22%3A%20%22OCT%22%2C%0A%20%20%20%20%20%20%20%20%2211-30%22%3A%20%22NOV%22%2C%0A%20%20%20%20%20%20%20%20%2212-31%22%3A%20%22DEC%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20for%20_last_day%2C%20month%20in%20month_end.items()%3A%0A%20%20%20%20%20%20%20%20periods%20%3D%20days_1.to_period(f%22Y-%7Bmonth%7D%22).year%0A%20%20%20%20%20%20%20%20_reference%20%3D%20np.array(%5BSeasonYear(_last_day).of(d)%20for%20d%20in%20days_1.date%5D)%0A%20%20%20%20%20%20%20%20print(f%22%7B_last_day%7D%20%20Y-%7Bmonth%7D%3A%20mismatches%20%3D%20%7B(periods%20!%3D%20_reference).sum()%7D%22)%0A%20%20%20%20return%20(days_1%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20It%20agrees%20everywhere%2C%20including%20%60%2202-28%22%60%20against%20%60Y-FEB%60%3A%20pandas%20anchors%20to%20the%20*month%20end*%2C%20so%2029%20February%20belongs%20with%20February%20automatically.%0A%0A%20%20%20%20The%20catch%20is%20that%20periods%20only%20end%20at%20month%20ends.%20US%20winter%20wheat%20ends%20on%20**1%20August**%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_pandas_annual_periods_2(days_1)%3A%0A%20%20%20%20_reference%20%3D%20np.array(%5BSeasonYear(%2208-01%22).of(d)%20for%20d%20in%20days_1.date%5D)%0A%20%20%20%20nearest%20%3D%20days_1.to_period(%22Y-JUL%22).year%0A%20%20%20%20wrong%20%3D%20days_1%5Bnearest%20!%3D%20_reference%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22Y-JUL%20vs%20SeasonYear('08-01')%3A%20%7Blen(wrong)%7D%20mismatching%20days%2C%20e.g.%20%7Bsorted(set(wrong.strftime('%25m-%25d')))%7D%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20One%20trap%20with%20these%20aliases%3A%20pandas%202.2%20split%20them.%20%60Y-AUG%60%20is%20still%20the%20**period**%20alias%2C%20but%20a%20%60date_range%60%20of%20year%20*ends*%20now%20wants%20%60YE-AUG%60.%20On%20the%20locked%20pandas%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_pandas_annual_periods_3()%3A%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20pd.date_range(%222000-01-01%22%2C%20periods%3D3%2C%20freq%3D%22Y-AUG%22)%0A%20%20%20%20print(%22date_range(freq%3D'Y-AUG')%20warns%3A%22%2C%20caught%5B0%5D.message)%0A%20%20%20%20print(%22to_period('Y-AUG')%20does%20not%3A%22%2C%20repr(pd.Timestamp(%222024-03-01%22).to_period(%22Y-AUG%22)))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.2%20Shift%20by%20a%20month%20offset%2C%20then%20take%20the%20year%0A%0A%20%20%20%20Another%20idiom%3A%20add%20however%20many%20months%20take%20%60last_day%60%20to%2031%20December%2C%20then%20read%20%60.year%60.%20With%20%60pd.DateOffset(months%3D4)%60%2C%2031%20August%20becomes%2031%20December.%20It's%20neat%20for%20month-end%20seasons%20and%20wrong%20for%20a%20mid-month%20one%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_shift_month_offset(days_1)%3A%0A%20%20%20%20for%20_last_day%2C%20months%20in%20%5B(%2208-31%22%2C%204)%2C%20(%2208-01%22%2C%205)%5D%3A%0A%20%20%20%20%20%20%20%20shifted%20%3D%20(days_1%20%2B%20pd.DateOffset(months%3Dmonths)).year%0A%20%20%20%20%20%20%20%20_reference%20%3D%20np.array(%5BSeasonYear(_last_day).of(d)%20for%20d%20in%20days_1.date%5D)%0A%20%20%20%20%20%20%20%20_bad%20%3D%20days_1%5Bshifted%20!%3D%20_reference%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_last_day%7D%20with%20%2B%7Bmonths%7D%20months%3A%20%7Blen(_bad)%7D%20mismatches%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22e.g.%20%7B_bad%5B0%5D.date()%7D%22%20if%20len(_bad)%20else%20%22%22%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.3%20Day-of-year%20arithmetic%0A%0A%20%20%20%20tf-data-ml-utils%20compares%20day-of-year%20numbers%2C%20and%20looks%20up%20the%20start's%20day%20of%20year%20**per%20calendar%20year**%2C%20so%20leap%20years%20shift%20correctly.%20The%20tempting%20shortcut%20is%20one%20fixed%20start%20day%20of%20year.%20Here's%20what%20that%20costs%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_3_day_year_arithmetic(days_1)%3A%0A%20%20%20%20start_doy_non_leap%20%3D%20pd.Timestamp(%222021-09-01%22).dayofyear%0A%20%20%20%20naive%20%3D%20np.where(days_1.dayofyear%20%3C%20start_doy_non_leap%2C%20days_1.year%2C%20days_1.year%20%2B%201)%0A%20%20%20%20_reference%20%3D%20np.array(%5BSeasonYear(%2208-31%22).of(d)%20for%20d%20in%20days_1.date%5D)%0A%20%20%20%20_bad%20%3D%20days_1%5Bnaive%20!%3D%20_reference%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22fixed%20day-of-year%20%7Bstart_doy_non_leap%7D%3A%20%7Blen(_bad)%7D%20wrong%20days%2C%20all%20on%22%2C%0A%20%20%20%20%20%20%20%20sorted(set(_bad.strftime(%22%25m-%25d%22)))%2C%0A%20%20%20%20%20%20%20%20%22in%22%2C%0A%20%20%20%20%20%20%20%20sorted(set(_bad.year))%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20every%20leap%20year%2C%2031%20August%20is%20day%20244%2C%20the%20same%20number%20that%201%20September%20has%20in%20a%20normal%20year%2C%20so%20the%20shortcut%20moves%20it%20into%20the%20next%20season.%0A%0A%20%20%20%20%23%23%23%204.4%20Vectorise%20%60of%60%0A%0A%20%20%20%20%60of%60%20runs%20once%20per%20Python%20%60date%60.%20That's%20fine%20for%20a%20recipe%2C%20but%20slow%20for%20millions%20of%20pixel-days.%20The%20fix%20is%20to%20encode%20the%20month-day%20as%20an%20integer%20%60100%20*%20month%20%2B%20day%60%2C%20apply%20the%20same%20fold%2C%20and%20compare%20whole%20arrays%20at%20once.%20That%20is%20code.md's%20%22Try%20it%22%20exercise%2C%20so%20the%20function%20below%20is%20a%20stub%3A%20write%20it%2C%20and%20the%20check%20underneath%20tells%20you%20when%20it's%20right.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4_vectorise(days_1)%3A%0A%20%20%20%20def%20of_vectorised(index%3A%20pd.DatetimeIndex%2C%20last_day%3A%20str)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22Your%20turn%3A%20use%20only%20index.month%2C%20index.day%20and%20index.year.%20No%20strftime%2C%20and%20no%20loop.%22%22%22%0A%20%20%20%20%20%20%20%20raise%20NotImplementedError%0A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20for%20_last_day%20in%20%5B%2212-31%22%2C%20%2208-31%22%2C%20%2202-28%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20expected%20%3D%20np.array(%5BSeasonYear(_last_day).of(d)%20for%20d%20in%20days_1.date%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20assert%20np.array_equal(of_vectorised(days_1%2C%20_last_day)%2C%20expected)%2C%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_last_day%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%23%20same%20shape%20too%0A%20%20%20%20%20%20%20%20print(%22of_vectorised%20%3D%3D%20SeasonYear.of%20for%20every%20day%202000-2030%20and%20three%20last_days%22)%0A%20%20%20%20%20%20%20%20written%20%3D%20True%0A%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20print(%22of_vectorised%20is%20not%20written%20yet%3A%20fill%20it%20in%2C%20then%20re-run%20this%20cell%20and%20the%20next%22)%0A%20%20%20%20%20%20%20%20written%20%3D%20False%0A%20%20%20%20return%20of_vectorised%2C%20written%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20How%20much%20does%20vectorising%20buy%3F%20The%20next%20cell%20times%20%60SeasonYear.of%60%20over%20a%20million%20hourly%20timestamps%20against%20pandas'%20vectorised%20%60to_period(%22Y-AUG%22)%60%2C%20which%20gives%20the%20same%20answer%20for%20%60%2208-31%22%60%20(4.1)%2C%20and%20against%20your%20%60of_vectorised%60%20once%20it's%20written.%20The%20per-date%20figure%20is%20split%20in%20two%2C%20because%20building%20a%20million%20Python%20%60date%60%20objects%20(%60million.date%60)%20is%20part%20of%20the%20cost%3B%20production's%20%60window_features%60%20pays%20it%20too.%20Like%20production%2C%20the%20loop%20builds%20one%20%60SeasonYear%60%20and%20reuses%20it%2C%20so%20the%20timing%20measures%20%60of%60%20alone%2C%20not%20one%20million%20constructions%20and%20validations.%20Your%20machine's%20numbers%20will%20differ%20from%20anyone%20else's.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4_vectorise_2(of_vectorised%2C%20written)%3A%0A%20%20%20%20million%20%3D%20pd.date_range(%221900-01-01%22%2C%20periods%3D1_000_000%2C%20freq%3D%22h%22)%0A%20%20%20%20t0%20%3D%20time.perf_counter()%0A%20%20%20%20python_dates%20%3D%20million.date%0A%20%20%20%20t_dates%20%3D%20time.perf_counter()%20-%20t0%0A%20%20%20%20august%20%3D%20SeasonYear(%2208-31%22)%20%20%23%20one%20instance%2C%20reused%2C%20as%20window_features%20reuses%20its%20recipe's%0A%20%20%20%20t0%20%3D%20time.perf_counter()%0A%20%20%20%20loop%20%3D%20np.array(%5Baugust.of(d)%20for%20d%20in%20python_dates%5D)%0A%20%20%20%20t_loop%20%3D%20time.perf_counter()%20-%20t0%0A%20%20%20%20per_date%20%3D%20t_dates%20%2B%20t_loop%0A%0A%20%20%20%20t0%20%3D%20time.perf_counter()%0A%20%20%20%20period%20%3D%20million.to_period(%22Y-AUG%22).year%0A%20%20%20%20t_period%20%3D%20time.perf_counter()%20-%20t0%0A%20%20%20%20assert%20(period%20%3D%3D%20loop).all()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22per-date%3A%20build%20%7Blen(million)%3A%2C%7D%20date%20objects%20%7Bt_dates%3A.2f%7D%20s%20%2B%20SeasonYear.of%20loop%20%7Bt_loop%3A.2f%7D%20s%20%3D%20%7Bper_date%3A.2f%7D%20s%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22vectorised%20to_period('Y-AUG')%3A%20%7Bt_period%20*%201000%3A.1f%7D%20ms%2C%20%7Bper_date%20%2F%20t_period%3A.0f%7Dx%20faster%20than%20per-date%22%0A%20%20%20%20)%0A%20%20%20%20if%20written%3A%0A%20%20%20%20%20%20%20%20t0%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20vec%20%3D%20of_vectorised(million%2C%20%2208-31%22)%0A%20%20%20%20%20%20%20%20t_vec%20%3D%20time.perf_counter()%20-%20t0%0A%20%20%20%20%20%20%20%20assert%20np.array_equal(vec%2C%20loop)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22vectorised%20of_vectorised%3A%20%7Bt_vec%20*%201000%3A.1f%7D%20ms%2C%20%7Bper_date%20%2F%20t_vec%3A.0f%7Dx%20faster%20than%20per-date%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20Approach%20%7C%20Arbitrary%20%60last_day%60%20%7C%20Leap%20day%20%7C%20Speed%20%7C%20Readability%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20%60SeasonYear.of%60%20(production)%20%7C%20yes%20%7C%20explicit%20fold%20%7C%20one%20%60date%60%20at%20a%20time%20%7C%20two%20lines%2C%20obvious%20%7C%0A%20%20%20%20%7C%20%60Period(%22Y-MON%22)%60%20%7C%20month%20ends%20only%20%7C%20automatic%20%7C%20vectorised%20%7C%20needs%20knowledge%20of%20pandas%20aliases%20%7C%0A%20%20%20%20%7C%20%60%2B%20DateOffset(months%3Dn)%60%20%7C%20month%20ends%20only%20%7C%20automatic%20%7C%20vectorised%20%7C%20clever%2C%20easy%20to%20get%20wrong%20%7C%0A%20%20%20%20%7C%20Fixed%20day%20of%20year%20%7C%20yes%20%7C%20**wrong**%20%7C%20vectorised%20%7C%20looks%20right%2C%20isn't%20%7C%0A%20%20%20%20%7C%20%60100%20*%20month%20%2B%20day%60%20%7C%20yes%20%7C%20explicit%20fold%20%7C%20vectorised%20%7C%20a%20short%20comment%20needed%20%7C%0A%0A%20%20%20%20Production%20chose%20the%20smallest%2C%20most%20readable%20version%2C%20and%20as%20measured%20above%2C%20it%20is%20the%20slowest.%20That's%20a%20fine%20trade%20until%20a%20hot%20loop%20calls%20it.%20The%20vectorised%20form%20is%20a%20candidate%20if%20one%20ever%20does.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%204.5%20A%20box%20window%20between%20stage%20anchors%0A%0A%20%20%20%20model-foundry%20has%20a%20second%20way%20to%20say%20%22this%20part%20of%20the%20season%20matters%22%3A%20a%20window%20between%20two%20stage%20anchors%2C%20which%20is%20a%20box-shaped%20weight.%20US%20corn%20yield's%20%60edd_jul%60%20is%20the%20mean%20daily%20EDD%20from%20%60vegetative.end-8%60%20to%20%60flowering.end%2B7%60%20(%5Bcorn_usa.yaml%20L16%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_corn_usa%2Fsrc%2Fyield_corn_usa%2Fcorn_usa.yaml%23L16)).%20Resolve%20the%20anchors%20from%20the%20bundled%20seed%2C%20as%20in%202.4%2C%20and%20score%202.7's%20two%20heatwaves%20with%20it%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_5_box_window_between(bundled_seed%2C%20heatwave_score%2C%20season_2023)%3A%0A%20%20%20%20%23%20Select%20the%20bundled%20seed%20here%3A%202.4's%20toy-seed%20cell%20may%20have%20switched%20it%20since.%0A%20%20%20%20os.environ%5B%22MI_CURATED_STAGE_SEED%22%5D%20%3D%20str(bundled_seed)%0A%20%20%20%20recipe.stage_seed.cache_clear()%0A%20%20%20%20jul_start%20%3D%20recipe.stage_mmdd(%22vegetative.end-8%22%2C%20crop%3D%22corn%22%2C%20country%3D%22United%20States%22)%0A%20%20%20%20jul_end%20%3D%20recipe.stage_mmdd(%22flowering.end%2B7%22%2C%20crop%3D%22corn%22%2C%20country%3D%22United%20States%22)%0A%20%20%20%20in_box%20%3D%20(season_2023%20%3E%3D%20f%222023-%7Bjul_start%7D%22)%20%26%20(season_2023%20%3C%3D%20f%222023-%7Bjul_end%7D%22)%0A%20%20%20%20for%20_first_day%20in%20(%222023-06-24%22%2C%20%222023-07-11%22)%3A%0A%20%20%20%20%20%20%20%20edd%20%3D%20np.zeros(season_2023.size)%0A%20%20%20%20%20%20%20%20edd%5Bseason_2023.isin(pd.date_range(_first_day%2C%20periods%3D5))%5D%20%3D%206.0%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22heatwave%20from%20%7B_first_day%7D%3A%20edd_jul%20(mean%20EDD%2C%20%7Bjul_start%7D%20to%20%7Bjul_end%7D)%20%3D%20%7Bedd%5Bin_box%5D.mean()%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20%20bell%20%3D%20%7Bheatwave_score(_first_day)%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20box%20can't%20see%20a%20heatwave%20that%20ends%20one%20day%20before%201%20July%2C%20and%20counts%20every%20July%20day%20the%20same%3B%20the%20bell%20fades%20in%20and%20out.%20Each%20has%20a%20place.%20The%20box%20follows%20the%20crop's%20own%20stage%20calendar%20and%20is%20easy%20to%20explain%3B%20the%20bell%20is%20smoother%2C%20but%20it%20sits%20on%20fixed%20days%20of%20the%20year%20unless%20it%20is%20gridded.%0A%0A%20%20%20%20%23%23%23%204.6%20A%20circular%20mean%20instead%20of%20the%20gap-cut%20median%0A%0A%20%20%20%20SciPy's%20%5B%60circmean%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.stats.circmean.html)%20is%20the%20textbook%20average%20of%20angles%3A%20map%20each%20day%20to%20an%20angle%2C%20average%20the%20unit%20vectors%2C%20map%20back.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_6_circular_mean_instead(planting_doys)%3A%0A%20%20%20%20from%20scipy.stats%20import%20circmean%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22circmean%3A%22%2C%0A%20%20%20%20%20%20%20%20round(float(circmean(planting_doys%2C%20high%3D365%2C%20low%3D0))%2C%201)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20circular_median%3A%22%2C%0A%20%20%20%20%20%20%20%20circular_median(planting_doys)%2C%0A%20%20%20%20)%0A%20%20%20%20spread%20%3D%20np.array(%5B1%2C%2092%2C%20183%2C%20274%5D)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22days%20spread%20round%20the%20year%3A%22%2C%0A%20%20%20%20%20%20%20%20%22circmean%22%2C%0A%20%20%20%20%20%20%20%20round(float(circmean(spread%2C%20high%3D365%2C%20low%3D0))%2C%201)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20circular_median%22%2C%0A%20%20%20%20%20%20%20%20circular_median(spread)%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20a%20clustered%20planting%20season%20the%20two%20agree%20to%20within%20a%20day.%20For%20days%20spread%20evenly%20round%20the%20year%2C%20both%20still%20return%20a%20number%2C%20and%20neither%20means%20anything%3A%20there%20is%20no%20middle.%20That%20is%20why%20%60circular_split%60%20separates%20the%20seasons%20first%2C%20and%20%60circular_median%60%20is%20only%20ever%20applied%20to%20one%20cluster.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Part%205%20%C2%B7%20Exercises%0A%0A%20%20%20%201.%20**Predict%2C%20then%20run.**%20Write%20a%20subclass%20%60NoFold(SeasonYear)%60%20whose%20%60of%60%20leaves%20out%20%60.replace(%2202-29%22%2C%20%2202-28%22)%60%2C%20and%20use%20it%20in%20place%20of%20%60SeasonYear%60%20in%202.2's%20first%20loop.%20Which%20line%20changes%2C%20and%20to%20what%3F%20*Check%3A*%20%60NoFold(%2202-28%22).of(date(2024%2C%202%2C%2029))%60%20is%20%602025%60%2C%20and%20%60NoFold%60%20agrees%20with%20%60last_season%60%20on%20all%20133%2C590%20pairs%20of%203.1's%20sweep.%0A%20%20%20%202.%20Write%20%60label_vectorised(index%2C%20last_day)%60%20returning%20%60%222023%2F24%22%60%20strings.%20*Check%3A*%20it%20equals%20%60SeasonYear(ld).label(SeasonYear(ld).of(d))%60%20on%20every%20day%20of%202000%E2%80%932030%20for%20%60%2208-31%22%60%20and%20%60%2212-31%22%60%3B%2015%20January%202024%20under%20%60%2208-31%22%60%20gives%20%60%222023%2F24%22%60.%0A%20%20%20%203.%20Make%20the%20start-year%20and%20end-year%20conventions%20meet.%20Write%20%60to_end_year(start_year%2C%20start_mmdd%2C%20end_mmdd)%60.%20*Check%3A*%20it%20maps%20%60create_season_year%60%20onto%20%60SeasonYear.of%60%20on%20every%20day%20of%203.1's%20Brazil%20soy%20range%20(%60%2205-01%22%60%2C%20%60%2204-30%22%60)%20and%20of%20a%20calendar-year%20crop%20(%60%2201-01%22%60%2C%20%60%2212-31%22%60)%3B%20%60to_end_year(2023%2C%20%2205-01%22%2C%20%2204-30%22)%20%3D%3D%202024%60.%0A%20%20%20%204.%20%60progress_of%60%20divides%20by%20364%20or%20365.%20Write%20a%20version%20that%20always%20divides%20by%20364%20by%20measuring%20progress%20in%20a%20reference%20season%20(hint%3A%20%60NON_LEAP_SEASON%60).%20Then%20compare%20it%20in%20a%20new%20cell%3A%20copy%203.2's%20%60leap_gap%60%2C%20call%20your%20function%20in%20place%20of%20%60sy.progress_of%60%2C%20and%20drop%20its%20formula%20check%2C%20which%20describes%20production's%20%60progress_of%60.%20*Check%3A*%20under%20%60%2208-31%22%60%2C%201%20March%20scores%200.497253%20in%20both%202023%20and%202024%2C%20and%20the%20last%20day%20of%20every%20season%20still%20scores%201.%20What%20would%20change%20for%20a%20model%20trained%20on%20it%3F%0A%20%20%20%205.%20In%204.3%2C%20use%20the%20*leap-year*%20start%20day%20instead%20(%60mmdd_to_doy(%2209-01%22%2C%20leap%3DTrue)%60%2C%20imported%20in%203.1).%20Predict%20which%20days%20are%20now%20wrong%2C%20and%20in%20which%20years%2C%20then%20run%20it.%20*Check%3A*%201%20September%20is%20wrong%20in%20every%20*non*-leap%20year%3A%2023%20days%20over%202000%E2%80%932030.%0A%20%20%20%206.%20**Make%20the%20ring%20circular.**%20Write%20%60wrapped_profile_ring(kernels)%60%20that%20measures%20each%20bell's%20distance%20round%20the%20366-day%20circle%2C%20then%20clips%20and%20normalises%20as%20%60_profile_ring%60%20does%20(no%20peeking%20at%203.3's%20%60circular_ring%60).%20*Check%3A*%20for%20Brazil%20soy's%20precip-deficit%20kernels%20December's%20share%20is%201.81%20%25%2C%20against%200.06%20%25%20as%20coded%3B%20for%20US%20corn's%20heat%20kernels%20it%20agrees%20with%20%60pw_xr._profile_ring%60%20to%20within%201e-6%20on%20every%20slot.%20Why%20does%20corn%20not%20change%3F%0A%20%20%20%207.%20**Anchor%20the%20curve%20per%20cell.**%20Call%20%60pw_xr.weights_from_onset(make_profile(corn_heat)%2C%20year_2023%2C%20xr.DataArray(%5B150%2C%20171%5D%2C%20dims%3D%22cell%22)%2C%20start_doy%3D150)%60.%20Predict%20where%20each%20cell's%20flat%20top%20falls%2C%20then%20check.%20*Check%3A*%20the%20first%20cell's%20runs%20from%207%20to%2021%20July%2C%20as%20in%203.4%3B%20the%20second's%2021%20days%20later%2C%20from%2028%20July%20to%2011%20August.%0A%20%20%20%208.%20**Count%20the%20lost%20days.**%20Build%20the%2072%20CHIRPS%20pentad%20start%20dates%20of%202023%20(the%201st%2C%206th%2C%2011th%2C%2016th%2C%2021st%20and%2026th%20of%20each%20month)%20and%20pass%20them%20to%20%60daily_support%60.%20How%20many%20days%20are%20covered%2C%20and%20which%20are%20missing%3F%20*Check%3A*%20358%3B%20the%20missing%20seven%20are%20the%2031sts%20of%20January%2C%20March%2C%20May%2C%20July%2C%20August%2C%20October%20and%20December.%20Then%20change%20the%20cap%20so%20each%20sample%20owns%20all%20the%20days%20to%20the%20next%20one%2C%20and%20say%20what%20you%20would%20do%20about%20the%20last%20sample.%0A%0A%20%20%20%20%23%23%20Sources%0A%0A%20%20%20%20-%20Python%203.12%20%60datetime%60%20(%60date.fromisoformat%60%20accepts%20more%20formats%20since%203.11%3B%20%60strftime%60)%3A%20https%3A%2F%2Fdocs.python.org%2F3.12%2Flibrary%2Fdatetime.html%0A%20%20%20%20-%20Python%203.12%20%60calendar.isleap%60%3A%20https%3A%2F%2Fdocs.python.org%2F3.12%2Flibrary%2Fcalendar.html%23calendar.isleap%0A%20%20%20%20-%20Python%20%60dataclasses%60%20(%60frozen%60%2C%20%60__post_init__%60%2C%20hashing%20rules)%3A%20https%3A%2F%2Fdocs.python.org%2F3.12%2Flibrary%2Fdataclasses.html%0A%20%20%20%20-%20Python%20%60inspect.getsource%60%3A%20https%3A%2F%2Fdocs.python.org%2F3.12%2Flibrary%2Finspect.html%0A%20%20%20%20-%20RFC%203339%20%C2%A75.1%2C%20why%20string%20order%20equals%20time%20order%3A%20https%3A%2F%2Fdatatracker.ietf.org%2Fdoc%2Fhtml%2Frfc3339%0A%20%20%20%20-%20Pydantic%202.14.3%20validators%2C%20including%20%60AfterValidator%60%20and%20%60model_validator(mode%3D%22before%22)%60%3A%20https%3A%2F%2Fgithub.com%2Fpydantic%2Fpydantic%2Fblob%2Fv2.14.3%2Fdocs%2Fconcepts%2Fvalidators.md%0A%20%20%20%20-%20pandas%202.3%20time%20series%2C%20periods%20and%20anchored%20offsets%3A%20https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3%2Fuser_guide%2Ftimeseries.html%23anchored-offsets%20and%20https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3%2Freference%2Fapi%2Fpandas.Period.html%0A%20%20%20%20-%20xarray%202025.11%20time%20series%20and%20the%20%60.dt%60%20accessor%20(used%20by%20tf-data-ml-utils)%3A%20https%3A%2F%2Fdocs.xarray.dev%2Fen%2Fv2025.11.0%2Fuser-guide%2Ftime-series.html%0A%20%20%20%20-%20FAO-56%20(Allen%20et%20al.%201998)%2C%20Chapter%206%2C%20Table%2011%2C%20lengths%20of%20crop%20development%20stages%3A%20https%3A%2F%2Fwww.fao.org%2F4%2Fx0490e%2Fx0490e0b.htm%0A%20%20%20%20-%20SciPy%20%60circmean%60%3A%20https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.stats.circmean.html%0A%20%20%20%20-%20NumPy%20%60ufunc.reduceat%60%2C%20the%20segment%20sums%20in%20%60integrate_to_samples%60%3A%20https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Fgenerated%2Fnumpy.ufunc.reduceat.html%0A%20%20%20%20-%20CF%20Conventions%201.11%20calendars%20(%60noleap%60%20%2F%20%60365_day%60)%2C%20where%20climate%20data%20make%2029%20February%20disappear%3A%20https%3A%2F%2Fcfconventions.org%2FData%2Fcf-conventions%2Fcf-conventions-1.11%2Fcf-conventions.html%0A%20%20%20%20%22%22%22)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
6d6b0d1b56f188121594b4c5336958b2