Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-qb committed Nov 25, 2024
1 parent db017a2 commit e6208fa
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 83 deletions.
2 changes: 1 addition & 1 deletion vizro-core/examples/scratch_dev/data.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Choose between 0-50
setosa: 5
versicolor: 10
#versicolor: 10
virginica: 15

# Choose between: 4.3 to 7.4
Expand Down
16 changes: 9 additions & 7 deletions vizro-core/src/vizro/models/_controls/filter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

from contextlib import suppress
from typing import Any, Literal, Union

import numpy as np
import pandas as pd
from dash import dcc
from contextlib import suppress
from pandas.api.types import is_datetime64_any_dtype, is_numeric_dtype

from vizro.managers._data_manager import DataSourceName
Expand Down Expand Up @@ -275,13 +274,16 @@ def _get_min_max(targeted_data: pd.DataFrame, current_value=None) -> tuple[float
_max = pd.to_datetime(_max)
current_value = pd.to_datetime(current_value)
# Convert DatetimeIndex to list of Timestamp objects so that we can use min and max functions below.
with suppress(AttributeError): current_value = current_value.tolist()
with suppress(AttributeError):
current_value = current_value.tolist()

# Use item() to convert to convert scalar from numpy to Python type. This isn't needed during pre_build because
# pydantic will coerce the type, but it is necessary in __call__ where we don't update model field values
# and instead just pass straight to the Dash component.
with suppress(AttributeError): _min = _min.item()
with suppress(AttributeError): _max = _max.item()
with suppress(AttributeError):
_min = _min.item()
with suppress(AttributeError):
_max = _max.item()

if current_value is not None:
current_value = current_value if isinstance(current_value, list) else [current_value]
Expand All @@ -305,7 +307,7 @@ def _get_options(targeted_data: pd.DataFrame, current_value=None) -> list[Any]:
if isinstance(current_value, list) and ALL_OPTION in current_value:
current_value.remove(ALL_OPTION)

targeted_data_series = targeted_data.stack().dropna()
targeted_data_series = targeted_data.stack().dropna() # noqa: PD013
current_value_series = pd.Series(current_value).astype(targeted_data_series.dtypes)

return sorted(list(pd.concat([targeted_data_series, current_value_series]).unique()))
return sorted(pd.concat([targeted_data_series, current_value_series]).unique())
1 change: 0 additions & 1 deletion vizro-core/tests/unit/vizro/actions/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pandas as pd
import pytest

import vizro.models as vm
Expand Down
164 changes: 90 additions & 74 deletions vizro-core/tests/unit/vizro/models/_controls/test_filter.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from datetime import date, datetime
from typing import Literal

from dash import dcc
import pandas as pd
import pytest
from asserts import assert_component_equal
from dash import dcc

import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.managers import data_manager, model_manager
from vizro.models import DatePicker
from vizro.models._action._actions_chain import ActionsChain
from vizro.models._controls.filter import Filter, _filter_between, _filter_isin
from vizro.models.types import CapturedCallable
Expand Down Expand Up @@ -231,18 +230,22 @@ class TestFilterStaticMethods:
([["A", "B", "A"]], ["A", "B"]),
([[1, 2, 1]], [1, 2]),
([[1.1, 2.2, 1.1]], [1.1, 2.2]),
([[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]],
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
]),
(
[
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]
],
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
],
),
([["A", "B"], ["B", "C"]], ["A", "B", "C"]),
([["A", "B"], ["C"]], ["A", "B", "C"]),
([["A" ], []], ["A" ]),
([["A"], []], ["A"]),
],
)
def test_get_options(self, data_columns, expected):
Expand All @@ -264,30 +267,38 @@ def test_get_options(self, data_columns, expected):
([[1, 2]], [3, 4], [1, 2, 3, 4]),
([[1.1, 2.2]], 3.3, [1.1, 2.2, 3.3]),
([[1.1, 2.2]], [3.3, 4.4], [1.1, 2.2, 3.3, 4.4]),
([[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
]],
datetime(2024, 1, 3),
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 3),
]),
([[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
]],
[
(
[
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
]
],
datetime(2024, 1, 3),
datetime(2024, 1, 4),
],
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 3),
datetime(2024, 1, 4),
]),
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 3),
],
),
(
[
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
]
],
[
datetime(2024, 1, 3),
datetime(2024, 1, 4),
],
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 3),
datetime(2024, 1, 4),
],
),
],
)
def test_get_options_with_current_value(self, data_columns, current_value, expected):
Expand All @@ -300,15 +311,19 @@ def test_get_options_with_current_value(self, data_columns, current_value, expec
[
([[1, 2, 1]], (1, 2)),
([[1.1, 2.2, 1.1]], (1.1, 2.2)),
([[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]],
(
datetime(2024, 1, 1),
datetime(2024, 1, 2),
)),
(
[
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]
],
(
datetime(2024, 1, 1),
datetime(2024, 1, 2),
),
),
([[1, 2], [2, 3]], (1, 3)),
([[1, 2], [3]], (1, 3)),
([[1, 2], []], (1, 2)),
Expand All @@ -326,29 +341,36 @@ def test_get_min_max(self, data_columns, expected):
([[1, 2]], [3, 4], (1, 4)),
([[1.1, 2.2]], 3.3, (1.1, 3.3)),
([[1.1, 2.2]], [3.3, 4.4], (1.1, 4.4)),
([[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]],
datetime(2024, 1, 3),
(
datetime(2024, 1, 1),
datetime(2024, 1, 3),
)),
([[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]],
[
(
[
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]
],
datetime(2024, 1, 3),
datetime(2024, 1, 4),
],
(
datetime(2024, 1, 1),
datetime(2024, 1, 4),
)
(
datetime(2024, 1, 1),
datetime(2024, 1, 3),
),
),
(
[
[
datetime(2024, 1, 1),
datetime(2024, 1, 2),
datetime(2024, 1, 1),
]
],
[
datetime(2024, 1, 3),
datetime(2024, 1, 4),
],
(
datetime(2024, 1, 1),
datetime(2024, 1, 4),
),
),
([[1, 2], [2, 3]], 4, (1, 4)),
([[1, 2], [2, 3]], [4, 5], (1, 5)),
Expand Down Expand Up @@ -552,10 +574,7 @@ def test_filter_is_not_dynamic(self):
],
)
def test_filter_is_dynamic_with_dynamic_selectors(
self,
test_column,
test_selector,
gapminder_dynamic_first_n_last_n_function
self, test_column, test_selector, gapminder_dynamic_first_n_last_n_function
):
data_manager["gapminder_dynamic_first_n_last_n"] = gapminder_dynamic_first_n_last_n_function
filter = vm.Filter(column=test_column, selector=test_selector)
Expand Down Expand Up @@ -590,10 +609,7 @@ def test_filter_is_not_dynamic_with_non_dynamic_selectors(self, gapminder_dynami
],
)
def test_filter_is_not_dynamic_with_options_min_max_specified(
self,
test_column,
test_selector,
gapminder_dynamic_first_n_last_n_function
self, test_column, test_selector, gapminder_dynamic_first_n_last_n_function
):
data_manager["gapminder_dynamic_first_n_last_n"] = gapminder_dynamic_first_n_last_n_function
filter = vm.Filter(column=test_column, selector=test_selector)
Expand Down

0 comments on commit e6208fa

Please sign in to comment.