Skip to content
Draft
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: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ dev = [
'types-openpyxl',
'types-seaborn',
'types-setuptools',
'xlwt'
'xlsxwriter>=3.2.3',
'xlwt',
]

[project.urls]
Expand Down
62 changes: 62 additions & 0 deletions tests/fmudesign/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import csv
from pathlib import Path

import pytest
from xlsxwriter import Workbook # type: ignore[import-untyped]
from xlsxwriter.worksheet import Worksheet # type: ignore[import-untyped]

from tests.fmudesign.workbook_specs import WORKBOOK_SPECS, CellValue, WorkbookSpec

SOURCE_DATA = Path(__file__).parent / "data"


def _write_cell(worksheet: Worksheet, row: int, column: int, value: CellValue) -> None:
if isinstance(value, str):
worksheet.write_string(row, column, value)
elif value is not None:
worksheet.write_number(row, column, value)


def _write_workbook(path: Path, spec: WorkbookSpec) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with Workbook(path) as workbook:
for sheet_name, rows in spec.items():
worksheet = workbook.add_worksheet(sheet_name)
for row_number, values in rows.items():
for column, value in enumerate(values):
_write_cell(worksheet, row_number - 1, column, value)


def _parse_csv_cell(value: str) -> CellValue:
if not value:
return None
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value


def _write_design_summary_workbook(path: Path) -> None:
with (SOURCE_DATA / "distributions/design.csv").open(
encoding="utf-8", newline=""
) as stream:
rows = {
row_number: tuple(_parse_csv_cell(value) for value in values)
for row_number, values in enumerate(csv.reader(stream), start=1)
}
# The legacy Excel fixture uses the full parameter name; the CSV abbreviates it.
rows[1] = (*rows[1][:-1], "RELP_GO_ILETOFTE")
_write_workbook(path, {"DesignSheet01": rows})


@pytest.fixture(scope="session")
def fmudesign_test_data(tmp_path_factory: pytest.TempPathFactory) -> Path:
test_data = tmp_path_factory.mktemp("fmudesign_test_data")
config_dir = test_data / "config"
for filename, spec in WORKBOOK_SPECS.items():
_write_workbook(config_dir / filename, spec)
_write_design_summary_workbook(test_data / "distributions/design.xlsx")
return test_data
11 changes: 7 additions & 4 deletions tests/fmudesign/data/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Testdata for tornadoplots from one by one sensitivities (design matrix)
distributions: contains design matrix on fmu standard format in excel and .csv format
results: contains in place volumes exported from RMS in fmu standard csv format
config: contains yaml config files for add_webviz_tornado_onebyone.py
Text test data for FMU-design.

- `distributions` contains a design matrix in FMU-standard CSV format.
- `results` contains in-place volumes exported from RMS in FMU-standard CSV format.
- `config` contains external seed data used by generated workbook fixtures.

Excel workbooks are generated from `tests/fmudesign/workbook_specs.py` during tests.
Binary file removed tests/fmudesign/data/config/correlations.xlsx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed tests/fmudesign/data/config/doe1.xlsx
Binary file not shown.
Binary file not shown.
Binary file removed tests/fmudesign/data/config/seeds.xlsx
Binary file not shown.
Binary file removed tests/fmudesign/data/distributions/design.xlsx
Binary file not shown.
24 changes: 11 additions & 13 deletions tests/fmudesign/test_create_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from semeio.fmudesign.create_design import MonteCarloSensitivity, _derive_rng
from semeio.fmudesign.quality_report import print_corrmat

TESTDATA = Path(__file__).parent / "data"


@pytest.mark.integration_test
@pytest.mark.parametrize("correlations", [True, False])
Expand Down Expand Up @@ -209,10 +207,10 @@ def gl(paramname, distname, p1, p2, p3="", p4=""):
assert np.sqrt(np.mean((obs_corr - corr_values) ** 2)) < 0.02


def test_generate_onebyone(tmpdir):
def test_generate_onebyone(tmpdir, fmudesign_test_data):
"""Test generation of onebyone design"""

inputfile = TESTDATA / "config/design_input_example1.xlsx"
inputfile = fmudesign_test_data / "config/design_input_example1.xlsx"

input_dict = excel_to_dict(inputfile)

Expand Down Expand Up @@ -379,14 +377,14 @@ def test_generate_onebyone(tmpdir):
pytest.fail("Timestamp in Metadata sheet is not in expected format")


def test_generate_full_mc_snapshot(snapshot):
def test_generate_full_mc_snapshot(snapshot, fmudesign_test_data):
"""Test that full monte carlo design matrix generation remains consistent.

This is a snapshot test that verifies the entire output of the design matrix
generation process, including both the design values and default values.
"""
# Setup
inputfile = TESTDATA / "config/design_input_mc_with_correls.xlsx"
inputfile = fmudesign_test_data / "config/design_input_mc_with_correls.xlsx"
input_dict = excel_to_dict(inputfile)
design = DesignMatrix()

Expand Down Expand Up @@ -415,11 +413,11 @@ def test_generate_full_mc_snapshot(snapshot):
snapshot.assert_match(snapshot_str, "design_output_mc_with_correls.json")


def test_generate_full_mc_snapshot_independent(snapshot):
def test_generate_full_mc_snapshot_independent(snapshot, fmudesign_test_data):
"""Same config as test_generate_full_mc_snapshot, but with the opt-in
'independent' seed strategy.
"""
inputfile = TESTDATA / "config/design_input_mc_with_correls.xlsx"
inputfile = fmudesign_test_data / "config/design_input_mc_with_correls.xlsx"
input_dict = excel_to_dict(inputfile)
input_dict["seed_strategy"] = "independent"
design = DesignMatrix()
Expand All @@ -440,9 +438,9 @@ def test_generate_full_mc_snapshot_independent(snapshot):
snapshot.assert_match(snapshot_str, "design_output_mc_with_correls.json")


