-
Notifications
You must be signed in to change notification settings - Fork 74
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
Add sum model #364
base: main
Are you sure you want to change the base?
Add sum model #364
Conversation
…operty to TPL models
…re derived from correlation not cor
…into add_sum_model
…into add_sum_model
@LSchueler this is ready for review. |
Awesome, I'll need some time to thoroughly go throw your changes. |
First come, first serve :-D |
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.
Sorry for taking so long to finish this review, but as you know, I had the best reasons ;-)
I'm really looking forward to sum models being integrated into GSTools, but I found quite a few things which should be addressed before merging.
We'll combine a Spherical and a Gaussian covariance model to construct | ||
a sum model, visualize its variogram, and generate spatial random fields. | ||
|
||
Let's start with importing GSTools setting up the domain size. |
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.
"and" missing
# First, we create two individual covariance models: a :any:`Spherical` model and a | ||
# :any:`Gaussian` model. The Spherical model will emphasize small-scale variability, | ||
# while the Gaussian model captures larger-scale patterns. | ||
|
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.
This formulation might give the impression that the small-scale and large-scale patterns are due to the covariance models. You should clarify that this is due to the chosen length scales.
# As shown, the Spherical model controls the behavior at shorter distances, | ||
# while the Gaussian model dominates at longer distances. |
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.
I think you should also briefly mention how the variance of the covariance models influences the resulting fields.
|
||
############################################################################### | ||
# We fit the sum model to the empirical variogram using GSTools' built-in | ||
# fitting capabilities. As seen in the representation, the variances and length |
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.
I don't really understand the "As seen in the representation" part.
############################################################################### | ||
# We fit the sum model to the empirical variogram using GSTools' built-in | ||
# fitting capabilities. As seen in the representation, the variances and length | ||
# scales of the individual models can be accessed by the attributes |
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.
I would also generalise this from "the variances and length scales" to "the properties of the individual models, e.g. variance and length scales" or something similar.
raise ValueError(msg) | ||
ids = range(len(summod)) | ||
if fail := set(skip) - set(ids): | ||
msg = f"SumModel.set_var_weights: skip ids not valid: {fail}" |
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.
"skip invalid ids"
fix this further down too, please.
var_sum = sum(summod.models[i].var for i in skip) | ||
var_diff = var - var_sum | ||
if var_diff < 0: | ||
msg = "SumModel.set_var_weights: skipped variances to big." |
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.
skipped too large variances
len_sum = sum(summod[i].len_scale * summod.ratios[i] for i in skip) | ||
len_diff = len_scale - len_sum | ||
if len_diff < 0: | ||
msg = "SumModel.set_len_weights: skipped length scales to big." |
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.
Please fix grammar
return para, sill, anis, sum_cfg | ||
|
||
|
||
def _check_sill(model, para_select, sill, bnd, sum_cfg): |
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.
Please add a short description of what exactly is checked.
if is_sum: | ||
if sum_cfg["var_size"] > 0: | ||
var_vals = popt[para_skip : para_skip + sum_cfg["var_size"]] | ||
para_skip += sum_cfg["var_size"] | ||
if sum_cfg["var_fix"]: | ||
model.set_var_weights( | ||
stick_breaking_uniform(var_vals), | ||
sum_cfg["var_skip"], | ||
sum_cfg["fix"]["var"], | ||
) | ||
else: | ||
for i, val in zip(sum_cfg["var_fit"], var_vals): | ||
setattr(model, f"var_{i}", val) | ||
if sum_cfg["len_size"] > 0: | ||
len_vals = popt[para_skip : para_skip + sum_cfg["len_size"]] | ||
para_skip += sum_cfg["len_size"] | ||
if sum_cfg["len_fix"]: | ||
model.set_len_weights( | ||
stick_breaking_uniform(len_vals), | ||
sum_cfg["len_skip"], | ||
sum_cfg["fix"]["len_scale"], | ||
) | ||
else: | ||
for i, val in zip(sum_cfg["len_fit"], len_vals): | ||
setattr(model, f"len_scale_{i}", val) | ||
for i in range(model.size): | ||
fit_para[f"var_{i}"] = model.vars[i] | ||
fit_para[f"len_scale_{i}"] = model.len_scales[i] | ||
# handle sill | ||
if sill is not None and para["var"]: | ||
nugget = sill - model.var | ||
fit_para["nugget"] = nugget | ||
model.nugget = nugget |
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.
There are quite a few pretty large if is_sum
branches all over the place here. Maybe you can find a better structure for the code in this module?
This PR adds a
SumModel
class to represent sums of Covariance models.Sum-Model
SumModel
class+
operator:model = m1 + m2
m1 = model[0]
model[0].nu == model.nu_0
len_scale
is fixed, none of thelen_scale_<i>
can be fixed since len_scale is calculated from variance ratioszero_var
andmodel
attributes to Generator ABC to shortcut field generation for pure nugget modelsOther changes
var_raw
attribute from CovModel (was rarely used and only relevant for the truncated power law models)intensity
attribute which calculates whatvar_raw
was beforevar_raw
was a bad idea in the first place)