Conversation
`make_index()` created a Python tuple per data row, which dominated the runtime of `filter()` (and every method using it) on large data.
`filter()` copies the full IamDataFrame, rebuilds the index and sorts the data, which dominated the runtime when aggregating many variables one by one. Use boolean masks on the data instead.
`MultiIndex.from_arrays()` keeps unused categories of categorical columns as levels, so they showed up in `IamDataFrame.model` and similar.
aggregate_region(), filtering and index creation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1000 +/- ##
=====================================
Coverage 94.7% 94.7%
=====================================
Files 70 70
Lines 6762 6771 +9
=====================================
+ Hits 6406 6415 +9
Misses 356 356 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Author
|
Some tests where failing with errors like these Are these intermittent test run failures something you have seen happen for |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please confirm that this PR has done the following:
Documentation AddedDescription of PR
Region processing in nomenclature calls
aggregate_region()once per common region for every variable with weights or components. That is up to about 13,000 calls in the example below (601 variables × 22 common regions). Every call deep-copies (almost) the entireIamDataFrame, which leads to massive memory usage for dataframes with millions of data points.This PR makes two changes:
make_index()builds the index withMultiIndex.from_arrays()instead of creating a Python tuple per data row and then usingMultiIndex.from_tuples().from_arrays()takes column-wise data as input and we already hold the data like that.from_tuplestakes row-wise input and then transforms row- to column-wise data under the hood. So usingfrom_arrays()directly saves two conversions: arrays -> tuples -> arrays. The old code ensured that unused categories of categorical columns don't become index levels. To keep the behavior the same,remove_unused_levels()was added,. This affectsIamDataFrameinitialization,filter()and everything else that usesmake_index(), and it mostly saves runtime.aggregate_region()selects rows with boolean masks instead offilter().filter()deep-copies theIamDataFrame(data, meta, exclude), rebuilds the index and sorts the data, although the aggregation only needs the selected rows. Usingfilter()withinplace=Trueis not an option, since it would change the callers dataframe. This is the change that reduces the memory usage.components=True(auto-detection) still usesfilter(), because the auto-detection relies onIamDataFrame._variable_components(). This might be changed in a dedicated follow-up PR.Neither of these changes the time or space order of complexity (big O), but both reduce constant factors of said complexity.
Behavior changes
The results are unchanged: the region processing below returns identical output (
pd.testing.assert_series_equal), and the test suite gives the same results as onmain.One minor difference:
aggregate_region()withsubregionsthat have no data no longer logs "Filtered IamDataFrame is empty!". That warning came from the internalfilter()call. The result is the same (empty, or only thecomponentsat theregionlevel). I think this is acceptable.Tests
test_init_df_with_unused_categories: anIamDataFramefrom a categorical column with unused categories (coversremove_unused_levels()).Benchmark
All numbers are from a ~15MiB IAMC spreadsheet with 2,076,660 data points, 2,226 variables and 33 native regions. I processed it with the
common-definitionsvariables and region mappings: 28 common regions (22 real aggregations and 6 renames), and 601 variables with weights or components. Python 3.12, pandas 2.3.3.Region processing, with nomenclature v0.32.0 and the commits of this PR added one at a time. "Peak memory increase" is the maximum RSS during
nomenclatureRegionProcessor.apply()minus the RSS right before the call (after loading data and definitions), sampled every 20 ms.mainmake_index()(change 1)aggregate_region()(change 2) = this PR* ran in parallel with other benchmarks, so the runtime is overestimated.
The remaining memory is mostly used by nomenclature's own processing. I will open a PR with changes to
nomenclatureto reduce that as well.Benchmark code
Region processing (each variant in a fresh process, on Linux):
make_index()microbenchmarks (runtime is the best of 3 runs after a warm-up):