-
Notifications
You must be signed in to change notification settings - Fork 0
Add function to plot scaling data. #11
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ dependencies = [ | |
| "xarray", | ||
| "pint", | ||
| "pint-xarray", | ||
| "matplotlib", | ||
| ] | ||
|
|
||
| [build-system] | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright 2025 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| def calculate_column_widths(table_data: list[list], first_col_fraction: float = None) -> list: | ||
| """Calculate column widths based on content character length and required width for first column. | ||
|
|
||
| Args: | ||
| table_data (list[list]): Table data including headers. e.g. | ||
| [[ "ncpus", "col1", "col2", "col3"], | ||
| ["region1", 0.1, 0.2, 0.3], | ||
| ["region2", 1. , 2. , 3. ]] | ||
| first_col_fraction (float): If provided, controls the fraction of the table width | ||
edoyango marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assigned to the first column. Default None. | ||
| If set to 0.0 or None, all columns have the same width. | ||
| Must be between 0.0 (inclusive) and 1.0 (exclusive). | ||
|
|
||
| Returns: | ||
| list : Column width fractions, adding up to 1. | ||
|
|
||
| Raises: | ||
| ValueError: If table_data has fewer than 2 rows or 2 columns. | ||
| ValueError: If table_data shape is not rectangular, i.e., all rows do not have the same number of columns. | ||
| """ | ||
| if not table_data: | ||
| return [] | ||
|
|
||
| # Check that table has a header row and row-label column, and no missing elements. | ||
| if len(table_data) > 1: | ||
| for row in table_data[1:]: | ||
| if len(row) != len(table_data[0]): | ||
| raise ValueError("Table rows must have the same number of elements") | ||
| else: | ||
| raise ValueError("Table must have at least 2 rows (first row is table header)") | ||
| if len(table_data[0]) < 2: | ||
| raise ValueError("Table must have at least 2 columns (first column is row label)") | ||
|
|
||
| if first_col_fraction is not None and not (0 <= first_col_fraction < 1): | ||
| raise ValueError("first_col_fration must be between 0 and 1 (exclusive)") | ||
|
|
||
| n_cols = len(table_data[0]) | ||
|
|
||
| # Calculate max content length for each column based on no. of chars | ||
| max_lengths = [] | ||
| for col in range(n_cols): | ||
| col_lengths = [len(str(row[col])) for row in table_data] | ||
| max_lengths.append(max(col_lengths)) | ||
|
|
||
| if first_col_fraction and first_col_fraction > 0: | ||
| # Set data columns to proportional widths based on content and first_col_fraction | ||
| data_cols_total = sum(max_lengths[1:]) | ||
| base_width = (1 - first_col_fraction) / data_cols_total | ||
|
|
||
| col_widths = [first_col_fraction] | ||
| for length in max_lengths[1:]: | ||
| col_widths.append(length * base_width) | ||
|
|
||
| else: | ||
| # Equal column width | ||
| total_length = sum(max_lengths) | ||
| col_widths = [length / total_length for length in max_lengths] | ||
|
|
||
| return col_widths | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # Copyright 2025 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import pytest | ||
|
|
||
| from access.profiling.plotting_utils import calculate_column_widths | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def table_data(): | ||
| """Fixture returning sample table data.""" | ||
| return [ | ||
| ["Cell00", "Cell01", "Cell 02"], | ||
| ["Cell10", "Cell11", "Cell 12"], | ||
| ["Cell20", "Cell21", "Cell 22"], | ||
| ["Cell30", "Cell31", "Cell32"], | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def nonrectangular_table_data(): | ||
| """Fixture returning sample invalid table data where table isn't rectangular.""" | ||
| return [ | ||
| ["Cell00", "Cell01", "Cell02"], | ||
| ["Cell10", "Cell11"], | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def singlerow_table_data(): | ||
| """Fixture returning sample invalid table data where table only has 1 row.""" | ||
| return [ | ||
| ["Cell00", "Cell01", "Cell02"], | ||
| ] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def singlecol_table_data(): | ||
| """Fixture returning sample invalid table data where table only has 1 column.""" | ||
| return [ | ||
| ["Cell00"], | ||
| ["Cell10"], | ||
| ] | ||
|
|
||
|
|
||
| def test_calculate_column_widths_flexible(table_data): | ||
| """Test the calculate_column_widths function.""" | ||
| # Test with empty table | ||
| assert calculate_column_widths([]) == [] | ||
|
|
||
| # Test with multiple rows, multiple columns | ||
| col_widths = calculate_column_widths(table_data, 0.4) | ||
| assert abs(sum(col_widths) - 1.0) < 1e-6 # Sum to 1.0 | ||
| assert col_widths == pytest.approx([0.4, 0.2, 0.4]) # Proportional to content length with first column flexible | ||
|
|
||
|
|
||
| def test_calculate_column_widths_fixed(table_data): | ||
| # Test with first_col_flexible=False | ||
| col_widths = calculate_column_widths(table_data) | ||
| assert abs(sum(col_widths) - 1.0) < 1e-6 # Sum to 1.0 | ||
| assert col_widths == [0.25, 0.25, 0.5] # Proportional to content length | ||
|
|
||
|
|
||
| def test_wrong_column_widths(table_data): | ||
| with pytest.raises(ValueError): | ||
| calculate_column_widths(table_data, first_col_fraction=1.0) | ||
| with pytest.raises(ValueError): | ||
| calculate_column_widths(table_data, first_col_fraction=-1.0) | ||
|
|
||
|
|
||
| def test_invalid_tables(nonrectangular_table_data, singlerow_table_data, singlecol_table_data): | ||
| with pytest.raises(ValueError): | ||
| calculate_column_widths(nonrectangular_table_data) | ||
| with pytest.raises(ValueError): | ||
| calculate_column_widths(singlerow_table_data) | ||
| with pytest.raises(ValueError): | ||
| calculate_column_widths(singlecol_table_data) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.