Skip to content

Commit

Permalink
Use ruff as linter and formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
larsevj committed Aug 26, 2024
1 parent 880fdec commit 2207f77
Show file tree
Hide file tree
Showing 57 changed files with 468 additions and 404 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ jobs:
pip install -r test_requirements.txt
pytest
- name: Run black
uses: psf/black@stable
- name: Run ruff
run: |
ruff check
ruff format --check
39 changes: 39 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,42 @@ addopts = "-m 'not spe1'"
markers = [
"spe1: marks tests needing results from ert model of the spe1 test case",
]

[tool.ruff]
src = ["webviz_ert"]
line-length = 88

[tool.ruff.lint]
select = [
"W", # pycodestyle
"I", # isort
"B", # flake-8-bugbear
"SIM", # flake-8-simplify
"F", # pyflakes
"PL", # pylint
"NPY", # numpy specific rules
"C4", # flake8-comprehensions
]
preview = true
ignore = ["PLW2901", # redefined-loop-name
"PLR2004", # magic-value-comparison
"PLR0915", # too-many-statements
"PLR0912", # too-many-branches
"PLR0911", # too-many-return-statements
"PLC2701", # import-private-name
"PLR6201", # literal-membership
"PLR0914", # too-many-locals
"PLR6301", # no-self-use
"PLW1641", # eq-without-hash
"PLR0904", # too-many-public-methods
"PLR1702", # too-many-nested-blocks
"PLW3201", # bad-dunder-method-name
"PLC0414", # useless-import-alias
]

[tool.ruff.lint.extend-per-file-ignores]
"webviz_ert/models/__init__.py" = ["I"]
"webviz_ert/plugins/__init__.py" = ["I"]

[tool.ruff.lint.pylint]
max-args = 10
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages
from setuptools import find_packages, setup

with open("README.md", "r") as fh:
with open("README.md", "r", encoding="utf-8") as fh:
LONG_DESCRIPTION = fh.read()

setup(
Expand Down
4 changes: 2 additions & 2 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pytest
pytest-mock
pylint
black
ruff
scipy
dash[testing]>=2.5.1
selenium
urllib3<2 # RHEL7 does not support urllib >= 2.0
urllib3
flask>=2.2.5 # not directly required, pinned by Snyk to avoid a vulnerability
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import contextlib

import dash
import pytest
from requests import HTTPError
import dash
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options

from tests.data.snake_oil_data import ensembles_response

Expand Down Expand Up @@ -200,10 +202,8 @@ def select_parameter(dash_duo, plugin, parameter_name=None, wait_for_plot=True)


def wait_a_bit(dash_duo, time_seconds=0.1):
try:
with contextlib.suppress(TimeoutException):
dash_duo.wait_for_element(".foo-elderberries-baarrrrr", timeout=time_seconds)
except TimeoutException:
pass


def verify_key_in_dropdown(dash_duo, selector, key):
Expand Down
4 changes: 2 additions & 2 deletions tests/controllers/test_controller_functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from webviz_ert.models.ensemble_model import EnsembleModel
from webviz_ert.controllers.controller_functions import (
response_options,
_valid_response_option,
response_options,
)
from webviz_ert.models.ensemble_model import EnsembleModel


def test_response_options(mock_data):
Expand Down
15 changes: 5 additions & 10 deletions tests/controllers/test_response_correlation_controller.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import pandas as pd
import pytest

from webviz_ert.controllers.response_correlation_controller import (
_define_style_ensemble,
_layout_figure,
_format_index_value,
_format_index_text,
)

from webviz_ert.controllers.response_correlation_controller import (
_sort_dataframe,
_format_index_value,
_get_selected_indexes,
_layout_figure,
_sort_dataframe,
)
from webviz_ert.models import PlotModel


PLOT_STYLE = {
"mode": "markers",
"line": None,
Expand Down Expand Up @@ -141,7 +136,7 @@ def test_format_index_value(raw_value: str, expected_formatted_value: str):
x_axis=[pd.Timestamp("2020-01-02")],
y_axis=[1, 1, 1],
name="plot2",
**PLOT_STYLE
**PLOT_STYLE,
),
],
{"x": ["2020-01-01", "2020-01-03"]},
Expand All @@ -156,7 +151,7 @@ def test_format_index_value(raw_value: str, expected_formatted_value: str):
x_axis=[pd.Timestamp("2020-01-02")],
y_axis=[1, 1, 1],
name="plot2",
**PLOT_STYLE
**PLOT_STYLE,
),
],
{"x": ["2020-01-01", "2020-01-03"], "x2": [4, 6]},
Expand Down
2 changes: 1 addition & 1 deletion tests/data/snake_oil_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import io
import json

