Skip to content
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

Splitattrs ncload #5384

Merged
merged 4 commits into from
Jul 21, 2023
Merged

Conversation

pp-mo
Copy link
Member

@pp-mo pp-mo commented Jul 20, 2023

Changes netcdf loading code to specifically load into cube.attributes.globals and cube.attributes.locals
Since the same attributes are then appearing in 'cube.attributes', this in itself (i.e. without changes to saving) should have no effect.
-- Except in some minor corner cases, where an attribute is present as both a local (variable-attribute)) + global (file-attribute).

So, I think that this is (almost) fully backwards-compatible, and does not require a control switch like a FUTURE flag (but reviewer may think differently).
( N.B. I am intending to create a FUTURE flag for the new split-attributes saving behaviour, since that will definitely change behaviour quite significantly. Covered in #4986 )

The actual changes in loading code are pretty small (f736c4b)

I have had to modify change some of the existing tests from #4960, by replacing cube.attributes == <expected-result-dictionary> with dict(cube.attributes) == <expected-result-dictionary>. In principle I could have done that first, to prove pre-existing behaviour, but I think the argument is pretty solid as it is.

( Note : replaces older version #5048 )

@codecov
Copy link

codecov bot commented Jul 20, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (8b751c5) 89.41% compared to head (522265e) 89.41%.

Additional details and impacted files
@@                 Coverage Diff                  @@
##           FEATURE_split_attrs    #5384   +/-   ##
====================================================
  Coverage                89.41%   89.41%           
====================================================
  Files                       89       89           
  Lines                    22497    22500    +3     
  Branches                  5395     5396    +1     
====================================================
+ Hits                     20116    20119    +3     
  Misses                    1636     1636           
  Partials                   745      745           
Impacted Files Coverage Δ
lib/iris/fileformats/_nc_load_rules/helpers.py 95.96% <100.00%> (ø)
lib/iris/fileformats/netcdf/loader.py 80.79% <100.00%> (+0.21%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@trexfeathers
Copy link
Contributor

trexfeathers commented Jul 20, 2023

@pp-mo can you clarify for me: cf_attrs_unused() is everything that doesn't get interpreted during NetCDF load? I.e. excluding coordinates, standard_name, etcetera?

@pp-mo
Copy link
Member Author

pp-mo commented Jul 21, 2023

@pp-mo can you clarify for me: cf_attrs_unused() is everything that doesn't get interpreted during NetCDF load? I.e. excluding coordinates, standard_name, etcetera?

Yes, I think so.
On a per-cube basis, any attribute that was read during the load-rules execution is automatically "crossed off a list".
Which happens here, and it gets reset here.
The comment about cacheing is misleading IMO : the cacheing operation is actually the setattr(self, ... line, and not the self._cf_attrs.add(... bit.

I also think the list of used attributes should be reset before each cube is processed.
But I'm just struggling a bit to find where, so I'm still looking into that. It could even be a 'hole' in the scheme.

@pp-mo
Copy link
Member Author

pp-mo commented Jul 21, 2023

I also think the list of used attributes should be reset before each cube is processed.

From experiment, no it does not do that, but I don't think it is a mistake.
It is the list of attributes which are not referenced during the initial "categorisation" of variables by 'fileformats/cf.py', and is not related to what happens in rules processing. It is static after that, not reset for each cube (before running rules).

The "unused_attributes" is only called from "loader._add_unused_attributes", and there it excludes any cf-specific attributes according to a list of 'recognised names'.
"_add_unused_attributes" is called once per built data object (cubes, coords, etc) during cube loading

So this list mustn't change, because it is used repeatedly (e.g. to build a new replica dimcoord for each data cube).
I guess the point is that these "unused" attributes are only fetched by calling cf_var.cf_attrs_unused(), and not through cf_var.__getattr__, so they don't get "crossed off" the list.

@pp-mo
Copy link
Member Author

pp-mo commented Jul 21, 2023

So this list mustn't change, because it is used repeatedly (e.g. to build a new replica dimcoord for each data cube). I guess the point is that these "unused" attributes are only fetched by calling cf_var.cf_attrs_unused(), and not through cf_var.__getattr__, so they don't get "crossed off" the list.

FTR I'm not a great fan of this code, due to the slightly 'hidden' way it does some things!
I don't like that the CFVariable classes acts as "wrappers" to the underlying netcdf variables, and I don't think providing access to pproperties of the underlying 'real' variable via the CFVariable.__getattr__ hook is a terribly good idea -- I think it would be a whole lot clearer if all such accesses were made explicitly with cf_var.cf_data.getncattr().
In fact, the CFVariable code uses self.getncattr(),
-- which only works because self.getncattr --> self.cf_data.getncattr, via the getattr` mechanism.
I really think that's a bit too clever, and doesn't help anything much.

I also find the naming of the "cf_var" and "cf_data" arguments in this code constantly confusing -- effectively sometimes "cf" in a name means it is an object of the 'fileformats/cf' module, and sometimes it means it is a netCDF4 object !

Copy link
Contributor

@trexfeathers trexfeathers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating the unused attributes @pp-mo. I agree it's confusing!

I remain surprised that it has taken so few changes to get the loading code working, however the tests are proof that you haven't missed anything.

I don't think there's any need to cover these changes with a FUTURE flag, since they only affect edge cases and not in a catastrophic way.

@trexfeathers trexfeathers merged commit 68eaa53 into SciTools:FEATURE_split_attrs Jul 21, 2023
15 checks passed
@pp-mo
Copy link
Member Author

pp-mo commented Jul 22, 2023

Brilliant, thanks for your careful approach @trexfeathers !

@pp-mo pp-mo deleted the splitattrs_ncload branch August 18, 2023 10:16
ESadek-MO pushed a commit that referenced this pull request Nov 21, 2023
* Split-attrs: Cube metadata refactortests (#4993)

* Convert Test___eq__ to pytest.

* Convert Test_combine to pytest.

* Convert Test_difference to pytest.

* Review changes.

* Split attrs - tests for status quo (#4960)

* Tests for attribute handling in netcdf load/save.

* Tidy test functions.

* Fix import order exception.

* Add cf-global attributes test.

* Towards more pytest-y implemenation.

* Replace 'create_testcase' with fixture which also handles temporary directory.

* Much tidy; use fixtures to parametrise over multiple attributes.

* Fix warnings; begin data-style attrs tests.

* Tests for data-style attributes.

* Simplify setup fixture + improve docstring.

* No parallel test runner, to avoid error for Python>3.8.

* Fixed for new-style netcdf module.

* Small review changes.

* Rename attributes set 'data-style' as 'local-style'.

* Simplify use of fixtures; clarify docstrings/comments and improve argument names.

* Clarify testing sections for different attribute 'styles'.

* Re-enable parallel testing.

* Sorted params to avoid parallel testing bug - pytest#432.

* Rename test functions to make alpha-order match order in class.

* Split netcdf load/save attribute testing into separate sourcefile.

* Add tests for loaded cube attributes; refactor to share code between Load and Roundtrip tests.

* Add tests for attribute saving.

* Fix method names in comments.

* Clarify source of Conventions attributes.

* Explain the test numbering in TestRoundtrip/TestLoad.

* Remove obsolete test helper method.

* Fix small typo; Fix numbering of testcases in TestSave.

* Implement split cube attributes. (#5040)

* Implement split cube attributes.

* Test fixes.

* Modify examples for simpler metadata printouts.

* Added tests, small behaviour fixes.

* Simplify copy.

* Fix doctests.

* Skip doctests with non-replicable outputs (from use of sets).

* Tidy test comments, and add extra test.

* Tiny typo.

* Remove redundant redefinition of Cube.attributes.

* Add CubeAttrsDict in module __all__ + improve docs coverage.

* Review changes - small test changes.

* More review changes.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix CubeAttrsDict example docstrings.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Odd small fixes.

* Improved docstrings and comments; fix doctests.

* Don't sidestep netcdf4 thread-safety.

* Publicise LimitedAttributeDict, so CubeAttrsDict can refer to it.

* Fix various internal + external links.

* Update lib/iris/cube.py

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Update lib/iris/cube.py

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Update lib/iris/cube.py

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Update lib/iris/cube.py

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Streamline docs.

* Review changes.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Splitattrs ncload (#5384)

* Distinguish local+global attributes in netcdf loads.

* Small test fixes.

* Small doctest fix.

* Fix attribute load-save tests for new behaviour, and old-behaviour equivalence.

* Split attrs docs (#5418)

* Clarification in CubeAttrsDict examples.

* CubeAttrsDict fix docstring typo.

* Raise awareness of split attributes in user guide.

* What's New entry.

* Changes to metadata documentation.

* Splitattrs ncsave redo (#5410)

* Add docs and future switch, no function yet.

* Typing enables code completion for Cube.attributes.

* Make roundtrip checking more precise + improve some tests accordingly (cf. #5403).

* Rework all tests to use common setup + results-checking code.

* Saver supports split-attributes saving (no tests yet).

* Tiny docs fix.

* Explain test routines better.

* Fix init of FUTURE object.

* Remove spurious re-test of FUTURE.save_split_attrs.

* Don't create Cube attrs of 'None' (n.b. but no effect as currently used).

* Remove/repair refs to obsolete routines.

* Check all warnings from save operations.

* Remove TestSave test numbers.

* More save cases: no match with missing, and different cube attribute types.

* Run save/roundtrip tests both with+without split saves.

* Fix.

* Review changes.

* Fix changed warning messages.

* Move warnings checking from 'run' to 'check' phase.

* Simplify and improve warnings checking code.

* Fix wrong testcase.

* Minor review changes.

* Fix reverted code.

* Use sets to simplify demoted-attributes code.

* WIP

* Working with iris 3.6.1, no errors TestSave or TestRoundtrip.

* Interim save (incomplete?).

* Different results form for split tests; working for roundtrip.

* Check that all param lists are sorted.

* Check matrix result-files compatibility; add test_save_matrix.

* test_load_matrix added; two types of load result.

* Finalise special-case attributes.

* Small docs tweaks.

* Add some more testcases,

* Ensure valid sort-order for globals of possibly different types.

* Initialise matrix results with legacy values from v3.6.1 -- all matching.

* Add full current matrix results, i.e. snapshot current behaviours.

* Review changes : rename some matrix testcases, for clarity.

* Splitattrs ncsave redo commonmeta (#5538)

* Define common-metadata operartions on split attribute dictionaries.

* Tests for split-attributes handling in CubeMetadata operations.

* Small tidy and clarify.

* Common metadata ops support mixed split/unsplit attribute dicts.

* Clarify with better naming, comments, docstrings.

* Remove split-attrs handling to own sourcefile, and implement as a decorator.

* Remove redundant tests duplicated by matrix testcases.

* Newstyle split-attrs matrix testing, with fewer testcases.

* Small improvements to comments + docstrings.

* Fix logic for equals expectation; expand primary/secondary independence test.

* Clarify result testing in metadata operations decorator.

* Splitattrs equalise (#5586)

* Add tests in advance for split-attributes handling cases.

* Move dict conversion inside utility, for use elsewhere.

* Add support for split-attributes to equalise_attributes.

* Update lib/iris/util.py

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Update lib/iris/tests/unit/util/test_equalise_attributes.py

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Simplify and clarify equalise_attributes code.

---------

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* Fix merge-fail messaging for attribute mismatches. (#5590)

* Extra CubeAttrsDict methods to emulate dictionary behaviours. (#5592)

* Extra CubeAttrsDict methods to emulate dictionary behaviours.

* Don't use staticmethod on fixture.

* Add Iris warning categories to saver warnings.

* Type equality fixes for new flake8.

* Licence header fixes.

* Splitattrs ncsave deprecation (#5595)

* Small improvement to split-attrs whatsnew.

* Emit deprecation warning when saving without split-attrs enabled.

* Stop legacy-split-attribute warnings from upsetting delayed-saving tests.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🏁 Done
Development

Successfully merging this pull request may close these issues.

2 participants