def test_generate_full_mc(tmpdir):
def test_generate_full_mc(tmpdir, fmudesign_test_data):
"""Test generation of full monte carlo"""
inputfile = TESTDATA / "config/design_input_mc_with_correls.xlsx"
inputfile = fmudesign_test_data / "config/design_input_mc_with_correls.xlsx"
input_dict = excel_to_dict(inputfile)

design = DesignMatrix()
Expand Down Expand Up @@ -517,10 +515,10 @@ def test_generate_full_mc(tmpdir):


@pytest.mark.integration_test
def test_generate_background(tmpdir):
inputfile = TESTDATA / "config/design_input_background.xlsx"
def test_generate_background(tmpdir, fmudesign_test_data):
inputfile = fmudesign_test_data / "config/design_input_background.xlsx"
input_dict = excel_to_dict(inputfile)
source_file = TESTDATA / "config/doe1.xlsx"
source_file = fmudesign_test_data / "config/doe1.xlsx"
dest_file = tmpdir.join("doe1.xlsx")
shutil.copy2(source_file, dest_file)

Expand Down
6 changes: 2 additions & 4 deletions tests/fmudesign/test_designmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

from semeio.fmudesign import DesignMatrix

TESTDATA = Path(__file__).parent / "data"


def matches(pattern: str, text: str) -> bool:
"""Match text against a pattern where <ANY> acts as a wildcard.
Expand Down Expand Up @@ -74,13 +72,13 @@ def test_designmatrix():


@pytest.mark.integration_test
def test_endpoint(tmpdir, monkeypatch):
def test_endpoint(tmpdir, monkeypatch, fmudesign_test_data):
"""Test the installed endpoint

Will write generated design matrices to the pytest tmpdir directory,
usually /tmp/pytest-of-<username>/
"""
designfile = TESTDATA / "config/design_input_onebyone.xlsx"
designfile = fmudesign_test_data / "config/design_input_onebyone.xlsx"

# The xlsx file contains a relative path, relative to the input design sheet:
dependency = (
Expand Down
8 changes: 4 additions & 4 deletions tests/fmudesign/test_designsummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from semeio.fmudesign import summarize_design

TESTDATA = Path(__file__).parent / "data"
SOURCE_DATA = Path(__file__).parent / "data"


def test_designsummary():
def test_designsummary(fmudesign_test_data):
"""Test import and summary of design matrix"""

snorrebergdesign = summarize_design(
TESTDATA / "distributions/design.xlsx", "DesignSheet01"
fmudesign_test_data / "distributions/design.xlsx", "DesignSheet01"
)
# checking dimensions and some values in summary of design matrix
assert snorrebergdesign.shape == (7, 9)
Expand Down Expand Up @@ -49,5 +49,5 @@ def test_designsummary():
assert snorrebergdesign["endreal1"].sum() == 333

# Test same also when design matrix is in .csv format
designcsv = summarize_design(TESTDATA / "distributions/design.csv")
designcsv = summarize_design(SOURCE_DATA / "distributions/design.csv")
assert snorrebergdesign.equals(designcsv)
18 changes: 11 additions & 7 deletions tests/fmudesign/test_use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

from semeio.fmudesign import DesignMatrix, excel_to_dict
from semeio.fmudesign.fmudesignrunner import EXAMPLES
from tests.fmudesign.workbook_specs import DESIGN_INPUT_FILES, DESIGN_INPUT_IDS

EXAMPLE_FILES = [ex.filename for ex in EXAMPLES]

TESTDATA = Path(__file__).parent / "data"
TEST_FILES = list((TESTDATA / "config").glob("design_input*.xlsx"))


def test_prediction_rejection_sampled_ensemble(tmpdir, monkeypatch):
"""Test making a design matrix for prediction realizations based on an
Expand Down Expand Up @@ -163,11 +161,14 @@ def test_constant_distribution(tmpdir, monkeypatch, gen_input_sheet):


@pytest.mark.integration_test
@pytest.mark.parametrize("designfile", TEST_FILES, ids=[p.stem for p in TEST_FILES])
@pytest.mark.parametrize("designfile", DESIGN_INPUT_FILES, ids=DESIGN_INPUT_IDS)
@pytest.mark.parametrize("verbosity", [0, 1, 2])
def test_all_input_files(tmpdir, monkeypatch, designfile, verbosity):
def test_all_input_files(
tmpdir, monkeypatch, fmudesign_test_data, designfile, verbosity
):
"""Smoketest all files."""

designfile = fmudesign_test_data / "config" / designfile
monkeypatch.chdir(tmpdir)

# Copy all example files over, to guarantee existence of dependency files
Expand Down Expand Up @@ -203,11 +204,14 @@ def test_all_example_files_cmd_init(tmpdir, monkeypatch, designfile, verbosity):


@pytest.mark.integration_test
@pytest.mark.parametrize("designfile", TEST_FILES, ids=[p.stem for p in TEST_FILES])
def test_all_input_files_relative_paths(tmpdir, monkeypatch, designfile):
@pytest.mark.parametrize("designfile", DESIGN_INPUT_FILES, ids=DESIGN_INPUT_IDS)
def test_all_input_files_relative_paths(
tmpdir, monkeypatch, fmudesign_test_data, designfile
):
"""Smoketest all files, but invoke them from a directory above.
This tests that relative paths in the Excel files work correctly."""

designfile = fmudesign_test_data / "config" / designfile
monkeypatch.chdir(tmpdir)
copy_to = os.path.join(".", "path", "going", "down")

Expand Down
Loading
Loading