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

Fix/Scatter plot density with few points #478

Merged
merged 21 commits into from
Dec 12, 2024
Merged

Conversation

jsmariegaard
Copy link
Member

Scatter plot can fail when strange error message if you trying to plot only few points. The problem is the density calculation which anyway does not make sense for few points. The solution in this PR is to change the default behavior of show_density if number of points is less than 200 to False.

Note that if user manually sets show_density=True or a very high number of bins are used, the same error could still occur. So it would be even better to catch the problem in the code and issue a better error message that could help the user.

@jsmariegaard
Copy link
Member Author

Closes #381

Copy link
Member

@ecomodeller ecomodeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of related to this PR

Colorbar related to density is only shown is show_points=True
image

Not by default?
image

@ecomodeller
Copy link
Member

Using show_density=True has no effect if show_points=False, I apparently don't understand how this is supposed to work.😐
image

@jsmariegaard
Copy link
Member Author

Using show_density=True has no effect if show_points=False, I apparently don't understand how this is supposed to work.😐 image

density is a feature (color) of the points, it's "point density" (implicitly) - so maybe that combination should raise an error 🤔

@jsmariegaard
Copy link
Member Author

@ecomodeller - why are the type checking suddenly failing... ?

mypy modelskill/ --config-file pyproject.toml
modelskill/metrics.py:229: error: Need type annotation for "residual" [var-annotated]
modelskill/metrics.py:231: error: "Never" has no attribute "mean" [attr-defined]
modelskill/metrics.py:341: error: Need type annotation for "residual" [var-annotated]
modelskill/metrics.py:466: error: Need type annotation for "residual" [var-annotated]
modelskill/metrics.py:467: error: "Never" has no attribute "mean" [attr-defined]
modelskill/metrics.py:664: error: Need type annotation for "residual" [var-annotated]
modelskill/metrics.py:894: error: Need type annotation for "resi" [var-annotated]
modelskill/metrics.py:895: error: Incompatible types in assignment (expression has type "int", variable has type "Never") [assignment]
modelskill/timeseries/_track.py:133: error: Argument "keep" to "drop_duplicates" of "Dataset" has incompatible type "Union[bool, str]"; expected "Literal['first', 'last', False]" [arg-type]
modelskill/timeseries/_point.py:89: error: Argument 1 to "Dataset" has incompatible type "DataArray"; expected "Union[Mapping[str, DataArray], Sequence[DataArray], Sequence[Any]]" [arg-type]
modelskill/model/dfsu.py:58: error: Incompatible types in assignment (expression has type "Union[Dfs0, Dfs1, Dfs2, Dfs3, Dfsu2DH, Dfsu2DV, Dfsu3D, DfsuSpectral, Mesh]", variable has type "Union[str, Path, Dfsu2DH, Dataset, DataArray]") [assignment]
modelskill/model/dfsu.py:77: error: Item "str" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr]
modelskill/model/dfsu.py:77: error: Item "Path" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr]
modelskill/model/dfsu.py:79: error: Item "str" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr]
modelskill/model/dfsu.py:79: error: Item "Path" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr]
modelskill/model/dfsu.py:88: error: Incompatible types in assignment (expression has type "Union[str, Path, Dataset]", variable has type "Union[Dfsu2DH, Dataset]") [assignment]
modelskill/model/dfsu.py:259: error: Argument "method" to "extract_track" of "Dfsu2DH" has incompatible type "str"; expected "Literal['nearest', 'inverse_distance']" [arg-type]
modelskill/model/dfsu.py:263: error: Argument "method" to "extract_track" of "Dataset" has incompatible type "str"; expected "Literal['nearest', 'inverse_distance']" [arg-type]
Found 18 errors in 4 files (checked 40 source files)
make: *** [Makefile:18: typecheck] Error 1
Error: Process completed with exit code 2.

@ecomodeller
Copy link
Member

ecomodeller commented Dec 10, 2024

@ecomodeller - why are the type checking suddenly failing... ?

mypy modelskill/ --config-file pyproject.toml modelskill/metrics.py:229: error: Need type annotation for "residual" [var-annotated] modelskill/metrics.py:231: error: "Never" has no attribute "mean" [attr-defined] modelskill/metrics.py:341: error: Need type annotation for "residual" [var-annotated] modelskill/metrics.py:466: error: Need type annotation for "residual" [var-annotated] modelskill/metrics.py:467: error: "Never" has no attribute "mean" [attr-defined] modelskill/metrics.py:664: error: Need type annotation for "residual" [var-annotated] modelskill/metrics.py:894: error: Need type annotation for "resi" [var-annotated] modelskill/metrics.py:895: error: Incompatible types in assignment (expression has type "int", variable has type "Never") [assignment] modelskill/timeseries/_track.py:133: error: Argument "keep" to "drop_duplicates" of "Dataset" has incompatible type "Union[bool, str]"; expected "Literal['first', 'last', False]" [arg-type] modelskill/timeseries/_point.py:89: error: Argument 1 to "Dataset" has incompatible type "DataArray"; expected "Union[Mapping[str, DataArray], Sequence[DataArray], Sequence[Any]]" [arg-type] modelskill/model/dfsu.py:58: error: Incompatible types in assignment (expression has type "Union[Dfs0, Dfs1, Dfs2, Dfs3, Dfsu2DH, Dfsu2DV, Dfsu3D, DfsuSpectral, Mesh]", variable has type "Union[str, Path, Dfsu2DH, Dataset, DataArray]") [assignment] modelskill/model/dfsu.py:77: error: Item "str" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr] modelskill/model/dfsu.py:77: error: Item "Path" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr] modelskill/model/dfsu.py:79: error: Item "str" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr] modelskill/model/dfsu.py:79: error: Item "Path" of "Union[str, Path, Dataset]" has no attribute "items" [union-attr] modelskill/model/dfsu.py:88: error: Incompatible types in assignment (expression has type "Union[str, Path, Dataset]", variable has type "Union[Dfsu2DH, Dataset]") [assignment] modelskill/model/dfsu.py:259: error: Argument "method" to "extract_track" of "Dfsu2DH" has incompatible type "str"; expected "Literal['nearest', 'inverse_distance']" [arg-type] modelskill/model/dfsu.py:263: error: Argument "method" to "extract_track" of "Dataset" has incompatible type "str"; expected "Literal['nearest', 'inverse_distance']" [arg-type] Found 18 errors in 4 files (checked 40 source files) make: *** [Makefile:18: typecheck] Error 1 Error: Process completed with exit code 2.

The ones related to MIKE IO fails because the last release includes a py.typed file. See PR #470

The ones related to NumPy is likely related to some changes in the last release two days ago.https://pypi.org/project/numpy/2.2.0/

Seems like we should be using numpy.typing.NDArray instead of numpy.ndarray to make mypy happy.
image

@jsmariegaard
Copy link
Member Author

Hmm, but that seems like something that should be fixed in another PR.

I looked at https://numpy.org/doc/stable/reference/typing.html and consider if we should actually use np.typing.ArrayLike?

@jsmariegaard
Copy link
Member Author

@ecomodeller, I have updated the code to consistently show the colorbar when relevant (referring to your comment and screenshots above). Could you please review?

@jsmariegaard jsmariegaard merged commit 1dde462 into main Dec 12, 2024
6 checks passed
@jsmariegaard jsmariegaard deleted the scatter-density-few-points branch December 12, 2024 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants