Skip to content
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

Handle BOMs when loading HRA criteria tables #1461

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Unreleased Changes
table columns: ``cur_path``, ``fut_path``, and ``base_path`` are meant
to be file system path strings.
https://github.com/natcap/invest/issues/1455
* HRA
* Fixed an issue preventing the HRA criteria table from loading when the
table was UTF-8 encoded with a Byte-Order Marker.
https://github.com/natcap/invest/issues/1460
* NDR
* Fixing an issue where minor geometric issues in the watersheds input
(such as a ring self-intersection) would raise an error in the model.
Expand Down
30 changes: 30 additions & 0 deletions tests/test_hra.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,36 @@ def test_criteria_table_parsing(self):
pandas.testing.assert_frame_equal(
expected_composite_dataframe, composite_dataframe)

def test_criteria_table_parsing_with_bom(self):
"""HRA: criteria table - parse a BOM."""
from natcap.invest import hra

criteria_table_path = os.path.join(self.workspace_dir, 'criteria.csv')
with open(criteria_table_path, 'w') as criteria_table:
bom_char = "\uFEFF" # byte-order marker in 16-bit hex value
criteria_table.write(
textwrap.dedent(
f"""\
{bom_char}HABITAT NAME,eelgrass,,,hardbottom,,,CRITERIA TYPE
HABITAT RESILIENCE ATTRIBUTES,RATING,DQ,WEIGHT,RATING,DQ,WEIGHT,E/C
recruitment rate,2,2,2,2,2,2,C
connectivity rate,2,2,2,2,2,2,C
,,,,,,,
HABITAT STRESSOR OVERLAP PROPERTIES,,,,,,,
oil,RATING,DQ,WEIGHT,RATING,DQ,WEIGHT,E/C
frequency of disturbance,2,2,3,2,2,3,C
management effectiveness,2,2,1,2,2,1,E
,,,,,,,
fishing,RATING,DQ,WEIGHT,RATING,DQ,WEIGHT,E/C
frequency of disturbance,2,2,3,2,2,3,C
management effectiveness,2,2,1,2,2,1,E
"""
))
target_composite_csv_path = os.path.join(self.workspace_dir,
'composite.csv')
hra._parse_criteria_table(criteria_table_path,
target_composite_csv_path)

def test_criteria_table_file_not_found(self):
"""HRA: criteria table - spatial file not found."""
from natcap.invest import hra
Expand Down
Loading