import pandas as pd


Expand Down
4 changes: 2 additions & 2 deletions tests/data/spe1_st/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
@pytest.fixture(scope="session", autouse=True)
def start_storage_server():
try:
from ert.services import StorageService as Storage
from ert.services import StorageService as Storage # noqa: PLC0415
except ImportError:
from ert.services import Storage
from ert.services import Storage # noqa: PLC0415

with Storage.start_server() as service:
service.wait_until_ready(timeout=30)
Expand Down
19 changes: 9 additions & 10 deletions tests/data/spe1_st/tests/test_webviz_ert.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import pytest
from webviz_ert.plugins import (
ParameterComparison,
ResponseComparison,
ResponseCorrelation,
ObservationAnalyzer,
)
from tests.conftest import (
select_first,
select_ensemble,
select_first,
select_parameter,
select_response,
setup_plugin,
verify_key_in_dropdown,
)
from webviz_ert.plugins import (
ObservationAnalyzer,
ParameterComparison,
ResponseComparison,
ResponseCorrelation,
)

parameter_keys = ["FIELD_PROPERTIES:POROSITY", "FIELD_PROPERTIES:X_MID_PERMEABILITY"]
response_keys = ["WGPT:PROD", "WWPT:PROD", "WOPT:PROD", "WWIT:INJ"]
Expand All @@ -39,7 +39,7 @@ def test_webviz_parameter_comparison(dash_duo):
"default",
)

ensemble_name = select_first(dash_duo, "#" + plugin.uuid("ensemble-multi-selector"))
_ensemble_name = select_first(dash_duo, "#" + plugin.uuid("ensemble-multi-selector"))
_verify_keys_in_menu(
dash_duo, plugin, parameter_keys, "parameter-selector-multi-param"
)
Expand All @@ -53,7 +53,6 @@ def test_webviz_response_correlation(dash_duo):
ensemble = "default"
response = "WOPT:PROD"
parameter = "FIELD_PROPERTIES:POROSITY::0"
index = "2016-01-01"

# Wait for the ensemble selector to be initialized
dash_duo.wait_for_contains_text(
Expand Down Expand Up @@ -96,7 +95,7 @@ def test_webviz_response_comparison(dash_duo):
"default",
)

ensemble_name = select_first(dash_duo, "#" + plugin.uuid("ensemble-multi-selector"))
_ensemble_name = select_first(dash_duo, "#" + plugin.uuid("ensemble-multi-selector"))
_verify_keys_in_menu(
dash_duo, plugin, parameter_keys, "parameter-selector-multi-param"
)
Expand Down
8 changes: 3 additions & 5 deletions tests/models/test_ensemble_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from webviz_ert.models import EnsembleModel
import dash
from webviz_ert.plugins import ParameterComparison
from webviz_ert.plugins import ObservationAnalyzer
from webviz_ert.models import load_ensemble
from webviz_ert.models import EnsembleModel, load_ensemble
from webviz_ert.plugins import ObservationAnalyzer, ParameterComparison


