gcages.testing#
Code to support our tests
This is here, rather than in our tests directory
because of the issues that come
when you turn your tests into a package using __init__.py files
(for details, see https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#choosing-an-import-mode).
Functions:
| Name | Description |
|---|---|
assert_frame_equal |
Assert two pd.DataFrame's are equal. |
get_ar6_all_emissions |
Get all emissions from AR6 for a given model-scenario |
get_ar6_harmonised_emissions |
Get all harmonised emissions from AR6 for a given model-scenario |
get_ar6_infilled_emissions |
Get all infilled emissions from AR6 for a given model-scenario |
get_ar6_metadata_outputs |
Get metadata from AR6 for a given model-scenario |
get_ar6_raw_emissions |
Get all raw emissions from AR6 for a given model-scenario |
get_ar6_temperature_outputs |
Get temperature outputs we've downloaded from AR6 for a given model-scenario |
get_cmip7_scenariomip_complete_emissions |
Get complete emissions from CMIP7 ScenarioMIP outputs |
get_cmip7_scenariomip_harmonised_emissions |
Get harmonised emissions from CMIP7 ScenarioMIP outputs |
get_cmip7_scenariomip_pre_processed_emissions |
Get pre-processed emissions from CMIP7 ScenarioMIP outputs |
get_key_testing_model_scenario_parameters |
Create a pytest parameterization decorator for model-scenario pairs. |
guess_magicc_exe |
Guess the MAGICC executable based on the operating system |
assert_frame_equal #
Assert two pd.DataFrame's are equal.
This is a very thin wrapper around pd.testing.assert_frame_equal that makes some use of pandas_indexing to give slightly nicer and clearer errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
DataFrame
|
Result |
required |
exp
|
DataFrame
|
Expected value |
required |
rtol
|
float
|
Relative tolerance |
1e-08
|
**kwargs
|
Any
|
Passed to pd.testing.assert_frame_equal |
{}
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
The frames aren't equal |
Source code in src/gcages/testing.py
get_ar6_all_emissions
cached
#
get_ar6_all_emissions(
model: str,
scenario: str,
processed_ar6_output_data_dir: Path,
) -> DataFrame
Get all emissions from AR6 for a given model-scenario
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model |
required |
scenario
|
str
|
Scenario |
required |
processed_ar6_output_data_dir
|
Path
|
Directory in which the AR6 was processed into individual model-scenario files (In the repo, see |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All emissions from AR6 for |
Source code in src/gcages/testing.py
get_ar6_harmonised_emissions
cached
#
get_ar6_harmonised_emissions(
model: str,
scenario: str,
processed_ar6_output_data_dir: Path,
) -> DataFrame
Get all harmonised emissions from AR6 for a given model-scenario
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model |
required |
scenario
|
str
|
Scenario |
required |
processed_ar6_output_data_dir
|
Path
|
Directory in which the AR6 was processed into individual model-scenario files (In the repo, see |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All harmonised emissions from AR6 for |
Source code in src/gcages/testing.py
get_ar6_infilled_emissions
cached
#
get_ar6_infilled_emissions(
model: str,
scenario: str,
processed_ar6_output_data_dir: Path,
) -> DataFrame
Get all infilled emissions from AR6 for a given model-scenario
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model |
required |
scenario
|
str
|
Scenario |
required |
processed_ar6_output_data_dir
|
Path
|
Directory in which the AR6 output was processed into model-scenario files (In the repo, see |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All infilled emissions from AR6 for |
Source code in src/gcages/testing.py
get_ar6_metadata_outputs
cached
#
get_ar6_metadata_outputs(
model: str,
scenario: str,
ar6_output_data_dir: Path,
filename: str = "AR6_Scenarios_Database_metadata_indicators_v1.1_meta.csv",
) -> DataFrame
Get metadata from AR6 for a given model-scenario
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model |
required |
scenario
|
str
|
Scenario |
required |
ar6_output_data_dir
|
Path
|
Directory in which the AR6 output was saved |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Metadata from AR6 for |
Source code in src/gcages/testing.py
get_ar6_raw_emissions
cached
#
get_ar6_raw_emissions(
model: str,
scenario: str,
processed_ar6_output_data_dir: Path,
) -> DataFrame
Get all raw emissions from AR6 for a given model-scenario
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model |
required |
scenario
|
str
|
Scenario |
required |
processed_ar6_output_data_dir
|
Path
|
Directory in which the AR6 was processed into individual model-scenario files (In the repo, see |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All raw emissions from AR6 for |
Source code in src/gcages/testing.py
get_ar6_temperature_outputs
cached
#
get_ar6_temperature_outputs(
model: str,
scenario: str,
processed_ar6_output_data_dir: Path,
dropna: bool = True,
) -> DataFrame
Get temperature outputs we've downloaded from AR6 for a given model-scenario
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model |
required |
scenario
|
str
|
Scenario |
required |
processed_ar6_output_data_dir
|
Path
|
Directory in which the AR6 output was processed into model-scenario files (In the repo, see |
required |
dropna
|
bool
|
Drop time columns that only contain NaN |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
All temperature outputs we've downloaded from AR6 for |
Source code in src/gcages/testing.py
get_cmip7_scenariomip_complete_emissions
cached
#
get_cmip7_scenariomip_complete_emissions(
model: str,
scenario: str,
processed_cmip7_scenariomip_output_data_dir: Path,
) -> DataFrame
Get complete emissions from CMIP7 ScenarioMIP outputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model for which to retrieve outputs |
required |
scenario
|
str
|
Scenario for which to retrieve outputs |
required |
processed_cmip7_scenariomip_output_data_dir
|
Path
|
Directory in which the CMIP7 ScenarioMIP output was saved |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All complete emissions from CMIP7 ScenarioMIP for |
Source code in src/gcages/testing.py
get_cmip7_scenariomip_harmonised_emissions
cached
#
get_cmip7_scenariomip_harmonised_emissions(
model: str,
scenario: str,
processed_cmip7_scenariomip_output_data_dir: Path,
) -> DataFrame
Get harmonised emissions from CMIP7 ScenarioMIP outputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model for which to retrieve outputs |
required |
scenario
|
str
|
Scenario for which to retrieve outputs |
required |
processed_cmip7_scenariomip_output_data_dir
|
Path
|
Directory in which the CMIP7 ScenarioMIP output was saved |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All harmonised emissions from CMIP7 ScenarioMIP for |
Source code in src/gcages/testing.py
get_cmip7_scenariomip_pre_processed_emissions
cached
#
get_cmip7_scenariomip_pre_processed_emissions(
model: str,
scenario: str,
processed_cmip7_scenariomip_output_data_dir: Path,
) -> DataFrame
Get pre-processed emissions from CMIP7 ScenarioMIP outputs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model for which to retrieve outputs |
required |
scenario
|
str
|
Scenario for which to retrieve outputs |
required |
processed_cmip7_scenariomip_output_data_dir
|
Path
|
Directory in which the CMIP7 ScenarioMIP output was saved |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
All pre-processed emissions from CMIP7 ScenarioMIP for |
Source code in src/gcages/testing.py
get_key_testing_model_scenario_parameters #
get_key_testing_model_scenario_parameters(
model_scenarios: tuple[tuple[str, str]],
) -> MarkDecorator
Create a pytest parameterization decorator for model-scenario pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_scenarios
|
tuple[tuple[str, str]]
|
Tuples of (model, scenario) pairs to parameterize. |
required |
Returns:
| Type | Description |
|---|---|
MarkDecorator
|
A pytest decorator that runs a test for each (model, scenario) pair. |
Raises:
| Type | Description |
|---|---|
MissingOptionalDependencyError
|
If pytest is not installed. |
Examples:
>>> @get_key_testing_model_scenario_parameters((("m1", "s1"), ("m2", "s2")))
... def test_func(model, scenario): ...
Source code in src/gcages/testing.py
guess_magicc_exe #
Guess the MAGICC executable based on the operating system
If the MAGICC_EXECUTABLE_7 environment variable is set,
that is simply used and this function becomes almost a no-op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
magicc_executables_dir
|
Path
|
Directory in which MAGICC executables are stored |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the MAGICC executable |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
The guessed path to the MAGICC executable does not exist |