-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partial and relative unit declarations #1414
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really neat idea. LGTM
Co-authored-by: Trevor James Smith <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I'm wondering about is whether we should create another function, e.g. declare_relative_units
, instead of adding another layer of logic inside declare_units
.
The fact that in this implementation declare_units
can return different wrappers based on partial makes it hard to understand.
Another issue is that this requires data
's dimension to be ultimately defined. In my mind, we'd have a mechanism to check that thresh
has the same units as data
, no matter what the dimensions of data
are. I suggest we start by discussing this, and then the questions above might resolve themselves.
@huard This new version implements your suggestion if splitting the two decorators: It's missing some doc (WIP), but the idea is that "generic" indices can be wrapped by You can't stack those decorator though (you could in my previous implementation). It's either a single A minor question I asked myself : what do we do if a
There's a double declaration here for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
Co-authored-by: Pascal Bourgault <[email protected]>
Co-authored-by: David Huard <[email protected]>
Pull Request Checklist:
number
) and pull request (:pull:number
) has been addedWhat kind of change does this PR introduce?
declare_units
can accept relative declaration. For examplethresh="<tas>"
means thatthresh
should have the same dimensions astas
. More complex declarations are possible :thresh="<tas> / <pr>"
orthresh="[length] / <tas> "
.partial=True
. In that case, the passed dimensions are stored in the wrapper, but thecheck_units
code is not injected.Both additions are useful for "generic" indices. For example:
Here,
spell_length
already "knows" that thethreshold
should have the same units (dimensions) asdata
, but we still haven't declare those. Different indicators can be created from the function, each one either explicitly re-wrapping withdeclare_units(data="...")
or simply usingIndicator
'sinput
argument to mapdata
to a known variable.Another change this brings is an additional
dimensions
field invariable.yml
. I realized that Indicators usinginput
to map generic inputs to precipitation variables were not "aware" that the "hydro" context could be used. This was because the mapping was passing the "canonical units" todeclare_units
. Units of "kg m-2 s-1" are not necessarily refering to liquid water... By passing a "[precipitation]" string, this allowedcheck_units
andinfer_context
to properly detect that the variable could use the "hydro" context.gen_indicator_docstring
for the case with no default. The bug was there before this PR.Indicator.__new__
were changed a bit so that the "unit" info of the parameters is parsed at the end, after the potential finaldeclare_units
, when relative dimensions are resolved.Does this PR introduce a breaking change?
I removed a check in
declare_units
, "Quantified" variables that have no declared units will raise errors even if they don't have a default. The previous check was there to allow "generic indices" to pass the test. But with the present changes, the idea would be to pass a relative dimension instead.I'm guessing
declare_units
is not being used much outside of xclim, with even fewer cases being broken.EDIT: Also removed the
has_units
arg ofxclim.core.utils.infer_kind_from_parameter
. The recent changes indeclare_units
and this PR remove the need for this check.Other information:
This is a first step in my plan to reorganize all generic indices so that it becomes easier to construct Indicators directly from these. And eventually remove many similar indices.
The next step, I think, will be to sync xclim with clix-meta: implement the new "index functions" (as generic indices) and update
cf.yml
.