Repository navigation
Add get_hierarchy_dataframe and Hierarchy.rebase - #1510
Merged
Merged
Conversation
HierarchyService.get_hierarchy_dataframe materializes a hierarchy in the shape update_or_create_hierarchy_from_dataframe expects, so a hierarchy can be copied to another server. It wraps ElementService.get_elements_dataframe and normalizes the element type column, attribute type suffixes and level column names. Hierarchy.rebase cuts a hierarchy at a TM1 level so those elements become the new leaves, e.g. to plan at product family instead of SKU level. Totals are preserved: no remaining consolidation loses a child, so short branches in ragged hierarchies and alternate rollups are kept as leaves, with a warning when new leaves overlap. get_hierarchy_dataframe(leaf_level=...) applies the same rebase to the DataFrame. Closes #1509 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Restore the pandas / admin decorators on update_or_create_hierarchy_from_dataframe. They had moved onto get_hierarchy_dataframe, which made a read require admin rights and let non-admins start an update that failed halfway. - Compare edge weights as numbers in update_or_create_hierarchy_from_dataframe. String weights such as '1.000000' never matched the weights in TM1, so every unchanged edge was deleted and added again. - Make Hierarchy.rebase(0) a true no-op. Consolidations without children were turned into Numeric elements. - Rebase before retrieving the elements in get_hierarchy_dataframe, so an invalid leaf_level fails early. - Replace the row-wise DataFrame.apply in _rebase_hierarchy_dataframe with itertuples. Add HierarchyService_dataframe_test.py, which covers these cases against a mocked server and runs without TM1. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Collaborator
|
Looks good overall, and I made a small enhancement. Will merge when the integration tests complete. |
Contributor
|
Tests completed for environment: tm1-11-cloud. Check artifacts for details. |
"rebase" reads as git rebase to developers and as re-indexing to a base year to finance users. "prune_to_level" says what happens: the hierarchy is cut back so that the given TM1 level becomes the new leaf level. - Rename the private helper to _prune_hierarchy_dataframe - Update docstrings and the overlap warning; clarify that levels are counted from the bottom, not as depth from the top - Rename the tests accordingly The leaf_level parameter is unchanged. No alias for the old name, as rebase was never released.
Contributor
|
Tests completed for environment: tm1-11-cloud. Check artifacts for details. |
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.
Closes #1509
Why
The main goal is to make it easy to take a hierarchy from one TM1 model and recreate it in another, either as is or at a coarser grain.
Today we can build a hierarchy from a DataFrame with
update_or_create_hierarchy_from_dataframe(), but there's no mirror operation that gives us an existing hierarchy in that same shape.ElementService.get_elements_dataframe()comes close, but its default output doesn't feed straight back in: the type column is calledTypeinstead ofElementType, attribute columns don't carry their type suffix (so numeric attributes would be recreated as strings), and custom TM1 level names (e.g.Family,SKU) aren't recognized as parent columns.The second need comes from planning models. A common setup is that actuals arrive at SKU level, but planning happens at product family level (e.g. 3 levels higher). The planning model needs the same hierarchy with the families as leaves, so the actuals can be loaded 1:1 from the families' consolidated cells in the source cube.
What changes
HierarchyService.get_hierarchy_dataframe(dimension_name, hierarchy_name, leaf_level=None, ...)This is a thin wrapper around
get_elements_dataframe()(no logic is duplicated) that normalizes the output, so the round trip just works:Elements with multiple parents get one row per path, so alternate rollups survive the trip. I also moved the level-column detection out of
update_or_create_hierarchy_from_dataframe()into a small shared helper (_identify_level_columns), because both functions need to read those columns the same way. Its behaviour is unchanged.Hierarchy.rebase(leaf_level)This cuts the hierarchy at a TM1 level (counted from the bottom, leaves are level 0). The elements on that level become numeric leaves and everything below them is removed, so a hierarchy with its top at level 8 rebased to level 3 ends up with its top at level 5. Element names are kept, and the method returns the new leaves, which is exactly the element set we'd use for the source view when moving the data.
Passing
leaf_leveltoget_hierarchy_dataframe()applies the same rebase to the DataFrame. The decision of what to keep is made on the full hierarchy fetched from TM1 (not on the DataFrame), and attribute values, including those of the new leaves, carry over to the target.How the cut works (and why)
The rule is built so that totals are preserved: if each new leaf is loaded with the consolidated value of the same element in the original hierarchy, every remaining consolidation adds up to the same total as before. The way to guarantee that is that no remaining consolidation ever loses a child:
Promoconsolidation underTotalthat also holds SKUs from the families): they're kept as leaves too, and aUserWarninglists the new leaves that share descendants. Their values overlap, but they overlap in the source in exactly the same way, so the totals still reconcile.One thing to bear in mind is that we initially considered dropping alternate rollups. Working through the numbers showed that dropping them is what would actually break reconciliation with the source, which is why they're kept and flagged instead.
Tests
Tests/Hierarchy_test.py: 14 new unit tests forrebase(balanced, ragged, alternate rollups, string elements, level 0 as a no-op, cutting at the top, invalid levels, case and space insensitivity, circular references). One of them consolidates sample values before and after the rebase and asserts every remaining total matches.Tests/HierarchyService_test.py: 4 new integration tests (DataFrame shape, full round trip into a second dimension, rebase vialeaf_level, andskip_consolidationscombined withleaf_level).Hierarchy_test.pyandHierarchyService_test.pysuites pass against a TM1 11.8 instance (99 tests), and the new integration tests also pass withuse_blob=True.blackandruffare clean on the changed files.Also worth flagging: renaming custom TM1 level names to
levelXXXis implemented, but it hasn't been run against a dimension that actually has custom level names.Out of scope
Moving the cube data itself. Loading the source's level N consolidated cells into the target's new leaves remains a separate step.
As a new feature, I'd suggest
release:minorwhen merging. Happy to walk through the rebase rule or adjust anything that doesn't fit how you'd like this exposed.🤖 Generated with Claude Code