Skip to content

Add Var.push_dim#354

Open
ph-kev wants to merge 2 commits into
mainfrom
kp/singleton
Open

Add Var.push_dim#354
ph-kev wants to merge 2 commits into
mainfrom
kp/singleton

Conversation

@ph-kev
Copy link
Copy Markdown
Member

@ph-kev ph-kev commented Oct 31, 2025

closes #353 - This PR adds Var.push_dim which adds an extra dimension of length 1 to a OutputVar.

@ph-kev ph-kev marked this pull request as draft October 31, 2025 23:47
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 31, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.44%. Comparing base (4eab8a6) to head (327c99f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #354   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files          18       18           
  Lines        1965     1978   +13     
=======================================
+ Hits         1954     1967   +13     
  Misses         11       11           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ph-kev ph-kev force-pushed the kp/singleton branch 4 times, most recently from b8809c7 to 3a8d485 Compare November 3, 2025 17:41
@ph-kev ph-kev marked this pull request as ready for review November 3, 2025 17:42
Comment thread src/Var.jl
Comment on lines +2661 to +2662
function push_dim(var::OutputVar, dim_name, dim_value, dim_attrib_pairs...;)
dims = deepcopy(var.dims)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This doesn't work properly if dim_value is a Dates.DateTime. I am not too sure how crucial that functionality is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this because dims is a dict with values that are arrays of floats? Could you just make a new dict and the loop over the old one and add the keys/values?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you don't plan on supporting this, it might be good to catch the case and give an informative error

Comment thread docs/src/api.md
Comment on lines 83 to 85
Base.permutedims(var::OutputVar, perm)
Var.push_dim
Var.reordered_as
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am also not sure if this belongs in ClimaAnalysis extension in ClimaCalibrate or ClimaAnalysis.

Comment thread src/Var.jl
Comment on lines +2661 to +2662
function push_dim(var::OutputVar, dim_name, dim_value, dim_attrib_pairs...;)
dims = deepcopy(var.dims)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this because dims is a dict with values that are arrays of floats? Could you just make a new dict and the loop over the old one and add the keys/values?

Comment thread src/Var.jl
If you want to change the order of the dimensions, then call `permutedims` after
this function.
"""
function push_dim(var::OutputVar, dim_name, dim_value, dim_attrib_pairs...;)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not a huge fan of this function name, but I also can't think of a better name. Maybe something like
append_singleton_dim?

Comment thread src/Var.jl
Comment on lines +2661 to +2662
function push_dim(var::OutputVar, dim_name, dim_value, dim_attrib_pairs...;)
dims = deepcopy(var.dims)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you don't plan on supporting this, it might be good to catch the case and give an informative error

Copy link
Copy Markdown
Member

@nefrathenrici nefrathenrici left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment thread NEWS.md
## Add singleton dimension

This release introduces `push_dim` which allows the user to add a singleton
dimension to end of the dimensions of a `OutputVar`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
dimension to end of the dimensions of a `OutputVar`.
dimension to the end of the dimensions of a `OutputVar`.

Comment thread src/Var.jl
Comment on lines +2668 to +2670
error(
"Dimension ($dim_name) cannot be added as it already corresponds to an existing dimension ($conventional_dim_names)",
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
error(
"Dimension ($dim_name) cannot be added as it already corresponds to an existing dimension ($conventional_dim_names)",
)
existing_dim = findfirst(d -> conventional_dim_name(d) == conventional_dim_name(dim_name), keys(dims))
error(
"Dimension \"$dim_name\" cannot be added as it already corresponds to an existing dimension \"$existing_dim\" (both map to \"$(conventional_dim_name(dim_name))\")"
)

Minor, but it would be good to print the conflicting dimension for the user.

Comment thread test/test_Var.jl
@test size(lat_var.data) == (2, 1)

# Error handling
@test_throws ErrorException ClimaAnalysis.push_dim(var, "lon", 42.0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add a test for adding a dimension with a different name but same conventional name? For example, adding a "longitude" dimension since the "lon" dimension exists already:
@test_throws ErrorException ClimaAnalysis.push_dim(var, "longitude", 42.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add function to add singleton dimension

3 participants