Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,6 @@ __main__.py

# Setup
_echoregions_version.py

.idea/
.DS_Store
2 changes: 1 addition & 1 deletion docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ To run in development mode, fork and clone the repository at `Echoregions <https
.. code-block:: console

$ mamba create -c conda-forge -n er-dev --yes python=3.10 --file requirements.txt --file requirements-dev.txt
$ pip install -e
$ pip install -e .
4 changes: 4 additions & 0 deletions echoregions/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pathlib import Path

HERE = Path(__file__).parent.absolute()
TEST_DATA_FOLDER = HERE / "test_data"
17 changes: 17 additions & 0 deletions echoregions/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""``pytest`` configuration."""

import pytest

from echoregions.testing import TEST_DATA_FOLDER


@pytest.fixture(scope="session")
def dump_output_dir():
return TEST_DATA_FOLDER / "dump"


@pytest.fixture(scope="session")
def test_path():
return {
"ROOT": TEST_DATA_FOLDER,
}
47 changes: 29 additions & 18 deletions echoregions/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@
import echoregions as er
from echoregions.lines.lines import Lines

DATA_DIR = Path("./echoregions/test_data/")
EVL_PATH = DATA_DIR / "transect.evl"
ZARR_PATH = DATA_DIR / "transect.zarr"

@pytest.fixture(scope="module")
def data_path(test_path):
return test_path["ROOT"]


@pytest.fixture(scope="module")
def evl_path(test_path):
return test_path["ROOT"] / "transect.evl"


@pytest.fixture(scope="module")
def zarr_path(test_path):
return test_path["ROOT"] / "transect.zarr"


@pytest.fixture(scope="function")
def lines_fixture() -> Lines:
def lines_fixture(evl_path) -> Lines:
"""
Lines object fixture.

Expand All @@ -26,12 +37,12 @@ def lines_fixture() -> Lines:
Object containing data of test EVL file.
"""

lines = er.read_evl(EVL_PATH)
lines = er.read_evl(evl_path)
return lines


@pytest.fixture(scope="function")
def da_Sv_fixture() -> DataArray:
def da_Sv_fixture(zarr_path) -> DataArray:
"""
Sonar ZARR data fixture.

Expand All @@ -41,12 +52,12 @@ def da_Sv_fixture() -> DataArray:
DataArray containing Sv data of test zarr file.
"""

da_Sv = xr.open_zarr(ZARR_PATH).Sv
da_Sv = xr.open_zarr(zarr_path).Sv
return da_Sv


@pytest.mark.lines
def test_lines_csv(lines_fixture: Lines) -> None:
def test_lines_csv(lines_fixture: Lines, data_path) -> None:
"""
Ensures that read_lines_csv provides the same Lines object
as read_evl.
Expand All @@ -62,7 +73,7 @@ def test_lines_csv(lines_fixture: Lines) -> None:
lines_1_df = lines_1.data

# Send to CSV
csv_file_path = DATA_DIR / "lines_to_csv_file.csv"
csv_file_path = data_path / "lines_to_csv_file.csv"
lines_1.to_csv(csv_file_path)

# Read Lines CSV and extract DataFrame
Expand All @@ -80,17 +91,17 @@ def test_lines_csv(lines_fixture: Lines) -> None:


@pytest.mark.lines
def test_to_evl() -> None:
def test_to_evl(data_path) -> None:
"""
Tests that when we save a `Lines` object to `.evl` and read
back that `.evl` file, we end up with the same inner dataframe.
"""
# Get Lines object and DataFrame
lines_1 = er.read_evl(DATA_DIR / "transect.evl")
lines_1 = er.read_evl(data_path / "transect.evl")
lines_1_df = lines_1.data

# Send to `.evl` file
evl_file_path = DATA_DIR / "lines_to_evl_file.evl"
evl_file_path = data_path / "lines_to_evl_file.evl"
lines_1.to_evl(evl_file_path)

# Read back `.lines` file and extract DataFrame
Expand Down Expand Up @@ -132,7 +143,7 @@ def test_lines_parsing(lines_fixture: Lines) -> None:


@pytest.mark.lines
def test_evl_to_file(lines_fixture: Lines) -> None:
def test_evl_to_file(lines_fixture: Lines, data_path) -> None:
"""
Test EVL to csv and to json; Creates and removes EVL .csv and .json objects.

Expand All @@ -143,8 +154,8 @@ def test_evl_to_file(lines_fixture: Lines) -> None:
"""

# Get output paths
output_csv = DATA_DIR / "output_CSV/"
output_json = DATA_DIR / "output_JSON/"
output_csv = data_path / "output_CSV/"
output_json = data_path / "output_JSON/"

# Create CSV and JSON files
lines_fixture.to_csv(output_csv)
Expand Down Expand Up @@ -220,17 +231,17 @@ def test_plot_type_error(lines_fixture: Lines) -> None:


@pytest.mark.lines
def test_replace_nan_depth() -> None:
def test_replace_nan_depth(evl_path) -> None:
"""
Test replacing NaN values in line for both inplace=True and inplace=False.
"""

lines_1 = er.read_evl(EVL_PATH, nan_depth_value=20)
lines_1 = er.read_evl(evl_path, nan_depth_value=20)
lines_1.data.loc[0, "depth"] = -10000.99 # Replace a value with the one used for nans
lines_1.replace_nan_depth(inplace=True)
assert lines_1.data.loc[0, "depth"] == 20

lines_2 = er.read_evl(EVL_PATH, nan_depth_value=20)
lines_2 = er.read_evl(evl_path, nan_depth_value=20)
lines_2.data.loc[0, "depth"] = -10000.99 # Replace a value with the one used for nans
lines2 = lines_2.replace_nan_depth(inplace=False)
assert lines2.loc[0, "depth"] == 20
Expand Down
69 changes: 42 additions & 27 deletions echoregions/tests/test_regions2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@
import echoregions as er
from echoregions.regions2d.regions2d import Regions2D

DATA_DIR = Path("./echoregions/test_data/")
EVR_PATH = DATA_DIR / "transect_multi_mask.evr"
ZARR_PATH = DATA_DIR / "transect.zarr"
# DATA_DIR = Path("./echoregions/test_data/")
# EVR_PATH = DATA_DIR / "transect_multi_mask.evr"
# ZARR_PATH = DATA_DIR / "transect.zarr"


@pytest.fixture(scope="module")
def data_path(test_path):
return test_path["ROOT"]


@pytest.fixture(scope="module")
def evr_path(test_path):
return test_path["ROOT"] / "transect_multi_mask.evr"


@pytest.fixture(scope="module")
def zarr_path(test_path):
return test_path["ROOT"] / "transect.zarr"


@pytest.fixture(scope="function")
def regions2d_fixture() -> Regions2D:
def regions2d_fixture(evr_path) -> Regions2D:
"""
Regions2D object fixture.

Expand All @@ -26,12 +41,12 @@ def regions2d_fixture() -> Regions2D:
Object containing data of test EVR file.
"""

r2d = er.read_evr(EVR_PATH)
r2d = er.read_evr(evr_path)
return r2d


@pytest.fixture(scope="function")
def da_Sv_fixture() -> DataArray:
def da_Sv_fixture(zarr_path) -> DataArray:
"""
Sonar ZARR data fixture.

Expand All @@ -41,12 +56,12 @@ def da_Sv_fixture() -> DataArray:
DataArray containing Sv data of test zarr file.
"""

da_Sv = xr.open_zarr(ZARR_PATH).Sv
da_Sv = xr.open_zarr(zarr_path).Sv
return da_Sv


@pytest.mark.regions2d
def test_read_regions_csv(regions2d_fixture: Regions2D) -> None:
def test_read_regions_csv(regions2d_fixture: Regions2D, data_path) -> None:
"""
Ensures that read_region_csv provides the same Regions2D object
as read_evr.
Expand All @@ -62,7 +77,7 @@ def test_read_regions_csv(regions2d_fixture: Regions2D) -> None:
r2d_1_df = r2d_1.data

# Send to CSV
csv_file_path = DATA_DIR / "r2d_to_csv_file.csv"
csv_file_path = data_path / "r2d_to_csv_file.csv"
r2d_1.to_csv(csv_file_path)

# Read Regions CSV and extract DataFrame
Expand Down Expand Up @@ -95,17 +110,17 @@ def test_read_regions_csv(regions2d_fixture: Regions2D) -> None:


@pytest.mark.regions2d
def test_to_evr() -> None:
def test_to_evr(data_path) -> None:
"""
Tests that when we save a `Regions2D` object to `.evr` and read
back that `.evr` file, we end up with the same inner dataframe.
"""
# Get Regions2D object and DataFrame
r2d_1 = er.read_evr(DATA_DIR / "transect.evr")
r2d_1 = er.read_evr(data_path / "transect.evr")
r2d_1_df = r2d_1.data

# Send to `.evr` file
evr_file_path = DATA_DIR / "r2d_to_evr_file.evr"
evr_file_path = data_path / "r2d_to_evr_file.evr"
r2d_1.to_evr(evr_file_path)

# Read back `.evr` file and extract DataFrame
Expand All @@ -120,27 +135,27 @@ def test_to_evr() -> None:


@pytest.mark.regions2d
def test_empty_regions2d_parsing() -> None:
def test_empty_regions2d_parsing(data_path) -> None:
"""
Tests empty EVR parsing.
"""

# Read evr into regions2d
r2d = er.read_evr(DATA_DIR / "transect_empty.evr")
r2d = er.read_evr(data_path / "transect_empty.evr")

# Check shapes
assert r2d.data.shape == (0, 22)
assert r2d.select_region([11]).shape == (0, 22)


@pytest.mark.regions2d
def test_missing_bbox_regions2d_parsing() -> None:
def test_missing_bbox_regions2d_parsing(data_path) -> None:
"""
Tests missing bbox EVR parsing.
"""

# Read evr into regions2d
r2d = er.read_evr(DATA_DIR / "transect_missing_bbox.evr")
r2d = er.read_evr(data_path / "transect_missing_bbox.evr")

# Test shape
assert r2d.data.shape == (2, 22)
Expand Down Expand Up @@ -235,7 +250,7 @@ def test_regions2d_parsing(regions2d_fixture: Regions2D) -> None:


@pytest.mark.regions2d
def test_evr_to_file(regions2d_fixture: Regions2D) -> None:
def test_evr_to_file(regions2d_fixture: Regions2D, data_path) -> None:
"""
Test converting an Echoview 2D Regions files (.EVR).

Expand All @@ -246,7 +261,7 @@ def test_evr_to_file(regions2d_fixture: Regions2D) -> None:
"""

# Get output path
output_csv = DATA_DIR / "output_CSV/"
output_csv = data_path / "output_CSV/"

# Create CSV
regions2d_fixture.to_csv(output_csv)
Expand Down Expand Up @@ -936,7 +951,7 @@ def test_within_transect_no_regions(regions2d_fixture: Regions2D, da_Sv_fixture:


@pytest.mark.regions2d
def test_within_transect_bad_dict(da_Sv_fixture: DataArray) -> None:
def test_within_transect_bad_dict(da_Sv_fixture: DataArray, data_path) -> None:
"""
Tests functionality for transect_mask with invalid dictionary values.

Expand All @@ -947,7 +962,7 @@ def test_within_transect_bad_dict(da_Sv_fixture: DataArray) -> None:
"""

# Get Regions2D Object
evr_path = DATA_DIR / "transect.evr"
evr_path = data_path / "transect.evr"
r2d = er.read_evr(evr_path)

# Create dictionary with duplicates
Expand All @@ -971,7 +986,7 @@ def test_within_transect_bad_dict(da_Sv_fixture: DataArray) -> None:


@pytest.mark.regions2d
def test_within_transect_invalid_next(da_Sv_fixture: DataArray) -> None:
def test_within_transect_invalid_next(da_Sv_fixture: DataArray, data_path) -> None:
"""
Tests functionality for evr file with invalid next transect type values.

Expand All @@ -986,7 +1001,7 @@ def test_within_transect_invalid_next(da_Sv_fixture: DataArray) -> None:

# Should raise Exception if ST is followed by ST
with pytest.raises(Exception):
evr_path = DATA_DIR / "x1_ST_ST.evr"
evr_path = data_path / "x1_ST_ST.evr"
r2d = er.read_evr(evr_path)
_ = r2d.transect_mask(
da_Sv=da_Sv_fixture,
Expand All @@ -996,7 +1011,7 @@ def test_within_transect_invalid_next(da_Sv_fixture: DataArray) -> None:

# Should raise Exception if RT is followed by RT
with pytest.raises(Exception):
evr_path = DATA_DIR / "transect_RT_RT.evr"
evr_path = data_path / "transect_RT_RT.evr"
r2d = er.read_evr(evr_path)
_ = r2d.transect_mask(
da_Sv=da_Sv_fixture,
Expand All @@ -1006,7 +1021,7 @@ def test_within_transect_invalid_next(da_Sv_fixture: DataArray) -> None:

# Should raise value Exception if BT is followed by ET
with pytest.raises(Exception):
evr_path = DATA_DIR / "transect_BT_ET.evr"
evr_path = data_path / "transect_BT_ET.evr"
r2d = er.read_evr(evr_path)
_ = r2d.transect_mask(
da_Sv=da_Sv_fixture,
Expand All @@ -1016,7 +1031,7 @@ def test_within_transect_invalid_next(da_Sv_fixture: DataArray) -> None:

# Should raises Exception if ET is followed by RT
with pytest.raises(Exception):
evr_path = DATA_DIR / "transect_ET_RT.evr"
evr_path = data_path / "transect_ET_RT.evr"
r2d = er.read_evr(evr_path)
_ = r2d.transect_mask(
da_Sv=da_Sv_fixture,
Expand All @@ -1026,7 +1041,7 @@ def test_within_transect_invalid_next(da_Sv_fixture: DataArray) -> None:


@pytest.mark.regions2d
def test_within_transect_small_bbox_distance_threshold(da_Sv_fixture: DataArray) -> None:
def test_within_transect_small_bbox_distance_threshold(da_Sv_fixture: DataArray, data_path) -> None:
"""
Tests functionality for transect_mask with small bbox distance threshold.

Expand All @@ -1037,7 +1052,7 @@ def test_within_transect_small_bbox_distance_threshold(da_Sv_fixture: DataArray)
"""

# Get Regions2D Object
evr_path = DATA_DIR / "transect.evr"
evr_path = data_path / "transect.evr"
r2d = er.read_evr(evr_path)

with pytest.raises(Exception):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build-backend = 'setuptools.build_meta'
fallback_version = "unknown"
local_scheme = "no-local-version"
write_to = "_echoregions_version.py"
write_to_template = 'version = "{version}"'
write_to_template = 'version = "{version}" # noqa'

[tool.black]
line-length = 100
Expand Down
Loading