Skip to content

Commit

Permalink
Merge pull request #333 from rl-institut/fix/unstacking_warning_cols
Browse files Browse the repository at this point in the history
Fix/unstacking warning cols
  • Loading branch information
Stefanie08 authored Jul 24, 2023
2 parents 63d01ad + 37290dc commit e33095a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
7 changes: 4 additions & 3 deletions oemof_b3/tools/data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,10 @@ def unstack_timeseries(df):
lost_columns = ["source", "comment"]
for col in lost_columns:
if col in list(df.columns):
logger.warning(
f"Caution any remarks in column '{col}' are lost after unstacking."
)
if not _df[col].isna().all() or _df[col].values.all() == "None":
logger.warning(
f"Caution any remarks in column '{col}' are lost after unstacking."
)

# Process values of series
values_series = []
Expand Down
19 changes: 19 additions & 0 deletions tests/_files/oemof_b3_resources_timeseries_stacked_comments.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
id_ts;region;var_name;timeindex_start;timeindex_stop;timeindex_resolution;series;var_unit;source;comment
0;BB;biomass-st_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
1;BB;ch4-gt_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
2;BB;electricity-curtailment_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
3;BB;electricity-demand_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1889794.8071409904, 1870448.7801216468, 1803840.2010708163];;;We have a comment here.
4;BB;electricity-liion-battery_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
5;BB;electricity-liion-battery_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[882505.2, 1194379.0, 1289504.5];;;We have a comment here.
6;BB;electricity-shortage_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
7;BB;solar-pv_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
8;BB;wind-onshore_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1145114.0, 801141.1, 595497.8];;;We have a comment here.
9;BE;biomass-st_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
10;BE;ch4-gt_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
11;BE;electricity-curtailment_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
12;BE;electricity-demand_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1889794.8071409904, 1870448.7801216468, 1803840.2010708163];;;We have a comment here.
13;BE;electricity-liion-battery_in_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
14;BE;electricity-liion-battery_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[882505.2, 1194379.0, 1289504.5];;;We have a comment here.
15;BE;electricity-shortage_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
16;BE;solar-pv_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[0.0, 0.0, 0.0];;;We have a comment here.
17;BE;wind-onshore_out_electricity;2019-01-01 00:00:00;2019-01-01 02:00:00;H;[1145114.0, 801141.1, 595497.8];;;We have a comment here.
56 changes: 56 additions & 0 deletions tests/test_data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
import pandas as pd
import pytest
import unittest
from unittest.mock import patch

from oemof_b3.tools.data_processing import (
HEADER_B3_SCAL,
Expand Down Expand Up @@ -38,6 +40,9 @@ def full_path(filename):
)
path_file_sc_mixed_types = full_path("oemof_b3_resources_scalars_mixed_types.csv")
path_file_ts_stacked = full_path("oemof_b3_resources_timeseries_stacked.csv")
path_file_ts_stacked_comments = full_path(
"oemof_b3_resources_timeseries_stacked_comments.csv"
)
path_oemof_results_flows = full_path("oemof_results_flows.csv")
path_oemof_b3_results_timeseries_flows = full_path(
"oemof_b3_results_timeseries_flows.csv"
Expand Down Expand Up @@ -482,6 +487,57 @@ def test_unstack_stack_scalars_on_example_data():
assert pd.testing.assert_frame_equal(df, df_stacked) is None


class test_unstack_warning_source_comment(unittest.TestCase):
"""
This test verifies whether the caution message is appropriately raised
when executing the function unstack_timeseries(). The caution message will
be raised if source and comments are not empty.
"""

def setUp(self):
self.df_with_comments = load_b3_timeseries(path_file_ts_stacked_comments)
self.df_wo_comments = load_b3_timeseries(path_file_ts_stacked)

# Assert WARNING with unstack_timeseries(df_with_comments)
def test_unstack_warning_source_comment(self):
# Patch the logger.warning method to capture the warning calls
with patch(
"oemof_b3.tools.data_processing.logger.warning"
) as mock_logger_warning:
# Call the function with df_with_comments, which should raise a warning
unstack_timeseries(self.df_with_comments)

# Check if the logger.warning was called with the expected messages
expected_warnings = [
"Caution any remarks in column 'source' are lost after unstacking.",
"Caution any remarks in column 'comment' are lost after unstacking.",
]

# Check if any of the expected warning messages are contained
# in the captured warning messages
warnings_called = [call[0][0] for call in mock_logger_warning.call_args_list]

self.assertTrue(
any(warning_msg in warnings_called for warning_msg in expected_warnings)
)

# Assert that unstack_timeseries(df_wo_comments) does not give the warning:
def test_unstack_no_warning(self):
# Patch the logger.warning method to capture the warning calls
with patch(
"oemof_b3.tools.data_processing.logger.warning"
) as mock_logger_warning:
# Call the function with df_wo_comments, which should not raise a warning
unstack_timeseries(self.df_wo_comments)

# Check that logger.warning was not called in this case
mock_logger_warning.assert_not_called()


if __name__ == "__main__":
unittest.main()


def test_merge_a_into_b():
r"""
Tests merge function.
Expand Down

0 comments on commit e33095a

Please sign in to comment.