Skip to content

Add D3_to_df utility to put 3D output into a Pandas DataFrame - #1226

Open
arihantlodha-cmd wants to merge 1 commit into
PSLmodels:masterfrom
arihantlodha-cmd:add-d3-to-df-util
Open

arihantlodha-cmd wants to merge 1 commit into
PSLmodels:masterfrom
arihantlodha-cmd:add-d3-to-df-util

Conversation

@arihantlodha-cmd

Copy link
Copy Markdown
Contributor

What this does

Closes #1009. Adds utils.D3_to_df, which reshapes a three dimensional (T x S x J) transition-path output array into a tidy DataFrame with a (Year, Age) MultiIndex and one column per ability type j. This makes it easier to use model output in other software (plotting, spreadsheets), where a panel layout is more convenient than a raw 3D NumPy array.

Follows the sketch in the issue, with input validation added: the function raises ValueError if the requested key is not in tpi_vars or the selected array is not three dimensional.

Usage

from ogcore import utils
df = utils.D3_to_df(tpi_vars, var="c_path", start_year=p.start_year)
df.to_csv("c_path.csv")

Testing

Adds test_D3_to_df (checks shape, index names, first/last year labels, and a value round-trip) and test_D3_to_df_errors (missing key and non-3D array). Both pass; ruff check and ruff format --check are clean.

Closes PSLmodels#1009. Adds utils.D3_to_df, which reshapes a three dimensional
(T x S x J) transition-path output array into a tidy DataFrame with a
(Year, Age) MultiIndex and one column per ability type j. This makes it
easier to use model output in other software (plotting, spreadsheets),
where a panel layout is more convenient than a raw 3D NumPy array.

The function validates that the requested key exists and that the selected
array is three dimensional, raising ValueError otherwise. Adds tests for the
reshape (shape, index labels, value round-trip) and both error cases.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.23%. Comparing base (9baa4b2) to head (5249a3d).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1226      +/-   ##
==========================================
+ Coverage   75.17%   75.23%   +0.05%     
==========================================
  Files          24       24              
  Lines        6031     6045      +14     
==========================================
+ Hits         4534     4548      +14     
  Misses       1497     1497              
Flag Coverage Δ
unittests 75.23% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
ogcore/utils.py 85.99% <100.00%> (+0.39%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

Create utility for putting 3D objects into Pandas DataFrames

2 participants