-
Notifications
You must be signed in to change notification settings - Fork 285
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
Iris's load and load_cubes utilities return a CubeList with its elements in random order #3314
Comments
I'd assert this is a feature not a bug – CubeLists are, and should be expected to be, unordered (think pre-Python 3.6 dictionary keys). If you wish to reproducibly select a cube from the CubeList you should use a Constraint that's defined by metadata of interest and extract from the CubeList using that Constraint. This is a generally more robust approach than indexing. |
Ah, right. I got thrown off by its name and parent class ( Thanks, I'll use the constraint route. |
Hmmm, I think once loaded cubelists are ordered aren't they? If I do: new_cubelist = iris.cube.Cube([cube.collapsed('foo', iris.analysis.MEAN) for cube in old_cubelist]) then I can expect the order in my new cubelist to be consistent with the old. However, I think I'd always be nervous about assuming a particular order in a freshly loaded cube, regardless of how |
Thanks @evertrol for this issue. Much appreciated! 👍 We've never intended the So the deal is that you can't rely on any particular ordering - if you consider it random, then that's a pretty safe assumption to make. We've left the ordering down to the user to decide, if that matters to them, post loading. As @DPeterK mentioned, use a constraint to refine and extract the cubes that you care about, rather than indexing - indexing is a very weak contract, there is no guarantee that the same cube will be at the same index when comparing independent loads of the same file/s. @evertrol If you don't mind, I'm going to close this issue. However, if you feel that you want to re-open it as a means to force an open discussion on |
Not a problem having this issue closed. It's mostly arguing semantics, and I wouldn't expect to see an API change/rename. As DPeterK remarks, it's mostly a dict keys style (or dict values, if items can be repeated). |
@evertrol Thanks. That seems like a happy compromise to me, so I'm going to re-open this issue with the task of updating the documentation and/or doc-strings to highlight the orders of cubes returned within a |
This has been addressed in @stephenworsley 's PR: #3370 so I'm going to close this issue |
Hiya all - see this issue is closed (and the reasoning given above). See also that the docs are nicely explicit about lack of inherent order. However... is there any option/feeling re option of adding a keyword to Straw API
Whichever of these is used, there could be a default Use-case for this: Notebooks of course not natively the best for diffing generally (thanks to several of you I know there are lots of tools to help with this!). But in this case the cubelist being unordered adds needless extra diff line noise I could do without, and quite a lot of it as I'm using html-formatted output. Not a major issue - but enough of a niggle that I thought worth poking this issue. Cons Moot, but FWIW I do agree as with original poster that this is a bit of a gotcha given Overall |
Could you sort your
|
Definitely! Perfect, thanks Jon! For my use-case this makes my request ignorable, as I can sort after loading but before printing. |
When loading a dataset that has multiple cubes,
load()
andload_cubes()
will return aCubeList
with its elements in random order. This makes it harder for following code to be reproducible: indexing into the list isn't guaranteed to always provide the same cube.Perhaps there is a good reason
CubeList
is inherently unordered, but sincelist
(from which it derives) is inherently ordered, I feel it's better to have it be ordered, so that there are no surprises.The cause seems to in the
iris.fileformats.netcdf.load_cubes
function, where it calls the.values()
method of the variousCFGroup
objects.One solution could be (untested) to make
CFGroup._cf_variables
acollections.OrderedDict
(and perhaps the other attributes ofCFGroup
as well).Potentially, this may also apply for various other dicts, or even the
MutableMapping
base class ofCFGroup
.The text was updated successfully, but these errors were encountered: