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

build: ⬆️ Updating to pydantic v2.0.0 #863

Merged
merged 5 commits into from
Jul 1, 2023
Merged
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
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ repos:
exclude: ^spectrafit/test/, ^examples/
entry: flake8 --max-doc-length 100 --max-line-length 88 --ignore E203,W503,W605
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.3.0
rev: v1.4.1
hooks:
- id: mypy
additional_dependencies:
Expand All @@ -49,7 +49,7 @@ repos:
types-tabulate==0.9.0.0,
types-mock==5.0.0.2,
numpy==1.23.4,
pydantic==1.10.1,
pydantic==2.0,
pytest==7.1.3,
]
- repo: https://github.com/pycqa/pydocstyle
Expand All @@ -61,7 +61,7 @@ repos:
rev: "v3.0.0a6"
hooks:
- id: pylint
additional_dependencies: [toml==0.10.2, numpy==1.23.4, pydantic==1.10.1]
additional_dependencies: [toml==0.10.2, numpy==1.23.4, pydantic==2.0]
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v3.0.0-alpha.9-for-vscode"
hooks:
Expand Down
2 changes: 1 addition & 1 deletion Examples/example_7.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"shift": 0,
"smooth": 0,
"autopeak": {
"model_type": "gaussian",
"modeltype": "gaussian",
"height": [0.0, 16],
"threshold": [0.0, 16],
"distance": 2,
Expand Down
2 changes: 1 addition & 1 deletion Examples/example_7_summary.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"header": 0,
"global_": 0,
"autopeak": {
"model_type": "gaussian",
"modeltype": "gaussian",
"height": [0.0, 16],
"threshold": [0.0, 16],
"distance": 2,
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/example7.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ In the following example the in example the section block looks like:
"shift": 0,
"smooth": 0,
"autopeak": {
"model_type": "gaussian",
"modeltype": "gaussian",
"height": [0.0, 16],
"threshold": [0.0, 16],
"distance": 2,
Expand Down
2 changes: 1 addition & 1 deletion docs/interface/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ follows:

```json
"autopeak": {
"model_type": "gaussian",
"modeltype": "gaussian",
"height": [0.0, 10],
"threshold": [0.0, 10],
"distance": 2,
Expand Down
395 changes: 291 additions & 104 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ exclude = ["docs/", "tools/"]
art = ">=5.8,<7.0"
tomli = "^2.0.1"
tomli-w = "^1.0.0"
pydantic = "^1.10.1"
pydantic = ">=1.10,<3.0"
jupyterlab = { version = ">=3.5.2,<5.0.0", optional = true }
plotly = { version = "^5.14.0", optional = true }
itables = { version = "^1.3.4", optional = true }
Expand Down Expand Up @@ -92,6 +92,7 @@ exclude = ["docs/", "tools/"]
dash = { extras = ["testing"], version = "^2.7.1" }
bandit = "^1.7.5"
prospector = {extras = ["bandit", "black", "isort", "pylint"], version = "^1.10.2"}
bump-pydantic = "^0.1.0"

[tool.poetry.group.docs.dependencies]
mkdocstrings = { extras = ["python"], version = ">=0.19,<0.23" }
Expand Down
6 changes: 3 additions & 3 deletions spectrafit/api/file_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pydantic import BaseModel
from pydantic import Field
from pydantic import validator
from pydantic.functional_validators import field_validator


class DataFileAPI(BaseModel):
Expand Down Expand Up @@ -43,15 +43,15 @@ class DataFileAPI(BaseModel):
description="File suffixes to use.",
)

@validator("delimiter")
@field_validator("delimiter")
@classmethod
def check_delimiter(cls, v: str) -> Optional[str]:
"""Check if the delimiter is valid."""
if v in {" ", "\t", ",", ";", "|", r"\s+"}:
return v
raise ValueError(f" {v} is not a valid delimiter.")

@validator("comment")
@field_validator("comment")
@classmethod
def check_comment(cls, v: str) -> Optional[str]:
"""Check if the comment marker is valid."""
Expand Down
4 changes: 2 additions & 2 deletions spectrafit/api/notebook_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from _plotly_utils.colors.qualitative import Plotly as PlotlyColors
from pydantic import BaseModel
from pydantic import Field
from pydantic import validator
from pydantic.functional_validators import field_validator


class XAxisAPI(BaseModel):
Expand Down Expand Up @@ -129,7 +129,7 @@ class ColorAPI(BaseModel):
ticks: str = Field(default="black", description="Color of the ticks.")
font: str = Field(default="black", description="Font color of the plot.")

@validator(
@field_validator(
"paper",
"layout",
"grid",
Expand Down
9 changes: 3 additions & 6 deletions spectrafit/api/report_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pandas import __version__ as pandas_version
from plotly import __version__ as plotly_version
from pydantic import BaseModel
from pydantic import ConfigDict
from pydantic import Field
from pydantic import __version__ as pydantic_version
from scipy import __version__ as scipy_version
Expand All @@ -23,7 +24,7 @@
from statsmodels import __version__ as statsmodels_version


class CreditsAPI(BaseModel, allow_mutation=False):
class CreditsAPI(BaseModel):
"""Credits API model."""

dtale: str = f"dtale v{dtale_version}"
Expand Down Expand Up @@ -122,11 +123,7 @@ class OutputAPI(BaseModel):
default={},
description="DataFrame of the pre-processed data via 'records' orient",
)

class Config:
"""Config for the OutputAPI of arbitary types."""

arbitrary_types_allowed = True
model_config = ConfigDict(arbitrary_types_allowed=True)


class ReportAPI(BaseModel):
Expand Down
49 changes: 21 additions & 28 deletions spectrafit/api/rixs_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""Reference model for the API of the Jupyter Notebook interface."""


from typing import Any
from typing import Optional
from typing import Tuple

import numpy as np

from numpy.typing import NDArray
from pydantic import BaseModel
from pydantic import ConfigDict
from pydantic import Field


Expand Down Expand Up @@ -72,46 +71,40 @@ class SizeRatioAPI(BaseModel):
class RIXSModelAPI(BaseModel):
"""Definition of the RIXS model."""

incident_energy: NDArray[np.float64] = Field(
incident_energy: Any = Field(
..., description="Incident energy values."
)
emission_energy: NDArray[np.float64] = Field(
) # Should be NDArray[np.float64] but that's not supported for 3.8
emission_energy: Any = Field(
..., description="Emission energy values."
)
rixs_map: NDArray[np.float64] = Field(..., description="RIXS map values.")

class Config:
"""Configurations for the RIXSModelAPI class."""

arbitrary_types_allowed = True
) # Should be NDArray[np.float64] but that's not supported for 3.8
rixs_map: Any = Field(
..., description="RIXS map values."
) # Should be NDArray[np.float64] but that's not supported for 3.8
model_config = ConfigDict(arbitrary_types_allowed=True)


class RIXSPlotAPI(BaseModel):
"""Definition of the plotly figure."""

incident_energy: NDArray[np.float64] = Field(
incident_energy: Any = Field(
..., description="Incident energy values."
)
emission_energy: NDArray[np.float64] = Field(
) # Should be NDArray[np.float64] but that's not supported for 3.8
emission_energy: Any = Field(
..., description="Emission energy values."
)
emission_intensity: NDArray[np.float64] = Field(
) # Should be NDArray[np.float64] but that's not supported for 3.8
emission_intensity: Any = Field(
..., description="Emission intensity values (RIXS), which has to be a 2D array."
)
energy_loss: Optional[NDArray[np.float64]] = Field(
) # Should be NDArray[np.float64] but that's not supported for 3.8
energy_loss: Optional[Any] = Field(
default=None, description="Energy loss values."
)
energy_loss_intensity: Optional[NDArray[np.float64]] = Field(
) # Should be NDArray[np.float64] but that's not supported for 3.8
energy_loss_intensity: Optional[Any] = Field(
default=None,
description="Energy loss intensity values (XES), which has to be a 2D array.",
)
) # Should be NDArray[np.float64] but that's not supported for 3.8
x_axis: XAxisAPI = XAxisAPI()
y_axis: YAxisAPI = YAxisAPI()
z_axis: ZAxisAPI = ZAxisAPI()
main_title: MainTitleAPI = MainTitleAPI()
size_ratio: SizeRatioAPI = SizeRatioAPI()

class Config:
"""Configurations for the RIXSPlotAPI class."""

arbitrary_types_allowed = True
model_config = ConfigDict(arbitrary_types_allowed=True)
4 changes: 2 additions & 2 deletions spectrafit/api/test/test_cmd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_overwrite_cmd() -> None:
column=["col_1", "col_2"],
separator="s+",
autopeak={
"model_type": "ga",
"modeltype": "ga",
"height": [1],
"threshold": [0.1],
"distance": 10,
Expand All @@ -63,7 +63,7 @@ def test_overwrite_cmd() -> None:
assert result["column"] == ["col_1", "col_2"]
assert result["separator"] == "s+"
assert result["decimal"] == "."
assert result["autopeak"]["model_type"] == "ga"
assert result["autopeak"]["modeltype"] == "ga"
assert result["autopeak"]["height"] == [1]
assert result["autopeak"]["threshold"] == [0.1]
assert result["autopeak"]["distance"] == 10
Expand Down
15 changes: 5 additions & 10 deletions spectrafit/api/tools_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import Union

from pydantic import BaseModel
from pydantic import Extra
from pydantic import ConfigDict
from pydantic import Field


Expand All @@ -23,7 +23,7 @@ class AutopeakAPI(BaseModel):
)
"""

model_type: Optional[str] = None
modeltype: Optional[str] = None
height: Optional[List[float]] = None
threshold: Optional[List[float]] = None
distance: Optional[int] = None
Expand All @@ -32,12 +32,7 @@ class AutopeakAPI(BaseModel):
wlen: Optional[int] = None
rel_height: Optional[float] = None
plateau_size: Optional[float] = None

class Config:
"""Activate the Validation Error Raise."""

extra = Extra.forbid
validate_assignment = True
model_config = ConfigDict(extra="forbid", validate_assignment=True)


class DataPreProcessingAPI(BaseModel):
Expand Down Expand Up @@ -69,7 +64,7 @@ class DataPreProcessingAPI(BaseModel):
description="Shift the energy axis; default to 0.",
)
column: List[Union[int, str]] = Field(
min_items=1,
min_length=1,
default=[0, 1],
dtypes=[int, str],
description="Column of the data.",
Expand Down Expand Up @@ -109,7 +104,7 @@ class GeneralSolverModelsAPI(BaseModel):
optimizer: Dict[str, Any] = SolverModelsAPI().optimizer


class ColumnNamesAPI(BaseModel, allow_mutation=False):
class ColumnNamesAPI(BaseModel):
"""Definition of the column names of the exported model."""

energy: str = "energy"
Expand Down
4 changes: 2 additions & 2 deletions spectrafit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,9 +1066,9 @@ def define_parameters_auto(self) -> None:
positions, properties = self.__autodetect__()
if (
not isinstance(self.args["autopeak"], bool)
and "model_type" in self.args["autopeak"]
and "modeltype" in self.args["autopeak"]
):
_model = self.args["autopeak"]["model_type"].lower()
_model = self.args["autopeak"]["modeltype"].lower()
ReferenceKeys().automodel_check(model=_model)
models = _model
else:
Expand Down
4 changes: 2 additions & 2 deletions spectrafit/plugins/color_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import BaseModel


class DraculaColor(BaseModel, allow_mutation=False):
class DraculaColor(BaseModel):
"""Dracula color schema for SpectraFit.

!!! info "Dracula Color"
Expand Down Expand Up @@ -43,7 +43,7 @@ class DraculaColor(BaseModel, allow_mutation=False):
font: str = "#f8f8f2"


class DraculaFont(BaseModel, allow_mutation=False):
class DraculaFont(BaseModel):
"""Dracula font schema for SpectraFit.

!!! info "Dracula Font"
Expand Down
2 changes: 1 addition & 1 deletion spectrafit/test/scripts/test_input_11.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"shift": 0,
"smooth": 0,
"autopeak": {
"model_type": "gaussian",
"modeltype": "gaussian",
"height": [0.0, 10],
"threshold": [0.0, 10],
"distance": 2,
Expand Down
8 changes: 4 additions & 4 deletions spectrafit/test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ def test_autopeakdetection_userdef_voigt(self) -> None:
"global_": 0,
"column": ["Energy", "Noisy_Intensity"],
"autopeak": {
"model_type": "voigt",
"modeltype": "voigt",
"height": (0.0, 10),
"threshold": (0.0, 10),
"distance": 2,
Expand All @@ -1093,7 +1093,7 @@ def test_autopeakdetection_userdef_pseudovoigt(self) -> None:
"global_": 0,
"column": ["Energy", "Noisy_Intensity"],
"autopeak": {
"model_type": "pseudovoigt",
"modeltype": "pseudovoigt",
"height": (0.0, 10),
"threshold": (0.0, 10),
"distance": 2,
Expand All @@ -1116,7 +1116,7 @@ def test_autopeakdetection_userdef_lorentzian(self) -> None:
"global_": 0,
"column": ["Energy", "Noisy_Intensity"],
"autopeak": {
"model_type": "lorentzian",
"modeltype": "lorentzian",
"height": (0.0, 10),
"threshold": (0.0, 10),
"distance": 2,
Expand Down Expand Up @@ -1161,7 +1161,7 @@ def test_autopeakdetection_userdef_failmodel(self) -> None:
"global_": 0,
"column": ["Energy", "Noisy_Intensity"],
"autopeak": {
"model_type": "nomodel",
"modeltype": "nomodel",
"height": (0.0, 10),
"threshold": (0.0, 10),
"distance": 2,
Expand Down
Loading