def test_ensemble_model(mock_data):
Expand All @@ -16,7 +14,7 @@ def test_ensemble_model_labeled_parameters(mock_data):
ens_model = EnsembleModel(ensemble_id=ens_id, project_id=None)
assert ens_model.name == "nr_42"
assert len(ens_model.parameters) == 5
for param_name, parameter in ens_model.parameters.items():
for param_name, _parameter in ens_model.parameters.items():
name, label = (
param_name.split("::", maxsplit=1)
if "::" in param_name
Expand Down
36 changes: 21 additions & 15 deletions tests/plots/test_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pandas as pd
import numpy as np
import pandas as pd
from webviz_ert import assets
from webviz_ert.controllers.multi_response_controller import (
_get_observation_plots,
_get_realizations_plots,
Expand All @@ -8,17 +9,14 @@
from webviz_ert.controllers.observation_response_controller import (
_get_univariate_misfits_boxplots,
)

from webviz_ert.data_loader import get_ensembles
from webviz_ert.models import EnsembleModel, PriorModel
from webviz_ert.models import (
BarChartPlotModel,
BoxPlotModel,
HistogramPlotModel,
MultiHistogramPlotModel,
BoxPlotModel,
ParallelCoordinatesPlotModel,
BarChartPlotModel,
PriorModel,
)
import webviz_ert.assets as assets


def test_observation_plot_representation():
Expand Down Expand Up @@ -55,7 +53,8 @@ def test_observation_plot_representation():


def test_realizations_plot_representation():
data = np.random.rand(200).reshape(-1, 20)
rng = np.random.default_rng()
data = rng.random(size=200).reshape(-1, 20)
realization_df = pd.DataFrame(data=data, index=range(10), columns=range(20))
x_axis = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
plots = _get_realizations_plots(
Expand All @@ -68,7 +67,8 @@ def test_realizations_plot_representation():


def test_realizations_statistics_plot_representation():
data = np.random.rand(200).reshape(-1, 20)
rng = np.random.default_rng()
data = rng.random(size=200).reshape(-1, 20)
realization_df = pd.DataFrame(data=data, index=range(10), columns=range(20))
x_axis = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
plots = _get_realizations_statistics_plots(
Expand All @@ -86,7 +86,8 @@ def test_realizations_statistics_plot_representation():


def test_histogram_plot_representation():
data = np.random.rand(20).reshape(-1, 20)
rng = np.random.default_rng()
data = rng.random(size=20).reshape(-1, 20)
data_df = pd.DataFrame(data=data, index=range(1), columns=range(20))
data_df.index.name = "key_name"

Expand All @@ -109,7 +110,8 @@ def test_multi_histogram_plot_representation():
zip(ensemble_names, colors[: len(ensemble_names)])
):
key = f"{ensemble_date}, {ensemble_name}"
data = np.random.rand(20).reshape(-1, 20)
rng = np.random.default_rng()
data = rng.random(size=20).reshape(-1, 20)
data_df = pd.DataFrame(data=data, index=range(1), columns=range(20))
data_df.index.name = "KEY_NAME"
data_dict[key] = data_df
Expand Down Expand Up @@ -156,7 +158,8 @@ def test_parallel_coordinates_representation():
zip(ensemble_names, colors[: len(ensemble_names)])
):
key = f"{idx}, {ensemble_name}"
data = np.random.rand(50).reshape(-1, 5)
rng = np.random.default_rng()
data = rng.random(size=50).reshape(-1, 5)
data_df = pd.DataFrame(data=data, columns=[f"PARAM_{i}" for i in range(5)])
data_df["ensemble_id"] = idx
data_dict[key] = data_df
Expand All @@ -175,7 +178,8 @@ def test_parallel_coordinates_representation():


def test_univariate_misfits_boxplot_representation():
data = np.random.rand(200).reshape(-1, 20)
rng = np.random.default_rng()
data = rng.random(size=200).reshape(-1, 20)
missfits_df = pd.DataFrame(data=data, index=range(10), columns=range(20))
ensemble_name = "test-ensemble"
plots = _get_univariate_misfits_boxplots(
Expand All @@ -194,7 +198,8 @@ def test_univariate_misfits_boxplot_representation():


def test_boxplot_representation():
data = np.random.rand(10)
rng = np.random.default_rng()
data = rng.random(size=10)
data_df = pd.DataFrame(data=data, index=range(10))

plot = BoxPlotModel(
Expand All @@ -219,7 +224,8 @@ def test_barchart_representation():
zip(ensemble_names, colors[: len(ensemble_names)])
):
key = f"{idx}, {ensemble_name}"
data = np.random.rand(param_num)
rng = np.random.default_rng()
data = rng.random(size=param_num)
data_df = pd.DataFrame(
data=data, index=[f"PARAM_{i}" for i in range(param_num)]
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data_loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from webviz_ert.data_loader import get_ensembles, refresh_data, DataLoader
from webviz_ert.data_loader import get_ensembles, refresh_data


def test_get_ensembles(mock_data):
Expand Down
6 changes: 2 additions & 4 deletions tests/views/test_ensemble_selector.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import dash
import pytest
from tests.conftest import get_options, select_ensemble, select_first, setup_plugin
from tests.data.snake_oil_data import all_ensemble_names
from webviz_ert.assets import get_color
from webviz_ert.plugins import ParameterComparison
from tests.conftest import select_first, get_options
from tests.data.snake_oil_data import all_ensemble_names
from tests.conftest import setup_plugin, select_ensemble


@pytest.mark.browser_test
Expand Down
6 changes: 2 additions & 4 deletions tests/views/test_general_stuff.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import pytest
import dash
from tests.conftest import setup_plugin, select_ensemble

from tests.conftest import select_ensemble, setup_plugin
from webviz_ert.plugins import (
ResponseComparison,
ObservationAnalyzer,
ParameterComparison,
ResponseComparison,
ResponseCorrelation,
)

Expand Down
5 changes: 2 additions & 3 deletions tests/views/test_observation_view.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import pytest
from webviz_ert.plugins import ObservationAnalyzer
from tests.conftest import (
get_options,
select_ensemble,
verify_key_in_dropdown,
setup_plugin,
verify_key_in_dropdown,
)
from webviz_ert.plugins import ObservationAnalyzer


@pytest.mark.browser_test
Expand Down
Loading

0 comments on commit 2207f77

Please sign in to comment.