linopy icon indicating copy to clipboard operation
linopy copied to clipboard

Add example of using `where` with `drop = True`

Open FabianHofmann opened this issue 7 months ago • 0 comments

Apologies to jump into an issue but I have some thoughts that are related to sparsity so thought I'd add to this discussion rather than create a new one for now.

I've just started using linopy (and not from a linear programming background either but have used xarray extensively in other contexts) and came across behaviour which I found unintuitive - which is the default behaviour of LinearExpression.where. I found I needed to pass drop=True to get rid of "null" terms in the equations (i.e. significantly reduce the size of the _term dimension)... but my intuition as a new user would have expected this to be the default.

So the question / discussion point is what are the reasons for drop=True not being default in LinearExpression.where (and presumably Variable.where as well but I've not thought about that so much yet). Are there situations where a user would want to keep entirely null terms in their equations? Or is it simply to align with the xarray defaults?

EDIT


On further thought I see why drop=False remains the default, because now in application code I need to deal with the case of drop=True resulting in misaligned / missing coordinates relative to other data. However, I think the documentation would benefit from a small example of using drop=True. Happy to open a PR if that is of interest?

Perhaps also some indication in the __repr__ of LinearExpression of the sparsity. I think most of my confusion came from the fact that the __repr__ hides all the nan coeffs, but they are still present in the _terms dimension of the underlaying data.

Originally posted by @ollie-bell in #248

FabianHofmann avatar Jun 15 '25 19:06 FabianHofmann